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