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