1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved. 6 * Copyright (c) 2021, 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "spdk/stdinc.h" 36 37 #include "spdk/bdev.h" 38 39 #include "spdk/config.h" 40 #include "spdk/env.h" 41 #include "spdk/thread.h" 42 #include "spdk/likely.h" 43 #include "spdk/queue.h" 44 #include "spdk/nvme_spec.h" 45 #include "spdk/scsi_spec.h" 46 #include "spdk/notify.h" 47 #include "spdk/util.h" 48 #include "spdk/trace.h" 49 #include "spdk/dma.h" 50 51 #include "spdk/bdev_module.h" 52 #include "spdk/log.h" 53 #include "spdk/string.h" 54 55 #include "bdev_internal.h" 56 #include "spdk_internal/trace_defs.h" 57 58 #ifdef SPDK_CONFIG_VTUNE 59 #include "ittnotify.h" 60 #include "ittnotify_types.h" 61 int __itt_init_ittlib(const char *, __itt_group_id); 62 #endif 63 64 #define SPDK_BDEV_IO_POOL_SIZE (64 * 1024 - 1) 65 #define SPDK_BDEV_IO_CACHE_SIZE 256 66 #define SPDK_BDEV_AUTO_EXAMINE true 67 #define BUF_SMALL_POOL_SIZE 8191 68 #define BUF_LARGE_POOL_SIZE 1023 69 #define NOMEM_THRESHOLD_COUNT 8 70 71 #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC 1000 72 #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE 1 73 #define SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE 512 74 #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC 1000 75 #define SPDK_BDEV_QOS_MIN_BYTES_PER_SEC (1024 * 1024) 76 #define SPDK_BDEV_QOS_LIMIT_NOT_DEFINED UINT64_MAX 77 #define SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC 1000 78 79 #define SPDK_BDEV_POOL_ALIGNMENT 512 80 81 /* The maximum number of children requests for a UNMAP or WRITE ZEROES command 82 * when splitting into children requests at a time. 83 */ 84 #define SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS (8) 85 86 static const char *qos_rpc_type[] = {"rw_ios_per_sec", 87 "rw_mbytes_per_sec", "r_mbytes_per_sec", "w_mbytes_per_sec" 88 }; 89 90 TAILQ_HEAD(spdk_bdev_list, spdk_bdev); 91 92 RB_HEAD(bdev_name_tree, spdk_bdev_name); 93 94 static int 95 bdev_name_cmp(struct spdk_bdev_name *name1, struct spdk_bdev_name *name2) 96 { 97 return strcmp(name1->name, name2->name); 98 } 99 100 RB_GENERATE_STATIC(bdev_name_tree, spdk_bdev_name, node, bdev_name_cmp); 101 102 struct spdk_bdev_mgr { 103 struct spdk_mempool *bdev_io_pool; 104 105 struct spdk_mempool *buf_small_pool; 106 struct spdk_mempool *buf_large_pool; 107 108 void *zero_buffer; 109 110 TAILQ_HEAD(bdev_module_list, spdk_bdev_module) bdev_modules; 111 112 struct spdk_bdev_list bdevs; 113 struct bdev_name_tree bdev_names; 114 115 bool init_complete; 116 bool module_init_complete; 117 118 pthread_mutex_t mutex; 119 120 #ifdef SPDK_CONFIG_VTUNE 121 __itt_domain *domain; 122 #endif 123 }; 124 125 static struct spdk_bdev_mgr g_bdev_mgr = { 126 .bdev_modules = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdev_modules), 127 .bdevs = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdevs), 128 .bdev_names = RB_INITIALIZER(g_bdev_mgr.bdev_names), 129 .init_complete = false, 130 .module_init_complete = false, 131 .mutex = PTHREAD_MUTEX_INITIALIZER, 132 }; 133 134 typedef void (*lock_range_cb)(void *ctx, int status); 135 136 typedef void (*bdev_copy_bounce_buffer_cpl)(void *ctx, int rc); 137 138 struct lba_range { 139 uint64_t offset; 140 uint64_t length; 141 void *locked_ctx; 142 struct spdk_bdev_channel *owner_ch; 143 TAILQ_ENTRY(lba_range) tailq; 144 }; 145 146 static struct spdk_bdev_opts g_bdev_opts = { 147 .bdev_io_pool_size = SPDK_BDEV_IO_POOL_SIZE, 148 .bdev_io_cache_size = SPDK_BDEV_IO_CACHE_SIZE, 149 .bdev_auto_examine = SPDK_BDEV_AUTO_EXAMINE, 150 .small_buf_pool_size = BUF_SMALL_POOL_SIZE, 151 .large_buf_pool_size = BUF_LARGE_POOL_SIZE, 152 }; 153 154 static spdk_bdev_init_cb g_init_cb_fn = NULL; 155 static void *g_init_cb_arg = NULL; 156 157 static spdk_bdev_fini_cb g_fini_cb_fn = NULL; 158 static void *g_fini_cb_arg = NULL; 159 static struct spdk_thread *g_fini_thread = NULL; 160 161 struct spdk_bdev_qos_limit { 162 /** IOs or bytes allowed per second (i.e., 1s). */ 163 uint64_t limit; 164 165 /** Remaining IOs or bytes allowed in current timeslice (e.g., 1ms). 166 * For remaining bytes, allowed to run negative if an I/O is submitted when 167 * some bytes are remaining, but the I/O is bigger than that amount. The 168 * excess will be deducted from the next timeslice. 169 */ 170 int64_t remaining_this_timeslice; 171 172 /** Minimum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */ 173 uint32_t min_per_timeslice; 174 175 /** Maximum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */ 176 uint32_t max_per_timeslice; 177 178 /** Function to check whether to queue the IO. */ 179 bool (*queue_io)(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io); 180 181 /** Function to update for the submitted IO. */ 182 void (*update_quota)(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io); 183 }; 184 185 struct spdk_bdev_qos { 186 /** Types of structure of rate limits. */ 187 struct spdk_bdev_qos_limit rate_limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES]; 188 189 /** The channel that all I/O are funneled through. */ 190 struct spdk_bdev_channel *ch; 191 192 /** The thread on which the poller is running. */ 193 struct spdk_thread *thread; 194 195 /** Queue of I/O waiting to be issued. */ 196 bdev_io_tailq_t queued; 197 198 /** Size of a timeslice in tsc ticks. */ 199 uint64_t timeslice_size; 200 201 /** Timestamp of start of last timeslice. */ 202 uint64_t last_timeslice; 203 204 /** Poller that processes queued I/O commands each time slice. */ 205 struct spdk_poller *poller; 206 }; 207 208 struct spdk_bdev_mgmt_channel { 209 bdev_io_stailq_t need_buf_small; 210 bdev_io_stailq_t need_buf_large; 211 212 /* 213 * Each thread keeps a cache of bdev_io - this allows 214 * bdev threads which are *not* DPDK threads to still 215 * benefit from a per-thread bdev_io cache. Without 216 * this, non-DPDK threads fetching from the mempool 217 * incur a cmpxchg on get and put. 218 */ 219 bdev_io_stailq_t per_thread_cache; 220 uint32_t per_thread_cache_count; 221 uint32_t bdev_io_cache_size; 222 223 TAILQ_HEAD(, spdk_bdev_shared_resource) shared_resources; 224 TAILQ_HEAD(, spdk_bdev_io_wait_entry) io_wait_queue; 225 }; 226 227 /* 228 * Per-module (or per-io_device) data. Multiple bdevs built on the same io_device 229 * will queue here their IO that awaits retry. It makes it possible to retry sending 230 * IO to one bdev after IO from other bdev completes. 231 */ 232 struct spdk_bdev_shared_resource { 233 /* The bdev management channel */ 234 struct spdk_bdev_mgmt_channel *mgmt_ch; 235 236 /* 237 * Count of I/O submitted to bdev module and waiting for completion. 238 * Incremented before submit_request() is called on an spdk_bdev_io. 239 */ 240 uint64_t io_outstanding; 241 242 /* 243 * Queue of IO awaiting retry because of a previous NOMEM status returned 244 * on this channel. 245 */ 246 bdev_io_tailq_t nomem_io; 247 248 /* 249 * Threshold which io_outstanding must drop to before retrying nomem_io. 250 */ 251 uint64_t nomem_threshold; 252 253 /* I/O channel allocated by a bdev module */ 254 struct spdk_io_channel *shared_ch; 255 256 /* Refcount of bdev channels using this resource */ 257 uint32_t ref; 258 259 TAILQ_ENTRY(spdk_bdev_shared_resource) link; 260 }; 261 262 #define BDEV_CH_RESET_IN_PROGRESS (1 << 0) 263 #define BDEV_CH_QOS_ENABLED (1 << 1) 264 265 struct spdk_bdev_channel { 266 struct spdk_bdev *bdev; 267 268 /* The channel for the underlying device */ 269 struct spdk_io_channel *channel; 270 271 /* Per io_device per thread data */ 272 struct spdk_bdev_shared_resource *shared_resource; 273 274 struct spdk_bdev_io_stat stat; 275 276 /* 277 * Count of I/O submitted to the underlying dev module through this channel 278 * and waiting for completion. 279 */ 280 uint64_t io_outstanding; 281 282 /* 283 * List of all submitted I/Os including I/O that are generated via splitting. 284 */ 285 bdev_io_tailq_t io_submitted; 286 287 /* 288 * List of spdk_bdev_io that are currently queued because they write to a locked 289 * LBA range. 290 */ 291 bdev_io_tailq_t io_locked; 292 293 uint32_t flags; 294 295 struct spdk_histogram_data *histogram; 296 297 #ifdef SPDK_CONFIG_VTUNE 298 uint64_t start_tsc; 299 uint64_t interval_tsc; 300 __itt_string_handle *handle; 301 struct spdk_bdev_io_stat prev_stat; 302 #endif 303 304 bdev_io_tailq_t queued_resets; 305 306 lba_range_tailq_t locked_ranges; 307 }; 308 309 struct media_event_entry { 310 struct spdk_bdev_media_event event; 311 TAILQ_ENTRY(media_event_entry) tailq; 312 }; 313 314 #define MEDIA_EVENT_POOL_SIZE 64 315 316 struct spdk_bdev_desc { 317 struct spdk_bdev *bdev; 318 struct spdk_thread *thread; 319 struct { 320 spdk_bdev_event_cb_t event_fn; 321 void *ctx; 322 } callback; 323 bool closed; 324 bool write; 325 bool memory_domains_supported; 326 pthread_mutex_t mutex; 327 uint32_t refs; 328 TAILQ_HEAD(, media_event_entry) pending_media_events; 329 TAILQ_HEAD(, media_event_entry) free_media_events; 330 struct media_event_entry *media_events_buffer; 331 TAILQ_ENTRY(spdk_bdev_desc) link; 332 333 uint64_t timeout_in_sec; 334 spdk_bdev_io_timeout_cb cb_fn; 335 void *cb_arg; 336 struct spdk_poller *io_timeout_poller; 337 }; 338 339 struct spdk_bdev_iostat_ctx { 340 struct spdk_bdev_io_stat *stat; 341 spdk_bdev_get_device_stat_cb cb; 342 void *cb_arg; 343 }; 344 345 struct set_qos_limit_ctx { 346 void (*cb_fn)(void *cb_arg, int status); 347 void *cb_arg; 348 struct spdk_bdev *bdev; 349 }; 350 351 #define __bdev_to_io_dev(bdev) (((char *)bdev) + 1) 352 #define __bdev_from_io_dev(io_dev) ((struct spdk_bdev *)(((char *)io_dev) - 1)) 353 354 static inline void bdev_io_complete(void *ctx); 355 356 static void bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 357 static void bdev_write_zero_buffer_next(void *_bdev_io); 358 359 static void bdev_enable_qos_msg(struct spdk_io_channel_iter *i); 360 static void bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status); 361 362 static int 363 bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 364 struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks, 365 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg, 366 struct spdk_bdev_ext_io_opts *opts, bool copy_opts); 367 static int 368 bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 369 struct iovec *iov, int iovcnt, void *md_buf, 370 uint64_t offset_blocks, uint64_t num_blocks, 371 spdk_bdev_io_completion_cb cb, void *cb_arg, 372 struct spdk_bdev_ext_io_opts *opts, bool copy_opts); 373 374 static int 375 bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 376 uint64_t offset, uint64_t length, 377 lock_range_cb cb_fn, void *cb_arg); 378 379 static int 380 bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 381 uint64_t offset, uint64_t length, 382 lock_range_cb cb_fn, void *cb_arg); 383 384 static inline void bdev_io_complete(void *ctx); 385 386 static bool bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort); 387 static bool bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_io *bio_to_abort); 388 389 void 390 spdk_bdev_get_opts(struct spdk_bdev_opts *opts, size_t opts_size) 391 { 392 if (!opts) { 393 SPDK_ERRLOG("opts should not be NULL\n"); 394 return; 395 } 396 397 if (!opts_size) { 398 SPDK_ERRLOG("opts_size should not be zero value\n"); 399 return; 400 } 401 402 opts->opts_size = opts_size; 403 404 #define SET_FIELD(field) \ 405 if (offsetof(struct spdk_bdev_opts, field) + sizeof(opts->field) <= opts_size) { \ 406 opts->field = g_bdev_opts.field; \ 407 } \ 408 409 SET_FIELD(bdev_io_pool_size); 410 SET_FIELD(bdev_io_cache_size); 411 SET_FIELD(bdev_auto_examine); 412 SET_FIELD(small_buf_pool_size); 413 SET_FIELD(large_buf_pool_size); 414 415 /* Do not remove this statement, you should always update this statement when you adding a new field, 416 * and do not forget to add the SET_FIELD statement for your added field. */ 417 SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_opts) == 32, "Incorrect size"); 418 419 #undef SET_FIELD 420 } 421 422 int 423 spdk_bdev_set_opts(struct spdk_bdev_opts *opts) 424 { 425 uint32_t min_pool_size; 426 427 if (!opts) { 428 SPDK_ERRLOG("opts cannot be NULL\n"); 429 return -1; 430 } 431 432 if (!opts->opts_size) { 433 SPDK_ERRLOG("opts_size inside opts cannot be zero value\n"); 434 return -1; 435 } 436 437 /* 438 * Add 1 to the thread count to account for the extra mgmt_ch that gets created during subsystem 439 * initialization. A second mgmt_ch will be created on the same thread when the application starts 440 * but before the deferred put_io_channel event is executed for the first mgmt_ch. 441 */ 442 min_pool_size = opts->bdev_io_cache_size * (spdk_thread_get_count() + 1); 443 if (opts->bdev_io_pool_size < min_pool_size) { 444 SPDK_ERRLOG("bdev_io_pool_size %" PRIu32 " is not compatible with bdev_io_cache_size %" PRIu32 445 " and %" PRIu32 " threads\n", opts->bdev_io_pool_size, opts->bdev_io_cache_size, 446 spdk_thread_get_count()); 447 SPDK_ERRLOG("bdev_io_pool_size must be at least %" PRIu32 "\n", min_pool_size); 448 return -1; 449 } 450 451 if (opts->small_buf_pool_size < BUF_SMALL_POOL_SIZE) { 452 SPDK_ERRLOG("small_buf_pool_size must be at least %" PRIu32 "\n", BUF_SMALL_POOL_SIZE); 453 return -1; 454 } 455 456 if (opts->large_buf_pool_size < BUF_LARGE_POOL_SIZE) { 457 SPDK_ERRLOG("large_buf_pool_size must be at least %" PRIu32 "\n", BUF_LARGE_POOL_SIZE); 458 return -1; 459 } 460 461 #define SET_FIELD(field) \ 462 if (offsetof(struct spdk_bdev_opts, field) + sizeof(opts->field) <= opts->opts_size) { \ 463 g_bdev_opts.field = opts->field; \ 464 } \ 465 466 SET_FIELD(bdev_io_pool_size); 467 SET_FIELD(bdev_io_cache_size); 468 SET_FIELD(bdev_auto_examine); 469 SET_FIELD(small_buf_pool_size); 470 SET_FIELD(large_buf_pool_size); 471 472 g_bdev_opts.opts_size = opts->opts_size; 473 474 #undef SET_FIELD 475 476 return 0; 477 } 478 479 static struct spdk_bdev * 480 bdev_get_by_name(const char *bdev_name) 481 { 482 struct spdk_bdev_name find; 483 struct spdk_bdev_name *res; 484 485 find.name = (char *)bdev_name; 486 res = RB_FIND(bdev_name_tree, &g_bdev_mgr.bdev_names, &find); 487 if (res != NULL) { 488 return res->bdev; 489 } 490 491 return NULL; 492 } 493 494 struct spdk_bdev * 495 spdk_bdev_get_by_name(const char *bdev_name) 496 { 497 struct spdk_bdev *bdev; 498 499 pthread_mutex_lock(&g_bdev_mgr.mutex); 500 bdev = bdev_get_by_name(bdev_name); 501 pthread_mutex_unlock(&g_bdev_mgr.mutex); 502 503 return bdev; 504 } 505 506 struct spdk_bdev_wait_for_examine_ctx { 507 struct spdk_poller *poller; 508 spdk_bdev_wait_for_examine_cb cb_fn; 509 void *cb_arg; 510 }; 511 512 static bool 513 bdev_module_all_actions_completed(void); 514 515 static int 516 bdev_wait_for_examine_cb(void *arg) 517 { 518 struct spdk_bdev_wait_for_examine_ctx *ctx = arg; 519 520 if (!bdev_module_all_actions_completed()) { 521 return SPDK_POLLER_IDLE; 522 } 523 524 spdk_poller_unregister(&ctx->poller); 525 ctx->cb_fn(ctx->cb_arg); 526 free(ctx); 527 528 return SPDK_POLLER_BUSY; 529 } 530 531 int 532 spdk_bdev_wait_for_examine(spdk_bdev_wait_for_examine_cb cb_fn, void *cb_arg) 533 { 534 struct spdk_bdev_wait_for_examine_ctx *ctx; 535 536 ctx = calloc(1, sizeof(*ctx)); 537 if (ctx == NULL) { 538 return -ENOMEM; 539 } 540 ctx->cb_fn = cb_fn; 541 ctx->cb_arg = cb_arg; 542 ctx->poller = SPDK_POLLER_REGISTER(bdev_wait_for_examine_cb, ctx, 0); 543 544 return 0; 545 } 546 547 struct spdk_bdev_examine_item { 548 char *name; 549 TAILQ_ENTRY(spdk_bdev_examine_item) link; 550 }; 551 552 TAILQ_HEAD(spdk_bdev_examine_allowlist, spdk_bdev_examine_item); 553 554 struct spdk_bdev_examine_allowlist g_bdev_examine_allowlist = TAILQ_HEAD_INITIALIZER( 555 g_bdev_examine_allowlist); 556 557 static inline bool 558 bdev_examine_allowlist_check(const char *name) 559 { 560 struct spdk_bdev_examine_item *item; 561 TAILQ_FOREACH(item, &g_bdev_examine_allowlist, link) { 562 if (strcmp(name, item->name) == 0) { 563 return true; 564 } 565 } 566 return false; 567 } 568 569 static inline void 570 bdev_examine_allowlist_free(void) 571 { 572 struct spdk_bdev_examine_item *item; 573 while (!TAILQ_EMPTY(&g_bdev_examine_allowlist)) { 574 item = TAILQ_FIRST(&g_bdev_examine_allowlist); 575 TAILQ_REMOVE(&g_bdev_examine_allowlist, item, link); 576 free(item->name); 577 free(item); 578 } 579 } 580 581 static inline bool 582 bdev_in_examine_allowlist(struct spdk_bdev *bdev) 583 { 584 struct spdk_bdev_alias *tmp; 585 if (bdev_examine_allowlist_check(bdev->name)) { 586 return true; 587 } 588 TAILQ_FOREACH(tmp, &bdev->aliases, tailq) { 589 if (bdev_examine_allowlist_check(tmp->alias.name)) { 590 return true; 591 } 592 } 593 return false; 594 } 595 596 static inline bool 597 bdev_ok_to_examine(struct spdk_bdev *bdev) 598 { 599 if (g_bdev_opts.bdev_auto_examine) { 600 return true; 601 } else { 602 return bdev_in_examine_allowlist(bdev); 603 } 604 } 605 606 static void 607 bdev_examine(struct spdk_bdev *bdev) 608 { 609 struct spdk_bdev_module *module; 610 uint32_t action; 611 612 TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) { 613 if (module->examine_config && bdev_ok_to_examine(bdev)) { 614 action = module->internal.action_in_progress; 615 module->internal.action_in_progress++; 616 module->examine_config(bdev); 617 if (action != module->internal.action_in_progress) { 618 SPDK_ERRLOG("examine_config for module %s did not call spdk_bdev_module_examine_done()\n", 619 module->name); 620 } 621 } 622 } 623 624 if (bdev->internal.claim_module && bdev_ok_to_examine(bdev)) { 625 if (bdev->internal.claim_module->examine_disk) { 626 bdev->internal.claim_module->internal.action_in_progress++; 627 bdev->internal.claim_module->examine_disk(bdev); 628 } 629 return; 630 } 631 632 TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) { 633 if (module->examine_disk && bdev_ok_to_examine(bdev)) { 634 module->internal.action_in_progress++; 635 module->examine_disk(bdev); 636 } 637 } 638 } 639 640 int 641 spdk_bdev_examine(const char *name) 642 { 643 struct spdk_bdev *bdev; 644 struct spdk_bdev_examine_item *item; 645 646 if (g_bdev_opts.bdev_auto_examine) { 647 SPDK_ERRLOG("Manual examine is not allowed if auto examine is enabled"); 648 return -EINVAL; 649 } 650 651 if (bdev_examine_allowlist_check(name)) { 652 SPDK_ERRLOG("Duplicate bdev name for manual examine: %s\n", name); 653 return -EEXIST; 654 } 655 656 item = calloc(1, sizeof(*item)); 657 if (!item) { 658 return -ENOMEM; 659 } 660 item->name = strdup(name); 661 if (!item->name) { 662 free(item); 663 return -ENOMEM; 664 } 665 TAILQ_INSERT_TAIL(&g_bdev_examine_allowlist, item, link); 666 667 bdev = spdk_bdev_get_by_name(name); 668 if (bdev) { 669 bdev_examine(bdev); 670 } 671 return 0; 672 } 673 674 static inline void 675 bdev_examine_allowlist_config_json(struct spdk_json_write_ctx *w) 676 { 677 struct spdk_bdev_examine_item *item; 678 TAILQ_FOREACH(item, &g_bdev_examine_allowlist, link) { 679 spdk_json_write_object_begin(w); 680 spdk_json_write_named_string(w, "method", "bdev_examine"); 681 spdk_json_write_named_object_begin(w, "params"); 682 spdk_json_write_named_string(w, "name", item->name); 683 spdk_json_write_object_end(w); 684 spdk_json_write_object_end(w); 685 } 686 } 687 688 struct spdk_bdev * 689 spdk_bdev_first(void) 690 { 691 struct spdk_bdev *bdev; 692 693 bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs); 694 if (bdev) { 695 SPDK_DEBUGLOG(bdev, "Starting bdev iteration at %s\n", bdev->name); 696 } 697 698 return bdev; 699 } 700 701 struct spdk_bdev * 702 spdk_bdev_next(struct spdk_bdev *prev) 703 { 704 struct spdk_bdev *bdev; 705 706 bdev = TAILQ_NEXT(prev, internal.link); 707 if (bdev) { 708 SPDK_DEBUGLOG(bdev, "Continuing bdev iteration at %s\n", bdev->name); 709 } 710 711 return bdev; 712 } 713 714 static struct spdk_bdev * 715 _bdev_next_leaf(struct spdk_bdev *bdev) 716 { 717 while (bdev != NULL) { 718 if (bdev->internal.claim_module == NULL) { 719 return bdev; 720 } else { 721 bdev = TAILQ_NEXT(bdev, internal.link); 722 } 723 } 724 725 return bdev; 726 } 727 728 struct spdk_bdev * 729 spdk_bdev_first_leaf(void) 730 { 731 struct spdk_bdev *bdev; 732 733 bdev = _bdev_next_leaf(TAILQ_FIRST(&g_bdev_mgr.bdevs)); 734 735 if (bdev) { 736 SPDK_DEBUGLOG(bdev, "Starting bdev iteration at %s\n", bdev->name); 737 } 738 739 return bdev; 740 } 741 742 struct spdk_bdev * 743 spdk_bdev_next_leaf(struct spdk_bdev *prev) 744 { 745 struct spdk_bdev *bdev; 746 747 bdev = _bdev_next_leaf(TAILQ_NEXT(prev, internal.link)); 748 749 if (bdev) { 750 SPDK_DEBUGLOG(bdev, "Continuing bdev iteration at %s\n", bdev->name); 751 } 752 753 return bdev; 754 } 755 756 static inline bool 757 bdev_io_use_memory_domain(struct spdk_bdev_io *bdev_io) 758 { 759 return bdev_io->internal.ext_opts && bdev_io->internal.ext_opts->memory_domain; 760 } 761 762 void 763 spdk_bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len) 764 { 765 struct iovec *iovs; 766 767 if (bdev_io->u.bdev.iovs == NULL) { 768 bdev_io->u.bdev.iovs = &bdev_io->iov; 769 bdev_io->u.bdev.iovcnt = 1; 770 } 771 772 iovs = bdev_io->u.bdev.iovs; 773 774 assert(iovs != NULL); 775 assert(bdev_io->u.bdev.iovcnt >= 1); 776 777 iovs[0].iov_base = buf; 778 iovs[0].iov_len = len; 779 } 780 781 void 782 spdk_bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len) 783 { 784 assert((len / spdk_bdev_get_md_size(bdev_io->bdev)) >= bdev_io->u.bdev.num_blocks); 785 bdev_io->u.bdev.md_buf = md_buf; 786 } 787 788 static bool 789 _is_buf_allocated(const struct iovec *iovs) 790 { 791 if (iovs == NULL) { 792 return false; 793 } 794 795 return iovs[0].iov_base != NULL; 796 } 797 798 static bool 799 _are_iovs_aligned(struct iovec *iovs, int iovcnt, uint32_t alignment) 800 { 801 int i; 802 uintptr_t iov_base; 803 804 if (spdk_likely(alignment == 1)) { 805 return true; 806 } 807 808 for (i = 0; i < iovcnt; i++) { 809 iov_base = (uintptr_t)iovs[i].iov_base; 810 if ((iov_base & (alignment - 1)) != 0) { 811 return false; 812 } 813 } 814 815 return true; 816 } 817 818 static void 819 _copy_iovs_to_buf(void *buf, size_t buf_len, struct iovec *iovs, int iovcnt) 820 { 821 int i; 822 size_t len; 823 824 for (i = 0; i < iovcnt; i++) { 825 len = spdk_min(iovs[i].iov_len, buf_len); 826 memcpy(buf, iovs[i].iov_base, len); 827 buf += len; 828 buf_len -= len; 829 } 830 } 831 832 static void 833 _copy_buf_to_iovs(struct iovec *iovs, int iovcnt, void *buf, size_t buf_len) 834 { 835 int i; 836 size_t len; 837 838 for (i = 0; i < iovcnt; i++) { 839 len = spdk_min(iovs[i].iov_len, buf_len); 840 memcpy(iovs[i].iov_base, buf, len); 841 buf += len; 842 buf_len -= len; 843 } 844 } 845 846 static void 847 bdev_io_get_buf_complete(struct spdk_bdev_io *bdev_io, bool status) 848 { 849 struct spdk_io_channel *ch = spdk_bdev_io_get_io_channel(bdev_io); 850 void *buf; 851 852 if (spdk_unlikely(bdev_io->internal.get_aux_buf_cb != NULL)) { 853 buf = bdev_io->internal.buf; 854 bdev_io->internal.buf = NULL; 855 bdev_io->internal.get_aux_buf_cb(ch, bdev_io, buf); 856 bdev_io->internal.get_aux_buf_cb = NULL; 857 } else { 858 assert(bdev_io->internal.get_buf_cb != NULL); 859 bdev_io->internal.get_buf_cb(ch, bdev_io, status); 860 bdev_io->internal.get_buf_cb = NULL; 861 } 862 } 863 864 static void 865 _bdev_io_pull_buffer_cpl(void *ctx, int rc) 866 { 867 struct spdk_bdev_io *bdev_io = ctx; 868 869 if (rc) { 870 SPDK_ERRLOG("Set bounce buffer failed with rc %d\n", rc); 871 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 872 } 873 bdev_io_get_buf_complete(bdev_io, !rc); 874 } 875 876 static void 877 _bdev_io_pull_bounce_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len) 878 { 879 int rc = 0; 880 881 /* save original md_buf */ 882 bdev_io->internal.orig_md_iov.iov_base = bdev_io->u.bdev.md_buf; 883 bdev_io->internal.orig_md_iov.iov_len = len; 884 bdev_io->internal.bounce_md_iov.iov_base = md_buf; 885 bdev_io->internal.bounce_md_iov.iov_len = len; 886 /* set bounce md_buf */ 887 bdev_io->u.bdev.md_buf = md_buf; 888 889 if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 890 if (bdev_io_use_memory_domain(bdev_io)) { 891 rc = spdk_memory_domain_pull_data(bdev_io->internal.ext_opts->memory_domain, 892 bdev_io->internal.ext_opts->memory_domain_ctx, 893 &bdev_io->internal.orig_md_iov, 1, 894 &bdev_io->internal.bounce_md_iov, 1, 895 bdev_io->internal.data_transfer_cpl, 896 bdev_io); 897 if (rc == 0) { 898 /* Continue to submit IO in completion callback */ 899 return; 900 } 901 SPDK_ERRLOG("Failed to pull data from memory domain %s, rc %d\n", 902 spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain), rc); 903 } else { 904 memcpy(md_buf, bdev_io->internal.orig_md_iov.iov_base, bdev_io->internal.orig_md_iov.iov_len); 905 } 906 } 907 908 assert(bdev_io->internal.data_transfer_cpl); 909 bdev_io->internal.data_transfer_cpl(bdev_io, rc); 910 } 911 912 static void 913 _bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io) 914 { 915 struct spdk_bdev *bdev = bdev_io->bdev; 916 uint64_t md_len; 917 void *buf; 918 919 if (spdk_bdev_is_md_separate(bdev)) { 920 buf = (char *)bdev_io->u.bdev.iovs[0].iov_base + bdev_io->u.bdev.iovs[0].iov_len; 921 md_len = bdev_io->u.bdev.num_blocks * bdev->md_len; 922 923 assert(((uintptr_t)buf & (spdk_bdev_get_buf_align(bdev) - 1)) == 0); 924 925 if (bdev_io->u.bdev.md_buf != NULL) { 926 _bdev_io_pull_bounce_md_buf(bdev_io, buf, md_len); 927 return; 928 } else { 929 spdk_bdev_io_set_md_buf(bdev_io, buf, md_len); 930 } 931 } 932 933 bdev_io_get_buf_complete(bdev_io, true); 934 } 935 936 static void 937 _bdev_io_pull_bounce_data_buf_done(void *ctx, int rc) 938 { 939 struct spdk_bdev_io *bdev_io = ctx; 940 941 if (rc) { 942 SPDK_ERRLOG("Failed to get data buffer\n"); 943 assert(bdev_io->internal.data_transfer_cpl); 944 bdev_io->internal.data_transfer_cpl(bdev_io, rc); 945 return; 946 } 947 948 _bdev_io_set_md_buf(bdev_io); 949 } 950 951 static void 952 _bdev_io_pull_bounce_data_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len, 953 bdev_copy_bounce_buffer_cpl cpl_cb) 954 { 955 int rc = 0; 956 957 bdev_io->internal.data_transfer_cpl = cpl_cb; 958 /* save original iovec */ 959 bdev_io->internal.orig_iovs = bdev_io->u.bdev.iovs; 960 bdev_io->internal.orig_iovcnt = bdev_io->u.bdev.iovcnt; 961 /* set bounce iov */ 962 bdev_io->u.bdev.iovs = &bdev_io->internal.bounce_iov; 963 bdev_io->u.bdev.iovcnt = 1; 964 /* set bounce buffer for this operation */ 965 bdev_io->u.bdev.iovs[0].iov_base = buf; 966 bdev_io->u.bdev.iovs[0].iov_len = len; 967 /* if this is write path, copy data from original buffer to bounce buffer */ 968 if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 969 if (bdev_io_use_memory_domain(bdev_io)) { 970 rc = spdk_memory_domain_pull_data(bdev_io->internal.ext_opts->memory_domain, 971 bdev_io->internal.ext_opts->memory_domain_ctx, 972 bdev_io->internal.orig_iovs, 973 (uint32_t) bdev_io->internal.orig_iovcnt, 974 bdev_io->u.bdev.iovs, 1, 975 _bdev_io_pull_bounce_data_buf_done, 976 bdev_io); 977 if (rc == 0) { 978 /* Continue to submit IO in completion callback */ 979 return; 980 } 981 SPDK_ERRLOG("Failed to pull data from memory domain %s\n", 982 spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain)); 983 } else { 984 _copy_iovs_to_buf(buf, len, bdev_io->internal.orig_iovs, bdev_io->internal.orig_iovcnt); 985 } 986 } 987 988 _bdev_io_pull_bounce_data_buf_done(bdev_io, rc); 989 } 990 991 static void 992 _bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t len) 993 { 994 struct spdk_bdev *bdev = bdev_io->bdev; 995 bool buf_allocated; 996 uint64_t alignment; 997 void *aligned_buf; 998 999 bdev_io->internal.buf = buf; 1000 1001 if (spdk_unlikely(bdev_io->internal.get_aux_buf_cb != NULL)) { 1002 bdev_io_get_buf_complete(bdev_io, true); 1003 return; 1004 } 1005 1006 alignment = spdk_bdev_get_buf_align(bdev); 1007 buf_allocated = _is_buf_allocated(bdev_io->u.bdev.iovs); 1008 aligned_buf = (void *)(((uintptr_t)buf + (alignment - 1)) & ~(alignment - 1)); 1009 1010 if (buf_allocated) { 1011 _bdev_io_pull_bounce_data_buf(bdev_io, aligned_buf, len, _bdev_io_pull_buffer_cpl); 1012 /* Continue in completion callback */ 1013 return; 1014 } else { 1015 spdk_bdev_io_set_buf(bdev_io, aligned_buf, len); 1016 } 1017 1018 _bdev_io_set_md_buf(bdev_io); 1019 } 1020 1021 static void 1022 _bdev_io_put_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t buf_len) 1023 { 1024 struct spdk_bdev *bdev = bdev_io->bdev; 1025 struct spdk_mempool *pool; 1026 struct spdk_bdev_io *tmp; 1027 bdev_io_stailq_t *stailq; 1028 struct spdk_bdev_mgmt_channel *ch; 1029 uint64_t md_len, alignment; 1030 1031 md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0; 1032 alignment = spdk_bdev_get_buf_align(bdev); 1033 ch = bdev_io->internal.ch->shared_resource->mgmt_ch; 1034 1035 if (buf_len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) + 1036 SPDK_BDEV_POOL_ALIGNMENT) { 1037 pool = g_bdev_mgr.buf_small_pool; 1038 stailq = &ch->need_buf_small; 1039 } else { 1040 pool = g_bdev_mgr.buf_large_pool; 1041 stailq = &ch->need_buf_large; 1042 } 1043 1044 if (STAILQ_EMPTY(stailq)) { 1045 spdk_mempool_put(pool, buf); 1046 } else { 1047 tmp = STAILQ_FIRST(stailq); 1048 STAILQ_REMOVE_HEAD(stailq, internal.buf_link); 1049 _bdev_io_set_buf(tmp, buf, tmp->internal.buf_len); 1050 } 1051 } 1052 1053 static void 1054 bdev_io_put_buf(struct spdk_bdev_io *bdev_io) 1055 { 1056 assert(bdev_io->internal.buf != NULL); 1057 _bdev_io_put_buf(bdev_io, bdev_io->internal.buf, bdev_io->internal.buf_len); 1058 bdev_io->internal.buf = NULL; 1059 } 1060 1061 void 1062 spdk_bdev_io_put_aux_buf(struct spdk_bdev_io *bdev_io, void *buf) 1063 { 1064 uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 1065 1066 assert(buf != NULL); 1067 _bdev_io_put_buf(bdev_io, buf, len); 1068 } 1069 1070 static void 1071 bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch) 1072 { 1073 struct spdk_bdev *bdev = bdev_ch->bdev; 1074 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 1075 struct spdk_bdev_io *bdev_io; 1076 1077 if (shared_resource->io_outstanding > shared_resource->nomem_threshold) { 1078 /* 1079 * Allow some more I/O to complete before retrying the nomem_io queue. 1080 * Some drivers (such as nvme) cannot immediately take a new I/O in 1081 * the context of a completion, because the resources for the I/O are 1082 * not released until control returns to the bdev poller. Also, we 1083 * may require several small I/O to complete before a larger I/O 1084 * (that requires splitting) can be submitted. 1085 */ 1086 return; 1087 } 1088 1089 while (!TAILQ_EMPTY(&shared_resource->nomem_io)) { 1090 bdev_io = TAILQ_FIRST(&shared_resource->nomem_io); 1091 TAILQ_REMOVE(&shared_resource->nomem_io, bdev_io, internal.link); 1092 bdev_io->internal.ch->io_outstanding++; 1093 shared_resource->io_outstanding++; 1094 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 1095 bdev_io->internal.error.nvme.cdw0 = 0; 1096 bdev_io->num_retries++; 1097 bdev->fn_table->submit_request(spdk_bdev_io_get_io_channel(bdev_io), bdev_io); 1098 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) { 1099 break; 1100 } 1101 } 1102 } 1103 1104 static inline void 1105 _bdev_io_decrement_outstanding(struct spdk_bdev_channel *bdev_ch, 1106 struct spdk_bdev_shared_resource *shared_resource) 1107 { 1108 assert(bdev_ch->io_outstanding > 0); 1109 assert(shared_resource->io_outstanding > 0); 1110 bdev_ch->io_outstanding--; 1111 shared_resource->io_outstanding--; 1112 } 1113 1114 static inline bool 1115 _bdev_io_handle_no_mem(struct spdk_bdev_io *bdev_io) 1116 { 1117 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 1118 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 1119 1120 if (spdk_unlikely(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM)) { 1121 TAILQ_INSERT_HEAD(&shared_resource->nomem_io, bdev_io, internal.link); 1122 /* 1123 * Wait for some of the outstanding I/O to complete before we 1124 * retry any of the nomem_io. Normally we will wait for 1125 * NOMEM_THRESHOLD_COUNT I/O to complete but for low queue 1126 * depth channels we will instead wait for half to complete. 1127 */ 1128 shared_resource->nomem_threshold = spdk_max((int64_t)shared_resource->io_outstanding / 2, 1129 (int64_t)shared_resource->io_outstanding - NOMEM_THRESHOLD_COUNT); 1130 return true; 1131 } 1132 1133 if (spdk_unlikely(!TAILQ_EMPTY(&shared_resource->nomem_io))) { 1134 bdev_ch_retry_io(bdev_ch); 1135 } 1136 1137 return false; 1138 } 1139 1140 static void 1141 _bdev_io_complete_push_bounce_done(void *ctx, int rc) 1142 { 1143 struct spdk_bdev_io *bdev_io = ctx; 1144 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 1145 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 1146 1147 if (rc) { 1148 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 1149 } 1150 /* We want to free the bounce buffer here since we know we're done with it (as opposed 1151 * to waiting for the conditional free of internal.buf in spdk_bdev_free_io()). 1152 */ 1153 bdev_io_put_buf(bdev_io); 1154 1155 /* Continue with IO completion flow */ 1156 _bdev_io_decrement_outstanding(bdev_ch, shared_resource); 1157 if (spdk_unlikely(_bdev_io_handle_no_mem(bdev_io))) { 1158 return; 1159 } 1160 1161 bdev_io_complete(bdev_io); 1162 } 1163 1164 static inline void 1165 _bdev_io_push_bounce_md_buffer(struct spdk_bdev_io *bdev_io) 1166 { 1167 int rc = 0; 1168 1169 /* do the same for metadata buffer */ 1170 if (spdk_unlikely(bdev_io->internal.orig_md_iov.iov_base != NULL)) { 1171 assert(spdk_bdev_is_md_separate(bdev_io->bdev)); 1172 1173 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ && 1174 bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 1175 if (bdev_io_use_memory_domain(bdev_io)) { 1176 /* If memory domain is used then we need to call async push function */ 1177 rc = spdk_memory_domain_push_data(bdev_io->internal.ext_opts->memory_domain, 1178 bdev_io->internal.ext_opts->memory_domain_ctx, 1179 &bdev_io->internal.orig_md_iov, 1180 (uint32_t)bdev_io->internal.orig_iovcnt, 1181 &bdev_io->internal.bounce_md_iov, 1, 1182 bdev_io->internal.data_transfer_cpl, 1183 bdev_io); 1184 if (rc == 0) { 1185 /* Continue IO completion in async callback */ 1186 return; 1187 } 1188 SPDK_ERRLOG("Failed to push md to memory domain %s\n", 1189 spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain)); 1190 } else { 1191 memcpy(bdev_io->internal.orig_md_iov.iov_base, bdev_io->u.bdev.md_buf, 1192 bdev_io->internal.orig_md_iov.iov_len); 1193 } 1194 } 1195 } 1196 1197 assert(bdev_io->internal.data_transfer_cpl); 1198 bdev_io->internal.data_transfer_cpl(bdev_io, rc); 1199 } 1200 1201 static void 1202 _bdev_io_push_bounce_data_buffer_done(void *ctx, int rc) 1203 { 1204 struct spdk_bdev_io *bdev_io = ctx; 1205 1206 assert(bdev_io->internal.data_transfer_cpl); 1207 1208 if (rc) { 1209 bdev_io->internal.data_transfer_cpl(bdev_io, rc); 1210 return; 1211 } 1212 1213 /* set original buffer for this io */ 1214 bdev_io->u.bdev.iovcnt = bdev_io->internal.orig_iovcnt; 1215 bdev_io->u.bdev.iovs = bdev_io->internal.orig_iovs; 1216 /* disable bouncing buffer for this io */ 1217 bdev_io->internal.orig_iovcnt = 0; 1218 bdev_io->internal.orig_iovs = NULL; 1219 1220 _bdev_io_push_bounce_md_buffer(bdev_io); 1221 } 1222 1223 static inline void 1224 _bdev_io_push_bounce_data_buffer(struct spdk_bdev_io *bdev_io, bdev_copy_bounce_buffer_cpl cpl_cb) 1225 { 1226 int rc = 0; 1227 1228 bdev_io->internal.data_transfer_cpl = cpl_cb; 1229 1230 /* if this is read path, copy data from bounce buffer to original buffer */ 1231 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ && 1232 bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 1233 if (bdev_io_use_memory_domain(bdev_io)) { 1234 /* If memory domain is used then we need to call async push function */ 1235 rc = spdk_memory_domain_push_data(bdev_io->internal.ext_opts->memory_domain, 1236 bdev_io->internal.ext_opts->memory_domain_ctx, 1237 bdev_io->internal.orig_iovs, 1238 (uint32_t)bdev_io->internal.orig_iovcnt, 1239 &bdev_io->internal.bounce_iov, 1, 1240 _bdev_io_push_bounce_data_buffer_done, 1241 bdev_io); 1242 if (rc == 0) { 1243 /* Continue IO completion in async callback */ 1244 return; 1245 } 1246 SPDK_ERRLOG("Failed to push data to memory domain %s\n", 1247 spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain)); 1248 } else { 1249 _copy_buf_to_iovs(bdev_io->internal.orig_iovs, 1250 bdev_io->internal.orig_iovcnt, 1251 bdev_io->internal.bounce_iov.iov_base, 1252 bdev_io->internal.bounce_iov.iov_len); 1253 } 1254 } 1255 1256 _bdev_io_push_bounce_data_buffer_done(bdev_io, rc); 1257 } 1258 1259 static void 1260 bdev_io_get_buf(struct spdk_bdev_io *bdev_io, uint64_t len) 1261 { 1262 struct spdk_bdev *bdev = bdev_io->bdev; 1263 struct spdk_mempool *pool; 1264 bdev_io_stailq_t *stailq; 1265 struct spdk_bdev_mgmt_channel *mgmt_ch; 1266 uint64_t alignment, md_len; 1267 void *buf; 1268 1269 alignment = spdk_bdev_get_buf_align(bdev); 1270 md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0; 1271 1272 if (len + alignment + md_len > SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) + 1273 SPDK_BDEV_POOL_ALIGNMENT) { 1274 SPDK_ERRLOG("Length + alignment %" PRIu64 " is larger than allowed\n", 1275 len + alignment); 1276 bdev_io_get_buf_complete(bdev_io, false); 1277 return; 1278 } 1279 1280 mgmt_ch = bdev_io->internal.ch->shared_resource->mgmt_ch; 1281 1282 bdev_io->internal.buf_len = len; 1283 1284 if (len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) + 1285 SPDK_BDEV_POOL_ALIGNMENT) { 1286 pool = g_bdev_mgr.buf_small_pool; 1287 stailq = &mgmt_ch->need_buf_small; 1288 } else { 1289 pool = g_bdev_mgr.buf_large_pool; 1290 stailq = &mgmt_ch->need_buf_large; 1291 } 1292 1293 buf = spdk_mempool_get(pool); 1294 if (!buf) { 1295 STAILQ_INSERT_TAIL(stailq, bdev_io, internal.buf_link); 1296 } else { 1297 _bdev_io_set_buf(bdev_io, buf, len); 1298 } 1299 } 1300 1301 void 1302 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len) 1303 { 1304 struct spdk_bdev *bdev = bdev_io->bdev; 1305 uint64_t alignment; 1306 1307 assert(cb != NULL); 1308 bdev_io->internal.get_buf_cb = cb; 1309 1310 alignment = spdk_bdev_get_buf_align(bdev); 1311 1312 if (_is_buf_allocated(bdev_io->u.bdev.iovs) && 1313 _are_iovs_aligned(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, alignment)) { 1314 /* Buffer already present and aligned */ 1315 cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true); 1316 return; 1317 } 1318 1319 bdev_io_get_buf(bdev_io, len); 1320 } 1321 1322 static void 1323 _bdev_memory_domain_get_io_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 1324 bool success) 1325 { 1326 if (!success) { 1327 SPDK_ERRLOG("Failed to get data buffer, completing IO\n"); 1328 bdev_io_complete(bdev_io); 1329 } else { 1330 bdev_io_submit(bdev_io); 1331 } 1332 } 1333 1334 static void 1335 _bdev_memory_domain_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, 1336 uint64_t len) 1337 { 1338 assert(cb != NULL); 1339 bdev_io->internal.get_buf_cb = cb; 1340 1341 bdev_io_get_buf(bdev_io, len); 1342 } 1343 1344 void 1345 spdk_bdev_io_get_aux_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_aux_buf_cb cb) 1346 { 1347 uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 1348 1349 assert(cb != NULL); 1350 assert(bdev_io->internal.get_aux_buf_cb == NULL); 1351 bdev_io->internal.get_aux_buf_cb = cb; 1352 bdev_io_get_buf(bdev_io, len); 1353 } 1354 1355 static int 1356 bdev_module_get_max_ctx_size(void) 1357 { 1358 struct spdk_bdev_module *bdev_module; 1359 int max_bdev_module_size = 0; 1360 1361 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 1362 if (bdev_module->get_ctx_size && bdev_module->get_ctx_size() > max_bdev_module_size) { 1363 max_bdev_module_size = bdev_module->get_ctx_size(); 1364 } 1365 } 1366 1367 return max_bdev_module_size; 1368 } 1369 1370 static void 1371 bdev_qos_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 1372 { 1373 int i; 1374 struct spdk_bdev_qos *qos = bdev->internal.qos; 1375 uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES]; 1376 1377 if (!qos) { 1378 return; 1379 } 1380 1381 spdk_bdev_get_qos_rate_limits(bdev, limits); 1382 1383 spdk_json_write_object_begin(w); 1384 spdk_json_write_named_string(w, "method", "bdev_set_qos_limit"); 1385 1386 spdk_json_write_named_object_begin(w, "params"); 1387 spdk_json_write_named_string(w, "name", bdev->name); 1388 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1389 if (limits[i] > 0) { 1390 spdk_json_write_named_uint64(w, qos_rpc_type[i], limits[i]); 1391 } 1392 } 1393 spdk_json_write_object_end(w); 1394 1395 spdk_json_write_object_end(w); 1396 } 1397 1398 void 1399 spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w) 1400 { 1401 struct spdk_bdev_module *bdev_module; 1402 struct spdk_bdev *bdev; 1403 1404 assert(w != NULL); 1405 1406 spdk_json_write_array_begin(w); 1407 1408 spdk_json_write_object_begin(w); 1409 spdk_json_write_named_string(w, "method", "bdev_set_options"); 1410 spdk_json_write_named_object_begin(w, "params"); 1411 spdk_json_write_named_uint32(w, "bdev_io_pool_size", g_bdev_opts.bdev_io_pool_size); 1412 spdk_json_write_named_uint32(w, "bdev_io_cache_size", g_bdev_opts.bdev_io_cache_size); 1413 spdk_json_write_named_bool(w, "bdev_auto_examine", g_bdev_opts.bdev_auto_examine); 1414 spdk_json_write_object_end(w); 1415 spdk_json_write_object_end(w); 1416 1417 bdev_examine_allowlist_config_json(w); 1418 1419 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 1420 if (bdev_module->config_json) { 1421 bdev_module->config_json(w); 1422 } 1423 } 1424 1425 pthread_mutex_lock(&g_bdev_mgr.mutex); 1426 1427 TAILQ_FOREACH(bdev, &g_bdev_mgr.bdevs, internal.link) { 1428 if (bdev->fn_table->write_config_json) { 1429 bdev->fn_table->write_config_json(bdev, w); 1430 } 1431 1432 bdev_qos_config_json(bdev, w); 1433 } 1434 1435 pthread_mutex_unlock(&g_bdev_mgr.mutex); 1436 1437 /* This has to be last RPC in array to make sure all bdevs finished examine */ 1438 spdk_json_write_object_begin(w); 1439 spdk_json_write_named_string(w, "method", "bdev_wait_for_examine"); 1440 spdk_json_write_object_end(w); 1441 1442 spdk_json_write_array_end(w); 1443 } 1444 1445 static int 1446 bdev_mgmt_channel_create(void *io_device, void *ctx_buf) 1447 { 1448 struct spdk_bdev_mgmt_channel *ch = ctx_buf; 1449 struct spdk_bdev_io *bdev_io; 1450 uint32_t i; 1451 1452 STAILQ_INIT(&ch->need_buf_small); 1453 STAILQ_INIT(&ch->need_buf_large); 1454 1455 STAILQ_INIT(&ch->per_thread_cache); 1456 ch->bdev_io_cache_size = g_bdev_opts.bdev_io_cache_size; 1457 1458 /* Pre-populate bdev_io cache to ensure this thread cannot be starved. */ 1459 ch->per_thread_cache_count = 0; 1460 for (i = 0; i < ch->bdev_io_cache_size; i++) { 1461 bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool); 1462 assert(bdev_io != NULL); 1463 ch->per_thread_cache_count++; 1464 STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link); 1465 } 1466 1467 TAILQ_INIT(&ch->shared_resources); 1468 TAILQ_INIT(&ch->io_wait_queue); 1469 1470 return 0; 1471 } 1472 1473 static void 1474 bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf) 1475 { 1476 struct spdk_bdev_mgmt_channel *ch = ctx_buf; 1477 struct spdk_bdev_io *bdev_io; 1478 1479 if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) { 1480 SPDK_ERRLOG("Pending I/O list wasn't empty on mgmt channel free\n"); 1481 } 1482 1483 if (!TAILQ_EMPTY(&ch->shared_resources)) { 1484 SPDK_ERRLOG("Module channel list wasn't empty on mgmt channel free\n"); 1485 } 1486 1487 while (!STAILQ_EMPTY(&ch->per_thread_cache)) { 1488 bdev_io = STAILQ_FIRST(&ch->per_thread_cache); 1489 STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link); 1490 ch->per_thread_cache_count--; 1491 spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io); 1492 } 1493 1494 assert(ch->per_thread_cache_count == 0); 1495 } 1496 1497 static void 1498 bdev_init_complete(int rc) 1499 { 1500 spdk_bdev_init_cb cb_fn = g_init_cb_fn; 1501 void *cb_arg = g_init_cb_arg; 1502 struct spdk_bdev_module *m; 1503 1504 g_bdev_mgr.init_complete = true; 1505 g_init_cb_fn = NULL; 1506 g_init_cb_arg = NULL; 1507 1508 /* 1509 * For modules that need to know when subsystem init is complete, 1510 * inform them now. 1511 */ 1512 if (rc == 0) { 1513 TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) { 1514 if (m->init_complete) { 1515 m->init_complete(); 1516 } 1517 } 1518 } 1519 1520 cb_fn(cb_arg, rc); 1521 } 1522 1523 static bool 1524 bdev_module_all_actions_completed(void) 1525 { 1526 struct spdk_bdev_module *m; 1527 1528 TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) { 1529 if (m->internal.action_in_progress > 0) { 1530 return false; 1531 } 1532 } 1533 return true; 1534 } 1535 1536 static void 1537 bdev_module_action_complete(void) 1538 { 1539 /* 1540 * Don't finish bdev subsystem initialization if 1541 * module pre-initialization is still in progress, or 1542 * the subsystem been already initialized. 1543 */ 1544 if (!g_bdev_mgr.module_init_complete || g_bdev_mgr.init_complete) { 1545 return; 1546 } 1547 1548 /* 1549 * Check all bdev modules for inits/examinations in progress. If any 1550 * exist, return immediately since we cannot finish bdev subsystem 1551 * initialization until all are completed. 1552 */ 1553 if (!bdev_module_all_actions_completed()) { 1554 return; 1555 } 1556 1557 /* 1558 * Modules already finished initialization - now that all 1559 * the bdev modules have finished their asynchronous I/O 1560 * processing, the entire bdev layer can be marked as complete. 1561 */ 1562 bdev_init_complete(0); 1563 } 1564 1565 static void 1566 bdev_module_action_done(struct spdk_bdev_module *module) 1567 { 1568 assert(module->internal.action_in_progress > 0); 1569 module->internal.action_in_progress--; 1570 bdev_module_action_complete(); 1571 } 1572 1573 void 1574 spdk_bdev_module_init_done(struct spdk_bdev_module *module) 1575 { 1576 bdev_module_action_done(module); 1577 } 1578 1579 void 1580 spdk_bdev_module_examine_done(struct spdk_bdev_module *module) 1581 { 1582 bdev_module_action_done(module); 1583 } 1584 1585 /** The last initialized bdev module */ 1586 static struct spdk_bdev_module *g_resume_bdev_module = NULL; 1587 1588 static void 1589 bdev_init_failed(void *cb_arg) 1590 { 1591 struct spdk_bdev_module *module = cb_arg; 1592 1593 module->internal.action_in_progress--; 1594 bdev_init_complete(-1); 1595 } 1596 1597 static int 1598 bdev_modules_init(void) 1599 { 1600 struct spdk_bdev_module *module; 1601 int rc = 0; 1602 1603 TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) { 1604 g_resume_bdev_module = module; 1605 if (module->async_init) { 1606 module->internal.action_in_progress = 1; 1607 } 1608 rc = module->module_init(); 1609 if (rc != 0) { 1610 /* Bump action_in_progress to prevent other modules from completion of modules_init 1611 * Send message to defer application shutdown until resources are cleaned up */ 1612 module->internal.action_in_progress = 1; 1613 spdk_thread_send_msg(spdk_get_thread(), bdev_init_failed, module); 1614 return rc; 1615 } 1616 } 1617 1618 g_resume_bdev_module = NULL; 1619 return 0; 1620 } 1621 1622 void 1623 spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg) 1624 { 1625 int cache_size; 1626 int rc = 0; 1627 char mempool_name[32]; 1628 1629 assert(cb_fn != NULL); 1630 1631 g_init_cb_fn = cb_fn; 1632 g_init_cb_arg = cb_arg; 1633 1634 spdk_notify_type_register("bdev_register"); 1635 spdk_notify_type_register("bdev_unregister"); 1636 1637 snprintf(mempool_name, sizeof(mempool_name), "bdev_io_%d", getpid()); 1638 1639 g_bdev_mgr.bdev_io_pool = spdk_mempool_create(mempool_name, 1640 g_bdev_opts.bdev_io_pool_size, 1641 sizeof(struct spdk_bdev_io) + 1642 bdev_module_get_max_ctx_size(), 1643 0, 1644 SPDK_ENV_SOCKET_ID_ANY); 1645 1646 if (g_bdev_mgr.bdev_io_pool == NULL) { 1647 SPDK_ERRLOG("could not allocate spdk_bdev_io pool\n"); 1648 bdev_init_complete(-1); 1649 return; 1650 } 1651 1652 /** 1653 * Ensure no more than half of the total buffers end up local caches, by 1654 * using spdk_env_get_core_count() to determine how many local caches we need 1655 * to account for. 1656 */ 1657 cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_env_get_core_count()); 1658 snprintf(mempool_name, sizeof(mempool_name), "buf_small_pool_%d", getpid()); 1659 1660 g_bdev_mgr.buf_small_pool = spdk_mempool_create(mempool_name, 1661 g_bdev_opts.small_buf_pool_size, 1662 SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) + 1663 SPDK_BDEV_POOL_ALIGNMENT, 1664 cache_size, 1665 SPDK_ENV_SOCKET_ID_ANY); 1666 if (!g_bdev_mgr.buf_small_pool) { 1667 SPDK_ERRLOG("create rbuf small pool failed\n"); 1668 bdev_init_complete(-1); 1669 return; 1670 } 1671 1672 cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_env_get_core_count()); 1673 snprintf(mempool_name, sizeof(mempool_name), "buf_large_pool_%d", getpid()); 1674 1675 g_bdev_mgr.buf_large_pool = spdk_mempool_create(mempool_name, 1676 g_bdev_opts.large_buf_pool_size, 1677 SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) + 1678 SPDK_BDEV_POOL_ALIGNMENT, 1679 cache_size, 1680 SPDK_ENV_SOCKET_ID_ANY); 1681 if (!g_bdev_mgr.buf_large_pool) { 1682 SPDK_ERRLOG("create rbuf large pool failed\n"); 1683 bdev_init_complete(-1); 1684 return; 1685 } 1686 1687 g_bdev_mgr.zero_buffer = spdk_zmalloc(ZERO_BUFFER_SIZE, ZERO_BUFFER_SIZE, 1688 NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 1689 if (!g_bdev_mgr.zero_buffer) { 1690 SPDK_ERRLOG("create bdev zero buffer failed\n"); 1691 bdev_init_complete(-1); 1692 return; 1693 } 1694 1695 #ifdef SPDK_CONFIG_VTUNE 1696 g_bdev_mgr.domain = __itt_domain_create("spdk_bdev"); 1697 #endif 1698 1699 spdk_io_device_register(&g_bdev_mgr, bdev_mgmt_channel_create, 1700 bdev_mgmt_channel_destroy, 1701 sizeof(struct spdk_bdev_mgmt_channel), 1702 "bdev_mgr"); 1703 1704 rc = bdev_modules_init(); 1705 g_bdev_mgr.module_init_complete = true; 1706 if (rc != 0) { 1707 SPDK_ERRLOG("bdev modules init failed\n"); 1708 return; 1709 } 1710 1711 bdev_module_action_complete(); 1712 } 1713 1714 static void 1715 bdev_mgr_unregister_cb(void *io_device) 1716 { 1717 spdk_bdev_fini_cb cb_fn = g_fini_cb_fn; 1718 1719 if (g_bdev_mgr.bdev_io_pool) { 1720 if (spdk_mempool_count(g_bdev_mgr.bdev_io_pool) != g_bdev_opts.bdev_io_pool_size) { 1721 SPDK_ERRLOG("bdev IO pool count is %zu but should be %u\n", 1722 spdk_mempool_count(g_bdev_mgr.bdev_io_pool), 1723 g_bdev_opts.bdev_io_pool_size); 1724 } 1725 1726 spdk_mempool_free(g_bdev_mgr.bdev_io_pool); 1727 } 1728 1729 if (g_bdev_mgr.buf_small_pool) { 1730 if (spdk_mempool_count(g_bdev_mgr.buf_small_pool) != g_bdev_opts.small_buf_pool_size) { 1731 SPDK_ERRLOG("Small buffer pool count is %zu but should be %u\n", 1732 spdk_mempool_count(g_bdev_mgr.buf_small_pool), 1733 g_bdev_opts.small_buf_pool_size); 1734 assert(false); 1735 } 1736 1737 spdk_mempool_free(g_bdev_mgr.buf_small_pool); 1738 } 1739 1740 if (g_bdev_mgr.buf_large_pool) { 1741 if (spdk_mempool_count(g_bdev_mgr.buf_large_pool) != g_bdev_opts.large_buf_pool_size) { 1742 SPDK_ERRLOG("Large buffer pool count is %zu but should be %u\n", 1743 spdk_mempool_count(g_bdev_mgr.buf_large_pool), 1744 g_bdev_opts.large_buf_pool_size); 1745 assert(false); 1746 } 1747 1748 spdk_mempool_free(g_bdev_mgr.buf_large_pool); 1749 } 1750 1751 spdk_free(g_bdev_mgr.zero_buffer); 1752 1753 bdev_examine_allowlist_free(); 1754 1755 cb_fn(g_fini_cb_arg); 1756 g_fini_cb_fn = NULL; 1757 g_fini_cb_arg = NULL; 1758 g_bdev_mgr.init_complete = false; 1759 g_bdev_mgr.module_init_complete = false; 1760 } 1761 1762 static void 1763 bdev_module_fini_iter(void *arg) 1764 { 1765 struct spdk_bdev_module *bdev_module; 1766 1767 /* FIXME: Handling initialization failures is broken now, 1768 * so we won't even try cleaning up after successfully 1769 * initialized modules. if module_init_complete is false, 1770 * just call spdk_bdev_mgr_unregister_cb 1771 */ 1772 if (!g_bdev_mgr.module_init_complete) { 1773 bdev_mgr_unregister_cb(NULL); 1774 return; 1775 } 1776 1777 /* Start iterating from the last touched module */ 1778 if (!g_resume_bdev_module) { 1779 bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list); 1780 } else { 1781 bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list, 1782 internal.tailq); 1783 } 1784 1785 while (bdev_module) { 1786 if (bdev_module->async_fini) { 1787 /* Save our place so we can resume later. We must 1788 * save the variable here, before calling module_fini() 1789 * below, because in some cases the module may immediately 1790 * call spdk_bdev_module_fini_done() and re-enter 1791 * this function to continue iterating. */ 1792 g_resume_bdev_module = bdev_module; 1793 } 1794 1795 if (bdev_module->module_fini) { 1796 bdev_module->module_fini(); 1797 } 1798 1799 if (bdev_module->async_fini) { 1800 return; 1801 } 1802 1803 bdev_module = TAILQ_PREV(bdev_module, bdev_module_list, 1804 internal.tailq); 1805 } 1806 1807 g_resume_bdev_module = NULL; 1808 spdk_io_device_unregister(&g_bdev_mgr, bdev_mgr_unregister_cb); 1809 } 1810 1811 void 1812 spdk_bdev_module_fini_done(void) 1813 { 1814 if (spdk_get_thread() != g_fini_thread) { 1815 spdk_thread_send_msg(g_fini_thread, bdev_module_fini_iter, NULL); 1816 } else { 1817 bdev_module_fini_iter(NULL); 1818 } 1819 } 1820 1821 static void 1822 bdev_finish_unregister_bdevs_iter(void *cb_arg, int bdeverrno) 1823 { 1824 struct spdk_bdev *bdev = cb_arg; 1825 1826 if (bdeverrno && bdev) { 1827 SPDK_WARNLOG("Unable to unregister bdev '%s' during spdk_bdev_finish()\n", 1828 bdev->name); 1829 1830 /* 1831 * Since the call to spdk_bdev_unregister() failed, we have no way to free this 1832 * bdev; try to continue by manually removing this bdev from the list and continue 1833 * with the next bdev in the list. 1834 */ 1835 TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link); 1836 } 1837 1838 if (TAILQ_EMPTY(&g_bdev_mgr.bdevs)) { 1839 SPDK_DEBUGLOG(bdev, "Done unregistering bdevs\n"); 1840 /* 1841 * Bdev module finish need to be deferred as we might be in the middle of some context 1842 * (like bdev part free) that will use this bdev (or private bdev driver ctx data) 1843 * after returning. 1844 */ 1845 spdk_thread_send_msg(spdk_get_thread(), bdev_module_fini_iter, NULL); 1846 return; 1847 } 1848 1849 /* 1850 * Unregister last unclaimed bdev in the list, to ensure that bdev subsystem 1851 * shutdown proceeds top-down. The goal is to give virtual bdevs an opportunity 1852 * to detect clean shutdown as opposed to run-time hot removal of the underlying 1853 * base bdevs. 1854 * 1855 * Also, walk the list in the reverse order. 1856 */ 1857 for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list); 1858 bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) { 1859 if (bdev->internal.claim_module != NULL) { 1860 SPDK_DEBUGLOG(bdev, "Skipping claimed bdev '%s'(<-'%s').\n", 1861 bdev->name, bdev->internal.claim_module->name); 1862 continue; 1863 } 1864 1865 SPDK_DEBUGLOG(bdev, "Unregistering bdev '%s'\n", bdev->name); 1866 spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev); 1867 return; 1868 } 1869 1870 /* 1871 * If any bdev fails to unclaim underlying bdev properly, we may face the 1872 * case of bdev list consisting of claimed bdevs only (if claims are managed 1873 * correctly, this would mean there's a loop in the claims graph which is 1874 * clearly impossible). Warn and unregister last bdev on the list then. 1875 */ 1876 for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list); 1877 bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) { 1878 SPDK_WARNLOG("Unregistering claimed bdev '%s'!\n", bdev->name); 1879 spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev); 1880 return; 1881 } 1882 } 1883 1884 static void 1885 bdev_module_fini_start_iter(void *arg) 1886 { 1887 struct spdk_bdev_module *bdev_module; 1888 1889 if (!g_resume_bdev_module) { 1890 bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list); 1891 } else { 1892 bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list, internal.tailq); 1893 } 1894 1895 while (bdev_module) { 1896 if (bdev_module->async_fini_start) { 1897 /* Save our place so we can resume later. We must 1898 * save the variable here, before calling fini_start() 1899 * below, because in some cases the module may immediately 1900 * call spdk_bdev_module_fini_start_done() and re-enter 1901 * this function to continue iterating. */ 1902 g_resume_bdev_module = bdev_module; 1903 } 1904 1905 if (bdev_module->fini_start) { 1906 bdev_module->fini_start(); 1907 } 1908 1909 if (bdev_module->async_fini_start) { 1910 return; 1911 } 1912 1913 bdev_module = TAILQ_PREV(bdev_module, bdev_module_list, internal.tailq); 1914 } 1915 1916 g_resume_bdev_module = NULL; 1917 1918 bdev_finish_unregister_bdevs_iter(NULL, 0); 1919 } 1920 1921 void 1922 spdk_bdev_module_fini_start_done(void) 1923 { 1924 if (spdk_get_thread() != g_fini_thread) { 1925 spdk_thread_send_msg(g_fini_thread, bdev_module_fini_start_iter, NULL); 1926 } else { 1927 bdev_module_fini_start_iter(NULL); 1928 } 1929 } 1930 1931 void 1932 spdk_bdev_finish(spdk_bdev_fini_cb cb_fn, void *cb_arg) 1933 { 1934 assert(cb_fn != NULL); 1935 1936 g_fini_thread = spdk_get_thread(); 1937 1938 g_fini_cb_fn = cb_fn; 1939 g_fini_cb_arg = cb_arg; 1940 1941 bdev_module_fini_start_iter(NULL); 1942 } 1943 1944 struct spdk_bdev_io * 1945 bdev_channel_get_io(struct spdk_bdev_channel *channel) 1946 { 1947 struct spdk_bdev_mgmt_channel *ch = channel->shared_resource->mgmt_ch; 1948 struct spdk_bdev_io *bdev_io; 1949 1950 if (ch->per_thread_cache_count > 0) { 1951 bdev_io = STAILQ_FIRST(&ch->per_thread_cache); 1952 STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link); 1953 ch->per_thread_cache_count--; 1954 } else if (spdk_unlikely(!TAILQ_EMPTY(&ch->io_wait_queue))) { 1955 /* 1956 * Don't try to look for bdev_ios in the global pool if there are 1957 * waiters on bdev_ios - we don't want this caller to jump the line. 1958 */ 1959 bdev_io = NULL; 1960 } else { 1961 bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool); 1962 } 1963 1964 return bdev_io; 1965 } 1966 1967 void 1968 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io) 1969 { 1970 struct spdk_bdev_mgmt_channel *ch; 1971 1972 assert(bdev_io != NULL); 1973 assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING); 1974 1975 ch = bdev_io->internal.ch->shared_resource->mgmt_ch; 1976 1977 if (bdev_io->internal.buf != NULL) { 1978 bdev_io_put_buf(bdev_io); 1979 } 1980 1981 if (spdk_unlikely(bdev_io->internal.ext_opts == &bdev_io->internal.ext_opts_copy)) { 1982 memset(&bdev_io->internal.ext_opts_copy, 0, sizeof(bdev_io->internal.ext_opts_copy)); 1983 } 1984 1985 if (ch->per_thread_cache_count < ch->bdev_io_cache_size) { 1986 ch->per_thread_cache_count++; 1987 STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link); 1988 while (ch->per_thread_cache_count > 0 && !TAILQ_EMPTY(&ch->io_wait_queue)) { 1989 struct spdk_bdev_io_wait_entry *entry; 1990 1991 entry = TAILQ_FIRST(&ch->io_wait_queue); 1992 TAILQ_REMOVE(&ch->io_wait_queue, entry, link); 1993 entry->cb_fn(entry->cb_arg); 1994 } 1995 } else { 1996 /* We should never have a full cache with entries on the io wait queue. */ 1997 assert(TAILQ_EMPTY(&ch->io_wait_queue)); 1998 spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io); 1999 } 2000 } 2001 2002 static bool 2003 bdev_qos_is_iops_rate_limit(enum spdk_bdev_qos_rate_limit_type limit) 2004 { 2005 assert(limit != SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES); 2006 2007 switch (limit) { 2008 case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT: 2009 return true; 2010 case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT: 2011 case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT: 2012 case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT: 2013 return false; 2014 case SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES: 2015 default: 2016 return false; 2017 } 2018 } 2019 2020 static bool 2021 bdev_qos_io_to_limit(struct spdk_bdev_io *bdev_io) 2022 { 2023 switch (bdev_io->type) { 2024 case SPDK_BDEV_IO_TYPE_NVME_IO: 2025 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 2026 case SPDK_BDEV_IO_TYPE_READ: 2027 case SPDK_BDEV_IO_TYPE_WRITE: 2028 return true; 2029 case SPDK_BDEV_IO_TYPE_ZCOPY: 2030 if (bdev_io->u.bdev.zcopy.start) { 2031 return true; 2032 } else { 2033 return false; 2034 } 2035 default: 2036 return false; 2037 } 2038 } 2039 2040 static bool 2041 bdev_is_read_io(struct spdk_bdev_io *bdev_io) 2042 { 2043 switch (bdev_io->type) { 2044 case SPDK_BDEV_IO_TYPE_NVME_IO: 2045 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 2046 /* Bit 1 (0x2) set for read operation */ 2047 if (bdev_io->u.nvme_passthru.cmd.opc & SPDK_NVME_OPC_READ) { 2048 return true; 2049 } else { 2050 return false; 2051 } 2052 case SPDK_BDEV_IO_TYPE_READ: 2053 return true; 2054 case SPDK_BDEV_IO_TYPE_ZCOPY: 2055 /* Populate to read from disk */ 2056 if (bdev_io->u.bdev.zcopy.populate) { 2057 return true; 2058 } else { 2059 return false; 2060 } 2061 default: 2062 return false; 2063 } 2064 } 2065 2066 static uint64_t 2067 bdev_get_io_size_in_byte(struct spdk_bdev_io *bdev_io) 2068 { 2069 struct spdk_bdev *bdev = bdev_io->bdev; 2070 2071 switch (bdev_io->type) { 2072 case SPDK_BDEV_IO_TYPE_NVME_IO: 2073 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 2074 return bdev_io->u.nvme_passthru.nbytes; 2075 case SPDK_BDEV_IO_TYPE_READ: 2076 case SPDK_BDEV_IO_TYPE_WRITE: 2077 return bdev_io->u.bdev.num_blocks * bdev->blocklen; 2078 case SPDK_BDEV_IO_TYPE_ZCOPY: 2079 /* Track the data in the start phase only */ 2080 if (bdev_io->u.bdev.zcopy.start) { 2081 return bdev_io->u.bdev.num_blocks * bdev->blocklen; 2082 } else { 2083 return 0; 2084 } 2085 default: 2086 return 0; 2087 } 2088 } 2089 2090 static bool 2091 bdev_qos_rw_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2092 { 2093 if (limit->max_per_timeslice > 0 && limit->remaining_this_timeslice <= 0) { 2094 return true; 2095 } else { 2096 return false; 2097 } 2098 } 2099 2100 static bool 2101 bdev_qos_r_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2102 { 2103 if (bdev_is_read_io(io) == false) { 2104 return false; 2105 } 2106 2107 return bdev_qos_rw_queue_io(limit, io); 2108 } 2109 2110 static bool 2111 bdev_qos_w_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2112 { 2113 if (bdev_is_read_io(io) == true) { 2114 return false; 2115 } 2116 2117 return bdev_qos_rw_queue_io(limit, io); 2118 } 2119 2120 static void 2121 bdev_qos_rw_iops_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2122 { 2123 limit->remaining_this_timeslice--; 2124 } 2125 2126 static void 2127 bdev_qos_rw_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2128 { 2129 limit->remaining_this_timeslice -= bdev_get_io_size_in_byte(io); 2130 } 2131 2132 static void 2133 bdev_qos_r_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2134 { 2135 if (bdev_is_read_io(io) == false) { 2136 return; 2137 } 2138 2139 return bdev_qos_rw_bps_update_quota(limit, io); 2140 } 2141 2142 static void 2143 bdev_qos_w_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 2144 { 2145 if (bdev_is_read_io(io) == true) { 2146 return; 2147 } 2148 2149 return bdev_qos_rw_bps_update_quota(limit, io); 2150 } 2151 2152 static void 2153 bdev_qos_set_ops(struct spdk_bdev_qos *qos) 2154 { 2155 int i; 2156 2157 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2158 if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 2159 qos->rate_limits[i].queue_io = NULL; 2160 qos->rate_limits[i].update_quota = NULL; 2161 continue; 2162 } 2163 2164 switch (i) { 2165 case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT: 2166 qos->rate_limits[i].queue_io = bdev_qos_rw_queue_io; 2167 qos->rate_limits[i].update_quota = bdev_qos_rw_iops_update_quota; 2168 break; 2169 case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT: 2170 qos->rate_limits[i].queue_io = bdev_qos_rw_queue_io; 2171 qos->rate_limits[i].update_quota = bdev_qos_rw_bps_update_quota; 2172 break; 2173 case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT: 2174 qos->rate_limits[i].queue_io = bdev_qos_r_queue_io; 2175 qos->rate_limits[i].update_quota = bdev_qos_r_bps_update_quota; 2176 break; 2177 case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT: 2178 qos->rate_limits[i].queue_io = bdev_qos_w_queue_io; 2179 qos->rate_limits[i].update_quota = bdev_qos_w_bps_update_quota; 2180 break; 2181 default: 2182 break; 2183 } 2184 } 2185 } 2186 2187 static void 2188 _bdev_io_complete_in_submit(struct spdk_bdev_channel *bdev_ch, 2189 struct spdk_bdev_io *bdev_io, 2190 enum spdk_bdev_io_status status) 2191 { 2192 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 2193 2194 bdev_io->internal.in_submit_request = true; 2195 bdev_ch->io_outstanding++; 2196 shared_resource->io_outstanding++; 2197 spdk_bdev_io_complete(bdev_io, status); 2198 bdev_io->internal.in_submit_request = false; 2199 } 2200 2201 static inline void 2202 bdev_io_do_submit(struct spdk_bdev_channel *bdev_ch, struct spdk_bdev_io *bdev_io) 2203 { 2204 struct spdk_bdev *bdev = bdev_io->bdev; 2205 struct spdk_io_channel *ch = bdev_ch->channel; 2206 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 2207 2208 if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) { 2209 struct spdk_bdev_mgmt_channel *mgmt_channel = shared_resource->mgmt_ch; 2210 struct spdk_bdev_io *bio_to_abort = bdev_io->u.abort.bio_to_abort; 2211 2212 if (bdev_abort_queued_io(&shared_resource->nomem_io, bio_to_abort) || 2213 bdev_abort_buf_io(&mgmt_channel->need_buf_small, bio_to_abort) || 2214 bdev_abort_buf_io(&mgmt_channel->need_buf_large, bio_to_abort)) { 2215 _bdev_io_complete_in_submit(bdev_ch, bdev_io, 2216 SPDK_BDEV_IO_STATUS_SUCCESS); 2217 return; 2218 } 2219 } 2220 2221 if (spdk_likely(TAILQ_EMPTY(&shared_resource->nomem_io))) { 2222 bdev_ch->io_outstanding++; 2223 shared_resource->io_outstanding++; 2224 bdev_io->internal.in_submit_request = true; 2225 bdev->fn_table->submit_request(ch, bdev_io); 2226 bdev_io->internal.in_submit_request = false; 2227 } else { 2228 TAILQ_INSERT_TAIL(&shared_resource->nomem_io, bdev_io, internal.link); 2229 } 2230 } 2231 2232 static int 2233 bdev_qos_io_submit(struct spdk_bdev_channel *ch, struct spdk_bdev_qos *qos) 2234 { 2235 struct spdk_bdev_io *bdev_io = NULL, *tmp = NULL; 2236 int i, submitted_ios = 0; 2237 2238 TAILQ_FOREACH_SAFE(bdev_io, &qos->queued, internal.link, tmp) { 2239 if (bdev_qos_io_to_limit(bdev_io) == true) { 2240 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2241 if (!qos->rate_limits[i].queue_io) { 2242 continue; 2243 } 2244 2245 if (qos->rate_limits[i].queue_io(&qos->rate_limits[i], 2246 bdev_io) == true) { 2247 return submitted_ios; 2248 } 2249 } 2250 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2251 if (!qos->rate_limits[i].update_quota) { 2252 continue; 2253 } 2254 2255 qos->rate_limits[i].update_quota(&qos->rate_limits[i], bdev_io); 2256 } 2257 } 2258 2259 TAILQ_REMOVE(&qos->queued, bdev_io, internal.link); 2260 bdev_io_do_submit(ch, bdev_io); 2261 submitted_ios++; 2262 } 2263 2264 return submitted_ios; 2265 } 2266 2267 static void 2268 bdev_queue_io_wait_with_cb(struct spdk_bdev_io *bdev_io, spdk_bdev_io_wait_cb cb_fn) 2269 { 2270 int rc; 2271 2272 bdev_io->internal.waitq_entry.bdev = bdev_io->bdev; 2273 bdev_io->internal.waitq_entry.cb_fn = cb_fn; 2274 bdev_io->internal.waitq_entry.cb_arg = bdev_io; 2275 rc = spdk_bdev_queue_io_wait(bdev_io->bdev, spdk_io_channel_from_ctx(bdev_io->internal.ch), 2276 &bdev_io->internal.waitq_entry); 2277 if (rc != 0) { 2278 SPDK_ERRLOG("Queue IO failed, rc=%d\n", rc); 2279 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 2280 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 2281 } 2282 } 2283 2284 static bool 2285 bdev_rw_should_split(struct spdk_bdev_io *bdev_io) 2286 { 2287 uint32_t io_boundary = bdev_io->bdev->optimal_io_boundary; 2288 uint32_t max_size = bdev_io->bdev->max_segment_size; 2289 int max_segs = bdev_io->bdev->max_num_segments; 2290 2291 io_boundary = bdev_io->bdev->split_on_optimal_io_boundary ? io_boundary : 0; 2292 2293 if (spdk_likely(!io_boundary && !max_segs && !max_size)) { 2294 return false; 2295 } 2296 2297 if (io_boundary) { 2298 uint64_t start_stripe, end_stripe; 2299 2300 start_stripe = bdev_io->u.bdev.offset_blocks; 2301 end_stripe = start_stripe + bdev_io->u.bdev.num_blocks - 1; 2302 /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */ 2303 if (spdk_likely(spdk_u32_is_pow2(io_boundary))) { 2304 start_stripe >>= spdk_u32log2(io_boundary); 2305 end_stripe >>= spdk_u32log2(io_boundary); 2306 } else { 2307 start_stripe /= io_boundary; 2308 end_stripe /= io_boundary; 2309 } 2310 2311 if (start_stripe != end_stripe) { 2312 return true; 2313 } 2314 } 2315 2316 if (max_segs) { 2317 if (bdev_io->u.bdev.iovcnt > max_segs) { 2318 return true; 2319 } 2320 } 2321 2322 if (max_size) { 2323 for (int i = 0; i < bdev_io->u.bdev.iovcnt; i++) { 2324 if (bdev_io->u.bdev.iovs[i].iov_len > max_size) { 2325 return true; 2326 } 2327 } 2328 } 2329 2330 return false; 2331 } 2332 2333 static bool 2334 bdev_unmap_should_split(struct spdk_bdev_io *bdev_io) 2335 { 2336 uint32_t num_unmap_segments; 2337 2338 if (!bdev_io->bdev->max_unmap || !bdev_io->bdev->max_unmap_segments) { 2339 return false; 2340 } 2341 num_unmap_segments = spdk_divide_round_up(bdev_io->u.bdev.num_blocks, bdev_io->bdev->max_unmap); 2342 if (num_unmap_segments > bdev_io->bdev->max_unmap_segments) { 2343 return true; 2344 } 2345 2346 return false; 2347 } 2348 2349 static bool 2350 bdev_write_zeroes_should_split(struct spdk_bdev_io *bdev_io) 2351 { 2352 if (!bdev_io->bdev->max_write_zeroes) { 2353 return false; 2354 } 2355 2356 if (bdev_io->u.bdev.num_blocks > bdev_io->bdev->max_write_zeroes) { 2357 return true; 2358 } 2359 2360 return false; 2361 } 2362 2363 static bool 2364 bdev_io_should_split(struct spdk_bdev_io *bdev_io) 2365 { 2366 switch (bdev_io->type) { 2367 case SPDK_BDEV_IO_TYPE_READ: 2368 case SPDK_BDEV_IO_TYPE_WRITE: 2369 return bdev_rw_should_split(bdev_io); 2370 case SPDK_BDEV_IO_TYPE_UNMAP: 2371 return bdev_unmap_should_split(bdev_io); 2372 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2373 return bdev_write_zeroes_should_split(bdev_io); 2374 default: 2375 return false; 2376 } 2377 } 2378 2379 static uint32_t 2380 _to_next_boundary(uint64_t offset, uint32_t boundary) 2381 { 2382 return (boundary - (offset % boundary)); 2383 } 2384 2385 static void 2386 bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 2387 2388 static void 2389 _bdev_rw_split(void *_bdev_io); 2390 2391 static void 2392 bdev_unmap_split(struct spdk_bdev_io *bdev_io); 2393 2394 static void 2395 _bdev_unmap_split(void *_bdev_io) 2396 { 2397 return bdev_unmap_split((struct spdk_bdev_io *)_bdev_io); 2398 } 2399 2400 static void 2401 bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io); 2402 2403 static void 2404 _bdev_write_zeroes_split(void *_bdev_io) 2405 { 2406 return bdev_write_zeroes_split((struct spdk_bdev_io *)_bdev_io); 2407 } 2408 2409 static int 2410 bdev_io_split_submit(struct spdk_bdev_io *bdev_io, struct iovec *iov, int iovcnt, void *md_buf, 2411 uint64_t num_blocks, uint64_t *offset, uint64_t *remaining) 2412 { 2413 int rc; 2414 uint64_t current_offset, current_remaining; 2415 spdk_bdev_io_wait_cb io_wait_fn; 2416 2417 current_offset = *offset; 2418 current_remaining = *remaining; 2419 2420 bdev_io->u.bdev.split_outstanding++; 2421 2422 io_wait_fn = _bdev_rw_split; 2423 switch (bdev_io->type) { 2424 case SPDK_BDEV_IO_TYPE_READ: 2425 rc = bdev_readv_blocks_with_md(bdev_io->internal.desc, 2426 spdk_io_channel_from_ctx(bdev_io->internal.ch), 2427 iov, iovcnt, md_buf, current_offset, 2428 num_blocks, 2429 bdev_io_split_done, bdev_io, 2430 bdev_io->internal.ext_opts, true); 2431 break; 2432 case SPDK_BDEV_IO_TYPE_WRITE: 2433 rc = bdev_writev_blocks_with_md(bdev_io->internal.desc, 2434 spdk_io_channel_from_ctx(bdev_io->internal.ch), 2435 iov, iovcnt, md_buf, current_offset, 2436 num_blocks, 2437 bdev_io_split_done, bdev_io, 2438 bdev_io->internal.ext_opts, true); 2439 break; 2440 case SPDK_BDEV_IO_TYPE_UNMAP: 2441 io_wait_fn = _bdev_unmap_split; 2442 rc = spdk_bdev_unmap_blocks(bdev_io->internal.desc, 2443 spdk_io_channel_from_ctx(bdev_io->internal.ch), 2444 current_offset, num_blocks, 2445 bdev_io_split_done, bdev_io); 2446 break; 2447 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2448 io_wait_fn = _bdev_write_zeroes_split; 2449 rc = spdk_bdev_write_zeroes_blocks(bdev_io->internal.desc, 2450 spdk_io_channel_from_ctx(bdev_io->internal.ch), 2451 current_offset, num_blocks, 2452 bdev_io_split_done, bdev_io); 2453 break; 2454 default: 2455 assert(false); 2456 rc = -EINVAL; 2457 break; 2458 } 2459 2460 if (rc == 0) { 2461 current_offset += num_blocks; 2462 current_remaining -= num_blocks; 2463 bdev_io->u.bdev.split_current_offset_blocks = current_offset; 2464 bdev_io->u.bdev.split_remaining_num_blocks = current_remaining; 2465 *offset = current_offset; 2466 *remaining = current_remaining; 2467 } else { 2468 bdev_io->u.bdev.split_outstanding--; 2469 if (rc == -ENOMEM) { 2470 if (bdev_io->u.bdev.split_outstanding == 0) { 2471 /* No I/O is outstanding. Hence we should wait here. */ 2472 bdev_queue_io_wait_with_cb(bdev_io, io_wait_fn); 2473 } 2474 } else { 2475 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 2476 if (bdev_io->u.bdev.split_outstanding == 0) { 2477 spdk_trace_record(TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, bdev_io->internal.caller_ctx); 2478 TAILQ_REMOVE(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link); 2479 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 2480 } 2481 } 2482 } 2483 2484 return rc; 2485 } 2486 2487 static void 2488 _bdev_rw_split(void *_bdev_io) 2489 { 2490 struct iovec *parent_iov, *iov; 2491 struct spdk_bdev_io *bdev_io = _bdev_io; 2492 struct spdk_bdev *bdev = bdev_io->bdev; 2493 uint64_t parent_offset, current_offset, remaining; 2494 uint32_t parent_iov_offset, parent_iovcnt, parent_iovpos, child_iovcnt; 2495 uint32_t to_next_boundary, to_next_boundary_bytes, to_last_block_bytes; 2496 uint32_t iovcnt, iov_len, child_iovsize; 2497 uint32_t blocklen = bdev->blocklen; 2498 uint32_t io_boundary = bdev->optimal_io_boundary; 2499 uint32_t max_segment_size = bdev->max_segment_size; 2500 uint32_t max_child_iovcnt = bdev->max_num_segments; 2501 void *md_buf = NULL; 2502 int rc; 2503 2504 max_segment_size = max_segment_size ? max_segment_size : UINT32_MAX; 2505 max_child_iovcnt = max_child_iovcnt ? spdk_min(max_child_iovcnt, BDEV_IO_NUM_CHILD_IOV) : 2506 BDEV_IO_NUM_CHILD_IOV; 2507 io_boundary = bdev->split_on_optimal_io_boundary ? io_boundary : UINT32_MAX; 2508 2509 remaining = bdev_io->u.bdev.split_remaining_num_blocks; 2510 current_offset = bdev_io->u.bdev.split_current_offset_blocks; 2511 parent_offset = bdev_io->u.bdev.offset_blocks; 2512 parent_iov_offset = (current_offset - parent_offset) * blocklen; 2513 parent_iovcnt = bdev_io->u.bdev.iovcnt; 2514 2515 for (parent_iovpos = 0; parent_iovpos < parent_iovcnt; parent_iovpos++) { 2516 parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos]; 2517 if (parent_iov_offset < parent_iov->iov_len) { 2518 break; 2519 } 2520 parent_iov_offset -= parent_iov->iov_len; 2521 } 2522 2523 child_iovcnt = 0; 2524 while (remaining > 0 && parent_iovpos < parent_iovcnt && child_iovcnt < BDEV_IO_NUM_CHILD_IOV) { 2525 to_next_boundary = _to_next_boundary(current_offset, io_boundary); 2526 to_next_boundary = spdk_min(remaining, to_next_boundary); 2527 to_next_boundary_bytes = to_next_boundary * blocklen; 2528 2529 iov = &bdev_io->child_iov[child_iovcnt]; 2530 iovcnt = 0; 2531 2532 if (bdev_io->u.bdev.md_buf) { 2533 md_buf = (char *)bdev_io->u.bdev.md_buf + 2534 (current_offset - parent_offset) * spdk_bdev_get_md_size(bdev); 2535 } 2536 2537 child_iovsize = spdk_min(BDEV_IO_NUM_CHILD_IOV - child_iovcnt, max_child_iovcnt); 2538 while (to_next_boundary_bytes > 0 && parent_iovpos < parent_iovcnt && 2539 iovcnt < child_iovsize) { 2540 parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos]; 2541 iov_len = parent_iov->iov_len - parent_iov_offset; 2542 2543 iov_len = spdk_min(iov_len, max_segment_size); 2544 iov_len = spdk_min(iov_len, to_next_boundary_bytes); 2545 to_next_boundary_bytes -= iov_len; 2546 2547 bdev_io->child_iov[child_iovcnt].iov_base = parent_iov->iov_base + parent_iov_offset; 2548 bdev_io->child_iov[child_iovcnt].iov_len = iov_len; 2549 2550 if (iov_len < parent_iov->iov_len - parent_iov_offset) { 2551 parent_iov_offset += iov_len; 2552 } else { 2553 parent_iovpos++; 2554 parent_iov_offset = 0; 2555 } 2556 child_iovcnt++; 2557 iovcnt++; 2558 } 2559 2560 if (to_next_boundary_bytes > 0) { 2561 /* We had to stop this child I/O early because we ran out of 2562 * child_iov space or were limited by max_num_segments. 2563 * Ensure the iovs to be aligned with block size and 2564 * then adjust to_next_boundary before starting the 2565 * child I/O. 2566 */ 2567 assert(child_iovcnt == BDEV_IO_NUM_CHILD_IOV || 2568 iovcnt == child_iovsize); 2569 to_last_block_bytes = to_next_boundary_bytes % blocklen; 2570 if (to_last_block_bytes != 0) { 2571 uint32_t child_iovpos = child_iovcnt - 1; 2572 /* don't decrease child_iovcnt when it equals to BDEV_IO_NUM_CHILD_IOV 2573 * so the loop will naturally end 2574 */ 2575 2576 to_last_block_bytes = blocklen - to_last_block_bytes; 2577 to_next_boundary_bytes += to_last_block_bytes; 2578 while (to_last_block_bytes > 0 && iovcnt > 0) { 2579 iov_len = spdk_min(to_last_block_bytes, 2580 bdev_io->child_iov[child_iovpos].iov_len); 2581 bdev_io->child_iov[child_iovpos].iov_len -= iov_len; 2582 if (bdev_io->child_iov[child_iovpos].iov_len == 0) { 2583 child_iovpos--; 2584 if (--iovcnt == 0) { 2585 /* If the child IO is less than a block size just return. 2586 * If the first child IO of any split round is less than 2587 * a block size, an error exit. 2588 */ 2589 if (bdev_io->u.bdev.split_outstanding == 0) { 2590 SPDK_ERRLOG("The first child io was less than a block size\n"); 2591 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 2592 spdk_trace_record(TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, bdev_io->internal.caller_ctx); 2593 TAILQ_REMOVE(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link); 2594 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 2595 } 2596 2597 return; 2598 } 2599 } 2600 2601 to_last_block_bytes -= iov_len; 2602 2603 if (parent_iov_offset == 0) { 2604 parent_iovpos--; 2605 parent_iov_offset = bdev_io->u.bdev.iovs[parent_iovpos].iov_len; 2606 } 2607 parent_iov_offset -= iov_len; 2608 } 2609 2610 assert(to_last_block_bytes == 0); 2611 } 2612 to_next_boundary -= to_next_boundary_bytes / blocklen; 2613 } 2614 2615 rc = bdev_io_split_submit(bdev_io, iov, iovcnt, md_buf, to_next_boundary, 2616 ¤t_offset, &remaining); 2617 if (spdk_unlikely(rc)) { 2618 return; 2619 } 2620 } 2621 } 2622 2623 static void 2624 bdev_unmap_split(struct spdk_bdev_io *bdev_io) 2625 { 2626 uint64_t offset, unmap_blocks, remaining, max_unmap_blocks; 2627 uint32_t num_children_reqs = 0; 2628 int rc; 2629 2630 offset = bdev_io->u.bdev.split_current_offset_blocks; 2631 remaining = bdev_io->u.bdev.split_remaining_num_blocks; 2632 max_unmap_blocks = bdev_io->bdev->max_unmap * bdev_io->bdev->max_unmap_segments; 2633 2634 while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS)) { 2635 unmap_blocks = spdk_min(remaining, max_unmap_blocks); 2636 2637 rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, unmap_blocks, 2638 &offset, &remaining); 2639 if (spdk_likely(rc == 0)) { 2640 num_children_reqs++; 2641 } else { 2642 return; 2643 } 2644 } 2645 } 2646 2647 static void 2648 bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io) 2649 { 2650 uint64_t offset, write_zeroes_blocks, remaining; 2651 uint32_t num_children_reqs = 0; 2652 int rc; 2653 2654 offset = bdev_io->u.bdev.split_current_offset_blocks; 2655 remaining = bdev_io->u.bdev.split_remaining_num_blocks; 2656 2657 while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS)) { 2658 write_zeroes_blocks = spdk_min(remaining, bdev_io->bdev->max_write_zeroes); 2659 2660 rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, write_zeroes_blocks, 2661 &offset, &remaining); 2662 if (spdk_likely(rc == 0)) { 2663 num_children_reqs++; 2664 } else { 2665 return; 2666 } 2667 } 2668 } 2669 2670 static void 2671 parent_bdev_io_complete(void *ctx, int rc) 2672 { 2673 struct spdk_bdev_io *parent_io = ctx; 2674 2675 if (rc) { 2676 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 2677 } 2678 2679 parent_io->internal.cb(parent_io, parent_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS, 2680 parent_io->internal.caller_ctx); 2681 } 2682 2683 static void 2684 bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 2685 { 2686 struct spdk_bdev_io *parent_io = cb_arg; 2687 2688 spdk_bdev_free_io(bdev_io); 2689 2690 if (!success) { 2691 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 2692 /* If any child I/O failed, stop further splitting process. */ 2693 parent_io->u.bdev.split_current_offset_blocks += parent_io->u.bdev.split_remaining_num_blocks; 2694 parent_io->u.bdev.split_remaining_num_blocks = 0; 2695 } 2696 parent_io->u.bdev.split_outstanding--; 2697 if (parent_io->u.bdev.split_outstanding != 0) { 2698 return; 2699 } 2700 2701 /* 2702 * Parent I/O finishes when all blocks are consumed. 2703 */ 2704 if (parent_io->u.bdev.split_remaining_num_blocks == 0) { 2705 assert(parent_io->internal.cb != bdev_io_split_done); 2706 spdk_trace_record(TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)parent_io, bdev_io->internal.caller_ctx); 2707 TAILQ_REMOVE(&parent_io->internal.ch->io_submitted, parent_io, internal.ch_link); 2708 2709 if (parent_io->internal.orig_iovcnt != 0) { 2710 _bdev_io_push_bounce_data_buffer(parent_io, parent_bdev_io_complete); 2711 /* bdev IO will be completed in the callback */ 2712 } else { 2713 parent_bdev_io_complete(parent_io, 0); 2714 } 2715 return; 2716 } 2717 2718 /* 2719 * Continue with the splitting process. This function will complete the parent I/O if the 2720 * splitting is done. 2721 */ 2722 switch (parent_io->type) { 2723 case SPDK_BDEV_IO_TYPE_READ: 2724 case SPDK_BDEV_IO_TYPE_WRITE: 2725 _bdev_rw_split(parent_io); 2726 break; 2727 case SPDK_BDEV_IO_TYPE_UNMAP: 2728 bdev_unmap_split(parent_io); 2729 break; 2730 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2731 bdev_write_zeroes_split(parent_io); 2732 break; 2733 default: 2734 assert(false); 2735 break; 2736 } 2737 } 2738 2739 static void 2740 bdev_rw_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success); 2741 2742 static void 2743 bdev_io_split(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 2744 { 2745 bdev_io->u.bdev.split_current_offset_blocks = bdev_io->u.bdev.offset_blocks; 2746 bdev_io->u.bdev.split_remaining_num_blocks = bdev_io->u.bdev.num_blocks; 2747 bdev_io->u.bdev.split_outstanding = 0; 2748 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 2749 2750 switch (bdev_io->type) { 2751 case SPDK_BDEV_IO_TYPE_READ: 2752 case SPDK_BDEV_IO_TYPE_WRITE: 2753 if (_is_buf_allocated(bdev_io->u.bdev.iovs)) { 2754 _bdev_rw_split(bdev_io); 2755 } else { 2756 assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ); 2757 spdk_bdev_io_get_buf(bdev_io, bdev_rw_split_get_buf_cb, 2758 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 2759 } 2760 break; 2761 case SPDK_BDEV_IO_TYPE_UNMAP: 2762 bdev_unmap_split(bdev_io); 2763 break; 2764 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2765 bdev_write_zeroes_split(bdev_io); 2766 break; 2767 default: 2768 assert(false); 2769 break; 2770 } 2771 } 2772 2773 static void 2774 bdev_rw_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success) 2775 { 2776 if (!success) { 2777 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 2778 return; 2779 } 2780 2781 _bdev_rw_split(bdev_io); 2782 } 2783 2784 /* Explicitly mark this inline, since it's used as a function pointer and otherwise won't 2785 * be inlined, at least on some compilers. 2786 */ 2787 static inline void 2788 _bdev_io_submit(void *ctx) 2789 { 2790 struct spdk_bdev_io *bdev_io = ctx; 2791 struct spdk_bdev *bdev = bdev_io->bdev; 2792 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 2793 uint64_t tsc; 2794 2795 tsc = spdk_get_ticks(); 2796 bdev_io->internal.submit_tsc = tsc; 2797 spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_START, 0, 0, (uintptr_t)bdev_io, 2798 (uint64_t)bdev_io->type, bdev_io->internal.caller_ctx, 2799 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks); 2800 2801 if (spdk_likely(bdev_ch->flags == 0)) { 2802 bdev_io_do_submit(bdev_ch, bdev_io); 2803 return; 2804 } 2805 2806 if (bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS) { 2807 _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_ABORTED); 2808 } else if (bdev_ch->flags & BDEV_CH_QOS_ENABLED) { 2809 if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT) && 2810 bdev_abort_queued_io(&bdev->internal.qos->queued, bdev_io->u.abort.bio_to_abort)) { 2811 _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS); 2812 } else { 2813 TAILQ_INSERT_TAIL(&bdev->internal.qos->queued, bdev_io, internal.link); 2814 bdev_qos_io_submit(bdev_ch, bdev->internal.qos); 2815 } 2816 } else { 2817 SPDK_ERRLOG("unknown bdev_ch flag %x found\n", bdev_ch->flags); 2818 _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 2819 } 2820 } 2821 2822 bool 2823 bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2); 2824 2825 bool 2826 bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2) 2827 { 2828 if (range1->length == 0 || range2->length == 0) { 2829 return false; 2830 } 2831 2832 if (range1->offset + range1->length <= range2->offset) { 2833 return false; 2834 } 2835 2836 if (range2->offset + range2->length <= range1->offset) { 2837 return false; 2838 } 2839 2840 return true; 2841 } 2842 2843 static bool 2844 bdev_io_range_is_locked(struct spdk_bdev_io *bdev_io, struct lba_range *range) 2845 { 2846 struct spdk_bdev_channel *ch = bdev_io->internal.ch; 2847 struct lba_range r; 2848 2849 switch (bdev_io->type) { 2850 case SPDK_BDEV_IO_TYPE_NVME_IO: 2851 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 2852 /* Don't try to decode the NVMe command - just assume worst-case and that 2853 * it overlaps a locked range. 2854 */ 2855 return true; 2856 case SPDK_BDEV_IO_TYPE_WRITE: 2857 case SPDK_BDEV_IO_TYPE_UNMAP: 2858 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2859 case SPDK_BDEV_IO_TYPE_ZCOPY: 2860 r.offset = bdev_io->u.bdev.offset_blocks; 2861 r.length = bdev_io->u.bdev.num_blocks; 2862 if (!bdev_lba_range_overlapped(range, &r)) { 2863 /* This I/O doesn't overlap the specified LBA range. */ 2864 return false; 2865 } else if (range->owner_ch == ch && range->locked_ctx == bdev_io->internal.caller_ctx) { 2866 /* This I/O overlaps, but the I/O is on the same channel that locked this 2867 * range, and the caller_ctx is the same as the locked_ctx. This means 2868 * that this I/O is associated with the lock, and is allowed to execute. 2869 */ 2870 return false; 2871 } else { 2872 return true; 2873 } 2874 default: 2875 return false; 2876 } 2877 } 2878 2879 void 2880 bdev_io_submit(struct spdk_bdev_io *bdev_io) 2881 { 2882 struct spdk_bdev *bdev = bdev_io->bdev; 2883 struct spdk_thread *thread = spdk_bdev_io_get_thread(bdev_io); 2884 struct spdk_bdev_channel *ch = bdev_io->internal.ch; 2885 2886 assert(thread != NULL); 2887 assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING); 2888 2889 if (!TAILQ_EMPTY(&ch->locked_ranges)) { 2890 struct lba_range *range; 2891 2892 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 2893 if (bdev_io_range_is_locked(bdev_io, range)) { 2894 TAILQ_INSERT_TAIL(&ch->io_locked, bdev_io, internal.ch_link); 2895 return; 2896 } 2897 } 2898 } 2899 2900 TAILQ_INSERT_TAIL(&ch->io_submitted, bdev_io, internal.ch_link); 2901 2902 if (bdev_io_should_split(bdev_io)) { 2903 bdev_io->internal.submit_tsc = spdk_get_ticks(); 2904 spdk_trace_record_tsc(bdev_io->internal.submit_tsc, TRACE_BDEV_IO_START, 0, 0, 2905 (uintptr_t)bdev_io, (uint64_t)bdev_io->type, bdev_io->internal.caller_ctx, 2906 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks); 2907 bdev_io_split(NULL, bdev_io); 2908 return; 2909 } 2910 2911 if (ch->flags & BDEV_CH_QOS_ENABLED) { 2912 if ((thread == bdev->internal.qos->thread) || !bdev->internal.qos->thread) { 2913 _bdev_io_submit(bdev_io); 2914 } else { 2915 bdev_io->internal.io_submit_ch = ch; 2916 bdev_io->internal.ch = bdev->internal.qos->ch; 2917 spdk_thread_send_msg(bdev->internal.qos->thread, _bdev_io_submit, bdev_io); 2918 } 2919 } else { 2920 _bdev_io_submit(bdev_io); 2921 } 2922 } 2923 2924 static void 2925 bdev_io_submit_reset(struct spdk_bdev_io *bdev_io) 2926 { 2927 struct spdk_bdev *bdev = bdev_io->bdev; 2928 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 2929 struct spdk_io_channel *ch = bdev_ch->channel; 2930 2931 assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING); 2932 2933 bdev_io->internal.in_submit_request = true; 2934 bdev->fn_table->submit_request(ch, bdev_io); 2935 bdev_io->internal.in_submit_request = false; 2936 } 2937 2938 void 2939 bdev_io_init(struct spdk_bdev_io *bdev_io, 2940 struct spdk_bdev *bdev, void *cb_arg, 2941 spdk_bdev_io_completion_cb cb) 2942 { 2943 bdev_io->bdev = bdev; 2944 bdev_io->internal.caller_ctx = cb_arg; 2945 bdev_io->internal.cb = cb; 2946 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 2947 bdev_io->internal.in_submit_request = false; 2948 bdev_io->internal.buf = NULL; 2949 bdev_io->internal.io_submit_ch = NULL; 2950 bdev_io->internal.orig_iovs = NULL; 2951 bdev_io->internal.orig_iovcnt = 0; 2952 bdev_io->internal.orig_md_iov.iov_base = NULL; 2953 bdev_io->internal.error.nvme.cdw0 = 0; 2954 bdev_io->num_retries = 0; 2955 bdev_io->internal.get_buf_cb = NULL; 2956 bdev_io->internal.get_aux_buf_cb = NULL; 2957 bdev_io->internal.ext_opts = NULL; 2958 bdev_io->internal.data_transfer_cpl = NULL; 2959 } 2960 2961 static bool 2962 bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type) 2963 { 2964 return bdev->fn_table->io_type_supported(bdev->ctxt, io_type); 2965 } 2966 2967 bool 2968 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type) 2969 { 2970 bool supported; 2971 2972 supported = bdev_io_type_supported(bdev, io_type); 2973 2974 if (!supported) { 2975 switch (io_type) { 2976 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2977 /* The bdev layer will emulate write zeroes as long as write is supported. */ 2978 supported = bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE); 2979 break; 2980 default: 2981 break; 2982 } 2983 } 2984 2985 return supported; 2986 } 2987 2988 int 2989 spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 2990 { 2991 if (bdev->fn_table->dump_info_json) { 2992 return bdev->fn_table->dump_info_json(bdev->ctxt, w); 2993 } 2994 2995 return 0; 2996 } 2997 2998 static void 2999 bdev_qos_update_max_quota_per_timeslice(struct spdk_bdev_qos *qos) 3000 { 3001 uint32_t max_per_timeslice = 0; 3002 int i; 3003 3004 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 3005 if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 3006 qos->rate_limits[i].max_per_timeslice = 0; 3007 continue; 3008 } 3009 3010 max_per_timeslice = qos->rate_limits[i].limit * 3011 SPDK_BDEV_QOS_TIMESLICE_IN_USEC / SPDK_SEC_TO_USEC; 3012 3013 qos->rate_limits[i].max_per_timeslice = spdk_max(max_per_timeslice, 3014 qos->rate_limits[i].min_per_timeslice); 3015 3016 qos->rate_limits[i].remaining_this_timeslice = qos->rate_limits[i].max_per_timeslice; 3017 } 3018 3019 bdev_qos_set_ops(qos); 3020 } 3021 3022 static int 3023 bdev_channel_poll_qos(void *arg) 3024 { 3025 struct spdk_bdev_qos *qos = arg; 3026 uint64_t now = spdk_get_ticks(); 3027 int i; 3028 3029 if (now < (qos->last_timeslice + qos->timeslice_size)) { 3030 /* We received our callback earlier than expected - return 3031 * immediately and wait to do accounting until at least one 3032 * timeslice has actually expired. This should never happen 3033 * with a well-behaved timer implementation. 3034 */ 3035 return SPDK_POLLER_IDLE; 3036 } 3037 3038 /* Reset for next round of rate limiting */ 3039 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 3040 /* We may have allowed the IOs or bytes to slightly overrun in the last 3041 * timeslice. remaining_this_timeslice is signed, so if it's negative 3042 * here, we'll account for the overrun so that the next timeslice will 3043 * be appropriately reduced. 3044 */ 3045 if (qos->rate_limits[i].remaining_this_timeslice > 0) { 3046 qos->rate_limits[i].remaining_this_timeslice = 0; 3047 } 3048 } 3049 3050 while (now >= (qos->last_timeslice + qos->timeslice_size)) { 3051 qos->last_timeslice += qos->timeslice_size; 3052 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 3053 qos->rate_limits[i].remaining_this_timeslice += 3054 qos->rate_limits[i].max_per_timeslice; 3055 } 3056 } 3057 3058 return bdev_qos_io_submit(qos->ch, qos); 3059 } 3060 3061 static void 3062 bdev_channel_destroy_resource(struct spdk_bdev_channel *ch) 3063 { 3064 struct spdk_bdev_shared_resource *shared_resource; 3065 struct lba_range *range; 3066 3067 while (!TAILQ_EMPTY(&ch->locked_ranges)) { 3068 range = TAILQ_FIRST(&ch->locked_ranges); 3069 TAILQ_REMOVE(&ch->locked_ranges, range, tailq); 3070 free(range); 3071 } 3072 3073 spdk_put_io_channel(ch->channel); 3074 3075 shared_resource = ch->shared_resource; 3076 3077 assert(TAILQ_EMPTY(&ch->io_locked)); 3078 assert(TAILQ_EMPTY(&ch->io_submitted)); 3079 assert(ch->io_outstanding == 0); 3080 assert(shared_resource->ref > 0); 3081 shared_resource->ref--; 3082 if (shared_resource->ref == 0) { 3083 assert(shared_resource->io_outstanding == 0); 3084 TAILQ_REMOVE(&shared_resource->mgmt_ch->shared_resources, shared_resource, link); 3085 spdk_put_io_channel(spdk_io_channel_from_ctx(shared_resource->mgmt_ch)); 3086 free(shared_resource); 3087 } 3088 } 3089 3090 /* Caller must hold bdev->internal.mutex. */ 3091 static void 3092 bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch) 3093 { 3094 struct spdk_bdev_qos *qos = bdev->internal.qos; 3095 int i; 3096 3097 /* Rate limiting on this bdev enabled */ 3098 if (qos) { 3099 if (qos->ch == NULL) { 3100 struct spdk_io_channel *io_ch; 3101 3102 SPDK_DEBUGLOG(bdev, "Selecting channel %p as QoS channel for bdev %s on thread %p\n", ch, 3103 bdev->name, spdk_get_thread()); 3104 3105 /* No qos channel has been selected, so set one up */ 3106 3107 /* Take another reference to ch */ 3108 io_ch = spdk_get_io_channel(__bdev_to_io_dev(bdev)); 3109 assert(io_ch != NULL); 3110 qos->ch = ch; 3111 3112 qos->thread = spdk_io_channel_get_thread(io_ch); 3113 3114 TAILQ_INIT(&qos->queued); 3115 3116 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 3117 if (bdev_qos_is_iops_rate_limit(i) == true) { 3118 qos->rate_limits[i].min_per_timeslice = 3119 SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE; 3120 } else { 3121 qos->rate_limits[i].min_per_timeslice = 3122 SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE; 3123 } 3124 3125 if (qos->rate_limits[i].limit == 0) { 3126 qos->rate_limits[i].limit = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED; 3127 } 3128 } 3129 bdev_qos_update_max_quota_per_timeslice(qos); 3130 qos->timeslice_size = 3131 SPDK_BDEV_QOS_TIMESLICE_IN_USEC * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC; 3132 qos->last_timeslice = spdk_get_ticks(); 3133 qos->poller = SPDK_POLLER_REGISTER(bdev_channel_poll_qos, 3134 qos, 3135 SPDK_BDEV_QOS_TIMESLICE_IN_USEC); 3136 } 3137 3138 ch->flags |= BDEV_CH_QOS_ENABLED; 3139 } 3140 } 3141 3142 struct poll_timeout_ctx { 3143 struct spdk_bdev_desc *desc; 3144 uint64_t timeout_in_sec; 3145 spdk_bdev_io_timeout_cb cb_fn; 3146 void *cb_arg; 3147 }; 3148 3149 static void 3150 bdev_desc_free(struct spdk_bdev_desc *desc) 3151 { 3152 pthread_mutex_destroy(&desc->mutex); 3153 free(desc->media_events_buffer); 3154 free(desc); 3155 } 3156 3157 static void 3158 bdev_channel_poll_timeout_io_done(struct spdk_io_channel_iter *i, int status) 3159 { 3160 struct poll_timeout_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 3161 struct spdk_bdev_desc *desc = ctx->desc; 3162 3163 free(ctx); 3164 3165 pthread_mutex_lock(&desc->mutex); 3166 desc->refs--; 3167 if (desc->closed == true && desc->refs == 0) { 3168 pthread_mutex_unlock(&desc->mutex); 3169 bdev_desc_free(desc); 3170 return; 3171 } 3172 pthread_mutex_unlock(&desc->mutex); 3173 } 3174 3175 static void 3176 bdev_channel_poll_timeout_io(struct spdk_io_channel_iter *i) 3177 { 3178 struct poll_timeout_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 3179 struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i); 3180 struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(io_ch); 3181 struct spdk_bdev_desc *desc = ctx->desc; 3182 struct spdk_bdev_io *bdev_io; 3183 uint64_t now; 3184 3185 pthread_mutex_lock(&desc->mutex); 3186 if (desc->closed == true) { 3187 pthread_mutex_unlock(&desc->mutex); 3188 spdk_for_each_channel_continue(i, -1); 3189 return; 3190 } 3191 pthread_mutex_unlock(&desc->mutex); 3192 3193 now = spdk_get_ticks(); 3194 TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) { 3195 /* Exclude any I/O that are generated via splitting. */ 3196 if (bdev_io->internal.cb == bdev_io_split_done) { 3197 continue; 3198 } 3199 3200 /* Once we find an I/O that has not timed out, we can immediately 3201 * exit the loop. 3202 */ 3203 if (now < (bdev_io->internal.submit_tsc + 3204 ctx->timeout_in_sec * spdk_get_ticks_hz())) { 3205 goto end; 3206 } 3207 3208 if (bdev_io->internal.desc == desc) { 3209 ctx->cb_fn(ctx->cb_arg, bdev_io); 3210 } 3211 } 3212 3213 end: 3214 spdk_for_each_channel_continue(i, 0); 3215 } 3216 3217 static int 3218 bdev_poll_timeout_io(void *arg) 3219 { 3220 struct spdk_bdev_desc *desc = arg; 3221 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3222 struct poll_timeout_ctx *ctx; 3223 3224 ctx = calloc(1, sizeof(struct poll_timeout_ctx)); 3225 if (!ctx) { 3226 SPDK_ERRLOG("failed to allocate memory\n"); 3227 return SPDK_POLLER_BUSY; 3228 } 3229 ctx->desc = desc; 3230 ctx->cb_arg = desc->cb_arg; 3231 ctx->cb_fn = desc->cb_fn; 3232 ctx->timeout_in_sec = desc->timeout_in_sec; 3233 3234 /* Take a ref on the descriptor in case it gets closed while we are checking 3235 * all of the channels. 3236 */ 3237 pthread_mutex_lock(&desc->mutex); 3238 desc->refs++; 3239 pthread_mutex_unlock(&desc->mutex); 3240 3241 spdk_for_each_channel(__bdev_to_io_dev(bdev), 3242 bdev_channel_poll_timeout_io, 3243 ctx, 3244 bdev_channel_poll_timeout_io_done); 3245 3246 return SPDK_POLLER_BUSY; 3247 } 3248 3249 int 3250 spdk_bdev_set_timeout(struct spdk_bdev_desc *desc, uint64_t timeout_in_sec, 3251 spdk_bdev_io_timeout_cb cb_fn, void *cb_arg) 3252 { 3253 assert(desc->thread == spdk_get_thread()); 3254 3255 spdk_poller_unregister(&desc->io_timeout_poller); 3256 3257 if (timeout_in_sec) { 3258 assert(cb_fn != NULL); 3259 desc->io_timeout_poller = SPDK_POLLER_REGISTER(bdev_poll_timeout_io, 3260 desc, 3261 SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * SPDK_SEC_TO_USEC / 3262 1000); 3263 if (desc->io_timeout_poller == NULL) { 3264 SPDK_ERRLOG("can not register the desc timeout IO poller\n"); 3265 return -1; 3266 } 3267 } 3268 3269 desc->cb_fn = cb_fn; 3270 desc->cb_arg = cb_arg; 3271 desc->timeout_in_sec = timeout_in_sec; 3272 3273 return 0; 3274 } 3275 3276 static int 3277 bdev_channel_create(void *io_device, void *ctx_buf) 3278 { 3279 struct spdk_bdev *bdev = __bdev_from_io_dev(io_device); 3280 struct spdk_bdev_channel *ch = ctx_buf; 3281 struct spdk_io_channel *mgmt_io_ch; 3282 struct spdk_bdev_mgmt_channel *mgmt_ch; 3283 struct spdk_bdev_shared_resource *shared_resource; 3284 struct lba_range *range; 3285 3286 ch->bdev = bdev; 3287 ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt); 3288 if (!ch->channel) { 3289 return -1; 3290 } 3291 3292 spdk_trace_record(TRACE_BDEV_IOCH_CREATE, 0, 0, 0, ch->bdev->name, 3293 spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel))); 3294 3295 assert(ch->histogram == NULL); 3296 if (bdev->internal.histogram_enabled) { 3297 ch->histogram = spdk_histogram_data_alloc(); 3298 if (ch->histogram == NULL) { 3299 SPDK_ERRLOG("Could not allocate histogram\n"); 3300 } 3301 } 3302 3303 mgmt_io_ch = spdk_get_io_channel(&g_bdev_mgr); 3304 if (!mgmt_io_ch) { 3305 spdk_put_io_channel(ch->channel); 3306 return -1; 3307 } 3308 3309 mgmt_ch = spdk_io_channel_get_ctx(mgmt_io_ch); 3310 TAILQ_FOREACH(shared_resource, &mgmt_ch->shared_resources, link) { 3311 if (shared_resource->shared_ch == ch->channel) { 3312 spdk_put_io_channel(mgmt_io_ch); 3313 shared_resource->ref++; 3314 break; 3315 } 3316 } 3317 3318 if (shared_resource == NULL) { 3319 shared_resource = calloc(1, sizeof(*shared_resource)); 3320 if (shared_resource == NULL) { 3321 spdk_put_io_channel(ch->channel); 3322 spdk_put_io_channel(mgmt_io_ch); 3323 return -1; 3324 } 3325 3326 shared_resource->mgmt_ch = mgmt_ch; 3327 shared_resource->io_outstanding = 0; 3328 TAILQ_INIT(&shared_resource->nomem_io); 3329 shared_resource->nomem_threshold = 0; 3330 shared_resource->shared_ch = ch->channel; 3331 shared_resource->ref = 1; 3332 TAILQ_INSERT_TAIL(&mgmt_ch->shared_resources, shared_resource, link); 3333 } 3334 3335 memset(&ch->stat, 0, sizeof(ch->stat)); 3336 ch->stat.ticks_rate = spdk_get_ticks_hz(); 3337 ch->io_outstanding = 0; 3338 TAILQ_INIT(&ch->queued_resets); 3339 TAILQ_INIT(&ch->locked_ranges); 3340 ch->flags = 0; 3341 ch->shared_resource = shared_resource; 3342 3343 TAILQ_INIT(&ch->io_submitted); 3344 TAILQ_INIT(&ch->io_locked); 3345 3346 #ifdef SPDK_CONFIG_VTUNE 3347 { 3348 char *name; 3349 __itt_init_ittlib(NULL, 0); 3350 name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch); 3351 if (!name) { 3352 bdev_channel_destroy_resource(ch); 3353 return -1; 3354 } 3355 ch->handle = __itt_string_handle_create(name); 3356 free(name); 3357 ch->start_tsc = spdk_get_ticks(); 3358 ch->interval_tsc = spdk_get_ticks_hz() / 100; 3359 memset(&ch->prev_stat, 0, sizeof(ch->prev_stat)); 3360 } 3361 #endif 3362 3363 pthread_mutex_lock(&bdev->internal.mutex); 3364 bdev_enable_qos(bdev, ch); 3365 3366 TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) { 3367 struct lba_range *new_range; 3368 3369 new_range = calloc(1, sizeof(*new_range)); 3370 if (new_range == NULL) { 3371 pthread_mutex_unlock(&bdev->internal.mutex); 3372 bdev_channel_destroy_resource(ch); 3373 return -1; 3374 } 3375 new_range->length = range->length; 3376 new_range->offset = range->offset; 3377 new_range->locked_ctx = range->locked_ctx; 3378 TAILQ_INSERT_TAIL(&ch->locked_ranges, new_range, tailq); 3379 } 3380 3381 pthread_mutex_unlock(&bdev->internal.mutex); 3382 3383 return 0; 3384 } 3385 3386 /* 3387 * Abort I/O that are waiting on a data buffer. These types of I/O are 3388 * linked using the spdk_bdev_io internal.buf_link TAILQ_ENTRY. 3389 */ 3390 static void 3391 bdev_abort_all_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_channel *ch) 3392 { 3393 bdev_io_stailq_t tmp; 3394 struct spdk_bdev_io *bdev_io; 3395 3396 STAILQ_INIT(&tmp); 3397 3398 while (!STAILQ_EMPTY(queue)) { 3399 bdev_io = STAILQ_FIRST(queue); 3400 STAILQ_REMOVE_HEAD(queue, internal.buf_link); 3401 if (bdev_io->internal.ch == ch) { 3402 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED); 3403 } else { 3404 STAILQ_INSERT_TAIL(&tmp, bdev_io, internal.buf_link); 3405 } 3406 } 3407 3408 STAILQ_SWAP(&tmp, queue, spdk_bdev_io); 3409 } 3410 3411 /* 3412 * Abort I/O that are queued waiting for submission. These types of I/O are 3413 * linked using the spdk_bdev_io link TAILQ_ENTRY. 3414 */ 3415 static void 3416 bdev_abort_all_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch) 3417 { 3418 struct spdk_bdev_io *bdev_io, *tmp; 3419 3420 TAILQ_FOREACH_SAFE(bdev_io, queue, internal.link, tmp) { 3421 if (bdev_io->internal.ch == ch) { 3422 TAILQ_REMOVE(queue, bdev_io, internal.link); 3423 /* 3424 * spdk_bdev_io_complete() assumes that the completed I/O had 3425 * been submitted to the bdev module. Since in this case it 3426 * hadn't, bump io_outstanding to account for the decrement 3427 * that spdk_bdev_io_complete() will do. 3428 */ 3429 if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) { 3430 ch->io_outstanding++; 3431 ch->shared_resource->io_outstanding++; 3432 } 3433 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED); 3434 } 3435 } 3436 } 3437 3438 static bool 3439 bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort) 3440 { 3441 struct spdk_bdev_io *bdev_io; 3442 3443 TAILQ_FOREACH(bdev_io, queue, internal.link) { 3444 if (bdev_io == bio_to_abort) { 3445 TAILQ_REMOVE(queue, bio_to_abort, internal.link); 3446 spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED); 3447 return true; 3448 } 3449 } 3450 3451 return false; 3452 } 3453 3454 static bool 3455 bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_io *bio_to_abort) 3456 { 3457 struct spdk_bdev_io *bdev_io; 3458 3459 STAILQ_FOREACH(bdev_io, queue, internal.buf_link) { 3460 if (bdev_io == bio_to_abort) { 3461 STAILQ_REMOVE(queue, bio_to_abort, spdk_bdev_io, internal.buf_link); 3462 spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED); 3463 return true; 3464 } 3465 } 3466 3467 return false; 3468 } 3469 3470 static void 3471 bdev_qos_channel_destroy(void *cb_arg) 3472 { 3473 struct spdk_bdev_qos *qos = cb_arg; 3474 3475 spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch)); 3476 spdk_poller_unregister(&qos->poller); 3477 3478 SPDK_DEBUGLOG(bdev, "Free QoS %p.\n", qos); 3479 3480 free(qos); 3481 } 3482 3483 static int 3484 bdev_qos_destroy(struct spdk_bdev *bdev) 3485 { 3486 int i; 3487 3488 /* 3489 * Cleanly shutting down the QoS poller is tricky, because 3490 * during the asynchronous operation the user could open 3491 * a new descriptor and create a new channel, spawning 3492 * a new QoS poller. 3493 * 3494 * The strategy is to create a new QoS structure here and swap it 3495 * in. The shutdown path then continues to refer to the old one 3496 * until it completes and then releases it. 3497 */ 3498 struct spdk_bdev_qos *new_qos, *old_qos; 3499 3500 old_qos = bdev->internal.qos; 3501 3502 new_qos = calloc(1, sizeof(*new_qos)); 3503 if (!new_qos) { 3504 SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n"); 3505 return -ENOMEM; 3506 } 3507 3508 /* Copy the old QoS data into the newly allocated structure */ 3509 memcpy(new_qos, old_qos, sizeof(*new_qos)); 3510 3511 /* Zero out the key parts of the QoS structure */ 3512 new_qos->ch = NULL; 3513 new_qos->thread = NULL; 3514 new_qos->poller = NULL; 3515 TAILQ_INIT(&new_qos->queued); 3516 /* 3517 * The limit member of spdk_bdev_qos_limit structure is not zeroed. 3518 * It will be used later for the new QoS structure. 3519 */ 3520 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 3521 new_qos->rate_limits[i].remaining_this_timeslice = 0; 3522 new_qos->rate_limits[i].min_per_timeslice = 0; 3523 new_qos->rate_limits[i].max_per_timeslice = 0; 3524 } 3525 3526 bdev->internal.qos = new_qos; 3527 3528 if (old_qos->thread == NULL) { 3529 free(old_qos); 3530 } else { 3531 spdk_thread_send_msg(old_qos->thread, bdev_qos_channel_destroy, old_qos); 3532 } 3533 3534 /* It is safe to continue with destroying the bdev even though the QoS channel hasn't 3535 * been destroyed yet. The destruction path will end up waiting for the final 3536 * channel to be put before it releases resources. */ 3537 3538 return 0; 3539 } 3540 3541 static void 3542 bdev_io_stat_add(struct spdk_bdev_io_stat *total, struct spdk_bdev_io_stat *add) 3543 { 3544 total->bytes_read += add->bytes_read; 3545 total->num_read_ops += add->num_read_ops; 3546 total->bytes_written += add->bytes_written; 3547 total->num_write_ops += add->num_write_ops; 3548 total->bytes_unmapped += add->bytes_unmapped; 3549 total->num_unmap_ops += add->num_unmap_ops; 3550 total->read_latency_ticks += add->read_latency_ticks; 3551 total->write_latency_ticks += add->write_latency_ticks; 3552 total->unmap_latency_ticks += add->unmap_latency_ticks; 3553 } 3554 3555 static void 3556 bdev_channel_destroy(void *io_device, void *ctx_buf) 3557 { 3558 struct spdk_bdev_channel *ch = ctx_buf; 3559 struct spdk_bdev_mgmt_channel *mgmt_ch; 3560 struct spdk_bdev_shared_resource *shared_resource = ch->shared_resource; 3561 3562 SPDK_DEBUGLOG(bdev, "Destroying channel %p for bdev %s on thread %p\n", ch, ch->bdev->name, 3563 spdk_get_thread()); 3564 3565 spdk_trace_record(TRACE_BDEV_IOCH_DESTROY, 0, 0, 0, ch->bdev->name, 3566 spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel))); 3567 3568 /* This channel is going away, so add its statistics into the bdev so that they don't get lost. */ 3569 pthread_mutex_lock(&ch->bdev->internal.mutex); 3570 bdev_io_stat_add(&ch->bdev->internal.stat, &ch->stat); 3571 pthread_mutex_unlock(&ch->bdev->internal.mutex); 3572 3573 mgmt_ch = shared_resource->mgmt_ch; 3574 3575 bdev_abort_all_queued_io(&ch->queued_resets, ch); 3576 bdev_abort_all_queued_io(&shared_resource->nomem_io, ch); 3577 bdev_abort_all_buf_io(&mgmt_ch->need_buf_small, ch); 3578 bdev_abort_all_buf_io(&mgmt_ch->need_buf_large, ch); 3579 3580 if (ch->histogram) { 3581 spdk_histogram_data_free(ch->histogram); 3582 } 3583 3584 bdev_channel_destroy_resource(ch); 3585 } 3586 3587 /* 3588 * If the name already exists in the global bdev name tree, RB_INSERT() returns a pointer 3589 * to it. Hence we do not have to call bdev_get_by_name() when using this function. 3590 */ 3591 static int 3592 bdev_name_add(struct spdk_bdev_name *bdev_name, struct spdk_bdev *bdev, const char *name) 3593 { 3594 struct spdk_bdev_name *tmp; 3595 3596 bdev_name->name = strdup(name); 3597 if (bdev_name->name == NULL) { 3598 SPDK_ERRLOG("Unable to allocate bdev name\n"); 3599 return -ENOMEM; 3600 } 3601 3602 bdev_name->bdev = bdev; 3603 3604 pthread_mutex_lock(&g_bdev_mgr.mutex); 3605 tmp = RB_INSERT(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name); 3606 pthread_mutex_unlock(&g_bdev_mgr.mutex); 3607 3608 if (tmp != NULL) { 3609 SPDK_ERRLOG("Bdev name %s already exists\n", name); 3610 free(bdev_name->name); 3611 return -EEXIST; 3612 } 3613 3614 return 0; 3615 } 3616 3617 static void 3618 bdev_name_del_unsafe(struct spdk_bdev_name *bdev_name) 3619 { 3620 RB_REMOVE(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name); 3621 free(bdev_name->name); 3622 } 3623 3624 static void 3625 bdev_name_del(struct spdk_bdev_name *bdev_name) 3626 { 3627 pthread_mutex_lock(&g_bdev_mgr.mutex); 3628 bdev_name_del_unsafe(bdev_name); 3629 pthread_mutex_unlock(&g_bdev_mgr.mutex); 3630 } 3631 3632 int 3633 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias) 3634 { 3635 struct spdk_bdev_alias *tmp; 3636 int ret; 3637 3638 if (alias == NULL) { 3639 SPDK_ERRLOG("Empty alias passed\n"); 3640 return -EINVAL; 3641 } 3642 3643 tmp = calloc(1, sizeof(*tmp)); 3644 if (tmp == NULL) { 3645 SPDK_ERRLOG("Unable to allocate alias\n"); 3646 return -ENOMEM; 3647 } 3648 3649 ret = bdev_name_add(&tmp->alias, bdev, alias); 3650 if (ret != 0) { 3651 free(tmp); 3652 return ret; 3653 } 3654 3655 TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq); 3656 3657 return 0; 3658 } 3659 3660 static int 3661 bdev_alias_del(struct spdk_bdev *bdev, const char *alias, 3662 void (*alias_del_fn)(struct spdk_bdev_name *n)) 3663 { 3664 struct spdk_bdev_alias *tmp; 3665 3666 TAILQ_FOREACH(tmp, &bdev->aliases, tailq) { 3667 if (strcmp(alias, tmp->alias.name) == 0) { 3668 TAILQ_REMOVE(&bdev->aliases, tmp, tailq); 3669 alias_del_fn(&tmp->alias); 3670 free(tmp); 3671 return 0; 3672 } 3673 } 3674 3675 return -ENOENT; 3676 } 3677 3678 int 3679 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias) 3680 { 3681 int rc; 3682 3683 rc = bdev_alias_del(bdev, alias, bdev_name_del); 3684 if (rc == -ENOENT) { 3685 SPDK_INFOLOG(bdev, "Alias %s does not exist\n", alias); 3686 } 3687 3688 return rc; 3689 } 3690 3691 void 3692 spdk_bdev_alias_del_all(struct spdk_bdev *bdev) 3693 { 3694 struct spdk_bdev_alias *p, *tmp; 3695 3696 TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) { 3697 TAILQ_REMOVE(&bdev->aliases, p, tailq); 3698 bdev_name_del(&p->alias); 3699 free(p); 3700 } 3701 } 3702 3703 struct spdk_io_channel * 3704 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc) 3705 { 3706 return spdk_get_io_channel(__bdev_to_io_dev(spdk_bdev_desc_get_bdev(desc))); 3707 } 3708 3709 void * 3710 spdk_bdev_get_module_ctx(struct spdk_bdev_desc *desc) 3711 { 3712 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3713 void *ctx = NULL; 3714 3715 if (bdev->fn_table->get_module_ctx) { 3716 ctx = bdev->fn_table->get_module_ctx(bdev->ctxt); 3717 } 3718 3719 return ctx; 3720 } 3721 3722 const char * 3723 spdk_bdev_get_module_name(const struct spdk_bdev *bdev) 3724 { 3725 return bdev->module->name; 3726 } 3727 3728 const char * 3729 spdk_bdev_get_name(const struct spdk_bdev *bdev) 3730 { 3731 return bdev->name; 3732 } 3733 3734 const char * 3735 spdk_bdev_get_product_name(const struct spdk_bdev *bdev) 3736 { 3737 return bdev->product_name; 3738 } 3739 3740 const struct spdk_bdev_aliases_list * 3741 spdk_bdev_get_aliases(const struct spdk_bdev *bdev) 3742 { 3743 return &bdev->aliases; 3744 } 3745 3746 uint32_t 3747 spdk_bdev_get_block_size(const struct spdk_bdev *bdev) 3748 { 3749 return bdev->blocklen; 3750 } 3751 3752 uint32_t 3753 spdk_bdev_get_write_unit_size(const struct spdk_bdev *bdev) 3754 { 3755 return bdev->write_unit_size; 3756 } 3757 3758 uint64_t 3759 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev) 3760 { 3761 return bdev->blockcnt; 3762 } 3763 3764 const char * 3765 spdk_bdev_get_qos_rpc_type(enum spdk_bdev_qos_rate_limit_type type) 3766 { 3767 return qos_rpc_type[type]; 3768 } 3769 3770 void 3771 spdk_bdev_get_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits) 3772 { 3773 int i; 3774 3775 memset(limits, 0, sizeof(*limits) * SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES); 3776 3777 pthread_mutex_lock(&bdev->internal.mutex); 3778 if (bdev->internal.qos) { 3779 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 3780 if (bdev->internal.qos->rate_limits[i].limit != 3781 SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 3782 limits[i] = bdev->internal.qos->rate_limits[i].limit; 3783 if (bdev_qos_is_iops_rate_limit(i) == false) { 3784 /* Change from Byte to Megabyte which is user visible. */ 3785 limits[i] = limits[i] / 1024 / 1024; 3786 } 3787 } 3788 } 3789 } 3790 pthread_mutex_unlock(&bdev->internal.mutex); 3791 } 3792 3793 size_t 3794 spdk_bdev_get_buf_align(const struct spdk_bdev *bdev) 3795 { 3796 return 1 << bdev->required_alignment; 3797 } 3798 3799 uint32_t 3800 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev) 3801 { 3802 return bdev->optimal_io_boundary; 3803 } 3804 3805 bool 3806 spdk_bdev_has_write_cache(const struct spdk_bdev *bdev) 3807 { 3808 return bdev->write_cache; 3809 } 3810 3811 const struct spdk_uuid * 3812 spdk_bdev_get_uuid(const struct spdk_bdev *bdev) 3813 { 3814 return &bdev->uuid; 3815 } 3816 3817 uint16_t 3818 spdk_bdev_get_acwu(const struct spdk_bdev *bdev) 3819 { 3820 return bdev->acwu; 3821 } 3822 3823 uint32_t 3824 spdk_bdev_get_md_size(const struct spdk_bdev *bdev) 3825 { 3826 return bdev->md_len; 3827 } 3828 3829 bool 3830 spdk_bdev_is_md_interleaved(const struct spdk_bdev *bdev) 3831 { 3832 return (bdev->md_len != 0) && bdev->md_interleave; 3833 } 3834 3835 bool 3836 spdk_bdev_is_md_separate(const struct spdk_bdev *bdev) 3837 { 3838 return (bdev->md_len != 0) && !bdev->md_interleave; 3839 } 3840 3841 bool 3842 spdk_bdev_is_zoned(const struct spdk_bdev *bdev) 3843 { 3844 return bdev->zoned; 3845 } 3846 3847 uint32_t 3848 spdk_bdev_get_data_block_size(const struct spdk_bdev *bdev) 3849 { 3850 if (spdk_bdev_is_md_interleaved(bdev)) { 3851 return bdev->blocklen - bdev->md_len; 3852 } else { 3853 return bdev->blocklen; 3854 } 3855 } 3856 3857 uint32_t 3858 spdk_bdev_get_physical_block_size(const struct spdk_bdev *bdev) 3859 { 3860 return bdev->phys_blocklen; 3861 } 3862 3863 static uint32_t 3864 _bdev_get_block_size_with_md(const struct spdk_bdev *bdev) 3865 { 3866 if (!spdk_bdev_is_md_interleaved(bdev)) { 3867 return bdev->blocklen + bdev->md_len; 3868 } else { 3869 return bdev->blocklen; 3870 } 3871 } 3872 3873 enum spdk_dif_type spdk_bdev_get_dif_type(const struct spdk_bdev *bdev) 3874 { 3875 if (bdev->md_len != 0) { 3876 return bdev->dif_type; 3877 } else { 3878 return SPDK_DIF_DISABLE; 3879 } 3880 } 3881 3882 bool 3883 spdk_bdev_is_dif_head_of_md(const struct spdk_bdev *bdev) 3884 { 3885 if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 3886 return bdev->dif_is_head_of_md; 3887 } else { 3888 return false; 3889 } 3890 } 3891 3892 bool 3893 spdk_bdev_is_dif_check_enabled(const struct spdk_bdev *bdev, 3894 enum spdk_dif_check_type check_type) 3895 { 3896 if (spdk_bdev_get_dif_type(bdev) == SPDK_DIF_DISABLE) { 3897 return false; 3898 } 3899 3900 switch (check_type) { 3901 case SPDK_DIF_CHECK_TYPE_REFTAG: 3902 return (bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) != 0; 3903 case SPDK_DIF_CHECK_TYPE_APPTAG: 3904 return (bdev->dif_check_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) != 0; 3905 case SPDK_DIF_CHECK_TYPE_GUARD: 3906 return (bdev->dif_check_flags & SPDK_DIF_FLAGS_GUARD_CHECK) != 0; 3907 default: 3908 return false; 3909 } 3910 } 3911 3912 uint64_t 3913 spdk_bdev_get_qd(const struct spdk_bdev *bdev) 3914 { 3915 return bdev->internal.measured_queue_depth; 3916 } 3917 3918 uint64_t 3919 spdk_bdev_get_qd_sampling_period(const struct spdk_bdev *bdev) 3920 { 3921 return bdev->internal.period; 3922 } 3923 3924 uint64_t 3925 spdk_bdev_get_weighted_io_time(const struct spdk_bdev *bdev) 3926 { 3927 return bdev->internal.weighted_io_time; 3928 } 3929 3930 uint64_t 3931 spdk_bdev_get_io_time(const struct spdk_bdev *bdev) 3932 { 3933 return bdev->internal.io_time; 3934 } 3935 3936 static void 3937 _calculate_measured_qd_cpl(struct spdk_io_channel_iter *i, int status) 3938 { 3939 struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i); 3940 3941 bdev->internal.measured_queue_depth = bdev->internal.temporary_queue_depth; 3942 3943 if (bdev->internal.measured_queue_depth) { 3944 bdev->internal.io_time += bdev->internal.period; 3945 bdev->internal.weighted_io_time += bdev->internal.period * bdev->internal.measured_queue_depth; 3946 } 3947 } 3948 3949 static void 3950 _calculate_measured_qd(struct spdk_io_channel_iter *i) 3951 { 3952 struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i); 3953 struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i); 3954 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(io_ch); 3955 3956 bdev->internal.temporary_queue_depth += ch->io_outstanding; 3957 spdk_for_each_channel_continue(i, 0); 3958 } 3959 3960 static int 3961 bdev_calculate_measured_queue_depth(void *ctx) 3962 { 3963 struct spdk_bdev *bdev = ctx; 3964 bdev->internal.temporary_queue_depth = 0; 3965 spdk_for_each_channel(__bdev_to_io_dev(bdev), _calculate_measured_qd, bdev, 3966 _calculate_measured_qd_cpl); 3967 return SPDK_POLLER_BUSY; 3968 } 3969 3970 void 3971 spdk_bdev_set_qd_sampling_period(struct spdk_bdev *bdev, uint64_t period) 3972 { 3973 bdev->internal.period = period; 3974 3975 if (bdev->internal.qd_poller != NULL) { 3976 spdk_poller_unregister(&bdev->internal.qd_poller); 3977 bdev->internal.measured_queue_depth = UINT64_MAX; 3978 } 3979 3980 if (period != 0) { 3981 bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth, bdev, 3982 period); 3983 } 3984 } 3985 3986 static void 3987 _resize_notify(void *arg) 3988 { 3989 struct spdk_bdev_desc *desc = arg; 3990 3991 pthread_mutex_lock(&desc->mutex); 3992 desc->refs--; 3993 if (!desc->closed) { 3994 pthread_mutex_unlock(&desc->mutex); 3995 desc->callback.event_fn(SPDK_BDEV_EVENT_RESIZE, 3996 desc->bdev, 3997 desc->callback.ctx); 3998 return; 3999 } else if (0 == desc->refs) { 4000 /* This descriptor was closed after this resize_notify message was sent. 4001 * spdk_bdev_close() could not free the descriptor since this message was 4002 * in flight, so we free it now using bdev_desc_free(). 4003 */ 4004 pthread_mutex_unlock(&desc->mutex); 4005 bdev_desc_free(desc); 4006 return; 4007 } 4008 pthread_mutex_unlock(&desc->mutex); 4009 } 4010 4011 int 4012 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size) 4013 { 4014 struct spdk_bdev_desc *desc; 4015 int ret; 4016 4017 if (size == bdev->blockcnt) { 4018 return 0; 4019 } 4020 4021 pthread_mutex_lock(&bdev->internal.mutex); 4022 4023 /* bdev has open descriptors */ 4024 if (!TAILQ_EMPTY(&bdev->internal.open_descs) && 4025 bdev->blockcnt > size) { 4026 ret = -EBUSY; 4027 } else { 4028 bdev->blockcnt = size; 4029 TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) { 4030 pthread_mutex_lock(&desc->mutex); 4031 if (!desc->closed) { 4032 desc->refs++; 4033 spdk_thread_send_msg(desc->thread, _resize_notify, desc); 4034 } 4035 pthread_mutex_unlock(&desc->mutex); 4036 } 4037 ret = 0; 4038 } 4039 4040 pthread_mutex_unlock(&bdev->internal.mutex); 4041 4042 return ret; 4043 } 4044 4045 /* 4046 * Convert I/O offset and length from bytes to blocks. 4047 * 4048 * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size. 4049 */ 4050 static uint64_t 4051 bdev_bytes_to_blocks(struct spdk_bdev *bdev, uint64_t offset_bytes, uint64_t *offset_blocks, 4052 uint64_t num_bytes, uint64_t *num_blocks) 4053 { 4054 uint32_t block_size = bdev->blocklen; 4055 uint8_t shift_cnt; 4056 4057 /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */ 4058 if (spdk_likely(spdk_u32_is_pow2(block_size))) { 4059 shift_cnt = spdk_u32log2(block_size); 4060 *offset_blocks = offset_bytes >> shift_cnt; 4061 *num_blocks = num_bytes >> shift_cnt; 4062 return (offset_bytes - (*offset_blocks << shift_cnt)) | 4063 (num_bytes - (*num_blocks << shift_cnt)); 4064 } else { 4065 *offset_blocks = offset_bytes / block_size; 4066 *num_blocks = num_bytes / block_size; 4067 return (offset_bytes % block_size) | (num_bytes % block_size); 4068 } 4069 } 4070 4071 static bool 4072 bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks) 4073 { 4074 /* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there 4075 * has been an overflow and hence the offset has been wrapped around */ 4076 if (offset_blocks + num_blocks < offset_blocks) { 4077 return false; 4078 } 4079 4080 /* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */ 4081 if (offset_blocks + num_blocks > bdev->blockcnt) { 4082 return false; 4083 } 4084 4085 return true; 4086 } 4087 4088 static bool 4089 _bdev_io_check_md_buf(const struct iovec *iovs, const void *md_buf) 4090 { 4091 return _is_buf_allocated(iovs) == (md_buf != NULL); 4092 } 4093 4094 static inline void 4095 _bdev_io_copy_ext_opts(struct spdk_bdev_io *bdev_io, struct spdk_bdev_ext_io_opts *opts) 4096 { 4097 struct spdk_bdev_ext_io_opts *opts_copy = &bdev_io->internal.ext_opts_copy; 4098 4099 memcpy(opts_copy, opts, opts->size); 4100 bdev_io->internal.ext_opts_copy.metadata = bdev_io->u.bdev.md_buf; 4101 /* Save pointer to the copied ext_opts which will be used by bdev modules */ 4102 bdev_io->u.bdev.ext_opts = opts_copy; 4103 } 4104 4105 static inline void 4106 _bdev_io_ext_use_bounce_buffer(struct spdk_bdev_io *bdev_io) 4107 { 4108 /* bdev doesn't support memory domains, thereby buffers in this IO request can't 4109 * be accessed directly. It is needed to allocate buffers before issuing IO operation. 4110 * For write operation we need to pull buffers from memory domain before submitting IO. 4111 * Once read operation completes, we need to use memory_domain push functionality to 4112 * update data in original memory domain IO buffer 4113 * This IO request will go through a regular IO flow, so clear memory domains pointers in 4114 * the copied ext_opts */ 4115 bdev_io->internal.ext_opts_copy.memory_domain = NULL; 4116 bdev_io->internal.ext_opts_copy.memory_domain_ctx = NULL; 4117 _bdev_memory_domain_io_get_buf(bdev_io, _bdev_memory_domain_get_io_cb, 4118 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 4119 } 4120 4121 static int 4122 bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 4123 void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 4124 spdk_bdev_io_completion_cb cb, void *cb_arg) 4125 { 4126 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4127 struct spdk_bdev_io *bdev_io; 4128 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4129 4130 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4131 return -EINVAL; 4132 } 4133 4134 bdev_io = bdev_channel_get_io(channel); 4135 if (!bdev_io) { 4136 return -ENOMEM; 4137 } 4138 4139 bdev_io->internal.ch = channel; 4140 bdev_io->internal.desc = desc; 4141 bdev_io->type = SPDK_BDEV_IO_TYPE_READ; 4142 bdev_io->u.bdev.iovs = &bdev_io->iov; 4143 bdev_io->u.bdev.iovs[0].iov_base = buf; 4144 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 4145 bdev_io->u.bdev.iovcnt = 1; 4146 bdev_io->u.bdev.md_buf = md_buf; 4147 bdev_io->u.bdev.num_blocks = num_blocks; 4148 bdev_io->u.bdev.offset_blocks = offset_blocks; 4149 bdev_io->u.bdev.ext_opts = NULL; 4150 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4151 4152 bdev_io_submit(bdev_io); 4153 return 0; 4154 } 4155 4156 int 4157 spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4158 void *buf, uint64_t offset, uint64_t nbytes, 4159 spdk_bdev_io_completion_cb cb, void *cb_arg) 4160 { 4161 uint64_t offset_blocks, num_blocks; 4162 4163 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 4164 nbytes, &num_blocks) != 0) { 4165 return -EINVAL; 4166 } 4167 4168 return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg); 4169 } 4170 4171 int 4172 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4173 void *buf, uint64_t offset_blocks, uint64_t num_blocks, 4174 spdk_bdev_io_completion_cb cb, void *cb_arg) 4175 { 4176 return bdev_read_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, cb, cb_arg); 4177 } 4178 4179 int 4180 spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4181 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 4182 spdk_bdev_io_completion_cb cb, void *cb_arg) 4183 { 4184 struct iovec iov = { 4185 .iov_base = buf, 4186 }; 4187 4188 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4189 return -EINVAL; 4190 } 4191 4192 if (!_bdev_io_check_md_buf(&iov, md_buf)) { 4193 return -EINVAL; 4194 } 4195 4196 return bdev_read_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks, 4197 cb, cb_arg); 4198 } 4199 4200 int 4201 spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4202 struct iovec *iov, int iovcnt, 4203 uint64_t offset, uint64_t nbytes, 4204 spdk_bdev_io_completion_cb cb, void *cb_arg) 4205 { 4206 uint64_t offset_blocks, num_blocks; 4207 4208 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 4209 nbytes, &num_blocks) != 0) { 4210 return -EINVAL; 4211 } 4212 4213 return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 4214 } 4215 4216 static int 4217 bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4218 struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks, 4219 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg, 4220 struct spdk_bdev_ext_io_opts *opts, bool copy_opts) 4221 { 4222 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4223 struct spdk_bdev_io *bdev_io; 4224 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4225 4226 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4227 return -EINVAL; 4228 } 4229 4230 bdev_io = bdev_channel_get_io(channel); 4231 if (!bdev_io) { 4232 return -ENOMEM; 4233 } 4234 4235 bdev_io->internal.ch = channel; 4236 bdev_io->internal.desc = desc; 4237 bdev_io->type = SPDK_BDEV_IO_TYPE_READ; 4238 bdev_io->u.bdev.iovs = iov; 4239 bdev_io->u.bdev.iovcnt = iovcnt; 4240 bdev_io->u.bdev.md_buf = md_buf; 4241 bdev_io->u.bdev.num_blocks = num_blocks; 4242 bdev_io->u.bdev.offset_blocks = offset_blocks; 4243 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4244 bdev_io->internal.ext_opts = opts; 4245 bdev_io->u.bdev.ext_opts = opts; 4246 4247 if (opts) { 4248 bool use_pull_push = opts->memory_domain && !desc->memory_domains_supported; 4249 4250 assert(opts->size <= sizeof(*opts)); 4251 if (copy_opts || use_pull_push) { 4252 _bdev_io_copy_ext_opts(bdev_io, opts); 4253 if (use_pull_push) { 4254 _bdev_io_ext_use_bounce_buffer(bdev_io); 4255 return 0; 4256 } 4257 } 4258 } 4259 4260 bdev_io_submit(bdev_io); 4261 4262 return 0; 4263 } 4264 4265 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4266 struct iovec *iov, int iovcnt, 4267 uint64_t offset_blocks, uint64_t num_blocks, 4268 spdk_bdev_io_completion_cb cb, void *cb_arg) 4269 { 4270 return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks, 4271 num_blocks, cb, cb_arg, NULL, false); 4272 } 4273 4274 int 4275 spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4276 struct iovec *iov, int iovcnt, void *md_buf, 4277 uint64_t offset_blocks, uint64_t num_blocks, 4278 spdk_bdev_io_completion_cb cb, void *cb_arg) 4279 { 4280 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4281 return -EINVAL; 4282 } 4283 4284 if (!_bdev_io_check_md_buf(iov, md_buf)) { 4285 return -EINVAL; 4286 } 4287 4288 return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks, 4289 num_blocks, cb, cb_arg, NULL, false); 4290 } 4291 4292 int 4293 spdk_bdev_readv_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4294 struct iovec *iov, int iovcnt, 4295 uint64_t offset_blocks, uint64_t num_blocks, 4296 spdk_bdev_io_completion_cb cb, void *cb_arg, 4297 struct spdk_bdev_ext_io_opts *opts) 4298 { 4299 void *md = NULL; 4300 4301 if (opts) { 4302 if (spdk_unlikely(!opts->size || opts->size > sizeof(struct spdk_bdev_ext_io_opts))) { 4303 return -EINVAL; 4304 } 4305 if (spdk_unlikely(opts->memory_domain && !(iov && iov[0].iov_base))) { 4306 /* When memory domain is used, the user must provide data buffers */ 4307 return -EINVAL; 4308 } 4309 md = opts->metadata; 4310 } 4311 4312 if (md && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4313 return -EINVAL; 4314 } 4315 4316 if (md && !_bdev_io_check_md_buf(iov, md)) { 4317 return -EINVAL; 4318 } 4319 4320 return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks, 4321 num_blocks, cb, cb_arg, opts, false); 4322 } 4323 4324 static int 4325 bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4326 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 4327 spdk_bdev_io_completion_cb cb, void *cb_arg) 4328 { 4329 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4330 struct spdk_bdev_io *bdev_io; 4331 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4332 4333 if (!desc->write) { 4334 return -EBADF; 4335 } 4336 4337 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4338 return -EINVAL; 4339 } 4340 4341 bdev_io = bdev_channel_get_io(channel); 4342 if (!bdev_io) { 4343 return -ENOMEM; 4344 } 4345 4346 bdev_io->internal.ch = channel; 4347 bdev_io->internal.desc = desc; 4348 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE; 4349 bdev_io->u.bdev.iovs = &bdev_io->iov; 4350 bdev_io->u.bdev.iovs[0].iov_base = buf; 4351 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 4352 bdev_io->u.bdev.iovcnt = 1; 4353 bdev_io->u.bdev.md_buf = md_buf; 4354 bdev_io->u.bdev.num_blocks = num_blocks; 4355 bdev_io->u.bdev.offset_blocks = offset_blocks; 4356 bdev_io->u.bdev.ext_opts = NULL; 4357 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4358 4359 bdev_io_submit(bdev_io); 4360 return 0; 4361 } 4362 4363 int 4364 spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4365 void *buf, uint64_t offset, uint64_t nbytes, 4366 spdk_bdev_io_completion_cb cb, void *cb_arg) 4367 { 4368 uint64_t offset_blocks, num_blocks; 4369 4370 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 4371 nbytes, &num_blocks) != 0) { 4372 return -EINVAL; 4373 } 4374 4375 return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg); 4376 } 4377 4378 int 4379 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4380 void *buf, uint64_t offset_blocks, uint64_t num_blocks, 4381 spdk_bdev_io_completion_cb cb, void *cb_arg) 4382 { 4383 return bdev_write_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, 4384 cb, cb_arg); 4385 } 4386 4387 int 4388 spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4389 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 4390 spdk_bdev_io_completion_cb cb, void *cb_arg) 4391 { 4392 struct iovec iov = { 4393 .iov_base = buf, 4394 }; 4395 4396 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4397 return -EINVAL; 4398 } 4399 4400 if (!_bdev_io_check_md_buf(&iov, md_buf)) { 4401 return -EINVAL; 4402 } 4403 4404 return bdev_write_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks, 4405 cb, cb_arg); 4406 } 4407 4408 static int 4409 bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4410 struct iovec *iov, int iovcnt, void *md_buf, 4411 uint64_t offset_blocks, uint64_t num_blocks, 4412 spdk_bdev_io_completion_cb cb, void *cb_arg, 4413 struct spdk_bdev_ext_io_opts *opts, bool copy_opts) 4414 { 4415 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4416 struct spdk_bdev_io *bdev_io; 4417 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4418 4419 if (!desc->write) { 4420 return -EBADF; 4421 } 4422 4423 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4424 return -EINVAL; 4425 } 4426 4427 bdev_io = bdev_channel_get_io(channel); 4428 if (!bdev_io) { 4429 return -ENOMEM; 4430 } 4431 4432 bdev_io->internal.ch = channel; 4433 bdev_io->internal.desc = desc; 4434 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE; 4435 bdev_io->u.bdev.iovs = iov; 4436 bdev_io->u.bdev.iovcnt = iovcnt; 4437 bdev_io->u.bdev.md_buf = md_buf; 4438 bdev_io->u.bdev.num_blocks = num_blocks; 4439 bdev_io->u.bdev.offset_blocks = offset_blocks; 4440 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4441 bdev_io->internal.ext_opts = opts; 4442 bdev_io->u.bdev.ext_opts = opts; 4443 4444 if (opts) { 4445 bool use_pull_push = opts->memory_domain && !desc->memory_domains_supported; 4446 4447 assert(opts->size <= sizeof(*opts)); 4448 if (copy_opts || use_pull_push) { 4449 _bdev_io_copy_ext_opts(bdev_io, opts); 4450 if (use_pull_push) { 4451 _bdev_io_ext_use_bounce_buffer(bdev_io); 4452 return 0; 4453 } 4454 } 4455 } 4456 4457 bdev_io_submit(bdev_io); 4458 4459 return 0; 4460 } 4461 4462 int 4463 spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4464 struct iovec *iov, int iovcnt, 4465 uint64_t offset, uint64_t len, 4466 spdk_bdev_io_completion_cb cb, void *cb_arg) 4467 { 4468 uint64_t offset_blocks, num_blocks; 4469 4470 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 4471 len, &num_blocks) != 0) { 4472 return -EINVAL; 4473 } 4474 4475 return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 4476 } 4477 4478 int 4479 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4480 struct iovec *iov, int iovcnt, 4481 uint64_t offset_blocks, uint64_t num_blocks, 4482 spdk_bdev_io_completion_cb cb, void *cb_arg) 4483 { 4484 return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks, 4485 num_blocks, cb, cb_arg, NULL, false); 4486 } 4487 4488 int 4489 spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4490 struct iovec *iov, int iovcnt, void *md_buf, 4491 uint64_t offset_blocks, uint64_t num_blocks, 4492 spdk_bdev_io_completion_cb cb, void *cb_arg) 4493 { 4494 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4495 return -EINVAL; 4496 } 4497 4498 if (!_bdev_io_check_md_buf(iov, md_buf)) { 4499 return -EINVAL; 4500 } 4501 4502 return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks, 4503 num_blocks, cb, cb_arg, NULL, false); 4504 } 4505 4506 int 4507 spdk_bdev_writev_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4508 struct iovec *iov, int iovcnt, 4509 uint64_t offset_blocks, uint64_t num_blocks, 4510 spdk_bdev_io_completion_cb cb, void *cb_arg, 4511 struct spdk_bdev_ext_io_opts *opts) 4512 { 4513 void *md = NULL; 4514 4515 if (opts) { 4516 if (spdk_unlikely(!opts->size || opts->size > sizeof(struct spdk_bdev_ext_io_opts))) { 4517 return -EINVAL; 4518 } 4519 if (spdk_unlikely(opts->memory_domain && !(iov && iov[0].iov_base))) { 4520 /* When memory domain is used, the user must provide data buffers */ 4521 return -EINVAL; 4522 } 4523 md = opts->metadata; 4524 } 4525 4526 if (md && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4527 return -EINVAL; 4528 } 4529 4530 if (md && !_bdev_io_check_md_buf(iov, md)) { 4531 return -EINVAL; 4532 } 4533 4534 return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks, 4535 num_blocks, cb, cb_arg, opts, false); 4536 } 4537 4538 static void 4539 bdev_compare_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 4540 { 4541 struct spdk_bdev_io *parent_io = cb_arg; 4542 uint8_t *read_buf = bdev_io->u.bdev.iovs[0].iov_base; 4543 int i, rc = 0; 4544 4545 if (!success) { 4546 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 4547 parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx); 4548 spdk_bdev_free_io(bdev_io); 4549 return; 4550 } 4551 4552 for (i = 0; i < parent_io->u.bdev.iovcnt; i++) { 4553 rc = memcmp(read_buf, 4554 parent_io->u.bdev.iovs[i].iov_base, 4555 parent_io->u.bdev.iovs[i].iov_len); 4556 if (rc) { 4557 break; 4558 } 4559 read_buf += parent_io->u.bdev.iovs[i].iov_len; 4560 } 4561 4562 spdk_bdev_free_io(bdev_io); 4563 4564 if (rc == 0) { 4565 parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 4566 parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx); 4567 } else { 4568 parent_io->internal.status = SPDK_BDEV_IO_STATUS_MISCOMPARE; 4569 parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx); 4570 } 4571 } 4572 4573 static void 4574 bdev_compare_do_read(void *_bdev_io) 4575 { 4576 struct spdk_bdev_io *bdev_io = _bdev_io; 4577 int rc; 4578 4579 rc = spdk_bdev_read_blocks(bdev_io->internal.desc, 4580 spdk_io_channel_from_ctx(bdev_io->internal.ch), NULL, 4581 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks, 4582 bdev_compare_do_read_done, bdev_io); 4583 4584 if (rc == -ENOMEM) { 4585 bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_do_read); 4586 } else if (rc != 0) { 4587 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 4588 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 4589 } 4590 } 4591 4592 static int 4593 bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4594 struct iovec *iov, int iovcnt, void *md_buf, 4595 uint64_t offset_blocks, uint64_t num_blocks, 4596 spdk_bdev_io_completion_cb cb, void *cb_arg) 4597 { 4598 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4599 struct spdk_bdev_io *bdev_io; 4600 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4601 4602 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4603 return -EINVAL; 4604 } 4605 4606 bdev_io = bdev_channel_get_io(channel); 4607 if (!bdev_io) { 4608 return -ENOMEM; 4609 } 4610 4611 bdev_io->internal.ch = channel; 4612 bdev_io->internal.desc = desc; 4613 bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE; 4614 bdev_io->u.bdev.iovs = iov; 4615 bdev_io->u.bdev.iovcnt = iovcnt; 4616 bdev_io->u.bdev.md_buf = md_buf; 4617 bdev_io->u.bdev.num_blocks = num_blocks; 4618 bdev_io->u.bdev.offset_blocks = offset_blocks; 4619 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4620 4621 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) { 4622 bdev_io_submit(bdev_io); 4623 return 0; 4624 } 4625 4626 bdev_compare_do_read(bdev_io); 4627 4628 return 0; 4629 } 4630 4631 int 4632 spdk_bdev_comparev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4633 struct iovec *iov, int iovcnt, 4634 uint64_t offset_blocks, uint64_t num_blocks, 4635 spdk_bdev_io_completion_cb cb, void *cb_arg) 4636 { 4637 return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks, 4638 num_blocks, cb, cb_arg); 4639 } 4640 4641 int 4642 spdk_bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4643 struct iovec *iov, int iovcnt, void *md_buf, 4644 uint64_t offset_blocks, uint64_t num_blocks, 4645 spdk_bdev_io_completion_cb cb, void *cb_arg) 4646 { 4647 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4648 return -EINVAL; 4649 } 4650 4651 if (!_bdev_io_check_md_buf(iov, md_buf)) { 4652 return -EINVAL; 4653 } 4654 4655 return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks, 4656 num_blocks, cb, cb_arg); 4657 } 4658 4659 static int 4660 bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4661 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 4662 spdk_bdev_io_completion_cb cb, void *cb_arg) 4663 { 4664 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4665 struct spdk_bdev_io *bdev_io; 4666 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4667 4668 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4669 return -EINVAL; 4670 } 4671 4672 bdev_io = bdev_channel_get_io(channel); 4673 if (!bdev_io) { 4674 return -ENOMEM; 4675 } 4676 4677 bdev_io->internal.ch = channel; 4678 bdev_io->internal.desc = desc; 4679 bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE; 4680 bdev_io->u.bdev.iovs = &bdev_io->iov; 4681 bdev_io->u.bdev.iovs[0].iov_base = buf; 4682 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 4683 bdev_io->u.bdev.iovcnt = 1; 4684 bdev_io->u.bdev.md_buf = md_buf; 4685 bdev_io->u.bdev.num_blocks = num_blocks; 4686 bdev_io->u.bdev.offset_blocks = offset_blocks; 4687 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4688 4689 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) { 4690 bdev_io_submit(bdev_io); 4691 return 0; 4692 } 4693 4694 bdev_compare_do_read(bdev_io); 4695 4696 return 0; 4697 } 4698 4699 int 4700 spdk_bdev_compare_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4701 void *buf, uint64_t offset_blocks, uint64_t num_blocks, 4702 spdk_bdev_io_completion_cb cb, void *cb_arg) 4703 { 4704 return bdev_compare_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, 4705 cb, cb_arg); 4706 } 4707 4708 int 4709 spdk_bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4710 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 4711 spdk_bdev_io_completion_cb cb, void *cb_arg) 4712 { 4713 struct iovec iov = { 4714 .iov_base = buf, 4715 }; 4716 4717 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 4718 return -EINVAL; 4719 } 4720 4721 if (!_bdev_io_check_md_buf(&iov, md_buf)) { 4722 return -EINVAL; 4723 } 4724 4725 return bdev_compare_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks, 4726 cb, cb_arg); 4727 } 4728 4729 static void 4730 bdev_comparev_and_writev_blocks_unlocked(void *ctx, int unlock_status) 4731 { 4732 struct spdk_bdev_io *bdev_io = ctx; 4733 4734 if (unlock_status) { 4735 SPDK_ERRLOG("LBA range unlock failed\n"); 4736 } 4737 4738 bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS ? true : 4739 false, bdev_io->internal.caller_ctx); 4740 } 4741 4742 static void 4743 bdev_comparev_and_writev_blocks_unlock(struct spdk_bdev_io *bdev_io, int status) 4744 { 4745 bdev_io->internal.status = status; 4746 4747 bdev_unlock_lba_range(bdev_io->internal.desc, spdk_io_channel_from_ctx(bdev_io->internal.ch), 4748 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks, 4749 bdev_comparev_and_writev_blocks_unlocked, bdev_io); 4750 } 4751 4752 static void 4753 bdev_compare_and_write_do_write_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 4754 { 4755 struct spdk_bdev_io *parent_io = cb_arg; 4756 4757 if (!success) { 4758 SPDK_ERRLOG("Compare and write operation failed\n"); 4759 } 4760 4761 spdk_bdev_free_io(bdev_io); 4762 4763 bdev_comparev_and_writev_blocks_unlock(parent_io, 4764 success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED); 4765 } 4766 4767 static void 4768 bdev_compare_and_write_do_write(void *_bdev_io) 4769 { 4770 struct spdk_bdev_io *bdev_io = _bdev_io; 4771 int rc; 4772 4773 rc = spdk_bdev_writev_blocks(bdev_io->internal.desc, 4774 spdk_io_channel_from_ctx(bdev_io->internal.ch), 4775 bdev_io->u.bdev.fused_iovs, bdev_io->u.bdev.fused_iovcnt, 4776 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks, 4777 bdev_compare_and_write_do_write_done, bdev_io); 4778 4779 4780 if (rc == -ENOMEM) { 4781 bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_write); 4782 } else if (rc != 0) { 4783 bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 4784 } 4785 } 4786 4787 static void 4788 bdev_compare_and_write_do_compare_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 4789 { 4790 struct spdk_bdev_io *parent_io = cb_arg; 4791 4792 spdk_bdev_free_io(bdev_io); 4793 4794 if (!success) { 4795 bdev_comparev_and_writev_blocks_unlock(parent_io, SPDK_BDEV_IO_STATUS_MISCOMPARE); 4796 return; 4797 } 4798 4799 bdev_compare_and_write_do_write(parent_io); 4800 } 4801 4802 static void 4803 bdev_compare_and_write_do_compare(void *_bdev_io) 4804 { 4805 struct spdk_bdev_io *bdev_io = _bdev_io; 4806 int rc; 4807 4808 rc = spdk_bdev_comparev_blocks(bdev_io->internal.desc, 4809 spdk_io_channel_from_ctx(bdev_io->internal.ch), bdev_io->u.bdev.iovs, 4810 bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks, 4811 bdev_compare_and_write_do_compare_done, bdev_io); 4812 4813 if (rc == -ENOMEM) { 4814 bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_compare); 4815 } else if (rc != 0) { 4816 bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED); 4817 } 4818 } 4819 4820 static void 4821 bdev_comparev_and_writev_blocks_locked(void *ctx, int status) 4822 { 4823 struct spdk_bdev_io *bdev_io = ctx; 4824 4825 if (status) { 4826 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED; 4827 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 4828 return; 4829 } 4830 4831 bdev_compare_and_write_do_compare(bdev_io); 4832 } 4833 4834 int 4835 spdk_bdev_comparev_and_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4836 struct iovec *compare_iov, int compare_iovcnt, 4837 struct iovec *write_iov, int write_iovcnt, 4838 uint64_t offset_blocks, uint64_t num_blocks, 4839 spdk_bdev_io_completion_cb cb, void *cb_arg) 4840 { 4841 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4842 struct spdk_bdev_io *bdev_io; 4843 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4844 4845 if (!desc->write) { 4846 return -EBADF; 4847 } 4848 4849 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4850 return -EINVAL; 4851 } 4852 4853 if (num_blocks > bdev->acwu) { 4854 return -EINVAL; 4855 } 4856 4857 bdev_io = bdev_channel_get_io(channel); 4858 if (!bdev_io) { 4859 return -ENOMEM; 4860 } 4861 4862 bdev_io->internal.ch = channel; 4863 bdev_io->internal.desc = desc; 4864 bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE; 4865 bdev_io->u.bdev.iovs = compare_iov; 4866 bdev_io->u.bdev.iovcnt = compare_iovcnt; 4867 bdev_io->u.bdev.fused_iovs = write_iov; 4868 bdev_io->u.bdev.fused_iovcnt = write_iovcnt; 4869 bdev_io->u.bdev.md_buf = NULL; 4870 bdev_io->u.bdev.num_blocks = num_blocks; 4871 bdev_io->u.bdev.offset_blocks = offset_blocks; 4872 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4873 4874 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE)) { 4875 bdev_io_submit(bdev_io); 4876 return 0; 4877 } 4878 4879 return bdev_lock_lba_range(desc, ch, offset_blocks, num_blocks, 4880 bdev_comparev_and_writev_blocks_locked, bdev_io); 4881 } 4882 4883 int 4884 spdk_bdev_zcopy_start(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4885 struct iovec *iov, int iovcnt, 4886 uint64_t offset_blocks, uint64_t num_blocks, 4887 bool populate, 4888 spdk_bdev_io_completion_cb cb, void *cb_arg) 4889 { 4890 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4891 struct spdk_bdev_io *bdev_io; 4892 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4893 4894 if (!desc->write) { 4895 return -EBADF; 4896 } 4897 4898 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4899 return -EINVAL; 4900 } 4901 4902 if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) { 4903 return -ENOTSUP; 4904 } 4905 4906 bdev_io = bdev_channel_get_io(channel); 4907 if (!bdev_io) { 4908 return -ENOMEM; 4909 } 4910 4911 bdev_io->internal.ch = channel; 4912 bdev_io->internal.desc = desc; 4913 bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY; 4914 bdev_io->u.bdev.num_blocks = num_blocks; 4915 bdev_io->u.bdev.offset_blocks = offset_blocks; 4916 bdev_io->u.bdev.iovs = iov; 4917 bdev_io->u.bdev.iovcnt = iovcnt; 4918 bdev_io->u.bdev.md_buf = NULL; 4919 bdev_io->u.bdev.zcopy.populate = populate ? 1 : 0; 4920 bdev_io->u.bdev.zcopy.commit = 0; 4921 bdev_io->u.bdev.zcopy.start = 1; 4922 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4923 4924 bdev_io_submit(bdev_io); 4925 4926 return 0; 4927 } 4928 4929 int 4930 spdk_bdev_zcopy_end(struct spdk_bdev_io *bdev_io, bool commit, 4931 spdk_bdev_io_completion_cb cb, void *cb_arg) 4932 { 4933 if (bdev_io->type != SPDK_BDEV_IO_TYPE_ZCOPY) { 4934 return -EINVAL; 4935 } 4936 4937 bdev_io->u.bdev.zcopy.commit = commit ? 1 : 0; 4938 bdev_io->u.bdev.zcopy.start = 0; 4939 bdev_io->internal.caller_ctx = cb_arg; 4940 bdev_io->internal.cb = cb; 4941 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 4942 4943 bdev_io_submit(bdev_io); 4944 4945 return 0; 4946 } 4947 4948 int 4949 spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4950 uint64_t offset, uint64_t len, 4951 spdk_bdev_io_completion_cb cb, void *cb_arg) 4952 { 4953 uint64_t offset_blocks, num_blocks; 4954 4955 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 4956 len, &num_blocks) != 0) { 4957 return -EINVAL; 4958 } 4959 4960 return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg); 4961 } 4962 4963 int 4964 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4965 uint64_t offset_blocks, uint64_t num_blocks, 4966 spdk_bdev_io_completion_cb cb, void *cb_arg) 4967 { 4968 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4969 struct spdk_bdev_io *bdev_io; 4970 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4971 4972 if (!desc->write) { 4973 return -EBADF; 4974 } 4975 4976 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 4977 return -EINVAL; 4978 } 4979 4980 if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) && 4981 !bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)) { 4982 return -ENOTSUP; 4983 } 4984 4985 bdev_io = bdev_channel_get_io(channel); 4986 4987 if (!bdev_io) { 4988 return -ENOMEM; 4989 } 4990 4991 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES; 4992 bdev_io->internal.ch = channel; 4993 bdev_io->internal.desc = desc; 4994 bdev_io->u.bdev.offset_blocks = offset_blocks; 4995 bdev_io->u.bdev.num_blocks = num_blocks; 4996 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4997 4998 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) { 4999 bdev_io_submit(bdev_io); 5000 return 0; 5001 } 5002 5003 assert(bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)); 5004 assert(_bdev_get_block_size_with_md(bdev) <= ZERO_BUFFER_SIZE); 5005 bdev_io->u.bdev.split_remaining_num_blocks = num_blocks; 5006 bdev_io->u.bdev.split_current_offset_blocks = offset_blocks; 5007 bdev_write_zero_buffer_next(bdev_io); 5008 5009 return 0; 5010 } 5011 5012 int 5013 spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5014 uint64_t offset, uint64_t nbytes, 5015 spdk_bdev_io_completion_cb cb, void *cb_arg) 5016 { 5017 uint64_t offset_blocks, num_blocks; 5018 5019 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 5020 nbytes, &num_blocks) != 0) { 5021 return -EINVAL; 5022 } 5023 5024 return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg); 5025 } 5026 5027 int 5028 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5029 uint64_t offset_blocks, uint64_t num_blocks, 5030 spdk_bdev_io_completion_cb cb, void *cb_arg) 5031 { 5032 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5033 struct spdk_bdev_io *bdev_io; 5034 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5035 5036 if (!desc->write) { 5037 return -EBADF; 5038 } 5039 5040 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 5041 return -EINVAL; 5042 } 5043 5044 if (num_blocks == 0) { 5045 SPDK_ERRLOG("Can't unmap 0 bytes\n"); 5046 return -EINVAL; 5047 } 5048 5049 bdev_io = bdev_channel_get_io(channel); 5050 if (!bdev_io) { 5051 return -ENOMEM; 5052 } 5053 5054 bdev_io->internal.ch = channel; 5055 bdev_io->internal.desc = desc; 5056 bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP; 5057 5058 bdev_io->u.bdev.iovs = &bdev_io->iov; 5059 bdev_io->u.bdev.iovs[0].iov_base = NULL; 5060 bdev_io->u.bdev.iovs[0].iov_len = 0; 5061 bdev_io->u.bdev.iovcnt = 1; 5062 5063 bdev_io->u.bdev.offset_blocks = offset_blocks; 5064 bdev_io->u.bdev.num_blocks = num_blocks; 5065 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5066 5067 bdev_io_submit(bdev_io); 5068 return 0; 5069 } 5070 5071 int 5072 spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5073 uint64_t offset, uint64_t length, 5074 spdk_bdev_io_completion_cb cb, void *cb_arg) 5075 { 5076 uint64_t offset_blocks, num_blocks; 5077 5078 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 5079 length, &num_blocks) != 0) { 5080 return -EINVAL; 5081 } 5082 5083 return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg); 5084 } 5085 5086 int 5087 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5088 uint64_t offset_blocks, uint64_t num_blocks, 5089 spdk_bdev_io_completion_cb cb, void *cb_arg) 5090 { 5091 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5092 struct spdk_bdev_io *bdev_io; 5093 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5094 5095 if (!desc->write) { 5096 return -EBADF; 5097 } 5098 5099 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 5100 return -EINVAL; 5101 } 5102 5103 bdev_io = bdev_channel_get_io(channel); 5104 if (!bdev_io) { 5105 return -ENOMEM; 5106 } 5107 5108 bdev_io->internal.ch = channel; 5109 bdev_io->internal.desc = desc; 5110 bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH; 5111 bdev_io->u.bdev.iovs = NULL; 5112 bdev_io->u.bdev.iovcnt = 0; 5113 bdev_io->u.bdev.offset_blocks = offset_blocks; 5114 bdev_io->u.bdev.num_blocks = num_blocks; 5115 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5116 5117 bdev_io_submit(bdev_io); 5118 return 0; 5119 } 5120 5121 static void 5122 bdev_reset_dev(struct spdk_io_channel_iter *i, int status) 5123 { 5124 struct spdk_bdev_channel *ch = spdk_io_channel_iter_get_ctx(i); 5125 struct spdk_bdev_io *bdev_io; 5126 5127 bdev_io = TAILQ_FIRST(&ch->queued_resets); 5128 TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link); 5129 bdev_io_submit_reset(bdev_io); 5130 } 5131 5132 static void 5133 bdev_reset_freeze_channel(struct spdk_io_channel_iter *i) 5134 { 5135 struct spdk_io_channel *ch; 5136 struct spdk_bdev_channel *channel; 5137 struct spdk_bdev_mgmt_channel *mgmt_channel; 5138 struct spdk_bdev_shared_resource *shared_resource; 5139 bdev_io_tailq_t tmp_queued; 5140 5141 TAILQ_INIT(&tmp_queued); 5142 5143 ch = spdk_io_channel_iter_get_channel(i); 5144 channel = spdk_io_channel_get_ctx(ch); 5145 shared_resource = channel->shared_resource; 5146 mgmt_channel = shared_resource->mgmt_ch; 5147 5148 channel->flags |= BDEV_CH_RESET_IN_PROGRESS; 5149 5150 if ((channel->flags & BDEV_CH_QOS_ENABLED) != 0) { 5151 /* The QoS object is always valid and readable while 5152 * the channel flag is set, so the lock here should not 5153 * be necessary. We're not in the fast path though, so 5154 * just take it anyway. */ 5155 pthread_mutex_lock(&channel->bdev->internal.mutex); 5156 if (channel->bdev->internal.qos->ch == channel) { 5157 TAILQ_SWAP(&channel->bdev->internal.qos->queued, &tmp_queued, spdk_bdev_io, internal.link); 5158 } 5159 pthread_mutex_unlock(&channel->bdev->internal.mutex); 5160 } 5161 5162 bdev_abort_all_queued_io(&shared_resource->nomem_io, channel); 5163 bdev_abort_all_buf_io(&mgmt_channel->need_buf_small, channel); 5164 bdev_abort_all_buf_io(&mgmt_channel->need_buf_large, channel); 5165 bdev_abort_all_queued_io(&tmp_queued, channel); 5166 5167 spdk_for_each_channel_continue(i, 0); 5168 } 5169 5170 static void 5171 bdev_start_reset(void *ctx) 5172 { 5173 struct spdk_bdev_channel *ch = ctx; 5174 5175 spdk_for_each_channel(__bdev_to_io_dev(ch->bdev), bdev_reset_freeze_channel, 5176 ch, bdev_reset_dev); 5177 } 5178 5179 static void 5180 bdev_channel_start_reset(struct spdk_bdev_channel *ch) 5181 { 5182 struct spdk_bdev *bdev = ch->bdev; 5183 5184 assert(!TAILQ_EMPTY(&ch->queued_resets)); 5185 5186 pthread_mutex_lock(&bdev->internal.mutex); 5187 if (bdev->internal.reset_in_progress == NULL) { 5188 bdev->internal.reset_in_progress = TAILQ_FIRST(&ch->queued_resets); 5189 /* 5190 * Take a channel reference for the target bdev for the life of this 5191 * reset. This guards against the channel getting destroyed while 5192 * spdk_for_each_channel() calls related to this reset IO are in 5193 * progress. We will release the reference when this reset is 5194 * completed. 5195 */ 5196 bdev->internal.reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev)); 5197 bdev_start_reset(ch); 5198 } 5199 pthread_mutex_unlock(&bdev->internal.mutex); 5200 } 5201 5202 int 5203 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5204 spdk_bdev_io_completion_cb cb, void *cb_arg) 5205 { 5206 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5207 struct spdk_bdev_io *bdev_io; 5208 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5209 5210 bdev_io = bdev_channel_get_io(channel); 5211 if (!bdev_io) { 5212 return -ENOMEM; 5213 } 5214 5215 bdev_io->internal.ch = channel; 5216 bdev_io->internal.desc = desc; 5217 bdev_io->internal.submit_tsc = spdk_get_ticks(); 5218 bdev_io->type = SPDK_BDEV_IO_TYPE_RESET; 5219 bdev_io->u.reset.ch_ref = NULL; 5220 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5221 5222 pthread_mutex_lock(&bdev->internal.mutex); 5223 TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, internal.link); 5224 pthread_mutex_unlock(&bdev->internal.mutex); 5225 5226 TAILQ_INSERT_TAIL(&bdev_io->internal.ch->io_submitted, bdev_io, 5227 internal.ch_link); 5228 5229 bdev_channel_start_reset(channel); 5230 5231 return 0; 5232 } 5233 5234 void 5235 spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch, 5236 struct spdk_bdev_io_stat *stat) 5237 { 5238 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5239 5240 *stat = channel->stat; 5241 } 5242 5243 static void 5244 bdev_get_device_stat_done(struct spdk_io_channel_iter *i, int status) 5245 { 5246 void *io_device = spdk_io_channel_iter_get_io_device(i); 5247 struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i); 5248 5249 bdev_iostat_ctx->cb(__bdev_from_io_dev(io_device), bdev_iostat_ctx->stat, 5250 bdev_iostat_ctx->cb_arg, 0); 5251 free(bdev_iostat_ctx); 5252 } 5253 5254 static void 5255 bdev_get_each_channel_stat(struct spdk_io_channel_iter *i) 5256 { 5257 struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i); 5258 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 5259 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5260 5261 bdev_io_stat_add(bdev_iostat_ctx->stat, &channel->stat); 5262 spdk_for_each_channel_continue(i, 0); 5263 } 5264 5265 void 5266 spdk_bdev_get_device_stat(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat, 5267 spdk_bdev_get_device_stat_cb cb, void *cb_arg) 5268 { 5269 struct spdk_bdev_iostat_ctx *bdev_iostat_ctx; 5270 5271 assert(bdev != NULL); 5272 assert(stat != NULL); 5273 assert(cb != NULL); 5274 5275 bdev_iostat_ctx = calloc(1, sizeof(struct spdk_bdev_iostat_ctx)); 5276 if (bdev_iostat_ctx == NULL) { 5277 SPDK_ERRLOG("Unable to allocate memory for spdk_bdev_iostat_ctx\n"); 5278 cb(bdev, stat, cb_arg, -ENOMEM); 5279 return; 5280 } 5281 5282 bdev_iostat_ctx->stat = stat; 5283 bdev_iostat_ctx->cb = cb; 5284 bdev_iostat_ctx->cb_arg = cb_arg; 5285 5286 /* Start with the statistics from previously deleted channels. */ 5287 pthread_mutex_lock(&bdev->internal.mutex); 5288 bdev_io_stat_add(bdev_iostat_ctx->stat, &bdev->internal.stat); 5289 pthread_mutex_unlock(&bdev->internal.mutex); 5290 5291 /* Then iterate and add the statistics from each existing channel. */ 5292 spdk_for_each_channel(__bdev_to_io_dev(bdev), 5293 bdev_get_each_channel_stat, 5294 bdev_iostat_ctx, 5295 bdev_get_device_stat_done); 5296 } 5297 5298 int 5299 spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5300 const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, 5301 spdk_bdev_io_completion_cb cb, void *cb_arg) 5302 { 5303 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5304 struct spdk_bdev_io *bdev_io; 5305 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5306 5307 if (!desc->write) { 5308 return -EBADF; 5309 } 5310 5311 if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN))) { 5312 return -ENOTSUP; 5313 } 5314 5315 bdev_io = bdev_channel_get_io(channel); 5316 if (!bdev_io) { 5317 return -ENOMEM; 5318 } 5319 5320 bdev_io->internal.ch = channel; 5321 bdev_io->internal.desc = desc; 5322 bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN; 5323 bdev_io->u.nvme_passthru.cmd = *cmd; 5324 bdev_io->u.nvme_passthru.buf = buf; 5325 bdev_io->u.nvme_passthru.nbytes = nbytes; 5326 bdev_io->u.nvme_passthru.md_buf = NULL; 5327 bdev_io->u.nvme_passthru.md_len = 0; 5328 5329 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5330 5331 bdev_io_submit(bdev_io); 5332 return 0; 5333 } 5334 5335 int 5336 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5337 const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, 5338 spdk_bdev_io_completion_cb cb, void *cb_arg) 5339 { 5340 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5341 struct spdk_bdev_io *bdev_io; 5342 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5343 5344 if (!desc->write) { 5345 /* 5346 * Do not try to parse the NVMe command - we could maybe use bits in the opcode 5347 * to easily determine if the command is a read or write, but for now just 5348 * do not allow io_passthru with a read-only descriptor. 5349 */ 5350 return -EBADF; 5351 } 5352 5353 if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO))) { 5354 return -ENOTSUP; 5355 } 5356 5357 bdev_io = bdev_channel_get_io(channel); 5358 if (!bdev_io) { 5359 return -ENOMEM; 5360 } 5361 5362 bdev_io->internal.ch = channel; 5363 bdev_io->internal.desc = desc; 5364 bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO; 5365 bdev_io->u.nvme_passthru.cmd = *cmd; 5366 bdev_io->u.nvme_passthru.buf = buf; 5367 bdev_io->u.nvme_passthru.nbytes = nbytes; 5368 bdev_io->u.nvme_passthru.md_buf = NULL; 5369 bdev_io->u.nvme_passthru.md_len = 0; 5370 5371 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5372 5373 bdev_io_submit(bdev_io); 5374 return 0; 5375 } 5376 5377 int 5378 spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5379 const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len, 5380 spdk_bdev_io_completion_cb cb, void *cb_arg) 5381 { 5382 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5383 struct spdk_bdev_io *bdev_io; 5384 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5385 5386 if (!desc->write) { 5387 /* 5388 * Do not try to parse the NVMe command - we could maybe use bits in the opcode 5389 * to easily determine if the command is a read or write, but for now just 5390 * do not allow io_passthru with a read-only descriptor. 5391 */ 5392 return -EBADF; 5393 } 5394 5395 if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO_MD))) { 5396 return -ENOTSUP; 5397 } 5398 5399 bdev_io = bdev_channel_get_io(channel); 5400 if (!bdev_io) { 5401 return -ENOMEM; 5402 } 5403 5404 bdev_io->internal.ch = channel; 5405 bdev_io->internal.desc = desc; 5406 bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD; 5407 bdev_io->u.nvme_passthru.cmd = *cmd; 5408 bdev_io->u.nvme_passthru.buf = buf; 5409 bdev_io->u.nvme_passthru.nbytes = nbytes; 5410 bdev_io->u.nvme_passthru.md_buf = md_buf; 5411 bdev_io->u.nvme_passthru.md_len = md_len; 5412 5413 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5414 5415 bdev_io_submit(bdev_io); 5416 return 0; 5417 } 5418 5419 static void bdev_abort_retry(void *ctx); 5420 static void bdev_abort(struct spdk_bdev_io *parent_io); 5421 5422 static void 5423 bdev_abort_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 5424 { 5425 struct spdk_bdev_channel *channel = bdev_io->internal.ch; 5426 struct spdk_bdev_io *parent_io = cb_arg; 5427 struct spdk_bdev_io *bio_to_abort, *tmp_io; 5428 5429 bio_to_abort = bdev_io->u.abort.bio_to_abort; 5430 5431 spdk_bdev_free_io(bdev_io); 5432 5433 if (!success) { 5434 /* Check if the target I/O completed in the meantime. */ 5435 TAILQ_FOREACH(tmp_io, &channel->io_submitted, internal.ch_link) { 5436 if (tmp_io == bio_to_abort) { 5437 break; 5438 } 5439 } 5440 5441 /* If the target I/O still exists, set the parent to failed. */ 5442 if (tmp_io != NULL) { 5443 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 5444 } 5445 } 5446 5447 parent_io->u.bdev.split_outstanding--; 5448 if (parent_io->u.bdev.split_outstanding == 0) { 5449 if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) { 5450 bdev_abort_retry(parent_io); 5451 } else { 5452 bdev_io_complete(parent_io); 5453 } 5454 } 5455 } 5456 5457 static int 5458 bdev_abort_io(struct spdk_bdev_desc *desc, struct spdk_bdev_channel *channel, 5459 struct spdk_bdev_io *bio_to_abort, 5460 spdk_bdev_io_completion_cb cb, void *cb_arg) 5461 { 5462 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5463 struct spdk_bdev_io *bdev_io; 5464 5465 if (bio_to_abort->type == SPDK_BDEV_IO_TYPE_ABORT || 5466 bio_to_abort->type == SPDK_BDEV_IO_TYPE_RESET) { 5467 /* TODO: Abort reset or abort request. */ 5468 return -ENOTSUP; 5469 } 5470 5471 bdev_io = bdev_channel_get_io(channel); 5472 if (bdev_io == NULL) { 5473 return -ENOMEM; 5474 } 5475 5476 bdev_io->internal.ch = channel; 5477 bdev_io->internal.desc = desc; 5478 bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT; 5479 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5480 5481 if (bdev->split_on_optimal_io_boundary && bdev_io_should_split(bio_to_abort)) { 5482 bdev_io->u.bdev.abort.bio_cb_arg = bio_to_abort; 5483 5484 /* Parent abort request is not submitted directly, but to manage its 5485 * execution add it to the submitted list here. 5486 */ 5487 bdev_io->internal.submit_tsc = spdk_get_ticks(); 5488 TAILQ_INSERT_TAIL(&channel->io_submitted, bdev_io, internal.ch_link); 5489 5490 bdev_abort(bdev_io); 5491 5492 return 0; 5493 } 5494 5495 bdev_io->u.abort.bio_to_abort = bio_to_abort; 5496 5497 /* Submit the abort request to the underlying bdev module. */ 5498 bdev_io_submit(bdev_io); 5499 5500 return 0; 5501 } 5502 5503 static uint32_t 5504 _bdev_abort(struct spdk_bdev_io *parent_io) 5505 { 5506 struct spdk_bdev_desc *desc = parent_io->internal.desc; 5507 struct spdk_bdev_channel *channel = parent_io->internal.ch; 5508 void *bio_cb_arg; 5509 struct spdk_bdev_io *bio_to_abort; 5510 uint32_t matched_ios; 5511 int rc; 5512 5513 bio_cb_arg = parent_io->u.bdev.abort.bio_cb_arg; 5514 5515 /* matched_ios is returned and will be kept by the caller. 5516 * 5517 * This funcion will be used for two cases, 1) the same cb_arg is used for 5518 * multiple I/Os, 2) a single large I/O is split into smaller ones. 5519 * Incrementing split_outstanding directly here may confuse readers especially 5520 * for the 1st case. 5521 * 5522 * Completion of I/O abort is processed after stack unwinding. Hence this trick 5523 * works as expected. 5524 */ 5525 matched_ios = 0; 5526 parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 5527 5528 TAILQ_FOREACH(bio_to_abort, &channel->io_submitted, internal.ch_link) { 5529 if (bio_to_abort->internal.caller_ctx != bio_cb_arg) { 5530 continue; 5531 } 5532 5533 if (bio_to_abort->internal.submit_tsc > parent_io->internal.submit_tsc) { 5534 /* Any I/O which was submitted after this abort command should be excluded. */ 5535 continue; 5536 } 5537 5538 rc = bdev_abort_io(desc, channel, bio_to_abort, bdev_abort_io_done, parent_io); 5539 if (rc != 0) { 5540 if (rc == -ENOMEM) { 5541 parent_io->internal.status = SPDK_BDEV_IO_STATUS_NOMEM; 5542 } else { 5543 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 5544 } 5545 break; 5546 } 5547 matched_ios++; 5548 } 5549 5550 return matched_ios; 5551 } 5552 5553 static void 5554 bdev_abort_retry(void *ctx) 5555 { 5556 struct spdk_bdev_io *parent_io = ctx; 5557 uint32_t matched_ios; 5558 5559 matched_ios = _bdev_abort(parent_io); 5560 5561 if (matched_ios == 0) { 5562 if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) { 5563 bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry); 5564 } else { 5565 /* For retry, the case that no target I/O was found is success 5566 * because it means target I/Os completed in the meantime. 5567 */ 5568 bdev_io_complete(parent_io); 5569 } 5570 return; 5571 } 5572 5573 /* Use split_outstanding to manage the progress of aborting I/Os. */ 5574 parent_io->u.bdev.split_outstanding = matched_ios; 5575 } 5576 5577 static void 5578 bdev_abort(struct spdk_bdev_io *parent_io) 5579 { 5580 uint32_t matched_ios; 5581 5582 matched_ios = _bdev_abort(parent_io); 5583 5584 if (matched_ios == 0) { 5585 if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) { 5586 bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry); 5587 } else { 5588 /* The case the no target I/O was found is failure. */ 5589 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 5590 bdev_io_complete(parent_io); 5591 } 5592 return; 5593 } 5594 5595 /* Use split_outstanding to manage the progress of aborting I/Os. */ 5596 parent_io->u.bdev.split_outstanding = matched_ios; 5597 } 5598 5599 int 5600 spdk_bdev_abort(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 5601 void *bio_cb_arg, 5602 spdk_bdev_io_completion_cb cb, void *cb_arg) 5603 { 5604 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5605 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5606 struct spdk_bdev_io *bdev_io; 5607 5608 if (bio_cb_arg == NULL) { 5609 return -EINVAL; 5610 } 5611 5612 if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT)) { 5613 return -ENOTSUP; 5614 } 5615 5616 bdev_io = bdev_channel_get_io(channel); 5617 if (bdev_io == NULL) { 5618 return -ENOMEM; 5619 } 5620 5621 bdev_io->internal.ch = channel; 5622 bdev_io->internal.desc = desc; 5623 bdev_io->internal.submit_tsc = spdk_get_ticks(); 5624 bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT; 5625 bdev_io_init(bdev_io, bdev, cb_arg, cb); 5626 5627 bdev_io->u.bdev.abort.bio_cb_arg = bio_cb_arg; 5628 5629 /* Parent abort request is not submitted directly, but to manage its execution, 5630 * add it to the submitted list here. 5631 */ 5632 TAILQ_INSERT_TAIL(&channel->io_submitted, bdev_io, internal.ch_link); 5633 5634 bdev_abort(bdev_io); 5635 5636 return 0; 5637 } 5638 5639 int 5640 spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch, 5641 struct spdk_bdev_io_wait_entry *entry) 5642 { 5643 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 5644 struct spdk_bdev_mgmt_channel *mgmt_ch = channel->shared_resource->mgmt_ch; 5645 5646 if (bdev != entry->bdev) { 5647 SPDK_ERRLOG("bdevs do not match\n"); 5648 return -EINVAL; 5649 } 5650 5651 if (mgmt_ch->per_thread_cache_count > 0) { 5652 SPDK_ERRLOG("Cannot queue io_wait if spdk_bdev_io available in per-thread cache\n"); 5653 return -EINVAL; 5654 } 5655 5656 TAILQ_INSERT_TAIL(&mgmt_ch->io_wait_queue, entry, link); 5657 return 0; 5658 } 5659 5660 static inline void 5661 bdev_io_complete(void *ctx) 5662 { 5663 struct spdk_bdev_io *bdev_io = ctx; 5664 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 5665 uint64_t tsc, tsc_diff; 5666 5667 if (spdk_unlikely(bdev_io->internal.in_submit_request || bdev_io->internal.io_submit_ch)) { 5668 /* 5669 * Send the completion to the thread that originally submitted the I/O, 5670 * which may not be the current thread in the case of QoS. 5671 */ 5672 if (bdev_io->internal.io_submit_ch) { 5673 bdev_io->internal.ch = bdev_io->internal.io_submit_ch; 5674 bdev_io->internal.io_submit_ch = NULL; 5675 } 5676 5677 /* 5678 * Defer completion to avoid potential infinite recursion if the 5679 * user's completion callback issues a new I/O. 5680 */ 5681 spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), 5682 bdev_io_complete, bdev_io); 5683 return; 5684 } 5685 5686 tsc = spdk_get_ticks(); 5687 tsc_diff = tsc - bdev_io->internal.submit_tsc; 5688 spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, 5689 bdev_io->internal.caller_ctx); 5690 5691 TAILQ_REMOVE(&bdev_ch->io_submitted, bdev_io, internal.ch_link); 5692 5693 if (bdev_io->internal.ch->histogram) { 5694 spdk_histogram_data_tally(bdev_io->internal.ch->histogram, tsc_diff); 5695 } 5696 5697 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 5698 switch (bdev_io->type) { 5699 case SPDK_BDEV_IO_TYPE_READ: 5700 bdev_io->internal.ch->stat.bytes_read += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 5701 bdev_io->internal.ch->stat.num_read_ops++; 5702 bdev_io->internal.ch->stat.read_latency_ticks += tsc_diff; 5703 break; 5704 case SPDK_BDEV_IO_TYPE_WRITE: 5705 bdev_io->internal.ch->stat.bytes_written += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 5706 bdev_io->internal.ch->stat.num_write_ops++; 5707 bdev_io->internal.ch->stat.write_latency_ticks += tsc_diff; 5708 break; 5709 case SPDK_BDEV_IO_TYPE_UNMAP: 5710 bdev_io->internal.ch->stat.bytes_unmapped += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 5711 bdev_io->internal.ch->stat.num_unmap_ops++; 5712 bdev_io->internal.ch->stat.unmap_latency_ticks += tsc_diff; 5713 break; 5714 case SPDK_BDEV_IO_TYPE_ZCOPY: 5715 /* Track the data in the start phase only */ 5716 if (bdev_io->u.bdev.zcopy.start) { 5717 if (bdev_io->u.bdev.zcopy.populate) { 5718 bdev_io->internal.ch->stat.bytes_read += 5719 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 5720 bdev_io->internal.ch->stat.num_read_ops++; 5721 bdev_io->internal.ch->stat.read_latency_ticks += tsc_diff; 5722 } else { 5723 bdev_io->internal.ch->stat.bytes_written += 5724 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 5725 bdev_io->internal.ch->stat.num_write_ops++; 5726 bdev_io->internal.ch->stat.write_latency_ticks += tsc_diff; 5727 } 5728 } 5729 break; 5730 default: 5731 break; 5732 } 5733 } 5734 5735 #ifdef SPDK_CONFIG_VTUNE 5736 uint64_t now_tsc = spdk_get_ticks(); 5737 if (now_tsc > (bdev_io->internal.ch->start_tsc + bdev_io->internal.ch->interval_tsc)) { 5738 uint64_t data[5]; 5739 5740 data[0] = bdev_io->internal.ch->stat.num_read_ops - bdev_io->internal.ch->prev_stat.num_read_ops; 5741 data[1] = bdev_io->internal.ch->stat.bytes_read - bdev_io->internal.ch->prev_stat.bytes_read; 5742 data[2] = bdev_io->internal.ch->stat.num_write_ops - bdev_io->internal.ch->prev_stat.num_write_ops; 5743 data[3] = bdev_io->internal.ch->stat.bytes_written - bdev_io->internal.ch->prev_stat.bytes_written; 5744 data[4] = bdev_io->bdev->fn_table->get_spin_time ? 5745 bdev_io->bdev->fn_table->get_spin_time(spdk_bdev_io_get_io_channel(bdev_io)) : 0; 5746 5747 __itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->internal.ch->handle, 5748 __itt_metadata_u64, 5, data); 5749 5750 bdev_io->internal.ch->prev_stat = bdev_io->internal.ch->stat; 5751 bdev_io->internal.ch->start_tsc = now_tsc; 5752 } 5753 #endif 5754 5755 assert(bdev_io->internal.cb != NULL); 5756 assert(spdk_get_thread() == spdk_bdev_io_get_thread(bdev_io)); 5757 5758 bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS, 5759 bdev_io->internal.caller_ctx); 5760 } 5761 5762 static void bdev_destroy_cb(void *io_device); 5763 5764 static void 5765 bdev_reset_complete(struct spdk_io_channel_iter *i, int status) 5766 { 5767 struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i); 5768 struct spdk_bdev *bdev = bdev_io->bdev; 5769 5770 if (bdev_io->u.reset.ch_ref != NULL) { 5771 spdk_put_io_channel(bdev_io->u.reset.ch_ref); 5772 bdev_io->u.reset.ch_ref = NULL; 5773 } 5774 5775 bdev_io_complete(bdev_io); 5776 5777 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && 5778 TAILQ_EMPTY(&bdev->internal.open_descs)) { 5779 spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb); 5780 } 5781 } 5782 5783 static void 5784 bdev_unfreeze_channel(struct spdk_io_channel_iter *i) 5785 { 5786 struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i); 5787 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5788 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5789 struct spdk_bdev_io *queued_reset; 5790 5791 ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS; 5792 while (!TAILQ_EMPTY(&ch->queued_resets)) { 5793 queued_reset = TAILQ_FIRST(&ch->queued_resets); 5794 TAILQ_REMOVE(&ch->queued_resets, queued_reset, internal.link); 5795 spdk_bdev_io_complete(queued_reset, bdev_io->internal.status); 5796 } 5797 5798 spdk_for_each_channel_continue(i, 0); 5799 } 5800 5801 void 5802 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status) 5803 { 5804 struct spdk_bdev *bdev = bdev_io->bdev; 5805 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 5806 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 5807 5808 bdev_io->internal.status = status; 5809 5810 if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) { 5811 bool unlock_channels = false; 5812 5813 if (status == SPDK_BDEV_IO_STATUS_NOMEM) { 5814 SPDK_ERRLOG("NOMEM returned for reset\n"); 5815 } 5816 pthread_mutex_lock(&bdev->internal.mutex); 5817 if (bdev_io == bdev->internal.reset_in_progress) { 5818 bdev->internal.reset_in_progress = NULL; 5819 unlock_channels = true; 5820 } 5821 pthread_mutex_unlock(&bdev->internal.mutex); 5822 5823 if (unlock_channels) { 5824 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_unfreeze_channel, 5825 bdev_io, bdev_reset_complete); 5826 return; 5827 } 5828 } else { 5829 if (spdk_unlikely(bdev_io->internal.orig_iovcnt != 0)) { 5830 _bdev_io_push_bounce_data_buffer(bdev_io, _bdev_io_complete_push_bounce_done); 5831 /* bdev IO will be completed in the callback */ 5832 return; 5833 } 5834 5835 _bdev_io_decrement_outstanding(bdev_ch, shared_resource); 5836 if (spdk_unlikely(_bdev_io_handle_no_mem(bdev_io))) { 5837 return; 5838 } 5839 } 5840 5841 bdev_io_complete(bdev_io); 5842 } 5843 5844 void 5845 spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc, 5846 enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq) 5847 { 5848 if (sc == SPDK_SCSI_STATUS_GOOD) { 5849 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 5850 } else { 5851 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SCSI_ERROR; 5852 bdev_io->internal.error.scsi.sc = sc; 5853 bdev_io->internal.error.scsi.sk = sk; 5854 bdev_io->internal.error.scsi.asc = asc; 5855 bdev_io->internal.error.scsi.ascq = ascq; 5856 } 5857 5858 spdk_bdev_io_complete(bdev_io, bdev_io->internal.status); 5859 } 5860 5861 void 5862 spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io, 5863 int *sc, int *sk, int *asc, int *ascq) 5864 { 5865 assert(sc != NULL); 5866 assert(sk != NULL); 5867 assert(asc != NULL); 5868 assert(ascq != NULL); 5869 5870 switch (bdev_io->internal.status) { 5871 case SPDK_BDEV_IO_STATUS_SUCCESS: 5872 *sc = SPDK_SCSI_STATUS_GOOD; 5873 *sk = SPDK_SCSI_SENSE_NO_SENSE; 5874 *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE; 5875 *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE; 5876 break; 5877 case SPDK_BDEV_IO_STATUS_NVME_ERROR: 5878 spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq); 5879 break; 5880 case SPDK_BDEV_IO_STATUS_SCSI_ERROR: 5881 *sc = bdev_io->internal.error.scsi.sc; 5882 *sk = bdev_io->internal.error.scsi.sk; 5883 *asc = bdev_io->internal.error.scsi.asc; 5884 *ascq = bdev_io->internal.error.scsi.ascq; 5885 break; 5886 default: 5887 *sc = SPDK_SCSI_STATUS_CHECK_CONDITION; 5888 *sk = SPDK_SCSI_SENSE_ABORTED_COMMAND; 5889 *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE; 5890 *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE; 5891 break; 5892 } 5893 } 5894 5895 void 5896 spdk_bdev_io_complete_aio_status(struct spdk_bdev_io *bdev_io, int aio_result) 5897 { 5898 if (aio_result == 0) { 5899 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 5900 } else { 5901 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_AIO_ERROR; 5902 } 5903 5904 bdev_io->internal.error.aio_result = aio_result; 5905 5906 spdk_bdev_io_complete(bdev_io, bdev_io->internal.status); 5907 } 5908 5909 void 5910 spdk_bdev_io_get_aio_status(const struct spdk_bdev_io *bdev_io, int *aio_result) 5911 { 5912 assert(aio_result != NULL); 5913 5914 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_AIO_ERROR) { 5915 *aio_result = bdev_io->internal.error.aio_result; 5916 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 5917 *aio_result = 0; 5918 } else { 5919 *aio_result = -EIO; 5920 } 5921 } 5922 5923 void 5924 spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, uint32_t cdw0, int sct, int sc) 5925 { 5926 if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS) { 5927 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 5928 } else if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_ABORTED_BY_REQUEST) { 5929 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_ABORTED; 5930 } else { 5931 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_NVME_ERROR; 5932 } 5933 5934 bdev_io->internal.error.nvme.cdw0 = cdw0; 5935 bdev_io->internal.error.nvme.sct = sct; 5936 bdev_io->internal.error.nvme.sc = sc; 5937 5938 spdk_bdev_io_complete(bdev_io, bdev_io->internal.status); 5939 } 5940 5941 void 5942 spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0, int *sct, int *sc) 5943 { 5944 assert(sct != NULL); 5945 assert(sc != NULL); 5946 assert(cdw0 != NULL); 5947 5948 if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) { 5949 *sct = SPDK_NVME_SCT_GENERIC; 5950 *sc = SPDK_NVME_SC_SUCCESS; 5951 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 5952 *cdw0 = 0; 5953 } else { 5954 *cdw0 = 1U; 5955 } 5956 return; 5957 } 5958 5959 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) { 5960 *sct = bdev_io->internal.error.nvme.sct; 5961 *sc = bdev_io->internal.error.nvme.sc; 5962 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 5963 *sct = SPDK_NVME_SCT_GENERIC; 5964 *sc = SPDK_NVME_SC_SUCCESS; 5965 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) { 5966 *sct = SPDK_NVME_SCT_GENERIC; 5967 *sc = SPDK_NVME_SC_ABORTED_BY_REQUEST; 5968 } else { 5969 *sct = SPDK_NVME_SCT_GENERIC; 5970 *sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 5971 } 5972 5973 *cdw0 = bdev_io->internal.error.nvme.cdw0; 5974 } 5975 5976 void 5977 spdk_bdev_io_get_nvme_fused_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0, 5978 int *first_sct, int *first_sc, int *second_sct, int *second_sc) 5979 { 5980 assert(first_sct != NULL); 5981 assert(first_sc != NULL); 5982 assert(second_sct != NULL); 5983 assert(second_sc != NULL); 5984 assert(cdw0 != NULL); 5985 5986 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) { 5987 if (bdev_io->internal.error.nvme.sct == SPDK_NVME_SCT_MEDIA_ERROR && 5988 bdev_io->internal.error.nvme.sc == SPDK_NVME_SC_COMPARE_FAILURE) { 5989 *first_sct = bdev_io->internal.error.nvme.sct; 5990 *first_sc = bdev_io->internal.error.nvme.sc; 5991 *second_sct = SPDK_NVME_SCT_GENERIC; 5992 *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED; 5993 } else { 5994 *first_sct = SPDK_NVME_SCT_GENERIC; 5995 *first_sc = SPDK_NVME_SC_SUCCESS; 5996 *second_sct = bdev_io->internal.error.nvme.sct; 5997 *second_sc = bdev_io->internal.error.nvme.sc; 5998 } 5999 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 6000 *first_sct = SPDK_NVME_SCT_GENERIC; 6001 *first_sc = SPDK_NVME_SC_SUCCESS; 6002 *second_sct = SPDK_NVME_SCT_GENERIC; 6003 *second_sc = SPDK_NVME_SC_SUCCESS; 6004 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED) { 6005 *first_sct = SPDK_NVME_SCT_GENERIC; 6006 *first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 6007 *second_sct = SPDK_NVME_SCT_GENERIC; 6008 *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED; 6009 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_MISCOMPARE) { 6010 *first_sct = SPDK_NVME_SCT_MEDIA_ERROR; 6011 *first_sc = SPDK_NVME_SC_COMPARE_FAILURE; 6012 *second_sct = SPDK_NVME_SCT_GENERIC; 6013 *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED; 6014 } else { 6015 *first_sct = SPDK_NVME_SCT_GENERIC; 6016 *first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 6017 *second_sct = SPDK_NVME_SCT_GENERIC; 6018 *second_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 6019 } 6020 6021 *cdw0 = bdev_io->internal.error.nvme.cdw0; 6022 } 6023 6024 struct spdk_thread * 6025 spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io) 6026 { 6027 return spdk_io_channel_get_thread(bdev_io->internal.ch->channel); 6028 } 6029 6030 struct spdk_io_channel * 6031 spdk_bdev_io_get_io_channel(struct spdk_bdev_io *bdev_io) 6032 { 6033 return bdev_io->internal.ch->channel; 6034 } 6035 6036 static int 6037 bdev_register(struct spdk_bdev *bdev) 6038 { 6039 char *bdev_name; 6040 char uuid[SPDK_UUID_STRING_LEN]; 6041 int ret; 6042 6043 assert(bdev->module != NULL); 6044 6045 if (!bdev->name) { 6046 SPDK_ERRLOG("Bdev name is NULL\n"); 6047 return -EINVAL; 6048 } 6049 6050 if (!strlen(bdev->name)) { 6051 SPDK_ERRLOG("Bdev name must not be an empty string\n"); 6052 return -EINVAL; 6053 } 6054 6055 /* Users often register their own I/O devices using the bdev name. In 6056 * order to avoid conflicts, prepend bdev_. */ 6057 bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name); 6058 if (!bdev_name) { 6059 SPDK_ERRLOG("Unable to allocate memory for internal bdev name.\n"); 6060 return -ENOMEM; 6061 } 6062 6063 bdev->internal.status = SPDK_BDEV_STATUS_READY; 6064 bdev->internal.measured_queue_depth = UINT64_MAX; 6065 bdev->internal.claim_module = NULL; 6066 bdev->internal.qd_poller = NULL; 6067 bdev->internal.qos = NULL; 6068 6069 TAILQ_INIT(&bdev->internal.open_descs); 6070 TAILQ_INIT(&bdev->internal.locked_ranges); 6071 TAILQ_INIT(&bdev->internal.pending_locked_ranges); 6072 TAILQ_INIT(&bdev->aliases); 6073 6074 ret = bdev_name_add(&bdev->internal.bdev_name, bdev, bdev->name); 6075 if (ret != 0) { 6076 free(bdev_name); 6077 return ret; 6078 } 6079 6080 /* If the user didn't specify a uuid, generate one. */ 6081 if (spdk_mem_all_zero(&bdev->uuid, sizeof(bdev->uuid))) { 6082 spdk_uuid_generate(&bdev->uuid); 6083 } 6084 6085 /* Add the UUID alias only if it's different than the name */ 6086 spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid); 6087 if (strcmp(bdev->name, uuid) != 0) { 6088 ret = spdk_bdev_alias_add(bdev, uuid); 6089 if (ret != 0) { 6090 SPDK_ERRLOG("Unable to add uuid:%s alias for bdev %s\n", uuid, bdev->name); 6091 bdev_name_del(&bdev->internal.bdev_name); 6092 free(bdev_name); 6093 return ret; 6094 } 6095 } 6096 6097 if (spdk_bdev_get_buf_align(bdev) > 1) { 6098 if (bdev->split_on_optimal_io_boundary) { 6099 bdev->optimal_io_boundary = spdk_min(bdev->optimal_io_boundary, 6100 SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen); 6101 } else { 6102 bdev->split_on_optimal_io_boundary = true; 6103 bdev->optimal_io_boundary = SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen; 6104 } 6105 } 6106 6107 /* If the user didn't specify a write unit size, set it to one. */ 6108 if (bdev->write_unit_size == 0) { 6109 bdev->write_unit_size = 1; 6110 } 6111 6112 /* Set ACWU value to 1 if bdev module did not set it (does not support it natively) */ 6113 if (bdev->acwu == 0) { 6114 bdev->acwu = 1; 6115 } 6116 6117 if (bdev->phys_blocklen == 0) { 6118 bdev->phys_blocklen = spdk_bdev_get_data_block_size(bdev); 6119 } 6120 6121 bdev->internal.reset_in_progress = NULL; 6122 6123 spdk_io_device_register(__bdev_to_io_dev(bdev), 6124 bdev_channel_create, bdev_channel_destroy, 6125 sizeof(struct spdk_bdev_channel), 6126 bdev_name); 6127 6128 free(bdev_name); 6129 6130 pthread_mutex_init(&bdev->internal.mutex, NULL); 6131 6132 SPDK_DEBUGLOG(bdev, "Inserting bdev %s into list\n", bdev->name); 6133 TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, internal.link); 6134 6135 return 0; 6136 } 6137 6138 static void 6139 bdev_destroy_cb(void *io_device) 6140 { 6141 int rc; 6142 struct spdk_bdev *bdev; 6143 spdk_bdev_unregister_cb cb_fn; 6144 void *cb_arg; 6145 6146 bdev = __bdev_from_io_dev(io_device); 6147 cb_fn = bdev->internal.unregister_cb; 6148 cb_arg = bdev->internal.unregister_ctx; 6149 6150 pthread_mutex_destroy(&bdev->internal.mutex); 6151 free(bdev->internal.qos); 6152 6153 rc = bdev->fn_table->destruct(bdev->ctxt); 6154 if (rc < 0) { 6155 SPDK_ERRLOG("destruct failed\n"); 6156 } 6157 if (rc <= 0 && cb_fn != NULL) { 6158 cb_fn(cb_arg, rc); 6159 } 6160 } 6161 6162 static void 6163 bdev_register_finished(void *arg) 6164 { 6165 struct spdk_bdev *bdev = arg; 6166 6167 spdk_notify_send("bdev_register", spdk_bdev_get_name(bdev)); 6168 } 6169 6170 int 6171 spdk_bdev_register(struct spdk_bdev *bdev) 6172 { 6173 int rc = bdev_register(bdev); 6174 6175 if (rc == 0) { 6176 /* Examine configuration before initializing I/O */ 6177 bdev_examine(bdev); 6178 6179 spdk_bdev_wait_for_examine(bdev_register_finished, bdev); 6180 } 6181 6182 return rc; 6183 } 6184 6185 void 6186 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno) 6187 { 6188 if (bdev->internal.unregister_cb != NULL) { 6189 bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno); 6190 } 6191 } 6192 6193 static void 6194 _remove_notify(void *arg) 6195 { 6196 struct spdk_bdev_desc *desc = arg; 6197 6198 pthread_mutex_lock(&desc->mutex); 6199 desc->refs--; 6200 6201 if (!desc->closed) { 6202 pthread_mutex_unlock(&desc->mutex); 6203 desc->callback.event_fn(SPDK_BDEV_EVENT_REMOVE, desc->bdev, desc->callback.ctx); 6204 return; 6205 } else if (0 == desc->refs) { 6206 /* This descriptor was closed after this remove_notify message was sent. 6207 * spdk_bdev_close() could not free the descriptor since this message was 6208 * in flight, so we free it now using bdev_desc_free(). 6209 */ 6210 pthread_mutex_unlock(&desc->mutex); 6211 bdev_desc_free(desc); 6212 return; 6213 } 6214 pthread_mutex_unlock(&desc->mutex); 6215 } 6216 6217 /* Must be called while holding g_bdev_mgr.mutex and bdev->internal.mutex. 6218 * returns: 0 - bdev removed and ready to be destructed. 6219 * -EBUSY - bdev can't be destructed yet. */ 6220 static int 6221 bdev_unregister_unsafe(struct spdk_bdev *bdev) 6222 { 6223 struct spdk_bdev_desc *desc, *tmp; 6224 int rc = 0; 6225 char uuid[SPDK_UUID_STRING_LEN]; 6226 6227 /* Notify each descriptor about hotremoval */ 6228 TAILQ_FOREACH_SAFE(desc, &bdev->internal.open_descs, link, tmp) { 6229 rc = -EBUSY; 6230 pthread_mutex_lock(&desc->mutex); 6231 /* 6232 * Defer invocation of the event_cb to a separate message that will 6233 * run later on its thread. This ensures this context unwinds and 6234 * we don't recursively unregister this bdev again if the event_cb 6235 * immediately closes its descriptor. 6236 */ 6237 desc->refs++; 6238 spdk_thread_send_msg(desc->thread, _remove_notify, desc); 6239 pthread_mutex_unlock(&desc->mutex); 6240 } 6241 6242 /* If there are no descriptors, proceed removing the bdev */ 6243 if (rc == 0) { 6244 TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link); 6245 SPDK_DEBUGLOG(bdev, "Removing bdev %s from list done\n", bdev->name); 6246 6247 /* Delete the name and the UUID alias */ 6248 spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid); 6249 bdev_name_del_unsafe(&bdev->internal.bdev_name); 6250 bdev_alias_del(bdev, uuid, bdev_name_del_unsafe); 6251 6252 spdk_notify_send("bdev_unregister", spdk_bdev_get_name(bdev)); 6253 6254 if (bdev->internal.reset_in_progress != NULL) { 6255 /* If reset is in progress, let the completion callback for reset 6256 * unregister the bdev. 6257 */ 6258 rc = -EBUSY; 6259 } 6260 } 6261 6262 return rc; 6263 } 6264 6265 void 6266 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg) 6267 { 6268 struct spdk_thread *thread; 6269 int rc; 6270 6271 SPDK_DEBUGLOG(bdev, "Removing bdev %s from list\n", bdev->name); 6272 6273 thread = spdk_get_thread(); 6274 if (!thread) { 6275 /* The user called this from a non-SPDK thread. */ 6276 if (cb_fn != NULL) { 6277 cb_fn(cb_arg, -ENOTSUP); 6278 } 6279 return; 6280 } 6281 6282 pthread_mutex_lock(&g_bdev_mgr.mutex); 6283 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) { 6284 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6285 if (cb_fn) { 6286 cb_fn(cb_arg, -EBUSY); 6287 } 6288 return; 6289 } 6290 6291 pthread_mutex_lock(&bdev->internal.mutex); 6292 bdev->internal.status = SPDK_BDEV_STATUS_REMOVING; 6293 bdev->internal.unregister_cb = cb_fn; 6294 bdev->internal.unregister_ctx = cb_arg; 6295 6296 /* Call under lock. */ 6297 rc = bdev_unregister_unsafe(bdev); 6298 pthread_mutex_unlock(&bdev->internal.mutex); 6299 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6300 6301 if (rc == 0) { 6302 spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb); 6303 } 6304 } 6305 6306 static void 6307 _tmp_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx) 6308 { 6309 SPDK_NOTICELOG("Unexpected event type: %d\n", type); 6310 } 6311 6312 int 6313 spdk_bdev_unregister_by_name(const char *bdev_name, struct spdk_bdev_module *module, 6314 spdk_bdev_unregister_cb cb_fn, void *cb_arg) 6315 { 6316 struct spdk_bdev_desc *desc; 6317 struct spdk_bdev *bdev; 6318 int rc; 6319 6320 rc = spdk_bdev_open_ext(bdev_name, false, _tmp_bdev_event_cb, NULL, &desc); 6321 if (rc != 0) { 6322 SPDK_ERRLOG("Failed to open bdev with name: %s\n", bdev_name); 6323 return rc; 6324 } 6325 6326 bdev = spdk_bdev_desc_get_bdev(desc); 6327 6328 if (bdev->module != module) { 6329 spdk_bdev_close(desc); 6330 SPDK_ERRLOG("Bdev %s was not registered by the specified module.\n", 6331 bdev_name); 6332 return -ENODEV; 6333 } 6334 6335 spdk_bdev_unregister(bdev, cb_fn, cb_arg); 6336 6337 spdk_bdev_close(desc); 6338 6339 return 0; 6340 } 6341 6342 static int 6343 bdev_start_qos(struct spdk_bdev *bdev) 6344 { 6345 struct set_qos_limit_ctx *ctx; 6346 6347 /* Enable QoS */ 6348 if (bdev->internal.qos && bdev->internal.qos->thread == NULL) { 6349 ctx = calloc(1, sizeof(*ctx)); 6350 if (ctx == NULL) { 6351 SPDK_ERRLOG("Failed to allocate memory for QoS context\n"); 6352 return -ENOMEM; 6353 } 6354 ctx->bdev = bdev; 6355 spdk_for_each_channel(__bdev_to_io_dev(bdev), 6356 bdev_enable_qos_msg, ctx, 6357 bdev_enable_qos_done); 6358 } 6359 6360 return 0; 6361 } 6362 6363 static int 6364 bdev_open(struct spdk_bdev *bdev, bool write, struct spdk_bdev_desc *desc) 6365 { 6366 struct spdk_thread *thread; 6367 int rc = 0; 6368 6369 thread = spdk_get_thread(); 6370 if (!thread) { 6371 SPDK_ERRLOG("Cannot open bdev from non-SPDK thread.\n"); 6372 return -ENOTSUP; 6373 } 6374 6375 SPDK_DEBUGLOG(bdev, "Opening descriptor %p for bdev %s on thread %p\n", desc, bdev->name, 6376 spdk_get_thread()); 6377 6378 desc->bdev = bdev; 6379 desc->thread = thread; 6380 desc->write = write; 6381 6382 pthread_mutex_lock(&bdev->internal.mutex); 6383 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) { 6384 pthread_mutex_unlock(&bdev->internal.mutex); 6385 return -ENODEV; 6386 } 6387 6388 if (write && bdev->internal.claim_module) { 6389 SPDK_ERRLOG("Could not open %s - %s module already claimed it\n", 6390 bdev->name, bdev->internal.claim_module->name); 6391 pthread_mutex_unlock(&bdev->internal.mutex); 6392 return -EPERM; 6393 } 6394 6395 rc = bdev_start_qos(bdev); 6396 if (rc != 0) { 6397 SPDK_ERRLOG("Failed to start QoS on bdev %s\n", bdev->name); 6398 pthread_mutex_unlock(&bdev->internal.mutex); 6399 return rc; 6400 } 6401 6402 TAILQ_INSERT_TAIL(&bdev->internal.open_descs, desc, link); 6403 6404 pthread_mutex_unlock(&bdev->internal.mutex); 6405 6406 return 0; 6407 } 6408 6409 static int 6410 bdev_desc_alloc(struct spdk_bdev *bdev, spdk_bdev_event_cb_t event_cb, void *event_ctx, 6411 struct spdk_bdev_desc **_desc) 6412 { 6413 struct spdk_bdev_desc *desc; 6414 unsigned int event_id; 6415 6416 desc = calloc(1, sizeof(*desc)); 6417 if (desc == NULL) { 6418 SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n"); 6419 return -ENOMEM; 6420 } 6421 6422 TAILQ_INIT(&desc->pending_media_events); 6423 TAILQ_INIT(&desc->free_media_events); 6424 6425 desc->memory_domains_supported = spdk_bdev_get_memory_domains(bdev, NULL, 0) > 0; 6426 desc->callback.event_fn = event_cb; 6427 desc->callback.ctx = event_ctx; 6428 pthread_mutex_init(&desc->mutex, NULL); 6429 6430 if (bdev->media_events) { 6431 desc->media_events_buffer = calloc(MEDIA_EVENT_POOL_SIZE, 6432 sizeof(*desc->media_events_buffer)); 6433 if (desc->media_events_buffer == NULL) { 6434 SPDK_ERRLOG("Failed to initialize media event pool\n"); 6435 bdev_desc_free(desc); 6436 return -ENOMEM; 6437 } 6438 6439 for (event_id = 0; event_id < MEDIA_EVENT_POOL_SIZE; ++event_id) { 6440 TAILQ_INSERT_TAIL(&desc->free_media_events, 6441 &desc->media_events_buffer[event_id], tailq); 6442 } 6443 } 6444 6445 *_desc = desc; 6446 6447 return 0; 6448 } 6449 6450 int 6451 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb, 6452 void *event_ctx, struct spdk_bdev_desc **_desc) 6453 { 6454 struct spdk_bdev_desc *desc; 6455 struct spdk_bdev *bdev; 6456 int rc; 6457 6458 if (event_cb == NULL) { 6459 SPDK_ERRLOG("Missing event callback function\n"); 6460 return -EINVAL; 6461 } 6462 6463 pthread_mutex_lock(&g_bdev_mgr.mutex); 6464 6465 bdev = bdev_get_by_name(bdev_name); 6466 6467 if (bdev == NULL) { 6468 SPDK_NOTICELOG("Currently unable to find bdev with name: %s\n", bdev_name); 6469 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6470 return -ENODEV; 6471 } 6472 6473 rc = bdev_desc_alloc(bdev, event_cb, event_ctx, &desc); 6474 if (rc != 0) { 6475 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6476 return rc; 6477 } 6478 6479 rc = bdev_open(bdev, write, desc); 6480 if (rc != 0) { 6481 bdev_desc_free(desc); 6482 desc = NULL; 6483 } 6484 6485 *_desc = desc; 6486 6487 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6488 6489 return rc; 6490 } 6491 6492 static void 6493 bdev_close(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc) 6494 { 6495 int rc; 6496 6497 pthread_mutex_lock(&bdev->internal.mutex); 6498 pthread_mutex_lock(&desc->mutex); 6499 6500 TAILQ_REMOVE(&bdev->internal.open_descs, desc, link); 6501 6502 desc->closed = true; 6503 6504 if (0 == desc->refs) { 6505 pthread_mutex_unlock(&desc->mutex); 6506 bdev_desc_free(desc); 6507 } else { 6508 pthread_mutex_unlock(&desc->mutex); 6509 } 6510 6511 /* If no more descriptors, kill QoS channel */ 6512 if (bdev->internal.qos && TAILQ_EMPTY(&bdev->internal.open_descs)) { 6513 SPDK_DEBUGLOG(bdev, "Closed last descriptor for bdev %s on thread %p. Stopping QoS.\n", 6514 bdev->name, spdk_get_thread()); 6515 6516 if (bdev_qos_destroy(bdev)) { 6517 /* There isn't anything we can do to recover here. Just let the 6518 * old QoS poller keep running. The QoS handling won't change 6519 * cores when the user allocates a new channel, but it won't break. */ 6520 SPDK_ERRLOG("Unable to shut down QoS poller. It will continue running on the current thread.\n"); 6521 } 6522 } 6523 6524 spdk_bdev_set_qd_sampling_period(bdev, 0); 6525 6526 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->internal.open_descs)) { 6527 rc = bdev_unregister_unsafe(bdev); 6528 pthread_mutex_unlock(&bdev->internal.mutex); 6529 6530 if (rc == 0) { 6531 spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb); 6532 } 6533 } else { 6534 pthread_mutex_unlock(&bdev->internal.mutex); 6535 } 6536 } 6537 6538 void 6539 spdk_bdev_close(struct spdk_bdev_desc *desc) 6540 { 6541 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 6542 6543 SPDK_DEBUGLOG(bdev, "Closing descriptor %p for bdev %s on thread %p\n", desc, bdev->name, 6544 spdk_get_thread()); 6545 6546 assert(desc->thread == spdk_get_thread()); 6547 6548 spdk_poller_unregister(&desc->io_timeout_poller); 6549 6550 pthread_mutex_lock(&g_bdev_mgr.mutex); 6551 6552 bdev_close(bdev, desc); 6553 6554 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6555 } 6556 6557 int 6558 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 6559 struct spdk_bdev_module *module) 6560 { 6561 if (bdev->internal.claim_module != NULL) { 6562 SPDK_ERRLOG("bdev %s already claimed by module %s\n", bdev->name, 6563 bdev->internal.claim_module->name); 6564 return -EPERM; 6565 } 6566 6567 if (desc && !desc->write) { 6568 desc->write = true; 6569 } 6570 6571 bdev->internal.claim_module = module; 6572 return 0; 6573 } 6574 6575 void 6576 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev) 6577 { 6578 assert(bdev->internal.claim_module != NULL); 6579 bdev->internal.claim_module = NULL; 6580 } 6581 6582 struct spdk_bdev * 6583 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc) 6584 { 6585 assert(desc != NULL); 6586 return desc->bdev; 6587 } 6588 6589 int 6590 spdk_for_each_bdev(void *ctx, spdk_for_each_bdev_fn fn) 6591 { 6592 struct spdk_bdev *bdev, *tmp; 6593 struct spdk_bdev_desc *desc; 6594 int rc = 0; 6595 6596 assert(fn != NULL); 6597 6598 pthread_mutex_lock(&g_bdev_mgr.mutex); 6599 bdev = spdk_bdev_first(); 6600 while (bdev != NULL) { 6601 rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, &desc); 6602 if (rc != 0) { 6603 break; 6604 } 6605 rc = bdev_open(bdev, false, desc); 6606 if (rc != 0) { 6607 bdev_desc_free(desc); 6608 break; 6609 } 6610 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6611 6612 rc = fn(ctx, bdev); 6613 6614 pthread_mutex_lock(&g_bdev_mgr.mutex); 6615 tmp = spdk_bdev_next(bdev); 6616 bdev_close(bdev, desc); 6617 if (rc != 0) { 6618 break; 6619 } 6620 bdev = tmp; 6621 } 6622 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6623 6624 return rc; 6625 } 6626 6627 int 6628 spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn) 6629 { 6630 struct spdk_bdev *bdev, *tmp; 6631 struct spdk_bdev_desc *desc; 6632 int rc = 0; 6633 6634 assert(fn != NULL); 6635 6636 pthread_mutex_lock(&g_bdev_mgr.mutex); 6637 bdev = spdk_bdev_first_leaf(); 6638 while (bdev != NULL) { 6639 rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, &desc); 6640 if (rc != 0) { 6641 break; 6642 } 6643 rc = bdev_open(bdev, false, desc); 6644 if (rc != 0) { 6645 bdev_desc_free(desc); 6646 break; 6647 } 6648 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6649 6650 rc = fn(ctx, bdev); 6651 6652 pthread_mutex_lock(&g_bdev_mgr.mutex); 6653 tmp = spdk_bdev_next_leaf(bdev); 6654 bdev_close(bdev, desc); 6655 if (rc != 0) { 6656 break; 6657 } 6658 bdev = tmp; 6659 } 6660 pthread_mutex_unlock(&g_bdev_mgr.mutex); 6661 6662 return rc; 6663 } 6664 6665 void 6666 spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp) 6667 { 6668 struct iovec *iovs; 6669 int iovcnt; 6670 6671 if (bdev_io == NULL) { 6672 return; 6673 } 6674 6675 switch (bdev_io->type) { 6676 case SPDK_BDEV_IO_TYPE_READ: 6677 case SPDK_BDEV_IO_TYPE_WRITE: 6678 case SPDK_BDEV_IO_TYPE_ZCOPY: 6679 iovs = bdev_io->u.bdev.iovs; 6680 iovcnt = bdev_io->u.bdev.iovcnt; 6681 break; 6682 default: 6683 iovs = NULL; 6684 iovcnt = 0; 6685 break; 6686 } 6687 6688 if (iovp) { 6689 *iovp = iovs; 6690 } 6691 if (iovcntp) { 6692 *iovcntp = iovcnt; 6693 } 6694 } 6695 6696 void * 6697 spdk_bdev_io_get_md_buf(struct spdk_bdev_io *bdev_io) 6698 { 6699 if (bdev_io == NULL) { 6700 return NULL; 6701 } 6702 6703 if (!spdk_bdev_is_md_separate(bdev_io->bdev)) { 6704 return NULL; 6705 } 6706 6707 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ || 6708 bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 6709 return bdev_io->u.bdev.md_buf; 6710 } 6711 6712 return NULL; 6713 } 6714 6715 void * 6716 spdk_bdev_io_get_cb_arg(struct spdk_bdev_io *bdev_io) 6717 { 6718 if (bdev_io == NULL) { 6719 assert(false); 6720 return NULL; 6721 } 6722 6723 return bdev_io->internal.caller_ctx; 6724 } 6725 6726 void 6727 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module) 6728 { 6729 6730 if (spdk_bdev_module_list_find(bdev_module->name)) { 6731 SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name); 6732 assert(false); 6733 } 6734 6735 /* 6736 * Modules with examine callbacks must be initialized first, so they are 6737 * ready to handle examine callbacks from later modules that will 6738 * register physical bdevs. 6739 */ 6740 if (bdev_module->examine_config != NULL || bdev_module->examine_disk != NULL) { 6741 TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq); 6742 } else { 6743 TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq); 6744 } 6745 } 6746 6747 struct spdk_bdev_module * 6748 spdk_bdev_module_list_find(const char *name) 6749 { 6750 struct spdk_bdev_module *bdev_module; 6751 6752 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 6753 if (strcmp(name, bdev_module->name) == 0) { 6754 break; 6755 } 6756 } 6757 6758 return bdev_module; 6759 } 6760 6761 static void 6762 bdev_write_zero_buffer_next(void *_bdev_io) 6763 { 6764 struct spdk_bdev_io *bdev_io = _bdev_io; 6765 uint64_t num_bytes, num_blocks; 6766 void *md_buf = NULL; 6767 int rc; 6768 6769 num_bytes = spdk_min(_bdev_get_block_size_with_md(bdev_io->bdev) * 6770 bdev_io->u.bdev.split_remaining_num_blocks, 6771 ZERO_BUFFER_SIZE); 6772 num_blocks = num_bytes / _bdev_get_block_size_with_md(bdev_io->bdev); 6773 6774 if (spdk_bdev_is_md_separate(bdev_io->bdev)) { 6775 md_buf = (char *)g_bdev_mgr.zero_buffer + 6776 spdk_bdev_get_block_size(bdev_io->bdev) * num_blocks; 6777 } 6778 6779 rc = bdev_write_blocks_with_md(bdev_io->internal.desc, 6780 spdk_io_channel_from_ctx(bdev_io->internal.ch), 6781 g_bdev_mgr.zero_buffer, md_buf, 6782 bdev_io->u.bdev.split_current_offset_blocks, num_blocks, 6783 bdev_write_zero_buffer_done, bdev_io); 6784 if (rc == 0) { 6785 bdev_io->u.bdev.split_remaining_num_blocks -= num_blocks; 6786 bdev_io->u.bdev.split_current_offset_blocks += num_blocks; 6787 } else if (rc == -ENOMEM) { 6788 bdev_queue_io_wait_with_cb(bdev_io, bdev_write_zero_buffer_next); 6789 } else { 6790 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 6791 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 6792 } 6793 } 6794 6795 static void 6796 bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 6797 { 6798 struct spdk_bdev_io *parent_io = cb_arg; 6799 6800 spdk_bdev_free_io(bdev_io); 6801 6802 if (!success) { 6803 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 6804 parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx); 6805 return; 6806 } 6807 6808 if (parent_io->u.bdev.split_remaining_num_blocks == 0) { 6809 parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 6810 parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx); 6811 return; 6812 } 6813 6814 bdev_write_zero_buffer_next(parent_io); 6815 } 6816 6817 static void 6818 bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status) 6819 { 6820 pthread_mutex_lock(&ctx->bdev->internal.mutex); 6821 ctx->bdev->internal.qos_mod_in_progress = false; 6822 pthread_mutex_unlock(&ctx->bdev->internal.mutex); 6823 6824 if (ctx->cb_fn) { 6825 ctx->cb_fn(ctx->cb_arg, status); 6826 } 6827 free(ctx); 6828 } 6829 6830 static void 6831 bdev_disable_qos_done(void *cb_arg) 6832 { 6833 struct set_qos_limit_ctx *ctx = cb_arg; 6834 struct spdk_bdev *bdev = ctx->bdev; 6835 struct spdk_bdev_io *bdev_io; 6836 struct spdk_bdev_qos *qos; 6837 6838 pthread_mutex_lock(&bdev->internal.mutex); 6839 qos = bdev->internal.qos; 6840 bdev->internal.qos = NULL; 6841 pthread_mutex_unlock(&bdev->internal.mutex); 6842 6843 while (!TAILQ_EMPTY(&qos->queued)) { 6844 /* Send queued I/O back to their original thread for resubmission. */ 6845 bdev_io = TAILQ_FIRST(&qos->queued); 6846 TAILQ_REMOVE(&qos->queued, bdev_io, internal.link); 6847 6848 if (bdev_io->internal.io_submit_ch) { 6849 /* 6850 * Channel was changed when sending it to the QoS thread - change it back 6851 * before sending it back to the original thread. 6852 */ 6853 bdev_io->internal.ch = bdev_io->internal.io_submit_ch; 6854 bdev_io->internal.io_submit_ch = NULL; 6855 } 6856 6857 spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), 6858 _bdev_io_submit, bdev_io); 6859 } 6860 6861 if (qos->thread != NULL) { 6862 spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch)); 6863 spdk_poller_unregister(&qos->poller); 6864 } 6865 6866 free(qos); 6867 6868 bdev_set_qos_limit_done(ctx, 0); 6869 } 6870 6871 static void 6872 bdev_disable_qos_msg_done(struct spdk_io_channel_iter *i, int status) 6873 { 6874 void *io_device = spdk_io_channel_iter_get_io_device(i); 6875 struct spdk_bdev *bdev = __bdev_from_io_dev(io_device); 6876 struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 6877 struct spdk_thread *thread; 6878 6879 pthread_mutex_lock(&bdev->internal.mutex); 6880 thread = bdev->internal.qos->thread; 6881 pthread_mutex_unlock(&bdev->internal.mutex); 6882 6883 if (thread != NULL) { 6884 spdk_thread_send_msg(thread, bdev_disable_qos_done, ctx); 6885 } else { 6886 bdev_disable_qos_done(ctx); 6887 } 6888 } 6889 6890 static void 6891 bdev_disable_qos_msg(struct spdk_io_channel_iter *i) 6892 { 6893 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 6894 struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch); 6895 6896 bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED; 6897 6898 spdk_for_each_channel_continue(i, 0); 6899 } 6900 6901 static void 6902 bdev_update_qos_rate_limit_msg(void *cb_arg) 6903 { 6904 struct set_qos_limit_ctx *ctx = cb_arg; 6905 struct spdk_bdev *bdev = ctx->bdev; 6906 6907 pthread_mutex_lock(&bdev->internal.mutex); 6908 bdev_qos_update_max_quota_per_timeslice(bdev->internal.qos); 6909 pthread_mutex_unlock(&bdev->internal.mutex); 6910 6911 bdev_set_qos_limit_done(ctx, 0); 6912 } 6913 6914 static void 6915 bdev_enable_qos_msg(struct spdk_io_channel_iter *i) 6916 { 6917 void *io_device = spdk_io_channel_iter_get_io_device(i); 6918 struct spdk_bdev *bdev = __bdev_from_io_dev(io_device); 6919 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 6920 struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch); 6921 6922 pthread_mutex_lock(&bdev->internal.mutex); 6923 bdev_enable_qos(bdev, bdev_ch); 6924 pthread_mutex_unlock(&bdev->internal.mutex); 6925 spdk_for_each_channel_continue(i, 0); 6926 } 6927 6928 static void 6929 bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status) 6930 { 6931 struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 6932 6933 bdev_set_qos_limit_done(ctx, status); 6934 } 6935 6936 static void 6937 bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits) 6938 { 6939 int i; 6940 6941 assert(bdev->internal.qos != NULL); 6942 6943 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 6944 if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 6945 bdev->internal.qos->rate_limits[i].limit = limits[i]; 6946 6947 if (limits[i] == 0) { 6948 bdev->internal.qos->rate_limits[i].limit = 6949 SPDK_BDEV_QOS_LIMIT_NOT_DEFINED; 6950 } 6951 } 6952 } 6953 } 6954 6955 void 6956 spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits, 6957 void (*cb_fn)(void *cb_arg, int status), void *cb_arg) 6958 { 6959 struct set_qos_limit_ctx *ctx; 6960 uint32_t limit_set_complement; 6961 uint64_t min_limit_per_sec; 6962 int i; 6963 bool disable_rate_limit = true; 6964 6965 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 6966 if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 6967 continue; 6968 } 6969 6970 if (limits[i] > 0) { 6971 disable_rate_limit = false; 6972 } 6973 6974 if (bdev_qos_is_iops_rate_limit(i) == true) { 6975 min_limit_per_sec = SPDK_BDEV_QOS_MIN_IOS_PER_SEC; 6976 } else { 6977 /* Change from megabyte to byte rate limit */ 6978 limits[i] = limits[i] * 1024 * 1024; 6979 min_limit_per_sec = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC; 6980 } 6981 6982 limit_set_complement = limits[i] % min_limit_per_sec; 6983 if (limit_set_complement) { 6984 SPDK_ERRLOG("Requested rate limit %" PRIu64 " is not a multiple of %" PRIu64 "\n", 6985 limits[i], min_limit_per_sec); 6986 limits[i] += min_limit_per_sec - limit_set_complement; 6987 SPDK_ERRLOG("Round up the rate limit to %" PRIu64 "\n", limits[i]); 6988 } 6989 } 6990 6991 ctx = calloc(1, sizeof(*ctx)); 6992 if (ctx == NULL) { 6993 cb_fn(cb_arg, -ENOMEM); 6994 return; 6995 } 6996 6997 ctx->cb_fn = cb_fn; 6998 ctx->cb_arg = cb_arg; 6999 ctx->bdev = bdev; 7000 7001 pthread_mutex_lock(&bdev->internal.mutex); 7002 if (bdev->internal.qos_mod_in_progress) { 7003 pthread_mutex_unlock(&bdev->internal.mutex); 7004 free(ctx); 7005 cb_fn(cb_arg, -EAGAIN); 7006 return; 7007 } 7008 bdev->internal.qos_mod_in_progress = true; 7009 7010 if (disable_rate_limit == true && bdev->internal.qos) { 7011 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 7012 if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED && 7013 (bdev->internal.qos->rate_limits[i].limit > 0 && 7014 bdev->internal.qos->rate_limits[i].limit != 7015 SPDK_BDEV_QOS_LIMIT_NOT_DEFINED)) { 7016 disable_rate_limit = false; 7017 break; 7018 } 7019 } 7020 } 7021 7022 if (disable_rate_limit == false) { 7023 if (bdev->internal.qos == NULL) { 7024 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 7025 if (!bdev->internal.qos) { 7026 pthread_mutex_unlock(&bdev->internal.mutex); 7027 SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n"); 7028 bdev_set_qos_limit_done(ctx, -ENOMEM); 7029 return; 7030 } 7031 } 7032 7033 if (bdev->internal.qos->thread == NULL) { 7034 /* Enabling */ 7035 bdev_set_qos_rate_limits(bdev, limits); 7036 7037 spdk_for_each_channel(__bdev_to_io_dev(bdev), 7038 bdev_enable_qos_msg, ctx, 7039 bdev_enable_qos_done); 7040 } else { 7041 /* Updating */ 7042 bdev_set_qos_rate_limits(bdev, limits); 7043 7044 spdk_thread_send_msg(bdev->internal.qos->thread, 7045 bdev_update_qos_rate_limit_msg, ctx); 7046 } 7047 } else { 7048 if (bdev->internal.qos != NULL) { 7049 bdev_set_qos_rate_limits(bdev, limits); 7050 7051 /* Disabling */ 7052 spdk_for_each_channel(__bdev_to_io_dev(bdev), 7053 bdev_disable_qos_msg, ctx, 7054 bdev_disable_qos_msg_done); 7055 } else { 7056 pthread_mutex_unlock(&bdev->internal.mutex); 7057 bdev_set_qos_limit_done(ctx, 0); 7058 return; 7059 } 7060 } 7061 7062 pthread_mutex_unlock(&bdev->internal.mutex); 7063 } 7064 7065 struct spdk_bdev_histogram_ctx { 7066 spdk_bdev_histogram_status_cb cb_fn; 7067 void *cb_arg; 7068 struct spdk_bdev *bdev; 7069 int status; 7070 }; 7071 7072 static void 7073 bdev_histogram_disable_channel_cb(struct spdk_io_channel_iter *i, int status) 7074 { 7075 struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7076 7077 pthread_mutex_lock(&ctx->bdev->internal.mutex); 7078 ctx->bdev->internal.histogram_in_progress = false; 7079 pthread_mutex_unlock(&ctx->bdev->internal.mutex); 7080 ctx->cb_fn(ctx->cb_arg, ctx->status); 7081 free(ctx); 7082 } 7083 7084 static void 7085 bdev_histogram_disable_channel(struct spdk_io_channel_iter *i) 7086 { 7087 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 7088 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7089 7090 if (ch->histogram != NULL) { 7091 spdk_histogram_data_free(ch->histogram); 7092 ch->histogram = NULL; 7093 } 7094 spdk_for_each_channel_continue(i, 0); 7095 } 7096 7097 static void 7098 bdev_histogram_enable_channel_cb(struct spdk_io_channel_iter *i, int status) 7099 { 7100 struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7101 7102 if (status != 0) { 7103 ctx->status = status; 7104 ctx->bdev->internal.histogram_enabled = false; 7105 spdk_for_each_channel(__bdev_to_io_dev(ctx->bdev), bdev_histogram_disable_channel, ctx, 7106 bdev_histogram_disable_channel_cb); 7107 } else { 7108 pthread_mutex_lock(&ctx->bdev->internal.mutex); 7109 ctx->bdev->internal.histogram_in_progress = false; 7110 pthread_mutex_unlock(&ctx->bdev->internal.mutex); 7111 ctx->cb_fn(ctx->cb_arg, ctx->status); 7112 free(ctx); 7113 } 7114 } 7115 7116 static void 7117 bdev_histogram_enable_channel(struct spdk_io_channel_iter *i) 7118 { 7119 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 7120 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7121 int status = 0; 7122 7123 if (ch->histogram == NULL) { 7124 ch->histogram = spdk_histogram_data_alloc(); 7125 if (ch->histogram == NULL) { 7126 status = -ENOMEM; 7127 } 7128 } 7129 7130 spdk_for_each_channel_continue(i, status); 7131 } 7132 7133 void 7134 spdk_bdev_histogram_enable(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn, 7135 void *cb_arg, bool enable) 7136 { 7137 struct spdk_bdev_histogram_ctx *ctx; 7138 7139 ctx = calloc(1, sizeof(struct spdk_bdev_histogram_ctx)); 7140 if (ctx == NULL) { 7141 cb_fn(cb_arg, -ENOMEM); 7142 return; 7143 } 7144 7145 ctx->bdev = bdev; 7146 ctx->status = 0; 7147 ctx->cb_fn = cb_fn; 7148 ctx->cb_arg = cb_arg; 7149 7150 pthread_mutex_lock(&bdev->internal.mutex); 7151 if (bdev->internal.histogram_in_progress) { 7152 pthread_mutex_unlock(&bdev->internal.mutex); 7153 free(ctx); 7154 cb_fn(cb_arg, -EAGAIN); 7155 return; 7156 } 7157 7158 bdev->internal.histogram_in_progress = true; 7159 pthread_mutex_unlock(&bdev->internal.mutex); 7160 7161 bdev->internal.histogram_enabled = enable; 7162 7163 if (enable) { 7164 /* Allocate histogram for each channel */ 7165 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_enable_channel, ctx, 7166 bdev_histogram_enable_channel_cb); 7167 } else { 7168 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_disable_channel, ctx, 7169 bdev_histogram_disable_channel_cb); 7170 } 7171 } 7172 7173 struct spdk_bdev_histogram_data_ctx { 7174 spdk_bdev_histogram_data_cb cb_fn; 7175 void *cb_arg; 7176 struct spdk_bdev *bdev; 7177 /** merged histogram data from all channels */ 7178 struct spdk_histogram_data *histogram; 7179 }; 7180 7181 static void 7182 bdev_histogram_get_channel_cb(struct spdk_io_channel_iter *i, int status) 7183 { 7184 struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7185 7186 ctx->cb_fn(ctx->cb_arg, status, ctx->histogram); 7187 free(ctx); 7188 } 7189 7190 static void 7191 bdev_histogram_get_channel(struct spdk_io_channel_iter *i) 7192 { 7193 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 7194 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7195 struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7196 int status = 0; 7197 7198 if (ch->histogram == NULL) { 7199 status = -EFAULT; 7200 } else { 7201 spdk_histogram_data_merge(ctx->histogram, ch->histogram); 7202 } 7203 7204 spdk_for_each_channel_continue(i, status); 7205 } 7206 7207 void 7208 spdk_bdev_histogram_get(struct spdk_bdev *bdev, struct spdk_histogram_data *histogram, 7209 spdk_bdev_histogram_data_cb cb_fn, 7210 void *cb_arg) 7211 { 7212 struct spdk_bdev_histogram_data_ctx *ctx; 7213 7214 ctx = calloc(1, sizeof(struct spdk_bdev_histogram_data_ctx)); 7215 if (ctx == NULL) { 7216 cb_fn(cb_arg, -ENOMEM, NULL); 7217 return; 7218 } 7219 7220 ctx->bdev = bdev; 7221 ctx->cb_fn = cb_fn; 7222 ctx->cb_arg = cb_arg; 7223 7224 ctx->histogram = histogram; 7225 7226 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_get_channel, ctx, 7227 bdev_histogram_get_channel_cb); 7228 } 7229 7230 size_t 7231 spdk_bdev_get_media_events(struct spdk_bdev_desc *desc, struct spdk_bdev_media_event *events, 7232 size_t max_events) 7233 { 7234 struct media_event_entry *entry; 7235 size_t num_events = 0; 7236 7237 for (; num_events < max_events; ++num_events) { 7238 entry = TAILQ_FIRST(&desc->pending_media_events); 7239 if (entry == NULL) { 7240 break; 7241 } 7242 7243 events[num_events] = entry->event; 7244 TAILQ_REMOVE(&desc->pending_media_events, entry, tailq); 7245 TAILQ_INSERT_TAIL(&desc->free_media_events, entry, tailq); 7246 } 7247 7248 return num_events; 7249 } 7250 7251 int 7252 spdk_bdev_push_media_events(struct spdk_bdev *bdev, const struct spdk_bdev_media_event *events, 7253 size_t num_events) 7254 { 7255 struct spdk_bdev_desc *desc; 7256 struct media_event_entry *entry; 7257 size_t event_id; 7258 int rc = 0; 7259 7260 assert(bdev->media_events); 7261 7262 pthread_mutex_lock(&bdev->internal.mutex); 7263 TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) { 7264 if (desc->write) { 7265 break; 7266 } 7267 } 7268 7269 if (desc == NULL || desc->media_events_buffer == NULL) { 7270 rc = -ENODEV; 7271 goto out; 7272 } 7273 7274 for (event_id = 0; event_id < num_events; ++event_id) { 7275 entry = TAILQ_FIRST(&desc->free_media_events); 7276 if (entry == NULL) { 7277 break; 7278 } 7279 7280 TAILQ_REMOVE(&desc->free_media_events, entry, tailq); 7281 TAILQ_INSERT_TAIL(&desc->pending_media_events, entry, tailq); 7282 entry->event = events[event_id]; 7283 } 7284 7285 rc = event_id; 7286 out: 7287 pthread_mutex_unlock(&bdev->internal.mutex); 7288 return rc; 7289 } 7290 7291 void 7292 spdk_bdev_notify_media_management(struct spdk_bdev *bdev) 7293 { 7294 struct spdk_bdev_desc *desc; 7295 7296 pthread_mutex_lock(&bdev->internal.mutex); 7297 TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) { 7298 if (!TAILQ_EMPTY(&desc->pending_media_events)) { 7299 desc->callback.event_fn(SPDK_BDEV_EVENT_MEDIA_MANAGEMENT, bdev, 7300 desc->callback.ctx); 7301 } 7302 } 7303 pthread_mutex_unlock(&bdev->internal.mutex); 7304 } 7305 7306 struct locked_lba_range_ctx { 7307 struct lba_range range; 7308 struct spdk_bdev *bdev; 7309 struct lba_range *current_range; 7310 struct lba_range *owner_range; 7311 struct spdk_poller *poller; 7312 lock_range_cb cb_fn; 7313 void *cb_arg; 7314 }; 7315 7316 static void 7317 bdev_lock_error_cleanup_cb(struct spdk_io_channel_iter *i, int status) 7318 { 7319 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7320 7321 ctx->cb_fn(ctx->cb_arg, -ENOMEM); 7322 free(ctx); 7323 } 7324 7325 static void 7326 bdev_unlock_lba_range_get_channel(struct spdk_io_channel_iter *i); 7327 7328 static void 7329 bdev_lock_lba_range_cb(struct spdk_io_channel_iter *i, int status) 7330 { 7331 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7332 struct spdk_bdev *bdev = ctx->bdev; 7333 7334 if (status == -ENOMEM) { 7335 /* One of the channels could not allocate a range object. 7336 * So we have to go back and clean up any ranges that were 7337 * allocated successfully before we return error status to 7338 * the caller. We can reuse the unlock function to do that 7339 * clean up. 7340 */ 7341 spdk_for_each_channel(__bdev_to_io_dev(bdev), 7342 bdev_unlock_lba_range_get_channel, ctx, 7343 bdev_lock_error_cleanup_cb); 7344 return; 7345 } 7346 7347 /* All channels have locked this range and no I/O overlapping the range 7348 * are outstanding! Set the owner_ch for the range object for the 7349 * locking channel, so that this channel will know that it is allowed 7350 * to write to this range. 7351 */ 7352 ctx->owner_range->owner_ch = ctx->range.owner_ch; 7353 ctx->cb_fn(ctx->cb_arg, status); 7354 7355 /* Don't free the ctx here. Its range is in the bdev's global list of 7356 * locked ranges still, and will be removed and freed when this range 7357 * is later unlocked. 7358 */ 7359 } 7360 7361 static int 7362 bdev_lock_lba_range_check_io(void *_i) 7363 { 7364 struct spdk_io_channel_iter *i = _i; 7365 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 7366 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7367 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7368 struct lba_range *range = ctx->current_range; 7369 struct spdk_bdev_io *bdev_io; 7370 7371 spdk_poller_unregister(&ctx->poller); 7372 7373 /* The range is now in the locked_ranges, so no new IO can be submitted to this 7374 * range. But we need to wait until any outstanding IO overlapping with this range 7375 * are completed. 7376 */ 7377 TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) { 7378 if (bdev_io_range_is_locked(bdev_io, range)) { 7379 ctx->poller = SPDK_POLLER_REGISTER(bdev_lock_lba_range_check_io, i, 100); 7380 return SPDK_POLLER_BUSY; 7381 } 7382 } 7383 7384 spdk_for_each_channel_continue(i, 0); 7385 return SPDK_POLLER_BUSY; 7386 } 7387 7388 static void 7389 bdev_lock_lba_range_get_channel(struct spdk_io_channel_iter *i) 7390 { 7391 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 7392 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7393 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7394 struct lba_range *range; 7395 7396 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 7397 if (range->length == ctx->range.length && 7398 range->offset == ctx->range.offset && 7399 range->locked_ctx == ctx->range.locked_ctx) { 7400 /* This range already exists on this channel, so don't add 7401 * it again. This can happen when a new channel is created 7402 * while the for_each_channel operation is in progress. 7403 * Do not check for outstanding I/O in that case, since the 7404 * range was locked before any I/O could be submitted to the 7405 * new channel. 7406 */ 7407 spdk_for_each_channel_continue(i, 0); 7408 return; 7409 } 7410 } 7411 7412 range = calloc(1, sizeof(*range)); 7413 if (range == NULL) { 7414 spdk_for_each_channel_continue(i, -ENOMEM); 7415 return; 7416 } 7417 7418 range->length = ctx->range.length; 7419 range->offset = ctx->range.offset; 7420 range->locked_ctx = ctx->range.locked_ctx; 7421 ctx->current_range = range; 7422 if (ctx->range.owner_ch == ch) { 7423 /* This is the range object for the channel that will hold 7424 * the lock. Store it in the ctx object so that we can easily 7425 * set its owner_ch after the lock is finally acquired. 7426 */ 7427 ctx->owner_range = range; 7428 } 7429 TAILQ_INSERT_TAIL(&ch->locked_ranges, range, tailq); 7430 bdev_lock_lba_range_check_io(i); 7431 } 7432 7433 static void 7434 bdev_lock_lba_range_ctx(struct spdk_bdev *bdev, struct locked_lba_range_ctx *ctx) 7435 { 7436 assert(spdk_get_thread() == spdk_io_channel_get_thread(ctx->range.owner_ch->channel)); 7437 7438 /* We will add a copy of this range to each channel now. */ 7439 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_lock_lba_range_get_channel, ctx, 7440 bdev_lock_lba_range_cb); 7441 } 7442 7443 static bool 7444 bdev_lba_range_overlaps_tailq(struct lba_range *range, lba_range_tailq_t *tailq) 7445 { 7446 struct lba_range *r; 7447 7448 TAILQ_FOREACH(r, tailq, tailq) { 7449 if (bdev_lba_range_overlapped(range, r)) { 7450 return true; 7451 } 7452 } 7453 return false; 7454 } 7455 7456 static int 7457 bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 7458 uint64_t offset, uint64_t length, 7459 lock_range_cb cb_fn, void *cb_arg) 7460 { 7461 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 7462 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7463 struct locked_lba_range_ctx *ctx; 7464 7465 if (cb_arg == NULL) { 7466 SPDK_ERRLOG("cb_arg must not be NULL\n"); 7467 return -EINVAL; 7468 } 7469 7470 ctx = calloc(1, sizeof(*ctx)); 7471 if (ctx == NULL) { 7472 return -ENOMEM; 7473 } 7474 7475 ctx->range.offset = offset; 7476 ctx->range.length = length; 7477 ctx->range.owner_ch = ch; 7478 ctx->range.locked_ctx = cb_arg; 7479 ctx->bdev = bdev; 7480 ctx->cb_fn = cb_fn; 7481 ctx->cb_arg = cb_arg; 7482 7483 pthread_mutex_lock(&bdev->internal.mutex); 7484 if (bdev_lba_range_overlaps_tailq(&ctx->range, &bdev->internal.locked_ranges)) { 7485 /* There is an active lock overlapping with this range. 7486 * Put it on the pending list until this range no 7487 * longer overlaps with another. 7488 */ 7489 TAILQ_INSERT_TAIL(&bdev->internal.pending_locked_ranges, &ctx->range, tailq); 7490 } else { 7491 TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, &ctx->range, tailq); 7492 bdev_lock_lba_range_ctx(bdev, ctx); 7493 } 7494 pthread_mutex_unlock(&bdev->internal.mutex); 7495 return 0; 7496 } 7497 7498 static void 7499 bdev_lock_lba_range_ctx_msg(void *_ctx) 7500 { 7501 struct locked_lba_range_ctx *ctx = _ctx; 7502 7503 bdev_lock_lba_range_ctx(ctx->bdev, ctx); 7504 } 7505 7506 static void 7507 bdev_unlock_lba_range_cb(struct spdk_io_channel_iter *i, int status) 7508 { 7509 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7510 struct locked_lba_range_ctx *pending_ctx; 7511 struct spdk_bdev_channel *ch = ctx->range.owner_ch; 7512 struct spdk_bdev *bdev = ch->bdev; 7513 struct lba_range *range, *tmp; 7514 7515 pthread_mutex_lock(&bdev->internal.mutex); 7516 /* Check if there are any pending locked ranges that overlap with this range 7517 * that was just unlocked. If there are, check that it doesn't overlap with any 7518 * other locked ranges before calling bdev_lock_lba_range_ctx which will start 7519 * the lock process. 7520 */ 7521 TAILQ_FOREACH_SAFE(range, &bdev->internal.pending_locked_ranges, tailq, tmp) { 7522 if (bdev_lba_range_overlapped(range, &ctx->range) && 7523 !bdev_lba_range_overlaps_tailq(range, &bdev->internal.locked_ranges)) { 7524 TAILQ_REMOVE(&bdev->internal.pending_locked_ranges, range, tailq); 7525 pending_ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range); 7526 TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, range, tailq); 7527 spdk_thread_send_msg(spdk_io_channel_get_thread(pending_ctx->range.owner_ch->channel), 7528 bdev_lock_lba_range_ctx_msg, pending_ctx); 7529 } 7530 } 7531 pthread_mutex_unlock(&bdev->internal.mutex); 7532 7533 ctx->cb_fn(ctx->cb_arg, status); 7534 free(ctx); 7535 } 7536 7537 static void 7538 bdev_unlock_lba_range_get_channel(struct spdk_io_channel_iter *i) 7539 { 7540 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 7541 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7542 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 7543 TAILQ_HEAD(, spdk_bdev_io) io_locked; 7544 struct spdk_bdev_io *bdev_io; 7545 struct lba_range *range; 7546 7547 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 7548 if (ctx->range.offset == range->offset && 7549 ctx->range.length == range->length && 7550 ctx->range.locked_ctx == range->locked_ctx) { 7551 TAILQ_REMOVE(&ch->locked_ranges, range, tailq); 7552 free(range); 7553 break; 7554 } 7555 } 7556 7557 /* Note: we should almost always be able to assert that the range specified 7558 * was found. But there are some very rare corner cases where a new channel 7559 * gets created simultaneously with a range unlock, where this function 7560 * would execute on that new channel and wouldn't have the range. 7561 * We also use this to clean up range allocations when a later allocation 7562 * fails in the locking path. 7563 * So we can't actually assert() here. 7564 */ 7565 7566 /* Swap the locked IO into a temporary list, and then try to submit them again. 7567 * We could hyper-optimize this to only resubmit locked I/O that overlap 7568 * with the range that was just unlocked, but this isn't a performance path so 7569 * we go for simplicity here. 7570 */ 7571 TAILQ_INIT(&io_locked); 7572 TAILQ_SWAP(&ch->io_locked, &io_locked, spdk_bdev_io, internal.ch_link); 7573 while (!TAILQ_EMPTY(&io_locked)) { 7574 bdev_io = TAILQ_FIRST(&io_locked); 7575 TAILQ_REMOVE(&io_locked, bdev_io, internal.ch_link); 7576 bdev_io_submit(bdev_io); 7577 } 7578 7579 spdk_for_each_channel_continue(i, 0); 7580 } 7581 7582 static int 7583 bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 7584 uint64_t offset, uint64_t length, 7585 lock_range_cb cb_fn, void *cb_arg) 7586 { 7587 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 7588 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 7589 struct locked_lba_range_ctx *ctx; 7590 struct lba_range *range; 7591 bool range_found = false; 7592 7593 /* Let's make sure the specified channel actually has a lock on 7594 * the specified range. Note that the range must match exactly. 7595 */ 7596 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 7597 if (range->offset == offset && range->length == length && 7598 range->owner_ch == ch && range->locked_ctx == cb_arg) { 7599 range_found = true; 7600 break; 7601 } 7602 } 7603 7604 if (!range_found) { 7605 return -EINVAL; 7606 } 7607 7608 pthread_mutex_lock(&bdev->internal.mutex); 7609 /* We confirmed that this channel has locked the specified range. To 7610 * start the unlock the process, we find the range in the bdev's locked_ranges 7611 * and remove it. This ensures new channels don't inherit the locked range. 7612 * Then we will send a message to each channel (including the one specified 7613 * here) to remove the range from its per-channel list. 7614 */ 7615 TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) { 7616 if (range->offset == offset && range->length == length && 7617 range->locked_ctx == cb_arg) { 7618 break; 7619 } 7620 } 7621 if (range == NULL) { 7622 assert(false); 7623 pthread_mutex_unlock(&bdev->internal.mutex); 7624 return -EINVAL; 7625 } 7626 TAILQ_REMOVE(&bdev->internal.locked_ranges, range, tailq); 7627 ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range); 7628 pthread_mutex_unlock(&bdev->internal.mutex); 7629 7630 ctx->cb_fn = cb_fn; 7631 ctx->cb_arg = cb_arg; 7632 7633 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_unlock_lba_range_get_channel, ctx, 7634 bdev_unlock_lba_range_cb); 7635 return 0; 7636 } 7637 7638 int 7639 spdk_bdev_get_memory_domains(struct spdk_bdev *bdev, struct spdk_memory_domain **domains, 7640 int array_size) 7641 { 7642 if (!bdev) { 7643 return -EINVAL; 7644 } 7645 7646 if (bdev->fn_table->get_memory_domains) { 7647 return bdev->fn_table->get_memory_domains(bdev->ctxt, domains, array_size); 7648 } 7649 7650 return 0; 7651 } 7652 7653 SPDK_LOG_REGISTER_COMPONENT(bdev) 7654 7655 SPDK_TRACE_REGISTER_FN(bdev_trace, "bdev", TRACE_GROUP_BDEV) 7656 { 7657 struct spdk_trace_tpoint_opts opts[] = { 7658 { 7659 "BDEV_IO_START", TRACE_BDEV_IO_START, 7660 OWNER_BDEV, OBJECT_BDEV_IO, 1, 7661 { 7662 { "type", SPDK_TRACE_ARG_TYPE_INT, 8 }, 7663 { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }, 7664 { "offset", SPDK_TRACE_ARG_TYPE_INT, 8 }, 7665 { "len", SPDK_TRACE_ARG_TYPE_INT, 8 } 7666 } 7667 }, 7668 { 7669 "BDEV_IO_DONE", TRACE_BDEV_IO_DONE, 7670 OWNER_BDEV, OBJECT_BDEV_IO, 0, 7671 {{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }} 7672 }, 7673 { 7674 "BDEV_IOCH_CREATE", TRACE_BDEV_IOCH_CREATE, 7675 OWNER_BDEV, OBJECT_NONE, 1, 7676 { 7677 { "name", SPDK_TRACE_ARG_TYPE_STR, 40 }, 7678 { "thread_id", SPDK_TRACE_ARG_TYPE_INT, 8} 7679 } 7680 }, 7681 { 7682 "BDEV_IOCH_DESTROY", TRACE_BDEV_IOCH_DESTROY, 7683 OWNER_BDEV, OBJECT_NONE, 0, 7684 { 7685 { "name", SPDK_TRACE_ARG_TYPE_STR, 40 }, 7686 { "thread_id", SPDK_TRACE_ARG_TYPE_INT, 8} 7687 } 7688 }, 7689 }; 7690 7691 7692 spdk_trace_register_owner(OWNER_BDEV, 'b'); 7693 spdk_trace_register_object(OBJECT_BDEV_IO, 'i'); 7694 spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts)); 7695 } 7696