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