LCOV - code coverage report
Current view: top level - spdk/lib/reduce - reduce.c (source / functions) Hit Total Coverage
Test: Combined Lines: 803 991 81.0 %
Date: 2024-12-14 17:45:12 Functions: 50 52 96.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2294 4700 48.8 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2018 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #include "spdk/reduce.h"
      10                 :            : #include "spdk/env.h"
      11                 :            : #include "spdk/string.h"
      12                 :            : #include "spdk/bit_array.h"
      13                 :            : #include "spdk/util.h"
      14                 :            : #include "spdk/log.h"
      15                 :            : #include "spdk/memory.h"
      16                 :            : 
      17                 :            : #include "libpmem.h"
      18                 :            : 
      19                 :            : /* Always round up the size of the PM region to the nearest cacheline. */
      20                 :            : #define REDUCE_PM_SIZE_ALIGNMENT        64
      21                 :            : 
      22                 :            : /* Offset into the backing device where the persistent memory file's path is stored. */
      23                 :            : #define REDUCE_BACKING_DEV_PATH_OFFSET  4096
      24                 :            : 
      25                 :            : #define REDUCE_EMPTY_MAP_ENTRY  -1ULL
      26                 :            : 
      27                 :            : #define REDUCE_NUM_VOL_REQUESTS 256
      28                 :            : 
      29                 :            : /* Structure written to offset 0 of both the pm file and the backing device. */
      30                 :            : struct spdk_reduce_vol_superblock {
      31                 :            :         uint8_t                         signature[8];
      32                 :            :         struct spdk_reduce_vol_params   params;
      33                 :            :         uint8_t                         reserved[4048];
      34                 :            : };
      35                 :            : SPDK_STATIC_ASSERT(sizeof(struct spdk_reduce_vol_superblock) == 4096, "size incorrect");
      36                 :            : 
      37                 :            : #define SPDK_REDUCE_SIGNATURE "SPDKREDU"
      38                 :            : /* null terminator counts one */
      39                 :            : SPDK_STATIC_ASSERT(sizeof(SPDK_REDUCE_SIGNATURE) - 1 ==
      40                 :            :                    SPDK_SIZEOF_MEMBER(struct spdk_reduce_vol_superblock, signature), "size incorrect");
      41                 :            : 
      42                 :            : #define REDUCE_PATH_MAX 4096
      43                 :            : 
      44                 :            : #define REDUCE_ZERO_BUF_SIZE 0x100000
      45                 :            : 
      46                 :            : /**
      47                 :            :  * Describes a persistent memory file used to hold metadata associated with a
      48                 :            :  *  compressed volume.
      49                 :            :  */
      50                 :            : struct spdk_reduce_pm_file {
      51                 :            :         char                    path[REDUCE_PATH_MAX];
      52                 :            :         void                    *pm_buf;
      53                 :            :         int                     pm_is_pmem;
      54                 :            :         uint64_t                size;
      55                 :            : };
      56                 :            : 
      57                 :            : #define REDUCE_IO_READV         1
      58                 :            : #define REDUCE_IO_WRITEV        2
      59                 :            : 
      60                 :            : struct spdk_reduce_chunk_map {
      61                 :            :         uint32_t                compressed_size;
      62                 :            :         uint32_t                reserved;
      63                 :            :         uint64_t                io_unit_index[0];
      64                 :            : };
      65                 :            : 
      66                 :            : struct spdk_reduce_vol_request {
      67                 :            :         /**
      68                 :            :          *  Scratch buffer used for uncompressed chunk.  This is used for:
      69                 :            :          *   1) source buffer for compression operations
      70                 :            :          *   2) destination buffer for decompression operations
      71                 :            :          *   3) data buffer when writing uncompressed chunk to disk
      72                 :            :          *   4) data buffer when reading uncompressed chunk from disk
      73                 :            :          */
      74                 :            :         uint8_t                                 *decomp_buf;
      75                 :            :         struct iovec                            *decomp_buf_iov;
      76                 :            : 
      77                 :            :         /**
      78                 :            :          * These are used to construct the iovecs that are sent to
      79                 :            :          *  the decomp engine, they point to a mix of the scratch buffer
      80                 :            :          *  and user buffer
      81                 :            :          */
      82                 :            :         struct iovec                            decomp_iov[REDUCE_MAX_IOVECS + 2];
      83                 :            :         int                                     decomp_iovcnt;
      84                 :            : 
      85                 :            :         /**
      86                 :            :          *  Scratch buffer used for compressed chunk.  This is used for:
      87                 :            :          *   1) destination buffer for compression operations
      88                 :            :          *   2) source buffer for decompression operations
      89                 :            :          *   3) data buffer when writing compressed chunk to disk
      90                 :            :          *   4) data buffer when reading compressed chunk from disk
      91                 :            :          */
      92                 :            :         uint8_t                                 *comp_buf;
      93                 :            :         struct iovec                            *comp_buf_iov;
      94                 :            :         struct iovec                            *iov;
      95                 :            :         bool                                    rmw;
      96                 :            :         struct spdk_reduce_vol                  *vol;
      97                 :            :         int                                     type;
      98                 :            :         int                                     reduce_errno;
      99                 :            :         int                                     iovcnt;
     100                 :            :         int                                     num_backing_ops;
     101                 :            :         uint32_t                                num_io_units;
     102                 :            :         bool                                    chunk_is_compressed;
     103                 :            :         bool                                    copy_after_decompress;
     104                 :            :         uint64_t                                offset;
     105                 :            :         uint64_t                                logical_map_index;
     106                 :            :         uint64_t                                length;
     107                 :            :         uint64_t                                chunk_map_index;
     108                 :            :         struct spdk_reduce_chunk_map            *chunk;
     109                 :            :         spdk_reduce_vol_op_complete             cb_fn;
     110                 :            :         void                                    *cb_arg;
     111                 :            :         TAILQ_ENTRY(spdk_reduce_vol_request)    tailq;
     112                 :            :         struct spdk_reduce_vol_cb_args          backing_cb_args;
     113                 :            : };
     114                 :            : 
     115                 :            : struct spdk_reduce_vol {
     116                 :            :         struct spdk_reduce_vol_params           params;
     117                 :            :         uint32_t                                backing_io_units_per_chunk;
     118                 :            :         uint32_t                                backing_lba_per_io_unit;
     119                 :            :         uint32_t                                logical_blocks_per_chunk;
     120                 :            :         struct spdk_reduce_pm_file              pm_file;
     121                 :            :         struct spdk_reduce_backing_dev          *backing_dev;
     122                 :            :         struct spdk_reduce_vol_superblock       *backing_super;
     123                 :            :         struct spdk_reduce_vol_superblock       *pm_super;
     124                 :            :         uint64_t                                *pm_logical_map;
     125                 :            :         uint64_t                                *pm_chunk_maps;
     126                 :            : 
     127                 :            :         struct spdk_bit_array                   *allocated_chunk_maps;
     128                 :            :         struct spdk_bit_array                   *allocated_backing_io_units;
     129                 :            : 
     130                 :            :         struct spdk_reduce_vol_request          *request_mem;
     131                 :            :         TAILQ_HEAD(, spdk_reduce_vol_request)   free_requests;
     132                 :            :         TAILQ_HEAD(, spdk_reduce_vol_request)   executing_requests;
     133                 :            :         TAILQ_HEAD(, spdk_reduce_vol_request)   queued_requests;
     134                 :            : 
     135                 :            :         /* Single contiguous buffer used for all request buffers for this volume. */
     136                 :            :         uint8_t                                 *buf_mem;
     137                 :            :         struct iovec                            *buf_iov_mem;
     138                 :            : };
     139                 :            : 
     140                 :            : static void _start_readv_request(struct spdk_reduce_vol_request *req);
     141                 :            : static void _start_writev_request(struct spdk_reduce_vol_request *req);
     142                 :            : static uint8_t *g_zero_buf;
     143                 :            : static int g_vol_count = 0;
     144                 :            : 
     145                 :            : /*
     146                 :            :  * Allocate extra metadata chunks and corresponding backing io units to account for
     147                 :            :  *  outstanding IO in worst case scenario where logical map is completely allocated
     148                 :            :  *  and no data can be compressed.  We need extra chunks in this case to handle
     149                 :            :  *  in-flight writes since reduce never writes data in place.
     150                 :            :  */
     151                 :            : #define REDUCE_NUM_EXTRA_CHUNKS 128
     152                 :            : 
     153                 :            : static void
     154                 :    1598114 : _reduce_persist(struct spdk_reduce_vol *vol, const void *addr, size_t len)
     155                 :            : {
     156   [ +  +  +  -  :    1598114 :         if (vol->pm_file.pm_is_pmem) {
             +  -  +  - ]
     157                 :        120 :                 pmem_persist(addr, len);
     158                 :         40 :         } else {
     159                 :    1597994 :                 pmem_msync(addr, len);
     160                 :            :         }
     161                 :    1598114 : }
     162                 :            : 
     163                 :            : static uint64_t
     164                 :        165 : _get_pm_logical_map_size(uint64_t vol_size, uint64_t chunk_size)
     165                 :            : {
     166                 :         47 :         uint64_t chunks_in_logical_map, logical_map_size;
     167                 :            : 
     168         [ +  + ]:        165 :         chunks_in_logical_map = vol_size / chunk_size;
     169                 :        165 :         logical_map_size = chunks_in_logical_map * sizeof(uint64_t);
     170                 :            : 
     171                 :            :         /* Round up to next cacheline. */
     172                 :        212 :         return spdk_divide_round_up(logical_map_size, REDUCE_PM_SIZE_ALIGNMENT) *
     173                 :            :                REDUCE_PM_SIZE_ALIGNMENT;
     174                 :         47 : }
     175                 :            : 
     176                 :            : static uint64_t
     177                 :    3150006 : _get_total_chunks(uint64_t vol_size, uint64_t chunk_size)
     178                 :            : {
     179                 :        336 :         uint64_t num_chunks;
     180                 :            : 
     181         [ +  + ]:    3150006 :         num_chunks = vol_size / chunk_size;
     182                 :    3150006 :         num_chunks += REDUCE_NUM_EXTRA_CHUNKS;
     183                 :            : 
     184                 :    3150342 :         return num_chunks;
     185                 :        336 : }
     186                 :            : 
     187                 :            : static inline uint32_t
     188                 :    3948955 : _reduce_vol_get_chunk_struct_size(uint64_t backing_io_units_per_chunk)
     189                 :            : {
     190                 :    3948955 :         return sizeof(struct spdk_reduce_chunk_map) + sizeof(uint64_t) * backing_io_units_per_chunk;
     191                 :            : }
     192                 :            : 
     193                 :            : static uint64_t
     194                 :         84 : _get_pm_total_chunks_size(uint64_t vol_size, uint64_t chunk_size, uint64_t backing_io_unit_size)
     195                 :            : {
     196                 :         24 :         uint64_t io_units_per_chunk, num_chunks, total_chunks_size;
     197                 :            : 
     198                 :         84 :         num_chunks = _get_total_chunks(vol_size, chunk_size);
     199         [ +  + ]:         84 :         io_units_per_chunk = chunk_size / backing_io_unit_size;
     200                 :            : 
     201                 :         84 :         total_chunks_size = num_chunks * _reduce_vol_get_chunk_struct_size(io_units_per_chunk);
     202                 :            : 
     203                 :        108 :         return spdk_divide_round_up(total_chunks_size, REDUCE_PM_SIZE_ALIGNMENT) *
     204                 :            :                REDUCE_PM_SIZE_ALIGNMENT;
     205                 :         24 : }
     206                 :            : 
     207                 :            : static struct spdk_reduce_chunk_map *
     208                 :    3149841 : _reduce_vol_get_chunk_map(struct spdk_reduce_vol *vol, uint64_t chunk_map_index)
     209                 :            : {
     210                 :        289 :         uintptr_t chunk_map_addr;
     211                 :            : 
     212   [ +  +  +  -  :    3149841 :         assert(chunk_map_index < _get_total_chunks(vol->params.vol_size, vol->params.chunk_size));
          +  -  +  -  +  
          -  +  -  +  -  
                   #  # ]
     213                 :            : 
     214   [ +  -  +  - ]:    3149841 :         chunk_map_addr = (uintptr_t)vol->pm_chunk_maps;
     215                 :    3150130 :         chunk_map_addr += chunk_map_index *
     216   [ +  -  +  - ]:    3149841 :                           _reduce_vol_get_chunk_struct_size(vol->backing_io_units_per_chunk);
     217                 :            : 
     218                 :    3150130 :         return (struct spdk_reduce_chunk_map *)chunk_map_addr;
     219                 :        289 : }
     220                 :            : 
     221                 :            : static int
     222                 :         72 : _validate_vol_params(struct spdk_reduce_vol_params *params)
     223                 :            : {
     224   [ +  +  +  -  :         72 :         if (params->vol_size > 0) {
                   -  + ]
     225                 :            :                 /**
     226                 :            :                  * User does not pass in the vol size - it gets calculated by libreduce from
     227                 :            :                  *  values in this structure plus the size of the backing device.
     228                 :            :                  */
     229                 :          0 :                 return -EINVAL;
     230                 :            :         }
     231                 :            : 
     232   [ +  -  +  -  :         92 :         if (params->chunk_size == 0 || params->backing_io_unit_size == 0 ||
          +  -  +  -  +  
             -  +  -  -  
                      + ]
     233   [ +  +  +  - ]:         72 :             params->logical_block_size == 0) {
     234                 :          0 :                 return -EINVAL;
     235                 :            :         }
     236                 :            : 
     237                 :            :         /* Chunk size must be an even multiple of the backing io unit size. */
     238   [ +  +  +  +  :         72 :         if ((params->chunk_size % params->backing_io_unit_size) != 0) {
          +  -  +  -  +  
                -  -  + ]
     239                 :          0 :                 return -EINVAL;
     240                 :            :         }
     241                 :            : 
     242                 :            :         /* Chunk size must be an even multiple of the logical block size. */
     243   [ +  +  +  +  :         72 :         if ((params->chunk_size % params->logical_block_size) != 0) {
          +  -  +  -  +  
                -  -  + ]
     244                 :          0 :                 return -1;
     245                 :            :         }
     246                 :            : 
     247                 :         72 :         return 0;
     248                 :         20 : }
     249                 :            : 
     250                 :            : static uint64_t
     251                 :         90 : _get_vol_size(uint64_t chunk_size, uint64_t backing_dev_size)
     252                 :            : {
     253                 :         26 :         uint64_t num_chunks;
     254                 :            : 
     255         [ +  + ]:         90 :         num_chunks = backing_dev_size / chunk_size;
     256         [ +  + ]:         90 :         if (num_chunks <= REDUCE_NUM_EXTRA_CHUNKS) {
     257                 :          3 :                 return 0;
     258                 :            :         }
     259                 :            : 
     260                 :         87 :         num_chunks -= REDUCE_NUM_EXTRA_CHUNKS;
     261                 :         87 :         return num_chunks * chunk_size;
     262                 :         26 : }
     263                 :            : 
     264                 :            : static uint64_t
     265                 :         84 : _get_pm_file_size(struct spdk_reduce_vol_params *params)
     266                 :            : {
     267                 :         24 :         uint64_t total_pm_size;
     268                 :            : 
     269                 :         84 :         total_pm_size = sizeof(struct spdk_reduce_vol_superblock);
     270   [ +  -  +  -  :         84 :         total_pm_size += _get_pm_logical_map_size(params->vol_size, params->chunk_size);
             +  -  +  - ]
     271   [ +  -  +  -  :        108 :         total_pm_size += _get_pm_total_chunks_size(params->vol_size, params->chunk_size,
             +  -  +  - ]
     272   [ +  -  +  - ]:         84 :                          params->backing_io_unit_size);
     273                 :        108 :         return total_pm_size;
     274                 :         24 : }
     275                 :            : 
     276                 :            : const struct spdk_uuid *
     277                 :          3 : spdk_reduce_vol_get_uuid(struct spdk_reduce_vol *vol)
     278                 :            : {
     279   [ +  -  +  - ]:          3 :         return &vol->params.uuid;
     280                 :            : }
     281                 :            : 
     282                 :            : static void
     283                 :         81 : _initialize_vol_pm_pointers(struct spdk_reduce_vol *vol)
     284                 :            : {
     285                 :         23 :         uint64_t logical_map_size;
     286                 :            : 
     287                 :            :         /* Superblock is at the beginning of the pm file. */
     288   [ +  -  +  -  :         81 :         vol->pm_super = (struct spdk_reduce_vol_superblock *)vol->pm_file.pm_buf;
          +  -  +  -  +  
                      - ]
     289                 :            : 
     290                 :            :         /* Logical map immediately follows the super block. */
     291   [ +  -  +  -  :         81 :         vol->pm_logical_map = (uint64_t *)(vol->pm_super + 1);
          +  -  +  -  +  
                      - ]
     292                 :            : 
     293                 :            :         /* Chunks maps follow the logical map. */
     294   [ +  -  +  -  :         81 :         logical_map_size = _get_pm_logical_map_size(vol->params.vol_size, vol->params.chunk_size);
          +  -  +  -  +  
                -  +  - ]
     295   [ +  -  +  -  :         81 :         vol->pm_chunk_maps = (uint64_t *)((uint8_t *)vol->pm_logical_map + logical_map_size);
          +  -  +  -  +  
                      - ]
     296                 :         81 : }
     297                 :            : 
     298                 :            : /* We need 2 iovs during load - one for the superblock, another for the path */
     299                 :            : #define LOAD_IOV_COUNT  2
     300                 :            : 
     301                 :            : struct reduce_init_load_ctx {
     302                 :            :         struct spdk_reduce_vol                  *vol;
     303                 :            :         struct spdk_reduce_vol_cb_args          backing_cb_args;
     304                 :            :         spdk_reduce_vol_op_with_handle_complete cb_fn;
     305                 :            :         void                                    *cb_arg;
     306                 :            :         struct iovec                            iov[LOAD_IOV_COUNT];
     307                 :            :         void                                    *path;
     308                 :            : };
     309                 :            : 
     310                 :            : static inline bool
     311                 :    1098453 : _addr_crosses_huge_page(const void *addr, size_t *size)
     312                 :            : {
     313                 :      13830 :         size_t _size;
     314                 :      13830 :         uint64_t rc;
     315                 :            : 
     316   [ +  +  #  # ]:    1098453 :         assert(size);
     317                 :            : 
     318         [ +  - ]:    1098453 :         _size = *size;
     319                 :    1098453 :         rc = spdk_vtophys(addr, size);
     320                 :            : 
     321   [ +  +  +  + ]:    1098453 :         return rc == SPDK_VTOPHYS_ERROR || _size != *size;
     322                 :      13830 : }
     323                 :            : 
     324                 :            : static inline int
     325                 :      47616 : _set_buffer(uint8_t **vol_buffer, uint8_t **_addr, uint8_t *addr_range, size_t buffer_size)
     326                 :            : {
     327                 :      13824 :         uint8_t *addr;
     328                 :      47616 :         size_t size_tmp = buffer_size;
     329                 :            : 
     330         [ +  - ]:      47616 :         addr = *_addr;
     331                 :            : 
     332                 :            :         /* Verify that addr + buffer_size doesn't cross huge page boundary */
     333         [ +  + ]:      47616 :         if (_addr_crosses_huge_page(addr, &size_tmp)) {
     334                 :            :                 /* Memory start is aligned on 2MiB, so buffer should be located at the end of the page.
     335                 :            :                  * Skip remaining bytes and continue from the beginning of the next page */
     336         [ +  - ]:         18 :                 addr += size_tmp;
     337                 :          6 :         }
     338                 :            : 
     339   [ +  +  -  + ]:      47616 :         if (addr + buffer_size > addr_range) {
     340                 :          0 :                 SPDK_ERRLOG("Vol buffer %p out of range %p\n", addr, addr_range);
     341                 :          0 :                 return -ERANGE;
     342                 :            :         }
     343                 :            : 
     344         [ +  - ]:      47616 :         *vol_buffer = addr;
     345   [ +  -  +  - ]:      47616 :         *_addr = addr + buffer_size;
     346                 :            : 
     347                 :      47616 :         return 0;
     348                 :      13824 : }
     349                 :            : 
     350                 :            : static int
     351                 :         93 : _allocate_vol_requests(struct spdk_reduce_vol *vol)
     352                 :            : {
     353                 :         27 :         struct spdk_reduce_vol_request *req;
     354                 :         27 :         uint32_t reqs_in_2mb_page, huge_pages_needed;
     355                 :         54 :         uint8_t *buffer, *buffer_end;
     356                 :         93 :         int i = 0;
     357                 :         93 :         int rc = 0;
     358                 :            : 
     359                 :            :         /* It is needed to allocate comp and decomp buffers so that they do not cross physical
     360                 :            :         * page boundaries. Assume that the system uses default 2MiB pages and chunk_size is not
     361                 :            :         * necessarily power of 2
     362                 :            :         * Allocate 2x since we need buffers for both read/write and compress/decompress
     363                 :            :         * intermediate buffers. */
     364   [ +  +  +  -  :         93 :         reqs_in_2mb_page = VALUE_2MB / (vol->params.chunk_size * 2);
          +  -  +  -  +  
                      - ]
     365         [ +  + ]:         93 :         if (!reqs_in_2mb_page) {
     366                 :          0 :                 return -EINVAL;
     367                 :            :         }
     368         [ +  + ]:         93 :         huge_pages_needed = SPDK_CEIL_DIV(REDUCE_NUM_VOL_REQUESTS, reqs_in_2mb_page);
     369                 :            : 
     370   [ +  -  +  -  :         93 :         vol->buf_mem = spdk_dma_malloc(VALUE_2MB * huge_pages_needed, VALUE_2MB, NULL);
             +  -  +  - ]
     371   [ +  +  +  -  :         93 :         if (vol->buf_mem == NULL) {
                   +  - ]
     372                 :          0 :                 return -ENOMEM;
     373                 :            :         }
     374                 :            : 
     375   [ +  -  +  - ]:         93 :         vol->request_mem = calloc(REDUCE_NUM_VOL_REQUESTS, sizeof(*req));
     376   [ +  +  +  -  :         93 :         if (vol->request_mem == NULL) {
                   +  - ]
     377   [ #  #  #  # ]:          0 :                 spdk_free(vol->buf_mem);
     378   [ #  #  #  # ]:          0 :                 vol->buf_mem = NULL;
     379                 :          0 :                 return -ENOMEM;
     380                 :            :         }
     381                 :            : 
     382                 :            :         /* Allocate 2x since we need iovs for both read/write and compress/decompress intermediate
     383                 :            :          *  buffers.
     384                 :            :          */
     385   [ +  -  +  - ]:         93 :         vol->buf_iov_mem = calloc(REDUCE_NUM_VOL_REQUESTS,
     386   [ +  -  +  - ]:         93 :                                   2 * sizeof(struct iovec) * vol->backing_io_units_per_chunk);
     387   [ +  +  +  -  :         93 :         if (vol->buf_iov_mem == NULL) {
                   +  - ]
     388   [ #  #  #  # ]:          0 :                 free(vol->request_mem);
     389   [ #  #  #  # ]:          0 :                 spdk_free(vol->buf_mem);
     390   [ #  #  #  # ]:          0 :                 vol->request_mem = NULL;
     391   [ #  #  #  # ]:          0 :                 vol->buf_mem = NULL;
     392                 :          0 :                 return -ENOMEM;
     393                 :            :         }
     394                 :            : 
     395   [ +  -  +  - ]:         93 :         buffer = vol->buf_mem;
     396   [ +  -  +  - ]:         93 :         buffer_end = buffer + VALUE_2MB * huge_pages_needed;
     397                 :            : 
     398   [ +  +  +  - ]:      23901 :         for (i = 0; i < REDUCE_NUM_VOL_REQUESTS; i++) {
     399   [ +  -  +  -  :      23808 :                 req = &vol->request_mem[i];
                   +  - ]
     400   [ +  +  +  -  :      23808 :                 TAILQ_INSERT_HEAD(&vol->free_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     401   [ +  -  +  -  :      23808 :                 req->decomp_buf_iov = &vol->buf_iov_mem[(2 * i) * vol->backing_io_units_per_chunk];
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     402   [ +  -  +  -  :      23808 :                 req->comp_buf_iov = &vol->buf_iov_mem[(2 * i + 1) * vol->backing_io_units_per_chunk];
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     403                 :            : 
     404   [ +  -  +  -  :      23808 :                 rc = _set_buffer(&req->comp_buf, &buffer, buffer_end, vol->params.chunk_size);
             +  -  +  - ]
     405         [ +  + ]:      23808 :                 if (rc) {
     406   [ #  #  #  # ]:          0 :                         SPDK_ERRLOG("Failed to set comp buffer for req idx %u, addr %p, start %p, end %p\n", i, buffer,
     407                 :            :                                     vol->buf_mem, buffer_end);
     408                 :          0 :                         break;
     409                 :            :                 }
     410   [ +  -  +  -  :      23808 :                 rc = _set_buffer(&req->decomp_buf, &buffer, buffer_end, vol->params.chunk_size);
             +  -  +  - ]
     411         [ +  + ]:      23808 :                 if (rc) {
     412   [ #  #  #  # ]:          0 :                         SPDK_ERRLOG("Failed to set decomp buffer for req idx %u, addr %p, start %p, end %p\n", i, buffer,
     413                 :            :                                     vol->buf_mem, buffer_end);
     414                 :          0 :                         break;
     415                 :            :                 }
     416                 :       6912 :         }
     417                 :            : 
     418         [ -  + ]:         93 :         if (rc) {
     419   [ #  #  #  # ]:          0 :                 free(vol->buf_iov_mem);
     420   [ #  #  #  # ]:          0 :                 free(vol->request_mem);
     421   [ #  #  #  # ]:          0 :                 spdk_free(vol->buf_mem);
     422   [ #  #  #  # ]:          0 :                 vol->buf_mem = NULL;
     423   [ #  #  #  # ]:          0 :                 vol->buf_iov_mem = NULL;
     424   [ #  #  #  # ]:          0 :                 vol->request_mem = NULL;
     425                 :          0 :         }
     426                 :            : 
     427                 :         93 :         return rc;
     428                 :         27 : }
     429                 :            : 
     430                 :            : static void
     431                 :        911 : _init_load_cleanup(struct spdk_reduce_vol *vol, struct reduce_init_load_ctx *ctx)
     432                 :            : {
     433         [ +  + ]:        911 :         if (ctx != NULL) {
     434   [ +  -  +  - ]:        803 :                 spdk_free(ctx->path);
     435                 :        803 :                 free(ctx);
     436                 :         25 :         }
     437                 :            : 
     438         [ +  + ]:        911 :         if (vol != NULL) {
     439   [ +  +  +  -  :        815 :                 if (vol->pm_file.pm_buf != NULL) {
             +  -  +  + ]
     440   [ +  -  +  -  :         81 :                         pmem_unmap(vol->pm_file.pm_buf, vol->pm_file.size);
          +  -  +  -  +  
                -  +  - ]
     441                 :         23 :                 }
     442                 :            : 
     443   [ +  -  +  - ]:        815 :                 spdk_free(vol->backing_super);
     444         [ +  - ]:        815 :                 spdk_bit_array_free(&vol->allocated_chunk_maps);
     445         [ +  - ]:        815 :                 spdk_bit_array_free(&vol->allocated_backing_io_units);
     446   [ +  -  +  - ]:        815 :                 free(vol->request_mem);
     447   [ +  -  +  - ]:        815 :                 free(vol->buf_iov_mem);
     448   [ +  -  +  - ]:        815 :                 spdk_free(vol->buf_mem);
     449                 :        815 :                 free(vol);
     450                 :         29 :         }
     451                 :        911 : }
     452                 :            : 
     453                 :            : static int
     454                 :        803 : _alloc_zero_buff(void)
     455                 :            : {
     456                 :        803 :         int rc = 0;
     457                 :            : 
     458                 :            :         /* The zero buffer is shared between all volumes and just used
     459                 :            :          * for reads so allocate one global instance here if not already
     460                 :            :          * allocated when another vol init'd or loaded.
     461                 :            :          */
     462   [ +  +  +  + ]:        803 :         if (g_vol_count++ == 0) {
     463                 :        174 :                 g_zero_buf = spdk_zmalloc(REDUCE_ZERO_BUF_SIZE,
     464                 :            :                                           64, NULL, SPDK_ENV_LCORE_ID_ANY,
     465                 :            :                                           SPDK_MALLOC_DMA);
     466         [ +  + ]:        174 :                 if (g_zero_buf == NULL) {
     467                 :          0 :                         rc = -ENOMEM;
     468                 :          0 :                 }
     469                 :         23 :         }
     470                 :        828 :         return rc;
     471                 :         25 : }
     472                 :            : 
     473                 :            : static void
     474                 :         54 : _init_write_super_cpl(void *cb_arg, int reduce_errno)
     475                 :            : {
     476                 :         54 :         struct reduce_init_load_ctx *init_ctx = cb_arg;
     477                 :         14 :         int rc;
     478                 :            : 
     479   [ +  -  +  - ]:         54 :         rc = _allocate_vol_requests(init_ctx->vol);
     480         [ -  + ]:         54 :         if (rc != 0) {
     481   [ #  #  #  #  :          0 :                 init_ctx->cb_fn(init_ctx->cb_arg, NULL, rc);
          #  #  #  #  #  
                #  #  # ]
     482   [ #  #  #  # ]:          0 :                 _init_load_cleanup(init_ctx->vol, init_ctx);
     483                 :          0 :                 return;
     484                 :            :         }
     485                 :            : 
     486                 :         54 :         rc = _alloc_zero_buff();
     487         [ -  + ]:         54 :         if (rc != 0) {
     488   [ #  #  #  #  :          0 :                 init_ctx->cb_fn(init_ctx->cb_arg, NULL, rc);
          #  #  #  #  #  
                #  #  # ]
     489   [ #  #  #  # ]:          0 :                 _init_load_cleanup(init_ctx->vol, init_ctx);
     490                 :          0 :                 return;
     491                 :            :         }
     492                 :            : 
     493   [ +  -  +  -  :         54 :         init_ctx->cb_fn(init_ctx->cb_arg, init_ctx->vol, reduce_errno);
          -  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
     494                 :            :         /* Only clean up the ctx - the vol has been passed to the application
     495                 :            :          *  for use now that initialization was successful.
     496                 :            :          */
     497                 :         54 :         _init_load_cleanup(NULL, init_ctx);
     498         [ -  + ]:         14 : }
     499                 :            : 
     500                 :            : static void
     501                 :         54 : _init_write_path_cpl(void *cb_arg, int reduce_errno)
     502                 :            : {
     503                 :         54 :         struct reduce_init_load_ctx *init_ctx = cb_arg;
     504   [ +  -  +  - ]:         54 :         struct spdk_reduce_vol *vol = init_ctx->vol;
     505                 :            : 
     506   [ +  -  +  -  :         54 :         init_ctx->iov[0].iov_base = vol->backing_super;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     507   [ +  -  +  -  :         54 :         init_ctx->iov[0].iov_len = sizeof(*vol->backing_super);
          +  -  +  -  +  
                      - ]
     508   [ +  -  +  -  :         54 :         init_ctx->backing_cb_args.cb_fn = _init_write_super_cpl;
                   +  - ]
     509   [ +  -  +  -  :         54 :         init_ctx->backing_cb_args.cb_arg = init_ctx;
                   +  - ]
     510   [ +  -  +  -  :        108 :         vol->backing_dev->writev(vol->backing_dev, init_ctx->iov, 1,
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     511   [ +  +  +  -  :         54 :                                  0, sizeof(*vol->backing_super) / vol->backing_dev->blocklen,
          +  -  +  -  +  
                      - ]
     512         [ +  - ]:         14 :                                  &init_ctx->backing_cb_args);
     513                 :         54 : }
     514                 :            : 
     515                 :            : static int
     516                 :         81 : _allocate_bit_arrays(struct spdk_reduce_vol *vol)
     517                 :            : {
     518                 :         23 :         uint64_t total_chunks, total_backing_io_units;
     519                 :         23 :         uint32_t i, num_metadata_io_units;
     520                 :            : 
     521   [ +  -  +  -  :         81 :         total_chunks = _get_total_chunks(vol->params.vol_size, vol->params.chunk_size);
          +  -  +  -  +  
                -  +  - ]
     522   [ +  -  +  - ]:         81 :         vol->allocated_chunk_maps = spdk_bit_array_create(total_chunks);
     523   [ +  +  +  -  :         81 :         total_backing_io_units = total_chunks * (vol->params.chunk_size / vol->params.backing_io_unit_size);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     524   [ +  -  +  - ]:         81 :         vol->allocated_backing_io_units = spdk_bit_array_create(total_backing_io_units);
     525                 :            : 
     526   [ +  -  +  +  :         81 :         if (vol->allocated_chunk_maps == NULL || vol->allocated_backing_io_units == NULL) {
          +  -  +  -  +  
                -  +  - ]
     527                 :          0 :                 return -ENOMEM;
     528                 :            :         }
     529                 :            : 
     530                 :            :         /* Set backing io unit bits associated with metadata. */
     531         [ +  - ]:         23 :         num_metadata_io_units = (sizeof(*vol->backing_super) + REDUCE_PATH_MAX) /
     532   [ +  +  +  -  :         81 :                                 vol->backing_dev->blocklen;
             +  -  +  - ]
     533         [ +  + ]:        999 :         for (i = 0; i < num_metadata_io_units; i++) {
     534   [ +  -  +  - ]:        918 :                 spdk_bit_array_set(vol->allocated_backing_io_units, i);
     535                 :        242 :         }
     536                 :            : 
     537                 :         81 :         return 0;
     538                 :         23 : }
     539                 :            : 
     540                 :            : void
     541                 :         60 : spdk_reduce_vol_init(struct spdk_reduce_vol_params *params,
     542                 :            :                      struct spdk_reduce_backing_dev *backing_dev,
     543                 :            :                      const char *pm_file_dir,
     544                 :            :                      spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg)
     545                 :            : {
     546                 :         16 :         struct spdk_reduce_vol *vol;
     547                 :         16 :         struct reduce_init_load_ctx *init_ctx;
     548                 :         16 :         uint64_t backing_dev_size;
     549                 :         32 :         size_t mapped_len;
     550                 :         16 :         int dir_len, max_dir_len, rc;
     551                 :            : 
     552                 :            :         /* We need to append a path separator and the UUID to the supplied
     553                 :            :          * path.
     554                 :            :          */
     555                 :         60 :         max_dir_len = REDUCE_PATH_MAX - SPDK_UUID_STRING_LEN - 1;
     556         [ +  + ]:         60 :         dir_len = strnlen(pm_file_dir, max_dir_len);
     557                 :            :         /* Strip trailing slash if the user provided one - we will add it back
     558                 :            :          * later when appending the filename.
     559                 :            :          */
     560   [ +  +  +  -  :         60 :         if (pm_file_dir[dir_len - 1] == '/') {
             +  -  +  - ]
     561         [ #  # ]:          0 :                 dir_len--;
     562                 :          0 :         }
     563         [ -  + ]:         60 :         if (dir_len == max_dir_len) {
     564                 :          0 :                 SPDK_ERRLOG("pm_file_dir (%s) too long\n", pm_file_dir);
     565   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -EINVAL);
     566                 :          2 :                 return;
     567                 :            :         }
     568                 :            : 
     569                 :         60 :         rc = _validate_vol_params(params);
     570         [ -  + ]:         60 :         if (rc != 0) {
     571                 :          0 :                 SPDK_ERRLOG("invalid vol params\n");
     572   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, rc);
     573                 :          0 :                 return;
     574                 :            :         }
     575                 :            : 
     576   [ +  -  +  -  :         60 :         backing_dev_size = backing_dev->blockcnt * backing_dev->blocklen;
             +  -  +  - ]
     577   [ +  -  +  -  :         60 :         params->vol_size = _get_vol_size(params->chunk_size, backing_dev_size);
             +  -  +  - ]
     578   [ +  +  +  -  :         60 :         if (params->vol_size == 0) {
                   +  + ]
     579                 :          3 :                 SPDK_ERRLOG("backing device is too small\n");
     580   [ -  +  +  - ]:          3 :                 cb_fn(cb_arg, NULL, -EINVAL);
     581                 :          3 :                 return;
     582                 :            :         }
     583                 :            : 
     584   [ +  +  +  -  :         71 :         if (backing_dev->readv == NULL || backing_dev->writev == NULL ||
          +  +  +  -  +  
             -  +  -  -  
                      + ]
     585   [ +  +  +  - ]:         54 :             backing_dev->unmap == NULL) {
     586                 :          3 :                 SPDK_ERRLOG("backing_dev function pointer not specified\n");
     587   [ -  +  +  - ]:          3 :                 cb_fn(cb_arg, NULL, -EINVAL);
     588                 :          3 :                 return;
     589                 :            :         }
     590                 :            : 
     591                 :         54 :         vol = calloc(1, sizeof(*vol));
     592         [ +  + ]:         54 :         if (vol == NULL) {
     593   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     594                 :          0 :                 return;
     595                 :            :         }
     596                 :            : 
     597   [ +  -  +  -  :         54 :         TAILQ_INIT(&vol->free_requests);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     598   [ +  -  +  -  :         54 :         TAILQ_INIT(&vol->executing_requests);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     599   [ +  -  +  -  :         54 :         TAILQ_INIT(&vol->queued_requests);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     600                 :            : 
     601   [ +  -  +  - ]:         54 :         vol->backing_super = spdk_zmalloc(sizeof(*vol->backing_super), 0, NULL,
     602                 :            :                                           SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     603   [ +  +  +  -  :         54 :         if (vol->backing_super == NULL) {
                   +  - ]
     604   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     605                 :          0 :                 _init_load_cleanup(vol, NULL);
     606                 :          0 :                 return;
     607                 :            :         }
     608                 :            : 
     609                 :         54 :         init_ctx = calloc(1, sizeof(*init_ctx));
     610         [ +  + ]:         54 :         if (init_ctx == NULL) {
     611   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     612                 :          0 :                 _init_load_cleanup(vol, NULL);
     613                 :          0 :                 return;
     614                 :            :         }
     615                 :            : 
     616   [ +  -  +  - ]:         54 :         init_ctx->path = spdk_zmalloc(REDUCE_PATH_MAX, 0, NULL,
     617                 :            :                                       SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     618   [ +  +  +  -  :         54 :         if (init_ctx->path == NULL) {
                   +  - ]
     619   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     620                 :          0 :                 _init_load_cleanup(vol, init_ctx);
     621                 :          0 :                 return;
     622                 :            :         }
     623                 :            : 
     624   [ +  +  +  + ]:         54 :         if (spdk_uuid_is_null(&params->uuid)) {
     625         [ +  - ]:         15 :                 spdk_uuid_generate(&params->uuid);
     626                 :          1 :         }
     627                 :            : 
     628   [ +  +  +  +  :         54 :         memcpy(vol->pm_file.path, pm_file_dir, dir_len);
             +  -  +  - ]
     629   [ +  -  +  -  :         54 :         vol->pm_file.path[dir_len] = '/';
          +  -  +  -  +  
                      - ]
     630   [ +  -  +  -  :         68 :         spdk_uuid_fmt_lower(&vol->pm_file.path[dir_len + 1], SPDK_UUID_STRING_LEN,
          +  -  +  -  +  
                      - ]
     631         [ +  - ]:         54 :                             &params->uuid);
     632   [ +  -  +  -  :         54 :         vol->pm_file.size = _get_pm_file_size(params);
                   +  - ]
     633   [ +  -  +  -  :         68 :         vol->pm_file.pm_buf = pmem_map_file(vol->pm_file.path, vol->pm_file.size,
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     634   [ -  +  +  -  :         14 :                                             PMEM_FILE_CREATE | PMEM_FILE_EXCL, 0600,
             -  +  +  - ]
     635   [ +  -  +  - ]:         14 :                                             &mapped_len, &vol->pm_file.pm_is_pmem);
     636   [ +  +  +  -  :         54 :         if (vol->pm_file.pm_buf == NULL) {
             +  -  +  - ]
     637   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("could not pmem_map_file(%s): %s\n",
                   #  # ]
     638                 :            :                             vol->pm_file.path, strerror(errno));
     639   [ #  #  #  #  :          0 :                 cb_fn(cb_arg, NULL, -errno);
             #  #  #  # ]
     640                 :          0 :                 _init_load_cleanup(vol, init_ctx);
     641                 :          0 :                 return;
     642                 :            :         }
     643                 :            : 
     644   [ +  +  +  -  :         54 :         if (vol->pm_file.size != mapped_len) {
             +  -  -  + ]
     645   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("could not map entire pmem file (size=%" PRIu64 " mapped=%" PRIu64 ")\n",
                   #  # ]
     646                 :            :                             vol->pm_file.size, mapped_len);
     647   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     648                 :          0 :                 _init_load_cleanup(vol, init_ctx);
     649                 :          0 :                 return;
     650                 :            :         }
     651                 :            : 
     652   [ +  +  +  -  :         54 :         vol->backing_io_units_per_chunk = params->chunk_size / params->backing_io_unit_size;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     653   [ +  +  +  -  :         54 :         vol->logical_blocks_per_chunk = params->chunk_size / params->logical_block_size;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     654   [ +  +  +  -  :         54 :         vol->backing_lba_per_io_unit = params->backing_io_unit_size / backing_dev->blocklen;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     655   [ +  +  +  +  :         54 :         memcpy(&vol->params, params, sizeof(*params));
                   +  - ]
     656                 :            : 
     657   [ +  -  +  - ]:         54 :         vol->backing_dev = backing_dev;
     658                 :            : 
     659                 :         54 :         rc = _allocate_bit_arrays(vol);
     660         [ +  + ]:         54 :         if (rc != 0) {
     661   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, rc);
     662                 :          0 :                 _init_load_cleanup(vol, init_ctx);
     663                 :          0 :                 return;
     664                 :            :         }
     665                 :            : 
     666   [ +  +  +  -  :         54 :         memcpy(vol->backing_super->signature, SPDK_REDUCE_SIGNATURE,
          +  -  +  -  +  
                      - ]
     667                 :            :                sizeof(vol->backing_super->signature));
     668   [ +  +  +  +  :         54 :         memcpy(&vol->backing_super->params, params, sizeof(*params));
          +  -  +  -  +  
                      - ]
     669                 :            : 
     670                 :         54 :         _initialize_vol_pm_pointers(vol);
     671                 :            : 
     672   [ +  +  +  +  :         54 :         memcpy(vol->pm_super, vol->backing_super, sizeof(*vol->backing_super));
          +  -  +  -  +  
                -  +  - ]
     673                 :            :         /* Writing 0xFF's is equivalent of filling it all with SPDK_EMPTY_MAP_ENTRY.
     674                 :            :          * Note that this writes 0xFF to not just the logical map but the chunk maps as well.
     675                 :            :          */
     676   [ +  +  +  -  :         54 :         memset(vol->pm_logical_map, 0xFF, vol->pm_file.size - sizeof(*vol->backing_super));
          +  -  +  -  +  
                -  +  - ]
     677   [ +  -  +  -  :         54 :         _reduce_persist(vol, vol->pm_file.pm_buf, vol->pm_file.size);
          +  -  +  -  +  
                -  +  - ]
     678                 :            : 
     679   [ +  -  +  - ]:         54 :         init_ctx->vol = vol;
     680   [ +  -  +  - ]:         54 :         init_ctx->cb_fn = cb_fn;
     681   [ +  -  +  - ]:         54 :         init_ctx->cb_arg = cb_arg;
     682                 :            : 
     683   [ +  +  +  +  :         54 :         memcpy(init_ctx->path, vol->pm_file.path, REDUCE_PATH_MAX);
          +  -  +  -  +  
                -  +  - ]
     684   [ +  -  +  -  :         54 :         init_ctx->iov[0].iov_base = init_ctx->path;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     685   [ +  -  +  -  :         54 :         init_ctx->iov[0].iov_len = REDUCE_PATH_MAX;
          +  -  +  -  +  
                      - ]
     686   [ +  -  +  -  :         54 :         init_ctx->backing_cb_args.cb_fn = _init_write_path_cpl;
                   +  - ]
     687   [ +  -  +  -  :         54 :         init_ctx->backing_cb_args.cb_arg = init_ctx;
                   +  - ]
     688                 :            :         /* Write path to offset 4K on backing device - just after where the super
     689                 :            :          *  block will be written.  We wait until this is committed before writing the
     690                 :            :          *  super block to guarantee we don't get the super block written without the
     691                 :            :          *  the path if the system crashed in the middle of a write operation.
     692                 :            :          */
     693   [ +  -  +  -  :        108 :         vol->backing_dev->writev(vol->backing_dev, init_ctx->iov, 1,
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     694   [ +  +  +  -  :         54 :                                  REDUCE_BACKING_DEV_PATH_OFFSET / vol->backing_dev->blocklen,
          +  -  +  -  +  
                      - ]
     695   [ +  +  +  -  :         54 :                                  REDUCE_PATH_MAX / vol->backing_dev->blocklen,
          +  -  +  -  +  
                      - ]
     696         [ +  - ]:         14 :                                  &init_ctx->backing_cb_args);
     697         [ -  + ]:         16 : }
     698                 :            : 
     699                 :            : static void destroy_load_cb(void *cb_arg, struct spdk_reduce_vol *vol, int reduce_errno);
     700                 :            : 
     701                 :            : static void
     702                 :        749 : _load_read_super_and_path_cpl(void *cb_arg, int reduce_errno)
     703                 :            : {
     704                 :        749 :         struct reduce_init_load_ctx *load_ctx = cb_arg;
     705   [ +  -  +  - ]:        749 :         struct spdk_reduce_vol *vol = load_ctx->vol;
     706                 :         11 :         uint64_t backing_dev_size;
     707                 :         11 :         uint64_t i, num_chunks, logical_map_index;
     708                 :         11 :         struct spdk_reduce_chunk_map *chunk;
     709                 :         22 :         size_t mapped_len;
     710                 :         11 :         uint32_t j;
     711                 :         11 :         int rc;
     712                 :            : 
     713                 :        749 :         rc = _alloc_zero_buff();
     714         [ -  + ]:        749 :         if (rc) {
     715                 :          0 :                 goto error;
     716                 :            :         }
     717                 :            : 
     718   [ +  +  +  +  :        749 :         if (memcmp(vol->backing_super->signature,
          +  -  +  -  +  
             -  +  +  +  
                      + ]
     719                 :            :                    SPDK_REDUCE_SIGNATURE,
     720                 :         11 :                    sizeof(vol->backing_super->signature)) != 0) {
     721                 :            :                 /* This backing device isn't a libreduce backing device. */
     722                 :        707 :                 rc = -EILSEQ;
     723                 :        707 :                 goto error;
     724                 :            :         }
     725                 :            : 
     726                 :            :         /* If the cb_fn is destroy_load_cb, it means we are wanting to destroy this compress bdev.
     727                 :            :          *  So don't bother getting the volume ready to use - invoke the callback immediately
     728                 :            :          *  so destroy_load_cb can delete the metadata off of the block device and delete the
     729                 :            :          *  persistent memory file if it exists.
     730                 :            :          */
     731   [ +  +  +  +  :         42 :         memcpy(vol->pm_file.path, load_ctx->path, sizeof(vol->pm_file.path));
          +  -  +  -  +  
                -  +  - ]
     732   [ +  +  +  -  :         42 :         if (load_ctx->cb_fn == (*destroy_load_cb)) {
                   +  + ]
     733   [ +  -  +  -  :         15 :                 load_ctx->cb_fn(load_ctx->cb_arg, vol, 0);
          -  +  +  -  +  
                -  +  - ]
     734                 :         15 :                 _init_load_cleanup(NULL, load_ctx);
     735                 :         24 :                 return;
     736                 :            :         }
     737                 :            : 
     738   [ +  +  +  +  :         27 :         memcpy(&vol->params, &vol->backing_super->params, sizeof(vol->params));
          +  -  +  -  +  
                -  +  - ]
     739   [ +  +  +  -  :         27 :         vol->backing_io_units_per_chunk = vol->params.chunk_size / vol->params.backing_io_unit_size;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     740   [ +  +  +  -  :         27 :         vol->logical_blocks_per_chunk = vol->params.chunk_size / vol->params.logical_block_size;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     741   [ +  +  +  -  :         27 :         vol->backing_lba_per_io_unit = vol->params.backing_io_unit_size / vol->backing_dev->blocklen;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     742                 :            : 
     743                 :         27 :         rc = _allocate_bit_arrays(vol);
     744         [ -  + ]:         27 :         if (rc != 0) {
     745                 :          0 :                 goto error;
     746                 :            :         }
     747                 :            : 
     748   [ +  -  +  -  :         27 :         backing_dev_size = vol->backing_dev->blockcnt * vol->backing_dev->blocklen;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     749   [ +  +  +  -  :         27 :         if (_get_vol_size(vol->params.chunk_size, backing_dev_size) < vol->params.vol_size) {
          +  -  +  -  +  
             -  +  -  -  
                      + ]
     750                 :          0 :                 SPDK_ERRLOG("backing device size %" PRIi64 " smaller than expected\n",
     751                 :            :                             backing_dev_size);
     752                 :          0 :                 rc = -EILSEQ;
     753                 :          0 :                 goto error;
     754                 :            :         }
     755                 :            : 
     756   [ +  -  +  -  :         27 :         vol->pm_file.size = _get_pm_file_size(&vol->params);
             +  -  +  - ]
     757   [ +  -  +  -  :         36 :         vol->pm_file.pm_buf = pmem_map_file(vol->pm_file.path, 0, 0, 0, &mapped_len,
          +  -  +  -  +  
                      - ]
     758   [ +  -  +  - ]:          9 :                                             &vol->pm_file.pm_is_pmem);
     759   [ +  +  +  -  :         27 :         if (vol->pm_file.pm_buf == NULL) {
             +  -  +  - ]
     760   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("could not pmem_map_file(%s): %s\n", vol->pm_file.path, strerror(errno));
                   #  # ]
     761   [ #  #  #  # ]:          0 :                 rc = -errno;
     762                 :          0 :                 goto error;
     763                 :            :         }
     764                 :            : 
     765   [ +  +  +  -  :         27 :         if (vol->pm_file.size != mapped_len) {
             +  -  -  + ]
     766   [ #  #  #  #  :          0 :                 SPDK_ERRLOG("could not map entire pmem file (size=%" PRIu64 " mapped=%" PRIu64 ")\n",
                   #  # ]
     767                 :            :                             vol->pm_file.size, mapped_len);
     768                 :          0 :                 rc = -ENOMEM;
     769                 :          0 :                 goto error;
     770                 :            :         }
     771                 :            : 
     772                 :         27 :         rc = _allocate_vol_requests(vol);
     773         [ -  + ]:         27 :         if (rc != 0) {
     774                 :          0 :                 goto error;
     775                 :            :         }
     776                 :            : 
     777                 :         27 :         _initialize_vol_pm_pointers(vol);
     778                 :            : 
     779   [ +  +  +  -  :         27 :         num_chunks = vol->params.vol_size / vol->params.chunk_size;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     780         [ +  + ]:       3483 :         for (i = 0; i < num_chunks; i++) {
     781   [ +  -  +  -  :       3456 :                 logical_map_index = vol->pm_logical_map[i];
             +  -  +  - ]
     782         [ +  + ]:       3456 :                 if (logical_map_index == REDUCE_EMPTY_MAP_ENTRY) {
     783                 :       3438 :                         continue;
     784                 :            :                 }
     785   [ +  -  +  - ]:         18 :                 spdk_bit_array_set(vol->allocated_chunk_maps, logical_map_index);
     786                 :         18 :                 chunk = _reduce_vol_get_chunk_map(vol, logical_map_index);
     787   [ +  +  +  -  :         90 :                 for (j = 0; j < vol->backing_io_units_per_chunk; j++) {
                   +  + ]
     788   [ +  +  +  -  :         72 :                         if (chunk->io_unit_index[j] != REDUCE_EMPTY_MAP_ENTRY) {
             +  -  +  + ]
     789   [ +  -  +  -  :         36 :                                 spdk_bit_array_set(vol->allocated_backing_io_units, chunk->io_unit_index[j]);
          +  -  +  -  +  
                      - ]
     790                 :         12 :                         }
     791                 :         24 :                 }
     792                 :          6 :         }
     793                 :            : 
     794   [ +  -  +  -  :         27 :         load_ctx->cb_fn(load_ctx->cb_arg, vol, 0);
          -  +  +  -  +  
                -  +  - ]
     795                 :            :         /* Only clean up the ctx - the vol has been passed to the application
     796                 :            :          *  for use now that volume load was successful.
     797                 :            :          */
     798                 :         27 :         _init_load_cleanup(NULL, load_ctx);
     799                 :         27 :         return;
     800                 :            : 
     801                 :        706 : error:
     802   [ +  -  +  -  :        707 :         load_ctx->cb_fn(load_ctx->cb_arg, NULL, rc);
          -  +  +  -  +  
                -  +  - ]
     803                 :        707 :         _init_load_cleanup(vol, load_ctx);
     804         [ -  + ]:         11 : }
     805                 :            : 
     806                 :            : void
     807                 :        749 : spdk_reduce_vol_load(struct spdk_reduce_backing_dev *backing_dev,
     808                 :            :                      spdk_reduce_vol_op_with_handle_complete cb_fn, void *cb_arg)
     809                 :            : {
     810                 :         11 :         struct spdk_reduce_vol *vol;
     811                 :         11 :         struct reduce_init_load_ctx *load_ctx;
     812                 :            : 
     813   [ +  -  +  -  :        760 :         if (backing_dev->readv == NULL || backing_dev->writev == NULL ||
          +  -  +  -  +  
             -  +  -  -  
                      + ]
     814   [ +  +  +  - ]:        749 :             backing_dev->unmap == NULL) {
     815                 :          0 :                 SPDK_ERRLOG("backing_dev function pointer not specified\n");
     816   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -EINVAL);
     817                 :          0 :                 return;
     818                 :            :         }
     819                 :            : 
     820                 :        749 :         vol = calloc(1, sizeof(*vol));
     821         [ +  + ]:        749 :         if (vol == NULL) {
     822   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     823                 :          0 :                 return;
     824                 :            :         }
     825                 :            : 
     826   [ +  -  +  -  :        749 :         TAILQ_INIT(&vol->free_requests);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     827   [ +  -  +  -  :        749 :         TAILQ_INIT(&vol->executing_requests);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     828   [ +  -  +  -  :        749 :         TAILQ_INIT(&vol->queued_requests);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     829                 :            : 
     830   [ +  -  +  - ]:        749 :         vol->backing_super = spdk_zmalloc(sizeof(*vol->backing_super), 64, NULL,
     831                 :            :                                           SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     832   [ +  +  +  -  :        749 :         if (vol->backing_super == NULL) {
                   +  - ]
     833                 :          0 :                 _init_load_cleanup(vol, NULL);
     834   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     835                 :          0 :                 return;
     836                 :            :         }
     837                 :            : 
     838   [ +  -  +  - ]:        749 :         vol->backing_dev = backing_dev;
     839                 :            : 
     840                 :        749 :         load_ctx = calloc(1, sizeof(*load_ctx));
     841         [ +  + ]:        749 :         if (load_ctx == NULL) {
     842                 :          0 :                 _init_load_cleanup(vol, NULL);
     843   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     844                 :          0 :                 return;
     845                 :            :         }
     846                 :            : 
     847   [ +  -  +  - ]:        749 :         load_ctx->path = spdk_zmalloc(REDUCE_PATH_MAX, 64, NULL,
     848                 :            :                                       SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     849   [ +  +  +  -  :        749 :         if (load_ctx->path == NULL) {
                   +  - ]
     850                 :          0 :                 _init_load_cleanup(vol, load_ctx);
     851   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
     852                 :          0 :                 return;
     853                 :            :         }
     854                 :            : 
     855   [ +  -  +  - ]:        749 :         load_ctx->vol = vol;
     856   [ +  -  +  - ]:        749 :         load_ctx->cb_fn = cb_fn;
     857   [ +  -  +  - ]:        749 :         load_ctx->cb_arg = cb_arg;
     858                 :            : 
     859   [ +  -  +  -  :        749 :         load_ctx->iov[0].iov_base = vol->backing_super;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     860   [ +  -  +  -  :        749 :         load_ctx->iov[0].iov_len = sizeof(*vol->backing_super);
          +  -  +  -  +  
                      - ]
     861   [ +  -  +  -  :        749 :         load_ctx->iov[1].iov_base = load_ctx->path;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     862   [ +  -  +  -  :        749 :         load_ctx->iov[1].iov_len = REDUCE_PATH_MAX;
          +  -  +  -  +  
                      - ]
     863   [ +  -  +  -  :        749 :         load_ctx->backing_cb_args.cb_fn = _load_read_super_and_path_cpl;
                   +  - ]
     864   [ +  -  +  -  :        749 :         load_ctx->backing_cb_args.cb_arg = load_ctx;
                   +  - ]
     865   [ +  -  +  -  :       1498 :         vol->backing_dev->readv(vol->backing_dev, load_ctx->iov, LOAD_IOV_COUNT, 0,
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     866         [ +  - ]:         11 :                                 (sizeof(*vol->backing_super) + REDUCE_PATH_MAX) /
     867   [ +  +  +  -  :        749 :                                 vol->backing_dev->blocklen,
             +  -  +  - ]
     868         [ +  - ]:         11 :                                 &load_ctx->backing_cb_args);
     869         [ -  + ]:         11 : }
     870                 :            : 
     871                 :            : void
     872                 :         96 : spdk_reduce_vol_unload(struct spdk_reduce_vol *vol,
     873                 :            :                        spdk_reduce_vol_op_complete cb_fn, void *cb_arg)
     874                 :            : {
     875         [ +  + ]:         96 :         if (vol == NULL) {
     876                 :            :                 /* This indicates a programming error. */
     877         [ #  # ]:          0 :                 assert(false);
     878                 :            :                 cb_fn(cb_arg, -EINVAL);
     879                 :            :                 return;
     880                 :            :         }
     881                 :            : 
     882   [ +  +  +  + ]:         96 :         if (--g_vol_count == 0) {
     883                 :         66 :                 spdk_free(g_zero_buf);
     884                 :         22 :         }
     885   [ +  +  #  # ]:         96 :         assert(g_vol_count >= 0);
     886                 :         96 :         _init_load_cleanup(vol, NULL);
     887   [ -  +  +  - ]:         96 :         cb_fn(cb_arg, 0);
     888                 :         24 : }
     889                 :            : 
     890                 :            : struct reduce_destroy_ctx {
     891                 :            :         spdk_reduce_vol_op_complete             cb_fn;
     892                 :            :         void                                    *cb_arg;
     893                 :            :         struct spdk_reduce_vol                  *vol;
     894                 :            :         struct spdk_reduce_vol_superblock       *super;
     895                 :            :         struct iovec                            iov;
     896                 :            :         struct spdk_reduce_vol_cb_args          backing_cb_args;
     897                 :            :         int                                     reduce_errno;
     898                 :            :         char                                    pm_path[REDUCE_PATH_MAX];
     899                 :            : };
     900                 :            : 
     901                 :            : static void
     902                 :         15 : destroy_unload_cpl(void *cb_arg, int reduce_errno)
     903                 :            : {
     904                 :         15 :         struct reduce_destroy_ctx *destroy_ctx = cb_arg;
     905                 :            : 
     906   [ +  -  +  -  :         15 :         if (destroy_ctx->reduce_errno == 0) {
                   -  + ]
     907   [ +  +  +  +  :         15 :                 if (unlink(destroy_ctx->pm_path)) {
                   -  + ]
     908   [ #  #  #  # ]:          0 :                         SPDK_ERRLOG("%s could not be unlinked: %s\n",
     909                 :            :                                     destroy_ctx->pm_path, strerror(errno));
     910                 :          0 :                 }
     911                 :          1 :         }
     912                 :            : 
     913                 :            :         /* Even if the unload somehow failed, we still pass the destroy_ctx
     914                 :            :          * reduce_errno since that indicates whether or not the volume was
     915                 :            :          * actually destroyed.
     916                 :            :          */
     917   [ +  -  +  -  :         15 :         destroy_ctx->cb_fn(destroy_ctx->cb_arg, destroy_ctx->reduce_errno);
          -  +  +  -  +  
          -  +  -  +  -  
                   +  - ]
     918   [ +  -  +  - ]:         15 :         spdk_free(destroy_ctx->super);
     919                 :         15 :         free(destroy_ctx);
     920                 :         15 : }
     921                 :            : 
     922                 :            : static void
     923                 :         15 : _destroy_zero_super_cpl(void *cb_arg, int reduce_errno)
     924                 :            : {
     925                 :         15 :         struct reduce_destroy_ctx *destroy_ctx = cb_arg;
     926   [ +  -  +  - ]:         15 :         struct spdk_reduce_vol *vol = destroy_ctx->vol;
     927                 :            : 
     928   [ +  -  +  - ]:         15 :         destroy_ctx->reduce_errno = reduce_errno;
     929                 :         15 :         spdk_reduce_vol_unload(vol, destroy_unload_cpl, destroy_ctx);
     930                 :         15 : }
     931                 :            : 
     932                 :            : static void
     933                 :         15 : destroy_load_cb(void *cb_arg, struct spdk_reduce_vol *vol, int reduce_errno)
     934                 :            : {
     935                 :         15 :         struct reduce_destroy_ctx *destroy_ctx = cb_arg;
     936                 :            : 
     937         [ -  + ]:         15 :         if (reduce_errno != 0) {
     938   [ #  #  #  #  :          0 :                 destroy_ctx->cb_fn(destroy_ctx->cb_arg, reduce_errno);
          #  #  #  #  #  
                #  #  # ]
     939   [ #  #  #  # ]:          0 :                 spdk_free(destroy_ctx->super);
     940                 :          0 :                 free(destroy_ctx);
     941                 :          0 :                 return;
     942                 :            :         }
     943                 :            : 
     944   [ +  -  +  - ]:         15 :         destroy_ctx->vol = vol;
     945   [ +  +  +  +  :         15 :         memcpy(destroy_ctx->pm_path, vol->pm_file.path, sizeof(destroy_ctx->pm_path));
          +  -  +  -  +  
                      - ]
     946   [ +  -  +  -  :         15 :         destroy_ctx->iov.iov_base = destroy_ctx->super;
          +  -  +  -  +  
                      - ]
     947   [ +  -  +  -  :         15 :         destroy_ctx->iov.iov_len = sizeof(*destroy_ctx->super);
                   +  - ]
     948   [ +  -  +  -  :         15 :         destroy_ctx->backing_cb_args.cb_fn = _destroy_zero_super_cpl;
                   +  - ]
     949   [ +  -  +  -  :         15 :         destroy_ctx->backing_cb_args.cb_arg = destroy_ctx;
                   +  - ]
     950   [ +  -  +  -  :         30 :         vol->backing_dev->writev(vol->backing_dev, &destroy_ctx->iov, 1, 0,
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
     951   [ +  +  +  -  :         15 :                                  sizeof(*destroy_ctx->super) / vol->backing_dev->blocklen,
          +  -  +  -  +  
                      - ]
     952         [ +  - ]:          1 :                                  &destroy_ctx->backing_cb_args);
     953         [ -  + ]:          1 : }
     954                 :            : 
     955                 :            : void
     956                 :         15 : spdk_reduce_vol_destroy(struct spdk_reduce_backing_dev *backing_dev,
     957                 :            :                         spdk_reduce_vol_op_complete cb_fn, void *cb_arg)
     958                 :            : {
     959                 :          1 :         struct reduce_destroy_ctx *destroy_ctx;
     960                 :            : 
     961                 :         15 :         destroy_ctx = calloc(1, sizeof(*destroy_ctx));
     962         [ +  + ]:         15 :         if (destroy_ctx == NULL) {
     963   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
     964                 :          0 :                 return;
     965                 :            :         }
     966                 :            : 
     967   [ +  -  +  - ]:         15 :         destroy_ctx->super = spdk_zmalloc(sizeof(*destroy_ctx->super), 64, NULL,
     968                 :            :                                           SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
     969   [ +  +  +  -  :         15 :         if (destroy_ctx->super == NULL) {
                   +  - ]
     970                 :          0 :                 free(destroy_ctx);
     971   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
     972                 :          0 :                 return;
     973                 :            :         }
     974   [ +  -  +  - ]:         15 :         destroy_ctx->cb_fn = cb_fn;
     975   [ +  -  +  - ]:         15 :         destroy_ctx->cb_arg = cb_arg;
     976                 :         15 :         spdk_reduce_vol_load(backing_dev, destroy_load_cb, destroy_ctx);
     977         [ -  + ]:          1 : }
     978                 :            : 
     979                 :            : static bool
     980                 :    2080763 : _request_spans_chunk_boundary(struct spdk_reduce_vol *vol, uint64_t offset, uint64_t length)
     981                 :            : {
     982                 :        273 :         uint64_t start_chunk, end_chunk;
     983                 :            : 
     984   [ +  +  +  -  :    2080763 :         start_chunk = offset / vol->logical_blocks_per_chunk;
                   +  - ]
     985   [ +  +  +  -  :    2080763 :         end_chunk = (offset + length - 1) / vol->logical_blocks_per_chunk;
                   +  - ]
     986                 :            : 
     987                 :    2081036 :         return (start_chunk != end_chunk);
     988                 :        273 : }
     989                 :            : 
     990                 :            : typedef void (*reduce_request_fn)(void *_req, int reduce_errno);
     991                 :            : 
     992                 :            : static void
     993                 :    2018166 : _reduce_vol_complete_req(struct spdk_reduce_vol_request *req, int reduce_errno)
     994                 :            : {
     995                 :        279 :         struct spdk_reduce_vol_request *next_req;
     996   [ +  -  +  - ]:    2018166 :         struct spdk_reduce_vol *vol = req->vol;
     997                 :            : 
     998   [ +  -  +  -  :    2018166 :         req->cb_fn(req->cb_arg, reduce_errno);
          -  +  +  -  +  
                -  +  - ]
     999   [ +  +  +  -  :    2018166 :         TAILQ_REMOVE(&vol->executing_requests, req, tailq);
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    1000                 :            : 
    1001   [ +  +  +  -  :    9144268 :         TAILQ_FOREACH(next_req, &vol->queued_requests, tailq) {
          +  -  +  +  #  
             #  #  #  #  
                      # ]
    1002   [ +  +  +  -  :    7689638 :                 if (next_req->logical_map_index == req->logical_map_index) {
          +  -  +  -  -  
                      + ]
    1003   [ +  +  +  -  :     563536 :                         TAILQ_REMOVE(&vol->queued_requests, next_req, tailq);
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    1004   [ +  +  +  -  :     563536 :                         if (next_req->type == REDUCE_IO_READV) {
                   -  + ]
    1005                 :     482423 :                                 _start_readv_request(next_req);
    1006                 :          0 :                         } else {
    1007   [ +  +  +  -  :      81113 :                                 assert(next_req->type == REDUCE_IO_WRITEV);
             +  -  #  # ]
    1008                 :      81113 :                                 _start_writev_request(next_req);
    1009                 :            :                         }
    1010                 :     563536 :                         break;
    1011                 :            :                 }
    1012                 :          0 :         }
    1013                 :            : 
    1014   [ +  +  +  -  :    2018166 :         TAILQ_INSERT_HEAD(&vol->free_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1015                 :    2018166 : }
    1016                 :            : 
    1017                 :            : static void
    1018                 :     799066 : _write_write_done(void *_req, int reduce_errno)
    1019                 :            : {
    1020                 :     799066 :         struct spdk_reduce_vol_request *req = _req;
    1021   [ +  -  +  - ]:     799066 :         struct spdk_reduce_vol *vol = req->vol;
    1022                 :         25 :         uint64_t old_chunk_map_index;
    1023                 :         25 :         struct spdk_reduce_chunk_map *old_chunk;
    1024                 :         25 :         uint32_t i;
    1025                 :            : 
    1026         [ +  + ]:     799066 :         if (reduce_errno != 0) {
    1027   [ #  #  #  # ]:          0 :                 req->reduce_errno = reduce_errno;
    1028                 :          0 :         }
    1029                 :            : 
    1030   [ +  +  +  -  :     799066 :         assert(req->num_backing_ops > 0);
             +  -  #  # ]
    1031   [ +  +  +  -  :     799066 :         if (--req->num_backing_ops > 0) {
                   +  + ]
    1032                 :         36 :                 return;
    1033                 :            :         }
    1034                 :            : 
    1035   [ +  +  +  -  :     799030 :         if (req->reduce_errno != 0) {
                   -  + ]
    1036   [ #  #  #  # ]:          0 :                 _reduce_vol_complete_req(req, req->reduce_errno);
    1037                 :          0 :                 return;
    1038                 :            :         }
    1039                 :            : 
    1040   [ +  -  +  -  :     799030 :         old_chunk_map_index = vol->pm_logical_map[req->logical_map_index];
          +  -  +  -  +  
                -  +  - ]
    1041         [ +  + ]:     799030 :         if (old_chunk_map_index != REDUCE_EMPTY_MAP_ENTRY) {
    1042                 :     736638 :                 old_chunk = _reduce_vol_get_chunk_map(vol, old_chunk_map_index);
    1043   [ +  +  +  -  :    1473294 :                 for (i = 0; i < vol->backing_io_units_per_chunk; i++) {
                   +  + ]
    1044   [ +  +  +  -  :    1473288 :                         if (old_chunk->io_unit_index[i] == REDUCE_EMPTY_MAP_ENTRY) {
             +  -  +  + ]
    1045                 :     736632 :                                 break;
    1046                 :            :                         }
    1047   [ +  +  +  -  :     736656 :                         assert(spdk_bit_array_get(vol->allocated_backing_io_units, old_chunk->io_unit_index[i]) == true);
          +  -  +  -  +  
             -  +  -  #  
                      # ]
    1048   [ +  -  +  -  :     736656 :                         spdk_bit_array_clear(vol->allocated_backing_io_units, old_chunk->io_unit_index[i]);
          +  -  +  -  +  
                      - ]
    1049   [ +  -  +  -  :     736656 :                         old_chunk->io_unit_index[i] = REDUCE_EMPTY_MAP_ENTRY;
                   +  - ]
    1050                 :         11 :                 }
    1051   [ +  -  +  - ]:     736638 :                 spdk_bit_array_clear(vol->allocated_chunk_maps, old_chunk_map_index);
    1052                 :          5 :         }
    1053                 :            : 
    1054                 :            :         /*
    1055                 :            :          * We don't need to persist the clearing of the old chunk map here.  The old chunk map
    1056                 :            :          * becomes invalid after we update the logical map, since the old chunk map will no
    1057                 :            :          * longer have a reference to it in the logical map.
    1058                 :            :          */
    1059                 :            : 
    1060                 :            :         /* Persist the new chunk map.  This must be persisted before we update the logical map. */
    1061   [ +  -  +  - ]:     799043 :         _reduce_persist(vol, req->chunk,
    1062   [ +  -  +  - ]:     799030 :                         _reduce_vol_get_chunk_struct_size(vol->backing_io_units_per_chunk));
    1063                 :            : 
    1064   [ +  -  +  -  :     799030 :         vol->pm_logical_map[req->logical_map_index] = req->chunk_map_index;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1065                 :            : 
    1066   [ +  -  +  -  :     799030 :         _reduce_persist(vol, &vol->pm_logical_map[req->logical_map_index], sizeof(uint64_t));
          +  -  +  -  +  
                      - ]
    1067                 :            : 
    1068                 :     799030 :         _reduce_vol_complete_req(req, 0);
    1069         [ -  + ]:         25 : }
    1070                 :            : 
    1071                 :            : static void
    1072                 :    2413173 : _issue_backing_ops(struct spdk_reduce_vol_request *req, struct spdk_reduce_vol *vol,
    1073                 :            :                    reduce_request_fn next_fn, bool is_write)
    1074                 :            : {
    1075                 :        274 :         struct iovec *iov;
    1076                 :        274 :         uint8_t *buf;
    1077                 :        274 :         uint32_t i;
    1078                 :            : 
    1079   [ +  +  +  +  :    2413173 :         if (req->chunk_is_compressed) {
             +  -  +  + ]
    1080   [ +  -  +  - ]:    2413161 :                 iov = req->comp_buf_iov;
    1081   [ +  -  +  - ]:    2413161 :                 buf = req->comp_buf;
    1082                 :        270 :         } else {
    1083   [ +  -  +  - ]:         12 :                 iov = req->decomp_buf_iov;
    1084   [ +  -  +  - ]:         12 :                 buf = req->decomp_buf;
    1085                 :            :         }
    1086                 :            : 
    1087   [ +  -  +  -  :    2413173 :         req->num_backing_ops = req->num_io_units;
             +  -  +  - ]
    1088   [ +  -  +  -  :    2413173 :         req->backing_cb_args.cb_fn = next_fn;
                   +  - ]
    1089   [ +  -  +  -  :    2413173 :         req->backing_cb_args.cb_arg = req;
                   +  - ]
    1090   [ +  +  +  -  :    4826382 :         for (i = 0; i < req->num_io_units; i++) {
                   +  + ]
    1091   [ +  -  +  -  :    2413209 :                 iov[i].iov_base = buf + i * vol->params.backing_io_unit_size;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1092   [ +  -  +  -  :    2413209 :                 iov[i].iov_len = vol->params.backing_io_unit_size;
          +  -  +  -  +  
                -  +  - ]
    1093   [ +  +  +  + ]:    2413209 :                 if (is_write) {
    1094   [ +  -  +  -  :    1598132 :                         vol->backing_dev->writev(vol->backing_dev, &iov[i], 1,
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1095   [ +  -  +  -  :     799066 :                                                  req->chunk->io_unit_index[i] * vol->backing_lba_per_io_unit,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1096   [ +  -  +  -  :         25 :                                                  vol->backing_lba_per_io_unit, &req->backing_cb_args);
                   +  - ]
    1097                 :         25 :                 } else {
    1098   [ +  -  +  -  :    3228286 :                         vol->backing_dev->readv(vol->backing_dev, &iov[i], 1,
          +  -  +  -  -  
          +  +  -  +  -  
             +  -  +  - ]
    1099   [ +  -  +  -  :    1614143 :                                                 req->chunk->io_unit_index[i] * vol->backing_lba_per_io_unit,
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1100   [ +  -  +  -  :        261 :                                                 vol->backing_lba_per_io_unit, &req->backing_cb_args);
                   +  - ]
    1101                 :            :                 }
    1102                 :        286 :         }
    1103                 :    2413173 : }
    1104                 :            : 
    1105                 :            : static void
    1106                 :     799030 : _reduce_vol_write_chunk(struct spdk_reduce_vol_request *req, reduce_request_fn next_fn,
    1107                 :            :                         uint32_t compressed_size)
    1108                 :            : {
    1109   [ +  -  +  - ]:     799030 :         struct spdk_reduce_vol *vol = req->vol;
    1110                 :         13 :         uint32_t i;
    1111                 :     799030 :         uint64_t chunk_offset, remainder, total_len = 0;
    1112                 :         13 :         uint8_t *buf;
    1113                 :         13 :         int j;
    1114                 :            : 
    1115   [ +  -  +  -  :     799030 :         req->chunk_map_index = spdk_bit_array_find_first_clear(vol->allocated_chunk_maps, 0);
             +  -  +  - ]
    1116                 :            : 
    1117                 :            :         /* TODO: fail if no chunk map found - but really this should not happen if we
    1118                 :            :          * size the number of requests similarly to number of extra chunk maps
    1119                 :            :          */
    1120   [ +  +  +  -  :     799030 :         assert(req->chunk_map_index != UINT32_MAX);
             +  -  #  # ]
    1121   [ +  -  +  -  :     799030 :         spdk_bit_array_set(vol->allocated_chunk_maps, req->chunk_map_index);
             +  -  +  - ]
    1122                 :            : 
    1123   [ +  -  +  -  :     799030 :         req->chunk = _reduce_vol_get_chunk_map(vol, req->chunk_map_index);
             +  -  +  - ]
    1124   [ +  -  +  - ]:     799043 :         req->num_io_units = spdk_divide_round_up(compressed_size,
    1125   [ +  -  +  -  :     799030 :                             vol->params.backing_io_unit_size);
                   +  - ]
    1126   [ +  -  +  -  :     799030 :         req->chunk_is_compressed = (req->num_io_units != vol->backing_io_units_per_chunk);
          +  -  +  -  +  
                -  +  - ]
    1127   [ +  -  +  -  :     799030 :         req->chunk->compressed_size =
             +  -  +  - ]
    1128   [ +  +  +  +  :     799030 :                 req->chunk_is_compressed ? compressed_size : vol->params.chunk_size;
          +  -  +  +  +  
             -  +  -  +  
                      - ]
    1129                 :            : 
    1130                 :            :         /* if the chunk is uncompressed we need to copy the data from the host buffers. */
    1131   [ +  +  +  +  :     799030 :         if (req->chunk_is_compressed == false) {
             +  -  +  + ]
    1132   [ +  +  +  -  :         12 :                 chunk_offset = req->offset % vol->logical_blocks_per_chunk;
          +  -  +  -  +  
                      - ]
    1133   [ +  -  +  - ]:         12 :                 buf = req->decomp_buf;
    1134   [ +  -  +  -  :         12 :                 total_len = chunk_offset * vol->params.logical_block_size;
                   +  - ]
    1135                 :            : 
    1136                 :            :                 /* zero any offset into chunk */
    1137   [ +  +  +  -  :         12 :                 if (req->rmw == false && chunk_offset) {
          +  +  +  -  +  
                      - ]
    1138         [ #  # ]:          0 :                         memset(buf, 0, total_len);
    1139                 :          0 :                 }
    1140         [ +  - ]:         12 :                 buf += total_len;
    1141                 :            : 
    1142                 :            :                 /* copy the data */
    1143   [ +  +  +  -  :         24 :                 for (j = 0; j < req->iovcnt; j++) {
             +  +  +  - ]
    1144   [ +  +  +  +  :         12 :                         memcpy(buf, req->iov[j].iov_base, req->iov[j].iov_len);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1145   [ +  -  +  -  :         12 :                         buf += req->iov[j].iov_len;
          +  -  +  -  +  
                -  +  - ]
    1146   [ +  -  +  -  :         12 :                         total_len += req->iov[j].iov_len;
          +  -  +  -  +  
                      - ]
    1147                 :          4 :                 }
    1148                 :            : 
    1149                 :            :                 /* zero any remainder */
    1150   [ +  -  +  -  :         12 :                 remainder = vol->params.chunk_size - total_len;
                   +  - ]
    1151                 :         12 :                 total_len += remainder;
    1152   [ +  +  +  -  :         12 :                 if (req->rmw == false && remainder) {
          +  +  +  -  +  
                      - ]
    1153         [ #  # ]:          0 :                         memset(buf, 0, remainder);
    1154                 :          0 :                 }
    1155   [ +  +  +  -  :         12 :                 assert(total_len == vol->params.chunk_size);
          +  -  +  -  #  
                      # ]
    1156                 :          4 :         }
    1157                 :            : 
    1158   [ +  +  +  -  :    1598096 :         for (i = 0; i < req->num_io_units; i++) {
                   +  + ]
    1159   [ +  -  +  -  :     799066 :                 req->chunk->io_unit_index[i] = spdk_bit_array_find_first_clear(vol->allocated_backing_io_units, 0);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1160                 :            :                 /* TODO: fail if no backing block found - but really this should also not
    1161                 :            :                  * happen (see comment above).
    1162                 :            :                  */
    1163   [ +  +  +  -  :     799066 :                 assert(req->chunk->io_unit_index[i] != UINT32_MAX);
          +  -  +  -  +  
             -  +  -  #  
                      # ]
    1164   [ +  -  +  -  :     799066 :                 spdk_bit_array_set(vol->allocated_backing_io_units, req->chunk->io_unit_index[i]);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1165                 :         25 :         }
    1166                 :            : 
    1167                 :     799030 :         _issue_backing_ops(req, vol, next_fn, true /* write */);
    1168                 :     799030 : }
    1169                 :            : 
    1170                 :            : static void
    1171                 :     799030 : _write_compress_done(void *_req, int reduce_errno)
    1172                 :            : {
    1173                 :     799030 :         struct spdk_reduce_vol_request *req = _req;
    1174                 :            : 
    1175                 :            :         /* Negative reduce_errno indicates failure for compression operations.
    1176                 :            :          * Just write the uncompressed data instead.  Force this to happen
    1177                 :            :          * by just passing the full chunk size to _reduce_vol_write_chunk.
    1178                 :            :          * When it sees the data couldn't be compressed, it will just write
    1179                 :            :          * the uncompressed buffer to disk.
    1180                 :            :          */
    1181         [ +  + ]:     799030 :         if (reduce_errno < 0) {
    1182   [ +  -  +  -  :         12 :                 req->backing_cb_args.output_size = req->vol->params.chunk_size;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1183                 :          4 :         }
    1184                 :            : 
    1185   [ +  -  +  -  :     799030 :         _reduce_vol_write_chunk(req, _write_write_done, req->backing_cb_args.output_size);
                   +  - ]
    1186                 :     799030 : }
    1187                 :            : 
    1188                 :            : static void
    1189                 :     799030 : _reduce_vol_compress_chunk(struct spdk_reduce_vol_request *req, reduce_request_fn next_fn)
    1190                 :            : {
    1191   [ +  -  +  - ]:     799030 :         struct spdk_reduce_vol *vol = req->vol;
    1192                 :            : 
    1193   [ +  -  +  -  :     799030 :         req->backing_cb_args.cb_fn = next_fn;
                   +  - ]
    1194   [ +  -  +  -  :     799030 :         req->backing_cb_args.cb_arg = req;
                   +  - ]
    1195   [ +  -  +  -  :     799030 :         req->comp_buf_iov[0].iov_base = req->comp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1196   [ +  -  +  -  :     799030 :         req->comp_buf_iov[0].iov_len = vol->params.chunk_size;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1197   [ +  -  +  -  :    1598060 :         vol->backing_dev->compress(vol->backing_dev,
          +  -  +  -  -  
          +  +  -  +  -  
                   +  - ]
    1198   [ +  -  +  -  :     799030 :                                    req->decomp_iov, req->decomp_iovcnt, req->comp_buf_iov, 1,
          +  -  +  -  +  
                      - ]
    1199         [ +  - ]:         13 :                                    &req->backing_cb_args);
    1200                 :     799030 : }
    1201                 :            : 
    1202                 :            : static void
    1203                 :     395031 : _reduce_vol_decompress_chunk_scratch(struct spdk_reduce_vol_request *req, reduce_request_fn next_fn)
    1204                 :            : {
    1205   [ +  -  +  - ]:     395031 :         struct spdk_reduce_vol *vol = req->vol;
    1206                 :            : 
    1207   [ +  -  +  -  :     395031 :         req->backing_cb_args.cb_fn = next_fn;
                   +  - ]
    1208   [ +  -  +  -  :     395031 :         req->backing_cb_args.cb_arg = req;
                   +  - ]
    1209   [ +  -  +  -  :     395031 :         req->comp_buf_iov[0].iov_base = req->comp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1210   [ +  -  +  -  :     395031 :         req->comp_buf_iov[0].iov_len = req->chunk->compressed_size;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1211   [ +  -  +  -  :     395031 :         req->decomp_buf_iov[0].iov_base = req->decomp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1212   [ +  -  +  -  :     395031 :         req->decomp_buf_iov[0].iov_len = vol->params.chunk_size;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1213   [ +  -  +  -  :     395034 :         vol->backing_dev->decompress(vol->backing_dev,
          +  -  +  -  -  
          +  +  -  +  -  
                   +  - ]
    1214   [ +  -  +  -  :          3 :                                      req->comp_buf_iov, 1, req->decomp_buf_iov, 1,
             +  -  +  - ]
    1215         [ +  - ]:          3 :                                      &req->backing_cb_args);
    1216                 :     395031 : }
    1217                 :            : 
    1218                 :            : static void
    1219                 :    1219136 : _reduce_vol_decompress_chunk(struct spdk_reduce_vol_request *req, reduce_request_fn next_fn)
    1220                 :            : {
    1221   [ +  -  +  - ]:    1219136 :         struct spdk_reduce_vol *vol = req->vol;
    1222                 :    1219136 :         uint64_t chunk_offset, remainder = 0;
    1223                 :    1219136 :         uint64_t ttl_len = 0;
    1224                 :        532 :         size_t iov_len;
    1225                 :        266 :         int i;
    1226                 :            : 
    1227   [ +  -  +  - ]:    1219136 :         req->decomp_iovcnt = 0;
    1228   [ +  +  +  -  :    1219136 :         chunk_offset = req->offset % vol->logical_blocks_per_chunk;
          +  -  +  -  +  
                      - ]
    1229                 :            : 
    1230                 :            :         /* If backing device doesn't support SGL output then we should copy the result of decompression to user's buffer
    1231                 :            :          * if at least one of the conditions below is true:
    1232                 :            :          * 1. User's buffer is fragmented
    1233                 :            :          * 2. Length of the user's buffer is less than the chunk
    1234                 :            :          * 3. User's buffer is contig, equals chunk_size but crosses huge page boundary */
    1235   [ +  -  +  -  :    1219136 :         iov_len = req->iov[0].iov_len;
          +  -  +  -  +  
                      - ]
    1236   [ +  +  +  +  :    2437479 :         req->copy_after_decompress = !vol->backing_dev->sgl_out && (req->iovcnt > 1 ||
          +  +  +  -  +  
          -  +  +  +  -  
          +  -  +  +  +  
                -  +  - ]
    1237   [ +  +  +  +  :    1914574 :                                      req->iov[0].iov_len < vol->params.chunk_size ||
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
    1238   [ +  -  +  -  :     696234 :                                      _addr_crosses_huge_page(req->iov[0].iov_base, &iov_len));
          +  -  +  -  +  
                      - ]
    1239   [ +  +  +  +  :    1219136 :         if (req->copy_after_decompress) {
             +  -  +  + ]
    1240   [ +  -  +  -  :     524317 :                 req->decomp_iov[0].iov_base = req->decomp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1241   [ +  -  +  -  :     524317 :                 req->decomp_iov[0].iov_len = vol->params.chunk_size;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1242   [ +  -  +  - ]:     524317 :                 req->decomp_iovcnt = 1;
    1243                 :     524317 :                 goto decompress;
    1244                 :            :         }
    1245                 :            : 
    1246         [ +  + ]:     694819 :         if (chunk_offset) {
    1247                 :            :                 /* first iov point to our scratch buffer for any offset into the chunk */
    1248   [ +  -  +  -  :        747 :                 req->decomp_iov[0].iov_base = req->decomp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1249   [ +  -  +  -  :        747 :                 req->decomp_iov[0].iov_len = chunk_offset * vol->params.logical_block_size;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1250   [ +  -  +  -  :        747 :                 ttl_len += req->decomp_iov[0].iov_len;
          +  -  +  -  +  
                      - ]
    1251   [ +  -  +  - ]:        747 :                 req->decomp_iovcnt = 1;
    1252                 :        249 :         }
    1253                 :            : 
    1254                 :            :         /* now the user data iov, direct to the user buffer */
    1255   [ +  +  +  -  :    1389647 :         for (i = 0; i < req->iovcnt; i++) {
             +  +  +  - ]
    1256   [ +  -  +  -  :     694828 :                 req->decomp_iov[i + req->decomp_iovcnt].iov_base = req->iov[i].iov_base;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1257   [ +  -  +  -  :     694828 :                 req->decomp_iov[i + req->decomp_iovcnt].iov_len = req->iov[i].iov_len;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1258   [ +  -  +  -  :     694828 :                 ttl_len += req->decomp_iov[i + req->decomp_iovcnt].iov_len;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1259                 :        265 :         }
    1260   [ +  -  +  -  :     694819 :         req->decomp_iovcnt += req->iovcnt;
          +  -  +  -  +  
                      - ]
    1261                 :            : 
    1262                 :            :         /* send the rest of the chunk to our scratch buffer */
    1263   [ +  -  +  -  :     694819 :         remainder = vol->params.chunk_size - ttl_len;
                   +  - ]
    1264         [ +  + ]:     694819 :         if (remainder) {
    1265   [ +  -  +  -  :        756 :                 req->decomp_iov[req->decomp_iovcnt].iov_base = req->decomp_buf + ttl_len;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
    1266   [ +  -  +  -  :        756 :                 req->decomp_iov[req->decomp_iovcnt].iov_len = remainder;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1267   [ +  -  +  -  :        756 :                 ttl_len += req->decomp_iov[req->decomp_iovcnt].iov_len;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1268   [ +  -  +  - ]:        756 :                 req->decomp_iovcnt++;
    1269                 :        252 :         }
    1270   [ +  +  +  -  :     694819 :         assert(ttl_len == vol->params.chunk_size);
          +  -  +  -  #  
                      # ]
    1271                 :            : 
    1272                 :    1218870 : decompress:
    1273   [ +  +  +  +  :    1219136 :         assert(!req->copy_after_decompress || (req->copy_after_decompress && req->decomp_iovcnt == 1));
          +  +  +  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  #  # ]
    1274   [ +  -  +  -  :    1219136 :         req->backing_cb_args.cb_fn = next_fn;
                   +  - ]
    1275   [ +  -  +  -  :    1219136 :         req->backing_cb_args.cb_arg = req;
                   +  - ]
    1276   [ +  -  +  -  :    1219136 :         req->comp_buf_iov[0].iov_base = req->comp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1277   [ +  -  +  -  :    1219136 :         req->comp_buf_iov[0].iov_len = req->chunk->compressed_size;
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1278   [ +  -  +  -  :    2438272 :         vol->backing_dev->decompress(vol->backing_dev,
          +  -  +  -  -  
          +  +  -  +  -  
                   +  - ]
    1279   [ +  -  +  -  :    1219136 :                                      req->comp_buf_iov, 1, req->decomp_iov, req->decomp_iovcnt,
          +  -  +  -  +  
                      - ]
    1280         [ +  - ]:        266 :                                      &req->backing_cb_args);
    1281                 :    1219136 : }
    1282                 :            : 
    1283                 :            : static inline void
    1284                 :     444424 : _prepare_compress_chunk_copy_user_buffers(struct spdk_reduce_vol_request *req, bool zero_paddings)
    1285                 :            : {
    1286   [ +  -  +  - ]:     444424 :         struct spdk_reduce_vol *vol = req->vol;
    1287   [ +  +  +  +  :     444424 :         char *padding_buffer = zero_paddings ? g_zero_buf : req->decomp_buf;
             +  -  +  - ]
    1288                 :     444424 :         uint64_t chunk_offset, ttl_len = 0;
    1289                 :     444424 :         uint64_t remainder = 0;
    1290                 :     444424 :         char *copy_offset = NULL;
    1291   [ +  -  +  -  :     444424 :         uint32_t lbsize = vol->params.logical_block_size;
                   +  - ]
    1292                 :          8 :         int i;
    1293                 :            : 
    1294   [ +  -  +  -  :     444424 :         req->decomp_iov[0].iov_base = req->decomp_buf;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1295   [ +  -  +  -  :     444424 :         req->decomp_iov[0].iov_len = vol->params.chunk_size;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1296   [ +  -  +  - ]:     444424 :         req->decomp_iovcnt = 1;
    1297   [ +  -  +  -  :     444424 :         copy_offset = req->decomp_iov[0].iov_base;
          +  -  +  -  +  
                      - ]
    1298   [ +  +  +  -  :     444424 :         chunk_offset = req->offset % vol->logical_blocks_per_chunk;
          +  -  +  -  +  
                      - ]
    1299                 :            : 
    1300         [ +  + ]:     444424 :         if (chunk_offset) {
    1301                 :     333816 :                 ttl_len += chunk_offset * lbsize;
    1302                 :            :                 /* copy_offset already points to padding buffer if zero_paddings=false */
    1303   [ +  +  +  + ]:     333816 :                 if (zero_paddings) {
    1304   [ +  +  +  + ]:      26740 :                         memcpy(copy_offset, padding_buffer, ttl_len);
    1305                 :          1 :                 }
    1306         [ +  - ]:     333816 :                 copy_offset += ttl_len;
    1307                 :          2 :         }
    1308                 :            : 
    1309                 :            :         /* now the user data iov, direct from the user buffer */
    1310   [ +  +  +  -  :     888926 :         for (i = 0; i < req->iovcnt; i++) {
             +  +  +  - ]
    1311   [ +  +  +  +  :     444502 :                 memcpy(copy_offset, req->iov[i].iov_base, req->iov[i].iov_len);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1312   [ +  -  +  -  :     444502 :                 copy_offset += req->iov[i].iov_len;
          +  -  +  -  +  
                -  +  - ]
    1313   [ +  -  +  -  :     444502 :                 ttl_len += req->iov[i].iov_len;
          +  -  +  -  +  
                      - ]
    1314                 :         14 :         }
    1315                 :            : 
    1316   [ +  -  +  -  :     444424 :         remainder = vol->params.chunk_size - ttl_len;
                   +  - ]
    1317         [ +  + ]:     444424 :         if (remainder) {
    1318                 :            :                 /* copy_offset already points to padding buffer if zero_paddings=false */
    1319   [ +  +  +  + ]:     333109 :                 if (zero_paddings) {
    1320   [ +  +  +  +  :      44052 :                         memcpy(copy_offset, padding_buffer + ttl_len, remainder);
                   +  - ]
    1321                 :          2 :                 }
    1322                 :     333109 :                 ttl_len += remainder;
    1323                 :          4 :         }
    1324                 :            : 
    1325   [ +  +  +  -  :     444424 :         assert(ttl_len == req->vol->params.chunk_size);
          +  -  +  -  +  
             -  +  -  #  
                      # ]
    1326                 :     444424 : }
    1327                 :            : 
    1328                 :            : /* This function can be called when we are compressing a new data or in case of read-modify-write
    1329                 :            :  * In the first case possible paddings should be filled with zeroes, in the second case the paddings
    1330                 :            :  * should point to already read and decompressed buffer */
    1331                 :            : static inline void
    1332                 :     799081 : _prepare_compress_chunk(struct spdk_reduce_vol_request *req, bool zero_paddings)
    1333                 :            : {
    1334   [ +  -  +  - ]:     799081 :         struct spdk_reduce_vol *vol = req->vol;
    1335   [ +  +  +  +  :     799081 :         char *padding_buffer = zero_paddings ? g_zero_buf : req->decomp_buf;
             +  -  +  - ]
    1336                 :     799081 :         uint64_t chunk_offset, ttl_len = 0;
    1337                 :     799081 :         uint64_t remainder = 0;
    1338   [ +  -  +  -  :     799081 :         uint32_t lbsize = vol->params.logical_block_size;
                   +  - ]
    1339                 :         60 :         size_t iov_len;
    1340                 :         30 :         int i;
    1341                 :            : 
    1342                 :            :         /* If backing device doesn't support SGL input then we should copy user's buffer into decomp_buf
    1343                 :            :          * if at least one of the conditions below is true:
    1344                 :            :          * 1. User's buffer is fragmented
    1345                 :            :          * 2. Length of the user's buffer is less than the chunk
    1346                 :            :          * 3. User's buffer is contig, equals chunk_size but crosses huge page boundary */
    1347   [ +  -  +  -  :     799081 :         iov_len = req->iov[0].iov_len;
          +  -  +  -  +  
                      - ]
    1348   [ +  +  +  +  :     799085 :         if (!vol->backing_dev->sgl_in && (req->iovcnt > 1 ||
          +  +  +  -  +  
          -  +  +  +  -  
          +  -  +  +  +  
                      + ]
    1349   [ +  +  +  +  :    1153598 :                                           req->iov[0].iov_len < vol->params.chunk_size ||
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1350   [ +  -  +  -  :     354603 :                                           _addr_crosses_huge_page(req->iov[0].iov_base, &iov_len))) {
          +  -  +  -  +  
                      - ]
    1351         [ +  - ]:     444424 :                 _prepare_compress_chunk_copy_user_buffers(req, zero_paddings);
    1352                 :     444424 :                 return;
    1353                 :            :         }
    1354                 :            : 
    1355   [ +  -  +  - ]:     354657 :         req->decomp_iovcnt = 0;
    1356   [ +  +  +  -  :     354657 :         chunk_offset = req->offset % vol->logical_blocks_per_chunk;
          +  -  +  -  +  
                      - ]
    1357                 :            : 
    1358         [ +  + ]:     354657 :         if (chunk_offset != 0) {
    1359                 :         30 :                 ttl_len += chunk_offset * lbsize;
    1360   [ +  -  +  -  :         30 :                 req->decomp_iov[0].iov_base = padding_buffer;
          +  -  +  -  +  
                      - ]
    1361   [ +  -  +  -  :         30 :                 req->decomp_iov[0].iov_len = ttl_len;
          +  -  +  -  +  
                      - ]
    1362   [ +  -  +  - ]:         30 :                 req->decomp_iovcnt = 1;
    1363                 :         10 :         }
    1364                 :            : 
    1365                 :            :         /* now the user data iov, direct from the user buffer */
    1366   [ +  +  +  -  :     709335 :         for (i = 0; i < req->iovcnt; i++) {
             +  +  +  - ]
    1367   [ +  -  +  -  :     354678 :                 req->decomp_iov[i + req->decomp_iovcnt].iov_base = req->iov[i].iov_base;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1368   [ +  -  +  -  :     354678 :                 req->decomp_iov[i + req->decomp_iovcnt].iov_len = req->iov[i].iov_len;
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1369   [ +  -  +  -  :     354678 :                 ttl_len += req->iov[i].iov_len;
          +  -  +  -  +  
                      - ]
    1370                 :         29 :         }
    1371   [ +  -  +  -  :     354657 :         req->decomp_iovcnt += req->iovcnt;
          +  -  +  -  +  
                      - ]
    1372                 :            : 
    1373   [ +  -  +  -  :     354657 :         remainder = vol->params.chunk_size - ttl_len;
                   +  - ]
    1374         [ +  + ]:     354657 :         if (remainder) {
    1375   [ +  -  +  -  :         42 :                 req->decomp_iov[req->decomp_iovcnt].iov_base = padding_buffer + ttl_len;
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1376   [ +  -  +  -  :         42 :                 req->decomp_iov[req->decomp_iovcnt].iov_len = remainder;
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1377   [ +  -  +  - ]:         42 :                 req->decomp_iovcnt++;
    1378                 :         42 :                 ttl_len += remainder;
    1379                 :         14 :         }
    1380   [ +  +  +  -  :     354657 :         assert(ttl_len == req->vol->params.chunk_size);
          +  -  +  -  +  
             -  +  -  #  
                      # ]
    1381         [ -  + ]:         30 : }
    1382                 :            : 
    1383                 :            : static void
    1384                 :     395031 : _write_decompress_done(void *_req, int reduce_errno)
    1385                 :            : {
    1386                 :     395031 :         struct spdk_reduce_vol_request *req = _req;
    1387                 :            : 
    1388                 :            :         /* Negative reduce_errno indicates failure for compression operations. */
    1389         [ +  + ]:     395031 :         if (reduce_errno < 0) {
    1390                 :          0 :                 _reduce_vol_complete_req(req, reduce_errno);
    1391                 :          0 :                 return;
    1392                 :            :         }
    1393                 :            : 
    1394                 :            :         /* Positive reduce_errno indicates that the output size field in the backing_cb_args
    1395                 :            :          * represents the output_size.
    1396                 :            :          */
    1397   [ +  +  +  -  :     395031 :         if (req->backing_cb_args.output_size != req->vol->params.chunk_size) {
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  -  + ]
    1398                 :          0 :                 _reduce_vol_complete_req(req, -EIO);
    1399                 :          0 :                 return;
    1400                 :            :         }
    1401                 :            : 
    1402                 :     395031 :         _prepare_compress_chunk(req, false);
    1403                 :     395031 :         _reduce_vol_compress_chunk(req, _write_compress_done);
    1404         [ -  + ]:          3 : }
    1405                 :            : 
    1406                 :            : static void
    1407                 :     395031 : _write_read_done(void *_req, int reduce_errno)
    1408                 :            : {
    1409                 :     395031 :         struct spdk_reduce_vol_request *req = _req;
    1410                 :            : 
    1411         [ +  + ]:     395031 :         if (reduce_errno != 0) {
    1412   [ #  #  #  # ]:          0 :                 req->reduce_errno = reduce_errno;
    1413                 :          0 :         }
    1414                 :            : 
    1415   [ +  +  +  -  :     395031 :         assert(req->num_backing_ops > 0);
             +  -  #  # ]
    1416   [ +  +  +  -  :     395031 :         if (--req->num_backing_ops > 0) {
                   -  + ]
    1417                 :          0 :                 return;
    1418                 :            :         }
    1419                 :            : 
    1420   [ +  +  +  -  :     395031 :         if (req->reduce_errno != 0) {
                   -  + ]
    1421   [ #  #  #  # ]:          0 :                 _reduce_vol_complete_req(req, req->reduce_errno);
    1422                 :          0 :                 return;
    1423                 :            :         }
    1424                 :            : 
    1425   [ +  +  +  -  :     395031 :         if (req->chunk_is_compressed) {
             +  -  +  - ]
    1426                 :     395031 :                 _reduce_vol_decompress_chunk_scratch(req, _write_decompress_done);
    1427                 :          3 :         } else {
    1428   [ #  #  #  #  :          0 :                 _write_decompress_done(req, req->chunk->compressed_size);
             #  #  #  # ]
    1429                 :            :         }
    1430         [ -  + ]:          3 : }
    1431                 :            : 
    1432                 :            : static void
    1433                 :    1219136 : _read_decompress_done(void *_req, int reduce_errno)
    1434                 :            : {
    1435                 :    1219136 :         struct spdk_reduce_vol_request *req = _req;
    1436   [ +  -  +  - ]:    1219136 :         struct spdk_reduce_vol *vol = req->vol;
    1437                 :            : 
    1438                 :            :         /* Negative reduce_errno indicates failure for compression operations. */
    1439         [ +  + ]:    1219136 :         if (reduce_errno < 0) {
    1440                 :          0 :                 _reduce_vol_complete_req(req, reduce_errno);
    1441                 :          0 :                 return;
    1442                 :            :         }
    1443                 :            : 
    1444                 :            :         /* Positive reduce_errno indicates that the output size field in the backing_cb_args
    1445                 :            :          * represents the output_size.
    1446                 :            :          */
    1447   [ +  +  +  -  :    1219136 :         if (req->backing_cb_args.output_size != vol->params.chunk_size) {
          +  -  +  -  +  
             -  +  -  -  
                      + ]
    1448                 :          0 :                 _reduce_vol_complete_req(req, -EIO);
    1449                 :          0 :                 return;
    1450                 :            :         }
    1451                 :            : 
    1452   [ +  +  +  +  :    1219136 :         if (req->copy_after_decompress) {
             +  -  +  + ]
    1453   [ +  +  +  -  :     524317 :                 uint64_t chunk_offset = req->offset % vol->logical_blocks_per_chunk;
          +  -  +  -  +  
                      - ]
    1454   [ +  -  +  -  :     524317 :                 char *decomp_buffer = (char *)req->decomp_buf + chunk_offset * vol->params.logical_block_size;
          +  -  +  -  +  
                -  +  - ]
    1455                 :          4 :                 int i;
    1456                 :            : 
    1457   [ +  +  +  -  :    1048703 :                 for (i = 0; i < req->iovcnt; i++) {
             +  +  +  - ]
    1458   [ +  +  +  +  :     524386 :                         memcpy(req->iov[i].iov_base, decomp_buffer, req->iov[i].iov_len);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1459   [ +  -  +  -  :     524386 :                         decomp_buffer += req->iov[i].iov_len;
          +  -  +  -  +  
                -  +  - ]
    1460   [ +  +  +  -  :     524386 :                         assert(decomp_buffer <= (char *)req->decomp_buf + vol->params.chunk_size);
          +  -  +  -  +  
          -  +  -  +  -  
                   #  # ]
    1461                 :          7 :                 }
    1462                 :          4 :         }
    1463                 :            : 
    1464                 :    1219136 :         _reduce_vol_complete_req(req, 0);
    1465         [ -  + ]:        266 : }
    1466                 :            : 
    1467                 :            : static void
    1468                 :    1219112 : _read_read_done(void *_req, int reduce_errno)
    1469                 :            : {
    1470                 :    1219112 :         struct spdk_reduce_vol_request *req = _req;
    1471                 :        258 :         uint64_t chunk_offset;
    1472                 :        258 :         uint8_t *buf;
    1473                 :        258 :         int i;
    1474                 :            : 
    1475         [ +  + ]:    1219112 :         if (reduce_errno != 0) {
    1476   [ #  #  #  # ]:          0 :                 req->reduce_errno = reduce_errno;
    1477                 :          0 :         }
    1478                 :            : 
    1479   [ +  +  +  -  :    1219112 :         assert(req->num_backing_ops > 0);
             +  -  #  # ]
    1480   [ +  +  +  -  :    1219112 :         if (--req->num_backing_ops > 0) {
                   -  + ]
    1481                 :          0 :                 return;
    1482                 :            :         }
    1483                 :            : 
    1484   [ +  +  +  -  :    1219112 :         if (req->reduce_errno != 0) {
                   -  + ]
    1485   [ #  #  #  # ]:          0 :                 _reduce_vol_complete_req(req, req->reduce_errno);
    1486                 :          0 :                 return;
    1487                 :            :         }
    1488                 :            : 
    1489   [ +  +  +  -  :    1219112 :         if (req->chunk_is_compressed) {
             +  -  +  - ]
    1490                 :    1219112 :                 _reduce_vol_decompress_chunk(req, _read_decompress_done);
    1491                 :        258 :         } else {
    1492                 :            : 
    1493                 :            :                 /* If the chunk was compressed, the data would have been sent to the
    1494                 :            :                  *  host buffers by the decompression operation, if not we need to memcpy here.
    1495                 :            :                  */
    1496   [ #  #  #  #  :          0 :                 chunk_offset = req->offset % req->vol->logical_blocks_per_chunk;
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1497   [ #  #  #  #  :          0 :                 buf = req->decomp_buf + chunk_offset * req->vol->params.logical_block_size;
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1498   [ #  #  #  #  :          0 :                 for (i = 0; i < req->iovcnt; i++) {
             #  #  #  # ]
    1499   [ #  #  #  #  :          0 :                         memcpy(req->iov[i].iov_base, buf, req->iov[i].iov_len);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1500   [ #  #  #  #  :          0 :                         buf += req->iov[i].iov_len;
          #  #  #  #  #  
                #  #  # ]
    1501                 :          0 :                 }
    1502                 :            : 
    1503   [ #  #  #  #  :          0 :                 _read_decompress_done(req, req->chunk->compressed_size);
             #  #  #  # ]
    1504                 :            :         }
    1505         [ -  + ]:        258 : }
    1506                 :            : 
    1507                 :            : static void
    1508                 :    1614143 : _reduce_vol_read_chunk(struct spdk_reduce_vol_request *req, reduce_request_fn next_fn)
    1509                 :            : {
    1510   [ +  -  +  - ]:    1614143 :         struct spdk_reduce_vol *vol = req->vol;
    1511                 :            : 
    1512   [ +  -  +  -  :    1614143 :         req->chunk_map_index = vol->pm_logical_map[req->logical_map_index];
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1513   [ +  +  +  -  :    1614143 :         assert(req->chunk_map_index != UINT32_MAX);
             +  -  #  # ]
    1514                 :            : 
    1515   [ +  -  +  -  :    1614143 :         req->chunk = _reduce_vol_get_chunk_map(vol, req->chunk_map_index);
             +  -  +  - ]
    1516   [ +  -  +  -  :    1614404 :         req->num_io_units = spdk_divide_round_up(req->chunk->compressed_size,
          +  -  +  -  +  
                -  +  - ]
    1517   [ +  -  +  -  :    1614143 :                             vol->params.backing_io_unit_size);
                   +  - ]
    1518   [ +  -  +  -  :    1614143 :         req->chunk_is_compressed = (req->num_io_units != vol->backing_io_units_per_chunk);
          +  -  +  -  +  
                -  +  - ]
    1519                 :            : 
    1520                 :    1614143 :         _issue_backing_ops(req, vol, next_fn, false /* read */);
    1521                 :    1614143 : }
    1522                 :            : 
    1523                 :            : static bool
    1524                 :    2080757 : _iov_array_is_valid(struct spdk_reduce_vol *vol, struct iovec *iov, int iovcnt,
    1525                 :            :                     uint64_t length)
    1526                 :            : {
    1527                 :    2080757 :         uint64_t size = 0;
    1528                 :        271 :         int i;
    1529                 :            : 
    1530         [ -  + ]:    2080757 :         if (iovcnt > REDUCE_MAX_IOVECS) {
    1531                 :          0 :                 return false;
    1532                 :            :         }
    1533                 :            : 
    1534   [ +  +  +  - ]:    4161634 :         for (i = 0; i < iovcnt; i++) {
    1535   [ +  -  +  -  :    2080877 :                 size += iov[i].iov_len;
                   +  - ]
    1536                 :        271 :         }
    1537                 :            : 
    1538   [ +  -  +  -  :    2080757 :         return size == (length * vol->params.logical_block_size);
                   +  - ]
    1539                 :        271 : }
    1540                 :            : 
    1541                 :            : static bool
    1542                 :    2080757 : _check_overlap(struct spdk_reduce_vol *vol, uint64_t logical_map_index)
    1543                 :            : {
    1544                 :        271 :         struct spdk_reduce_vol_request *req;
    1545                 :            : 
    1546   [ +  +  +  -  :  111103024 :         TAILQ_FOREACH(req, &vol->executing_requests, tailq) {
          +  -  +  +  #  
             #  #  #  #  
                      # ]
    1547   [ +  +  +  -  :  109585803 :                 if (logical_map_index == req->logical_map_index) {
                   -  + ]
    1548                 :     563536 :                         return true;
    1549                 :            :                 }
    1550                 :          0 :         }
    1551                 :            : 
    1552                 :    1517221 :         return false;
    1553                 :        271 : }
    1554                 :            : 
    1555                 :            : static void
    1556                 :    1219112 : _start_readv_request(struct spdk_reduce_vol_request *req)
    1557                 :            : {
    1558   [ +  -  +  -  :    1219112 :         TAILQ_INSERT_TAIL(&req->vol->executing_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1559                 :    1219112 :         _reduce_vol_read_chunk(req, _read_read_done);
    1560                 :    1219112 : }
    1561                 :            : 
    1562                 :            : void
    1563                 :    1281727 : spdk_reduce_vol_readv(struct spdk_reduce_vol *vol,
    1564                 :            :                       struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    1565                 :            :                       spdk_reduce_vol_op_complete cb_fn, void *cb_arg)
    1566                 :            : {
    1567                 :        258 :         struct spdk_reduce_vol_request *req;
    1568                 :        258 :         uint64_t logical_map_index;
    1569                 :        258 :         bool overlapped;
    1570                 :        258 :         int i;
    1571                 :            : 
    1572         [ +  + ]:    1281727 :         if (length == 0) {
    1573   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, 0);
    1574                 :          0 :                 return;
    1575                 :            :         }
    1576                 :            : 
    1577         [ -  + ]:    1281727 :         if (_request_spans_chunk_boundary(vol, offset, length)) {
    1578   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -EINVAL);
    1579                 :          0 :                 return;
    1580                 :            :         }
    1581                 :            : 
    1582         [ +  + ]:    1281727 :         if (!_iov_array_is_valid(vol, iov, iovcnt, length)) {
    1583   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -EINVAL);
    1584                 :          0 :                 return;
    1585                 :            :         }
    1586                 :            : 
    1587   [ +  +  +  -  :    1281727 :         logical_map_index = offset / vol->logical_blocks_per_chunk;
                   +  - ]
    1588                 :    1281727 :         overlapped = _check_overlap(vol, logical_map_index);
    1589                 :            : 
    1590   [ +  +  +  +  :    1281727 :         if (!overlapped && vol->pm_logical_map[logical_map_index] == REDUCE_EMPTY_MAP_ENTRY) {
          +  -  +  -  +  
             -  +  -  +  
                      - ]
    1591                 :            :                 /*
    1592                 :            :                  * This chunk hasn't been allocated.  So treat the data as all
    1593                 :            :                  * zeroes for this chunk - do the memset and immediately complete
    1594                 :            :                  * the operation.
    1595                 :            :                  */
    1596   [ +  +  #  # ]:     125230 :                 for (i = 0; i < iovcnt; i++) {
    1597   [ -  +  #  #  :      62615 :                         memset(iov[i].iov_base, 0, iov[i].iov_len);
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1598                 :          0 :                 }
    1599   [ #  #  #  # ]:      62615 :                 cb_fn(cb_arg, 0);
    1600                 :      62615 :                 return;
    1601                 :            :         }
    1602                 :            : 
    1603   [ +  -  +  -  :    1219112 :         req = TAILQ_FIRST(&vol->free_requests);
                   +  - ]
    1604         [ +  + ]:    1219112 :         if (req == NULL) {
    1605   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
    1606                 :          0 :                 return;
    1607                 :            :         }
    1608                 :            : 
    1609   [ +  -  +  -  :    1219112 :         TAILQ_REMOVE(&vol->free_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    1610   [ +  -  +  - ]:    1219112 :         req->type = REDUCE_IO_READV;
    1611   [ +  -  +  - ]:    1219112 :         req->vol = vol;
    1612   [ +  -  +  - ]:    1219112 :         req->iov = iov;
    1613   [ +  -  +  - ]:    1219112 :         req->iovcnt = iovcnt;
    1614   [ +  -  +  - ]:    1219112 :         req->offset = offset;
    1615   [ +  -  +  - ]:    1219112 :         req->logical_map_index = logical_map_index;
    1616   [ +  -  +  - ]:    1219112 :         req->length = length;
    1617   [ +  -  +  - ]:    1219112 :         req->copy_after_decompress = false;
    1618   [ +  -  +  - ]:    1219112 :         req->cb_fn = cb_fn;
    1619   [ +  -  +  - ]:    1219112 :         req->cb_arg = cb_arg;
    1620                 :            : 
    1621   [ +  +  -  + ]:    1219112 :         if (!overlapped) {
    1622                 :     736689 :                 _start_readv_request(req);
    1623                 :        258 :         } else {
    1624   [ #  #  #  #  :     482423 :                 TAILQ_INSERT_TAIL(&vol->queued_requests, req, tailq);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1625                 :            :         }
    1626         [ -  + ]:        258 : }
    1627                 :            : 
    1628                 :            : static void
    1629                 :     799030 : _start_writev_request(struct spdk_reduce_vol_request *req)
    1630                 :            : {
    1631   [ +  -  +  - ]:     799030 :         struct spdk_reduce_vol *vol = req->vol;
    1632                 :            : 
    1633   [ +  -  +  -  :     799030 :         TAILQ_INSERT_TAIL(&req->vol->executing_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
    1634   [ +  +  +  -  :     799030 :         if (vol->pm_logical_map[req->logical_map_index] != REDUCE_EMPTY_MAP_ENTRY) {
          +  -  +  -  +  
             -  +  -  +  
                      + ]
    1635   [ +  +  +  -  :     736638 :                 if ((req->length * vol->params.logical_block_size) < vol->params.chunk_size) {
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  + ]
    1636                 :            :                         /* Read old chunk, then overwrite with data from this write
    1637                 :            :                          *  operation.
    1638                 :            :                          */
    1639   [ +  -  +  - ]:     395031 :                         req->rmw = true;
    1640                 :     395031 :                         _reduce_vol_read_chunk(req, _write_read_done);
    1641                 :     395031 :                         return;
    1642                 :            :                 }
    1643                 :          2 :         }
    1644                 :            : 
    1645   [ +  -  +  - ]:     403999 :         req->rmw = false;
    1646                 :            : 
    1647                 :     403999 :         _prepare_compress_chunk(req, true);
    1648                 :     403999 :         _reduce_vol_compress_chunk(req, _write_compress_done);
    1649         [ -  + ]:         13 : }
    1650                 :            : 
    1651                 :            : void
    1652                 :     799036 : spdk_reduce_vol_writev(struct spdk_reduce_vol *vol,
    1653                 :            :                        struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    1654                 :            :                        spdk_reduce_vol_op_complete cb_fn, void *cb_arg)
    1655                 :            : {
    1656                 :         15 :         struct spdk_reduce_vol_request *req;
    1657                 :         15 :         uint64_t logical_map_index;
    1658                 :         15 :         bool overlapped;
    1659                 :            : 
    1660         [ +  + ]:     799036 :         if (length == 0) {
    1661   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, 0);
    1662                 :          0 :                 return;
    1663                 :            :         }
    1664                 :            : 
    1665         [ +  + ]:     799036 :         if (_request_spans_chunk_boundary(vol, offset, length)) {
    1666   [ -  +  +  - ]:          6 :                 cb_fn(cb_arg, -EINVAL);
    1667                 :          6 :                 return;
    1668                 :            :         }
    1669                 :            : 
    1670         [ +  + ]:     799030 :         if (!_iov_array_is_valid(vol, iov, iovcnt, length)) {
    1671   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -EINVAL);
    1672                 :          0 :                 return;
    1673                 :            :         }
    1674                 :            : 
    1675   [ +  +  +  -  :     799030 :         logical_map_index = offset / vol->logical_blocks_per_chunk;
                   +  - ]
    1676                 :     799030 :         overlapped = _check_overlap(vol, logical_map_index);
    1677                 :            : 
    1678   [ +  -  +  -  :     799030 :         req = TAILQ_FIRST(&vol->free_requests);
                   +  - ]
    1679         [ +  + ]:     799030 :         if (req == NULL) {
    1680   [ #  #  #  # ]:          0 :                 cb_fn(cb_arg, -ENOMEM);
    1681                 :          0 :                 return;
    1682                 :            :         }
    1683                 :            : 
    1684   [ +  -  +  -  :     799030 :         TAILQ_REMOVE(&vol->free_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  #  #  #  
          #  #  #  #  #  
          #  #  #  #  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
    1685   [ +  -  +  - ]:     799030 :         req->type = REDUCE_IO_WRITEV;
    1686   [ +  -  +  - ]:     799030 :         req->vol = vol;
    1687   [ +  -  +  - ]:     799030 :         req->iov = iov;
    1688   [ +  -  +  - ]:     799030 :         req->iovcnt = iovcnt;
    1689   [ +  -  +  - ]:     799030 :         req->offset = offset;
    1690   [ +  -  +  - ]:     799030 :         req->logical_map_index = logical_map_index;
    1691   [ +  -  +  - ]:     799030 :         req->length = length;
    1692   [ +  -  +  - ]:     799030 :         req->copy_after_decompress = false;
    1693   [ +  -  +  - ]:     799030 :         req->cb_fn = cb_fn;
    1694   [ +  -  +  - ]:     799030 :         req->cb_arg = cb_arg;
    1695                 :            : 
    1696   [ +  +  +  + ]:     799030 :         if (!overlapped) {
    1697                 :     717917 :                 _start_writev_request(req);
    1698                 :         12 :         } else {
    1699   [ +  -  +  -  :      81113 :                 TAILQ_INSERT_TAIL(&vol->queued_requests, req, tailq);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
    1700                 :            :         }
    1701         [ -  + ]:         15 : }
    1702                 :            : 
    1703                 :            : const struct spdk_reduce_vol_params *
    1704                 :          0 : spdk_reduce_vol_get_params(struct spdk_reduce_vol *vol)
    1705                 :            : {
    1706         [ #  # ]:          0 :         return &vol->params;
    1707                 :            : }
    1708                 :            : 
    1709                 :            : void
    1710                 :          0 : spdk_reduce_vol_print_info(struct spdk_reduce_vol *vol)
    1711                 :            : {
    1712                 :          0 :         uint64_t logical_map_size, num_chunks, ttl_chunk_sz;
    1713                 :          0 :         uint32_t struct_size;
    1714                 :          0 :         uint64_t chunk_map_size;
    1715                 :            : 
    1716                 :          0 :         SPDK_NOTICELOG("vol info:\n");
    1717   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\tvol->params.backing_io_unit_size = 0x%x\n", vol->params.backing_io_unit_size);
                   #  # ]
    1718   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\tvol->params.logical_block_size = 0x%x\n", vol->params.logical_block_size);
                   #  # ]
    1719   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\tvol->params.chunk_size = 0x%x\n", vol->params.chunk_size);
                   #  # ]
    1720   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\tvol->params.vol_size = 0x%" PRIx64 "\n", vol->params.vol_size);
                   #  # ]
    1721   [ #  #  #  #  :          0 :         num_chunks = _get_total_chunks(vol->params.vol_size, vol->params.chunk_size);
          #  #  #  #  #  
                #  #  # ]
    1722                 :          0 :         SPDK_NOTICELOG("\ttotal chunks (including extra) = 0x%" PRIx64 "\n", num_chunks);
    1723   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\ttotal chunks (excluding extra) = 0x%" PRIx64 "\n",
          #  #  #  #  #  
             #  #  #  #  
                      # ]
    1724                 :            :                        vol->params.vol_size / vol->params.chunk_size);
    1725   [ #  #  #  #  :          0 :         ttl_chunk_sz = _get_pm_total_chunks_size(vol->params.vol_size, vol->params.chunk_size,
          #  #  #  #  #  
                #  #  # ]
    1726   [ #  #  #  #  :          0 :                         vol->params.backing_io_unit_size);
                   #  # ]
    1727                 :          0 :         SPDK_NOTICELOG("\ttotal_chunks_size = 0x%" PRIx64 "\n", ttl_chunk_sz);
    1728   [ #  #  #  # ]:          0 :         struct_size = _reduce_vol_get_chunk_struct_size(vol->backing_io_units_per_chunk);
    1729                 :          0 :         SPDK_NOTICELOG("\tchunk_struct_size = 0x%x\n", struct_size);
    1730                 :            : 
    1731                 :          0 :         SPDK_NOTICELOG("pmem info:\n");
    1732   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\tvol->pm_file.size = 0x%" PRIx64 "\n", vol->pm_file.size);
                   #  # ]
    1733   [ #  #  #  #  :          0 :         SPDK_NOTICELOG("\tvol->pm_file.pm_buf = %p\n", (void *)vol->pm_file.pm_buf);
                   #  # ]
    1734   [ #  #  #  # ]:          0 :         SPDK_NOTICELOG("\tvol->pm_super = %p\n", (void *)vol->pm_super);
    1735   [ #  #  #  # ]:          0 :         SPDK_NOTICELOG("\tvol->pm_logical_map = %p\n", (void *)vol->pm_logical_map);
    1736   [ #  #  #  #  :          0 :         logical_map_size = _get_pm_logical_map_size(vol->params.vol_size,
                   #  # ]
    1737   [ #  #  #  #  :          0 :                            vol->params.chunk_size);
                   #  # ]
    1738                 :          0 :         SPDK_NOTICELOG("\tlogical_map_size = 0x%" PRIx64 "\n", logical_map_size);
    1739   [ #  #  #  # ]:          0 :         SPDK_NOTICELOG("\tvol->pm_chunk_maps = %p\n", (void *)vol->pm_chunk_maps);
    1740   [ #  #  #  #  :          0 :         chunk_map_size = _get_pm_total_chunks_size(vol->params.vol_size, vol->params.chunk_size,
          #  #  #  #  #  
                #  #  # ]
    1741   [ #  #  #  #  :          0 :                          vol->params.backing_io_unit_size);
                   #  # ]
    1742                 :          0 :         SPDK_NOTICELOG("\tchunk_map_size = 0x%" PRIx64 "\n", chunk_map_size);
    1743                 :          0 : }
    1744                 :            : 
    1745                 :        132 : SPDK_LOG_REGISTER_COMPONENT(reduce)

Generated by: LCOV version 1.15