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