LCOV - code coverage report
Current view: top level - spdk/lib/ftl - ftl_nv_cache.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1028 1326 77.5 %
Date: 2024-07-15 21:16:48 Functions: 100 108 92.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 351 638 55.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2022 Intel Corporation.
       3                 :            :  *   Copyright 2023 Solidigm All Rights Reserved
       4                 :            :  *   All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : 
       8                 :            : #include "spdk/bdev.h"
       9                 :            : #include "spdk/bdev_module.h"
      10                 :            : #include "spdk/ftl.h"
      11                 :            : #include "spdk/string.h"
      12                 :            : 
      13                 :            : #include "ftl_nv_cache.h"
      14                 :            : #include "ftl_nv_cache_io.h"
      15                 :            : #include "ftl_core.h"
      16                 :            : #include "ftl_band.h"
      17                 :            : #include "utils/ftl_addr_utils.h"
      18                 :            : #include "mngt/ftl_mngt.h"
      19                 :            : 
      20                 :            : static inline uint64_t nvc_data_blocks(struct ftl_nv_cache *nv_cache) __attribute__((unused));
      21                 :            : static struct ftl_nv_cache_compactor *compactor_alloc(struct spdk_ftl_dev *dev);
      22                 :            : static void compactor_free(struct spdk_ftl_dev *dev, struct ftl_nv_cache_compactor *compactor);
      23                 :            : static void compaction_process_ftl_done(struct ftl_rq *rq);
      24                 :            : static void compaction_process_read_entry(void *arg);
      25                 :            : static void ftl_property_dump_cache_dev(struct spdk_ftl_dev *dev,
      26                 :            :                                         const struct ftl_property *property,
      27                 :            :                                         struct spdk_json_write_ctx *w);
      28                 :            : 
      29                 :            : static inline void
      30                 :        330 : nvc_validate_md(struct ftl_nv_cache *nv_cache,
      31                 :            :                 struct ftl_nv_cache_chunk_md *chunk_md)
      32                 :            : {
      33                 :        330 :         struct ftl_md *md = nv_cache->md;
      34                 :        330 :         void *buffer = ftl_md_get_buffer(md);
      35                 :        330 :         uint64_t size = ftl_md_get_buffer_size(md);
      36                 :        330 :         void *ptr = chunk_md;
      37                 :            : 
      38         [ -  + ]:        330 :         if (ptr < buffer) {
      39                 :          0 :                 ftl_abort();
      40                 :            :         }
      41                 :            : 
      42                 :        330 :         ptr += sizeof(*chunk_md);
      43         [ -  + ]:        330 :         if (ptr > buffer + size) {
      44                 :          0 :                 ftl_abort();
      45                 :            :         }
      46                 :        330 : }
      47                 :            : 
      48                 :            : static inline uint64_t
      49                 :         61 : nvc_data_offset(struct ftl_nv_cache *nv_cache)
      50                 :            : {
      51                 :         61 :         return 0;
      52                 :            : }
      53                 :            : 
      54                 :            : static inline uint64_t
      55                 :         22 : nvc_data_blocks(struct ftl_nv_cache *nv_cache)
      56                 :            : {
      57                 :         22 :         return nv_cache->chunk_blocks * nv_cache->chunk_count;
      58                 :            : }
      59                 :            : 
      60                 :            : size_t
      61                 :         44 : ftl_nv_cache_chunk_tail_md_num_blocks(const struct ftl_nv_cache *nv_cache)
      62                 :            : {
      63                 :         44 :         struct spdk_ftl_dev *dev =  SPDK_CONTAINEROF(nv_cache,
      64                 :            :                                     struct spdk_ftl_dev, nv_cache);
      65                 :         44 :         return spdk_divide_round_up(dev->layout.nvc.chunk_data_blocks * dev->layout.l2p.addr_size,
      66                 :            :                                     FTL_BLOCK_SIZE);
      67                 :            : }
      68                 :            : 
      69                 :            : static size_t
      70                 :         22 : nv_cache_p2l_map_pool_elem_size(const struct ftl_nv_cache *nv_cache)
      71                 :            : {
      72                 :            :         /* Map pool element holds the whole tail md */
      73                 :         22 :         return nv_cache->tail_md_chunk_blocks * FTL_BLOCK_SIZE;
      74                 :            : }
      75                 :            : 
      76                 :            : static uint64_t
      77                 :         71 : get_chunk_idx(struct ftl_nv_cache_chunk *chunk)
      78                 :            : {
      79                 :         71 :         struct ftl_nv_cache_chunk *first_chunk = chunk->nv_cache->chunks;
      80                 :            : 
      81         [ -  + ]:         71 :         return (chunk->offset - first_chunk->offset) / chunk->nv_cache->chunk_blocks;
      82                 :            : }
      83                 :            : 
      84                 :            : static void
      85                 :         39 : ftl_nv_cache_init_update_limits(struct spdk_ftl_dev *dev)
      86                 :            : {
      87                 :         39 :         struct ftl_nv_cache *nvc = &dev->nv_cache;
      88                 :         39 :         uint64_t usable_chunks = nvc->chunk_count - nvc->chunk_inactive_count;
      89                 :            : 
      90                 :            :         /* Start compaction when full chunks exceed given % of entire active chunks */
      91                 :         39 :         nvc->chunk_compaction_threshold = usable_chunks *
      92                 :         39 :                                           dev->conf.nv_cache.chunk_compaction_threshold /
      93                 :            :                                           100;
      94                 :            : 
      95                 :         39 :         nvc->throttle.interval_tsc = FTL_NV_CACHE_THROTTLE_INTERVAL_MS *
      96                 :         39 :                                      (spdk_get_ticks_hz() / 1000);
      97                 :            : 
      98                 :         39 :         nvc->chunk_free_target = spdk_divide_round_up(usable_chunks *
      99                 :         39 :                                  dev->conf.nv_cache.chunk_free_target,
     100                 :            :                                  100);
     101                 :         39 : }
     102                 :            : 
     103                 :            : struct nvc_scrub_ctx {
     104                 :            :         uint64_t chunk_no;
     105                 :            :         nvc_scrub_cb cb;
     106                 :            :         void *cb_ctx;
     107                 :            : 
     108                 :            :         struct ftl_layout_region reg_chunk;
     109                 :            :         struct ftl_md *md_chunk;
     110                 :            : };
     111                 :            : 
     112                 :            : static int
     113                 :         25 : nvc_scrub_find_next_chunk(struct spdk_ftl_dev *dev, struct nvc_scrub_ctx *scrub_ctx)
     114                 :            : {
     115         [ +  + ]:         30 :         while (scrub_ctx->chunk_no < dev->layout.nvc.chunk_count) {
     116         [ +  + ]:         25 :                 if (dev->nv_cache.nvc_type->ops.is_chunk_active(dev, scrub_ctx->reg_chunk.current.offset)) {
     117                 :         20 :                         return 0;
     118                 :            :                 }
     119                 :            : 
     120                 :            :                 /* Move the dummy region along with the active chunk */
     121                 :          5 :                 scrub_ctx->reg_chunk.current.offset += dev->layout.nvc.chunk_data_blocks;
     122                 :          5 :                 scrub_ctx->chunk_no++;
     123                 :            :         }
     124                 :          5 :         return -ENOENT;
     125                 :            : }
     126                 :            : 
     127                 :            : static void
     128                 :         20 : nvc_scrub_clear_cb(struct spdk_ftl_dev *dev, struct ftl_md *md, int status)
     129                 :            : {
     130                 :         20 :         struct nvc_scrub_ctx *scrub_ctx = md->owner.cb_ctx;
     131                 :         20 :         union ftl_md_vss vss;
     132                 :            : 
     133                 :            :         /* Move to the next chunk */
     134                 :         20 :         scrub_ctx->chunk_no++;
     135                 :         20 :         scrub_ctx->reg_chunk.current.offset += dev->layout.nvc.chunk_data_blocks;
     136                 :            : 
     137         [ +  - ]:         20 :         FTL_DEBUGLOG(dev, "Scrub progress: %"PRIu64"/%"PRIu64" chunks\n",
     138                 :            :                      scrub_ctx->chunk_no, dev->layout.nvc.chunk_count);
     139                 :            : 
     140   [ +  -  +  + ]:         20 :         if (status || nvc_scrub_find_next_chunk(dev, scrub_ctx)) {
     141                 :            :                 /* IO error or no more active chunks found. Scrubbing finished. */
     142                 :          5 :                 scrub_ctx->cb(dev, scrub_ctx->cb_ctx, status);
     143                 :          5 :                 ftl_md_destroy(scrub_ctx->md_chunk, 0);
     144                 :          5 :                 free(scrub_ctx);
     145                 :          5 :                 return;
     146                 :            :         }
     147                 :            : 
     148                 :            :         /* Scrub the next chunk */
     149                 :         15 :         vss.version.md_version = 0;
     150                 :         15 :         vss.nv_cache.lba = FTL_ADDR_INVALID;
     151                 :            : 
     152                 :         15 :         scrub_ctx->md_chunk->cb = nvc_scrub_clear_cb;
     153                 :         15 :         scrub_ctx->md_chunk->owner.cb_ctx = scrub_ctx;
     154                 :            : 
     155                 :         15 :         ftl_md_clear(scrub_ctx->md_chunk, 0, &vss);
     156                 :            : }
     157                 :            : 
     158                 :            : void
     159                 :          5 : ftl_nv_cache_scrub(struct spdk_ftl_dev *dev, nvc_scrub_cb cb, void *cb_ctx)
     160                 :            : {
     161                 :          5 :         struct nvc_scrub_ctx *scrub_ctx = calloc(1, sizeof(*scrub_ctx));
     162                 :          5 :         union ftl_md_vss vss;
     163                 :            : 
     164         [ -  + ]:          5 :         if (!scrub_ctx) {
     165                 :          0 :                 cb(dev, cb_ctx, -ENOMEM);
     166                 :          0 :                 return;
     167                 :            :         }
     168                 :            : 
     169                 :          5 :         scrub_ctx->cb = cb;
     170                 :          5 :         scrub_ctx->cb_ctx = cb_ctx;
     171                 :            : 
     172                 :            :         /* Setup a dummy region for the first chunk */
     173                 :          5 :         scrub_ctx->reg_chunk.name = ftl_md_region_name(FTL_LAYOUT_REGION_TYPE_DATA_NVC);
     174                 :          5 :         scrub_ctx->reg_chunk.type = FTL_LAYOUT_REGION_TYPE_DATA_NVC;
     175                 :          5 :         scrub_ctx->reg_chunk.mirror_type = FTL_LAYOUT_REGION_TYPE_INVALID;
     176                 :          5 :         scrub_ctx->reg_chunk.current.version = 0;
     177                 :          5 :         scrub_ctx->reg_chunk.current.offset = 0;
     178                 :          5 :         scrub_ctx->reg_chunk.current.blocks = dev->layout.nvc.chunk_data_blocks;
     179                 :          5 :         scrub_ctx->reg_chunk.entry_size = FTL_BLOCK_SIZE;
     180                 :          5 :         scrub_ctx->reg_chunk.num_entries = dev->layout.nvc.chunk_data_blocks;
     181                 :          5 :         scrub_ctx->reg_chunk.vss_blksz = dev->nv_cache.md_size;
     182                 :          5 :         scrub_ctx->reg_chunk.bdev_desc = dev->nv_cache.bdev_desc;
     183                 :          5 :         scrub_ctx->reg_chunk.ioch = dev->nv_cache.cache_ioch;
     184                 :            : 
     185                 :            :         /* Setup an MD object for the region */
     186                 :          5 :         scrub_ctx->md_chunk = ftl_md_create(dev, scrub_ctx->reg_chunk.current.blocks,
     187                 :            :                                             scrub_ctx->reg_chunk.vss_blksz, scrub_ctx->reg_chunk.name, FTL_MD_CREATE_NO_MEM,
     188                 :          5 :                                             &scrub_ctx->reg_chunk);
     189                 :            : 
     190         [ -  + ]:          5 :         if (!scrub_ctx->md_chunk) {
     191                 :          0 :                 free(scrub_ctx);
     192                 :          0 :                 cb(dev, cb_ctx, -ENOMEM);
     193                 :          0 :                 return;
     194                 :            :         }
     195                 :            : 
     196         [ -  + ]:          5 :         if (nvc_scrub_find_next_chunk(dev, scrub_ctx)) {
     197                 :            :                 /* No active chunks found */
     198                 :          0 :                 ftl_md_destroy(scrub_ctx->md_chunk, 0);
     199                 :          0 :                 free(scrub_ctx);
     200                 :          0 :                 cb(dev, cb_ctx, -ENOENT);
     201                 :          0 :                 return;
     202                 :            :         }
     203                 :            : 
     204                 :            :         /* Scrub the first chunk */
     205                 :          5 :         vss.version.md_version = 0;
     206                 :          5 :         vss.nv_cache.lba = FTL_ADDR_INVALID;
     207                 :            : 
     208                 :          5 :         scrub_ctx->md_chunk->cb = nvc_scrub_clear_cb;
     209                 :          5 :         scrub_ctx->md_chunk->owner.cb_ctx = scrub_ctx;
     210                 :            : 
     211                 :          5 :         ftl_md_clear(scrub_ctx->md_chunk, 0, &vss);
     212                 :          5 :         return;
     213                 :            : }
     214                 :            : 
     215                 :            : int
     216                 :         22 : ftl_nv_cache_init(struct spdk_ftl_dev *dev)
     217                 :            : {
     218                 :         22 :         struct ftl_nv_cache *nv_cache = &dev->nv_cache;
     219                 :            :         struct ftl_nv_cache_chunk *chunk;
     220                 :            :         struct ftl_nv_cache_chunk_md *md;
     221                 :            :         struct ftl_nv_cache_compactor *compactor;
     222                 :            :         uint64_t i, offset;
     223                 :            : 
     224                 :         22 :         nv_cache->halt = true;
     225                 :            : 
     226                 :         22 :         nv_cache->md = dev->layout.md[FTL_LAYOUT_REGION_TYPE_NVC_MD];
     227         [ -  + ]:         22 :         if (!nv_cache->md) {
     228         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "No NV cache metadata object\n");
     229                 :          0 :                 return -1;
     230                 :            :         }
     231                 :            : 
     232                 :         22 :         nv_cache->md_pool = ftl_mempool_create(dev->conf.user_io_pool_size,
     233                 :         22 :                                                nv_cache->md_size * dev->xfer_size,
     234                 :            :                                                FTL_BLOCK_SIZE, SPDK_ENV_SOCKET_ID_ANY);
     235         [ -  + ]:         22 :         if (!nv_cache->md_pool) {
     236         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Failed to initialize NV cache metadata pool\n");
     237                 :          0 :                 return -1;
     238                 :            :         }
     239                 :            : 
     240                 :            :         /*
     241                 :            :          * Initialize chunk info
     242                 :            :          */
     243                 :         22 :         nv_cache->chunk_blocks = dev->layout.nvc.chunk_data_blocks;
     244                 :         22 :         nv_cache->chunk_count = dev->layout.nvc.chunk_count;
     245                 :         22 :         nv_cache->tail_md_chunk_blocks = ftl_nv_cache_chunk_tail_md_num_blocks(nv_cache);
     246                 :            : 
     247                 :            :         /* Allocate chunks */
     248                 :         22 :         nv_cache->chunks = calloc(nv_cache->chunk_count,
     249                 :            :                                   sizeof(nv_cache->chunks[0]));
     250         [ -  + ]:         22 :         if (!nv_cache->chunks) {
     251         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Failed to initialize NV cache chunks\n");
     252                 :          0 :                 return -1;
     253                 :            :         }
     254                 :            : 
     255                 :         22 :         TAILQ_INIT(&nv_cache->chunk_free_list);
     256                 :         22 :         TAILQ_INIT(&nv_cache->chunk_open_list);
     257                 :         22 :         TAILQ_INIT(&nv_cache->chunk_full_list);
     258                 :         22 :         TAILQ_INIT(&nv_cache->chunk_comp_list);
     259                 :         22 :         TAILQ_INIT(&nv_cache->chunk_inactive_list);
     260                 :         22 :         TAILQ_INIT(&nv_cache->needs_free_persist_list);
     261                 :            : 
     262                 :            :         /* First chunk metadata */
     263                 :         22 :         md = ftl_md_get_buffer(nv_cache->md);
     264         [ -  + ]:         22 :         if (!md) {
     265         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "No NV cache metadata\n");
     266                 :          0 :                 return -1;
     267                 :            :         }
     268                 :            : 
     269                 :         22 :         chunk = nv_cache->chunks;
     270                 :         22 :         offset = nvc_data_offset(nv_cache);
     271         [ +  + ]:        132 :         for (i = 0; i < nv_cache->chunk_count; i++, chunk++, md++) {
     272                 :        110 :                 chunk->nv_cache = nv_cache;
     273                 :        110 :                 chunk->md = md;
     274                 :        110 :                 chunk->md->version = FTL_NVC_VERSION_CURRENT;
     275                 :        110 :                 nvc_validate_md(nv_cache, md);
     276                 :        110 :                 chunk->offset = offset;
     277                 :        110 :                 offset += nv_cache->chunk_blocks;
     278                 :            : 
     279         [ +  + ]:        110 :                 if (nv_cache->nvc_type->ops.is_chunk_active(dev, chunk->offset)) {
     280                 :         88 :                         nv_cache->chunk_free_count++;
     281                 :         88 :                         TAILQ_INSERT_TAIL(&nv_cache->chunk_free_list, chunk, entry);
     282                 :            :                 } else {
     283                 :         22 :                         chunk->md->state = FTL_CHUNK_STATE_INACTIVE;
     284                 :         22 :                         nv_cache->chunk_inactive_count++;
     285                 :         22 :                         TAILQ_INSERT_TAIL(&nv_cache->chunk_inactive_list, chunk, entry);
     286                 :            :                 }
     287                 :            :         }
     288         [ -  + ]:         22 :         assert(nv_cache->chunk_free_count + nv_cache->chunk_inactive_count == nv_cache->chunk_count);
     289         [ -  + ]:         22 :         assert(offset <= nvc_data_offset(nv_cache) + nvc_data_blocks(nv_cache));
     290                 :            : 
     291                 :         22 :         TAILQ_INIT(&nv_cache->compactor_list);
     292         [ +  + ]:        198 :         for (i = 0; i < FTL_NV_CACHE_NUM_COMPACTORS; i++) {
     293                 :        176 :                 compactor = compactor_alloc(dev);
     294                 :            : 
     295         [ -  + ]:        176 :                 if (!compactor) {
     296         [ #  # ]:          0 :                         FTL_ERRLOG(dev, "Cannot allocate compaction process\n");
     297                 :          0 :                         return -1;
     298                 :            :                 }
     299                 :            : 
     300                 :        176 :                 TAILQ_INSERT_TAIL(&nv_cache->compactor_list, compactor, entry);
     301                 :            :         }
     302                 :            : 
     303                 :            : #define FTL_MAX_OPEN_CHUNKS 2
     304                 :         22 :         nv_cache->p2l_pool = ftl_mempool_create(FTL_MAX_OPEN_CHUNKS,
     305                 :            :                                                 nv_cache_p2l_map_pool_elem_size(nv_cache),
     306                 :            :                                                 FTL_BLOCK_SIZE,
     307                 :            :                                                 SPDK_ENV_SOCKET_ID_ANY);
     308         [ -  + ]:         22 :         if (!nv_cache->p2l_pool) {
     309                 :          0 :                 return -ENOMEM;
     310                 :            :         }
     311                 :            : 
     312                 :            :         /* One entry per open chunk */
     313                 :         22 :         nv_cache->chunk_md_pool = ftl_mempool_create(FTL_MAX_OPEN_CHUNKS,
     314                 :            :                                   sizeof(struct ftl_nv_cache_chunk_md),
     315                 :            :                                   FTL_BLOCK_SIZE,
     316                 :            :                                   SPDK_ENV_SOCKET_ID_ANY);
     317         [ -  + ]:         22 :         if (!nv_cache->chunk_md_pool) {
     318                 :          0 :                 return -ENOMEM;
     319                 :            :         }
     320                 :            : 
     321                 :            :         /* Each compactor can be reading a different chunk which it needs to switch state to free to at the end,
     322                 :            :          * plus one backup each for high invalidity chunks processing (if there's a backlog of chunks with extremely
     323                 :            :          * small, even 0, validity then they can be processed by the compactors quickly and trigger a lot of updates
     324                 :            :          * to free state at once) */
     325                 :         22 :         nv_cache->free_chunk_md_pool = ftl_mempool_create(2 * FTL_NV_CACHE_NUM_COMPACTORS,
     326                 :            :                                        sizeof(struct ftl_nv_cache_chunk_md),
     327                 :            :                                        FTL_BLOCK_SIZE,
     328                 :            :                                        SPDK_ENV_SOCKET_ID_ANY);
     329         [ -  + ]:         22 :         if (!nv_cache->free_chunk_md_pool) {
     330                 :          0 :                 return -ENOMEM;
     331                 :            :         }
     332                 :            : 
     333                 :         22 :         ftl_nv_cache_init_update_limits(dev);
     334                 :         22 :         ftl_property_register(dev, "cache_device", NULL, 0, NULL, NULL, ftl_property_dump_cache_dev, NULL,
     335                 :            :                               NULL, true);
     336                 :         22 :         return 0;
     337                 :            : }
     338                 :            : 
     339                 :            : void
     340                 :         22 : ftl_nv_cache_deinit(struct spdk_ftl_dev *dev)
     341                 :            : {
     342                 :         22 :         struct ftl_nv_cache *nv_cache = &dev->nv_cache;
     343                 :            :         struct ftl_nv_cache_compactor *compactor;
     344                 :            : 
     345         [ +  + ]:        198 :         while (!TAILQ_EMPTY(&nv_cache->compactor_list)) {
     346                 :        176 :                 compactor = TAILQ_FIRST(&nv_cache->compactor_list);
     347         [ +  + ]:        176 :                 TAILQ_REMOVE(&nv_cache->compactor_list, compactor, entry);
     348                 :            : 
     349                 :        176 :                 compactor_free(dev, compactor);
     350                 :            :         }
     351                 :            : 
     352                 :         22 :         ftl_mempool_destroy(nv_cache->md_pool);
     353                 :         22 :         ftl_mempool_destroy(nv_cache->p2l_pool);
     354                 :         22 :         ftl_mempool_destroy(nv_cache->chunk_md_pool);
     355                 :         22 :         ftl_mempool_destroy(nv_cache->free_chunk_md_pool);
     356                 :         22 :         nv_cache->md_pool = NULL;
     357                 :         22 :         nv_cache->p2l_pool = NULL;
     358                 :         22 :         nv_cache->chunk_md_pool = NULL;
     359                 :         22 :         nv_cache->free_chunk_md_pool = NULL;
     360                 :            : 
     361                 :         22 :         free(nv_cache->chunks);
     362                 :         22 :         nv_cache->chunks = NULL;
     363                 :         22 : }
     364                 :            : 
     365                 :            : static uint64_t
     366                 :    1130125 : chunk_get_free_space(struct ftl_nv_cache *nv_cache,
     367                 :            :                      struct ftl_nv_cache_chunk *chunk)
     368                 :            : {
     369         [ -  + ]:    1130125 :         assert(chunk->md->write_pointer + nv_cache->tail_md_chunk_blocks <=
     370                 :            :                nv_cache->chunk_blocks);
     371                 :    1130125 :         return nv_cache->chunk_blocks - chunk->md->write_pointer -
     372                 :    1130125 :                nv_cache->tail_md_chunk_blocks;
     373                 :            : }
     374                 :            : 
     375                 :            : static bool
     376                 :    1130111 : chunk_is_closed(struct ftl_nv_cache_chunk *chunk)
     377                 :            : {
     378                 :    1130111 :         return chunk->md->write_pointer == chunk->nv_cache->chunk_blocks;
     379                 :            : }
     380                 :            : 
     381                 :            : static void ftl_chunk_close(struct ftl_nv_cache_chunk *chunk);
     382                 :            : 
     383                 :            : static uint64_t
     384                 :    1130111 : ftl_nv_cache_get_wr_buffer(struct ftl_nv_cache *nv_cache, struct ftl_io *io)
     385                 :            : {
     386                 :    1130111 :         uint64_t address = FTL_LBA_INVALID;
     387                 :    1130111 :         uint64_t num_blocks = io->num_blocks;
     388                 :            :         uint64_t free_space;
     389                 :            :         struct ftl_nv_cache_chunk *chunk;
     390                 :            : 
     391                 :            :         do {
     392                 :    1130115 :                 chunk = nv_cache->chunk_current;
     393                 :            :                 /* Chunk has been closed so pick new one */
     394   [ +  +  +  + ]:    1130115 :                 if (chunk && chunk_is_closed(chunk))  {
     395                 :          1 :                         chunk = NULL;
     396                 :            :                 }
     397                 :            : 
     398         [ +  + ]:    1130115 :                 if (!chunk) {
     399                 :         15 :                         chunk = TAILQ_FIRST(&nv_cache->chunk_open_list);
     400   [ +  -  +  - ]:         15 :                         if (chunk && chunk->md->state == FTL_CHUNK_STATE_OPEN) {
     401         [ +  + ]:         15 :                                 TAILQ_REMOVE(&nv_cache->chunk_open_list, chunk, entry);
     402                 :         15 :                                 nv_cache->chunk_current = chunk;
     403                 :            :                         } else {
     404                 :            :                                 break;
     405                 :            :                         }
     406                 :            :                 }
     407                 :            : 
     408                 :    1130115 :                 free_space = chunk_get_free_space(nv_cache, chunk);
     409                 :            : 
     410         [ +  + ]:    1130115 :                 if (free_space >= num_blocks) {
     411                 :            :                         /* Enough space in chunk */
     412                 :            : 
     413                 :            :                         /* Calculate address in NV cache */
     414                 :    1130111 :                         address = chunk->offset + chunk->md->write_pointer;
     415                 :            : 
     416                 :            :                         /* Set chunk in IO */
     417                 :    1130111 :                         io->nv_cache_chunk = chunk;
     418                 :            : 
     419                 :            :                         /* Move write pointer */
     420                 :    1130111 :                         chunk->md->write_pointer += num_blocks;
     421                 :    1130111 :                         break;
     422                 :            :                 }
     423                 :            : 
     424                 :            :                 /* Not enough space in nv_cache_chunk */
     425                 :          4 :                 nv_cache->chunk_current = NULL;
     426                 :            : 
     427         [ +  - ]:          4 :                 if (0 == free_space) {
     428                 :          4 :                         continue;
     429                 :            :                 }
     430                 :            : 
     431                 :          0 :                 chunk->md->blocks_skipped = free_space;
     432                 :          0 :                 chunk->md->blocks_written += free_space;
     433                 :          0 :                 chunk->md->write_pointer += free_space;
     434                 :            : 
     435         [ #  # ]:          0 :                 if (chunk->md->blocks_written == chunk_tail_md_offset(nv_cache)) {
     436                 :          0 :                         ftl_chunk_close(chunk);
     437                 :            :                 }
     438                 :            :         } while (1);
     439                 :            : 
     440                 :    1130111 :         return address;
     441                 :            : }
     442                 :            : 
     443                 :            : void
     444                 :    1130111 : ftl_nv_cache_fill_md(struct ftl_io *io)
     445                 :            : {
     446                 :    1130111 :         struct ftl_nv_cache_chunk *chunk = io->nv_cache_chunk;
     447                 :            :         uint64_t i;
     448                 :    1130111 :         union ftl_md_vss *metadata = io->md;
     449                 :    1130111 :         uint64_t lba = ftl_io_get_lba(io, 0);
     450                 :            : 
     451         [ +  + ]:    2943082 :         for (i = 0; i < io->num_blocks; ++i, lba++, metadata++) {
     452                 :    1812971 :                 metadata->nv_cache.lba = lba;
     453                 :    1812971 :                 metadata->nv_cache.seq_id = chunk->md->seq_id;
     454                 :            :         }
     455                 :    1130111 : }
     456                 :            : 
     457                 :            : uint64_t
     458                 :    1134239 : chunk_tail_md_offset(struct ftl_nv_cache *nv_cache)
     459                 :            : {
     460                 :    1134239 :         return nv_cache->chunk_blocks - nv_cache->tail_md_chunk_blocks;
     461                 :            : }
     462                 :            : 
     463                 :            : static void
     464                 :    1130128 : chunk_advance_blocks(struct ftl_nv_cache *nv_cache, struct ftl_nv_cache_chunk *chunk,
     465                 :            :                      uint64_t advanced_blocks)
     466                 :            : {
     467                 :    1130128 :         chunk->md->blocks_written += advanced_blocks;
     468                 :            : 
     469         [ -  + ]:    1130128 :         assert(chunk->md->blocks_written <= nv_cache->chunk_blocks);
     470                 :            : 
     471         [ +  + ]:    1130128 :         if (chunk->md->blocks_written == chunk_tail_md_offset(nv_cache)) {
     472                 :          5 :                 ftl_chunk_close(chunk);
     473                 :            :         }
     474                 :    1130128 : }
     475                 :            : 
     476                 :            : static uint64_t
     477                 :    3690512 : chunk_user_blocks_written(struct ftl_nv_cache_chunk *chunk)
     478                 :            : {
     479                 :    3690512 :         return chunk->md->blocks_written - chunk->md->blocks_skipped -
     480                 :    3690512 :                chunk->nv_cache->tail_md_chunk_blocks;
     481                 :            : }
     482                 :            : 
     483                 :            : static bool
     484                 :     922628 : is_chunk_compacted(struct ftl_nv_cache_chunk *chunk)
     485                 :            : {
     486         [ -  + ]:     922628 :         assert(chunk->md->blocks_written != 0);
     487                 :            : 
     488         [ +  + ]:     922628 :         if (chunk_user_blocks_written(chunk) == chunk->md->blocks_compacted) {
     489                 :          7 :                 return true;
     490                 :            :         }
     491                 :            : 
     492                 :     922621 :         return false;
     493                 :            : }
     494                 :            : 
     495                 :            : static int
     496                 :         49 : ftl_chunk_alloc_md_entry(struct ftl_nv_cache_chunk *chunk)
     497                 :            : {
     498                 :         49 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
     499                 :         49 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
     500                 :            : 
     501                 :         49 :         p2l_map->chunk_dma_md = ftl_mempool_get(nv_cache->chunk_md_pool);
     502                 :            : 
     503         [ -  + ]:         49 :         if (!p2l_map->chunk_dma_md) {
     504                 :          0 :                 return -ENOMEM;
     505                 :            :         }
     506                 :            : 
     507                 :         49 :         ftl_nv_cache_chunk_md_initialize(p2l_map->chunk_dma_md);
     508                 :         49 :         return 0;
     509                 :            : }
     510                 :            : 
     511                 :            : static void
     512                 :         49 : ftl_chunk_free_md_entry(struct ftl_nv_cache_chunk *chunk)
     513                 :            : {
     514                 :         49 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
     515                 :            : 
     516                 :         49 :         ftl_mempool_put(chunk->nv_cache->chunk_md_pool, p2l_map->chunk_dma_md);
     517                 :         49 :         p2l_map->chunk_dma_md = NULL;
     518                 :         49 : }
     519                 :            : 
     520                 :            : static void
     521                 :          7 : ftl_chunk_free(struct ftl_nv_cache_chunk *chunk)
     522                 :            : {
     523                 :          7 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
     524                 :            : 
     525                 :            :         /* Reset chunk */
     526                 :          7 :         ftl_nv_cache_chunk_md_initialize(chunk->md);
     527                 :            : 
     528                 :          7 :         TAILQ_INSERT_TAIL(&nv_cache->needs_free_persist_list, chunk, entry);
     529                 :          7 :         nv_cache->chunk_free_persist_count++;
     530                 :          7 : }
     531                 :            : 
     532                 :            : static int
     533                 :          7 : ftl_chunk_alloc_chunk_free_entry(struct ftl_nv_cache_chunk *chunk)
     534                 :            : {
     535                 :          7 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
     536                 :          7 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
     537                 :            : 
     538                 :          7 :         p2l_map->chunk_dma_md = ftl_mempool_get(nv_cache->free_chunk_md_pool);
     539         [ -  + ]:          7 :         if (!p2l_map->chunk_dma_md) {
     540                 :          0 :                 return -ENOMEM;
     541                 :            :         }
     542                 :            : 
     543                 :          7 :         ftl_nv_cache_chunk_md_initialize(p2l_map->chunk_dma_md);
     544                 :          7 :         return 0;
     545                 :            : }
     546                 :            : 
     547                 :            : static void
     548                 :          7 : ftl_chunk_free_chunk_free_entry(struct ftl_nv_cache_chunk *chunk)
     549                 :            : {
     550                 :          7 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
     551                 :            : 
     552                 :          7 :         ftl_mempool_put(chunk->nv_cache->free_chunk_md_pool, p2l_map->chunk_dma_md);
     553                 :          7 :         p2l_map->chunk_dma_md = NULL;
     554                 :          7 : }
     555                 :            : 
     556                 :            : static void
     557                 :          7 : chunk_free_cb(int status, void *ctx)
     558                 :            : {
     559                 :          7 :         struct ftl_nv_cache_chunk *chunk = (struct ftl_nv_cache_chunk *)ctx;
     560                 :            : 
     561         [ +  - ]:          7 :         if (spdk_likely(!status)) {
     562                 :          7 :                 struct ftl_nv_cache *nv_cache = chunk->nv_cache;
     563                 :            : 
     564                 :          7 :                 nv_cache->chunk_free_persist_count--;
     565                 :          7 :                 TAILQ_INSERT_TAIL(&nv_cache->chunk_free_list, chunk, entry);
     566                 :          7 :                 nv_cache->chunk_free_count++;
     567                 :          7 :                 nv_cache->chunk_full_count--;
     568                 :          7 :                 chunk->md->state = FTL_CHUNK_STATE_FREE;
     569                 :          7 :                 chunk->md->close_seq_id = 0;
     570                 :          7 :                 ftl_chunk_free_chunk_free_entry(chunk);
     571                 :            :         } else {
     572                 :            : #ifdef SPDK_FTL_RETRY_ON_ERROR
     573                 :            :                 ftl_md_persist_entry_retry(&chunk->md_persist_entry_ctx);
     574                 :            : #else
     575                 :          0 :                 ftl_abort();
     576                 :            : #endif
     577                 :            :         }
     578                 :          7 : }
     579                 :            : 
     580                 :            : static void
     581                 :   21620057 : ftl_chunk_persist_free_state(struct ftl_nv_cache *nv_cache)
     582                 :            : {
     583                 :            :         int rc;
     584                 :   21620057 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
     585                 :            :         struct ftl_p2l_map *p2l_map;
     586                 :   21620057 :         struct ftl_md *md = dev->layout.md[FTL_LAYOUT_REGION_TYPE_NVC_MD];
     587                 :   21620057 :         struct ftl_layout_region *region = ftl_layout_region_get(dev, FTL_LAYOUT_REGION_TYPE_NVC_MD);
     588                 :   21620057 :         struct ftl_nv_cache_chunk *tchunk, *chunk = NULL;
     589                 :            : 
     590         [ +  + ]:   21620064 :         TAILQ_FOREACH_SAFE(chunk, &nv_cache->needs_free_persist_list, entry, tchunk) {
     591                 :          7 :                 p2l_map = &chunk->p2l_map;
     592                 :          7 :                 rc = ftl_chunk_alloc_chunk_free_entry(chunk);
     593         [ -  + ]:          7 :                 if (rc) {
     594                 :          0 :                         break;
     595                 :            :                 }
     596                 :            : 
     597         [ -  + ]:          7 :                 TAILQ_REMOVE(&nv_cache->needs_free_persist_list, chunk, entry);
     598                 :            : 
     599   [ -  +  -  + ]:          7 :                 memcpy(p2l_map->chunk_dma_md, chunk->md, region->entry_size * FTL_BLOCK_SIZE);
     600                 :          7 :                 p2l_map->chunk_dma_md->state = FTL_CHUNK_STATE_FREE;
     601                 :          7 :                 p2l_map->chunk_dma_md->close_seq_id = 0;
     602                 :          7 :                 p2l_map->chunk_dma_md->p2l_map_checksum = 0;
     603                 :            : 
     604                 :          7 :                 ftl_md_persist_entries(md, get_chunk_idx(chunk), 1, p2l_map->chunk_dma_md, NULL,
     605                 :            :                                        chunk_free_cb, chunk, &chunk->md_persist_entry_ctx);
     606                 :            :         }
     607                 :   21620057 : }
     608                 :            : 
     609                 :            : static void
     610                 :          7 : compaction_stats_update(struct ftl_nv_cache_chunk *chunk)
     611                 :            : {
     612                 :          7 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
     613                 :          7 :         struct compaction_bw_stats *compaction_bw = &nv_cache->compaction_recent_bw;
     614                 :            :         double *ptr;
     615                 :            : 
     616         [ +  + ]:          7 :         if (spdk_unlikely(chunk->compaction_length_tsc == 0)) {
     617                 :          1 :                 return;
     618                 :            :         }
     619                 :            : 
     620         [ -  + ]:          6 :         if (spdk_likely(compaction_bw->count == FTL_NV_CACHE_COMPACTION_SMA_N)) {
     621                 :          0 :                 ptr = compaction_bw->buf + compaction_bw->first;
     622                 :          0 :                 compaction_bw->first++;
     623         [ #  # ]:          0 :                 if (compaction_bw->first == FTL_NV_CACHE_COMPACTION_SMA_N) {
     624                 :          0 :                         compaction_bw->first = 0;
     625                 :            :                 }
     626                 :          0 :                 compaction_bw->sum -= *ptr;
     627                 :            :         } else {
     628                 :          6 :                 ptr = compaction_bw->buf + compaction_bw->count;
     629                 :          6 :                 compaction_bw->count++;
     630                 :            :         }
     631                 :            : 
     632                 :          6 :         *ptr = (double)chunk->md->blocks_compacted * FTL_BLOCK_SIZE / chunk->compaction_length_tsc;
     633                 :          6 :         chunk->compaction_length_tsc = 0;
     634                 :            : 
     635                 :          6 :         compaction_bw->sum += *ptr;
     636                 :          6 :         nv_cache->compaction_sma = compaction_bw->sum / compaction_bw->count;
     637                 :            : }
     638                 :            : 
     639                 :            : static void
     640                 :     922628 : chunk_compaction_advance(struct ftl_nv_cache_chunk *chunk, uint64_t num_blocks)
     641                 :            : {
     642                 :     922628 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
     643                 :     922628 :         uint64_t tsc = spdk_thread_get_last_tsc(spdk_get_thread());
     644                 :            : 
     645                 :     922628 :         chunk->compaction_length_tsc += tsc - chunk->compaction_start_tsc;
     646                 :     922628 :         chunk->compaction_start_tsc = tsc;
     647                 :            : 
     648                 :     922628 :         chunk->md->blocks_compacted += num_blocks;
     649         [ -  + ]:     922628 :         assert(chunk->md->blocks_compacted <= chunk_user_blocks_written(chunk));
     650         [ +  + ]:     922628 :         if (!is_chunk_compacted(chunk)) {
     651                 :     922621 :                 return;
     652                 :            :         }
     653                 :            : 
     654                 :            :         /* Remove chunk from compacted list */
     655         [ +  + ]:          7 :         TAILQ_REMOVE(&nv_cache->chunk_comp_list, chunk, entry);
     656                 :          7 :         nv_cache->chunk_comp_count--;
     657                 :            : 
     658                 :          7 :         compaction_stats_update(chunk);
     659                 :            : 
     660                 :          7 :         ftl_chunk_free(chunk);
     661                 :            : }
     662                 :            : 
     663                 :            : static bool
     664                 :     309037 : is_compaction_required_for_upgrade(struct ftl_nv_cache *nv_cache)
     665                 :            : {
     666                 :     309037 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
     667                 :            : 
     668   [ -  +  +  + ]:     309037 :         if (dev->conf.prep_upgrade_on_shutdown) {
     669   [ +  +  -  + ]:     305990 :                 if (nv_cache->chunk_full_count || nv_cache->chunk_open_count) {
     670                 :       3383 :                         return true;
     671                 :            :                 }
     672                 :            :         }
     673                 :            : 
     674                 :     305654 :         return false;
     675                 :            : }
     676                 :            : 
     677                 :            : static bool
     678                 :   21620057 : is_compaction_required(struct ftl_nv_cache *nv_cache)
     679                 :            : {
     680   [ -  +  +  + ]:   21620057 :         if (spdk_unlikely(nv_cache->halt)) {
     681                 :     157311 :                 return is_compaction_required_for_upgrade(nv_cache);
     682                 :            :         }
     683                 :            : 
     684         [ +  + ]:   21462746 :         if (nv_cache->chunk_full_count >= nv_cache->chunk_compaction_threshold) {
     685                 :       1739 :                 return true;
     686                 :            :         }
     687                 :            : 
     688                 :   21461007 :         return false;
     689                 :            : }
     690                 :            : 
     691                 :            : static void compaction_process_finish_read(struct ftl_nv_cache_compactor *compactor);
     692                 :            : static void compaction_process_pin_lba(struct ftl_nv_cache_compactor *comp);
     693                 :            : 
     694                 :            : static void
     695                 :          0 : _compaction_process_pin_lba(void *_comp)
     696                 :            : {
     697                 :          0 :         struct ftl_nv_cache_compactor *comp = _comp;
     698                 :            : 
     699                 :          0 :         compaction_process_pin_lba(comp);
     700                 :          0 : }
     701                 :            : 
     702                 :            : static void
     703                 :     922624 : compaction_process_pin_lba_cb(struct spdk_ftl_dev *dev, int status, struct ftl_l2p_pin_ctx *pin_ctx)
     704                 :            : {
     705                 :     922624 :         struct ftl_nv_cache_compactor *comp = pin_ctx->cb_ctx;
     706                 :     922624 :         struct ftl_rq *rq = comp->rq;
     707                 :            : 
     708         [ -  + ]:     922624 :         if (status) {
     709                 :          0 :                 rq->iter.status = status;
     710                 :          0 :                 pin_ctx->lba = FTL_LBA_INVALID;
     711                 :            :         }
     712                 :            : 
     713         [ +  + ]:     922624 :         if (--rq->iter.remaining == 0) {
     714         [ -  + ]:       3604 :                 if (rq->iter.status) {
     715                 :            :                         /* unpin and try again */
     716                 :          0 :                         ftl_rq_unpin(rq);
     717                 :          0 :                         spdk_thread_send_msg(spdk_get_thread(), _compaction_process_pin_lba, comp);
     718                 :          0 :                         return;
     719                 :            :                 }
     720                 :            : 
     721                 :       3604 :                 compaction_process_finish_read(comp);
     722                 :            :         }
     723                 :            : }
     724                 :            : 
     725                 :            : static void
     726                 :       3604 : compaction_process_pin_lba(struct ftl_nv_cache_compactor *comp)
     727                 :            : {
     728                 :       3604 :         struct ftl_rq *rq = comp->rq;
     729                 :       3604 :         struct spdk_ftl_dev *dev = rq->dev;
     730                 :            :         struct ftl_rq_entry *entry;
     731                 :            : 
     732         [ -  + ]:       3604 :         assert(rq->iter.count);
     733                 :       3604 :         rq->iter.remaining = rq->iter.count;
     734                 :       3604 :         rq->iter.status = 0;
     735                 :            : 
     736   [ +  +  +  - ]:     926228 :         FTL_RQ_ENTRY_LOOP(rq, entry, rq->iter.count) {
     737                 :     922624 :                 struct ftl_nv_cache_chunk *chunk = entry->owner.priv;
     738                 :     922624 :                 struct ftl_l2p_pin_ctx *pin_ctx = &entry->l2p_pin_ctx;
     739                 :     922624 :                 union ftl_md_vss *md = entry->io_md;
     740                 :            : 
     741   [ +  -  -  + ]:     922624 :                 if (md->nv_cache.lba == FTL_LBA_INVALID || md->nv_cache.seq_id != chunk->md->seq_id) {
     742                 :          0 :                         ftl_l2p_pin_skip(dev, compaction_process_pin_lba_cb, comp, pin_ctx);
     743                 :            :                 } else {
     744                 :     922624 :                         ftl_l2p_pin(dev, md->nv_cache.lba, 1, compaction_process_pin_lba_cb, comp, pin_ctx);
     745                 :            :                 }
     746                 :            :         }
     747                 :       3604 : }
     748                 :            : 
     749                 :            : static void
     750                 :       3604 : compaction_process_read_entry_cb(struct spdk_bdev_io *bdev_io, bool success, void *arg)
     751                 :            : {
     752                 :       3604 :         struct ftl_rq_entry *entry = arg;
     753                 :       3604 :         struct ftl_rq *rq = ftl_rq_from_entry(entry);
     754                 :       3604 :         struct spdk_ftl_dev *dev = rq->dev;
     755                 :       3604 :         struct ftl_nv_cache_compactor *compactor = rq->owner.priv;
     756                 :            : 
     757                 :       3604 :         ftl_stats_bdev_io_completed(dev, FTL_STATS_TYPE_CMP, bdev_io);
     758                 :            : 
     759                 :       3604 :         spdk_bdev_free_io(bdev_io);
     760                 :            : 
     761         [ -  + ]:       3604 :         if (!success) {
     762                 :            :                 /* retry */
     763                 :          0 :                 spdk_thread_send_msg(spdk_get_thread(), compaction_process_read_entry, entry);
     764                 :          0 :                 return;
     765                 :            :         }
     766                 :            : 
     767         [ -  + ]:       3604 :         assert(rq->iter.remaining >= entry->bdev_io.num_blocks);
     768                 :       3604 :         rq->iter.remaining -= entry->bdev_io.num_blocks;
     769         [ +  - ]:       3604 :         if (0 == rq->iter.remaining) {
     770                 :            :                 /* All IOs processed, go to next phase - pining */
     771                 :       3604 :                 compaction_process_pin_lba(compactor);
     772                 :            :         }
     773                 :            : }
     774                 :            : 
     775                 :            : static void
     776                 :       3604 : compaction_process_read_entry(void *arg)
     777                 :            : {
     778                 :       3604 :         struct ftl_rq_entry *entry = arg;
     779                 :       3604 :         struct ftl_rq *rq = ftl_rq_from_entry(entry);
     780                 :       3604 :         struct spdk_ftl_dev *dev = rq->dev;
     781                 :            : 
     782                 :       3604 :         int rc = ftl_nv_cache_bdev_read_blocks_with_md(dev, dev->nv_cache.bdev_desc,
     783                 :            :                         dev->nv_cache.cache_ioch, entry->io_payload, entry->io_md,
     784                 :            :                         entry->bdev_io.offset_blocks, entry->bdev_io.num_blocks,
     785                 :            :                         compaction_process_read_entry_cb, entry);
     786                 :            : 
     787         [ -  + ]:       3604 :         if (spdk_unlikely(rc)) {
     788         [ #  # ]:          0 :                 if (rc == -ENOMEM) {
     789                 :          0 :                         struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(dev->nv_cache.bdev_desc);
     790                 :          0 :                         entry->bdev_io.wait_entry.bdev = bdev;
     791                 :          0 :                         entry->bdev_io.wait_entry.cb_fn = compaction_process_read_entry;
     792                 :          0 :                         entry->bdev_io.wait_entry.cb_arg = entry;
     793                 :          0 :                         spdk_bdev_queue_io_wait(bdev, dev->nv_cache.cache_ioch, &entry->bdev_io.wait_entry);
     794                 :            :                 } else {
     795                 :          0 :                         ftl_abort();
     796                 :            :                 }
     797                 :            :         }
     798                 :            : 
     799                 :       3604 :         dev->stats.io_activity_total += entry->bdev_io.num_blocks;
     800                 :       3604 : }
     801                 :            : 
     802                 :            : static bool
     803                 :     922630 : is_chunk_to_read(struct ftl_nv_cache_chunk *chunk)
     804                 :            : {
     805         [ -  + ]:     922630 :         assert(chunk->md->blocks_written != 0);
     806                 :            : 
     807         [ +  + ]:     922630 :         if (chunk_user_blocks_written(chunk) == chunk->md->read_pointer) {
     808                 :         15 :                 return false;
     809                 :            :         }
     810                 :            : 
     811                 :     922615 :         return true;
     812                 :            : }
     813                 :            : 
     814                 :            : static struct ftl_nv_cache_chunk *
     815                 :     922637 : get_chunk_for_compaction(struct ftl_nv_cache *nv_cache)
     816                 :            : {
     817                 :     922637 :         struct ftl_nv_cache_chunk *chunk = NULL;
     818                 :            : 
     819         [ +  + ]:     922637 :         if (!TAILQ_EMPTY(&nv_cache->chunk_comp_list)) {
     820                 :     922630 :                 chunk = TAILQ_FIRST(&nv_cache->chunk_comp_list);
     821         [ +  + ]:     922630 :                 if (is_chunk_to_read(chunk)) {
     822                 :     922615 :                         return chunk;
     823                 :            :                 }
     824                 :            :         }
     825                 :            : 
     826         [ +  + ]:         22 :         if (!TAILQ_EMPTY(&nv_cache->chunk_full_list)) {
     827                 :         11 :                 chunk = TAILQ_FIRST(&nv_cache->chunk_full_list);
     828         [ +  + ]:         11 :                 TAILQ_REMOVE(&nv_cache->chunk_full_list, chunk, entry);
     829                 :            : 
     830         [ -  + ]:         11 :                 assert(chunk->md->write_pointer);
     831                 :            :         } else {
     832                 :         11 :                 return NULL;
     833                 :            :         }
     834                 :            : 
     835         [ +  - ]:         11 :         if (spdk_likely(chunk)) {
     836         [ -  + ]:         11 :                 assert(chunk->md->write_pointer != 0);
     837         [ +  + ]:         11 :                 TAILQ_INSERT_HEAD(&nv_cache->chunk_comp_list, chunk, entry);
     838                 :         11 :                 nv_cache->chunk_comp_count++;
     839                 :            :         }
     840                 :            : 
     841                 :         11 :         return chunk;
     842                 :            : }
     843                 :            : 
     844                 :            : static uint64_t
     845                 :     922626 : chunk_blocks_to_read(struct ftl_nv_cache_chunk *chunk)
     846                 :            : {
     847                 :            :         uint64_t blocks_written;
     848                 :            :         uint64_t blocks_to_read;
     849                 :            : 
     850         [ -  + ]:     922626 :         assert(chunk->md->blocks_written >= chunk->md->blocks_skipped);
     851                 :     922626 :         blocks_written = chunk_user_blocks_written(chunk);
     852                 :            : 
     853         [ -  + ]:     922626 :         assert(blocks_written >= chunk->md->read_pointer);
     854                 :     922626 :         blocks_to_read = blocks_written - chunk->md->read_pointer;
     855                 :            : 
     856                 :     922626 :         return blocks_to_read;
     857                 :            : }
     858                 :            : 
     859                 :            : static void
     860                 :       3615 : compactor_deactivate(struct ftl_nv_cache_compactor *compactor)
     861                 :            : {
     862                 :       3615 :         struct ftl_nv_cache *nv_cache = compactor->nv_cache;
     863                 :            : 
     864                 :       3615 :         compactor->rq->iter.count = 0;
     865         [ -  + ]:       3615 :         assert(nv_cache->compaction_active_count);
     866                 :       3615 :         nv_cache->compaction_active_count--;
     867                 :       3615 :         TAILQ_INSERT_TAIL(&nv_cache->compactor_list, compactor, entry);
     868                 :       3615 : }
     869                 :            : 
     870                 :            : static void
     871                 :     970496 : compaction_process_invalidate_entry(struct ftl_rq_entry *entry)
     872                 :            : {
     873                 :     970496 :         entry->addr = FTL_ADDR_INVALID;
     874                 :     970496 :         entry->lba = FTL_LBA_INVALID;
     875                 :     970496 :         entry->seq_id = 0;
     876                 :     970496 :         entry->owner.priv = NULL;
     877                 :     970496 : }
     878                 :            : 
     879                 :            : static void
     880                 :         11 : compaction_process_pad(struct ftl_nv_cache_compactor *compactor, uint64_t idx)
     881                 :            : {
     882                 :         11 :         struct ftl_rq *rq = compactor->rq;
     883                 :            :         struct ftl_rq_entry *entry;
     884                 :            : 
     885         [ -  + ]:         11 :         assert(idx < rq->num_blocks);
     886   [ +  +  +  - ]:       2827 :         FTL_RQ_ENTRY_LOOP_FROM(rq, &rq->entries[idx], entry, rq->num_blocks) {
     887                 :       2816 :                 compaction_process_invalidate_entry(entry);
     888                 :            :         }
     889                 :         11 : }
     890                 :            : 
     891                 :            : static void
     892                 :       3604 : compaction_process_read(struct ftl_nv_cache_compactor *compactor)
     893                 :            : {
     894                 :       3604 :         struct ftl_rq *rq = compactor->rq;
     895                 :       3604 :         struct ftl_nv_cache *nv_cache = compactor->nv_cache;
     896                 :       3604 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
     897                 :            :         struct ftl_rq_entry *entry, *io;
     898                 :            : 
     899         [ -  + ]:       3604 :         assert(rq->iter.count);
     900                 :       3604 :         rq->iter.remaining = rq->iter.count;
     901                 :            : 
     902                 :       3604 :         io = rq->entries;
     903                 :       3604 :         io->bdev_io.num_blocks = 1;
     904                 :       3604 :         io->bdev_io.offset_blocks = ftl_addr_to_nvc_offset(dev, io->addr);
     905   [ +  +  +  - ]:     922624 :         FTL_RQ_ENTRY_LOOP_FROM(rq, &rq->entries[1], entry,  rq->iter.count) {
     906         [ +  - ]:     919020 :                 if (entry->addr == io->addr + io->bdev_io.num_blocks) {
     907                 :     919020 :                         io->bdev_io.num_blocks++;
     908                 :            :                 } else {
     909                 :          0 :                         compaction_process_read_entry(io);
     910                 :          0 :                         io = entry;
     911                 :          0 :                         io->bdev_io.num_blocks = 1;
     912                 :          0 :                         io->bdev_io.offset_blocks = ftl_addr_to_nvc_offset(dev, io->addr);
     913                 :            :                 }
     914                 :            :         }
     915                 :       3604 :         compaction_process_read_entry(io);
     916                 :       3604 : }
     917                 :            : 
     918                 :            : static ftl_addr
     919                 :     922626 : compaction_chunk_read_pos(struct spdk_ftl_dev *dev, struct ftl_nv_cache_chunk *chunk)
     920                 :            : {
     921                 :            :         ftl_addr start, pos;
     922                 :     922626 :         uint64_t skip, to_read = chunk_blocks_to_read(chunk);
     923                 :            : 
     924         [ -  + ]:     922626 :         if (0 == to_read) {
     925                 :          0 :                 return FTL_ADDR_INVALID;
     926                 :            :         }
     927                 :            : 
     928                 :     922626 :         start = ftl_addr_from_nvc_offset(dev, chunk->offset + chunk->md->read_pointer);
     929                 :     922626 :         pos = ftl_bitmap_find_first_set(dev->valid_map, start, start + to_read - 1);
     930                 :            : 
     931         [ +  + ]:     922626 :         if (pos == UINT64_MAX) {
     932                 :          2 :                 chunk->md->read_pointer += to_read;
     933                 :          2 :                 chunk_compaction_advance(chunk, to_read);
     934                 :          2 :                 return FTL_ADDR_INVALID;
     935                 :            :         }
     936                 :            : 
     937         [ -  + ]:     922624 :         assert(pos >= start);
     938                 :     922624 :         skip = pos - start;
     939         [ +  + ]:     922624 :         if (skip) {
     940                 :          2 :                 chunk->md->read_pointer += skip;
     941                 :          2 :                 chunk_compaction_advance(chunk, skip);
     942                 :            :         }
     943                 :            : 
     944                 :     922624 :         return pos;
     945                 :            : }
     946                 :            : 
     947                 :            : static bool
     948                 :     922635 : compaction_entry_read_pos(struct ftl_nv_cache *nv_cache, struct ftl_rq_entry *entry)
     949                 :            : {
     950                 :     922635 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
     951                 :     922635 :         struct ftl_nv_cache_chunk *chunk = NULL;
     952                 :     922635 :         ftl_addr addr = FTL_ADDR_INVALID;
     953                 :            : 
     954         [ +  + ]:    1845261 :         while (!chunk) {
     955                 :            :                 /* Get currently handled chunk */
     956                 :     922637 :                 chunk = get_chunk_for_compaction(nv_cache);
     957         [ +  + ]:     922637 :                 if (!chunk) {
     958                 :         11 :                         return false;
     959                 :            :                 }
     960                 :     922626 :                 chunk->compaction_start_tsc = spdk_thread_get_last_tsc(spdk_get_thread());
     961                 :            : 
     962                 :            :                 /* Get next read position in chunk */
     963                 :     922626 :                 addr = compaction_chunk_read_pos(dev, chunk);
     964         [ +  + ]:     922626 :                 if (FTL_ADDR_INVALID == addr) {
     965                 :          2 :                         chunk = NULL;
     966                 :            :                 }
     967                 :            :         }
     968                 :            : 
     969         [ -  + ]:     922624 :         assert(FTL_ADDR_INVALID != addr);
     970                 :            : 
     971                 :            :         /* Set entry address info and chunk */
     972                 :     922624 :         entry->addr = addr;
     973                 :     922624 :         entry->owner.priv = chunk;
     974                 :            : 
     975                 :            :         /* Move read pointer in the chunk */
     976                 :     922624 :         chunk->md->read_pointer++;
     977                 :            : 
     978                 :     922624 :         return true;
     979                 :            : }
     980                 :            : 
     981                 :            : static void
     982                 :       3615 : compaction_process_start(struct ftl_nv_cache_compactor *compactor)
     983                 :            : {
     984                 :       3615 :         struct ftl_rq *rq = compactor->rq;
     985                 :       3615 :         struct ftl_nv_cache *nv_cache = compactor->nv_cache;
     986                 :            :         struct ftl_rq_entry *entry;
     987                 :            : 
     988         [ -  + ]:       3615 :         assert(0 == compactor->rq->iter.count);
     989   [ +  +  +  - ]:     926239 :         FTL_RQ_ENTRY_LOOP(rq, entry, rq->num_blocks) {
     990         [ +  + ]:     922635 :                 if (!compaction_entry_read_pos(nv_cache, entry)) {
     991                 :         11 :                         compaction_process_pad(compactor, entry->index);
     992                 :         11 :                         break;
     993                 :            :                 }
     994                 :     922624 :                 rq->iter.count++;
     995                 :            :         }
     996                 :            : 
     997         [ +  + ]:       3615 :         if (rq->iter.count) {
     998                 :            :                 /* Schedule Read IOs */
     999                 :       3604 :                 compaction_process_read(compactor);
    1000                 :            :         } else {
    1001                 :         11 :                 compactor_deactivate(compactor);
    1002                 :            :         }
    1003                 :       3615 : }
    1004                 :            : 
    1005                 :            : static void
    1006                 :   21620057 : compaction_process(struct ftl_nv_cache *nv_cache)
    1007                 :            : {
    1008                 :   21620057 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    1009                 :            :         struct ftl_nv_cache_compactor *compactor;
    1010                 :            : 
    1011         [ +  + ]:   21620057 :         if (!is_compaction_required(nv_cache)) {
    1012                 :   21614937 :                 return;
    1013                 :            :         }
    1014                 :            : 
    1015                 :       5120 :         compactor = TAILQ_FIRST(&nv_cache->compactor_list);
    1016         [ +  + ]:       5120 :         if (!compactor) {
    1017                 :       1505 :                 return;
    1018                 :            :         }
    1019                 :            : 
    1020         [ +  + ]:       3615 :         TAILQ_REMOVE(&nv_cache->compactor_list, compactor, entry);
    1021                 :       3615 :         compactor->nv_cache->compaction_active_count++;
    1022                 :       3615 :         compaction_process_start(compactor);
    1023                 :       3615 :         ftl_add_io_activity(dev);
    1024                 :            : }
    1025                 :            : 
    1026                 :            : static void
    1027                 :       3604 : compaction_process_ftl_done(struct ftl_rq *rq)
    1028                 :            : {
    1029                 :       3604 :         struct spdk_ftl_dev *dev = rq->dev;
    1030                 :       3604 :         struct ftl_nv_cache_compactor *compactor = rq->owner.priv;
    1031                 :       3604 :         struct ftl_band *band = rq->io.band;
    1032                 :            :         struct ftl_rq_entry *entry;
    1033                 :            :         ftl_addr addr;
    1034                 :            : 
    1035   [ -  +  -  + ]:       3604 :         if (spdk_unlikely(false == rq->success)) {
    1036                 :            :                 /* IO error retry writing */
    1037                 :            : #ifdef SPDK_FTL_RETRY_ON_ERROR
    1038                 :            :                 ftl_writer_queue_rq(&dev->writer_user, rq);
    1039                 :            :                 return;
    1040                 :            : #else
    1041                 :          0 :                 ftl_abort();
    1042                 :            : #endif
    1043                 :            :         }
    1044                 :            : 
    1045         [ -  + ]:       3604 :         assert(rq->iter.count);
    1046                 :            : 
    1047                 :            :         /* Update L2P table */
    1048                 :       3604 :         addr = rq->io.addr;
    1049   [ +  +  +  - ]:     926228 :         FTL_RQ_ENTRY_LOOP(rq, entry, rq->iter.count) {
    1050                 :     922624 :                 struct ftl_nv_cache_chunk *chunk = entry->owner.priv;
    1051                 :            : 
    1052         [ +  - ]:     922624 :                 if (entry->lba != FTL_LBA_INVALID) {
    1053                 :     922624 :                         ftl_l2p_update_base(dev, entry->lba, addr, entry->addr);
    1054                 :     922624 :                         ftl_l2p_unpin(dev, entry->lba, 1);
    1055                 :     922624 :                         chunk_compaction_advance(chunk, 1);
    1056                 :            :                 } else {
    1057         [ #  # ]:          0 :                         assert(entry->addr == FTL_ADDR_INVALID);
    1058                 :            :                 }
    1059                 :            : 
    1060                 :     922624 :                 addr = ftl_band_next_addr(band, addr, 1);
    1061                 :     922624 :                 compaction_process_invalidate_entry(entry);
    1062                 :            :         }
    1063                 :            : 
    1064                 :       3604 :         compactor_deactivate(compactor);
    1065                 :       3604 : }
    1066                 :            : 
    1067                 :            : static void
    1068                 :       3604 : compaction_process_finish_read(struct ftl_nv_cache_compactor *compactor)
    1069                 :            : {
    1070                 :       3604 :         struct ftl_rq *rq = compactor->rq;
    1071                 :       3604 :         struct spdk_ftl_dev *dev = rq->dev;
    1072                 :            :         struct ftl_rq_entry *entry;
    1073                 :            :         ftl_addr current_addr;
    1074                 :       3604 :         uint64_t skip = 0;
    1075                 :            : 
    1076   [ +  +  +  - ]:     926228 :         FTL_RQ_ENTRY_LOOP(rq, entry, rq->iter.count) {
    1077                 :     922624 :                 struct ftl_nv_cache_chunk *chunk = entry->owner.priv;
    1078                 :     922624 :                 union ftl_md_vss *md = entry->io_md;
    1079                 :            : 
    1080   [ +  -  -  + ]:     922624 :                 if (md->nv_cache.lba == FTL_LBA_INVALID || md->nv_cache.seq_id != chunk->md->seq_id) {
    1081                 :          0 :                         skip++;
    1082                 :          0 :                         compaction_process_invalidate_entry(entry);
    1083                 :          0 :                         chunk_compaction_advance(chunk, 1);
    1084                 :          0 :                         continue;
    1085                 :            :                 }
    1086                 :            : 
    1087                 :     922624 :                 current_addr = ftl_l2p_get(dev, md->nv_cache.lba);
    1088         [ +  - ]:     922624 :                 if (current_addr == entry->addr) {
    1089                 :     922624 :                         entry->lba = md->nv_cache.lba;
    1090                 :     922624 :                         entry->seq_id = chunk->md->seq_id;
    1091                 :            :                 } else {
    1092                 :            :                         /* This address already invalidated, just omit this block */
    1093                 :          0 :                         chunk_compaction_advance(chunk, 1);
    1094                 :          0 :                         ftl_l2p_unpin(dev, md->nv_cache.lba, 1);
    1095                 :          0 :                         compaction_process_invalidate_entry(entry);
    1096                 :          0 :                         skip++;
    1097                 :            :                 }
    1098                 :            :         }
    1099                 :            : 
    1100         [ +  - ]:       3604 :         if (skip < rq->iter.count) {
    1101                 :            :                 /*
    1102                 :            :                  * Request contains data to be placed on FTL, compact it
    1103                 :            :                  */
    1104                 :       3604 :                 ftl_writer_queue_rq(&dev->writer_user, rq);
    1105                 :            :         } else {
    1106                 :          0 :                 compactor_deactivate(compactor);
    1107                 :            :         }
    1108                 :       3604 : }
    1109                 :            : 
    1110                 :            : static void
    1111                 :        176 : compactor_free(struct spdk_ftl_dev *dev, struct ftl_nv_cache_compactor *compactor)
    1112                 :            : {
    1113         [ -  + ]:        176 :         if (!compactor) {
    1114                 :          0 :                 return;
    1115                 :            :         }
    1116                 :            : 
    1117                 :        176 :         ftl_rq_del(compactor->rq);
    1118                 :        176 :         free(compactor);
    1119                 :            : }
    1120                 :            : 
    1121                 :            : static struct ftl_nv_cache_compactor *
    1122                 :        176 : compactor_alloc(struct spdk_ftl_dev *dev)
    1123                 :            : {
    1124                 :            :         struct ftl_nv_cache_compactor *compactor;
    1125                 :            :         struct ftl_rq_entry *entry;
    1126                 :            : 
    1127                 :        176 :         compactor = calloc(1, sizeof(*compactor));
    1128         [ -  + ]:        176 :         if (!compactor) {
    1129                 :          0 :                 goto error;
    1130                 :            :         }
    1131                 :            : 
    1132                 :            :         /* Allocate help request for reading */
    1133                 :        176 :         compactor->rq = ftl_rq_new(dev, dev->nv_cache.md_size);
    1134         [ -  + ]:        176 :         if (!compactor->rq) {
    1135                 :          0 :                 goto error;
    1136                 :            :         }
    1137                 :            : 
    1138                 :        176 :         compactor->nv_cache = &dev->nv_cache;
    1139                 :        176 :         compactor->rq->owner.priv = compactor;
    1140                 :        176 :         compactor->rq->owner.cb = compaction_process_ftl_done;
    1141                 :        176 :         compactor->rq->owner.compaction = true;
    1142                 :            : 
    1143   [ +  +  +  - ]:      45232 :         FTL_RQ_ENTRY_LOOP(compactor->rq, entry, compactor->rq->num_blocks) {
    1144                 :      45056 :                 compaction_process_invalidate_entry(entry);
    1145                 :            :         }
    1146                 :            : 
    1147                 :        176 :         return compactor;
    1148                 :            : 
    1149                 :          0 : error:
    1150                 :          0 :         compactor_free(dev, compactor);
    1151                 :          0 :         return NULL;
    1152                 :            : }
    1153                 :            : 
    1154                 :            : static void
    1155                 :    1130111 : ftl_nv_cache_submit_cb_done(struct ftl_io *io)
    1156                 :            : {
    1157                 :    1130111 :         struct ftl_nv_cache *nv_cache = &io->dev->nv_cache;
    1158                 :            : 
    1159                 :    1130111 :         chunk_advance_blocks(nv_cache, io->nv_cache_chunk, io->num_blocks);
    1160                 :    1130111 :         io->nv_cache_chunk = NULL;
    1161                 :            : 
    1162                 :    1130111 :         ftl_mempool_put(nv_cache->md_pool, io->md);
    1163                 :    1130111 :         ftl_io_complete(io);
    1164                 :    1130111 : }
    1165                 :            : 
    1166                 :            : static void
    1167                 :    1130111 : ftl_nv_cache_l2p_update(struct ftl_io *io)
    1168                 :            : {
    1169                 :    1130111 :         struct spdk_ftl_dev *dev = io->dev;
    1170                 :    1130111 :         ftl_addr next_addr = io->addr;
    1171                 :            :         size_t i;
    1172                 :            : 
    1173         [ +  + ]:    2943082 :         for (i = 0; i < io->num_blocks; ++i, ++next_addr) {
    1174                 :    1812971 :                 ftl_l2p_update_cache(dev, ftl_io_get_lba(io, i), next_addr, io->map[i]);
    1175                 :            :         }
    1176                 :            : 
    1177                 :    1130111 :         ftl_l2p_unpin(dev, io->lba, io->num_blocks);
    1178                 :    1130111 :         ftl_nv_cache_submit_cb_done(io);
    1179                 :    1130111 : }
    1180                 :            : 
    1181                 :            : static void
    1182                 :    1130111 : ftl_nv_cache_submit_cb(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
    1183                 :            : {
    1184                 :    1130111 :         struct ftl_io *io = cb_arg;
    1185                 :            : 
    1186                 :    1130111 :         ftl_stats_bdev_io_completed(io->dev, FTL_STATS_TYPE_USER, bdev_io);
    1187                 :            : 
    1188                 :    1130111 :         spdk_bdev_free_io(bdev_io);
    1189                 :            : 
    1190         [ -  + ]:    1130111 :         if (spdk_unlikely(!success)) {
    1191         [ #  # ]:          0 :                 FTL_ERRLOG(io->dev, "Non-volatile cache write failed at %"PRIx64"\n",
    1192                 :            :                            io->addr);
    1193                 :          0 :                 io->status = -EIO;
    1194                 :          0 :                 ftl_nv_cache_submit_cb_done(io);
    1195                 :            :         } else {
    1196                 :    1130111 :                 ftl_nv_cache_l2p_update(io);
    1197                 :            :         }
    1198                 :    1130111 : }
    1199                 :            : 
    1200                 :            : static void
    1201                 :    1130111 : nv_cache_write(void *_io)
    1202                 :            : {
    1203                 :    1130111 :         struct ftl_io *io = _io;
    1204                 :    1130111 :         struct spdk_ftl_dev *dev = io->dev;
    1205                 :    1130111 :         struct ftl_nv_cache *nv_cache = &dev->nv_cache;
    1206                 :            :         int rc;
    1207                 :            : 
    1208                 :    2260222 :         rc = spdk_bdev_writev_blocks_with_md(nv_cache->bdev_desc, nv_cache->cache_ioch,
    1209                 :    1130111 :                                              io->iov, io->iov_cnt, io->md,
    1210                 :            :                                              ftl_addr_to_nvc_offset(dev, io->addr), io->num_blocks,
    1211                 :            :                                              ftl_nv_cache_submit_cb, io);
    1212         [ -  + ]:    1130111 :         if (spdk_unlikely(rc)) {
    1213         [ #  # ]:          0 :                 if (rc == -ENOMEM) {
    1214                 :          0 :                         struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(nv_cache->bdev_desc);
    1215                 :          0 :                         io->bdev_io_wait.bdev = bdev;
    1216                 :          0 :                         io->bdev_io_wait.cb_fn = nv_cache_write;
    1217                 :          0 :                         io->bdev_io_wait.cb_arg = io;
    1218                 :          0 :                         spdk_bdev_queue_io_wait(bdev, nv_cache->cache_ioch, &io->bdev_io_wait);
    1219                 :            :                 } else {
    1220                 :          0 :                         ftl_abort();
    1221                 :            :                 }
    1222                 :            :         }
    1223                 :    1130111 : }
    1224                 :            : 
    1225                 :            : static void
    1226                 :    1130111 : ftl_nv_cache_pin_cb(struct spdk_ftl_dev *dev, int status, struct ftl_l2p_pin_ctx *pin_ctx)
    1227                 :            : {
    1228                 :    1130111 :         struct ftl_io *io = pin_ctx->cb_ctx;
    1229                 :            :         size_t i;
    1230                 :            : 
    1231         [ -  + ]:    1130111 :         if (spdk_unlikely(status != 0)) {
    1232                 :            :                 /* Retry on the internal L2P fault */
    1233         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Cannot PIN LBA for NV cache write failed at %"PRIx64"\n",
    1234                 :            :                            io->addr);
    1235                 :          0 :                 io->status = -EAGAIN;
    1236                 :          0 :                 ftl_nv_cache_submit_cb_done(io);
    1237                 :          0 :                 return;
    1238                 :            :         }
    1239                 :            : 
    1240                 :            :         /* Remember previous l2p mapping to resolve conflicts in case of outstanding write-after-write */
    1241         [ +  + ]:    2943082 :         for (i = 0; i < io->num_blocks; ++i) {
    1242                 :    1812971 :                 io->map[i] = ftl_l2p_get(dev, ftl_io_get_lba(io, i));
    1243                 :            :         }
    1244                 :            : 
    1245         [ -  + ]:    1130111 :         assert(io->iov_pos == 0);
    1246                 :            : 
    1247                 :    1130111 :         ftl_trace_submission(io->dev, io, io->addr, io->num_blocks);
    1248                 :            : 
    1249                 :    1130111 :         nv_cache_write(io);
    1250                 :            : }
    1251                 :            : 
    1252                 :            : bool
    1253                 :    1130111 : ftl_nv_cache_write(struct ftl_io *io)
    1254                 :            : {
    1255                 :    1130111 :         struct spdk_ftl_dev *dev = io->dev;
    1256                 :            :         uint64_t cache_offset;
    1257                 :            : 
    1258                 :    1130111 :         io->md = ftl_mempool_get(dev->nv_cache.md_pool);
    1259         [ -  + ]:    1130111 :         if (spdk_unlikely(!io->md)) {
    1260                 :          0 :                 return false;
    1261                 :            :         }
    1262                 :            : 
    1263                 :            :         /* Reserve area on the write buffer cache */
    1264                 :    1130111 :         cache_offset = ftl_nv_cache_get_wr_buffer(&dev->nv_cache, io);
    1265         [ -  + ]:    1130111 :         if (cache_offset == FTL_LBA_INVALID) {
    1266                 :            :                 /* No free space in NV cache, resubmit request */
    1267                 :          0 :                 ftl_mempool_put(dev->nv_cache.md_pool, io->md);
    1268                 :          0 :                 return false;
    1269                 :            :         }
    1270                 :    1130111 :         io->addr = ftl_addr_from_nvc_offset(dev, cache_offset);
    1271                 :    1130111 :         io->nv_cache_chunk = dev->nv_cache.chunk_current;
    1272                 :            : 
    1273                 :    1130111 :         ftl_nv_cache_fill_md(io);
    1274                 :    1130111 :         ftl_l2p_pin(io->dev, io->lba, io->num_blocks,
    1275                 :            :                     ftl_nv_cache_pin_cb, io,
    1276                 :            :                     &io->l2p_pin_ctx);
    1277                 :            : 
    1278                 :    1130111 :         dev->nv_cache.throttle.blocks_submitted += io->num_blocks;
    1279                 :            : 
    1280                 :    1130111 :         return true;
    1281                 :            : }
    1282                 :            : 
    1283                 :            : int
    1284                 :    1150069 : ftl_nv_cache_read(struct ftl_io *io, ftl_addr addr, uint32_t num_blocks,
    1285                 :            :                   spdk_bdev_io_completion_cb cb, void *cb_arg)
    1286                 :            : {
    1287                 :            :         int rc;
    1288                 :    1150069 :         struct ftl_nv_cache *nv_cache = &io->dev->nv_cache;
    1289                 :            : 
    1290         [ -  + ]:    1150069 :         assert(ftl_addr_in_nvc(io->dev, addr));
    1291                 :            : 
    1292                 :    1150069 :         rc = ftl_nv_cache_bdev_read_blocks_with_md(io->dev, nv_cache->bdev_desc, nv_cache->cache_ioch,
    1293                 :    1150069 :                         ftl_io_iovec_addr(io), NULL, ftl_addr_to_nvc_offset(io->dev, addr),
    1294                 :            :                         num_blocks, cb, cb_arg);
    1295                 :            : 
    1296                 :    1150069 :         return rc;
    1297                 :            : }
    1298                 :            : 
    1299                 :            : bool
    1300                 :     156234 : ftl_nv_cache_is_halted(struct ftl_nv_cache *nv_cache)
    1301                 :            : {
    1302         [ +  + ]:     156234 :         if (nv_cache->compaction_active_count) {
    1303                 :       3565 :                 return false;
    1304                 :            :         }
    1305                 :            : 
    1306         [ +  + ]:     152669 :         if (nv_cache->chunk_open_count > 0) {
    1307                 :        943 :                 return false;
    1308                 :            :         }
    1309                 :            : 
    1310         [ +  + ]:     151726 :         if (is_compaction_required_for_upgrade(nv_cache)) {
    1311                 :          2 :                 return false;
    1312                 :            :         }
    1313                 :            : 
    1314                 :     151724 :         return true;
    1315                 :            : }
    1316                 :            : 
    1317                 :            : void
    1318                 :    2336747 : ftl_chunk_map_set_lba(struct ftl_nv_cache_chunk *chunk,
    1319                 :            :                       uint64_t offset, uint64_t lba)
    1320                 :            : {
    1321                 :    2336747 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    1322                 :    2336747 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1323                 :            : 
    1324                 :    2336747 :         ftl_lba_store(dev, p2l_map->chunk_map, offset, lba);
    1325                 :    2336747 : }
    1326                 :            : 
    1327                 :            : uint64_t
    1328                 :          0 : ftl_chunk_map_get_lba(struct ftl_nv_cache_chunk *chunk, uint64_t offset)
    1329                 :            : {
    1330                 :          0 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    1331                 :          0 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1332                 :            : 
    1333                 :          0 :         return ftl_lba_load(dev, p2l_map->chunk_map, offset);
    1334                 :            : }
    1335                 :            : 
    1336                 :            : static void
    1337                 :    2336747 : ftl_chunk_set_addr(struct ftl_nv_cache_chunk *chunk, uint64_t lba, ftl_addr addr)
    1338                 :            : {
    1339                 :    2336747 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    1340                 :    2336747 :         uint64_t cache_offset = ftl_addr_to_nvc_offset(dev, addr);
    1341                 :            :         uint64_t offset;
    1342                 :            : 
    1343         [ -  + ]:    2336747 :         offset = (cache_offset - chunk->offset) % chunk->nv_cache->chunk_blocks;
    1344                 :    2336747 :         ftl_chunk_map_set_lba(chunk, offset, lba);
    1345                 :    2336747 : }
    1346                 :            : 
    1347                 :            : struct ftl_nv_cache_chunk *
    1348                 :    3446323 : ftl_nv_cache_get_chunk_from_addr(struct spdk_ftl_dev *dev, ftl_addr addr)
    1349                 :            : {
    1350                 :    3446323 :         struct ftl_nv_cache_chunk *chunk = dev->nv_cache.chunks;
    1351                 :            :         uint64_t chunk_idx;
    1352                 :    3446323 :         uint64_t cache_offset = ftl_addr_to_nvc_offset(dev, addr);
    1353                 :            : 
    1354         [ -  + ]:    3446323 :         assert(chunk != NULL);
    1355         [ -  + ]:    3446323 :         chunk_idx = (cache_offset - chunk->offset) / chunk->nv_cache->chunk_blocks;
    1356                 :    3446323 :         chunk += chunk_idx;
    1357                 :            : 
    1358                 :    3446323 :         return chunk;
    1359                 :            : }
    1360                 :            : 
    1361                 :            : void
    1362                 :    1812971 : ftl_nv_cache_set_addr(struct spdk_ftl_dev *dev, uint64_t lba, ftl_addr addr)
    1363                 :            : {
    1364                 :            :         struct ftl_nv_cache_chunk *chunk;
    1365                 :            : 
    1366                 :    1812971 :         chunk = ftl_nv_cache_get_chunk_from_addr(dev, addr);
    1367                 :            : 
    1368         [ -  + ]:    1812971 :         assert(lba != FTL_LBA_INVALID);
    1369                 :            : 
    1370                 :    1812971 :         ftl_chunk_set_addr(chunk, lba, addr);
    1371                 :    1812971 :         ftl_bitmap_set(dev->valid_map, addr);
    1372                 :    1812971 : }
    1373                 :            : 
    1374                 :            : static void
    1375                 :      23077 : ftl_nv_cache_throttle_update(struct ftl_nv_cache *nv_cache)
    1376                 :            : {
    1377                 :            :         double err;
    1378                 :            :         double modifier;
    1379                 :            : 
    1380                 :      23077 :         err = ((double)nv_cache->chunk_free_count - nv_cache->chunk_free_target) / nv_cache->chunk_count;
    1381                 :      23077 :         modifier = FTL_NV_CACHE_THROTTLE_MODIFIER_KP * err;
    1382                 :            : 
    1383         [ +  + ]:      23077 :         if (modifier < FTL_NV_CACHE_THROTTLE_MODIFIER_MIN) {
    1384                 :      15766 :                 modifier = FTL_NV_CACHE_THROTTLE_MODIFIER_MIN;
    1385         [ +  + ]:       7311 :         } else if (modifier > FTL_NV_CACHE_THROTTLE_MODIFIER_MAX) {
    1386                 :       4353 :                 modifier = FTL_NV_CACHE_THROTTLE_MODIFIER_MAX;
    1387                 :            :         }
    1388                 :            : 
    1389   [ +  +  +  + ]:      23077 :         if (spdk_unlikely(nv_cache->compaction_sma == 0 || nv_cache->compaction_active_count == 0)) {
    1390                 :      22897 :                 nv_cache->throttle.blocks_submitted_limit = UINT64_MAX;
    1391                 :            :         } else {
    1392                 :        180 :                 double blocks_per_interval = nv_cache->compaction_sma * nv_cache->throttle.interval_tsc /
    1393                 :            :                                              FTL_BLOCK_SIZE;
    1394                 :        180 :                 nv_cache->throttle.blocks_submitted_limit = blocks_per_interval * (1.0 + modifier);
    1395                 :            :         }
    1396                 :      23077 : }
    1397                 :            : 
    1398                 :            : static void
    1399                 :   21620057 : ftl_nv_cache_process_throttle(struct ftl_nv_cache *nv_cache)
    1400                 :            : {
    1401                 :   21620057 :         uint64_t tsc = spdk_thread_get_last_tsc(spdk_get_thread());
    1402                 :            : 
    1403         [ +  + ]:   21620057 :         if (spdk_unlikely(!nv_cache->throttle.start_tsc)) {
    1404                 :         22 :                 nv_cache->throttle.start_tsc = tsc;
    1405         [ +  + ]:   21620035 :         } else if (tsc - nv_cache->throttle.start_tsc >= nv_cache->throttle.interval_tsc) {
    1406                 :      23077 :                 ftl_nv_cache_throttle_update(nv_cache);
    1407                 :      23077 :                 nv_cache->throttle.start_tsc = tsc;
    1408                 :      23077 :                 nv_cache->throttle.blocks_submitted = 0;
    1409                 :            :         }
    1410                 :   21620057 : }
    1411                 :            : 
    1412                 :            : static void ftl_chunk_open(struct ftl_nv_cache_chunk *chunk);
    1413                 :            : 
    1414                 :            : void
    1415                 :   21620057 : ftl_nv_cache_process(struct spdk_ftl_dev *dev)
    1416                 :            : {
    1417                 :   21620057 :         struct ftl_nv_cache *nv_cache = &dev->nv_cache;
    1418                 :            : 
    1419         [ -  + ]:   21620057 :         assert(dev->nv_cache.bdev_desc);
    1420                 :            : 
    1421   [ +  +  -  +  :   21620057 :         if (nv_cache->chunk_open_count < FTL_MAX_OPEN_CHUNKS && spdk_likely(!nv_cache->halt) &&
                   +  + ]
    1422         [ +  + ]:       1784 :             !TAILQ_EMPTY(&nv_cache->chunk_free_list)) {
    1423                 :         47 :                 struct ftl_nv_cache_chunk *chunk = TAILQ_FIRST(&nv_cache->chunk_free_list);
    1424         [ +  + ]:         47 :                 TAILQ_REMOVE(&nv_cache->chunk_free_list, chunk, entry);
    1425                 :         47 :                 TAILQ_INSERT_TAIL(&nv_cache->chunk_open_list, chunk, entry);
    1426                 :         47 :                 nv_cache->chunk_free_count--;
    1427                 :         47 :                 chunk->md->seq_id = ftl_get_next_seq_id(dev);
    1428                 :         47 :                 ftl_chunk_open(chunk);
    1429                 :         47 :                 ftl_add_io_activity(dev);
    1430                 :            :         }
    1431                 :            : 
    1432                 :   21620057 :         compaction_process(nv_cache);
    1433                 :   21620057 :         ftl_chunk_persist_free_state(nv_cache);
    1434                 :   21620057 :         ftl_nv_cache_process_throttle(nv_cache);
    1435                 :   21620057 : }
    1436                 :            : 
    1437                 :            : static bool
    1438                 :    1130111 : ftl_nv_cache_full(struct ftl_nv_cache *nv_cache)
    1439                 :            : {
    1440   [ -  +  -  - ]:    1130111 :         if (0 == nv_cache->chunk_open_count && NULL == nv_cache->chunk_current) {
    1441                 :          0 :                 return true;
    1442                 :            :         } else {
    1443                 :    1130111 :                 return false;
    1444                 :            :         }
    1445                 :            : }
    1446                 :            : 
    1447                 :            : bool
    1448                 :    1130111 : ftl_nv_cache_throttle(struct spdk_ftl_dev *dev)
    1449                 :            : {
    1450                 :    1130111 :         struct ftl_nv_cache *nv_cache = &dev->nv_cache;
    1451                 :            : 
    1452   [ +  -  -  + ]:    2260222 :         if (dev->nv_cache.throttle.blocks_submitted >= nv_cache->throttle.blocks_submitted_limit ||
    1453                 :    1130111 :             ftl_nv_cache_full(nv_cache)) {
    1454                 :          0 :                 return true;
    1455                 :            :         }
    1456                 :            : 
    1457                 :    1130111 :         return false;
    1458                 :            : }
    1459                 :            : 
    1460                 :            : static void
    1461                 :         49 : chunk_free_p2l_map(struct ftl_nv_cache_chunk *chunk)
    1462                 :            : {
    1463                 :         49 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
    1464                 :         49 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1465                 :            : 
    1466                 :         49 :         ftl_mempool_put(nv_cache->p2l_pool, p2l_map->chunk_map);
    1467                 :         49 :         p2l_map->chunk_map = NULL;
    1468                 :            : 
    1469                 :         49 :         ftl_chunk_free_md_entry(chunk);
    1470                 :         49 : }
    1471                 :            : 
    1472                 :            : int
    1473                 :         27 : ftl_nv_cache_save_state(struct ftl_nv_cache *nv_cache)
    1474                 :            : {
    1475                 :         27 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    1476                 :            :         struct ftl_nv_cache_chunk *chunk;
    1477                 :         27 :         int status = 0;
    1478                 :            :         uint64_t i;
    1479                 :            : 
    1480         [ -  + ]:         27 :         assert(nv_cache->chunk_open_count == 0);
    1481                 :            : 
    1482         [ -  + ]:         27 :         if (nv_cache->compaction_active_count) {
    1483         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Cannot save NV cache state, compaction in progress\n");
    1484                 :          0 :                 return -EINVAL;
    1485                 :            :         }
    1486                 :            : 
    1487                 :         27 :         chunk = nv_cache->chunks;
    1488         [ -  + ]:         27 :         if (!chunk) {
    1489         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Cannot save NV cache state, no NV cache metadata\n");
    1490                 :          0 :                 return -ENOMEM;
    1491                 :            :         }
    1492                 :            : 
    1493         [ +  + ]:        162 :         for (i = 0; i < nv_cache->chunk_count; i++, chunk++) {
    1494                 :        135 :                 nvc_validate_md(nv_cache, chunk->md);
    1495                 :            : 
    1496         [ +  + ]:        135 :                 if (chunk->md->read_pointer)  {
    1497                 :            :                         /* Only full chunks can be compacted */
    1498         [ -  + ]:          4 :                         if (chunk->md->blocks_written != nv_cache->chunk_blocks) {
    1499                 :          0 :                                 assert(0);
    1500                 :            :                                 status = -EINVAL;
    1501                 :            :                                 break;
    1502                 :            :                         }
    1503                 :            : 
    1504                 :            :                         /*
    1505                 :            :                          * Chunk in the middle of compaction, start over after
    1506                 :            :                          * load
    1507                 :            :                          */
    1508                 :          4 :                         chunk->md->read_pointer = chunk->md->blocks_compacted = 0;
    1509         [ +  + ]:        131 :                 } else if (chunk->md->blocks_written == nv_cache->chunk_blocks) {
    1510                 :            :                         /* Full chunk */
    1511         [ -  + ]:         99 :                 } else if (0 == chunk->md->blocks_written) {
    1512                 :            :                         /* Empty chunk */
    1513                 :            :                 } else {
    1514                 :          0 :                         assert(0);
    1515                 :            :                         status = -EINVAL;
    1516                 :            :                         break;
    1517                 :            :                 }
    1518                 :            :         }
    1519                 :            : 
    1520         [ -  + ]:         27 :         if (status) {
    1521         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Cannot save NV cache state, inconsistent NV cache"
    1522                 :            :                            "metadata\n");
    1523                 :            :         }
    1524                 :            : 
    1525                 :         27 :         return status;
    1526                 :            : }
    1527                 :            : 
    1528                 :            : static int
    1529                 :         30 : sort_chunks_cmp(const void *a, const void *b)
    1530                 :            : {
    1531                 :         30 :         struct ftl_nv_cache_chunk *a_chunk = *(struct ftl_nv_cache_chunk **)a;
    1532                 :         30 :         struct ftl_nv_cache_chunk *b_chunk = *(struct ftl_nv_cache_chunk **)b;
    1533                 :            : 
    1534                 :         30 :         return a_chunk->md->seq_id - b_chunk->md->seq_id;
    1535                 :            : }
    1536                 :            : 
    1537                 :            : static int
    1538                 :         17 : sort_chunks(struct ftl_nv_cache *nv_cache)
    1539                 :            : {
    1540                 :            :         struct ftl_nv_cache_chunk **chunks_list;
    1541                 :            :         struct ftl_nv_cache_chunk *chunk;
    1542                 :            :         uint32_t i;
    1543                 :            : 
    1544         [ +  + ]:         17 :         if (TAILQ_EMPTY(&nv_cache->chunk_full_list)) {
    1545                 :          3 :                 return 0;
    1546                 :            :         }
    1547                 :            : 
    1548                 :         14 :         chunks_list = calloc(nv_cache->chunk_full_count,
    1549                 :            :                              sizeof(chunks_list[0]));
    1550         [ -  + ]:         14 :         if (!chunks_list) {
    1551                 :          0 :                 return -ENOMEM;
    1552                 :            :         }
    1553                 :            : 
    1554                 :         14 :         i = 0;
    1555         [ +  + ]:         42 :         TAILQ_FOREACH(chunk, &nv_cache->chunk_full_list, entry) {
    1556                 :         28 :                 chunks_list[i] = chunk;
    1557                 :         28 :                 i++;
    1558                 :            :         }
    1559         [ -  + ]:         14 :         assert(i == nv_cache->chunk_full_count);
    1560                 :            : 
    1561   [ -  +  -  + ]:         14 :         qsort(chunks_list, nv_cache->chunk_full_count, sizeof(chunks_list[0]),
    1562                 :            :               sort_chunks_cmp);
    1563                 :            : 
    1564                 :         14 :         TAILQ_INIT(&nv_cache->chunk_full_list);
    1565         [ +  + ]:         42 :         for (i = 0; i < nv_cache->chunk_full_count; i++) {
    1566                 :         28 :                 chunk = chunks_list[i];
    1567                 :         28 :                 TAILQ_INSERT_TAIL(&nv_cache->chunk_full_list, chunk, entry);
    1568                 :            :         }
    1569                 :            : 
    1570                 :         14 :         free(chunks_list);
    1571                 :         14 :         return 0;
    1572                 :            : }
    1573                 :            : 
    1574                 :            : static int
    1575                 :         49 : chunk_alloc_p2l_map(struct ftl_nv_cache_chunk *chunk)
    1576                 :            : {
    1577                 :         49 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
    1578                 :         49 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1579                 :            : 
    1580         [ -  + ]:         49 :         assert(p2l_map->ref_cnt == 0);
    1581         [ -  + ]:         49 :         assert(p2l_map->chunk_map == NULL);
    1582                 :            : 
    1583                 :         49 :         p2l_map->chunk_map = ftl_mempool_get(nv_cache->p2l_pool);
    1584                 :            : 
    1585         [ -  + ]:         49 :         if (!p2l_map->chunk_map) {
    1586                 :          0 :                 return -ENOMEM;
    1587                 :            :         }
    1588                 :            : 
    1589         [ -  + ]:         49 :         if (ftl_chunk_alloc_md_entry(chunk)) {
    1590                 :          0 :                 ftl_mempool_put(nv_cache->p2l_pool, p2l_map->chunk_map);
    1591                 :          0 :                 p2l_map->chunk_map = NULL;
    1592                 :          0 :                 return -ENOMEM;
    1593                 :            :         }
    1594                 :            : 
    1595                 :            :         /* Set the P2L to FTL_LBA_INVALID */
    1596         [ -  + ]:         49 :         memset(p2l_map->chunk_map, -1, FTL_BLOCK_SIZE * nv_cache->tail_md_chunk_blocks);
    1597                 :            : 
    1598                 :         49 :         return 0;
    1599                 :            : }
    1600                 :            : 
    1601                 :            : int
    1602                 :         17 : ftl_nv_cache_load_state(struct ftl_nv_cache *nv_cache)
    1603                 :            : {
    1604                 :         17 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    1605                 :            :         struct ftl_nv_cache_chunk *chunk;
    1606                 :            :         uint64_t chunks_number, offset, i;
    1607                 :         17 :         int status = 0;
    1608                 :            :         bool active;
    1609                 :            : 
    1610                 :         17 :         nv_cache->chunk_current = NULL;
    1611                 :         17 :         TAILQ_INIT(&nv_cache->chunk_free_list);
    1612                 :         17 :         TAILQ_INIT(&nv_cache->chunk_full_list);
    1613                 :         17 :         TAILQ_INIT(&nv_cache->chunk_inactive_list);
    1614                 :         17 :         nv_cache->chunk_full_count = 0;
    1615                 :         17 :         nv_cache->chunk_free_count = 0;
    1616                 :         17 :         nv_cache->chunk_inactive_count = 0;
    1617                 :            : 
    1618         [ -  + ]:         17 :         assert(nv_cache->chunk_open_count == 0);
    1619                 :         17 :         offset = nvc_data_offset(nv_cache);
    1620         [ -  + ]:         17 :         if (!nv_cache->chunks) {
    1621         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "No NV cache metadata\n");
    1622                 :          0 :                 return -1;
    1623                 :            :         }
    1624                 :            : 
    1625         [ -  + ]:         17 :         if (dev->sb->upgrade_ready) {
    1626                 :            :                 /*
    1627                 :            :                  * During upgrade some transitions are allowed:
    1628                 :            :                  *
    1629                 :            :                  * 1. FREE -> INACTIVE
    1630                 :            :                  * 2. INACTIVE -> FREE
    1631                 :            :                  */
    1632                 :          0 :                 chunk = nv_cache->chunks;
    1633         [ #  # ]:          0 :                 for (i = 0; i < nv_cache->chunk_count; i++, chunk++) {
    1634                 :          0 :                         active = nv_cache->nvc_type->ops.is_chunk_active(dev, chunk->offset);
    1635                 :            : 
    1636         [ #  # ]:          0 :                         if (chunk->md->state == FTL_CHUNK_STATE_FREE) {
    1637         [ #  # ]:          0 :                                 if (!active) {
    1638                 :          0 :                                         chunk->md->state = FTL_CHUNK_STATE_INACTIVE;
    1639                 :            :                                 }
    1640         [ #  # ]:          0 :                         } else if (chunk->md->state == FTL_CHUNK_STATE_INACTIVE) {
    1641         [ #  # ]:          0 :                                 if (active) {
    1642                 :          0 :                                         chunk->md->state = FTL_CHUNK_STATE_FREE;
    1643                 :            :                                 }
    1644                 :            :                         }
    1645                 :            :                 }
    1646                 :            :         }
    1647                 :            : 
    1648                 :         17 :         chunk = nv_cache->chunks;
    1649         [ +  + ]:        102 :         for (i = 0; i < nv_cache->chunk_count; i++, chunk++) {
    1650                 :         85 :                 chunk->nv_cache = nv_cache;
    1651                 :         85 :                 nvc_validate_md(nv_cache, chunk->md);
    1652                 :            : 
    1653         [ -  + ]:         85 :                 if (offset != chunk->offset) {
    1654                 :          0 :                         status = -EINVAL;
    1655                 :          0 :                         goto error;
    1656                 :            :                 }
    1657                 :            : 
    1658         [ -  + ]:         85 :                 if (chunk->md->version != FTL_NVC_VERSION_CURRENT) {
    1659                 :          0 :                         status = -EINVAL;
    1660                 :          0 :                         goto error;
    1661                 :            :                 }
    1662                 :            : 
    1663                 :         85 :                 active = nv_cache->nvc_type->ops.is_chunk_active(dev, chunk->offset);
    1664         [ +  + ]:         85 :                 if (false == active) {
    1665         [ -  + ]:         17 :                         if (chunk->md->state != FTL_CHUNK_STATE_INACTIVE) {
    1666                 :          0 :                                 status = -EINVAL;
    1667                 :          0 :                                 goto error;
    1668                 :            :                         }
    1669                 :            :                 }
    1670                 :            : 
    1671   [ +  -  +  +  :         85 :                 switch (chunk->md->state) {
                      - ]
    1672                 :         40 :                 case FTL_CHUNK_STATE_FREE:
    1673   [ +  -  -  + ]:         40 :                         if (chunk->md->blocks_written || chunk->md->write_pointer) {
    1674                 :          0 :                                 status = -EINVAL;
    1675                 :          0 :                                 goto error;
    1676                 :            :                         }
    1677                 :            :                         /* Chunk empty, move it on empty list */
    1678                 :         40 :                         TAILQ_INSERT_TAIL(&nv_cache->chunk_free_list, chunk, entry);
    1679                 :         40 :                         nv_cache->chunk_free_count++;
    1680                 :         40 :                         break;
    1681                 :          0 :                 case FTL_CHUNK_STATE_OPEN:
    1682                 :            :                         /* All chunks needs to closed at this point */
    1683                 :          0 :                         status = -EINVAL;
    1684                 :          0 :                         goto error;
    1685                 :            :                         break;
    1686                 :         28 :                 case FTL_CHUNK_STATE_CLOSED:
    1687         [ -  + ]:         28 :                         if (chunk->md->blocks_written != nv_cache->chunk_blocks) {
    1688                 :          0 :                                 status = -EINVAL;
    1689                 :          0 :                                 goto error;
    1690                 :            :                         }
    1691                 :            :                         /* Chunk full, move it on full list */
    1692                 :         28 :                         TAILQ_INSERT_TAIL(&nv_cache->chunk_full_list, chunk, entry);
    1693                 :         28 :                         nv_cache->chunk_full_count++;
    1694                 :         28 :                         break;
    1695                 :         17 :                 case FTL_CHUNK_STATE_INACTIVE:
    1696                 :         17 :                         TAILQ_INSERT_TAIL(&nv_cache->chunk_inactive_list, chunk, entry);
    1697                 :         17 :                         nv_cache->chunk_inactive_count++;
    1698                 :         17 :                         break;
    1699                 :          0 :                 default:
    1700                 :          0 :                         status = -EINVAL;
    1701         [ #  # ]:          0 :                         FTL_ERRLOG(dev, "Invalid chunk state\n");
    1702                 :          0 :                         goto error;
    1703                 :            :                 }
    1704                 :            : 
    1705                 :         85 :                 offset += nv_cache->chunk_blocks;
    1706                 :            :         }
    1707                 :            : 
    1708                 :         17 :         chunks_number = nv_cache->chunk_free_count + nv_cache->chunk_full_count +
    1709                 :         17 :                         nv_cache->chunk_inactive_count;
    1710         [ -  + ]:         17 :         assert(nv_cache->chunk_current == NULL);
    1711                 :            : 
    1712         [ -  + ]:         17 :         if (chunks_number != nv_cache->chunk_count) {
    1713         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Inconsistent NV cache metadata\n");
    1714                 :          0 :                 status = -EINVAL;
    1715                 :          0 :                 goto error;
    1716                 :            :         }
    1717                 :            : 
    1718                 :         17 :         status = sort_chunks(nv_cache);
    1719         [ -  + ]:         17 :         if (status) {
    1720         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "FTL NV Cache: sorting chunks ERROR\n");
    1721                 :            :         }
    1722                 :            : 
    1723         [ +  - ]:         17 :         FTL_NOTICELOG(dev, "FTL NV Cache: full chunks = %lu, empty chunks = %lu\n",
    1724                 :            :                       nv_cache->chunk_full_count, nv_cache->chunk_free_count);
    1725                 :            : 
    1726         [ +  - ]:         17 :         if (0 == status) {
    1727         [ +  - ]:         17 :                 FTL_NOTICELOG(dev, "FTL NV Cache: state loaded successfully\n");
    1728                 :            :         } else {
    1729         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "FTL NV Cache: loading state ERROR\n");
    1730                 :            :         }
    1731                 :            : 
    1732                 :            :         /* The number of active/inactive chunks calculated at initialization can change at this point due to metadata
    1733                 :            :          * upgrade. Recalculate the thresholds that depend on active chunk count.
    1734                 :            :          */
    1735                 :         17 :         ftl_nv_cache_init_update_limits(dev);
    1736                 :         17 : error:
    1737                 :         17 :         return status;
    1738                 :            : }
    1739                 :            : 
    1740                 :            : void
    1741                 :         23 : ftl_nv_cache_get_max_seq_id(struct ftl_nv_cache *nv_cache, uint64_t *open_seq_id,
    1742                 :            :                             uint64_t *close_seq_id)
    1743                 :            : {
    1744                 :         23 :         uint64_t i, o_seq_id = 0, c_seq_id = 0;
    1745                 :            :         struct ftl_nv_cache_chunk *chunk;
    1746                 :            : 
    1747                 :         23 :         chunk = nv_cache->chunks;
    1748         [ -  + ]:         23 :         assert(chunk);
    1749                 :            : 
    1750                 :            :         /* Iterate over chunks and get their max open and close seq id */
    1751         [ +  + ]:        138 :         for (i = 0; i < nv_cache->chunk_count; i++, chunk++) {
    1752                 :        115 :                 o_seq_id = spdk_max(o_seq_id, chunk->md->seq_id);
    1753                 :        115 :                 c_seq_id = spdk_max(c_seq_id, chunk->md->close_seq_id);
    1754                 :            :         }
    1755                 :            : 
    1756                 :         23 :         *open_seq_id = o_seq_id;
    1757                 :         23 :         *close_seq_id = c_seq_id;
    1758                 :         23 : }
    1759                 :            : 
    1760                 :            : typedef void (*ftl_chunk_ops_cb)(struct ftl_nv_cache_chunk *chunk, void *cntx, bool status);
    1761                 :            : 
    1762                 :            : static void
    1763                 :         17 : write_brq_end(struct spdk_bdev_io *bdev_io, bool success, void *arg)
    1764                 :            : {
    1765                 :         17 :         struct ftl_basic_rq *brq = arg;
    1766                 :         17 :         struct ftl_nv_cache_chunk *chunk = brq->io.chunk;
    1767                 :            : 
    1768                 :         17 :         ftl_stats_bdev_io_completed(brq->dev, FTL_STATS_TYPE_MD_NV_CACHE, bdev_io);
    1769                 :            : 
    1770                 :         17 :         brq->success = success;
    1771         [ +  - ]:         17 :         if (spdk_likely(success)) {
    1772                 :         17 :                 chunk_advance_blocks(chunk->nv_cache, chunk, brq->num_blocks);
    1773                 :            :         }
    1774                 :            : 
    1775                 :         17 :         spdk_bdev_free_io(bdev_io);
    1776                 :         17 :         brq->owner.cb(brq);
    1777                 :         17 : }
    1778                 :            : 
    1779                 :            : static void
    1780                 :         17 : _ftl_chunk_basic_rq_write(void *_brq)
    1781                 :            : {
    1782                 :         17 :         struct ftl_basic_rq *brq = _brq;
    1783                 :         17 :         struct ftl_nv_cache *nv_cache = brq->io.chunk->nv_cache;
    1784                 :         17 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    1785                 :            :         int rc;
    1786                 :            : 
    1787                 :         17 :         rc = ftl_nv_cache_bdev_write_blocks_with_md(dev, nv_cache->bdev_desc, nv_cache->cache_ioch,
    1788                 :            :                         brq->io_payload, NULL, brq->io.addr,
    1789                 :            :                         brq->num_blocks, write_brq_end, brq);
    1790         [ -  + ]:         17 :         if (spdk_unlikely(rc)) {
    1791         [ #  # ]:          0 :                 if (rc == -ENOMEM) {
    1792                 :          0 :                         struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(nv_cache->bdev_desc);
    1793                 :          0 :                         brq->io.bdev_io_wait.bdev = bdev;
    1794                 :          0 :                         brq->io.bdev_io_wait.cb_fn = _ftl_chunk_basic_rq_write;
    1795                 :          0 :                         brq->io.bdev_io_wait.cb_arg = brq;
    1796                 :          0 :                         spdk_bdev_queue_io_wait(bdev, nv_cache->cache_ioch, &brq->io.bdev_io_wait);
    1797                 :            :                 } else {
    1798                 :          0 :                         ftl_abort();
    1799                 :            :                 }
    1800                 :            :         }
    1801                 :         17 : }
    1802                 :            : 
    1803                 :            : static void
    1804                 :         17 : ftl_chunk_basic_rq_write(struct ftl_nv_cache_chunk *chunk, struct ftl_basic_rq *brq)
    1805                 :            : {
    1806                 :         17 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
    1807                 :         17 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    1808                 :            : 
    1809                 :         17 :         brq->io.chunk = chunk;
    1810                 :         17 :         brq->success = false;
    1811                 :            : 
    1812                 :         17 :         _ftl_chunk_basic_rq_write(brq);
    1813                 :            : 
    1814                 :         17 :         chunk->md->write_pointer += brq->num_blocks;
    1815                 :         17 :         dev->stats.io_activity_total += brq->num_blocks;
    1816                 :         17 : }
    1817                 :            : 
    1818                 :            : static void
    1819                 :          0 : read_brq_end(struct spdk_bdev_io *bdev_io, bool success, void *arg)
    1820                 :            : {
    1821                 :          0 :         struct ftl_basic_rq *brq = arg;
    1822                 :            : 
    1823                 :          0 :         ftl_stats_bdev_io_completed(brq->dev, FTL_STATS_TYPE_MD_NV_CACHE, bdev_io);
    1824                 :            : 
    1825                 :          0 :         brq->success = success;
    1826                 :            : 
    1827                 :          0 :         brq->owner.cb(brq);
    1828                 :          0 :         spdk_bdev_free_io(bdev_io);
    1829                 :          0 : }
    1830                 :            : 
    1831                 :            : static int
    1832                 :          0 : ftl_chunk_basic_rq_read(struct ftl_nv_cache_chunk *chunk, struct ftl_basic_rq *brq)
    1833                 :            : {
    1834                 :          0 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
    1835                 :          0 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    1836                 :            :         int rc;
    1837                 :            : 
    1838                 :          0 :         brq->io.chunk = chunk;
    1839                 :          0 :         brq->success = false;
    1840                 :            : 
    1841                 :          0 :         rc = ftl_nv_cache_bdev_read_blocks_with_md(dev, nv_cache->bdev_desc, nv_cache->cache_ioch,
    1842                 :            :                         brq->io_payload, NULL, brq->io.addr, brq->num_blocks, read_brq_end, brq);
    1843                 :            : 
    1844         [ #  # ]:          0 :         if (spdk_likely(!rc)) {
    1845                 :          0 :                 dev->stats.io_activity_total += brq->num_blocks;
    1846                 :            :         }
    1847                 :            : 
    1848                 :          0 :         return rc;
    1849                 :            : }
    1850                 :            : 
    1851                 :            : static void
    1852                 :         47 : chunk_open_cb(int status, void *ctx)
    1853                 :            : {
    1854                 :         47 :         struct ftl_nv_cache_chunk *chunk = (struct ftl_nv_cache_chunk *)ctx;
    1855                 :            : 
    1856         [ -  + ]:         47 :         if (spdk_unlikely(status)) {
    1857                 :            : #ifdef SPDK_FTL_RETRY_ON_ERROR
    1858                 :            :                 ftl_md_persist_entry_retry(&chunk->md_persist_entry_ctx);
    1859                 :            :                 return;
    1860                 :            : #else
    1861                 :          0 :                 ftl_abort();
    1862                 :            : #endif
    1863                 :            :         }
    1864                 :            : 
    1865                 :         47 :         chunk->md->state = FTL_CHUNK_STATE_OPEN;
    1866                 :         47 : }
    1867                 :            : 
    1868                 :            : static void
    1869                 :         47 : ftl_chunk_open(struct ftl_nv_cache_chunk *chunk)
    1870                 :            : {
    1871                 :         47 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    1872                 :         47 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1873                 :         47 :         struct ftl_layout_region *region = ftl_layout_region_get(dev, FTL_LAYOUT_REGION_TYPE_NVC_MD);
    1874                 :         47 :         struct ftl_md *md = dev->layout.md[FTL_LAYOUT_REGION_TYPE_NVC_MD];
    1875                 :            : 
    1876         [ -  + ]:         47 :         if (chunk_alloc_p2l_map(chunk)) {
    1877                 :          0 :                 assert(0);
    1878                 :            :                 /*
    1879                 :            :                  * We control number of opening chunk and it shall be consistent with size of chunk
    1880                 :            :                  * P2L map pool
    1881                 :            :                  */
    1882                 :            :                 ftl_abort();
    1883                 :            :                 return;
    1884                 :            :         }
    1885                 :            : 
    1886                 :         47 :         chunk->nv_cache->chunk_open_count++;
    1887                 :            : 
    1888         [ -  + ]:         47 :         assert(chunk->md->write_pointer == 0);
    1889         [ -  + ]:         47 :         assert(chunk->md->blocks_written == 0);
    1890                 :            : 
    1891   [ -  +  -  + ]:         47 :         memcpy(p2l_map->chunk_dma_md, chunk->md, region->entry_size * FTL_BLOCK_SIZE);
    1892                 :         47 :         p2l_map->chunk_dma_md->state = FTL_CHUNK_STATE_OPEN;
    1893                 :         47 :         p2l_map->chunk_dma_md->p2l_map_checksum = 0;
    1894                 :            : 
    1895                 :         47 :         ftl_md_persist_entries(md, get_chunk_idx(chunk), 1, p2l_map->chunk_dma_md,
    1896                 :            :                                NULL, chunk_open_cb, chunk,
    1897                 :            :                                &chunk->md_persist_entry_ctx);
    1898                 :            : }
    1899                 :            : 
    1900                 :            : static void
    1901                 :         15 : chunk_close_cb(int status, void *ctx)
    1902                 :            : {
    1903                 :         15 :         struct ftl_nv_cache_chunk *chunk = (struct ftl_nv_cache_chunk *)ctx;
    1904                 :            : 
    1905         [ -  + ]:         15 :         assert(chunk->md->write_pointer == chunk->nv_cache->chunk_blocks);
    1906                 :            : 
    1907         [ +  - ]:         15 :         if (spdk_likely(!status)) {
    1908                 :         15 :                 chunk->md->p2l_map_checksum = chunk->p2l_map.chunk_dma_md->p2l_map_checksum;
    1909                 :         15 :                 chunk_free_p2l_map(chunk);
    1910                 :            : 
    1911         [ -  + ]:         15 :                 assert(chunk->nv_cache->chunk_open_count > 0);
    1912                 :         15 :                 chunk->nv_cache->chunk_open_count--;
    1913                 :            : 
    1914                 :            :                 /* Chunk full move it on full list */
    1915                 :         15 :                 TAILQ_INSERT_TAIL(&chunk->nv_cache->chunk_full_list, chunk, entry);
    1916                 :         15 :                 chunk->nv_cache->chunk_full_count++;
    1917                 :            : 
    1918                 :         15 :                 chunk->nv_cache->last_seq_id = chunk->md->close_seq_id;
    1919                 :            : 
    1920                 :         15 :                 chunk->md->state = FTL_CHUNK_STATE_CLOSED;
    1921                 :            :         } else {
    1922                 :            : #ifdef SPDK_FTL_RETRY_ON_ERROR
    1923                 :            :                 ftl_md_persist_entry_retry(&chunk->md_persist_entry_ctx);
    1924                 :            : #else
    1925                 :          0 :                 ftl_abort();
    1926                 :            : #endif
    1927                 :            :         }
    1928                 :         15 : }
    1929                 :            : 
    1930                 :            : static void
    1931                 :         15 : chunk_map_write_cb(struct ftl_basic_rq *brq)
    1932                 :            : {
    1933                 :         15 :         struct ftl_nv_cache_chunk *chunk = brq->io.chunk;
    1934                 :         15 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1935                 :         15 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    1936                 :         15 :         struct ftl_layout_region *region = ftl_layout_region_get(dev, FTL_LAYOUT_REGION_TYPE_NVC_MD);
    1937                 :         15 :         struct ftl_md *md = dev->layout.md[FTL_LAYOUT_REGION_TYPE_NVC_MD];
    1938                 :            :         uint32_t chunk_map_crc;
    1939                 :            : 
    1940   [ -  +  +  - ]:         15 :         if (spdk_likely(brq->success)) {
    1941                 :         15 :                 chunk_map_crc = spdk_crc32c_update(p2l_map->chunk_map,
    1942                 :         15 :                                                    chunk->nv_cache->tail_md_chunk_blocks * FTL_BLOCK_SIZE, 0);
    1943   [ -  +  -  + ]:         15 :                 memcpy(p2l_map->chunk_dma_md, chunk->md, region->entry_size * FTL_BLOCK_SIZE);
    1944                 :         15 :                 p2l_map->chunk_dma_md->state = FTL_CHUNK_STATE_CLOSED;
    1945                 :         15 :                 p2l_map->chunk_dma_md->p2l_map_checksum = chunk_map_crc;
    1946                 :         15 :                 ftl_md_persist_entries(md, get_chunk_idx(chunk), 1, chunk->p2l_map.chunk_dma_md,
    1947                 :            :                                        NULL, chunk_close_cb, chunk,
    1948                 :            :                                        &chunk->md_persist_entry_ctx);
    1949                 :            :         } else {
    1950                 :            : #ifdef SPDK_FTL_RETRY_ON_ERROR
    1951                 :            :                 /* retry */
    1952                 :            :                 chunk->md->write_pointer -= brq->num_blocks;
    1953                 :            :                 ftl_chunk_basic_rq_write(chunk, brq);
    1954                 :            : #else
    1955                 :          0 :                 ftl_abort();
    1956                 :            : #endif
    1957                 :            :         }
    1958                 :         15 : }
    1959                 :            : 
    1960                 :            : static void
    1961                 :         15 : ftl_chunk_close(struct ftl_nv_cache_chunk *chunk)
    1962                 :            : {
    1963                 :         15 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    1964                 :         15 :         struct ftl_basic_rq *brq = &chunk->metadata_rq;
    1965                 :         15 :         void *metadata = chunk->p2l_map.chunk_map;
    1966                 :            : 
    1967                 :         15 :         chunk->md->close_seq_id = ftl_get_next_seq_id(dev);
    1968                 :         15 :         ftl_basic_rq_init(dev, brq, metadata, chunk->nv_cache->tail_md_chunk_blocks);
    1969                 :         15 :         ftl_basic_rq_set_owner(brq, chunk_map_write_cb, chunk);
    1970                 :            : 
    1971         [ -  + ]:         15 :         assert(chunk->md->write_pointer == chunk_tail_md_offset(chunk->nv_cache));
    1972                 :         15 :         brq->io.addr = chunk->offset + chunk->md->write_pointer;
    1973                 :            : 
    1974                 :         15 :         ftl_chunk_basic_rq_write(chunk, brq);
    1975                 :         15 : }
    1976                 :            : 
    1977                 :            : static int ftl_chunk_read_tail_md(struct ftl_nv_cache_chunk *chunk, struct ftl_basic_rq *brq,
    1978                 :            :                                   void (*cb)(struct ftl_basic_rq *brq), void *cb_ctx);
    1979                 :            : static void read_tail_md_cb(struct ftl_basic_rq *brq);
    1980                 :            : static void recover_open_chunk_cb(struct ftl_basic_rq *brq);
    1981                 :            : 
    1982                 :            : static void
    1983                 :          2 : restore_chunk_close_cb(int status, void *ctx)
    1984                 :            : {
    1985                 :          2 :         struct ftl_basic_rq *parent = (struct ftl_basic_rq *)ctx;
    1986                 :          2 :         struct ftl_nv_cache_chunk *chunk = parent->io.chunk;
    1987                 :          2 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    1988                 :            : 
    1989         [ -  + ]:          2 :         if (spdk_unlikely(status)) {
    1990                 :          0 :                 parent->success = false;
    1991                 :            :         } else {
    1992                 :          2 :                 chunk->md->p2l_map_checksum = p2l_map->chunk_dma_md->p2l_map_checksum;
    1993                 :          2 :                 chunk->md->state = FTL_CHUNK_STATE_CLOSED;
    1994                 :            :         }
    1995                 :            : 
    1996                 :          2 :         read_tail_md_cb(parent);
    1997                 :          2 : }
    1998                 :            : 
    1999                 :            : static void
    2000                 :          2 : restore_fill_p2l_map_cb(struct ftl_basic_rq *parent)
    2001                 :            : {
    2002                 :          2 :         struct ftl_nv_cache_chunk *chunk = parent->io.chunk;
    2003                 :          2 :         struct ftl_p2l_map *p2l_map = &chunk->p2l_map;
    2004                 :          2 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    2005                 :          2 :         struct ftl_md *md = dev->layout.md[FTL_LAYOUT_REGION_TYPE_NVC_MD];
    2006                 :          2 :         struct ftl_layout_region *region = ftl_layout_region_get(dev, FTL_LAYOUT_REGION_TYPE_NVC_MD);
    2007                 :            :         uint32_t chunk_map_crc;
    2008                 :            : 
    2009                 :            :         /* Set original callback */
    2010                 :          2 :         ftl_basic_rq_set_owner(parent, recover_open_chunk_cb, parent->owner.priv);
    2011                 :            : 
    2012   [ -  +  -  + ]:          2 :         if (spdk_unlikely(!parent->success)) {
    2013                 :          0 :                 read_tail_md_cb(parent);
    2014                 :          0 :                 return;
    2015                 :            :         }
    2016                 :            : 
    2017                 :          2 :         chunk_map_crc = spdk_crc32c_update(p2l_map->chunk_map,
    2018                 :          2 :                                            chunk->nv_cache->tail_md_chunk_blocks * FTL_BLOCK_SIZE, 0);
    2019   [ -  +  -  + ]:          2 :         memcpy(p2l_map->chunk_dma_md, chunk->md, region->entry_size * FTL_BLOCK_SIZE);
    2020                 :          2 :         p2l_map->chunk_dma_md->state = FTL_CHUNK_STATE_CLOSED;
    2021                 :          2 :         p2l_map->chunk_dma_md->write_pointer = chunk->nv_cache->chunk_blocks;
    2022                 :          2 :         p2l_map->chunk_dma_md->blocks_written = chunk->nv_cache->chunk_blocks;
    2023                 :          2 :         p2l_map->chunk_dma_md->p2l_map_checksum = chunk_map_crc;
    2024                 :            : 
    2025                 :          2 :         ftl_md_persist_entries(md, get_chunk_idx(chunk), 1, p2l_map->chunk_dma_md, NULL,
    2026                 :            :                                restore_chunk_close_cb, parent, &chunk->md_persist_entry_ctx);
    2027                 :            : }
    2028                 :            : 
    2029                 :            : static void
    2030                 :          2 : restore_fill_tail_md(struct ftl_basic_rq *parent, struct ftl_nv_cache_chunk *chunk)
    2031                 :            : {
    2032                 :          2 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    2033                 :            :         void *metadata;
    2034                 :            : 
    2035                 :          2 :         chunk->md->close_seq_id = ftl_get_next_seq_id(dev);
    2036                 :            : 
    2037                 :          2 :         metadata = chunk->p2l_map.chunk_map;
    2038                 :          2 :         ftl_basic_rq_init(dev, parent, metadata, chunk->nv_cache->tail_md_chunk_blocks);
    2039                 :          2 :         ftl_basic_rq_set_owner(parent, restore_fill_p2l_map_cb, parent->owner.priv);
    2040                 :            : 
    2041                 :          2 :         parent->io.addr = chunk->offset + chunk_tail_md_offset(chunk->nv_cache);
    2042                 :          2 :         parent->io.chunk = chunk;
    2043                 :            : 
    2044                 :          2 :         ftl_chunk_basic_rq_write(chunk, parent);
    2045                 :          2 : }
    2046                 :            : 
    2047                 :            : static void
    2048                 :       2046 : read_open_chunk_cb(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
    2049                 :            : {
    2050                 :       2046 :         struct ftl_rq *rq = (struct ftl_rq *)cb_arg;
    2051                 :       2046 :         struct ftl_basic_rq *parent = (struct ftl_basic_rq *)rq->owner.priv;
    2052                 :       2046 :         struct ftl_nv_cache_chunk *chunk = parent->io.chunk;
    2053                 :       2046 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
    2054                 :       2046 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    2055                 :            :         union ftl_md_vss *md;
    2056                 :       2046 :         uint64_t cache_offset = bdev_io->u.bdev.offset_blocks;
    2057                 :       2046 :         uint64_t len = bdev_io->u.bdev.num_blocks;
    2058                 :       2046 :         ftl_addr addr = ftl_addr_from_nvc_offset(dev, cache_offset);
    2059                 :            :         int rc;
    2060                 :            : 
    2061                 :       2046 :         ftl_stats_bdev_io_completed(dev, FTL_STATS_TYPE_USER, bdev_io);
    2062                 :            : 
    2063                 :       2046 :         spdk_bdev_free_io(bdev_io);
    2064                 :            : 
    2065         [ -  + ]:       2046 :         if (!success) {
    2066                 :          0 :                 parent->success = false;
    2067                 :          0 :                 read_tail_md_cb(parent);
    2068                 :          0 :                 return;
    2069                 :            :         }
    2070                 :            : 
    2071         [ +  + ]:     525822 :         while (rq->iter.idx < rq->iter.count) {
    2072                 :            :                 /* Get metadata */
    2073                 :     523776 :                 md = rq->entries[rq->iter.idx].io_md;
    2074         [ +  - ]:     523776 :                 if (md->nv_cache.seq_id != chunk->md->seq_id) {
    2075                 :     523776 :                         md->nv_cache.lba = FTL_LBA_INVALID;
    2076                 :            :                 }
    2077                 :            :                 /*
    2078                 :            :                  * The p2l map contains effectively random data at this point (since it contains arbitrary
    2079                 :            :                  * blocks from potentially not even filled tail md), so even LBA_INVALID needs to be set explicitly
    2080                 :            :                  */
    2081                 :            : 
    2082                 :     523776 :                 ftl_chunk_set_addr(chunk,  md->nv_cache.lba, addr + rq->iter.idx);
    2083                 :     523776 :                 rq->iter.idx++;
    2084                 :            :         }
    2085                 :            : 
    2086         [ +  + ]:       2046 :         if (cache_offset + len < chunk->offset + chunk_tail_md_offset(nv_cache)) {
    2087                 :       2044 :                 cache_offset += len;
    2088         [ +  + ]:       2044 :                 len = spdk_min(dev->xfer_size, chunk->offset + chunk_tail_md_offset(nv_cache) - cache_offset);
    2089                 :       2044 :                 rq->iter.idx = 0;
    2090                 :       2044 :                 rq->iter.count = len;
    2091                 :            : 
    2092                 :       2044 :                 rc = ftl_nv_cache_bdev_read_blocks_with_md(dev, nv_cache->bdev_desc,
    2093                 :            :                                 nv_cache->cache_ioch,
    2094                 :            :                                 rq->io_payload,
    2095                 :            :                                 rq->io_md,
    2096                 :            :                                 cache_offset, len,
    2097                 :            :                                 read_open_chunk_cb,
    2098                 :            :                                 rq);
    2099                 :            : 
    2100         [ -  + ]:       2044 :                 if (rc) {
    2101                 :          0 :                         ftl_rq_del(rq);
    2102                 :          0 :                         parent->success = false;
    2103                 :          0 :                         read_tail_md_cb(parent);
    2104                 :          0 :                         return;
    2105                 :            :                 }
    2106                 :            :         } else {
    2107                 :          2 :                 ftl_rq_del(rq);
    2108                 :          2 :                 restore_fill_tail_md(parent, chunk);
    2109                 :            :         }
    2110                 :            : }
    2111                 :            : 
    2112                 :            : static void
    2113                 :          2 : restore_open_chunk(struct ftl_nv_cache_chunk *chunk, struct ftl_basic_rq *parent)
    2114                 :            : {
    2115                 :          2 :         struct ftl_nv_cache *nv_cache = chunk->nv_cache;
    2116                 :          2 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(nv_cache, struct spdk_ftl_dev, nv_cache);
    2117                 :            :         struct ftl_rq *rq;
    2118                 :            :         uint64_t addr;
    2119                 :          2 :         uint64_t len = dev->xfer_size;
    2120                 :            :         int rc;
    2121                 :            : 
    2122                 :            :         /*
    2123                 :            :          * We've just read the p2l map, prefill it with INVALID LBA
    2124                 :            :          * TODO we need to do this because tail md blocks (p2l map) are also represented in the p2l map, instead of just user data region
    2125                 :            :          */
    2126         [ -  + ]:          2 :         memset(chunk->p2l_map.chunk_map, -1, FTL_BLOCK_SIZE * nv_cache->tail_md_chunk_blocks);
    2127                 :            : 
    2128                 :            :         /* Need to read user data, recalculate chunk's P2L and write tail md with it */
    2129                 :          2 :         rq = ftl_rq_new(dev, dev->nv_cache.md_size);
    2130         [ -  + ]:          2 :         if (!rq) {
    2131                 :          0 :                 parent->success = false;
    2132                 :          0 :                 read_tail_md_cb(parent);
    2133                 :          0 :                 return;
    2134                 :            :         }
    2135                 :            : 
    2136                 :          2 :         rq->owner.priv = parent;
    2137                 :          2 :         rq->iter.idx = 0;
    2138                 :          2 :         rq->iter.count = len;
    2139                 :            : 
    2140                 :          2 :         addr = chunk->offset;
    2141                 :            : 
    2142         [ +  - ]:          2 :         len = spdk_min(dev->xfer_size, chunk->offset + chunk_tail_md_offset(nv_cache) - addr);
    2143                 :            : 
    2144                 :          2 :         rc = ftl_nv_cache_bdev_read_blocks_with_md(dev, nv_cache->bdev_desc,
    2145                 :            :                         nv_cache->cache_ioch,
    2146                 :            :                         rq->io_payload,
    2147                 :            :                         rq->io_md,
    2148                 :            :                         addr, len,
    2149                 :            :                         read_open_chunk_cb,
    2150                 :            :                         rq);
    2151                 :            : 
    2152         [ -  + ]:          2 :         if (rc) {
    2153                 :          0 :                 ftl_rq_del(rq);
    2154                 :          0 :                 parent->success = false;
    2155                 :          0 :                 read_tail_md_cb(parent);
    2156                 :            :         }
    2157                 :            : }
    2158                 :            : 
    2159                 :            : static void
    2160                 :          2 : read_tail_md_cb(struct ftl_basic_rq *brq)
    2161                 :            : {
    2162                 :          2 :         brq->owner.cb(brq);
    2163                 :          2 : }
    2164                 :            : 
    2165                 :            : static int
    2166                 :          0 : ftl_chunk_read_tail_md(struct ftl_nv_cache_chunk *chunk, struct ftl_basic_rq *brq,
    2167                 :            :                        void (*cb)(struct ftl_basic_rq *brq), void *cb_ctx)
    2168                 :            : {
    2169                 :          0 :         struct spdk_ftl_dev *dev = SPDK_CONTAINEROF(chunk->nv_cache, struct spdk_ftl_dev, nv_cache);
    2170                 :            :         void *metadata;
    2171                 :            :         int rc;
    2172                 :            : 
    2173                 :          0 :         metadata = chunk->p2l_map.chunk_map;
    2174                 :          0 :         ftl_basic_rq_init(dev, brq, metadata, chunk->nv_cache->tail_md_chunk_blocks);
    2175                 :          0 :         ftl_basic_rq_set_owner(brq, cb, cb_ctx);
    2176                 :            : 
    2177                 :          0 :         brq->io.addr = chunk->offset + chunk_tail_md_offset(chunk->nv_cache);
    2178                 :          0 :         rc = ftl_chunk_basic_rq_read(chunk, brq);
    2179                 :            : 
    2180                 :          0 :         return rc;
    2181                 :            : }
    2182                 :            : 
    2183                 :            : struct restore_chunk_md_ctx {
    2184                 :            :         ftl_chunk_md_cb cb;
    2185                 :            :         void *cb_ctx;
    2186                 :            :         int status;
    2187                 :            :         uint64_t qd;
    2188                 :            :         uint64_t id;
    2189                 :            : };
    2190                 :            : 
    2191                 :            : static inline bool
    2192                 :          1 : is_chunk_count_valid(struct ftl_nv_cache *nv_cache)
    2193                 :            : {
    2194                 :          1 :         uint64_t chunk_count = 0;
    2195                 :            : 
    2196                 :          1 :         chunk_count += nv_cache->chunk_open_count;
    2197                 :          1 :         chunk_count += nv_cache->chunk_free_count;
    2198                 :          1 :         chunk_count += nv_cache->chunk_full_count;
    2199                 :          1 :         chunk_count += nv_cache->chunk_comp_count;
    2200                 :          1 :         chunk_count += nv_cache->chunk_inactive_count;
    2201                 :            : 
    2202                 :          1 :         return chunk_count == nv_cache->chunk_count;
    2203                 :            : }
    2204                 :            : 
    2205                 :            : static void
    2206                 :          0 : walk_tail_md_cb(struct ftl_basic_rq *brq)
    2207                 :            : {
    2208                 :          0 :         struct ftl_mngt_process *mngt = brq->owner.priv;
    2209                 :          0 :         struct ftl_nv_cache_chunk *chunk = brq->io.chunk;
    2210                 :          0 :         struct restore_chunk_md_ctx *ctx = ftl_mngt_get_step_ctx(mngt);
    2211                 :          0 :         int rc = 0;
    2212                 :            : 
    2213   [ #  #  #  # ]:          0 :         if (brq->success) {
    2214                 :          0 :                 rc = ctx->cb(chunk, ctx->cb_ctx);
    2215                 :            :         } else {
    2216                 :          0 :                 rc = -EIO;
    2217                 :            :         }
    2218                 :            : 
    2219         [ #  # ]:          0 :         if (rc) {
    2220                 :          0 :                 ctx->status = rc;
    2221                 :            :         }
    2222                 :          0 :         ctx->qd--;
    2223                 :          0 :         chunk_free_p2l_map(chunk);
    2224                 :          0 :         ftl_mngt_continue_step(mngt);
    2225                 :          0 : }
    2226                 :            : 
    2227                 :            : static void
    2228                 :          0 : ftl_mngt_nv_cache_walk_tail_md(struct spdk_ftl_dev *dev, struct ftl_mngt_process *mngt,
    2229                 :            :                                uint64_t seq_id, ftl_chunk_md_cb cb, void *cb_ctx)
    2230                 :            : {
    2231                 :          0 :         struct ftl_nv_cache *nvc = &dev->nv_cache;
    2232                 :            :         struct restore_chunk_md_ctx *ctx;
    2233                 :            : 
    2234                 :          0 :         ctx = ftl_mngt_get_step_ctx(mngt);
    2235         [ #  # ]:          0 :         if (!ctx) {
    2236         [ #  # ]:          0 :                 if (ftl_mngt_alloc_step_ctx(mngt, sizeof(*ctx))) {
    2237                 :          0 :                         ftl_mngt_fail_step(mngt);
    2238                 :          0 :                         return;
    2239                 :            :                 }
    2240                 :          0 :                 ctx = ftl_mngt_get_step_ctx(mngt);
    2241         [ #  # ]:          0 :                 assert(ctx);
    2242                 :            : 
    2243                 :          0 :                 ctx->cb = cb;
    2244                 :          0 :                 ctx->cb_ctx = cb_ctx;
    2245                 :            :         }
    2246                 :            : 
    2247                 :            :         /*
    2248                 :            :          * This function generates a high queue depth and will utilize ftl_mngt_continue_step during completions to make sure all chunks
    2249                 :            :          * are processed before returning an error (if any were found) or continuing on.
    2250                 :            :          */
    2251   [ #  #  #  # ]:          0 :         if (0 == ctx->qd && ctx->id == nvc->chunk_count) {
    2252         [ #  # ]:          0 :                 if (!is_chunk_count_valid(nvc)) {
    2253         [ #  # ]:          0 :                         FTL_ERRLOG(dev, "Recovery ERROR, invalid number of chunk\n");
    2254                 :          0 :                         assert(false);
    2255                 :            :                         ctx->status = -EINVAL;
    2256                 :            :                 }
    2257                 :            : 
    2258         [ #  # ]:          0 :                 if (ctx->status) {
    2259                 :          0 :                         ftl_mngt_fail_step(mngt);
    2260                 :            :                 } else {
    2261                 :          0 :                         ftl_mngt_next_step(mngt);
    2262                 :            :                 }
    2263                 :          0 :                 return;
    2264                 :            :         }
    2265                 :            : 
    2266         [ #  # ]:          0 :         while (ctx->id < nvc->chunk_count) {
    2267                 :          0 :                 struct ftl_nv_cache_chunk *chunk = &nvc->chunks[ctx->id];
    2268                 :            :                 int rc;
    2269                 :            : 
    2270   [ #  #  #  # ]:          0 :                 if (!chunk->recovery) {
    2271                 :            :                         /* This chunk is inactive or empty and not used in recovery */
    2272                 :          0 :                         ctx->id++;
    2273                 :          0 :                         continue;
    2274                 :            :                 }
    2275                 :            : 
    2276   [ #  #  #  # ]:          0 :                 if (seq_id && (chunk->md->close_seq_id <= seq_id)) {
    2277                 :          0 :                         ctx->id++;
    2278                 :          0 :                         continue;
    2279                 :            :                 }
    2280                 :            : 
    2281         [ #  # ]:          0 :                 if (chunk_alloc_p2l_map(chunk)) {
    2282                 :            :                         /* No more free P2L map, break and continue later */
    2283                 :          0 :                         break;
    2284                 :            :                 }
    2285                 :          0 :                 ctx->id++;
    2286                 :            : 
    2287                 :          0 :                 rc = ftl_chunk_read_tail_md(chunk, &chunk->metadata_rq, walk_tail_md_cb, mngt);
    2288                 :            : 
    2289         [ #  # ]:          0 :                 if (0 == rc) {
    2290                 :          0 :                         ctx->qd++;
    2291                 :            :                 } else {
    2292                 :          0 :                         chunk_free_p2l_map(chunk);
    2293                 :          0 :                         ctx->status = rc;
    2294                 :            :                 }
    2295                 :            :         }
    2296                 :            : 
    2297         [ #  # ]:          0 :         if (0 == ctx->qd) {
    2298                 :            :                 /*
    2299                 :            :                  * No QD could happen due to all leftover chunks being in free state.
    2300                 :            :                  * Additionally ftl_chunk_read_tail_md could fail starting with the first IO in a given patch.
    2301                 :            :                  * For streamlining of all potential error handling (since many chunks are reading P2L at the same time),
    2302                 :            :                  * we're using ftl_mngt_continue_step to arrive at the same spot of checking for mngt step end (see beginning of function).
    2303                 :            :                  */
    2304                 :          0 :                 ftl_mngt_continue_step(mngt);
    2305                 :            :         }
    2306                 :            : 
    2307                 :            : }
    2308                 :            : 
    2309                 :            : void
    2310                 :          0 : ftl_mngt_nv_cache_restore_l2p(struct spdk_ftl_dev *dev, struct ftl_mngt_process *mngt,
    2311                 :            :                               ftl_chunk_md_cb cb, void *cb_ctx)
    2312                 :            : {
    2313                 :          0 :         ftl_mngt_nv_cache_walk_tail_md(dev, mngt, dev->sb->ckpt_seq_id, cb, cb_ctx);
    2314                 :          0 : }
    2315                 :            : 
    2316                 :            : static void
    2317                 :          1 : restore_chunk_state_cb(struct spdk_ftl_dev *dev, struct ftl_md *md, int status)
    2318                 :            : {
    2319                 :          1 :         struct ftl_mngt_process *mngt = md->owner.cb_ctx;
    2320                 :          1 :         struct ftl_nv_cache *nvc = &dev->nv_cache;
    2321                 :            :         struct ftl_nv_cache_chunk *chunk;
    2322                 :            :         uint64_t i;
    2323                 :            : 
    2324         [ -  + ]:          1 :         if (status) {
    2325                 :            :                 /* Restore error, end step */
    2326                 :          0 :                 ftl_mngt_fail_step(mngt);
    2327                 :          0 :                 return;
    2328                 :            :         }
    2329                 :            : 
    2330         [ +  + ]:          6 :         for (i = 0; i < nvc->chunk_count; i++) {
    2331                 :          5 :                 chunk = &nvc->chunks[i];
    2332                 :            : 
    2333         [ +  + ]:          5 :                 if (false == nvc->nvc_type->ops.is_chunk_active(dev, chunk->offset) &&
    2334         [ -  + ]:          1 :                     chunk->md->state != FTL_CHUNK_STATE_INACTIVE) {
    2335                 :          0 :                         status = -EINVAL;
    2336                 :          0 :                         break;
    2337                 :            :                 }
    2338                 :            : 
    2339         [ -  + ]:          5 :                 if (chunk->md->version != FTL_NVC_VERSION_CURRENT) {
    2340                 :          0 :                         status = -EINVAL;
    2341                 :          0 :                         break;
    2342                 :            :                 }
    2343                 :            : 
    2344   [ +  +  -  +  :          5 :                 switch (chunk->md->state) {
                      - ]
    2345                 :          2 :                 case FTL_CHUNK_STATE_FREE:
    2346                 :          2 :                         break;
    2347                 :          2 :                 case FTL_CHUNK_STATE_OPEN:
    2348         [ +  - ]:          2 :                         TAILQ_REMOVE(&nvc->chunk_free_list, chunk, entry);
    2349                 :          2 :                         nvc->chunk_free_count--;
    2350                 :            : 
    2351                 :          2 :                         TAILQ_INSERT_TAIL(&nvc->chunk_open_list, chunk, entry);
    2352                 :          2 :                         nvc->chunk_open_count++;
    2353                 :            : 
    2354                 :            :                         /* Chunk is not empty, mark it to be recovered */
    2355                 :          2 :                         chunk->recovery = true;
    2356                 :          2 :                         break;
    2357                 :          0 :                 case FTL_CHUNK_STATE_CLOSED:
    2358         [ #  # ]:          0 :                         TAILQ_REMOVE(&nvc->chunk_free_list, chunk, entry);
    2359                 :          0 :                         nvc->chunk_free_count--;
    2360                 :            : 
    2361                 :          0 :                         TAILQ_INSERT_TAIL(&nvc->chunk_full_list, chunk, entry);
    2362                 :          0 :                         nvc->chunk_full_count++;
    2363                 :            : 
    2364                 :            :                         /* Chunk is not empty, mark it to be recovered */
    2365                 :          0 :                         chunk->recovery = true;
    2366                 :          0 :                         break;
    2367                 :          1 :                 case FTL_CHUNK_STATE_INACTIVE:
    2368                 :          1 :                         break;
    2369                 :          0 :                 default:
    2370                 :          0 :                         status = -EINVAL;
    2371                 :            :                 }
    2372                 :            :         }
    2373                 :            : 
    2374         [ -  + ]:          1 :         if (status) {
    2375                 :          0 :                 ftl_mngt_fail_step(mngt);
    2376                 :            :         } else {
    2377                 :          1 :                 ftl_mngt_next_step(mngt);
    2378                 :            :         }
    2379                 :            : }
    2380                 :            : 
    2381                 :            : void
    2382                 :          1 : ftl_mngt_nv_cache_restore_chunk_state(struct spdk_ftl_dev *dev, struct ftl_mngt_process *mngt)
    2383                 :            : {
    2384                 :          1 :         struct ftl_md *md = dev->layout.md[FTL_LAYOUT_REGION_TYPE_NVC_MD];
    2385                 :            : 
    2386                 :          1 :         md->owner.cb_ctx = mngt;
    2387                 :          1 :         md->cb = restore_chunk_state_cb;
    2388                 :          1 :         ftl_md_restore(md);
    2389                 :          1 : }
    2390                 :            : 
    2391                 :            : static void
    2392                 :          2 : recover_open_chunk_cb(struct ftl_basic_rq *brq)
    2393                 :            : {
    2394                 :          2 :         struct ftl_mngt_process *mngt = brq->owner.priv;
    2395                 :          2 :         struct ftl_nv_cache_chunk *chunk = brq->io.chunk;
    2396                 :          2 :         struct ftl_nv_cache *nvc = chunk->nv_cache;
    2397                 :          2 :         struct spdk_ftl_dev *dev = ftl_mngt_get_dev(mngt);
    2398                 :            : 
    2399                 :          2 :         chunk_free_p2l_map(chunk);
    2400                 :            : 
    2401   [ -  +  -  + ]:          2 :         if (!brq->success) {
    2402         [ #  # ]:          0 :                 FTL_ERRLOG(dev, "Recovery chunk ERROR, offset = %"PRIu64", seq id %"PRIu64"\n", chunk->offset,
    2403                 :            :                            chunk->md->seq_id);
    2404                 :          0 :                 ftl_mngt_fail_step(mngt);
    2405                 :          0 :                 return;
    2406                 :            :         }
    2407                 :            : 
    2408         [ +  - ]:          2 :         FTL_NOTICELOG(dev, "Recovered chunk, offset = %"PRIu64", seq id %"PRIu64"\n", chunk->offset,
    2409                 :            :                       chunk->md->seq_id);
    2410                 :            : 
    2411         [ +  + ]:          2 :         TAILQ_REMOVE(&nvc->chunk_open_list, chunk, entry);
    2412                 :          2 :         nvc->chunk_open_count--;
    2413                 :            : 
    2414                 :          2 :         TAILQ_INSERT_TAIL(&nvc->chunk_full_list, chunk, entry);
    2415                 :          2 :         nvc->chunk_full_count++;
    2416                 :            : 
    2417                 :            :         /* This is closed chunk */
    2418                 :          2 :         chunk->md->write_pointer = nvc->chunk_blocks;
    2419                 :          2 :         chunk->md->blocks_written = nvc->chunk_blocks;
    2420                 :            : 
    2421                 :          2 :         ftl_mngt_continue_step(mngt);
    2422                 :            : }
    2423                 :            : 
    2424                 :            : void
    2425                 :          3 : ftl_mngt_nv_cache_recover_open_chunk(struct spdk_ftl_dev *dev, struct ftl_mngt_process *mngt)
    2426                 :            : {
    2427                 :          3 :         struct ftl_nv_cache *nvc = &dev->nv_cache;
    2428                 :            :         struct ftl_nv_cache_chunk *chunk;
    2429                 :          3 :         struct ftl_basic_rq *brq = ftl_mngt_get_step_ctx(mngt);
    2430                 :            : 
    2431         [ +  + ]:          3 :         if (!brq) {
    2432         [ -  + ]:          1 :                 if (TAILQ_EMPTY(&nvc->chunk_open_list)) {
    2433         [ #  # ]:          0 :                         FTL_NOTICELOG(dev, "No open chunks to recover P2L\n");
    2434                 :          0 :                         ftl_mngt_next_step(mngt);
    2435                 :          0 :                         return;
    2436                 :            :                 }
    2437                 :            : 
    2438         [ -  + ]:          1 :                 if (ftl_mngt_alloc_step_ctx(mngt, sizeof(*brq))) {
    2439                 :          0 :                         ftl_mngt_fail_step(mngt);
    2440                 :          0 :                         return;
    2441                 :            :                 }
    2442                 :          1 :                 brq = ftl_mngt_get_step_ctx(mngt);
    2443                 :          1 :                 ftl_basic_rq_set_owner(brq, recover_open_chunk_cb, mngt);
    2444                 :            :         }
    2445                 :            : 
    2446         [ +  + ]:          3 :         if (TAILQ_EMPTY(&nvc->chunk_open_list)) {
    2447         [ -  + ]:          1 :                 if (!is_chunk_count_valid(nvc)) {
    2448         [ #  # ]:          0 :                         FTL_ERRLOG(dev, "Recovery ERROR, invalid number of chunk\n");
    2449                 :          0 :                         ftl_mngt_fail_step(mngt);
    2450                 :          0 :                         return;
    2451                 :            :                 }
    2452                 :            : 
    2453                 :            :                 /*
    2454                 :            :                  * Now all chunks loaded and closed, do final step of restoring
    2455                 :            :                  * chunks state
    2456                 :            :                  */
    2457         [ -  + ]:          1 :                 if (ftl_nv_cache_load_state(nvc)) {
    2458                 :          0 :                         ftl_mngt_fail_step(mngt);
    2459                 :            :                 } else {
    2460                 :          1 :                         ftl_mngt_next_step(mngt);
    2461                 :            :                 }
    2462                 :            :         } else {
    2463                 :          2 :                 chunk = TAILQ_FIRST(&nvc->chunk_open_list);
    2464         [ -  + ]:          2 :                 if (chunk_alloc_p2l_map(chunk)) {
    2465                 :          0 :                         ftl_mngt_fail_step(mngt);
    2466                 :          0 :                         return;
    2467                 :            :                 }
    2468                 :            : 
    2469                 :          2 :                 brq->io.chunk = chunk;
    2470                 :            : 
    2471         [ +  - ]:          2 :                 FTL_NOTICELOG(dev, "Start recovery open chunk, offset = %"PRIu64", seq id %"PRIu64"\n",
    2472                 :            :                               chunk->offset, chunk->md->seq_id);
    2473                 :          2 :                 restore_open_chunk(chunk, brq);
    2474                 :            :         }
    2475                 :            : }
    2476                 :            : 
    2477                 :            : int
    2478                 :       1053 : ftl_nv_cache_chunks_busy(struct ftl_nv_cache *nv_cache)
    2479                 :            : {
    2480                 :            :         /* chunk_current is migrating to closed status when closing, any others should already be
    2481                 :            :          * moved to free chunk list. Also need to wait for free md requests */
    2482   [ +  -  +  - ]:       1053 :         return nv_cache->chunk_open_count == 0 && nv_cache->chunk_free_persist_count == 0;
    2483                 :            : }
    2484                 :            : 
    2485                 :            : void
    2486                 :       4510 : ftl_nv_cache_halt(struct ftl_nv_cache *nv_cache)
    2487                 :            : {
    2488                 :            :         struct ftl_nv_cache_chunk *chunk;
    2489                 :            :         uint64_t free_space;
    2490                 :            : 
    2491                 :       4510 :         nv_cache->halt = true;
    2492                 :            : 
    2493                 :            :         /* Set chunks on open list back to free state since no user data has been written to it */
    2494         [ +  + ]:       4542 :         while (!TAILQ_EMPTY(&nv_cache->chunk_open_list)) {
    2495                 :         32 :                 chunk = TAILQ_FIRST(&nv_cache->chunk_open_list);
    2496                 :            : 
    2497                 :            :                 /* Chunks are moved between lists on metadata update submission, but state is changed
    2498                 :            :                  * on completion. Breaking early in such a case to make sure all the necessary resources
    2499                 :            :                  * will be freed (during next pass(es) of ftl_nv_cache_halt).
    2500                 :            :                  */
    2501         [ -  + ]:         32 :                 if (chunk->md->state != FTL_CHUNK_STATE_OPEN) {
    2502                 :          0 :                         break;
    2503                 :            :                 }
    2504                 :            : 
    2505         [ +  + ]:         32 :                 TAILQ_REMOVE(&nv_cache->chunk_open_list, chunk, entry);
    2506                 :         32 :                 chunk_free_p2l_map(chunk);
    2507                 :         32 :                 ftl_nv_cache_chunk_md_initialize(chunk->md);
    2508         [ -  + ]:         32 :                 assert(nv_cache->chunk_open_count > 0);
    2509                 :         32 :                 nv_cache->chunk_open_count--;
    2510                 :            :         }
    2511                 :            : 
    2512                 :            :         /* Close current chunk by skipping all not written blocks */
    2513                 :       4510 :         chunk = nv_cache->chunk_current;
    2514         [ +  + ]:       4510 :         if (chunk != NULL) {
    2515                 :         10 :                 nv_cache->chunk_current = NULL;
    2516         [ -  + ]:         10 :                 if (chunk_is_closed(chunk)) {
    2517                 :          0 :                         return;
    2518                 :            :                 }
    2519                 :            : 
    2520                 :         10 :                 free_space = chunk_get_free_space(nv_cache, chunk);
    2521                 :         10 :                 chunk->md->blocks_skipped = free_space;
    2522                 :         10 :                 chunk->md->blocks_written += free_space;
    2523                 :         10 :                 chunk->md->write_pointer += free_space;
    2524                 :         10 :                 ftl_chunk_close(chunk);
    2525                 :            :         }
    2526                 :            : }
    2527                 :            : 
    2528                 :            : uint64_t
    2529                 :          4 : ftl_nv_cache_acquire_trim_seq_id(struct ftl_nv_cache *nv_cache)
    2530                 :            : {
    2531                 :          4 :         struct ftl_nv_cache_chunk *chunk = nv_cache->chunk_current;
    2532                 :            :         uint64_t seq_id, free_space;
    2533                 :            : 
    2534         [ +  - ]:          4 :         if (!chunk) {
    2535                 :          4 :                 chunk = TAILQ_FIRST(&nv_cache->chunk_open_list);
    2536   [ +  -  +  - ]:          4 :                 if (chunk && chunk->md->state == FTL_CHUNK_STATE_OPEN) {
    2537                 :          4 :                         return chunk->md->seq_id;
    2538                 :            :                 } else {
    2539                 :          0 :                         return 0;
    2540                 :            :                 }
    2541                 :            :         }
    2542                 :            : 
    2543         [ #  # ]:          0 :         if (chunk_is_closed(chunk)) {
    2544                 :          0 :                 return 0;
    2545                 :            :         }
    2546                 :            : 
    2547                 :          0 :         seq_id = nv_cache->chunk_current->md->seq_id;
    2548                 :          0 :         free_space = chunk_get_free_space(nv_cache, chunk);
    2549                 :            : 
    2550                 :          0 :         chunk->md->blocks_skipped = free_space;
    2551                 :          0 :         chunk->md->blocks_written += free_space;
    2552                 :          0 :         chunk->md->write_pointer += free_space;
    2553         [ #  # ]:          0 :         if (chunk->md->blocks_written == chunk_tail_md_offset(nv_cache)) {
    2554                 :          0 :                 ftl_chunk_close(chunk);
    2555                 :            :         }
    2556                 :          0 :         nv_cache->chunk_current = NULL;
    2557                 :            : 
    2558                 :          0 :         seq_id++;
    2559                 :          0 :         return seq_id;
    2560                 :            : }
    2561                 :            : 
    2562                 :            : static double
    2563                 :         15 : ftl_nv_cache_get_chunk_utilization(struct ftl_nv_cache *nv_cache,
    2564                 :            :                                    struct ftl_nv_cache_chunk *chunk)
    2565                 :            : {
    2566                 :         15 :         double capacity = nv_cache->chunk_blocks;
    2567                 :         15 :         double used = chunk->md->blocks_written + chunk->md->blocks_skipped;
    2568                 :            : 
    2569                 :         15 :         return used / capacity;
    2570                 :            : }
    2571                 :            : 
    2572                 :            : static const char *
    2573                 :         15 : ftl_nv_cache_get_chunk_state_name(struct ftl_nv_cache_chunk *chunk)
    2574                 :            : {
    2575                 :            :         static const char *names[] = {
    2576                 :            :                 "FREE", "OPEN", "CLOSED", "INACTIVE"
    2577                 :            :         };
    2578                 :            : 
    2579         [ -  + ]:         15 :         assert(chunk->md->state < SPDK_COUNTOF(names));
    2580         [ +  - ]:         15 :         if (chunk->md->state < SPDK_COUNTOF(names)) {
    2581                 :         15 :                 return names[chunk->md->state];
    2582                 :            :         } else {
    2583                 :          0 :                 assert(false);
    2584                 :            :                 return "?";
    2585                 :            :         }
    2586                 :            : }
    2587                 :            : 
    2588                 :            : static void
    2589                 :          3 : ftl_property_dump_cache_dev(struct spdk_ftl_dev *dev, const struct ftl_property *property,
    2590                 :            :                             struct spdk_json_write_ctx *w)
    2591                 :            : {
    2592                 :            :         uint64_t i;
    2593                 :            :         struct ftl_nv_cache_chunk *chunk;
    2594                 :            : 
    2595                 :          3 :         spdk_json_write_named_string(w, "type", dev->nv_cache.nvc_type->name);
    2596                 :          3 :         spdk_json_write_named_array_begin(w, "chunks");
    2597         [ +  + ]:         18 :         for (i = 0, chunk = dev->nv_cache.chunks; i < dev->nv_cache.chunk_count; i++, chunk++) {
    2598                 :         15 :                 spdk_json_write_object_begin(w);
    2599                 :         15 :                 spdk_json_write_named_uint64(w, "id", i);
    2600                 :         15 :                 spdk_json_write_named_string(w, "state", ftl_nv_cache_get_chunk_state_name(chunk));
    2601                 :         15 :                 spdk_json_write_named_double(w, "utilization",
    2602                 :            :                                              ftl_nv_cache_get_chunk_utilization(&dev->nv_cache, chunk));
    2603                 :         15 :                 spdk_json_write_object_end(w);
    2604                 :            :         }
    2605                 :          3 :         spdk_json_write_array_end(w);
    2606                 :          3 : }
    2607                 :            : 
    2608                 :            : void
    2609                 :         95 : ftl_nv_cache_chunk_md_initialize(struct ftl_nv_cache_chunk_md *md)
    2610                 :            : {
    2611         [ -  + ]:         95 :         memset(md, 0, sizeof(*md));
    2612                 :         95 :         md->version = FTL_NVC_VERSION_CURRENT;
    2613                 :         95 : }

Generated by: LCOV version 1.14