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