LCOV - code coverage report
Current view: top level - spdk/lib/blob - blobstore.c (source / functions) Hit Total Coverage
Test: Combined Lines: 4137 5117 80.8 %
Date: 2024-07-12 11:58:45 Functions: 339 360 94.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1939 3136 61.8 %

           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                 :            : #include "spdk/trace.h"
      20                 :            : 
      21                 :            : #include "spdk_internal/assert.h"
      22                 :            : #include "spdk_internal/trace_defs.h"
      23                 :            : #include "spdk/log.h"
      24                 :            : 
      25                 :            : #include "blobstore.h"
      26                 :            : 
      27                 :            : #define BLOB_CRC32C_INITIAL    0xffffffffUL
      28                 :            : 
      29                 :            : static int bs_register_md_thread(struct spdk_blob_store *bs);
      30                 :            : static int bs_unregister_md_thread(struct spdk_blob_store *bs);
      31                 :            : static void blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
      32                 :            : static void blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
      33                 :            :                 uint64_t cluster, uint32_t extent, struct spdk_blob_md_page *page,
      34                 :            :                 spdk_blob_op_complete cb_fn, void *cb_arg);
      35                 :            : static void blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
      36                 :            :                 uint32_t extent_page, struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
      37                 :            : 
      38                 :            : static int blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
      39                 :            :                           uint16_t value_len, bool internal);
      40                 :            : static int blob_get_xattr_value(struct spdk_blob *blob, const char *name,
      41                 :            :                                 const void **value, size_t *value_len, bool internal);
      42                 :            : static int blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal);
      43                 :            : 
      44                 :            : static void blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
      45                 :            :                                    struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
      46                 :            : static void blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg);
      47                 :            : 
      48                 :            : static void bs_shallow_copy_cluster_find_next(void *cb_arg);
      49                 :            : 
      50                 :            : /*
      51                 :            :  * External snapshots require a channel per thread per esnap bdev.  The tree
      52                 :            :  * is populated lazily as blob IOs are handled by the back_bs_dev. When this
      53                 :            :  * channel is destroyed, all the channels in the tree are destroyed.
      54                 :            :  */
      55                 :            : 
      56                 :            : struct blob_esnap_channel {
      57                 :            :         RB_ENTRY(blob_esnap_channel)    node;
      58                 :            :         spdk_blob_id                    blob_id;
      59                 :            :         struct spdk_io_channel          *channel;
      60                 :            : };
      61                 :            : 
      62                 :            : static int blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2);
      63                 :            : static void blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
      64                 :            :                 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg);
      65                 :            : static void blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch);
      66                 :            : static void blob_set_back_bs_dev_frozen(void *_ctx, int bserrno);
      67   [ +  +  +  +  :      62292 : RB_GENERATE_STATIC(blob_esnap_channel_tree, blob_esnap_channel, node, blob_esnap_channel_compare)
          +  +  +  -  +  
          +  +  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
      68                 :            : 
      69                 :            : static inline bool
      70                 :    6782433 : blob_is_esnap_clone(const struct spdk_blob *blob)
      71                 :            : {
      72         [ -  + ]:    6782433 :         assert(blob != NULL);
      73                 :    6782433 :         return !!(blob->invalid_flags & SPDK_BLOB_EXTERNAL_SNAPSHOT);
      74                 :            : }
      75                 :            : 
      76                 :            : static int
      77                 :      34504 : blob_id_cmp(struct spdk_blob *blob1, struct spdk_blob *blob2)
      78                 :            : {
      79   [ +  -  +  + ]:      34504 :         assert(blob1 != NULL && blob2 != NULL);
      80         [ +  + ]:      34504 :         return (blob1->id < blob2->id ? -1 : blob1->id > blob2->id);
      81                 :            : }
      82                 :            : 
      83   [ +  +  +  +  :     127160 : RB_GENERATE_STATIC(spdk_blob_tree, spdk_blob, link, blob_id_cmp);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
      84                 :            : 
      85                 :            : static void
      86                 :     935914 : blob_verify_md_op(struct spdk_blob *blob)
      87                 :            : {
      88         [ -  + ]:     935914 :         assert(blob != NULL);
      89         [ -  + ]:     935914 :         assert(spdk_get_thread() == blob->bs->md_thread);
      90         [ -  + ]:     935914 :         assert(blob->state != SPDK_BLOB_STATE_LOADING);
      91                 :     935914 : }
      92                 :            : 
      93                 :            : static struct spdk_blob_list *
      94                 :      19947 : bs_get_snapshot_entry(struct spdk_blob_store *bs, spdk_blob_id blobid)
      95                 :            : {
      96                 :      19947 :         struct spdk_blob_list *snapshot_entry = NULL;
      97                 :            : 
      98         [ +  + ]:      24234 :         TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
      99         [ +  + ]:       7716 :                 if (snapshot_entry->id == blobid) {
     100                 :       3429 :                         break;
     101                 :            :                 }
     102                 :            :         }
     103                 :            : 
     104                 :      19947 :         return snapshot_entry;
     105                 :            : }
     106                 :            : 
     107                 :            : static void
     108                 :      14756 : bs_claim_md_page(struct spdk_blob_store *bs, uint32_t page)
     109                 :            : {
     110         [ -  + ]:      14756 :         assert(spdk_spin_held(&bs->used_lock));
     111         [ -  + ]:      14756 :         assert(page < spdk_bit_array_capacity(bs->used_md_pages));
     112         [ -  + ]:      14756 :         assert(spdk_bit_array_get(bs->used_md_pages, page) == false);
     113                 :            : 
     114                 :      14756 :         spdk_bit_array_set(bs->used_md_pages, page);
     115                 :      14756 : }
     116                 :            : 
     117                 :            : static void
     118                 :      11084 : bs_release_md_page(struct spdk_blob_store *bs, uint32_t page)
     119                 :            : {
     120         [ -  + ]:      11084 :         assert(spdk_spin_held(&bs->used_lock));
     121         [ -  + ]:      11084 :         assert(page < spdk_bit_array_capacity(bs->used_md_pages));
     122         [ -  + ]:      11084 :         assert(spdk_bit_array_get(bs->used_md_pages, page) == true);
     123                 :            : 
     124                 :      11084 :         spdk_bit_array_clear(bs->used_md_pages, page);
     125                 :      11084 : }
     126                 :            : 
     127                 :            : static uint32_t
     128                 :     589440 : bs_claim_cluster(struct spdk_blob_store *bs)
     129                 :            : {
     130                 :            :         uint32_t cluster_num;
     131                 :            : 
     132         [ -  + ]:     589440 :         assert(spdk_spin_held(&bs->used_lock));
     133                 :            : 
     134                 :     589440 :         cluster_num = spdk_bit_pool_allocate_bit(bs->used_clusters);
     135         [ +  + ]:     589440 :         if (cluster_num == UINT32_MAX) {
     136                 :          1 :                 return UINT32_MAX;
     137                 :            :         }
     138                 :            : 
     139   [ -  +  -  + ]:     589439 :         SPDK_DEBUGLOG(blob, "Claiming cluster %u\n", cluster_num);
     140                 :     589439 :         bs->num_free_clusters--;
     141                 :            : 
     142                 :     589439 :         return cluster_num;
     143                 :            : }
     144                 :            : 
     145                 :            : static void
     146                 :     532680 : bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
     147                 :            : {
     148         [ -  + ]:     532680 :         assert(spdk_spin_held(&bs->used_lock));
     149         [ -  + ]:     532680 :         assert(cluster_num < spdk_bit_pool_capacity(bs->used_clusters));
     150         [ -  + ]:     532680 :         assert(spdk_bit_pool_is_allocated(bs->used_clusters, cluster_num) == true);
     151         [ -  + ]:     532680 :         assert(bs->num_free_clusters < bs->total_clusters);
     152                 :            : 
     153   [ -  +  -  + ]:     532680 :         SPDK_DEBUGLOG(blob, "Releasing cluster %u\n", cluster_num);
     154                 :            : 
     155                 :     532680 :         spdk_bit_pool_free_bit(bs->used_clusters, cluster_num);
     156                 :     532680 :         bs->num_free_clusters++;
     157                 :     532680 : }
     158                 :            : 
     159                 :            : static int
     160                 :     589439 : blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster)
     161                 :            : {
     162                 :     589439 :         uint64_t *cluster_lba = &blob->active.clusters[cluster_num];
     163                 :            : 
     164                 :     589439 :         blob_verify_md_op(blob);
     165                 :            : 
     166         [ +  + ]:     589439 :         if (*cluster_lba != 0) {
     167                 :         22 :                 return -EEXIST;
     168                 :            :         }
     169                 :            : 
     170                 :     589417 :         *cluster_lba = bs_cluster_to_lba(blob->bs, cluster);
     171                 :     589417 :         blob->active.num_allocated_clusters++;
     172                 :            : 
     173                 :     589417 :         return 0;
     174                 :            : }
     175                 :            : 
     176                 :            : static int
     177                 :     589440 : bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num,
     178                 :            :                     uint64_t *cluster, uint32_t *lowest_free_md_page, bool update_map)
     179                 :            : {
     180                 :     589440 :         uint32_t *extent_page = 0;
     181                 :            : 
     182         [ -  + ]:     589440 :         assert(spdk_spin_held(&blob->bs->used_lock));
     183                 :            : 
     184                 :     589440 :         *cluster = bs_claim_cluster(blob->bs);
     185         [ +  + ]:     589440 :         if (*cluster == UINT32_MAX) {
     186                 :            :                 /* No more free clusters. Cannot satisfy the request */
     187                 :          1 :                 return -ENOSPC;
     188                 :            :         }
     189                 :            : 
     190   [ +  +  +  + ]:     589439 :         if (blob->use_extent_table) {
     191                 :     573231 :                 extent_page = bs_cluster_to_extent_page(blob, cluster_num);
     192         [ +  + ]:     573231 :                 if (*extent_page == 0) {
     193                 :            :                         /* Extent page shall never occupy md_page so start the search from 1 */
     194         [ +  + ]:       4839 :                         if (*lowest_free_md_page == 0) {
     195                 :       3929 :                                 *lowest_free_md_page = 1;
     196                 :            :                         }
     197                 :            :                         /* No extent_page is allocated for the cluster */
     198                 :       4839 :                         *lowest_free_md_page = spdk_bit_array_find_first_clear(blob->bs->used_md_pages,
     199                 :            :                                                *lowest_free_md_page);
     200         [ -  + ]:       4839 :                         if (*lowest_free_md_page == UINT32_MAX) {
     201                 :            :                                 /* No more free md pages. Cannot satisfy the request */
     202                 :          0 :                                 bs_release_cluster(blob->bs, *cluster);
     203                 :          0 :                                 return -ENOSPC;
     204                 :            :                         }
     205                 :       4839 :                         bs_claim_md_page(blob->bs, *lowest_free_md_page);
     206                 :            :                 }
     207                 :            :         }
     208                 :            : 
     209   [ -  +  -  + ]:     589439 :         SPDK_DEBUGLOG(blob, "Claiming cluster %" PRIu64 " for blob 0x%" PRIx64 "\n", *cluster,
     210                 :            :                       blob->id);
     211                 :            : 
     212         [ +  + ]:     589439 :         if (update_map) {
     213                 :     584397 :                 blob_insert_cluster(blob, cluster_num, *cluster);
     214   [ +  +  +  +  :     584397 :                 if (blob->use_extent_table && *extent_page == 0) {
                   +  + ]
     215                 :       4436 :                         *extent_page = *lowest_free_md_page;
     216                 :            :                 }
     217                 :            :         }
     218                 :            : 
     219                 :     589439 :         return 0;
     220                 :            : }
     221                 :            : 
     222                 :            : static void
     223                 :      25028 : blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs)
     224                 :            : {
     225                 :      25028 :         xattrs->count = 0;
     226                 :      25028 :         xattrs->names = NULL;
     227                 :      25028 :         xattrs->ctx = NULL;
     228                 :      25028 :         xattrs->get_value = NULL;
     229                 :      25028 : }
     230                 :            : 
     231                 :            : void
     232                 :      16250 : spdk_blob_opts_init(struct spdk_blob_opts *opts, size_t opts_size)
     233                 :            : {
     234         [ -  + ]:      16250 :         if (!opts) {
     235                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     236                 :          0 :                 return;
     237                 :            :         }
     238                 :            : 
     239         [ -  + ]:      16250 :         if (!opts_size) {
     240                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
     241                 :          0 :                 return;
     242                 :            :         }
     243                 :            : 
     244         [ -  + ]:      16250 :         memset(opts, 0, opts_size);
     245                 :      16250 :         opts->opts_size = opts_size;
     246                 :            : 
     247                 :            : #define FIELD_OK(field) \
     248                 :            :         offsetof(struct spdk_blob_opts, field) + sizeof(opts->field) <= opts_size
     249                 :            : 
     250                 :            : #define SET_FIELD(field, value) \
     251                 :            :         if (FIELD_OK(field)) { \
     252                 :            :                 opts->field = value; \
     253                 :            :         } \
     254                 :            : 
     255         [ +  - ]:      16250 :         SET_FIELD(num_clusters, 0);
     256         [ +  - ]:      16250 :         SET_FIELD(thin_provision, false);
     257         [ +  - ]:      16250 :         SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
     258                 :            : 
     259         [ +  - ]:      16250 :         if (FIELD_OK(xattrs)) {
     260                 :      16250 :                 blob_xattrs_init(&opts->xattrs);
     261                 :            :         }
     262                 :            : 
     263         [ +  - ]:      16250 :         SET_FIELD(use_extent_table, true);
     264                 :            : 
     265                 :            : #undef FIELD_OK
     266                 :            : #undef SET_FIELD
     267                 :            : }
     268                 :            : 
     269                 :            : void
     270                 :      20801 : spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts, size_t opts_size)
     271                 :            : {
     272         [ -  + ]:      20801 :         if (!opts) {
     273                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     274                 :          0 :                 return;
     275                 :            :         }
     276                 :            : 
     277         [ -  + ]:      20801 :         if (!opts_size) {
     278                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
     279                 :          0 :                 return;
     280                 :            :         }
     281                 :            : 
     282         [ -  + ]:      20801 :         memset(opts, 0, opts_size);
     283                 :      20801 :         opts->opts_size = opts_size;
     284                 :            : 
     285                 :            : #define FIELD_OK(field) \
     286                 :            :         offsetof(struct spdk_blob_open_opts, field) + sizeof(opts->field) <= opts_size
     287                 :            : 
     288                 :            : #define SET_FIELD(field, value) \
     289                 :            :         if (FIELD_OK(field)) { \
     290                 :            :                 opts->field = value; \
     291                 :            :         } \
     292                 :            : 
     293         [ +  - ]:      20801 :         SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
     294                 :            : 
     295                 :            : #undef FIELD_OK
     296                 :            : #undef SET_FILED
     297                 :            : }
     298                 :            : 
     299                 :            : static struct spdk_blob *
     300                 :      29161 : blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
     301                 :            : {
     302                 :            :         struct spdk_blob *blob;
     303                 :            : 
     304                 :      29161 :         blob = calloc(1, sizeof(*blob));
     305         [ -  + ]:      29161 :         if (!blob) {
     306                 :          0 :                 return NULL;
     307                 :            :         }
     308                 :            : 
     309                 :      29161 :         blob->id = id;
     310                 :      29161 :         blob->bs = bs;
     311                 :            : 
     312                 :      29161 :         blob->parent_id = SPDK_BLOBID_INVALID;
     313                 :            : 
     314                 :      29161 :         blob->state = SPDK_BLOB_STATE_DIRTY;
     315                 :      29161 :         blob->extent_rle_found = false;
     316                 :      29161 :         blob->extent_table_found = false;
     317                 :      29161 :         blob->active.num_pages = 1;
     318                 :      29161 :         blob->active.pages = calloc(1, sizeof(*blob->active.pages));
     319         [ -  + ]:      29161 :         if (!blob->active.pages) {
     320                 :          0 :                 free(blob);
     321                 :          0 :                 return NULL;
     322                 :            :         }
     323                 :            : 
     324                 :      29161 :         blob->active.pages[0] = bs_blobid_to_page(id);
     325                 :            : 
     326                 :      29161 :         TAILQ_INIT(&blob->xattrs);
     327                 :      29161 :         TAILQ_INIT(&blob->xattrs_internal);
     328                 :      29161 :         TAILQ_INIT(&blob->pending_persists);
     329                 :      29161 :         TAILQ_INIT(&blob->persists_to_complete);
     330                 :            : 
     331                 :      29161 :         return blob;
     332                 :            : }
     333                 :            : 
     334                 :            : static void
     335                 :      58322 : xattrs_free(struct spdk_xattr_tailq *xattrs)
     336                 :            : {
     337                 :            :         struct spdk_xattr       *xattr, *xattr_tmp;
     338                 :            : 
     339         [ +  + ]:      79293 :         TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) {
     340         [ +  + ]:      20971 :                 TAILQ_REMOVE(xattrs, xattr, link);
     341                 :      20971 :                 free(xattr->name);
     342                 :      20971 :                 free(xattr->value);
     343                 :      20971 :                 free(xattr);
     344                 :            :         }
     345                 :      58322 : }
     346                 :            : 
     347                 :            : static void
     348                 :      29161 : blob_free(struct spdk_blob *blob)
     349                 :            : {
     350         [ -  + ]:      29161 :         assert(blob != NULL);
     351         [ -  + ]:      29161 :         assert(TAILQ_EMPTY(&blob->pending_persists));
     352         [ -  + ]:      29161 :         assert(TAILQ_EMPTY(&blob->persists_to_complete));
     353                 :            : 
     354                 :      29161 :         free(blob->active.extent_pages);
     355                 :      29161 :         free(blob->clean.extent_pages);
     356                 :      29161 :         free(blob->active.clusters);
     357                 :      29161 :         free(blob->clean.clusters);
     358                 :      29161 :         free(blob->active.pages);
     359                 :      29161 :         free(blob->clean.pages);
     360                 :            : 
     361                 :      29161 :         xattrs_free(&blob->xattrs);
     362                 :      29161 :         xattrs_free(&blob->xattrs_internal);
     363                 :            : 
     364         [ +  + ]:      29161 :         if (blob->back_bs_dev) {
     365                 :       4710 :                 blob->back_bs_dev->destroy(blob->back_bs_dev);
     366                 :            :         }
     367                 :            : 
     368                 :      29161 :         free(blob);
     369                 :      29161 : }
     370                 :            : 
     371                 :            : static void
     372                 :       1332 : blob_back_bs_destroy_esnap_done(void *ctx, struct spdk_blob *blob, int bserrno)
     373                 :            : {
     374                 :       1332 :         struct spdk_bs_dev      *bs_dev = ctx;
     375                 :            : 
     376         [ -  + ]:       1332 :         if (bserrno != 0) {
     377                 :            :                 /*
     378                 :            :                  * This is probably due to a memory allocation failure when creating the
     379                 :            :                  * blob_esnap_destroy_ctx before iterating threads.
     380                 :            :                  */
     381                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": Unable to destroy bs dev channels: error %d\n",
     382                 :            :                             blob->id, bserrno);
     383                 :          0 :                 assert(false);
     384                 :            :         }
     385                 :            : 
     386         [ -  + ]:       1332 :         if (bs_dev == NULL) {
     387                 :            :                 /*
     388                 :            :                  * This check exists to make scanbuild happy.
     389                 :            :                  *
     390                 :            :                  * blob->back_bs_dev for an esnap is NULL during the first iteration of blobs while
     391                 :            :                  * the blobstore is being loaded. It could also be NULL if there was an error
     392                 :            :                  * opening the esnap device. In each of these cases, no channels could have been
     393                 :            :                  * created because back_bs_dev->create_channel() would have led to a NULL pointer
     394                 :            :                  * deref.
     395                 :            :                  */
     396                 :          0 :                 assert(false);
     397                 :            :                 return;
     398                 :            :         }
     399                 :            : 
     400   [ -  +  -  + ]:       1332 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": calling destroy on back_bs_dev\n", blob->id);
     401                 :       1332 :         bs_dev->destroy(bs_dev);
     402                 :            : }
     403                 :            : 
     404                 :            : static void
     405                 :       1332 : blob_back_bs_destroy(struct spdk_blob *blob)
     406                 :            : {
     407   [ -  +  -  + ]:       1332 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": preparing to destroy back_bs_dev\n",
     408                 :            :                       blob->id);
     409                 :            : 
     410                 :       1332 :         blob_esnap_destroy_bs_dev_channels(blob, false, blob_back_bs_destroy_esnap_done,
     411                 :       1332 :                                            blob->back_bs_dev);
     412                 :       1332 :         blob->back_bs_dev = NULL;
     413                 :       1332 : }
     414                 :            : 
     415                 :            : struct blob_parent {
     416                 :            :         union {
     417                 :            :                 struct {
     418                 :            :                         spdk_blob_id id;
     419                 :            :                         struct spdk_blob *blob;
     420                 :            :                 } snapshot;
     421                 :            : 
     422                 :            :                 struct {
     423                 :            :                         void *id;
     424                 :            :                         uint32_t id_len;
     425                 :            :                         struct spdk_bs_dev *back_bs_dev;
     426                 :            :                 } esnap;
     427                 :            :         } u;
     428                 :            : };
     429                 :            : 
     430                 :            : typedef int (*set_parent_refs_cb)(struct spdk_blob *blob, struct blob_parent *parent);
     431                 :            : 
     432                 :            : struct set_bs_dev_ctx {
     433                 :            :         struct spdk_blob        *blob;
     434                 :            :         struct spdk_bs_dev      *back_bs_dev;
     435                 :            : 
     436                 :            :         /*
     437                 :            :          * This callback is used during a set parent operation to change the references
     438                 :            :          * to the parent of the blob.
     439                 :            :          */
     440                 :            :         set_parent_refs_cb      parent_refs_cb_fn;
     441                 :            :         struct blob_parent      *parent_refs_cb_arg;
     442                 :            : 
     443                 :            :         spdk_blob_op_complete   cb_fn;
     444                 :            :         void                    *cb_arg;
     445                 :            :         int                     bserrno;
     446                 :            : };
     447                 :            : 
     448                 :            : static void
     449                 :        121 : blob_set_back_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
     450                 :            :                      set_parent_refs_cb parent_refs_cb_fn, struct blob_parent *parent_refs_cb_arg,
     451                 :            :                      spdk_blob_op_complete cb_fn, void *cb_arg)
     452                 :            : {
     453                 :            :         struct set_bs_dev_ctx   *ctx;
     454                 :            : 
     455                 :        121 :         ctx = calloc(1, sizeof(*ctx));
     456         [ -  + ]:        121 :         if (ctx == NULL) {
     457                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": out of memory while setting back_bs_dev\n",
     458                 :            :                             blob->id);
     459                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     460                 :          0 :                 return;
     461                 :            :         }
     462                 :            : 
     463                 :        121 :         ctx->parent_refs_cb_fn = parent_refs_cb_fn;
     464                 :        121 :         ctx->parent_refs_cb_arg = parent_refs_cb_arg;
     465                 :        121 :         ctx->cb_fn = cb_fn;
     466                 :        121 :         ctx->cb_arg = cb_arg;
     467                 :        121 :         ctx->back_bs_dev = back_bs_dev;
     468                 :        121 :         ctx->blob = blob;
     469                 :            : 
     470                 :        121 :         blob_freeze_io(blob, blob_set_back_bs_dev_frozen, ctx);
     471                 :            : }
     472                 :            : 
     473                 :            : struct freeze_io_ctx {
     474                 :            :         struct spdk_bs_cpl cpl;
     475                 :            :         struct spdk_blob *blob;
     476                 :            : };
     477                 :            : 
     478                 :            : static void
     479                 :      29954 : blob_io_sync(struct spdk_io_channel_iter *i)
     480                 :            : {
     481                 :      29954 :         spdk_for_each_channel_continue(i, 0);
     482                 :      29954 : }
     483                 :            : 
     484                 :            : static void
     485                 :      29906 : blob_execute_queued_io(struct spdk_io_channel_iter *i)
     486                 :            : {
     487                 :      29906 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
     488                 :      29906 :         struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch);
     489                 :      29906 :         struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     490                 :            :         struct spdk_bs_request_set      *set;
     491                 :            :         struct spdk_bs_user_op_args     *args;
     492                 :            :         spdk_bs_user_op_t *op, *tmp;
     493                 :            : 
     494         [ +  + ]:      31492 :         TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) {
     495                 :       1586 :                 set = (struct spdk_bs_request_set *)op;
     496                 :       1586 :                 args = &set->u.user_op;
     497                 :            : 
     498         [ +  - ]:       1586 :                 if (args->blob == ctx->blob) {
     499         [ +  + ]:       1586 :                         TAILQ_REMOVE(&ch->queued_io, op, link);
     500                 :       1586 :                         bs_user_op_execute(op);
     501                 :            :                 }
     502                 :            :         }
     503                 :            : 
     504                 :      29906 :         spdk_for_each_channel_continue(i, 0);
     505                 :      29906 : }
     506                 :            : 
     507                 :            : static void
     508                 :      59684 : blob_io_cpl(struct spdk_io_channel_iter *i, int status)
     509                 :            : {
     510                 :      59684 :         struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     511                 :            : 
     512                 :      59684 :         ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0);
     513                 :            : 
     514                 :      59684 :         free(ctx);
     515                 :      59684 : }
     516                 :            : 
     517                 :            : static void
     518                 :      29866 : blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
     519                 :            : {
     520                 :            :         struct freeze_io_ctx *ctx;
     521                 :            : 
     522                 :      29866 :         blob_verify_md_op(blob);
     523                 :            : 
     524                 :      29866 :         ctx = calloc(1, sizeof(*ctx));
     525         [ -  + ]:      29866 :         if (!ctx) {
     526                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     527                 :          0 :                 return;
     528                 :            :         }
     529                 :            : 
     530                 :      29866 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
     531                 :      29866 :         ctx->cpl.u.blob_basic.cb_fn = cb_fn;
     532                 :      29866 :         ctx->cpl.u.blob_basic.cb_arg = cb_arg;
     533                 :      29866 :         ctx->blob = blob;
     534                 :            : 
     535                 :            :         /* Freeze I/O on blob */
     536                 :      29866 :         blob->frozen_refcnt++;
     537                 :            : 
     538                 :      29866 :         spdk_for_each_channel(blob->bs, blob_io_sync, ctx, blob_io_cpl);
     539                 :            : }
     540                 :            : 
     541                 :            : static void
     542                 :      29818 : blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
     543                 :            : {
     544                 :            :         struct freeze_io_ctx *ctx;
     545                 :            : 
     546                 :      29818 :         blob_verify_md_op(blob);
     547                 :            : 
     548                 :      29818 :         ctx = calloc(1, sizeof(*ctx));
     549         [ -  + ]:      29818 :         if (!ctx) {
     550                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     551                 :          0 :                 return;
     552                 :            :         }
     553                 :            : 
     554                 :      29818 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
     555                 :      29818 :         ctx->cpl.u.blob_basic.cb_fn = cb_fn;
     556                 :      29818 :         ctx->cpl.u.blob_basic.cb_arg = cb_arg;
     557                 :      29818 :         ctx->blob = blob;
     558                 :            : 
     559         [ -  + ]:      29818 :         assert(blob->frozen_refcnt > 0);
     560                 :            : 
     561                 :      29818 :         blob->frozen_refcnt--;
     562                 :            : 
     563                 :      29818 :         spdk_for_each_channel(blob->bs, blob_execute_queued_io, ctx, blob_io_cpl);
     564                 :            : }
     565                 :            : 
     566                 :            : static int
     567                 :      81337 : blob_mark_clean(struct spdk_blob *blob)
     568                 :            : {
     569                 :      81337 :         uint32_t *extent_pages = NULL;
     570                 :      81337 :         uint64_t *clusters = NULL;
     571                 :      81337 :         uint32_t *pages = NULL;
     572                 :            : 
     573         [ -  + ]:      81337 :         assert(blob != NULL);
     574                 :            : 
     575         [ +  + ]:      81337 :         if (blob->active.num_extent_pages) {
     576         [ -  + ]:      55926 :                 assert(blob->active.extent_pages);
     577                 :      55926 :                 extent_pages = calloc(blob->active.num_extent_pages, sizeof(*blob->active.extent_pages));
     578         [ -  + ]:      55926 :                 if (!extent_pages) {
     579                 :          0 :                         return -ENOMEM;
     580                 :            :                 }
     581   [ -  +  -  + ]:      55926 :                 memcpy(extent_pages, blob->active.extent_pages,
     582                 :      55926 :                        blob->active.num_extent_pages * sizeof(*extent_pages));
     583                 :            :         }
     584                 :            : 
     585         [ +  + ]:      81337 :         if (blob->active.num_clusters) {
     586         [ -  + ]:      68218 :                 assert(blob->active.clusters);
     587                 :      68218 :                 clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
     588         [ -  + ]:      68218 :                 if (!clusters) {
     589                 :          0 :                         free(extent_pages);
     590                 :          0 :                         return -ENOMEM;
     591                 :            :                 }
     592   [ -  +  -  + ]:      68218 :                 memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
     593                 :            :         }
     594                 :            : 
     595         [ +  + ]:      81337 :         if (blob->active.num_pages) {
     596         [ -  + ]:      74637 :                 assert(blob->active.pages);
     597                 :      74637 :                 pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
     598         [ -  + ]:      74637 :                 if (!pages) {
     599                 :          0 :                         free(extent_pages);
     600                 :          0 :                         free(clusters);
     601                 :          0 :                         return -ENOMEM;
     602                 :            :                 }
     603   [ -  +  -  + ]:      74637 :                 memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
     604                 :            :         }
     605                 :            : 
     606                 :      81337 :         free(blob->clean.extent_pages);
     607                 :      81337 :         free(blob->clean.clusters);
     608                 :      81337 :         free(blob->clean.pages);
     609                 :            : 
     610                 :      81337 :         blob->clean.num_extent_pages = blob->active.num_extent_pages;
     611                 :      81337 :         blob->clean.extent_pages = blob->active.extent_pages;
     612                 :      81337 :         blob->clean.num_clusters = blob->active.num_clusters;
     613                 :      81337 :         blob->clean.clusters = blob->active.clusters;
     614                 :      81337 :         blob->clean.num_allocated_clusters = blob->active.num_allocated_clusters;
     615                 :      81337 :         blob->clean.num_pages = blob->active.num_pages;
     616                 :      81337 :         blob->clean.pages = blob->active.pages;
     617                 :            : 
     618                 :      81337 :         blob->active.extent_pages = extent_pages;
     619                 :      81337 :         blob->active.clusters = clusters;
     620                 :      81337 :         blob->active.pages = pages;
     621                 :            : 
     622                 :            :         /* If the metadata was dirtied again while the metadata was being written to disk,
     623                 :            :          *  we do not want to revert the DIRTY state back to CLEAN here.
     624                 :            :          */
     625         [ +  + ]:      81337 :         if (blob->state == SPDK_BLOB_STATE_LOADING) {
     626                 :      20127 :                 blob->state = SPDK_BLOB_STATE_CLEAN;
     627                 :            :         }
     628                 :            : 
     629                 :      81337 :         return 0;
     630                 :            : }
     631                 :            : 
     632                 :            : static int
     633                 :      16555 : blob_deserialize_xattr(struct spdk_blob *blob,
     634                 :            :                        struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal)
     635                 :            : {
     636                 :            :         struct spdk_xattr                       *xattr;
     637                 :            : 
     638                 :      16555 :         if (desc_xattr->length != sizeof(desc_xattr->name_length) +
     639                 :            :             sizeof(desc_xattr->value_length) +
     640         [ -  + ]:      16555 :             desc_xattr->name_length + desc_xattr->value_length) {
     641                 :          0 :                 return -EINVAL;
     642                 :            :         }
     643                 :            : 
     644                 :      16555 :         xattr = calloc(1, sizeof(*xattr));
     645         [ -  + ]:      16555 :         if (xattr == NULL) {
     646                 :          0 :                 return -ENOMEM;
     647                 :            :         }
     648                 :            : 
     649                 :      16555 :         xattr->name = malloc(desc_xattr->name_length + 1);
     650         [ -  + ]:      16555 :         if (xattr->name == NULL) {
     651                 :          0 :                 free(xattr);
     652                 :          0 :                 return -ENOMEM;
     653                 :            :         }
     654                 :            : 
     655                 :      16555 :         xattr->value = malloc(desc_xattr->value_length);
     656         [ -  + ]:      16555 :         if (xattr->value == NULL) {
     657                 :          0 :                 free(xattr->name);
     658                 :          0 :                 free(xattr);
     659                 :          0 :                 return -ENOMEM;
     660                 :            :         }
     661                 :            : 
     662   [ -  +  -  + ]:      16555 :         memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
     663                 :      16555 :         xattr->name[desc_xattr->name_length] = '\0';
     664                 :      16555 :         xattr->value_len = desc_xattr->value_length;
     665   [ -  +  -  + ]:      16555 :         memcpy(xattr->value,
     666                 :      16555 :                (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
     667                 :      16555 :                desc_xattr->value_length);
     668                 :            : 
     669   [ +  +  +  +  :      16555 :         TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link);
                   +  + ]
     670                 :            : 
     671                 :      16555 :         return 0;
     672                 :            : }
     673                 :            : 
     674                 :            : 
     675                 :            : static int
     676                 :      31723 : blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
     677                 :            : {
     678                 :            :         struct spdk_blob_md_descriptor *desc;
     679                 :      31723 :         size_t  cur_desc = 0;
     680                 :            :         void *tmp;
     681                 :            : 
     682                 :      31723 :         desc = (struct spdk_blob_md_descriptor *)page->descriptors;
     683         [ +  - ]:      98552 :         while (cur_desc < sizeof(page->descriptors)) {
     684         [ +  + ]:      98552 :                 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
     685         [ +  - ]:      31531 :                         if (desc->length == 0) {
     686                 :            :                                 /* If padding and length are 0, this terminates the page */
     687                 :      31531 :                                 break;
     688                 :            :                         }
     689         [ +  + ]:      67021 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
     690                 :            :                         struct spdk_blob_md_descriptor_flags    *desc_flags;
     691                 :            : 
     692                 :      20255 :                         desc_flags = (struct spdk_blob_md_descriptor_flags *)desc;
     693                 :            : 
     694         [ -  + ]:      20255 :                         if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) {
     695                 :          0 :                                 return -EINVAL;
     696                 :            :                         }
     697                 :            : 
     698         [ +  + ]:      20255 :                         if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) !=
     699                 :            :                             SPDK_BLOB_INVALID_FLAGS_MASK) {
     700                 :         32 :                                 return -EINVAL;
     701                 :            :                         }
     702                 :            : 
     703         [ +  + ]:      20223 :                         if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) !=
     704                 :            :                             SPDK_BLOB_DATA_RO_FLAGS_MASK) {
     705                 :         48 :                                 blob->data_ro = true;
     706                 :         48 :                                 blob->md_ro = true;
     707                 :            :                         }
     708                 :            : 
     709         [ +  + ]:      20223 :                         if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) !=
     710                 :            :                             SPDK_BLOB_MD_RO_FLAGS_MASK) {
     711                 :         48 :                                 blob->md_ro = true;
     712                 :            :                         }
     713                 :            : 
     714         [ +  + ]:      20223 :                         if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
     715                 :       2373 :                                 blob->data_ro = true;
     716                 :       2373 :                                 blob->md_ro = true;
     717                 :            :                         }
     718                 :            : 
     719                 :      20223 :                         blob->invalid_flags = desc_flags->invalid_flags;
     720                 :      20223 :                         blob->data_ro_flags = desc_flags->data_ro_flags;
     721                 :      20223 :                         blob->md_ro_flags = desc_flags->md_ro_flags;
     722                 :            : 
     723         [ +  + ]:      46766 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
     724                 :            :                         struct spdk_blob_md_descriptor_extent_rle       *desc_extent_rle;
     725                 :            :                         unsigned int                            i, j;
     726                 :       5568 :                         unsigned int                            cluster_count = blob->active.num_clusters;
     727                 :            : 
     728   [ -  +  -  + ]:       5568 :                         if (blob->extent_table_found) {
     729                 :            :                                 /* Extent Table already present in the md,
     730                 :            :                                  * both descriptors should never be at the same time. */
     731                 :          0 :                                 return -EINVAL;
     732                 :            :                         }
     733                 :       5568 :                         blob->extent_rle_found = true;
     734                 :            : 
     735                 :       5568 :                         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
     736                 :            : 
     737         [ +  - ]:       5568 :                         if (desc_extent_rle->length == 0 ||
     738         [ -  + ]:       5568 :                             (desc_extent_rle->length % sizeof(desc_extent_rle->extents[0]) != 0)) {
     739                 :          0 :                                 return -EINVAL;
     740                 :            :                         }
     741                 :            : 
     742         [ +  + ]:      11848 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
     743         [ +  + ]:      84952 :                                 for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
     744         [ +  + ]:      78672 :                                         if (desc_extent_rle->extents[i].cluster_idx != 0) {
     745         [ -  + ]:      26768 :                                                 if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters,
     746                 :      26768 :                                                                                 desc_extent_rle->extents[i].cluster_idx + j)) {
     747                 :          0 :                                                         return -EINVAL;
     748                 :            :                                                 }
     749                 :            :                                         }
     750                 :      78672 :                                         cluster_count++;
     751                 :            :                                 }
     752                 :            :                         }
     753                 :            : 
     754         [ -  + ]:       5568 :                         if (cluster_count == 0) {
     755                 :          0 :                                 return -EINVAL;
     756                 :            :                         }
     757                 :       5568 :                         tmp = realloc(blob->active.clusters, cluster_count * sizeof(*blob->active.clusters));
     758         [ -  + ]:       5568 :                         if (tmp == NULL) {
     759                 :          0 :                                 return -ENOMEM;
     760                 :            :                         }
     761                 :       5568 :                         blob->active.clusters = tmp;
     762                 :       5568 :                         blob->active.cluster_array_size = cluster_count;
     763                 :            : 
     764         [ +  + ]:      11848 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
     765         [ +  + ]:      84952 :                                 for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
     766         [ +  + ]:      78672 :                                         if (desc_extent_rle->extents[i].cluster_idx != 0) {
     767                 :      40152 :                                                 blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
     768                 :      26768 :                                                                 desc_extent_rle->extents[i].cluster_idx + j);
     769                 :      26768 :                                                 blob->active.num_allocated_clusters++;
     770         [ +  - ]:      51904 :                                         } else if (spdk_blob_is_thin_provisioned(blob)) {
     771                 :      51904 :                                                 blob->active.clusters[blob->active.num_clusters++] = 0;
     772                 :            :                                         } else {
     773                 :          0 :                                                 return -EINVAL;
     774                 :            :                                         }
     775                 :            :                                 }
     776                 :            :                         }
     777         [ +  + ]:      41198 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
     778                 :            :                         struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
     779                 :      13577 :                         uint32_t num_extent_pages = blob->active.num_extent_pages;
     780                 :            :                         uint32_t i, j;
     781                 :            :                         size_t extent_pages_length;
     782                 :            : 
     783                 :      13577 :                         desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
     784                 :      13577 :                         extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
     785                 :            : 
     786   [ -  +  -  + ]:      13577 :                         if (blob->extent_rle_found) {
     787                 :            :                                 /* This means that Extent RLE is present in MD,
     788                 :            :                                  * both should never be at the same time. */
     789                 :          0 :                                 return -EINVAL;
     790   [ -  +  +  + ]:      13577 :                         } else if (blob->extent_table_found &&
     791         [ -  + ]:          2 :                                    desc_extent_table->num_clusters != blob->remaining_clusters_in_et) {
     792                 :            :                                 /* Number of clusters in this ET does not match number
     793                 :            :                                  * from previously read EXTENT_TABLE. */
     794                 :          0 :                                 return -EINVAL;
     795                 :            :                         }
     796                 :            : 
     797         [ +  - ]:      13577 :                         if (desc_extent_table->length == 0 ||
     798         [ -  + ]:      13577 :                             (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
     799                 :          0 :                                 return -EINVAL;
     800                 :            :                         }
     801                 :            : 
     802                 :      13577 :                         blob->extent_table_found = true;
     803                 :            : 
     804         [ +  + ]:      26746 :                         for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
     805                 :      13169 :                                 num_extent_pages += desc_extent_table->extent_page[i].num_pages;
     806                 :            :                         }
     807                 :            : 
     808         [ +  + ]:      13577 :                         if (num_extent_pages > 0) {
     809                 :      11149 :                                 tmp = realloc(blob->active.extent_pages, num_extent_pages * sizeof(uint32_t));
     810         [ -  + ]:      11149 :                                 if (tmp == NULL) {
     811                 :          0 :                                         return -ENOMEM;
     812                 :            :                                 }
     813                 :      11149 :                                 blob->active.extent_pages = tmp;
     814                 :            :                         }
     815                 :      13577 :                         blob->active.extent_pages_array_size = num_extent_pages;
     816                 :            : 
     817                 :      13577 :                         blob->remaining_clusters_in_et = desc_extent_table->num_clusters;
     818                 :            : 
     819                 :            :                         /* Extent table entries contain md page numbers for extent pages.
     820                 :            :                          * Zeroes represent unallocated extent pages, those are run-length-encoded.
     821                 :            :                          */
     822         [ +  + ]:      26746 :                         for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
     823         [ +  + ]:      13169 :                                 if (desc_extent_table->extent_page[i].page_idx != 0) {
     824         [ -  + ]:      11090 :                                         assert(desc_extent_table->extent_page[i].num_pages == 1);
     825                 :      20043 :                                         blob->active.extent_pages[blob->active.num_extent_pages++] =
     826                 :      11090 :                                                 desc_extent_table->extent_page[i].page_idx;
     827         [ +  - ]:       2079 :                                 } else if (spdk_blob_is_thin_provisioned(blob)) {
     828         [ +  + ]:       9278 :                                         for (j = 0; j < desc_extent_table->extent_page[i].num_pages; j++) {
     829                 :       7199 :                                                 blob->active.extent_pages[blob->active.num_extent_pages++] = 0;
     830                 :            :                                         }
     831                 :            :                                 } else {
     832                 :          0 :                                         return -EINVAL;
     833                 :            :                                 }
     834                 :            :                         }
     835         [ +  + ]:      27621 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
     836                 :            :                         struct spdk_blob_md_descriptor_extent_page      *desc_extent;
     837                 :            :                         unsigned int                                    i;
     838                 :      11066 :                         unsigned int                                    cluster_count = 0;
     839                 :            :                         size_t                                          cluster_idx_length;
     840                 :            : 
     841   [ -  +  -  + ]:      11066 :                         if (blob->extent_rle_found) {
     842                 :            :                                 /* This means that Extent RLE is present in MD,
     843                 :            :                                  * both should never be at the same time. */
     844                 :          0 :                                 return -EINVAL;
     845                 :            :                         }
     846                 :            : 
     847                 :      11066 :                         desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
     848                 :      11066 :                         cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
     849                 :            : 
     850         [ +  - ]:      11066 :                         if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
     851         [ -  + ]:      11066 :                             (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
     852                 :          0 :                                 return -EINVAL;
     853                 :            :                         }
     854                 :            : 
     855         [ +  + ]:    1449238 :                         for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
     856         [ +  + ]:    1438172 :                                 if (desc_extent->cluster_idx[i] != 0) {
     857         [ -  + ]:    1352274 :                                         if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters, desc_extent->cluster_idx[i])) {
     858                 :          0 :                                                 return -EINVAL;
     859                 :            :                                         }
     860                 :            :                                 }
     861                 :    1438172 :                                 cluster_count++;
     862                 :            :                         }
     863                 :            : 
     864         [ -  + ]:      11066 :                         if (cluster_count == 0) {
     865                 :          0 :                                 return -EINVAL;
     866                 :            :                         }
     867                 :            : 
     868                 :            :                         /* When reading extent pages sequentially starting cluster idx should match
     869                 :            :                          * current size of a blob.
     870                 :            :                          * If changed to batch reading, this check shall be removed. */
     871         [ -  + ]:      11066 :                         if (desc_extent->start_cluster_idx != blob->active.num_clusters) {
     872                 :          0 :                                 return -EINVAL;
     873                 :            :                         }
     874                 :            : 
     875                 :      11066 :                         tmp = realloc(blob->active.clusters,
     876                 :      11066 :                                       (cluster_count + blob->active.num_clusters) * sizeof(*blob->active.clusters));
     877         [ -  + ]:      11066 :                         if (tmp == NULL) {
     878                 :          0 :                                 return -ENOMEM;
     879                 :            :                         }
     880                 :      11066 :                         blob->active.clusters = tmp;
     881                 :      11066 :                         blob->active.cluster_array_size = (cluster_count + blob->active.num_clusters);
     882                 :            : 
     883         [ +  + ]:    1449238 :                         for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
     884         [ +  + ]:    1438172 :                                 if (desc_extent->cluster_idx[i] != 0) {
     885                 :    1352274 :                                         blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
     886                 :            :                                                         desc_extent->cluster_idx[i]);
     887                 :    1352274 :                                         blob->active.num_allocated_clusters++;
     888         [ +  - ]:      85898 :                                 } else if (spdk_blob_is_thin_provisioned(blob)) {
     889                 :      85898 :                                         blob->active.clusters[blob->active.num_clusters++] = 0;
     890                 :            :                                 } else {
     891                 :          0 :                                         return -EINVAL;
     892                 :            :                                 }
     893                 :            :                         }
     894         [ -  + ]:      11066 :                         assert(desc_extent->start_cluster_idx + cluster_count == blob->active.num_clusters);
     895         [ -  + ]:      11066 :                         assert(blob->remaining_clusters_in_et >= cluster_count);
     896                 :      11066 :                         blob->remaining_clusters_in_et -= cluster_count;
     897         [ +  + ]:      16555 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
     898                 :            :                         int rc;
     899                 :            : 
     900                 :      12761 :                         rc = blob_deserialize_xattr(blob,
     901                 :            :                                                     (struct spdk_blob_md_descriptor_xattr *) desc, false);
     902         [ -  + ]:      12761 :                         if (rc != 0) {
     903                 :          0 :                                 return rc;
     904                 :            :                         }
     905         [ +  - ]:       3794 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
     906                 :            :                         int rc;
     907                 :            : 
     908                 :       3794 :                         rc = blob_deserialize_xattr(blob,
     909                 :            :                                                     (struct spdk_blob_md_descriptor_xattr *) desc, true);
     910         [ -  + ]:       3794 :                         if (rc != 0) {
     911                 :          0 :                                 return rc;
     912                 :            :                         }
     913                 :            :                 } else {
     914                 :            :                         /* Unrecognized descriptor type.  Do not fail - just continue to the
     915                 :            :                          *  next descriptor.  If this descriptor is associated with some feature
     916                 :            :                          *  defined in a newer version of blobstore, that version of blobstore
     917                 :            :                          *  should create and set an associated feature flag to specify if this
     918                 :            :                          *  blob can be loaded or not.
     919                 :            :                          */
     920                 :            :                 }
     921                 :            : 
     922                 :            :                 /* Advance to the next descriptor */
     923                 :      66989 :                 cur_desc += sizeof(*desc) + desc->length;
     924         [ +  + ]:      66989 :                 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
     925                 :        160 :                         break;
     926                 :            :                 }
     927                 :      66829 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
     928                 :            :         }
     929                 :            : 
     930                 :      31691 :         return 0;
     931                 :            : }
     932                 :            : 
     933                 :            : static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page);
     934                 :            : 
     935                 :            : static int
     936                 :      11066 : blob_parse_extent_page(struct spdk_blob_md_page *extent_page, struct spdk_blob *blob)
     937                 :            : {
     938         [ -  + ]:      11066 :         assert(blob != NULL);
     939         [ -  + ]:      11066 :         assert(blob->state == SPDK_BLOB_STATE_LOADING);
     940                 :            : 
     941         [ -  + ]:      11066 :         if (bs_load_cur_extent_page_valid(extent_page) == false) {
     942                 :          0 :                 return -ENOENT;
     943                 :            :         }
     944                 :            : 
     945                 :      11066 :         return blob_parse_page(extent_page, blob);
     946                 :            : }
     947                 :            : 
     948                 :            : static int
     949                 :      20271 : blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
     950                 :            :            struct spdk_blob *blob)
     951                 :            : {
     952                 :            :         const struct spdk_blob_md_page *page;
     953                 :            :         uint32_t i;
     954                 :            :         int rc;
     955                 :            :         void *tmp;
     956                 :            : 
     957         [ -  + ]:      20271 :         assert(page_count > 0);
     958         [ -  + ]:      20271 :         assert(pages[0].sequence_num == 0);
     959         [ -  + ]:      20271 :         assert(blob != NULL);
     960         [ -  + ]:      20271 :         assert(blob->state == SPDK_BLOB_STATE_LOADING);
     961         [ -  + ]:      20271 :         assert(blob->active.clusters == NULL);
     962                 :            : 
     963                 :            :         /* The blobid provided doesn't match what's in the MD, this can
     964                 :            :          * happen for example if a bogus blobid is passed in through open.
     965                 :            :          */
     966         [ +  + ]:      20271 :         if (blob->id != pages[0].id) {
     967                 :         16 :                 SPDK_ERRLOG("Blobid (0x%" PRIx64 ") doesn't match what's in metadata "
     968                 :            :                             "(0x%" PRIx64 ")\n", blob->id, pages[0].id);
     969                 :         16 :                 return -ENOENT;
     970                 :            :         }
     971                 :            : 
     972                 :      20255 :         tmp = realloc(blob->active.pages, page_count * sizeof(*blob->active.pages));
     973         [ -  + ]:      20255 :         if (!tmp) {
     974                 :          0 :                 return -ENOMEM;
     975                 :            :         }
     976                 :      20255 :         blob->active.pages = tmp;
     977                 :            : 
     978                 :      20255 :         blob->active.pages[0] = pages[0].id;
     979                 :            : 
     980         [ +  + ]:      20657 :         for (i = 1; i < page_count; i++) {
     981         [ -  + ]:        402 :                 assert(spdk_bit_array_get(blob->bs->used_md_pages, pages[i - 1].next));
     982                 :        402 :                 blob->active.pages[i] = pages[i - 1].next;
     983                 :            :         }
     984                 :      20255 :         blob->active.num_pages = page_count;
     985                 :            : 
     986         [ +  + ]:      40880 :         for (i = 0; i < page_count; i++) {
     987                 :      20657 :                 page = &pages[i];
     988                 :            : 
     989         [ -  + ]:      20657 :                 assert(page->id == blob->id);
     990         [ -  + ]:      20657 :                 assert(page->sequence_num == i);
     991                 :            : 
     992                 :      20657 :                 rc = blob_parse_page(page, blob);
     993         [ +  + ]:      20657 :                 if (rc != 0) {
     994                 :         32 :                         return rc;
     995                 :            :                 }
     996                 :            :         }
     997                 :            : 
     998                 :      20223 :         return 0;
     999                 :            : }
    1000                 :            : 
    1001                 :            : static int
    1002                 :      86558 : blob_serialize_add_page(const struct spdk_blob *blob,
    1003                 :            :                         struct spdk_blob_md_page **pages,
    1004                 :            :                         uint32_t *page_count,
    1005                 :            :                         struct spdk_blob_md_page **last_page)
    1006                 :            : {
    1007                 :            :         struct spdk_blob_md_page *page, *tmp_pages;
    1008                 :            : 
    1009         [ -  + ]:      86558 :         assert(pages != NULL);
    1010         [ -  + ]:      86558 :         assert(page_count != NULL);
    1011                 :            : 
    1012                 :      86558 :         *last_page = NULL;
    1013         [ +  + ]:      86558 :         if (*page_count == 0) {
    1014         [ -  + ]:      86205 :                 assert(*pages == NULL);
    1015                 :      86205 :                 *pages = spdk_malloc(SPDK_BS_PAGE_SIZE, 0,
    1016                 :            :                                      NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    1017         [ -  + ]:      86205 :                 if (*pages == NULL) {
    1018                 :          0 :                         return -ENOMEM;
    1019                 :            :                 }
    1020                 :      86205 :                 *page_count = 1;
    1021                 :            :         } else {
    1022         [ -  + ]:        353 :                 assert(*pages != NULL);
    1023                 :        353 :                 tmp_pages = spdk_realloc(*pages, SPDK_BS_PAGE_SIZE * (*page_count + 1), 0);
    1024         [ -  + ]:        353 :                 if (tmp_pages == NULL) {
    1025                 :          0 :                         return -ENOMEM;
    1026                 :            :                 }
    1027                 :        353 :                 (*page_count)++;
    1028                 :        353 :                 *pages = tmp_pages;
    1029                 :            :         }
    1030                 :            : 
    1031                 :      86558 :         page = &(*pages)[*page_count - 1];
    1032         [ -  + ]:      86558 :         memset(page, 0, sizeof(*page));
    1033                 :      86558 :         page->id = blob->id;
    1034                 :      86558 :         page->sequence_num = *page_count - 1;
    1035                 :      86558 :         page->next = SPDK_INVALID_MD_PAGE;
    1036                 :      86558 :         *last_page = page;
    1037                 :            : 
    1038                 :      86558 :         return 0;
    1039                 :            : }
    1040                 :            : 
    1041                 :            : /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
    1042                 :            :  * Update required_sz on both success and failure.
    1043                 :            :  *
    1044                 :            :  */
    1045                 :            : static int
    1046                 :      86070 : blob_serialize_xattr(const struct spdk_xattr *xattr,
    1047                 :            :                      uint8_t *buf, size_t buf_sz,
    1048                 :            :                      size_t *required_sz, bool internal)
    1049                 :            : {
    1050                 :            :         struct spdk_blob_md_descriptor_xattr    *desc;
    1051                 :            : 
    1052                 :     168526 :         *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
    1053         [ -  + ]:      86070 :                        strlen(xattr->name) +
    1054                 :      86070 :                        xattr->value_len;
    1055                 :            : 
    1056         [ +  + ]:      86070 :         if (buf_sz < *required_sz) {
    1057                 :        192 :                 return -1;
    1058                 :            :         }
    1059                 :            : 
    1060                 :      85878 :         desc = (struct spdk_blob_md_descriptor_xattr *)buf;
    1061                 :            : 
    1062         [ +  + ]:      85878 :         desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR;
    1063                 :      85878 :         desc->length = sizeof(desc->name_length) +
    1064                 :            :                        sizeof(desc->value_length) +
    1065         [ -  + ]:      85878 :                        strlen(xattr->name) +
    1066                 :      85878 :                        xattr->value_len;
    1067         [ -  + ]:      85878 :         desc->name_length = strlen(xattr->name);
    1068                 :      85878 :         desc->value_length = xattr->value_len;
    1069                 :            : 
    1070   [ -  +  -  + ]:      85878 :         memcpy(desc->name, xattr->name, desc->name_length);
    1071   [ -  +  -  + ]:      85878 :         memcpy((void *)((uintptr_t)desc->name + desc->name_length),
    1072                 :      85878 :                xattr->value,
    1073                 :      85878 :                desc->value_length);
    1074                 :            : 
    1075                 :      85878 :         return 0;
    1076                 :            : }
    1077                 :            : 
    1078                 :            : static void
    1079                 :      47027 : blob_serialize_extent_table_entry(const struct spdk_blob *blob,
    1080                 :            :                                   uint64_t start_ep, uint64_t *next_ep,
    1081                 :            :                                   uint8_t **buf, size_t *remaining_sz)
    1082                 :            : {
    1083                 :            :         struct spdk_blob_md_descriptor_extent_table *desc;
    1084                 :            :         size_t cur_sz;
    1085                 :            :         uint64_t i, et_idx;
    1086                 :            :         uint32_t extent_page, ep_len;
    1087                 :            : 
    1088                 :            :         /* The buffer must have room for at least num_clusters entry */
    1089                 :      47027 :         cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->num_clusters);
    1090         [ +  + ]:      47027 :         if (*remaining_sz < cur_sz) {
    1091                 :         80 :                 *next_ep = start_ep;
    1092                 :         80 :                 return;
    1093                 :            :         }
    1094                 :            : 
    1095                 :      46947 :         desc = (struct spdk_blob_md_descriptor_extent_table *)*buf;
    1096                 :      46947 :         desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE;
    1097                 :            : 
    1098                 :      46947 :         desc->num_clusters = blob->active.num_clusters;
    1099                 :            : 
    1100                 :      46947 :         ep_len = 1;
    1101                 :      46947 :         et_idx = 0;
    1102         [ +  + ]:      99795 :         for (i = start_ep; i < blob->active.num_extent_pages; i++) {
    1103         [ +  + ]:      52849 :                 if (*remaining_sz < cur_sz  + sizeof(desc->extent_page[0])) {
    1104                 :            :                         /* If we ran out of buffer space, return */
    1105                 :          1 :                         break;
    1106                 :            :                 }
    1107                 :            : 
    1108                 :      52848 :                 extent_page = blob->active.extent_pages[i];
    1109                 :            :                 /* Verify that next extent_page is unallocated */
    1110         [ +  + ]:      52848 :                 if (extent_page == 0 &&
    1111   [ +  +  +  + ]:       8672 :                     (i + 1 < blob->active.num_extent_pages && blob->active.extent_pages[i + 1] == 0)) {
    1112                 :       6718 :                         ep_len++;
    1113                 :       6718 :                         continue;
    1114                 :            :                 }
    1115                 :      46130 :                 desc->extent_page[et_idx].page_idx = extent_page;
    1116                 :      46130 :                 desc->extent_page[et_idx].num_pages = ep_len;
    1117                 :      46130 :                 et_idx++;
    1118                 :            : 
    1119                 :      46130 :                 ep_len = 1;
    1120                 :      46130 :                 cur_sz += sizeof(desc->extent_page[et_idx]);
    1121                 :            :         }
    1122                 :      46947 :         *next_ep = i;
    1123                 :            : 
    1124                 :      46947 :         desc->length = sizeof(desc->num_clusters) + sizeof(desc->extent_page[0]) * et_idx;
    1125                 :      46947 :         *remaining_sz -= sizeof(struct spdk_blob_md_descriptor) + desc->length;
    1126                 :      46947 :         *buf += sizeof(struct spdk_blob_md_descriptor) + desc->length;
    1127                 :            : }
    1128                 :            : 
    1129                 :            : static int
    1130                 :      46954 : blob_serialize_extent_table(const struct spdk_blob *blob,
    1131                 :            :                             struct spdk_blob_md_page **pages,
    1132                 :            :                             struct spdk_blob_md_page *cur_page,
    1133                 :            :                             uint32_t *page_count, uint8_t **buf,
    1134                 :            :                             size_t *remaining_sz)
    1135                 :            : {
    1136                 :      46730 :         uint64_t                                last_extent_page;
    1137                 :            :         int                                     rc;
    1138                 :            : 
    1139                 :      46954 :         last_extent_page = 0;
    1140                 :            :         /* At least single extent table entry has to be always persisted.
    1141                 :            :          * Such case occurs with num_extent_pages == 0. */
    1142         [ +  - ]:      47027 :         while (last_extent_page <= blob->active.num_extent_pages) {
    1143                 :      47027 :                 blob_serialize_extent_table_entry(blob, last_extent_page, &last_extent_page, buf,
    1144                 :            :                                                   remaining_sz);
    1145                 :            : 
    1146         [ +  + ]:      47027 :                 if (last_extent_page == blob->active.num_extent_pages) {
    1147                 :      46954 :                         break;
    1148                 :            :                 }
    1149                 :            : 
    1150                 :         73 :                 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
    1151         [ -  + ]:         73 :                 if (rc < 0) {
    1152                 :          0 :                         return rc;
    1153                 :            :                 }
    1154                 :            : 
    1155                 :         73 :                 *buf = (uint8_t *)cur_page->descriptors;
    1156                 :         73 :                 *remaining_sz = sizeof(cur_page->descriptors);
    1157                 :            :         }
    1158                 :            : 
    1159                 :      46954 :         return 0;
    1160                 :            : }
    1161                 :            : 
    1162                 :            : static void
    1163                 :       6948 : blob_serialize_extent_rle(const struct spdk_blob *blob,
    1164                 :            :                           uint64_t start_cluster, uint64_t *next_cluster,
    1165                 :            :                           uint8_t **buf, size_t *buf_sz)
    1166                 :            : {
    1167                 :            :         struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
    1168                 :            :         size_t cur_sz;
    1169                 :            :         uint64_t i, extent_idx;
    1170                 :            :         uint64_t lba, lba_per_cluster, lba_count;
    1171                 :            : 
    1172                 :            :         /* The buffer must have room for at least one extent */
    1173                 :       6948 :         cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc_extent_rle->extents[0]);
    1174         [ +  + ]:       6948 :         if (*buf_sz < cur_sz) {
    1175                 :         72 :                 *next_cluster = start_cluster;
    1176                 :         72 :                 return;
    1177                 :            :         }
    1178                 :            : 
    1179                 :       6876 :         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)*buf;
    1180                 :       6876 :         desc_extent_rle->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE;
    1181                 :            : 
    1182                 :       6876 :         lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
    1183                 :            :         /* Assert for scan-build false positive */
    1184         [ -  + ]:       6876 :         assert(lba_per_cluster > 0);
    1185                 :            : 
    1186                 :       6876 :         lba = blob->active.clusters[start_cluster];
    1187                 :       6876 :         lba_count = lba_per_cluster;
    1188                 :       6876 :         extent_idx = 0;
    1189         [ +  + ]:    3241800 :         for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
    1190   [ +  +  +  + ]:    3234940 :                 if ((lba + lba_count) == blob->active.clusters[i] && lba != 0) {
    1191                 :            :                         /* Run-length encode sequential non-zero LBA */
    1192                 :      29104 :                         lba_count += lba_per_cluster;
    1193                 :      29104 :                         continue;
    1194   [ +  +  +  + ]:    3205836 :                 } else if (lba == 0 && blob->active.clusters[i] == 0) {
    1195                 :            :                         /* Run-length encode unallocated clusters */
    1196                 :    3201064 :                         lba_count += lba_per_cluster;
    1197                 :    3201064 :                         continue;
    1198                 :            :                 }
    1199         [ -  + ]:       4772 :                 desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
    1200         [ -  + ]:       4772 :                 desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
    1201                 :       4772 :                 extent_idx++;
    1202                 :            : 
    1203                 :       4772 :                 cur_sz += sizeof(desc_extent_rle->extents[extent_idx]);
    1204                 :            : 
    1205         [ +  + ]:       4772 :                 if (*buf_sz < cur_sz) {
    1206                 :            :                         /* If we ran out of buffer space, return */
    1207                 :         16 :                         *next_cluster = i;
    1208                 :         16 :                         break;
    1209                 :            :                 }
    1210                 :            : 
    1211                 :       4756 :                 lba = blob->active.clusters[i];
    1212                 :       4756 :                 lba_count = lba_per_cluster;
    1213                 :            :         }
    1214                 :            : 
    1215         [ +  + ]:       6876 :         if (*buf_sz >= cur_sz) {
    1216         [ -  + ]:       6860 :                 desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
    1217         [ -  + ]:       6860 :                 desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
    1218                 :       6860 :                 extent_idx++;
    1219                 :            : 
    1220                 :       6860 :                 *next_cluster = blob->active.num_clusters;
    1221                 :            :         }
    1222                 :            : 
    1223                 :       6876 :         desc_extent_rle->length = sizeof(desc_extent_rle->extents[0]) * extent_idx;
    1224                 :       6876 :         *buf_sz -= sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
    1225                 :       6876 :         *buf += sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
    1226                 :            : }
    1227                 :            : 
    1228                 :            : static int
    1229                 :       7716 : blob_serialize_extents_rle(const struct spdk_blob *blob,
    1230                 :            :                            struct spdk_blob_md_page **pages,
    1231                 :            :                            struct spdk_blob_md_page *cur_page,
    1232                 :            :                            uint32_t *page_count, uint8_t **buf,
    1233                 :            :                            size_t *remaining_sz)
    1234                 :            : {
    1235                 :       7716 :         uint64_t                                last_cluster;
    1236                 :            :         int                                     rc;
    1237                 :            : 
    1238                 :       7716 :         last_cluster = 0;
    1239         [ +  + ]:       7804 :         while (last_cluster < blob->active.num_clusters) {
    1240                 :       6948 :                 blob_serialize_extent_rle(blob, last_cluster, &last_cluster, buf, remaining_sz);
    1241                 :            : 
    1242         [ +  + ]:       6948 :                 if (last_cluster == blob->active.num_clusters) {
    1243                 :       6860 :                         break;
    1244                 :            :                 }
    1245                 :            : 
    1246                 :         88 :                 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
    1247         [ -  + ]:         88 :                 if (rc < 0) {
    1248                 :          0 :                         return rc;
    1249                 :            :                 }
    1250                 :            : 
    1251                 :         88 :                 *buf = (uint8_t *)cur_page->descriptors;
    1252                 :         88 :                 *remaining_sz = sizeof(cur_page->descriptors);
    1253                 :            :         }
    1254                 :            : 
    1255                 :       7716 :         return 0;
    1256                 :            : }
    1257                 :            : 
    1258                 :            : static void
    1259                 :      35047 : blob_serialize_extent_page(const struct spdk_blob *blob,
    1260                 :            :                            uint64_t cluster, struct spdk_blob_md_page *page)
    1261                 :            : {
    1262                 :            :         struct spdk_blob_md_descriptor_extent_page *desc_extent;
    1263                 :            :         uint64_t i, extent_idx;
    1264                 :            :         uint64_t lba, lba_per_cluster;
    1265         [ -  + ]:      35047 :         uint64_t start_cluster_idx = (cluster / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
    1266                 :            : 
    1267                 :      35047 :         desc_extent = (struct spdk_blob_md_descriptor_extent_page *) page->descriptors;
    1268                 :      35047 :         desc_extent->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE;
    1269                 :            : 
    1270                 :      35047 :         lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
    1271                 :            : 
    1272                 :      35047 :         desc_extent->start_cluster_idx = start_cluster_idx;
    1273                 :      35047 :         extent_idx = 0;
    1274         [ +  + ]:    2683247 :         for (i = start_cluster_idx; i < blob->active.num_clusters; i++) {
    1275                 :    2650788 :                 lba = blob->active.clusters[i];
    1276         [ -  + ]:    2650788 :                 desc_extent->cluster_idx[extent_idx++] = lba / lba_per_cluster;
    1277         [ +  + ]:    2650788 :                 if (extent_idx >= SPDK_EXTENTS_PER_EP) {
    1278                 :       2588 :                         break;
    1279                 :            :                 }
    1280                 :            :         }
    1281                 :      35047 :         desc_extent->length = sizeof(desc_extent->start_cluster_idx) +
    1282                 :            :                               sizeof(desc_extent->cluster_idx[0]) * extent_idx;
    1283                 :      35047 : }
    1284                 :            : 
    1285                 :            : static void
    1286                 :      54670 : blob_serialize_flags(const struct spdk_blob *blob,
    1287                 :            :                      uint8_t *buf, size_t *buf_sz)
    1288                 :            : {
    1289                 :            :         struct spdk_blob_md_descriptor_flags *desc;
    1290                 :            : 
    1291                 :            :         /*
    1292                 :            :          * Flags get serialized first, so we should always have room for the flags
    1293                 :            :          *  descriptor.
    1294                 :            :          */
    1295         [ -  + ]:      54670 :         assert(*buf_sz >= sizeof(*desc));
    1296                 :            : 
    1297                 :      54670 :         desc = (struct spdk_blob_md_descriptor_flags *)buf;
    1298                 :      54670 :         desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS;
    1299                 :      54670 :         desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor);
    1300                 :      54670 :         desc->invalid_flags = blob->invalid_flags;
    1301                 :      54670 :         desc->data_ro_flags = blob->data_ro_flags;
    1302                 :      54670 :         desc->md_ro_flags = blob->md_ro_flags;
    1303                 :            : 
    1304                 :      54670 :         *buf_sz -= sizeof(*desc);
    1305                 :      54670 : }
    1306                 :            : 
    1307                 :            : static int
    1308                 :     109340 : blob_serialize_xattrs(const struct spdk_blob *blob,
    1309                 :            :                       const struct spdk_xattr_tailq *xattrs, bool internal,
    1310                 :            :                       struct spdk_blob_md_page **pages,
    1311                 :            :                       struct spdk_blob_md_page *cur_page,
    1312                 :            :                       uint32_t *page_count, uint8_t **buf,
    1313                 :            :                       size_t *remaining_sz)
    1314                 :            : {
    1315                 :            :         const struct spdk_xattr *xattr;
    1316                 :            :         int     rc;
    1317                 :            : 
    1318         [ +  + ]:     195218 :         TAILQ_FOREACH(xattr, xattrs, link) {
    1319                 :      85878 :                 size_t required_sz = 0;
    1320                 :            : 
    1321                 :      85878 :                 rc = blob_serialize_xattr(xattr,
    1322                 :            :                                           *buf, *remaining_sz,
    1323                 :            :                                           &required_sz, internal);
    1324         [ +  + ]:      85878 :                 if (rc < 0) {
    1325                 :            :                         /* Need to add a new page to the chain */
    1326                 :        192 :                         rc = blob_serialize_add_page(blob, pages, page_count,
    1327                 :            :                                                      &cur_page);
    1328         [ -  + ]:        192 :                         if (rc < 0) {
    1329                 :          0 :                                 spdk_free(*pages);
    1330                 :          0 :                                 *pages = NULL;
    1331                 :          0 :                                 *page_count = 0;
    1332                 :          0 :                                 return rc;
    1333                 :            :                         }
    1334                 :            : 
    1335                 :        192 :                         *buf = (uint8_t *)cur_page->descriptors;
    1336                 :        192 :                         *remaining_sz = sizeof(cur_page->descriptors);
    1337                 :            : 
    1338                 :            :                         /* Try again */
    1339                 :        192 :                         required_sz = 0;
    1340                 :        192 :                         rc = blob_serialize_xattr(xattr,
    1341                 :            :                                                   *buf, *remaining_sz,
    1342                 :            :                                                   &required_sz, internal);
    1343                 :            : 
    1344         [ -  + ]:        192 :                         if (rc < 0) {
    1345                 :          0 :                                 spdk_free(*pages);
    1346                 :          0 :                                 *pages = NULL;
    1347                 :          0 :                                 *page_count = 0;
    1348                 :          0 :                                 return rc;
    1349                 :            :                         }
    1350                 :            :                 }
    1351                 :            : 
    1352                 :      85878 :                 *remaining_sz -= required_sz;
    1353                 :      85878 :                 *buf += required_sz;
    1354                 :            :         }
    1355                 :            : 
    1356                 :     109340 :         return 0;
    1357                 :            : }
    1358                 :            : 
    1359                 :            : static int
    1360                 :      54670 : blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
    1361                 :            :                uint32_t *page_count)
    1362                 :            : {
    1363                 :      54446 :         struct spdk_blob_md_page                *cur_page;
    1364                 :            :         int                                     rc;
    1365                 :      54446 :         uint8_t                                 *buf;
    1366                 :      54446 :         size_t                                  remaining_sz;
    1367                 :            : 
    1368         [ -  + ]:      54670 :         assert(pages != NULL);
    1369         [ -  + ]:      54670 :         assert(page_count != NULL);
    1370         [ -  + ]:      54670 :         assert(blob != NULL);
    1371         [ -  + ]:      54670 :         assert(blob->state == SPDK_BLOB_STATE_DIRTY);
    1372                 :            : 
    1373                 :      54670 :         *pages = NULL;
    1374                 :      54670 :         *page_count = 0;
    1375                 :            : 
    1376                 :            :         /* A blob always has at least 1 page, even if it has no descriptors */
    1377                 :      54670 :         rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
    1378         [ -  + ]:      54670 :         if (rc < 0) {
    1379                 :          0 :                 return rc;
    1380                 :            :         }
    1381                 :            : 
    1382                 :      54670 :         buf = (uint8_t *)cur_page->descriptors;
    1383                 :      54670 :         remaining_sz = sizeof(cur_page->descriptors);
    1384                 :            : 
    1385                 :            :         /* Serialize flags */
    1386                 :      54670 :         blob_serialize_flags(blob, buf, &remaining_sz);
    1387                 :      54670 :         buf += sizeof(struct spdk_blob_md_descriptor_flags);
    1388                 :            : 
    1389                 :            :         /* Serialize xattrs */
    1390                 :      54670 :         rc = blob_serialize_xattrs(blob, &blob->xattrs, false,
    1391                 :            :                                    pages, cur_page, page_count, &buf, &remaining_sz);
    1392         [ -  + ]:      54670 :         if (rc < 0) {
    1393                 :          0 :                 return rc;
    1394                 :            :         }
    1395                 :            : 
    1396                 :            :         /* Serialize internal xattrs */
    1397                 :      54670 :         rc = blob_serialize_xattrs(blob, &blob->xattrs_internal, true,
    1398                 :            :                                    pages, cur_page, page_count, &buf, &remaining_sz);
    1399         [ -  + ]:      54670 :         if (rc < 0) {
    1400                 :          0 :                 return rc;
    1401                 :            :         }
    1402                 :            : 
    1403   [ +  +  +  + ]:      54670 :         if (blob->use_extent_table) {
    1404                 :            :                 /* Serialize extent table */
    1405                 :      46954 :                 rc = blob_serialize_extent_table(blob, pages, cur_page, page_count, &buf, &remaining_sz);
    1406                 :            :         } else {
    1407                 :            :                 /* Serialize extents */
    1408                 :       7716 :                 rc = blob_serialize_extents_rle(blob, pages, cur_page, page_count, &buf, &remaining_sz);
    1409                 :            :         }
    1410                 :            : 
    1411                 :      54670 :         return rc;
    1412                 :            : }
    1413                 :            : 
    1414                 :            : struct spdk_blob_load_ctx {
    1415                 :            :         struct spdk_blob                *blob;
    1416                 :            : 
    1417                 :            :         struct spdk_blob_md_page        *pages;
    1418                 :            :         uint32_t                        num_pages;
    1419                 :            :         uint32_t                        next_extent_page;
    1420                 :            :         spdk_bs_sequence_t              *seq;
    1421                 :            : 
    1422                 :            :         spdk_bs_sequence_cpl            cb_fn;
    1423                 :            :         void                            *cb_arg;
    1424                 :            : };
    1425                 :            : 
    1426                 :            : static uint32_t
    1427                 :     173743 : blob_md_page_calc_crc(void *page)
    1428                 :            : {
    1429                 :            :         uint32_t                crc;
    1430                 :            : 
    1431                 :     173743 :         crc = BLOB_CRC32C_INITIAL;
    1432                 :     173743 :         crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
    1433                 :     173743 :         crc ^= BLOB_CRC32C_INITIAL;
    1434                 :            : 
    1435                 :     173743 :         return crc;
    1436                 :            : 
    1437                 :            : }
    1438                 :            : 
    1439                 :            : static void
    1440                 :      20383 : blob_load_final(struct spdk_blob_load_ctx *ctx, int bserrno)
    1441                 :            : {
    1442                 :      20383 :         struct spdk_blob                *blob = ctx->blob;
    1443                 :            : 
    1444         [ +  + ]:      20383 :         if (bserrno == 0) {
    1445                 :      20127 :                 blob_mark_clean(blob);
    1446                 :            :         }
    1447                 :            : 
    1448                 :      20383 :         ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno);
    1449                 :            : 
    1450                 :            :         /* Free the memory */
    1451                 :      20383 :         spdk_free(ctx->pages);
    1452                 :      20383 :         free(ctx);
    1453                 :      20383 : }
    1454                 :            : 
    1455                 :            : static void
    1456                 :       1964 : blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
    1457                 :            : {
    1458                 :       1964 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1459                 :       1964 :         struct spdk_blob                *blob = ctx->blob;
    1460                 :            : 
    1461         [ +  + ]:       1964 :         if (bserrno == 0) {
    1462                 :       1940 :                 blob->back_bs_dev = bs_create_blob_bs_dev(snapshot);
    1463         [ -  + ]:       1940 :                 if (blob->back_bs_dev == NULL) {
    1464                 :          0 :                         bserrno = -ENOMEM;
    1465                 :            :                 }
    1466                 :            :         }
    1467         [ +  + ]:       1964 :         if (bserrno != 0) {
    1468                 :         24 :                 SPDK_ERRLOG("Snapshot fail\n");
    1469                 :            :         }
    1470                 :            : 
    1471                 :       1964 :         blob_load_final(ctx, bserrno);
    1472                 :       1964 : }
    1473                 :            : 
    1474                 :            : static void blob_update_clear_method(struct spdk_blob *blob);
    1475                 :            : 
    1476                 :            : static int
    1477                 :        557 : blob_load_esnap(struct spdk_blob *blob, void *blob_ctx)
    1478                 :            : {
    1479                 :        557 :         struct spdk_blob_store *bs = blob->bs;
    1480                 :        557 :         struct spdk_bs_dev *bs_dev = NULL;
    1481                 :        557 :         const void *esnap_id = NULL;
    1482                 :        557 :         size_t id_len = 0;
    1483                 :            :         int rc;
    1484                 :            : 
    1485         [ +  + ]:        557 :         if (bs->esnap_bs_dev_create == NULL) {
    1486                 :         32 :                 SPDK_NOTICELOG("blob 0x%" PRIx64 " is an esnap clone but the blobstore was opened "
    1487                 :            :                                "without support for esnap clones\n", blob->id);
    1488                 :         32 :                 return -ENOTSUP;
    1489                 :            :         }
    1490         [ -  + ]:        525 :         assert(blob->back_bs_dev == NULL);
    1491                 :            : 
    1492                 :        525 :         rc = blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, &esnap_id, &id_len, true);
    1493         [ -  + ]:        525 :         if (rc != 0) {
    1494                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " is an esnap clone but has no esnap ID\n", blob->id);
    1495                 :          0 :                 return -EINVAL;
    1496                 :            :         }
    1497   [ +  -  +  + ]:        525 :         assert(id_len > 0 && id_len < UINT32_MAX);
    1498                 :            : 
    1499   [ -  +  -  + ]:        525 :         SPDK_INFOLOG(blob, "Creating external snapshot device\n");
    1500                 :            : 
    1501                 :        525 :         rc = bs->esnap_bs_dev_create(bs->esnap_ctx, blob_ctx, blob, esnap_id, (uint32_t)id_len,
    1502                 :            :                                      &bs_dev);
    1503         [ -  + ]:        525 :         if (rc != 0) {
    1504   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": failed to load back_bs_dev "
    1505                 :            :                               "with error %d\n", blob->id, rc);
    1506                 :          0 :                 return rc;
    1507                 :            :         }
    1508                 :            : 
    1509                 :            :         /*
    1510                 :            :          * Note: bs_dev might be NULL if the consumer chose to not open the external snapshot.
    1511                 :            :          * This especially might happen during spdk_bs_load() iteration.
    1512                 :            :          */
    1513         [ +  + ]:        525 :         if (bs_dev != NULL) {
    1514   [ -  +  -  + ]:        481 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": loaded back_bs_dev\n", blob->id);
    1515   [ +  +  +  + ]:        481 :                 if ((bs->io_unit_size % bs_dev->blocklen) != 0) {
    1516                 :         16 :                         SPDK_NOTICELOG("blob 0x%" PRIx64 " external snapshot device block size %u "
    1517                 :            :                                        "is not compatible with blobstore block size %u\n",
    1518                 :            :                                        blob->id, bs_dev->blocklen, bs->io_unit_size);
    1519                 :         16 :                         bs_dev->destroy(bs_dev);
    1520                 :         16 :                         return -EINVAL;
    1521                 :            :                 }
    1522                 :            :         }
    1523                 :            : 
    1524                 :        509 :         blob->back_bs_dev = bs_dev;
    1525                 :        509 :         blob->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
    1526                 :            : 
    1527                 :        509 :         return 0;
    1528                 :            : }
    1529                 :            : 
    1530                 :            : static void
    1531                 :      20199 : blob_load_backing_dev(spdk_bs_sequence_t *seq, void *cb_arg)
    1532                 :            : {
    1533                 :      20199 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1534                 :      20199 :         struct spdk_blob                *blob = ctx->blob;
    1535                 :      19851 :         const void                      *value;
    1536                 :      19851 :         size_t                          len;
    1537                 :            :         int                             rc;
    1538                 :            : 
    1539         [ +  + ]:      20199 :         if (blob_is_esnap_clone(blob)) {
    1540                 :        557 :                 rc = blob_load_esnap(blob, seq->cpl.u.blob_handle.esnap_ctx);
    1541                 :        557 :                 blob_load_final(ctx, rc);
    1542                 :        579 :                 return;
    1543                 :            :         }
    1544                 :            : 
    1545         [ +  + ]:      19642 :         if (spdk_blob_is_thin_provisioned(blob)) {
    1546                 :       4487 :                 rc = blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true);
    1547         [ +  + ]:       4487 :                 if (rc == 0) {
    1548         [ -  + ]:       1964 :                         if (len != sizeof(spdk_blob_id)) {
    1549                 :          0 :                                 blob_load_final(ctx, -EINVAL);
    1550                 :          0 :                                 return;
    1551                 :            :                         }
    1552                 :            :                         /* open snapshot blob and continue in the callback function */
    1553                 :       1964 :                         blob->parent_id = *(spdk_blob_id *)value;
    1554                 :       1964 :                         spdk_bs_open_blob(blob->bs, blob->parent_id,
    1555                 :            :                                           blob_load_snapshot_cpl, ctx);
    1556                 :       1964 :                         return;
    1557                 :            :                 } else {
    1558                 :            :                         /* add zeroes_dev for thin provisioned blob */
    1559                 :       2523 :                         blob->back_bs_dev = bs_create_zeroes_dev();
    1560                 :            :                 }
    1561                 :            :         } else {
    1562                 :            :                 /* standard blob */
    1563                 :      15155 :                 blob->back_bs_dev = NULL;
    1564                 :            :         }
    1565                 :      17678 :         blob_load_final(ctx, 0);
    1566                 :            : }
    1567                 :            : 
    1568                 :            : static void
    1569                 :      24665 : blob_load_cpl_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1570                 :            : {
    1571                 :      24665 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1572                 :      24665 :         struct spdk_blob                *blob = ctx->blob;
    1573                 :            :         struct spdk_blob_md_page        *page;
    1574                 :            :         uint64_t                        i;
    1575                 :            :         uint32_t                        crc;
    1576                 :            :         uint64_t                        lba;
    1577                 :            :         void                            *tmp;
    1578                 :            :         uint64_t                        sz;
    1579                 :            : 
    1580         [ +  + ]:      24665 :         if (bserrno) {
    1581                 :         24 :                 SPDK_ERRLOG("Extent page read failed: %d\n", bserrno);
    1582                 :         24 :                 blob_load_final(ctx, bserrno);
    1583                 :         24 :                 return;
    1584                 :            :         }
    1585                 :            : 
    1586         [ +  + ]:      24641 :         if (ctx->pages == NULL) {
    1587                 :            :                 /* First iteration of this function, allocate buffer for single EXTENT_PAGE */
    1588                 :      13575 :                 ctx->pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
    1589                 :            :                                           NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    1590         [ -  + ]:      13575 :                 if (!ctx->pages) {
    1591                 :          0 :                         blob_load_final(ctx, -ENOMEM);
    1592                 :          0 :                         return;
    1593                 :            :                 }
    1594                 :      13575 :                 ctx->num_pages = 1;
    1595                 :      13575 :                 ctx->next_extent_page = 0;
    1596                 :            :         } else {
    1597                 :      11066 :                 page = &ctx->pages[0];
    1598                 :      11066 :                 crc = blob_md_page_calc_crc(page);
    1599         [ -  + ]:      11066 :                 if (crc != page->crc) {
    1600                 :          0 :                         blob_load_final(ctx, -EINVAL);
    1601                 :          0 :                         return;
    1602                 :            :                 }
    1603                 :            : 
    1604         [ -  + ]:      11066 :                 if (page->next != SPDK_INVALID_MD_PAGE) {
    1605                 :          0 :                         blob_load_final(ctx, -EINVAL);
    1606                 :          0 :                         return;
    1607                 :            :                 }
    1608                 :            : 
    1609                 :      11066 :                 bserrno = blob_parse_extent_page(page, blob);
    1610         [ -  + ]:      11066 :                 if (bserrno) {
    1611                 :          0 :                         blob_load_final(ctx, bserrno);
    1612                 :          0 :                         return;
    1613                 :            :                 }
    1614                 :            :         }
    1615                 :            : 
    1616         [ +  + ]:      31840 :         for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) {
    1617         [ +  + ]:      18289 :                 if (blob->active.extent_pages[i] != 0) {
    1618                 :            :                         /* Extent page was allocated, read and parse it. */
    1619                 :      11090 :                         lba = bs_md_page_to_lba(blob->bs, blob->active.extent_pages[i]);
    1620                 :      11090 :                         ctx->next_extent_page = i + 1;
    1621                 :            : 
    1622                 :      11090 :                         bs_sequence_read_dev(seq, &ctx->pages[0], lba,
    1623                 :      11090 :                                              bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
    1624                 :            :                                              blob_load_cpl_extents_cpl, ctx);
    1625                 :      11090 :                         return;
    1626                 :            :                 } else {
    1627                 :            :                         /* Thin provisioned blobs can point to unallocated extent pages.
    1628                 :            :                          * In this case blob size should be increased by up to the amount left in remaining_clusters_in_et. */
    1629                 :            : 
    1630         [ +  + ]:       7199 :                         sz = spdk_min(blob->remaining_clusters_in_et, SPDK_EXTENTS_PER_EP);
    1631                 :       7199 :                         blob->active.num_clusters += sz;
    1632                 :       7199 :                         blob->remaining_clusters_in_et -= sz;
    1633                 :            : 
    1634         [ -  + ]:       7199 :                         assert(spdk_blob_is_thin_provisioned(blob));
    1635   [ +  +  -  + ]:       7199 :                         assert(i + 1 < blob->active.num_extent_pages || blob->remaining_clusters_in_et == 0);
    1636                 :            : 
    1637                 :       7199 :                         tmp = realloc(blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
    1638         [ -  + ]:       7199 :                         if (tmp == NULL) {
    1639                 :          0 :                                 blob_load_final(ctx, -ENOMEM);
    1640                 :          0 :                                 return;
    1641                 :            :                         }
    1642         [ -  + ]:       7199 :                         memset(tmp + sizeof(*blob->active.clusters) * blob->active.cluster_array_size, 0,
    1643                 :       7199 :                                sizeof(*blob->active.clusters) * (blob->active.num_clusters - blob->active.cluster_array_size));
    1644                 :       7199 :                         blob->active.clusters = tmp;
    1645                 :       7199 :                         blob->active.cluster_array_size = blob->active.num_clusters;
    1646                 :            :                 }
    1647                 :            :         }
    1648                 :            : 
    1649                 :      13551 :         blob_load_backing_dev(seq, ctx);
    1650                 :            : }
    1651                 :            : 
    1652                 :            : static void
    1653                 :      20785 : blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1654                 :            : {
    1655                 :      20785 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1656                 :      20785 :         struct spdk_blob                *blob = ctx->blob;
    1657                 :            :         struct spdk_blob_md_page        *page;
    1658                 :            :         int                             rc;
    1659                 :            :         uint32_t                        crc;
    1660                 :            :         uint32_t                        current_page;
    1661                 :            : 
    1662         [ +  + ]:      20785 :         if (ctx->num_pages == 1) {
    1663                 :      20383 :                 current_page = bs_blobid_to_page(blob->id);
    1664                 :            :         } else {
    1665         [ -  + ]:        402 :                 assert(ctx->num_pages != 0);
    1666                 :        402 :                 page = &ctx->pages[ctx->num_pages - 2];
    1667                 :        402 :                 current_page = page->next;
    1668                 :            :         }
    1669                 :            : 
    1670         [ +  + ]:      20785 :         if (bserrno) {
    1671                 :         80 :                 SPDK_ERRLOG("Metadata page %d read failed for blobid 0x%" PRIx64 ": %d\n",
    1672                 :            :                             current_page, blob->id, bserrno);
    1673                 :         80 :                 blob_load_final(ctx, bserrno);
    1674                 :         80 :                 return;
    1675                 :            :         }
    1676                 :            : 
    1677                 :      20705 :         page = &ctx->pages[ctx->num_pages - 1];
    1678                 :      20705 :         crc = blob_md_page_calc_crc(page);
    1679         [ +  + ]:      20705 :         if (crc != page->crc) {
    1680                 :         32 :                 SPDK_ERRLOG("Metadata page %d crc mismatch for blobid 0x%" PRIx64 "\n",
    1681                 :            :                             current_page, blob->id);
    1682                 :         32 :                 blob_load_final(ctx, -EINVAL);
    1683                 :         32 :                 return;
    1684                 :            :         }
    1685                 :            : 
    1686         [ +  + ]:      20673 :         if (page->next != SPDK_INVALID_MD_PAGE) {
    1687                 :            :                 struct spdk_blob_md_page *tmp_pages;
    1688                 :        402 :                 uint32_t next_page = page->next;
    1689                 :        402 :                 uint64_t next_lba = bs_md_page_to_lba(blob->bs, next_page);
    1690                 :            : 
    1691                 :            :                 /* Read the next page */
    1692                 :        402 :                 tmp_pages = spdk_realloc(ctx->pages, (sizeof(*page) * (ctx->num_pages + 1)), 0);
    1693         [ -  + ]:        402 :                 if (tmp_pages == NULL) {
    1694                 :          0 :                         blob_load_final(ctx, -ENOMEM);
    1695                 :          0 :                         return;
    1696                 :            :                 }
    1697                 :        402 :                 ctx->num_pages++;
    1698                 :        402 :                 ctx->pages = tmp_pages;
    1699                 :            : 
    1700                 :        402 :                 bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1],
    1701                 :            :                                      next_lba,
    1702                 :        402 :                                      bs_byte_to_lba(blob->bs, sizeof(*page)),
    1703                 :            :                                      blob_load_cpl, ctx);
    1704                 :        402 :                 return;
    1705                 :            :         }
    1706                 :            : 
    1707                 :            :         /* Parse the pages */
    1708                 :      20271 :         rc = blob_parse(ctx->pages, ctx->num_pages, blob);
    1709         [ +  + ]:      20271 :         if (rc) {
    1710                 :         48 :                 blob_load_final(ctx, rc);
    1711                 :         48 :                 return;
    1712                 :            :         }
    1713                 :            : 
    1714   [ +  +  +  + ]:      20223 :         if (blob->extent_table_found == true) {
    1715                 :            :                 /* If EXTENT_TABLE was found, that means support for it should be enabled. */
    1716   [ -  +  -  + ]:      13575 :                 assert(blob->extent_rle_found == false);
    1717                 :      13575 :                 blob->use_extent_table = true;
    1718                 :            :         } else {
    1719                 :            :                 /* If EXTENT_RLE or no extent_* descriptor was found disable support
    1720                 :            :                  * for extent table. No extent_* descriptors means that blob has length of 0
    1721                 :            :                  * and no extent_rle descriptors were persisted for it.
    1722                 :            :                  * EXTENT_TABLE if used, is always present in metadata regardless of length. */
    1723                 :       6648 :                 blob->use_extent_table = false;
    1724                 :            :         }
    1725                 :            : 
    1726                 :            :         /* Check the clear_method stored in metadata vs what may have been passed
    1727                 :            :          * via spdk_bs_open_blob_ext() and update accordingly.
    1728                 :            :          */
    1729                 :      20223 :         blob_update_clear_method(blob);
    1730                 :            : 
    1731                 :      20223 :         spdk_free(ctx->pages);
    1732                 :      20223 :         ctx->pages = NULL;
    1733                 :            : 
    1734   [ +  +  +  + ]:      20223 :         if (blob->extent_table_found) {
    1735                 :      13575 :                 blob_load_cpl_extents_cpl(seq, ctx, 0);
    1736                 :            :         } else {
    1737                 :       6648 :                 blob_load_backing_dev(seq, ctx);
    1738                 :            :         }
    1739                 :            : }
    1740                 :            : 
    1741                 :            : /* Load a blob from disk given a blobid */
    1742                 :            : static void
    1743                 :      20383 : blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
    1744                 :            :           spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    1745                 :            : {
    1746                 :            :         struct spdk_blob_load_ctx *ctx;
    1747                 :            :         struct spdk_blob_store *bs;
    1748                 :            :         uint32_t page_num;
    1749                 :            :         uint64_t lba;
    1750                 :            : 
    1751                 :      20383 :         blob_verify_md_op(blob);
    1752                 :            : 
    1753                 :      20383 :         bs = blob->bs;
    1754                 :            : 
    1755                 :      20383 :         ctx = calloc(1, sizeof(*ctx));
    1756         [ -  + ]:      20383 :         if (!ctx) {
    1757                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    1758                 :          0 :                 return;
    1759                 :            :         }
    1760                 :            : 
    1761                 :      20383 :         ctx->blob = blob;
    1762                 :      20383 :         ctx->pages = spdk_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 0);
    1763         [ -  + ]:      20383 :         if (!ctx->pages) {
    1764                 :          0 :                 free(ctx);
    1765                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    1766                 :          0 :                 return;
    1767                 :            :         }
    1768                 :      20383 :         ctx->num_pages = 1;
    1769                 :      20383 :         ctx->cb_fn = cb_fn;
    1770                 :      20383 :         ctx->cb_arg = cb_arg;
    1771                 :      20383 :         ctx->seq = seq;
    1772                 :            : 
    1773                 :      20383 :         page_num = bs_blobid_to_page(blob->id);
    1774                 :      20383 :         lba = bs_md_page_to_lba(blob->bs, page_num);
    1775                 :            : 
    1776                 :      20383 :         blob->state = SPDK_BLOB_STATE_LOADING;
    1777                 :            : 
    1778                 :      20383 :         bs_sequence_read_dev(seq, &ctx->pages[0], lba,
    1779                 :      20383 :                              bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
    1780                 :            :                              blob_load_cpl, ctx);
    1781                 :            : }
    1782                 :            : 
    1783                 :            : struct spdk_blob_persist_ctx {
    1784                 :            :         struct spdk_blob                *blob;
    1785                 :            : 
    1786                 :            :         struct spdk_blob_md_page        *pages;
    1787                 :            :         uint32_t                        next_extent_page;
    1788                 :            :         struct spdk_blob_md_page        *extent_page;
    1789                 :            : 
    1790                 :            :         spdk_bs_sequence_t              *seq;
    1791                 :            :         spdk_bs_sequence_cpl            cb_fn;
    1792                 :            :         void                            *cb_arg;
    1793                 :            :         TAILQ_ENTRY(spdk_blob_persist_ctx) link;
    1794                 :            : };
    1795                 :            : 
    1796                 :            : static void
    1797                 :       6582 : bs_batch_clear_dev(struct spdk_blob *blob, spdk_bs_batch_t *batch, uint64_t lba,
    1798                 :            :                    uint64_t lba_count)
    1799                 :            : {
    1800      [ +  -  + ]:       6582 :         switch (blob->clear_method) {
    1801                 :       6581 :         case BLOB_CLEAR_WITH_DEFAULT:
    1802                 :            :         case BLOB_CLEAR_WITH_UNMAP:
    1803                 :       6581 :                 bs_batch_unmap_dev(batch, lba, lba_count);
    1804                 :       6581 :                 break;
    1805                 :          0 :         case BLOB_CLEAR_WITH_WRITE_ZEROES:
    1806                 :          0 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    1807                 :          0 :                 break;
    1808                 :          1 :         case BLOB_CLEAR_WITH_NONE:
    1809                 :            :         default:
    1810                 :          1 :                 break;
    1811                 :            :         }
    1812                 :       6582 : }
    1813                 :            : 
    1814                 :            : static int
    1815                 :      12196 : bs_super_validate(struct spdk_bs_super_block *super, struct spdk_blob_store *bs)
    1816                 :            : {
    1817                 :            :         uint32_t        crc;
    1818                 :            :         static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
    1819                 :            : 
    1820         [ +  + ]:      12196 :         if (super->version > SPDK_BS_VERSION ||
    1821         [ +  + ]:      11528 :             super->version < SPDK_BS_INITIAL_VERSION) {
    1822                 :       6843 :                 return -EILSEQ;
    1823                 :            :         }
    1824                 :            : 
    1825   [ +  +  +  + ]:       5353 :         if (memcmp(super->signature, SPDK_BS_SUPER_BLOCK_SIG,
    1826                 :            :                    sizeof(super->signature)) != 0) {
    1827                 :        349 :                 return -EILSEQ;
    1828                 :            :         }
    1829                 :            : 
    1830                 :       5004 :         crc = blob_md_page_calc_crc(super);
    1831         [ +  + ]:       5004 :         if (crc != super->crc) {
    1832                 :         16 :                 return -EILSEQ;
    1833                 :            :         }
    1834                 :            : 
    1835   [ +  +  -  +  :       4988 :         if (memcmp(&bs->bstype, &super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
                   +  + ]
    1836   [ -  +  -  + ]:       4914 :                 SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n");
    1837   [ +  +  +  + ]:         74 :         } else if (memcmp(&bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
    1838   [ -  +  -  + ]:         34 :                 SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n");
    1839                 :            :         } else {
    1840   [ -  +  -  + ]:         40 :                 SPDK_DEBUGLOG(blob, "Unexpected bstype\n");
    1841   [ -  +  -  + ]:         40 :                 SPDK_LOGDUMP(blob, "Expected:", bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
    1842   [ -  +  -  + ]:         40 :                 SPDK_LOGDUMP(blob, "Found:", super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
    1843                 :         40 :                 return -ENXIO;
    1844                 :            :         }
    1845                 :            : 
    1846         [ +  + ]:       4948 :         if (super->size > bs->dev->blockcnt * bs->dev->blocklen) {
    1847                 :         32 :                 SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n",
    1848                 :            :                                bs->dev->blockcnt * bs->dev->blocklen, super->size);
    1849                 :         32 :                 return -EILSEQ;
    1850                 :            :         }
    1851                 :            : 
    1852                 :       4916 :         return 0;
    1853                 :            : }
    1854                 :            : 
    1855                 :            : static void bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    1856                 :            :                           spdk_bs_sequence_cpl cb_fn, void *cb_arg);
    1857                 :            : 
    1858                 :            : static void
    1859                 :      61418 : blob_persist_complete_cb(void *arg)
    1860                 :            : {
    1861                 :      61418 :         struct spdk_blob_persist_ctx *ctx = arg;
    1862                 :            : 
    1863                 :            :         /* Call user callback */
    1864                 :      61418 :         ctx->cb_fn(ctx->seq, ctx->cb_arg, 0);
    1865                 :            : 
    1866                 :            :         /* Free the memory */
    1867                 :      61418 :         spdk_free(ctx->pages);
    1868                 :      61418 :         free(ctx);
    1869                 :      61418 : }
    1870                 :            : 
    1871                 :            : static void blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
    1872                 :            : 
    1873                 :            : static void
    1874                 :      61418 : blob_persist_complete(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx, int bserrno)
    1875                 :            : {
    1876                 :            :         struct spdk_blob_persist_ctx    *next_persist, *tmp;
    1877                 :      61418 :         struct spdk_blob                *blob = ctx->blob;
    1878                 :            : 
    1879         [ +  + ]:      61418 :         if (bserrno == 0) {
    1880                 :      61210 :                 blob_mark_clean(blob);
    1881                 :            :         }
    1882                 :            : 
    1883         [ -  + ]:      61418 :         assert(ctx == TAILQ_FIRST(&blob->persists_to_complete));
    1884                 :            : 
    1885                 :            :         /* Complete all persists that were pending when the current persist started */
    1886         [ +  + ]:     122836 :         TAILQ_FOREACH_SAFE(next_persist, &blob->persists_to_complete, link, tmp) {
    1887         [ -  + ]:      61418 :                 TAILQ_REMOVE(&blob->persists_to_complete, next_persist, link);
    1888                 :      61418 :                 spdk_thread_send_msg(spdk_get_thread(), blob_persist_complete_cb, next_persist);
    1889                 :            :         }
    1890                 :            : 
    1891         [ +  + ]:      61418 :         if (TAILQ_EMPTY(&blob->pending_persists)) {
    1892                 :      61325 :                 return;
    1893                 :            :         }
    1894                 :            : 
    1895                 :            :         /* Queue up all pending persists for completion and start blob persist with first one */
    1896   [ +  -  -  + ]:         93 :         TAILQ_SWAP(&blob->persists_to_complete, &blob->pending_persists, spdk_blob_persist_ctx, link);
    1897                 :         93 :         next_persist = TAILQ_FIRST(&blob->persists_to_complete);
    1898                 :            : 
    1899                 :         93 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    1900                 :         93 :         bs_mark_dirty(seq, blob->bs, blob_persist_start, next_persist);
    1901                 :            : }
    1902                 :            : 
    1903                 :            : static void
    1904                 :      61210 : blob_persist_clear_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1905                 :            : {
    1906                 :      61210 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    1907                 :      61210 :         struct spdk_blob                *blob = ctx->blob;
    1908                 :      61210 :         struct spdk_blob_store          *bs = blob->bs;
    1909                 :            :         size_t                          i;
    1910                 :            : 
    1911         [ -  + ]:      61210 :         if (bserrno != 0) {
    1912                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    1913                 :          0 :                 return;
    1914                 :            :         }
    1915                 :            : 
    1916                 :      61210 :         spdk_spin_lock(&bs->used_lock);
    1917                 :            : 
    1918                 :            :         /* Release all extent_pages that were truncated */
    1919         [ +  + ]:      70818 :         for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
    1920                 :            :                 /* Nothing to release if it was not allocated */
    1921         [ +  + ]:       9608 :                 if (blob->active.extent_pages[i] != 0) {
    1922                 :       4033 :                         bs_release_md_page(bs, blob->active.extent_pages[i]);
    1923                 :            :                 }
    1924                 :            :         }
    1925                 :            : 
    1926                 :      61210 :         spdk_spin_unlock(&bs->used_lock);
    1927                 :            : 
    1928         [ +  + ]:      61210 :         if (blob->active.num_extent_pages == 0) {
    1929                 :      16367 :                 free(blob->active.extent_pages);
    1930                 :      16367 :                 blob->active.extent_pages = NULL;
    1931                 :      16367 :                 blob->active.extent_pages_array_size = 0;
    1932         [ +  + ]:      44843 :         } else if (blob->active.num_extent_pages != blob->active.extent_pages_array_size) {
    1933                 :            : #ifndef __clang_analyzer__
    1934                 :            :                 void *tmp;
    1935                 :            : 
    1936                 :            :                 /* scan-build really can't figure reallocs, workaround it */
    1937                 :          8 :                 tmp = realloc(blob->active.extent_pages, sizeof(uint32_t) * blob->active.num_extent_pages);
    1938         [ -  + ]:          8 :                 assert(tmp != NULL);
    1939                 :          8 :                 blob->active.extent_pages = tmp;
    1940                 :            : #endif
    1941                 :          8 :                 blob->active.extent_pages_array_size = blob->active.num_extent_pages;
    1942                 :            :         }
    1943                 :            : 
    1944                 :      61210 :         blob_persist_complete(seq, ctx, bserrno);
    1945                 :            : }
    1946                 :            : 
    1947                 :            : static void
    1948                 :      61210 : blob_persist_clear_extents(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
    1949                 :            : {
    1950                 :      61210 :         struct spdk_blob                *blob = ctx->blob;
    1951                 :      61210 :         struct spdk_blob_store          *bs = blob->bs;
    1952                 :            :         size_t                          i;
    1953                 :            :         uint64_t                        lba;
    1954                 :            :         uint64_t                        lba_count;
    1955                 :            :         spdk_bs_batch_t                 *batch;
    1956                 :            : 
    1957                 :      61210 :         batch = bs_sequence_to_batch(seq, blob_persist_clear_extents_cpl, ctx);
    1958                 :      61210 :         lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
    1959                 :            : 
    1960                 :            :         /* Clear all extent_pages that were truncated */
    1961         [ +  + ]:      70818 :         for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
    1962                 :            :                 /* Nothing to clear if it was not allocated */
    1963         [ +  + ]:       9608 :                 if (blob->active.extent_pages[i] != 0) {
    1964                 :       4033 :                         lba = bs_md_page_to_lba(bs, blob->active.extent_pages[i]);
    1965                 :       4033 :                         bs_batch_write_zeroes_dev(batch, lba, lba_count);
    1966                 :            :                 }
    1967                 :            :         }
    1968                 :            : 
    1969                 :      61210 :         bs_batch_close(batch);
    1970                 :      61210 : }
    1971                 :            : 
    1972                 :            : static void
    1973                 :      61210 : blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1974                 :            : {
    1975                 :      61210 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    1976                 :      61210 :         struct spdk_blob                *blob = ctx->blob;
    1977                 :      61210 :         struct spdk_blob_store          *bs = blob->bs;
    1978                 :            :         size_t                          i;
    1979                 :            : 
    1980         [ -  + ]:      61210 :         if (bserrno != 0) {
    1981                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    1982                 :          0 :                 return;
    1983                 :            :         }
    1984                 :            : 
    1985                 :      61210 :         spdk_spin_lock(&bs->used_lock);
    1986                 :            :         /* Release all clusters that were truncated */
    1987         [ +  + ]:    5426542 :         for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
    1988                 :    5365339 :                 uint32_t cluster_num = bs_lba_to_cluster(bs, blob->active.clusters[i]);
    1989                 :            : 
    1990                 :            :                 /* Nothing to release if it was not allocated */
    1991         [ +  + ]:    5365339 :                 if (blob->active.clusters[i] != 0) {
    1992                 :     532450 :                         bs_release_cluster(bs, cluster_num);
    1993                 :            :                 }
    1994                 :            :         }
    1995                 :      61210 :         spdk_spin_unlock(&bs->used_lock);
    1996                 :            : 
    1997         [ +  + ]:      61210 :         if (blob->active.num_clusters == 0) {
    1998                 :       9579 :                 free(blob->active.clusters);
    1999                 :       9579 :                 blob->active.clusters = NULL;
    2000                 :       9579 :                 blob->active.cluster_array_size = 0;
    2001         [ +  + ]:      51631 :         } else if (blob->active.num_clusters != blob->active.cluster_array_size) {
    2002                 :            : #ifndef __clang_analyzer__
    2003                 :            :                 void *tmp;
    2004                 :            : 
    2005                 :            :                 /* scan-build really can't figure reallocs, workaround it */
    2006                 :         59 :                 tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * blob->active.num_clusters);
    2007         [ -  + ]:         59 :                 assert(tmp != NULL);
    2008                 :         59 :                 blob->active.clusters = tmp;
    2009                 :            : 
    2010                 :            : #endif
    2011                 :         59 :                 blob->active.cluster_array_size = blob->active.num_clusters;
    2012                 :            :         }
    2013                 :            : 
    2014                 :            :         /* Move on to clearing extent pages */
    2015                 :      61210 :         blob_persist_clear_extents(seq, ctx);
    2016                 :            : }
    2017                 :            : 
    2018                 :            : static void
    2019                 :      61210 : blob_persist_clear_clusters(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
    2020                 :            : {
    2021                 :      61210 :         struct spdk_blob                *blob = ctx->blob;
    2022                 :      61210 :         struct spdk_blob_store          *bs = blob->bs;
    2023                 :            :         spdk_bs_batch_t                 *batch;
    2024                 :            :         size_t                          i;
    2025                 :            :         uint64_t                        lba;
    2026                 :            :         uint64_t                        lba_count;
    2027                 :            : 
    2028                 :            :         /* Clusters don't move around in blobs. The list shrinks or grows
    2029                 :            :          * at the end, but no changes ever occur in the middle of the list.
    2030                 :            :          */
    2031                 :            : 
    2032                 :      61210 :         batch = bs_sequence_to_batch(seq, blob_persist_clear_clusters_cpl, ctx);
    2033                 :            : 
    2034                 :            :         /* Clear all clusters that were truncated */
    2035                 :      61210 :         lba = 0;
    2036                 :      61210 :         lba_count = 0;
    2037         [ +  + ]:    5426542 :         for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
    2038                 :    5365339 :                 uint64_t next_lba = blob->active.clusters[i];
    2039                 :    5365339 :                 uint64_t next_lba_count = bs_cluster_to_lba(bs, 1);
    2040                 :            : 
    2041   [ +  +  +  + ]:    5365339 :                 if (next_lba > 0 && (lba + lba_count) == next_lba) {
    2042                 :            :                         /* This cluster is contiguous with the previous one. */
    2043                 :     525890 :                         lba_count += next_lba_count;
    2044                 :     525890 :                         continue;
    2045         [ +  + ]:    4839447 :                 } else if (next_lba == 0) {
    2046                 :    4832881 :                         continue;
    2047                 :            :                 }
    2048                 :            : 
    2049                 :            :                 /* This cluster is not contiguous with the previous one. */
    2050                 :            : 
    2051                 :            :                 /* If a run of LBAs previously existing, clear them now */
    2052         [ +  + ]:       6560 :                 if (lba_count > 0) {
    2053                 :       1051 :                         bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
    2054                 :            :                 }
    2055                 :            : 
    2056                 :            :                 /* Start building the next batch */
    2057                 :       6560 :                 lba = next_lba;
    2058         [ +  - ]:       6560 :                 if (next_lba > 0) {
    2059                 :       6560 :                         lba_count = next_lba_count;
    2060                 :            :                 } else {
    2061                 :          0 :                         lba_count = 0;
    2062                 :            :                 }
    2063                 :            :         }
    2064                 :            : 
    2065                 :            :         /* If we ended with a contiguous set of LBAs, clear them now */
    2066         [ +  + ]:      61210 :         if (lba_count > 0) {
    2067                 :       5509 :                 bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
    2068                 :            :         }
    2069                 :            : 
    2070                 :      61210 :         bs_batch_close(batch);
    2071                 :      61210 : }
    2072                 :            : 
    2073                 :            : static void
    2074                 :      61226 : blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2075                 :            : {
    2076                 :      61226 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2077                 :      61226 :         struct spdk_blob                *blob = ctx->blob;
    2078                 :      61226 :         struct spdk_blob_store          *bs = blob->bs;
    2079                 :            :         size_t                          i;
    2080                 :            : 
    2081         [ +  + ]:      61226 :         if (bserrno != 0) {
    2082                 :         16 :                 blob_persist_complete(seq, ctx, bserrno);
    2083                 :         16 :                 return;
    2084                 :            :         }
    2085                 :            : 
    2086                 :      61210 :         spdk_spin_lock(&bs->used_lock);
    2087                 :            : 
    2088                 :            :         /* This loop starts at 1 because the first page is special and handled
    2089                 :            :          * below. The pages (except the first) are never written in place,
    2090                 :            :          * so any pages in the clean list must be zeroed.
    2091                 :            :          */
    2092         [ +  + ]:      61483 :         for (i = 1; i < blob->clean.num_pages; i++) {
    2093                 :        273 :                 bs_release_md_page(bs, blob->clean.pages[i]);
    2094                 :            :         }
    2095                 :            : 
    2096         [ +  + ]:      61210 :         if (blob->active.num_pages == 0) {
    2097                 :            :                 uint32_t page_num;
    2098                 :            : 
    2099                 :       6700 :                 page_num = bs_blobid_to_page(blob->id);
    2100                 :       6700 :                 bs_release_md_page(bs, page_num);
    2101                 :            :         }
    2102                 :            : 
    2103                 :      61210 :         spdk_spin_unlock(&bs->used_lock);
    2104                 :            : 
    2105                 :            :         /* Move on to clearing clusters */
    2106                 :      61210 :         blob_persist_clear_clusters(seq, ctx);
    2107                 :            : }
    2108                 :            : 
    2109                 :            : static void
    2110                 :      61386 : blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2111                 :            : {
    2112                 :      61386 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2113                 :      61386 :         struct spdk_blob                *blob = ctx->blob;
    2114                 :      61386 :         struct spdk_blob_store          *bs = blob->bs;
    2115                 :            :         uint64_t                        lba;
    2116                 :            :         uint64_t                        lba_count;
    2117                 :            :         spdk_bs_batch_t                 *batch;
    2118                 :            :         size_t                          i;
    2119                 :            : 
    2120         [ +  + ]:      61386 :         if (bserrno != 0) {
    2121                 :        160 :                 blob_persist_complete(seq, ctx, bserrno);
    2122                 :        160 :                 return;
    2123                 :            :         }
    2124                 :            : 
    2125                 :      61226 :         batch = bs_sequence_to_batch(seq, blob_persist_zero_pages_cpl, ctx);
    2126                 :            : 
    2127                 :      61226 :         lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
    2128                 :            : 
    2129                 :            :         /* This loop starts at 1 because the first page is special and handled
    2130                 :            :          * below. The pages (except the first) are never written in place,
    2131                 :            :          * so any pages in the clean list must be zeroed.
    2132                 :            :          */
    2133         [ +  + ]:      61499 :         for (i = 1; i < blob->clean.num_pages; i++) {
    2134                 :        273 :                 lba = bs_md_page_to_lba(bs, blob->clean.pages[i]);
    2135                 :            : 
    2136                 :        273 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    2137                 :            :         }
    2138                 :            : 
    2139                 :            :         /* The first page will only be zeroed if this is a delete. */
    2140         [ +  + ]:      61226 :         if (blob->active.num_pages == 0) {
    2141                 :            :                 uint32_t page_num;
    2142                 :            : 
    2143                 :            :                 /* The first page in the metadata goes where the blobid indicates */
    2144                 :       6716 :                 page_num = bs_blobid_to_page(blob->id);
    2145                 :       6716 :                 lba = bs_md_page_to_lba(bs, page_num);
    2146                 :            : 
    2147                 :       6716 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    2148                 :            :         }
    2149                 :            : 
    2150                 :      61226 :         bs_batch_close(batch);
    2151                 :            : }
    2152                 :            : 
    2153                 :            : static void
    2154                 :      54670 : blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2155                 :            : {
    2156                 :      54670 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2157                 :      54670 :         struct spdk_blob                *blob = ctx->blob;
    2158                 :      54670 :         struct spdk_blob_store          *bs = blob->bs;
    2159                 :            :         uint64_t                        lba;
    2160                 :            :         uint32_t                        lba_count;
    2161                 :            :         struct spdk_blob_md_page        *page;
    2162                 :            : 
    2163         [ -  + ]:      54670 :         if (bserrno != 0) {
    2164                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    2165                 :          0 :                 return;
    2166                 :            :         }
    2167                 :            : 
    2168         [ -  + ]:      54670 :         if (blob->active.num_pages == 0) {
    2169                 :            :                 /* Move on to the next step */
    2170                 :          0 :                 blob_persist_zero_pages(seq, ctx, 0);
    2171                 :          0 :                 return;
    2172                 :            :         }
    2173                 :            : 
    2174                 :      54670 :         lba_count = bs_byte_to_lba(bs, sizeof(*page));
    2175                 :            : 
    2176                 :      54670 :         page = &ctx->pages[0];
    2177                 :            :         /* The first page in the metadata goes where the blobid indicates */
    2178                 :      54670 :         lba = bs_md_page_to_lba(bs, bs_blobid_to_page(blob->id));
    2179                 :            : 
    2180                 :      54670 :         bs_sequence_write_dev(seq, page, lba, lba_count,
    2181                 :            :                               blob_persist_zero_pages, ctx);
    2182                 :            : }
    2183                 :            : 
    2184                 :            : static void
    2185                 :      54670 : blob_persist_write_page_chain(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
    2186                 :            : {
    2187                 :      54670 :         struct spdk_blob                *blob = ctx->blob;
    2188                 :      54670 :         struct spdk_blob_store          *bs = blob->bs;
    2189                 :            :         uint64_t                        lba;
    2190                 :            :         uint32_t                        lba_count;
    2191                 :            :         struct spdk_blob_md_page        *page;
    2192                 :            :         spdk_bs_batch_t                 *batch;
    2193                 :            :         size_t                          i;
    2194                 :            : 
    2195                 :            :         /* Clusters don't move around in blobs. The list shrinks or grows
    2196                 :            :          * at the end, but no changes ever occur in the middle of the list.
    2197                 :            :          */
    2198                 :            : 
    2199                 :      54670 :         lba_count = bs_byte_to_lba(bs, sizeof(*page));
    2200                 :            : 
    2201                 :      54670 :         batch = bs_sequence_to_batch(seq, blob_persist_write_page_root, ctx);
    2202                 :            : 
    2203                 :            :         /* This starts at 1. The root page is not written until
    2204                 :            :          * all of the others are finished
    2205                 :            :          */
    2206         [ +  + ]:      55023 :         for (i = 1; i < blob->active.num_pages; i++) {
    2207                 :        353 :                 page = &ctx->pages[i];
    2208         [ -  + ]:        353 :                 assert(page->sequence_num == i);
    2209                 :            : 
    2210                 :        353 :                 lba = bs_md_page_to_lba(bs, blob->active.pages[i]);
    2211                 :            : 
    2212                 :        353 :                 bs_batch_write_dev(batch, page, lba, lba_count);
    2213                 :            :         }
    2214                 :            : 
    2215                 :      54670 :         bs_batch_close(batch);
    2216                 :      54670 : }
    2217                 :            : 
    2218                 :            : static int
    2219                 :      44026 : blob_resize(struct spdk_blob *blob, uint64_t sz)
    2220                 :            : {
    2221                 :            :         uint64_t        i;
    2222                 :            :         uint64_t        *tmp;
    2223                 :      43766 :         uint64_t        cluster;
    2224                 :      43766 :         uint32_t        lfmd; /*  lowest free md page */
    2225                 :            :         uint64_t        num_clusters;
    2226                 :            :         uint32_t        *ep_tmp;
    2227                 :      44026 :         uint64_t        new_num_ep = 0, current_num_ep = 0;
    2228                 :            :         struct spdk_blob_store *bs;
    2229                 :            :         int             rc;
    2230                 :            : 
    2231                 :      44026 :         bs = blob->bs;
    2232                 :            : 
    2233                 :      44026 :         blob_verify_md_op(blob);
    2234                 :            : 
    2235         [ +  + ]:      44026 :         if (blob->active.num_clusters == sz) {
    2236                 :       2829 :                 return 0;
    2237                 :            :         }
    2238                 :            : 
    2239         [ -  + ]:      41197 :         if (blob->active.num_clusters < blob->active.cluster_array_size) {
    2240                 :            :                 /* If this blob was resized to be larger, then smaller, then
    2241                 :            :                  * larger without syncing, then the cluster array already
    2242                 :            :                  * contains spare assigned clusters we can use.
    2243                 :            :                  */
    2244                 :          0 :                 num_clusters = spdk_min(blob->active.cluster_array_size,
    2245                 :            :                                         sz);
    2246                 :            :         } else {
    2247                 :      41197 :                 num_clusters = blob->active.num_clusters;
    2248                 :            :         }
    2249                 :            : 
    2250   [ +  +  +  + ]:      41197 :         if (blob->use_extent_table) {
    2251                 :            :                 /* Round up since every cluster beyond current Extent Table size,
    2252                 :            :                  * requires new extent page. */
    2253                 :      35061 :                 new_num_ep = spdk_divide_round_up(sz, SPDK_EXTENTS_PER_EP);
    2254                 :      35061 :                 current_num_ep = spdk_divide_round_up(num_clusters, SPDK_EXTENTS_PER_EP);
    2255                 :            :         }
    2256                 :            : 
    2257         [ -  + ]:      41197 :         assert(!spdk_spin_held(&bs->used_lock));
    2258                 :            : 
    2259                 :            :         /* Check first that we have enough clusters and md pages before we start claiming them.
    2260                 :            :          * bs->used_lock is held to ensure that clusters we think are free are still free when we go
    2261                 :            :          * to claim them later in this function.
    2262                 :            :          */
    2263   [ +  +  +  + ]:      41197 :         if (sz > num_clusters && spdk_blob_is_thin_provisioned(blob) == false) {
    2264                 :      33169 :                 spdk_spin_lock(&bs->used_lock);
    2265         [ +  + ]:      33169 :                 if ((sz - num_clusters) > bs->num_free_clusters) {
    2266                 :         35 :                         rc = -ENOSPC;
    2267                 :         35 :                         goto out;
    2268                 :            :                 }
    2269                 :      33134 :                 lfmd = 0;
    2270         [ +  + ]:      37570 :                 for (i = current_num_ep; i < new_num_ep ; i++) {
    2271                 :       4436 :                         lfmd = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, lfmd);
    2272         [ -  + ]:       4436 :                         if (lfmd == UINT32_MAX) {
    2273                 :            :                                 /* No more free md pages. Cannot satisfy the request */
    2274                 :          0 :                                 rc = -ENOSPC;
    2275                 :          0 :                                 goto out;
    2276                 :            :                         }
    2277                 :            :                 }
    2278                 :            :         }
    2279                 :            : 
    2280         [ +  + ]:      41162 :         if (sz > num_clusters) {
    2281                 :            :                 /* Expand the cluster array if necessary.
    2282                 :            :                  * We only shrink the array when persisting.
    2283                 :            :                  */
    2284                 :      34884 :                 tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * sz);
    2285   [ +  -  -  + ]:      34884 :                 if (sz > 0 && tmp == NULL) {
    2286                 :          0 :                         rc = -ENOMEM;
    2287                 :          0 :                         goto out;
    2288                 :            :                 }
    2289         [ -  + ]:      34884 :                 memset(tmp + blob->active.cluster_array_size, 0,
    2290                 :      34884 :                        sizeof(*blob->active.clusters) * (sz - blob->active.cluster_array_size));
    2291                 :      34884 :                 blob->active.clusters = tmp;
    2292                 :      34884 :                 blob->active.cluster_array_size = sz;
    2293                 :            : 
    2294                 :            :                 /* Expand the extents table, only if enough clusters were added */
    2295   [ +  +  +  +  :      34884 :                 if (new_num_ep > current_num_ep && blob->use_extent_table) {
                   +  - ]
    2296                 :       4431 :                         ep_tmp = realloc(blob->active.extent_pages, sizeof(*blob->active.extent_pages) * new_num_ep);
    2297   [ +  -  -  + ]:       4431 :                         if (new_num_ep > 0 && ep_tmp == NULL) {
    2298                 :          0 :                                 rc = -ENOMEM;
    2299                 :          0 :                                 goto out;
    2300                 :            :                         }
    2301         [ -  + ]:       4431 :                         memset(ep_tmp + blob->active.extent_pages_array_size, 0,
    2302                 :       4431 :                                sizeof(*blob->active.extent_pages) * (new_num_ep - blob->active.extent_pages_array_size));
    2303                 :       4431 :                         blob->active.extent_pages = ep_tmp;
    2304                 :       4431 :                         blob->active.extent_pages_array_size = new_num_ep;
    2305                 :            :                 }
    2306                 :            :         }
    2307                 :            : 
    2308                 :      41162 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    2309                 :            : 
    2310         [ +  + ]:      41162 :         if (spdk_blob_is_thin_provisioned(blob) == false) {
    2311                 :      38247 :                 cluster = 0;
    2312                 :      38247 :                 lfmd = 0;
    2313         [ +  + ]:     622644 :                 for (i = num_clusters; i < sz; i++) {
    2314                 :     584397 :                         bs_allocate_cluster(blob, i, &cluster, &lfmd, true);
    2315                 :            :                         /* Do not increment lfmd here.  lfmd will get updated
    2316                 :            :                          * to the md_page allocated (if any) when a new extent
    2317                 :            :                          * page is needed.  Just pass that value again,
    2318                 :            :                          * bs_allocate_cluster will just start at that index
    2319                 :            :                          * to find the next free md_page when needed.
    2320                 :            :                          */
    2321                 :            :                 }
    2322                 :            :         }
    2323                 :            : 
    2324                 :            :         /* If we are shrinking the blob, we must adjust num_allocated_clusters */
    2325         [ +  + ]:    5406661 :         for (i = sz; i < num_clusters; i++) {
    2326         [ +  + ]:    5365499 :                 if (blob->active.clusters[i] != 0) {
    2327                 :     532450 :                         blob->active.num_allocated_clusters--;
    2328                 :            :                 }
    2329                 :            :         }
    2330                 :            : 
    2331                 :      41162 :         blob->active.num_clusters = sz;
    2332                 :      41162 :         blob->active.num_extent_pages = new_num_ep;
    2333                 :            : 
    2334                 :      41162 :         rc = 0;
    2335                 :      41197 : out:
    2336         [ +  + ]:      41197 :         if (spdk_spin_held(&bs->used_lock)) {
    2337                 :      33169 :                 spdk_spin_unlock(&bs->used_lock);
    2338                 :            :         }
    2339                 :            : 
    2340                 :      41197 :         return rc;
    2341                 :            : }
    2342                 :            : 
    2343                 :            : static void
    2344                 :      54670 : blob_persist_generate_new_md(struct spdk_blob_persist_ctx *ctx)
    2345                 :            : {
    2346                 :      54670 :         spdk_bs_sequence_t *seq = ctx->seq;
    2347                 :      54670 :         struct spdk_blob *blob = ctx->blob;
    2348                 :      54670 :         struct spdk_blob_store *bs = blob->bs;
    2349                 :            :         uint64_t i;
    2350                 :            :         uint32_t page_num;
    2351                 :            :         void *tmp;
    2352                 :            :         int rc;
    2353                 :            : 
    2354                 :            :         /* Generate the new metadata */
    2355                 :      54670 :         rc = blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
    2356         [ -  + ]:      54670 :         if (rc < 0) {
    2357                 :          0 :                 blob_persist_complete(seq, ctx, rc);
    2358                 :          0 :                 return;
    2359                 :            :         }
    2360                 :            : 
    2361         [ -  + ]:      54670 :         assert(blob->active.num_pages >= 1);
    2362                 :            : 
    2363                 :            :         /* Resize the cache of page indices */
    2364                 :      54670 :         tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
    2365         [ -  + ]:      54670 :         if (!tmp) {
    2366                 :          0 :                 blob_persist_complete(seq, ctx, -ENOMEM);
    2367                 :          0 :                 return;
    2368                 :            :         }
    2369                 :      54670 :         blob->active.pages = tmp;
    2370                 :            : 
    2371                 :            :         /* Assign this metadata to pages. This requires two passes - one to verify that there are
    2372                 :            :          * enough pages and a second to actually claim them. The used_lock is held across
    2373                 :            :          * both passes to ensure things don't change in the middle.
    2374                 :            :          */
    2375                 :      54670 :         spdk_spin_lock(&bs->used_lock);
    2376                 :      54670 :         page_num = 0;
    2377                 :            :         /* Note that this loop starts at one. The first page location is fixed by the blobid. */
    2378         [ +  + ]:      55023 :         for (i = 1; i < blob->active.num_pages; i++) {
    2379                 :        353 :                 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
    2380         [ -  + ]:        353 :                 if (page_num == UINT32_MAX) {
    2381                 :          0 :                         spdk_spin_unlock(&bs->used_lock);
    2382                 :          0 :                         blob_persist_complete(seq, ctx, -ENOMEM);
    2383                 :          0 :                         return;
    2384                 :            :                 }
    2385                 :        353 :                 page_num++;
    2386                 :            :         }
    2387                 :            : 
    2388                 :      54670 :         page_num = 0;
    2389                 :      54670 :         blob->active.pages[0] = bs_blobid_to_page(blob->id);
    2390         [ +  + ]:      55023 :         for (i = 1; i < blob->active.num_pages; i++) {
    2391                 :        353 :                 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
    2392                 :        353 :                 ctx->pages[i - 1].next = page_num;
    2393                 :            :                 /* Now that previous metadata page is complete, calculate the crc for it. */
    2394                 :        353 :                 ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
    2395                 :        353 :                 blob->active.pages[i] = page_num;
    2396                 :        353 :                 bs_claim_md_page(bs, page_num);
    2397   [ -  +  -  + ]:        353 :                 SPDK_DEBUGLOG(blob, "Claiming page %u for blob 0x%" PRIx64 "\n", page_num,
    2398                 :            :                               blob->id);
    2399                 :        353 :                 page_num++;
    2400                 :            :         }
    2401                 :      54670 :         spdk_spin_unlock(&bs->used_lock);
    2402                 :      54670 :         ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
    2403                 :            :         /* Start writing the metadata from last page to first */
    2404                 :      54670 :         blob->state = SPDK_BLOB_STATE_CLEAN;
    2405                 :      54670 :         blob_persist_write_page_chain(seq, ctx);
    2406                 :            : }
    2407                 :            : 
    2408                 :            : static void
    2409                 :      66368 : blob_persist_write_extent_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2410                 :            : {
    2411                 :      66368 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2412                 :      66368 :         struct spdk_blob                *blob = ctx->blob;
    2413                 :            :         size_t                          i;
    2414                 :            :         uint32_t                        extent_page_id;
    2415                 :      66368 :         uint32_t                        page_count = 0;
    2416                 :            :         int                             rc;
    2417                 :            : 
    2418         [ +  + ]:      66368 :         if (ctx->extent_page != NULL) {
    2419                 :      31535 :                 spdk_free(ctx->extent_page);
    2420                 :      31535 :                 ctx->extent_page = NULL;
    2421                 :            :         }
    2422                 :            : 
    2423         [ -  + ]:      66368 :         if (bserrno != 0) {
    2424                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    2425                 :         90 :                 return;
    2426                 :            :         }
    2427                 :            : 
    2428                 :            :         /* Only write out Extent Pages when blob was resized. */
    2429         [ +  + ]:      76602 :         for (i = ctx->next_extent_page; i < blob->active.extent_pages_array_size; i++) {
    2430                 :      41769 :                 extent_page_id = blob->active.extent_pages[i];
    2431         [ +  + ]:      41769 :                 if (extent_page_id == 0) {
    2432                 :            :                         /* No Extent Page to persist */
    2433         [ -  + ]:      10234 :                         assert(spdk_blob_is_thin_provisioned(blob));
    2434                 :      10234 :                         continue;
    2435                 :            :                 }
    2436         [ -  + ]:      31535 :                 assert(spdk_bit_array_get(blob->bs->used_md_pages, extent_page_id));
    2437                 :      31535 :                 ctx->next_extent_page = i + 1;
    2438                 :      31535 :                 rc = blob_serialize_add_page(ctx->blob, &ctx->extent_page, &page_count, &ctx->extent_page);
    2439         [ -  + ]:      31535 :                 if (rc < 0) {
    2440                 :          0 :                         blob_persist_complete(seq, ctx, rc);
    2441                 :          0 :                         return;
    2442                 :            :                 }
    2443                 :            : 
    2444                 :      31535 :                 blob->state = SPDK_BLOB_STATE_DIRTY;
    2445                 :      31535 :                 blob_serialize_extent_page(blob, i * SPDK_EXTENTS_PER_EP, ctx->extent_page);
    2446                 :            : 
    2447                 :      31535 :                 ctx->extent_page->crc = blob_md_page_calc_crc(ctx->extent_page);
    2448                 :            : 
    2449                 :      31535 :                 bs_sequence_write_dev(seq, ctx->extent_page, bs_md_page_to_lba(blob->bs, extent_page_id),
    2450                 :      31535 :                                       bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
    2451                 :            :                                       blob_persist_write_extent_pages, ctx);
    2452                 :      31535 :                 return;
    2453                 :            :         }
    2454                 :            : 
    2455                 :      34833 :         blob_persist_generate_new_md(ctx);
    2456                 :            : }
    2457                 :            : 
    2458                 :            : static void
    2459                 :      61418 : blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2460                 :            : {
    2461                 :      61418 :         struct spdk_blob_persist_ctx *ctx = cb_arg;
    2462                 :      61418 :         struct spdk_blob *blob = ctx->blob;
    2463                 :            : 
    2464         [ +  + ]:      61418 :         if (bserrno != 0) {
    2465                 :         32 :                 blob_persist_complete(seq, ctx, bserrno);
    2466                 :         32 :                 return;
    2467                 :            :         }
    2468                 :            : 
    2469         [ +  + ]:      61386 :         if (blob->active.num_pages == 0) {
    2470                 :            :                 /* This is the signal that the blob should be deleted.
    2471                 :            :                  * Immediately jump to the clean up routine. */
    2472         [ -  + ]:       6716 :                 assert(blob->clean.num_pages > 0);
    2473                 :       6716 :                 blob->state = SPDK_BLOB_STATE_CLEAN;
    2474                 :       6716 :                 blob_persist_zero_pages(seq, ctx, 0);
    2475                 :       6716 :                 return;
    2476                 :            : 
    2477                 :            :         }
    2478                 :            : 
    2479         [ +  + ]:      54670 :         if (blob->clean.num_clusters < blob->active.num_clusters) {
    2480                 :            :                 /* Blob was resized up */
    2481         [ -  + ]:      34772 :                 assert(blob->clean.num_extent_pages <= blob->active.num_extent_pages);
    2482         [ +  + ]:      34772 :                 ctx->next_extent_page = spdk_max(1, blob->clean.num_extent_pages) - 1;
    2483         [ +  + ]:      19898 :         } else if (blob->active.num_clusters < blob->active.cluster_array_size) {
    2484                 :            :                 /* Blob was resized down */
    2485         [ -  + ]:         61 :                 assert(blob->clean.num_extent_pages >= blob->active.num_extent_pages);
    2486         [ +  + ]:         61 :                 ctx->next_extent_page = spdk_max(1, blob->active.num_extent_pages) - 1;
    2487                 :            :         } else {
    2488                 :            :                 /* No change in size occurred */
    2489                 :      19837 :                 blob_persist_generate_new_md(ctx);
    2490                 :      19837 :                 return;
    2491                 :            :         }
    2492                 :            : 
    2493                 :      34833 :         blob_persist_write_extent_pages(seq, ctx, 0);
    2494                 :            : }
    2495                 :            : 
    2496                 :            : struct spdk_bs_mark_dirty {
    2497                 :            :         struct spdk_blob_store          *bs;
    2498                 :            :         struct spdk_bs_super_block      *super;
    2499                 :            :         spdk_bs_sequence_cpl            cb_fn;
    2500                 :            :         void                            *cb_arg;
    2501                 :            : };
    2502                 :            : 
    2503                 :            : static void
    2504                 :        676 : bs_mark_dirty_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2505                 :            : {
    2506                 :        676 :         struct spdk_bs_mark_dirty *ctx = cb_arg;
    2507                 :            : 
    2508         [ +  + ]:        676 :         if (bserrno == 0) {
    2509                 :        644 :                 ctx->bs->clean = 0;
    2510                 :            :         }
    2511                 :            : 
    2512                 :        676 :         ctx->cb_fn(seq, ctx->cb_arg, bserrno);
    2513                 :            : 
    2514                 :        676 :         spdk_free(ctx->super);
    2515                 :        676 :         free(ctx);
    2516                 :        676 : }
    2517                 :            : 
    2518                 :            : static void bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    2519                 :            :                            struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg);
    2520                 :            : 
    2521                 :            : 
    2522                 :            : static void
    2523                 :        676 : bs_mark_dirty_write(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2524                 :            : {
    2525                 :        676 :         struct spdk_bs_mark_dirty *ctx = cb_arg;
    2526                 :            :         int rc;
    2527                 :            : 
    2528         [ +  + ]:        676 :         if (bserrno != 0) {
    2529                 :         16 :                 bs_mark_dirty_write_cpl(seq, ctx, bserrno);
    2530                 :         16 :                 return;
    2531                 :            :         }
    2532                 :            : 
    2533                 :        660 :         rc = bs_super_validate(ctx->super, ctx->bs);
    2534         [ -  + ]:        660 :         if (rc != 0) {
    2535                 :          0 :                 bs_mark_dirty_write_cpl(seq, ctx, rc);
    2536                 :          0 :                 return;
    2537                 :            :         }
    2538                 :            : 
    2539                 :        660 :         ctx->super->clean = 0;
    2540         [ +  + ]:        660 :         if (ctx->super->size == 0) {
    2541                 :         16 :                 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    2542                 :            :         }
    2543                 :            : 
    2544                 :        660 :         bs_write_super(seq, ctx->bs, ctx->super, bs_mark_dirty_write_cpl, ctx);
    2545                 :            : }
    2546                 :            : 
    2547                 :            : static void
    2548                 :      64930 : bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    2549                 :            :               spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    2550                 :            : {
    2551                 :            :         struct spdk_bs_mark_dirty *ctx;
    2552                 :            : 
    2553                 :            :         /* Blobstore is already marked dirty */
    2554   [ +  +  +  + ]:      64930 :         if (bs->clean == 0) {
    2555                 :      64254 :                 cb_fn(seq, cb_arg, 0);
    2556                 :      64254 :                 return;
    2557                 :            :         }
    2558                 :            : 
    2559                 :        676 :         ctx = calloc(1, sizeof(*ctx));
    2560         [ -  + ]:        676 :         if (!ctx) {
    2561                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    2562                 :          0 :                 return;
    2563                 :            :         }
    2564                 :        676 :         ctx->bs = bs;
    2565                 :        676 :         ctx->cb_fn = cb_fn;
    2566                 :        676 :         ctx->cb_arg = cb_arg;
    2567                 :            : 
    2568                 :        676 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    2569                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    2570         [ -  + ]:        676 :         if (!ctx->super) {
    2571                 :          0 :                 free(ctx);
    2572                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    2573                 :          0 :                 return;
    2574                 :            :         }
    2575                 :            : 
    2576                 :        676 :         bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
    2577                 :        676 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    2578                 :            :                              bs_mark_dirty_write, ctx);
    2579                 :            : }
    2580                 :            : 
    2581                 :            : /* Write a blob to disk */
    2582                 :            : static void
    2583                 :      83441 : blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
    2584                 :            :              spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    2585                 :            : {
    2586                 :            :         struct spdk_blob_persist_ctx *ctx;
    2587                 :            : 
    2588                 :      83441 :         blob_verify_md_op(blob);
    2589                 :            : 
    2590   [ +  +  +  + ]:      83441 :         if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->persists_to_complete)) {
    2591                 :      22023 :                 cb_fn(seq, cb_arg, 0);
    2592                 :      22023 :                 return;
    2593                 :            :         }
    2594                 :            : 
    2595                 :      61418 :         ctx = calloc(1, sizeof(*ctx));
    2596         [ -  + ]:      61418 :         if (!ctx) {
    2597                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    2598                 :          0 :                 return;
    2599                 :            :         }
    2600                 :      61418 :         ctx->blob = blob;
    2601                 :      61418 :         ctx->seq = seq;
    2602                 :      61418 :         ctx->cb_fn = cb_fn;
    2603                 :      61418 :         ctx->cb_arg = cb_arg;
    2604                 :            : 
    2605                 :            :         /* Multiple blob persists can affect one another, via blob->state or
    2606                 :            :          * blob mutable data changes. To prevent it, queue up the persists. */
    2607         [ +  + ]:      61418 :         if (!TAILQ_EMPTY(&blob->persists_to_complete)) {
    2608                 :         93 :                 TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link);
    2609                 :         93 :                 return;
    2610                 :            :         }
    2611         [ -  + ]:      61325 :         TAILQ_INSERT_HEAD(&blob->persists_to_complete, ctx, link);
    2612                 :            : 
    2613                 :      61325 :         bs_mark_dirty(seq, blob->bs, blob_persist_start, ctx);
    2614                 :            : }
    2615                 :            : 
    2616                 :            : struct spdk_blob_copy_cluster_ctx {
    2617                 :            :         struct spdk_blob *blob;
    2618                 :            :         uint8_t *buf;
    2619                 :            :         uint64_t page;
    2620                 :            :         uint64_t new_cluster;
    2621                 :            :         uint32_t new_extent_page;
    2622                 :            :         spdk_bs_sequence_t *seq;
    2623                 :            :         struct spdk_blob_md_page *new_cluster_page;
    2624                 :            : };
    2625                 :            : 
    2626                 :            : struct spdk_blob_free_cluster_ctx {
    2627                 :            :         struct spdk_blob *blob;
    2628                 :            :         uint64_t page;
    2629                 :            :         struct spdk_blob_md_page *md_page;
    2630                 :            :         uint64_t cluster_num;
    2631                 :            :         uint32_t extent_page;
    2632                 :            :         spdk_bs_sequence_t *seq;
    2633                 :            : };
    2634                 :            : 
    2635                 :            : static void
    2636                 :       5026 : blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno)
    2637                 :            : {
    2638                 :       5026 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2639                 :       5026 :         struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq;
    2640                 :       4902 :         TAILQ_HEAD(, spdk_bs_request_set) requests;
    2641                 :            :         spdk_bs_user_op_t *op;
    2642                 :            : 
    2643                 :       5026 :         TAILQ_INIT(&requests);
    2644   [ -  +  +  - ]:       5026 :         TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link);
    2645                 :            : 
    2646         [ +  + ]:      22987 :         while (!TAILQ_EMPTY(&requests)) {
    2647                 :      17961 :                 op = TAILQ_FIRST(&requests);
    2648         [ +  + ]:      17961 :                 TAILQ_REMOVE(&requests, op, link);
    2649         [ +  - ]:      17961 :                 if (bserrno == 0) {
    2650                 :      17961 :                         bs_user_op_execute(op);
    2651                 :            :                 } else {
    2652                 :          0 :                         bs_user_op_abort(op, bserrno);
    2653                 :            :                 }
    2654                 :            :         }
    2655                 :            : 
    2656                 :       5026 :         spdk_free(ctx->buf);
    2657                 :       5026 :         free(ctx);
    2658                 :       5026 : }
    2659                 :            : 
    2660                 :            : static void
    2661                 :        240 : blob_free_cluster_cpl(void *cb_arg, int bserrno)
    2662                 :            : {
    2663                 :        240 :         struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
    2664                 :        240 :         spdk_bs_sequence_t *seq = ctx->seq;
    2665                 :            : 
    2666                 :        240 :         bs_sequence_finish(seq, bserrno);
    2667                 :            : 
    2668                 :        240 :         free(ctx);
    2669                 :        240 : }
    2670                 :            : 
    2671                 :            : static void
    2672                 :         22 : blob_insert_cluster_revert(struct spdk_blob_copy_cluster_ctx *ctx)
    2673                 :            : {
    2674                 :         22 :         spdk_spin_lock(&ctx->blob->bs->used_lock);
    2675                 :         22 :         bs_release_cluster(ctx->blob->bs, ctx->new_cluster);
    2676         [ +  + ]:         22 :         if (ctx->new_extent_page != 0) {
    2677                 :          8 :                 bs_release_md_page(ctx->blob->bs, ctx->new_extent_page);
    2678                 :            :         }
    2679                 :         22 :         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    2680                 :         22 : }
    2681                 :            : 
    2682                 :            : static void
    2683                 :         22 : blob_insert_cluster_clear_cpl(void *cb_arg, int bserrno)
    2684                 :            : {
    2685                 :         22 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2686                 :            : 
    2687         [ -  + ]:         22 :         if (bserrno) {
    2688                 :          0 :                 SPDK_WARNLOG("Failed to clear cluster: %d\n", bserrno);
    2689                 :            :         }
    2690                 :            : 
    2691                 :         22 :         blob_insert_cluster_revert(ctx);
    2692                 :         22 :         bs_sequence_finish(ctx->seq, bserrno);
    2693                 :         22 : }
    2694                 :            : 
    2695                 :            : static void
    2696                 :         22 : blob_insert_cluster_clear(struct spdk_blob_copy_cluster_ctx *ctx)
    2697                 :            : {
    2698                 :         16 :         struct spdk_bs_cpl cpl;
    2699                 :            :         spdk_bs_batch_t *batch;
    2700                 :         22 :         struct spdk_io_channel *ch = spdk_io_channel_from_ctx(ctx->seq->channel);
    2701                 :            : 
    2702                 :            :         /*
    2703                 :            :          * We allocated a cluster and we copied data to it. But now, we realized that we don't need
    2704                 :            :          * this cluster and we want to release it. We must ensure that we clear the data on this
    2705                 :            :          * cluster.
    2706                 :            :          * The cluster may later be re-allocated by a thick-provisioned blob for example. When
    2707                 :            :          * reading from this thick-provisioned blob before writing data, we should read zeroes.
    2708                 :            :          */
    2709                 :            : 
    2710                 :         22 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    2711                 :         22 :         cpl.u.blob_basic.cb_fn = blob_insert_cluster_clear_cpl;
    2712                 :         22 :         cpl.u.blob_basic.cb_arg = ctx;
    2713                 :            : 
    2714                 :         22 :         batch = bs_batch_open(ch, &cpl, ctx->blob);
    2715         [ -  + ]:         22 :         if (!batch) {
    2716                 :          0 :                 blob_insert_cluster_clear_cpl(ctx, -ENOMEM);
    2717                 :          0 :                 return;
    2718                 :            :         }
    2719                 :            : 
    2720                 :         22 :         bs_batch_clear_dev(ctx->blob, batch, bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
    2721                 :         22 :                            bs_cluster_to_lba(ctx->blob->bs, 1));
    2722                 :         22 :         bs_batch_close(batch);
    2723                 :            : }
    2724                 :            : 
    2725                 :            : static void
    2726                 :       5026 : blob_insert_cluster_cpl(void *cb_arg, int bserrno)
    2727                 :            : {
    2728                 :       5026 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2729                 :            : 
    2730         [ +  + ]:       5026 :         if (bserrno) {
    2731         [ +  - ]:         22 :                 if (bserrno == -EEXIST) {
    2732                 :            :                         /* The metadata insert failed because another thread
    2733                 :            :                          * allocated the cluster first. Clear and free our cluster
    2734                 :            :                          * but continue without error. */
    2735                 :         22 :                         blob_insert_cluster_clear(ctx);
    2736                 :         22 :                         return;
    2737                 :            :                 }
    2738                 :            : 
    2739                 :          0 :                 blob_insert_cluster_revert(ctx);
    2740                 :            :         }
    2741                 :            : 
    2742                 :       5004 :         bs_sequence_finish(ctx->seq, bserrno);
    2743                 :            : }
    2744                 :            : 
    2745                 :            : static void
    2746                 :       1740 : blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2747                 :            : {
    2748                 :       1740 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2749                 :            :         uint32_t cluster_number;
    2750                 :            : 
    2751         [ -  + ]:       1740 :         if (bserrno) {
    2752                 :            :                 /* The write failed, so jump to the final completion handler */
    2753                 :          0 :                 bs_sequence_finish(seq, bserrno);
    2754                 :          0 :                 return;
    2755                 :            :         }
    2756                 :            : 
    2757                 :       1740 :         cluster_number = bs_page_to_cluster(ctx->blob->bs, ctx->page);
    2758                 :            : 
    2759                 :       1740 :         blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
    2760                 :            :                                          ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
    2761                 :            : }
    2762                 :            : 
    2763                 :            : static void
    2764                 :       1174 : blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2765                 :            : {
    2766                 :       1174 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2767                 :            : 
    2768         [ -  + ]:       1174 :         if (bserrno != 0) {
    2769                 :            :                 /* The read failed, so jump to the final completion handler */
    2770                 :          0 :                 bs_sequence_finish(seq, bserrno);
    2771                 :          0 :                 return;
    2772                 :            :         }
    2773                 :            : 
    2774                 :            :         /* Write whole cluster */
    2775                 :       1174 :         bs_sequence_write_dev(seq, ctx->buf,
    2776                 :       1174 :                               bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
    2777                 :       1174 :                               bs_cluster_to_lba(ctx->blob->bs, 1),
    2778                 :            :                               blob_write_copy_cpl, ctx);
    2779                 :            : }
    2780                 :            : 
    2781                 :            : static bool
    2782                 :       4963 : blob_can_copy(struct spdk_blob *blob, uint32_t cluster_start_page, uint64_t *base_lba)
    2783                 :            : {
    2784                 :       4963 :         uint64_t lba = bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page);
    2785                 :            : 
    2786   [ +  +  +  +  :       7774 :         return (!blob_is_esnap_clone(blob) && blob->bs->dev->copy != NULL) &&
                   +  + ]
    2787                 :       2811 :                blob->back_bs_dev->translate_lba(blob->back_bs_dev, lba, base_lba);
    2788                 :            : }
    2789                 :            : 
    2790                 :            : static void
    2791                 :        566 : blob_copy(struct spdk_blob_copy_cluster_ctx *ctx, spdk_bs_user_op_t *op, uint64_t src_lba)
    2792                 :            : {
    2793                 :        566 :         struct spdk_blob *blob = ctx->blob;
    2794                 :        566 :         uint64_t lba_count = bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz);
    2795                 :            : 
    2796                 :        566 :         bs_sequence_copy_dev(ctx->seq,
    2797                 :        566 :                              bs_cluster_to_lba(blob->bs, ctx->new_cluster),
    2798                 :            :                              src_lba,
    2799                 :            :                              lba_count,
    2800                 :            :                              blob_write_copy_cpl, ctx);
    2801                 :        566 : }
    2802                 :            : 
    2803                 :            : static void
    2804                 :      17962 : bs_allocate_and_copy_cluster(struct spdk_blob *blob,
    2805                 :            :                              struct spdk_io_channel *_ch,
    2806                 :            :                              uint64_t io_unit, spdk_bs_user_op_t *op)
    2807                 :            : {
    2808                 :      14986 :         struct spdk_bs_cpl cpl;
    2809                 :            :         struct spdk_bs_channel *ch;
    2810                 :            :         struct spdk_blob_copy_cluster_ctx *ctx;
    2811                 :            :         uint32_t cluster_start_page;
    2812                 :            :         uint32_t cluster_number;
    2813                 :            :         bool is_zeroes;
    2814                 :            :         bool can_copy;
    2815                 :            :         bool is_valid_range;
    2816                 :      14986 :         uint64_t copy_src_lba;
    2817                 :            :         int rc;
    2818                 :            : 
    2819                 :      17962 :         ch = spdk_io_channel_get_ctx(_ch);
    2820                 :            : 
    2821         [ +  + ]:      17962 :         if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) {
    2822                 :            :                 /* There are already operations pending. Queue this user op
    2823                 :            :                  * and return because it will be re-executed when the outstanding
    2824                 :            :                  * cluster allocation completes. */
    2825                 :      12935 :                 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
    2826                 :      12935 :                 return;
    2827                 :            :         }
    2828                 :            : 
    2829                 :            :         /* Round the io_unit offset down to the first page in the cluster */
    2830                 :       5027 :         cluster_start_page = bs_io_unit_to_cluster_start(blob, io_unit);
    2831                 :            : 
    2832                 :            :         /* Calculate which index in the metadata cluster array the corresponding
    2833                 :            :          * cluster is supposed to be at. */
    2834                 :       5027 :         cluster_number = bs_io_unit_to_cluster_number(blob, io_unit);
    2835                 :            : 
    2836                 :       5027 :         ctx = calloc(1, sizeof(*ctx));
    2837         [ -  + ]:       5027 :         if (!ctx) {
    2838                 :          0 :                 bs_user_op_abort(op, -ENOMEM);
    2839                 :          0 :                 return;
    2840                 :            :         }
    2841                 :            : 
    2842   [ -  +  -  + ]:       5027 :         assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0);
    2843                 :            : 
    2844                 :       5027 :         ctx->blob = blob;
    2845                 :       5027 :         ctx->page = cluster_start_page;
    2846                 :       5027 :         ctx->new_cluster_page = ch->new_cluster_page;
    2847         [ -  + ]:       5027 :         memset(ctx->new_cluster_page, 0, SPDK_BS_PAGE_SIZE);
    2848                 :            : 
    2849                 :            :         /* Check if the cluster that we intend to do CoW for is valid for
    2850                 :            :          * the backing dev. For zeroes backing dev, it'll be always valid.
    2851                 :            :          * For other backing dev e.g. a snapshot, it could be invalid if
    2852                 :            :          * the blob has been resized after snapshot was taken. */
    2853                 :       8424 :         is_valid_range = blob->back_bs_dev->is_range_valid(blob->back_bs_dev,
    2854                 :            :                          bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
    2855                 :       5027 :                          bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
    2856                 :            : 
    2857   [ +  +  +  + ]:       5027 :         can_copy = is_valid_range && blob_can_copy(blob, cluster_start_page, &copy_src_lba);
    2858                 :            : 
    2859   [ +  +  +  + ]:       9990 :         is_zeroes = is_valid_range && blob->back_bs_dev->is_zeroes(blob->back_bs_dev,
    2860                 :            :                         bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
    2861                 :       4963 :                         bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
    2862   [ +  +  +  +  :       5027 :         if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes && !can_copy) {
                   +  + ]
    2863                 :       1174 :                 ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen,
    2864                 :            :                                        NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    2865         [ -  + ]:       1174 :                 if (!ctx->buf) {
    2866                 :          0 :                         SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n",
    2867                 :            :                                     blob->bs->cluster_sz);
    2868                 :          0 :                         free(ctx);
    2869                 :          0 :                         bs_user_op_abort(op, -ENOMEM);
    2870                 :          0 :                         return;
    2871                 :            :                 }
    2872                 :            :         }
    2873                 :            : 
    2874                 :       5027 :         spdk_spin_lock(&blob->bs->used_lock);
    2875                 :       5027 :         rc = bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page,
    2876                 :            :                                  false);
    2877                 :       5027 :         spdk_spin_unlock(&blob->bs->used_lock);
    2878         [ +  + ]:       5027 :         if (rc != 0) {
    2879                 :          1 :                 spdk_free(ctx->buf);
    2880                 :          1 :                 free(ctx);
    2881                 :          1 :                 bs_user_op_abort(op, rc);
    2882                 :          1 :                 return;
    2883                 :            :         }
    2884                 :            : 
    2885                 :       5026 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    2886                 :       5026 :         cpl.u.blob_basic.cb_fn = blob_allocate_and_copy_cluster_cpl;
    2887                 :       5026 :         cpl.u.blob_basic.cb_arg = ctx;
    2888                 :            : 
    2889                 :       5026 :         ctx->seq = bs_sequence_start_blob(_ch, &cpl, blob);
    2890         [ -  + ]:       5026 :         if (!ctx->seq) {
    2891                 :          0 :                 spdk_spin_lock(&blob->bs->used_lock);
    2892                 :          0 :                 bs_release_cluster(blob->bs, ctx->new_cluster);
    2893                 :          0 :                 spdk_spin_unlock(&blob->bs->used_lock);
    2894                 :          0 :                 spdk_free(ctx->buf);
    2895                 :          0 :                 free(ctx);
    2896                 :          0 :                 bs_user_op_abort(op, -ENOMEM);
    2897                 :          0 :                 return;
    2898                 :            :         }
    2899                 :            : 
    2900                 :            :         /* Queue the user op to block other incoming operations */
    2901                 :       5026 :         TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
    2902                 :            : 
    2903   [ +  +  +  + ]:       5026 :         if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes) {
    2904         [ +  + ]:       1740 :                 if (can_copy) {
    2905                 :        566 :                         blob_copy(ctx, op, copy_src_lba);
    2906                 :            :                 } else {
    2907                 :            :                         /* Read cluster from backing device */
    2908                 :       1174 :                         bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf,
    2909                 :            :                                                 bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
    2910                 :       1174 :                                                 bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz),
    2911                 :            :                                                 blob_write_copy, ctx);
    2912                 :            :                 }
    2913                 :            : 
    2914                 :            :         } else {
    2915                 :       3286 :                 blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
    2916                 :            :                                                  ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
    2917                 :            :         }
    2918                 :            : }
    2919                 :            : 
    2920                 :            : static inline bool
    2921                 :    6735080 : blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length,
    2922                 :            :                                  uint64_t *lba, uint64_t *lba_count)
    2923                 :            : {
    2924                 :    6735080 :         *lba_count = length;
    2925                 :            : 
    2926         [ +  + ]:    6735080 :         if (!bs_io_unit_is_allocated(blob, io_unit)) {
    2927         [ -  + ]:     822943 :                 assert(blob->back_bs_dev != NULL);
    2928                 :     822943 :                 *lba = bs_io_unit_to_back_dev_lba(blob, io_unit);
    2929                 :     822943 :                 *lba_count = bs_io_unit_to_back_dev_lba(blob, *lba_count);
    2930                 :     822943 :                 return false;
    2931                 :            :         } else {
    2932                 :    5912137 :                 *lba = bs_blob_io_unit_to_lba(blob, io_unit);
    2933                 :    5912137 :                 return true;
    2934                 :            :         }
    2935                 :            : }
    2936                 :            : 
    2937                 :            : struct op_split_ctx {
    2938                 :            :         struct spdk_blob *blob;
    2939                 :            :         struct spdk_io_channel *channel;
    2940                 :            :         uint64_t io_unit_offset;
    2941                 :            :         uint64_t io_units_remaining;
    2942                 :            :         void *curr_payload;
    2943                 :            :         enum spdk_blob_op_type op_type;
    2944                 :            :         spdk_bs_sequence_t *seq;
    2945                 :            :         bool in_submit_ctx;
    2946                 :            :         bool completed_in_submit_ctx;
    2947                 :            :         bool done;
    2948                 :            : };
    2949                 :            : 
    2950                 :            : static void
    2951                 :      10011 : blob_request_submit_op_split_next(void *cb_arg, int bserrno)
    2952                 :            : {
    2953                 :      10011 :         struct op_split_ctx     *ctx = cb_arg;
    2954                 :      10011 :         struct spdk_blob        *blob = ctx->blob;
    2955                 :      10011 :         struct spdk_io_channel  *ch = ctx->channel;
    2956                 :      10011 :         enum spdk_blob_op_type  op_type = ctx->op_type;
    2957                 :            :         uint8_t                 *buf;
    2958                 :            :         uint64_t                offset;
    2959                 :            :         uint64_t                length;
    2960                 :            :         uint64_t                op_length;
    2961                 :            : 
    2962   [ +  -  +  + ]:      10011 :         if (bserrno != 0 || ctx->io_units_remaining == 0) {
    2963                 :        746 :                 bs_sequence_finish(ctx->seq, bserrno);
    2964   [ +  +  +  + ]:        746 :                 if (ctx->in_submit_ctx) {
    2965                 :            :                         /* Defer freeing of the ctx object, since it will be
    2966                 :            :                          * accessed when this unwinds back to the submisison
    2967                 :            :                          * context.
    2968                 :            :                          */
    2969                 :        160 :                         ctx->done = true;
    2970                 :            :                 } else {
    2971                 :        586 :                         free(ctx);
    2972                 :            :                 }
    2973                 :        746 :                 return;
    2974                 :            :         }
    2975                 :            : 
    2976   [ +  +  +  + ]:       9265 :         if (ctx->in_submit_ctx) {
    2977                 :            :                 /* If this split operation completed in the context
    2978                 :            :                  * of its submission, mark the flag and return immediately
    2979                 :            :                  * to avoid recursion.
    2980                 :            :                  */
    2981                 :        272 :                 ctx->completed_in_submit_ctx = true;
    2982                 :        272 :                 return;
    2983                 :            :         }
    2984                 :            : 
    2985                 :            :         while (true) {
    2986                 :       9265 :                 ctx->completed_in_submit_ctx = false;
    2987                 :            : 
    2988                 :       9265 :                 offset = ctx->io_unit_offset;
    2989                 :       9265 :                 length = ctx->io_units_remaining;
    2990                 :       9265 :                 buf = ctx->curr_payload;
    2991         [ +  + ]:       9265 :                 op_length = spdk_min(length, bs_num_io_units_to_cluster_boundary(blob,
    2992                 :            :                                      offset));
    2993                 :            : 
    2994                 :            :                 /* Update length and payload for next operation */
    2995                 :       9265 :                 ctx->io_units_remaining -= op_length;
    2996                 :       9265 :                 ctx->io_unit_offset += op_length;
    2997   [ +  +  +  + ]:       9265 :                 if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) {
    2998                 :       2144 :                         ctx->curr_payload += op_length * blob->bs->io_unit_size;
    2999                 :            :                 }
    3000                 :            : 
    3001   [ -  +  -  + ]:       9265 :                 assert(!ctx->in_submit_ctx);
    3002                 :       9265 :                 ctx->in_submit_ctx = true;
    3003                 :            : 
    3004   [ +  +  +  +  :       9265 :                 switch (op_type) {
                   -  - ]
    3005                 :       1688 :                 case SPDK_BLOB_READ:
    3006                 :       1688 :                         spdk_blob_io_read(blob, ch, buf, offset, op_length,
    3007                 :            :                                           blob_request_submit_op_split_next, ctx);
    3008                 :       1688 :                         break;
    3009                 :        456 :                 case SPDK_BLOB_WRITE:
    3010                 :        456 :                         spdk_blob_io_write(blob, ch, buf, offset, op_length,
    3011                 :            :                                            blob_request_submit_op_split_next, ctx);
    3012                 :        456 :                         break;
    3013                 :       6993 :                 case SPDK_BLOB_UNMAP:
    3014                 :       6993 :                         spdk_blob_io_unmap(blob, ch, offset, op_length,
    3015                 :            :                                            blob_request_submit_op_split_next, ctx);
    3016                 :       6993 :                         break;
    3017                 :        128 :                 case SPDK_BLOB_WRITE_ZEROES:
    3018                 :        128 :                         spdk_blob_io_write_zeroes(blob, ch, offset, op_length,
    3019                 :            :                                                   blob_request_submit_op_split_next, ctx);
    3020                 :        128 :                         break;
    3021                 :          0 :                 case SPDK_BLOB_READV:
    3022                 :            :                 case SPDK_BLOB_WRITEV:
    3023                 :          0 :                         SPDK_ERRLOG("readv/write not valid\n");
    3024                 :          0 :                         bs_sequence_finish(ctx->seq, -EINVAL);
    3025                 :          0 :                         free(ctx);
    3026                 :          0 :                         return;
    3027                 :            :                 }
    3028                 :            : 
    3029                 :            : #ifndef __clang_analyzer__
    3030                 :            :                 /* scan-build reports a false positive around accessing the ctx here. It
    3031                 :            :                  * forms a path that recursively calls this function, but then says
    3032                 :            :                  * "assuming ctx->in_submit_ctx is false", when that isn't possible.
    3033                 :            :                  * This path does free(ctx), returns to here, and reports a use-after-free
    3034                 :            :                  * bug.  Wrapping this bit of code so that scan-build doesn't see it
    3035                 :            :                  * works around the scan-build bug.
    3036                 :            :                  */
    3037   [ -  +  -  + ]:       9265 :                 assert(ctx->in_submit_ctx);
    3038                 :       9265 :                 ctx->in_submit_ctx = false;
    3039                 :            : 
    3040                 :            :                 /* If the operation completed immediately, loop back and submit the
    3041                 :            :                  * next operation.  Otherwise we can return and the next split
    3042                 :            :                  * operation will get submitted when this current operation is
    3043                 :            :                  * later completed asynchronously.
    3044                 :            :                  */
    3045   [ +  +  +  + ]:       9265 :                 if (ctx->completed_in_submit_ctx) {
    3046                 :        272 :                         continue;
    3047   [ +  +  +  + ]:       8993 :                 } else if (ctx->done) {
    3048                 :        160 :                         free(ctx);
    3049                 :            :                 }
    3050                 :            : #endif
    3051                 :       8993 :                 break;
    3052                 :            :         }
    3053                 :            : }
    3054                 :            : 
    3055                 :            : static void
    3056                 :        746 : blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob,
    3057                 :            :                              void *payload, uint64_t offset, uint64_t length,
    3058                 :            :                              spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
    3059                 :            : {
    3060                 :            :         struct op_split_ctx *ctx;
    3061                 :            :         spdk_bs_sequence_t *seq;
    3062                 :        738 :         struct spdk_bs_cpl cpl;
    3063                 :            : 
    3064         [ -  + ]:        746 :         assert(blob != NULL);
    3065                 :            : 
    3066                 :        746 :         ctx = calloc(1, sizeof(struct op_split_ctx));
    3067         [ -  + ]:        746 :         if (ctx == NULL) {
    3068                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    3069                 :          0 :                 return;
    3070                 :            :         }
    3071                 :            : 
    3072                 :        746 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    3073                 :        746 :         cpl.u.blob_basic.cb_fn = cb_fn;
    3074                 :        746 :         cpl.u.blob_basic.cb_arg = cb_arg;
    3075                 :            : 
    3076                 :        746 :         seq = bs_sequence_start_blob(ch, &cpl, blob);
    3077         [ -  + ]:        746 :         if (!seq) {
    3078                 :          0 :                 free(ctx);
    3079                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    3080                 :          0 :                 return;
    3081                 :            :         }
    3082                 :            : 
    3083                 :        746 :         ctx->blob = blob;
    3084                 :        746 :         ctx->channel = ch;
    3085                 :        746 :         ctx->curr_payload = payload;
    3086                 :        746 :         ctx->io_unit_offset = offset;
    3087                 :        746 :         ctx->io_units_remaining = length;
    3088                 :        746 :         ctx->op_type = op_type;
    3089                 :        746 :         ctx->seq = seq;
    3090                 :            : 
    3091                 :        746 :         blob_request_submit_op_split_next(ctx, 0);
    3092                 :            : }
    3093                 :            : 
    3094                 :            : static void
    3095                 :        240 : spdk_free_cluster_unmap_complete(void *cb_arg, int bserrno)
    3096                 :            : {
    3097                 :        240 :         struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
    3098                 :            : 
    3099         [ -  + ]:        240 :         if (bserrno) {
    3100                 :          0 :                 bs_sequence_finish(ctx->seq, bserrno);
    3101                 :          0 :                 free(ctx);
    3102                 :          0 :                 return;
    3103                 :            :         }
    3104                 :            : 
    3105                 :        240 :         blob_free_cluster_on_md_thread(ctx->blob, ctx->cluster_num,
    3106                 :            :                                        ctx->extent_page, ctx->md_page, blob_free_cluster_cpl, ctx);
    3107                 :            : }
    3108                 :            : 
    3109                 :            : static void
    3110                 :    1870117 : blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob,
    3111                 :            :                               void *payload, uint64_t offset, uint64_t length,
    3112                 :            :                               spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
    3113                 :            : {
    3114                 :    1866987 :         struct spdk_bs_cpl cpl;
    3115                 :    1866987 :         uint64_t lba;
    3116                 :    1866987 :         uint64_t lba_count;
    3117                 :            :         bool is_allocated;
    3118                 :            : 
    3119         [ -  + ]:    1870117 :         assert(blob != NULL);
    3120                 :            : 
    3121                 :    1870117 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    3122                 :    1870117 :         cpl.u.blob_basic.cb_fn = cb_fn;
    3123                 :    1870117 :         cpl.u.blob_basic.cb_arg = cb_arg;
    3124                 :            : 
    3125         [ +  + ]:    1870117 :         if (blob->frozen_refcnt) {
    3126                 :            :                 /* This blob I/O is frozen */
    3127                 :            :                 spdk_bs_user_op_t *op;
    3128                 :         20 :                 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
    3129                 :            : 
    3130                 :         20 :                 op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
    3131         [ -  + ]:         20 :                 if (!op) {
    3132                 :          0 :                         cb_fn(cb_arg, -ENOMEM);
    3133                 :          0 :                         return;
    3134                 :            :                 }
    3135                 :            : 
    3136                 :         20 :                 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
    3137                 :            : 
    3138                 :         20 :                 return;
    3139                 :            :         }
    3140                 :            : 
    3141                 :    1870097 :         is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
    3142                 :            : 
    3143   [ +  +  +  -  :    1870097 :         switch (op_type) {
                      - ]
    3144                 :    1386038 :         case SPDK_BLOB_READ: {
    3145                 :            :                 spdk_bs_batch_t *batch;
    3146                 :            : 
    3147                 :    1386038 :                 batch = bs_batch_open(_ch, &cpl, blob);
    3148         [ -  + ]:    1386038 :                 if (!batch) {
    3149                 :          0 :                         cb_fn(cb_arg, -ENOMEM);
    3150                 :          0 :                         return;
    3151                 :            :                 }
    3152                 :            : 
    3153         [ +  + ]:    1386038 :                 if (is_allocated) {
    3154                 :            :                         /* Read from the blob */
    3155                 :    1381686 :                         bs_batch_read_dev(batch, payload, lba, lba_count);
    3156                 :            :                 } else {
    3157                 :            :                         /* Read from the backing block device */
    3158                 :       4352 :                         bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count);
    3159                 :            :                 }
    3160                 :            : 
    3161                 :    1386038 :                 bs_batch_close(batch);
    3162                 :    1386038 :                 break;
    3163                 :            :         }
    3164                 :     333025 :         case SPDK_BLOB_WRITE:
    3165                 :            :         case SPDK_BLOB_WRITE_ZEROES: {
    3166         [ +  + ]:     333025 :                 if (is_allocated) {
    3167                 :            :                         /* Write to the blob */
    3168                 :            :                         spdk_bs_batch_t *batch;
    3169                 :            : 
    3170         [ -  + ]:     331649 :                         if (lba_count == 0) {
    3171                 :          0 :                                 cb_fn(cb_arg, 0);
    3172                 :          0 :                                 return;
    3173                 :            :                         }
    3174                 :            : 
    3175                 :     331649 :                         batch = bs_batch_open(_ch, &cpl, blob);
    3176         [ -  + ]:     331649 :                         if (!batch) {
    3177                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3178                 :          0 :                                 return;
    3179                 :            :                         }
    3180                 :            : 
    3181         [ +  + ]:     331649 :                         if (op_type == SPDK_BLOB_WRITE) {
    3182                 :     331516 :                                 bs_batch_write_dev(batch, payload, lba, lba_count);
    3183                 :            :                         } else {
    3184                 :        133 :                                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    3185                 :            :                         }
    3186                 :            : 
    3187                 :     331649 :                         bs_batch_close(batch);
    3188                 :            :                 } else {
    3189                 :            :                         /* Queue this operation and allocate the cluster */
    3190                 :            :                         spdk_bs_user_op_t *op;
    3191                 :            : 
    3192                 :       1376 :                         op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
    3193         [ -  + ]:       1376 :                         if (!op) {
    3194                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3195                 :          0 :                                 return;
    3196                 :            :                         }
    3197                 :            : 
    3198                 :       1376 :                         bs_allocate_and_copy_cluster(blob, _ch, offset, op);
    3199                 :            :                 }
    3200                 :     333025 :                 break;
    3201                 :            :         }
    3202                 :     151034 :         case SPDK_BLOB_UNMAP: {
    3203                 :     151034 :                 struct spdk_blob_free_cluster_ctx *ctx = NULL;
    3204                 :            :                 spdk_bs_batch_t *batch;
    3205                 :            : 
    3206                 :            :                 /* if aligned with cluster release cluster */
    3207   [ +  +  +  -  :     151306 :                 if (spdk_blob_is_thin_provisioned(blob) && is_allocated &&
                   +  + ]
    3208                 :        272 :                     bs_io_units_per_cluster(blob) == length) {
    3209                 :        240 :                         struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
    3210                 :            :                         uint32_t cluster_start_page;
    3211                 :            :                         uint32_t cluster_number;
    3212                 :            : 
    3213   [ -  +  -  + ]:        240 :                         assert(offset % bs_io_units_per_cluster(blob) == 0);
    3214                 :            : 
    3215                 :            :                         /* Round the io_unit offset down to the first page in the cluster */
    3216                 :        240 :                         cluster_start_page = bs_io_unit_to_cluster_start(blob, offset);
    3217                 :            : 
    3218                 :            :                         /* Calculate which index in the metadata cluster array the corresponding
    3219                 :            :                          * cluster is supposed to be at. */
    3220                 :        240 :                         cluster_number = bs_io_unit_to_cluster_number(blob, offset);
    3221                 :            : 
    3222                 :        240 :                         ctx = calloc(1, sizeof(*ctx));
    3223         [ -  + ]:        240 :                         if (!ctx) {
    3224                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3225                 :          0 :                                 return;
    3226                 :            :                         }
    3227                 :            :                         /* When freeing a cluster the flow should be (in order):
    3228                 :            :                          * 1. Unmap the underlying area (so if the cluster is reclaimed in the future, it won't leak
    3229                 :            :                          * old data)
    3230                 :            :                          * 2. Once the unmap completes (to avoid any races with incoming writes that may claim the
    3231                 :            :                          * cluster), update and sync metadata freeing the cluster
    3232                 :            :                          * 3. Once metadata update is done, complete the user unmap request
    3233                 :            :                          */
    3234                 :        240 :                         ctx->blob = blob;
    3235                 :        240 :                         ctx->page = cluster_start_page;
    3236                 :        240 :                         ctx->cluster_num = cluster_number;
    3237                 :        240 :                         ctx->md_page = bs_channel->new_cluster_page;
    3238                 :        240 :                         ctx->seq = bs_sequence_start_bs(_ch, &cpl);
    3239         [ -  + ]:        240 :                         if (!ctx->seq) {
    3240                 :          0 :                                 free(ctx);
    3241                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3242                 :          0 :                                 return;
    3243                 :            :                         }
    3244                 :            : 
    3245   [ +  +  +  + ]:        240 :                         if (blob->use_extent_table) {
    3246                 :        120 :                                 ctx->extent_page = *bs_cluster_to_extent_page(blob, cluster_number);
    3247                 :            :                         }
    3248                 :            : 
    3249                 :        240 :                         cpl.u.blob_basic.cb_fn = spdk_free_cluster_unmap_complete;
    3250                 :        240 :                         cpl.u.blob_basic.cb_arg = ctx;
    3251                 :            :                 }
    3252                 :            : 
    3253                 :     151034 :                 batch = bs_batch_open(_ch, &cpl, blob);
    3254         [ -  + ]:     151034 :                 if (!batch) {
    3255                 :          0 :                         free(ctx);
    3256                 :          0 :                         cb_fn(cb_arg, -ENOMEM);
    3257                 :          0 :                         return;
    3258                 :            :                 }
    3259                 :            : 
    3260         [ +  - ]:     151034 :                 if (is_allocated) {
    3261                 :     151034 :                         bs_batch_unmap_dev(batch, lba, lba_count);
    3262                 :            :                 }
    3263                 :            : 
    3264                 :     151034 :                 bs_batch_close(batch);
    3265                 :     151034 :                 break;
    3266                 :            :         }
    3267                 :          0 :         case SPDK_BLOB_READV:
    3268                 :            :         case SPDK_BLOB_WRITEV:
    3269                 :          0 :                 SPDK_ERRLOG("readv/write not valid\n");
    3270                 :          0 :                 cb_fn(cb_arg, -EINVAL);
    3271                 :          0 :                 break;
    3272                 :            :         }
    3273                 :      75660 : }
    3274                 :            : 
    3275                 :            : static void
    3276                 :    1872937 : blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel,
    3277                 :            :                        void *payload, uint64_t offset, uint64_t length,
    3278                 :            :                        spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
    3279                 :            : {
    3280         [ -  + ]:    1872937 :         assert(blob != NULL);
    3281                 :            : 
    3282   [ +  +  +  +  :    1872937 :         if (blob->data_ro && op_type != SPDK_BLOB_READ) {
                   +  + ]
    3283                 :         16 :                 cb_fn(cb_arg, -EPERM);
    3284                 :         16 :                 return;
    3285                 :            :         }
    3286                 :            : 
    3287         [ +  + ]:    1872921 :         if (length == 0) {
    3288                 :       1996 :                 cb_fn(cb_arg, 0);
    3289                 :       1996 :                 return;
    3290                 :            :         }
    3291                 :            : 
    3292         [ +  + ]:    1870925 :         if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
    3293                 :         96 :                 cb_fn(cb_arg, -EINVAL);
    3294                 :         96 :                 return;
    3295                 :            :         }
    3296         [ +  + ]:    1870829 :         if (length <= bs_num_io_units_to_cluster_boundary(blob, offset)) {
    3297                 :    1870083 :                 blob_request_submit_op_single(_channel, blob, payload, offset, length,
    3298                 :            :                                               cb_fn, cb_arg, op_type);
    3299                 :            :         } else {
    3300                 :        746 :                 blob_request_submit_op_split(_channel, blob, payload, offset, length,
    3301                 :            :                                              cb_fn, cb_arg, op_type);
    3302                 :            :         }
    3303                 :            : }
    3304                 :            : 
    3305                 :            : struct rw_iov_ctx {
    3306                 :            :         struct spdk_blob *blob;
    3307                 :            :         struct spdk_io_channel *channel;
    3308                 :            :         spdk_blob_op_complete cb_fn;
    3309                 :            :         void *cb_arg;
    3310                 :            :         bool read;
    3311                 :            :         int iovcnt;
    3312                 :            :         struct iovec *orig_iov;
    3313                 :            :         uint64_t io_unit_offset;
    3314                 :            :         uint64_t io_units_remaining;
    3315                 :            :         uint64_t io_units_done;
    3316                 :            :         struct spdk_blob_ext_io_opts *ext_io_opts;
    3317                 :            :         struct iovec iov[0];
    3318                 :            : };
    3319                 :            : 
    3320                 :            : static void
    3321                 :    4820165 : rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    3322                 :            : {
    3323         [ -  + ]:    4820165 :         assert(cb_arg == NULL);
    3324                 :    4820165 :         bs_sequence_finish(seq, bserrno);
    3325                 :    4820165 : }
    3326                 :            : 
    3327                 :            : static void
    3328                 :       2976 : rw_iov_split_next(void *cb_arg, int bserrno)
    3329                 :            : {
    3330                 :       2976 :         struct rw_iov_ctx *ctx = cb_arg;
    3331                 :       2976 :         struct spdk_blob *blob = ctx->blob;
    3332                 :            :         struct iovec *iov, *orig_iov;
    3333                 :            :         int iovcnt;
    3334                 :            :         size_t orig_iovoff;
    3335                 :            :         uint64_t io_units_count, io_units_to_boundary, io_unit_offset;
    3336                 :            :         uint64_t byte_count;
    3337                 :            : 
    3338   [ +  -  +  + ]:       2976 :         if (bserrno != 0 || ctx->io_units_remaining == 0) {
    3339                 :        816 :                 ctx->cb_fn(ctx->cb_arg, bserrno);
    3340                 :        816 :                 free(ctx);
    3341                 :        816 :                 return;
    3342                 :            :         }
    3343                 :            : 
    3344                 :       2160 :         io_unit_offset = ctx->io_unit_offset;
    3345                 :       2160 :         io_units_to_boundary = bs_num_io_units_to_cluster_boundary(blob, io_unit_offset);
    3346                 :       2160 :         io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary);
    3347                 :            :         /*
    3348                 :            :          * Get index and offset into the original iov array for our current position in the I/O sequence.
    3349                 :            :          *  byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
    3350                 :            :          *  point to the current position in the I/O sequence.
    3351                 :            :          */
    3352                 :       2160 :         byte_count = ctx->io_units_done * blob->bs->io_unit_size;
    3353                 :       2160 :         orig_iov = &ctx->orig_iov[0];
    3354                 :       2160 :         orig_iovoff = 0;
    3355         [ +  + ]:       4592 :         while (byte_count > 0) {
    3356         [ +  + ]:       2432 :                 if (byte_count >= orig_iov->iov_len) {
    3357                 :       1408 :                         byte_count -= orig_iov->iov_len;
    3358                 :       1408 :                         orig_iov++;
    3359                 :            :                 } else {
    3360                 :       1024 :                         orig_iovoff = byte_count;
    3361                 :       1024 :                         byte_count = 0;
    3362                 :            :                 }
    3363                 :            :         }
    3364                 :            : 
    3365                 :            :         /*
    3366                 :            :          * Build an iov array for the next I/O in the sequence.  byte_count will keep track of how many
    3367                 :            :          *  bytes of this next I/O remain to be accounted for in the new iov array.
    3368                 :            :          */
    3369                 :       2160 :         byte_count = io_units_count * blob->bs->io_unit_size;
    3370                 :       2160 :         iov = &ctx->iov[0];
    3371                 :       2160 :         iovcnt = 0;
    3372         [ +  + ]:       5520 :         while (byte_count > 0) {
    3373         [ -  + ]:       3360 :                 assert(iovcnt < ctx->iovcnt);
    3374                 :       3360 :                 iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
    3375                 :       3360 :                 iov->iov_base = orig_iov->iov_base + orig_iovoff;
    3376                 :       3360 :                 byte_count -= iov->iov_len;
    3377                 :       3360 :                 orig_iovoff = 0;
    3378                 :       3360 :                 orig_iov++;
    3379                 :       3360 :                 iov++;
    3380                 :       3360 :                 iovcnt++;
    3381                 :            :         }
    3382                 :            : 
    3383                 :       2160 :         ctx->io_unit_offset += io_units_count;
    3384                 :       2160 :         ctx->io_units_remaining -= io_units_count;
    3385                 :       2160 :         ctx->io_units_done += io_units_count;
    3386                 :       2160 :         iov = &ctx->iov[0];
    3387                 :            : 
    3388   [ +  +  +  + ]:       2160 :         if (ctx->read) {
    3389                 :       1632 :                 spdk_blob_io_readv_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
    3390                 :            :                                        io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
    3391                 :            :         } else {
    3392                 :        528 :                 spdk_blob_io_writev_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
    3393                 :            :                                         io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
    3394                 :            :         }
    3395                 :            : }
    3396                 :            : 
    3397                 :            : static void
    3398                 :    4867430 : blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
    3399                 :            :                            struct iovec *iov, int iovcnt,
    3400                 :            :                            uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg, bool read,
    3401                 :            :                            struct spdk_blob_ext_io_opts *ext_io_opts)
    3402                 :            : {
    3403                 :    1934437 :         struct spdk_bs_cpl      cpl;
    3404                 :            : 
    3405         [ -  + ]:    4867430 :         assert(blob != NULL);
    3406                 :            : 
    3407   [ +  +  +  +  :    4867430 :         if (!read && blob->data_ro) {
                   +  + ]
    3408                 :         49 :                 cb_fn(cb_arg, -EPERM);
    3409                 :       8364 :                 return;
    3410                 :            :         }
    3411                 :            : 
    3412         [ -  + ]:    4867381 :         if (length == 0) {
    3413                 :          0 :                 cb_fn(cb_arg, 0);
    3414                 :          0 :                 return;
    3415                 :            :         }
    3416                 :            : 
    3417         [ -  + ]:    4867381 :         if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
    3418                 :          0 :                 cb_fn(cb_arg, -EINVAL);
    3419                 :          0 :                 return;
    3420                 :            :         }
    3421                 :            : 
    3422                 :            :         /*
    3423                 :            :          * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
    3424                 :            :          *  to split a request that spans a cluster boundary.  For I/O that do not span a cluster boundary,
    3425                 :            :          *  there will be no noticeable difference compared to using a batch.  For I/O that do span a cluster
    3426                 :            :          *  boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
    3427                 :            :          *  to allocate a separate iov array and split the I/O such that none of the resulting
    3428                 :            :          *  smaller I/O cross a cluster boundary.  These smaller I/O will be issued in sequence (not in parallel)
    3429                 :            :          *  but since this case happens very infrequently, any performance impact will be negligible.
    3430                 :            :          *
    3431                 :            :          * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
    3432                 :            :          *  for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
    3433                 :            :          *  in a batch.  That would also require creating an intermediate spdk_bs_cpl that would get called
    3434                 :            :          *  when the batch was completed, to allow for freeing the memory for the iov arrays.
    3435                 :            :          */
    3436         [ +  + ]:    4867381 :         if (spdk_likely(length <= bs_num_io_units_to_cluster_boundary(blob, offset))) {
    3437                 :    1933556 :                 uint64_t lba_count;
    3438                 :    1933556 :                 uint64_t lba;
    3439                 :            :                 bool is_allocated;
    3440                 :            : 
    3441                 :    4866549 :                 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    3442                 :    4866549 :                 cpl.u.blob_basic.cb_fn = cb_fn;
    3443                 :    4866549 :                 cpl.u.blob_basic.cb_arg = cb_arg;
    3444                 :            : 
    3445         [ +  + ]:    4866549 :                 if (blob->frozen_refcnt) {
    3446                 :            :                         /* This blob I/O is frozen */
    3447                 :            :                         enum spdk_blob_op_type op_type;
    3448                 :            :                         spdk_bs_user_op_t *op;
    3449                 :       1566 :                         struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel);
    3450                 :            : 
    3451         [ -  + ]:       1566 :                         op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV;
    3452                 :       1566 :                         op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length);
    3453         [ -  + ]:       1566 :                         if (!op) {
    3454                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3455                 :       8315 :                                 return;
    3456                 :            :                         }
    3457                 :            : 
    3458                 :       1566 :                         TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
    3459                 :            : 
    3460                 :       1566 :                         return;
    3461                 :            :                 }
    3462                 :            : 
    3463                 :    4864983 :                 is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
    3464                 :            : 
    3465         [ +  + ]:    4864983 :                 if (read) {
    3466                 :            :                         spdk_bs_sequence_t *seq;
    3467                 :            : 
    3468                 :    2288371 :                         seq = bs_sequence_start_blob(_channel, &cpl, blob);
    3469         [ +  + ]:    2288371 :                         if (!seq) {
    3470                 :        698 :                                 cb_fn(cb_arg, -ENOMEM);
    3471                 :        698 :                                 return;
    3472                 :            :                         }
    3473                 :            : 
    3474                 :    2287673 :                         seq->ext_io_opts = ext_io_opts;
    3475                 :            : 
    3476         [ +  + ]:    2287673 :                         if (is_allocated) {
    3477                 :    1485208 :                                 bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
    3478                 :            :                         } else {
    3479                 :     802465 :                                 bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count,
    3480                 :            :                                                          rw_iov_done, NULL);
    3481                 :            :                         }
    3482                 :            :                 } else {
    3483         [ +  + ]:    2576612 :                         if (is_allocated) {
    3484                 :            :                                 spdk_bs_sequence_t *seq;
    3485                 :            : 
    3486                 :    2561862 :                                 seq = bs_sequence_start_blob(_channel, &cpl, blob);
    3487         [ +  + ]:    2561862 :                                 if (!seq) {
    3488                 :      29370 :                                         cb_fn(cb_arg, -ENOMEM);
    3489                 :      29370 :                                         return;
    3490                 :            :                                 }
    3491                 :            : 
    3492                 :    2532492 :                                 seq->ext_io_opts = ext_io_opts;
    3493                 :            : 
    3494                 :    2532492 :                                 bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
    3495                 :            :                         } else {
    3496                 :            :                                 /* Queue this operation and allocate the cluster */
    3497                 :            :                                 spdk_bs_user_op_t *op;
    3498                 :            : 
    3499                 :      14750 :                                 op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset,
    3500                 :            :                                                       length);
    3501         [ -  + ]:      14750 :                                 if (!op) {
    3502                 :          0 :                                         cb_fn(cb_arg, -ENOMEM);
    3503                 :          0 :                                         return;
    3504                 :            :                                 }
    3505                 :            : 
    3506                 :      14750 :                                 op->ext_io_opts = ext_io_opts;
    3507                 :            : 
    3508                 :      14750 :                                 bs_allocate_and_copy_cluster(blob, _channel, offset, op);
    3509                 :            :                         }
    3510                 :            :                 }
    3511                 :            :         } else {
    3512                 :            :                 struct rw_iov_ctx *ctx;
    3513                 :            : 
    3514                 :        832 :                 ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
    3515         [ +  + ]:        832 :                 if (ctx == NULL) {
    3516                 :         16 :                         cb_fn(cb_arg, -ENOMEM);
    3517                 :         16 :                         return;
    3518                 :            :                 }
    3519                 :            : 
    3520                 :        816 :                 ctx->blob = blob;
    3521                 :        816 :                 ctx->channel = _channel;
    3522                 :        816 :                 ctx->cb_fn = cb_fn;
    3523                 :        816 :                 ctx->cb_arg = cb_arg;
    3524                 :        816 :                 ctx->read = read;
    3525                 :        816 :                 ctx->orig_iov = iov;
    3526                 :        816 :                 ctx->iovcnt = iovcnt;
    3527                 :        816 :                 ctx->io_unit_offset = offset;
    3528                 :        816 :                 ctx->io_units_remaining = length;
    3529                 :        816 :                 ctx->io_units_done = 0;
    3530                 :        816 :                 ctx->ext_io_opts = ext_io_opts;
    3531                 :            : 
    3532                 :        816 :                 rw_iov_split_next(ctx, 0);
    3533                 :            :         }
    3534                 :            : }
    3535                 :            : 
    3536                 :            : static struct spdk_blob *
    3537                 :      44111 : blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
    3538                 :            : {
    3539                 :      43380 :         struct spdk_blob find;
    3540                 :            : 
    3541         [ +  + ]:      44111 :         if (spdk_bit_array_get(bs->open_blobids, blobid) == 0) {
    3542                 :      40751 :                 return NULL;
    3543                 :            :         }
    3544                 :            : 
    3545                 :       3360 :         find.id = blobid;
    3546                 :       3360 :         return RB_FIND(spdk_blob_tree, &bs->open_blobs, &find);
    3547                 :            : }
    3548                 :            : 
    3549                 :            : static void
    3550                 :       8007 : blob_get_snapshot_and_clone_entries(struct spdk_blob *blob,
    3551                 :            :                                     struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry)
    3552                 :            : {
    3553         [ -  + ]:       8007 :         assert(blob != NULL);
    3554                 :       8007 :         *snapshot_entry = NULL;
    3555                 :       8007 :         *clone_entry = NULL;
    3556                 :            : 
    3557         [ +  + ]:       8007 :         if (blob->parent_id == SPDK_BLOBID_INVALID) {
    3558                 :       6813 :                 return;
    3559                 :            :         }
    3560                 :            : 
    3561         [ +  + ]:       1781 :         TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) {
    3562         [ +  + ]:       1556 :                 if ((*snapshot_entry)->id == blob->parent_id) {
    3563                 :        969 :                         break;
    3564                 :            :                 }
    3565                 :            :         }
    3566                 :            : 
    3567         [ +  + ]:       1194 :         if (*snapshot_entry != NULL) {
    3568         [ +  - ]:       1159 :                 TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) {
    3569         [ +  + ]:       1159 :                         if ((*clone_entry)->id == blob->id) {
    3570                 :        969 :                                 break;
    3571                 :            :                         }
    3572                 :            :                 }
    3573                 :            : 
    3574         [ -  + ]:        969 :                 assert(*clone_entry != NULL);
    3575                 :            :         }
    3576                 :            : }
    3577                 :            : 
    3578                 :            : static int
    3579                 :      10650 : bs_channel_create(void *io_device, void *ctx_buf)
    3580                 :            : {
    3581                 :      10650 :         struct spdk_blob_store          *bs = io_device;
    3582                 :      10650 :         struct spdk_bs_channel          *channel = ctx_buf;
    3583                 :            :         struct spdk_bs_dev              *dev;
    3584                 :      10650 :         uint32_t                        max_ops = bs->max_channel_ops;
    3585                 :            :         uint32_t                        i;
    3586                 :            : 
    3587                 :      10650 :         dev = bs->dev;
    3588                 :            : 
    3589                 :      10650 :         channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
    3590         [ -  + ]:      10650 :         if (!channel->req_mem) {
    3591                 :          0 :                 return -1;
    3592                 :            :         }
    3593                 :            : 
    3594                 :      10650 :         TAILQ_INIT(&channel->reqs);
    3595                 :            : 
    3596         [ +  + ]:    5463450 :         for (i = 0; i < max_ops; i++) {
    3597                 :    5452800 :                 TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
    3598                 :            :         }
    3599                 :            : 
    3600                 :      10650 :         channel->bs = bs;
    3601                 :      10650 :         channel->dev = dev;
    3602                 :      10650 :         channel->dev_channel = dev->create_channel(dev);
    3603                 :            : 
    3604         [ -  + ]:      10650 :         if (!channel->dev_channel) {
    3605                 :          0 :                 SPDK_ERRLOG("Failed to create device channel.\n");
    3606                 :          0 :                 free(channel->req_mem);
    3607                 :          0 :                 return -1;
    3608                 :            :         }
    3609                 :            : 
    3610                 :      10650 :         channel->new_cluster_page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY,
    3611                 :            :                                     SPDK_MALLOC_DMA);
    3612         [ -  + ]:      10650 :         if (!channel->new_cluster_page) {
    3613                 :          0 :                 SPDK_ERRLOG("Failed to allocate new cluster page\n");
    3614                 :          0 :                 free(channel->req_mem);
    3615                 :          0 :                 channel->dev->destroy_channel(channel->dev, channel->dev_channel);
    3616                 :          0 :                 return -1;
    3617                 :            :         }
    3618                 :            : 
    3619                 :      10650 :         TAILQ_INIT(&channel->need_cluster_alloc);
    3620                 :      10650 :         TAILQ_INIT(&channel->queued_io);
    3621                 :      10650 :         RB_INIT(&channel->esnap_channels);
    3622                 :            : 
    3623                 :      10650 :         return 0;
    3624                 :            : }
    3625                 :            : 
    3626                 :            : static void
    3627                 :      10650 : bs_channel_destroy(void *io_device, void *ctx_buf)
    3628                 :            : {
    3629                 :      10650 :         struct spdk_bs_channel *channel = ctx_buf;
    3630                 :            :         spdk_bs_user_op_t *op;
    3631                 :            : 
    3632         [ -  + ]:      10650 :         while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) {
    3633                 :          0 :                 op = TAILQ_FIRST(&channel->need_cluster_alloc);
    3634         [ #  # ]:          0 :                 TAILQ_REMOVE(&channel->need_cluster_alloc, op, link);
    3635                 :          0 :                 bs_user_op_abort(op, -EIO);
    3636                 :            :         }
    3637                 :            : 
    3638         [ -  + ]:      10650 :         while (!TAILQ_EMPTY(&channel->queued_io)) {
    3639                 :          0 :                 op = TAILQ_FIRST(&channel->queued_io);
    3640         [ #  # ]:          0 :                 TAILQ_REMOVE(&channel->queued_io, op, link);
    3641                 :          0 :                 bs_user_op_abort(op, -EIO);
    3642                 :            :         }
    3643                 :            : 
    3644                 :      10650 :         blob_esnap_destroy_bs_channel(channel);
    3645                 :            : 
    3646                 :      10650 :         free(channel->req_mem);
    3647                 :      10650 :         spdk_free(channel->new_cluster_page);
    3648                 :      10650 :         channel->dev->destroy_channel(channel->dev, channel->dev_channel);
    3649                 :      10650 : }
    3650                 :            : 
    3651                 :            : static void
    3652                 :      10526 : bs_dev_destroy(void *io_device)
    3653                 :            : {
    3654                 :      10526 :         struct spdk_blob_store *bs = io_device;
    3655                 :            :         struct spdk_blob        *blob, *blob_tmp;
    3656                 :            : 
    3657                 :      10526 :         bs->dev->destroy(bs->dev);
    3658                 :            : 
    3659   [ -  +  -  - ]:      10526 :         RB_FOREACH_SAFE(blob, spdk_blob_tree, &bs->open_blobs, blob_tmp) {
    3660                 :          0 :                 RB_REMOVE(spdk_blob_tree, &bs->open_blobs, blob);
    3661                 :          0 :                 spdk_bit_array_clear(bs->open_blobids, blob->id);
    3662                 :          0 :                 blob_free(blob);
    3663                 :            :         }
    3664                 :            : 
    3665                 :      10526 :         spdk_spin_destroy(&bs->used_lock);
    3666                 :            : 
    3667                 :      10526 :         spdk_bit_array_free(&bs->open_blobids);
    3668                 :      10526 :         spdk_bit_array_free(&bs->used_blobids);
    3669                 :      10526 :         spdk_bit_array_free(&bs->used_md_pages);
    3670                 :      10526 :         spdk_bit_pool_free(&bs->used_clusters);
    3671                 :            :         /*
    3672                 :            :          * If this function is called for any reason except a successful unload,
    3673                 :            :          * the unload_cpl type will be NONE and this will be a nop.
    3674                 :            :          */
    3675                 :      10526 :         bs_call_cpl(&bs->unload_cpl, bs->unload_err);
    3676                 :            : 
    3677                 :      10526 :         free(bs);
    3678                 :      10526 : }
    3679                 :            : 
    3680                 :            : static int
    3681                 :       5408 : bs_blob_list_add(struct spdk_blob *blob)
    3682                 :            : {
    3683                 :            :         spdk_blob_id snapshot_id;
    3684                 :       5408 :         struct spdk_blob_list *snapshot_entry = NULL;
    3685                 :       5408 :         struct spdk_blob_list *clone_entry = NULL;
    3686                 :            : 
    3687         [ -  + ]:       5408 :         assert(blob != NULL);
    3688                 :            : 
    3689                 :       5408 :         snapshot_id = blob->parent_id;
    3690   [ +  +  +  + ]:       5408 :         if (snapshot_id == SPDK_BLOBID_INVALID ||
    3691                 :            :             snapshot_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    3692                 :       3672 :                 return 0;
    3693                 :            :         }
    3694                 :            : 
    3695                 :       1736 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, snapshot_id);
    3696         [ +  + ]:       1736 :         if (snapshot_entry == NULL) {
    3697                 :            :                 /* Snapshot not found */
    3698                 :       1189 :                 snapshot_entry = calloc(1, sizeof(struct spdk_blob_list));
    3699         [ -  + ]:       1189 :                 if (snapshot_entry == NULL) {
    3700                 :          0 :                         return -ENOMEM;
    3701                 :            :                 }
    3702                 :       1189 :                 snapshot_entry->id = snapshot_id;
    3703                 :       1189 :                 TAILQ_INIT(&snapshot_entry->clones);
    3704                 :       1189 :                 TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link);
    3705                 :            :         } else {
    3706         [ +  + ]:        883 :                 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
    3707         [ -  + ]:        336 :                         if (clone_entry->id == blob->id) {
    3708                 :          0 :                                 break;
    3709                 :            :                         }
    3710                 :            :                 }
    3711                 :            :         }
    3712                 :            : 
    3713         [ +  - ]:       1736 :         if (clone_entry == NULL) {
    3714                 :            :                 /* Clone not found */
    3715                 :       1736 :                 clone_entry = calloc(1, sizeof(struct spdk_blob_list));
    3716         [ -  + ]:       1736 :                 if (clone_entry == NULL) {
    3717                 :          0 :                         return -ENOMEM;
    3718                 :            :                 }
    3719                 :       1736 :                 clone_entry->id = blob->id;
    3720                 :       1736 :                 TAILQ_INIT(&clone_entry->clones);
    3721                 :       1736 :                 TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link);
    3722                 :       1736 :                 snapshot_entry->clone_count++;
    3723                 :            :         }
    3724                 :            : 
    3725                 :       1736 :         return 0;
    3726                 :            : }
    3727                 :            : 
    3728                 :            : static void
    3729                 :       7686 : bs_blob_list_remove(struct spdk_blob *blob)
    3730                 :            : {
    3731                 :       7686 :         struct spdk_blob_list *snapshot_entry = NULL;
    3732                 :       7686 :         struct spdk_blob_list *clone_entry = NULL;
    3733                 :            : 
    3734                 :       7686 :         blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry);
    3735                 :            : 
    3736         [ +  + ]:       7686 :         if (snapshot_entry == NULL) {
    3737                 :       6783 :                 return;
    3738                 :            :         }
    3739                 :            : 
    3740                 :        903 :         blob->parent_id = SPDK_BLOBID_INVALID;
    3741         [ +  + ]:        903 :         TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
    3742                 :        903 :         free(clone_entry);
    3743                 :            : 
    3744                 :        903 :         snapshot_entry->clone_count--;
    3745                 :            : }
    3746                 :            : 
    3747                 :            : static int
    3748                 :      10526 : bs_blob_list_free(struct spdk_blob_store *bs)
    3749                 :            : {
    3750                 :            :         struct spdk_blob_list *snapshot_entry;
    3751                 :            :         struct spdk_blob_list *snapshot_entry_tmp;
    3752                 :            :         struct spdk_blob_list *clone_entry;
    3753                 :            :         struct spdk_blob_list *clone_entry_tmp;
    3754                 :            : 
    3755         [ +  + ]:      11119 :         TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) {
    3756         [ +  + ]:       1226 :                 TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) {
    3757         [ +  + ]:        633 :                         TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
    3758                 :        633 :                         free(clone_entry);
    3759                 :            :                 }
    3760         [ +  + ]:        593 :                 TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link);
    3761                 :        593 :                 free(snapshot_entry);
    3762                 :            :         }
    3763                 :            : 
    3764                 :      10526 :         return 0;
    3765                 :            : }
    3766                 :            : 
    3767                 :            : static void
    3768                 :      10526 : bs_free(struct spdk_blob_store *bs)
    3769                 :            : {
    3770                 :      10526 :         bs_blob_list_free(bs);
    3771                 :            : 
    3772                 :      10526 :         bs_unregister_md_thread(bs);
    3773                 :      10526 :         spdk_io_device_unregister(bs, bs_dev_destroy);
    3774                 :      10526 : }
    3775                 :            : 
    3776                 :            : void
    3777                 :      19088 : spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size)
    3778                 :            : {
    3779                 :            : 
    3780         [ -  + ]:      19088 :         if (!opts) {
    3781                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
    3782                 :          0 :                 return;
    3783                 :            :         }
    3784                 :            : 
    3785         [ -  + ]:      19088 :         if (!opts_size) {
    3786                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
    3787                 :          0 :                 return;
    3788                 :            :         }
    3789                 :            : 
    3790         [ -  + ]:      19088 :         memset(opts, 0, opts_size);
    3791                 :      19088 :         opts->opts_size = opts_size;
    3792                 :            : 
    3793                 :            : #define FIELD_OK(field) \
    3794                 :            :         offsetof(struct spdk_bs_opts, field) + sizeof(opts->field) <= opts_size
    3795                 :            : 
    3796                 :            : #define SET_FIELD(field, value) \
    3797                 :            :         if (FIELD_OK(field)) { \
    3798                 :            :                 opts->field = value; \
    3799                 :            :         } \
    3800                 :            : 
    3801         [ +  - ]:      19088 :         SET_FIELD(cluster_sz, SPDK_BLOB_OPTS_CLUSTER_SZ);
    3802         [ +  - ]:      19088 :         SET_FIELD(num_md_pages, SPDK_BLOB_OPTS_NUM_MD_PAGES);
    3803         [ +  - ]:      19088 :         SET_FIELD(max_md_ops, SPDK_BLOB_OPTS_NUM_MD_PAGES);
    3804         [ +  - ]:      19088 :         SET_FIELD(max_channel_ops, SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS);
    3805         [ +  - ]:      19088 :         SET_FIELD(clear_method,  BS_CLEAR_WITH_UNMAP);
    3806                 :            : 
    3807         [ +  - ]:      19088 :         if (FIELD_OK(bstype)) {
    3808         [ -  + ]:      19088 :                 memset(&opts->bstype, 0, sizeof(opts->bstype));
    3809                 :            :         }
    3810                 :            : 
    3811         [ +  - ]:      19088 :         SET_FIELD(iter_cb_fn, NULL);
    3812         [ +  - ]:      19088 :         SET_FIELD(iter_cb_arg, NULL);
    3813         [ +  - ]:      19088 :         SET_FIELD(force_recover, false);
    3814         [ +  - ]:      19088 :         SET_FIELD(esnap_bs_dev_create, NULL);
    3815         [ +  - ]:      19088 :         SET_FIELD(esnap_ctx, NULL);
    3816                 :            : 
    3817                 :            : #undef FIELD_OK
    3818                 :            : #undef SET_FIELD
    3819                 :            : }
    3820                 :            : 
    3821                 :            : static int
    3822                 :       2088 : bs_opts_verify(struct spdk_bs_opts *opts)
    3823                 :            : {
    3824   [ +  +  +  -  :       2088 :         if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
                   +  - ]
    3825         [ -  + ]:       2072 :             opts->max_channel_ops == 0) {
    3826                 :         16 :                 SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
    3827                 :         16 :                 return -1;
    3828                 :            :         }
    3829                 :            : 
    3830                 :       2072 :         return 0;
    3831                 :            : }
    3832                 :            : 
    3833                 :            : /* START spdk_bs_load */
    3834                 :            : 
    3835                 :            : /* spdk_bs_load_ctx is used for init, load, unload and dump code paths. */
    3836                 :            : 
    3837                 :            : struct spdk_bs_load_ctx {
    3838                 :            :         struct spdk_blob_store          *bs;
    3839                 :            :         struct spdk_bs_super_block      *super;
    3840                 :            : 
    3841                 :            :         struct spdk_bs_md_mask          *mask;
    3842                 :            :         bool                            in_page_chain;
    3843                 :            :         uint32_t                        page_index;
    3844                 :            :         uint32_t                        cur_page;
    3845                 :            :         struct spdk_blob_md_page        *page;
    3846                 :            : 
    3847                 :            :         uint64_t                        num_extent_pages;
    3848                 :            :         uint32_t                        *extent_page_num;
    3849                 :            :         struct spdk_blob_md_page        *extent_pages;
    3850                 :            :         struct spdk_bit_array           *used_clusters;
    3851                 :            : 
    3852                 :            :         spdk_bs_sequence_t                      *seq;
    3853                 :            :         spdk_blob_op_with_handle_complete       iter_cb_fn;
    3854                 :            :         void                                    *iter_cb_arg;
    3855                 :            :         struct spdk_blob                        *blob;
    3856                 :            :         spdk_blob_id                            blobid;
    3857                 :            : 
    3858                 :            :         bool                                    force_recover;
    3859                 :            : 
    3860                 :            :         /* These fields are used in the spdk_bs_dump path. */
    3861                 :            :         bool                                    dumping;
    3862                 :            :         FILE                                    *fp;
    3863                 :            :         spdk_bs_dump_print_xattr                print_xattr_fn;
    3864                 :            :         char                                    xattr_name[4096];
    3865                 :            : };
    3866                 :            : 
    3867                 :            : static int
    3868                 :      10590 : bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs,
    3869                 :            :          struct spdk_bs_load_ctx **_ctx)
    3870                 :            : {
    3871                 :            :         struct spdk_blob_store  *bs;
    3872                 :            :         struct spdk_bs_load_ctx *ctx;
    3873                 :            :         uint64_t dev_size;
    3874                 :            :         int rc;
    3875                 :            : 
    3876                 :      10590 :         dev_size = dev->blocklen * dev->blockcnt;
    3877         [ +  + ]:      10590 :         if (dev_size < opts->cluster_sz) {
    3878                 :            :                 /* Device size cannot be smaller than cluster size of blobstore */
    3879   [ -  +  -  + ]:         48 :                 SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n",
    3880                 :            :                              dev_size, opts->cluster_sz);
    3881                 :         48 :                 return -ENOSPC;
    3882                 :            :         }
    3883         [ +  + ]:      10542 :         if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
    3884                 :            :                 /* Cluster size cannot be smaller than page size */
    3885                 :         16 :                 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n",
    3886                 :            :                             opts->cluster_sz, SPDK_BS_PAGE_SIZE);
    3887                 :         16 :                 return -EINVAL;
    3888                 :            :         }
    3889                 :      10526 :         bs = calloc(1, sizeof(struct spdk_blob_store));
    3890         [ -  + ]:      10526 :         if (!bs) {
    3891                 :          0 :                 return -ENOMEM;
    3892                 :            :         }
    3893                 :            : 
    3894                 :      10526 :         ctx = calloc(1, sizeof(struct spdk_bs_load_ctx));
    3895         [ -  + ]:      10526 :         if (!ctx) {
    3896                 :          0 :                 free(bs);
    3897                 :          0 :                 return -ENOMEM;
    3898                 :            :         }
    3899                 :            : 
    3900                 :      10526 :         ctx->bs = bs;
    3901                 :      10526 :         ctx->iter_cb_fn = opts->iter_cb_fn;
    3902                 :      10526 :         ctx->iter_cb_arg = opts->iter_cb_arg;
    3903                 :      10526 :         ctx->force_recover = opts->force_recover;
    3904                 :            : 
    3905                 :      10526 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    3906                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    3907         [ -  + ]:      10526 :         if (!ctx->super) {
    3908                 :          0 :                 free(ctx);
    3909                 :          0 :                 free(bs);
    3910                 :          0 :                 return -ENOMEM;
    3911                 :            :         }
    3912                 :            : 
    3913                 :      10526 :         RB_INIT(&bs->open_blobs);
    3914                 :      10526 :         TAILQ_INIT(&bs->snapshots);
    3915                 :      10526 :         bs->dev = dev;
    3916                 :      10526 :         bs->md_thread = spdk_get_thread();
    3917         [ -  + ]:      10526 :         assert(bs->md_thread != NULL);
    3918                 :            : 
    3919                 :            :         /*
    3920                 :            :          * Do not use bs_lba_to_cluster() here since blockcnt may not be an
    3921                 :            :          *  even multiple of the cluster size.
    3922                 :            :          */
    3923                 :      10526 :         bs->cluster_sz = opts->cluster_sz;
    3924   [ -  +  -  + ]:      10526 :         bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
    3925                 :      10526 :         ctx->used_clusters = spdk_bit_array_create(bs->total_clusters);
    3926         [ -  + ]:      10526 :         if (!ctx->used_clusters) {
    3927                 :          0 :                 spdk_free(ctx->super);
    3928                 :          0 :                 free(ctx);
    3929                 :          0 :                 free(bs);
    3930                 :          0 :                 return -ENOMEM;
    3931                 :            :         }
    3932                 :            : 
    3933                 :      10526 :         bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
    3934         [ +  - ]:      10526 :         if (spdk_u32_is_pow2(bs->pages_per_cluster)) {
    3935                 :      10526 :                 bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster);
    3936                 :            :         }
    3937                 :      10526 :         bs->num_free_clusters = bs->total_clusters;
    3938                 :      10526 :         bs->io_unit_size = dev->blocklen;
    3939                 :            : 
    3940                 :      10526 :         bs->max_channel_ops = opts->max_channel_ops;
    3941                 :      10526 :         bs->super_blob = SPDK_BLOBID_INVALID;
    3942                 :      10526 :         memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
    3943                 :      10526 :         bs->esnap_bs_dev_create = opts->esnap_bs_dev_create;
    3944                 :      10526 :         bs->esnap_ctx = opts->esnap_ctx;
    3945                 :            : 
    3946                 :            :         /* The metadata is assumed to be at least 1 page */
    3947                 :      10526 :         bs->used_md_pages = spdk_bit_array_create(1);
    3948                 :      10526 :         bs->used_blobids = spdk_bit_array_create(0);
    3949                 :      10526 :         bs->open_blobids = spdk_bit_array_create(0);
    3950                 :            : 
    3951                 :      10526 :         spdk_spin_init(&bs->used_lock);
    3952                 :            : 
    3953                 :      10526 :         spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy,
    3954                 :            :                                 sizeof(struct spdk_bs_channel), "blobstore");
    3955                 :      10526 :         rc = bs_register_md_thread(bs);
    3956         [ -  + ]:      10526 :         if (rc == -1) {
    3957                 :          0 :                 spdk_io_device_unregister(bs, NULL);
    3958                 :          0 :                 spdk_spin_destroy(&bs->used_lock);
    3959                 :          0 :                 spdk_bit_array_free(&bs->open_blobids);
    3960                 :          0 :                 spdk_bit_array_free(&bs->used_blobids);
    3961                 :          0 :                 spdk_bit_array_free(&bs->used_md_pages);
    3962                 :          0 :                 spdk_bit_array_free(&ctx->used_clusters);
    3963                 :          0 :                 spdk_free(ctx->super);
    3964                 :          0 :                 free(ctx);
    3965                 :          0 :                 free(bs);
    3966                 :            :                 /* FIXME: this is a lie but don't know how to get a proper error code here */
    3967                 :          0 :                 return -ENOMEM;
    3968                 :            :         }
    3969                 :            : 
    3970                 :      10526 :         *_ctx = ctx;
    3971                 :      10526 :         *_bs = bs;
    3972                 :      10526 :         return 0;
    3973                 :            : }
    3974                 :            : 
    3975                 :            : static void
    3976                 :       7264 : bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno)
    3977                 :            : {
    3978         [ -  + ]:       7264 :         assert(bserrno != 0);
    3979                 :            : 
    3980                 :       7264 :         spdk_free(ctx->super);
    3981                 :       7264 :         bs_sequence_finish(ctx->seq, bserrno);
    3982                 :       7264 :         bs_free(ctx->bs);
    3983                 :       7264 :         spdk_bit_array_free(&ctx->used_clusters);
    3984                 :       7264 :         free(ctx);
    3985                 :       7264 : }
    3986                 :            : 
    3987                 :            : static void
    3988                 :       3630 : bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    3989                 :            :                struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    3990                 :            : {
    3991                 :            :         /* Update the values in the super block */
    3992                 :       3630 :         super->super_blob = bs->super_blob;
    3993                 :       3630 :         memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
    3994                 :       3630 :         super->crc = blob_md_page_calc_crc(super);
    3995                 :       3630 :         bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0),
    3996                 :       3630 :                               bs_byte_to_lba(bs, sizeof(*super)),
    3997                 :            :                               cb_fn, cb_arg);
    3998                 :       3630 : }
    3999                 :            : 
    4000                 :            : static void
    4001                 :       3185 : bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
    4002                 :            : {
    4003                 :       3185 :         struct spdk_bs_load_ctx *ctx = arg;
    4004                 :            :         uint64_t        mask_size, lba, lba_count;
    4005                 :            : 
    4006                 :            :         /* Write out the used clusters mask */
    4007                 :       3185 :         mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
    4008                 :       3185 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
    4009                 :            :                                  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4010         [ -  + ]:       3185 :         if (!ctx->mask) {
    4011                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4012                 :          0 :                 return;
    4013                 :            :         }
    4014                 :            : 
    4015                 :       3185 :         ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
    4016                 :       3185 :         ctx->mask->length = ctx->bs->total_clusters;
    4017                 :            :         /* We could get here through the normal unload path, or through dirty
    4018                 :            :          * shutdown recovery.  For the normal unload path, we use the mask from
    4019                 :            :          * the bit pool.  For dirty shutdown recovery, we don't have a bit pool yet -
    4020                 :            :          * only the bit array from the load ctx.
    4021                 :            :          */
    4022         [ +  + ]:       3185 :         if (ctx->bs->used_clusters) {
    4023         [ -  + ]:       2756 :                 assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters));
    4024                 :       2756 :                 spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask);
    4025                 :            :         } else {
    4026         [ -  + ]:        429 :                 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters));
    4027                 :        429 :                 spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask);
    4028                 :            :         }
    4029                 :       3185 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    4030                 :       3185 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    4031                 :       3185 :         bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
    4032                 :            : }
    4033                 :            : 
    4034                 :            : static void
    4035                 :       3185 : bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
    4036                 :            : {
    4037                 :       3185 :         struct spdk_bs_load_ctx *ctx = arg;
    4038                 :            :         uint64_t        mask_size, lba, lba_count;
    4039                 :            : 
    4040                 :       3185 :         mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
    4041                 :       3185 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
    4042                 :            :                                  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4043         [ -  + ]:       3185 :         if (!ctx->mask) {
    4044                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4045                 :          0 :                 return;
    4046                 :            :         }
    4047                 :            : 
    4048                 :       3185 :         ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
    4049                 :       3185 :         ctx->mask->length = ctx->super->md_len;
    4050         [ -  + ]:       3185 :         assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
    4051                 :            : 
    4052                 :       3185 :         spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask);
    4053                 :       3185 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
    4054                 :       3185 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
    4055                 :       3185 :         bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
    4056                 :            : }
    4057                 :            : 
    4058                 :            : static void
    4059                 :       3185 : bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
    4060                 :            : {
    4061                 :       3185 :         struct spdk_bs_load_ctx *ctx = arg;
    4062                 :            :         uint64_t        mask_size, lba, lba_count;
    4063                 :            : 
    4064         [ +  + ]:       3185 :         if (ctx->super->used_blobid_mask_len == 0) {
    4065                 :            :                 /*
    4066                 :            :                  * This is a pre-v3 on-disk format where the blobid mask does not get
    4067                 :            :                  *  written to disk.
    4068                 :            :                  */
    4069                 :         96 :                 cb_fn(seq, arg, 0);
    4070                 :         96 :                 return;
    4071                 :            :         }
    4072                 :            : 
    4073                 :       3089 :         mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
    4074                 :       3089 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    4075                 :            :                                  SPDK_MALLOC_DMA);
    4076         [ -  + ]:       3089 :         if (!ctx->mask) {
    4077                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4078                 :          0 :                 return;
    4079                 :            :         }
    4080                 :            : 
    4081                 :       3089 :         ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS;
    4082                 :       3089 :         ctx->mask->length = ctx->super->md_len;
    4083         [ -  + ]:       3089 :         assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids));
    4084                 :            : 
    4085                 :       3089 :         spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask);
    4086                 :       3089 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
    4087                 :       3089 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
    4088                 :       3089 :         bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
    4089                 :            : }
    4090                 :            : 
    4091                 :            : static void
    4092                 :       2952 : blob_set_thin_provision(struct spdk_blob *blob)
    4093                 :            : {
    4094                 :       2952 :         blob_verify_md_op(blob);
    4095                 :       2952 :         blob->invalid_flags |= SPDK_BLOB_THIN_PROV;
    4096                 :       2952 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    4097                 :       2952 : }
    4098                 :            : 
    4099                 :            : static void
    4100                 :       9599 : blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method)
    4101                 :            : {
    4102                 :       9599 :         blob_verify_md_op(blob);
    4103                 :       9599 :         blob->clear_method = clear_method;
    4104                 :       9599 :         blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT);
    4105                 :       9599 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    4106                 :       9599 : }
    4107                 :            : 
    4108                 :            : static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno);
    4109                 :            : 
    4110                 :            : static void
    4111                 :         96 : bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno)
    4112                 :            : {
    4113                 :         96 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4114                 :            :         spdk_blob_id id;
    4115                 :            :         int64_t page_num;
    4116                 :            : 
    4117                 :            :         /* Iterate to next blob (we can't use spdk_bs_iter_next function as our
    4118                 :            :          * last blob has been removed */
    4119                 :         96 :         page_num = bs_blobid_to_page(ctx->blobid);
    4120                 :         96 :         page_num++;
    4121                 :         96 :         page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num);
    4122         [ +  - ]:         96 :         if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) {
    4123                 :         96 :                 bs_load_iter(ctx, NULL, -ENOENT);
    4124                 :         96 :                 return;
    4125                 :            :         }
    4126                 :            : 
    4127                 :          0 :         id = bs_page_to_blobid(page_num);
    4128                 :            : 
    4129                 :          0 :         spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx);
    4130                 :            : }
    4131                 :            : 
    4132                 :            : static void
    4133                 :         96 : bs_delete_corrupted_close_cb(void *cb_arg, int bserrno)
    4134                 :            : {
    4135                 :         96 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4136                 :            : 
    4137         [ -  + ]:         96 :         if (bserrno != 0) {
    4138                 :          0 :                 SPDK_ERRLOG("Failed to close corrupted blob\n");
    4139                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4140                 :          0 :                 return;
    4141                 :            :         }
    4142                 :            : 
    4143                 :         96 :         spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx);
    4144                 :            : }
    4145                 :            : 
    4146                 :            : static void
    4147                 :         96 : bs_delete_corrupted_blob(void *cb_arg, int bserrno)
    4148                 :            : {
    4149                 :         96 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4150                 :            :         uint64_t i;
    4151                 :            : 
    4152         [ -  + ]:         96 :         if (bserrno != 0) {
    4153                 :          0 :                 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
    4154                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4155                 :          0 :                 return;
    4156                 :            :         }
    4157                 :            : 
    4158                 :            :         /* Snapshot and clone have the same copy of cluster map and extent pages
    4159                 :            :          * at this point. Let's clear both for snapshot now,
    4160                 :            :          * so that it won't be cleared for clone later when we remove snapshot.
    4161                 :            :          * Also set thin provision to pass data corruption check */
    4162         [ +  + ]:       1056 :         for (i = 0; i < ctx->blob->active.num_clusters; i++) {
    4163                 :        960 :                 ctx->blob->active.clusters[i] = 0;
    4164                 :            :         }
    4165         [ +  + ]:        144 :         for (i = 0; i < ctx->blob->active.num_extent_pages; i++) {
    4166                 :         48 :                 ctx->blob->active.extent_pages[i] = 0;
    4167                 :            :         }
    4168                 :            : 
    4169                 :         96 :         ctx->blob->active.num_allocated_clusters = 0;
    4170                 :            : 
    4171                 :         96 :         ctx->blob->md_ro = false;
    4172                 :            : 
    4173                 :         96 :         blob_set_thin_provision(ctx->blob);
    4174                 :            : 
    4175                 :         96 :         ctx->blobid = ctx->blob->id;
    4176                 :            : 
    4177                 :         96 :         spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx);
    4178                 :            : }
    4179                 :            : 
    4180                 :            : static void
    4181                 :         48 : bs_update_corrupted_blob(void *cb_arg, int bserrno)
    4182                 :            : {
    4183                 :         48 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4184                 :            : 
    4185         [ -  + ]:         48 :         if (bserrno != 0) {
    4186                 :          0 :                 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
    4187                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4188                 :          0 :                 return;
    4189                 :            :         }
    4190                 :            : 
    4191                 :         48 :         ctx->blob->md_ro = false;
    4192                 :         48 :         blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true);
    4193                 :         48 :         blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true);
    4194                 :         48 :         spdk_blob_set_read_only(ctx->blob);
    4195                 :            : 
    4196         [ -  + ]:         48 :         if (ctx->iter_cb_fn) {
    4197                 :          0 :                 ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0);
    4198                 :            :         }
    4199                 :         48 :         bs_blob_list_add(ctx->blob);
    4200                 :            : 
    4201                 :         48 :         spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4202                 :            : }
    4203                 :            : 
    4204                 :            : static void
    4205                 :        144 : bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno)
    4206                 :            : {
    4207                 :        144 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4208                 :            : 
    4209         [ -  + ]:        144 :         if (bserrno != 0) {
    4210                 :          0 :                 SPDK_ERRLOG("Failed to open clone of a corrupted blob\n");
    4211                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4212                 :          0 :                 return;
    4213                 :            :         }
    4214                 :            : 
    4215         [ +  + ]:        144 :         if (blob->parent_id == ctx->blob->id) {
    4216                 :            :                 /* Power failure occurred before updating clone (snapshot delete case)
    4217                 :            :                  * or after updating clone (creating snapshot case) - keep snapshot */
    4218                 :         48 :                 spdk_blob_close(blob, bs_update_corrupted_blob, ctx);
    4219                 :            :         } else {
    4220                 :            :                 /* Power failure occurred after updating clone (snapshot delete case)
    4221                 :            :                  * or before updating clone (creating snapshot case) - remove snapshot */
    4222                 :         96 :                 spdk_blob_close(blob, bs_delete_corrupted_blob, ctx);
    4223                 :            :         }
    4224                 :            : }
    4225                 :            : 
    4226                 :            : static void
    4227                 :       4671 : bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno)
    4228                 :            : {
    4229                 :       4671 :         struct spdk_bs_load_ctx *ctx = arg;
    4230                 :       4631 :         const void *value;
    4231                 :       4631 :         size_t len;
    4232                 :       4671 :         int rc = 0;
    4233                 :            : 
    4234         [ +  + ]:       4671 :         if (bserrno == 0) {
    4235                 :            :                 /* Examine blob if it is corrupted after power failure. Fix
    4236                 :            :                  * the ones that can be fixed and remove any other corrupted
    4237                 :            :                  * ones. If it is not corrupted just process it */
    4238                 :       3465 :                 rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true);
    4239         [ +  + ]:       3465 :                 if (rc != 0) {
    4240                 :       3385 :                         rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true);
    4241         [ +  + ]:       3385 :                         if (rc != 0) {
    4242                 :            :                                 /* Not corrupted - process it and continue with iterating through blobs */
    4243         [ +  + ]:       3321 :                                 if (ctx->iter_cb_fn) {
    4244                 :       1318 :                                         ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0);
    4245                 :            :                                 }
    4246                 :       3321 :                                 bs_blob_list_add(blob);
    4247                 :       3321 :                                 spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx);
    4248                 :       3321 :                                 return;
    4249                 :            :                         }
    4250                 :            : 
    4251                 :            :                 }
    4252                 :            : 
    4253         [ -  + ]:        144 :                 assert(len == sizeof(spdk_blob_id));
    4254                 :            : 
    4255                 :        144 :                 ctx->blob = blob;
    4256                 :            : 
    4257                 :            :                 /* Open clone to check if we are able to fix this blob or should we remove it */
    4258                 :        144 :                 spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx);
    4259                 :        144 :                 return;
    4260         [ +  - ]:       1206 :         } else if (bserrno == -ENOENT) {
    4261                 :       1206 :                 bserrno = 0;
    4262                 :            :         } else {
    4263                 :            :                 /*
    4264                 :            :                  * This case needs to be looked at further.  Same problem
    4265                 :            :                  *  exists with applications that rely on explicit blob
    4266                 :            :                  *  iteration.  We should just skip the blob that failed
    4267                 :            :                  *  to load and continue on to the next one.
    4268                 :            :                  */
    4269                 :          0 :                 SPDK_ERRLOG("Error in iterating blobs\n");
    4270                 :            :         }
    4271                 :            : 
    4272                 :       1206 :         ctx->iter_cb_fn = NULL;
    4273                 :            : 
    4274                 :       1206 :         spdk_free(ctx->super);
    4275                 :       1206 :         spdk_free(ctx->mask);
    4276                 :       1206 :         bs_sequence_finish(ctx->seq, bserrno);
    4277                 :       1206 :         free(ctx);
    4278                 :            : }
    4279                 :            : 
    4280                 :            : static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
    4281                 :            : 
    4282                 :            : static void
    4283                 :       1206 : bs_load_complete(struct spdk_bs_load_ctx *ctx)
    4284                 :            : {
    4285                 :       1206 :         ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
    4286   [ -  +  -  + ]:       1206 :         if (ctx->dumping) {
    4287                 :          0 :                 bs_dump_read_md_page(ctx->seq, ctx);
    4288                 :          0 :                 return;
    4289                 :            :         }
    4290                 :       1206 :         spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx);
    4291                 :            : }
    4292                 :            : 
    4293                 :            : static void
    4294                 :        777 : bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4295                 :            : {
    4296                 :        777 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4297                 :            :         int rc;
    4298                 :            : 
    4299                 :            :         /* The type must be correct */
    4300         [ -  + ]:        777 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS);
    4301                 :            : 
    4302                 :            :         /* The length of the mask (in bits) must not be greater than
    4303                 :            :          * the length of the buffer (converted to bits) */
    4304         [ -  + ]:        777 :         assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8));
    4305                 :            : 
    4306                 :            :         /* The length of the mask must be exactly equal to the size
    4307                 :            :          * (in pages) of the metadata region */
    4308         [ -  + ]:        777 :         assert(ctx->mask->length == ctx->super->md_len);
    4309                 :            : 
    4310                 :        777 :         rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length);
    4311         [ -  + ]:        777 :         if (rc < 0) {
    4312                 :          0 :                 spdk_free(ctx->mask);
    4313                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4314                 :          0 :                 return;
    4315                 :            :         }
    4316                 :            : 
    4317                 :        777 :         spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask);
    4318                 :        777 :         bs_load_complete(ctx);
    4319                 :            : }
    4320                 :            : 
    4321                 :            : static void
    4322                 :        777 : bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4323                 :            : {
    4324                 :        777 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4325                 :            :         uint64_t                lba, lba_count, mask_size;
    4326                 :            :         int                     rc;
    4327                 :            : 
    4328         [ -  + ]:        777 :         if (bserrno != 0) {
    4329                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4330                 :          0 :                 return;
    4331                 :            :         }
    4332                 :            : 
    4333                 :            :         /* The type must be correct */
    4334         [ -  + ]:        777 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
    4335                 :            :         /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
    4336         [ -  + ]:        777 :         assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
    4337                 :            :                                              struct spdk_blob_md_page) * 8));
    4338                 :            :         /*
    4339                 :            :          * The length of the mask must be equal to or larger than the total number of clusters. It may be
    4340                 :            :          * larger than the total number of clusters due to a failure spdk_bs_grow.
    4341                 :            :          */
    4342         [ -  + ]:        777 :         assert(ctx->mask->length >= ctx->bs->total_clusters);
    4343         [ +  + ]:        777 :         if (ctx->mask->length > ctx->bs->total_clusters) {
    4344                 :         16 :                 SPDK_WARNLOG("Shrink the used_custers mask length to total_clusters");
    4345                 :         16 :                 ctx->mask->length = ctx->bs->total_clusters;
    4346                 :            :         }
    4347                 :            : 
    4348                 :        777 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length);
    4349         [ -  + ]:        777 :         if (rc < 0) {
    4350                 :          0 :                 spdk_free(ctx->mask);
    4351                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4352                 :          0 :                 return;
    4353                 :            :         }
    4354                 :            : 
    4355                 :        777 :         spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask);
    4356                 :        777 :         ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters);
    4357         [ -  + ]:        777 :         assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
    4358                 :            : 
    4359                 :        777 :         spdk_free(ctx->mask);
    4360                 :            : 
    4361                 :            :         /* Read the used blobids mask */
    4362                 :        777 :         mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
    4363                 :        777 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    4364                 :            :                                  SPDK_MALLOC_DMA);
    4365         [ -  + ]:        777 :         if (!ctx->mask) {
    4366                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4367                 :          0 :                 return;
    4368                 :            :         }
    4369                 :        777 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
    4370                 :        777 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
    4371                 :        777 :         bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
    4372                 :            :                              bs_load_used_blobids_cpl, ctx);
    4373                 :            : }
    4374                 :            : 
    4375                 :            : static void
    4376                 :        777 : bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4377                 :            : {
    4378                 :        777 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4379                 :            :         uint64_t                lba, lba_count, mask_size;
    4380                 :            :         int                     rc;
    4381                 :            : 
    4382         [ -  + ]:        777 :         if (bserrno != 0) {
    4383                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4384                 :          0 :                 return;
    4385                 :            :         }
    4386                 :            : 
    4387                 :            :         /* The type must be correct */
    4388         [ -  + ]:        777 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
    4389                 :            :         /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
    4390         [ -  + ]:        777 :         assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
    4391                 :            :                                      8));
    4392                 :            :         /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
    4393         [ -  + ]:        777 :         if (ctx->mask->length != ctx->super->md_len) {
    4394                 :          0 :                 SPDK_ERRLOG("mismatched md_len in used_pages mask: "
    4395                 :            :                             "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n",
    4396                 :            :                             ctx->mask->length, ctx->super->md_len);
    4397                 :          0 :                 assert(false);
    4398                 :            :         }
    4399                 :            : 
    4400                 :        777 :         rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
    4401         [ -  + ]:        777 :         if (rc < 0) {
    4402                 :          0 :                 spdk_free(ctx->mask);
    4403                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4404                 :          0 :                 return;
    4405                 :            :         }
    4406                 :            : 
    4407                 :        777 :         spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask);
    4408                 :        777 :         spdk_free(ctx->mask);
    4409                 :            : 
    4410                 :            :         /* Read the used clusters mask */
    4411                 :        777 :         mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
    4412                 :        777 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    4413                 :            :                                  SPDK_MALLOC_DMA);
    4414         [ -  + ]:        777 :         if (!ctx->mask) {
    4415                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4416                 :          0 :                 return;
    4417                 :            :         }
    4418                 :        777 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    4419                 :        777 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    4420                 :        777 :         bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
    4421                 :            :                              bs_load_used_clusters_cpl, ctx);
    4422                 :            : }
    4423                 :            : 
    4424                 :            : static void
    4425                 :        777 : bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx)
    4426                 :            : {
    4427                 :            :         uint64_t lba, lba_count, mask_size;
    4428                 :            : 
    4429                 :            :         /* Read the used pages mask */
    4430                 :        777 :         mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
    4431                 :        777 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
    4432                 :            :                                  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4433         [ -  + ]:        777 :         if (!ctx->mask) {
    4434                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4435                 :          0 :                 return;
    4436                 :            :         }
    4437                 :            : 
    4438                 :        777 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
    4439                 :        777 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
    4440                 :        777 :         bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
    4441                 :            :                              bs_load_used_pages_cpl, ctx);
    4442                 :            : }
    4443                 :            : 
    4444                 :            : static int
    4445                 :       1000 : bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page)
    4446                 :            : {
    4447                 :       1000 :         struct spdk_blob_store *bs = ctx->bs;
    4448                 :            :         struct spdk_blob_md_descriptor *desc;
    4449                 :       1000 :         size_t  cur_desc = 0;
    4450                 :            : 
    4451                 :       1000 :         desc = (struct spdk_blob_md_descriptor *)page->descriptors;
    4452         [ +  - ]:       2934 :         while (cur_desc < sizeof(page->descriptors)) {
    4453         [ +  + ]:       2934 :                 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
    4454         [ +  - ]:        920 :                         if (desc->length == 0) {
    4455                 :            :                                 /* If padding and length are 0, this terminates the page */
    4456                 :        920 :                                 break;
    4457                 :            :                         }
    4458         [ +  + ]:       2014 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
    4459                 :            :                         struct spdk_blob_md_descriptor_extent_rle       *desc_extent_rle;
    4460                 :            :                         unsigned int                            i, j;
    4461                 :        272 :                         unsigned int                            cluster_count = 0;
    4462                 :            :                         uint32_t                                cluster_idx;
    4463                 :            : 
    4464                 :        272 :                         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
    4465                 :            : 
    4466         [ +  + ]:        544 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
    4467         [ +  + ]:       3312 :                                 for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
    4468                 :       3040 :                                         cluster_idx = desc_extent_rle->extents[i].cluster_idx;
    4469                 :            :                                         /*
    4470                 :            :                                          * cluster_idx = 0 means an unallocated cluster - don't mark that
    4471                 :            :                                          * in the used cluster map.
    4472                 :            :                                          */
    4473         [ +  + ]:       3040 :                                         if (cluster_idx != 0) {
    4474                 :       2160 :                                                 SPDK_NOTICELOG("Recover: cluster %" PRIu32 "\n", cluster_idx + j);
    4475                 :       2160 :                                                 spdk_bit_array_set(ctx->used_clusters, cluster_idx + j);
    4476         [ -  + ]:       2160 :                                                 if (bs->num_free_clusters == 0) {
    4477                 :          0 :                                                         return -ENOSPC;
    4478                 :            :                                                 }
    4479                 :       2160 :                                                 bs->num_free_clusters--;
    4480                 :            :                                         }
    4481                 :       3040 :                                         cluster_count++;
    4482                 :            :                                 }
    4483                 :            :                         }
    4484         [ -  + ]:        272 :                         if (cluster_count == 0) {
    4485                 :          0 :                                 return -EINVAL;
    4486                 :            :                         }
    4487         [ +  + ]:       1742 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
    4488                 :            :                         struct spdk_blob_md_descriptor_extent_page      *desc_extent;
    4489                 :            :                         uint32_t                                        i;
    4490                 :        214 :                         uint32_t                                        cluster_count = 0;
    4491                 :            :                         uint32_t                                        cluster_idx;
    4492                 :            :                         size_t                                          cluster_idx_length;
    4493                 :            : 
    4494                 :        214 :                         desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
    4495                 :        214 :                         cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
    4496                 :            : 
    4497         [ +  - ]:        214 :                         if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
    4498         [ -  + ]:        214 :                             (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
    4499                 :          0 :                                 return -EINVAL;
    4500                 :            :                         }
    4501                 :            : 
    4502         [ +  + ]:       3534 :                         for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
    4503                 :       3320 :                                 cluster_idx = desc_extent->cluster_idx[i];
    4504                 :            :                                 /*
    4505                 :            :                                  * cluster_idx = 0 means an unallocated cluster - don't mark that
    4506                 :            :                                  * in the used cluster map.
    4507                 :            :                                  */
    4508         [ +  + ]:       3320 :                                 if (cluster_idx != 0) {
    4509         [ +  + ]:       2554 :                                         if (cluster_idx < desc_extent->start_cluster_idx &&
    4510         [ -  + ]:          1 :                                             cluster_idx >= desc_extent->start_cluster_idx + cluster_count) {
    4511                 :          0 :                                                 return -EINVAL;
    4512                 :            :                                         }
    4513                 :       2554 :                                         spdk_bit_array_set(ctx->used_clusters, cluster_idx);
    4514         [ -  + ]:       2554 :                                         if (bs->num_free_clusters == 0) {
    4515                 :          0 :                                                 return -ENOSPC;
    4516                 :            :                                         }
    4517                 :       2554 :                                         bs->num_free_clusters--;
    4518                 :            :                                 }
    4519                 :       3320 :                                 cluster_count++;
    4520                 :            :                         }
    4521                 :            : 
    4522         [ -  + ]:        214 :                         if (cluster_count == 0) {
    4523                 :          0 :                                 return -EINVAL;
    4524                 :            :                         }
    4525         [ +  + ]:       1528 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
    4526                 :            :                         /* Skip this item */
    4527         [ +  + ]:       1204 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
    4528                 :            :                         /* Skip this item */
    4529         [ +  + ]:        964 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
    4530                 :            :                         /* Skip this item */
    4531         [ +  - ]:        338 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
    4532                 :            :                         struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
    4533                 :        338 :                         uint32_t num_extent_pages = ctx->num_extent_pages;
    4534                 :            :                         uint32_t i;
    4535                 :            :                         size_t extent_pages_length;
    4536                 :            :                         void *tmp;
    4537                 :            : 
    4538                 :        338 :                         desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
    4539                 :        338 :                         extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
    4540                 :            : 
    4541         [ +  - ]:        338 :                         if (desc_extent_table->length == 0 ||
    4542         [ -  + ]:        338 :                             (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
    4543                 :          0 :                                 return -EINVAL;
    4544                 :            :                         }
    4545                 :            : 
    4546         [ +  + ]:        657 :                         for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
    4547         [ +  + ]:        319 :                                 if (desc_extent_table->extent_page[i].page_idx != 0) {
    4548         [ -  + ]:        214 :                                         if (desc_extent_table->extent_page[i].num_pages != 1) {
    4549                 :          0 :                                                 return -EINVAL;
    4550                 :            :                                         }
    4551                 :        214 :                                         num_extent_pages += 1;
    4552                 :            :                                 }
    4553                 :            :                         }
    4554                 :            : 
    4555         [ +  + ]:        338 :                         if (num_extent_pages > 0) {
    4556                 :        213 :                                 tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t));
    4557         [ -  + ]:        213 :                                 if (tmp == NULL) {
    4558                 :          0 :                                         return -ENOMEM;
    4559                 :            :                                 }
    4560                 :        213 :                                 ctx->extent_page_num = tmp;
    4561                 :            : 
    4562                 :            :                                 /* Extent table entries contain md page numbers for extent pages.
    4563                 :            :                                  * Zeroes represent unallocated extent pages, those are run-length-encoded.
    4564                 :            :                                  */
    4565         [ +  + ]:        428 :                                 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
    4566         [ +  + ]:        215 :                                         if (desc_extent_table->extent_page[i].page_idx != 0) {
    4567                 :        214 :                                                 ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx;
    4568                 :        214 :                                                 ctx->num_extent_pages += 1;
    4569                 :            :                                         }
    4570                 :            :                                 }
    4571                 :            :                         }
    4572                 :            :                 } else {
    4573                 :            :                         /* Error */
    4574                 :          0 :                         return -EINVAL;
    4575                 :            :                 }
    4576                 :            :                 /* Advance to the next descriptor */
    4577                 :       2014 :                 cur_desc += sizeof(*desc) + desc->length;
    4578         [ +  + ]:       2014 :                 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
    4579                 :         80 :                         break;
    4580                 :            :                 }
    4581                 :       1934 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
    4582                 :            :         }
    4583                 :       1000 :         return 0;
    4584                 :            : }
    4585                 :            : 
    4586                 :            : static bool
    4587                 :      12082 : bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page)
    4588                 :            : {
    4589                 :            :         uint32_t crc;
    4590                 :      12082 :         struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors;
    4591                 :            :         size_t desc_len;
    4592                 :            : 
    4593                 :      12082 :         crc = blob_md_page_calc_crc(page);
    4594         [ -  + ]:      12082 :         if (crc != page->crc) {
    4595                 :          0 :                 return false;
    4596                 :            :         }
    4597                 :            : 
    4598                 :            :         /* Extent page should always be of sequence num 0. */
    4599         [ +  + ]:      12082 :         if (page->sequence_num != 0) {
    4600                 :        176 :                 return false;
    4601                 :            :         }
    4602                 :            : 
    4603                 :            :         /* Descriptor type must be EXTENT_PAGE. */
    4604         [ +  + ]:      11906 :         if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
    4605                 :        626 :                 return false;
    4606                 :            :         }
    4607                 :            : 
    4608                 :            :         /* Descriptor length cannot exceed the page. */
    4609                 :      11280 :         desc_len = sizeof(*desc) + desc->length;
    4610         [ -  + ]:      11280 :         if (desc_len > sizeof(page->descriptors)) {
    4611                 :          0 :                 return false;
    4612                 :            :         }
    4613                 :            : 
    4614                 :            :         /* It has to be the only descriptor in the page. */
    4615         [ +  - ]:      11280 :         if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) {
    4616                 :      11280 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len);
    4617         [ -  + ]:      11280 :                 if (desc->length != 0) {
    4618                 :          0 :                         return false;
    4619                 :            :                 }
    4620                 :            :         }
    4621                 :            : 
    4622                 :      11280 :         return true;
    4623                 :            : }
    4624                 :            : 
    4625                 :            : static bool
    4626                 :      28890 : bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
    4627                 :            : {
    4628                 :            :         uint32_t crc;
    4629                 :      28890 :         struct spdk_blob_md_page *page = ctx->page;
    4630                 :            : 
    4631                 :      28890 :         crc = blob_md_page_calc_crc(page);
    4632         [ +  + ]:      28890 :         if (crc != page->crc) {
    4633                 :      28016 :                 return false;
    4634                 :            :         }
    4635                 :            : 
    4636                 :            :         /* First page of a sequence should match the blobid. */
    4637         [ +  + ]:        874 :         if (page->sequence_num == 0 &&
    4638         [ +  + ]:        698 :             bs_page_to_blobid(ctx->cur_page) != page->id) {
    4639                 :         72 :                 return false;
    4640                 :            :         }
    4641         [ -  + ]:        802 :         assert(bs_load_cur_extent_page_valid(page) == false);
    4642                 :            : 
    4643                 :        802 :         return true;
    4644                 :            : }
    4645                 :            : 
    4646                 :            : static void bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx);
    4647                 :            : 
    4648                 :            : static void
    4649                 :        429 : bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4650                 :            : {
    4651                 :        429 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4652                 :            : 
    4653         [ -  + ]:        429 :         if (bserrno != 0) {
    4654                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4655                 :          0 :                 return;
    4656                 :            :         }
    4657                 :            : 
    4658                 :        429 :         bs_load_complete(ctx);
    4659                 :            : }
    4660                 :            : 
    4661                 :            : static void
    4662                 :        429 : bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4663                 :            : {
    4664                 :        429 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4665                 :            : 
    4666                 :        429 :         spdk_free(ctx->mask);
    4667                 :        429 :         ctx->mask = NULL;
    4668                 :            : 
    4669         [ -  + ]:        429 :         if (bserrno != 0) {
    4670                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4671                 :          0 :                 return;
    4672                 :            :         }
    4673                 :            : 
    4674                 :        429 :         bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl);
    4675                 :            : }
    4676                 :            : 
    4677                 :            : static void
    4678                 :        429 : bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4679                 :            : {
    4680                 :        429 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4681                 :            : 
    4682                 :        429 :         spdk_free(ctx->mask);
    4683                 :        429 :         ctx->mask = NULL;
    4684                 :            : 
    4685         [ -  + ]:        429 :         if (bserrno != 0) {
    4686                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4687                 :          0 :                 return;
    4688                 :            :         }
    4689                 :            : 
    4690                 :        429 :         bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl);
    4691                 :            : }
    4692                 :            : 
    4693                 :            : static void
    4694                 :        429 : bs_load_write_used_md(struct spdk_bs_load_ctx *ctx)
    4695                 :            : {
    4696                 :        429 :         bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl);
    4697                 :        429 : }
    4698                 :            : 
    4699                 :            : static void
    4700                 :      28730 : bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx)
    4701                 :            : {
    4702                 :            :         uint64_t num_md_clusters;
    4703                 :            :         uint64_t i;
    4704                 :            : 
    4705                 :      28730 :         ctx->in_page_chain = false;
    4706                 :            : 
    4707                 :            :         do {
    4708                 :      29016 :                 ctx->page_index++;
    4709         [ +  + ]:      29016 :         } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
    4710                 :            : 
    4711         [ +  + ]:      28730 :         if (ctx->page_index < ctx->super->md_len) {
    4712                 :      28301 :                 ctx->cur_page = ctx->page_index;
    4713                 :      28301 :                 bs_load_replay_cur_md_page(ctx);
    4714                 :            :         } else {
    4715                 :            :                 /* Claim all of the clusters used by the metadata */
    4716                 :        429 :                 num_md_clusters = spdk_divide_round_up(
    4717                 :        646 :                                           ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster);
    4718         [ +  + ]:       1931 :                 for (i = 0; i < num_md_clusters; i++) {
    4719                 :       1502 :                         spdk_bit_array_set(ctx->used_clusters, i);
    4720                 :            :                 }
    4721                 :        429 :                 ctx->bs->num_free_clusters -= num_md_clusters;
    4722                 :        429 :                 spdk_free(ctx->page);
    4723                 :        429 :                 bs_load_write_used_md(ctx);
    4724                 :            :         }
    4725                 :      28730 : }
    4726                 :            : 
    4727                 :            : static void
    4728                 :        213 : bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4729                 :            : {
    4730                 :        213 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4731                 :            :         uint32_t page_num;
    4732                 :            :         uint64_t i;
    4733                 :            : 
    4734         [ -  + ]:        213 :         if (bserrno != 0) {
    4735                 :          0 :                 spdk_free(ctx->extent_pages);
    4736                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4737                 :          0 :                 return;
    4738                 :            :         }
    4739                 :            : 
    4740         [ +  + ]:        427 :         for (i = 0; i < ctx->num_extent_pages; i++) {
    4741                 :            :                 /* Extent pages are only read when present within in chain md.
    4742                 :            :                  * Integrity of md is not right if that page was not a valid extent page. */
    4743         [ -  + ]:        214 :                 if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) {
    4744                 :          0 :                         spdk_free(ctx->extent_pages);
    4745                 :          0 :                         bs_load_ctx_fail(ctx, -EILSEQ);
    4746                 :          0 :                         return;
    4747                 :            :                 }
    4748                 :            : 
    4749                 :        214 :                 page_num = ctx->extent_page_num[i];
    4750                 :        214 :                 spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
    4751         [ -  + ]:        214 :                 if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) {
    4752                 :          0 :                         spdk_free(ctx->extent_pages);
    4753                 :          0 :                         bs_load_ctx_fail(ctx, -EILSEQ);
    4754                 :          0 :                         return;
    4755                 :            :                 }
    4756                 :            :         }
    4757                 :            : 
    4758                 :        213 :         spdk_free(ctx->extent_pages);
    4759                 :        213 :         free(ctx->extent_page_num);
    4760                 :        213 :         ctx->extent_page_num = NULL;
    4761                 :        213 :         ctx->num_extent_pages = 0;
    4762                 :            : 
    4763                 :        213 :         bs_load_replay_md_chain_cpl(ctx);
    4764                 :            : }
    4765                 :            : 
    4766                 :            : static void
    4767                 :        213 : bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx)
    4768                 :            : {
    4769                 :            :         spdk_bs_batch_t *batch;
    4770                 :            :         uint32_t page;
    4771                 :            :         uint64_t lba;
    4772                 :            :         uint64_t i;
    4773                 :            : 
    4774                 :        213 :         ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0,
    4775                 :            :                                          NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4776         [ -  + ]:        213 :         if (!ctx->extent_pages) {
    4777                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4778                 :          0 :                 return;
    4779                 :            :         }
    4780                 :            : 
    4781                 :        213 :         batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx);
    4782                 :            : 
    4783         [ +  + ]:        427 :         for (i = 0; i < ctx->num_extent_pages; i++) {
    4784                 :        214 :                 page = ctx->extent_page_num[i];
    4785         [ -  + ]:        214 :                 assert(page < ctx->super->md_len);
    4786                 :        214 :                 lba = bs_md_page_to_lba(ctx->bs, page);
    4787                 :        214 :                 bs_batch_read_dev(batch, &ctx->extent_pages[i], lba,
    4788                 :        214 :                                   bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE));
    4789                 :            :         }
    4790                 :            : 
    4791                 :        213 :         bs_batch_close(batch);
    4792                 :            : }
    4793                 :            : 
    4794                 :            : static void
    4795                 :      28890 : bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4796                 :            : {
    4797                 :      28890 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4798                 :            :         uint32_t page_num;
    4799                 :            :         struct spdk_blob_md_page *page;
    4800                 :            : 
    4801         [ -  + ]:      28890 :         if (bserrno != 0) {
    4802                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4803                 :          0 :                 return;
    4804                 :            :         }
    4805                 :            : 
    4806                 :      28890 :         page_num = ctx->cur_page;
    4807                 :      28890 :         page = ctx->page;
    4808         [ +  + ]:      28890 :         if (bs_load_cur_md_page_valid(ctx) == true) {
    4809   [ +  +  +  +  :        802 :                 if (page->sequence_num == 0 || ctx->in_page_chain == true) {
                   +  + ]
    4810                 :        786 :                         spdk_spin_lock(&ctx->bs->used_lock);
    4811                 :        786 :                         bs_claim_md_page(ctx->bs, page_num);
    4812                 :        786 :                         spdk_spin_unlock(&ctx->bs->used_lock);
    4813         [ +  + ]:        786 :                         if (page->sequence_num == 0) {
    4814                 :        626 :                                 SPDK_NOTICELOG("Recover: blob 0x%" PRIx32 "\n", page_num);
    4815                 :        626 :                                 spdk_bit_array_set(ctx->bs->used_blobids, page_num);
    4816                 :            :                         }
    4817         [ -  + ]:        786 :                         if (bs_load_replay_md_parse_page(ctx, page)) {
    4818                 :          0 :                                 bs_load_ctx_fail(ctx, -EILSEQ);
    4819                 :          0 :                                 return;
    4820                 :            :                         }
    4821         [ +  + ]:        786 :                         if (page->next != SPDK_INVALID_MD_PAGE) {
    4822                 :        160 :                                 ctx->in_page_chain = true;
    4823                 :        160 :                                 ctx->cur_page = page->next;
    4824                 :        160 :                                 bs_load_replay_cur_md_page(ctx);
    4825                 :        160 :                                 return;
    4826                 :            :                         }
    4827         [ +  + ]:        626 :                         if (ctx->num_extent_pages != 0) {
    4828                 :        213 :                                 bs_load_replay_extent_pages(ctx);
    4829                 :        213 :                                 return;
    4830                 :            :                         }
    4831                 :            :                 }
    4832                 :            :         }
    4833                 :      28517 :         bs_load_replay_md_chain_cpl(ctx);
    4834                 :            : }
    4835                 :            : 
    4836                 :            : static void
    4837                 :      28890 : bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx)
    4838                 :            : {
    4839                 :            :         uint64_t lba;
    4840                 :            : 
    4841         [ -  + ]:      28890 :         assert(ctx->cur_page < ctx->super->md_len);
    4842                 :      28890 :         lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page);
    4843                 :      28890 :         bs_sequence_read_dev(ctx->seq, ctx->page, lba,
    4844                 :      28890 :                              bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
    4845                 :            :                              bs_load_replay_md_cpl, ctx);
    4846                 :      28890 : }
    4847                 :            : 
    4848                 :            : static void
    4849                 :        429 : bs_load_replay_md(struct spdk_bs_load_ctx *ctx)
    4850                 :            : {
    4851                 :        429 :         ctx->page_index = 0;
    4852                 :        429 :         ctx->cur_page = 0;
    4853                 :        429 :         ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
    4854                 :            :                                  NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4855         [ -  + ]:        429 :         if (!ctx->page) {
    4856                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4857                 :          0 :                 return;
    4858                 :            :         }
    4859                 :        429 :         bs_load_replay_cur_md_page(ctx);
    4860                 :            : }
    4861                 :            : 
    4862                 :            : static void
    4863                 :        429 : bs_recover(struct spdk_bs_load_ctx *ctx)
    4864                 :            : {
    4865                 :            :         int             rc;
    4866                 :            : 
    4867                 :        429 :         SPDK_NOTICELOG("Performing recovery on blobstore\n");
    4868                 :        429 :         rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
    4869         [ -  + ]:        429 :         if (rc < 0) {
    4870                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4871                 :          0 :                 return;
    4872                 :            :         }
    4873                 :            : 
    4874                 :        429 :         rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len);
    4875         [ -  + ]:        429 :         if (rc < 0) {
    4876                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4877                 :          0 :                 return;
    4878                 :            :         }
    4879                 :            : 
    4880                 :        429 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
    4881         [ -  + ]:        429 :         if (rc < 0) {
    4882                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4883                 :          0 :                 return;
    4884                 :            :         }
    4885                 :            : 
    4886                 :        429 :         rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len);
    4887         [ -  + ]:        429 :         if (rc < 0) {
    4888                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4889                 :          0 :                 return;
    4890                 :            :         }
    4891                 :            : 
    4892                 :        429 :         ctx->bs->num_free_clusters = ctx->bs->total_clusters;
    4893                 :        429 :         bs_load_replay_md(ctx);
    4894                 :            : }
    4895                 :            : 
    4896                 :            : static int
    4897                 :       1190 : bs_parse_super(struct spdk_bs_load_ctx *ctx)
    4898                 :            : {
    4899                 :            :         int rc;
    4900                 :            : 
    4901         [ +  + ]:       1190 :         if (ctx->super->size == 0) {
    4902                 :         32 :                 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    4903                 :            :         }
    4904                 :            : 
    4905         [ +  + ]:       1190 :         if (ctx->super->io_unit_size == 0) {
    4906                 :         32 :                 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
    4907                 :            :         }
    4908                 :            : 
    4909                 :       1190 :         ctx->bs->clean = 1;
    4910                 :       1190 :         ctx->bs->cluster_sz = ctx->super->cluster_size;
    4911         [ -  + ]:       1190 :         ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
    4912                 :       1190 :         ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
    4913         [ +  - ]:       1190 :         if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
    4914                 :       1190 :                 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
    4915                 :            :         }
    4916                 :       1190 :         ctx->bs->io_unit_size = ctx->super->io_unit_size;
    4917                 :       1190 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
    4918         [ -  + ]:       1190 :         if (rc < 0) {
    4919                 :          0 :                 return -ENOMEM;
    4920                 :            :         }
    4921                 :       1190 :         ctx->bs->md_start = ctx->super->md_start;
    4922                 :       1190 :         ctx->bs->md_len = ctx->super->md_len;
    4923                 :       1190 :         rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
    4924         [ -  + ]:       1190 :         if (rc < 0) {
    4925                 :          0 :                 return -ENOMEM;
    4926                 :            :         }
    4927                 :            : 
    4928                 :       3012 :         ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
    4929                 :       1822 :                                                ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
    4930                 :       1190 :         ctx->bs->super_blob = ctx->super->super_blob;
    4931                 :       1190 :         memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
    4932                 :            : 
    4933                 :       1190 :         return 0;
    4934                 :            : }
    4935                 :            : 
    4936                 :            : static void
    4937                 :       8454 : bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4938                 :            : {
    4939                 :       8454 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4940                 :            :         int rc;
    4941                 :            : 
    4942                 :       8454 :         rc = bs_super_validate(ctx->super, ctx->bs);
    4943         [ +  + ]:       8454 :         if (rc != 0) {
    4944                 :       7264 :                 bs_load_ctx_fail(ctx, rc);
    4945                 :       7264 :                 return;
    4946                 :            :         }
    4947                 :            : 
    4948                 :       1190 :         rc = bs_parse_super(ctx);
    4949         [ -  + ]:       1190 :         if (rc < 0) {
    4950                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4951                 :          0 :                 return;
    4952                 :            :         }
    4953                 :            : 
    4954   [ +  +  +  +  :       1190 :         if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0 || ctx->force_recover) {
             -  +  -  + ]
    4955                 :        429 :                 bs_recover(ctx);
    4956                 :            :         } else {
    4957                 :        761 :                 bs_load_read_used_pages(ctx);
    4958                 :            :         }
    4959                 :            : }
    4960                 :            : 
    4961                 :            : static inline int
    4962                 :       8673 : bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst)
    4963                 :            : {
    4964                 :            : 
    4965         [ -  + ]:       8673 :         if (!src->opts_size) {
    4966                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
    4967                 :          0 :                 return -1;
    4968                 :            :         }
    4969                 :            : 
    4970                 :            : #define FIELD_OK(field) \
    4971                 :            :         offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size
    4972                 :            : 
    4973                 :            : #define SET_FIELD(field) \
    4974                 :            :         if (FIELD_OK(field)) { \
    4975                 :            :                 dst->field = src->field; \
    4976                 :            :         } \
    4977                 :            : 
    4978         [ +  - ]:       8673 :         SET_FIELD(cluster_sz);
    4979         [ +  - ]:       8673 :         SET_FIELD(num_md_pages);
    4980         [ +  - ]:       8673 :         SET_FIELD(max_md_ops);
    4981         [ +  - ]:       8673 :         SET_FIELD(max_channel_ops);
    4982         [ +  - ]:       8673 :         SET_FIELD(clear_method);
    4983                 :            : 
    4984         [ +  - ]:       8673 :         if (FIELD_OK(bstype)) {
    4985                 :       8673 :                 memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype));
    4986                 :            :         }
    4987         [ +  - ]:       8673 :         SET_FIELD(iter_cb_fn);
    4988         [ +  - ]:       8673 :         SET_FIELD(iter_cb_arg);
    4989         [ +  - ]:       8673 :         SET_FIELD(force_recover);
    4990         [ +  - ]:       8673 :         SET_FIELD(esnap_bs_dev_create);
    4991         [ +  - ]:       8673 :         SET_FIELD(esnap_ctx);
    4992                 :            : 
    4993                 :       8673 :         dst->opts_size = src->opts_size;
    4994                 :            : 
    4995                 :            :         /* You should not remove this statement, but need to update the assert statement
    4996                 :            :          * if you add a new field, and also add a corresponding SET_FIELD statement */
    4997                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 88, "Incorrect size");
    4998                 :            : 
    4999                 :            : #undef FIELD_OK
    5000                 :            : #undef SET_FIELD
    5001                 :            : 
    5002                 :       8673 :         return 0;
    5003                 :            : }
    5004                 :            : 
    5005                 :            : void
    5006                 :       8550 : spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
    5007                 :            :              spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
    5008                 :            : {
    5009                 :       5810 :         struct spdk_blob_store  *bs;
    5010                 :       5810 :         struct spdk_bs_cpl      cpl;
    5011                 :       5810 :         struct spdk_bs_load_ctx *ctx;
    5012                 :       8550 :         struct spdk_bs_opts     opts = {};
    5013                 :            :         int err;
    5014                 :            : 
    5015   [ -  +  -  + ]:       8550 :         SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
    5016                 :            : 
    5017   [ +  +  +  + ]:       8550 :         if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
    5018   [ -  +  -  + ]:         16 :                 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
    5019                 :         16 :                 dev->destroy(dev);
    5020                 :         16 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5021                 :         38 :                 return;
    5022                 :            :         }
    5023                 :            : 
    5024                 :       8534 :         spdk_bs_opts_init(&opts, sizeof(opts));
    5025         [ +  + ]:       8534 :         if (o) {
    5026         [ -  + ]:       7779 :                 if (bs_opts_copy(o, &opts)) {
    5027                 :          0 :                         return;
    5028                 :            :                 }
    5029                 :            :         }
    5030                 :            : 
    5031   [ +  +  +  + ]:       8534 :         if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
    5032                 :         32 :                 dev->destroy(dev);
    5033                 :         32 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5034                 :         32 :                 return;
    5035                 :            :         }
    5036                 :            : 
    5037                 :       8502 :         err = bs_alloc(dev, &opts, &bs, &ctx);
    5038         [ +  + ]:       8502 :         if (err) {
    5039                 :         48 :                 dev->destroy(dev);
    5040                 :         48 :                 cb_fn(cb_arg, NULL, err);
    5041                 :         48 :                 return;
    5042                 :            :         }
    5043                 :            : 
    5044                 :       8454 :         cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
    5045                 :       8454 :         cpl.u.bs_handle.cb_fn = cb_fn;
    5046                 :       8454 :         cpl.u.bs_handle.cb_arg = cb_arg;
    5047                 :       8454 :         cpl.u.bs_handle.bs = bs;
    5048                 :            : 
    5049                 :       8454 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5050         [ -  + ]:       8454 :         if (!ctx->seq) {
    5051                 :          0 :                 spdk_free(ctx->super);
    5052                 :          0 :                 free(ctx);
    5053                 :          0 :                 bs_free(bs);
    5054                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5055                 :          0 :                 return;
    5056                 :            :         }
    5057                 :            : 
    5058                 :            :         /* Read the super block */
    5059                 :       8454 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    5060                 :       8454 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    5061                 :            :                              bs_load_super_cpl, ctx);
    5062                 :            : }
    5063                 :            : 
    5064                 :            : /* END spdk_bs_load */
    5065                 :            : 
    5066                 :            : /* START spdk_bs_dump */
    5067                 :            : 
    5068                 :            : static void
    5069                 :          0 : bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno)
    5070                 :            : {
    5071                 :          0 :         spdk_free(ctx->super);
    5072                 :            : 
    5073                 :            :         /*
    5074                 :            :          * We need to defer calling bs_call_cpl() until after
    5075                 :            :          * dev destruction, so tuck these away for later use.
    5076                 :            :          */
    5077                 :          0 :         ctx->bs->unload_err = bserrno;
    5078   [ #  #  #  # ]:          0 :         memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
    5079                 :          0 :         seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
    5080                 :            : 
    5081                 :          0 :         bs_sequence_finish(seq, 0);
    5082                 :          0 :         bs_free(ctx->bs);
    5083                 :          0 :         free(ctx);
    5084                 :          0 : }
    5085                 :            : 
    5086                 :            : static void
    5087                 :          0 : bs_dump_print_xattr(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
    5088                 :            : {
    5089                 :            :         struct spdk_blob_md_descriptor_xattr *desc_xattr;
    5090                 :            :         uint32_t i;
    5091                 :            :         const char *type;
    5092                 :            : 
    5093                 :          0 :         desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc;
    5094                 :            : 
    5095                 :          0 :         if (desc_xattr->length !=
    5096                 :            :             sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) +
    5097                 :          0 :             desc_xattr->name_length + desc_xattr->value_length) {
    5098                 :            :         }
    5099                 :            : 
    5100   [ #  #  #  # ]:          0 :         memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length);
    5101                 :          0 :         ctx->xattr_name[desc_xattr->name_length] = '\0';
    5102         [ #  # ]:          0 :         if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
    5103                 :          0 :                 type = "XATTR";
    5104         [ #  # ]:          0 :         } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
    5105                 :          0 :                 type = "XATTR_INTERNAL";
    5106                 :            :         } else {
    5107                 :          0 :                 assert(false);
    5108                 :            :                 type = "XATTR_?";
    5109                 :            :         }
    5110   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "%s: name = \"%s\"\n", type, ctx->xattr_name);
    5111   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "       value = \"");
    5112                 :          0 :         ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name,
    5113                 :          0 :                             (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
    5114                 :          0 :                             desc_xattr->value_length);
    5115   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\"\n");
    5116         [ #  # ]:          0 :         for (i = 0; i < desc_xattr->value_length; i++) {
    5117         [ #  # ]:          0 :                 if (i % 16 == 0) {
    5118   [ #  #  #  # ]:          0 :                         fprintf(ctx->fp, "               ");
    5119                 :            :                 }
    5120   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i));
    5121         [ #  # ]:          0 :                 if ((i + 1) % 16 == 0) {
    5122         [ #  # ]:          0 :                         fprintf(ctx->fp, "\n");
    5123                 :            :                 }
    5124                 :            :         }
    5125         [ #  # ]:          0 :         if (i % 16 != 0) {
    5126         [ #  # ]:          0 :                 fprintf(ctx->fp, "\n");
    5127                 :            :         }
    5128                 :          0 : }
    5129                 :            : 
    5130                 :            : struct type_flag_desc {
    5131                 :            :         uint64_t mask;
    5132                 :            :         uint64_t val;
    5133                 :            :         const char *name;
    5134                 :            : };
    5135                 :            : 
    5136                 :            : static void
    5137                 :          0 : bs_dump_print_type_bits(struct spdk_bs_load_ctx *ctx, uint64_t flags,
    5138                 :            :                         struct type_flag_desc *desc, size_t numflags)
    5139                 :            : {
    5140                 :          0 :         uint64_t covered = 0;
    5141                 :            :         size_t i;
    5142                 :            : 
    5143         [ #  # ]:          0 :         for (i = 0; i < numflags; i++) {
    5144         [ #  # ]:          0 :                 if ((desc[i].mask & flags) != desc[i].val) {
    5145                 :          0 :                         continue;
    5146                 :            :                 }
    5147   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " %s", desc[i].val, desc[i].name);
    5148         [ #  # ]:          0 :                 if (desc[i].mask != desc[i].val) {
    5149   [ #  #  #  # ]:          0 :                         fprintf(ctx->fp, " (mask 0x%" PRIx64 " value 0x%" PRIx64 ")",
    5150                 :          0 :                                 desc[i].mask, desc[i].val);
    5151                 :            :                 }
    5152         [ #  # ]:          0 :                 fprintf(ctx->fp, "\n");
    5153                 :          0 :                 covered |= desc[i].mask;
    5154                 :            :         }
    5155         [ #  # ]:          0 :         if ((flags & ~covered) != 0) {
    5156   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " Unknown\n", flags & ~covered);
    5157                 :            :         }
    5158                 :          0 : }
    5159                 :            : 
    5160                 :            : static void
    5161                 :          0 : bs_dump_print_type_flags(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
    5162                 :            : {
    5163                 :            :         struct spdk_blob_md_descriptor_flags *type_desc;
    5164                 :            : #define ADD_FLAG(f) { f, f, #f }
    5165                 :            : #define ADD_MASK_VAL(m, v) { m, v, #v }
    5166                 :            :         static struct type_flag_desc invalid[] = {
    5167                 :            :                 ADD_FLAG(SPDK_BLOB_THIN_PROV),
    5168                 :            :                 ADD_FLAG(SPDK_BLOB_INTERNAL_XATTR),
    5169                 :            :                 ADD_FLAG(SPDK_BLOB_EXTENT_TABLE),
    5170                 :            :         };
    5171                 :            :         static struct type_flag_desc data_ro[] = {
    5172                 :            :                 ADD_FLAG(SPDK_BLOB_READ_ONLY),
    5173                 :            :         };
    5174                 :            :         static struct type_flag_desc md_ro[] = {
    5175                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_DEFAULT),
    5176                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_NONE),
    5177                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_UNMAP),
    5178                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_WRITE_ZEROES),
    5179                 :            :         };
    5180                 :            : #undef ADD_FLAG
    5181                 :            : #undef ADD_MASK_VAL
    5182                 :            : 
    5183                 :          0 :         type_desc = (struct spdk_blob_md_descriptor_flags *)desc;
    5184   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Flags:\n");
    5185   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\tinvalid: 0x%016" PRIx64 "\n", type_desc->invalid_flags);
    5186                 :          0 :         bs_dump_print_type_bits(ctx, type_desc->invalid_flags, invalid,
    5187                 :            :                                 SPDK_COUNTOF(invalid));
    5188   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\tdata_ro: 0x%016" PRIx64 "\n", type_desc->data_ro_flags);
    5189                 :          0 :         bs_dump_print_type_bits(ctx, type_desc->data_ro_flags, data_ro,
    5190                 :            :                                 SPDK_COUNTOF(data_ro));
    5191   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\t  md_ro: 0x%016" PRIx64 "\n", type_desc->md_ro_flags);
    5192                 :          0 :         bs_dump_print_type_bits(ctx, type_desc->md_ro_flags, md_ro,
    5193                 :            :                                 SPDK_COUNTOF(md_ro));
    5194                 :          0 : }
    5195                 :            : 
    5196                 :            : static void
    5197                 :          0 : bs_dump_print_extent_table(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
    5198                 :            : {
    5199                 :            :         struct spdk_blob_md_descriptor_extent_table *et_desc;
    5200                 :            :         uint64_t num_extent_pages;
    5201                 :            :         uint32_t et_idx;
    5202                 :            : 
    5203                 :          0 :         et_desc = (struct spdk_blob_md_descriptor_extent_table *)desc;
    5204                 :          0 :         num_extent_pages = (et_desc->length - sizeof(et_desc->num_clusters)) /
    5205                 :            :                            sizeof(et_desc->extent_page[0]);
    5206                 :            : 
    5207   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Extent table:\n");
    5208         [ #  # ]:          0 :         for (et_idx = 0; et_idx < num_extent_pages; et_idx++) {
    5209         [ #  # ]:          0 :                 if (et_desc->extent_page[et_idx].page_idx == 0) {
    5210                 :            :                         /* Zeroes represent unallocated extent pages. */
    5211                 :          0 :                         continue;
    5212                 :            :                 }
    5213   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "\tExtent page: %5" PRIu32 " length %3" PRIu32
    5214                 :            :                         " at LBA %" PRIu64 "\n", et_desc->extent_page[et_idx].page_idx,
    5215                 :            :                         et_desc->extent_page[et_idx].num_pages,
    5216                 :            :                         bs_md_page_to_lba(ctx->bs, et_desc->extent_page[et_idx].page_idx));
    5217                 :            :         }
    5218                 :          0 : }
    5219                 :            : 
    5220                 :            : static void
    5221                 :          0 : bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx)
    5222                 :            : {
    5223                 :          0 :         uint32_t page_idx = ctx->cur_page;
    5224                 :          0 :         struct spdk_blob_md_page *page = ctx->page;
    5225                 :            :         struct spdk_blob_md_descriptor *desc;
    5226                 :          0 :         size_t cur_desc = 0;
    5227                 :            :         uint32_t crc;
    5228                 :            : 
    5229   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "=========\n");
    5230   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx);
    5231   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Start LBA: %" PRIu64 "\n", bs_md_page_to_lba(ctx->bs, page_idx));
    5232   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id);
    5233   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Sequence: %" PRIu32 "\n", page->sequence_num);
    5234         [ #  # ]:          0 :         if (page->next == SPDK_INVALID_MD_PAGE) {
    5235   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "Next: None\n");
    5236                 :            :         } else {
    5237   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "Next: %" PRIu32 "\n", page->next);
    5238                 :            :         }
    5239   [ #  #  #  #  :          0 :         fprintf(ctx->fp, "In used bit array%s:", ctx->super->clean ? "" : " (not clean: dubious)");
                   #  # ]
    5240         [ #  # ]:          0 :         if (spdk_bit_array_get(ctx->bs->used_md_pages, page_idx)) {
    5241   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, " md");
    5242                 :            :         }
    5243         [ #  # ]:          0 :         if (spdk_bit_array_get(ctx->bs->used_blobids, page_idx)) {
    5244   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, " blob");
    5245                 :            :         }
    5246         [ #  # ]:          0 :         fprintf(ctx->fp, "\n");
    5247                 :            : 
    5248                 :          0 :         crc = blob_md_page_calc_crc(page);
    5249   [ #  #  #  #  :          0 :         fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch");
                   #  # ]
    5250                 :            : 
    5251                 :          0 :         desc = (struct spdk_blob_md_descriptor *)page->descriptors;
    5252         [ #  # ]:          0 :         while (cur_desc < sizeof(page->descriptors)) {
    5253         [ #  # ]:          0 :                 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
    5254         [ #  # ]:          0 :                         if (desc->length == 0) {
    5255                 :            :                                 /* If padding and length are 0, this terminates the page */
    5256                 :          0 :                                 break;
    5257                 :            :                         }
    5258         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
    5259                 :            :                         struct spdk_blob_md_descriptor_extent_rle       *desc_extent_rle;
    5260                 :            :                         unsigned int                            i;
    5261                 :            : 
    5262                 :          0 :                         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
    5263                 :            : 
    5264         [ #  # ]:          0 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
    5265         [ #  # ]:          0 :                                 if (desc_extent_rle->extents[i].cluster_idx != 0) {
    5266   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
    5267                 :            :                                                 desc_extent_rle->extents[i].cluster_idx);
    5268                 :            :                                 } else {
    5269   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Unallocated Extent - ");
    5270                 :            :                                 }
    5271   [ #  #  #  # ]:          0 :                                 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length);
    5272         [ #  # ]:          0 :                                 fprintf(ctx->fp, "\n");
    5273                 :            :                         }
    5274         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
    5275                 :            :                         struct spdk_blob_md_descriptor_extent_page      *desc_extent;
    5276                 :            :                         unsigned int                                    i;
    5277                 :            : 
    5278                 :          0 :                         desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
    5279                 :            : 
    5280         [ #  # ]:          0 :                         for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) {
    5281         [ #  # ]:          0 :                                 if (desc_extent->cluster_idx[i] != 0) {
    5282   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
    5283                 :            :                                                 desc_extent->cluster_idx[i]);
    5284                 :            :                                 } else {
    5285   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Unallocated Extent");
    5286                 :            :                                 }
    5287         [ #  # ]:          0 :                                 fprintf(ctx->fp, "\n");
    5288                 :            :                         }
    5289         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
    5290                 :          0 :                         bs_dump_print_xattr(ctx, desc);
    5291         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
    5292                 :          0 :                         bs_dump_print_xattr(ctx, desc);
    5293         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
    5294                 :          0 :                         bs_dump_print_type_flags(ctx, desc);
    5295         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
    5296                 :          0 :                         bs_dump_print_extent_table(ctx, desc);
    5297                 :            :                 } else {
    5298                 :            :                         /* Error */
    5299   [ #  #  #  # ]:          0 :                         fprintf(ctx->fp, "Unknown descriptor type %" PRIu8 "\n", desc->type);
    5300                 :            :                 }
    5301                 :            :                 /* Advance to the next descriptor */
    5302                 :          0 :                 cur_desc += sizeof(*desc) + desc->length;
    5303         [ #  # ]:          0 :                 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
    5304                 :          0 :                         break;
    5305                 :            :                 }
    5306                 :          0 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
    5307                 :            :         }
    5308                 :          0 : }
    5309                 :            : 
    5310                 :            : static void
    5311                 :          0 : bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5312                 :            : {
    5313                 :          0 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5314                 :            : 
    5315         [ #  # ]:          0 :         if (bserrno != 0) {
    5316                 :          0 :                 bs_dump_finish(seq, ctx, bserrno);
    5317                 :          0 :                 return;
    5318                 :            :         }
    5319                 :            : 
    5320         [ #  # ]:          0 :         if (ctx->page->id != 0) {
    5321                 :          0 :                 bs_dump_print_md_page(ctx);
    5322                 :            :         }
    5323                 :            : 
    5324                 :          0 :         ctx->cur_page++;
    5325                 :            : 
    5326         [ #  # ]:          0 :         if (ctx->cur_page < ctx->super->md_len) {
    5327                 :          0 :                 bs_dump_read_md_page(seq, ctx);
    5328                 :            :         } else {
    5329                 :          0 :                 spdk_free(ctx->page);
    5330                 :          0 :                 bs_dump_finish(seq, ctx, 0);
    5331                 :            :         }
    5332                 :            : }
    5333                 :            : 
    5334                 :            : static void
    5335                 :          0 : bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
    5336                 :            : {
    5337                 :          0 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5338                 :            :         uint64_t lba;
    5339                 :            : 
    5340         [ #  # ]:          0 :         assert(ctx->cur_page < ctx->super->md_len);
    5341                 :          0 :         lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
    5342                 :          0 :         bs_sequence_read_dev(seq, ctx->page, lba,
    5343                 :          0 :                              bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
    5344                 :            :                              bs_dump_read_md_page_cpl, ctx);
    5345                 :          0 : }
    5346                 :            : 
    5347                 :            : static void
    5348                 :          0 : bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5349                 :            : {
    5350                 :          0 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5351                 :            :         int rc;
    5352                 :            : 
    5353         [ #  # ]:          0 :         fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature);
    5354   [ #  #  #  # ]:          0 :         if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
    5355                 :            :                    sizeof(ctx->super->signature)) != 0) {
    5356         [ #  # ]:          0 :                 fprintf(ctx->fp, "(Mismatch)\n");
    5357                 :          0 :                 bs_dump_finish(seq, ctx, bserrno);
    5358                 :          0 :                 return;
    5359                 :            :         } else {
    5360         [ #  # ]:          0 :                 fprintf(ctx->fp, "(OK)\n");
    5361                 :            :         }
    5362         [ #  # ]:          0 :         fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version);
    5363   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc,
    5364                 :          0 :                 (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch");
    5365         [ #  # ]:          0 :         fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype);
    5366         [ #  # ]:          0 :         fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size);
    5367         [ #  # ]:          0 :         fprintf(ctx->fp, "Super Blob ID: ");
    5368         [ #  # ]:          0 :         if (ctx->super->super_blob == SPDK_BLOBID_INVALID) {
    5369         [ #  # ]:          0 :                 fprintf(ctx->fp, "(None)\n");
    5370                 :            :         } else {
    5371         [ #  # ]:          0 :                 fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob);
    5372                 :            :         }
    5373         [ #  # ]:          0 :         fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean);
    5374         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start);
    5375         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len);
    5376         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start);
    5377         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len);
    5378         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start);
    5379         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len);
    5380         [ #  # ]:          0 :         fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start);
    5381         [ #  # ]:          0 :         fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len);
    5382                 :            : 
    5383                 :          0 :         ctx->cur_page = 0;
    5384                 :          0 :         ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
    5385                 :            :                                  NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5386         [ #  # ]:          0 :         if (!ctx->page) {
    5387                 :          0 :                 bs_dump_finish(seq, ctx, -ENOMEM);
    5388                 :          0 :                 return;
    5389                 :            :         }
    5390                 :            : 
    5391                 :          0 :         rc = bs_parse_super(ctx);
    5392         [ #  # ]:          0 :         if (rc < 0) {
    5393                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    5394                 :          0 :                 return;
    5395                 :            :         }
    5396                 :            : 
    5397                 :          0 :         bs_load_read_used_pages(ctx);
    5398                 :            : }
    5399                 :            : 
    5400                 :            : void
    5401                 :          0 : spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn,
    5402                 :            :              spdk_bs_op_complete cb_fn, void *cb_arg)
    5403                 :            : {
    5404                 :          0 :         struct spdk_blob_store  *bs;
    5405                 :          0 :         struct spdk_bs_cpl      cpl;
    5406                 :          0 :         struct spdk_bs_load_ctx *ctx;
    5407                 :          0 :         struct spdk_bs_opts     opts = {};
    5408                 :            :         int err;
    5409                 :            : 
    5410   [ #  #  #  # ]:          0 :         SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev);
    5411                 :            : 
    5412                 :          0 :         spdk_bs_opts_init(&opts, sizeof(opts));
    5413                 :            : 
    5414                 :          0 :         err = bs_alloc(dev, &opts, &bs, &ctx);
    5415         [ #  # ]:          0 :         if (err) {
    5416                 :          0 :                 dev->destroy(dev);
    5417                 :          0 :                 cb_fn(cb_arg, err);
    5418                 :          0 :                 return;
    5419                 :            :         }
    5420                 :            : 
    5421                 :          0 :         ctx->dumping = true;
    5422                 :          0 :         ctx->fp = fp;
    5423                 :          0 :         ctx->print_xattr_fn = print_xattr_fn;
    5424                 :            : 
    5425                 :          0 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5426                 :          0 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5427                 :          0 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5428                 :            : 
    5429                 :          0 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5430         [ #  # ]:          0 :         if (!ctx->seq) {
    5431                 :          0 :                 spdk_free(ctx->super);
    5432                 :          0 :                 free(ctx);
    5433                 :          0 :                 bs_free(bs);
    5434                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5435                 :          0 :                 return;
    5436                 :            :         }
    5437                 :            : 
    5438                 :            :         /* Read the super block */
    5439                 :          0 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    5440                 :          0 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    5441                 :            :                              bs_dump_super_cpl, ctx);
    5442                 :            : }
    5443                 :            : 
    5444                 :            : /* END spdk_bs_dump */
    5445                 :            : 
    5446                 :            : /* START spdk_bs_init */
    5447                 :            : 
    5448                 :            : static void
    5449                 :       2039 : bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5450                 :            : {
    5451                 :       2039 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5452                 :            : 
    5453                 :       2039 :         ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
    5454                 :       2039 :         spdk_free(ctx->super);
    5455                 :       2039 :         free(ctx);
    5456                 :            : 
    5457                 :       2039 :         bs_sequence_finish(seq, bserrno);
    5458                 :       2039 : }
    5459                 :            : 
    5460                 :            : static void
    5461                 :       2039 : bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5462                 :            : {
    5463                 :       2039 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5464                 :            : 
    5465                 :            :         /* Write super block */
    5466                 :       2039 :         bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
    5467                 :       2039 :                               bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
    5468                 :            :                               bs_init_persist_super_cpl, ctx);
    5469                 :       2039 : }
    5470                 :            : 
    5471                 :            : void
    5472                 :       2104 : spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
    5473                 :            :              spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
    5474                 :            : {
    5475                 :       2068 :         struct spdk_bs_load_ctx *ctx;
    5476                 :       2068 :         struct spdk_blob_store  *bs;
    5477                 :       2068 :         struct spdk_bs_cpl      cpl;
    5478                 :            :         spdk_bs_sequence_t      *seq;
    5479                 :            :         spdk_bs_batch_t         *batch;
    5480                 :            :         uint64_t                num_md_lba;
    5481                 :            :         uint64_t                num_md_pages;
    5482                 :            :         uint64_t                num_md_clusters;
    5483                 :            :         uint64_t                max_used_cluster_mask_len;
    5484                 :            :         uint32_t                i;
    5485                 :       2104 :         struct spdk_bs_opts     opts = {};
    5486                 :            :         int                     rc;
    5487                 :            :         uint64_t                lba, lba_count;
    5488                 :            : 
    5489   [ -  +  -  + ]:       2104 :         SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev);
    5490                 :            : 
    5491   [ +  +  +  + ]:       2104 :         if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
    5492                 :         16 :                 SPDK_ERRLOG("unsupported dev block length of %d\n",
    5493                 :            :                             dev->blocklen);
    5494                 :         16 :                 dev->destroy(dev);
    5495                 :         16 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5496                 :         16 :                 return;
    5497                 :            :         }
    5498                 :            : 
    5499                 :       2088 :         spdk_bs_opts_init(&opts, sizeof(opts));
    5500         [ +  + ]:       2088 :         if (o) {
    5501         [ -  + ]:        878 :                 if (bs_opts_copy(o, &opts)) {
    5502                 :          0 :                         return;
    5503                 :            :                 }
    5504                 :            :         }
    5505                 :            : 
    5506         [ +  + ]:       2088 :         if (bs_opts_verify(&opts) != 0) {
    5507                 :         16 :                 dev->destroy(dev);
    5508                 :         16 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5509                 :         16 :                 return;
    5510                 :            :         }
    5511                 :            : 
    5512                 :       2072 :         rc = bs_alloc(dev, &opts, &bs, &ctx);
    5513         [ +  + ]:       2072 :         if (rc) {
    5514                 :         16 :                 dev->destroy(dev);
    5515                 :         16 :                 cb_fn(cb_arg, NULL, rc);
    5516                 :         16 :                 return;
    5517                 :            :         }
    5518                 :            : 
    5519         [ +  + ]:       2056 :         if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
    5520                 :            :                 /* By default, allocate 1 page per cluster.
    5521                 :            :                  * Technically, this over-allocates metadata
    5522                 :            :                  * because more metadata will reduce the number
    5523                 :            :                  * of usable clusters. This can be addressed with
    5524                 :            :                  * more complex math in the future.
    5525                 :            :                  */
    5526                 :       1877 :                 bs->md_len = bs->total_clusters;
    5527                 :            :         } else {
    5528                 :        179 :                 bs->md_len = opts.num_md_pages;
    5529                 :            :         }
    5530                 :       2056 :         rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
    5531         [ -  + ]:       2056 :         if (rc < 0) {
    5532                 :          0 :                 spdk_free(ctx->super);
    5533                 :          0 :                 free(ctx);
    5534                 :          0 :                 bs_free(bs);
    5535                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5536                 :          0 :                 return;
    5537                 :            :         }
    5538                 :            : 
    5539                 :       2056 :         rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len);
    5540         [ -  + ]:       2056 :         if (rc < 0) {
    5541                 :          0 :                 spdk_free(ctx->super);
    5542                 :          0 :                 free(ctx);
    5543                 :          0 :                 bs_free(bs);
    5544                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5545                 :          0 :                 return;
    5546                 :            :         }
    5547                 :            : 
    5548                 :       2056 :         rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len);
    5549         [ -  + ]:       2056 :         if (rc < 0) {
    5550                 :          0 :                 spdk_free(ctx->super);
    5551                 :          0 :                 free(ctx);
    5552                 :          0 :                 bs_free(bs);
    5553                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5554                 :          0 :                 return;
    5555                 :            :         }
    5556                 :            : 
    5557   [ -  +  -  + ]:       2056 :         memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
    5558                 :            :                sizeof(ctx->super->signature));
    5559                 :       2056 :         ctx->super->version = SPDK_BS_VERSION;
    5560                 :       2056 :         ctx->super->length = sizeof(*ctx->super);
    5561                 :       2056 :         ctx->super->super_blob = bs->super_blob;
    5562                 :       2056 :         ctx->super->clean = 0;
    5563                 :       2056 :         ctx->super->cluster_size = bs->cluster_sz;
    5564                 :       2056 :         ctx->super->io_unit_size = bs->io_unit_size;
    5565                 :       2056 :         memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
    5566                 :            : 
    5567                 :            :         /* Calculate how many pages the metadata consumes at the front
    5568                 :            :          * of the disk.
    5569                 :            :          */
    5570                 :            : 
    5571                 :            :         /* The super block uses 1 page */
    5572                 :       2056 :         num_md_pages = 1;
    5573                 :            : 
    5574                 :            :         /* The used_md_pages mask requires 1 bit per metadata page, rounded
    5575                 :            :          * up to the nearest page, plus a header.
    5576                 :            :          */
    5577                 :       2056 :         ctx->super->used_page_mask_start = num_md_pages;
    5578                 :       2056 :         ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5579                 :       2056 :                                          spdk_divide_round_up(bs->md_len, 8),
    5580                 :            :                                          SPDK_BS_PAGE_SIZE);
    5581                 :       2056 :         num_md_pages += ctx->super->used_page_mask_len;
    5582                 :            : 
    5583                 :            :         /* The used_clusters mask requires 1 bit per cluster, rounded
    5584                 :            :          * up to the nearest page, plus a header.
    5585                 :            :          */
    5586                 :       2056 :         ctx->super->used_cluster_mask_start = num_md_pages;
    5587                 :       2056 :         ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5588                 :       2056 :                                             spdk_divide_round_up(bs->total_clusters, 8),
    5589                 :            :                                             SPDK_BS_PAGE_SIZE);
    5590                 :            :         /* The blobstore might be extended, then the used_cluster bitmap will need more space.
    5591                 :            :          * Here we calculate the max clusters we can support according to the
    5592                 :            :          * num_md_pages (bs->md_len).
    5593                 :            :          */
    5594                 :       2056 :         max_used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5595                 :       2056 :                                     spdk_divide_round_up(bs->md_len, 8),
    5596                 :            :                                     SPDK_BS_PAGE_SIZE);
    5597                 :       2056 :         max_used_cluster_mask_len = spdk_max(max_used_cluster_mask_len,
    5598                 :            :                                              ctx->super->used_cluster_mask_len);
    5599                 :       2056 :         num_md_pages += max_used_cluster_mask_len;
    5600                 :            : 
    5601                 :            :         /* The used_blobids mask requires 1 bit per metadata page, rounded
    5602                 :            :          * up to the nearest page, plus a header.
    5603                 :            :          */
    5604                 :       2056 :         ctx->super->used_blobid_mask_start = num_md_pages;
    5605                 :       2056 :         ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5606                 :       2056 :                                            spdk_divide_round_up(bs->md_len, 8),
    5607                 :            :                                            SPDK_BS_PAGE_SIZE);
    5608                 :       2056 :         num_md_pages += ctx->super->used_blobid_mask_len;
    5609                 :            : 
    5610                 :            :         /* The metadata region size was chosen above */
    5611                 :       2056 :         ctx->super->md_start = bs->md_start = num_md_pages;
    5612                 :       2056 :         ctx->super->md_len = bs->md_len;
    5613                 :       2056 :         num_md_pages += bs->md_len;
    5614                 :            : 
    5615                 :       2056 :         num_md_lba = bs_page_to_lba(bs, num_md_pages);
    5616                 :            : 
    5617                 :       2056 :         ctx->super->size = dev->blockcnt * dev->blocklen;
    5618                 :            : 
    5619                 :       2056 :         ctx->super->crc = blob_md_page_calc_crc(ctx->super);
    5620                 :            : 
    5621                 :       2056 :         num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster);
    5622         [ +  + ]:       2056 :         if (num_md_clusters > bs->total_clusters) {
    5623                 :         17 :                 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
    5624                 :            :                             "please decrease number of pages reserved for metadata "
    5625                 :            :                             "or increase cluster size.\n");
    5626                 :         17 :                 spdk_free(ctx->super);
    5627                 :         17 :                 spdk_bit_array_free(&ctx->used_clusters);
    5628                 :         17 :                 free(ctx);
    5629                 :         17 :                 bs_free(bs);
    5630                 :         17 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5631                 :         17 :                 return;
    5632                 :            :         }
    5633                 :            :         /* Claim all of the clusters used by the metadata */
    5634         [ +  + ]:     377374 :         for (i = 0; i < num_md_clusters; i++) {
    5635                 :     375335 :                 spdk_bit_array_set(ctx->used_clusters, i);
    5636                 :            :         }
    5637                 :            : 
    5638                 :       2039 :         bs->num_free_clusters -= num_md_clusters;
    5639                 :       2039 :         bs->total_data_clusters = bs->num_free_clusters;
    5640                 :            : 
    5641                 :       2039 :         cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
    5642                 :       2039 :         cpl.u.bs_handle.cb_fn = cb_fn;
    5643                 :       2039 :         cpl.u.bs_handle.cb_arg = cb_arg;
    5644                 :       2039 :         cpl.u.bs_handle.bs = bs;
    5645                 :            : 
    5646                 :       2039 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5647         [ -  + ]:       2039 :         if (!seq) {
    5648                 :          0 :                 spdk_free(ctx->super);
    5649                 :          0 :                 free(ctx);
    5650                 :          0 :                 bs_free(bs);
    5651                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5652                 :          0 :                 return;
    5653                 :            :         }
    5654                 :            : 
    5655                 :       2039 :         batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx);
    5656                 :            : 
    5657                 :            :         /* Clear metadata space */
    5658                 :       2039 :         bs_batch_write_zeroes_dev(batch, 0, num_md_lba);
    5659                 :            : 
    5660                 :       2039 :         lba = num_md_lba;
    5661                 :       2039 :         lba_count = ctx->bs->dev->blockcnt - lba;
    5662      [ +  +  + ]:       2039 :         switch (opts.clear_method) {
    5663                 :       1964 :         case BS_CLEAR_WITH_UNMAP:
    5664                 :            :                 /* Trim data clusters */
    5665                 :       1964 :                 bs_batch_unmap_dev(batch, lba, lba_count);
    5666                 :       1964 :                 break;
    5667                 :          1 :         case BS_CLEAR_WITH_WRITE_ZEROES:
    5668                 :            :                 /* Write_zeroes to data clusters */
    5669                 :          1 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    5670                 :          1 :                 break;
    5671                 :         74 :         case BS_CLEAR_WITH_NONE:
    5672                 :            :         default:
    5673                 :         74 :                 break;
    5674                 :            :         }
    5675                 :            : 
    5676                 :       2039 :         bs_batch_close(batch);
    5677                 :            : }
    5678                 :            : 
    5679                 :            : /* END spdk_bs_init */
    5680                 :            : 
    5681                 :            : /* START spdk_bs_destroy */
    5682                 :            : 
    5683                 :            : static void
    5684                 :        113 : bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5685                 :            : {
    5686                 :        113 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5687                 :        113 :         struct spdk_blob_store *bs = ctx->bs;
    5688                 :            : 
    5689                 :            :         /*
    5690                 :            :          * We need to defer calling bs_call_cpl() until after
    5691                 :            :          * dev destruction, so tuck these away for later use.
    5692                 :            :          */
    5693                 :        113 :         bs->unload_err = bserrno;
    5694   [ -  +  -  + ]:        113 :         memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
    5695                 :        113 :         seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
    5696                 :            : 
    5697                 :        113 :         bs_sequence_finish(seq, bserrno);
    5698                 :            : 
    5699                 :        113 :         bs_free(bs);
    5700                 :        113 :         free(ctx);
    5701                 :        113 : }
    5702                 :            : 
    5703                 :            : void
    5704                 :        113 : spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn,
    5705                 :            :                 void *cb_arg)
    5706                 :            : {
    5707                 :         92 :         struct spdk_bs_cpl      cpl;
    5708                 :            :         spdk_bs_sequence_t      *seq;
    5709                 :            :         struct spdk_bs_load_ctx *ctx;
    5710                 :            : 
    5711   [ -  +  -  + ]:        113 :         SPDK_DEBUGLOG(blob, "Destroying blobstore\n");
    5712                 :            : 
    5713         [ -  + ]:        113 :         if (!RB_EMPTY(&bs->open_blobs)) {
    5714                 :          0 :                 SPDK_ERRLOG("Blobstore still has open blobs\n");
    5715                 :          0 :                 cb_fn(cb_arg, -EBUSY);
    5716                 :          0 :                 return;
    5717                 :            :         }
    5718                 :            : 
    5719                 :        113 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5720                 :        113 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5721                 :        113 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5722                 :            : 
    5723                 :        113 :         ctx = calloc(1, sizeof(*ctx));
    5724         [ -  + ]:        113 :         if (!ctx) {
    5725                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5726                 :          0 :                 return;
    5727                 :            :         }
    5728                 :            : 
    5729                 :        113 :         ctx->bs = bs;
    5730                 :            : 
    5731                 :        113 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5732         [ -  + ]:        113 :         if (!seq) {
    5733                 :          0 :                 free(ctx);
    5734                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5735                 :          0 :                 return;
    5736                 :            :         }
    5737                 :            : 
    5738                 :            :         /* Write zeroes to the super block */
    5739                 :        113 :         bs_sequence_write_zeroes_dev(seq,
    5740                 :            :                                      bs_page_to_lba(bs, 0),
    5741                 :            :                                      bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)),
    5742                 :            :                                      bs_destroy_trim_cpl, ctx);
    5743                 :            : }
    5744                 :            : 
    5745                 :            : /* END spdk_bs_destroy */
    5746                 :            : 
    5747                 :            : /* START spdk_bs_unload */
    5748                 :            : 
    5749                 :            : static void
    5750                 :       2756 : bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno)
    5751                 :            : {
    5752                 :       2756 :         spdk_bs_sequence_t *seq = ctx->seq;
    5753                 :            : 
    5754                 :       2756 :         spdk_free(ctx->super);
    5755                 :            : 
    5756                 :            :         /*
    5757                 :            :          * We need to defer calling bs_call_cpl() until after
    5758                 :            :          * dev destruction, so tuck these away for later use.
    5759                 :            :          */
    5760                 :       2756 :         ctx->bs->unload_err = bserrno;
    5761   [ -  +  -  + ]:       2756 :         memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
    5762                 :       2756 :         seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
    5763                 :            : 
    5764                 :       2756 :         bs_sequence_finish(seq, bserrno);
    5765                 :            : 
    5766                 :       2756 :         bs_free(ctx->bs);
    5767                 :       2756 :         free(ctx);
    5768                 :       2756 : }
    5769                 :            : 
    5770                 :            : static void
    5771                 :       2756 : bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5772                 :            : {
    5773                 :       2756 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5774                 :            : 
    5775                 :       2756 :         bs_unload_finish(ctx, bserrno);
    5776                 :       2756 : }
    5777                 :            : 
    5778                 :            : static void
    5779                 :       2756 : bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5780                 :            : {
    5781                 :       2756 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5782                 :            : 
    5783                 :       2756 :         spdk_free(ctx->mask);
    5784                 :            : 
    5785         [ -  + ]:       2756 :         if (bserrno != 0) {
    5786                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5787                 :          0 :                 return;
    5788                 :            :         }
    5789                 :            : 
    5790                 :       2756 :         ctx->super->clean = 1;
    5791                 :            : 
    5792                 :       2756 :         bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx);
    5793                 :            : }
    5794                 :            : 
    5795                 :            : static void
    5796                 :       2756 : bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5797                 :            : {
    5798                 :       2756 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5799                 :            : 
    5800                 :       2756 :         spdk_free(ctx->mask);
    5801                 :       2756 :         ctx->mask = NULL;
    5802                 :            : 
    5803         [ -  + ]:       2756 :         if (bserrno != 0) {
    5804                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5805                 :          0 :                 return;
    5806                 :            :         }
    5807                 :            : 
    5808                 :       2756 :         bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl);
    5809                 :            : }
    5810                 :            : 
    5811                 :            : static void
    5812                 :       2756 : bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5813                 :            : {
    5814                 :       2756 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5815                 :            : 
    5816                 :       2756 :         spdk_free(ctx->mask);
    5817                 :       2756 :         ctx->mask = NULL;
    5818                 :            : 
    5819         [ -  + ]:       2756 :         if (bserrno != 0) {
    5820                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5821                 :          0 :                 return;
    5822                 :            :         }
    5823                 :            : 
    5824                 :       2756 :         bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl);
    5825                 :            : }
    5826                 :            : 
    5827                 :            : static void
    5828                 :       2756 : bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5829                 :            : {
    5830                 :       2756 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5831                 :            :         int rc;
    5832                 :            : 
    5833         [ -  + ]:       2756 :         if (bserrno != 0) {
    5834                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5835                 :          0 :                 return;
    5836                 :            :         }
    5837                 :            : 
    5838                 :       2756 :         rc = bs_super_validate(ctx->super, ctx->bs);
    5839         [ -  + ]:       2756 :         if (rc != 0) {
    5840                 :          0 :                 bs_unload_finish(ctx, rc);
    5841                 :          0 :                 return;
    5842                 :            :         }
    5843                 :            : 
    5844                 :       2756 :         bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl);
    5845                 :            : }
    5846                 :            : 
    5847                 :            : void
    5848                 :       2804 : spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
    5849                 :            : {
    5850                 :       2776 :         struct spdk_bs_cpl      cpl;
    5851                 :            :         struct spdk_bs_load_ctx *ctx;
    5852                 :            : 
    5853   [ -  +  -  + ]:       2804 :         SPDK_DEBUGLOG(blob, "Syncing blobstore\n");
    5854                 :            : 
    5855                 :            :         /*
    5856                 :            :          * If external snapshot channels are being destroyed while the blobstore is unloaded, the
    5857                 :            :          * unload is deferred until after the channel destruction completes.
    5858                 :            :          */
    5859         [ +  + ]:       2804 :         if (bs->esnap_channels_unloading != 0) {
    5860         [ -  + ]:         32 :                 if (bs->esnap_unload_cb_fn != NULL) {
    5861                 :          0 :                         SPDK_ERRLOG("Blobstore unload in progress\n");
    5862                 :          0 :                         cb_fn(cb_arg, -EBUSY);
    5863                 :          0 :                         return;
    5864                 :            :                 }
    5865   [ -  +  -  + ]:         32 :                 SPDK_DEBUGLOG(blob_esnap, "Blobstore unload deferred: %" PRIu32
    5866                 :            :                               " esnap clones are unloading\n", bs->esnap_channels_unloading);
    5867                 :         32 :                 bs->esnap_unload_cb_fn = cb_fn;
    5868                 :         32 :                 bs->esnap_unload_cb_arg = cb_arg;
    5869                 :         32 :                 return;
    5870                 :            :         }
    5871         [ +  + ]:       2772 :         if (bs->esnap_unload_cb_fn != NULL) {
    5872   [ -  +  -  + ]:         32 :                 SPDK_DEBUGLOG(blob_esnap, "Blobstore deferred unload progressing\n");
    5873         [ -  + ]:         32 :                 assert(bs->esnap_unload_cb_fn == cb_fn);
    5874         [ -  + ]:         32 :                 assert(bs->esnap_unload_cb_arg == cb_arg);
    5875                 :         32 :                 bs->esnap_unload_cb_fn = NULL;
    5876                 :         32 :                 bs->esnap_unload_cb_arg = NULL;
    5877                 :            :         }
    5878                 :            : 
    5879         [ +  + ]:       2772 :         if (!RB_EMPTY(&bs->open_blobs)) {
    5880                 :         16 :                 SPDK_ERRLOG("Blobstore still has open blobs\n");
    5881                 :         16 :                 cb_fn(cb_arg, -EBUSY);
    5882                 :         16 :                 return;
    5883                 :            :         }
    5884                 :            : 
    5885                 :       2756 :         ctx = calloc(1, sizeof(*ctx));
    5886         [ -  + ]:       2756 :         if (!ctx) {
    5887                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5888                 :          0 :                 return;
    5889                 :            :         }
    5890                 :            : 
    5891                 :       2756 :         ctx->bs = bs;
    5892                 :            : 
    5893                 :       2756 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    5894                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5895         [ -  + ]:       2756 :         if (!ctx->super) {
    5896                 :          0 :                 free(ctx);
    5897                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5898                 :          0 :                 return;
    5899                 :            :         }
    5900                 :            : 
    5901                 :       2756 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5902                 :       2756 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5903                 :       2756 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5904                 :            : 
    5905                 :       2756 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5906         [ -  + ]:       2756 :         if (!ctx->seq) {
    5907                 :          0 :                 spdk_free(ctx->super);
    5908                 :          0 :                 free(ctx);
    5909                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5910                 :          0 :                 return;
    5911                 :            :         }
    5912                 :            : 
    5913                 :            :         /* Read super block */
    5914                 :       2756 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    5915                 :       2756 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    5916                 :            :                              bs_unload_read_super_cpl, ctx);
    5917                 :            : }
    5918                 :            : 
    5919                 :            : /* END spdk_bs_unload */
    5920                 :            : 
    5921                 :            : /* START spdk_bs_set_super */
    5922                 :            : 
    5923                 :            : struct spdk_bs_set_super_ctx {
    5924                 :            :         struct spdk_blob_store          *bs;
    5925                 :            :         struct spdk_bs_super_block      *super;
    5926                 :            : };
    5927                 :            : 
    5928                 :            : static void
    5929                 :        180 : bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5930                 :            : {
    5931                 :        180 :         struct spdk_bs_set_super_ctx    *ctx = cb_arg;
    5932                 :            : 
    5933         [ -  + ]:        180 :         if (bserrno != 0) {
    5934                 :          0 :                 SPDK_ERRLOG("Unable to write to super block of blobstore\n");
    5935                 :            :         }
    5936                 :            : 
    5937                 :        180 :         spdk_free(ctx->super);
    5938                 :            : 
    5939                 :        180 :         bs_sequence_finish(seq, bserrno);
    5940                 :            : 
    5941                 :        180 :         free(ctx);
    5942                 :        180 : }
    5943                 :            : 
    5944                 :            : static void
    5945                 :        180 : bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5946                 :            : {
    5947                 :        180 :         struct spdk_bs_set_super_ctx    *ctx = cb_arg;
    5948                 :            :         int rc;
    5949                 :            : 
    5950         [ -  + ]:        180 :         if (bserrno != 0) {
    5951                 :          0 :                 SPDK_ERRLOG("Unable to read super block of blobstore\n");
    5952                 :          0 :                 spdk_free(ctx->super);
    5953                 :          0 :                 bs_sequence_finish(seq, bserrno);
    5954                 :          0 :                 free(ctx);
    5955                 :          0 :                 return;
    5956                 :            :         }
    5957                 :            : 
    5958                 :        180 :         rc = bs_super_validate(ctx->super, ctx->bs);
    5959         [ -  + ]:        180 :         if (rc != 0) {
    5960                 :          0 :                 SPDK_ERRLOG("Not a valid super block\n");
    5961                 :          0 :                 spdk_free(ctx->super);
    5962                 :          0 :                 bs_sequence_finish(seq, rc);
    5963                 :          0 :                 free(ctx);
    5964                 :          0 :                 return;
    5965                 :            :         }
    5966                 :            : 
    5967                 :        180 :         bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx);
    5968                 :            : }
    5969                 :            : 
    5970                 :            : void
    5971                 :        180 : spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
    5972                 :            :                   spdk_bs_op_complete cb_fn, void *cb_arg)
    5973                 :            : {
    5974                 :        144 :         struct spdk_bs_cpl              cpl;
    5975                 :            :         spdk_bs_sequence_t              *seq;
    5976                 :            :         struct spdk_bs_set_super_ctx    *ctx;
    5977                 :            : 
    5978   [ -  +  -  + ]:        180 :         SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n");
    5979                 :            : 
    5980                 :        180 :         ctx = calloc(1, sizeof(*ctx));
    5981         [ -  + ]:        180 :         if (!ctx) {
    5982                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5983                 :          0 :                 return;
    5984                 :            :         }
    5985                 :            : 
    5986                 :        180 :         ctx->bs = bs;
    5987                 :            : 
    5988                 :        180 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    5989                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5990         [ -  + ]:        180 :         if (!ctx->super) {
    5991                 :          0 :                 free(ctx);
    5992                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5993                 :          0 :                 return;
    5994                 :            :         }
    5995                 :            : 
    5996                 :        180 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5997                 :        180 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5998                 :        180 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5999                 :            : 
    6000                 :        180 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    6001         [ -  + ]:        180 :         if (!seq) {
    6002                 :          0 :                 spdk_free(ctx->super);
    6003                 :          0 :                 free(ctx);
    6004                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    6005                 :          0 :                 return;
    6006                 :            :         }
    6007                 :            : 
    6008                 :        180 :         bs->super_blob = blobid;
    6009                 :            : 
    6010                 :            :         /* Read super block */
    6011                 :        180 :         bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
    6012                 :        180 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    6013                 :            :                              bs_set_super_read_cpl, ctx);
    6014                 :            : }
    6015                 :            : 
    6016                 :            : /* END spdk_bs_set_super */
    6017                 :            : 
    6018                 :            : void
    6019                 :        116 : spdk_bs_get_super(struct spdk_blob_store *bs,
    6020                 :            :                   spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6021                 :            : {
    6022         [ +  + ]:        116 :         if (bs->super_blob == SPDK_BLOBID_INVALID) {
    6023                 :         16 :                 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
    6024                 :            :         } else {
    6025                 :        100 :                 cb_fn(cb_arg, bs->super_blob, 0);
    6026                 :            :         }
    6027                 :        116 : }
    6028                 :            : 
    6029                 :            : uint64_t
    6030                 :       1797 : spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
    6031                 :            : {
    6032                 :       1797 :         return bs->cluster_sz;
    6033                 :            : }
    6034                 :            : 
    6035                 :            : uint64_t
    6036                 :        283 : spdk_bs_get_page_size(struct spdk_blob_store *bs)
    6037                 :            : {
    6038                 :        283 :         return SPDK_BS_PAGE_SIZE;
    6039                 :            : }
    6040                 :            : 
    6041                 :            : uint64_t
    6042                 :    1567646 : spdk_bs_get_io_unit_size(struct spdk_blob_store *bs)
    6043                 :            : {
    6044                 :    1567646 :         return bs->io_unit_size;
    6045                 :            : }
    6046                 :            : 
    6047                 :            : uint64_t
    6048                 :       2334 : spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
    6049                 :            : {
    6050                 :       2334 :         return bs->num_free_clusters;
    6051                 :            : }
    6052                 :            : 
    6053                 :            : uint64_t
    6054                 :        539 : spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs)
    6055                 :            : {
    6056                 :        539 :         return bs->total_data_clusters;
    6057                 :            : }
    6058                 :            : 
    6059                 :            : static int
    6060                 :      10526 : bs_register_md_thread(struct spdk_blob_store *bs)
    6061                 :            : {
    6062                 :      10526 :         bs->md_channel = spdk_get_io_channel(bs);
    6063         [ -  + ]:      10526 :         if (!bs->md_channel) {
    6064                 :          0 :                 SPDK_ERRLOG("Failed to get IO channel.\n");
    6065                 :          0 :                 return -1;
    6066                 :            :         }
    6067                 :            : 
    6068                 :      10526 :         return 0;
    6069                 :            : }
    6070                 :            : 
    6071                 :            : static int
    6072                 :      10526 : bs_unregister_md_thread(struct spdk_blob_store *bs)
    6073                 :            : {
    6074                 :      10526 :         spdk_put_io_channel(bs->md_channel);
    6075                 :            : 
    6076                 :      10526 :         return 0;
    6077                 :            : }
    6078                 :            : 
    6079                 :            : spdk_blob_id
    6080                 :       4291 : spdk_blob_get_id(struct spdk_blob *blob)
    6081                 :            : {
    6082         [ -  + ]:       4291 :         assert(blob != NULL);
    6083                 :            : 
    6084                 :       4291 :         return blob->id;
    6085                 :            : }
    6086                 :            : 
    6087                 :            : uint64_t
    6088                 :        135 : spdk_blob_get_num_pages(struct spdk_blob *blob)
    6089                 :            : {
    6090         [ -  + ]:        135 :         assert(blob != NULL);
    6091                 :            : 
    6092                 :        135 :         return bs_cluster_to_page(blob->bs, blob->active.num_clusters);
    6093                 :            : }
    6094                 :            : 
    6095                 :            : uint64_t
    6096                 :        134 : spdk_blob_get_num_io_units(struct spdk_blob *blob)
    6097                 :            : {
    6098         [ -  + ]:        134 :         assert(blob != NULL);
    6099                 :            : 
    6100                 :        134 :         return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs);
    6101                 :            : }
    6102                 :            : 
    6103                 :            : uint64_t
    6104                 :     303880 : spdk_blob_get_num_clusters(struct spdk_blob *blob)
    6105                 :            : {
    6106         [ -  + ]:     303880 :         assert(blob != NULL);
    6107                 :            : 
    6108                 :     303880 :         return blob->active.num_clusters;
    6109                 :            : }
    6110                 :            : 
    6111                 :            : uint64_t
    6112                 :       2508 : spdk_blob_get_num_allocated_clusters(struct spdk_blob *blob)
    6113                 :            : {
    6114         [ -  + ]:       2508 :         assert(blob != NULL);
    6115                 :            : 
    6116                 :       2508 :         return blob->active.num_allocated_clusters;
    6117                 :            : }
    6118                 :            : 
    6119                 :            : static uint64_t
    6120                 :        132 : blob_find_io_unit(struct spdk_blob *blob, uint64_t offset, bool is_allocated)
    6121                 :            : {
    6122                 :        132 :         uint64_t blob_io_unit_num = spdk_blob_get_num_io_units(blob);
    6123                 :            : 
    6124         [ +  + ]:        266 :         while (offset < blob_io_unit_num) {
    6125         [ +  + ]:        244 :                 if (bs_io_unit_is_allocated(blob, offset) == is_allocated) {
    6126                 :        110 :                         return offset;
    6127                 :            :                 }
    6128                 :            : 
    6129                 :        134 :                 offset += bs_num_io_units_to_cluster_boundary(blob, offset);
    6130                 :            :         }
    6131                 :            : 
    6132                 :         22 :         return UINT64_MAX;
    6133                 :            : }
    6134                 :            : 
    6135                 :            : uint64_t
    6136                 :         66 : spdk_blob_get_next_allocated_io_unit(struct spdk_blob *blob, uint64_t offset)
    6137                 :            : {
    6138                 :         66 :         return blob_find_io_unit(blob, offset, true);
    6139                 :            : }
    6140                 :            : 
    6141                 :            : uint64_t
    6142                 :         66 : spdk_blob_get_next_unallocated_io_unit(struct spdk_blob *blob, uint64_t offset)
    6143                 :            : {
    6144                 :         66 :         return blob_find_io_unit(blob, offset, false);
    6145                 :            : }
    6146                 :            : 
    6147                 :            : /* START spdk_bs_create_blob */
    6148                 :            : 
    6149                 :            : static void
    6150                 :       8712 : bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    6151                 :            : {
    6152                 :       8712 :         struct spdk_blob *blob = cb_arg;
    6153                 :       8712 :         uint32_t page_idx = bs_blobid_to_page(blob->id);
    6154                 :            : 
    6155         [ -  + ]:       8712 :         if (bserrno != 0) {
    6156                 :          0 :                 spdk_spin_lock(&blob->bs->used_lock);
    6157                 :          0 :                 spdk_bit_array_clear(blob->bs->used_blobids, page_idx);
    6158                 :          0 :                 bs_release_md_page(blob->bs, page_idx);
    6159                 :          0 :                 spdk_spin_unlock(&blob->bs->used_lock);
    6160                 :            :         }
    6161                 :            : 
    6162                 :       8712 :         blob_free(blob);
    6163                 :            : 
    6164                 :       8712 :         bs_sequence_finish(seq, bserrno);
    6165                 :       8712 : }
    6166                 :            : 
    6167                 :            : static int
    6168                 :      17508 : blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs,
    6169                 :            :                 bool internal)
    6170                 :            : {
    6171                 :            :         uint64_t i;
    6172                 :      17508 :         size_t value_len = 0;
    6173                 :            :         int rc;
    6174                 :      17508 :         const void *value = NULL;
    6175   [ +  +  +  + ]:      17508 :         if (xattrs->count > 0 && xattrs->get_value == NULL) {
    6176                 :         32 :                 return -EINVAL;
    6177                 :            :         }
    6178         [ +  + ]:      19407 :         for (i = 0; i < xattrs->count; i++) {
    6179                 :       1947 :                 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len);
    6180   [ +  +  -  + ]:       1947 :                 if (value == NULL || value_len == 0) {
    6181                 :         16 :                         return -EINVAL;
    6182                 :            :                 }
    6183                 :       1931 :                 rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal);
    6184         [ -  + ]:       1931 :                 if (rc < 0) {
    6185                 :          0 :                         return rc;
    6186                 :            :                 }
    6187                 :            :         }
    6188                 :      17460 :         return 0;
    6189                 :            : }
    6190                 :            : 
    6191                 :            : static void
    6192                 :       7744 : blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst)
    6193                 :            : {
    6194                 :            : #define FIELD_OK(field) \
    6195                 :            :         offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size
    6196                 :            : 
    6197                 :            : #define SET_FIELD(field) \
    6198                 :            :         if (FIELD_OK(field)) { \
    6199                 :            :                 dst->field = src->field; \
    6200                 :            :         } \
    6201                 :            : 
    6202         [ +  - ]:       7744 :         SET_FIELD(num_clusters);
    6203   [ +  -  -  + ]:       7744 :         SET_FIELD(thin_provision);
    6204         [ +  - ]:       7744 :         SET_FIELD(clear_method);
    6205                 :            : 
    6206         [ +  - ]:       7744 :         if (FIELD_OK(xattrs)) {
    6207   [ -  +  -  + ]:       7744 :                 memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs));
    6208                 :            :         }
    6209                 :            : 
    6210   [ +  -  -  + ]:       7744 :         SET_FIELD(use_extent_table);
    6211         [ +  - ]:       7744 :         SET_FIELD(esnap_id);
    6212         [ +  - ]:       7744 :         SET_FIELD(esnap_id_len);
    6213                 :            : 
    6214                 :       7744 :         dst->opts_size = src->opts_size;
    6215                 :            : 
    6216                 :            :         /* You should not remove this statement, but need to update the assert statement
    6217                 :            :          * if you add a new field, and also add a corresponding SET_FIELD statement */
    6218                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 80, "Incorrect size");
    6219                 :            : 
    6220                 :            : #undef FIELD_OK
    6221                 :            : #undef SET_FIELD
    6222                 :       7744 : }
    6223                 :            : 
    6224                 :            : static void
    6225                 :       8778 : bs_create_blob(struct spdk_blob_store *bs,
    6226                 :            :                const struct spdk_blob_opts *opts,
    6227                 :            :                const struct spdk_blob_xattr_opts *internal_xattrs,
    6228                 :            :                spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6229                 :            : {
    6230                 :            :         struct spdk_blob        *blob;
    6231                 :            :         uint32_t                page_idx;
    6232                 :       8633 :         struct spdk_bs_cpl      cpl;
    6233                 :       8633 :         struct spdk_blob_opts   opts_local;
    6234                 :       8633 :         struct spdk_blob_xattr_opts internal_xattrs_default;
    6235                 :            :         spdk_bs_sequence_t      *seq;
    6236                 :            :         spdk_blob_id            id;
    6237                 :            :         int rc;
    6238                 :            : 
    6239         [ -  + ]:       8778 :         assert(spdk_get_thread() == bs->md_thread);
    6240                 :            : 
    6241                 :       8778 :         spdk_spin_lock(&bs->used_lock);
    6242                 :       8778 :         page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
    6243         [ -  + ]:       8778 :         if (page_idx == UINT32_MAX) {
    6244                 :          0 :                 spdk_spin_unlock(&bs->used_lock);
    6245                 :          0 :                 cb_fn(cb_arg, 0, -ENOMEM);
    6246                 :        145 :                 return;
    6247                 :            :         }
    6248                 :       8778 :         spdk_bit_array_set(bs->used_blobids, page_idx);
    6249                 :       8778 :         bs_claim_md_page(bs, page_idx);
    6250                 :       8778 :         spdk_spin_unlock(&bs->used_lock);
    6251                 :            : 
    6252                 :       8778 :         id = bs_page_to_blobid(page_idx);
    6253                 :            : 
    6254   [ -  +  -  + ]:       8778 :         SPDK_DEBUGLOG(blob, "Creating blob with id 0x%" PRIx64 " at page %u\n", id, page_idx);
    6255                 :            : 
    6256                 :       8778 :         spdk_blob_opts_init(&opts_local, sizeof(opts_local));
    6257         [ +  + ]:       8778 :         if (opts) {
    6258                 :       7744 :                 blob_opts_copy(opts, &opts_local);
    6259                 :            :         }
    6260                 :            : 
    6261                 :       8778 :         blob = blob_alloc(bs, id);
    6262         [ -  + ]:       8778 :         if (!blob) {
    6263                 :          0 :                 rc = -ENOMEM;
    6264                 :          0 :                 goto error;
    6265                 :            :         }
    6266                 :            : 
    6267         [ -  + ]:       8778 :         blob->use_extent_table = opts_local.use_extent_table;
    6268   [ +  +  +  + ]:       8778 :         if (blob->use_extent_table) {
    6269                 :       5082 :                 blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE;
    6270                 :            :         }
    6271                 :            : 
    6272         [ +  + ]:       8778 :         if (!internal_xattrs) {
    6273                 :       7647 :                 blob_xattrs_init(&internal_xattrs_default);
    6274                 :       7647 :                 internal_xattrs = &internal_xattrs_default;
    6275                 :            :         }
    6276                 :            : 
    6277                 :       8778 :         rc = blob_set_xattrs(blob, &opts_local.xattrs, false);
    6278         [ +  + ]:       8778 :         if (rc < 0) {
    6279                 :         48 :                 goto error;
    6280                 :            :         }
    6281                 :            : 
    6282                 :       8730 :         rc = blob_set_xattrs(blob, internal_xattrs, true);
    6283         [ -  + ]:       8730 :         if (rc < 0) {
    6284                 :          0 :                 goto error;
    6285                 :            :         }
    6286                 :            : 
    6287   [ +  +  +  + ]:       8730 :         if (opts_local.thin_provision) {
    6288                 :       1521 :                 blob_set_thin_provision(blob);
    6289                 :            :         }
    6290                 :            : 
    6291                 :       8730 :         blob_set_clear_method(blob, opts_local.clear_method);
    6292                 :            : 
    6293         [ +  + ]:       8730 :         if (opts_local.esnap_id != NULL) {
    6294         [ -  + ]:        250 :                 if (opts_local.esnap_id_len > UINT16_MAX) {
    6295                 :          0 :                         SPDK_ERRLOG("esnap id length %" PRIu64 "is too long\n",
    6296                 :            :                                     opts_local.esnap_id_len);
    6297                 :          0 :                         rc = -EINVAL;
    6298                 :          0 :                         goto error;
    6299                 :            : 
    6300                 :            :                 }
    6301                 :        250 :                 blob_set_thin_provision(blob);
    6302                 :        250 :                 blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6303                 :        250 :                 rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID,
    6304                 :        250 :                                     opts_local.esnap_id, opts_local.esnap_id_len, true);
    6305         [ -  + ]:        250 :                 if (rc != 0) {
    6306                 :          0 :                         goto error;
    6307                 :            :                 }
    6308                 :            :         }
    6309                 :            : 
    6310                 :       8730 :         rc = blob_resize(blob, opts_local.num_clusters);
    6311         [ +  + ]:       8730 :         if (rc < 0) {
    6312                 :         18 :                 goto error;
    6313                 :            :         }
    6314                 :       8712 :         cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
    6315                 :       8712 :         cpl.u.blobid.cb_fn = cb_fn;
    6316                 :       8712 :         cpl.u.blobid.cb_arg = cb_arg;
    6317                 :       8712 :         cpl.u.blobid.blobid = blob->id;
    6318                 :            : 
    6319                 :       8712 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    6320         [ -  + ]:       8712 :         if (!seq) {
    6321                 :          0 :                 rc = -ENOMEM;
    6322                 :          0 :                 goto error;
    6323                 :            :         }
    6324                 :            : 
    6325                 :       8712 :         blob_persist(seq, blob, bs_create_blob_cpl, blob);
    6326                 :       8712 :         return;
    6327                 :            : 
    6328                 :         66 : error:
    6329                 :         66 :         SPDK_ERRLOG("Failed to create blob: %s, size in clusters/size: %lu (clusters)\n",
    6330                 :            :                     spdk_strerror(rc), opts_local.num_clusters);
    6331         [ +  - ]:         66 :         if (blob != NULL) {
    6332                 :         66 :                 blob_free(blob);
    6333                 :            :         }
    6334                 :         66 :         spdk_spin_lock(&bs->used_lock);
    6335                 :         66 :         spdk_bit_array_clear(bs->used_blobids, page_idx);
    6336                 :         66 :         bs_release_md_page(bs, page_idx);
    6337                 :         66 :         spdk_spin_unlock(&bs->used_lock);
    6338                 :         66 :         cb_fn(cb_arg, 0, rc);
    6339                 :            : }
    6340                 :            : 
    6341                 :            : void
    6342                 :        970 : spdk_bs_create_blob(struct spdk_blob_store *bs,
    6343                 :            :                     spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6344                 :            : {
    6345                 :        970 :         bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg);
    6346                 :        970 : }
    6347                 :            : 
    6348                 :            : void
    6349                 :       6645 : spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts,
    6350                 :            :                         spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6351                 :            : {
    6352                 :       6645 :         bs_create_blob(bs, opts, NULL, cb_fn, cb_arg);
    6353                 :       6645 : }
    6354                 :            : 
    6355                 :            : /* END spdk_bs_create_blob */
    6356                 :            : 
    6357                 :            : /* START blob_cleanup */
    6358                 :            : 
    6359                 :            : struct spdk_clone_snapshot_ctx {
    6360                 :            :         struct spdk_bs_cpl      cpl;
    6361                 :            :         int bserrno;
    6362                 :            :         bool frozen;
    6363                 :            : 
    6364                 :            :         struct spdk_io_channel *channel;
    6365                 :            : 
    6366                 :            :         /* Current cluster for inflate operation */
    6367                 :            :         uint64_t cluster;
    6368                 :            : 
    6369                 :            :         /* For inflation force allocation of all unallocated clusters and remove
    6370                 :            :          * thin-provisioning. Otherwise only decouple parent and keep clone thin. */
    6371                 :            :         bool allocate_all;
    6372                 :            : 
    6373                 :            :         struct {
    6374                 :            :                 spdk_blob_id id;
    6375                 :            :                 struct spdk_blob *blob;
    6376                 :            :                 bool md_ro;
    6377                 :            :         } original;
    6378                 :            :         struct {
    6379                 :            :                 spdk_blob_id id;
    6380                 :            :                 struct spdk_blob *blob;
    6381                 :            :         } new;
    6382                 :            : 
    6383                 :            :         /* xattrs specified for snapshot/clones only. They have no impact on
    6384                 :            :          * the original blobs xattrs. */
    6385                 :            :         const struct spdk_blob_xattr_opts *xattrs;
    6386                 :            : };
    6387                 :            : 
    6388                 :            : static void
    6389                 :       1422 : bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno)
    6390                 :            : {
    6391                 :       1422 :         struct spdk_clone_snapshot_ctx *ctx = cb_arg;
    6392                 :       1422 :         struct spdk_bs_cpl *cpl = &ctx->cpl;
    6393                 :            : 
    6394         [ +  + ]:       1422 :         if (bserrno != 0) {
    6395         [ -  + ]:         24 :                 if (ctx->bserrno != 0) {
    6396                 :          0 :                         SPDK_ERRLOG("Cleanup error %d\n", bserrno);
    6397                 :            :                 } else {
    6398                 :         24 :                         ctx->bserrno = bserrno;
    6399                 :            :                 }
    6400                 :            :         }
    6401                 :            : 
    6402      [ +  +  - ]:       1422 :         switch (cpl->type) {
    6403                 :       1174 :         case SPDK_BS_CPL_TYPE_BLOBID:
    6404                 :       1174 :                 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno);
    6405                 :       1174 :                 break;
    6406                 :        248 :         case SPDK_BS_CPL_TYPE_BLOB_BASIC:
    6407                 :        248 :                 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
    6408                 :        248 :                 break;
    6409                 :          0 :         default:
    6410                 :          0 :                 SPDK_UNREACHABLE();
    6411                 :            :                 break;
    6412                 :            :         }
    6413                 :            : 
    6414                 :       1422 :         free(ctx);
    6415                 :       1422 : }
    6416                 :            : 
    6417                 :            : static void
    6418                 :       1363 : bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
    6419                 :            : {
    6420                 :       1363 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6421                 :       1363 :         struct spdk_blob *origblob = ctx->original.blob;
    6422                 :            : 
    6423         [ -  + ]:       1363 :         if (bserrno != 0) {
    6424         [ #  # ]:          0 :                 if (ctx->bserrno != 0) {
    6425                 :          0 :                         SPDK_ERRLOG("Unfreeze error %d\n", bserrno);
    6426                 :            :                 } else {
    6427                 :          0 :                         ctx->bserrno = bserrno;
    6428                 :            :                 }
    6429                 :            :         }
    6430                 :            : 
    6431                 :       1363 :         ctx->original.id = origblob->id;
    6432                 :       1363 :         origblob->locked_operation_in_progress = false;
    6433                 :            : 
    6434                 :            :         /* Revert md_ro to original state */
    6435         [ -  + ]:       1363 :         origblob->md_ro = ctx->original.md_ro;
    6436                 :            : 
    6437                 :       1363 :         spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx);
    6438                 :       1363 : }
    6439                 :            : 
    6440                 :            : static void
    6441                 :       1363 : bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno)
    6442                 :            : {
    6443                 :       1363 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6444                 :       1363 :         struct spdk_blob *origblob = ctx->original.blob;
    6445                 :            : 
    6446         [ +  + ]:       1363 :         if (bserrno != 0) {
    6447         [ +  + ]:         97 :                 if (ctx->bserrno != 0) {
    6448                 :         16 :                         SPDK_ERRLOG("Cleanup error %d\n", bserrno);
    6449                 :            :                 } else {
    6450                 :         81 :                         ctx->bserrno = bserrno;
    6451                 :            :                 }
    6452                 :            :         }
    6453                 :            : 
    6454   [ +  +  +  + ]:       1363 :         if (ctx->frozen) {
    6455                 :            :                 /* Unfreeze any outstanding I/O */
    6456                 :        869 :                 blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx);
    6457                 :            :         } else {
    6458                 :        494 :                 bs_snapshot_unfreeze_cpl(ctx, 0);
    6459                 :            :         }
    6460                 :            : 
    6461                 :       1363 : }
    6462                 :            : 
    6463                 :            : static void
    6464                 :         16 : bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno)
    6465                 :            : {
    6466                 :         16 :         struct spdk_blob *newblob = ctx->new.blob;
    6467                 :            : 
    6468         [ +  - ]:         16 :         if (bserrno != 0) {
    6469         [ -  + ]:         16 :                 if (ctx->bserrno != 0) {
    6470                 :          0 :                         SPDK_ERRLOG("Cleanup error %d\n", bserrno);
    6471                 :            :                 } else {
    6472                 :         16 :                         ctx->bserrno = bserrno;
    6473                 :            :                 }
    6474                 :            :         }
    6475                 :            : 
    6476                 :         16 :         ctx->new.id = newblob->id;
    6477                 :         16 :         spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
    6478                 :         16 : }
    6479                 :            : 
    6480                 :            : /* END blob_cleanup */
    6481                 :            : 
    6482                 :            : /* START spdk_bs_create_snapshot */
    6483                 :            : 
    6484                 :            : static void
    6485                 :        901 : bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2)
    6486                 :            : {
    6487                 :            :         uint64_t *cluster_temp;
    6488                 :            :         uint64_t num_allocated_clusters_temp;
    6489                 :            :         uint32_t *extent_page_temp;
    6490                 :            : 
    6491                 :        901 :         cluster_temp = blob1->active.clusters;
    6492                 :        901 :         blob1->active.clusters = blob2->active.clusters;
    6493                 :        901 :         blob2->active.clusters = cluster_temp;
    6494                 :            : 
    6495                 :        901 :         num_allocated_clusters_temp = blob1->active.num_allocated_clusters;
    6496                 :        901 :         blob1->active.num_allocated_clusters = blob2->active.num_allocated_clusters;
    6497                 :        901 :         blob2->active.num_allocated_clusters = num_allocated_clusters_temp;
    6498                 :            : 
    6499                 :        901 :         extent_page_temp = blob1->active.extent_pages;
    6500                 :        901 :         blob1->active.extent_pages = blob2->active.extent_pages;
    6501                 :        901 :         blob2->active.extent_pages = extent_page_temp;
    6502                 :        901 : }
    6503                 :            : 
    6504                 :            : /* Copies an internal xattr */
    6505                 :            : static int
    6506                 :         88 : bs_snapshot_copy_xattr(struct spdk_blob *toblob, struct spdk_blob *fromblob, const char *name)
    6507                 :            : {
    6508                 :         88 :         const void      *val = NULL;
    6509                 :         88 :         size_t          len;
    6510                 :            :         int             bserrno;
    6511                 :            : 
    6512                 :         88 :         bserrno = blob_get_xattr_value(fromblob, name, &val, &len, true);
    6513         [ -  + ]:         88 :         if (bserrno != 0) {
    6514                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " missing %s XATTR\n", fromblob->id, name);
    6515                 :          0 :                 return bserrno;
    6516                 :            :         }
    6517                 :            : 
    6518                 :         88 :         bserrno = blob_set_xattr(toblob, name, val, len, true);
    6519         [ -  + ]:         88 :         if (bserrno != 0) {
    6520                 :          0 :                 SPDK_ERRLOG("could not set %s XATTR on blob 0x%" PRIx64 "\n",
    6521                 :            :                             name, toblob->id);
    6522                 :          0 :                 return bserrno;
    6523                 :            :         }
    6524                 :         88 :         return 0;
    6525                 :            : }
    6526                 :            : 
    6527                 :            : static void
    6528                 :        853 : bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno)
    6529                 :            : {
    6530                 :        853 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6531                 :        853 :         struct spdk_blob *origblob = ctx->original.blob;
    6532                 :        853 :         struct spdk_blob *newblob = ctx->new.blob;
    6533                 :            : 
    6534         [ +  + ]:        853 :         if (bserrno != 0) {
    6535                 :         16 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6536         [ -  + ]:         16 :                 if (blob_is_esnap_clone(newblob)) {
    6537                 :          0 :                         bs_snapshot_copy_xattr(origblob, newblob, BLOB_EXTERNAL_SNAPSHOT_ID);
    6538                 :          0 :                         origblob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6539                 :            :                 }
    6540                 :         16 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6541                 :         16 :                 return;
    6542                 :            :         }
    6543                 :            : 
    6544                 :            :         /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */
    6545                 :        837 :         bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true);
    6546         [ -  + ]:        837 :         if (bserrno != 0) {
    6547                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6548                 :          0 :                 return;
    6549                 :            :         }
    6550                 :            : 
    6551                 :        837 :         bs_blob_list_add(ctx->original.blob);
    6552                 :            : 
    6553                 :        837 :         spdk_blob_set_read_only(newblob);
    6554                 :            : 
    6555                 :            :         /* sync snapshot metadata */
    6556                 :        837 :         spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
    6557                 :            : }
    6558                 :            : 
    6559                 :            : static void
    6560                 :        869 : bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno)
    6561                 :            : {
    6562                 :        869 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6563                 :        869 :         struct spdk_blob *origblob = ctx->original.blob;
    6564                 :        869 :         struct spdk_blob *newblob = ctx->new.blob;
    6565                 :            : 
    6566         [ +  + ]:        869 :         if (bserrno != 0) {
    6567                 :            :                 /* return cluster map back to original */
    6568                 :         16 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6569                 :            : 
    6570                 :            :                 /* Newblob md sync failed. Valid clusters are only present in origblob.
    6571                 :            :                  * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred.
    6572                 :            :                  * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */
    6573                 :         16 :                 blob_set_thin_provision(newblob);
    6574         [ -  + ]:         16 :                 assert(spdk_mem_all_zero(newblob->active.clusters,
    6575                 :            :                                          newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
    6576         [ -  + ]:         16 :                 assert(spdk_mem_all_zero(newblob->active.extent_pages,
    6577                 :            :                                          newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
    6578                 :            : 
    6579                 :         16 :                 bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6580                 :         16 :                 return;
    6581                 :            :         }
    6582                 :            : 
    6583                 :            :         /* Set internal xattr for snapshot id */
    6584                 :        853 :         bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true);
    6585         [ -  + ]:        853 :         if (bserrno != 0) {
    6586                 :            :                 /* return cluster map back to original */
    6587                 :          0 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6588                 :          0 :                 blob_set_thin_provision(newblob);
    6589                 :          0 :                 bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6590                 :          0 :                 return;
    6591                 :            :         }
    6592                 :            : 
    6593                 :            :         /* Create new back_bs_dev for snapshot */
    6594                 :        853 :         origblob->back_bs_dev = bs_create_blob_bs_dev(newblob);
    6595         [ -  + ]:        853 :         if (origblob->back_bs_dev == NULL) {
    6596                 :            :                 /* return cluster map back to original */
    6597                 :          0 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6598                 :          0 :                 blob_set_thin_provision(newblob);
    6599                 :          0 :                 bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL);
    6600                 :          0 :                 return;
    6601                 :            :         }
    6602                 :            : 
    6603                 :            :         /* Remove the xattr that references an external snapshot */
    6604         [ +  + ]:        853 :         if (blob_is_esnap_clone(origblob)) {
    6605                 :         52 :                 origblob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6606                 :         52 :                 bserrno = blob_remove_xattr(origblob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
    6607         [ -  + ]:         52 :                 if (bserrno != 0) {
    6608         [ #  # ]:          0 :                         if (bserrno == -ENOENT) {
    6609                 :          0 :                                 SPDK_ERRLOG("blob 0x%" PRIx64 " has no " BLOB_EXTERNAL_SNAPSHOT_ID
    6610                 :            :                                             " xattr to remove\n", origblob->id);
    6611                 :          0 :                                 assert(false);
    6612                 :            :                         } else {
    6613                 :            :                                 /* return cluster map back to original */
    6614                 :          0 :                                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6615                 :          0 :                                 blob_set_thin_provision(newblob);
    6616                 :          0 :                                 bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6617                 :          0 :                                 return;
    6618                 :            :                         }
    6619                 :            :                 }
    6620                 :            :         }
    6621                 :            : 
    6622                 :        853 :         bs_blob_list_remove(origblob);
    6623                 :        853 :         origblob->parent_id = newblob->id;
    6624                 :            :         /* set clone blob as thin provisioned */
    6625                 :        853 :         blob_set_thin_provision(origblob);
    6626                 :            : 
    6627                 :        853 :         bs_blob_list_add(newblob);
    6628                 :            : 
    6629                 :            :         /* sync clone metadata */
    6630                 :        853 :         spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx);
    6631                 :            : }
    6632                 :            : 
    6633                 :            : static void
    6634                 :        869 : bs_snapshot_freeze_cpl(void *cb_arg, int rc)
    6635                 :            : {
    6636                 :        869 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6637                 :        869 :         struct spdk_blob *origblob = ctx->original.blob;
    6638                 :        869 :         struct spdk_blob *newblob = ctx->new.blob;
    6639                 :            :         int bserrno;
    6640                 :            : 
    6641         [ -  + ]:        869 :         if (rc != 0) {
    6642                 :          0 :                 bs_clone_snapshot_newblob_cleanup(ctx, rc);
    6643                 :          0 :                 return;
    6644                 :            :         }
    6645                 :            : 
    6646                 :        869 :         ctx->frozen = true;
    6647                 :            : 
    6648         [ +  + ]:        869 :         if (blob_is_esnap_clone(origblob)) {
    6649                 :            :                 /* Clean up any channels associated with the original blob id because future IO will
    6650                 :            :                  * perform IO using the snapshot blob_id.
    6651                 :            :                  */
    6652                 :         52 :                 blob_esnap_destroy_bs_dev_channels(origblob, false, NULL, NULL);
    6653                 :            :         }
    6654         [ +  - ]:        869 :         if (newblob->back_bs_dev) {
    6655                 :        869 :                 blob_back_bs_destroy(newblob);
    6656                 :            :         }
    6657                 :            :         /* set new back_bs_dev for snapshot */
    6658                 :        869 :         newblob->back_bs_dev = origblob->back_bs_dev;
    6659                 :            :         /* Set invalid flags from origblob */
    6660                 :        869 :         newblob->invalid_flags = origblob->invalid_flags;
    6661                 :            : 
    6662                 :            :         /* inherit parent from original blob if set */
    6663                 :        869 :         newblob->parent_id = origblob->parent_id;
    6664      [ +  +  + ]:        869 :         switch (origblob->parent_id) {
    6665                 :         52 :         case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
    6666                 :         52 :                 bserrno = bs_snapshot_copy_xattr(newblob, origblob, BLOB_EXTERNAL_SNAPSHOT_ID);
    6667         [ -  + ]:         52 :                 if (bserrno != 0) {
    6668                 :          0 :                         bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6669                 :          0 :                         return;
    6670                 :            :                 }
    6671                 :         52 :                 break;
    6672                 :        604 :         case SPDK_BLOBID_INVALID:
    6673                 :        604 :                 break;
    6674                 :        213 :         default:
    6675                 :            :                 /* Set internal xattr for snapshot id */
    6676                 :        213 :                 bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT,
    6677                 :        213 :                                          &origblob->parent_id, sizeof(spdk_blob_id), true);
    6678         [ -  + ]:        213 :                 if (bserrno != 0) {
    6679                 :          0 :                         bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6680                 :          0 :                         return;
    6681                 :            :                 }
    6682                 :            :         }
    6683                 :            : 
    6684                 :            :         /* swap cluster maps */
    6685                 :        869 :         bs_snapshot_swap_cluster_maps(newblob, origblob);
    6686                 :            : 
    6687                 :            :         /* Set the clear method on the new blob to match the original. */
    6688                 :        869 :         blob_set_clear_method(newblob, origblob->clear_method);
    6689                 :            : 
    6690                 :            :         /* sync snapshot metadata */
    6691                 :        869 :         spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx);
    6692                 :            : }
    6693                 :            : 
    6694                 :            : static void
    6695                 :        885 : bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6696                 :            : {
    6697                 :        885 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6698                 :        885 :         struct spdk_blob *origblob = ctx->original.blob;
    6699                 :        885 :         struct spdk_blob *newblob = _blob;
    6700                 :            : 
    6701         [ +  + ]:        885 :         if (bserrno != 0) {
    6702                 :         16 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6703                 :         16 :                 return;
    6704                 :            :         }
    6705                 :            : 
    6706                 :        869 :         ctx->new.blob = newblob;
    6707         [ -  + ]:        869 :         assert(spdk_blob_is_thin_provisioned(newblob));
    6708         [ -  + ]:        869 :         assert(spdk_mem_all_zero(newblob->active.clusters,
    6709                 :            :                                  newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
    6710         [ -  + ]:        869 :         assert(spdk_mem_all_zero(newblob->active.extent_pages,
    6711                 :            :                                  newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
    6712                 :            : 
    6713                 :        869 :         blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx);
    6714                 :            : }
    6715                 :            : 
    6716                 :            : static void
    6717                 :        901 : bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
    6718                 :            : {
    6719                 :        901 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6720                 :        901 :         struct spdk_blob *origblob = ctx->original.blob;
    6721                 :            : 
    6722         [ +  + ]:        901 :         if (bserrno != 0) {
    6723                 :         16 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6724                 :         16 :                 return;
    6725                 :            :         }
    6726                 :            : 
    6727                 :        885 :         ctx->new.id = blobid;
    6728                 :        885 :         ctx->cpl.u.blobid.blobid = blobid;
    6729                 :            : 
    6730                 :        885 :         spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx);
    6731                 :            : }
    6732                 :            : 
    6733                 :            : 
    6734                 :            : static void
    6735                 :        901 : bs_xattr_snapshot(void *arg, const char *name,
    6736                 :            :                   const void **value, size_t *value_len)
    6737                 :            : {
    6738   [ -  +  -  + ]:        901 :         assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0);
    6739                 :            : 
    6740                 :        901 :         struct spdk_blob *blob = (struct spdk_blob *)arg;
    6741                 :        901 :         *value = &blob->id;
    6742                 :        901 :         *value_len = sizeof(blob->id);
    6743                 :        901 : }
    6744                 :            : 
    6745                 :            : static void
    6746                 :        942 : bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6747                 :            : {
    6748                 :        942 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6749                 :        937 :         struct spdk_blob_opts opts;
    6750                 :        937 :         struct spdk_blob_xattr_opts internal_xattrs;
    6751                 :        942 :         char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS };
    6752                 :            : 
    6753         [ +  + ]:        942 :         if (bserrno != 0) {
    6754                 :         24 :                 bs_clone_snapshot_cleanup_finish(ctx, bserrno);
    6755                 :         24 :                 return;
    6756                 :            :         }
    6757                 :            : 
    6758                 :        918 :         ctx->original.blob = _blob;
    6759                 :            : 
    6760   [ +  +  +  +  :        918 :         if (_blob->data_ro || _blob->md_ro) {
             -  +  -  + ]
    6761   [ -  +  -  + ]:         17 :                 SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id 0x%"
    6762                 :            :                               PRIx64 "\n", _blob->id);
    6763                 :         17 :                 ctx->bserrno = -EINVAL;
    6764                 :         17 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6765                 :         17 :                 return;
    6766                 :            :         }
    6767                 :            : 
    6768   [ -  +  -  + ]:        901 :         if (_blob->locked_operation_in_progress) {
    6769   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n");
    6770                 :          0 :                 ctx->bserrno = -EBUSY;
    6771                 :          0 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6772                 :          0 :                 return;
    6773                 :            :         }
    6774                 :            : 
    6775                 :        901 :         _blob->locked_operation_in_progress = true;
    6776                 :            : 
    6777                 :        901 :         spdk_blob_opts_init(&opts, sizeof(opts));
    6778                 :        901 :         blob_xattrs_init(&internal_xattrs);
    6779                 :            : 
    6780                 :            :         /* Change the size of new blob to the same as in original blob,
    6781                 :            :          * but do not allocate clusters */
    6782                 :        901 :         opts.thin_provision = true;
    6783                 :        901 :         opts.num_clusters = spdk_blob_get_num_clusters(_blob);
    6784         [ -  + ]:        901 :         opts.use_extent_table = _blob->use_extent_table;
    6785                 :            : 
    6786                 :            :         /* If there are any xattrs specified for snapshot, set them now */
    6787         [ +  + ]:        901 :         if (ctx->xattrs) {
    6788   [ -  +  -  + ]:         53 :                 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
    6789                 :            :         }
    6790                 :            :         /* Set internal xattr SNAPSHOT_IN_PROGRESS */
    6791                 :        901 :         internal_xattrs.count = 1;
    6792                 :        901 :         internal_xattrs.ctx = _blob;
    6793                 :        901 :         internal_xattrs.names = xattrs_names;
    6794                 :        901 :         internal_xattrs.get_value = bs_xattr_snapshot;
    6795                 :            : 
    6796                 :        901 :         bs_create_blob(_blob->bs, &opts, &internal_xattrs,
    6797                 :            :                        bs_snapshot_newblob_create_cpl, ctx);
    6798                 :            : }
    6799                 :            : 
    6800                 :            : void
    6801                 :        942 : spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid,
    6802                 :            :                         const struct spdk_blob_xattr_opts *snapshot_xattrs,
    6803                 :            :                         spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6804                 :            : {
    6805                 :        942 :         struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
    6806                 :            : 
    6807         [ -  + ]:        942 :         if (!ctx) {
    6808                 :          0 :                 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
    6809                 :          0 :                 return;
    6810                 :            :         }
    6811                 :        942 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
    6812                 :        942 :         ctx->cpl.u.blobid.cb_fn = cb_fn;
    6813                 :        942 :         ctx->cpl.u.blobid.cb_arg = cb_arg;
    6814                 :        942 :         ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
    6815                 :        942 :         ctx->bserrno = 0;
    6816                 :        942 :         ctx->frozen = false;
    6817                 :        942 :         ctx->original.id = blobid;
    6818                 :        942 :         ctx->xattrs = snapshot_xattrs;
    6819                 :            : 
    6820                 :        942 :         spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx);
    6821                 :            : }
    6822                 :            : /* END spdk_bs_create_snapshot */
    6823                 :            : 
    6824                 :            : /* START spdk_bs_create_clone */
    6825                 :            : 
    6826                 :            : static void
    6827                 :        214 : bs_xattr_clone(void *arg, const char *name,
    6828                 :            :                const void **value, size_t *value_len)
    6829                 :            : {
    6830   [ -  +  -  + ]:        214 :         assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0);
    6831                 :            : 
    6832                 :        214 :         struct spdk_blob *blob = (struct spdk_blob *)arg;
    6833                 :        214 :         *value = &blob->id;
    6834                 :        214 :         *value_len = sizeof(blob->id);
    6835                 :        214 : }
    6836                 :            : 
    6837                 :            : static void
    6838                 :        214 : bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6839                 :            : {
    6840                 :        214 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6841                 :        214 :         struct spdk_blob *clone = _blob;
    6842                 :            : 
    6843                 :        214 :         ctx->new.blob = clone;
    6844                 :        214 :         bs_blob_list_add(clone);
    6845                 :            : 
    6846                 :        214 :         spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx);
    6847                 :        214 : }
    6848                 :            : 
    6849                 :            : static void
    6850                 :        214 : bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
    6851                 :            : {
    6852                 :        214 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6853                 :            : 
    6854                 :        214 :         ctx->cpl.u.blobid.blobid = blobid;
    6855                 :        214 :         spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx);
    6856                 :        214 : }
    6857                 :            : 
    6858                 :            : static void
    6859                 :        232 : bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6860                 :            : {
    6861                 :        232 :         struct spdk_clone_snapshot_ctx  *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6862                 :        227 :         struct spdk_blob_opts           opts;
    6863                 :        227 :         struct spdk_blob_xattr_opts internal_xattrs;
    6864                 :        232 :         char *xattr_names[] = { BLOB_SNAPSHOT };
    6865                 :            : 
    6866         [ -  + ]:        232 :         if (bserrno != 0) {
    6867                 :          0 :                 bs_clone_snapshot_cleanup_finish(ctx, bserrno);
    6868                 :          0 :                 return;
    6869                 :            :         }
    6870                 :            : 
    6871                 :        232 :         ctx->original.blob = _blob;
    6872         [ -  + ]:        232 :         ctx->original.md_ro = _blob->md_ro;
    6873                 :            : 
    6874   [ +  +  +  +  :        232 :         if (!_blob->data_ro || !_blob->md_ro) {
             -  +  -  + ]
    6875   [ -  +  -  + ]:         18 :                 SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n");
    6876                 :         18 :                 ctx->bserrno = -EINVAL;
    6877                 :         18 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6878                 :         18 :                 return;
    6879                 :            :         }
    6880                 :            : 
    6881   [ -  +  -  + ]:        214 :         if (_blob->locked_operation_in_progress) {
    6882   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n");
    6883                 :          0 :                 ctx->bserrno = -EBUSY;
    6884                 :          0 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6885                 :          0 :                 return;
    6886                 :            :         }
    6887                 :            : 
    6888                 :        214 :         _blob->locked_operation_in_progress = true;
    6889                 :            : 
    6890                 :        214 :         spdk_blob_opts_init(&opts, sizeof(opts));
    6891                 :        214 :         blob_xattrs_init(&internal_xattrs);
    6892                 :            : 
    6893                 :        214 :         opts.thin_provision = true;
    6894                 :        214 :         opts.num_clusters = spdk_blob_get_num_clusters(_blob);
    6895         [ -  + ]:        214 :         opts.use_extent_table = _blob->use_extent_table;
    6896         [ +  + ]:        214 :         if (ctx->xattrs) {
    6897   [ -  +  -  + ]:         38 :                 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
    6898                 :            :         }
    6899                 :            : 
    6900                 :            :         /* Set internal xattr BLOB_SNAPSHOT */
    6901                 :        214 :         internal_xattrs.count = 1;
    6902                 :        214 :         internal_xattrs.ctx = _blob;
    6903                 :        214 :         internal_xattrs.names = xattr_names;
    6904                 :        214 :         internal_xattrs.get_value = bs_xattr_clone;
    6905                 :            : 
    6906                 :        214 :         bs_create_blob(_blob->bs, &opts, &internal_xattrs,
    6907                 :            :                        bs_clone_newblob_create_cpl, ctx);
    6908                 :            : }
    6909                 :            : 
    6910                 :            : void
    6911                 :        232 : spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid,
    6912                 :            :                      const struct spdk_blob_xattr_opts *clone_xattrs,
    6913                 :            :                      spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6914                 :            : {
    6915                 :        232 :         struct spdk_clone_snapshot_ctx  *ctx = calloc(1, sizeof(*ctx));
    6916                 :            : 
    6917         [ -  + ]:        232 :         if (!ctx) {
    6918                 :          0 :                 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
    6919                 :          0 :                 return;
    6920                 :            :         }
    6921                 :            : 
    6922                 :        232 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
    6923                 :        232 :         ctx->cpl.u.blobid.cb_fn = cb_fn;
    6924                 :        232 :         ctx->cpl.u.blobid.cb_arg = cb_arg;
    6925                 :        232 :         ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
    6926                 :        232 :         ctx->bserrno = 0;
    6927                 :        232 :         ctx->xattrs = clone_xattrs;
    6928                 :        232 :         ctx->original.id = blobid;
    6929                 :            : 
    6930                 :        232 :         spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx);
    6931                 :            : }
    6932                 :            : 
    6933                 :            : /* END spdk_bs_create_clone */
    6934                 :            : 
    6935                 :            : /* START spdk_bs_inflate_blob */
    6936                 :            : 
    6937                 :            : static void
    6938                 :         49 : bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno)
    6939                 :            : {
    6940                 :         49 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6941                 :         49 :         struct spdk_blob *_blob = ctx->original.blob;
    6942                 :            : 
    6943         [ -  + ]:         49 :         if (bserrno != 0) {
    6944                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6945                 :          0 :                 return;
    6946                 :            :         }
    6947                 :            : 
    6948                 :            :         /* Temporarily override md_ro flag for MD modification */
    6949                 :         49 :         _blob->md_ro = false;
    6950                 :            : 
    6951                 :         49 :         bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true);
    6952         [ -  + ]:         49 :         if (bserrno != 0) {
    6953                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6954                 :          0 :                 return;
    6955                 :            :         }
    6956                 :            : 
    6957         [ -  + ]:         49 :         assert(_parent != NULL);
    6958                 :            : 
    6959                 :         49 :         bs_blob_list_remove(_blob);
    6960                 :         49 :         _blob->parent_id = _parent->id;
    6961                 :            : 
    6962                 :         49 :         blob_back_bs_destroy(_blob);
    6963                 :         49 :         _blob->back_bs_dev = bs_create_blob_bs_dev(_parent);
    6964                 :         49 :         bs_blob_list_add(_blob);
    6965                 :            : 
    6966                 :         49 :         spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
    6967                 :            : }
    6968                 :            : 
    6969                 :            : static void
    6970                 :        231 : bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx)
    6971                 :            : {
    6972                 :        231 :         struct spdk_blob *_blob = ctx->original.blob;
    6973                 :            :         struct spdk_blob *_parent;
    6974                 :            : 
    6975   [ +  +  +  + ]:        231 :         if (ctx->allocate_all) {
    6976                 :            :                 /* remove thin provisioning */
    6977                 :        133 :                 bs_blob_list_remove(_blob);
    6978         [ +  + ]:        133 :                 if (_blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    6979                 :         32 :                         blob_remove_xattr(_blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
    6980                 :         32 :                         _blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6981                 :            :                 } else {
    6982                 :        101 :                         blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
    6983                 :            :                 }
    6984                 :        133 :                 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV;
    6985                 :        133 :                 blob_back_bs_destroy(_blob);
    6986                 :        133 :                 _blob->parent_id = SPDK_BLOBID_INVALID;
    6987                 :            :         } else {
    6988                 :            :                 /* For now, esnap clones always have allocate_all set. */
    6989         [ -  + ]:         98 :                 assert(!blob_is_esnap_clone(_blob));
    6990                 :            : 
    6991                 :         98 :                 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob;
    6992         [ +  + ]:         98 :                 if (_parent->parent_id != SPDK_BLOBID_INVALID) {
    6993                 :            :                         /* We must change the parent of the inflated blob */
    6994                 :         49 :                         spdk_bs_open_blob(_blob->bs, _parent->parent_id,
    6995                 :            :                                           bs_inflate_blob_set_parent_cpl, ctx);
    6996                 :         49 :                         return;
    6997                 :            :                 }
    6998                 :            : 
    6999                 :         49 :                 bs_blob_list_remove(_blob);
    7000                 :         49 :                 _blob->parent_id = SPDK_BLOBID_INVALID;
    7001                 :         49 :                 blob_back_bs_destroy(_blob);
    7002                 :         49 :                 _blob->back_bs_dev = bs_create_zeroes_dev();
    7003                 :            :         }
    7004                 :            : 
    7005                 :            :         /* Temporarily override md_ro flag for MD modification */
    7006                 :        182 :         _blob->md_ro = false;
    7007                 :        182 :         blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
    7008                 :        182 :         _blob->state = SPDK_BLOB_STATE_DIRTY;
    7009                 :            : 
    7010                 :        182 :         spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
    7011                 :            : }
    7012                 :            : 
    7013                 :            : /* Check if cluster needs allocation */
    7014                 :            : static inline bool
    7015                 :       4874 : bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all)
    7016                 :            : {
    7017                 :            :         struct spdk_blob_bs_dev *b;
    7018                 :            : 
    7019         [ -  + ]:       4874 :         assert(blob != NULL);
    7020                 :            : 
    7021         [ +  + ]:       4874 :         if (blob->active.clusters[cluster] != 0) {
    7022                 :            :                 /* Cluster is already allocated */
    7023                 :        138 :                 return false;
    7024                 :            :         }
    7025                 :            : 
    7026         [ +  + ]:       4736 :         if (blob->parent_id == SPDK_BLOBID_INVALID) {
    7027                 :            :                 /* Blob have no parent blob */
    7028                 :        320 :                 return allocate_all;
    7029                 :            :         }
    7030                 :            : 
    7031         [ +  + ]:       4416 :         if (blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    7032                 :        256 :                 return true;
    7033                 :            :         }
    7034                 :            : 
    7035                 :       4160 :         b = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
    7036   [ +  +  +  + ]:       4160 :         return (allocate_all || b->blob->active.clusters[cluster] != 0);
    7037                 :            : }
    7038                 :            : 
    7039                 :            : static void
    7040                 :       2067 : bs_inflate_blob_touch_next(void *cb_arg, int bserrno)
    7041                 :            : {
    7042                 :       2067 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    7043                 :       2067 :         struct spdk_blob *_blob = ctx->original.blob;
    7044                 :       2043 :         struct spdk_bs_cpl cpl;
    7045                 :            :         spdk_bs_user_op_t *op;
    7046                 :            :         uint64_t offset;
    7047                 :            : 
    7048         [ -  + ]:       2067 :         if (bserrno != 0) {
    7049                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    7050                 :          0 :                 return;
    7051                 :            :         }
    7052                 :            : 
    7053         [ +  + ]:       2668 :         for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) {
    7054   [ +  +  +  + ]:       2437 :                 if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) {
    7055                 :       1836 :                         break;
    7056                 :            :                 }
    7057                 :            :         }
    7058                 :            : 
    7059         [ +  + ]:       2067 :         if (ctx->cluster < _blob->active.num_clusters) {
    7060                 :       1836 :                 offset = bs_cluster_to_lba(_blob->bs, ctx->cluster);
    7061                 :            : 
    7062                 :            :                 /* We may safely increment a cluster before copying */
    7063                 :       1836 :                 ctx->cluster++;
    7064                 :            : 
    7065                 :            :                 /* Use a dummy 0B read as a context for cluster copy */
    7066                 :       1836 :                 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    7067                 :       1836 :                 cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next;
    7068                 :       1836 :                 cpl.u.blob_basic.cb_arg = ctx;
    7069                 :            : 
    7070                 :       1836 :                 op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob,
    7071                 :            :                                       NULL, 0, offset, 0);
    7072         [ -  + ]:       1836 :                 if (!op) {
    7073                 :          0 :                         bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM);
    7074                 :          0 :                         return;
    7075                 :            :                 }
    7076                 :            : 
    7077                 :       1836 :                 bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op);
    7078                 :            :         } else {
    7079                 :        231 :                 bs_inflate_blob_done(ctx);
    7080                 :            :         }
    7081                 :            : }
    7082                 :            : 
    7083                 :            : static void
    7084                 :        248 : bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    7085                 :            : {
    7086                 :        248 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    7087                 :            :         uint64_t clusters_needed;
    7088                 :            :         uint64_t i;
    7089                 :            : 
    7090         [ -  + ]:        248 :         if (bserrno != 0) {
    7091                 :          0 :                 bs_clone_snapshot_cleanup_finish(ctx, bserrno);
    7092                 :          0 :                 return;
    7093                 :            :         }
    7094                 :            : 
    7095                 :        248 :         ctx->original.blob = _blob;
    7096         [ -  + ]:        248 :         ctx->original.md_ro = _blob->md_ro;
    7097                 :            : 
    7098   [ -  +  -  + ]:        248 :         if (_blob->locked_operation_in_progress) {
    7099   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n");
    7100                 :          0 :                 ctx->bserrno = -EBUSY;
    7101                 :          0 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    7102                 :          0 :                 return;
    7103                 :            :         }
    7104                 :            : 
    7105                 :        248 :         _blob->locked_operation_in_progress = true;
    7106                 :            : 
    7107      [ +  +  + ]:        248 :         switch (_blob->parent_id) {
    7108                 :         33 :         case SPDK_BLOBID_INVALID:
    7109   [ +  +  +  + ]:         33 :                 if (!ctx->allocate_all) {
    7110                 :            :                         /* This blob has no parent, so we cannot decouple it. */
    7111                 :         17 :                         SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n");
    7112                 :         17 :                         bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL);
    7113                 :         17 :                         return;
    7114                 :            :                 }
    7115                 :         16 :                 break;
    7116                 :         32 :         case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
    7117                 :            :                 /*
    7118                 :            :                  * It would be better to rely on back_bs_dev->is_zeroes(), to determine which
    7119                 :            :                  * clusters require allocation. Until there is a blobstore consumer that
    7120                 :            :                  * uses esnaps with an spdk_bs_dev that implements a useful is_zeroes() it is not
    7121                 :            :                  * worth the effort.
    7122                 :            :                  */
    7123                 :         32 :                 ctx->allocate_all = true;
    7124                 :         32 :                 break;
    7125                 :        183 :         default:
    7126                 :        183 :                 break;
    7127                 :            :         }
    7128                 :            : 
    7129         [ -  + ]:        231 :         if (spdk_blob_is_thin_provisioned(_blob) == false) {
    7130                 :            :                 /* This is not thin provisioned blob. No need to inflate. */
    7131                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, 0);
    7132                 :          0 :                 return;
    7133                 :            :         }
    7134                 :            : 
    7135                 :            :         /* Do two passes - one to verify that we can obtain enough clusters
    7136                 :            :          * and another to actually claim them.
    7137                 :            :          */
    7138                 :        231 :         clusters_needed = 0;
    7139         [ +  + ]:       2668 :         for (i = 0; i < _blob->active.num_clusters; i++) {
    7140   [ +  +  +  + ]:       2437 :                 if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) {
    7141                 :       1836 :                         clusters_needed++;
    7142                 :            :                 }
    7143                 :            :         }
    7144                 :            : 
    7145         [ -  + ]:        231 :         if (clusters_needed > _blob->bs->num_free_clusters) {
    7146                 :            :                 /* Not enough free clusters. Cannot satisfy the request. */
    7147                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC);
    7148                 :          0 :                 return;
    7149                 :            :         }
    7150                 :            : 
    7151                 :        231 :         ctx->cluster = 0;
    7152                 :        231 :         bs_inflate_blob_touch_next(ctx, 0);
    7153                 :            : }
    7154                 :            : 
    7155                 :            : static void
    7156                 :        248 : bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7157                 :            :                 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg)
    7158                 :            : {
    7159                 :        248 :         struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
    7160                 :            : 
    7161         [ -  + ]:        248 :         if (!ctx) {
    7162                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7163                 :          0 :                 return;
    7164                 :            :         }
    7165                 :        248 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    7166                 :        248 :         ctx->cpl.u.bs_basic.cb_fn = cb_fn;
    7167                 :        248 :         ctx->cpl.u.bs_basic.cb_arg = cb_arg;
    7168                 :        248 :         ctx->bserrno = 0;
    7169                 :        248 :         ctx->original.id = blobid;
    7170                 :        248 :         ctx->channel = channel;
    7171                 :        248 :         ctx->allocate_all = allocate_all;
    7172                 :            : 
    7173                 :        248 :         spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx);
    7174                 :            : }
    7175                 :            : 
    7176                 :            : void
    7177                 :        117 : spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7178                 :            :                      spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
    7179                 :            : {
    7180                 :        117 :         bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg);
    7181                 :        117 : }
    7182                 :            : 
    7183                 :            : void
    7184                 :        131 : spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7185                 :            :                              spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
    7186                 :            : {
    7187                 :        131 :         bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg);
    7188                 :        131 : }
    7189                 :            : /* END spdk_bs_inflate_blob */
    7190                 :            : 
    7191                 :            : /* START spdk_bs_blob_shallow_copy */
    7192                 :            : 
    7193                 :            : struct shallow_copy_ctx {
    7194                 :            :         struct spdk_bs_cpl cpl;
    7195                 :            :         int bserrno;
    7196                 :            : 
    7197                 :            :         /* Blob source for copy */
    7198                 :            :         struct spdk_blob_store *bs;
    7199                 :            :         spdk_blob_id blobid;
    7200                 :            :         struct spdk_blob *blob;
    7201                 :            :         struct spdk_io_channel *blob_channel;
    7202                 :            : 
    7203                 :            :         /* Destination device for copy */
    7204                 :            :         struct spdk_bs_dev *ext_dev;
    7205                 :            :         struct spdk_io_channel *ext_channel;
    7206                 :            : 
    7207                 :            :         /* Current cluster for copy operation */
    7208                 :            :         uint64_t cluster;
    7209                 :            : 
    7210                 :            :         /* Buffer for blob reading */
    7211                 :            :         uint8_t *read_buff;
    7212                 :            : 
    7213                 :            :         /* Struct for external device writing */
    7214                 :            :         struct spdk_bs_dev_cb_args ext_args;
    7215                 :            : 
    7216                 :            :         /* Actual number of copied clusters */
    7217                 :            :         uint64_t copied_clusters_count;
    7218                 :            : 
    7219                 :            :         /* Status callback for updates about the ongoing operation */
    7220                 :            :         spdk_blob_shallow_copy_status status_cb;
    7221                 :            : 
    7222                 :            :         /* Argument passed to function status_cb */
    7223                 :            :         void *status_cb_arg;
    7224                 :            : };
    7225                 :            : 
    7226                 :            : static void
    7227                 :         65 : bs_shallow_copy_cleanup_finish(void *cb_arg, int bserrno)
    7228                 :            : {
    7229                 :         65 :         struct shallow_copy_ctx *ctx = cb_arg;
    7230                 :         65 :         struct spdk_bs_cpl *cpl = &ctx->cpl;
    7231                 :            : 
    7232         [ -  + ]:         65 :         if (bserrno != 0) {
    7233                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, cleanup error %d\n", ctx->blob->id, bserrno);
    7234                 :          0 :                 ctx->bserrno = bserrno;
    7235                 :            :         }
    7236                 :            : 
    7237                 :         65 :         ctx->ext_dev->destroy_channel(ctx->ext_dev, ctx->ext_channel);
    7238                 :         65 :         spdk_free(ctx->read_buff);
    7239                 :            : 
    7240                 :         65 :         cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
    7241                 :            : 
    7242                 :         65 :         free(ctx);
    7243                 :         65 : }
    7244                 :            : 
    7245                 :            : static void
    7246                 :         34 : bs_shallow_copy_bdev_write_cpl(struct spdk_io_channel *channel, void *cb_arg, int bserrno)
    7247                 :            : {
    7248                 :         34 :         struct shallow_copy_ctx *ctx = cb_arg;
    7249                 :         34 :         struct spdk_blob *_blob = ctx->blob;
    7250                 :            : 
    7251         [ -  + ]:         34 :         if (bserrno != 0) {
    7252                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, ext dev write error %d\n", ctx->blob->id, bserrno);
    7253                 :          0 :                 ctx->bserrno = bserrno;
    7254                 :          0 :                 _blob->locked_operation_in_progress = false;
    7255                 :          0 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7256                 :          0 :                 return;
    7257                 :            :         }
    7258                 :            : 
    7259                 :         34 :         ctx->cluster++;
    7260         [ +  - ]:         34 :         if (ctx->status_cb) {
    7261                 :         34 :                 ctx->copied_clusters_count++;
    7262                 :         34 :                 ctx->status_cb(ctx->copied_clusters_count, ctx->status_cb_arg);
    7263                 :            :         }
    7264                 :            : 
    7265                 :         34 :         bs_shallow_copy_cluster_find_next(ctx);
    7266                 :            : }
    7267                 :            : 
    7268                 :            : static void
    7269                 :         34 : bs_shallow_copy_blob_read_cpl(void *cb_arg, int bserrno)
    7270                 :            : {
    7271                 :         34 :         struct shallow_copy_ctx *ctx = cb_arg;
    7272                 :         34 :         struct spdk_bs_dev *ext_dev = ctx->ext_dev;
    7273                 :         34 :         struct spdk_blob *_blob = ctx->blob;
    7274                 :            : 
    7275         [ -  + ]:         34 :         if (bserrno != 0) {
    7276                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob read error %d\n", ctx->blob->id, bserrno);
    7277                 :          0 :                 ctx->bserrno = bserrno;
    7278                 :          0 :                 _blob->locked_operation_in_progress = false;
    7279                 :          0 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7280                 :          0 :                 return;
    7281                 :            :         }
    7282                 :            : 
    7283                 :         34 :         ctx->ext_args.channel = ctx->ext_channel;
    7284                 :         34 :         ctx->ext_args.cb_fn = bs_shallow_copy_bdev_write_cpl;
    7285                 :         34 :         ctx->ext_args.cb_arg = ctx;
    7286                 :            : 
    7287                 :         52 :         ext_dev->write(ext_dev, ctx->ext_channel, ctx->read_buff,
    7288                 :         34 :                        bs_cluster_to_lba(_blob->bs, ctx->cluster),
    7289                 :         34 :                        bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
    7290                 :            :                        &ctx->ext_args);
    7291                 :            : }
    7292                 :            : 
    7293                 :            : static void
    7294                 :         51 : bs_shallow_copy_cluster_find_next(void *cb_arg)
    7295                 :            : {
    7296                 :         51 :         struct shallow_copy_ctx *ctx = cb_arg;
    7297                 :         51 :         struct spdk_blob *_blob = ctx->blob;
    7298                 :            : 
    7299         [ +  + ]:         85 :         while (ctx->cluster < _blob->active.num_clusters) {
    7300         [ +  + ]:         68 :                 if (_blob->active.clusters[ctx->cluster] != 0) {
    7301                 :         34 :                         break;
    7302                 :            :                 }
    7303                 :            : 
    7304                 :         34 :                 ctx->cluster++;
    7305                 :            :         }
    7306                 :            : 
    7307         [ +  + ]:         51 :         if (ctx->cluster < _blob->active.num_clusters) {
    7308                 :         52 :                 blob_request_submit_op_single(ctx->blob_channel, _blob, ctx->read_buff,
    7309                 :         34 :                                               bs_cluster_to_lba(_blob->bs, ctx->cluster),
    7310                 :         34 :                                               bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
    7311                 :            :                                               bs_shallow_copy_blob_read_cpl, ctx, SPDK_BLOB_READ);
    7312                 :            :         } else {
    7313                 :         17 :                 _blob->locked_operation_in_progress = false;
    7314                 :         17 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7315                 :            :         }
    7316                 :         51 : }
    7317                 :            : 
    7318                 :            : static void
    7319                 :         65 : bs_shallow_copy_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    7320                 :            : {
    7321                 :         65 :         struct shallow_copy_ctx *ctx = cb_arg;
    7322                 :         65 :         struct spdk_bs_dev *ext_dev = ctx->ext_dev;
    7323                 :            :         uint32_t blob_block_size;
    7324                 :            :         uint64_t blob_total_size;
    7325                 :            : 
    7326         [ -  + ]:         65 :         if (bserrno != 0) {
    7327                 :          0 :                 SPDK_ERRLOG("Shallow copy blob open error %d\n", bserrno);
    7328                 :          0 :                 ctx->bserrno = bserrno;
    7329                 :          0 :                 bs_shallow_copy_cleanup_finish(ctx, 0);
    7330                 :          0 :                 return;
    7331                 :            :         }
    7332                 :            : 
    7333         [ +  + ]:         65 :         if (!spdk_blob_is_read_only(_blob)) {
    7334                 :         16 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob must be read only\n", _blob->id);
    7335                 :         16 :                 ctx->bserrno = -EPERM;
    7336                 :         16 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7337                 :         16 :                 return;
    7338                 :            :         }
    7339                 :            : 
    7340                 :         49 :         blob_block_size = _blob->bs->dev->blocklen;
    7341                 :         49 :         blob_total_size = spdk_blob_get_num_clusters(_blob) * spdk_bs_get_cluster_size(_blob->bs);
    7342                 :            : 
    7343         [ +  + ]:         49 :         if (blob_total_size > ext_dev->blockcnt * ext_dev->blocklen) {
    7344                 :         16 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device must have at least blob size\n",
    7345                 :            :                             _blob->id);
    7346                 :         16 :                 ctx->bserrno = -EINVAL;
    7347                 :         16 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7348                 :         16 :                 return;
    7349                 :            :         }
    7350                 :            : 
    7351   [ +  +  +  + ]:         33 :         if (blob_block_size % ext_dev->blocklen != 0) {
    7352                 :         16 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device block size is not compatible with \
    7353                 :            : blobstore block size\n", _blob->id);
    7354                 :         16 :                 ctx->bserrno = -EINVAL;
    7355                 :         16 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7356                 :         16 :                 return;
    7357                 :            :         }
    7358                 :            : 
    7359                 :         17 :         ctx->blob = _blob;
    7360                 :            : 
    7361   [ -  +  -  + ]:         17 :         if (_blob->locked_operation_in_progress) {
    7362   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "blob 0x%" PRIx64 " shallow copy - another operation in progress\n", _blob->id);
    7363                 :          0 :                 ctx->bserrno = -EBUSY;
    7364                 :          0 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7365                 :          0 :                 return;
    7366                 :            :         }
    7367                 :            : 
    7368                 :         17 :         _blob->locked_operation_in_progress = true;
    7369                 :            : 
    7370                 :         17 :         ctx->cluster = 0;
    7371                 :         17 :         bs_shallow_copy_cluster_find_next(ctx);
    7372                 :            : }
    7373                 :            : 
    7374                 :            : int
    7375                 :         65 : spdk_bs_blob_shallow_copy(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7376                 :            :                           spdk_blob_id blobid, struct spdk_bs_dev *ext_dev,
    7377                 :            :                           spdk_blob_shallow_copy_status status_cb_fn, void *status_cb_arg,
    7378                 :            :                           spdk_blob_op_complete cb_fn, void *cb_arg)
    7379                 :            : {
    7380                 :            :         struct shallow_copy_ctx *ctx;
    7381                 :            :         struct spdk_io_channel *ext_channel;
    7382                 :            : 
    7383                 :         65 :         ctx = calloc(1, sizeof(*ctx));
    7384         [ -  + ]:         65 :         if (!ctx) {
    7385                 :          0 :                 return -ENOMEM;
    7386                 :            :         }
    7387                 :            : 
    7388                 :         65 :         ctx->bs = bs;
    7389                 :         65 :         ctx->blobid = blobid;
    7390                 :         65 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    7391                 :         65 :         ctx->cpl.u.bs_basic.cb_fn = cb_fn;
    7392                 :         65 :         ctx->cpl.u.bs_basic.cb_arg = cb_arg;
    7393                 :         65 :         ctx->bserrno = 0;
    7394                 :         65 :         ctx->blob_channel = channel;
    7395                 :         65 :         ctx->status_cb = status_cb_fn;
    7396                 :         65 :         ctx->status_cb_arg = status_cb_arg;
    7397                 :         65 :         ctx->read_buff = spdk_malloc(bs->cluster_sz, bs->dev->blocklen, NULL,
    7398                 :            :                                      SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
    7399         [ -  + ]:         65 :         if (!ctx->read_buff) {
    7400                 :          0 :                 free(ctx);
    7401                 :          0 :                 return -ENOMEM;
    7402                 :            :         }
    7403                 :            : 
    7404                 :         65 :         ext_channel = ext_dev->create_channel(ext_dev);
    7405         [ -  + ]:         65 :         if (!ext_channel) {
    7406                 :          0 :                 spdk_free(ctx->read_buff);
    7407                 :          0 :                 free(ctx);
    7408                 :          0 :                 return -ENOMEM;
    7409                 :            :         }
    7410                 :         65 :         ctx->ext_dev = ext_dev;
    7411                 :         65 :         ctx->ext_channel = ext_channel;
    7412                 :            : 
    7413                 :         65 :         spdk_bs_open_blob(ctx->bs, ctx->blobid, bs_shallow_copy_blob_open_cpl, ctx);
    7414                 :            : 
    7415                 :         65 :         return 0;
    7416                 :            : }
    7417                 :            : /* END spdk_bs_blob_shallow_copy */
    7418                 :            : 
    7419                 :            : /* START spdk_bs_blob_set_parent */
    7420                 :            : 
    7421                 :            : struct set_parent_ctx {
    7422                 :            :         struct spdk_blob_store *bs;
    7423                 :            :         int                     bserrno;
    7424                 :            :         spdk_bs_op_complete     cb_fn;
    7425                 :            :         void                    *cb_arg;
    7426                 :            : 
    7427                 :            :         struct spdk_blob        *blob;
    7428                 :            :         bool                    blob_md_ro;
    7429                 :            : 
    7430                 :            :         struct blob_parent      parent;
    7431                 :            : };
    7432                 :            : 
    7433                 :            : static void
    7434                 :        100 : bs_set_parent_cleanup_finish(void *cb_arg, int bserrno)
    7435                 :            : {
    7436                 :        100 :         struct set_parent_ctx *ctx = cb_arg;
    7437                 :            : 
    7438         [ -  + ]:        100 :         assert(ctx != NULL);
    7439                 :            : 
    7440         [ -  + ]:        100 :         if (bserrno != 0) {
    7441                 :          0 :                 SPDK_ERRLOG("blob set parent finish error %d\n", bserrno);
    7442         [ #  # ]:          0 :                 if (ctx->bserrno == 0) {
    7443                 :          0 :                         ctx->bserrno = bserrno;
    7444                 :            :                 }
    7445                 :            :         }
    7446                 :            : 
    7447                 :        100 :         ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
    7448                 :            : 
    7449                 :        100 :         free(ctx);
    7450                 :        100 : }
    7451                 :            : 
    7452                 :            : static void
    7453                 :         83 : bs_set_parent_close_snapshot(void *cb_arg, int bserrno)
    7454                 :            : {
    7455                 :         83 :         struct set_parent_ctx *ctx = cb_arg;
    7456                 :            : 
    7457         [ +  + ]:         83 :         if (ctx->bserrno != 0) {
    7458                 :         32 :                 spdk_blob_close(ctx->parent.u.snapshot.blob, bs_set_parent_cleanup_finish, ctx);
    7459                 :         32 :                 return;
    7460                 :            :         }
    7461                 :            : 
    7462         [ -  + ]:         51 :         if (bserrno != 0) {
    7463                 :          0 :                 SPDK_ERRLOG("blob close error %d\n", bserrno);
    7464                 :          0 :                 ctx->bserrno = bserrno;
    7465                 :            :         }
    7466                 :            : 
    7467                 :         51 :         bs_set_parent_cleanup_finish(ctx, ctx->bserrno);
    7468                 :            : }
    7469                 :            : 
    7470                 :            : static void
    7471                 :         51 : bs_set_parent_close_blob(void *cb_arg, int bserrno)
    7472                 :            : {
    7473                 :         51 :         struct set_parent_ctx *ctx = cb_arg;
    7474                 :         51 :         struct spdk_blob *blob = ctx->blob;
    7475                 :         51 :         struct spdk_blob *snapshot = ctx->parent.u.snapshot.blob;
    7476                 :            : 
    7477   [ -  +  -  - ]:         51 :         if (bserrno != 0 && ctx->bserrno == 0) {
    7478                 :          0 :                 SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
    7479                 :          0 :                 ctx->bserrno = bserrno;
    7480                 :            :         }
    7481                 :            : 
    7482                 :            :         /* Revert md_ro to original state */
    7483         [ -  + ]:         51 :         blob->md_ro = ctx->blob_md_ro;
    7484                 :            : 
    7485                 :         51 :         blob->locked_operation_in_progress = false;
    7486                 :         51 :         snapshot->locked_operation_in_progress = false;
    7487                 :            : 
    7488                 :         51 :         spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7489                 :         51 : }
    7490                 :            : 
    7491                 :            : static void
    7492                 :         51 : bs_set_parent_set_back_bs_dev_done(void *cb_arg, int bserrno)
    7493                 :            : {
    7494                 :         51 :         struct set_parent_ctx *ctx = cb_arg;
    7495                 :         51 :         struct spdk_blob *blob = ctx->blob;
    7496                 :            : 
    7497         [ -  + ]:         51 :         if (bserrno != 0) {
    7498                 :          0 :                 SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
    7499                 :          0 :                 ctx->bserrno = bserrno;
    7500                 :          0 :                 bs_set_parent_close_blob(ctx, bserrno);
    7501                 :          0 :                 return;
    7502                 :            :         }
    7503                 :            : 
    7504                 :         51 :         spdk_blob_sync_md(blob, bs_set_parent_close_blob, ctx);
    7505                 :            : }
    7506                 :            : 
    7507                 :            : static int
    7508                 :         51 : bs_set_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
    7509                 :            : {
    7510                 :            :         int rc;
    7511                 :            : 
    7512                 :         51 :         bs_blob_list_remove(blob);
    7513                 :            : 
    7514                 :         51 :         rc = blob_set_xattr(blob, BLOB_SNAPSHOT, &parent->u.snapshot.id, sizeof(spdk_blob_id), true);
    7515         [ -  + ]:         51 :         if (rc != 0) {
    7516                 :          0 :                 SPDK_ERRLOG("error %d setting snapshot xattr\n", rc);
    7517                 :          0 :                 return rc;
    7518                 :            :         }
    7519                 :         51 :         blob->parent_id = parent->u.snapshot.id;
    7520                 :            : 
    7521         [ +  + ]:         51 :         if (blob_is_esnap_clone(blob)) {
    7522                 :            :                 /* Remove the xattr that references the external snapshot */
    7523                 :         17 :                 blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
    7524                 :         17 :                 blob_remove_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
    7525                 :            :         }
    7526                 :            : 
    7527                 :         51 :         bs_blob_list_add(blob);
    7528                 :            : 
    7529                 :         51 :         return 0;
    7530                 :            : }
    7531                 :            : 
    7532                 :            : static void
    7533                 :         84 : bs_set_parent_snapshot_open_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
    7534                 :            : {
    7535                 :         84 :         struct set_parent_ctx *ctx = cb_arg;
    7536                 :         84 :         struct spdk_blob *blob = ctx->blob;
    7537                 :            :         struct spdk_bs_dev *back_bs_dev;
    7538                 :            : 
    7539         [ +  + ]:         84 :         if (bserrno != 0) {
    7540                 :          1 :                 SPDK_ERRLOG("snapshot open error %d\n", bserrno);
    7541                 :          1 :                 ctx->bserrno = bserrno;
    7542                 :          1 :                 spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
    7543                 :          1 :                 return;
    7544                 :            :         }
    7545                 :            : 
    7546                 :         83 :         ctx->parent.u.snapshot.blob = snapshot;
    7547                 :         83 :         ctx->parent.u.snapshot.id = snapshot->id;
    7548                 :            : 
    7549         [ +  + ]:         83 :         if (!spdk_blob_is_snapshot(snapshot)) {
    7550                 :         16 :                 SPDK_ERRLOG("parent blob is not a snapshot\n");
    7551                 :         16 :                 ctx->bserrno = -EINVAL;
    7552                 :         16 :                 spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7553                 :         16 :                 return;
    7554                 :            :         }
    7555                 :            : 
    7556         [ +  + ]:         67 :         if (blob->active.num_clusters != snapshot->active.num_clusters) {
    7557                 :         16 :                 SPDK_ERRLOG("parent blob has a number of clusters different from child's ones\n");
    7558                 :         16 :                 ctx->bserrno = -EINVAL;
    7559                 :         16 :                 spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7560                 :         16 :                 return;
    7561                 :            :         }
    7562                 :            : 
    7563   [ +  +  +  +  :         51 :         if (blob->locked_operation_in_progress || snapshot->locked_operation_in_progress) {
             -  +  -  + ]
    7564                 :          0 :                 SPDK_ERRLOG("cannot set parent of blob, another operation in progress\n");
    7565                 :          0 :                 ctx->bserrno = -EBUSY;
    7566                 :          0 :                 spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7567                 :          0 :                 return;
    7568                 :            :         }
    7569                 :            : 
    7570                 :         51 :         blob->locked_operation_in_progress = true;
    7571                 :         51 :         snapshot->locked_operation_in_progress = true;
    7572                 :            : 
    7573                 :            :         /* Temporarily override md_ro flag for MD modification */
    7574                 :         51 :         blob->md_ro = false;
    7575                 :            : 
    7576                 :         51 :         back_bs_dev = bs_create_blob_bs_dev(snapshot);
    7577                 :            : 
    7578                 :         51 :         blob_set_back_bs_dev(blob, back_bs_dev, bs_set_parent_refs, &ctx->parent,
    7579                 :            :                              bs_set_parent_set_back_bs_dev_done,
    7580                 :            :                              ctx);
    7581                 :            : }
    7582                 :            : 
    7583                 :            : static void
    7584                 :        100 : bs_set_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
    7585                 :            : {
    7586                 :        100 :         struct set_parent_ctx *ctx = cb_arg;
    7587                 :            : 
    7588         [ -  + ]:        100 :         if (bserrno != 0) {
    7589                 :          0 :                 SPDK_ERRLOG("blob open error %d\n", bserrno);
    7590                 :          0 :                 ctx->bserrno = bserrno;
    7591                 :          0 :                 bs_set_parent_cleanup_finish(ctx, 0);
    7592                 :          0 :                 return;
    7593                 :            :         }
    7594                 :            : 
    7595         [ +  + ]:        100 :         if (!spdk_blob_is_thin_provisioned(blob)) {
    7596                 :         16 :                 SPDK_ERRLOG("blob is not thin-provisioned\n");
    7597                 :         16 :                 ctx->bserrno = -EINVAL;
    7598                 :         16 :                 spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
    7599                 :         16 :                 return;
    7600                 :            :         }
    7601                 :            : 
    7602                 :         84 :         ctx->blob = blob;
    7603         [ -  + ]:         84 :         ctx->blob_md_ro = blob->md_ro;
    7604                 :            : 
    7605                 :         84 :         spdk_bs_open_blob(ctx->bs, ctx->parent.u.snapshot.id, bs_set_parent_snapshot_open_cpl, ctx);
    7606                 :            : }
    7607                 :            : 
    7608                 :            : void
    7609                 :        151 : spdk_bs_blob_set_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
    7610                 :            :                         spdk_blob_id snapshot_id, spdk_blob_op_complete cb_fn, void *cb_arg)
    7611                 :            : {
    7612                 :            :         struct set_parent_ctx *ctx;
    7613                 :            : 
    7614         [ +  + ]:        151 :         if (snapshot_id == SPDK_BLOBID_INVALID) {
    7615                 :         16 :                 SPDK_ERRLOG("snapshot id not valid\n");
    7616                 :         16 :                 cb_fn(cb_arg, -EINVAL);
    7617                 :         16 :                 return;
    7618                 :            :         }
    7619                 :            : 
    7620         [ +  + ]:        135 :         if (blob_id == snapshot_id) {
    7621                 :         16 :                 SPDK_ERRLOG("blob id and snapshot id cannot be the same\n");
    7622                 :         16 :                 cb_fn(cb_arg, -EINVAL);
    7623                 :         16 :                 return;
    7624                 :            :         }
    7625                 :            : 
    7626         [ +  + ]:        119 :         if (spdk_blob_get_parent_snapshot(bs, blob_id) == snapshot_id) {
    7627                 :         19 :                 SPDK_NOTICELOG("snapshot is already the parent of blob\n");
    7628                 :         19 :                 cb_fn(cb_arg, -EEXIST);
    7629                 :         19 :                 return;
    7630                 :            :         }
    7631                 :            : 
    7632                 :        100 :         ctx = calloc(1, sizeof(*ctx));
    7633         [ -  + ]:        100 :         if (!ctx) {
    7634                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7635                 :          0 :                 return;
    7636                 :            :         }
    7637                 :            : 
    7638                 :        100 :         ctx->bs = bs;
    7639                 :        100 :         ctx->parent.u.snapshot.id = snapshot_id;
    7640                 :        100 :         ctx->cb_fn = cb_fn;
    7641                 :        100 :         ctx->cb_arg = cb_arg;
    7642                 :        100 :         ctx->bserrno = 0;
    7643                 :            : 
    7644                 :        100 :         spdk_bs_open_blob(bs, blob_id, bs_set_parent_blob_open_cpl, ctx);
    7645                 :            : }
    7646                 :            : /* END spdk_bs_blob_set_parent */
    7647                 :            : 
    7648                 :            : /* START spdk_bs_blob_set_external_parent */
    7649                 :            : 
    7650                 :            : static void
    7651                 :         70 : bs_set_external_parent_cleanup_finish(void *cb_arg, int bserrno)
    7652                 :            : {
    7653                 :         70 :         struct set_parent_ctx *ctx = cb_arg;
    7654                 :            : 
    7655         [ -  + ]:         70 :         if (bserrno != 0) {
    7656                 :          0 :                 SPDK_ERRLOG("blob set external parent finish error %d\n", bserrno);
    7657         [ #  # ]:          0 :                 if (ctx->bserrno == 0) {
    7658                 :          0 :                         ctx->bserrno = bserrno;
    7659                 :            :                 }
    7660                 :            :         }
    7661                 :            : 
    7662                 :         70 :         ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
    7663                 :            : 
    7664                 :         70 :         free(ctx->parent.u.esnap.id);
    7665                 :         70 :         free(ctx);
    7666                 :         70 : }
    7667                 :            : 
    7668                 :            : static void
    7669                 :         35 : bs_set_external_parent_close_blob(void *cb_arg, int bserrno)
    7670                 :            : {
    7671                 :         35 :         struct set_parent_ctx *ctx = cb_arg;
    7672                 :         35 :         struct spdk_blob *blob = ctx->blob;
    7673                 :            : 
    7674   [ -  +  -  - ]:         35 :         if (bserrno != 0 && ctx->bserrno == 0) {
    7675                 :          0 :                 SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
    7676                 :          0 :                 ctx->bserrno = bserrno;
    7677                 :            :         }
    7678                 :            : 
    7679                 :            :         /* Revert md_ro to original state */
    7680         [ -  + ]:         35 :         blob->md_ro = ctx->blob_md_ro;
    7681                 :            : 
    7682                 :         35 :         blob->locked_operation_in_progress = false;
    7683                 :            : 
    7684                 :         35 :         spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
    7685                 :         35 : }
    7686                 :            : 
    7687                 :            : static void
    7688                 :         35 : bs_set_external_parent_unfrozen(void *cb_arg, int bserrno)
    7689                 :            : {
    7690                 :         35 :         struct set_parent_ctx *ctx = cb_arg;
    7691                 :         35 :         struct spdk_blob *blob = ctx->blob;
    7692                 :            : 
    7693         [ -  + ]:         35 :         if (bserrno != 0) {
    7694                 :          0 :                 SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
    7695                 :          0 :                 ctx->bserrno = bserrno;
    7696                 :          0 :                 bs_set_external_parent_close_blob(ctx, bserrno);
    7697                 :          0 :                 return;
    7698                 :            :         }
    7699                 :            : 
    7700                 :         35 :         spdk_blob_sync_md(blob, bs_set_external_parent_close_blob, ctx);
    7701                 :            : }
    7702                 :            : 
    7703                 :            : static int
    7704                 :         35 : bs_set_external_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
    7705                 :            : {
    7706                 :            :         int rc;
    7707                 :            : 
    7708                 :         35 :         bs_blob_list_remove(blob);
    7709                 :            : 
    7710         [ -  + ]:         35 :         if (spdk_blob_is_clone(blob)) {
    7711                 :            :                 /* Remove the xattr that references the snapshot */
    7712                 :          0 :                 blob->parent_id = SPDK_BLOBID_INVALID;
    7713                 :          0 :                 blob_remove_xattr(blob, BLOB_SNAPSHOT, true);
    7714                 :            :         }
    7715                 :            : 
    7716                 :         35 :         rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, parent->u.esnap.id,
    7717                 :         35 :                             parent->u.esnap.id_len, true);
    7718         [ -  + ]:         35 :         if (rc != 0) {
    7719                 :          0 :                 SPDK_ERRLOG("error %d setting external snapshot xattr\n", rc);
    7720                 :          0 :                 return rc;
    7721                 :            :         }
    7722                 :         35 :         blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    7723                 :            : 
    7724                 :         35 :         bs_blob_list_add(blob);
    7725                 :            : 
    7726                 :         35 :         return 0;
    7727                 :            : }
    7728                 :            : 
    7729                 :            : static void
    7730                 :         70 : bs_set_external_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
    7731                 :            : {
    7732                 :         70 :         struct set_parent_ctx *ctx = cb_arg;
    7733                 :         70 :         const void *esnap_id;
    7734                 :         70 :         size_t esnap_id_len;
    7735                 :            :         int rc;
    7736                 :            : 
    7737         [ -  + ]:         70 :         if (bserrno != 0) {
    7738                 :          0 :                 SPDK_ERRLOG("blob open error %d\n", bserrno);
    7739                 :          0 :                 ctx->bserrno = bserrno;
    7740                 :          0 :                 bs_set_parent_cleanup_finish(ctx, 0);
    7741                 :          0 :                 return;
    7742                 :            :         }
    7743                 :            : 
    7744                 :         70 :         ctx->blob = blob;
    7745         [ -  + ]:         70 :         ctx->blob_md_ro = blob->md_ro;
    7746                 :            : 
    7747                 :         70 :         rc = spdk_blob_get_esnap_id(blob, &esnap_id, &esnap_id_len);
    7748   [ +  +  +  -  :         70 :         if (rc == 0 && esnap_id != NULL && esnap_id_len == ctx->parent.u.esnap.id_len &&
                   +  - ]
    7749   [ +  +  -  +  :         20 :             memcmp(esnap_id, ctx->parent.u.esnap.id, esnap_id_len) == 0) {
                   +  + ]
    7750                 :         19 :                 SPDK_ERRLOG("external snapshot is already the parent of blob\n");
    7751                 :         19 :                 ctx->bserrno = -EEXIST;
    7752                 :         19 :                 goto error;
    7753                 :            :         }
    7754                 :            : 
    7755         [ +  + ]:         51 :         if (!spdk_blob_is_thin_provisioned(blob)) {
    7756                 :         16 :                 SPDK_ERRLOG("blob is not thin-provisioned\n");
    7757                 :         16 :                 ctx->bserrno = -EINVAL;
    7758                 :         16 :                 goto error;
    7759                 :            :         }
    7760                 :            : 
    7761   [ -  +  -  + ]:         35 :         if (blob->locked_operation_in_progress) {
    7762                 :          0 :                 SPDK_ERRLOG("cannot set external parent of blob, another operation in progress\n");
    7763                 :          0 :                 ctx->bserrno = -EBUSY;
    7764                 :          0 :                 goto error;
    7765                 :            :         }
    7766                 :            : 
    7767                 :         35 :         blob->locked_operation_in_progress = true;
    7768                 :            : 
    7769                 :            :         /* Temporarily override md_ro flag for MD modification */
    7770                 :         35 :         blob->md_ro = false;
    7771                 :            : 
    7772                 :         35 :         blob_set_back_bs_dev(blob, ctx->parent.u.esnap.back_bs_dev, bs_set_external_parent_refs,
    7773                 :            :                              &ctx->parent, bs_set_external_parent_unfrozen, ctx);
    7774                 :         35 :         return;
    7775                 :            : 
    7776                 :         35 : error:
    7777                 :         35 :         spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
    7778                 :            : }
    7779                 :            : 
    7780                 :            : void
    7781                 :        102 : spdk_bs_blob_set_external_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
    7782                 :            :                                  struct spdk_bs_dev *esnap_bs_dev, const void *esnap_id,
    7783                 :            :                                  uint32_t esnap_id_len, spdk_blob_op_complete cb_fn, void *cb_arg)
    7784                 :            : {
    7785                 :            :         struct set_parent_ctx *ctx;
    7786                 :            :         uint64_t esnap_dev_size, cluster_sz;
    7787                 :            : 
    7788   [ +  +  +  +  :        102 :         if (sizeof(blob_id) == esnap_id_len && memcmp(&blob_id, esnap_id, sizeof(blob_id)) == 0) {
                   +  - ]
    7789                 :         16 :                 SPDK_ERRLOG("blob id and external snapshot id cannot be the same\n");
    7790                 :         16 :                 cb_fn(cb_arg, -EINVAL);
    7791                 :         16 :                 return;
    7792                 :            :         }
    7793                 :            : 
    7794                 :         86 :         esnap_dev_size = esnap_bs_dev->blockcnt * esnap_bs_dev->blocklen;
    7795                 :         86 :         cluster_sz = spdk_bs_get_cluster_size(bs);
    7796   [ +  +  +  + ]:         86 :         if ((esnap_dev_size % cluster_sz) != 0) {
    7797                 :         16 :                 SPDK_ERRLOG("Esnap device size %" PRIu64 " is not an integer multiple of "
    7798                 :            :                             "cluster size %" PRIu64 "\n", esnap_dev_size, cluster_sz);
    7799                 :         16 :                 cb_fn(cb_arg, -EINVAL);
    7800                 :         16 :                 return;
    7801                 :            :         }
    7802                 :            : 
    7803                 :         70 :         ctx = calloc(1, sizeof(*ctx));
    7804         [ -  + ]:         70 :         if (!ctx) {
    7805                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7806                 :          0 :                 return;
    7807                 :            :         }
    7808                 :            : 
    7809                 :         70 :         ctx->parent.u.esnap.id = calloc(1, esnap_id_len);
    7810         [ -  + ]:         70 :         if (!ctx->parent.u.esnap.id) {
    7811                 :          0 :                 free(ctx);
    7812                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7813                 :          0 :                 return;
    7814                 :            :         }
    7815                 :            : 
    7816                 :         70 :         ctx->bs = bs;
    7817                 :         70 :         ctx->parent.u.esnap.back_bs_dev = esnap_bs_dev;
    7818   [ -  +  -  + ]:         70 :         memcpy(ctx->parent.u.esnap.id, esnap_id, esnap_id_len);
    7819                 :         70 :         ctx->parent.u.esnap.id_len = esnap_id_len;
    7820                 :         70 :         ctx->cb_fn = cb_fn;
    7821                 :         70 :         ctx->cb_arg = cb_arg;
    7822                 :         70 :         ctx->bserrno = 0;
    7823                 :            : 
    7824                 :         70 :         spdk_bs_open_blob(bs, blob_id, bs_set_external_parent_blob_open_cpl, ctx);
    7825                 :            : }
    7826                 :            : /* END spdk_bs_blob_set_external_parent */
    7827                 :            : 
    7828                 :            : /* START spdk_blob_resize */
    7829                 :            : struct spdk_bs_resize_ctx {
    7830                 :            :         spdk_blob_op_complete cb_fn;
    7831                 :            :         void *cb_arg;
    7832                 :            :         struct spdk_blob *blob;
    7833                 :            :         uint64_t sz;
    7834                 :            :         int rc;
    7835                 :            : };
    7836                 :            : 
    7837                 :            : static void
    7838                 :      28580 : bs_resize_unfreeze_cpl(void *cb_arg, int rc)
    7839                 :            : {
    7840                 :      28580 :         struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
    7841                 :            : 
    7842         [ -  + ]:      28580 :         if (rc != 0) {
    7843                 :          0 :                 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc);
    7844                 :            :         }
    7845                 :            : 
    7846         [ +  + ]:      28580 :         if (ctx->rc != 0) {
    7847                 :         17 :                 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc);
    7848                 :         17 :                 rc = ctx->rc;
    7849                 :            :         }
    7850                 :            : 
    7851                 :      28580 :         ctx->blob->locked_operation_in_progress = false;
    7852                 :            : 
    7853                 :      28580 :         ctx->cb_fn(ctx->cb_arg, rc);
    7854                 :      28580 :         free(ctx);
    7855                 :      28580 : }
    7856                 :            : 
    7857                 :            : static void
    7858                 :      28580 : bs_resize_freeze_cpl(void *cb_arg, int rc)
    7859                 :            : {
    7860                 :      28580 :         struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
    7861                 :            : 
    7862         [ -  + ]:      28580 :         if (rc != 0) {
    7863                 :          0 :                 ctx->blob->locked_operation_in_progress = false;
    7864                 :          0 :                 ctx->cb_fn(ctx->cb_arg, rc);
    7865                 :          0 :                 free(ctx);
    7866                 :          0 :                 return;
    7867                 :            :         }
    7868                 :            : 
    7869                 :      28580 :         ctx->rc = blob_resize(ctx->blob, ctx->sz);
    7870                 :            : 
    7871                 :      28580 :         blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx);
    7872                 :            : }
    7873                 :            : 
    7874                 :            : void
    7875                 :      28636 : spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg)
    7876                 :            : {
    7877                 :            :         struct spdk_bs_resize_ctx *ctx;
    7878                 :            : 
    7879                 :      28636 :         blob_verify_md_op(blob);
    7880                 :            : 
    7881   [ -  +  -  + ]:      28636 :         SPDK_DEBUGLOG(blob, "Resizing blob 0x%" PRIx64 " to %" PRIu64 " clusters\n", blob->id, sz);
    7882                 :            : 
    7883   [ +  +  +  + ]:      28636 :         if (blob->md_ro) {
    7884                 :         16 :                 cb_fn(cb_arg, -EPERM);
    7885                 :         16 :                 return;
    7886                 :            :         }
    7887                 :            : 
    7888         [ +  + ]:      28620 :         if (sz == blob->active.num_clusters) {
    7889                 :         40 :                 cb_fn(cb_arg, 0);
    7890                 :         40 :                 return;
    7891                 :            :         }
    7892                 :            : 
    7893   [ -  +  -  + ]:      28580 :         if (blob->locked_operation_in_progress) {
    7894                 :          0 :                 cb_fn(cb_arg, -EBUSY);
    7895                 :          0 :                 return;
    7896                 :            :         }
    7897                 :            : 
    7898                 :      28580 :         ctx = calloc(1, sizeof(*ctx));
    7899         [ -  + ]:      28580 :         if (!ctx) {
    7900                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7901                 :          0 :                 return;
    7902                 :            :         }
    7903                 :            : 
    7904                 :      28580 :         blob->locked_operation_in_progress = true;
    7905                 :      28580 :         ctx->cb_fn = cb_fn;
    7906                 :      28580 :         ctx->cb_arg = cb_arg;
    7907                 :      28580 :         ctx->blob = blob;
    7908                 :      28580 :         ctx->sz = sz;
    7909                 :      28580 :         blob_freeze_io(blob, bs_resize_freeze_cpl, ctx);
    7910                 :            : }
    7911                 :            : 
    7912                 :            : /* END spdk_blob_resize */
    7913                 :            : 
    7914                 :            : 
    7915                 :            : /* START spdk_bs_delete_blob */
    7916                 :            : 
    7917                 :            : static void
    7918                 :       6716 : bs_delete_close_cpl(void *cb_arg, int bserrno)
    7919                 :            : {
    7920                 :       6716 :         spdk_bs_sequence_t *seq = cb_arg;
    7921                 :            : 
    7922                 :       6716 :         bs_sequence_finish(seq, bserrno);
    7923                 :       6716 : }
    7924                 :            : 
    7925                 :            : static void
    7926                 :       6716 : bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    7927                 :            : {
    7928                 :       6716 :         struct spdk_blob *blob = cb_arg;
    7929                 :            : 
    7930         [ -  + ]:       6716 :         if (bserrno != 0) {
    7931                 :            :                 /*
    7932                 :            :                  * We already removed this blob from the blobstore tailq, so
    7933                 :            :                  *  we need to free it here since this is the last reference
    7934                 :            :                  *  to it.
    7935                 :            :                  */
    7936                 :          0 :                 blob_free(blob);
    7937                 :          0 :                 bs_delete_close_cpl(seq, bserrno);
    7938                 :          0 :                 return;
    7939                 :            :         }
    7940                 :            : 
    7941                 :            :         /*
    7942                 :            :          * This will immediately decrement the ref_count and call
    7943                 :            :          *  the completion routine since the metadata state is clean.
    7944                 :            :          *  By calling spdk_blob_close, we reduce the number of call
    7945                 :            :          *  points into code that touches the blob->open_ref count
    7946                 :            :          *  and the blobstore's blob list.
    7947                 :            :          */
    7948                 :       6716 :         spdk_blob_close(blob, bs_delete_close_cpl, seq);
    7949                 :            : }
    7950                 :            : 
    7951                 :            : struct delete_snapshot_ctx {
    7952                 :            :         struct spdk_blob_list *parent_snapshot_entry;
    7953                 :            :         struct spdk_blob *snapshot;
    7954                 :            :         struct spdk_blob_md_page *page;
    7955                 :            :         bool snapshot_md_ro;
    7956                 :            :         struct spdk_blob *clone;
    7957                 :            :         bool clone_md_ro;
    7958                 :            :         spdk_blob_op_with_handle_complete cb_fn;
    7959                 :            :         void *cb_arg;
    7960                 :            :         int bserrno;
    7961                 :            :         uint32_t next_extent_page;
    7962                 :            : };
    7963                 :            : 
    7964                 :            : static void
    7965                 :        448 : delete_blob_cleanup_finish(void *cb_arg, int bserrno)
    7966                 :            : {
    7967                 :        448 :         struct delete_snapshot_ctx *ctx = cb_arg;
    7968                 :            : 
    7969         [ -  + ]:        448 :         if (bserrno != 0) {
    7970                 :          0 :                 SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno);
    7971                 :            :         }
    7972                 :            : 
    7973         [ -  + ]:        448 :         assert(ctx != NULL);
    7974                 :            : 
    7975   [ -  +  -  - ]:        448 :         if (bserrno != 0 && ctx->bserrno == 0) {
    7976                 :          0 :                 ctx->bserrno = bserrno;
    7977                 :            :         }
    7978                 :            : 
    7979                 :        448 :         ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno);
    7980                 :        448 :         spdk_free(ctx->page);
    7981                 :        448 :         free(ctx);
    7982                 :        448 : }
    7983                 :            : 
    7984                 :            : static void
    7985                 :         88 : delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno)
    7986                 :            : {
    7987                 :         88 :         struct delete_snapshot_ctx *ctx = cb_arg;
    7988                 :            : 
    7989         [ -  + ]:         88 :         if (bserrno != 0) {
    7990                 :          0 :                 ctx->bserrno = bserrno;
    7991                 :          0 :                 SPDK_ERRLOG("Clone cleanup error %d\n", bserrno);
    7992                 :            :         }
    7993                 :            : 
    7994         [ +  - ]:         88 :         if (ctx->bserrno != 0) {
    7995         [ -  + ]:         88 :                 assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL);
    7996                 :         88 :                 RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot);
    7997                 :         88 :                 spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id);
    7998                 :            :         }
    7999                 :            : 
    8000                 :         88 :         ctx->snapshot->locked_operation_in_progress = false;
    8001         [ -  + ]:         88 :         ctx->snapshot->md_ro = ctx->snapshot_md_ro;
    8002                 :            : 
    8003                 :         88 :         spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx);
    8004                 :         88 : }
    8005                 :            : 
    8006                 :            : static void
    8007                 :         48 : delete_snapshot_cleanup_clone(void *cb_arg, int bserrno)
    8008                 :            : {
    8009                 :         48 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8010                 :            : 
    8011                 :         48 :         ctx->clone->locked_operation_in_progress = false;
    8012         [ -  + ]:         48 :         ctx->clone->md_ro = ctx->clone_md_ro;
    8013                 :            : 
    8014                 :         48 :         spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
    8015                 :         48 : }
    8016                 :            : 
    8017                 :            : static void
    8018                 :        200 : delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
    8019                 :            : {
    8020                 :        200 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8021                 :            : 
    8022         [ -  + ]:        200 :         if (bserrno) {
    8023                 :          0 :                 ctx->bserrno = bserrno;
    8024                 :          0 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8025                 :          0 :                 return;
    8026                 :            :         }
    8027                 :            : 
    8028                 :        200 :         ctx->clone->locked_operation_in_progress = false;
    8029                 :        200 :         spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx);
    8030                 :            : }
    8031                 :            : 
    8032                 :            : static void
    8033                 :        216 : delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno)
    8034                 :            : {
    8035                 :        216 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8036                 :        216 :         struct spdk_blob_list *parent_snapshot_entry = NULL;
    8037                 :        216 :         struct spdk_blob_list *snapshot_entry = NULL;
    8038                 :        216 :         struct spdk_blob_list *clone_entry = NULL;
    8039                 :        216 :         struct spdk_blob_list *snapshot_clone_entry = NULL;
    8040                 :            : 
    8041         [ +  + ]:        216 :         if (bserrno) {
    8042                 :         16 :                 SPDK_ERRLOG("Failed to sync MD on blob\n");
    8043                 :         16 :                 ctx->bserrno = bserrno;
    8044                 :         16 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8045                 :         16 :                 return;
    8046                 :            :         }
    8047                 :            : 
    8048                 :            :         /* Get snapshot entry for the snapshot we want to remove */
    8049                 :        200 :         snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id);
    8050                 :            : 
    8051         [ -  + ]:        200 :         assert(snapshot_entry != NULL);
    8052                 :            : 
    8053                 :            :         /* Remove clone entry in this snapshot (at this point there can be only one clone) */
    8054                 :        200 :         clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
    8055         [ -  + ]:        200 :         assert(clone_entry != NULL);
    8056         [ -  + ]:        200 :         TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
    8057                 :        200 :         snapshot_entry->clone_count--;
    8058         [ -  + ]:        200 :         assert(TAILQ_EMPTY(&snapshot_entry->clones));
    8059                 :            : 
    8060         [ +  + ]:        200 :         switch (ctx->snapshot->parent_id) {
    8061                 :        167 :         case SPDK_BLOBID_INVALID:
    8062                 :            :         case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
    8063                 :            :                 /* No parent snapshot - just remove clone entry */
    8064                 :        167 :                 free(clone_entry);
    8065                 :        167 :                 break;
    8066                 :         33 :         default:
    8067                 :            :                 /* This snapshot is at the same time a clone of another snapshot - we need to
    8068                 :            :                  * update parent snapshot (remove current clone, add new one inherited from
    8069                 :            :                  * the snapshot that is being removed) */
    8070                 :            : 
    8071                 :            :                 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
    8072                 :            :                  * snapshot that we are removing */
    8073                 :         33 :                 blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry,
    8074                 :            :                                                     &snapshot_clone_entry);
    8075                 :            : 
    8076                 :            :                 /* Switch clone entry in parent snapshot */
    8077                 :         33 :                 TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link);
    8078         [ +  - ]:         33 :                 TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link);
    8079                 :         33 :                 free(snapshot_clone_entry);
    8080                 :            :         }
    8081                 :            : 
    8082                 :            :         /* Restore md_ro flags */
    8083         [ -  + ]:        200 :         ctx->clone->md_ro = ctx->clone_md_ro;
    8084         [ -  + ]:        200 :         ctx->snapshot->md_ro = ctx->snapshot_md_ro;
    8085                 :            : 
    8086                 :        200 :         blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx);
    8087                 :            : }
    8088                 :            : 
    8089                 :            : static void
    8090                 :        232 : delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno)
    8091                 :            : {
    8092                 :        232 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8093                 :            :         uint64_t i;
    8094                 :            : 
    8095                 :        232 :         ctx->snapshot->md_ro = false;
    8096                 :            : 
    8097         [ +  + ]:        232 :         if (bserrno) {
    8098                 :         16 :                 SPDK_ERRLOG("Failed to sync MD on clone\n");
    8099                 :         16 :                 ctx->bserrno = bserrno;
    8100                 :            : 
    8101                 :            :                 /* Restore snapshot to previous state */
    8102                 :         16 :                 bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
    8103         [ -  + ]:         16 :                 if (bserrno != 0) {
    8104                 :          0 :                         delete_snapshot_cleanup_clone(ctx, bserrno);
    8105                 :          0 :                         return;
    8106                 :            :                 }
    8107                 :            : 
    8108                 :         16 :                 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
    8109                 :         16 :                 return;
    8110                 :            :         }
    8111                 :            : 
    8112                 :            :         /* Clear cluster map entries for snapshot */
    8113   [ +  +  +  - ]:       2441 :         for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
    8114         [ +  + ]:       2225 :                 if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) {
    8115         [ +  + ]:       2177 :                         if (ctx->snapshot->active.clusters[i] != 0) {
    8116                 :       1325 :                                 ctx->snapshot->active.num_allocated_clusters--;
    8117                 :            :                         }
    8118                 :       2177 :                         ctx->snapshot->active.clusters[i] = 0;
    8119                 :            :                 }
    8120                 :            :         }
    8121         [ +  + ]:        328 :         for (i = 0; i < ctx->snapshot->active.num_extent_pages &&
    8122         [ +  - ]:        224 :              i < ctx->clone->active.num_extent_pages; i++) {
    8123         [ +  + ]:        112 :                 if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) {
    8124                 :        100 :                         ctx->snapshot->active.extent_pages[i] = 0;
    8125                 :            :                 }
    8126                 :            :         }
    8127                 :            : 
    8128                 :        216 :         blob_set_thin_provision(ctx->snapshot);
    8129                 :        216 :         ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY;
    8130                 :            : 
    8131         [ +  + ]:        216 :         if (ctx->parent_snapshot_entry != NULL) {
    8132                 :         33 :                 ctx->snapshot->back_bs_dev = NULL;
    8133                 :            :         }
    8134                 :            : 
    8135                 :        216 :         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx);
    8136                 :            : }
    8137                 :            : 
    8138                 :            : static void
    8139                 :        232 : delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx)
    8140                 :            : {
    8141                 :            :         int bserrno;
    8142                 :            : 
    8143                 :            :         /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */
    8144                 :        232 :         blob_back_bs_destroy(ctx->clone);
    8145                 :            : 
    8146                 :            :         /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */
    8147         [ +  + ]:        232 :         if (ctx->snapshot->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    8148                 :         36 :                 bserrno = bs_snapshot_copy_xattr(ctx->clone, ctx->snapshot,
    8149                 :            :                                                  BLOB_EXTERNAL_SNAPSHOT_ID);
    8150         [ -  + ]:         36 :                 if (bserrno != 0) {
    8151                 :          0 :                         ctx->bserrno = bserrno;
    8152                 :            : 
    8153                 :            :                         /* Restore snapshot to previous state */
    8154                 :          0 :                         bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
    8155         [ #  # ]:          0 :                         if (bserrno != 0) {
    8156                 :          0 :                                 delete_snapshot_cleanup_clone(ctx, bserrno);
    8157                 :          0 :                                 return;
    8158                 :            :                         }
    8159                 :            : 
    8160                 :          0 :                         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
    8161                 :          0 :                         return;
    8162                 :            :                 }
    8163                 :         36 :                 ctx->clone->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
    8164                 :         36 :                 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
    8165                 :            :                 /* Do not delete the external snapshot along with this snapshot */
    8166                 :         36 :                 ctx->snapshot->back_bs_dev = NULL;
    8167                 :         36 :                 ctx->clone->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    8168         [ +  + ]:        196 :         } else if (ctx->parent_snapshot_entry != NULL) {
    8169                 :            :                 /* ...to parent snapshot */
    8170                 :         33 :                 ctx->clone->parent_id = ctx->parent_snapshot_entry->id;
    8171                 :         33 :                 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
    8172                 :         33 :                 blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id,
    8173                 :            :                                sizeof(spdk_blob_id),
    8174                 :            :                                true);
    8175                 :            :         } else {
    8176                 :            :                 /* ...to blobid invalid and zeroes dev */
    8177                 :        163 :                 ctx->clone->parent_id = SPDK_BLOBID_INVALID;
    8178                 :        163 :                 ctx->clone->back_bs_dev = bs_create_zeroes_dev();
    8179                 :        163 :                 blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true);
    8180                 :            :         }
    8181                 :            : 
    8182                 :        232 :         spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx);
    8183                 :            : }
    8184                 :            : 
    8185                 :            : static void
    8186                 :        244 : delete_snapshot_update_extent_pages(void *cb_arg, int bserrno)
    8187                 :            : {
    8188                 :        244 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8189                 :            :         uint32_t *extent_page;
    8190                 :            :         uint64_t i;
    8191                 :            : 
    8192         [ +  + ]:        352 :         for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages &&
    8193         [ +  - ]:        228 :              i < ctx->clone->active.num_extent_pages; i++) {
    8194         [ +  + ]:        120 :                 if (ctx->snapshot->active.extent_pages[i] == 0) {
    8195                 :            :                         /* No extent page to use from snapshot */
    8196                 :         36 :                         continue;
    8197                 :            :                 }
    8198                 :            : 
    8199                 :         84 :                 extent_page = &ctx->clone->active.extent_pages[i];
    8200         [ +  + ]:         84 :                 if (*extent_page == 0) {
    8201                 :            :                         /* Copy extent page from snapshot when clone did not have a matching one */
    8202                 :         72 :                         *extent_page = ctx->snapshot->active.extent_pages[i];
    8203                 :         72 :                         continue;
    8204                 :            :                 }
    8205                 :            : 
    8206                 :            :                 /* Clone and snapshot both contain partially filled matching extent pages.
    8207                 :            :                  * Update the clone extent page in place with cluster map containing the mix of both. */
    8208                 :         12 :                 ctx->next_extent_page = i + 1;
    8209         [ -  + ]:         12 :                 memset(ctx->page, 0, SPDK_BS_PAGE_SIZE);
    8210                 :            : 
    8211                 :         12 :                 blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, ctx->page,
    8212                 :            :                                        delete_snapshot_update_extent_pages, ctx);
    8213                 :         12 :                 return;
    8214                 :            :         }
    8215                 :        232 :         delete_snapshot_update_extent_pages_cpl(ctx);
    8216                 :            : }
    8217                 :            : 
    8218                 :            : static void
    8219                 :        248 : delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno)
    8220                 :            : {
    8221                 :        248 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8222                 :            :         uint64_t i;
    8223                 :            : 
    8224                 :            :         /* Temporarily override md_ro flag for clone for MD modification */
    8225         [ -  + ]:        248 :         ctx->clone_md_ro = ctx->clone->md_ro;
    8226                 :        248 :         ctx->clone->md_ro = false;
    8227                 :            : 
    8228         [ +  + ]:        248 :         if (bserrno) {
    8229                 :         16 :                 SPDK_ERRLOG("Failed to sync MD with xattr on blob\n");
    8230                 :         16 :                 ctx->bserrno = bserrno;
    8231                 :         16 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8232                 :         16 :                 return;
    8233                 :            :         }
    8234                 :            : 
    8235                 :            :         /* Copy snapshot map to clone map (only unallocated clusters in clone) */
    8236   [ +  +  +  - ]:       2617 :         for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
    8237         [ +  + ]:       2385 :                 if (ctx->clone->active.clusters[i] == 0) {
    8238                 :       2337 :                         ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i];
    8239         [ +  + ]:       2337 :                         if (ctx->clone->active.clusters[i] != 0) {
    8240                 :       1485 :                                 ctx->clone->active.num_allocated_clusters++;
    8241                 :            :                         }
    8242                 :            :                 }
    8243                 :            :         }
    8244                 :        232 :         ctx->next_extent_page = 0;
    8245                 :        232 :         delete_snapshot_update_extent_pages(ctx, 0);
    8246                 :            : }
    8247                 :            : 
    8248                 :            : static void
    8249                 :         36 : delete_snapshot_esnap_channels_destroyed_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
    8250                 :            : {
    8251                 :         36 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8252                 :            : 
    8253         [ -  + ]:         36 :         if (bserrno != 0) {
    8254                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to destroy esnap channels: %d\n",
    8255                 :            :                             blob->id, bserrno);
    8256                 :            :                 /* That error should not stop us from syncing metadata. */
    8257                 :            :         }
    8258                 :            : 
    8259                 :         36 :         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
    8260                 :         36 : }
    8261                 :            : 
    8262                 :            : static void
    8263                 :        248 : delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno)
    8264                 :            : {
    8265                 :        248 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8266                 :            : 
    8267         [ -  + ]:        248 :         if (bserrno) {
    8268                 :          0 :                 SPDK_ERRLOG("Failed to freeze I/O on clone\n");
    8269                 :          0 :                 ctx->bserrno = bserrno;
    8270                 :          0 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8271                 :          0 :                 return;
    8272                 :            :         }
    8273                 :            : 
    8274                 :            :         /* Temporarily override md_ro flag for snapshot for MD modification */
    8275         [ -  + ]:        248 :         ctx->snapshot_md_ro = ctx->snapshot->md_ro;
    8276                 :        248 :         ctx->snapshot->md_ro = false;
    8277                 :            : 
    8278                 :            :         /* Mark blob as pending for removal for power failure safety, use clone id for recovery */
    8279                 :        248 :         ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id,
    8280                 :            :                                       sizeof(spdk_blob_id), true);
    8281         [ -  + ]:        248 :         if (ctx->bserrno != 0) {
    8282                 :          0 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8283                 :          0 :                 return;
    8284                 :            :         }
    8285                 :            : 
    8286         [ +  + ]:        248 :         if (blob_is_esnap_clone(ctx->snapshot)) {
    8287                 :         36 :                 blob_esnap_destroy_bs_dev_channels(ctx->snapshot, false,
    8288                 :            :                                                    delete_snapshot_esnap_channels_destroyed_cb,
    8289                 :            :                                                    ctx);
    8290                 :         36 :                 return;
    8291                 :            :         }
    8292                 :            : 
    8293                 :        212 :         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
    8294                 :            : }
    8295                 :            : 
    8296                 :            : static void
    8297                 :        288 : delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno)
    8298                 :            : {
    8299                 :        288 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8300                 :            : 
    8301         [ +  + ]:        288 :         if (bserrno) {
    8302                 :         40 :                 SPDK_ERRLOG("Failed to open clone\n");
    8303                 :         40 :                 ctx->bserrno = bserrno;
    8304                 :         40 :                 delete_snapshot_cleanup_snapshot(ctx, 0);
    8305                 :         40 :                 return;
    8306                 :            :         }
    8307                 :            : 
    8308                 :        248 :         ctx->clone = clone;
    8309                 :            : 
    8310   [ -  +  -  + ]:        248 :         if (clone->locked_operation_in_progress) {
    8311   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n");
    8312                 :          0 :                 ctx->bserrno = -EBUSY;
    8313                 :          0 :                 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
    8314                 :          0 :                 return;
    8315                 :            :         }
    8316                 :            : 
    8317                 :        248 :         clone->locked_operation_in_progress = true;
    8318                 :            : 
    8319                 :        248 :         blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx);
    8320                 :            : }
    8321                 :            : 
    8322                 :            : static void
    8323                 :        288 : update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx)
    8324                 :            : {
    8325                 :        288 :         struct spdk_blob_list *snapshot_entry = NULL;
    8326                 :        288 :         struct spdk_blob_list *clone_entry = NULL;
    8327                 :        288 :         struct spdk_blob_list *snapshot_clone_entry = NULL;
    8328                 :            : 
    8329                 :            :         /* Get snapshot entry for the snapshot we want to remove */
    8330                 :        288 :         snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id);
    8331                 :            : 
    8332         [ -  + ]:        288 :         assert(snapshot_entry != NULL);
    8333                 :            : 
    8334                 :            :         /* Get clone of the snapshot (at this point there can be only one clone) */
    8335                 :        288 :         clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
    8336         [ -  + ]:        288 :         assert(snapshot_entry->clone_count == 1);
    8337         [ -  + ]:        288 :         assert(clone_entry != NULL);
    8338                 :            : 
    8339                 :            :         /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
    8340                 :            :          * snapshot that we are removing */
    8341                 :        288 :         blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry,
    8342                 :            :                                             &snapshot_clone_entry);
    8343                 :            : 
    8344                 :        288 :         spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx);
    8345                 :        288 : }
    8346                 :            : 
    8347                 :            : static void
    8348                 :       6964 : bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno)
    8349                 :            : {
    8350                 :       6964 :         spdk_bs_sequence_t *seq = cb_arg;
    8351                 :       6964 :         struct spdk_blob_list *snapshot_entry = NULL;
    8352                 :            :         uint32_t page_num;
    8353                 :            : 
    8354         [ +  + ]:       6964 :         if (bserrno) {
    8355                 :        248 :                 SPDK_ERRLOG("Failed to remove blob\n");
    8356                 :        248 :                 bs_sequence_finish(seq, bserrno);
    8357                 :        248 :                 return;
    8358                 :            :         }
    8359                 :            : 
    8360                 :            :         /* Remove snapshot from the list */
    8361                 :       6716 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
    8362         [ +  + ]:       6716 :         if (snapshot_entry != NULL) {
    8363         [ +  + ]:        596 :                 TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link);
    8364                 :        596 :                 free(snapshot_entry);
    8365                 :            :         }
    8366                 :            : 
    8367                 :       6716 :         page_num = bs_blobid_to_page(blob->id);
    8368                 :       6716 :         spdk_bit_array_clear(blob->bs->used_blobids, page_num);
    8369                 :       6716 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    8370                 :       6716 :         blob->active.num_pages = 0;
    8371                 :       6716 :         blob_resize(blob, 0);
    8372                 :            : 
    8373                 :       6716 :         blob_persist(seq, blob, bs_delete_persist_cpl, blob);
    8374                 :            : }
    8375                 :            : 
    8376                 :            : static int
    8377                 :       6964 : bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone)
    8378                 :            : {
    8379                 :       6964 :         struct spdk_blob_list *snapshot_entry = NULL;
    8380                 :       6964 :         struct spdk_blob_list *clone_entry = NULL;
    8381                 :       6964 :         struct spdk_blob *clone = NULL;
    8382                 :       6964 :         bool has_one_clone = false;
    8383                 :            : 
    8384                 :            :         /* Check if this is a snapshot with clones */
    8385                 :       6964 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
    8386         [ +  + ]:       6964 :         if (snapshot_entry != NULL) {
    8387         [ +  + ]:        796 :                 if (snapshot_entry->clone_count > 1) {
    8388                 :         96 :                         SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n");
    8389                 :         96 :                         return -EBUSY;
    8390         [ +  + ]:        700 :                 } else if (snapshot_entry->clone_count == 1) {
    8391                 :        288 :                         has_one_clone = true;
    8392                 :            :                 }
    8393                 :            :         }
    8394                 :            : 
    8395                 :            :         /* Check if someone has this blob open (besides this delete context):
    8396                 :            :          * - open_ref = 1 - only this context opened blob, so it is ok to remove it
    8397                 :            :          * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot
    8398                 :            :          *      and that is ok, because we will update it accordingly */
    8399   [ +  +  +  + ]:       6868 :         if (blob->open_ref <= 2 && has_one_clone) {
    8400                 :        288 :                 clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
    8401         [ -  + ]:        288 :                 assert(clone_entry != NULL);
    8402                 :        288 :                 clone = blob_lookup(blob->bs, clone_entry->id);
    8403                 :            : 
    8404   [ +  +  -  + ]:        288 :                 if (blob->open_ref == 2 && clone == NULL) {
    8405                 :            :                         /* Clone is closed and someone else opened this blob */
    8406                 :          0 :                         SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
    8407                 :          0 :                         return -EBUSY;
    8408                 :            :                 }
    8409                 :            : 
    8410                 :        288 :                 *update_clone = true;
    8411                 :        288 :                 return 0;
    8412                 :            :         }
    8413                 :            : 
    8414         [ +  + ]:       6580 :         if (blob->open_ref > 1) {
    8415                 :         64 :                 SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
    8416                 :         64 :                 return -EBUSY;
    8417                 :            :         }
    8418                 :            : 
    8419         [ -  + ]:       6516 :         assert(has_one_clone == false);
    8420                 :       6516 :         *update_clone = false;
    8421                 :       6516 :         return 0;
    8422                 :            : }
    8423                 :            : 
    8424                 :            : static void
    8425                 :          0 : bs_delete_enomem_close_cpl(void *cb_arg, int bserrno)
    8426                 :            : {
    8427                 :          0 :         spdk_bs_sequence_t *seq = cb_arg;
    8428                 :            : 
    8429                 :          0 :         bs_sequence_finish(seq, -ENOMEM);
    8430                 :          0 : }
    8431                 :            : 
    8432                 :            : static void
    8433                 :       7004 : bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
    8434                 :            : {
    8435                 :       7004 :         spdk_bs_sequence_t *seq = cb_arg;
    8436                 :            :         struct delete_snapshot_ctx *ctx;
    8437                 :       7004 :         bool update_clone = false;
    8438                 :            : 
    8439         [ +  + ]:       7004 :         if (bserrno != 0) {
    8440                 :         40 :                 bs_sequence_finish(seq, bserrno);
    8441                 :         40 :                 return;
    8442                 :            :         }
    8443                 :            : 
    8444                 :       6964 :         blob_verify_md_op(blob);
    8445                 :            : 
    8446                 :       6964 :         ctx = calloc(1, sizeof(*ctx));
    8447         [ -  + ]:       6964 :         if (ctx == NULL) {
    8448                 :          0 :                 spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq);
    8449                 :          0 :                 return;
    8450                 :            :         }
    8451                 :            : 
    8452                 :       6964 :         ctx->snapshot = blob;
    8453                 :       6964 :         ctx->cb_fn = bs_delete_blob_finish;
    8454                 :       6964 :         ctx->cb_arg = seq;
    8455                 :            : 
    8456                 :            :         /* Check if blob can be removed and if it is a snapshot with clone on top of it */
    8457                 :       6964 :         ctx->bserrno = bs_is_blob_deletable(blob, &update_clone);
    8458         [ +  + ]:       6964 :         if (ctx->bserrno) {
    8459                 :        160 :                 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
    8460                 :        160 :                 return;
    8461                 :            :         }
    8462                 :            : 
    8463   [ -  +  -  + ]:       6804 :         if (blob->locked_operation_in_progress) {
    8464   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n");
    8465                 :          0 :                 ctx->bserrno = -EBUSY;
    8466                 :          0 :                 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
    8467                 :          0 :                 return;
    8468                 :            :         }
    8469                 :            : 
    8470                 :       6804 :         blob->locked_operation_in_progress = true;
    8471                 :            : 
    8472                 :            :         /*
    8473                 :            :          * Remove the blob from the blob_store list now, to ensure it does not
    8474                 :            :          *  get returned after this point by blob_lookup().
    8475                 :            :          */
    8476                 :       6804 :         spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
    8477                 :       6804 :         RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
    8478                 :            : 
    8479   [ +  +  +  + ]:       6804 :         if (update_clone) {
    8480                 :        288 :                 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    8481         [ -  + ]:        288 :                 if (!ctx->page) {
    8482                 :          0 :                         ctx->bserrno = -ENOMEM;
    8483                 :          0 :                         spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
    8484                 :          0 :                         return;
    8485                 :            :                 }
    8486                 :            :                 /* This blob is a snapshot with active clone - update clone first */
    8487                 :        288 :                 update_clone_on_snapshot_deletion(blob, ctx);
    8488                 :            :         } else {
    8489                 :            :                 /* This blob does not have any clones - just remove it */
    8490                 :       6516 :                 bs_blob_list_remove(blob);
    8491                 :       6516 :                 bs_delete_blob_finish(seq, blob, 0);
    8492                 :       6516 :                 free(ctx);
    8493                 :            :         }
    8494                 :            : }
    8495                 :            : 
    8496                 :            : void
    8497                 :       7004 : spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
    8498                 :            :                     spdk_blob_op_complete cb_fn, void *cb_arg)
    8499                 :            : {
    8500                 :       6893 :         struct spdk_bs_cpl      cpl;
    8501                 :            :         spdk_bs_sequence_t      *seq;
    8502                 :            : 
    8503   [ -  +  -  + ]:       7004 :         SPDK_DEBUGLOG(blob, "Deleting blob 0x%" PRIx64 "\n", blobid);
    8504                 :            : 
    8505         [ -  + ]:       7004 :         assert(spdk_get_thread() == bs->md_thread);
    8506                 :            : 
    8507                 :       7004 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    8508                 :       7004 :         cpl.u.blob_basic.cb_fn = cb_fn;
    8509                 :       7004 :         cpl.u.blob_basic.cb_arg = cb_arg;
    8510                 :            : 
    8511                 :       7004 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    8512         [ -  + ]:       7004 :         if (!seq) {
    8513                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8514                 :          0 :                 return;
    8515                 :            :         }
    8516                 :            : 
    8517                 :       7004 :         spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq);
    8518                 :            : }
    8519                 :            : 
    8520                 :            : /* END spdk_bs_delete_blob */
    8521                 :            : 
    8522                 :            : /* START spdk_bs_open_blob */
    8523                 :            : 
    8524                 :            : static void
    8525                 :      20383 : bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8526                 :            : {
    8527                 :      20383 :         struct spdk_blob *blob = cb_arg;
    8528                 :            :         struct spdk_blob *existing;
    8529                 :            : 
    8530         [ +  + ]:      20383 :         if (bserrno != 0) {
    8531                 :        256 :                 blob_free(blob);
    8532                 :        256 :                 seq->cpl.u.blob_handle.blob = NULL;
    8533                 :        256 :                 bs_sequence_finish(seq, bserrno);
    8534                 :        256 :                 return;
    8535                 :            :         }
    8536                 :            : 
    8537                 :      20127 :         existing = blob_lookup(blob->bs, blob->id);
    8538         [ +  + ]:      20127 :         if (existing) {
    8539                 :         31 :                 blob_free(blob);
    8540                 :         31 :                 existing->open_ref++;
    8541                 :         31 :                 seq->cpl.u.blob_handle.blob = existing;
    8542                 :         31 :                 bs_sequence_finish(seq, 0);
    8543                 :         31 :                 return;
    8544                 :            :         }
    8545                 :            : 
    8546                 :      20096 :         blob->open_ref++;
    8547                 :            : 
    8548                 :      20096 :         spdk_bit_array_set(blob->bs->open_blobids, blob->id);
    8549                 :      20096 :         RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob);
    8550                 :            : 
    8551                 :      20096 :         bs_sequence_finish(seq, bserrno);
    8552                 :            : }
    8553                 :            : 
    8554                 :            : static inline void
    8555                 :        381 : blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst)
    8556                 :            : {
    8557                 :            : #define FIELD_OK(field) \
    8558                 :            :         offsetof(struct spdk_blob_open_opts, field) + sizeof(src->field) <= src->opts_size
    8559                 :            : 
    8560                 :            : #define SET_FIELD(field) \
    8561                 :            :         if (FIELD_OK(field)) { \
    8562                 :            :                 dst->field = src->field; \
    8563                 :            :         } \
    8564                 :            : 
    8565         [ +  - ]:        381 :         SET_FIELD(clear_method);
    8566         [ +  - ]:        381 :         SET_FIELD(esnap_ctx);
    8567                 :            : 
    8568                 :        381 :         dst->opts_size = src->opts_size;
    8569                 :            : 
    8570                 :            :         /* You should not remove this statement, but need to update the assert statement
    8571                 :            :          * if you add a new field, and also add a corresponding SET_FIELD statement */
    8572                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 24, "Incorrect size");
    8573                 :            : 
    8574                 :            : #undef FIELD_OK
    8575                 :            : #undef SET_FIELD
    8576                 :        381 : }
    8577                 :            : 
    8578                 :            : static void
    8579                 :      23801 : bs_open_blob(struct spdk_blob_store *bs,
    8580                 :            :              spdk_blob_id blobid,
    8581                 :            :              struct spdk_blob_open_opts *opts,
    8582                 :            :              spdk_blob_op_with_handle_complete cb_fn,
    8583                 :            :              void *cb_arg)
    8584                 :            : {
    8585                 :            :         struct spdk_blob                *blob;
    8586                 :      23418 :         struct spdk_bs_cpl              cpl;
    8587                 :      23418 :         struct spdk_blob_open_opts      opts_local;
    8588                 :            :         spdk_bs_sequence_t              *seq;
    8589                 :            :         uint32_t                        page_num;
    8590                 :            : 
    8591   [ -  +  -  + ]:      23801 :         SPDK_DEBUGLOG(blob, "Opening blob 0x%" PRIx64 "\n", blobid);
    8592         [ -  + ]:      23801 :         assert(spdk_get_thread() == bs->md_thread);
    8593                 :            : 
    8594                 :      23801 :         page_num = bs_blobid_to_page(blobid);
    8595         [ +  + ]:      23801 :         if (spdk_bit_array_get(bs->used_blobids, page_num) == false) {
    8596                 :            :                 /* Invalid blobid */
    8597                 :        193 :                 cb_fn(cb_arg, NULL, -ENOENT);
    8598                 :        228 :                 return;
    8599                 :            :         }
    8600                 :            : 
    8601                 :      23608 :         blob = blob_lookup(bs, blobid);
    8602         [ +  + ]:      23608 :         if (blob) {
    8603                 :       3225 :                 blob->open_ref++;
    8604                 :       3225 :                 cb_fn(cb_arg, blob, 0);
    8605                 :       3225 :                 return;
    8606                 :            :         }
    8607                 :            : 
    8608                 :      20383 :         blob = blob_alloc(bs, blobid);
    8609         [ -  + ]:      20383 :         if (!blob) {
    8610                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    8611                 :          0 :                 return;
    8612                 :            :         }
    8613                 :            : 
    8614                 :      20383 :         spdk_blob_open_opts_init(&opts_local, sizeof(opts_local));
    8615         [ +  + ]:      20383 :         if (opts) {
    8616                 :        381 :                 blob_open_opts_copy(opts, &opts_local);
    8617                 :            :         }
    8618                 :            : 
    8619                 :      20383 :         blob->clear_method = opts_local.clear_method;
    8620                 :            : 
    8621                 :      20383 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
    8622                 :      20383 :         cpl.u.blob_handle.cb_fn = cb_fn;
    8623                 :      20383 :         cpl.u.blob_handle.cb_arg = cb_arg;
    8624                 :      20383 :         cpl.u.blob_handle.blob = blob;
    8625                 :      20383 :         cpl.u.blob_handle.esnap_ctx = opts_local.esnap_ctx;
    8626                 :            : 
    8627                 :      20383 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    8628         [ -  + ]:      20383 :         if (!seq) {
    8629                 :          0 :                 blob_free(blob);
    8630                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    8631                 :          0 :                 return;
    8632                 :            :         }
    8633                 :            : 
    8634                 :      20383 :         blob_load(seq, blob, bs_open_blob_cpl, blob);
    8635                 :            : }
    8636                 :            : 
    8637                 :            : void
    8638                 :      23383 : spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
    8639                 :            :                   spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    8640                 :            : {
    8641                 :      23383 :         bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg);
    8642                 :      23383 : }
    8643                 :            : 
    8644                 :            : void
    8645                 :        418 : spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid,
    8646                 :            :                       struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    8647                 :            : {
    8648                 :        418 :         bs_open_blob(bs, blobid, opts, cb_fn, cb_arg);
    8649                 :        418 : }
    8650                 :            : 
    8651                 :            : /* END spdk_bs_open_blob */
    8652                 :            : 
    8653                 :            : /* START spdk_blob_set_read_only */
    8654                 :            : int
    8655                 :        969 : spdk_blob_set_read_only(struct spdk_blob *blob)
    8656                 :            : {
    8657                 :        969 :         blob_verify_md_op(blob);
    8658                 :            : 
    8659                 :        969 :         blob->data_ro_flags |= SPDK_BLOB_READ_ONLY;
    8660                 :            : 
    8661                 :        969 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    8662                 :        969 :         return 0;
    8663                 :            : }
    8664                 :            : /* END spdk_blob_set_read_only */
    8665                 :            : 
    8666                 :            : /* START spdk_blob_sync_md */
    8667                 :            : 
    8668                 :            : static void
    8669                 :      44661 : blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8670                 :            : {
    8671                 :      44661 :         struct spdk_blob *blob = cb_arg;
    8672                 :            : 
    8673   [ +  -  +  + ]:      44661 :         if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
    8674                 :       1658 :                 blob->data_ro = true;
    8675                 :       1658 :                 blob->md_ro = true;
    8676                 :            :         }
    8677                 :            : 
    8678                 :      44661 :         bs_sequence_finish(seq, bserrno);
    8679                 :      44661 : }
    8680                 :            : 
    8681                 :            : static void
    8682                 :      44661 : blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
    8683                 :            : {
    8684                 :      44582 :         struct spdk_bs_cpl      cpl;
    8685                 :            :         spdk_bs_sequence_t      *seq;
    8686                 :            : 
    8687                 :      44661 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    8688                 :      44661 :         cpl.u.blob_basic.cb_fn = cb_fn;
    8689                 :      44661 :         cpl.u.blob_basic.cb_arg = cb_arg;
    8690                 :            : 
    8691                 :      44661 :         seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
    8692         [ -  + ]:      44661 :         if (!seq) {
    8693                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8694                 :          0 :                 return;
    8695                 :            :         }
    8696                 :            : 
    8697                 :      44661 :         blob_persist(seq, blob, blob_sync_md_cpl, blob);
    8698                 :            : }
    8699                 :            : 
    8700                 :            : void
    8701                 :      42558 : spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
    8702                 :            : {
    8703                 :      42558 :         blob_verify_md_op(blob);
    8704                 :            : 
    8705   [ -  +  -  + ]:      42558 :         SPDK_DEBUGLOG(blob, "Syncing blob 0x%" PRIx64 "\n", blob->id);
    8706                 :            : 
    8707   [ +  +  +  + ]:      42558 :         if (blob->md_ro) {
    8708         [ -  + ]:         16 :                 assert(blob->state == SPDK_BLOB_STATE_CLEAN);
    8709                 :         16 :                 cb_fn(cb_arg, 0);
    8710                 :         16 :                 return;
    8711                 :            :         }
    8712                 :            : 
    8713                 :      42542 :         blob_sync_md(blob, cb_fn, cb_arg);
    8714                 :            : }
    8715                 :            : 
    8716                 :            : /* END spdk_blob_sync_md */
    8717                 :            : 
    8718                 :            : struct spdk_blob_cluster_op_ctx {
    8719                 :            :         struct spdk_thread      *thread;
    8720                 :            :         struct spdk_blob        *blob;
    8721                 :            :         uint32_t                cluster_num;    /* cluster index in blob */
    8722                 :            :         uint32_t                cluster;        /* cluster on disk */
    8723                 :            :         uint32_t                extent_page;    /* extent page on disk */
    8724                 :            :         struct spdk_blob_md_page *page; /* preallocated extent page */
    8725                 :            :         int                     rc;
    8726                 :            :         spdk_blob_op_complete   cb_fn;
    8727                 :            :         void                    *cb_arg;
    8728                 :            : };
    8729                 :            : 
    8730                 :            : static void
    8731                 :       5282 : blob_op_cluster_msg_cpl(void *arg)
    8732                 :            : {
    8733                 :       5282 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8734                 :            : 
    8735                 :       5282 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
    8736                 :       5282 :         free(ctx);
    8737                 :       5282 : }
    8738                 :            : 
    8739                 :            : static void
    8740                 :       5156 : blob_op_cluster_msg_cb(void *arg, int bserrno)
    8741                 :            : {
    8742                 :       5156 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8743                 :            : 
    8744                 :       5156 :         ctx->rc = bserrno;
    8745                 :       5156 :         spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
    8746                 :       5156 : }
    8747                 :            : 
    8748                 :            : static void
    8749                 :        391 : blob_insert_new_ep_cb(void *arg, int bserrno)
    8750                 :            : {
    8751                 :        391 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8752                 :            :         uint32_t *extent_page;
    8753                 :            : 
    8754                 :        391 :         extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
    8755                 :        391 :         *extent_page = ctx->extent_page;
    8756                 :        391 :         ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8757                 :        391 :         blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
    8758                 :        391 : }
    8759                 :            : 
    8760                 :            : struct spdk_blob_write_extent_page_ctx {
    8761                 :            :         struct spdk_blob_store          *bs;
    8762                 :            : 
    8763                 :            :         uint32_t                        extent;
    8764                 :            :         struct spdk_blob_md_page        *page;
    8765                 :            : };
    8766                 :            : 
    8767                 :            : static void
    8768                 :        104 : blob_free_cluster_msg_cb(void *arg, int bserrno)
    8769                 :            : {
    8770                 :        104 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8771                 :            : 
    8772                 :        104 :         spdk_spin_lock(&ctx->blob->bs->used_lock);
    8773                 :        104 :         bs_release_cluster(ctx->blob->bs, ctx->cluster);
    8774                 :        104 :         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8775                 :            : 
    8776                 :        104 :         ctx->rc = bserrno;
    8777                 :        104 :         spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
    8778                 :        104 : }
    8779                 :            : 
    8780                 :            : static void
    8781                 :        104 : blob_free_cluster_update_ep_cb(void *arg, int bserrno)
    8782                 :            : {
    8783                 :        104 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8784                 :            : 
    8785   [ +  -  +  +  :        104 :         if (bserrno != 0 || ctx->blob->bs->clean == 0) {
                   +  - ]
    8786                 :        104 :                 blob_free_cluster_msg_cb(ctx, bserrno);
    8787                 :        104 :                 return;
    8788                 :            :         }
    8789                 :            : 
    8790                 :          0 :         ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8791                 :          0 :         blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
    8792                 :            : }
    8793                 :            : 
    8794                 :            : static void
    8795                 :          0 : blob_free_cluster_free_ep_cb(void *arg, int bserrno)
    8796                 :            : {
    8797                 :          0 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8798                 :            : 
    8799                 :          0 :         spdk_spin_lock(&ctx->blob->bs->used_lock);
    8800         [ #  # ]:          0 :         assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8801                 :          0 :         bs_release_md_page(ctx->blob->bs, ctx->extent_page);
    8802                 :          0 :         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8803                 :          0 :         ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8804                 :          0 :         blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
    8805                 :          0 : }
    8806                 :            : 
    8807                 :            : static void
    8808                 :       3512 : blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8809                 :            : {
    8810                 :       3512 :         struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
    8811                 :            : 
    8812                 :       3512 :         free(ctx);
    8813                 :       3512 :         bs_sequence_finish(seq, bserrno);
    8814                 :       3512 : }
    8815                 :            : 
    8816                 :            : static void
    8817                 :       3512 : blob_write_extent_page_ready(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8818                 :            : {
    8819                 :       3512 :         struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
    8820                 :            : 
    8821         [ -  + ]:       3512 :         if (bserrno != 0) {
    8822                 :          0 :                 blob_persist_extent_page_cpl(seq, ctx, bserrno);
    8823                 :          0 :                 return;
    8824                 :            :         }
    8825                 :       3512 :         bs_sequence_write_dev(seq, ctx->page, bs_md_page_to_lba(ctx->bs, ctx->extent),
    8826                 :       3512 :                               bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
    8827                 :            :                               blob_persist_extent_page_cpl, ctx);
    8828                 :            : }
    8829                 :            : 
    8830                 :            : static void
    8831                 :       3512 : blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
    8832                 :            :                        struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
    8833                 :            : {
    8834                 :            :         struct spdk_blob_write_extent_page_ctx  *ctx;
    8835                 :            :         spdk_bs_sequence_t                      *seq;
    8836                 :       3394 :         struct spdk_bs_cpl                      cpl;
    8837                 :            : 
    8838                 :       3512 :         ctx = calloc(1, sizeof(*ctx));
    8839         [ -  + ]:       3512 :         if (!ctx) {
    8840                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8841                 :          0 :                 return;
    8842                 :            :         }
    8843                 :       3512 :         ctx->bs = blob->bs;
    8844                 :       3512 :         ctx->extent = extent;
    8845                 :       3512 :         ctx->page = page;
    8846                 :            : 
    8847                 :       3512 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    8848                 :       3512 :         cpl.u.blob_basic.cb_fn = cb_fn;
    8849                 :       3512 :         cpl.u.blob_basic.cb_arg = cb_arg;
    8850                 :            : 
    8851                 :       3512 :         seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
    8852         [ -  + ]:       3512 :         if (!seq) {
    8853                 :          0 :                 free(ctx);
    8854                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8855                 :          0 :                 return;
    8856                 :            :         }
    8857                 :            : 
    8858         [ -  + ]:       3512 :         assert(page);
    8859                 :       3512 :         page->next = SPDK_INVALID_MD_PAGE;
    8860                 :       3512 :         page->id = blob->id;
    8861                 :       3512 :         page->sequence_num = 0;
    8862                 :            : 
    8863                 :       3512 :         blob_serialize_extent_page(blob, cluster_num, page);
    8864                 :            : 
    8865                 :       3512 :         page->crc = blob_md_page_calc_crc(page);
    8866                 :            : 
    8867         [ -  + ]:       3512 :         assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true);
    8868                 :            : 
    8869                 :       3512 :         bs_mark_dirty(seq, blob->bs, blob_write_extent_page_ready, ctx);
    8870                 :            : }
    8871                 :            : 
    8872                 :            : static void
    8873                 :       5042 : blob_insert_cluster_msg(void *arg)
    8874                 :            : {
    8875                 :       5042 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8876                 :            :         uint32_t *extent_page;
    8877                 :            : 
    8878                 :       5042 :         ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster);
    8879         [ +  + ]:       5042 :         if (ctx->rc != 0) {
    8880                 :         22 :                 spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
    8881                 :         22 :                 return;
    8882                 :            :         }
    8883                 :            : 
    8884   [ +  +  +  + ]:       5020 :         if (ctx->blob->use_extent_table == false) {
    8885                 :            :                 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
    8886                 :       1624 :                 ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8887                 :       1624 :                 blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
    8888                 :       1624 :                 return;
    8889                 :            :         }
    8890                 :            : 
    8891                 :       3396 :         extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
    8892         [ +  + ]:       3396 :         if (*extent_page == 0) {
    8893                 :            :                 /* Extent page requires allocation.
    8894                 :            :                  * It was already claimed in the used_md_pages map and placed in ctx. */
    8895         [ -  + ]:        391 :                 assert(ctx->extent_page != 0);
    8896         [ -  + ]:        391 :                 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8897                 :        391 :                 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
    8898                 :            :                                        blob_insert_new_ep_cb, ctx);
    8899                 :            :         } else {
    8900                 :            :                 /* It is possible for original thread to allocate extent page for
    8901                 :            :                  * different cluster in the same extent page. In such case proceed with
    8902                 :            :                  * updating the existing extent page, but release the additional one. */
    8903         [ +  + ]:       3005 :                 if (ctx->extent_page != 0) {
    8904                 :          4 :                         spdk_spin_lock(&ctx->blob->bs->used_lock);
    8905         [ -  + ]:          4 :                         assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8906                 :          4 :                         bs_release_md_page(ctx->blob->bs, ctx->extent_page);
    8907                 :          4 :                         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8908                 :          4 :                         ctx->extent_page = 0;
    8909                 :            :                 }
    8910                 :            :                 /* Extent page already allocated.
    8911                 :            :                  * Every cluster allocation, requires just an update of single extent page. */
    8912                 :       3005 :                 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
    8913                 :            :                                        blob_op_cluster_msg_cb, ctx);
    8914                 :            :         }
    8915                 :            : }
    8916                 :            : 
    8917                 :            : static void
    8918                 :       5042 : blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
    8919                 :            :                                  uint64_t cluster, uint32_t extent_page, struct spdk_blob_md_page *page,
    8920                 :            :                                  spdk_blob_op_complete cb_fn, void *cb_arg)
    8921                 :            : {
    8922                 :            :         struct spdk_blob_cluster_op_ctx *ctx;
    8923                 :            : 
    8924                 :       5042 :         ctx = calloc(1, sizeof(*ctx));
    8925         [ -  + ]:       5042 :         if (ctx == NULL) {
    8926                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8927                 :          0 :                 return;
    8928                 :            :         }
    8929                 :            : 
    8930                 :       5042 :         ctx->thread = spdk_get_thread();
    8931                 :       5042 :         ctx->blob = blob;
    8932                 :       5042 :         ctx->cluster_num = cluster_num;
    8933                 :       5042 :         ctx->cluster = cluster;
    8934                 :       5042 :         ctx->extent_page = extent_page;
    8935                 :       5042 :         ctx->page = page;
    8936                 :       5042 :         ctx->cb_fn = cb_fn;
    8937                 :       5042 :         ctx->cb_arg = cb_arg;
    8938                 :            : 
    8939                 :       5042 :         spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx);
    8940                 :            : }
    8941                 :            : 
    8942                 :            : static void
    8943                 :        240 : blob_free_cluster_msg(void *arg)
    8944                 :            : {
    8945                 :        240 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8946                 :            :         uint32_t *extent_page;
    8947                 :            :         uint32_t start_cluster_idx;
    8948                 :        240 :         bool free_extent_page = true;
    8949                 :            :         size_t i;
    8950                 :            : 
    8951                 :        240 :         ctx->cluster = bs_lba_to_cluster(ctx->blob->bs, ctx->blob->active.clusters[ctx->cluster_num]);
    8952                 :            : 
    8953                 :            :         /* There were concurrent unmaps to the same cluster, only release the cluster on the first one */
    8954         [ +  + ]:        240 :         if (ctx->cluster == 0) {
    8955                 :         32 :                 blob_op_cluster_msg_cb(ctx, 0);
    8956                 :         32 :                 return;
    8957                 :            :         }
    8958                 :            : 
    8959                 :        208 :         ctx->blob->active.clusters[ctx->cluster_num] = 0;
    8960         [ +  - ]:        208 :         if (ctx->cluster != 0) {
    8961                 :        208 :                 ctx->blob->active.num_allocated_clusters--;
    8962                 :            :         }
    8963                 :            : 
    8964   [ +  +  +  + ]:        208 :         if (ctx->blob->use_extent_table == false) {
    8965                 :            :                 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
    8966                 :        104 :                 spdk_spin_lock(&ctx->blob->bs->used_lock);
    8967                 :        104 :                 bs_release_cluster(ctx->blob->bs, ctx->cluster);
    8968                 :        104 :                 spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8969                 :        104 :                 ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8970                 :        104 :                 blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
    8971                 :        104 :                 return;
    8972                 :            :         }
    8973                 :            : 
    8974                 :        104 :         extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
    8975                 :            : 
    8976                 :            :         /* There shouldn't be parallel release operations on same cluster */
    8977         [ -  + ]:        104 :         assert(*extent_page == ctx->extent_page);
    8978                 :            : 
    8979         [ -  + ]:        104 :         start_cluster_idx = (ctx->cluster_num / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
    8980         [ +  - ]:        192 :         for (i = 0; i < SPDK_EXTENTS_PER_EP; ++i) {
    8981         [ +  + ]:        192 :                 if (ctx->blob->active.clusters[start_cluster_idx + i] != 0) {
    8982                 :        104 :                         free_extent_page = false;
    8983                 :        104 :                         break;
    8984                 :            :                 }
    8985                 :            :         }
    8986                 :            : 
    8987         [ -  + ]:        104 :         if (free_extent_page) {
    8988         [ #  # ]:          0 :                 assert(ctx->extent_page != 0);
    8989         [ #  # ]:          0 :                 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8990                 :          0 :                 ctx->blob->active.extent_pages[bs_cluster_to_extent_table_id(ctx->cluster_num)] = 0;
    8991                 :          0 :                 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
    8992                 :            :                                        blob_free_cluster_free_ep_cb, ctx);
    8993                 :            :         } else {
    8994                 :        104 :                 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
    8995                 :            :                                        blob_free_cluster_update_ep_cb, ctx);
    8996                 :            :         }
    8997                 :            : }
    8998                 :            : 
    8999                 :            : 
    9000                 :            : static void
    9001                 :        240 : blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, uint32_t extent_page,
    9002                 :            :                                struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
    9003                 :            : {
    9004                 :            :         struct spdk_blob_cluster_op_ctx *ctx;
    9005                 :            : 
    9006                 :        240 :         ctx = calloc(1, sizeof(*ctx));
    9007         [ -  + ]:        240 :         if (ctx == NULL) {
    9008                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9009                 :          0 :                 return;
    9010                 :            :         }
    9011                 :            : 
    9012                 :        240 :         ctx->thread = spdk_get_thread();
    9013                 :        240 :         ctx->blob = blob;
    9014                 :        240 :         ctx->cluster_num = cluster_num;
    9015                 :        240 :         ctx->extent_page = extent_page;
    9016                 :        240 :         ctx->page = page;
    9017                 :        240 :         ctx->cb_fn = cb_fn;
    9018                 :        240 :         ctx->cb_arg = cb_arg;
    9019                 :            : 
    9020                 :        240 :         spdk_thread_send_msg(blob->bs->md_thread, blob_free_cluster_msg, ctx);
    9021                 :            : }
    9022                 :            : 
    9023                 :            : /* START spdk_blob_close */
    9024                 :            : 
    9025                 :            : static void
    9026                 :      23352 : blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9027                 :            : {
    9028                 :      23352 :         struct spdk_blob *blob = cb_arg;
    9029                 :            : 
    9030         [ +  - ]:      23352 :         if (bserrno == 0) {
    9031                 :      23352 :                 blob->open_ref--;
    9032         [ +  + ]:      23352 :                 if (blob->open_ref == 0) {
    9033                 :            :                         /*
    9034                 :            :                          * Blobs with active.num_pages == 0 are deleted blobs.
    9035                 :            :                          *  these blobs are removed from the blob_store list
    9036                 :            :                          *  when the deletion process starts - so don't try to
    9037                 :            :                          *  remove them again.
    9038                 :            :                          */
    9039         [ +  + ]:      20096 :                         if (blob->active.num_pages > 0) {
    9040                 :      13380 :                                 spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
    9041                 :      13380 :                                 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
    9042                 :            :                         }
    9043                 :      20096 :                         blob_free(blob);
    9044                 :            :                 }
    9045                 :            :         }
    9046                 :            : 
    9047                 :      23352 :         bs_sequence_finish(seq, bserrno);
    9048                 :      23352 : }
    9049                 :            : 
    9050                 :            : static void
    9051                 :        526 : blob_close_esnap_done(void *cb_arg, struct spdk_blob *blob, int bserrno)
    9052                 :            : {
    9053                 :        526 :         spdk_bs_sequence_t      *seq = cb_arg;
    9054                 :            : 
    9055         [ -  + ]:        526 :         if (bserrno != 0) {
    9056   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": close failed with error %d\n",
    9057                 :            :                               blob->id, bserrno);
    9058                 :          0 :                 bs_sequence_finish(seq, bserrno);
    9059                 :          0 :                 return;
    9060                 :            :         }
    9061                 :            : 
    9062   [ -  +  -  + ]:        526 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": closed, syncing metadata on thread %s\n",
    9063                 :            :                       blob->id, spdk_thread_get_name(spdk_get_thread()));
    9064                 :            : 
    9065                 :            :         /* Sync metadata */
    9066                 :        526 :         blob_persist(seq, blob, blob_close_cpl, blob);
    9067                 :            : }
    9068                 :            : 
    9069                 :            : void
    9070                 :      23352 : spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
    9071                 :            : {
    9072                 :      22969 :         struct spdk_bs_cpl      cpl;
    9073                 :            :         spdk_bs_sequence_t      *seq;
    9074                 :            : 
    9075                 :      23352 :         blob_verify_md_op(blob);
    9076                 :            : 
    9077   [ -  +  -  + ]:      23352 :         SPDK_DEBUGLOG(blob, "Closing blob 0x%" PRIx64 "\n", blob->id);
    9078                 :            : 
    9079         [ -  + ]:      23352 :         if (blob->open_ref == 0) {
    9080                 :          0 :                 cb_fn(cb_arg, -EBADF);
    9081                 :          0 :                 return;
    9082                 :            :         }
    9083                 :            : 
    9084                 :      23352 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    9085                 :      23352 :         cpl.u.blob_basic.cb_fn = cb_fn;
    9086                 :      23352 :         cpl.u.blob_basic.cb_arg = cb_arg;
    9087                 :            : 
    9088                 :      23352 :         seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
    9089         [ -  + ]:      23352 :         if (!seq) {
    9090                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9091                 :          0 :                 return;
    9092                 :            :         }
    9093                 :            : 
    9094   [ +  +  +  + ]:      23352 :         if (blob->open_ref == 1 && blob_is_esnap_clone(blob)) {
    9095                 :        526 :                 blob_esnap_destroy_bs_dev_channels(blob, false, blob_close_esnap_done, seq);
    9096                 :        526 :                 return;
    9097                 :            :         }
    9098                 :            : 
    9099                 :            :         /* Sync metadata */
    9100                 :      22826 :         blob_persist(seq, blob, blob_close_cpl, blob);
    9101                 :            : }
    9102                 :            : 
    9103                 :            : /* END spdk_blob_close */
    9104                 :            : 
    9105                 :       1605 : struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
    9106                 :            : {
    9107                 :       1605 :         return spdk_get_io_channel(bs);
    9108                 :            : }
    9109                 :            : 
    9110                 :            : void
    9111                 :        961 : spdk_bs_free_io_channel(struct spdk_io_channel *channel)
    9112                 :            : {
    9113                 :        961 :         blob_esnap_destroy_bs_channel(spdk_io_channel_get_ctx(channel));
    9114                 :        961 :         spdk_put_io_channel(channel);
    9115                 :        961 : }
    9116                 :            : 
    9117                 :            : void
    9118                 :     151116 : spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9119                 :            :                    uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
    9120                 :            : {
    9121                 :     151116 :         blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
    9122                 :            :                                SPDK_BLOB_UNMAP);
    9123                 :     151116 : }
    9124                 :            : 
    9125                 :            : void
    9126                 :        197 : spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9127                 :            :                           uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
    9128                 :            : {
    9129                 :        197 :         blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
    9130                 :            :                                SPDK_BLOB_WRITE_ZEROES);
    9131                 :        197 : }
    9132                 :            : 
    9133                 :            : void
    9134                 :     333100 : spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9135                 :            :                    void *payload, uint64_t offset, uint64_t length,
    9136                 :            :                    spdk_blob_op_complete cb_fn, void *cb_arg)
    9137                 :            : {
    9138                 :     333100 :         blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
    9139                 :            :                                SPDK_BLOB_WRITE);
    9140                 :     333100 : }
    9141                 :            : 
    9142                 :            : void
    9143                 :    1388524 : spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9144                 :            :                   void *payload, uint64_t offset, uint64_t length,
    9145                 :            :                   spdk_blob_op_complete cb_fn, void *cb_arg)
    9146                 :            : {
    9147                 :    1388524 :         blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
    9148                 :            :                                SPDK_BLOB_READ);
    9149                 :    1388524 : }
    9150                 :            : 
    9151                 :            : void
    9152                 :        560 : spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9153                 :            :                     struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9154                 :            :                     spdk_blob_op_complete cb_fn, void *cb_arg)
    9155                 :            : {
    9156                 :        560 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false, NULL);
    9157                 :        560 : }
    9158                 :            : 
    9159                 :            : void
    9160                 :       3760 : spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9161                 :            :                    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9162                 :            :                    spdk_blob_op_complete cb_fn, void *cb_arg)
    9163                 :            : {
    9164                 :       3760 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true, NULL);
    9165                 :       3760 : }
    9166                 :            : 
    9167                 :            : void
    9168                 :    2577875 : spdk_blob_io_writev_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9169                 :            :                         struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9170                 :            :                         spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
    9171                 :            : {
    9172                 :    2577875 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false,
    9173                 :            :                                    io_opts);
    9174                 :    2577875 : }
    9175                 :            : 
    9176                 :            : void
    9177                 :    2285235 : spdk_blob_io_readv_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9178                 :            :                        struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9179                 :            :                        spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
    9180                 :            : {
    9181                 :    2285235 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true,
    9182                 :            :                                    io_opts);
    9183                 :    2285235 : }
    9184                 :            : 
    9185                 :            : struct spdk_bs_iter_ctx {
    9186                 :            :         int64_t page_num;
    9187                 :            :         struct spdk_blob_store *bs;
    9188                 :            : 
    9189                 :            :         spdk_blob_op_with_handle_complete cb_fn;
    9190                 :            :         void *cb_arg;
    9191                 :            : };
    9192                 :            : 
    9193                 :            : static void
    9194                 :       8539 : bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    9195                 :            : {
    9196                 :       8539 :         struct spdk_bs_iter_ctx *ctx = cb_arg;
    9197                 :       8539 :         struct spdk_blob_store *bs = ctx->bs;
    9198                 :            :         spdk_blob_id id;
    9199                 :            : 
    9200         [ +  + ]:       8539 :         if (bserrno == 0) {
    9201                 :       3641 :                 ctx->cb_fn(ctx->cb_arg, _blob, bserrno);
    9202                 :       3641 :                 free(ctx);
    9203                 :       3641 :                 return;
    9204                 :            :         }
    9205                 :            : 
    9206                 :       4898 :         ctx->page_num++;
    9207                 :       4898 :         ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num);
    9208         [ +  + ]:       4898 :         if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) {
    9209                 :       1225 :                 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
    9210                 :       1225 :                 free(ctx);
    9211                 :       1225 :                 return;
    9212                 :            :         }
    9213                 :            : 
    9214                 :       3673 :         id = bs_page_to_blobid(ctx->page_num);
    9215                 :            : 
    9216                 :       3673 :         spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx);
    9217                 :            : }
    9218                 :            : 
    9219                 :            : void
    9220                 :       1321 : spdk_bs_iter_first(struct spdk_blob_store *bs,
    9221                 :            :                    spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    9222                 :            : {
    9223                 :            :         struct spdk_bs_iter_ctx *ctx;
    9224                 :            : 
    9225                 :       1321 :         ctx = calloc(1, sizeof(*ctx));
    9226         [ -  + ]:       1321 :         if (!ctx) {
    9227                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    9228                 :          0 :                 return;
    9229                 :            :         }
    9230                 :            : 
    9231                 :       1321 :         ctx->page_num = -1;
    9232                 :       1321 :         ctx->bs = bs;
    9233                 :       1321 :         ctx->cb_fn = cb_fn;
    9234                 :       1321 :         ctx->cb_arg = cb_arg;
    9235                 :            : 
    9236                 :       1321 :         bs_iter_cpl(ctx, NULL, -1);
    9237                 :            : }
    9238                 :            : 
    9239                 :            : static void
    9240                 :       3545 : bs_iter_close_cpl(void *cb_arg, int bserrno)
    9241                 :            : {
    9242                 :       3545 :         struct spdk_bs_iter_ctx *ctx = cb_arg;
    9243                 :            : 
    9244                 :       3545 :         bs_iter_cpl(ctx, NULL, -1);
    9245                 :       3545 : }
    9246                 :            : 
    9247                 :            : void
    9248                 :       3545 : spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob,
    9249                 :            :                   spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    9250                 :            : {
    9251                 :            :         struct spdk_bs_iter_ctx *ctx;
    9252                 :            : 
    9253         [ -  + ]:       3545 :         assert(blob != NULL);
    9254                 :            : 
    9255                 :       3545 :         ctx = calloc(1, sizeof(*ctx));
    9256         [ -  + ]:       3545 :         if (!ctx) {
    9257                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    9258                 :          0 :                 return;
    9259                 :            :         }
    9260                 :            : 
    9261                 :       3545 :         ctx->page_num = bs_blobid_to_page(blob->id);
    9262                 :       3545 :         ctx->bs = bs;
    9263                 :       3545 :         ctx->cb_fn = cb_fn;
    9264                 :       3545 :         ctx->cb_arg = cb_arg;
    9265                 :            : 
    9266                 :            :         /* Close the existing blob */
    9267                 :       3545 :         spdk_blob_close(blob, bs_iter_close_cpl, ctx);
    9268                 :            : }
    9269                 :            : 
    9270                 :            : static int
    9271                 :      17742 : blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
    9272                 :            :                uint16_t value_len, bool internal)
    9273                 :            : {
    9274                 :            :         struct spdk_xattr_tailq *xattrs;
    9275                 :            :         struct spdk_xattr       *xattr;
    9276                 :            :         size_t                  desc_size;
    9277                 :            :         void                    *tmp;
    9278                 :            : 
    9279                 :      17742 :         blob_verify_md_op(blob);
    9280                 :            : 
    9281   [ +  +  +  + ]:      17742 :         if (blob->md_ro) {
    9282                 :         16 :                 return -EPERM;
    9283                 :            :         }
    9284                 :            : 
    9285         [ -  + ]:      17726 :         desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len;
    9286         [ +  + ]:      17726 :         if (desc_size > SPDK_BS_MAX_DESC_SIZE) {
    9287   [ -  +  -  + ]:         16 :                 SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name,
    9288                 :            :                               desc_size, SPDK_BS_MAX_DESC_SIZE);
    9289                 :         16 :                 return -ENOMEM;
    9290                 :            :         }
    9291                 :            : 
    9292         [ +  + ]:      17710 :         if (internal) {
    9293                 :       3031 :                 xattrs = &blob->xattrs_internal;
    9294                 :       3031 :                 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR;
    9295                 :            :         } else {
    9296                 :      14679 :                 xattrs = &blob->xattrs;
    9297                 :            :         }
    9298                 :            : 
    9299         [ +  + ]:      31224 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9300   [ +  +  -  +  :      25364 :                 if (!strcmp(name, xattr->name)) {
                   +  + ]
    9301                 :      11850 :                         tmp = malloc(value_len);
    9302         [ -  + ]:      11850 :                         if (!tmp) {
    9303                 :          0 :                                 return -ENOMEM;
    9304                 :            :                         }
    9305                 :            : 
    9306                 :      11850 :                         free(xattr->value);
    9307                 :      11850 :                         xattr->value_len = value_len;
    9308                 :      11850 :                         xattr->value = tmp;
    9309   [ -  +  -  + ]:      11850 :                         memcpy(xattr->value, value, value_len);
    9310                 :            : 
    9311                 :      11850 :                         blob->state = SPDK_BLOB_STATE_DIRTY;
    9312                 :            : 
    9313                 :      11850 :                         return 0;
    9314                 :            :                 }
    9315                 :            :         }
    9316                 :            : 
    9317                 :       5860 :         xattr = calloc(1, sizeof(*xattr));
    9318         [ -  + ]:       5860 :         if (!xattr) {
    9319                 :          0 :                 return -ENOMEM;
    9320                 :            :         }
    9321                 :            : 
    9322         [ -  + ]:       5860 :         xattr->name = strdup(name);
    9323         [ -  + ]:       5860 :         if (!xattr->name) {
    9324                 :          0 :                 free(xattr);
    9325                 :          0 :                 return -ENOMEM;
    9326                 :            :         }
    9327                 :            : 
    9328                 :       5860 :         xattr->value_len = value_len;
    9329                 :       5860 :         xattr->value = malloc(value_len);
    9330         [ -  + ]:       5860 :         if (!xattr->value) {
    9331                 :          0 :                 free(xattr->name);
    9332                 :          0 :                 free(xattr);
    9333                 :          0 :                 return -ENOMEM;
    9334                 :            :         }
    9335   [ -  +  -  + ]:       5860 :         memcpy(xattr->value, value, value_len);
    9336                 :       5860 :         TAILQ_INSERT_TAIL(xattrs, xattr, link);
    9337                 :            : 
    9338                 :       5860 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    9339                 :            : 
    9340                 :       5860 :         return 0;
    9341                 :            : }
    9342                 :            : 
    9343                 :            : int
    9344                 :      13943 : spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
    9345                 :            :                     uint16_t value_len)
    9346                 :            : {
    9347                 :      13943 :         return blob_set_xattr(blob, name, value, value_len, false);
    9348                 :            : }
    9349                 :            : 
    9350                 :            : static int
    9351                 :       1673 : blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal)
    9352                 :            : {
    9353                 :            :         struct spdk_xattr_tailq *xattrs;
    9354                 :            :         struct spdk_xattr       *xattr;
    9355                 :            : 
    9356                 :       1673 :         blob_verify_md_op(blob);
    9357                 :            : 
    9358   [ +  +  +  + ]:       1673 :         if (blob->md_ro) {
    9359                 :         16 :                 return -EPERM;
    9360                 :            :         }
    9361         [ +  + ]:       1657 :         xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
    9362                 :            : 
    9363         [ +  + ]:       1706 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9364   [ +  +  -  +  :       1493 :                 if (!strcmp(name, xattr->name)) {
                   +  + ]
    9365         [ +  + ]:       1444 :                         TAILQ_REMOVE(xattrs, xattr, link);
    9366                 :       1444 :                         free(xattr->value);
    9367                 :       1444 :                         free(xattr->name);
    9368                 :       1444 :                         free(xattr);
    9369                 :            : 
    9370   [ +  +  +  + ]:       1444 :                         if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) {
    9371                 :        997 :                                 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR;
    9372                 :            :                         }
    9373                 :       1444 :                         blob->state = SPDK_BLOB_STATE_DIRTY;
    9374                 :            : 
    9375                 :       1444 :                         return 0;
    9376                 :            :                 }
    9377                 :            :         }
    9378                 :            : 
    9379                 :        213 :         return -ENOENT;
    9380                 :            : }
    9381                 :            : 
    9382                 :            : int
    9383                 :        145 : spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name)
    9384                 :            : {
    9385                 :        145 :         return blob_remove_xattr(blob, name, false);
    9386                 :            : }
    9387                 :            : 
    9388                 :            : static int
    9389                 :      16824 : blob_get_xattr_value(struct spdk_blob *blob, const char *name,
    9390                 :            :                      const void **value, size_t *value_len, bool internal)
    9391                 :            : {
    9392                 :            :         struct spdk_xattr       *xattr;
    9393                 :            :         struct spdk_xattr_tailq *xattrs;
    9394                 :            : 
    9395         [ +  + ]:      16824 :         xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
    9396                 :            : 
    9397         [ +  + ]:      23126 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9398   [ +  +  -  +  :      12243 :                 if (!strcmp(name, xattr->name)) {
                   +  + ]
    9399                 :       5941 :                         *value = xattr->value;
    9400                 :       5941 :                         *value_len = xattr->value_len;
    9401                 :       5941 :                         return 0;
    9402                 :            :                 }
    9403                 :            :         }
    9404                 :      10883 :         return -ENOENT;
    9405                 :            : }
    9406                 :            : 
    9407                 :            : int
    9408                 :       4479 : spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
    9409                 :            :                           const void **value, size_t *value_len)
    9410                 :            : {
    9411                 :       4479 :         blob_verify_md_op(blob);
    9412                 :            : 
    9413                 :       4479 :         return blob_get_xattr_value(blob, name, value, value_len, false);
    9414                 :            : }
    9415                 :            : 
    9416                 :            : struct spdk_xattr_names {
    9417                 :            :         uint32_t        count;
    9418                 :            :         const char      *names[0];
    9419                 :            : };
    9420                 :            : 
    9421                 :            : static int
    9422                 :         17 : blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names)
    9423                 :            : {
    9424                 :            :         struct spdk_xattr       *xattr;
    9425                 :         17 :         int                     count = 0;
    9426                 :            : 
    9427         [ +  + ]:         50 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9428                 :         33 :                 count++;
    9429                 :            :         }
    9430                 :            : 
    9431                 :         17 :         *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
    9432         [ -  + ]:         17 :         if (*names == NULL) {
    9433                 :          0 :                 return -ENOMEM;
    9434                 :            :         }
    9435                 :            : 
    9436         [ +  + ]:         50 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9437                 :         33 :                 (*names)->names[(*names)->count++] = xattr->name;
    9438                 :            :         }
    9439                 :            : 
    9440                 :         17 :         return 0;
    9441                 :            : }
    9442                 :            : 
    9443                 :            : int
    9444                 :         17 : spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names)
    9445                 :            : {
    9446                 :         17 :         blob_verify_md_op(blob);
    9447                 :            : 
    9448                 :         17 :         return blob_get_xattr_names(&blob->xattrs, names);
    9449                 :            : }
    9450                 :            : 
    9451                 :            : uint32_t
    9452                 :         19 : spdk_xattr_names_get_count(struct spdk_xattr_names *names)
    9453                 :            : {
    9454         [ -  + ]:         19 :         assert(names != NULL);
    9455                 :            : 
    9456                 :         19 :         return names->count;
    9457                 :            : }
    9458                 :            : 
    9459                 :            : const char *
    9460                 :         34 : spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
    9461                 :            : {
    9462         [ -  + ]:         34 :         if (index >= names->count) {
    9463                 :          0 :                 return NULL;
    9464                 :            :         }
    9465                 :            : 
    9466                 :         34 :         return names->names[index];
    9467                 :            : }
    9468                 :            : 
    9469                 :            : void
    9470                 :         17 : spdk_xattr_names_free(struct spdk_xattr_names *names)
    9471                 :            : {
    9472                 :         17 :         free(names);
    9473                 :         17 : }
    9474                 :            : 
    9475                 :            : struct spdk_bs_type
    9476                 :         19 : spdk_bs_get_bstype(struct spdk_blob_store *bs)
    9477                 :            : {
    9478                 :         19 :         return bs->bstype;
    9479                 :            : }
    9480                 :            : 
    9481                 :            : void
    9482                 :          0 : spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
    9483                 :            : {
    9484                 :          0 :         memcpy(&bs->bstype, &bstype, sizeof(bstype));
    9485                 :          0 : }
    9486                 :            : 
    9487                 :            : bool
    9488                 :       6316 : spdk_blob_is_read_only(struct spdk_blob *blob)
    9489                 :            : {
    9490         [ -  + ]:       6316 :         assert(blob != NULL);
    9491   [ +  +  +  +  :       6316 :         return (blob->data_ro || blob->md_ro);
             -  +  -  + ]
    9492                 :            : }
    9493                 :            : 
    9494                 :            : bool
    9495                 :       2609 : spdk_blob_is_snapshot(struct spdk_blob *blob)
    9496                 :            : {
    9497                 :            :         struct spdk_blob_list *snapshot_entry;
    9498                 :            : 
    9499         [ -  + ]:       2609 :         assert(blob != NULL);
    9500                 :            : 
    9501                 :       2609 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
    9502         [ +  + ]:       2609 :         if (snapshot_entry == NULL) {
    9503                 :       2432 :                 return false;
    9504                 :            :         }
    9505                 :            : 
    9506                 :        177 :         return true;
    9507                 :            : }
    9508                 :            : 
    9509                 :            : bool
    9510                 :       2673 : spdk_blob_is_clone(struct spdk_blob *blob)
    9511                 :            : {
    9512         [ -  + ]:       2673 :         assert(blob != NULL);
    9513                 :            : 
    9514         [ +  + ]:       2673 :         if (blob->parent_id != SPDK_BLOBID_INVALID &&
    9515         [ +  + ]:        340 :             blob->parent_id != SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    9516         [ -  + ]:        268 :                 assert(spdk_blob_is_thin_provisioned(blob));
    9517                 :        268 :                 return true;
    9518                 :            :         }
    9519                 :            : 
    9520                 :       2405 :         return false;
    9521                 :            : }
    9522                 :            : 
    9523                 :            : bool
    9524                 :    1231863 : spdk_blob_is_thin_provisioned(struct spdk_blob *blob)
    9525                 :            : {
    9526         [ -  + ]:    1231863 :         assert(blob != NULL);
    9527                 :    1231863 :         return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
    9528                 :            : }
    9529                 :            : 
    9530                 :            : bool
    9531                 :    6732629 : spdk_blob_is_esnap_clone(const struct spdk_blob *blob)
    9532                 :            : {
    9533                 :    6732629 :         return blob_is_esnap_clone(blob);
    9534                 :            : }
    9535                 :            : 
    9536                 :            : static void
    9537                 :      20223 : blob_update_clear_method(struct spdk_blob *blob)
    9538                 :            : {
    9539                 :            :         enum blob_clear_method stored_cm;
    9540                 :            : 
    9541         [ -  + ]:      20223 :         assert(blob != NULL);
    9542                 :            : 
    9543                 :            :         /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored
    9544                 :            :          * in metadata previously.  If something other than the default was
    9545                 :            :          * specified, ignore stored value and used what was passed in.
    9546                 :            :          */
    9547                 :      20223 :         stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT);
    9548                 :            : 
    9549         [ +  + ]:      20223 :         if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) {
    9550                 :      20220 :                 blob->clear_method = stored_cm;
    9551         [ -  + ]:          3 :         } else if (blob->clear_method != stored_cm) {
    9552                 :          0 :                 SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n",
    9553                 :            :                              blob->clear_method, stored_cm);
    9554                 :            :         }
    9555                 :      20223 : }
    9556                 :            : 
    9557                 :            : spdk_blob_id
    9558                 :       1088 : spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id)
    9559                 :            : {
    9560                 :       1088 :         struct spdk_blob_list *snapshot_entry = NULL;
    9561                 :       1088 :         struct spdk_blob_list *clone_entry = NULL;
    9562                 :            : 
    9563         [ +  + ]:       2038 :         TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
    9564         [ +  + ]:       3014 :                 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
    9565         [ +  + ]:       2064 :                         if (clone_entry->id == blob_id) {
    9566                 :        725 :                                 return snapshot_entry->id;
    9567                 :            :                         }
    9568                 :            :                 }
    9569                 :            :         }
    9570                 :            : 
    9571                 :        363 :         return SPDK_BLOBID_INVALID;
    9572                 :            : }
    9573                 :            : 
    9574                 :            : int
    9575                 :       1434 : spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids,
    9576                 :            :                      size_t *count)
    9577                 :            : {
    9578                 :            :         struct spdk_blob_list *snapshot_entry, *clone_entry;
    9579                 :            :         size_t n;
    9580                 :            : 
    9581                 :       1434 :         snapshot_entry = bs_get_snapshot_entry(bs, blobid);
    9582         [ +  + ]:       1434 :         if (snapshot_entry == NULL) {
    9583                 :        609 :                 *count = 0;
    9584                 :        609 :                 return 0;
    9585                 :            :         }
    9586                 :            : 
    9587   [ +  +  +  + ]:        825 :         if (ids == NULL || *count < snapshot_entry->clone_count) {
    9588                 :        116 :                 *count = snapshot_entry->clone_count;
    9589                 :        116 :                 return -ENOMEM;
    9590                 :            :         }
    9591                 :        709 :         *count = snapshot_entry->clone_count;
    9592                 :            : 
    9593                 :        709 :         n = 0;
    9594         [ +  + ]:       1481 :         TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
    9595                 :        772 :                 ids[n++] = clone_entry->id;
    9596                 :            :         }
    9597                 :            : 
    9598                 :        709 :         return 0;
    9599                 :            : }
    9600                 :            : 
    9601                 :            : static void
    9602                 :         16 : bs_load_grow_continue(struct spdk_bs_load_ctx *ctx)
    9603                 :            : {
    9604                 :            :         int rc;
    9605                 :            : 
    9606         [ -  + ]:         16 :         if (ctx->super->size == 0) {
    9607                 :          0 :                 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9608                 :            :         }
    9609                 :            : 
    9610         [ -  + ]:         16 :         if (ctx->super->io_unit_size == 0) {
    9611                 :          0 :                 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
    9612                 :            :         }
    9613                 :            : 
    9614                 :            :         /* Parse the super block */
    9615                 :         16 :         ctx->bs->clean = 1;
    9616                 :         16 :         ctx->bs->cluster_sz = ctx->super->cluster_size;
    9617         [ -  + ]:         16 :         ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
    9618                 :         16 :         ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
    9619         [ +  - ]:         16 :         if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
    9620                 :         16 :                 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
    9621                 :            :         }
    9622                 :         16 :         ctx->bs->io_unit_size = ctx->super->io_unit_size;
    9623                 :         16 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
    9624         [ -  + ]:         16 :         if (rc < 0) {
    9625                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    9626                 :          0 :                 return;
    9627                 :            :         }
    9628                 :         16 :         ctx->bs->md_start = ctx->super->md_start;
    9629                 :         16 :         ctx->bs->md_len = ctx->super->md_len;
    9630                 :         16 :         rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
    9631         [ -  + ]:         16 :         if (rc < 0) {
    9632                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    9633                 :          0 :                 return;
    9634                 :            :         }
    9635                 :            : 
    9636                 :         40 :         ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
    9637                 :         24 :                                                ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
    9638                 :         16 :         ctx->bs->super_blob = ctx->super->super_blob;
    9639                 :         16 :         memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
    9640                 :            : 
    9641   [ +  -  -  + ]:         16 :         if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) {
    9642                 :          0 :                 SPDK_ERRLOG("Can not grow an unclean blobstore, please load it normally to clean it.\n");
    9643                 :          0 :                 bs_load_ctx_fail(ctx, -EIO);
    9644                 :          0 :                 return;
    9645                 :            :         } else {
    9646                 :         16 :                 bs_load_read_used_pages(ctx);
    9647                 :            :         }
    9648                 :            : }
    9649                 :            : 
    9650                 :            : static void
    9651                 :         16 : bs_load_grow_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9652                 :            : {
    9653                 :         16 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9654                 :            : 
    9655         [ -  + ]:         16 :         if (bserrno != 0) {
    9656                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    9657                 :          0 :                 return;
    9658                 :            :         }
    9659                 :         16 :         bs_load_grow_continue(ctx);
    9660                 :            : }
    9661                 :            : 
    9662                 :            : static void
    9663                 :         16 : bs_load_grow_used_clusters_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9664                 :            : {
    9665                 :         16 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9666                 :            : 
    9667         [ -  + ]:         16 :         if (bserrno != 0) {
    9668                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    9669                 :          0 :                 return;
    9670                 :            :         }
    9671                 :            : 
    9672                 :         16 :         spdk_free(ctx->mask);
    9673                 :            : 
    9674                 :         16 :         bs_sequence_write_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
    9675                 :         16 :                               bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
    9676                 :            :                               bs_load_grow_super_write_cpl, ctx);
    9677                 :            : }
    9678                 :            : 
    9679                 :            : static void
    9680                 :         16 : bs_load_grow_used_clusters_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9681                 :            : {
    9682                 :         16 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9683                 :            :         uint64_t                lba, lba_count;
    9684                 :            :         uint64_t                dev_size;
    9685                 :            :         uint64_t                total_clusters;
    9686                 :            : 
    9687         [ -  + ]:         16 :         if (bserrno != 0) {
    9688                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    9689                 :          0 :                 return;
    9690                 :            :         }
    9691                 :            : 
    9692                 :            :         /* The type must be correct */
    9693         [ -  + ]:         16 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
    9694                 :            :         /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
    9695         [ -  + ]:         16 :         assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
    9696                 :            :                                              struct spdk_blob_md_page) * 8));
    9697                 :         16 :         dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9698         [ -  + ]:         16 :         total_clusters = dev_size / ctx->super->cluster_size;
    9699                 :         16 :         ctx->mask->length = total_clusters;
    9700                 :            : 
    9701                 :         16 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    9702                 :         16 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    9703                 :         16 :         bs_sequence_write_dev(ctx->seq, ctx->mask, lba, lba_count,
    9704                 :            :                               bs_load_grow_used_clusters_write_cpl, ctx);
    9705                 :            : }
    9706                 :            : 
    9707                 :            : static void
    9708                 :         16 : bs_load_try_to_grow(struct spdk_bs_load_ctx *ctx)
    9709                 :            : {
    9710                 :            :         uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
    9711                 :            :         uint64_t lba, lba_count, mask_size;
    9712                 :            : 
    9713                 :         16 :         dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9714         [ -  + ]:         16 :         total_clusters = dev_size / ctx->super->cluster_size;
    9715                 :         16 :         used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    9716                 :         16 :                                 spdk_divide_round_up(total_clusters, 8),
    9717                 :            :                                 SPDK_BS_PAGE_SIZE);
    9718                 :         16 :         max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
    9719                 :            :         /* No necessary to grow or no space to grow */
    9720   [ +  -  -  + ]:         16 :         if (ctx->super->size >= dev_size || used_cluster_mask_len > max_used_cluster_mask) {
    9721   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "No grow\n");
    9722                 :          0 :                 bs_load_grow_continue(ctx);
    9723                 :          0 :                 return;
    9724                 :            :         }
    9725                 :            : 
    9726   [ -  +  -  + ]:         16 :         SPDK_DEBUGLOG(blob, "Resize blobstore\n");
    9727                 :            : 
    9728                 :         16 :         ctx->super->size = dev_size;
    9729                 :         16 :         ctx->super->used_cluster_mask_len = used_cluster_mask_len;
    9730                 :         16 :         ctx->super->crc = blob_md_page_calc_crc(ctx->super);
    9731                 :            : 
    9732                 :         16 :         mask_size = used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
    9733                 :         16 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    9734                 :            :                                  SPDK_MALLOC_DMA);
    9735         [ -  + ]:         16 :         if (!ctx->mask) {
    9736                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    9737                 :          0 :                 return;
    9738                 :            :         }
    9739                 :         16 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    9740                 :         16 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    9741                 :         16 :         bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
    9742                 :            :                              bs_load_grow_used_clusters_read_cpl, ctx);
    9743                 :            : }
    9744                 :            : 
    9745                 :            : static void
    9746                 :         16 : bs_grow_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9747                 :            : {
    9748                 :         16 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9749                 :            :         int rc;
    9750                 :            : 
    9751                 :         16 :         rc = bs_super_validate(ctx->super, ctx->bs);
    9752         [ -  + ]:         16 :         if (rc != 0) {
    9753                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    9754                 :          0 :                 return;
    9755                 :            :         }
    9756                 :            : 
    9757                 :         16 :         bs_load_try_to_grow(ctx);
    9758                 :            : }
    9759                 :            : 
    9760                 :            : struct spdk_bs_grow_ctx {
    9761                 :            :         struct spdk_blob_store          *bs;
    9762                 :            :         struct spdk_bs_super_block      *super;
    9763                 :            : 
    9764                 :            :         struct spdk_bit_pool            *new_used_clusters;
    9765                 :            :         struct spdk_bs_md_mask          *new_used_clusters_mask;
    9766                 :            : 
    9767                 :            :         spdk_bs_sequence_t              *seq;
    9768                 :            : };
    9769                 :            : 
    9770                 :            : static void
    9771                 :        130 : bs_grow_live_done(struct spdk_bs_grow_ctx *ctx, int bserrno)
    9772                 :            : {
    9773         [ +  + ]:        130 :         if (bserrno != 0) {
    9774                 :         32 :                 spdk_bit_pool_free(&ctx->new_used_clusters);
    9775                 :            :         }
    9776                 :            : 
    9777                 :        130 :         bs_sequence_finish(ctx->seq, bserrno);
    9778                 :        130 :         free(ctx->new_used_clusters_mask);
    9779                 :        130 :         spdk_free(ctx->super);
    9780                 :        130 :         free(ctx);
    9781                 :        130 : }
    9782                 :            : 
    9783                 :            : static void
    9784                 :         34 : bs_grow_live_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9785                 :            : {
    9786                 :         34 :         struct spdk_bs_grow_ctx *ctx = cb_arg;
    9787                 :         34 :         struct spdk_blob_store *bs = ctx->bs;
    9788                 :            :         uint64_t total_clusters;
    9789                 :            : 
    9790         [ -  + ]:         34 :         if (bserrno != 0) {
    9791                 :          0 :                 bs_grow_live_done(ctx, bserrno);
    9792                 :          0 :                 return;
    9793                 :            :         }
    9794                 :            : 
    9795                 :            :         /*
    9796                 :            :          * Blobstore is not clean until unload, for now only the super block is up to date.
    9797                 :            :          * This is similar to state right after blobstore init, when bs_write_used_md() didn't
    9798                 :            :          * yet execute.
    9799                 :            :          * When cleanly unloaded, the used md pages will be written out.
    9800                 :            :          * In case of unclean shutdown, loading blobstore will go through recovery path correctly
    9801                 :            :          * filling out the used_clusters with new size and writing it out.
    9802                 :            :          */
    9803                 :         34 :         bs->clean = 0;
    9804                 :            : 
    9805                 :            :         /* Reverting the super->size past this point is complex, avoid any error paths
    9806                 :            :          * that require to do so. */
    9807                 :         34 :         spdk_spin_lock(&bs->used_lock);
    9808                 :            : 
    9809         [ -  + ]:         34 :         total_clusters = ctx->super->size / ctx->super->cluster_size;
    9810                 :            : 
    9811         [ -  + ]:         34 :         assert(total_clusters >= spdk_bit_pool_capacity(bs->used_clusters));
    9812                 :         34 :         spdk_bit_pool_store_mask(bs->used_clusters, ctx->new_used_clusters_mask);
    9813                 :            : 
    9814         [ -  + ]:         34 :         assert(total_clusters == spdk_bit_pool_capacity(ctx->new_used_clusters));
    9815                 :         34 :         spdk_bit_pool_load_mask(ctx->new_used_clusters, ctx->new_used_clusters_mask);
    9816                 :            : 
    9817                 :         34 :         spdk_bit_pool_free(&bs->used_clusters);
    9818                 :         34 :         bs->used_clusters = ctx->new_used_clusters;
    9819                 :            : 
    9820                 :         34 :         bs->total_clusters = total_clusters;
    9821                 :         68 :         bs->total_data_clusters = bs->total_clusters - spdk_divide_round_up(
    9822                 :         34 :                                           bs->md_start + bs->md_len, bs->pages_per_cluster);
    9823                 :            : 
    9824                 :         34 :         bs->num_free_clusters = spdk_bit_pool_count_free(bs->used_clusters);
    9825         [ -  + ]:         34 :         assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
    9826                 :         34 :         spdk_spin_unlock(&bs->used_lock);
    9827                 :            : 
    9828                 :         34 :         bs_grow_live_done(ctx, 0);
    9829                 :            : }
    9830                 :            : 
    9831                 :            : static void
    9832                 :        130 : bs_grow_live_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9833                 :            : {
    9834                 :        130 :         struct spdk_bs_grow_ctx *ctx = cb_arg;
    9835                 :            :         uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
    9836                 :            :         int rc;
    9837                 :            : 
    9838         [ -  + ]:        130 :         if (bserrno != 0) {
    9839                 :          0 :                 bs_grow_live_done(ctx, bserrno);
    9840                 :          0 :                 return;
    9841                 :            :         }
    9842                 :            : 
    9843                 :        130 :         rc = bs_super_validate(ctx->super, ctx->bs);
    9844         [ +  + ]:        130 :         if (rc != 0) {
    9845                 :         16 :                 bs_grow_live_done(ctx, rc);
    9846                 :         16 :                 return;
    9847                 :            :         }
    9848                 :            : 
    9849                 :        114 :         dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9850         [ -  + ]:        114 :         total_clusters = dev_size / ctx->super->cluster_size;
    9851                 :        114 :         used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    9852                 :        114 :                                 spdk_divide_round_up(total_clusters, 8),
    9853                 :            :                                 SPDK_BS_PAGE_SIZE);
    9854                 :        114 :         max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
    9855                 :            :         /* Only checking dev_size. Since it can change, but total_clusters remain the same. */
    9856         [ +  + ]:        114 :         if (dev_size == ctx->super->size) {
    9857   [ -  +  -  + ]:         64 :                 SPDK_DEBUGLOG(blob, "No need to grow blobstore\n");
    9858                 :         64 :                 bs_grow_live_done(ctx, 0);
    9859                 :         64 :                 return;
    9860                 :            :         }
    9861                 :            :         /*
    9862                 :            :          * Blobstore cannot be shrunk, so check before if:
    9863                 :            :          * - new size of the device is smaller than size in super_block
    9864                 :            :          * - new total number of clusters is smaller than used_clusters bit_pool
    9865                 :            :          * - there is enough space in metadata for used_cluster_mask to be written out
    9866                 :            :          */
    9867         [ +  - ]:         50 :         if (dev_size < ctx->super->size ||
    9868   [ +  -  +  + ]:         50 :             total_clusters < spdk_bit_pool_capacity(ctx->bs->used_clusters) ||
    9869                 :            :             used_cluster_mask_len > max_used_cluster_mask) {
    9870   [ -  +  -  + ]:         16 :                 SPDK_DEBUGLOG(blob, "No space to grow blobstore\n");
    9871                 :         16 :                 bs_grow_live_done(ctx, -ENOSPC);
    9872                 :         16 :                 return;
    9873                 :            :         }
    9874                 :            : 
    9875   [ -  +  -  + ]:         34 :         SPDK_DEBUGLOG(blob, "Resizing blobstore\n");
    9876                 :            : 
    9877                 :         34 :         ctx->new_used_clusters_mask = calloc(1, total_clusters);
    9878         [ -  + ]:         34 :         if (!ctx->new_used_clusters_mask) {
    9879                 :          0 :                 bs_grow_live_done(ctx, -ENOMEM);
    9880                 :          0 :                 return;
    9881                 :            :         }
    9882                 :         34 :         ctx->new_used_clusters = spdk_bit_pool_create(total_clusters);
    9883         [ -  + ]:         34 :         if (!ctx->new_used_clusters) {
    9884                 :          0 :                 bs_grow_live_done(ctx, -ENOMEM);
    9885                 :          0 :                 return;
    9886                 :            :         }
    9887                 :            : 
    9888                 :         34 :         ctx->super->clean = 0;
    9889                 :         34 :         ctx->super->size = dev_size;
    9890                 :         34 :         ctx->super->used_cluster_mask_len = used_cluster_mask_len;
    9891                 :         34 :         bs_write_super(seq, ctx->bs, ctx->super, bs_grow_live_super_write_cpl, ctx);
    9892                 :            : }
    9893                 :            : 
    9894                 :            : void
    9895                 :        130 : spdk_bs_grow_live(struct spdk_blob_store *bs,
    9896                 :            :                   spdk_bs_op_complete cb_fn, void *cb_arg)
    9897                 :            : {
    9898                 :        130 :         struct spdk_bs_cpl      cpl;
    9899                 :            :         struct spdk_bs_grow_ctx *ctx;
    9900                 :            : 
    9901         [ -  + ]:        130 :         assert(spdk_get_thread() == bs->md_thread);
    9902                 :            : 
    9903   [ -  +  -  + ]:        130 :         SPDK_DEBUGLOG(blob, "Growing blobstore on dev %p\n", bs->dev);
    9904                 :            : 
    9905                 :        130 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    9906                 :        130 :         cpl.u.bs_basic.cb_fn = cb_fn;
    9907                 :        130 :         cpl.u.bs_basic.cb_arg = cb_arg;
    9908                 :            : 
    9909                 :        130 :         ctx = calloc(1, sizeof(struct spdk_bs_grow_ctx));
    9910         [ -  + ]:        130 :         if (!ctx) {
    9911                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9912                 :          0 :                 return;
    9913                 :            :         }
    9914                 :        130 :         ctx->bs = bs;
    9915                 :            : 
    9916                 :        130 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    9917                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    9918         [ -  + ]:        130 :         if (!ctx->super) {
    9919                 :          0 :                 free(ctx);
    9920                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9921                 :          0 :                 return;
    9922                 :            :         }
    9923                 :            : 
    9924                 :        130 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    9925         [ -  + ]:        130 :         if (!ctx->seq) {
    9926                 :          0 :                 spdk_free(ctx->super);
    9927                 :          0 :                 free(ctx);
    9928                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9929                 :          0 :                 return;
    9930                 :            :         }
    9931                 :            : 
    9932                 :            :         /* Read the super block */
    9933                 :        130 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    9934                 :        130 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    9935                 :            :                              bs_grow_live_load_super_cpl, ctx);
    9936                 :            : }
    9937                 :            : 
    9938                 :            : void
    9939                 :         16 : spdk_bs_grow(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
    9940                 :            :              spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
    9941                 :            : {
    9942                 :         16 :         struct spdk_blob_store  *bs;
    9943                 :         16 :         struct spdk_bs_cpl      cpl;
    9944                 :         16 :         struct spdk_bs_load_ctx *ctx;
    9945                 :         16 :         struct spdk_bs_opts     opts = {};
    9946                 :            :         int err;
    9947                 :            : 
    9948   [ -  +  -  + ]:         16 :         SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
    9949                 :            : 
    9950   [ -  +  -  + ]:         16 :         if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
    9951   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
    9952                 :          0 :                 dev->destroy(dev);
    9953                 :          0 :                 cb_fn(cb_arg, NULL, -EINVAL);
    9954                 :          0 :                 return;
    9955                 :            :         }
    9956                 :            : 
    9957                 :         16 :         spdk_bs_opts_init(&opts, sizeof(opts));
    9958         [ +  - ]:         16 :         if (o) {
    9959         [ -  + ]:         16 :                 if (bs_opts_copy(o, &opts)) {
    9960                 :          0 :                         return;
    9961                 :            :                 }
    9962                 :            :         }
    9963                 :            : 
    9964   [ +  -  -  + ]:         16 :         if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
    9965                 :          0 :                 dev->destroy(dev);
    9966                 :          0 :                 cb_fn(cb_arg, NULL, -EINVAL);
    9967                 :          0 :                 return;
    9968                 :            :         }
    9969                 :            : 
    9970                 :         16 :         err = bs_alloc(dev, &opts, &bs, &ctx);
    9971         [ -  + ]:         16 :         if (err) {
    9972                 :          0 :                 dev->destroy(dev);
    9973                 :          0 :                 cb_fn(cb_arg, NULL, err);
    9974                 :          0 :                 return;
    9975                 :            :         }
    9976                 :            : 
    9977                 :         16 :         cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
    9978                 :         16 :         cpl.u.bs_handle.cb_fn = cb_fn;
    9979                 :         16 :         cpl.u.bs_handle.cb_arg = cb_arg;
    9980                 :         16 :         cpl.u.bs_handle.bs = bs;
    9981                 :            : 
    9982                 :         16 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    9983         [ -  + ]:         16 :         if (!ctx->seq) {
    9984                 :          0 :                 spdk_free(ctx->super);
    9985                 :          0 :                 free(ctx);
    9986                 :          0 :                 bs_free(bs);
    9987                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    9988                 :          0 :                 return;
    9989                 :            :         }
    9990                 :            : 
    9991                 :            :         /* Read the super block */
    9992                 :         16 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    9993                 :         16 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    9994                 :            :                              bs_grow_load_super_cpl, ctx);
    9995                 :            : }
    9996                 :            : 
    9997                 :            : int
    9998                 :        125 : spdk_blob_get_esnap_id(struct spdk_blob *blob, const void **id, size_t *len)
    9999                 :            : {
   10000         [ +  + ]:        125 :         if (!blob_is_esnap_clone(blob)) {
   10001                 :         50 :                 return -EINVAL;
   10002                 :            :         }
   10003                 :            : 
   10004                 :         75 :         return blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, id, len, true);
   10005                 :            : }
   10006                 :            : 
   10007                 :            : struct spdk_io_channel *
   10008                 :      49135 : blob_esnap_get_io_channel(struct spdk_io_channel *ch, struct spdk_blob *blob)
   10009                 :            : {
   10010                 :      49135 :         struct spdk_bs_channel          *bs_channel = spdk_io_channel_get_ctx(ch);
   10011                 :      49135 :         struct spdk_bs_dev              *bs_dev = blob->back_bs_dev;
   10012                 :      49135 :         struct blob_esnap_channel       find = {};
   10013                 :            :         struct blob_esnap_channel       *esnap_channel, *existing;
   10014                 :            : 
   10015                 :      49135 :         find.blob_id = blob->id;
   10016                 :      49135 :         esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
   10017         [ +  + ]:      49135 :         if (spdk_likely(esnap_channel != NULL)) {
   10018   [ -  +  -  + ]:      48938 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": using cached channel on thread %s\n",
   10019                 :            :                               blob->id, spdk_thread_get_name(spdk_get_thread()));
   10020                 :      48938 :                 return esnap_channel->channel;
   10021                 :            :         }
   10022                 :            : 
   10023   [ -  +  -  + ]:        197 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": allocating channel on thread %s\n",
   10024                 :            :                       blob->id, spdk_thread_get_name(spdk_get_thread()));
   10025                 :            : 
   10026                 :        197 :         esnap_channel = calloc(1, sizeof(*esnap_channel));
   10027         [ -  + ]:        197 :         if (esnap_channel == NULL) {
   10028                 :          0 :                 SPDK_NOTICELOG("blob 0x%" PRIx64 " channel allocation failed: no memory\n",
   10029                 :            :                                find.blob_id);
   10030                 :          0 :                 return NULL;
   10031                 :            :         }
   10032                 :        197 :         esnap_channel->channel = bs_dev->create_channel(bs_dev);
   10033         [ -  + ]:        197 :         if (esnap_channel->channel == NULL) {
   10034                 :          0 :                 SPDK_NOTICELOG("blob 0x%" PRIx64 " back channel allocation failed\n", blob->id);
   10035                 :          0 :                 free(esnap_channel);
   10036                 :          0 :                 return NULL;
   10037                 :            :         }
   10038                 :        197 :         esnap_channel->blob_id = find.blob_id;
   10039                 :        197 :         existing = RB_INSERT(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
   10040         [ -  + ]:        197 :         if (spdk_unlikely(existing != NULL)) {
   10041                 :            :                 /*
   10042                 :            :                  * This should be unreachable: all modifications to this tree happen on this thread.
   10043                 :            :                  */
   10044                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 "lost race to allocate a channel\n", find.blob_id);
   10045                 :          0 :                 assert(false);
   10046                 :            : 
   10047                 :            :                 bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
   10048                 :            :                 free(esnap_channel);
   10049                 :            : 
   10050                 :            :                 return existing->channel;
   10051                 :            :         }
   10052                 :            : 
   10053                 :        197 :         return esnap_channel->channel;
   10054                 :            : }
   10055                 :            : 
   10056                 :            : static int
   10057                 :      49039 : blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2)
   10058                 :            : {
   10059         [ +  - ]:      49039 :         return (c1->blob_id < c2->blob_id ? -1 : c1->blob_id > c2->blob_id);
   10060                 :            : }
   10061                 :            : 
   10062                 :            : struct blob_esnap_destroy_ctx {
   10063                 :            :         spdk_blob_op_with_handle_complete       cb_fn;
   10064                 :            :         void                                    *cb_arg;
   10065                 :            :         struct spdk_blob                        *blob;
   10066                 :            :         struct spdk_bs_dev                      *back_bs_dev;
   10067                 :            :         bool                                    abort_io;
   10068                 :            : };
   10069                 :            : 
   10070                 :            : static void
   10071                 :        587 : blob_esnap_destroy_channels_done(struct spdk_io_channel_iter *i, int status)
   10072                 :            : {
   10073                 :        587 :         struct blob_esnap_destroy_ctx   *ctx = spdk_io_channel_iter_get_ctx(i);
   10074                 :        587 :         struct spdk_blob                *blob = ctx->blob;
   10075                 :        587 :         struct spdk_blob_store          *bs = blob->bs;
   10076                 :            : 
   10077   [ -  +  -  + ]:        587 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": done destroying channels for this blob\n",
   10078                 :            :                       blob->id);
   10079                 :            : 
   10080         [ +  + ]:        587 :         if (ctx->cb_fn != NULL) {
   10081                 :        535 :                 ctx->cb_fn(ctx->cb_arg, blob, status);
   10082                 :            :         }
   10083                 :        587 :         free(ctx);
   10084                 :            : 
   10085                 :        587 :         bs->esnap_channels_unloading--;
   10086   [ +  -  +  + ]:        587 :         if (bs->esnap_channels_unloading == 0 && bs->esnap_unload_cb_fn != NULL) {
   10087                 :         32 :                 spdk_bs_unload(bs, bs->esnap_unload_cb_fn, bs->esnap_unload_cb_arg);
   10088                 :            :         }
   10089                 :        587 : }
   10090                 :            : 
   10091                 :            : static void
   10092                 :        619 : blob_esnap_destroy_one_channel(struct spdk_io_channel_iter *i)
   10093                 :            : {
   10094                 :        619 :         struct blob_esnap_destroy_ctx   *ctx = spdk_io_channel_iter_get_ctx(i);
   10095                 :        619 :         struct spdk_blob                *blob = ctx->blob;
   10096                 :        619 :         struct spdk_bs_dev              *bs_dev = ctx->back_bs_dev;
   10097                 :        619 :         struct spdk_io_channel          *channel = spdk_io_channel_iter_get_channel(i);
   10098                 :        619 :         struct spdk_bs_channel          *bs_channel = spdk_io_channel_get_ctx(channel);
   10099                 :            :         struct blob_esnap_channel       *esnap_channel;
   10100                 :        619 :         struct blob_esnap_channel       find = {};
   10101                 :            : 
   10102         [ -  + ]:        619 :         assert(spdk_get_thread() == spdk_io_channel_get_thread(channel));
   10103                 :            : 
   10104                 :        619 :         find.blob_id = blob->id;
   10105                 :        619 :         esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
   10106         [ +  + ]:        619 :         if (esnap_channel != NULL) {
   10107   [ -  +  -  + ]:         69 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channel on thread %s\n",
   10108                 :            :                               blob->id, spdk_thread_get_name(spdk_get_thread()));
   10109                 :         69 :                 RB_REMOVE(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
   10110                 :            : 
   10111   [ +  +  +  + ]:         69 :                 if (ctx->abort_io) {
   10112                 :            :                         spdk_bs_user_op_t *op, *tmp;
   10113                 :            : 
   10114         [ -  + ]:         34 :                         TAILQ_FOREACH_SAFE(op, &bs_channel->queued_io, link, tmp) {
   10115         [ #  # ]:          0 :                                 if (op->back_channel == esnap_channel->channel) {
   10116         [ #  # ]:          0 :                                         TAILQ_REMOVE(&bs_channel->queued_io, op, link);
   10117                 :          0 :                                         bs_user_op_abort(op, -EIO);
   10118                 :            :                                 }
   10119                 :            :                         }
   10120                 :            :                 }
   10121                 :            : 
   10122                 :         69 :                 bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
   10123                 :         69 :                 free(esnap_channel);
   10124                 :            :         }
   10125                 :            : 
   10126                 :        619 :         spdk_for_each_channel_continue(i, 0);
   10127                 :        619 : }
   10128                 :            : 
   10129                 :            : /*
   10130                 :            :  * Destroy the channels for a specific blob on each thread with a blobstore channel. This should be
   10131                 :            :  * used when closing an esnap clone blob and after decoupling from the parent.
   10132                 :            :  */
   10133                 :            : static void
   10134                 :       2067 : blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
   10135                 :            :                                    spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
   10136                 :            : {
   10137                 :            :         struct blob_esnap_destroy_ctx   *ctx;
   10138                 :            : 
   10139   [ +  +  +  + ]:       2067 :         if (!blob_is_esnap_clone(blob) || blob->back_bs_dev == NULL) {
   10140         [ +  - ]:       1480 :                 if (cb_fn != NULL) {
   10141                 :       1480 :                         cb_fn(cb_arg, blob, 0);
   10142                 :            :                 }
   10143                 :       1480 :                 return;
   10144                 :            :         }
   10145                 :            : 
   10146                 :        587 :         ctx = calloc(1, sizeof(*ctx));
   10147         [ -  + ]:        587 :         if (ctx == NULL) {
   10148         [ #  # ]:          0 :                 if (cb_fn != NULL) {
   10149                 :          0 :                         cb_fn(cb_arg, blob, -ENOMEM);
   10150                 :            :                 }
   10151                 :          0 :                 return;
   10152                 :            :         }
   10153                 :        587 :         ctx->cb_fn = cb_fn;
   10154                 :        587 :         ctx->cb_arg = cb_arg;
   10155                 :        587 :         ctx->blob = blob;
   10156                 :        587 :         ctx->back_bs_dev = blob->back_bs_dev;
   10157                 :        587 :         ctx->abort_io = abort_io;
   10158                 :            : 
   10159   [ -  +  -  + ]:        587 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channels for this blob\n",
   10160                 :            :                       blob->id);
   10161                 :            : 
   10162                 :        587 :         blob->bs->esnap_channels_unloading++;
   10163                 :        587 :         spdk_for_each_channel(blob->bs, blob_esnap_destroy_one_channel, ctx,
   10164                 :            :                               blob_esnap_destroy_channels_done);
   10165                 :            : }
   10166                 :            : 
   10167                 :            : /*
   10168                 :            :  * Destroy all bs_dev channels on a specific blobstore channel. This should be used when a
   10169                 :            :  * bs_channel is destroyed.
   10170                 :            :  */
   10171                 :            : static void
   10172                 :      11611 : blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch)
   10173                 :            : {
   10174                 :            :         struct blob_esnap_channel *esnap_channel, *esnap_channel_tmp;
   10175                 :            : 
   10176         [ -  + ]:      11611 :         assert(spdk_get_thread() == spdk_io_channel_get_thread(spdk_io_channel_from_ctx(ch)));
   10177                 :            : 
   10178   [ -  +  -  + ]:      11611 :         SPDK_DEBUGLOG(blob_esnap, "destroying channels on thread %s\n",
   10179                 :            :                       spdk_thread_get_name(spdk_get_thread()));
   10180   [ +  +  +  - ]:      11739 :         RB_FOREACH_SAFE(esnap_channel, blob_esnap_channel_tree, &ch->esnap_channels,
   10181                 :            :                         esnap_channel_tmp) {
   10182   [ -  +  -  + ]:        128 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64
   10183                 :            :                               ": destroying one channel in thread %s\n",
   10184                 :            :                               esnap_channel->blob_id, spdk_thread_get_name(spdk_get_thread()));
   10185                 :        128 :                 RB_REMOVE(blob_esnap_channel_tree, &ch->esnap_channels, esnap_channel);
   10186                 :        128 :                 spdk_put_io_channel(esnap_channel->channel);
   10187                 :        128 :                 free(esnap_channel);
   10188                 :            :         }
   10189   [ -  +  -  + ]:      11611 :         SPDK_DEBUGLOG(blob_esnap, "done destroying channels on thread %s\n",
   10190                 :            :                       spdk_thread_get_name(spdk_get_thread()));
   10191                 :      11611 : }
   10192                 :            : 
   10193                 :            : static void
   10194                 :        121 : blob_set_back_bs_dev_done(void *_ctx, int bserrno)
   10195                 :            : {
   10196                 :        121 :         struct set_bs_dev_ctx   *ctx = _ctx;
   10197                 :            : 
   10198         [ -  + ]:        121 :         if (bserrno != 0) {
   10199                 :            :                 /* Even though the unfreeze failed, the update may have succeed. */
   10200                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": unfreeze failed with error %d\n", ctx->blob->id,
   10201                 :            :                             bserrno);
   10202                 :            :         }
   10203                 :        121 :         ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
   10204                 :        121 :         free(ctx);
   10205                 :        121 : }
   10206                 :            : 
   10207                 :            : static void
   10208                 :        121 : blob_frozen_set_back_bs_dev(void *_ctx, struct spdk_blob *blob, int bserrno)
   10209                 :            : {
   10210                 :        121 :         struct set_bs_dev_ctx   *ctx = _ctx;
   10211                 :            :         int rc;
   10212                 :            : 
   10213         [ -  + ]:        121 :         if (bserrno != 0) {
   10214                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to release old back_bs_dev with error %d\n",
   10215                 :            :                             blob->id, bserrno);
   10216                 :          0 :                 ctx->bserrno = bserrno;
   10217                 :          0 :                 blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
   10218                 :          0 :                 return;
   10219                 :            :         }
   10220                 :            : 
   10221         [ +  - ]:        121 :         if (blob->back_bs_dev != NULL) {
   10222                 :        121 :                 blob->back_bs_dev->destroy(blob->back_bs_dev);
   10223                 :        121 :                 blob->back_bs_dev = NULL;
   10224                 :            :         }
   10225                 :            : 
   10226         [ +  + ]:        121 :         if (ctx->parent_refs_cb_fn) {
   10227                 :         86 :                 rc = ctx->parent_refs_cb_fn(blob, ctx->parent_refs_cb_arg);
   10228         [ -  + ]:         86 :                 if (rc != 0) {
   10229                 :          0 :                         ctx->bserrno = rc;
   10230                 :          0 :                         blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
   10231                 :          0 :                         return;
   10232                 :            :                 }
   10233                 :            :         }
   10234                 :            : 
   10235                 :        121 :         SPDK_NOTICELOG("blob 0x%" PRIx64 ": hotplugged back_bs_dev\n", blob->id);
   10236                 :        121 :         blob->back_bs_dev = ctx->back_bs_dev;
   10237                 :        121 :         ctx->bserrno = 0;
   10238                 :            : 
   10239                 :        121 :         blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
   10240                 :            : }
   10241                 :            : 
   10242                 :            : static void
   10243                 :        121 : blob_set_back_bs_dev_frozen(void *_ctx, int bserrno)
   10244                 :            : {
   10245                 :        121 :         struct set_bs_dev_ctx   *ctx = _ctx;
   10246                 :        121 :         struct spdk_blob        *blob = ctx->blob;
   10247                 :            : 
   10248         [ -  + ]:        121 :         if (bserrno != 0) {
   10249                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to freeze with error %d\n", blob->id,
   10250                 :            :                             bserrno);
   10251                 :          0 :                 ctx->cb_fn(ctx->cb_arg, bserrno);
   10252                 :          0 :                 free(ctx);
   10253                 :          0 :                 return;
   10254                 :            :         }
   10255                 :            : 
   10256                 :            :         /*
   10257                 :            :          * This does not prevent future reads from the esnap device because any future IO will
   10258                 :            :          * lazily create a new esnap IO channel.
   10259                 :            :          */
   10260                 :        121 :         blob_esnap_destroy_bs_dev_channels(blob, true, blob_frozen_set_back_bs_dev, ctx);
   10261                 :            : }
   10262                 :            : 
   10263                 :            : void
   10264                 :         35 : spdk_blob_set_esnap_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
   10265                 :            :                            spdk_blob_op_complete cb_fn, void *cb_arg)
   10266                 :            : {
   10267         [ -  + ]:         35 :         if (!blob_is_esnap_clone(blob)) {
   10268                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
   10269                 :          0 :                 cb_fn(cb_arg, -EINVAL);
   10270                 :          0 :                 return;
   10271                 :            :         }
   10272                 :            : 
   10273                 :         35 :         blob_set_back_bs_dev(blob, back_bs_dev, NULL, NULL, cb_fn, cb_arg);
   10274                 :            : }
   10275                 :            : 
   10276                 :            : struct spdk_bs_dev *
   10277                 :        168 : spdk_blob_get_esnap_bs_dev(const struct spdk_blob *blob)
   10278                 :            : {
   10279         [ -  + ]:        168 :         if (!blob_is_esnap_clone(blob)) {
   10280                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
   10281                 :          0 :                 return NULL;
   10282                 :            :         }
   10283                 :            : 
   10284                 :        168 :         return blob->back_bs_dev;
   10285                 :            : }
   10286                 :            : 
   10287                 :            : bool
   10288                 :        898 : spdk_blob_is_degraded(const struct spdk_blob *blob)
   10289                 :            : {
   10290   [ +  +  +  + ]:        898 :         if (blob->bs->dev->is_degraded != NULL && blob->bs->dev->is_degraded(blob->bs->dev)) {
   10291                 :         16 :                 return true;
   10292                 :            :         }
   10293   [ +  +  +  + ]:        882 :         if (blob->back_bs_dev == NULL || blob->back_bs_dev->is_degraded == NULL) {
   10294                 :        703 :                 return false;
   10295                 :            :         }
   10296                 :            : 
   10297                 :        179 :         return blob->back_bs_dev->is_degraded(blob->back_bs_dev);
   10298                 :            : }
   10299                 :            : 
   10300                 :       2345 : SPDK_LOG_REGISTER_COMPONENT(blob)
   10301                 :       2345 : SPDK_LOG_REGISTER_COMPONENT(blob_esnap)
   10302                 :            : 
   10303                 :            : 
   10304                 :       4511 : SPDK_TRACE_REGISTER_FN(blob_trace, "blob", TRACE_GROUP_BLOB)
   10305                 :            : {
   10306                 :       2166 :         struct spdk_trace_tpoint_opts opts[] = {
   10307                 :            :                 {
   10308                 :            :                         "BLOB_PROCESS_START", TRACE_BLOB_PROCESS_START,
   10309                 :            :                         OWNER_TYPE_NONE, OBJECT_BLOB_CB_ARG, 1,
   10310                 :            :                         {
   10311                 :            :                                 { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }
   10312                 :            :                         }
   10313                 :            :                 },
   10314                 :            :                 {
   10315                 :            :                         "BLOB_PROCESS_COMPLETE", TRACE_BLOB_PROCESS_COMPLETE,
   10316                 :            :                         OWNER_TYPE_NONE, OBJECT_BLOB_CB_ARG, 0,
   10317                 :            :                         {
   10318                 :            :                                 { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }
   10319                 :            :                         }
   10320                 :            :                 },
   10321                 :            :         };
   10322                 :            : 
   10323                 :            : 
   10324                 :       2166 :         spdk_trace_register_object(OBJECT_BLOB_CB_ARG, 'a');
   10325                 :       2166 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
   10326                 :       2166 :         spdk_trace_tpoint_register_relation(TRACE_BDEV_IO_START, OBJECT_BLOB_CB_ARG, 1);
   10327                 :       2166 :         spdk_trace_tpoint_register_relation(TRACE_BDEV_IO_DONE, OBJECT_BLOB_CB_ARG, 0);
   10328                 :       2166 : }

Generated by: LCOV version 1.14