1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/bdev.h" 37 #include "spdk/conf.h" 38 39 #include "spdk/config.h" 40 #include "spdk/env.h" 41 #include "spdk/event.h" 42 #include "spdk/thread.h" 43 #include "spdk/likely.h" 44 #include "spdk/queue.h" 45 #include "spdk/nvme_spec.h" 46 #include "spdk/scsi_spec.h" 47 #include "spdk/notify.h" 48 #include "spdk/util.h" 49 #include "spdk/trace.h" 50 51 #include "spdk/bdev_module.h" 52 #include "spdk_internal/log.h" 53 #include "spdk/string.h" 54 55 #include "bdev_internal.h" 56 57 #ifdef SPDK_CONFIG_VTUNE 58 #include "ittnotify.h" 59 #include "ittnotify_types.h" 60 int __itt_init_ittlib(const char *, __itt_group_id); 61 #endif 62 63 #define SPDK_BDEV_IO_POOL_SIZE (64 * 1024 - 1) 64 #define SPDK_BDEV_IO_CACHE_SIZE 256 65 #define BUF_SMALL_POOL_SIZE 8191 66 #define BUF_LARGE_POOL_SIZE 1023 67 #define NOMEM_THRESHOLD_COUNT 8 68 #define ZERO_BUFFER_SIZE 0x100000 69 70 #define OWNER_BDEV 0x2 71 72 #define OBJECT_BDEV_IO 0x2 73 74 #define TRACE_GROUP_BDEV 0x3 75 #define TRACE_BDEV_IO_START SPDK_TPOINT_ID(TRACE_GROUP_BDEV, 0x0) 76 #define TRACE_BDEV_IO_DONE SPDK_TPOINT_ID(TRACE_GROUP_BDEV, 0x1) 77 78 #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC 1000 79 #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE 1 80 #define SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE 512 81 #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC 1000 82 #define SPDK_BDEV_QOS_MIN_BYTES_PER_SEC (1024 * 1024) 83 #define SPDK_BDEV_QOS_LIMIT_NOT_DEFINED UINT64_MAX 84 #define SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC 1000 85 86 #define SPDK_BDEV_POOL_ALIGNMENT 512 87 88 static const char *qos_conf_type[] = {"Limit_IOPS", 89 "Limit_BPS", "Limit_Read_BPS", "Limit_Write_BPS" 90 }; 91 static const char *qos_rpc_type[] = {"rw_ios_per_sec", 92 "rw_mbytes_per_sec", "r_mbytes_per_sec", "w_mbytes_per_sec" 93 }; 94 95 TAILQ_HEAD(spdk_bdev_list, spdk_bdev); 96 97 struct spdk_bdev_mgr { 98 struct spdk_mempool *bdev_io_pool; 99 100 struct spdk_mempool *buf_small_pool; 101 struct spdk_mempool *buf_large_pool; 102 103 void *zero_buffer; 104 105 TAILQ_HEAD(bdev_module_list, spdk_bdev_module) bdev_modules; 106 107 struct spdk_bdev_list bdevs; 108 109 bool init_complete; 110 bool module_init_complete; 111 112 pthread_mutex_t mutex; 113 114 #ifdef SPDK_CONFIG_VTUNE 115 __itt_domain *domain; 116 #endif 117 }; 118 119 static struct spdk_bdev_mgr g_bdev_mgr = { 120 .bdev_modules = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdev_modules), 121 .bdevs = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdevs), 122 .init_complete = false, 123 .module_init_complete = false, 124 .mutex = PTHREAD_MUTEX_INITIALIZER, 125 }; 126 127 typedef void (*lock_range_cb)(void *ctx, int status); 128 129 struct lba_range { 130 uint64_t offset; 131 uint64_t length; 132 void *locked_ctx; 133 struct spdk_bdev_channel *owner_ch; 134 TAILQ_ENTRY(lba_range) tailq; 135 }; 136 137 static struct spdk_bdev_opts g_bdev_opts = { 138 .bdev_io_pool_size = SPDK_BDEV_IO_POOL_SIZE, 139 .bdev_io_cache_size = SPDK_BDEV_IO_CACHE_SIZE, 140 }; 141 142 static spdk_bdev_init_cb g_init_cb_fn = NULL; 143 static void *g_init_cb_arg = NULL; 144 145 static spdk_bdev_fini_cb g_fini_cb_fn = NULL; 146 static void *g_fini_cb_arg = NULL; 147 static struct spdk_thread *g_fini_thread = NULL; 148 149 struct spdk_bdev_qos_limit { 150 /** IOs or bytes allowed per second (i.e., 1s). */ 151 uint64_t limit; 152 153 /** Remaining IOs or bytes allowed in current timeslice (e.g., 1ms). 154 * For remaining bytes, allowed to run negative if an I/O is submitted when 155 * some bytes are remaining, but the I/O is bigger than that amount. The 156 * excess will be deducted from the next timeslice. 157 */ 158 int64_t remaining_this_timeslice; 159 160 /** Minimum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */ 161 uint32_t min_per_timeslice; 162 163 /** Maximum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */ 164 uint32_t max_per_timeslice; 165 166 /** Function to check whether to queue the IO. */ 167 bool (*queue_io)(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io); 168 169 /** Function to update for the submitted IO. */ 170 void (*update_quota)(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io); 171 }; 172 173 struct spdk_bdev_qos { 174 /** Types of structure of rate limits. */ 175 struct spdk_bdev_qos_limit rate_limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES]; 176 177 /** The channel that all I/O are funneled through. */ 178 struct spdk_bdev_channel *ch; 179 180 /** The thread on which the poller is running. */ 181 struct spdk_thread *thread; 182 183 /** Queue of I/O waiting to be issued. */ 184 bdev_io_tailq_t queued; 185 186 /** Size of a timeslice in tsc ticks. */ 187 uint64_t timeslice_size; 188 189 /** Timestamp of start of last timeslice. */ 190 uint64_t last_timeslice; 191 192 /** Poller that processes queued I/O commands each time slice. */ 193 struct spdk_poller *poller; 194 }; 195 196 struct spdk_bdev_mgmt_channel { 197 bdev_io_stailq_t need_buf_small; 198 bdev_io_stailq_t need_buf_large; 199 200 /* 201 * Each thread keeps a cache of bdev_io - this allows 202 * bdev threads which are *not* DPDK threads to still 203 * benefit from a per-thread bdev_io cache. Without 204 * this, non-DPDK threads fetching from the mempool 205 * incur a cmpxchg on get and put. 206 */ 207 bdev_io_stailq_t per_thread_cache; 208 uint32_t per_thread_cache_count; 209 uint32_t bdev_io_cache_size; 210 211 TAILQ_HEAD(, spdk_bdev_shared_resource) shared_resources; 212 TAILQ_HEAD(, spdk_bdev_io_wait_entry) io_wait_queue; 213 }; 214 215 /* 216 * Per-module (or per-io_device) data. Multiple bdevs built on the same io_device 217 * will queue here their IO that awaits retry. It makes it possible to retry sending 218 * IO to one bdev after IO from other bdev completes. 219 */ 220 struct spdk_bdev_shared_resource { 221 /* The bdev management channel */ 222 struct spdk_bdev_mgmt_channel *mgmt_ch; 223 224 /* 225 * Count of I/O submitted to bdev module and waiting for completion. 226 * Incremented before submit_request() is called on an spdk_bdev_io. 227 */ 228 uint64_t io_outstanding; 229 230 /* 231 * Queue of IO awaiting retry because of a previous NOMEM status returned 232 * on this channel. 233 */ 234 bdev_io_tailq_t nomem_io; 235 236 /* 237 * Threshold which io_outstanding must drop to before retrying nomem_io. 238 */ 239 uint64_t nomem_threshold; 240 241 /* I/O channel allocated by a bdev module */ 242 struct spdk_io_channel *shared_ch; 243 244 /* Refcount of bdev channels using this resource */ 245 uint32_t ref; 246 247 TAILQ_ENTRY(spdk_bdev_shared_resource) link; 248 }; 249 250 #define BDEV_CH_RESET_IN_PROGRESS (1 << 0) 251 #define BDEV_CH_QOS_ENABLED (1 << 1) 252 253 struct spdk_bdev_channel { 254 struct spdk_bdev *bdev; 255 256 /* The channel for the underlying device */ 257 struct spdk_io_channel *channel; 258 259 /* Per io_device per thread data */ 260 struct spdk_bdev_shared_resource *shared_resource; 261 262 struct spdk_bdev_io_stat stat; 263 264 /* 265 * Count of I/O submitted to the underlying dev module through this channel 266 * and waiting for completion. 267 */ 268 uint64_t io_outstanding; 269 270 /* 271 * List of spdk_bdev_io directly associated with a call to the public bdev API. 272 * It does not include any spdk_bdev_io that are generated via splitting. 273 */ 274 bdev_io_tailq_t io_submitted; 275 276 /* 277 * List of spdk_bdev_io that are currently queued because they write to a locked 278 * LBA range. 279 */ 280 bdev_io_tailq_t io_locked; 281 282 uint32_t flags; 283 284 struct spdk_histogram_data *histogram; 285 286 #ifdef SPDK_CONFIG_VTUNE 287 uint64_t start_tsc; 288 uint64_t interval_tsc; 289 __itt_string_handle *handle; 290 struct spdk_bdev_io_stat prev_stat; 291 #endif 292 293 bdev_io_tailq_t queued_resets; 294 295 lba_range_tailq_t locked_ranges; 296 }; 297 298 struct media_event_entry { 299 struct spdk_bdev_media_event event; 300 TAILQ_ENTRY(media_event_entry) tailq; 301 }; 302 303 #define MEDIA_EVENT_POOL_SIZE 64 304 305 struct spdk_bdev_desc { 306 struct spdk_bdev *bdev; 307 struct spdk_thread *thread; 308 struct { 309 bool open_with_ext; 310 union { 311 spdk_bdev_remove_cb_t remove_fn; 312 spdk_bdev_event_cb_t event_fn; 313 }; 314 void *ctx; 315 } callback; 316 bool closed; 317 bool write; 318 pthread_mutex_t mutex; 319 uint32_t refs; 320 TAILQ_HEAD(, media_event_entry) pending_media_events; 321 TAILQ_HEAD(, media_event_entry) free_media_events; 322 struct media_event_entry *media_events_buffer; 323 TAILQ_ENTRY(spdk_bdev_desc) link; 324 325 uint64_t timeout_in_sec; 326 spdk_bdev_io_timeout_cb cb_fn; 327 void *cb_arg; 328 struct spdk_poller *io_timeout_poller; 329 }; 330 331 struct spdk_bdev_iostat_ctx { 332 struct spdk_bdev_io_stat *stat; 333 spdk_bdev_get_device_stat_cb cb; 334 void *cb_arg; 335 }; 336 337 struct set_qos_limit_ctx { 338 void (*cb_fn)(void *cb_arg, int status); 339 void *cb_arg; 340 struct spdk_bdev *bdev; 341 }; 342 343 #define __bdev_to_io_dev(bdev) (((char *)bdev) + 1) 344 #define __bdev_from_io_dev(io_dev) ((struct spdk_bdev *)(((char *)io_dev) - 1)) 345 346 static void bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 347 static void bdev_write_zero_buffer_next(void *_bdev_io); 348 349 static void bdev_enable_qos_msg(struct spdk_io_channel_iter *i); 350 static void bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status); 351 352 static int 353 bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 354 struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks, 355 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg); 356 static int 357 bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 358 struct iovec *iov, int iovcnt, void *md_buf, 359 uint64_t offset_blocks, uint64_t num_blocks, 360 spdk_bdev_io_completion_cb cb, void *cb_arg); 361 362 void 363 spdk_bdev_get_opts(struct spdk_bdev_opts *opts) 364 { 365 *opts = g_bdev_opts; 366 } 367 368 int 369 spdk_bdev_set_opts(struct spdk_bdev_opts *opts) 370 { 371 uint32_t min_pool_size; 372 373 /* 374 * Add 1 to the thread count to account for the extra mgmt_ch that gets created during subsystem 375 * initialization. A second mgmt_ch will be created on the same thread when the application starts 376 * but before the deferred put_io_channel event is executed for the first mgmt_ch. 377 */ 378 min_pool_size = opts->bdev_io_cache_size * (spdk_thread_get_count() + 1); 379 if (opts->bdev_io_pool_size < min_pool_size) { 380 SPDK_ERRLOG("bdev_io_pool_size %" PRIu32 " is not compatible with bdev_io_cache_size %" PRIu32 381 " and %" PRIu32 " threads\n", opts->bdev_io_pool_size, opts->bdev_io_cache_size, 382 spdk_thread_get_count()); 383 SPDK_ERRLOG("bdev_io_pool_size must be at least %" PRIu32 "\n", min_pool_size); 384 return -1; 385 } 386 387 g_bdev_opts = *opts; 388 return 0; 389 } 390 391 struct spdk_bdev * 392 spdk_bdev_first(void) 393 { 394 struct spdk_bdev *bdev; 395 396 bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs); 397 if (bdev) { 398 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Starting bdev iteration at %s\n", bdev->name); 399 } 400 401 return bdev; 402 } 403 404 struct spdk_bdev * 405 spdk_bdev_next(struct spdk_bdev *prev) 406 { 407 struct spdk_bdev *bdev; 408 409 bdev = TAILQ_NEXT(prev, internal.link); 410 if (bdev) { 411 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Continuing bdev iteration at %s\n", bdev->name); 412 } 413 414 return bdev; 415 } 416 417 static struct spdk_bdev * 418 _bdev_next_leaf(struct spdk_bdev *bdev) 419 { 420 while (bdev != NULL) { 421 if (bdev->internal.claim_module == NULL) { 422 return bdev; 423 } else { 424 bdev = TAILQ_NEXT(bdev, internal.link); 425 } 426 } 427 428 return bdev; 429 } 430 431 struct spdk_bdev * 432 spdk_bdev_first_leaf(void) 433 { 434 struct spdk_bdev *bdev; 435 436 bdev = _bdev_next_leaf(TAILQ_FIRST(&g_bdev_mgr.bdevs)); 437 438 if (bdev) { 439 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Starting bdev iteration at %s\n", bdev->name); 440 } 441 442 return bdev; 443 } 444 445 struct spdk_bdev * 446 spdk_bdev_next_leaf(struct spdk_bdev *prev) 447 { 448 struct spdk_bdev *bdev; 449 450 bdev = _bdev_next_leaf(TAILQ_NEXT(prev, internal.link)); 451 452 if (bdev) { 453 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Continuing bdev iteration at %s\n", bdev->name); 454 } 455 456 return bdev; 457 } 458 459 struct spdk_bdev * 460 spdk_bdev_get_by_name(const char *bdev_name) 461 { 462 struct spdk_bdev_alias *tmp; 463 struct spdk_bdev *bdev = spdk_bdev_first(); 464 465 while (bdev != NULL) { 466 if (strcmp(bdev_name, bdev->name) == 0) { 467 return bdev; 468 } 469 470 TAILQ_FOREACH(tmp, &bdev->aliases, tailq) { 471 if (strcmp(bdev_name, tmp->alias) == 0) { 472 return bdev; 473 } 474 } 475 476 bdev = spdk_bdev_next(bdev); 477 } 478 479 return NULL; 480 } 481 482 void 483 spdk_bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len) 484 { 485 struct iovec *iovs; 486 487 if (bdev_io->u.bdev.iovs == NULL) { 488 bdev_io->u.bdev.iovs = &bdev_io->iov; 489 bdev_io->u.bdev.iovcnt = 1; 490 } 491 492 iovs = bdev_io->u.bdev.iovs; 493 494 assert(iovs != NULL); 495 assert(bdev_io->u.bdev.iovcnt >= 1); 496 497 iovs[0].iov_base = buf; 498 iovs[0].iov_len = len; 499 } 500 501 void 502 spdk_bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len) 503 { 504 assert((len / spdk_bdev_get_md_size(bdev_io->bdev)) >= bdev_io->u.bdev.num_blocks); 505 bdev_io->u.bdev.md_buf = md_buf; 506 } 507 508 static bool 509 _is_buf_allocated(const struct iovec *iovs) 510 { 511 if (iovs == NULL) { 512 return false; 513 } 514 515 return iovs[0].iov_base != NULL; 516 } 517 518 static bool 519 _are_iovs_aligned(struct iovec *iovs, int iovcnt, uint32_t alignment) 520 { 521 int i; 522 uintptr_t iov_base; 523 524 if (spdk_likely(alignment == 1)) { 525 return true; 526 } 527 528 for (i = 0; i < iovcnt; i++) { 529 iov_base = (uintptr_t)iovs[i].iov_base; 530 if ((iov_base & (alignment - 1)) != 0) { 531 return false; 532 } 533 } 534 535 return true; 536 } 537 538 static void 539 _copy_iovs_to_buf(void *buf, size_t buf_len, struct iovec *iovs, int iovcnt) 540 { 541 int i; 542 size_t len; 543 544 for (i = 0; i < iovcnt; i++) { 545 len = spdk_min(iovs[i].iov_len, buf_len); 546 memcpy(buf, iovs[i].iov_base, len); 547 buf += len; 548 buf_len -= len; 549 } 550 } 551 552 static void 553 _copy_buf_to_iovs(struct iovec *iovs, int iovcnt, void *buf, size_t buf_len) 554 { 555 int i; 556 size_t len; 557 558 for (i = 0; i < iovcnt; i++) { 559 len = spdk_min(iovs[i].iov_len, buf_len); 560 memcpy(iovs[i].iov_base, buf, len); 561 buf += len; 562 buf_len -= len; 563 } 564 } 565 566 static void 567 _bdev_io_set_bounce_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len) 568 { 569 /* save original iovec */ 570 bdev_io->internal.orig_iovs = bdev_io->u.bdev.iovs; 571 bdev_io->internal.orig_iovcnt = bdev_io->u.bdev.iovcnt; 572 /* set bounce iov */ 573 bdev_io->u.bdev.iovs = &bdev_io->internal.bounce_iov; 574 bdev_io->u.bdev.iovcnt = 1; 575 /* set bounce buffer for this operation */ 576 bdev_io->u.bdev.iovs[0].iov_base = buf; 577 bdev_io->u.bdev.iovs[0].iov_len = len; 578 /* if this is write path, copy data from original buffer to bounce buffer */ 579 if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 580 _copy_iovs_to_buf(buf, len, bdev_io->internal.orig_iovs, bdev_io->internal.orig_iovcnt); 581 } 582 } 583 584 static void 585 _bdev_io_set_bounce_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len) 586 { 587 /* save original md_buf */ 588 bdev_io->internal.orig_md_buf = bdev_io->u.bdev.md_buf; 589 /* set bounce md_buf */ 590 bdev_io->u.bdev.md_buf = md_buf; 591 592 if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 593 memcpy(md_buf, bdev_io->internal.orig_md_buf, len); 594 } 595 } 596 597 static void 598 _bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t len) 599 { 600 struct spdk_bdev *bdev = bdev_io->bdev; 601 bool buf_allocated; 602 uint64_t md_len, alignment; 603 void *aligned_buf; 604 605 alignment = spdk_bdev_get_buf_align(bdev); 606 buf_allocated = _is_buf_allocated(bdev_io->u.bdev.iovs); 607 aligned_buf = (void *)(((uintptr_t)buf + (alignment - 1)) & ~(alignment - 1)); 608 609 if (buf_allocated) { 610 _bdev_io_set_bounce_buf(bdev_io, aligned_buf, len); 611 } else { 612 spdk_bdev_io_set_buf(bdev_io, aligned_buf, len); 613 } 614 615 if (spdk_bdev_is_md_separate(bdev)) { 616 aligned_buf = (char *)aligned_buf + len; 617 md_len = bdev_io->u.bdev.num_blocks * bdev->md_len; 618 619 assert(((uintptr_t)aligned_buf & (alignment - 1)) == 0); 620 621 if (bdev_io->u.bdev.md_buf != NULL) { 622 _bdev_io_set_bounce_md_buf(bdev_io, aligned_buf, md_len); 623 } else { 624 spdk_bdev_io_set_md_buf(bdev_io, aligned_buf, md_len); 625 } 626 } 627 628 bdev_io->internal.buf = buf; 629 bdev_io->internal.get_buf_cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true); 630 } 631 632 static void 633 bdev_io_put_buf(struct spdk_bdev_io *bdev_io) 634 { 635 struct spdk_bdev *bdev = bdev_io->bdev; 636 struct spdk_mempool *pool; 637 struct spdk_bdev_io *tmp; 638 bdev_io_stailq_t *stailq; 639 struct spdk_bdev_mgmt_channel *ch; 640 uint64_t buf_len, md_len, alignment; 641 void *buf; 642 643 buf = bdev_io->internal.buf; 644 buf_len = bdev_io->internal.buf_len; 645 md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0; 646 alignment = spdk_bdev_get_buf_align(bdev); 647 ch = bdev_io->internal.ch->shared_resource->mgmt_ch; 648 649 bdev_io->internal.buf = NULL; 650 651 if (buf_len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) + 652 SPDK_BDEV_POOL_ALIGNMENT) { 653 pool = g_bdev_mgr.buf_small_pool; 654 stailq = &ch->need_buf_small; 655 } else { 656 pool = g_bdev_mgr.buf_large_pool; 657 stailq = &ch->need_buf_large; 658 } 659 660 if (STAILQ_EMPTY(stailq)) { 661 spdk_mempool_put(pool, buf); 662 } else { 663 tmp = STAILQ_FIRST(stailq); 664 STAILQ_REMOVE_HEAD(stailq, internal.buf_link); 665 _bdev_io_set_buf(tmp, buf, tmp->internal.buf_len); 666 } 667 } 668 669 static void 670 _bdev_io_unset_bounce_buf(struct spdk_bdev_io *bdev_io) 671 { 672 if (spdk_likely(bdev_io->internal.orig_iovcnt == 0)) { 673 assert(bdev_io->internal.orig_md_buf == NULL); 674 return; 675 } 676 677 /* if this is read path, copy data from bounce buffer to original buffer */ 678 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ && 679 bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 680 _copy_buf_to_iovs(bdev_io->internal.orig_iovs, 681 bdev_io->internal.orig_iovcnt, 682 bdev_io->internal.bounce_iov.iov_base, 683 bdev_io->internal.bounce_iov.iov_len); 684 } 685 /* set orignal buffer for this io */ 686 bdev_io->u.bdev.iovcnt = bdev_io->internal.orig_iovcnt; 687 bdev_io->u.bdev.iovs = bdev_io->internal.orig_iovs; 688 /* disable bouncing buffer for this io */ 689 bdev_io->internal.orig_iovcnt = 0; 690 bdev_io->internal.orig_iovs = NULL; 691 692 /* do the same for metadata buffer */ 693 if (spdk_unlikely(bdev_io->internal.orig_md_buf != NULL)) { 694 assert(spdk_bdev_is_md_separate(bdev_io->bdev)); 695 696 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ && 697 bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 698 memcpy(bdev_io->internal.orig_md_buf, bdev_io->u.bdev.md_buf, 699 bdev_io->u.bdev.num_blocks * spdk_bdev_get_md_size(bdev_io->bdev)); 700 } 701 702 bdev_io->u.bdev.md_buf = bdev_io->internal.orig_md_buf; 703 bdev_io->internal.orig_md_buf = NULL; 704 } 705 706 bdev_io_put_buf(bdev_io); 707 } 708 709 void 710 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len) 711 { 712 struct spdk_bdev *bdev = bdev_io->bdev; 713 struct spdk_mempool *pool; 714 bdev_io_stailq_t *stailq; 715 struct spdk_bdev_mgmt_channel *mgmt_ch; 716 uint64_t alignment, md_len; 717 void *buf; 718 719 assert(cb != NULL); 720 721 alignment = spdk_bdev_get_buf_align(bdev); 722 md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0; 723 724 if (_is_buf_allocated(bdev_io->u.bdev.iovs) && 725 _are_iovs_aligned(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, alignment)) { 726 /* Buffer already present and aligned */ 727 cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true); 728 return; 729 } 730 731 if (len + alignment + md_len > SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) + 732 SPDK_BDEV_POOL_ALIGNMENT) { 733 SPDK_ERRLOG("Length + alignment %" PRIu64 " is larger than allowed\n", 734 len + alignment); 735 cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, false); 736 return; 737 } 738 739 mgmt_ch = bdev_io->internal.ch->shared_resource->mgmt_ch; 740 741 bdev_io->internal.buf_len = len; 742 bdev_io->internal.get_buf_cb = cb; 743 744 if (len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) + 745 SPDK_BDEV_POOL_ALIGNMENT) { 746 pool = g_bdev_mgr.buf_small_pool; 747 stailq = &mgmt_ch->need_buf_small; 748 } else { 749 pool = g_bdev_mgr.buf_large_pool; 750 stailq = &mgmt_ch->need_buf_large; 751 } 752 753 buf = spdk_mempool_get(pool); 754 if (!buf) { 755 STAILQ_INSERT_TAIL(stailq, bdev_io, internal.buf_link); 756 } else { 757 _bdev_io_set_buf(bdev_io, buf, len); 758 } 759 } 760 761 static int 762 bdev_module_get_max_ctx_size(void) 763 { 764 struct spdk_bdev_module *bdev_module; 765 int max_bdev_module_size = 0; 766 767 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 768 if (bdev_module->get_ctx_size && bdev_module->get_ctx_size() > max_bdev_module_size) { 769 max_bdev_module_size = bdev_module->get_ctx_size(); 770 } 771 } 772 773 return max_bdev_module_size; 774 } 775 776 void 777 spdk_bdev_config_text(FILE *fp) 778 { 779 struct spdk_bdev_module *bdev_module; 780 781 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 782 if (bdev_module->config_text) { 783 bdev_module->config_text(fp); 784 } 785 } 786 } 787 788 static void 789 bdev_qos_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 790 { 791 int i; 792 struct spdk_bdev_qos *qos = bdev->internal.qos; 793 uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES]; 794 795 if (!qos) { 796 return; 797 } 798 799 spdk_bdev_get_qos_rate_limits(bdev, limits); 800 801 spdk_json_write_object_begin(w); 802 spdk_json_write_named_string(w, "method", "bdev_set_qos_limit"); 803 804 spdk_json_write_named_object_begin(w, "params"); 805 spdk_json_write_named_string(w, "name", bdev->name); 806 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 807 if (limits[i] > 0) { 808 spdk_json_write_named_uint64(w, qos_rpc_type[i], limits[i]); 809 } 810 } 811 spdk_json_write_object_end(w); 812 813 spdk_json_write_object_end(w); 814 } 815 816 void 817 spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w) 818 { 819 struct spdk_bdev_module *bdev_module; 820 struct spdk_bdev *bdev; 821 822 assert(w != NULL); 823 824 spdk_json_write_array_begin(w); 825 826 spdk_json_write_object_begin(w); 827 spdk_json_write_named_string(w, "method", "bdev_set_options"); 828 spdk_json_write_named_object_begin(w, "params"); 829 spdk_json_write_named_uint32(w, "bdev_io_pool_size", g_bdev_opts.bdev_io_pool_size); 830 spdk_json_write_named_uint32(w, "bdev_io_cache_size", g_bdev_opts.bdev_io_cache_size); 831 spdk_json_write_object_end(w); 832 spdk_json_write_object_end(w); 833 834 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 835 if (bdev_module->config_json) { 836 bdev_module->config_json(w); 837 } 838 } 839 840 pthread_mutex_lock(&g_bdev_mgr.mutex); 841 842 TAILQ_FOREACH(bdev, &g_bdev_mgr.bdevs, internal.link) { 843 if (bdev->fn_table->write_config_json) { 844 bdev->fn_table->write_config_json(bdev, w); 845 } 846 847 bdev_qos_config_json(bdev, w); 848 } 849 850 pthread_mutex_unlock(&g_bdev_mgr.mutex); 851 852 spdk_json_write_array_end(w); 853 } 854 855 static int 856 bdev_mgmt_channel_create(void *io_device, void *ctx_buf) 857 { 858 struct spdk_bdev_mgmt_channel *ch = ctx_buf; 859 struct spdk_bdev_io *bdev_io; 860 uint32_t i; 861 862 STAILQ_INIT(&ch->need_buf_small); 863 STAILQ_INIT(&ch->need_buf_large); 864 865 STAILQ_INIT(&ch->per_thread_cache); 866 ch->bdev_io_cache_size = g_bdev_opts.bdev_io_cache_size; 867 868 /* Pre-populate bdev_io cache to ensure this thread cannot be starved. */ 869 ch->per_thread_cache_count = 0; 870 for (i = 0; i < ch->bdev_io_cache_size; i++) { 871 bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool); 872 assert(bdev_io != NULL); 873 ch->per_thread_cache_count++; 874 STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link); 875 } 876 877 TAILQ_INIT(&ch->shared_resources); 878 TAILQ_INIT(&ch->io_wait_queue); 879 880 return 0; 881 } 882 883 static void 884 bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf) 885 { 886 struct spdk_bdev_mgmt_channel *ch = ctx_buf; 887 struct spdk_bdev_io *bdev_io; 888 889 if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) { 890 SPDK_ERRLOG("Pending I/O list wasn't empty on mgmt channel free\n"); 891 } 892 893 if (!TAILQ_EMPTY(&ch->shared_resources)) { 894 SPDK_ERRLOG("Module channel list wasn't empty on mgmt channel free\n"); 895 } 896 897 while (!STAILQ_EMPTY(&ch->per_thread_cache)) { 898 bdev_io = STAILQ_FIRST(&ch->per_thread_cache); 899 STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link); 900 ch->per_thread_cache_count--; 901 spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io); 902 } 903 904 assert(ch->per_thread_cache_count == 0); 905 } 906 907 static void 908 bdev_init_complete(int rc) 909 { 910 spdk_bdev_init_cb cb_fn = g_init_cb_fn; 911 void *cb_arg = g_init_cb_arg; 912 struct spdk_bdev_module *m; 913 914 g_bdev_mgr.init_complete = true; 915 g_init_cb_fn = NULL; 916 g_init_cb_arg = NULL; 917 918 /* 919 * For modules that need to know when subsystem init is complete, 920 * inform them now. 921 */ 922 if (rc == 0) { 923 TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) { 924 if (m->init_complete) { 925 m->init_complete(); 926 } 927 } 928 } 929 930 cb_fn(cb_arg, rc); 931 } 932 933 static void 934 bdev_module_action_complete(void) 935 { 936 struct spdk_bdev_module *m; 937 938 /* 939 * Don't finish bdev subsystem initialization if 940 * module pre-initialization is still in progress, or 941 * the subsystem been already initialized. 942 */ 943 if (!g_bdev_mgr.module_init_complete || g_bdev_mgr.init_complete) { 944 return; 945 } 946 947 /* 948 * Check all bdev modules for inits/examinations in progress. If any 949 * exist, return immediately since we cannot finish bdev subsystem 950 * initialization until all are completed. 951 */ 952 TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) { 953 if (m->internal.action_in_progress > 0) { 954 return; 955 } 956 } 957 958 /* 959 * Modules already finished initialization - now that all 960 * the bdev modules have finished their asynchronous I/O 961 * processing, the entire bdev layer can be marked as complete. 962 */ 963 bdev_init_complete(0); 964 } 965 966 static void 967 bdev_module_action_done(struct spdk_bdev_module *module) 968 { 969 assert(module->internal.action_in_progress > 0); 970 module->internal.action_in_progress--; 971 bdev_module_action_complete(); 972 } 973 974 void 975 spdk_bdev_module_init_done(struct spdk_bdev_module *module) 976 { 977 bdev_module_action_done(module); 978 } 979 980 void 981 spdk_bdev_module_examine_done(struct spdk_bdev_module *module) 982 { 983 bdev_module_action_done(module); 984 } 985 986 /** The last initialized bdev module */ 987 static struct spdk_bdev_module *g_resume_bdev_module = NULL; 988 989 static void 990 bdev_init_failed(void *cb_arg) 991 { 992 struct spdk_bdev_module *module = cb_arg; 993 994 module->internal.action_in_progress--; 995 bdev_init_complete(-1); 996 } 997 998 static int 999 bdev_modules_init(void) 1000 { 1001 struct spdk_bdev_module *module; 1002 int rc = 0; 1003 1004 TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) { 1005 g_resume_bdev_module = module; 1006 if (module->async_init) { 1007 module->internal.action_in_progress = 1; 1008 } 1009 rc = module->module_init(); 1010 if (rc != 0) { 1011 /* Bump action_in_progress to prevent other modules from completion of modules_init 1012 * Send message to defer application shutdown until resources are cleaned up */ 1013 module->internal.action_in_progress = 1; 1014 spdk_thread_send_msg(spdk_get_thread(), bdev_init_failed, module); 1015 return rc; 1016 } 1017 } 1018 1019 g_resume_bdev_module = NULL; 1020 return 0; 1021 } 1022 1023 void 1024 spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg) 1025 { 1026 struct spdk_conf_section *sp; 1027 struct spdk_bdev_opts bdev_opts; 1028 int32_t bdev_io_pool_size, bdev_io_cache_size; 1029 int cache_size; 1030 int rc = 0; 1031 char mempool_name[32]; 1032 1033 assert(cb_fn != NULL); 1034 1035 sp = spdk_conf_find_section(NULL, "Bdev"); 1036 if (sp != NULL) { 1037 spdk_bdev_get_opts(&bdev_opts); 1038 1039 bdev_io_pool_size = spdk_conf_section_get_intval(sp, "BdevIoPoolSize"); 1040 if (bdev_io_pool_size >= 0) { 1041 bdev_opts.bdev_io_pool_size = bdev_io_pool_size; 1042 } 1043 1044 bdev_io_cache_size = spdk_conf_section_get_intval(sp, "BdevIoCacheSize"); 1045 if (bdev_io_cache_size >= 0) { 1046 bdev_opts.bdev_io_cache_size = bdev_io_cache_size; 1047 } 1048 1049 if (spdk_bdev_set_opts(&bdev_opts)) { 1050 bdev_init_complete(-1); 1051 return; 1052 } 1053 1054 assert(memcmp(&bdev_opts, &g_bdev_opts, sizeof(bdev_opts)) == 0); 1055 } 1056 1057 g_init_cb_fn = cb_fn; 1058 g_init_cb_arg = cb_arg; 1059 1060 spdk_notify_type_register("bdev_register"); 1061 spdk_notify_type_register("bdev_unregister"); 1062 1063 snprintf(mempool_name, sizeof(mempool_name), "bdev_io_%d", getpid()); 1064 1065 g_bdev_mgr.bdev_io_pool = spdk_mempool_create(mempool_name, 1066 g_bdev_opts.bdev_io_pool_size, 1067 sizeof(struct spdk_bdev_io) + 1068 bdev_module_get_max_ctx_size(), 1069 0, 1070 SPDK_ENV_SOCKET_ID_ANY); 1071 1072 if (g_bdev_mgr.bdev_io_pool == NULL) { 1073 SPDK_ERRLOG("could not allocate spdk_bdev_io pool\n"); 1074 bdev_init_complete(-1); 1075 return; 1076 } 1077 1078 /** 1079 * Ensure no more than half of the total buffers end up local caches, by 1080 * using spdk_thread_get_count() to determine how many local caches we need 1081 * to account for. 1082 */ 1083 cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_thread_get_count()); 1084 snprintf(mempool_name, sizeof(mempool_name), "buf_small_pool_%d", getpid()); 1085 1086 g_bdev_mgr.buf_small_pool = spdk_mempool_create(mempool_name, 1087 BUF_SMALL_POOL_SIZE, 1088 SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) + 1089 SPDK_BDEV_POOL_ALIGNMENT, 1090 cache_size, 1091 SPDK_ENV_SOCKET_ID_ANY); 1092 if (!g_bdev_mgr.buf_small_pool) { 1093 SPDK_ERRLOG("create rbuf small pool failed\n"); 1094 bdev_init_complete(-1); 1095 return; 1096 } 1097 1098 cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_thread_get_count()); 1099 snprintf(mempool_name, sizeof(mempool_name), "buf_large_pool_%d", getpid()); 1100 1101 g_bdev_mgr.buf_large_pool = spdk_mempool_create(mempool_name, 1102 BUF_LARGE_POOL_SIZE, 1103 SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) + 1104 SPDK_BDEV_POOL_ALIGNMENT, 1105 cache_size, 1106 SPDK_ENV_SOCKET_ID_ANY); 1107 if (!g_bdev_mgr.buf_large_pool) { 1108 SPDK_ERRLOG("create rbuf large pool failed\n"); 1109 bdev_init_complete(-1); 1110 return; 1111 } 1112 1113 g_bdev_mgr.zero_buffer = spdk_zmalloc(ZERO_BUFFER_SIZE, ZERO_BUFFER_SIZE, 1114 NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA); 1115 if (!g_bdev_mgr.zero_buffer) { 1116 SPDK_ERRLOG("create bdev zero buffer failed\n"); 1117 bdev_init_complete(-1); 1118 return; 1119 } 1120 1121 #ifdef SPDK_CONFIG_VTUNE 1122 g_bdev_mgr.domain = __itt_domain_create("spdk_bdev"); 1123 #endif 1124 1125 spdk_io_device_register(&g_bdev_mgr, bdev_mgmt_channel_create, 1126 bdev_mgmt_channel_destroy, 1127 sizeof(struct spdk_bdev_mgmt_channel), 1128 "bdev_mgr"); 1129 1130 rc = bdev_modules_init(); 1131 g_bdev_mgr.module_init_complete = true; 1132 if (rc != 0) { 1133 SPDK_ERRLOG("bdev modules init failed\n"); 1134 return; 1135 } 1136 1137 bdev_module_action_complete(); 1138 } 1139 1140 static void 1141 bdev_mgr_unregister_cb(void *io_device) 1142 { 1143 spdk_bdev_fini_cb cb_fn = g_fini_cb_fn; 1144 1145 if (g_bdev_mgr.bdev_io_pool) { 1146 if (spdk_mempool_count(g_bdev_mgr.bdev_io_pool) != g_bdev_opts.bdev_io_pool_size) { 1147 SPDK_ERRLOG("bdev IO pool count is %zu but should be %u\n", 1148 spdk_mempool_count(g_bdev_mgr.bdev_io_pool), 1149 g_bdev_opts.bdev_io_pool_size); 1150 } 1151 1152 spdk_mempool_free(g_bdev_mgr.bdev_io_pool); 1153 } 1154 1155 if (g_bdev_mgr.buf_small_pool) { 1156 if (spdk_mempool_count(g_bdev_mgr.buf_small_pool) != BUF_SMALL_POOL_SIZE) { 1157 SPDK_ERRLOG("Small buffer pool count is %zu but should be %u\n", 1158 spdk_mempool_count(g_bdev_mgr.buf_small_pool), 1159 BUF_SMALL_POOL_SIZE); 1160 assert(false); 1161 } 1162 1163 spdk_mempool_free(g_bdev_mgr.buf_small_pool); 1164 } 1165 1166 if (g_bdev_mgr.buf_large_pool) { 1167 if (spdk_mempool_count(g_bdev_mgr.buf_large_pool) != BUF_LARGE_POOL_SIZE) { 1168 SPDK_ERRLOG("Large buffer pool count is %zu but should be %u\n", 1169 spdk_mempool_count(g_bdev_mgr.buf_large_pool), 1170 BUF_LARGE_POOL_SIZE); 1171 assert(false); 1172 } 1173 1174 spdk_mempool_free(g_bdev_mgr.buf_large_pool); 1175 } 1176 1177 spdk_free(g_bdev_mgr.zero_buffer); 1178 1179 cb_fn(g_fini_cb_arg); 1180 g_fini_cb_fn = NULL; 1181 g_fini_cb_arg = NULL; 1182 g_bdev_mgr.init_complete = false; 1183 g_bdev_mgr.module_init_complete = false; 1184 pthread_mutex_destroy(&g_bdev_mgr.mutex); 1185 } 1186 1187 static void 1188 bdev_module_finish_iter(void *arg) 1189 { 1190 struct spdk_bdev_module *bdev_module; 1191 1192 /* FIXME: Handling initialization failures is broken now, 1193 * so we won't even try cleaning up after successfully 1194 * initialized modules. if module_init_complete is false, 1195 * just call spdk_bdev_mgr_unregister_cb 1196 */ 1197 if (!g_bdev_mgr.module_init_complete) { 1198 bdev_mgr_unregister_cb(NULL); 1199 return; 1200 } 1201 1202 /* Start iterating from the last touched module */ 1203 if (!g_resume_bdev_module) { 1204 bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list); 1205 } else { 1206 bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list, 1207 internal.tailq); 1208 } 1209 1210 while (bdev_module) { 1211 if (bdev_module->async_fini) { 1212 /* Save our place so we can resume later. We must 1213 * save the variable here, before calling module_fini() 1214 * below, because in some cases the module may immediately 1215 * call spdk_bdev_module_finish_done() and re-enter 1216 * this function to continue iterating. */ 1217 g_resume_bdev_module = bdev_module; 1218 } 1219 1220 if (bdev_module->module_fini) { 1221 bdev_module->module_fini(); 1222 } 1223 1224 if (bdev_module->async_fini) { 1225 return; 1226 } 1227 1228 bdev_module = TAILQ_PREV(bdev_module, bdev_module_list, 1229 internal.tailq); 1230 } 1231 1232 g_resume_bdev_module = NULL; 1233 spdk_io_device_unregister(&g_bdev_mgr, bdev_mgr_unregister_cb); 1234 } 1235 1236 void 1237 spdk_bdev_module_finish_done(void) 1238 { 1239 if (spdk_get_thread() != g_fini_thread) { 1240 spdk_thread_send_msg(g_fini_thread, bdev_module_finish_iter, NULL); 1241 } else { 1242 bdev_module_finish_iter(NULL); 1243 } 1244 } 1245 1246 static void 1247 bdev_finish_unregister_bdevs_iter(void *cb_arg, int bdeverrno) 1248 { 1249 struct spdk_bdev *bdev = cb_arg; 1250 1251 if (bdeverrno && bdev) { 1252 SPDK_WARNLOG("Unable to unregister bdev '%s' during spdk_bdev_finish()\n", 1253 bdev->name); 1254 1255 /* 1256 * Since the call to spdk_bdev_unregister() failed, we have no way to free this 1257 * bdev; try to continue by manually removing this bdev from the list and continue 1258 * with the next bdev in the list. 1259 */ 1260 TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link); 1261 } 1262 1263 if (TAILQ_EMPTY(&g_bdev_mgr.bdevs)) { 1264 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Done unregistering bdevs\n"); 1265 /* 1266 * Bdev module finish need to be deferred as we might be in the middle of some context 1267 * (like bdev part free) that will use this bdev (or private bdev driver ctx data) 1268 * after returning. 1269 */ 1270 spdk_thread_send_msg(spdk_get_thread(), bdev_module_finish_iter, NULL); 1271 return; 1272 } 1273 1274 /* 1275 * Unregister last unclaimed bdev in the list, to ensure that bdev subsystem 1276 * shutdown proceeds top-down. The goal is to give virtual bdevs an opportunity 1277 * to detect clean shutdown as opposed to run-time hot removal of the underlying 1278 * base bdevs. 1279 * 1280 * Also, walk the list in the reverse order. 1281 */ 1282 for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list); 1283 bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) { 1284 if (bdev->internal.claim_module != NULL) { 1285 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Skipping claimed bdev '%s'(<-'%s').\n", 1286 bdev->name, bdev->internal.claim_module->name); 1287 continue; 1288 } 1289 1290 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Unregistering bdev '%s'\n", bdev->name); 1291 spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev); 1292 return; 1293 } 1294 1295 /* 1296 * If any bdev fails to unclaim underlying bdev properly, we may face the 1297 * case of bdev list consisting of claimed bdevs only (if claims are managed 1298 * correctly, this would mean there's a loop in the claims graph which is 1299 * clearly impossible). Warn and unregister last bdev on the list then. 1300 */ 1301 for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list); 1302 bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) { 1303 SPDK_WARNLOG("Unregistering claimed bdev '%s'!\n", bdev->name); 1304 spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev); 1305 return; 1306 } 1307 } 1308 1309 void 1310 spdk_bdev_finish(spdk_bdev_fini_cb cb_fn, void *cb_arg) 1311 { 1312 struct spdk_bdev_module *m; 1313 1314 assert(cb_fn != NULL); 1315 1316 g_fini_thread = spdk_get_thread(); 1317 1318 g_fini_cb_fn = cb_fn; 1319 g_fini_cb_arg = cb_arg; 1320 1321 TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) { 1322 if (m->fini_start) { 1323 m->fini_start(); 1324 } 1325 } 1326 1327 bdev_finish_unregister_bdevs_iter(NULL, 0); 1328 } 1329 1330 struct spdk_bdev_io * 1331 bdev_channel_get_io(struct spdk_bdev_channel *channel) 1332 { 1333 struct spdk_bdev_mgmt_channel *ch = channel->shared_resource->mgmt_ch; 1334 struct spdk_bdev_io *bdev_io; 1335 1336 if (ch->per_thread_cache_count > 0) { 1337 bdev_io = STAILQ_FIRST(&ch->per_thread_cache); 1338 STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link); 1339 ch->per_thread_cache_count--; 1340 } else if (spdk_unlikely(!TAILQ_EMPTY(&ch->io_wait_queue))) { 1341 /* 1342 * Don't try to look for bdev_ios in the global pool if there are 1343 * waiters on bdev_ios - we don't want this caller to jump the line. 1344 */ 1345 bdev_io = NULL; 1346 } else { 1347 bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool); 1348 } 1349 1350 return bdev_io; 1351 } 1352 1353 void 1354 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io) 1355 { 1356 struct spdk_bdev_mgmt_channel *ch; 1357 1358 assert(bdev_io != NULL); 1359 assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING); 1360 1361 ch = bdev_io->internal.ch->shared_resource->mgmt_ch; 1362 1363 if (bdev_io->internal.buf != NULL) { 1364 bdev_io_put_buf(bdev_io); 1365 } 1366 1367 if (ch->per_thread_cache_count < ch->bdev_io_cache_size) { 1368 ch->per_thread_cache_count++; 1369 STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link); 1370 while (ch->per_thread_cache_count > 0 && !TAILQ_EMPTY(&ch->io_wait_queue)) { 1371 struct spdk_bdev_io_wait_entry *entry; 1372 1373 entry = TAILQ_FIRST(&ch->io_wait_queue); 1374 TAILQ_REMOVE(&ch->io_wait_queue, entry, link); 1375 entry->cb_fn(entry->cb_arg); 1376 } 1377 } else { 1378 /* We should never have a full cache with entries on the io wait queue. */ 1379 assert(TAILQ_EMPTY(&ch->io_wait_queue)); 1380 spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io); 1381 } 1382 } 1383 1384 static bool 1385 bdev_qos_is_iops_rate_limit(enum spdk_bdev_qos_rate_limit_type limit) 1386 { 1387 assert(limit != SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES); 1388 1389 switch (limit) { 1390 case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT: 1391 return true; 1392 case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT: 1393 case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT: 1394 case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT: 1395 return false; 1396 case SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES: 1397 default: 1398 return false; 1399 } 1400 } 1401 1402 static bool 1403 bdev_qos_io_to_limit(struct spdk_bdev_io *bdev_io) 1404 { 1405 switch (bdev_io->type) { 1406 case SPDK_BDEV_IO_TYPE_NVME_IO: 1407 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 1408 case SPDK_BDEV_IO_TYPE_READ: 1409 case SPDK_BDEV_IO_TYPE_WRITE: 1410 return true; 1411 default: 1412 return false; 1413 } 1414 } 1415 1416 static bool 1417 bdev_is_read_io(struct spdk_bdev_io *bdev_io) 1418 { 1419 switch (bdev_io->type) { 1420 case SPDK_BDEV_IO_TYPE_NVME_IO: 1421 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 1422 /* Bit 1 (0x2) set for read operation */ 1423 if (bdev_io->u.nvme_passthru.cmd.opc & SPDK_NVME_OPC_READ) { 1424 return true; 1425 } else { 1426 return false; 1427 } 1428 case SPDK_BDEV_IO_TYPE_READ: 1429 return true; 1430 default: 1431 return false; 1432 } 1433 } 1434 1435 static uint64_t 1436 bdev_get_io_size_in_byte(struct spdk_bdev_io *bdev_io) 1437 { 1438 struct spdk_bdev *bdev = bdev_io->bdev; 1439 1440 switch (bdev_io->type) { 1441 case SPDK_BDEV_IO_TYPE_NVME_IO: 1442 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 1443 return bdev_io->u.nvme_passthru.nbytes; 1444 case SPDK_BDEV_IO_TYPE_READ: 1445 case SPDK_BDEV_IO_TYPE_WRITE: 1446 return bdev_io->u.bdev.num_blocks * bdev->blocklen; 1447 default: 1448 return 0; 1449 } 1450 } 1451 1452 static bool 1453 bdev_qos_rw_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1454 { 1455 if (limit->max_per_timeslice > 0 && limit->remaining_this_timeslice <= 0) { 1456 return true; 1457 } else { 1458 return false; 1459 } 1460 } 1461 1462 static bool 1463 bdev_qos_r_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1464 { 1465 if (bdev_is_read_io(io) == false) { 1466 return false; 1467 } 1468 1469 return bdev_qos_rw_queue_io(limit, io); 1470 } 1471 1472 static bool 1473 bdev_qos_w_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1474 { 1475 if (bdev_is_read_io(io) == true) { 1476 return false; 1477 } 1478 1479 return bdev_qos_rw_queue_io(limit, io); 1480 } 1481 1482 static void 1483 bdev_qos_rw_iops_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1484 { 1485 limit->remaining_this_timeslice--; 1486 } 1487 1488 static void 1489 bdev_qos_rw_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1490 { 1491 limit->remaining_this_timeslice -= bdev_get_io_size_in_byte(io); 1492 } 1493 1494 static void 1495 bdev_qos_r_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1496 { 1497 if (bdev_is_read_io(io) == false) { 1498 return; 1499 } 1500 1501 return bdev_qos_rw_bps_update_quota(limit, io); 1502 } 1503 1504 static void 1505 bdev_qos_w_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io) 1506 { 1507 if (bdev_is_read_io(io) == true) { 1508 return; 1509 } 1510 1511 return bdev_qos_rw_bps_update_quota(limit, io); 1512 } 1513 1514 static void 1515 bdev_qos_set_ops(struct spdk_bdev_qos *qos) 1516 { 1517 int i; 1518 1519 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1520 if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 1521 qos->rate_limits[i].queue_io = NULL; 1522 qos->rate_limits[i].update_quota = NULL; 1523 continue; 1524 } 1525 1526 switch (i) { 1527 case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT: 1528 qos->rate_limits[i].queue_io = bdev_qos_rw_queue_io; 1529 qos->rate_limits[i].update_quota = bdev_qos_rw_iops_update_quota; 1530 break; 1531 case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT: 1532 qos->rate_limits[i].queue_io = bdev_qos_rw_queue_io; 1533 qos->rate_limits[i].update_quota = bdev_qos_rw_bps_update_quota; 1534 break; 1535 case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT: 1536 qos->rate_limits[i].queue_io = bdev_qos_r_queue_io; 1537 qos->rate_limits[i].update_quota = bdev_qos_r_bps_update_quota; 1538 break; 1539 case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT: 1540 qos->rate_limits[i].queue_io = bdev_qos_w_queue_io; 1541 qos->rate_limits[i].update_quota = bdev_qos_w_bps_update_quota; 1542 break; 1543 default: 1544 break; 1545 } 1546 } 1547 } 1548 1549 static inline void 1550 bdev_io_do_submit(struct spdk_bdev_channel *bdev_ch, struct spdk_bdev_io *bdev_io) 1551 { 1552 struct spdk_bdev *bdev = bdev_io->bdev; 1553 struct spdk_io_channel *ch = bdev_ch->channel; 1554 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 1555 1556 if (spdk_likely(TAILQ_EMPTY(&shared_resource->nomem_io))) { 1557 bdev_ch->io_outstanding++; 1558 shared_resource->io_outstanding++; 1559 bdev_io->internal.in_submit_request = true; 1560 bdev->fn_table->submit_request(ch, bdev_io); 1561 bdev_io->internal.in_submit_request = false; 1562 } else { 1563 TAILQ_INSERT_TAIL(&shared_resource->nomem_io, bdev_io, internal.link); 1564 } 1565 } 1566 1567 static int 1568 bdev_qos_io_submit(struct spdk_bdev_channel *ch, struct spdk_bdev_qos *qos) 1569 { 1570 struct spdk_bdev_io *bdev_io = NULL, *tmp = NULL; 1571 int i, submitted_ios = 0; 1572 1573 TAILQ_FOREACH_SAFE(bdev_io, &qos->queued, internal.link, tmp) { 1574 if (bdev_qos_io_to_limit(bdev_io) == true) { 1575 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1576 if (!qos->rate_limits[i].queue_io) { 1577 continue; 1578 } 1579 1580 if (qos->rate_limits[i].queue_io(&qos->rate_limits[i], 1581 bdev_io) == true) { 1582 return submitted_ios; 1583 } 1584 } 1585 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 1586 if (!qos->rate_limits[i].update_quota) { 1587 continue; 1588 } 1589 1590 qos->rate_limits[i].update_quota(&qos->rate_limits[i], bdev_io); 1591 } 1592 } 1593 1594 TAILQ_REMOVE(&qos->queued, bdev_io, internal.link); 1595 bdev_io_do_submit(ch, bdev_io); 1596 submitted_ios++; 1597 } 1598 1599 return submitted_ios; 1600 } 1601 1602 static void 1603 bdev_queue_io_wait_with_cb(struct spdk_bdev_io *bdev_io, spdk_bdev_io_wait_cb cb_fn) 1604 { 1605 int rc; 1606 1607 bdev_io->internal.waitq_entry.bdev = bdev_io->bdev; 1608 bdev_io->internal.waitq_entry.cb_fn = cb_fn; 1609 bdev_io->internal.waitq_entry.cb_arg = bdev_io; 1610 rc = spdk_bdev_queue_io_wait(bdev_io->bdev, spdk_io_channel_from_ctx(bdev_io->internal.ch), 1611 &bdev_io->internal.waitq_entry); 1612 if (rc != 0) { 1613 SPDK_ERRLOG("Queue IO failed, rc=%d\n", rc); 1614 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 1615 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 1616 } 1617 } 1618 1619 static bool 1620 bdev_io_type_can_split(uint8_t type) 1621 { 1622 assert(type != SPDK_BDEV_IO_TYPE_INVALID); 1623 assert(type < SPDK_BDEV_NUM_IO_TYPES); 1624 1625 /* Only split READ and WRITE I/O. Theoretically other types of I/O like 1626 * UNMAP could be split, but these types of I/O are typically much larger 1627 * in size (sometimes the size of the entire block device), and the bdev 1628 * module can more efficiently split these types of I/O. Plus those types 1629 * of I/O do not have a payload, which makes the splitting process simpler. 1630 */ 1631 if (type == SPDK_BDEV_IO_TYPE_READ || type == SPDK_BDEV_IO_TYPE_WRITE) { 1632 return true; 1633 } else { 1634 return false; 1635 } 1636 } 1637 1638 static bool 1639 bdev_io_should_split(struct spdk_bdev_io *bdev_io) 1640 { 1641 uint64_t start_stripe, end_stripe; 1642 uint32_t io_boundary = bdev_io->bdev->optimal_io_boundary; 1643 1644 if (io_boundary == 0) { 1645 return false; 1646 } 1647 1648 if (!bdev_io_type_can_split(bdev_io->type)) { 1649 return false; 1650 } 1651 1652 start_stripe = bdev_io->u.bdev.offset_blocks; 1653 end_stripe = start_stripe + bdev_io->u.bdev.num_blocks - 1; 1654 /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */ 1655 if (spdk_likely(spdk_u32_is_pow2(io_boundary))) { 1656 start_stripe >>= spdk_u32log2(io_boundary); 1657 end_stripe >>= spdk_u32log2(io_boundary); 1658 } else { 1659 start_stripe /= io_boundary; 1660 end_stripe /= io_boundary; 1661 } 1662 return (start_stripe != end_stripe); 1663 } 1664 1665 static uint32_t 1666 _to_next_boundary(uint64_t offset, uint32_t boundary) 1667 { 1668 return (boundary - (offset % boundary)); 1669 } 1670 1671 static void 1672 bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg); 1673 1674 static void 1675 _bdev_io_split(void *_bdev_io) 1676 { 1677 struct spdk_bdev_io *bdev_io = _bdev_io; 1678 uint64_t current_offset, remaining; 1679 uint32_t blocklen, to_next_boundary, to_next_boundary_bytes, to_last_block_bytes; 1680 struct iovec *parent_iov, *iov; 1681 uint64_t parent_iov_offset, iov_len; 1682 uint32_t parent_iovpos, parent_iovcnt, child_iovcnt, iovcnt; 1683 void *md_buf = NULL; 1684 int rc; 1685 1686 remaining = bdev_io->u.bdev.split_remaining_num_blocks; 1687 current_offset = bdev_io->u.bdev.split_current_offset_blocks; 1688 blocklen = bdev_io->bdev->blocklen; 1689 parent_iov_offset = (current_offset - bdev_io->u.bdev.offset_blocks) * blocklen; 1690 parent_iovcnt = bdev_io->u.bdev.iovcnt; 1691 1692 for (parent_iovpos = 0; parent_iovpos < parent_iovcnt; parent_iovpos++) { 1693 parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos]; 1694 if (parent_iov_offset < parent_iov->iov_len) { 1695 break; 1696 } 1697 parent_iov_offset -= parent_iov->iov_len; 1698 } 1699 1700 child_iovcnt = 0; 1701 while (remaining > 0 && parent_iovpos < parent_iovcnt && child_iovcnt < BDEV_IO_NUM_CHILD_IOV) { 1702 to_next_boundary = _to_next_boundary(current_offset, bdev_io->bdev->optimal_io_boundary); 1703 to_next_boundary = spdk_min(remaining, to_next_boundary); 1704 to_next_boundary_bytes = to_next_boundary * blocklen; 1705 iov = &bdev_io->child_iov[child_iovcnt]; 1706 iovcnt = 0; 1707 1708 if (bdev_io->u.bdev.md_buf) { 1709 assert((parent_iov_offset % blocklen) > 0); 1710 md_buf = (char *)bdev_io->u.bdev.md_buf + (parent_iov_offset / blocklen) * 1711 spdk_bdev_get_md_size(bdev_io->bdev); 1712 } 1713 1714 while (to_next_boundary_bytes > 0 && parent_iovpos < parent_iovcnt && 1715 child_iovcnt < BDEV_IO_NUM_CHILD_IOV) { 1716 parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos]; 1717 iov_len = spdk_min(to_next_boundary_bytes, parent_iov->iov_len - parent_iov_offset); 1718 to_next_boundary_bytes -= iov_len; 1719 1720 bdev_io->child_iov[child_iovcnt].iov_base = parent_iov->iov_base + parent_iov_offset; 1721 bdev_io->child_iov[child_iovcnt].iov_len = iov_len; 1722 1723 if (iov_len < parent_iov->iov_len - parent_iov_offset) { 1724 parent_iov_offset += iov_len; 1725 } else { 1726 parent_iovpos++; 1727 parent_iov_offset = 0; 1728 } 1729 child_iovcnt++; 1730 iovcnt++; 1731 } 1732 1733 if (to_next_boundary_bytes > 0) { 1734 /* We had to stop this child I/O early because we ran out of 1735 * child_iov space. Ensure the iovs to be aligned with block 1736 * size and then adjust to_next_boundary before starting the 1737 * child I/O. 1738 */ 1739 assert(child_iovcnt == BDEV_IO_NUM_CHILD_IOV); 1740 to_last_block_bytes = to_next_boundary_bytes % blocklen; 1741 if (to_last_block_bytes != 0) { 1742 uint32_t child_iovpos = child_iovcnt - 1; 1743 /* don't decrease child_iovcnt so the loop will naturally end */ 1744 1745 to_last_block_bytes = blocklen - to_last_block_bytes; 1746 to_next_boundary_bytes += to_last_block_bytes; 1747 while (to_last_block_bytes > 0 && iovcnt > 0) { 1748 iov_len = spdk_min(to_last_block_bytes, 1749 bdev_io->child_iov[child_iovpos].iov_len); 1750 bdev_io->child_iov[child_iovpos].iov_len -= iov_len; 1751 if (bdev_io->child_iov[child_iovpos].iov_len == 0) { 1752 child_iovpos--; 1753 if (--iovcnt == 0) { 1754 return; 1755 } 1756 } 1757 to_last_block_bytes -= iov_len; 1758 } 1759 1760 assert(to_last_block_bytes == 0); 1761 } 1762 to_next_boundary -= to_next_boundary_bytes / blocklen; 1763 } 1764 1765 bdev_io->u.bdev.split_outstanding++; 1766 1767 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) { 1768 rc = bdev_readv_blocks_with_md(bdev_io->internal.desc, 1769 spdk_io_channel_from_ctx(bdev_io->internal.ch), 1770 iov, iovcnt, md_buf, current_offset, 1771 to_next_boundary, 1772 bdev_io_split_done, bdev_io); 1773 } else { 1774 rc = bdev_writev_blocks_with_md(bdev_io->internal.desc, 1775 spdk_io_channel_from_ctx(bdev_io->internal.ch), 1776 iov, iovcnt, md_buf, current_offset, 1777 to_next_boundary, 1778 bdev_io_split_done, bdev_io); 1779 } 1780 1781 if (rc == 0) { 1782 current_offset += to_next_boundary; 1783 remaining -= to_next_boundary; 1784 bdev_io->u.bdev.split_current_offset_blocks = current_offset; 1785 bdev_io->u.bdev.split_remaining_num_blocks = remaining; 1786 } else { 1787 bdev_io->u.bdev.split_outstanding--; 1788 if (rc == -ENOMEM) { 1789 if (bdev_io->u.bdev.split_outstanding == 0) { 1790 /* No I/O is outstanding. Hence we should wait here. */ 1791 bdev_queue_io_wait_with_cb(bdev_io, _bdev_io_split); 1792 } 1793 } else { 1794 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 1795 if (bdev_io->u.bdev.split_outstanding == 0) { 1796 spdk_trace_record_tsc(spdk_get_ticks(), TRACE_BDEV_IO_DONE, 0, 0, 1797 (uintptr_t)bdev_io, 0); 1798 TAILQ_REMOVE(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link); 1799 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 1800 } 1801 } 1802 1803 return; 1804 } 1805 } 1806 } 1807 1808 static void 1809 bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 1810 { 1811 struct spdk_bdev_io *parent_io = cb_arg; 1812 1813 spdk_bdev_free_io(bdev_io); 1814 1815 if (!success) { 1816 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 1817 } 1818 parent_io->u.bdev.split_outstanding--; 1819 if (parent_io->u.bdev.split_outstanding != 0) { 1820 return; 1821 } 1822 1823 /* 1824 * Parent I/O finishes when all blocks are consumed. 1825 */ 1826 if (parent_io->u.bdev.split_remaining_num_blocks == 0) { 1827 assert(parent_io->internal.cb != bdev_io_split_done); 1828 spdk_trace_record_tsc(spdk_get_ticks(), TRACE_BDEV_IO_DONE, 0, 0, 1829 (uintptr_t)parent_io, 0); 1830 TAILQ_REMOVE(&parent_io->internal.ch->io_submitted, parent_io, internal.ch_link); 1831 parent_io->internal.cb(parent_io, parent_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS, 1832 parent_io->internal.caller_ctx); 1833 return; 1834 } 1835 1836 /* 1837 * Continue with the splitting process. This function will complete the parent I/O if the 1838 * splitting is done. 1839 */ 1840 _bdev_io_split(parent_io); 1841 } 1842 1843 static void 1844 bdev_io_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success); 1845 1846 static void 1847 bdev_io_split(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 1848 { 1849 assert(bdev_io_type_can_split(bdev_io->type)); 1850 1851 bdev_io->u.bdev.split_current_offset_blocks = bdev_io->u.bdev.offset_blocks; 1852 bdev_io->u.bdev.split_remaining_num_blocks = bdev_io->u.bdev.num_blocks; 1853 bdev_io->u.bdev.split_outstanding = 0; 1854 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 1855 1856 if (_is_buf_allocated(bdev_io->u.bdev.iovs)) { 1857 _bdev_io_split(bdev_io); 1858 } else { 1859 assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ); 1860 spdk_bdev_io_get_buf(bdev_io, bdev_io_split_get_buf_cb, 1861 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 1862 } 1863 } 1864 1865 static void 1866 bdev_io_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success) 1867 { 1868 if (!success) { 1869 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 1870 return; 1871 } 1872 1873 bdev_io_split(ch, bdev_io); 1874 } 1875 1876 /* Explicitly mark this inline, since it's used as a function pointer and otherwise won't 1877 * be inlined, at least on some compilers. 1878 */ 1879 static inline void 1880 _bdev_io_submit(void *ctx) 1881 { 1882 struct spdk_bdev_io *bdev_io = ctx; 1883 struct spdk_bdev *bdev = bdev_io->bdev; 1884 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 1885 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 1886 uint64_t tsc; 1887 1888 tsc = spdk_get_ticks(); 1889 bdev_io->internal.submit_tsc = tsc; 1890 spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_START, 0, 0, (uintptr_t)bdev_io, bdev_io->type); 1891 1892 if (spdk_likely(bdev_ch->flags == 0)) { 1893 bdev_io_do_submit(bdev_ch, bdev_io); 1894 return; 1895 } 1896 1897 bdev_ch->io_outstanding++; 1898 shared_resource->io_outstanding++; 1899 bdev_io->internal.in_submit_request = true; 1900 if (bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS) { 1901 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 1902 } else if (bdev_ch->flags & BDEV_CH_QOS_ENABLED) { 1903 bdev_ch->io_outstanding--; 1904 shared_resource->io_outstanding--; 1905 TAILQ_INSERT_TAIL(&bdev->internal.qos->queued, bdev_io, internal.link); 1906 bdev_qos_io_submit(bdev_ch, bdev->internal.qos); 1907 } else { 1908 SPDK_ERRLOG("unknown bdev_ch flag %x found\n", bdev_ch->flags); 1909 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 1910 } 1911 bdev_io->internal.in_submit_request = false; 1912 } 1913 1914 bool 1915 bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2); 1916 1917 bool 1918 bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2) 1919 { 1920 if (range1->length == 0 || range2->length == 0) { 1921 return false; 1922 } 1923 1924 if (range1->offset + range1->length <= range2->offset) { 1925 return false; 1926 } 1927 1928 if (range2->offset + range2->length <= range1->offset) { 1929 return false; 1930 } 1931 1932 return true; 1933 } 1934 1935 static bool 1936 bdev_io_range_is_locked(struct spdk_bdev_io *bdev_io, struct lba_range *range) 1937 { 1938 struct spdk_bdev_channel *ch = bdev_io->internal.ch; 1939 struct lba_range r; 1940 1941 switch (bdev_io->type) { 1942 case SPDK_BDEV_IO_TYPE_NVME_IO: 1943 case SPDK_BDEV_IO_TYPE_NVME_IO_MD: 1944 /* Don't try to decode the NVMe command - just assume worst-case and that 1945 * it overlaps a locked range. 1946 */ 1947 return true; 1948 case SPDK_BDEV_IO_TYPE_WRITE: 1949 case SPDK_BDEV_IO_TYPE_UNMAP: 1950 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 1951 case SPDK_BDEV_IO_TYPE_ZCOPY: 1952 r.offset = bdev_io->u.bdev.offset_blocks; 1953 r.length = bdev_io->u.bdev.num_blocks; 1954 if (!bdev_lba_range_overlapped(range, &r)) { 1955 /* This I/O doesn't overlap the specified LBA range. */ 1956 return false; 1957 } else if (range->owner_ch == ch && range->locked_ctx == bdev_io->internal.caller_ctx) { 1958 /* This I/O overlaps, but the I/O is on the same channel that locked this 1959 * range, and the caller_ctx is the same as the locked_ctx. This means 1960 * that this I/O is associated with the lock, and is allowed to execute. 1961 */ 1962 return false; 1963 } else { 1964 return true; 1965 } 1966 default: 1967 return false; 1968 } 1969 } 1970 1971 void 1972 bdev_io_submit(struct spdk_bdev_io *bdev_io) 1973 { 1974 struct spdk_bdev *bdev = bdev_io->bdev; 1975 struct spdk_thread *thread = spdk_bdev_io_get_thread(bdev_io); 1976 struct spdk_bdev_channel *ch = bdev_io->internal.ch; 1977 1978 assert(thread != NULL); 1979 assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING); 1980 1981 if (!TAILQ_EMPTY(&ch->locked_ranges)) { 1982 struct lba_range *range; 1983 1984 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 1985 if (bdev_io_range_is_locked(bdev_io, range)) { 1986 TAILQ_INSERT_TAIL(&ch->io_locked, bdev_io, internal.ch_link); 1987 return; 1988 } 1989 } 1990 } 1991 1992 /* Add the bdev_io to io_submitted only if it is the original 1993 * submission from the bdev user. When a bdev_io is split, 1994 * it comes back through this code path, so we need to make sure 1995 * we don't try to add it a second time. 1996 */ 1997 if (bdev_io->internal.cb != bdev_io_split_done) { 1998 TAILQ_INSERT_TAIL(&ch->io_submitted, bdev_io, internal.ch_link); 1999 } 2000 2001 if (bdev->split_on_optimal_io_boundary && bdev_io_should_split(bdev_io)) { 2002 bdev_io->internal.submit_tsc = spdk_get_ticks(); 2003 spdk_trace_record_tsc(bdev_io->internal.submit_tsc, TRACE_BDEV_IO_START, 0, 0, 2004 (uintptr_t)bdev_io, bdev_io->type); 2005 bdev_io_split(NULL, bdev_io); 2006 return; 2007 } 2008 2009 if (ch->flags & BDEV_CH_QOS_ENABLED) { 2010 if ((thread == bdev->internal.qos->thread) || !bdev->internal.qos->thread) { 2011 _bdev_io_submit(bdev_io); 2012 } else { 2013 bdev_io->internal.io_submit_ch = ch; 2014 bdev_io->internal.ch = bdev->internal.qos->ch; 2015 spdk_thread_send_msg(bdev->internal.qos->thread, _bdev_io_submit, bdev_io); 2016 } 2017 } else { 2018 _bdev_io_submit(bdev_io); 2019 } 2020 } 2021 2022 static void 2023 bdev_io_submit_reset(struct spdk_bdev_io *bdev_io) 2024 { 2025 struct spdk_bdev *bdev = bdev_io->bdev; 2026 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 2027 struct spdk_io_channel *ch = bdev_ch->channel; 2028 2029 assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING); 2030 2031 bdev_io->internal.in_submit_request = true; 2032 bdev->fn_table->submit_request(ch, bdev_io); 2033 bdev_io->internal.in_submit_request = false; 2034 } 2035 2036 void 2037 bdev_io_init(struct spdk_bdev_io *bdev_io, 2038 struct spdk_bdev *bdev, void *cb_arg, 2039 spdk_bdev_io_completion_cb cb) 2040 { 2041 bdev_io->bdev = bdev; 2042 bdev_io->internal.caller_ctx = cb_arg; 2043 bdev_io->internal.cb = cb; 2044 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 2045 bdev_io->internal.in_submit_request = false; 2046 bdev_io->internal.buf = NULL; 2047 bdev_io->internal.io_submit_ch = NULL; 2048 bdev_io->internal.orig_iovs = NULL; 2049 bdev_io->internal.orig_iovcnt = 0; 2050 bdev_io->internal.orig_md_buf = NULL; 2051 bdev_io->internal.error.nvme.cdw0 = 0; 2052 } 2053 2054 static bool 2055 bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type) 2056 { 2057 return bdev->fn_table->io_type_supported(bdev->ctxt, io_type); 2058 } 2059 2060 bool 2061 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type) 2062 { 2063 bool supported; 2064 2065 supported = bdev_io_type_supported(bdev, io_type); 2066 2067 if (!supported) { 2068 switch (io_type) { 2069 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 2070 /* The bdev layer will emulate write zeroes as long as write is supported. */ 2071 supported = bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE); 2072 break; 2073 case SPDK_BDEV_IO_TYPE_ZCOPY: 2074 /* Zero copy can be emulated with regular read and write */ 2075 supported = bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_READ) && 2076 bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE); 2077 break; 2078 default: 2079 break; 2080 } 2081 } 2082 2083 return supported; 2084 } 2085 2086 int 2087 spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 2088 { 2089 if (bdev->fn_table->dump_info_json) { 2090 return bdev->fn_table->dump_info_json(bdev->ctxt, w); 2091 } 2092 2093 return 0; 2094 } 2095 2096 static void 2097 bdev_qos_update_max_quota_per_timeslice(struct spdk_bdev_qos *qos) 2098 { 2099 uint32_t max_per_timeslice = 0; 2100 int i; 2101 2102 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2103 if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 2104 qos->rate_limits[i].max_per_timeslice = 0; 2105 continue; 2106 } 2107 2108 max_per_timeslice = qos->rate_limits[i].limit * 2109 SPDK_BDEV_QOS_TIMESLICE_IN_USEC / SPDK_SEC_TO_USEC; 2110 2111 qos->rate_limits[i].max_per_timeslice = spdk_max(max_per_timeslice, 2112 qos->rate_limits[i].min_per_timeslice); 2113 2114 qos->rate_limits[i].remaining_this_timeslice = qos->rate_limits[i].max_per_timeslice; 2115 } 2116 2117 bdev_qos_set_ops(qos); 2118 } 2119 2120 static int 2121 bdev_channel_poll_qos(void *arg) 2122 { 2123 struct spdk_bdev_qos *qos = arg; 2124 uint64_t now = spdk_get_ticks(); 2125 int i; 2126 2127 if (now < (qos->last_timeslice + qos->timeslice_size)) { 2128 /* We received our callback earlier than expected - return 2129 * immediately and wait to do accounting until at least one 2130 * timeslice has actually expired. This should never happen 2131 * with a well-behaved timer implementation. 2132 */ 2133 return 0; 2134 } 2135 2136 /* Reset for next round of rate limiting */ 2137 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2138 /* We may have allowed the IOs or bytes to slightly overrun in the last 2139 * timeslice. remaining_this_timeslice is signed, so if it's negative 2140 * here, we'll account for the overrun so that the next timeslice will 2141 * be appropriately reduced. 2142 */ 2143 if (qos->rate_limits[i].remaining_this_timeslice > 0) { 2144 qos->rate_limits[i].remaining_this_timeslice = 0; 2145 } 2146 } 2147 2148 while (now >= (qos->last_timeslice + qos->timeslice_size)) { 2149 qos->last_timeslice += qos->timeslice_size; 2150 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2151 qos->rate_limits[i].remaining_this_timeslice += 2152 qos->rate_limits[i].max_per_timeslice; 2153 } 2154 } 2155 2156 return bdev_qos_io_submit(qos->ch, qos); 2157 } 2158 2159 static void 2160 bdev_channel_destroy_resource(struct spdk_bdev_channel *ch) 2161 { 2162 struct spdk_bdev_shared_resource *shared_resource; 2163 struct lba_range *range; 2164 2165 while (!TAILQ_EMPTY(&ch->locked_ranges)) { 2166 range = TAILQ_FIRST(&ch->locked_ranges); 2167 TAILQ_REMOVE(&ch->locked_ranges, range, tailq); 2168 free(range); 2169 } 2170 2171 spdk_put_io_channel(ch->channel); 2172 2173 shared_resource = ch->shared_resource; 2174 2175 assert(TAILQ_EMPTY(&ch->io_locked)); 2176 assert(TAILQ_EMPTY(&ch->io_submitted)); 2177 assert(ch->io_outstanding == 0); 2178 assert(shared_resource->ref > 0); 2179 shared_resource->ref--; 2180 if (shared_resource->ref == 0) { 2181 assert(shared_resource->io_outstanding == 0); 2182 TAILQ_REMOVE(&shared_resource->mgmt_ch->shared_resources, shared_resource, link); 2183 spdk_put_io_channel(spdk_io_channel_from_ctx(shared_resource->mgmt_ch)); 2184 free(shared_resource); 2185 } 2186 } 2187 2188 /* Caller must hold bdev->internal.mutex. */ 2189 static void 2190 bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch) 2191 { 2192 struct spdk_bdev_qos *qos = bdev->internal.qos; 2193 int i; 2194 2195 /* Rate limiting on this bdev enabled */ 2196 if (qos) { 2197 if (qos->ch == NULL) { 2198 struct spdk_io_channel *io_ch; 2199 2200 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Selecting channel %p as QoS channel for bdev %s on thread %p\n", ch, 2201 bdev->name, spdk_get_thread()); 2202 2203 /* No qos channel has been selected, so set one up */ 2204 2205 /* Take another reference to ch */ 2206 io_ch = spdk_get_io_channel(__bdev_to_io_dev(bdev)); 2207 assert(io_ch != NULL); 2208 qos->ch = ch; 2209 2210 qos->thread = spdk_io_channel_get_thread(io_ch); 2211 2212 TAILQ_INIT(&qos->queued); 2213 2214 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2215 if (bdev_qos_is_iops_rate_limit(i) == true) { 2216 qos->rate_limits[i].min_per_timeslice = 2217 SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE; 2218 } else { 2219 qos->rate_limits[i].min_per_timeslice = 2220 SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE; 2221 } 2222 2223 if (qos->rate_limits[i].limit == 0) { 2224 qos->rate_limits[i].limit = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED; 2225 } 2226 } 2227 bdev_qos_update_max_quota_per_timeslice(qos); 2228 qos->timeslice_size = 2229 SPDK_BDEV_QOS_TIMESLICE_IN_USEC * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC; 2230 qos->last_timeslice = spdk_get_ticks(); 2231 qos->poller = spdk_poller_register(bdev_channel_poll_qos, 2232 qos, 2233 SPDK_BDEV_QOS_TIMESLICE_IN_USEC); 2234 } 2235 2236 ch->flags |= BDEV_CH_QOS_ENABLED; 2237 } 2238 } 2239 2240 struct poll_timeout_ctx { 2241 struct spdk_bdev_desc *desc; 2242 uint64_t timeout_in_sec; 2243 spdk_bdev_io_timeout_cb cb_fn; 2244 void *cb_arg; 2245 }; 2246 2247 static void 2248 bdev_desc_free(struct spdk_bdev_desc *desc) 2249 { 2250 pthread_mutex_destroy(&desc->mutex); 2251 free(desc->media_events_buffer); 2252 free(desc); 2253 } 2254 2255 static void 2256 bdev_channel_poll_timeout_io_done(struct spdk_io_channel_iter *i, int status) 2257 { 2258 struct poll_timeout_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2259 struct spdk_bdev_desc *desc = ctx->desc; 2260 2261 free(ctx); 2262 2263 pthread_mutex_lock(&desc->mutex); 2264 desc->refs--; 2265 if (desc->closed == true && desc->refs == 0) { 2266 pthread_mutex_unlock(&desc->mutex); 2267 bdev_desc_free(desc); 2268 return; 2269 } 2270 pthread_mutex_unlock(&desc->mutex); 2271 } 2272 2273 static void 2274 bdev_channel_poll_timeout_io(struct spdk_io_channel_iter *i) 2275 { 2276 struct poll_timeout_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2277 struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i); 2278 struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(io_ch); 2279 struct spdk_bdev_desc *desc = ctx->desc; 2280 struct spdk_bdev_io *bdev_io; 2281 uint64_t now; 2282 2283 pthread_mutex_lock(&desc->mutex); 2284 if (desc->closed == true) { 2285 pthread_mutex_unlock(&desc->mutex); 2286 spdk_for_each_channel_continue(i, -1); 2287 return; 2288 } 2289 pthread_mutex_unlock(&desc->mutex); 2290 2291 now = spdk_get_ticks(); 2292 TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) { 2293 /* I/O are added to this TAILQ as they are submitted. 2294 * So once we find an I/O that has not timed out, we can immediately exit the loop. */ 2295 if (now < (bdev_io->internal.submit_tsc + 2296 ctx->timeout_in_sec * spdk_get_ticks_hz())) { 2297 goto end; 2298 } 2299 2300 if (bdev_io->internal.desc == desc) { 2301 ctx->cb_fn(ctx->cb_arg, bdev_io); 2302 } 2303 } 2304 2305 end: 2306 spdk_for_each_channel_continue(i, 0); 2307 } 2308 2309 static int 2310 bdev_poll_timeout_io(void *arg) 2311 { 2312 struct spdk_bdev_desc *desc = arg; 2313 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 2314 struct poll_timeout_ctx *ctx; 2315 2316 ctx = calloc(1, sizeof(struct poll_timeout_ctx)); 2317 if (!ctx) { 2318 SPDK_ERRLOG("failed to allocate memory\n"); 2319 return 1; 2320 } 2321 ctx->desc = desc; 2322 ctx->cb_arg = desc->cb_arg; 2323 ctx->cb_fn = desc->cb_fn; 2324 ctx->timeout_in_sec = desc->timeout_in_sec; 2325 2326 /* Take a ref on the descriptor in case it gets closed while we are checking 2327 * all of the channels. 2328 */ 2329 pthread_mutex_lock(&desc->mutex); 2330 desc->refs++; 2331 pthread_mutex_unlock(&desc->mutex); 2332 2333 spdk_for_each_channel(__bdev_to_io_dev(bdev), 2334 bdev_channel_poll_timeout_io, 2335 ctx, 2336 bdev_channel_poll_timeout_io_done); 2337 2338 return 1; 2339 } 2340 2341 int 2342 spdk_bdev_set_timeout(struct spdk_bdev_desc *desc, uint64_t timeout_in_sec, 2343 spdk_bdev_io_timeout_cb cb_fn, void *cb_arg) 2344 { 2345 assert(desc->thread == spdk_get_thread()); 2346 2347 spdk_poller_unregister(&desc->io_timeout_poller); 2348 2349 if (timeout_in_sec) { 2350 assert(cb_fn != NULL); 2351 desc->io_timeout_poller = spdk_poller_register(bdev_poll_timeout_io, 2352 desc, 2353 SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * SPDK_SEC_TO_USEC / 2354 1000); 2355 if (desc->io_timeout_poller == NULL) { 2356 SPDK_ERRLOG("can not register the desc timeout IO poller\n"); 2357 return -1; 2358 } 2359 } 2360 2361 desc->cb_fn = cb_fn; 2362 desc->cb_arg = cb_arg; 2363 desc->timeout_in_sec = timeout_in_sec; 2364 2365 return 0; 2366 } 2367 2368 static int 2369 bdev_channel_create(void *io_device, void *ctx_buf) 2370 { 2371 struct spdk_bdev *bdev = __bdev_from_io_dev(io_device); 2372 struct spdk_bdev_channel *ch = ctx_buf; 2373 struct spdk_io_channel *mgmt_io_ch; 2374 struct spdk_bdev_mgmt_channel *mgmt_ch; 2375 struct spdk_bdev_shared_resource *shared_resource; 2376 struct lba_range *range; 2377 2378 ch->bdev = bdev; 2379 ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt); 2380 if (!ch->channel) { 2381 return -1; 2382 } 2383 2384 assert(ch->histogram == NULL); 2385 if (bdev->internal.histogram_enabled) { 2386 ch->histogram = spdk_histogram_data_alloc(); 2387 if (ch->histogram == NULL) { 2388 SPDK_ERRLOG("Could not allocate histogram\n"); 2389 } 2390 } 2391 2392 mgmt_io_ch = spdk_get_io_channel(&g_bdev_mgr); 2393 if (!mgmt_io_ch) { 2394 spdk_put_io_channel(ch->channel); 2395 return -1; 2396 } 2397 2398 mgmt_ch = spdk_io_channel_get_ctx(mgmt_io_ch); 2399 TAILQ_FOREACH(shared_resource, &mgmt_ch->shared_resources, link) { 2400 if (shared_resource->shared_ch == ch->channel) { 2401 spdk_put_io_channel(mgmt_io_ch); 2402 shared_resource->ref++; 2403 break; 2404 } 2405 } 2406 2407 if (shared_resource == NULL) { 2408 shared_resource = calloc(1, sizeof(*shared_resource)); 2409 if (shared_resource == NULL) { 2410 spdk_put_io_channel(ch->channel); 2411 spdk_put_io_channel(mgmt_io_ch); 2412 return -1; 2413 } 2414 2415 shared_resource->mgmt_ch = mgmt_ch; 2416 shared_resource->io_outstanding = 0; 2417 TAILQ_INIT(&shared_resource->nomem_io); 2418 shared_resource->nomem_threshold = 0; 2419 shared_resource->shared_ch = ch->channel; 2420 shared_resource->ref = 1; 2421 TAILQ_INSERT_TAIL(&mgmt_ch->shared_resources, shared_resource, link); 2422 } 2423 2424 memset(&ch->stat, 0, sizeof(ch->stat)); 2425 ch->stat.ticks_rate = spdk_get_ticks_hz(); 2426 ch->io_outstanding = 0; 2427 TAILQ_INIT(&ch->queued_resets); 2428 TAILQ_INIT(&ch->locked_ranges); 2429 ch->flags = 0; 2430 ch->shared_resource = shared_resource; 2431 2432 TAILQ_INIT(&ch->io_submitted); 2433 TAILQ_INIT(&ch->io_locked); 2434 2435 #ifdef SPDK_CONFIG_VTUNE 2436 { 2437 char *name; 2438 __itt_init_ittlib(NULL, 0); 2439 name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch); 2440 if (!name) { 2441 bdev_channel_destroy_resource(ch); 2442 return -1; 2443 } 2444 ch->handle = __itt_string_handle_create(name); 2445 free(name); 2446 ch->start_tsc = spdk_get_ticks(); 2447 ch->interval_tsc = spdk_get_ticks_hz() / 100; 2448 memset(&ch->prev_stat, 0, sizeof(ch->prev_stat)); 2449 } 2450 #endif 2451 2452 pthread_mutex_lock(&bdev->internal.mutex); 2453 bdev_enable_qos(bdev, ch); 2454 2455 TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) { 2456 struct lba_range *new_range; 2457 2458 new_range = calloc(1, sizeof(*new_range)); 2459 if (new_range == NULL) { 2460 pthread_mutex_unlock(&bdev->internal.mutex); 2461 bdev_channel_destroy_resource(ch); 2462 return -1; 2463 } 2464 new_range->length = range->length; 2465 new_range->offset = range->offset; 2466 TAILQ_INSERT_TAIL(&ch->locked_ranges, new_range, tailq); 2467 } 2468 2469 pthread_mutex_unlock(&bdev->internal.mutex); 2470 2471 return 0; 2472 } 2473 2474 /* 2475 * Abort I/O that are waiting on a data buffer. These types of I/O are 2476 * linked using the spdk_bdev_io internal.buf_link TAILQ_ENTRY. 2477 */ 2478 static void 2479 bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_channel *ch) 2480 { 2481 bdev_io_stailq_t tmp; 2482 struct spdk_bdev_io *bdev_io; 2483 2484 STAILQ_INIT(&tmp); 2485 2486 while (!STAILQ_EMPTY(queue)) { 2487 bdev_io = STAILQ_FIRST(queue); 2488 STAILQ_REMOVE_HEAD(queue, internal.buf_link); 2489 if (bdev_io->internal.ch == ch) { 2490 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 2491 } else { 2492 STAILQ_INSERT_TAIL(&tmp, bdev_io, internal.buf_link); 2493 } 2494 } 2495 2496 STAILQ_SWAP(&tmp, queue, spdk_bdev_io); 2497 } 2498 2499 /* 2500 * Abort I/O that are queued waiting for submission. These types of I/O are 2501 * linked using the spdk_bdev_io link TAILQ_ENTRY. 2502 */ 2503 static void 2504 bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch) 2505 { 2506 struct spdk_bdev_io *bdev_io, *tmp; 2507 2508 TAILQ_FOREACH_SAFE(bdev_io, queue, internal.link, tmp) { 2509 if (bdev_io->internal.ch == ch) { 2510 TAILQ_REMOVE(queue, bdev_io, internal.link); 2511 /* 2512 * spdk_bdev_io_complete() assumes that the completed I/O had 2513 * been submitted to the bdev module. Since in this case it 2514 * hadn't, bump io_outstanding to account for the decrement 2515 * that spdk_bdev_io_complete() will do. 2516 */ 2517 if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) { 2518 ch->io_outstanding++; 2519 ch->shared_resource->io_outstanding++; 2520 } 2521 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 2522 } 2523 } 2524 } 2525 2526 static void 2527 bdev_qos_channel_destroy(void *cb_arg) 2528 { 2529 struct spdk_bdev_qos *qos = cb_arg; 2530 2531 spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch)); 2532 spdk_poller_unregister(&qos->poller); 2533 2534 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Free QoS %p.\n", qos); 2535 2536 free(qos); 2537 } 2538 2539 static int 2540 bdev_qos_destroy(struct spdk_bdev *bdev) 2541 { 2542 int i; 2543 2544 /* 2545 * Cleanly shutting down the QoS poller is tricky, because 2546 * during the asynchronous operation the user could open 2547 * a new descriptor and create a new channel, spawning 2548 * a new QoS poller. 2549 * 2550 * The strategy is to create a new QoS structure here and swap it 2551 * in. The shutdown path then continues to refer to the old one 2552 * until it completes and then releases it. 2553 */ 2554 struct spdk_bdev_qos *new_qos, *old_qos; 2555 2556 old_qos = bdev->internal.qos; 2557 2558 new_qos = calloc(1, sizeof(*new_qos)); 2559 if (!new_qos) { 2560 SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n"); 2561 return -ENOMEM; 2562 } 2563 2564 /* Copy the old QoS data into the newly allocated structure */ 2565 memcpy(new_qos, old_qos, sizeof(*new_qos)); 2566 2567 /* Zero out the key parts of the QoS structure */ 2568 new_qos->ch = NULL; 2569 new_qos->thread = NULL; 2570 new_qos->poller = NULL; 2571 TAILQ_INIT(&new_qos->queued); 2572 /* 2573 * The limit member of spdk_bdev_qos_limit structure is not zeroed. 2574 * It will be used later for the new QoS structure. 2575 */ 2576 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2577 new_qos->rate_limits[i].remaining_this_timeslice = 0; 2578 new_qos->rate_limits[i].min_per_timeslice = 0; 2579 new_qos->rate_limits[i].max_per_timeslice = 0; 2580 } 2581 2582 bdev->internal.qos = new_qos; 2583 2584 if (old_qos->thread == NULL) { 2585 free(old_qos); 2586 } else { 2587 spdk_thread_send_msg(old_qos->thread, bdev_qos_channel_destroy, old_qos); 2588 } 2589 2590 /* It is safe to continue with destroying the bdev even though the QoS channel hasn't 2591 * been destroyed yet. The destruction path will end up waiting for the final 2592 * channel to be put before it releases resources. */ 2593 2594 return 0; 2595 } 2596 2597 static void 2598 bdev_io_stat_add(struct spdk_bdev_io_stat *total, struct spdk_bdev_io_stat *add) 2599 { 2600 total->bytes_read += add->bytes_read; 2601 total->num_read_ops += add->num_read_ops; 2602 total->bytes_written += add->bytes_written; 2603 total->num_write_ops += add->num_write_ops; 2604 total->bytes_unmapped += add->bytes_unmapped; 2605 total->num_unmap_ops += add->num_unmap_ops; 2606 total->read_latency_ticks += add->read_latency_ticks; 2607 total->write_latency_ticks += add->write_latency_ticks; 2608 total->unmap_latency_ticks += add->unmap_latency_ticks; 2609 } 2610 2611 static void 2612 bdev_channel_destroy(void *io_device, void *ctx_buf) 2613 { 2614 struct spdk_bdev_channel *ch = ctx_buf; 2615 struct spdk_bdev_mgmt_channel *mgmt_ch; 2616 struct spdk_bdev_shared_resource *shared_resource = ch->shared_resource; 2617 2618 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Destroying channel %p for bdev %s on thread %p\n", ch, ch->bdev->name, 2619 spdk_get_thread()); 2620 2621 /* This channel is going away, so add its statistics into the bdev so that they don't get lost. */ 2622 pthread_mutex_lock(&ch->bdev->internal.mutex); 2623 bdev_io_stat_add(&ch->bdev->internal.stat, &ch->stat); 2624 pthread_mutex_unlock(&ch->bdev->internal.mutex); 2625 2626 mgmt_ch = shared_resource->mgmt_ch; 2627 2628 bdev_abort_queued_io(&ch->queued_resets, ch); 2629 bdev_abort_queued_io(&shared_resource->nomem_io, ch); 2630 bdev_abort_buf_io(&mgmt_ch->need_buf_small, ch); 2631 bdev_abort_buf_io(&mgmt_ch->need_buf_large, ch); 2632 2633 if (ch->histogram) { 2634 spdk_histogram_data_free(ch->histogram); 2635 } 2636 2637 bdev_channel_destroy_resource(ch); 2638 } 2639 2640 int 2641 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias) 2642 { 2643 struct spdk_bdev_alias *tmp; 2644 2645 if (alias == NULL) { 2646 SPDK_ERRLOG("Empty alias passed\n"); 2647 return -EINVAL; 2648 } 2649 2650 if (spdk_bdev_get_by_name(alias)) { 2651 SPDK_ERRLOG("Bdev name/alias: %s already exists\n", alias); 2652 return -EEXIST; 2653 } 2654 2655 tmp = calloc(1, sizeof(*tmp)); 2656 if (tmp == NULL) { 2657 SPDK_ERRLOG("Unable to allocate alias\n"); 2658 return -ENOMEM; 2659 } 2660 2661 tmp->alias = strdup(alias); 2662 if (tmp->alias == NULL) { 2663 free(tmp); 2664 SPDK_ERRLOG("Unable to allocate alias\n"); 2665 return -ENOMEM; 2666 } 2667 2668 TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq); 2669 2670 return 0; 2671 } 2672 2673 int 2674 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias) 2675 { 2676 struct spdk_bdev_alias *tmp; 2677 2678 TAILQ_FOREACH(tmp, &bdev->aliases, tailq) { 2679 if (strcmp(alias, tmp->alias) == 0) { 2680 TAILQ_REMOVE(&bdev->aliases, tmp, tailq); 2681 free(tmp->alias); 2682 free(tmp); 2683 return 0; 2684 } 2685 } 2686 2687 SPDK_INFOLOG(SPDK_LOG_BDEV, "Alias %s does not exists\n", alias); 2688 2689 return -ENOENT; 2690 } 2691 2692 void 2693 spdk_bdev_alias_del_all(struct spdk_bdev *bdev) 2694 { 2695 struct spdk_bdev_alias *p, *tmp; 2696 2697 TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) { 2698 TAILQ_REMOVE(&bdev->aliases, p, tailq); 2699 free(p->alias); 2700 free(p); 2701 } 2702 } 2703 2704 struct spdk_io_channel * 2705 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc) 2706 { 2707 return spdk_get_io_channel(__bdev_to_io_dev(spdk_bdev_desc_get_bdev(desc))); 2708 } 2709 2710 const char * 2711 spdk_bdev_get_name(const struct spdk_bdev *bdev) 2712 { 2713 return bdev->name; 2714 } 2715 2716 const char * 2717 spdk_bdev_get_product_name(const struct spdk_bdev *bdev) 2718 { 2719 return bdev->product_name; 2720 } 2721 2722 const struct spdk_bdev_aliases_list * 2723 spdk_bdev_get_aliases(const struct spdk_bdev *bdev) 2724 { 2725 return &bdev->aliases; 2726 } 2727 2728 uint32_t 2729 spdk_bdev_get_block_size(const struct spdk_bdev *bdev) 2730 { 2731 return bdev->blocklen; 2732 } 2733 2734 uint32_t 2735 spdk_bdev_get_write_unit_size(const struct spdk_bdev *bdev) 2736 { 2737 return bdev->write_unit_size; 2738 } 2739 2740 uint64_t 2741 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev) 2742 { 2743 return bdev->blockcnt; 2744 } 2745 2746 const char * 2747 spdk_bdev_get_qos_rpc_type(enum spdk_bdev_qos_rate_limit_type type) 2748 { 2749 return qos_rpc_type[type]; 2750 } 2751 2752 void 2753 spdk_bdev_get_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits) 2754 { 2755 int i; 2756 2757 memset(limits, 0, sizeof(*limits) * SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES); 2758 2759 pthread_mutex_lock(&bdev->internal.mutex); 2760 if (bdev->internal.qos) { 2761 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 2762 if (bdev->internal.qos->rate_limits[i].limit != 2763 SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 2764 limits[i] = bdev->internal.qos->rate_limits[i].limit; 2765 if (bdev_qos_is_iops_rate_limit(i) == false) { 2766 /* Change from Byte to Megabyte which is user visible. */ 2767 limits[i] = limits[i] / 1024 / 1024; 2768 } 2769 } 2770 } 2771 } 2772 pthread_mutex_unlock(&bdev->internal.mutex); 2773 } 2774 2775 size_t 2776 spdk_bdev_get_buf_align(const struct spdk_bdev *bdev) 2777 { 2778 return 1 << bdev->required_alignment; 2779 } 2780 2781 uint32_t 2782 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev) 2783 { 2784 return bdev->optimal_io_boundary; 2785 } 2786 2787 bool 2788 spdk_bdev_has_write_cache(const struct spdk_bdev *bdev) 2789 { 2790 return bdev->write_cache; 2791 } 2792 2793 const struct spdk_uuid * 2794 spdk_bdev_get_uuid(const struct spdk_bdev *bdev) 2795 { 2796 return &bdev->uuid; 2797 } 2798 2799 uint32_t 2800 spdk_bdev_get_md_size(const struct spdk_bdev *bdev) 2801 { 2802 return bdev->md_len; 2803 } 2804 2805 bool 2806 spdk_bdev_is_md_interleaved(const struct spdk_bdev *bdev) 2807 { 2808 return (bdev->md_len != 0) && bdev->md_interleave; 2809 } 2810 2811 bool 2812 spdk_bdev_is_md_separate(const struct spdk_bdev *bdev) 2813 { 2814 return (bdev->md_len != 0) && !bdev->md_interleave; 2815 } 2816 2817 bool 2818 spdk_bdev_is_zoned(const struct spdk_bdev *bdev) 2819 { 2820 return bdev->zoned; 2821 } 2822 2823 uint32_t 2824 spdk_bdev_get_data_block_size(const struct spdk_bdev *bdev) 2825 { 2826 if (spdk_bdev_is_md_interleaved(bdev)) { 2827 return bdev->blocklen - bdev->md_len; 2828 } else { 2829 return bdev->blocklen; 2830 } 2831 } 2832 2833 static uint32_t 2834 _bdev_get_block_size_with_md(const struct spdk_bdev *bdev) 2835 { 2836 if (!spdk_bdev_is_md_interleaved(bdev)) { 2837 return bdev->blocklen + bdev->md_len; 2838 } else { 2839 return bdev->blocklen; 2840 } 2841 } 2842 2843 enum spdk_dif_type spdk_bdev_get_dif_type(const struct spdk_bdev *bdev) 2844 { 2845 if (bdev->md_len != 0) { 2846 return bdev->dif_type; 2847 } else { 2848 return SPDK_DIF_DISABLE; 2849 } 2850 } 2851 2852 bool 2853 spdk_bdev_is_dif_head_of_md(const struct spdk_bdev *bdev) 2854 { 2855 if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 2856 return bdev->dif_is_head_of_md; 2857 } else { 2858 return false; 2859 } 2860 } 2861 2862 bool 2863 spdk_bdev_is_dif_check_enabled(const struct spdk_bdev *bdev, 2864 enum spdk_dif_check_type check_type) 2865 { 2866 if (spdk_bdev_get_dif_type(bdev) == SPDK_DIF_DISABLE) { 2867 return false; 2868 } 2869 2870 switch (check_type) { 2871 case SPDK_DIF_CHECK_TYPE_REFTAG: 2872 return (bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) != 0; 2873 case SPDK_DIF_CHECK_TYPE_APPTAG: 2874 return (bdev->dif_check_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) != 0; 2875 case SPDK_DIF_CHECK_TYPE_GUARD: 2876 return (bdev->dif_check_flags & SPDK_DIF_FLAGS_GUARD_CHECK) != 0; 2877 default: 2878 return false; 2879 } 2880 } 2881 2882 uint64_t 2883 spdk_bdev_get_qd(const struct spdk_bdev *bdev) 2884 { 2885 return bdev->internal.measured_queue_depth; 2886 } 2887 2888 uint64_t 2889 spdk_bdev_get_qd_sampling_period(const struct spdk_bdev *bdev) 2890 { 2891 return bdev->internal.period; 2892 } 2893 2894 uint64_t 2895 spdk_bdev_get_weighted_io_time(const struct spdk_bdev *bdev) 2896 { 2897 return bdev->internal.weighted_io_time; 2898 } 2899 2900 uint64_t 2901 spdk_bdev_get_io_time(const struct spdk_bdev *bdev) 2902 { 2903 return bdev->internal.io_time; 2904 } 2905 2906 static void 2907 _calculate_measured_qd_cpl(struct spdk_io_channel_iter *i, int status) 2908 { 2909 struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i); 2910 2911 bdev->internal.measured_queue_depth = bdev->internal.temporary_queue_depth; 2912 2913 if (bdev->internal.measured_queue_depth) { 2914 bdev->internal.io_time += bdev->internal.period; 2915 bdev->internal.weighted_io_time += bdev->internal.period * bdev->internal.measured_queue_depth; 2916 } 2917 } 2918 2919 static void 2920 _calculate_measured_qd(struct spdk_io_channel_iter *i) 2921 { 2922 struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i); 2923 struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i); 2924 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(io_ch); 2925 2926 bdev->internal.temporary_queue_depth += ch->io_outstanding; 2927 spdk_for_each_channel_continue(i, 0); 2928 } 2929 2930 static int 2931 bdev_calculate_measured_queue_depth(void *ctx) 2932 { 2933 struct spdk_bdev *bdev = ctx; 2934 bdev->internal.temporary_queue_depth = 0; 2935 spdk_for_each_channel(__bdev_to_io_dev(bdev), _calculate_measured_qd, bdev, 2936 _calculate_measured_qd_cpl); 2937 return 0; 2938 } 2939 2940 void 2941 spdk_bdev_set_qd_sampling_period(struct spdk_bdev *bdev, uint64_t period) 2942 { 2943 bdev->internal.period = period; 2944 2945 if (bdev->internal.qd_poller != NULL) { 2946 spdk_poller_unregister(&bdev->internal.qd_poller); 2947 bdev->internal.measured_queue_depth = UINT64_MAX; 2948 } 2949 2950 if (period != 0) { 2951 bdev->internal.qd_poller = spdk_poller_register(bdev_calculate_measured_queue_depth, bdev, 2952 period); 2953 } 2954 } 2955 2956 static void 2957 _resize_notify(void *arg) 2958 { 2959 struct spdk_bdev_desc *desc = arg; 2960 2961 pthread_mutex_lock(&desc->mutex); 2962 desc->refs--; 2963 if (!desc->closed) { 2964 pthread_mutex_unlock(&desc->mutex); 2965 desc->callback.event_fn(SPDK_BDEV_EVENT_RESIZE, 2966 desc->bdev, 2967 desc->callback.ctx); 2968 return; 2969 } else if (0 == desc->refs) { 2970 /* This descriptor was closed after this resize_notify message was sent. 2971 * spdk_bdev_close() could not free the descriptor since this message was 2972 * in flight, so we free it now using bdev_desc_free(). 2973 */ 2974 pthread_mutex_unlock(&desc->mutex); 2975 bdev_desc_free(desc); 2976 return; 2977 } 2978 pthread_mutex_unlock(&desc->mutex); 2979 } 2980 2981 int 2982 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size) 2983 { 2984 struct spdk_bdev_desc *desc; 2985 int ret; 2986 2987 pthread_mutex_lock(&bdev->internal.mutex); 2988 2989 /* bdev has open descriptors */ 2990 if (!TAILQ_EMPTY(&bdev->internal.open_descs) && 2991 bdev->blockcnt > size) { 2992 ret = -EBUSY; 2993 } else { 2994 bdev->blockcnt = size; 2995 TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) { 2996 pthread_mutex_lock(&desc->mutex); 2997 if (desc->callback.open_with_ext && !desc->closed) { 2998 desc->refs++; 2999 spdk_thread_send_msg(desc->thread, _resize_notify, desc); 3000 } 3001 pthread_mutex_unlock(&desc->mutex); 3002 } 3003 ret = 0; 3004 } 3005 3006 pthread_mutex_unlock(&bdev->internal.mutex); 3007 3008 return ret; 3009 } 3010 3011 /* 3012 * Convert I/O offset and length from bytes to blocks. 3013 * 3014 * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size. 3015 */ 3016 static uint64_t 3017 bdev_bytes_to_blocks(struct spdk_bdev *bdev, uint64_t offset_bytes, uint64_t *offset_blocks, 3018 uint64_t num_bytes, uint64_t *num_blocks) 3019 { 3020 uint32_t block_size = bdev->blocklen; 3021 uint8_t shift_cnt; 3022 3023 /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */ 3024 if (spdk_likely(spdk_u32_is_pow2(block_size))) { 3025 shift_cnt = spdk_u32log2(block_size); 3026 *offset_blocks = offset_bytes >> shift_cnt; 3027 *num_blocks = num_bytes >> shift_cnt; 3028 return (offset_bytes - (*offset_blocks << shift_cnt)) | 3029 (num_bytes - (*num_blocks << shift_cnt)); 3030 } else { 3031 *offset_blocks = offset_bytes / block_size; 3032 *num_blocks = num_bytes / block_size; 3033 return (offset_bytes % block_size) | (num_bytes % block_size); 3034 } 3035 } 3036 3037 static bool 3038 bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks) 3039 { 3040 /* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there 3041 * has been an overflow and hence the offset has been wrapped around */ 3042 if (offset_blocks + num_blocks < offset_blocks) { 3043 return false; 3044 } 3045 3046 /* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */ 3047 if (offset_blocks + num_blocks > bdev->blockcnt) { 3048 return false; 3049 } 3050 3051 return true; 3052 } 3053 3054 static bool 3055 _bdev_io_check_md_buf(const struct iovec *iovs, const void *md_buf) 3056 { 3057 return _is_buf_allocated(iovs) == (md_buf != NULL); 3058 } 3059 3060 static int 3061 bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf, 3062 void *md_buf, int64_t offset_blocks, uint64_t num_blocks, 3063 spdk_bdev_io_completion_cb cb, void *cb_arg) 3064 { 3065 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3066 struct spdk_bdev_io *bdev_io; 3067 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3068 3069 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3070 return -EINVAL; 3071 } 3072 3073 bdev_io = bdev_channel_get_io(channel); 3074 if (!bdev_io) { 3075 return -ENOMEM; 3076 } 3077 3078 bdev_io->internal.ch = channel; 3079 bdev_io->internal.desc = desc; 3080 bdev_io->type = SPDK_BDEV_IO_TYPE_READ; 3081 bdev_io->u.bdev.iovs = &bdev_io->iov; 3082 bdev_io->u.bdev.iovs[0].iov_base = buf; 3083 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 3084 bdev_io->u.bdev.iovcnt = 1; 3085 bdev_io->u.bdev.md_buf = md_buf; 3086 bdev_io->u.bdev.num_blocks = num_blocks; 3087 bdev_io->u.bdev.offset_blocks = offset_blocks; 3088 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3089 3090 bdev_io_submit(bdev_io); 3091 return 0; 3092 } 3093 3094 int 3095 spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3096 void *buf, uint64_t offset, uint64_t nbytes, 3097 spdk_bdev_io_completion_cb cb, void *cb_arg) 3098 { 3099 uint64_t offset_blocks, num_blocks; 3100 3101 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3102 nbytes, &num_blocks) != 0) { 3103 return -EINVAL; 3104 } 3105 3106 return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg); 3107 } 3108 3109 int 3110 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3111 void *buf, uint64_t offset_blocks, uint64_t num_blocks, 3112 spdk_bdev_io_completion_cb cb, void *cb_arg) 3113 { 3114 return bdev_read_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, cb, cb_arg); 3115 } 3116 3117 int 3118 spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3119 void *buf, void *md_buf, int64_t offset_blocks, uint64_t num_blocks, 3120 spdk_bdev_io_completion_cb cb, void *cb_arg) 3121 { 3122 struct iovec iov = { 3123 .iov_base = buf, 3124 }; 3125 3126 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 3127 return -EINVAL; 3128 } 3129 3130 if (!_bdev_io_check_md_buf(&iov, md_buf)) { 3131 return -EINVAL; 3132 } 3133 3134 return bdev_read_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks, 3135 cb, cb_arg); 3136 } 3137 3138 int 3139 spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3140 struct iovec *iov, int iovcnt, 3141 uint64_t offset, uint64_t nbytes, 3142 spdk_bdev_io_completion_cb cb, void *cb_arg) 3143 { 3144 uint64_t offset_blocks, num_blocks; 3145 3146 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3147 nbytes, &num_blocks) != 0) { 3148 return -EINVAL; 3149 } 3150 3151 return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 3152 } 3153 3154 static int 3155 bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3156 struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks, 3157 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg) 3158 { 3159 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3160 struct spdk_bdev_io *bdev_io; 3161 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3162 3163 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3164 return -EINVAL; 3165 } 3166 3167 bdev_io = bdev_channel_get_io(channel); 3168 if (!bdev_io) { 3169 return -ENOMEM; 3170 } 3171 3172 bdev_io->internal.ch = channel; 3173 bdev_io->internal.desc = desc; 3174 bdev_io->type = SPDK_BDEV_IO_TYPE_READ; 3175 bdev_io->u.bdev.iovs = iov; 3176 bdev_io->u.bdev.iovcnt = iovcnt; 3177 bdev_io->u.bdev.md_buf = md_buf; 3178 bdev_io->u.bdev.num_blocks = num_blocks; 3179 bdev_io->u.bdev.offset_blocks = offset_blocks; 3180 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3181 3182 bdev_io_submit(bdev_io); 3183 return 0; 3184 } 3185 3186 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3187 struct iovec *iov, int iovcnt, 3188 uint64_t offset_blocks, uint64_t num_blocks, 3189 spdk_bdev_io_completion_cb cb, void *cb_arg) 3190 { 3191 return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks, 3192 num_blocks, cb, cb_arg); 3193 } 3194 3195 int 3196 spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3197 struct iovec *iov, int iovcnt, void *md_buf, 3198 uint64_t offset_blocks, uint64_t num_blocks, 3199 spdk_bdev_io_completion_cb cb, void *cb_arg) 3200 { 3201 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 3202 return -EINVAL; 3203 } 3204 3205 if (!_bdev_io_check_md_buf(iov, md_buf)) { 3206 return -EINVAL; 3207 } 3208 3209 return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks, 3210 num_blocks, cb, cb_arg); 3211 } 3212 3213 static int 3214 bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3215 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 3216 spdk_bdev_io_completion_cb cb, void *cb_arg) 3217 { 3218 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3219 struct spdk_bdev_io *bdev_io; 3220 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3221 3222 if (!desc->write) { 3223 return -EBADF; 3224 } 3225 3226 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3227 return -EINVAL; 3228 } 3229 3230 bdev_io = bdev_channel_get_io(channel); 3231 if (!bdev_io) { 3232 return -ENOMEM; 3233 } 3234 3235 bdev_io->internal.ch = channel; 3236 bdev_io->internal.desc = desc; 3237 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE; 3238 bdev_io->u.bdev.iovs = &bdev_io->iov; 3239 bdev_io->u.bdev.iovs[0].iov_base = buf; 3240 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 3241 bdev_io->u.bdev.iovcnt = 1; 3242 bdev_io->u.bdev.md_buf = md_buf; 3243 bdev_io->u.bdev.num_blocks = num_blocks; 3244 bdev_io->u.bdev.offset_blocks = offset_blocks; 3245 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3246 3247 bdev_io_submit(bdev_io); 3248 return 0; 3249 } 3250 3251 int 3252 spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3253 void *buf, uint64_t offset, uint64_t nbytes, 3254 spdk_bdev_io_completion_cb cb, void *cb_arg) 3255 { 3256 uint64_t offset_blocks, num_blocks; 3257 3258 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3259 nbytes, &num_blocks) != 0) { 3260 return -EINVAL; 3261 } 3262 3263 return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg); 3264 } 3265 3266 int 3267 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3268 void *buf, uint64_t offset_blocks, uint64_t num_blocks, 3269 spdk_bdev_io_completion_cb cb, void *cb_arg) 3270 { 3271 return bdev_write_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, 3272 cb, cb_arg); 3273 } 3274 3275 int 3276 spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3277 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 3278 spdk_bdev_io_completion_cb cb, void *cb_arg) 3279 { 3280 struct iovec iov = { 3281 .iov_base = buf, 3282 }; 3283 3284 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 3285 return -EINVAL; 3286 } 3287 3288 if (!_bdev_io_check_md_buf(&iov, md_buf)) { 3289 return -EINVAL; 3290 } 3291 3292 return bdev_write_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks, 3293 cb, cb_arg); 3294 } 3295 3296 static int 3297 bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3298 struct iovec *iov, int iovcnt, void *md_buf, 3299 uint64_t offset_blocks, uint64_t num_blocks, 3300 spdk_bdev_io_completion_cb cb, void *cb_arg) 3301 { 3302 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3303 struct spdk_bdev_io *bdev_io; 3304 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3305 3306 if (!desc->write) { 3307 return -EBADF; 3308 } 3309 3310 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3311 return -EINVAL; 3312 } 3313 3314 bdev_io = bdev_channel_get_io(channel); 3315 if (!bdev_io) { 3316 return -ENOMEM; 3317 } 3318 3319 bdev_io->internal.ch = channel; 3320 bdev_io->internal.desc = desc; 3321 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE; 3322 bdev_io->u.bdev.iovs = iov; 3323 bdev_io->u.bdev.iovcnt = iovcnt; 3324 bdev_io->u.bdev.md_buf = md_buf; 3325 bdev_io->u.bdev.num_blocks = num_blocks; 3326 bdev_io->u.bdev.offset_blocks = offset_blocks; 3327 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3328 3329 bdev_io_submit(bdev_io); 3330 return 0; 3331 } 3332 3333 int 3334 spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3335 struct iovec *iov, int iovcnt, 3336 uint64_t offset, uint64_t len, 3337 spdk_bdev_io_completion_cb cb, void *cb_arg) 3338 { 3339 uint64_t offset_blocks, num_blocks; 3340 3341 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3342 len, &num_blocks) != 0) { 3343 return -EINVAL; 3344 } 3345 3346 return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg); 3347 } 3348 3349 int 3350 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3351 struct iovec *iov, int iovcnt, 3352 uint64_t offset_blocks, uint64_t num_blocks, 3353 spdk_bdev_io_completion_cb cb, void *cb_arg) 3354 { 3355 return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks, 3356 num_blocks, cb, cb_arg); 3357 } 3358 3359 int 3360 spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3361 struct iovec *iov, int iovcnt, void *md_buf, 3362 uint64_t offset_blocks, uint64_t num_blocks, 3363 spdk_bdev_io_completion_cb cb, void *cb_arg) 3364 { 3365 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 3366 return -EINVAL; 3367 } 3368 3369 if (!_bdev_io_check_md_buf(iov, md_buf)) { 3370 return -EINVAL; 3371 } 3372 3373 return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks, 3374 num_blocks, cb, cb_arg); 3375 } 3376 3377 static void 3378 bdev_compare_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 3379 { 3380 struct spdk_bdev_io *parent_io = cb_arg; 3381 uint8_t *read_buf = bdev_io->u.bdev.iovs[0].iov_base; 3382 int i, rc = 0; 3383 3384 if (!success) { 3385 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 3386 parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx); 3387 spdk_bdev_free_io(bdev_io); 3388 return; 3389 } 3390 3391 for (i = 0; i < parent_io->u.bdev.iovcnt; i++) { 3392 rc = memcmp(read_buf, 3393 parent_io->u.bdev.iovs[i].iov_base, 3394 parent_io->u.bdev.iovs[i].iov_len); 3395 if (rc) { 3396 break; 3397 } 3398 read_buf += parent_io->u.bdev.iovs[i].iov_len; 3399 } 3400 3401 spdk_bdev_free_io(bdev_io); 3402 3403 if (rc == 0) { 3404 parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 3405 parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx); 3406 } else { 3407 parent_io->internal.status = SPDK_BDEV_IO_STATUS_MISCOMPARE; 3408 parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx); 3409 } 3410 } 3411 3412 static void 3413 bdev_compare_do_read(void *_bdev_io) 3414 { 3415 struct spdk_bdev_io *bdev_io = _bdev_io; 3416 int rc; 3417 3418 rc = spdk_bdev_read_blocks(bdev_io->internal.desc, 3419 spdk_io_channel_from_ctx(bdev_io->internal.ch), NULL, 3420 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks, 3421 bdev_compare_do_read_done, bdev_io); 3422 3423 if (rc == -ENOMEM) { 3424 bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_do_read); 3425 } else if (rc != 0) { 3426 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 3427 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 3428 } 3429 } 3430 3431 static int 3432 bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3433 struct iovec *iov, int iovcnt, void *md_buf, 3434 uint64_t offset_blocks, uint64_t num_blocks, 3435 spdk_bdev_io_completion_cb cb, void *cb_arg) 3436 { 3437 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3438 struct spdk_bdev_io *bdev_io; 3439 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3440 3441 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3442 return -EINVAL; 3443 } 3444 3445 bdev_io = bdev_channel_get_io(channel); 3446 if (!bdev_io) { 3447 return -ENOMEM; 3448 } 3449 3450 bdev_io->internal.ch = channel; 3451 bdev_io->internal.desc = desc; 3452 bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE; 3453 bdev_io->u.bdev.iovs = iov; 3454 bdev_io->u.bdev.iovcnt = iovcnt; 3455 bdev_io->u.bdev.md_buf = md_buf; 3456 bdev_io->u.bdev.num_blocks = num_blocks; 3457 bdev_io->u.bdev.offset_blocks = offset_blocks; 3458 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3459 3460 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) { 3461 bdev_io_submit(bdev_io); 3462 return 0; 3463 } 3464 3465 bdev_compare_do_read(bdev_io); 3466 3467 return 0; 3468 } 3469 3470 int 3471 spdk_bdev_comparev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3472 struct iovec *iov, int iovcnt, 3473 uint64_t offset_blocks, uint64_t num_blocks, 3474 spdk_bdev_io_completion_cb cb, void *cb_arg) 3475 { 3476 return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks, 3477 num_blocks, cb, cb_arg); 3478 } 3479 3480 int 3481 spdk_bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3482 struct iovec *iov, int iovcnt, void *md_buf, 3483 uint64_t offset_blocks, uint64_t num_blocks, 3484 spdk_bdev_io_completion_cb cb, void *cb_arg) 3485 { 3486 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 3487 return -EINVAL; 3488 } 3489 3490 if (!_bdev_io_check_md_buf(iov, md_buf)) { 3491 return -EINVAL; 3492 } 3493 3494 return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks, 3495 num_blocks, cb, cb_arg); 3496 } 3497 3498 static int 3499 bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3500 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 3501 spdk_bdev_io_completion_cb cb, void *cb_arg) 3502 { 3503 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3504 struct spdk_bdev_io *bdev_io; 3505 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3506 3507 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3508 return -EINVAL; 3509 } 3510 3511 bdev_io = bdev_channel_get_io(channel); 3512 if (!bdev_io) { 3513 return -ENOMEM; 3514 } 3515 3516 bdev_io->internal.ch = channel; 3517 bdev_io->internal.desc = desc; 3518 bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE; 3519 bdev_io->u.bdev.iovs = &bdev_io->iov; 3520 bdev_io->u.bdev.iovs[0].iov_base = buf; 3521 bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen; 3522 bdev_io->u.bdev.iovcnt = 1; 3523 bdev_io->u.bdev.md_buf = md_buf; 3524 bdev_io->u.bdev.num_blocks = num_blocks; 3525 bdev_io->u.bdev.offset_blocks = offset_blocks; 3526 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3527 3528 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) { 3529 bdev_io_submit(bdev_io); 3530 return 0; 3531 } 3532 3533 bdev_compare_do_read(bdev_io); 3534 3535 return 0; 3536 } 3537 3538 int 3539 spdk_bdev_compare_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3540 void *buf, uint64_t offset_blocks, uint64_t num_blocks, 3541 spdk_bdev_io_completion_cb cb, void *cb_arg) 3542 { 3543 return bdev_compare_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, 3544 cb, cb_arg); 3545 } 3546 3547 int 3548 spdk_bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3549 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks, 3550 spdk_bdev_io_completion_cb cb, void *cb_arg) 3551 { 3552 struct iovec iov = { 3553 .iov_base = buf, 3554 }; 3555 3556 if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) { 3557 return -EINVAL; 3558 } 3559 3560 if (!_bdev_io_check_md_buf(&iov, md_buf)) { 3561 return -EINVAL; 3562 } 3563 3564 return bdev_compare_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks, 3565 cb, cb_arg); 3566 } 3567 3568 static void 3569 bdev_zcopy_get_buf(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success) 3570 { 3571 if (!success) { 3572 /* Don't use spdk_bdev_io_complete here - this bdev_io was never actually submitted. */ 3573 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_NOMEM; 3574 bdev_io->internal.cb(bdev_io, success, bdev_io->internal.caller_ctx); 3575 return; 3576 } 3577 3578 if (bdev_io->u.bdev.zcopy.populate) { 3579 /* Read the real data into the buffer */ 3580 bdev_io->type = SPDK_BDEV_IO_TYPE_READ; 3581 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 3582 bdev_io_submit(bdev_io); 3583 return; 3584 } 3585 3586 /* Don't use spdk_bdev_io_complete here - this bdev_io was never actually submitted. */ 3587 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 3588 bdev_io->internal.cb(bdev_io, success, bdev_io->internal.caller_ctx); 3589 } 3590 3591 int 3592 spdk_bdev_zcopy_start(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3593 uint64_t offset_blocks, uint64_t num_blocks, 3594 bool populate, 3595 spdk_bdev_io_completion_cb cb, void *cb_arg) 3596 { 3597 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3598 struct spdk_bdev_io *bdev_io; 3599 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3600 3601 if (!desc->write) { 3602 return -EBADF; 3603 } 3604 3605 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3606 return -EINVAL; 3607 } 3608 3609 if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) { 3610 return -ENOTSUP; 3611 } 3612 3613 bdev_io = bdev_channel_get_io(channel); 3614 if (!bdev_io) { 3615 return -ENOMEM; 3616 } 3617 3618 bdev_io->internal.ch = channel; 3619 bdev_io->internal.desc = desc; 3620 bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY; 3621 bdev_io->u.bdev.num_blocks = num_blocks; 3622 bdev_io->u.bdev.offset_blocks = offset_blocks; 3623 bdev_io->u.bdev.iovs = NULL; 3624 bdev_io->u.bdev.iovcnt = 0; 3625 bdev_io->u.bdev.md_buf = NULL; 3626 bdev_io->u.bdev.zcopy.populate = populate ? 1 : 0; 3627 bdev_io->u.bdev.zcopy.commit = 0; 3628 bdev_io->u.bdev.zcopy.start = 1; 3629 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3630 3631 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) { 3632 bdev_io_submit(bdev_io); 3633 } else { 3634 /* Emulate zcopy by allocating a buffer */ 3635 spdk_bdev_io_get_buf(bdev_io, bdev_zcopy_get_buf, 3636 bdev_io->u.bdev.num_blocks * bdev->blocklen); 3637 } 3638 3639 return 0; 3640 } 3641 3642 int 3643 spdk_bdev_zcopy_end(struct spdk_bdev_io *bdev_io, bool commit, 3644 spdk_bdev_io_completion_cb cb, void *cb_arg) 3645 { 3646 struct spdk_bdev *bdev = bdev_io->bdev; 3647 3648 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) { 3649 /* This can happen if the zcopy was emulated in start */ 3650 if (bdev_io->u.bdev.zcopy.start != 1) { 3651 return -EINVAL; 3652 } 3653 bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY; 3654 } 3655 3656 if (bdev_io->type != SPDK_BDEV_IO_TYPE_ZCOPY) { 3657 return -EINVAL; 3658 } 3659 3660 bdev_io->u.bdev.zcopy.commit = commit ? 1 : 0; 3661 bdev_io->u.bdev.zcopy.start = 0; 3662 bdev_io->internal.caller_ctx = cb_arg; 3663 bdev_io->internal.cb = cb; 3664 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 3665 3666 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) { 3667 bdev_io_submit(bdev_io); 3668 return 0; 3669 } 3670 3671 if (!bdev_io->u.bdev.zcopy.commit) { 3672 /* Don't use spdk_bdev_io_complete here - this bdev_io was never actually submitted. */ 3673 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 3674 bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx); 3675 return 0; 3676 } 3677 3678 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE; 3679 bdev_io_submit(bdev_io); 3680 3681 return 0; 3682 } 3683 3684 int 3685 spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3686 uint64_t offset, uint64_t len, 3687 spdk_bdev_io_completion_cb cb, void *cb_arg) 3688 { 3689 uint64_t offset_blocks, num_blocks; 3690 3691 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3692 len, &num_blocks) != 0) { 3693 return -EINVAL; 3694 } 3695 3696 return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg); 3697 } 3698 3699 int 3700 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3701 uint64_t offset_blocks, uint64_t num_blocks, 3702 spdk_bdev_io_completion_cb cb, void *cb_arg) 3703 { 3704 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3705 struct spdk_bdev_io *bdev_io; 3706 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3707 3708 if (!desc->write) { 3709 return -EBADF; 3710 } 3711 3712 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3713 return -EINVAL; 3714 } 3715 3716 if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) && 3717 !bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)) { 3718 return -ENOTSUP; 3719 } 3720 3721 bdev_io = bdev_channel_get_io(channel); 3722 3723 if (!bdev_io) { 3724 return -ENOMEM; 3725 } 3726 3727 bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES; 3728 bdev_io->internal.ch = channel; 3729 bdev_io->internal.desc = desc; 3730 bdev_io->u.bdev.offset_blocks = offset_blocks; 3731 bdev_io->u.bdev.num_blocks = num_blocks; 3732 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3733 3734 if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) { 3735 bdev_io_submit(bdev_io); 3736 return 0; 3737 } 3738 3739 assert(bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)); 3740 assert(_bdev_get_block_size_with_md(bdev) <= ZERO_BUFFER_SIZE); 3741 bdev_io->u.bdev.split_remaining_num_blocks = num_blocks; 3742 bdev_io->u.bdev.split_current_offset_blocks = offset_blocks; 3743 bdev_write_zero_buffer_next(bdev_io); 3744 3745 return 0; 3746 } 3747 3748 int 3749 spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3750 uint64_t offset, uint64_t nbytes, 3751 spdk_bdev_io_completion_cb cb, void *cb_arg) 3752 { 3753 uint64_t offset_blocks, num_blocks; 3754 3755 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3756 nbytes, &num_blocks) != 0) { 3757 return -EINVAL; 3758 } 3759 3760 return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg); 3761 } 3762 3763 int 3764 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3765 uint64_t offset_blocks, uint64_t num_blocks, 3766 spdk_bdev_io_completion_cb cb, void *cb_arg) 3767 { 3768 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3769 struct spdk_bdev_io *bdev_io; 3770 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3771 3772 if (!desc->write) { 3773 return -EBADF; 3774 } 3775 3776 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3777 return -EINVAL; 3778 } 3779 3780 if (num_blocks == 0) { 3781 SPDK_ERRLOG("Can't unmap 0 bytes\n"); 3782 return -EINVAL; 3783 } 3784 3785 bdev_io = bdev_channel_get_io(channel); 3786 if (!bdev_io) { 3787 return -ENOMEM; 3788 } 3789 3790 bdev_io->internal.ch = channel; 3791 bdev_io->internal.desc = desc; 3792 bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP; 3793 3794 bdev_io->u.bdev.iovs = &bdev_io->iov; 3795 bdev_io->u.bdev.iovs[0].iov_base = NULL; 3796 bdev_io->u.bdev.iovs[0].iov_len = 0; 3797 bdev_io->u.bdev.iovcnt = 1; 3798 3799 bdev_io->u.bdev.offset_blocks = offset_blocks; 3800 bdev_io->u.bdev.num_blocks = num_blocks; 3801 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3802 3803 bdev_io_submit(bdev_io); 3804 return 0; 3805 } 3806 3807 int 3808 spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3809 uint64_t offset, uint64_t length, 3810 spdk_bdev_io_completion_cb cb, void *cb_arg) 3811 { 3812 uint64_t offset_blocks, num_blocks; 3813 3814 if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks, 3815 length, &num_blocks) != 0) { 3816 return -EINVAL; 3817 } 3818 3819 return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg); 3820 } 3821 3822 int 3823 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3824 uint64_t offset_blocks, uint64_t num_blocks, 3825 spdk_bdev_io_completion_cb cb, void *cb_arg) 3826 { 3827 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3828 struct spdk_bdev_io *bdev_io; 3829 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3830 3831 if (!desc->write) { 3832 return -EBADF; 3833 } 3834 3835 if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) { 3836 return -EINVAL; 3837 } 3838 3839 bdev_io = bdev_channel_get_io(channel); 3840 if (!bdev_io) { 3841 return -ENOMEM; 3842 } 3843 3844 bdev_io->internal.ch = channel; 3845 bdev_io->internal.desc = desc; 3846 bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH; 3847 bdev_io->u.bdev.iovs = NULL; 3848 bdev_io->u.bdev.iovcnt = 0; 3849 bdev_io->u.bdev.offset_blocks = offset_blocks; 3850 bdev_io->u.bdev.num_blocks = num_blocks; 3851 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3852 3853 bdev_io_submit(bdev_io); 3854 return 0; 3855 } 3856 3857 static void 3858 bdev_reset_dev(struct spdk_io_channel_iter *i, int status) 3859 { 3860 struct spdk_bdev_channel *ch = spdk_io_channel_iter_get_ctx(i); 3861 struct spdk_bdev_io *bdev_io; 3862 3863 bdev_io = TAILQ_FIRST(&ch->queued_resets); 3864 TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link); 3865 bdev_io_submit_reset(bdev_io); 3866 } 3867 3868 static void 3869 bdev_reset_freeze_channel(struct spdk_io_channel_iter *i) 3870 { 3871 struct spdk_io_channel *ch; 3872 struct spdk_bdev_channel *channel; 3873 struct spdk_bdev_mgmt_channel *mgmt_channel; 3874 struct spdk_bdev_shared_resource *shared_resource; 3875 bdev_io_tailq_t tmp_queued; 3876 3877 TAILQ_INIT(&tmp_queued); 3878 3879 ch = spdk_io_channel_iter_get_channel(i); 3880 channel = spdk_io_channel_get_ctx(ch); 3881 shared_resource = channel->shared_resource; 3882 mgmt_channel = shared_resource->mgmt_ch; 3883 3884 channel->flags |= BDEV_CH_RESET_IN_PROGRESS; 3885 3886 if ((channel->flags & BDEV_CH_QOS_ENABLED) != 0) { 3887 /* The QoS object is always valid and readable while 3888 * the channel flag is set, so the lock here should not 3889 * be necessary. We're not in the fast path though, so 3890 * just take it anyway. */ 3891 pthread_mutex_lock(&channel->bdev->internal.mutex); 3892 if (channel->bdev->internal.qos->ch == channel) { 3893 TAILQ_SWAP(&channel->bdev->internal.qos->queued, &tmp_queued, spdk_bdev_io, internal.link); 3894 } 3895 pthread_mutex_unlock(&channel->bdev->internal.mutex); 3896 } 3897 3898 bdev_abort_queued_io(&shared_resource->nomem_io, channel); 3899 bdev_abort_buf_io(&mgmt_channel->need_buf_small, channel); 3900 bdev_abort_buf_io(&mgmt_channel->need_buf_large, channel); 3901 bdev_abort_queued_io(&tmp_queued, channel); 3902 3903 spdk_for_each_channel_continue(i, 0); 3904 } 3905 3906 static void 3907 bdev_start_reset(void *ctx) 3908 { 3909 struct spdk_bdev_channel *ch = ctx; 3910 3911 spdk_for_each_channel(__bdev_to_io_dev(ch->bdev), bdev_reset_freeze_channel, 3912 ch, bdev_reset_dev); 3913 } 3914 3915 static void 3916 bdev_channel_start_reset(struct spdk_bdev_channel *ch) 3917 { 3918 struct spdk_bdev *bdev = ch->bdev; 3919 3920 assert(!TAILQ_EMPTY(&ch->queued_resets)); 3921 3922 pthread_mutex_lock(&bdev->internal.mutex); 3923 if (bdev->internal.reset_in_progress == NULL) { 3924 bdev->internal.reset_in_progress = TAILQ_FIRST(&ch->queued_resets); 3925 /* 3926 * Take a channel reference for the target bdev for the life of this 3927 * reset. This guards against the channel getting destroyed while 3928 * spdk_for_each_channel() calls related to this reset IO are in 3929 * progress. We will release the reference when this reset is 3930 * completed. 3931 */ 3932 bdev->internal.reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev)); 3933 bdev_start_reset(ch); 3934 } 3935 pthread_mutex_unlock(&bdev->internal.mutex); 3936 } 3937 3938 int 3939 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 3940 spdk_bdev_io_completion_cb cb, void *cb_arg) 3941 { 3942 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 3943 struct spdk_bdev_io *bdev_io; 3944 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3945 3946 bdev_io = bdev_channel_get_io(channel); 3947 if (!bdev_io) { 3948 return -ENOMEM; 3949 } 3950 3951 bdev_io->internal.ch = channel; 3952 bdev_io->internal.desc = desc; 3953 bdev_io->internal.submit_tsc = spdk_get_ticks(); 3954 bdev_io->type = SPDK_BDEV_IO_TYPE_RESET; 3955 bdev_io->u.reset.ch_ref = NULL; 3956 bdev_io_init(bdev_io, bdev, cb_arg, cb); 3957 3958 pthread_mutex_lock(&bdev->internal.mutex); 3959 TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, internal.link); 3960 pthread_mutex_unlock(&bdev->internal.mutex); 3961 3962 TAILQ_INSERT_TAIL(&bdev_io->internal.ch->io_submitted, bdev_io, 3963 internal.ch_link); 3964 3965 bdev_channel_start_reset(channel); 3966 3967 return 0; 3968 } 3969 3970 void 3971 spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch, 3972 struct spdk_bdev_io_stat *stat) 3973 { 3974 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3975 3976 *stat = channel->stat; 3977 } 3978 3979 static void 3980 bdev_get_device_stat_done(struct spdk_io_channel_iter *i, int status) 3981 { 3982 void *io_device = spdk_io_channel_iter_get_io_device(i); 3983 struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i); 3984 3985 bdev_iostat_ctx->cb(__bdev_from_io_dev(io_device), bdev_iostat_ctx->stat, 3986 bdev_iostat_ctx->cb_arg, 0); 3987 free(bdev_iostat_ctx); 3988 } 3989 3990 static void 3991 bdev_get_each_channel_stat(struct spdk_io_channel_iter *i) 3992 { 3993 struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i); 3994 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 3995 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 3996 3997 bdev_io_stat_add(bdev_iostat_ctx->stat, &channel->stat); 3998 spdk_for_each_channel_continue(i, 0); 3999 } 4000 4001 void 4002 spdk_bdev_get_device_stat(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat, 4003 spdk_bdev_get_device_stat_cb cb, void *cb_arg) 4004 { 4005 struct spdk_bdev_iostat_ctx *bdev_iostat_ctx; 4006 4007 assert(bdev != NULL); 4008 assert(stat != NULL); 4009 assert(cb != NULL); 4010 4011 bdev_iostat_ctx = calloc(1, sizeof(struct spdk_bdev_iostat_ctx)); 4012 if (bdev_iostat_ctx == NULL) { 4013 SPDK_ERRLOG("Unable to allocate memory for spdk_bdev_iostat_ctx\n"); 4014 cb(bdev, stat, cb_arg, -ENOMEM); 4015 return; 4016 } 4017 4018 bdev_iostat_ctx->stat = stat; 4019 bdev_iostat_ctx->cb = cb; 4020 bdev_iostat_ctx->cb_arg = cb_arg; 4021 4022 /* Start with the statistics from previously deleted channels. */ 4023 pthread_mutex_lock(&bdev->internal.mutex); 4024 bdev_io_stat_add(bdev_iostat_ctx->stat, &bdev->internal.stat); 4025 pthread_mutex_unlock(&bdev->internal.mutex); 4026 4027 /* Then iterate and add the statistics from each existing channel. */ 4028 spdk_for_each_channel(__bdev_to_io_dev(bdev), 4029 bdev_get_each_channel_stat, 4030 bdev_iostat_ctx, 4031 bdev_get_device_stat_done); 4032 } 4033 4034 int 4035 spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4036 const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, 4037 spdk_bdev_io_completion_cb cb, void *cb_arg) 4038 { 4039 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4040 struct spdk_bdev_io *bdev_io; 4041 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4042 4043 if (!desc->write) { 4044 return -EBADF; 4045 } 4046 4047 bdev_io = bdev_channel_get_io(channel); 4048 if (!bdev_io) { 4049 return -ENOMEM; 4050 } 4051 4052 bdev_io->internal.ch = channel; 4053 bdev_io->internal.desc = desc; 4054 bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN; 4055 bdev_io->u.nvme_passthru.cmd = *cmd; 4056 bdev_io->u.nvme_passthru.buf = buf; 4057 bdev_io->u.nvme_passthru.nbytes = nbytes; 4058 bdev_io->u.nvme_passthru.md_buf = NULL; 4059 bdev_io->u.nvme_passthru.md_len = 0; 4060 4061 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4062 4063 bdev_io_submit(bdev_io); 4064 return 0; 4065 } 4066 4067 int 4068 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4069 const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, 4070 spdk_bdev_io_completion_cb cb, void *cb_arg) 4071 { 4072 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4073 struct spdk_bdev_io *bdev_io; 4074 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4075 4076 if (!desc->write) { 4077 /* 4078 * Do not try to parse the NVMe command - we could maybe use bits in the opcode 4079 * to easily determine if the command is a read or write, but for now just 4080 * do not allow io_passthru with a read-only descriptor. 4081 */ 4082 return -EBADF; 4083 } 4084 4085 bdev_io = bdev_channel_get_io(channel); 4086 if (!bdev_io) { 4087 return -ENOMEM; 4088 } 4089 4090 bdev_io->internal.ch = channel; 4091 bdev_io->internal.desc = desc; 4092 bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO; 4093 bdev_io->u.nvme_passthru.cmd = *cmd; 4094 bdev_io->u.nvme_passthru.buf = buf; 4095 bdev_io->u.nvme_passthru.nbytes = nbytes; 4096 bdev_io->u.nvme_passthru.md_buf = NULL; 4097 bdev_io->u.nvme_passthru.md_len = 0; 4098 4099 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4100 4101 bdev_io_submit(bdev_io); 4102 return 0; 4103 } 4104 4105 int 4106 spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, 4107 const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len, 4108 spdk_bdev_io_completion_cb cb, void *cb_arg) 4109 { 4110 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 4111 struct spdk_bdev_io *bdev_io; 4112 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4113 4114 if (!desc->write) { 4115 /* 4116 * Do not try to parse the NVMe command - we could maybe use bits in the opcode 4117 * to easily determine if the command is a read or write, but for now just 4118 * do not allow io_passthru with a read-only descriptor. 4119 */ 4120 return -EBADF; 4121 } 4122 4123 bdev_io = bdev_channel_get_io(channel); 4124 if (!bdev_io) { 4125 return -ENOMEM; 4126 } 4127 4128 bdev_io->internal.ch = channel; 4129 bdev_io->internal.desc = desc; 4130 bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD; 4131 bdev_io->u.nvme_passthru.cmd = *cmd; 4132 bdev_io->u.nvme_passthru.buf = buf; 4133 bdev_io->u.nvme_passthru.nbytes = nbytes; 4134 bdev_io->u.nvme_passthru.md_buf = md_buf; 4135 bdev_io->u.nvme_passthru.md_len = md_len; 4136 4137 bdev_io_init(bdev_io, bdev, cb_arg, cb); 4138 4139 bdev_io_submit(bdev_io); 4140 return 0; 4141 } 4142 4143 int 4144 spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch, 4145 struct spdk_bdev_io_wait_entry *entry) 4146 { 4147 struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch); 4148 struct spdk_bdev_mgmt_channel *mgmt_ch = channel->shared_resource->mgmt_ch; 4149 4150 if (bdev != entry->bdev) { 4151 SPDK_ERRLOG("bdevs do not match\n"); 4152 return -EINVAL; 4153 } 4154 4155 if (mgmt_ch->per_thread_cache_count > 0) { 4156 SPDK_ERRLOG("Cannot queue io_wait if spdk_bdev_io available in per-thread cache\n"); 4157 return -EINVAL; 4158 } 4159 4160 TAILQ_INSERT_TAIL(&mgmt_ch->io_wait_queue, entry, link); 4161 return 0; 4162 } 4163 4164 static void 4165 bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch) 4166 { 4167 struct spdk_bdev *bdev = bdev_ch->bdev; 4168 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 4169 struct spdk_bdev_io *bdev_io; 4170 4171 if (shared_resource->io_outstanding > shared_resource->nomem_threshold) { 4172 /* 4173 * Allow some more I/O to complete before retrying the nomem_io queue. 4174 * Some drivers (such as nvme) cannot immediately take a new I/O in 4175 * the context of a completion, because the resources for the I/O are 4176 * not released until control returns to the bdev poller. Also, we 4177 * may require several small I/O to complete before a larger I/O 4178 * (that requires splitting) can be submitted. 4179 */ 4180 return; 4181 } 4182 4183 while (!TAILQ_EMPTY(&shared_resource->nomem_io)) { 4184 bdev_io = TAILQ_FIRST(&shared_resource->nomem_io); 4185 TAILQ_REMOVE(&shared_resource->nomem_io, bdev_io, internal.link); 4186 bdev_io->internal.ch->io_outstanding++; 4187 shared_resource->io_outstanding++; 4188 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING; 4189 bdev_io->internal.error.nvme.cdw0 = 0; 4190 bdev->fn_table->submit_request(spdk_bdev_io_get_io_channel(bdev_io), bdev_io); 4191 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) { 4192 break; 4193 } 4194 } 4195 } 4196 4197 static inline void 4198 bdev_io_complete(void *ctx) 4199 { 4200 struct spdk_bdev_io *bdev_io = ctx; 4201 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 4202 uint64_t tsc, tsc_diff; 4203 4204 if (spdk_unlikely(bdev_io->internal.in_submit_request || bdev_io->internal.io_submit_ch)) { 4205 /* 4206 * Send the completion to the thread that originally submitted the I/O, 4207 * which may not be the current thread in the case of QoS. 4208 */ 4209 if (bdev_io->internal.io_submit_ch) { 4210 bdev_io->internal.ch = bdev_io->internal.io_submit_ch; 4211 bdev_io->internal.io_submit_ch = NULL; 4212 } 4213 4214 /* 4215 * Defer completion to avoid potential infinite recursion if the 4216 * user's completion callback issues a new I/O. 4217 */ 4218 spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), 4219 bdev_io_complete, bdev_io); 4220 return; 4221 } 4222 4223 tsc = spdk_get_ticks(); 4224 tsc_diff = tsc - bdev_io->internal.submit_tsc; 4225 spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, 0); 4226 /* When a bdev_io is split, the children bdev_io are not added 4227 * to the io_submitted list. So don't try to remove them in that 4228 * case. 4229 */ 4230 if (bdev_io->internal.cb != bdev_io_split_done) { 4231 TAILQ_REMOVE(&bdev_ch->io_submitted, bdev_io, internal.ch_link); 4232 } 4233 4234 if (bdev_io->internal.ch->histogram) { 4235 spdk_histogram_data_tally(bdev_io->internal.ch->histogram, tsc_diff); 4236 } 4237 4238 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 4239 switch (bdev_io->type) { 4240 case SPDK_BDEV_IO_TYPE_READ: 4241 bdev_io->internal.ch->stat.bytes_read += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 4242 bdev_io->internal.ch->stat.num_read_ops++; 4243 bdev_io->internal.ch->stat.read_latency_ticks += tsc_diff; 4244 break; 4245 case SPDK_BDEV_IO_TYPE_WRITE: 4246 bdev_io->internal.ch->stat.bytes_written += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 4247 bdev_io->internal.ch->stat.num_write_ops++; 4248 bdev_io->internal.ch->stat.write_latency_ticks += tsc_diff; 4249 break; 4250 case SPDK_BDEV_IO_TYPE_UNMAP: 4251 bdev_io->internal.ch->stat.bytes_unmapped += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 4252 bdev_io->internal.ch->stat.num_unmap_ops++; 4253 bdev_io->internal.ch->stat.unmap_latency_ticks += tsc_diff; 4254 default: 4255 break; 4256 } 4257 } 4258 4259 #ifdef SPDK_CONFIG_VTUNE 4260 uint64_t now_tsc = spdk_get_ticks(); 4261 if (now_tsc > (bdev_io->internal.ch->start_tsc + bdev_io->internal.ch->interval_tsc)) { 4262 uint64_t data[5]; 4263 4264 data[0] = bdev_io->internal.ch->stat.num_read_ops - bdev_io->internal.ch->prev_stat.num_read_ops; 4265 data[1] = bdev_io->internal.ch->stat.bytes_read - bdev_io->internal.ch->prev_stat.bytes_read; 4266 data[2] = bdev_io->internal.ch->stat.num_write_ops - bdev_io->internal.ch->prev_stat.num_write_ops; 4267 data[3] = bdev_io->internal.ch->stat.bytes_written - bdev_io->internal.ch->prev_stat.bytes_written; 4268 data[4] = bdev_io->bdev->fn_table->get_spin_time ? 4269 bdev_io->bdev->fn_table->get_spin_time(spdk_bdev_io_get_io_channel(bdev_io)) : 0; 4270 4271 __itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->internal.ch->handle, 4272 __itt_metadata_u64, 5, data); 4273 4274 bdev_io->internal.ch->prev_stat = bdev_io->internal.ch->stat; 4275 bdev_io->internal.ch->start_tsc = now_tsc; 4276 } 4277 #endif 4278 4279 assert(bdev_io->internal.cb != NULL); 4280 assert(spdk_get_thread() == spdk_bdev_io_get_thread(bdev_io)); 4281 4282 bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS, 4283 bdev_io->internal.caller_ctx); 4284 } 4285 4286 static void 4287 bdev_reset_complete(struct spdk_io_channel_iter *i, int status) 4288 { 4289 struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i); 4290 4291 if (bdev_io->u.reset.ch_ref != NULL) { 4292 spdk_put_io_channel(bdev_io->u.reset.ch_ref); 4293 bdev_io->u.reset.ch_ref = NULL; 4294 } 4295 4296 bdev_io_complete(bdev_io); 4297 } 4298 4299 static void 4300 bdev_unfreeze_channel(struct spdk_io_channel_iter *i) 4301 { 4302 struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i); 4303 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 4304 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 4305 struct spdk_bdev_io *queued_reset; 4306 4307 ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS; 4308 while (!TAILQ_EMPTY(&ch->queued_resets)) { 4309 queued_reset = TAILQ_FIRST(&ch->queued_resets); 4310 TAILQ_REMOVE(&ch->queued_resets, queued_reset, internal.link); 4311 spdk_bdev_io_complete(queued_reset, bdev_io->internal.status); 4312 } 4313 4314 spdk_for_each_channel_continue(i, 0); 4315 } 4316 4317 void 4318 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status) 4319 { 4320 struct spdk_bdev *bdev = bdev_io->bdev; 4321 struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch; 4322 struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource; 4323 4324 bdev_io->internal.status = status; 4325 4326 if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) { 4327 bool unlock_channels = false; 4328 4329 if (status == SPDK_BDEV_IO_STATUS_NOMEM) { 4330 SPDK_ERRLOG("NOMEM returned for reset\n"); 4331 } 4332 pthread_mutex_lock(&bdev->internal.mutex); 4333 if (bdev_io == bdev->internal.reset_in_progress) { 4334 bdev->internal.reset_in_progress = NULL; 4335 unlock_channels = true; 4336 } 4337 pthread_mutex_unlock(&bdev->internal.mutex); 4338 4339 if (unlock_channels) { 4340 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_unfreeze_channel, 4341 bdev_io, bdev_reset_complete); 4342 return; 4343 } 4344 } else { 4345 _bdev_io_unset_bounce_buf(bdev_io); 4346 4347 assert(bdev_ch->io_outstanding > 0); 4348 assert(shared_resource->io_outstanding > 0); 4349 bdev_ch->io_outstanding--; 4350 shared_resource->io_outstanding--; 4351 4352 if (spdk_unlikely(status == SPDK_BDEV_IO_STATUS_NOMEM)) { 4353 TAILQ_INSERT_HEAD(&shared_resource->nomem_io, bdev_io, internal.link); 4354 /* 4355 * Wait for some of the outstanding I/O to complete before we 4356 * retry any of the nomem_io. Normally we will wait for 4357 * NOMEM_THRESHOLD_COUNT I/O to complete but for low queue 4358 * depth channels we will instead wait for half to complete. 4359 */ 4360 shared_resource->nomem_threshold = spdk_max((int64_t)shared_resource->io_outstanding / 2, 4361 (int64_t)shared_resource->io_outstanding - NOMEM_THRESHOLD_COUNT); 4362 return; 4363 } 4364 4365 if (spdk_unlikely(!TAILQ_EMPTY(&shared_resource->nomem_io))) { 4366 bdev_ch_retry_io(bdev_ch); 4367 } 4368 } 4369 4370 bdev_io_complete(bdev_io); 4371 } 4372 4373 void 4374 spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc, 4375 enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq) 4376 { 4377 if (sc == SPDK_SCSI_STATUS_GOOD) { 4378 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 4379 } else { 4380 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SCSI_ERROR; 4381 bdev_io->internal.error.scsi.sc = sc; 4382 bdev_io->internal.error.scsi.sk = sk; 4383 bdev_io->internal.error.scsi.asc = asc; 4384 bdev_io->internal.error.scsi.ascq = ascq; 4385 } 4386 4387 spdk_bdev_io_complete(bdev_io, bdev_io->internal.status); 4388 } 4389 4390 void 4391 spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io, 4392 int *sc, int *sk, int *asc, int *ascq) 4393 { 4394 assert(sc != NULL); 4395 assert(sk != NULL); 4396 assert(asc != NULL); 4397 assert(ascq != NULL); 4398 4399 switch (bdev_io->internal.status) { 4400 case SPDK_BDEV_IO_STATUS_SUCCESS: 4401 *sc = SPDK_SCSI_STATUS_GOOD; 4402 *sk = SPDK_SCSI_SENSE_NO_SENSE; 4403 *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE; 4404 *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE; 4405 break; 4406 case SPDK_BDEV_IO_STATUS_NVME_ERROR: 4407 spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq); 4408 break; 4409 case SPDK_BDEV_IO_STATUS_SCSI_ERROR: 4410 *sc = bdev_io->internal.error.scsi.sc; 4411 *sk = bdev_io->internal.error.scsi.sk; 4412 *asc = bdev_io->internal.error.scsi.asc; 4413 *ascq = bdev_io->internal.error.scsi.ascq; 4414 break; 4415 default: 4416 *sc = SPDK_SCSI_STATUS_CHECK_CONDITION; 4417 *sk = SPDK_SCSI_SENSE_ABORTED_COMMAND; 4418 *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE; 4419 *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE; 4420 break; 4421 } 4422 } 4423 4424 void 4425 spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, uint32_t cdw0, int sct, int sc) 4426 { 4427 if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS) { 4428 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 4429 } else { 4430 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_NVME_ERROR; 4431 } 4432 4433 bdev_io->internal.error.nvme.cdw0 = cdw0; 4434 bdev_io->internal.error.nvme.sct = sct; 4435 bdev_io->internal.error.nvme.sc = sc; 4436 4437 spdk_bdev_io_complete(bdev_io, bdev_io->internal.status); 4438 } 4439 4440 void 4441 spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0, int *sct, int *sc) 4442 { 4443 assert(sct != NULL); 4444 assert(sc != NULL); 4445 assert(cdw0 != NULL); 4446 4447 if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) { 4448 *sct = bdev_io->internal.error.nvme.sct; 4449 *sc = bdev_io->internal.error.nvme.sc; 4450 } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) { 4451 *sct = SPDK_NVME_SCT_GENERIC; 4452 *sc = SPDK_NVME_SC_SUCCESS; 4453 } else { 4454 *sct = SPDK_NVME_SCT_GENERIC; 4455 *sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 4456 } 4457 4458 *cdw0 = bdev_io->internal.error.nvme.cdw0; 4459 } 4460 4461 struct spdk_thread * 4462 spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io) 4463 { 4464 return spdk_io_channel_get_thread(bdev_io->internal.ch->channel); 4465 } 4466 4467 struct spdk_io_channel * 4468 spdk_bdev_io_get_io_channel(struct spdk_bdev_io *bdev_io) 4469 { 4470 return bdev_io->internal.ch->channel; 4471 } 4472 4473 static void 4474 bdev_qos_config_limit(struct spdk_bdev *bdev, uint64_t *limits) 4475 { 4476 uint64_t min_qos_set; 4477 int i; 4478 4479 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 4480 if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 4481 break; 4482 } 4483 } 4484 4485 if (i == SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) { 4486 SPDK_ERRLOG("Invalid rate limits set.\n"); 4487 return; 4488 } 4489 4490 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 4491 if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 4492 continue; 4493 } 4494 4495 if (bdev_qos_is_iops_rate_limit(i) == true) { 4496 min_qos_set = SPDK_BDEV_QOS_MIN_IOS_PER_SEC; 4497 } else { 4498 min_qos_set = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC; 4499 } 4500 4501 if (limits[i] == 0 || limits[i] % min_qos_set) { 4502 SPDK_ERRLOG("Assigned limit %" PRIu64 " on bdev %s is not multiple of %" PRIu64 "\n", 4503 limits[i], bdev->name, min_qos_set); 4504 SPDK_ERRLOG("Failed to enable QoS on this bdev %s\n", bdev->name); 4505 return; 4506 } 4507 } 4508 4509 if (!bdev->internal.qos) { 4510 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 4511 if (!bdev->internal.qos) { 4512 SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n"); 4513 return; 4514 } 4515 } 4516 4517 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 4518 bdev->internal.qos->rate_limits[i].limit = limits[i]; 4519 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Bdev:%s QoS type:%d set:%lu\n", 4520 bdev->name, i, limits[i]); 4521 } 4522 4523 return; 4524 } 4525 4526 static void 4527 bdev_qos_config(struct spdk_bdev *bdev) 4528 { 4529 struct spdk_conf_section *sp = NULL; 4530 const char *val = NULL; 4531 int i = 0, j = 0; 4532 uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES] = {}; 4533 bool config_qos = false; 4534 4535 sp = spdk_conf_find_section(NULL, "QoS"); 4536 if (!sp) { 4537 return; 4538 } 4539 4540 while (j < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) { 4541 limits[j] = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED; 4542 4543 i = 0; 4544 while (true) { 4545 val = spdk_conf_section_get_nmval(sp, qos_conf_type[j], i, 0); 4546 if (!val) { 4547 break; 4548 } 4549 4550 if (strcmp(bdev->name, val) != 0) { 4551 i++; 4552 continue; 4553 } 4554 4555 val = spdk_conf_section_get_nmval(sp, qos_conf_type[j], i, 1); 4556 if (val) { 4557 if (bdev_qos_is_iops_rate_limit(j) == true) { 4558 limits[j] = strtoull(val, NULL, 10); 4559 } else { 4560 limits[j] = strtoull(val, NULL, 10) * 1024 * 1024; 4561 } 4562 config_qos = true; 4563 } 4564 4565 break; 4566 } 4567 4568 j++; 4569 } 4570 4571 if (config_qos == true) { 4572 bdev_qos_config_limit(bdev, limits); 4573 } 4574 4575 return; 4576 } 4577 4578 static int 4579 bdev_init(struct spdk_bdev *bdev) 4580 { 4581 char *bdev_name; 4582 4583 assert(bdev->module != NULL); 4584 4585 if (!bdev->name) { 4586 SPDK_ERRLOG("Bdev name is NULL\n"); 4587 return -EINVAL; 4588 } 4589 4590 if (!strlen(bdev->name)) { 4591 SPDK_ERRLOG("Bdev name must not be an empty string\n"); 4592 return -EINVAL; 4593 } 4594 4595 if (spdk_bdev_get_by_name(bdev->name)) { 4596 SPDK_ERRLOG("Bdev name:%s already exists\n", bdev->name); 4597 return -EEXIST; 4598 } 4599 4600 /* Users often register their own I/O devices using the bdev name. In 4601 * order to avoid conflicts, prepend bdev_. */ 4602 bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name); 4603 if (!bdev_name) { 4604 SPDK_ERRLOG("Unable to allocate memory for internal bdev name.\n"); 4605 return -ENOMEM; 4606 } 4607 4608 bdev->internal.status = SPDK_BDEV_STATUS_READY; 4609 bdev->internal.measured_queue_depth = UINT64_MAX; 4610 bdev->internal.claim_module = NULL; 4611 bdev->internal.qd_poller = NULL; 4612 bdev->internal.qos = NULL; 4613 4614 /* If the user didn't specify a uuid, generate one. */ 4615 if (spdk_mem_all_zero(&bdev->uuid, sizeof(bdev->uuid))) { 4616 spdk_uuid_generate(&bdev->uuid); 4617 } 4618 4619 if (spdk_bdev_get_buf_align(bdev) > 1) { 4620 if (bdev->split_on_optimal_io_boundary) { 4621 bdev->optimal_io_boundary = spdk_min(bdev->optimal_io_boundary, 4622 SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen); 4623 } else { 4624 bdev->split_on_optimal_io_boundary = true; 4625 bdev->optimal_io_boundary = SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen; 4626 } 4627 } 4628 4629 /* If the user didn't specify a write unit size, set it to one. */ 4630 if (bdev->write_unit_size == 0) { 4631 bdev->write_unit_size = 1; 4632 } 4633 4634 TAILQ_INIT(&bdev->internal.open_descs); 4635 TAILQ_INIT(&bdev->internal.locked_ranges); 4636 4637 TAILQ_INIT(&bdev->aliases); 4638 4639 bdev->internal.reset_in_progress = NULL; 4640 4641 bdev_qos_config(bdev); 4642 4643 spdk_io_device_register(__bdev_to_io_dev(bdev), 4644 bdev_channel_create, bdev_channel_destroy, 4645 sizeof(struct spdk_bdev_channel), 4646 bdev_name); 4647 4648 free(bdev_name); 4649 4650 pthread_mutex_init(&bdev->internal.mutex, NULL); 4651 return 0; 4652 } 4653 4654 static void 4655 bdev_destroy_cb(void *io_device) 4656 { 4657 int rc; 4658 struct spdk_bdev *bdev; 4659 spdk_bdev_unregister_cb cb_fn; 4660 void *cb_arg; 4661 4662 bdev = __bdev_from_io_dev(io_device); 4663 cb_fn = bdev->internal.unregister_cb; 4664 cb_arg = bdev->internal.unregister_ctx; 4665 4666 rc = bdev->fn_table->destruct(bdev->ctxt); 4667 if (rc < 0) { 4668 SPDK_ERRLOG("destruct failed\n"); 4669 } 4670 if (rc <= 0 && cb_fn != NULL) { 4671 cb_fn(cb_arg, rc); 4672 } 4673 } 4674 4675 4676 static void 4677 bdev_fini(struct spdk_bdev *bdev) 4678 { 4679 pthread_mutex_destroy(&bdev->internal.mutex); 4680 4681 free(bdev->internal.qos); 4682 4683 spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb); 4684 } 4685 4686 static void 4687 bdev_start(struct spdk_bdev *bdev) 4688 { 4689 struct spdk_bdev_module *module; 4690 uint32_t action; 4691 4692 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Inserting bdev %s into list\n", bdev->name); 4693 TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, internal.link); 4694 4695 /* Examine configuration before initializing I/O */ 4696 TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) { 4697 if (module->examine_config) { 4698 action = module->internal.action_in_progress; 4699 module->internal.action_in_progress++; 4700 module->examine_config(bdev); 4701 if (action != module->internal.action_in_progress) { 4702 SPDK_ERRLOG("examine_config for module %s did not call spdk_bdev_module_examine_done()\n", 4703 module->name); 4704 } 4705 } 4706 } 4707 4708 if (bdev->internal.claim_module) { 4709 if (bdev->internal.claim_module->examine_disk) { 4710 bdev->internal.claim_module->internal.action_in_progress++; 4711 bdev->internal.claim_module->examine_disk(bdev); 4712 } 4713 return; 4714 } 4715 4716 TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) { 4717 if (module->examine_disk) { 4718 module->internal.action_in_progress++; 4719 module->examine_disk(bdev); 4720 } 4721 } 4722 } 4723 4724 int 4725 spdk_bdev_register(struct spdk_bdev *bdev) 4726 { 4727 int rc = bdev_init(bdev); 4728 4729 if (rc == 0) { 4730 bdev_start(bdev); 4731 } 4732 4733 spdk_notify_send("bdev_register", spdk_bdev_get_name(bdev)); 4734 return rc; 4735 } 4736 4737 int 4738 spdk_vbdev_register(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, int base_bdev_count) 4739 { 4740 SPDK_ERRLOG("This function is deprecated. Use spdk_bdev_register() instead.\n"); 4741 return spdk_bdev_register(vbdev); 4742 } 4743 4744 void 4745 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno) 4746 { 4747 if (bdev->internal.unregister_cb != NULL) { 4748 bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno); 4749 } 4750 } 4751 4752 static void 4753 _remove_notify(void *arg) 4754 { 4755 struct spdk_bdev_desc *desc = arg; 4756 4757 pthread_mutex_lock(&desc->mutex); 4758 desc->refs--; 4759 4760 if (!desc->closed) { 4761 pthread_mutex_unlock(&desc->mutex); 4762 if (desc->callback.open_with_ext) { 4763 desc->callback.event_fn(SPDK_BDEV_EVENT_REMOVE, desc->bdev, desc->callback.ctx); 4764 } else { 4765 desc->callback.remove_fn(desc->callback.ctx); 4766 } 4767 return; 4768 } else if (0 == desc->refs) { 4769 /* This descriptor was closed after this remove_notify message was sent. 4770 * spdk_bdev_close() could not free the descriptor since this message was 4771 * in flight, so we free it now using bdev_desc_free(). 4772 */ 4773 pthread_mutex_unlock(&desc->mutex); 4774 bdev_desc_free(desc); 4775 return; 4776 } 4777 pthread_mutex_unlock(&desc->mutex); 4778 } 4779 4780 /* Must be called while holding bdev->internal.mutex. 4781 * returns: 0 - bdev removed and ready to be destructed. 4782 * -EBUSY - bdev can't be destructed yet. */ 4783 static int 4784 bdev_unregister_unsafe(struct spdk_bdev *bdev) 4785 { 4786 struct spdk_bdev_desc *desc, *tmp; 4787 int rc = 0; 4788 4789 /* Notify each descriptor about hotremoval */ 4790 TAILQ_FOREACH_SAFE(desc, &bdev->internal.open_descs, link, tmp) { 4791 rc = -EBUSY; 4792 pthread_mutex_lock(&desc->mutex); 4793 /* 4794 * Defer invocation of the event_cb to a separate message that will 4795 * run later on its thread. This ensures this context unwinds and 4796 * we don't recursively unregister this bdev again if the event_cb 4797 * immediately closes its descriptor. 4798 */ 4799 desc->refs++; 4800 spdk_thread_send_msg(desc->thread, _remove_notify, desc); 4801 pthread_mutex_unlock(&desc->mutex); 4802 } 4803 4804 /* If there are no descriptors, proceed removing the bdev */ 4805 if (rc == 0) { 4806 TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link); 4807 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Removing bdev %s from list done\n", bdev->name); 4808 spdk_notify_send("bdev_unregister", spdk_bdev_get_name(bdev)); 4809 } 4810 4811 return rc; 4812 } 4813 4814 void 4815 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg) 4816 { 4817 struct spdk_thread *thread; 4818 int rc; 4819 4820 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Removing bdev %s from list\n", bdev->name); 4821 4822 thread = spdk_get_thread(); 4823 if (!thread) { 4824 /* The user called this from a non-SPDK thread. */ 4825 if (cb_fn != NULL) { 4826 cb_fn(cb_arg, -ENOTSUP); 4827 } 4828 return; 4829 } 4830 4831 pthread_mutex_lock(&g_bdev_mgr.mutex); 4832 pthread_mutex_lock(&bdev->internal.mutex); 4833 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) { 4834 pthread_mutex_unlock(&bdev->internal.mutex); 4835 pthread_mutex_unlock(&g_bdev_mgr.mutex); 4836 if (cb_fn) { 4837 cb_fn(cb_arg, -EBUSY); 4838 } 4839 return; 4840 } 4841 4842 bdev->internal.status = SPDK_BDEV_STATUS_REMOVING; 4843 bdev->internal.unregister_cb = cb_fn; 4844 bdev->internal.unregister_ctx = cb_arg; 4845 4846 /* Call under lock. */ 4847 rc = bdev_unregister_unsafe(bdev); 4848 pthread_mutex_unlock(&bdev->internal.mutex); 4849 pthread_mutex_unlock(&g_bdev_mgr.mutex); 4850 4851 if (rc == 0) { 4852 bdev_fini(bdev); 4853 } 4854 } 4855 4856 static void 4857 bdev_dummy_event_cb(void *remove_ctx) 4858 { 4859 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Bdev remove event received with no remove callback specified"); 4860 } 4861 4862 static int 4863 bdev_open(struct spdk_bdev *bdev, bool write, struct spdk_bdev_desc *desc) 4864 { 4865 struct spdk_thread *thread; 4866 struct set_qos_limit_ctx *ctx; 4867 4868 thread = spdk_get_thread(); 4869 if (!thread) { 4870 SPDK_ERRLOG("Cannot open bdev from non-SPDK thread.\n"); 4871 return -ENOTSUP; 4872 } 4873 4874 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Opening descriptor %p for bdev %s on thread %p\n", desc, bdev->name, 4875 spdk_get_thread()); 4876 4877 desc->bdev = bdev; 4878 desc->thread = thread; 4879 desc->write = write; 4880 4881 pthread_mutex_lock(&bdev->internal.mutex); 4882 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) { 4883 pthread_mutex_unlock(&bdev->internal.mutex); 4884 return -ENODEV; 4885 } 4886 4887 if (write && bdev->internal.claim_module) { 4888 SPDK_ERRLOG("Could not open %s - %s module already claimed it\n", 4889 bdev->name, bdev->internal.claim_module->name); 4890 pthread_mutex_unlock(&bdev->internal.mutex); 4891 return -EPERM; 4892 } 4893 4894 /* Enable QoS */ 4895 if (bdev->internal.qos && bdev->internal.qos->thread == NULL) { 4896 ctx = calloc(1, sizeof(*ctx)); 4897 if (ctx == NULL) { 4898 SPDK_ERRLOG("Failed to allocate memory for QoS context\n"); 4899 pthread_mutex_unlock(&bdev->internal.mutex); 4900 return -ENOMEM; 4901 } 4902 ctx->bdev = bdev; 4903 spdk_for_each_channel(__bdev_to_io_dev(bdev), 4904 bdev_enable_qos_msg, ctx, 4905 bdev_enable_qos_done); 4906 } 4907 4908 TAILQ_INSERT_TAIL(&bdev->internal.open_descs, desc, link); 4909 4910 pthread_mutex_unlock(&bdev->internal.mutex); 4911 4912 return 0; 4913 } 4914 4915 int 4916 spdk_bdev_open(struct spdk_bdev *bdev, bool write, spdk_bdev_remove_cb_t remove_cb, 4917 void *remove_ctx, struct spdk_bdev_desc **_desc) 4918 { 4919 struct spdk_bdev_desc *desc; 4920 int rc; 4921 4922 desc = calloc(1, sizeof(*desc)); 4923 if (desc == NULL) { 4924 SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n"); 4925 return -ENOMEM; 4926 } 4927 4928 if (remove_cb == NULL) { 4929 remove_cb = bdev_dummy_event_cb; 4930 } 4931 4932 TAILQ_INIT(&desc->pending_media_events); 4933 TAILQ_INIT(&desc->free_media_events); 4934 4935 desc->callback.open_with_ext = false; 4936 desc->callback.remove_fn = remove_cb; 4937 desc->callback.ctx = remove_ctx; 4938 pthread_mutex_init(&desc->mutex, NULL); 4939 4940 pthread_mutex_lock(&g_bdev_mgr.mutex); 4941 4942 rc = bdev_open(bdev, write, desc); 4943 if (rc != 0) { 4944 bdev_desc_free(desc); 4945 desc = NULL; 4946 } 4947 4948 *_desc = desc; 4949 4950 pthread_mutex_unlock(&g_bdev_mgr.mutex); 4951 4952 return rc; 4953 } 4954 4955 int 4956 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb, 4957 void *event_ctx, struct spdk_bdev_desc **_desc) 4958 { 4959 struct spdk_bdev_desc *desc; 4960 struct spdk_bdev *bdev; 4961 unsigned int event_id; 4962 int rc; 4963 4964 if (event_cb == NULL) { 4965 SPDK_ERRLOG("Missing event callback function\n"); 4966 return -EINVAL; 4967 } 4968 4969 pthread_mutex_lock(&g_bdev_mgr.mutex); 4970 4971 bdev = spdk_bdev_get_by_name(bdev_name); 4972 4973 if (bdev == NULL) { 4974 SPDK_ERRLOG("Failed to find bdev with name: %s\n", bdev_name); 4975 pthread_mutex_unlock(&g_bdev_mgr.mutex); 4976 return -EINVAL; 4977 } 4978 4979 desc = calloc(1, sizeof(*desc)); 4980 if (desc == NULL) { 4981 SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n"); 4982 pthread_mutex_unlock(&g_bdev_mgr.mutex); 4983 return -ENOMEM; 4984 } 4985 4986 TAILQ_INIT(&desc->pending_media_events); 4987 TAILQ_INIT(&desc->free_media_events); 4988 4989 desc->callback.open_with_ext = true; 4990 desc->callback.event_fn = event_cb; 4991 desc->callback.ctx = event_ctx; 4992 pthread_mutex_init(&desc->mutex, NULL); 4993 4994 if (bdev->media_events) { 4995 desc->media_events_buffer = calloc(MEDIA_EVENT_POOL_SIZE, 4996 sizeof(*desc->media_events_buffer)); 4997 if (desc->media_events_buffer == NULL) { 4998 SPDK_ERRLOG("Failed to initialize media event pool\n"); 4999 bdev_desc_free(desc); 5000 pthread_mutex_unlock(&g_bdev_mgr.mutex); 5001 return -ENOMEM; 5002 } 5003 5004 for (event_id = 0; event_id < MEDIA_EVENT_POOL_SIZE; ++event_id) { 5005 TAILQ_INSERT_TAIL(&desc->free_media_events, 5006 &desc->media_events_buffer[event_id], tailq); 5007 } 5008 } 5009 5010 rc = bdev_open(bdev, write, desc); 5011 if (rc != 0) { 5012 bdev_desc_free(desc); 5013 desc = NULL; 5014 } 5015 5016 *_desc = desc; 5017 5018 pthread_mutex_unlock(&g_bdev_mgr.mutex); 5019 5020 return rc; 5021 } 5022 5023 void 5024 spdk_bdev_close(struct spdk_bdev_desc *desc) 5025 { 5026 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5027 int rc; 5028 5029 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Closing descriptor %p for bdev %s on thread %p\n", desc, bdev->name, 5030 spdk_get_thread()); 5031 5032 assert(desc->thread == spdk_get_thread()); 5033 5034 spdk_poller_unregister(&desc->io_timeout_poller); 5035 5036 pthread_mutex_lock(&bdev->internal.mutex); 5037 pthread_mutex_lock(&desc->mutex); 5038 5039 TAILQ_REMOVE(&bdev->internal.open_descs, desc, link); 5040 5041 desc->closed = true; 5042 5043 if (0 == desc->refs) { 5044 pthread_mutex_unlock(&desc->mutex); 5045 bdev_desc_free(desc); 5046 } else { 5047 pthread_mutex_unlock(&desc->mutex); 5048 } 5049 5050 /* If no more descriptors, kill QoS channel */ 5051 if (bdev->internal.qos && TAILQ_EMPTY(&bdev->internal.open_descs)) { 5052 SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Closed last descriptor for bdev %s on thread %p. Stopping QoS.\n", 5053 bdev->name, spdk_get_thread()); 5054 5055 if (bdev_qos_destroy(bdev)) { 5056 /* There isn't anything we can do to recover here. Just let the 5057 * old QoS poller keep running. The QoS handling won't change 5058 * cores when the user allocates a new channel, but it won't break. */ 5059 SPDK_ERRLOG("Unable to shut down QoS poller. It will continue running on the current thread.\n"); 5060 } 5061 } 5062 5063 spdk_bdev_set_qd_sampling_period(bdev, 0); 5064 5065 if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->internal.open_descs)) { 5066 rc = bdev_unregister_unsafe(bdev); 5067 pthread_mutex_unlock(&bdev->internal.mutex); 5068 5069 if (rc == 0) { 5070 bdev_fini(bdev); 5071 } 5072 } else { 5073 pthread_mutex_unlock(&bdev->internal.mutex); 5074 } 5075 } 5076 5077 int 5078 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc, 5079 struct spdk_bdev_module *module) 5080 { 5081 if (bdev->internal.claim_module != NULL) { 5082 SPDK_ERRLOG("bdev %s already claimed by module %s\n", bdev->name, 5083 bdev->internal.claim_module->name); 5084 return -EPERM; 5085 } 5086 5087 if (desc && !desc->write) { 5088 desc->write = true; 5089 } 5090 5091 bdev->internal.claim_module = module; 5092 return 0; 5093 } 5094 5095 void 5096 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev) 5097 { 5098 assert(bdev->internal.claim_module != NULL); 5099 bdev->internal.claim_module = NULL; 5100 } 5101 5102 struct spdk_bdev * 5103 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc) 5104 { 5105 assert(desc != NULL); 5106 return desc->bdev; 5107 } 5108 5109 void 5110 spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp) 5111 { 5112 struct iovec *iovs; 5113 int iovcnt; 5114 5115 if (bdev_io == NULL) { 5116 return; 5117 } 5118 5119 switch (bdev_io->type) { 5120 case SPDK_BDEV_IO_TYPE_READ: 5121 case SPDK_BDEV_IO_TYPE_WRITE: 5122 case SPDK_BDEV_IO_TYPE_ZCOPY: 5123 iovs = bdev_io->u.bdev.iovs; 5124 iovcnt = bdev_io->u.bdev.iovcnt; 5125 break; 5126 default: 5127 iovs = NULL; 5128 iovcnt = 0; 5129 break; 5130 } 5131 5132 if (iovp) { 5133 *iovp = iovs; 5134 } 5135 if (iovcntp) { 5136 *iovcntp = iovcnt; 5137 } 5138 } 5139 5140 void * 5141 spdk_bdev_io_get_md_buf(struct spdk_bdev_io *bdev_io) 5142 { 5143 if (bdev_io == NULL) { 5144 return NULL; 5145 } 5146 5147 if (!spdk_bdev_is_md_separate(bdev_io->bdev)) { 5148 return NULL; 5149 } 5150 5151 if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ || 5152 bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) { 5153 return bdev_io->u.bdev.md_buf; 5154 } 5155 5156 return NULL; 5157 } 5158 5159 void 5160 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module) 5161 { 5162 5163 if (spdk_bdev_module_list_find(bdev_module->name)) { 5164 SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name); 5165 assert(false); 5166 } 5167 5168 /* 5169 * Modules with examine callbacks must be initialized first, so they are 5170 * ready to handle examine callbacks from later modules that will 5171 * register physical bdevs. 5172 */ 5173 if (bdev_module->examine_config != NULL || bdev_module->examine_disk != NULL) { 5174 TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq); 5175 } else { 5176 TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq); 5177 } 5178 } 5179 5180 struct spdk_bdev_module * 5181 spdk_bdev_module_list_find(const char *name) 5182 { 5183 struct spdk_bdev_module *bdev_module; 5184 5185 TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) { 5186 if (strcmp(name, bdev_module->name) == 0) { 5187 break; 5188 } 5189 } 5190 5191 return bdev_module; 5192 } 5193 5194 static void 5195 bdev_write_zero_buffer_next(void *_bdev_io) 5196 { 5197 struct spdk_bdev_io *bdev_io = _bdev_io; 5198 uint64_t num_bytes, num_blocks; 5199 void *md_buf = NULL; 5200 int rc; 5201 5202 num_bytes = spdk_min(_bdev_get_block_size_with_md(bdev_io->bdev) * 5203 bdev_io->u.bdev.split_remaining_num_blocks, 5204 ZERO_BUFFER_SIZE); 5205 num_blocks = num_bytes / _bdev_get_block_size_with_md(bdev_io->bdev); 5206 5207 if (spdk_bdev_is_md_separate(bdev_io->bdev)) { 5208 md_buf = (char *)g_bdev_mgr.zero_buffer + 5209 spdk_bdev_get_block_size(bdev_io->bdev) * num_blocks; 5210 } 5211 5212 rc = bdev_write_blocks_with_md(bdev_io->internal.desc, 5213 spdk_io_channel_from_ctx(bdev_io->internal.ch), 5214 g_bdev_mgr.zero_buffer, md_buf, 5215 bdev_io->u.bdev.split_current_offset_blocks, num_blocks, 5216 bdev_write_zero_buffer_done, bdev_io); 5217 if (rc == 0) { 5218 bdev_io->u.bdev.split_remaining_num_blocks -= num_blocks; 5219 bdev_io->u.bdev.split_current_offset_blocks += num_blocks; 5220 } else if (rc == -ENOMEM) { 5221 bdev_queue_io_wait_with_cb(bdev_io, bdev_write_zero_buffer_next); 5222 } else { 5223 bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 5224 bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx); 5225 } 5226 } 5227 5228 static void 5229 bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 5230 { 5231 struct spdk_bdev_io *parent_io = cb_arg; 5232 5233 spdk_bdev_free_io(bdev_io); 5234 5235 if (!success) { 5236 parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED; 5237 parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx); 5238 return; 5239 } 5240 5241 if (parent_io->u.bdev.split_remaining_num_blocks == 0) { 5242 parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS; 5243 parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx); 5244 return; 5245 } 5246 5247 bdev_write_zero_buffer_next(parent_io); 5248 } 5249 5250 static void 5251 bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status) 5252 { 5253 pthread_mutex_lock(&ctx->bdev->internal.mutex); 5254 ctx->bdev->internal.qos_mod_in_progress = false; 5255 pthread_mutex_unlock(&ctx->bdev->internal.mutex); 5256 5257 if (ctx->cb_fn) { 5258 ctx->cb_fn(ctx->cb_arg, status); 5259 } 5260 free(ctx); 5261 } 5262 5263 static void 5264 bdev_disable_qos_done(void *cb_arg) 5265 { 5266 struct set_qos_limit_ctx *ctx = cb_arg; 5267 struct spdk_bdev *bdev = ctx->bdev; 5268 struct spdk_bdev_io *bdev_io; 5269 struct spdk_bdev_qos *qos; 5270 5271 pthread_mutex_lock(&bdev->internal.mutex); 5272 qos = bdev->internal.qos; 5273 bdev->internal.qos = NULL; 5274 pthread_mutex_unlock(&bdev->internal.mutex); 5275 5276 while (!TAILQ_EMPTY(&qos->queued)) { 5277 /* Send queued I/O back to their original thread for resubmission. */ 5278 bdev_io = TAILQ_FIRST(&qos->queued); 5279 TAILQ_REMOVE(&qos->queued, bdev_io, internal.link); 5280 5281 if (bdev_io->internal.io_submit_ch) { 5282 /* 5283 * Channel was changed when sending it to the QoS thread - change it back 5284 * before sending it back to the original thread. 5285 */ 5286 bdev_io->internal.ch = bdev_io->internal.io_submit_ch; 5287 bdev_io->internal.io_submit_ch = NULL; 5288 } 5289 5290 spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io), 5291 _bdev_io_submit, bdev_io); 5292 } 5293 5294 if (qos->thread != NULL) { 5295 spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch)); 5296 spdk_poller_unregister(&qos->poller); 5297 } 5298 5299 free(qos); 5300 5301 bdev_set_qos_limit_done(ctx, 0); 5302 } 5303 5304 static void 5305 bdev_disable_qos_msg_done(struct spdk_io_channel_iter *i, int status) 5306 { 5307 void *io_device = spdk_io_channel_iter_get_io_device(i); 5308 struct spdk_bdev *bdev = __bdev_from_io_dev(io_device); 5309 struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5310 struct spdk_thread *thread; 5311 5312 pthread_mutex_lock(&bdev->internal.mutex); 5313 thread = bdev->internal.qos->thread; 5314 pthread_mutex_unlock(&bdev->internal.mutex); 5315 5316 if (thread != NULL) { 5317 spdk_thread_send_msg(thread, bdev_disable_qos_done, ctx); 5318 } else { 5319 bdev_disable_qos_done(ctx); 5320 } 5321 } 5322 5323 static void 5324 bdev_disable_qos_msg(struct spdk_io_channel_iter *i) 5325 { 5326 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 5327 struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch); 5328 5329 bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED; 5330 5331 spdk_for_each_channel_continue(i, 0); 5332 } 5333 5334 static void 5335 bdev_update_qos_rate_limit_msg(void *cb_arg) 5336 { 5337 struct set_qos_limit_ctx *ctx = cb_arg; 5338 struct spdk_bdev *bdev = ctx->bdev; 5339 5340 pthread_mutex_lock(&bdev->internal.mutex); 5341 bdev_qos_update_max_quota_per_timeslice(bdev->internal.qos); 5342 pthread_mutex_unlock(&bdev->internal.mutex); 5343 5344 bdev_set_qos_limit_done(ctx, 0); 5345 } 5346 5347 static void 5348 bdev_enable_qos_msg(struct spdk_io_channel_iter *i) 5349 { 5350 void *io_device = spdk_io_channel_iter_get_io_device(i); 5351 struct spdk_bdev *bdev = __bdev_from_io_dev(io_device); 5352 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 5353 struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch); 5354 5355 pthread_mutex_lock(&bdev->internal.mutex); 5356 bdev_enable_qos(bdev, bdev_ch); 5357 pthread_mutex_unlock(&bdev->internal.mutex); 5358 spdk_for_each_channel_continue(i, 0); 5359 } 5360 5361 static void 5362 bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status) 5363 { 5364 struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5365 5366 bdev_set_qos_limit_done(ctx, status); 5367 } 5368 5369 static void 5370 bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits) 5371 { 5372 int i; 5373 5374 assert(bdev->internal.qos != NULL); 5375 5376 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 5377 if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 5378 bdev->internal.qos->rate_limits[i].limit = limits[i]; 5379 5380 if (limits[i] == 0) { 5381 bdev->internal.qos->rate_limits[i].limit = 5382 SPDK_BDEV_QOS_LIMIT_NOT_DEFINED; 5383 } 5384 } 5385 } 5386 } 5387 5388 void 5389 spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits, 5390 void (*cb_fn)(void *cb_arg, int status), void *cb_arg) 5391 { 5392 struct set_qos_limit_ctx *ctx; 5393 uint32_t limit_set_complement; 5394 uint64_t min_limit_per_sec; 5395 int i; 5396 bool disable_rate_limit = true; 5397 5398 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 5399 if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) { 5400 continue; 5401 } 5402 5403 if (limits[i] > 0) { 5404 disable_rate_limit = false; 5405 } 5406 5407 if (bdev_qos_is_iops_rate_limit(i) == true) { 5408 min_limit_per_sec = SPDK_BDEV_QOS_MIN_IOS_PER_SEC; 5409 } else { 5410 /* Change from megabyte to byte rate limit */ 5411 limits[i] = limits[i] * 1024 * 1024; 5412 min_limit_per_sec = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC; 5413 } 5414 5415 limit_set_complement = limits[i] % min_limit_per_sec; 5416 if (limit_set_complement) { 5417 SPDK_ERRLOG("Requested rate limit %" PRIu64 " is not a multiple of %" PRIu64 "\n", 5418 limits[i], min_limit_per_sec); 5419 limits[i] += min_limit_per_sec - limit_set_complement; 5420 SPDK_ERRLOG("Round up the rate limit to %" PRIu64 "\n", limits[i]); 5421 } 5422 } 5423 5424 ctx = calloc(1, sizeof(*ctx)); 5425 if (ctx == NULL) { 5426 cb_fn(cb_arg, -ENOMEM); 5427 return; 5428 } 5429 5430 ctx->cb_fn = cb_fn; 5431 ctx->cb_arg = cb_arg; 5432 ctx->bdev = bdev; 5433 5434 pthread_mutex_lock(&bdev->internal.mutex); 5435 if (bdev->internal.qos_mod_in_progress) { 5436 pthread_mutex_unlock(&bdev->internal.mutex); 5437 free(ctx); 5438 cb_fn(cb_arg, -EAGAIN); 5439 return; 5440 } 5441 bdev->internal.qos_mod_in_progress = true; 5442 5443 if (disable_rate_limit == true && bdev->internal.qos) { 5444 for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) { 5445 if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED && 5446 (bdev->internal.qos->rate_limits[i].limit > 0 && 5447 bdev->internal.qos->rate_limits[i].limit != 5448 SPDK_BDEV_QOS_LIMIT_NOT_DEFINED)) { 5449 disable_rate_limit = false; 5450 break; 5451 } 5452 } 5453 } 5454 5455 if (disable_rate_limit == false) { 5456 if (bdev->internal.qos == NULL) { 5457 bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos)); 5458 if (!bdev->internal.qos) { 5459 pthread_mutex_unlock(&bdev->internal.mutex); 5460 SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n"); 5461 bdev_set_qos_limit_done(ctx, -ENOMEM); 5462 return; 5463 } 5464 } 5465 5466 if (bdev->internal.qos->thread == NULL) { 5467 /* Enabling */ 5468 bdev_set_qos_rate_limits(bdev, limits); 5469 5470 spdk_for_each_channel(__bdev_to_io_dev(bdev), 5471 bdev_enable_qos_msg, ctx, 5472 bdev_enable_qos_done); 5473 } else { 5474 /* Updating */ 5475 bdev_set_qos_rate_limits(bdev, limits); 5476 5477 spdk_thread_send_msg(bdev->internal.qos->thread, 5478 bdev_update_qos_rate_limit_msg, ctx); 5479 } 5480 } else { 5481 if (bdev->internal.qos != NULL) { 5482 bdev_set_qos_rate_limits(bdev, limits); 5483 5484 /* Disabling */ 5485 spdk_for_each_channel(__bdev_to_io_dev(bdev), 5486 bdev_disable_qos_msg, ctx, 5487 bdev_disable_qos_msg_done); 5488 } else { 5489 pthread_mutex_unlock(&bdev->internal.mutex); 5490 bdev_set_qos_limit_done(ctx, 0); 5491 return; 5492 } 5493 } 5494 5495 pthread_mutex_unlock(&bdev->internal.mutex); 5496 } 5497 5498 struct spdk_bdev_histogram_ctx { 5499 spdk_bdev_histogram_status_cb cb_fn; 5500 void *cb_arg; 5501 struct spdk_bdev *bdev; 5502 int status; 5503 }; 5504 5505 static void 5506 bdev_histogram_disable_channel_cb(struct spdk_io_channel_iter *i, int status) 5507 { 5508 struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5509 5510 pthread_mutex_lock(&ctx->bdev->internal.mutex); 5511 ctx->bdev->internal.histogram_in_progress = false; 5512 pthread_mutex_unlock(&ctx->bdev->internal.mutex); 5513 ctx->cb_fn(ctx->cb_arg, ctx->status); 5514 free(ctx); 5515 } 5516 5517 static void 5518 bdev_histogram_disable_channel(struct spdk_io_channel_iter *i) 5519 { 5520 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5521 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5522 5523 if (ch->histogram != NULL) { 5524 spdk_histogram_data_free(ch->histogram); 5525 ch->histogram = NULL; 5526 } 5527 spdk_for_each_channel_continue(i, 0); 5528 } 5529 5530 static void 5531 bdev_histogram_enable_channel_cb(struct spdk_io_channel_iter *i, int status) 5532 { 5533 struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5534 5535 if (status != 0) { 5536 ctx->status = status; 5537 ctx->bdev->internal.histogram_enabled = false; 5538 spdk_for_each_channel(__bdev_to_io_dev(ctx->bdev), bdev_histogram_disable_channel, ctx, 5539 bdev_histogram_disable_channel_cb); 5540 } else { 5541 pthread_mutex_lock(&ctx->bdev->internal.mutex); 5542 ctx->bdev->internal.histogram_in_progress = false; 5543 pthread_mutex_unlock(&ctx->bdev->internal.mutex); 5544 ctx->cb_fn(ctx->cb_arg, ctx->status); 5545 free(ctx); 5546 } 5547 } 5548 5549 static void 5550 bdev_histogram_enable_channel(struct spdk_io_channel_iter *i) 5551 { 5552 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5553 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5554 int status = 0; 5555 5556 if (ch->histogram == NULL) { 5557 ch->histogram = spdk_histogram_data_alloc(); 5558 if (ch->histogram == NULL) { 5559 status = -ENOMEM; 5560 } 5561 } 5562 5563 spdk_for_each_channel_continue(i, status); 5564 } 5565 5566 void 5567 spdk_bdev_histogram_enable(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn, 5568 void *cb_arg, bool enable) 5569 { 5570 struct spdk_bdev_histogram_ctx *ctx; 5571 5572 ctx = calloc(1, sizeof(struct spdk_bdev_histogram_ctx)); 5573 if (ctx == NULL) { 5574 cb_fn(cb_arg, -ENOMEM); 5575 return; 5576 } 5577 5578 ctx->bdev = bdev; 5579 ctx->status = 0; 5580 ctx->cb_fn = cb_fn; 5581 ctx->cb_arg = cb_arg; 5582 5583 pthread_mutex_lock(&bdev->internal.mutex); 5584 if (bdev->internal.histogram_in_progress) { 5585 pthread_mutex_unlock(&bdev->internal.mutex); 5586 free(ctx); 5587 cb_fn(cb_arg, -EAGAIN); 5588 return; 5589 } 5590 5591 bdev->internal.histogram_in_progress = true; 5592 pthread_mutex_unlock(&bdev->internal.mutex); 5593 5594 bdev->internal.histogram_enabled = enable; 5595 5596 if (enable) { 5597 /* Allocate histogram for each channel */ 5598 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_enable_channel, ctx, 5599 bdev_histogram_enable_channel_cb); 5600 } else { 5601 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_disable_channel, ctx, 5602 bdev_histogram_disable_channel_cb); 5603 } 5604 } 5605 5606 struct spdk_bdev_histogram_data_ctx { 5607 spdk_bdev_histogram_data_cb cb_fn; 5608 void *cb_arg; 5609 struct spdk_bdev *bdev; 5610 /** merged histogram data from all channels */ 5611 struct spdk_histogram_data *histogram; 5612 }; 5613 5614 static void 5615 bdev_histogram_get_channel_cb(struct spdk_io_channel_iter *i, int status) 5616 { 5617 struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5618 5619 ctx->cb_fn(ctx->cb_arg, status, ctx->histogram); 5620 free(ctx); 5621 } 5622 5623 static void 5624 bdev_histogram_get_channel(struct spdk_io_channel_iter *i) 5625 { 5626 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5627 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5628 struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5629 int status = 0; 5630 5631 if (ch->histogram == NULL) { 5632 status = -EFAULT; 5633 } else { 5634 spdk_histogram_data_merge(ctx->histogram, ch->histogram); 5635 } 5636 5637 spdk_for_each_channel_continue(i, status); 5638 } 5639 5640 void 5641 spdk_bdev_histogram_get(struct spdk_bdev *bdev, struct spdk_histogram_data *histogram, 5642 spdk_bdev_histogram_data_cb cb_fn, 5643 void *cb_arg) 5644 { 5645 struct spdk_bdev_histogram_data_ctx *ctx; 5646 5647 ctx = calloc(1, sizeof(struct spdk_bdev_histogram_data_ctx)); 5648 if (ctx == NULL) { 5649 cb_fn(cb_arg, -ENOMEM, NULL); 5650 return; 5651 } 5652 5653 ctx->bdev = bdev; 5654 ctx->cb_fn = cb_fn; 5655 ctx->cb_arg = cb_arg; 5656 5657 ctx->histogram = histogram; 5658 5659 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_get_channel, ctx, 5660 bdev_histogram_get_channel_cb); 5661 } 5662 5663 size_t 5664 spdk_bdev_get_media_events(struct spdk_bdev_desc *desc, struct spdk_bdev_media_event *events, 5665 size_t max_events) 5666 { 5667 struct media_event_entry *entry; 5668 size_t num_events = 0; 5669 5670 for (; num_events < max_events; ++num_events) { 5671 entry = TAILQ_FIRST(&desc->pending_media_events); 5672 if (entry == NULL) { 5673 break; 5674 } 5675 5676 events[num_events] = entry->event; 5677 TAILQ_REMOVE(&desc->pending_media_events, entry, tailq); 5678 TAILQ_INSERT_TAIL(&desc->free_media_events, entry, tailq); 5679 } 5680 5681 return num_events; 5682 } 5683 5684 int 5685 spdk_bdev_push_media_events(struct spdk_bdev *bdev, const struct spdk_bdev_media_event *events, 5686 size_t num_events) 5687 { 5688 struct spdk_bdev_desc *desc; 5689 struct media_event_entry *entry; 5690 size_t event_id; 5691 int rc = 0; 5692 5693 assert(bdev->media_events); 5694 5695 pthread_mutex_lock(&bdev->internal.mutex); 5696 TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) { 5697 if (desc->write) { 5698 break; 5699 } 5700 } 5701 5702 if (desc == NULL || desc->media_events_buffer == NULL) { 5703 rc = -ENODEV; 5704 goto out; 5705 } 5706 5707 for (event_id = 0; event_id < num_events; ++event_id) { 5708 entry = TAILQ_FIRST(&desc->free_media_events); 5709 if (entry == NULL) { 5710 break; 5711 } 5712 5713 TAILQ_REMOVE(&desc->free_media_events, entry, tailq); 5714 TAILQ_INSERT_TAIL(&desc->pending_media_events, entry, tailq); 5715 entry->event = events[event_id]; 5716 } 5717 5718 rc = event_id; 5719 out: 5720 pthread_mutex_unlock(&bdev->internal.mutex); 5721 return rc; 5722 } 5723 5724 void 5725 spdk_bdev_notify_media_management(struct spdk_bdev *bdev) 5726 { 5727 struct spdk_bdev_desc *desc; 5728 5729 pthread_mutex_lock(&bdev->internal.mutex); 5730 TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) { 5731 if (!TAILQ_EMPTY(&desc->pending_media_events)) { 5732 desc->callback.event_fn(SPDK_BDEV_EVENT_MEDIA_MANAGEMENT, bdev, 5733 desc->callback.ctx); 5734 } 5735 } 5736 pthread_mutex_unlock(&bdev->internal.mutex); 5737 } 5738 5739 struct locked_lba_range_ctx { 5740 struct lba_range range; 5741 struct spdk_bdev *bdev; 5742 struct lba_range *current_range; 5743 struct lba_range *owner_range; 5744 struct spdk_poller *poller; 5745 lock_range_cb cb_fn; 5746 void *cb_arg; 5747 }; 5748 5749 static void 5750 bdev_lock_error_cleanup_cb(struct spdk_io_channel_iter *i, int status) 5751 { 5752 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5753 5754 ctx->cb_fn(ctx->cb_arg, -ENOMEM); 5755 free(ctx); 5756 } 5757 5758 static void 5759 bdev_unlock_lba_range_get_channel(struct spdk_io_channel_iter *i); 5760 5761 static void 5762 bdev_lock_lba_range_cb(struct spdk_io_channel_iter *i, int status) 5763 { 5764 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5765 struct spdk_bdev *bdev = ctx->bdev; 5766 5767 if (status == -ENOMEM) { 5768 /* One of the channels could not allocate a range object. 5769 * So we have to go back and clean up any ranges that were 5770 * allocated successfully before we return error status to 5771 * the caller. We can reuse the unlock function to do that 5772 * clean up. 5773 */ 5774 spdk_for_each_channel(__bdev_to_io_dev(bdev), 5775 bdev_unlock_lba_range_get_channel, ctx, 5776 bdev_lock_error_cleanup_cb); 5777 return; 5778 } 5779 5780 /* All channels have locked this range and no I/O overlapping the range 5781 * are outstanding! Set the owner_ch for the range object for the 5782 * locking channel, so that this channel will know that it is allowed 5783 * to write to this range. 5784 */ 5785 ctx->owner_range->owner_ch = ctx->range.owner_ch; 5786 ctx->cb_fn(ctx->cb_arg, status); 5787 5788 /* Don't free the ctx here. Its range is in the bdev's global list of 5789 * locked ranges still, and will be removed and freed when this range 5790 * is later unlocked. 5791 */ 5792 } 5793 5794 static int 5795 bdev_lock_lba_range_check_io(void *_i) 5796 { 5797 struct spdk_io_channel_iter *i = _i; 5798 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5799 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5800 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5801 struct lba_range *range = ctx->current_range; 5802 struct spdk_bdev_io *bdev_io; 5803 5804 spdk_poller_unregister(&ctx->poller); 5805 5806 /* The range is now in the locked_ranges, so no new IO can be submitted to this 5807 * range. But we need to wait until any outstanding IO overlapping with this range 5808 * are completed. 5809 */ 5810 TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) { 5811 if (bdev_io_range_is_locked(bdev_io, range)) { 5812 ctx->poller = spdk_poller_register(bdev_lock_lba_range_check_io, i, 100); 5813 return 1; 5814 } 5815 } 5816 5817 spdk_for_each_channel_continue(i, 0); 5818 return 1; 5819 } 5820 5821 static void 5822 bdev_lock_lba_range_get_channel(struct spdk_io_channel_iter *i) 5823 { 5824 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5825 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5826 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5827 struct lba_range *range; 5828 5829 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 5830 if (range->length == ctx->range.length && range->offset == ctx->range.offset) { 5831 /* This range already exists on this channel, so don't add 5832 * it again. This can happen when a new channel is created 5833 * while the for_each_channel operation is in progress. 5834 * Do not check for outstanding I/O in that case, since the 5835 * range was locked before any I/O could be submitted to the 5836 * new channel. 5837 */ 5838 spdk_for_each_channel_continue(i, 0); 5839 return; 5840 } 5841 } 5842 5843 range = calloc(1, sizeof(*range)); 5844 if (range == NULL) { 5845 spdk_for_each_channel_continue(i, -ENOMEM); 5846 return; 5847 } 5848 5849 range->length = ctx->range.length; 5850 range->offset = ctx->range.offset; 5851 range->locked_ctx = ctx->range.locked_ctx; 5852 ctx->current_range = range; 5853 if (ctx->range.owner_ch == ch) { 5854 /* This is the range object for the channel that will hold 5855 * the lock. Store it in the ctx object so that we can easily 5856 * set its owner_ch after the lock is finally acquired. 5857 */ 5858 ctx->owner_range = range; 5859 } 5860 TAILQ_INSERT_TAIL(&ch->locked_ranges, range, tailq); 5861 bdev_lock_lba_range_check_io(i); 5862 } 5863 5864 static void 5865 bdev_lock_lba_range_ctx(struct spdk_bdev *bdev, struct locked_lba_range_ctx *ctx) 5866 { 5867 /* We will add a copy of this range to each channel now. */ 5868 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_lock_lba_range_get_channel, ctx, 5869 bdev_lock_lba_range_cb); 5870 } 5871 5872 int 5873 bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 5874 uint64_t offset, uint64_t length, 5875 lock_range_cb cb_fn, void *cb_arg); 5876 5877 int 5878 bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 5879 uint64_t offset, uint64_t length, 5880 lock_range_cb cb_fn, void *cb_arg) 5881 { 5882 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5883 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5884 struct locked_lba_range_ctx *ctx; 5885 5886 if (cb_arg == NULL) { 5887 SPDK_ERRLOG("cb_arg must not be NULL\n"); 5888 return -EINVAL; 5889 } 5890 5891 ctx = calloc(1, sizeof(*ctx)); 5892 if (ctx == NULL) { 5893 return -ENOMEM; 5894 } 5895 5896 ctx->range.offset = offset; 5897 ctx->range.length = length; 5898 ctx->range.owner_ch = ch; 5899 ctx->range.locked_ctx = cb_arg; 5900 ctx->bdev = bdev; 5901 ctx->cb_fn = cb_fn; 5902 ctx->cb_arg = cb_arg; 5903 5904 pthread_mutex_lock(&bdev->internal.mutex); 5905 TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, &ctx->range, tailq); 5906 bdev_lock_lba_range_ctx(bdev, ctx); 5907 pthread_mutex_unlock(&bdev->internal.mutex); 5908 return 0; 5909 } 5910 5911 static void 5912 bdev_unlock_lba_range_cb(struct spdk_io_channel_iter *i, int status) 5913 { 5914 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5915 5916 ctx->cb_fn(ctx->cb_arg, status); 5917 free(ctx); 5918 } 5919 5920 static void 5921 bdev_unlock_lba_range_get_channel(struct spdk_io_channel_iter *i) 5922 { 5923 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 5924 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5925 struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 5926 TAILQ_HEAD(, spdk_bdev_io) io_locked; 5927 struct spdk_bdev_io *bdev_io; 5928 struct lba_range *range; 5929 5930 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 5931 if (ctx->range.offset == range->offset && 5932 ctx->range.length == range->length) { 5933 TAILQ_REMOVE(&ch->locked_ranges, range, tailq); 5934 free(range); 5935 break; 5936 } 5937 } 5938 5939 /* Note: we should almost always be able to assert that the range specified 5940 * was found. But there are some very rare corner cases where a new channel 5941 * gets created simultaneously with a range unlock, where this function 5942 * would execute on that new channel and wouldn't have the range. 5943 * We also use this to clean up range allocations when a later allocation 5944 * fails in the locking path. 5945 * So we can't actually assert() here. 5946 */ 5947 5948 /* Swap the locked IO into a temporary list, and then try to submit them again. 5949 * We could hyper-optimize this to only resubmit locked I/O that overlap 5950 * with the range that was just unlocked, but this isn't a performance path so 5951 * we go for simplicity here. 5952 */ 5953 TAILQ_INIT(&io_locked); 5954 TAILQ_SWAP(&ch->io_locked, &io_locked, spdk_bdev_io, internal.ch_link); 5955 while (!TAILQ_EMPTY(&io_locked)) { 5956 bdev_io = TAILQ_FIRST(&io_locked); 5957 TAILQ_REMOVE(&io_locked, bdev_io, internal.ch_link); 5958 bdev_io_submit(bdev_io); 5959 } 5960 5961 spdk_for_each_channel_continue(i, 0); 5962 } 5963 5964 int 5965 bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 5966 uint64_t offset, uint64_t length, 5967 lock_range_cb cb_fn, void *cb_arg); 5968 5969 int 5970 bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch, 5971 uint64_t offset, uint64_t length, 5972 lock_range_cb cb_fn, void *cb_arg) 5973 { 5974 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc); 5975 struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch); 5976 struct locked_lba_range_ctx *ctx; 5977 struct lba_range *range; 5978 bool range_found = false; 5979 5980 /* Let's make sure the specified channel actually has a lock on 5981 * the specified range. Note that the range must match exactly. 5982 */ 5983 TAILQ_FOREACH(range, &ch->locked_ranges, tailq) { 5984 if (range->offset == offset && range->length == length && 5985 range->owner_ch == ch && range->locked_ctx == cb_arg) { 5986 range_found = true; 5987 break; 5988 } 5989 } 5990 5991 if (!range_found) { 5992 return -EINVAL; 5993 } 5994 5995 pthread_mutex_lock(&bdev->internal.mutex); 5996 /* We confirmed that this channel has locked the specified range. To 5997 * start the unlock the process, we find the range in the bdev's locked_ranges 5998 * and remove it. This ensures new channels don't inherit the locked range. 5999 * Then we will send a message to each channel (including the one specified 6000 * here) to remove the range from its per-channel list. 6001 */ 6002 TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) { 6003 if (range->offset == offset && range->length == length && 6004 range->locked_ctx == cb_arg) { 6005 break; 6006 } 6007 } 6008 if (range == NULL) { 6009 assert(false); 6010 pthread_mutex_unlock(&bdev->internal.mutex); 6011 return -EINVAL; 6012 } 6013 TAILQ_REMOVE(&bdev->internal.locked_ranges, range, tailq); 6014 ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range); 6015 pthread_mutex_unlock(&bdev->internal.mutex); 6016 6017 ctx->cb_fn = cb_fn; 6018 ctx->cb_arg = cb_arg; 6019 6020 spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_unlock_lba_range_get_channel, ctx, 6021 bdev_unlock_lba_range_cb); 6022 return 0; 6023 } 6024 6025 SPDK_LOG_REGISTER_COMPONENT("bdev", SPDK_LOG_BDEV) 6026 6027 SPDK_TRACE_REGISTER_FN(bdev_trace, "bdev", TRACE_GROUP_BDEV) 6028 { 6029 spdk_trace_register_owner(OWNER_BDEV, 'b'); 6030 spdk_trace_register_object(OBJECT_BDEV_IO, 'i'); 6031 spdk_trace_register_description("BDEV_IO_START", TRACE_BDEV_IO_START, OWNER_BDEV, 6032 OBJECT_BDEV_IO, 1, 0, "type: "); 6033 spdk_trace_register_description("BDEV_IO_DONE", TRACE_BDEV_IO_DONE, OWNER_BDEV, 6034 OBJECT_BDEV_IO, 0, 0, ""); 6035 } 6036