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