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