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