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