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