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