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