1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/likely.h" 35 #include "spdk/stdinc.h" 36 #include "spdk/nvme.h" 37 #include "spdk/io_channel.h" 38 #include "spdk/bdev_module.h" 39 #include "spdk_internal/log.h" 40 #include "spdk/ftl.h" 41 42 #include "ftl_core.h" 43 #include "ftl_band.h" 44 #include "ftl_io.h" 45 #include "ftl_anm.h" 46 #include "ftl_rwb.h" 47 #include "ftl_debug.h" 48 #include "ftl_reloc.h" 49 50 /* Max number of iovecs */ 51 #define FTL_MAX_IOV 1024 52 53 struct ftl_wptr { 54 /* Owner device */ 55 struct spdk_ftl_dev *dev; 56 57 /* Current PPA */ 58 struct ftl_ppa ppa; 59 60 /* Band currently being written to */ 61 struct ftl_band *band; 62 63 /* Current logical block's offset */ 64 uint64_t offset; 65 66 /* Current erase block */ 67 struct ftl_chunk *chunk; 68 69 /* Metadata DMA buffer */ 70 void *md_buf; 71 72 /* List link */ 73 LIST_ENTRY(ftl_wptr) list_entry; 74 }; 75 76 struct ftl_flush { 77 /* Owner device */ 78 struct spdk_ftl_dev *dev; 79 80 /* Number of batches to wait for */ 81 size_t num_req; 82 83 /* Callback */ 84 struct ftl_cb cb; 85 86 /* Batch bitmap */ 87 struct spdk_bit_array *bmap; 88 89 /* List link */ 90 LIST_ENTRY(ftl_flush) list_entry; 91 }; 92 93 typedef int (*ftl_next_ppa_fn)(struct ftl_io *, struct ftl_ppa *, size_t, void *); 94 static void _ftl_read(void *); 95 static void _ftl_write(void *); 96 97 static int 98 ftl_rwb_flags_from_io(const struct ftl_io *io) 99 { 100 int valid_flags = FTL_IO_INTERNAL | FTL_IO_WEAK | FTL_IO_PAD; 101 return io->flags & valid_flags; 102 } 103 104 static int 105 ftl_rwb_entry_weak(const struct ftl_rwb_entry *entry) 106 { 107 return entry->flags & FTL_IO_WEAK; 108 } 109 110 static void 111 ftl_wptr_free(struct ftl_wptr *wptr) 112 { 113 if (!wptr) { 114 return; 115 } 116 117 spdk_dma_free(wptr->md_buf); 118 free(wptr); 119 } 120 121 static void 122 ftl_remove_wptr(struct ftl_wptr *wptr) 123 { 124 LIST_REMOVE(wptr, list_entry); 125 ftl_wptr_free(wptr); 126 } 127 128 static void 129 ftl_io_cmpl_cb(void *arg, const struct spdk_nvme_cpl *status) 130 { 131 struct ftl_io *io = arg; 132 133 if (spdk_nvme_cpl_is_error(status)) { 134 ftl_io_process_error(io, status); 135 } 136 137 ftl_trace_completion(io->dev, io, FTL_TRACE_COMPLETION_DISK); 138 139 if (!ftl_io_dec_req(io)) { 140 ftl_io_complete(io); 141 } 142 } 143 144 static void 145 ftl_halt_writes(struct spdk_ftl_dev *dev, struct ftl_band *band) 146 { 147 struct ftl_wptr *wptr = NULL; 148 149 LIST_FOREACH(wptr, &dev->wptr_list, list_entry) { 150 if (wptr->band == band) { 151 break; 152 } 153 } 154 155 /* If the band already has the high_prio flag set, other writes must */ 156 /* have failed earlier, so it's already taken care of. */ 157 if (band->high_prio) { 158 assert(wptr == NULL); 159 return; 160 } 161 162 ftl_band_write_failed(band); 163 ftl_remove_wptr(wptr); 164 } 165 166 static struct ftl_wptr * 167 ftl_wptr_from_band(struct ftl_band *band) 168 { 169 struct spdk_ftl_dev *dev = band->dev; 170 struct ftl_wptr *wptr = NULL; 171 172 LIST_FOREACH(wptr, &dev->wptr_list, list_entry) { 173 if (wptr->band == band) { 174 return wptr; 175 } 176 } 177 178 return NULL; 179 } 180 181 static void 182 ftl_md_write_fail(struct ftl_io *io, int status) 183 { 184 struct ftl_band *band = io->band; 185 struct ftl_wptr *wptr; 186 char buf[128]; 187 188 wptr = ftl_wptr_from_band(band); 189 190 SPDK_ERRLOG("Metadata write failed @ppa: %s, status: %d\n", 191 ftl_ppa2str(wptr->ppa, buf, sizeof(buf)), status); 192 193 ftl_halt_writes(io->dev, band); 194 } 195 196 static void 197 ftl_md_write_cb(void *arg, int status) 198 { 199 struct ftl_io *io = arg; 200 struct ftl_wptr *wptr; 201 202 wptr = ftl_wptr_from_band(io->band); 203 204 if (status) { 205 ftl_md_write_fail(io, status); 206 return; 207 } 208 209 ftl_band_set_next_state(io->band); 210 if (io->band->state == FTL_BAND_STATE_CLOSED) { 211 ftl_remove_wptr(wptr); 212 } 213 } 214 215 static int 216 ftl_ppa_read_next_ppa(struct ftl_io *io, struct ftl_ppa *ppa, 217 size_t lbk, void *ctx) 218 { 219 struct spdk_ftl_dev *dev = io->dev; 220 size_t lbk_cnt, max_lbks; 221 222 assert(ftl_io_mode_ppa(io)); 223 assert(io->iov_pos < io->iov_cnt); 224 225 if (lbk == 0) { 226 *ppa = io->ppa; 227 } else { 228 *ppa = ftl_band_next_xfer_ppa(io->band, io->ppa, lbk); 229 } 230 231 assert(!ftl_ppa_invalid(*ppa)); 232 233 /* Metadata has to be read in the way it's written (jumping across */ 234 /* the chunks in xfer_size increments) */ 235 if (io->flags & FTL_IO_MD) { 236 max_lbks = dev->xfer_size - (ppa->lbk % dev->xfer_size); 237 lbk_cnt = spdk_min(ftl_io_iovec_len_left(io), max_lbks); 238 assert(ppa->lbk / dev->xfer_size == (ppa->lbk + lbk_cnt - 1) / dev->xfer_size); 239 } else { 240 lbk_cnt = ftl_io_iovec_len_left(io); 241 } 242 243 return lbk_cnt; 244 } 245 246 static int 247 ftl_wptr_close_band(struct ftl_wptr *wptr) 248 { 249 struct ftl_band *band = wptr->band; 250 251 ftl_band_set_state(band, FTL_BAND_STATE_CLOSING); 252 band->tail_md_ppa = wptr->ppa; 253 254 return ftl_band_write_tail_md(band, wptr->md_buf, ftl_md_write_cb); 255 } 256 257 static int 258 ftl_wptr_open_band(struct ftl_wptr *wptr) 259 { 260 struct ftl_band *band = wptr->band; 261 262 assert(ftl_band_chunk_is_first(band, wptr->chunk)); 263 assert(band->md.num_vld == 0); 264 265 ftl_band_clear_md(band); 266 267 assert(band->state == FTL_BAND_STATE_PREP); 268 ftl_band_set_state(band, FTL_BAND_STATE_OPENING); 269 270 return ftl_band_write_head_md(band, wptr->md_buf, ftl_md_write_cb); 271 } 272 273 static int 274 ftl_submit_erase(struct ftl_io *io) 275 { 276 struct spdk_ftl_dev *dev = io->dev; 277 struct ftl_band *band = io->band; 278 struct ftl_ppa ppa = io->ppa; 279 struct ftl_chunk *chunk; 280 uint64_t ppa_packed; 281 int rc = 0; 282 size_t i; 283 284 for (i = 0; i < io->lbk_cnt; ++i) { 285 if (i != 0) { 286 chunk = ftl_band_next_chunk(band, ftl_band_chunk_from_ppa(band, ppa)); 287 assert(chunk->state == FTL_CHUNK_STATE_CLOSED || 288 chunk->state == FTL_CHUNK_STATE_VACANT); 289 ppa = chunk->start_ppa; 290 } 291 292 assert(ppa.lbk == 0); 293 ppa_packed = ftl_ppa_addr_pack(dev, ppa); 294 295 ftl_io_inc_req(io); 296 297 ftl_trace_submission(dev, io, ppa, 1); 298 rc = spdk_nvme_ocssd_ns_cmd_vector_reset(dev->ns, ftl_get_write_qpair(dev), 299 &ppa_packed, 1, NULL, ftl_io_cmpl_cb, io); 300 if (rc) { 301 SPDK_ERRLOG("Vector reset failed with status: %d\n", rc); 302 ftl_io_dec_req(io); 303 break; 304 } 305 } 306 307 if (ftl_io_done(io)) { 308 ftl_io_complete(io); 309 } 310 311 return rc; 312 } 313 314 static void 315 _ftl_io_erase(void *ctx) 316 { 317 ftl_io_erase((struct ftl_io *)ctx); 318 } 319 320 static bool 321 ftl_check_core_thread(const struct spdk_ftl_dev *dev) 322 { 323 return dev->core_thread.thread == spdk_get_thread(); 324 } 325 326 static bool 327 ftl_check_read_thread(const struct spdk_ftl_dev *dev) 328 { 329 return dev->read_thread.thread == spdk_get_thread(); 330 } 331 332 int 333 ftl_io_erase(struct ftl_io *io) 334 { 335 struct spdk_ftl_dev *dev = io->dev; 336 337 if (ftl_check_core_thread(dev)) { 338 return ftl_submit_erase(io); 339 } 340 341 spdk_thread_send_msg(ftl_get_core_thread(dev), _ftl_io_erase, io); 342 return 0; 343 } 344 345 static struct ftl_band * 346 ftl_next_write_band(struct spdk_ftl_dev *dev) 347 { 348 struct ftl_band *band; 349 350 band = LIST_FIRST(&dev->free_bands); 351 if (!band) { 352 return NULL; 353 } 354 assert(band->state == FTL_BAND_STATE_FREE); 355 356 if (ftl_band_erase(band)) { 357 /* TODO: handle erase failure */ 358 return NULL; 359 } 360 361 return band; 362 } 363 364 static struct ftl_band * 365 ftl_next_wptr_band(struct spdk_ftl_dev *dev) 366 { 367 struct ftl_band *band; 368 369 if (!dev->next_band) { 370 band = ftl_next_write_band(dev); 371 } else { 372 assert(dev->next_band->state == FTL_BAND_STATE_PREP); 373 band = dev->next_band; 374 dev->next_band = NULL; 375 } 376 377 return band; 378 } 379 380 static struct ftl_wptr * 381 ftl_wptr_init(struct ftl_band *band) 382 { 383 struct spdk_ftl_dev *dev = band->dev; 384 struct ftl_wptr *wptr; 385 386 wptr = calloc(1, sizeof(*wptr)); 387 if (!wptr) { 388 return NULL; 389 } 390 391 wptr->md_buf = spdk_dma_zmalloc(ftl_tail_md_num_lbks(dev) * FTL_BLOCK_SIZE, 392 FTL_BLOCK_SIZE, NULL); 393 if (!wptr->md_buf) { 394 ftl_wptr_free(wptr); 395 return NULL; 396 } 397 398 wptr->dev = dev; 399 wptr->band = band; 400 wptr->chunk = CIRCLEQ_FIRST(&band->chunks); 401 wptr->ppa = wptr->chunk->start_ppa; 402 403 return wptr; 404 } 405 406 static int 407 ftl_add_wptr(struct spdk_ftl_dev *dev) 408 { 409 struct ftl_band *band; 410 struct ftl_wptr *wptr; 411 412 band = ftl_next_wptr_band(dev); 413 if (!band) { 414 return -1; 415 } 416 417 wptr = ftl_wptr_init(band); 418 if (!wptr) { 419 return -1; 420 } 421 422 if (ftl_band_write_prep(band)) { 423 ftl_wptr_free(wptr); 424 return -1; 425 } 426 427 LIST_INSERT_HEAD(&dev->wptr_list, wptr, list_entry); 428 429 SPDK_DEBUGLOG(SPDK_LOG_FTL_CORE, "wptr: band %u\n", band->id); 430 ftl_trace_write_band(dev, band); 431 return 0; 432 } 433 434 static void 435 ftl_wptr_advance(struct ftl_wptr *wptr, size_t xfer_size) 436 { 437 struct ftl_band *band = wptr->band; 438 struct spdk_ftl_dev *dev = wptr->dev; 439 struct spdk_ftl_conf *conf = &dev->conf; 440 size_t next_thld; 441 442 wptr->offset += xfer_size; 443 next_thld = (ftl_band_num_usable_lbks(band) * conf->band_thld) / 100; 444 445 if (ftl_band_full(band, wptr->offset)) { 446 ftl_band_set_state(band, FTL_BAND_STATE_FULL); 447 } 448 449 wptr->ppa = ftl_band_next_xfer_ppa(band, wptr->ppa, xfer_size); 450 wptr->chunk = ftl_band_next_operational_chunk(band, wptr->chunk); 451 452 assert(!ftl_ppa_invalid(wptr->ppa)); 453 454 SPDK_DEBUGLOG(SPDK_LOG_FTL_CORE, "wptr: grp:%d, pu:%d chunk:%d, lbk:%u\n", 455 wptr->ppa.grp, wptr->ppa.pu, wptr->ppa.chk, wptr->ppa.lbk); 456 457 if (wptr->offset >= next_thld && !dev->next_band) { 458 dev->next_band = ftl_next_write_band(dev); 459 } 460 } 461 462 static int 463 ftl_wptr_ready(struct ftl_wptr *wptr) 464 { 465 struct ftl_band *band = wptr->band; 466 467 /* TODO: add handling of empty bands */ 468 469 if (spdk_unlikely(!ftl_chunk_is_writable(wptr->chunk))) { 470 /* Erasing band may fail after it was assigned to wptr. */ 471 if (spdk_unlikely(wptr->chunk->state == FTL_CHUNK_STATE_BAD)) { 472 ftl_wptr_advance(wptr, wptr->dev->xfer_size); 473 } 474 return 0; 475 } 476 477 /* If we're in the process of writing metadata, wait till it is */ 478 /* completed. */ 479 /* TODO: we should probably change bands once we're writing tail md */ 480 if (ftl_band_state_changing(band)) { 481 return 0; 482 } 483 484 if (band->state == FTL_BAND_STATE_FULL) { 485 if (ftl_wptr_close_band(wptr)) { 486 /* TODO: need recovery here */ 487 assert(false); 488 } 489 return 0; 490 } 491 492 if (band->state != FTL_BAND_STATE_OPEN) { 493 if (ftl_wptr_open_band(wptr)) { 494 /* TODO: need recovery here */ 495 assert(false); 496 } 497 return 0; 498 } 499 500 return 1; 501 } 502 503 static const struct spdk_ftl_limit * 504 ftl_get_limit(const struct spdk_ftl_dev *dev, int type) 505 { 506 assert(type < SPDK_FTL_LIMIT_MAX); 507 return &dev->conf.defrag.limits[type]; 508 } 509 510 static bool 511 ftl_cache_lba_valid(struct spdk_ftl_dev *dev, struct ftl_rwb_entry *entry) 512 { 513 struct ftl_ppa ppa; 514 515 /* If the LBA is invalid don't bother checking the md and l2p */ 516 if (spdk_unlikely(entry->lba == FTL_LBA_INVALID)) { 517 return false; 518 } 519 520 ppa = ftl_l2p_get(dev, entry->lba); 521 if (!(ftl_ppa_cached(ppa) && ppa.offset == entry->pos)) { 522 return false; 523 } 524 525 return true; 526 } 527 528 static void 529 ftl_evict_cache_entry(struct spdk_ftl_dev *dev, struct ftl_rwb_entry *entry) 530 { 531 pthread_spin_lock(&entry->lock); 532 533 if (!ftl_rwb_entry_valid(entry)) { 534 goto unlock; 535 } 536 537 /* If the l2p wasn't updated and still points at the entry, fill it with the */ 538 /* on-disk PPA and clear the cache status bit. Otherwise, skip the l2p update */ 539 /* and just clear the cache status. */ 540 if (!ftl_cache_lba_valid(dev, entry)) { 541 goto clear; 542 } 543 544 ftl_l2p_set(dev, entry->lba, entry->ppa); 545 clear: 546 ftl_rwb_entry_invalidate(entry); 547 unlock: 548 pthread_spin_unlock(&entry->lock); 549 } 550 551 static struct ftl_rwb_entry * 552 ftl_acquire_entry(struct spdk_ftl_dev *dev, int flags) 553 { 554 struct ftl_rwb_entry *entry; 555 556 entry = ftl_rwb_acquire(dev->rwb, ftl_rwb_type_from_flags(flags)); 557 if (!entry) { 558 return NULL; 559 } 560 561 ftl_evict_cache_entry(dev, entry); 562 563 entry->flags = flags; 564 return entry; 565 } 566 567 static void 568 ftl_rwb_pad(struct spdk_ftl_dev *dev, size_t size) 569 { 570 struct ftl_rwb_entry *entry; 571 int flags = FTL_IO_PAD | FTL_IO_INTERNAL; 572 573 for (size_t i = 0; i < size; ++i) { 574 entry = ftl_acquire_entry(dev, flags); 575 if (!entry) { 576 break; 577 } 578 579 entry->lba = FTL_LBA_INVALID; 580 entry->ppa = ftl_to_ppa(FTL_PPA_INVALID); 581 memset(entry->data, 0, FTL_BLOCK_SIZE); 582 ftl_rwb_push(entry); 583 } 584 } 585 586 static void 587 ftl_remove_free_bands(struct spdk_ftl_dev *dev) 588 { 589 while (!LIST_EMPTY(&dev->free_bands)) { 590 LIST_REMOVE(LIST_FIRST(&dev->free_bands), list_entry); 591 } 592 593 dev->next_band = NULL; 594 } 595 596 static void 597 ftl_process_shutdown(struct spdk_ftl_dev *dev) 598 { 599 size_t size = ftl_rwb_num_acquired(dev->rwb, FTL_RWB_TYPE_INTERNAL) + 600 ftl_rwb_num_acquired(dev->rwb, FTL_RWB_TYPE_USER); 601 602 if (size >= dev->xfer_size) { 603 return; 604 } 605 606 /* If we reach this point we need to remove free bands */ 607 /* and pad current wptr band to the end */ 608 ftl_remove_free_bands(dev); 609 610 /* Pad write buffer until band is full */ 611 ftl_rwb_pad(dev, dev->xfer_size - size); 612 } 613 614 static int 615 ftl_shutdown_complete(struct spdk_ftl_dev *dev) 616 { 617 return !__atomic_load_n(&dev->num_inflight, __ATOMIC_SEQ_CST) && 618 LIST_EMPTY(&dev->wptr_list); 619 } 620 621 void 622 ftl_apply_limits(struct spdk_ftl_dev *dev) 623 { 624 const struct spdk_ftl_limit *limit; 625 struct ftl_stats *stats = &dev->stats; 626 size_t rwb_limit[FTL_RWB_TYPE_MAX]; 627 int i; 628 629 ftl_rwb_get_limits(dev->rwb, rwb_limit); 630 631 /* Clear existing limit */ 632 dev->limit = SPDK_FTL_LIMIT_MAX; 633 634 for (i = SPDK_FTL_LIMIT_CRIT; i < SPDK_FTL_LIMIT_MAX; ++i) { 635 limit = ftl_get_limit(dev, i); 636 637 if (dev->num_free <= limit->thld) { 638 rwb_limit[FTL_RWB_TYPE_USER] = 639 (limit->limit * ftl_rwb_entry_cnt(dev->rwb)) / 100; 640 stats->limits[i]++; 641 dev->limit = i; 642 goto apply; 643 } 644 } 645 646 /* Clear the limits, since we don't need to apply them anymore */ 647 rwb_limit[FTL_RWB_TYPE_USER] = ftl_rwb_entry_cnt(dev->rwb); 648 apply: 649 ftl_trace_limits(dev, rwb_limit, dev->num_free); 650 ftl_rwb_set_limits(dev->rwb, rwb_limit); 651 } 652 653 static int 654 ftl_invalidate_addr_unlocked(struct spdk_ftl_dev *dev, struct ftl_ppa ppa) 655 { 656 struct ftl_band *band = ftl_band_from_ppa(dev, ppa); 657 struct ftl_md *md = &band->md; 658 uint64_t offset; 659 660 offset = ftl_band_lbkoff_from_ppa(band, ppa); 661 662 /* The bit might be already cleared if two writes are scheduled to the */ 663 /* same LBA at the same time */ 664 if (spdk_bit_array_get(md->vld_map, offset)) { 665 assert(md->num_vld > 0); 666 spdk_bit_array_clear(md->vld_map, offset); 667 md->num_vld--; 668 return 1; 669 } 670 671 return 0; 672 } 673 674 int 675 ftl_invalidate_addr(struct spdk_ftl_dev *dev, struct ftl_ppa ppa) 676 { 677 struct ftl_band *band; 678 int rc; 679 680 assert(!ftl_ppa_cached(ppa)); 681 band = ftl_band_from_ppa(dev, ppa); 682 683 pthread_spin_lock(&band->md.lock); 684 rc = ftl_invalidate_addr_unlocked(dev, ppa); 685 pthread_spin_unlock(&band->md.lock); 686 687 return rc; 688 } 689 690 static int 691 ftl_read_retry(int rc) 692 { 693 return rc == -EAGAIN; 694 } 695 696 static int 697 ftl_read_canceled(int rc) 698 { 699 return rc == 0; 700 } 701 702 static int 703 ftl_submit_read(struct ftl_io *io, ftl_next_ppa_fn next_ppa, 704 void *ctx) 705 { 706 struct spdk_ftl_dev *dev = io->dev; 707 struct ftl_ppa ppa; 708 size_t lbk = 0; 709 int rc = 0, lbk_cnt; 710 711 while (lbk < io->lbk_cnt) { 712 /* We might hit the cache here, if so, skip the read */ 713 lbk_cnt = rc = next_ppa(io, &ppa, lbk, ctx); 714 715 /* We might need to retry the read from scratch (e.g. */ 716 /* because write was under way and completed before */ 717 /* we could read it from rwb */ 718 if (ftl_read_retry(rc)) { 719 continue; 720 } 721 722 /* We don't have to schedule the read, as it was read from cache */ 723 if (ftl_read_canceled(rc)) { 724 ftl_io_update_iovec(io, 1); 725 lbk++; 726 continue; 727 } 728 729 assert(lbk_cnt > 0); 730 731 ftl_trace_submission(dev, io, ppa, lbk_cnt); 732 rc = spdk_nvme_ns_cmd_read(dev->ns, ftl_get_read_qpair(dev), 733 ftl_io_iovec_addr(io), 734 ftl_ppa_addr_pack(io->dev, ppa), lbk_cnt, 735 ftl_io_cmpl_cb, io, 0); 736 if (rc) { 737 SPDK_ERRLOG("spdk_nvme_ns_cmd_read failed with status: %d\n", rc); 738 io->status = -EIO; 739 break; 740 } 741 742 ftl_io_update_iovec(io, lbk_cnt); 743 ftl_io_inc_req(io); 744 lbk += lbk_cnt; 745 } 746 747 /* If we didn't have to read anything from the device, */ 748 /* complete the request right away */ 749 if (ftl_io_done(io)) { 750 ftl_io_complete(io); 751 } 752 753 return rc; 754 } 755 756 static int 757 ftl_ppa_cache_read(struct ftl_io *io, uint64_t lba, 758 struct ftl_ppa ppa, void *buf) 759 { 760 struct ftl_rwb *rwb = io->dev->rwb; 761 struct ftl_rwb_entry *entry; 762 struct ftl_ppa nppa; 763 int rc = 0; 764 765 entry = ftl_rwb_entry_from_offset(rwb, ppa.offset); 766 pthread_spin_lock(&entry->lock); 767 768 nppa = ftl_l2p_get(io->dev, lba); 769 if (ppa.ppa != nppa.ppa) { 770 rc = -1; 771 goto out; 772 } 773 774 memcpy(buf, entry->data, FTL_BLOCK_SIZE); 775 out: 776 pthread_spin_unlock(&entry->lock); 777 return rc; 778 } 779 780 static int 781 ftl_lba_read_next_ppa(struct ftl_io *io, struct ftl_ppa *ppa, 782 size_t lbk, void *ctx) 783 { 784 struct spdk_ftl_dev *dev = io->dev; 785 *ppa = ftl_l2p_get(dev, io->lba + lbk); 786 787 (void) ctx; 788 789 SPDK_DEBUGLOG(SPDK_LOG_FTL_CORE, "Read ppa:%lx, lba:%lu\n", ppa->ppa, io->lba); 790 791 /* If the PPA is invalid, skip it (the buffer should already be zero'ed) */ 792 if (ftl_ppa_invalid(*ppa)) { 793 ftl_trace_completion(io->dev, io, FTL_TRACE_COMPLETION_INVALID); 794 return 0; 795 } 796 797 if (ftl_ppa_cached(*ppa)) { 798 if (!ftl_ppa_cache_read(io, io->lba + lbk, *ppa, ftl_io_iovec_addr(io))) { 799 ftl_trace_completion(io->dev, io, FTL_TRACE_COMPLETION_CACHE); 800 return 0; 801 } 802 803 /* If the state changed, we have to re-read the l2p */ 804 return -EAGAIN; 805 } 806 807 /* We want to read one lbk at a time */ 808 return 1; 809 } 810 811 static void 812 ftl_complete_flush(struct ftl_flush *flush) 813 { 814 assert(flush->num_req == 0); 815 LIST_REMOVE(flush, list_entry); 816 817 flush->cb.fn(flush->cb.ctx, 0); 818 819 spdk_bit_array_free(&flush->bmap); 820 free(flush); 821 } 822 823 static void 824 ftl_process_flush(struct spdk_ftl_dev *dev, struct ftl_rwb_batch *batch) 825 { 826 struct ftl_flush *flush, *tflush; 827 size_t offset; 828 829 LIST_FOREACH_SAFE(flush, &dev->flush_list, list_entry, tflush) { 830 offset = ftl_rwb_batch_get_offset(batch); 831 832 if (spdk_bit_array_get(flush->bmap, offset)) { 833 spdk_bit_array_set(flush->bmap, offset); 834 if (!(--flush->num_req)) { 835 ftl_complete_flush(flush); 836 } 837 } 838 } 839 } 840 841 static void 842 ftl_write_fail(struct ftl_io *io, int status) 843 { 844 struct ftl_rwb_batch *batch = io->rwb_batch; 845 struct spdk_ftl_dev *dev = io->dev; 846 struct ftl_rwb_entry *entry; 847 struct ftl_band *band; 848 char buf[128]; 849 850 entry = ftl_rwb_batch_first_entry(batch); 851 852 band = ftl_band_from_ppa(io->dev, entry->ppa); 853 SPDK_ERRLOG("Write failed @ppa: %s, status: %d\n", 854 ftl_ppa2str(entry->ppa, buf, sizeof(buf)), status); 855 856 /* Close the band and, halt wptr and defrag */ 857 ftl_halt_writes(dev, band); 858 859 ftl_rwb_foreach(entry, batch) { 860 /* Invalidate meta set by process_writes() */ 861 ftl_invalidate_addr(dev, entry->ppa); 862 } 863 864 /* Reset the batch back to the the RWB to resend it later */ 865 ftl_rwb_batch_revert(batch); 866 } 867 868 static void 869 ftl_write_cb(void *arg, int status) 870 { 871 struct ftl_io *io = arg; 872 struct spdk_ftl_dev *dev = io->dev; 873 struct ftl_rwb_batch *batch = io->rwb_batch; 874 struct ftl_rwb_entry *entry; 875 876 if (status) { 877 ftl_write_fail(io, status); 878 return; 879 } 880 881 assert(io->lbk_cnt == dev->xfer_size); 882 ftl_rwb_foreach(entry, batch) { 883 if (!(io->flags & FTL_IO_MD) && !(entry->flags & FTL_IO_PAD)) { 884 /* Verify that the LBA is set for user lbks */ 885 assert(entry->lba != FTL_LBA_INVALID); 886 } 887 888 SPDK_DEBUGLOG(SPDK_LOG_FTL_CORE, "Write ppa:%lu, lba:%lu\n", 889 entry->ppa.ppa, entry->lba); 890 } 891 892 ftl_process_flush(dev, batch); 893 ftl_rwb_batch_release(batch); 894 } 895 896 static void 897 ftl_update_rwb_stats(struct spdk_ftl_dev *dev, const struct ftl_rwb_entry *entry) 898 { 899 if (!ftl_rwb_entry_internal(entry)) { 900 dev->stats.write_user++; 901 } 902 dev->stats.write_total++; 903 } 904 905 static void 906 ftl_update_l2p(struct spdk_ftl_dev *dev, const struct ftl_rwb_entry *entry, 907 struct ftl_ppa ppa) 908 { 909 struct ftl_ppa prev_ppa; 910 struct ftl_rwb_entry *prev; 911 struct ftl_band *band; 912 int valid; 913 914 prev_ppa = ftl_l2p_get(dev, entry->lba); 915 if (ftl_ppa_invalid(prev_ppa)) { 916 ftl_l2p_set(dev, entry->lba, ppa); 917 return; 918 } 919 920 /* If the L2P's PPA is different than what we expected we don't need to */ 921 /* do anything (someone's already overwritten our data). */ 922 if (ftl_rwb_entry_weak(entry) && !ftl_ppa_cmp(prev_ppa, entry->ppa)) { 923 return; 924 } 925 926 if (ftl_ppa_cached(prev_ppa)) { 927 assert(!ftl_rwb_entry_weak(entry)); 928 prev = ftl_rwb_entry_from_offset(dev->rwb, prev_ppa.offset); 929 pthread_spin_lock(&prev->lock); 930 931 /* Re-read the L2P under the lock to protect against updates */ 932 /* to this LBA from other threads */ 933 prev_ppa = ftl_l2p_get(dev, entry->lba); 934 935 /* If the entry is no longer in cache, another write has been */ 936 /* scheduled in the meantime, so we have to invalidate its LBA */ 937 if (!ftl_ppa_cached(prev_ppa)) { 938 ftl_invalidate_addr(dev, prev_ppa); 939 } 940 941 /* If previous entry is part of cache, remove and invalidate it */ 942 if (ftl_rwb_entry_valid(prev)) { 943 ftl_invalidate_addr(dev, prev->ppa); 944 ftl_rwb_entry_invalidate(prev); 945 } 946 947 ftl_l2p_set(dev, entry->lba, ppa); 948 pthread_spin_unlock(&prev->lock); 949 return; 950 } 951 952 /* Lock the band containing previous PPA. This assures atomic changes to */ 953 /* the L2P as wall as metadata. The valid bits in metadata are used to */ 954 /* check weak writes validity. */ 955 band = ftl_band_from_ppa(dev, prev_ppa); 956 pthread_spin_lock(&band->md.lock); 957 958 valid = ftl_invalidate_addr_unlocked(dev, prev_ppa); 959 960 /* If the address has been invalidated already, we don't want to update */ 961 /* the L2P for weak writes, as it means the write is no longer valid. */ 962 if (!ftl_rwb_entry_weak(entry) || valid) { 963 ftl_l2p_set(dev, entry->lba, ppa); 964 } 965 966 pthread_spin_unlock(&band->md.lock); 967 } 968 969 static int 970 ftl_submit_write(struct ftl_wptr *wptr, struct ftl_io *io) 971 { 972 struct spdk_ftl_dev *dev = io->dev; 973 struct iovec *iov = ftl_io_iovec(io); 974 int rc = 0; 975 size_t i; 976 977 for (i = 0; i < io->iov_cnt; ++i) { 978 assert(iov[i].iov_len > 0); 979 assert(iov[i].iov_len / PAGE_SIZE == dev->xfer_size); 980 981 ftl_trace_submission(dev, io, wptr->ppa, iov[i].iov_len / PAGE_SIZE); 982 rc = spdk_nvme_ns_cmd_write_with_md(dev->ns, ftl_get_write_qpair(dev), 983 iov[i].iov_base, ftl_io_get_md(io), 984 ftl_ppa_addr_pack(dev, wptr->ppa), 985 iov[i].iov_len / PAGE_SIZE, 986 ftl_io_cmpl_cb, io, 0, 0, 0); 987 if (rc) { 988 SPDK_ERRLOG("spdk_nvme_ns_cmd_write failed with status:%d, ppa:%lu\n", 989 rc, wptr->ppa.ppa); 990 io->status = -EIO; 991 break; 992 } 993 994 io->pos = iov[i].iov_len / PAGE_SIZE; 995 ftl_io_inc_req(io); 996 ftl_wptr_advance(wptr, iov[i].iov_len / PAGE_SIZE); 997 } 998 999 if (ftl_io_done(io)) { 1000 ftl_io_complete(io); 1001 } 1002 1003 return rc; 1004 } 1005 1006 static void 1007 ftl_flush_pad_batch(struct spdk_ftl_dev *dev) 1008 { 1009 struct ftl_rwb *rwb = dev->rwb; 1010 size_t size; 1011 1012 size = ftl_rwb_num_acquired(rwb, FTL_RWB_TYPE_INTERNAL) + 1013 ftl_rwb_num_acquired(rwb, FTL_RWB_TYPE_USER); 1014 1015 /* There must be something in the RWB, otherwise the flush */ 1016 /* wouldn't be waiting for anything */ 1017 assert(size > 0); 1018 1019 /* Only add padding when there's less than xfer size */ 1020 /* entries in the buffer. Otherwise we just have to wait */ 1021 /* for the entries to become ready. */ 1022 if (size < dev->xfer_size) { 1023 ftl_rwb_pad(dev, dev->xfer_size - (size % dev->xfer_size)); 1024 } 1025 } 1026 1027 static int 1028 ftl_wptr_process_writes(struct ftl_wptr *wptr) 1029 { 1030 struct spdk_ftl_dev *dev = wptr->dev; 1031 struct ftl_rwb_batch *batch; 1032 struct ftl_rwb_entry *entry; 1033 struct ftl_io *io; 1034 struct ftl_ppa ppa, prev_ppa; 1035 1036 /* Make sure the band is prepared for writing */ 1037 if (!ftl_wptr_ready(wptr)) { 1038 return 0; 1039 } 1040 1041 if (dev->halt) { 1042 ftl_process_shutdown(dev); 1043 } 1044 1045 batch = ftl_rwb_pop(dev->rwb); 1046 if (!batch) { 1047 /* If there are queued flush requests we need to pad the RWB to */ 1048 /* force out remaining entries */ 1049 if (!LIST_EMPTY(&dev->flush_list)) { 1050 ftl_flush_pad_batch(dev); 1051 } 1052 1053 return 0; 1054 } 1055 1056 io = ftl_io_rwb_init(dev, wptr->band, batch, ftl_write_cb); 1057 if (!io) { 1058 goto error; 1059 } 1060 1061 ppa = wptr->ppa; 1062 ftl_rwb_foreach(entry, batch) { 1063 entry->ppa = ppa; 1064 1065 if (entry->lba != FTL_LBA_INVALID) { 1066 pthread_spin_lock(&entry->lock); 1067 prev_ppa = ftl_l2p_get(dev, entry->lba); 1068 1069 /* If the l2p was updated in the meantime, don't update band's metadata */ 1070 if (ftl_ppa_cached(prev_ppa) && prev_ppa.offset == entry->pos) { 1071 /* Setting entry's cache bit needs to be done after metadata */ 1072 /* within the band is updated to make sure that writes */ 1073 /* invalidating the entry clear the metadata as well */ 1074 ftl_band_set_addr(wptr->band, entry->lba, entry->ppa); 1075 ftl_rwb_entry_set_valid(entry); 1076 } 1077 pthread_spin_unlock(&entry->lock); 1078 } 1079 1080 ftl_trace_rwb_pop(dev, entry); 1081 ftl_update_rwb_stats(dev, entry); 1082 1083 ppa = ftl_band_next_ppa(wptr->band, ppa, 1); 1084 } 1085 1086 SPDK_DEBUGLOG(SPDK_LOG_FTL_CORE, "Write ppa:%lx, %lx\n", wptr->ppa.ppa, 1087 ftl_ppa_addr_pack(dev, wptr->ppa)); 1088 1089 if (ftl_submit_write(wptr, io)) { 1090 /* TODO: we need some recovery here */ 1091 assert(0 && "Write submit failed"); 1092 if (ftl_io_done(io)) { 1093 ftl_io_free(io); 1094 } 1095 } 1096 1097 return dev->xfer_size; 1098 error: 1099 ftl_rwb_batch_revert(batch); 1100 return 0; 1101 } 1102 1103 static int 1104 ftl_process_writes(struct spdk_ftl_dev *dev) 1105 { 1106 struct ftl_wptr *wptr, *twptr; 1107 size_t num_active = 0; 1108 enum ftl_band_state state; 1109 1110 LIST_FOREACH_SAFE(wptr, &dev->wptr_list, list_entry, twptr) { 1111 ftl_wptr_process_writes(wptr); 1112 state = wptr->band->state; 1113 1114 if (state != FTL_BAND_STATE_FULL && 1115 state != FTL_BAND_STATE_CLOSING && 1116 state != FTL_BAND_STATE_CLOSED) { 1117 num_active++; 1118 } 1119 } 1120 1121 if (num_active < 1) { 1122 ftl_add_wptr(dev); 1123 } 1124 1125 return 0; 1126 } 1127 1128 static void 1129 ftl_rwb_entry_fill(struct ftl_rwb_entry *entry, struct ftl_io *io) 1130 { 1131 struct ftl_band *band; 1132 1133 memcpy(entry->data, ftl_io_iovec_addr(io), FTL_BLOCK_SIZE); 1134 1135 if (ftl_rwb_entry_weak(entry)) { 1136 band = ftl_band_from_ppa(io->dev, io->ppa); 1137 entry->ppa = ftl_band_next_ppa(band, io->ppa, io->pos); 1138 } 1139 1140 entry->trace = io->trace; 1141 1142 if (entry->md) { 1143 memcpy(entry->md, &entry->lba, sizeof(io->lba)); 1144 } 1145 } 1146 1147 static int 1148 ftl_rwb_fill(struct ftl_io *io) 1149 { 1150 struct spdk_ftl_dev *dev = io->dev; 1151 struct ftl_rwb_entry *entry; 1152 struct ftl_ppa ppa = { .cached = 1 }; 1153 int flags = ftl_rwb_flags_from_io(io); 1154 uint64_t lba; 1155 1156 for (; io->pos < io->lbk_cnt; ++io->pos) { 1157 lba = ftl_io_current_lba(io); 1158 if (lba == FTL_LBA_INVALID) { 1159 ftl_io_update_iovec(io, 1); 1160 continue; 1161 } 1162 1163 entry = ftl_acquire_entry(dev, flags); 1164 if (!entry) { 1165 return -EAGAIN; 1166 } 1167 1168 entry->lba = lba; 1169 ftl_rwb_entry_fill(entry, io); 1170 1171 ppa.offset = entry->pos; 1172 1173 ftl_io_update_iovec(io, 1); 1174 ftl_update_l2p(dev, entry, ppa); 1175 1176 /* Needs to be done after L2P is updated to avoid race with */ 1177 /* write completion callback when it's processed faster than */ 1178 /* L2P is set in update_l2p(). */ 1179 ftl_rwb_push(entry); 1180 ftl_trace_rwb_fill(dev, io); 1181 } 1182 1183 ftl_io_complete(io); 1184 return 0; 1185 } 1186 1187 static bool 1188 ftl_dev_needs_defrag(struct spdk_ftl_dev *dev) 1189 { 1190 const struct spdk_ftl_limit *limit = ftl_get_limit(dev, SPDK_FTL_LIMIT_START); 1191 1192 if (ftl_reloc_is_halted(dev->reloc)) { 1193 return false; 1194 } 1195 1196 if (dev->df_band) { 1197 return false; 1198 } 1199 1200 if (dev->num_free <= limit->thld) { 1201 return true; 1202 } 1203 1204 return false; 1205 } 1206 1207 static double 1208 ftl_band_calc_merit(struct ftl_band *band, size_t *threshold_valid) 1209 { 1210 size_t usable, valid, invalid; 1211 double vld_ratio; 1212 1213 /* If the band doesn't have any usable lbks it's of no use */ 1214 usable = ftl_band_num_usable_lbks(band); 1215 if (usable == 0) { 1216 return 0.0; 1217 } 1218 1219 valid = threshold_valid ? (usable - *threshold_valid) : band->md.num_vld; 1220 invalid = usable - valid; 1221 1222 /* Add one to avoid division by 0 */ 1223 vld_ratio = (double)invalid / (double)(valid + 1); 1224 return vld_ratio * ftl_band_age(band); 1225 } 1226 1227 static bool 1228 ftl_band_needs_defrag(struct ftl_band *band, struct spdk_ftl_dev *dev) 1229 { 1230 struct spdk_ftl_conf *conf = &dev->conf; 1231 size_t thld_vld; 1232 1233 /* If we're in dire need of free bands, every band is worth defragging */ 1234 if (ftl_current_limit(dev) == SPDK_FTL_LIMIT_CRIT) { 1235 return true; 1236 } 1237 1238 thld_vld = (ftl_band_num_usable_lbks(band) * conf->defrag.invalid_thld) / 100; 1239 1240 return band->merit > ftl_band_calc_merit(band, &thld_vld); 1241 } 1242 1243 static struct ftl_band * 1244 ftl_select_defrag_band(struct spdk_ftl_dev *dev) 1245 { 1246 struct ftl_band *band, *mband = NULL; 1247 double merit = 0; 1248 1249 LIST_FOREACH(band, &dev->shut_bands, list_entry) { 1250 assert(band->state == FTL_BAND_STATE_CLOSED); 1251 band->merit = ftl_band_calc_merit(band, NULL); 1252 if (band->merit > merit) { 1253 merit = band->merit; 1254 mband = band; 1255 } 1256 } 1257 1258 if (mband && !ftl_band_needs_defrag(mband, dev)) { 1259 mband = NULL; 1260 } 1261 1262 return mband; 1263 } 1264 1265 static void 1266 ftl_process_relocs(struct spdk_ftl_dev *dev) 1267 { 1268 if (ftl_dev_needs_defrag(dev)) { 1269 dev->df_band = ftl_select_defrag_band(dev); 1270 if (dev->df_band) { 1271 ftl_reloc_add(dev->reloc, dev->df_band, 0, ftl_num_band_lbks(dev), 0); 1272 } 1273 } 1274 1275 ftl_reloc(dev->reloc); 1276 } 1277 1278 int 1279 ftl_current_limit(const struct spdk_ftl_dev *dev) 1280 { 1281 return dev->limit; 1282 } 1283 1284 void 1285 spdk_ftl_dev_get_attrs(const struct spdk_ftl_dev *dev, struct spdk_ftl_attrs *attrs) 1286 { 1287 attrs->uuid = dev->uuid; 1288 attrs->lbk_cnt = dev->num_lbas; 1289 attrs->lbk_size = FTL_BLOCK_SIZE; 1290 attrs->range = dev->range; 1291 } 1292 1293 static void 1294 _ftl_io_write(void *ctx) 1295 { 1296 ftl_io_write((struct ftl_io *)ctx); 1297 } 1298 1299 int 1300 ftl_io_write(struct ftl_io *io) 1301 { 1302 struct spdk_ftl_dev *dev = io->dev; 1303 1304 /* For normal IOs we just need to copy the data onto the rwb */ 1305 if (!(io->flags & FTL_IO_MD)) { 1306 return ftl_rwb_fill(io); 1307 } 1308 1309 /* Metadata has its own buffer, so it doesn't have to be copied, so just */ 1310 /* send it the the core thread and schedule the write immediately */ 1311 if (ftl_check_core_thread(dev)) { 1312 return ftl_submit_write(ftl_wptr_from_band(io->band), io); 1313 } 1314 1315 spdk_thread_send_msg(ftl_get_core_thread(dev), _ftl_io_write, io); 1316 1317 return 0; 1318 } 1319 1320 1321 1322 static int 1323 _spdk_ftl_write(struct ftl_io *io) 1324 { 1325 int rc; 1326 1327 rc = ftl_io_write(io); 1328 if (rc == -EAGAIN) { 1329 spdk_thread_send_msg(spdk_io_channel_get_thread(io->ch), 1330 _ftl_write, io); 1331 return 0; 1332 } 1333 1334 if (rc) { 1335 ftl_io_free(io); 1336 } 1337 1338 return rc; 1339 } 1340 1341 static void 1342 _ftl_write(void *ctx) 1343 { 1344 _spdk_ftl_write(ctx); 1345 } 1346 1347 int 1348 spdk_ftl_write(struct spdk_ftl_dev *dev, struct spdk_io_channel *ch, uint64_t lba, size_t lba_cnt, 1349 struct iovec *iov, size_t iov_cnt, spdk_ftl_fn cb_fn, void *cb_arg) 1350 { 1351 struct ftl_io *io; 1352 1353 if (iov_cnt == 0 || iov_cnt > FTL_MAX_IOV) { 1354 return -EINVAL; 1355 } 1356 1357 if (lba_cnt == 0) { 1358 return -EINVAL; 1359 } 1360 1361 if (lba_cnt != ftl_iovec_num_lbks(iov, iov_cnt)) { 1362 return -EINVAL; 1363 } 1364 1365 if (!dev->initialized) { 1366 return -EBUSY; 1367 } 1368 1369 io = ftl_io_alloc(ch); 1370 if (!io) { 1371 return -ENOMEM; 1372 } 1373 1374 ftl_io_user_init(dev, io, lba, lba_cnt, iov, iov_cnt, cb_fn, cb_arg, FTL_IO_WRITE); 1375 return _spdk_ftl_write(io); 1376 } 1377 1378 int 1379 ftl_io_read(struct ftl_io *io) 1380 { 1381 struct spdk_ftl_dev *dev = io->dev; 1382 ftl_next_ppa_fn next_ppa; 1383 1384 if (ftl_check_read_thread(dev)) { 1385 if (ftl_io_mode_ppa(io)) { 1386 next_ppa = ftl_ppa_read_next_ppa; 1387 } else { 1388 next_ppa = ftl_lba_read_next_ppa; 1389 } 1390 1391 return ftl_submit_read(io, next_ppa, NULL); 1392 } 1393 1394 spdk_thread_send_msg(ftl_get_read_thread(dev), _ftl_read, io); 1395 return 0; 1396 } 1397 1398 static void 1399 _ftl_read(void *arg) 1400 { 1401 ftl_io_read((struct ftl_io *)arg); 1402 } 1403 1404 int 1405 spdk_ftl_read(struct spdk_ftl_dev *dev, struct spdk_io_channel *ch, uint64_t lba, size_t lba_cnt, 1406 struct iovec *iov, size_t iov_cnt, spdk_ftl_fn cb_fn, void *cb_arg) 1407 { 1408 struct ftl_io *io; 1409 1410 if (iov_cnt == 0 || iov_cnt > FTL_MAX_IOV) { 1411 return -EINVAL; 1412 } 1413 1414 if (lba_cnt == 0) { 1415 return -EINVAL; 1416 } 1417 1418 if (lba_cnt != ftl_iovec_num_lbks(iov, iov_cnt)) { 1419 return -EINVAL; 1420 } 1421 1422 if (!dev->initialized) { 1423 return -EBUSY; 1424 } 1425 1426 io = ftl_io_alloc(ch); 1427 if (!io) { 1428 return -ENOMEM; 1429 } 1430 1431 ftl_io_user_init(dev, io, lba, lba_cnt, iov, iov_cnt, cb_fn, cb_arg, FTL_IO_READ); 1432 return ftl_io_read(io); 1433 } 1434 1435 static struct ftl_flush * 1436 ftl_flush_init(struct spdk_ftl_dev *dev, spdk_ftl_fn cb_fn, void *cb_arg) 1437 { 1438 struct ftl_flush *flush; 1439 struct ftl_rwb *rwb = dev->rwb; 1440 1441 flush = calloc(1, sizeof(*flush)); 1442 if (!flush) { 1443 return NULL; 1444 } 1445 1446 flush->bmap = spdk_bit_array_create(ftl_rwb_num_batches(rwb)); 1447 if (!flush->bmap) { 1448 goto error; 1449 } 1450 1451 flush->dev = dev; 1452 flush->cb.fn = cb_fn; 1453 flush->cb.ctx = cb_arg; 1454 1455 return flush; 1456 error: 1457 free(flush); 1458 return NULL; 1459 } 1460 1461 static void 1462 _ftl_flush(void *ctx) 1463 { 1464 struct ftl_flush *flush = ctx; 1465 struct spdk_ftl_dev *dev = flush->dev; 1466 struct ftl_rwb *rwb = dev->rwb; 1467 struct ftl_rwb_batch *batch; 1468 1469 /* Attach flush object to all non-empty batches */ 1470 ftl_rwb_foreach_batch(batch, rwb) { 1471 if (!ftl_rwb_batch_empty(batch)) { 1472 spdk_bit_array_set(flush->bmap, ftl_rwb_batch_get_offset(batch)); 1473 flush->num_req++; 1474 } 1475 } 1476 1477 LIST_INSERT_HEAD(&dev->flush_list, flush, list_entry); 1478 1479 /* If the RWB was already empty, the flush can be completed right away */ 1480 if (!flush->num_req) { 1481 ftl_complete_flush(flush); 1482 } 1483 } 1484 1485 int 1486 spdk_ftl_flush(struct spdk_ftl_dev *dev, spdk_ftl_fn cb_fn, void *cb_arg) 1487 { 1488 struct ftl_flush *flush; 1489 1490 if (!dev->initialized) { 1491 return -EBUSY; 1492 } 1493 1494 flush = ftl_flush_init(dev, cb_fn, cb_arg); 1495 if (!flush) { 1496 return -ENOMEM; 1497 } 1498 1499 spdk_thread_send_msg(ftl_get_core_thread(dev), _ftl_flush, flush); 1500 return 0; 1501 } 1502 1503 void 1504 ftl_process_anm_event(struct ftl_anm_event *event) 1505 { 1506 SPDK_DEBUGLOG(SPDK_LOG_FTL_CORE, "Unconsumed ANM received for dev: %p...\n", event->dev); 1507 ftl_anm_event_complete(event); 1508 } 1509 1510 int 1511 ftl_task_read(void *ctx) 1512 { 1513 struct ftl_thread *thread = ctx; 1514 struct spdk_ftl_dev *dev = thread->dev; 1515 struct spdk_nvme_qpair *qpair = ftl_get_read_qpair(dev); 1516 1517 if (dev->halt) { 1518 if (ftl_shutdown_complete(dev)) { 1519 spdk_poller_unregister(&thread->poller); 1520 return 0; 1521 } 1522 } 1523 1524 return spdk_nvme_qpair_process_completions(qpair, 1); 1525 } 1526 1527 int 1528 ftl_task_core(void *ctx) 1529 { 1530 struct ftl_thread *thread = ctx; 1531 struct spdk_ftl_dev *dev = thread->dev; 1532 struct spdk_nvme_qpair *qpair = ftl_get_write_qpair(dev); 1533 1534 if (dev->halt) { 1535 if (ftl_shutdown_complete(dev)) { 1536 spdk_poller_unregister(&thread->poller); 1537 return 0; 1538 } 1539 } 1540 1541 ftl_process_writes(dev); 1542 spdk_nvme_qpair_process_completions(qpair, 1); 1543 ftl_process_relocs(dev); 1544 1545 return 0; 1546 } 1547 1548 SPDK_LOG_REGISTER_COMPONENT("ftl_core", SPDK_LOG_FTL_CORE) 1549