LCOV - code coverage report
Current view: top level - spdk/lib/ftl - ftl_nv_cache.c (source / functions) Hit Total Coverage
Test: Combined Lines: 945 1440 65.6 %
Date: 2024-12-17 00:28:57 Functions: 96 104 92.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 328 6291 5.2 %

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

Generated by: LCOV version 1.15