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