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