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