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