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