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