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