1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * All rights reserved. 4 * Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 */ 6 7 #include "bdev_raid.h" 8 #include "spdk/env.h" 9 #include "spdk/thread.h" 10 #include "spdk/log.h" 11 #include "spdk/string.h" 12 #include "spdk/util.h" 13 #include "spdk/json.h" 14 #include "spdk/likely.h" 15 16 #define RAID_OFFSET_BLOCKS_INVALID UINT64_MAX 17 #define RAID_BDEV_PROCESS_MAX_QD 16 18 19 #define RAID_BDEV_PROCESS_WINDOW_SIZE_KB_DEFAULT 1024 20 21 static bool g_shutdown_started = false; 22 23 /* List of all raid bdevs */ 24 struct raid_all_tailq g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list); 25 26 static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules); 27 28 /* 29 * raid_bdev_io_channel is the context of spdk_io_channel for raid bdev device. It 30 * contains the relationship of raid bdev io channel with base bdev io channels. 31 */ 32 struct raid_bdev_io_channel { 33 /* Array of IO channels of base bdevs */ 34 struct spdk_io_channel **base_channel; 35 36 /* Private raid module IO channel */ 37 struct spdk_io_channel *module_channel; 38 39 /* Background process data */ 40 struct { 41 uint64_t offset; 42 struct spdk_io_channel *target_ch; 43 struct raid_bdev_io_channel *ch_processed; 44 } process; 45 }; 46 47 enum raid_bdev_process_state { 48 RAID_PROCESS_STATE_INIT, 49 RAID_PROCESS_STATE_RUNNING, 50 RAID_PROCESS_STATE_STOPPING, 51 RAID_PROCESS_STATE_STOPPED, 52 }; 53 54 struct raid_bdev_process { 55 struct raid_bdev *raid_bdev; 56 enum raid_process_type type; 57 enum raid_bdev_process_state state; 58 struct spdk_thread *thread; 59 struct raid_bdev_io_channel *raid_ch; 60 TAILQ_HEAD(, raid_bdev_process_request) requests; 61 uint64_t max_window_size; 62 uint64_t window_size; 63 uint64_t window_remaining; 64 int window_status; 65 uint64_t window_offset; 66 bool window_range_locked; 67 struct raid_base_bdev_info *target; 68 int status; 69 TAILQ_HEAD(, raid_process_finish_action) finish_actions; 70 }; 71 72 struct raid_process_finish_action { 73 spdk_msg_fn cb; 74 void *cb_ctx; 75 TAILQ_ENTRY(raid_process_finish_action) link; 76 }; 77 78 static struct spdk_raid_bdev_opts g_opts = { 79 .process_window_size_kb = RAID_BDEV_PROCESS_WINDOW_SIZE_KB_DEFAULT, 80 }; 81 82 void 83 raid_bdev_get_opts(struct spdk_raid_bdev_opts *opts) 84 { 85 *opts = g_opts; 86 } 87 88 int 89 raid_bdev_set_opts(const struct spdk_raid_bdev_opts *opts) 90 { 91 if (opts->process_window_size_kb == 0) { 92 return -EINVAL; 93 } 94 95 g_opts = *opts; 96 97 return 0; 98 } 99 100 static struct raid_bdev_module * 101 raid_bdev_module_find(enum raid_level level) 102 { 103 struct raid_bdev_module *raid_module; 104 105 TAILQ_FOREACH(raid_module, &g_raid_modules, link) { 106 if (raid_module->level == level) { 107 return raid_module; 108 } 109 } 110 111 return NULL; 112 } 113 114 void 115 raid_bdev_module_list_add(struct raid_bdev_module *raid_module) 116 { 117 if (raid_bdev_module_find(raid_module->level) != NULL) { 118 SPDK_ERRLOG("module for raid level '%s' already registered.\n", 119 raid_bdev_level_to_str(raid_module->level)); 120 assert(false); 121 } else { 122 TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link); 123 } 124 } 125 126 struct spdk_io_channel * 127 raid_bdev_channel_get_base_channel(struct raid_bdev_io_channel *raid_ch, uint8_t idx) 128 { 129 return raid_ch->base_channel[idx]; 130 } 131 132 void * 133 raid_bdev_channel_get_module_ctx(struct raid_bdev_io_channel *raid_ch) 134 { 135 assert(raid_ch->module_channel != NULL); 136 137 return spdk_io_channel_get_ctx(raid_ch->module_channel); 138 } 139 140 /* Function declarations */ 141 static void raid_bdev_examine(struct spdk_bdev *bdev); 142 static int raid_bdev_init(void); 143 static void raid_bdev_deconfigure(struct raid_bdev *raid_bdev, 144 raid_bdev_destruct_cb cb_fn, void *cb_arg); 145 146 static void 147 raid_bdev_ch_process_cleanup(struct raid_bdev_io_channel *raid_ch) 148 { 149 raid_ch->process.offset = RAID_OFFSET_BLOCKS_INVALID; 150 151 if (raid_ch->process.target_ch != NULL) { 152 spdk_put_io_channel(raid_ch->process.target_ch); 153 raid_ch->process.target_ch = NULL; 154 } 155 156 if (raid_ch->process.ch_processed != NULL) { 157 free(raid_ch->process.ch_processed->base_channel); 158 free(raid_ch->process.ch_processed); 159 raid_ch->process.ch_processed = NULL; 160 } 161 } 162 163 static int 164 raid_bdev_ch_process_setup(struct raid_bdev_io_channel *raid_ch, struct raid_bdev_process *process) 165 { 166 struct raid_bdev *raid_bdev = process->raid_bdev; 167 struct raid_bdev_io_channel *raid_ch_processed; 168 struct raid_base_bdev_info *base_info; 169 170 raid_ch->process.offset = process->window_offset; 171 172 /* In the future we may have other types of processes which don't use a target bdev, 173 * like data scrubbing or strip size migration. Until then, expect that there always is 174 * a process target. */ 175 assert(process->target != NULL); 176 177 raid_ch->process.target_ch = spdk_bdev_get_io_channel(process->target->desc); 178 if (raid_ch->process.target_ch == NULL) { 179 goto err; 180 } 181 182 raid_ch_processed = calloc(1, sizeof(*raid_ch_processed)); 183 if (raid_ch_processed == NULL) { 184 goto err; 185 } 186 raid_ch->process.ch_processed = raid_ch_processed; 187 188 raid_ch_processed->base_channel = calloc(raid_bdev->num_base_bdevs, 189 sizeof(*raid_ch_processed->base_channel)); 190 if (raid_ch_processed->base_channel == NULL) { 191 goto err; 192 } 193 194 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 195 uint8_t slot = raid_bdev_base_bdev_slot(base_info); 196 197 if (base_info != process->target) { 198 raid_ch_processed->base_channel[slot] = raid_ch->base_channel[slot]; 199 } else { 200 raid_ch_processed->base_channel[slot] = raid_ch->process.target_ch; 201 } 202 } 203 204 raid_ch_processed->module_channel = raid_ch->module_channel; 205 raid_ch_processed->process.offset = RAID_OFFSET_BLOCKS_INVALID; 206 207 return 0; 208 err: 209 raid_bdev_ch_process_cleanup(raid_ch); 210 return -ENOMEM; 211 } 212 213 /* 214 * brief: 215 * raid_bdev_create_cb function is a cb function for raid bdev which creates the 216 * hierarchy from raid bdev to base bdev io channels. It will be called per core 217 * params: 218 * io_device - pointer to raid bdev io device represented by raid_bdev 219 * ctx_buf - pointer to context buffer for raid bdev io channel 220 * returns: 221 * 0 - success 222 * non zero - failure 223 */ 224 static int 225 raid_bdev_create_cb(void *io_device, void *ctx_buf) 226 { 227 struct raid_bdev *raid_bdev = io_device; 228 struct raid_bdev_io_channel *raid_ch = ctx_buf; 229 uint8_t i; 230 int ret = -ENOMEM; 231 232 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_create_cb, %p\n", raid_ch); 233 234 assert(raid_bdev != NULL); 235 assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE); 236 237 raid_ch->base_channel = calloc(raid_bdev->num_base_bdevs, sizeof(struct spdk_io_channel *)); 238 if (!raid_ch->base_channel) { 239 SPDK_ERRLOG("Unable to allocate base bdevs io channel\n"); 240 return -ENOMEM; 241 } 242 243 spdk_spin_lock(&raid_bdev->base_bdev_lock); 244 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 245 /* 246 * Get the spdk_io_channel for all the base bdevs. This is used during 247 * split logic to send the respective child bdev ios to respective base 248 * bdev io channel. 249 * Skip missing base bdevs and the process target, which should also be treated as 250 * missing until the process completes. 251 */ 252 if (raid_bdev->base_bdev_info[i].desc == NULL || 253 (raid_bdev->process != NULL && raid_bdev->process->target == &raid_bdev->base_bdev_info[i])) { 254 continue; 255 } 256 raid_ch->base_channel[i] = spdk_bdev_get_io_channel( 257 raid_bdev->base_bdev_info[i].desc); 258 if (!raid_ch->base_channel[i]) { 259 SPDK_ERRLOG("Unable to create io channel for base bdev\n"); 260 goto err; 261 } 262 } 263 264 if (raid_bdev->process != NULL) { 265 ret = raid_bdev_ch_process_setup(raid_ch, raid_bdev->process); 266 if (ret != 0) { 267 SPDK_ERRLOG("Failed to setup process io channel\n"); 268 goto err; 269 } 270 } else { 271 raid_ch->process.offset = RAID_OFFSET_BLOCKS_INVALID; 272 } 273 spdk_spin_unlock(&raid_bdev->base_bdev_lock); 274 275 if (raid_bdev->module->get_io_channel) { 276 raid_ch->module_channel = raid_bdev->module->get_io_channel(raid_bdev); 277 if (!raid_ch->module_channel) { 278 SPDK_ERRLOG("Unable to create io channel for raid module\n"); 279 goto err_unlocked; 280 } 281 } 282 283 return 0; 284 err: 285 spdk_spin_unlock(&raid_bdev->base_bdev_lock); 286 err_unlocked: 287 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 288 if (raid_ch->base_channel[i] != NULL) { 289 spdk_put_io_channel(raid_ch->base_channel[i]); 290 } 291 } 292 free(raid_ch->base_channel); 293 294 raid_bdev_ch_process_cleanup(raid_ch); 295 296 return ret; 297 } 298 299 /* 300 * brief: 301 * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the 302 * hierarchy from raid bdev to base bdev io channels. It will be called per core 303 * params: 304 * io_device - pointer to raid bdev io device represented by raid_bdev 305 * ctx_buf - pointer to context buffer for raid bdev io channel 306 * returns: 307 * none 308 */ 309 static void 310 raid_bdev_destroy_cb(void *io_device, void *ctx_buf) 311 { 312 struct raid_bdev *raid_bdev = io_device; 313 struct raid_bdev_io_channel *raid_ch = ctx_buf; 314 uint8_t i; 315 316 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destroy_cb\n"); 317 318 assert(raid_ch != NULL); 319 assert(raid_ch->base_channel); 320 321 if (raid_ch->module_channel) { 322 spdk_put_io_channel(raid_ch->module_channel); 323 } 324 325 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 326 /* Free base bdev channels */ 327 if (raid_ch->base_channel[i] != NULL) { 328 spdk_put_io_channel(raid_ch->base_channel[i]); 329 } 330 } 331 free(raid_ch->base_channel); 332 raid_ch->base_channel = NULL; 333 334 raid_bdev_ch_process_cleanup(raid_ch); 335 } 336 337 /* 338 * brief: 339 * raid_bdev_cleanup is used to cleanup raid_bdev related data 340 * structures. 341 * params: 342 * raid_bdev - pointer to raid_bdev 343 * returns: 344 * none 345 */ 346 static void 347 raid_bdev_cleanup(struct raid_bdev *raid_bdev) 348 { 349 struct raid_base_bdev_info *base_info; 350 351 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_cleanup, %p name %s, state %s\n", 352 raid_bdev, raid_bdev->bdev.name, raid_bdev_state_to_str(raid_bdev->state)); 353 assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE); 354 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 355 356 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 357 assert(base_info->desc == NULL); 358 free(base_info->name); 359 } 360 361 TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link); 362 } 363 364 static void 365 raid_bdev_free(struct raid_bdev *raid_bdev) 366 { 367 raid_bdev_free_superblock(raid_bdev); 368 spdk_spin_destroy(&raid_bdev->base_bdev_lock); 369 free(raid_bdev->base_bdev_info); 370 free(raid_bdev->bdev.name); 371 free(raid_bdev); 372 } 373 374 static void 375 raid_bdev_cleanup_and_free(struct raid_bdev *raid_bdev) 376 { 377 raid_bdev_cleanup(raid_bdev); 378 raid_bdev_free(raid_bdev); 379 } 380 381 /* 382 * brief: 383 * free resource of base bdev for raid bdev 384 * params: 385 * base_info - raid base bdev info 386 * returns: 387 * none 388 */ 389 static void 390 raid_bdev_free_base_bdev_resource(struct raid_base_bdev_info *base_info) 391 { 392 struct raid_bdev *raid_bdev = base_info->raid_bdev; 393 394 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 395 396 free(base_info->name); 397 base_info->name = NULL; 398 if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) { 399 spdk_uuid_set_null(&base_info->uuid); 400 } 401 402 if (base_info->desc == NULL) { 403 return; 404 } 405 406 spdk_bdev_module_release_bdev(spdk_bdev_desc_get_bdev(base_info->desc)); 407 spdk_bdev_close(base_info->desc); 408 base_info->desc = NULL; 409 spdk_put_io_channel(base_info->app_thread_ch); 410 base_info->app_thread_ch = NULL; 411 412 if (base_info->is_configured) { 413 assert(raid_bdev->num_base_bdevs_discovered); 414 raid_bdev->num_base_bdevs_discovered--; 415 base_info->is_configured = false; 416 } 417 } 418 419 static void 420 raid_bdev_io_device_unregister_cb(void *io_device) 421 { 422 struct raid_bdev *raid_bdev = io_device; 423 424 if (raid_bdev->num_base_bdevs_discovered == 0) { 425 /* Free raid_bdev when there are no base bdevs left */ 426 SPDK_DEBUGLOG(bdev_raid, "raid bdev base bdevs is 0, going to free all in destruct\n"); 427 raid_bdev_cleanup(raid_bdev); 428 spdk_bdev_destruct_done(&raid_bdev->bdev, 0); 429 raid_bdev_free(raid_bdev); 430 } else { 431 spdk_bdev_destruct_done(&raid_bdev->bdev, 0); 432 } 433 } 434 435 void 436 raid_bdev_module_stop_done(struct raid_bdev *raid_bdev) 437 { 438 if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) { 439 spdk_io_device_unregister(raid_bdev, raid_bdev_io_device_unregister_cb); 440 } 441 } 442 443 static void 444 _raid_bdev_destruct(void *ctxt) 445 { 446 struct raid_bdev *raid_bdev = ctxt; 447 struct raid_base_bdev_info *base_info; 448 449 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destruct\n"); 450 451 assert(raid_bdev->process == NULL); 452 453 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 454 /* 455 * Close all base bdev descriptors for which call has come from below 456 * layers. Also close the descriptors if we have started shutdown. 457 */ 458 if (g_shutdown_started || base_info->remove_scheduled == true) { 459 raid_bdev_free_base_bdev_resource(base_info); 460 } 461 } 462 463 if (g_shutdown_started) { 464 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 465 } 466 467 if (raid_bdev->module->stop != NULL) { 468 if (raid_bdev->module->stop(raid_bdev) == false) { 469 return; 470 } 471 } 472 473 raid_bdev_module_stop_done(raid_bdev); 474 } 475 476 static int 477 raid_bdev_destruct(void *ctx) 478 { 479 spdk_thread_exec_msg(spdk_thread_get_app_thread(), _raid_bdev_destruct, ctx); 480 481 return 1; 482 } 483 484 static int 485 raid_bdev_remap_dix_reftag(void *md_buf, uint64_t num_blocks, 486 struct spdk_bdev *bdev, uint32_t remapped_offset) 487 { 488 struct spdk_dif_ctx dif_ctx; 489 struct spdk_dif_error err_blk = {}; 490 int rc; 491 struct spdk_dif_ctx_init_ext_opts dif_opts; 492 struct iovec md_iov = { 493 .iov_base = md_buf, 494 .iov_len = num_blocks * bdev->md_len, 495 }; 496 497 if (md_buf == NULL) { 498 return 0; 499 } 500 501 dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format); 502 dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16; 503 rc = spdk_dif_ctx_init(&dif_ctx, 504 bdev->blocklen, bdev->md_len, bdev->md_interleave, 505 bdev->dif_is_head_of_md, bdev->dif_type, 506 SPDK_DIF_FLAGS_REFTAG_CHECK, 507 0, 0, 0, 0, 0, &dif_opts); 508 if (rc != 0) { 509 SPDK_ERRLOG("Initialization of DIF context failed\n"); 510 return rc; 511 } 512 513 spdk_dif_ctx_set_remapped_init_ref_tag(&dif_ctx, remapped_offset); 514 515 rc = spdk_dix_remap_ref_tag(&md_iov, num_blocks, &dif_ctx, &err_blk, false); 516 if (rc != 0) { 517 SPDK_ERRLOG("Remapping reference tag failed. type=%d, offset=%d" 518 PRIu32 "\n", err_blk.err_type, err_blk.err_offset); 519 } 520 521 return rc; 522 } 523 524 int 525 raid_bdev_verify_dix_reftag(struct iovec *iovs, int iovcnt, void *md_buf, 526 uint64_t num_blocks, struct spdk_bdev *bdev, uint32_t offset_blocks) 527 { 528 struct spdk_dif_ctx dif_ctx; 529 struct spdk_dif_error err_blk = {}; 530 int rc; 531 struct spdk_dif_ctx_init_ext_opts dif_opts; 532 struct iovec md_iov = { 533 .iov_base = md_buf, 534 .iov_len = num_blocks * bdev->md_len, 535 }; 536 537 if (md_buf == NULL) { 538 return 0; 539 } 540 541 dif_opts.size = SPDK_SIZEOF(&dif_opts, dif_pi_format); 542 dif_opts.dif_pi_format = SPDK_DIF_PI_FORMAT_16; 543 rc = spdk_dif_ctx_init(&dif_ctx, 544 bdev->blocklen, bdev->md_len, bdev->md_interleave, 545 bdev->dif_is_head_of_md, bdev->dif_type, 546 SPDK_DIF_FLAGS_REFTAG_CHECK, 547 offset_blocks, 0, 0, 0, 0, &dif_opts); 548 if (rc != 0) { 549 SPDK_ERRLOG("Initialization of DIF context failed\n"); 550 return rc; 551 } 552 553 rc = spdk_dix_verify(iovs, iovcnt, &md_iov, num_blocks, &dif_ctx, &err_blk); 554 if (rc != 0) { 555 SPDK_ERRLOG("Reference tag check failed. type=%d, offset=%d" 556 PRIu32 "\n", err_blk.err_type, err_blk.err_offset); 557 } 558 559 return rc; 560 } 561 562 /** 563 * Raid bdev I/O read/write wrapper for spdk_bdev_readv_blocks_ext function. 564 */ 565 int 566 raid_bdev_readv_blocks_ext(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch, 567 struct iovec *iov, int iovcnt, uint64_t offset_blocks, 568 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg, 569 struct spdk_bdev_ext_io_opts *opts) 570 { 571 return spdk_bdev_readv_blocks_ext(base_info->desc, ch, iov, iovcnt, 572 base_info->data_offset + offset_blocks, num_blocks, cb, cb_arg, opts); 573 } 574 575 /** 576 * Raid bdev I/O read/write wrapper for spdk_bdev_writev_blocks_ext function. 577 */ 578 int 579 raid_bdev_writev_blocks_ext(struct raid_base_bdev_info *base_info, struct spdk_io_channel *ch, 580 struct iovec *iov, int iovcnt, uint64_t offset_blocks, 581 uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg, 582 struct spdk_bdev_ext_io_opts *opts) 583 { 584 int rc; 585 uint64_t remapped_offset_blocks = base_info->data_offset + offset_blocks; 586 587 if (spdk_unlikely(spdk_bdev_get_dif_type(&base_info->raid_bdev->bdev) != SPDK_DIF_DISABLE && 588 base_info->raid_bdev->bdev.dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK)) { 589 590 rc = raid_bdev_remap_dix_reftag(opts->metadata, num_blocks, &base_info->raid_bdev->bdev, 591 remapped_offset_blocks); 592 if (rc != 0) { 593 return rc; 594 } 595 } 596 597 return spdk_bdev_writev_blocks_ext(base_info->desc, ch, iov, iovcnt, 598 remapped_offset_blocks, num_blocks, cb, cb_arg, opts); 599 } 600 601 void 602 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status) 603 { 604 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io); 605 int rc; 606 607 if (raid_io->split.offset != RAID_OFFSET_BLOCKS_INVALID) { 608 struct iovec *split_iov = raid_io->split.iov; 609 const struct iovec *split_iov_orig = &raid_io->split.iov_copy; 610 611 /* 612 * Non-zero offset here means that this is the completion of the first part of the 613 * split I/O (the higher LBAs). Then, we submit the second part and set offset to 0. 614 */ 615 if (raid_io->split.offset != 0) { 616 raid_io->offset_blocks = bdev_io->u.bdev.offset_blocks; 617 raid_io->md_buf = bdev_io->u.bdev.md_buf; 618 619 if (status == SPDK_BDEV_IO_STATUS_SUCCESS) { 620 raid_io->num_blocks = raid_io->split.offset; 621 raid_io->iovcnt = raid_io->iovs - bdev_io->u.bdev.iovs; 622 raid_io->iovs = bdev_io->u.bdev.iovs; 623 if (split_iov != NULL) { 624 raid_io->iovcnt++; 625 split_iov->iov_len = split_iov->iov_base - split_iov_orig->iov_base; 626 split_iov->iov_base = split_iov_orig->iov_base; 627 } 628 629 raid_io->split.offset = 0; 630 raid_io->base_bdev_io_submitted = 0; 631 raid_io->raid_ch = raid_io->raid_ch->process.ch_processed; 632 633 raid_io->raid_bdev->module->submit_rw_request(raid_io); 634 return; 635 } 636 } 637 638 raid_io->num_blocks = bdev_io->u.bdev.num_blocks; 639 raid_io->iovcnt = bdev_io->u.bdev.iovcnt; 640 raid_io->iovs = bdev_io->u.bdev.iovs; 641 if (split_iov != NULL) { 642 *split_iov = *split_iov_orig; 643 } 644 } 645 646 if (spdk_unlikely(raid_io->completion_cb != NULL)) { 647 raid_io->completion_cb(raid_io, status); 648 } else { 649 if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_READ && 650 spdk_bdev_get_dif_type(bdev_io->bdev) != SPDK_DIF_DISABLE && 651 bdev_io->bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK && 652 status == SPDK_BDEV_IO_STATUS_SUCCESS)) { 653 654 rc = raid_bdev_remap_dix_reftag(bdev_io->u.bdev.md_buf, 655 bdev_io->u.bdev.num_blocks, bdev_io->bdev, 656 bdev_io->u.bdev.offset_blocks); 657 if (rc != 0) { 658 status = SPDK_BDEV_IO_STATUS_FAILED; 659 } 660 } 661 spdk_bdev_io_complete(bdev_io, status); 662 } 663 } 664 665 /* 666 * brief: 667 * raid_bdev_io_complete_part - signal the completion of a part of the expected 668 * base bdev IOs and complete the raid_io if this is the final expected IO. 669 * The caller should first set raid_io->base_bdev_io_remaining. This function 670 * will decrement this counter by the value of the 'completed' parameter and 671 * complete the raid_io if the counter reaches 0. The caller is free to 672 * interpret the 'base_bdev_io_remaining' and 'completed' values as needed, 673 * it can represent e.g. blocks or IOs. 674 * params: 675 * raid_io - pointer to raid_bdev_io 676 * completed - the part of the raid_io that has been completed 677 * status - status of the base IO 678 * returns: 679 * true - if the raid_io is completed 680 * false - otherwise 681 */ 682 bool 683 raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, 684 enum spdk_bdev_io_status status) 685 { 686 assert(raid_io->base_bdev_io_remaining >= completed); 687 raid_io->base_bdev_io_remaining -= completed; 688 689 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 690 raid_io->base_bdev_io_status = status; 691 } 692 693 if (raid_io->base_bdev_io_remaining == 0) { 694 raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status); 695 return true; 696 } else { 697 return false; 698 } 699 } 700 701 /* 702 * brief: 703 * raid_bdev_queue_io_wait function processes the IO which failed to submit. 704 * It will try to queue the IOs after storing the context to bdev wait queue logic. 705 * params: 706 * raid_io - pointer to raid_bdev_io 707 * bdev - the block device that the IO is submitted to 708 * ch - io channel 709 * cb_fn - callback when the spdk_bdev_io for bdev becomes available 710 * returns: 711 * none 712 */ 713 void 714 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 715 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn) 716 { 717 raid_io->waitq_entry.bdev = bdev; 718 raid_io->waitq_entry.cb_fn = cb_fn; 719 raid_io->waitq_entry.cb_arg = raid_io; 720 spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry); 721 } 722 723 static void 724 raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 725 { 726 struct raid_bdev_io *raid_io = cb_arg; 727 728 spdk_bdev_free_io(bdev_io); 729 730 raid_bdev_io_complete_part(raid_io, 1, success ? 731 SPDK_BDEV_IO_STATUS_SUCCESS : 732 SPDK_BDEV_IO_STATUS_FAILED); 733 } 734 735 static void raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io); 736 737 static void 738 _raid_bdev_submit_reset_request(void *_raid_io) 739 { 740 struct raid_bdev_io *raid_io = _raid_io; 741 742 raid_bdev_submit_reset_request(raid_io); 743 } 744 745 /* 746 * brief: 747 * raid_bdev_submit_reset_request function submits reset requests 748 * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in 749 * which case it will queue it for later submission 750 * params: 751 * raid_io 752 * returns: 753 * none 754 */ 755 static void 756 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io) 757 { 758 struct raid_bdev *raid_bdev; 759 int ret; 760 uint8_t i; 761 struct raid_base_bdev_info *base_info; 762 struct spdk_io_channel *base_ch; 763 764 raid_bdev = raid_io->raid_bdev; 765 766 if (raid_io->base_bdev_io_remaining == 0) { 767 raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs; 768 } 769 770 for (i = raid_io->base_bdev_io_submitted; i < raid_bdev->num_base_bdevs; i++) { 771 base_info = &raid_bdev->base_bdev_info[i]; 772 base_ch = raid_io->raid_ch->base_channel[i]; 773 if (base_ch == NULL) { 774 raid_io->base_bdev_io_submitted++; 775 raid_bdev_io_complete_part(raid_io, 1, SPDK_BDEV_IO_STATUS_SUCCESS); 776 continue; 777 } 778 ret = spdk_bdev_reset(base_info->desc, base_ch, 779 raid_base_bdev_reset_complete, raid_io); 780 if (ret == 0) { 781 raid_io->base_bdev_io_submitted++; 782 } else if (ret == -ENOMEM) { 783 raid_bdev_queue_io_wait(raid_io, spdk_bdev_desc_get_bdev(base_info->desc), 784 base_ch, _raid_bdev_submit_reset_request); 785 return; 786 } else { 787 SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); 788 assert(false); 789 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 790 return; 791 } 792 } 793 } 794 795 static void 796 raid_bdev_io_split(struct raid_bdev_io *raid_io, uint64_t split_offset) 797 { 798 struct raid_bdev *raid_bdev = raid_io->raid_bdev; 799 size_t iov_offset = split_offset * raid_bdev->bdev.blocklen; 800 int i; 801 802 assert(split_offset != 0); 803 assert(raid_io->split.offset == RAID_OFFSET_BLOCKS_INVALID); 804 raid_io->split.offset = split_offset; 805 806 raid_io->offset_blocks += split_offset; 807 raid_io->num_blocks -= split_offset; 808 if (raid_io->md_buf != NULL) { 809 raid_io->md_buf += (split_offset * raid_bdev->bdev.md_len); 810 } 811 812 for (i = 0; i < raid_io->iovcnt; i++) { 813 struct iovec *iov = &raid_io->iovs[i]; 814 815 if (iov_offset < iov->iov_len) { 816 if (iov_offset == 0) { 817 raid_io->split.iov = NULL; 818 } else { 819 raid_io->split.iov = iov; 820 raid_io->split.iov_copy = *iov; 821 iov->iov_base += iov_offset; 822 iov->iov_len -= iov_offset; 823 } 824 raid_io->iovs += i; 825 raid_io->iovcnt -= i; 826 break; 827 } 828 829 iov_offset -= iov->iov_len; 830 } 831 } 832 833 static void 834 raid_bdev_submit_rw_request(struct raid_bdev_io *raid_io) 835 { 836 struct raid_bdev_io_channel *raid_ch = raid_io->raid_ch; 837 838 if (raid_ch->process.offset != RAID_OFFSET_BLOCKS_INVALID) { 839 uint64_t offset_begin = raid_io->offset_blocks; 840 uint64_t offset_end = offset_begin + raid_io->num_blocks; 841 842 if (offset_end > raid_ch->process.offset) { 843 if (offset_begin < raid_ch->process.offset) { 844 /* 845 * If the I/O spans both the processed and unprocessed ranges, 846 * split it and first handle the unprocessed part. After it 847 * completes, the rest will be handled. 848 * This situation occurs when the process thread is not active 849 * or is waiting for the process window range to be locked 850 * (quiesced). When a window is being processed, such I/Os will be 851 * deferred by the bdev layer until the window is unlocked. 852 */ 853 SPDK_DEBUGLOG(bdev_raid, "split: process_offset: %lu offset_begin: %lu offset_end: %lu\n", 854 raid_ch->process.offset, offset_begin, offset_end); 855 raid_bdev_io_split(raid_io, raid_ch->process.offset - offset_begin); 856 } 857 } else { 858 /* Use the child channel, which corresponds to the already processed range */ 859 raid_io->raid_ch = raid_ch->process.ch_processed; 860 } 861 } 862 863 raid_io->raid_bdev->module->submit_rw_request(raid_io); 864 } 865 866 /* 867 * brief: 868 * Callback function to spdk_bdev_io_get_buf. 869 * params: 870 * ch - pointer to raid bdev io channel 871 * bdev_io - pointer to parent bdev_io on raid bdev device 872 * success - True if buffer is allocated or false otherwise. 873 * returns: 874 * none 875 */ 876 static void 877 raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 878 bool success) 879 { 880 struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; 881 882 if (!success) { 883 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 884 return; 885 } 886 887 raid_bdev_submit_rw_request(raid_io); 888 } 889 890 void 891 raid_bdev_io_init(struct raid_bdev_io *raid_io, struct raid_bdev_io_channel *raid_ch, 892 enum spdk_bdev_io_type type, uint64_t offset_blocks, 893 uint64_t num_blocks, struct iovec *iovs, int iovcnt, void *md_buf, 894 struct spdk_memory_domain *memory_domain, void *memory_domain_ctx) 895 { 896 struct spdk_io_channel *ch = spdk_io_channel_from_ctx(raid_ch); 897 struct raid_bdev *raid_bdev = spdk_io_channel_get_io_device(ch); 898 899 raid_io->type = type; 900 raid_io->offset_blocks = offset_blocks; 901 raid_io->num_blocks = num_blocks; 902 raid_io->iovs = iovs; 903 raid_io->iovcnt = iovcnt; 904 raid_io->memory_domain = memory_domain; 905 raid_io->memory_domain_ctx = memory_domain_ctx; 906 raid_io->md_buf = md_buf; 907 908 raid_io->raid_bdev = raid_bdev; 909 raid_io->raid_ch = raid_ch; 910 raid_io->base_bdev_io_remaining = 0; 911 raid_io->base_bdev_io_submitted = 0; 912 raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS; 913 raid_io->completion_cb = NULL; 914 raid_io->split.offset = RAID_OFFSET_BLOCKS_INVALID; 915 } 916 917 /* 918 * brief: 919 * raid_bdev_submit_request function is the submit_request function pointer of 920 * raid bdev function table. This is used to submit the io on raid_bdev to below 921 * layers. 922 * params: 923 * ch - pointer to raid bdev io channel 924 * bdev_io - pointer to parent bdev_io on raid bdev device 925 * returns: 926 * none 927 */ 928 static void 929 raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 930 { 931 struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; 932 933 raid_bdev_io_init(raid_io, spdk_io_channel_get_ctx(ch), bdev_io->type, 934 bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks, 935 bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.md_buf, 936 bdev_io->u.bdev.memory_domain, bdev_io->u.bdev.memory_domain_ctx); 937 938 switch (bdev_io->type) { 939 case SPDK_BDEV_IO_TYPE_READ: 940 spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb, 941 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 942 break; 943 case SPDK_BDEV_IO_TYPE_WRITE: 944 raid_bdev_submit_rw_request(raid_io); 945 break; 946 947 case SPDK_BDEV_IO_TYPE_RESET: 948 raid_bdev_submit_reset_request(raid_io); 949 break; 950 951 case SPDK_BDEV_IO_TYPE_FLUSH: 952 case SPDK_BDEV_IO_TYPE_UNMAP: 953 if (raid_io->raid_bdev->process != NULL) { 954 /* TODO: rebuild support */ 955 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 956 return; 957 } 958 raid_io->raid_bdev->module->submit_null_payload_request(raid_io); 959 break; 960 961 default: 962 SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type); 963 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 964 break; 965 } 966 } 967 968 /* 969 * brief: 970 * _raid_bdev_io_type_supported checks whether io_type is supported in 971 * all base bdev modules of raid bdev module. If anyone among the base_bdevs 972 * doesn't support, the raid device doesn't supports. 973 * 974 * params: 975 * raid_bdev - pointer to raid bdev context 976 * io_type - io type 977 * returns: 978 * true - io_type is supported 979 * false - io_type is not supported 980 */ 981 inline static bool 982 _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type) 983 { 984 struct raid_base_bdev_info *base_info; 985 986 if (io_type == SPDK_BDEV_IO_TYPE_FLUSH || 987 io_type == SPDK_BDEV_IO_TYPE_UNMAP) { 988 if (raid_bdev->module->submit_null_payload_request == NULL) { 989 return false; 990 } 991 } 992 993 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 994 if (base_info->desc == NULL) { 995 continue; 996 } 997 998 if (spdk_bdev_io_type_supported(spdk_bdev_desc_get_bdev(base_info->desc), io_type) == false) { 999 return false; 1000 } 1001 } 1002 1003 return true; 1004 } 1005 1006 /* 1007 * brief: 1008 * raid_bdev_io_type_supported is the io_supported function for bdev function 1009 * table which returns whether the particular io type is supported or not by 1010 * raid bdev module 1011 * params: 1012 * ctx - pointer to raid bdev context 1013 * type - io type 1014 * returns: 1015 * true - io_type is supported 1016 * false - io_type is not supported 1017 */ 1018 static bool 1019 raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 1020 { 1021 switch (io_type) { 1022 case SPDK_BDEV_IO_TYPE_READ: 1023 case SPDK_BDEV_IO_TYPE_WRITE: 1024 return true; 1025 1026 case SPDK_BDEV_IO_TYPE_FLUSH: 1027 case SPDK_BDEV_IO_TYPE_RESET: 1028 case SPDK_BDEV_IO_TYPE_UNMAP: 1029 return _raid_bdev_io_type_supported(ctx, io_type); 1030 1031 default: 1032 return false; 1033 } 1034 1035 return false; 1036 } 1037 1038 /* 1039 * brief: 1040 * raid_bdev_get_io_channel is the get_io_channel function table pointer for 1041 * raid bdev. This is used to return the io channel for this raid bdev 1042 * params: 1043 * ctxt - pointer to raid_bdev 1044 * returns: 1045 * pointer to io channel for raid bdev 1046 */ 1047 static struct spdk_io_channel * 1048 raid_bdev_get_io_channel(void *ctxt) 1049 { 1050 struct raid_bdev *raid_bdev = ctxt; 1051 1052 return spdk_get_io_channel(raid_bdev); 1053 } 1054 1055 void 1056 raid_bdev_write_info_json(struct raid_bdev *raid_bdev, struct spdk_json_write_ctx *w) 1057 { 1058 struct raid_base_bdev_info *base_info; 1059 char uuid_str[SPDK_UUID_STRING_LEN]; 1060 1061 assert(raid_bdev != NULL); 1062 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 1063 1064 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &raid_bdev->bdev.uuid); 1065 spdk_json_write_named_string(w, "uuid", uuid_str); 1066 spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb); 1067 spdk_json_write_named_string(w, "state", raid_bdev_state_to_str(raid_bdev->state)); 1068 spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); 1069 spdk_json_write_named_bool(w, "superblock", raid_bdev->superblock_enabled); 1070 spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs); 1071 spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered); 1072 spdk_json_write_named_uint32(w, "num_base_bdevs_operational", 1073 raid_bdev->num_base_bdevs_operational); 1074 if (raid_bdev->process) { 1075 struct raid_bdev_process *process = raid_bdev->process; 1076 uint64_t offset = process->window_offset; 1077 1078 spdk_json_write_named_object_begin(w, "process"); 1079 spdk_json_write_name(w, "type"); 1080 spdk_json_write_string(w, raid_bdev_process_to_str(process->type)); 1081 spdk_json_write_named_string(w, "target", process->target->name); 1082 spdk_json_write_named_object_begin(w, "progress"); 1083 spdk_json_write_named_uint64(w, "blocks", offset); 1084 spdk_json_write_named_uint32(w, "percent", offset * 100.0 / raid_bdev->bdev.blockcnt); 1085 spdk_json_write_object_end(w); 1086 spdk_json_write_object_end(w); 1087 } 1088 spdk_json_write_name(w, "base_bdevs_list"); 1089 spdk_json_write_array_begin(w); 1090 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1091 spdk_json_write_object_begin(w); 1092 spdk_json_write_name(w, "name"); 1093 if (base_info->name) { 1094 spdk_json_write_string(w, base_info->name); 1095 } else { 1096 spdk_json_write_null(w); 1097 } 1098 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid); 1099 spdk_json_write_named_string(w, "uuid", uuid_str); 1100 spdk_json_write_named_bool(w, "is_configured", base_info->is_configured); 1101 spdk_json_write_named_uint64(w, "data_offset", base_info->data_offset); 1102 spdk_json_write_named_uint64(w, "data_size", base_info->data_size); 1103 spdk_json_write_object_end(w); 1104 } 1105 spdk_json_write_array_end(w); 1106 } 1107 1108 /* 1109 * brief: 1110 * raid_bdev_dump_info_json is the function table pointer for raid bdev 1111 * params: 1112 * ctx - pointer to raid_bdev 1113 * w - pointer to json context 1114 * returns: 1115 * 0 - success 1116 * non zero - failure 1117 */ 1118 static int 1119 raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 1120 { 1121 struct raid_bdev *raid_bdev = ctx; 1122 1123 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_dump_config_json\n"); 1124 1125 /* Dump the raid bdev configuration related information */ 1126 spdk_json_write_named_object_begin(w, "raid"); 1127 raid_bdev_write_info_json(raid_bdev, w); 1128 spdk_json_write_object_end(w); 1129 1130 return 0; 1131 } 1132 1133 /* 1134 * brief: 1135 * raid_bdev_write_config_json is the function table pointer for raid bdev 1136 * params: 1137 * bdev - pointer to spdk_bdev 1138 * w - pointer to json context 1139 * returns: 1140 * none 1141 */ 1142 static void 1143 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 1144 { 1145 struct raid_bdev *raid_bdev = bdev->ctxt; 1146 struct raid_base_bdev_info *base_info; 1147 char uuid_str[SPDK_UUID_STRING_LEN]; 1148 1149 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 1150 1151 if (raid_bdev->superblock_enabled) { 1152 /* raid bdev configuration is stored in the superblock */ 1153 return; 1154 } 1155 1156 spdk_json_write_object_begin(w); 1157 1158 spdk_json_write_named_string(w, "method", "bdev_raid_create"); 1159 1160 spdk_json_write_named_object_begin(w, "params"); 1161 spdk_json_write_named_string(w, "name", bdev->name); 1162 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &raid_bdev->bdev.uuid); 1163 spdk_json_write_named_string(w, "uuid", uuid_str); 1164 spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb); 1165 spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); 1166 spdk_json_write_named_bool(w, "superblock", raid_bdev->superblock_enabled); 1167 1168 spdk_json_write_named_array_begin(w, "base_bdevs"); 1169 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1170 if (base_info->desc) { 1171 spdk_json_write_string(w, spdk_bdev_desc_get_bdev(base_info->desc)->name); 1172 } 1173 } 1174 spdk_json_write_array_end(w); 1175 spdk_json_write_object_end(w); 1176 1177 spdk_json_write_object_end(w); 1178 } 1179 1180 static int 1181 raid_bdev_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size) 1182 { 1183 struct raid_bdev *raid_bdev = ctx; 1184 struct raid_base_bdev_info *base_info; 1185 int domains_count = 0, rc = 0; 1186 1187 if (raid_bdev->module->memory_domains_supported == false) { 1188 return 0; 1189 } 1190 1191 spdk_spin_lock(&raid_bdev->base_bdev_lock); 1192 1193 /* First loop to get the number of memory domains */ 1194 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1195 if (base_info->desc == NULL) { 1196 continue; 1197 } 1198 rc = spdk_bdev_get_memory_domains(spdk_bdev_desc_get_bdev(base_info->desc), NULL, 0); 1199 if (rc < 0) { 1200 goto out; 1201 } 1202 domains_count += rc; 1203 } 1204 1205 if (!domains || array_size < domains_count) { 1206 goto out; 1207 } 1208 1209 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1210 if (base_info->desc == NULL) { 1211 continue; 1212 } 1213 rc = spdk_bdev_get_memory_domains(spdk_bdev_desc_get_bdev(base_info->desc), domains, array_size); 1214 if (rc < 0) { 1215 goto out; 1216 } 1217 domains += rc; 1218 array_size -= rc; 1219 } 1220 out: 1221 spdk_spin_unlock(&raid_bdev->base_bdev_lock); 1222 1223 if (rc < 0) { 1224 return rc; 1225 } 1226 1227 return domains_count; 1228 } 1229 1230 /* g_raid_bdev_fn_table is the function table for raid bdev */ 1231 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = { 1232 .destruct = raid_bdev_destruct, 1233 .submit_request = raid_bdev_submit_request, 1234 .io_type_supported = raid_bdev_io_type_supported, 1235 .get_io_channel = raid_bdev_get_io_channel, 1236 .dump_info_json = raid_bdev_dump_info_json, 1237 .write_config_json = raid_bdev_write_config_json, 1238 .get_memory_domains = raid_bdev_get_memory_domains, 1239 }; 1240 1241 struct raid_bdev * 1242 raid_bdev_find_by_name(const char *name) 1243 { 1244 struct raid_bdev *raid_bdev; 1245 1246 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 1247 if (strcmp(raid_bdev->bdev.name, name) == 0) { 1248 return raid_bdev; 1249 } 1250 } 1251 1252 return NULL; 1253 } 1254 1255 static struct { 1256 const char *name; 1257 enum raid_level value; 1258 } g_raid_level_names[] = { 1259 { "raid0", RAID0 }, 1260 { "0", RAID0 }, 1261 { "raid1", RAID1 }, 1262 { "1", RAID1 }, 1263 { "raid5f", RAID5F }, 1264 { "5f", RAID5F }, 1265 { "concat", CONCAT }, 1266 { } 1267 }; 1268 1269 const char *g_raid_state_names[] = { 1270 [RAID_BDEV_STATE_ONLINE] = "online", 1271 [RAID_BDEV_STATE_CONFIGURING] = "configuring", 1272 [RAID_BDEV_STATE_OFFLINE] = "offline", 1273 [RAID_BDEV_STATE_MAX] = NULL 1274 }; 1275 1276 static const char *g_raid_process_type_names[] = { 1277 [RAID_PROCESS_NONE] = "none", 1278 [RAID_PROCESS_REBUILD] = "rebuild", 1279 [RAID_PROCESS_MAX] = NULL 1280 }; 1281 1282 /* We have to use the typedef in the function declaration to appease astyle. */ 1283 typedef enum raid_level raid_level_t; 1284 typedef enum raid_bdev_state raid_bdev_state_t; 1285 1286 raid_level_t 1287 raid_bdev_str_to_level(const char *str) 1288 { 1289 unsigned int i; 1290 1291 assert(str != NULL); 1292 1293 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 1294 if (strcasecmp(g_raid_level_names[i].name, str) == 0) { 1295 return g_raid_level_names[i].value; 1296 } 1297 } 1298 1299 return INVALID_RAID_LEVEL; 1300 } 1301 1302 const char * 1303 raid_bdev_level_to_str(enum raid_level level) 1304 { 1305 unsigned int i; 1306 1307 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 1308 if (g_raid_level_names[i].value == level) { 1309 return g_raid_level_names[i].name; 1310 } 1311 } 1312 1313 return ""; 1314 } 1315 1316 raid_bdev_state_t 1317 raid_bdev_str_to_state(const char *str) 1318 { 1319 unsigned int i; 1320 1321 assert(str != NULL); 1322 1323 for (i = 0; i < RAID_BDEV_STATE_MAX; i++) { 1324 if (strcasecmp(g_raid_state_names[i], str) == 0) { 1325 break; 1326 } 1327 } 1328 1329 return i; 1330 } 1331 1332 const char * 1333 raid_bdev_state_to_str(enum raid_bdev_state state) 1334 { 1335 if (state >= RAID_BDEV_STATE_MAX) { 1336 return ""; 1337 } 1338 1339 return g_raid_state_names[state]; 1340 } 1341 1342 const char * 1343 raid_bdev_process_to_str(enum raid_process_type value) 1344 { 1345 if (value >= RAID_PROCESS_MAX) { 1346 return ""; 1347 } 1348 1349 return g_raid_process_type_names[value]; 1350 } 1351 1352 /* 1353 * brief: 1354 * raid_bdev_fini_start is called when bdev layer is starting the 1355 * shutdown process 1356 * params: 1357 * none 1358 * returns: 1359 * none 1360 */ 1361 static void 1362 raid_bdev_fini_start(void) 1363 { 1364 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_fini_start\n"); 1365 g_shutdown_started = true; 1366 } 1367 1368 /* 1369 * brief: 1370 * raid_bdev_exit is called on raid bdev module exit time by bdev layer 1371 * params: 1372 * none 1373 * returns: 1374 * none 1375 */ 1376 static void 1377 raid_bdev_exit(void) 1378 { 1379 struct raid_bdev *raid_bdev, *tmp; 1380 1381 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_exit\n"); 1382 1383 TAILQ_FOREACH_SAFE(raid_bdev, &g_raid_bdev_list, global_link, tmp) { 1384 raid_bdev_cleanup_and_free(raid_bdev); 1385 } 1386 } 1387 1388 static void 1389 raid_bdev_opts_config_json(struct spdk_json_write_ctx *w) 1390 { 1391 spdk_json_write_object_begin(w); 1392 1393 spdk_json_write_named_string(w, "method", "bdev_raid_set_options"); 1394 1395 spdk_json_write_named_object_begin(w, "params"); 1396 spdk_json_write_named_uint32(w, "process_window_size_kb", g_opts.process_window_size_kb); 1397 spdk_json_write_object_end(w); 1398 1399 spdk_json_write_object_end(w); 1400 } 1401 1402 static int 1403 raid_bdev_config_json(struct spdk_json_write_ctx *w) 1404 { 1405 raid_bdev_opts_config_json(w); 1406 1407 return 0; 1408 } 1409 1410 /* 1411 * brief: 1412 * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid 1413 * module 1414 * params: 1415 * none 1416 * returns: 1417 * size of spdk_bdev_io context for raid 1418 */ 1419 static int 1420 raid_bdev_get_ctx_size(void) 1421 { 1422 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_get_ctx_size\n"); 1423 return sizeof(struct raid_bdev_io); 1424 } 1425 1426 static struct spdk_bdev_module g_raid_if = { 1427 .name = "raid", 1428 .module_init = raid_bdev_init, 1429 .fini_start = raid_bdev_fini_start, 1430 .module_fini = raid_bdev_exit, 1431 .config_json = raid_bdev_config_json, 1432 .get_ctx_size = raid_bdev_get_ctx_size, 1433 .examine_disk = raid_bdev_examine, 1434 .async_init = false, 1435 .async_fini = false, 1436 }; 1437 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if) 1438 1439 /* 1440 * brief: 1441 * raid_bdev_init is the initialization function for raid bdev module 1442 * params: 1443 * none 1444 * returns: 1445 * 0 - success 1446 * non zero - failure 1447 */ 1448 static int 1449 raid_bdev_init(void) 1450 { 1451 return 0; 1452 } 1453 1454 static int 1455 _raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs, 1456 enum raid_level level, bool superblock_enabled, const struct spdk_uuid *uuid, 1457 struct raid_bdev **raid_bdev_out) 1458 { 1459 struct raid_bdev *raid_bdev; 1460 struct spdk_bdev *raid_bdev_gen; 1461 struct raid_bdev_module *module; 1462 struct raid_base_bdev_info *base_info; 1463 uint8_t min_operational; 1464 1465 if (strnlen(name, RAID_BDEV_SB_NAME_SIZE) == RAID_BDEV_SB_NAME_SIZE) { 1466 SPDK_ERRLOG("Raid bdev name '%s' exceeds %d characters\n", name, RAID_BDEV_SB_NAME_SIZE - 1); 1467 return -EINVAL; 1468 } 1469 1470 if (raid_bdev_find_by_name(name) != NULL) { 1471 SPDK_ERRLOG("Duplicate raid bdev name found: %s\n", name); 1472 return -EEXIST; 1473 } 1474 1475 if (level == RAID1) { 1476 if (strip_size != 0) { 1477 SPDK_ERRLOG("Strip size is not supported by raid1\n"); 1478 return -EINVAL; 1479 } 1480 } else if (spdk_u32_is_pow2(strip_size) == false) { 1481 SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size); 1482 return -EINVAL; 1483 } 1484 1485 module = raid_bdev_module_find(level); 1486 if (module == NULL) { 1487 SPDK_ERRLOG("Unsupported raid level '%d'\n", level); 1488 return -EINVAL; 1489 } 1490 1491 assert(module->base_bdevs_min != 0); 1492 if (num_base_bdevs < module->base_bdevs_min) { 1493 SPDK_ERRLOG("At least %u base devices required for %s\n", 1494 module->base_bdevs_min, 1495 raid_bdev_level_to_str(level)); 1496 return -EINVAL; 1497 } 1498 1499 switch (module->base_bdevs_constraint.type) { 1500 case CONSTRAINT_MAX_BASE_BDEVS_REMOVED: 1501 min_operational = num_base_bdevs - module->base_bdevs_constraint.value; 1502 break; 1503 case CONSTRAINT_MIN_BASE_BDEVS_OPERATIONAL: 1504 min_operational = module->base_bdevs_constraint.value; 1505 break; 1506 case CONSTRAINT_UNSET: 1507 if (module->base_bdevs_constraint.value != 0) { 1508 SPDK_ERRLOG("Unexpected constraint value '%u' provided for raid bdev '%s'.\n", 1509 (uint8_t)module->base_bdevs_constraint.value, name); 1510 return -EINVAL; 1511 } 1512 min_operational = num_base_bdevs; 1513 break; 1514 default: 1515 SPDK_ERRLOG("Unrecognised constraint type '%u' in module for raid level '%s'.\n", 1516 (uint8_t)module->base_bdevs_constraint.type, 1517 raid_bdev_level_to_str(module->level)); 1518 return -EINVAL; 1519 }; 1520 1521 if (min_operational == 0 || min_operational > num_base_bdevs) { 1522 SPDK_ERRLOG("Wrong constraint value for raid level '%s'.\n", 1523 raid_bdev_level_to_str(module->level)); 1524 return -EINVAL; 1525 } 1526 1527 raid_bdev = calloc(1, sizeof(*raid_bdev)); 1528 if (!raid_bdev) { 1529 SPDK_ERRLOG("Unable to allocate memory for raid bdev\n"); 1530 return -ENOMEM; 1531 } 1532 1533 spdk_spin_init(&raid_bdev->base_bdev_lock); 1534 raid_bdev->module = module; 1535 raid_bdev->num_base_bdevs = num_base_bdevs; 1536 raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs, 1537 sizeof(struct raid_base_bdev_info)); 1538 if (!raid_bdev->base_bdev_info) { 1539 SPDK_ERRLOG("Unable able to allocate base bdev info\n"); 1540 raid_bdev_free(raid_bdev); 1541 return -ENOMEM; 1542 } 1543 1544 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1545 base_info->raid_bdev = raid_bdev; 1546 } 1547 1548 /* strip_size_kb is from the rpc param. strip_size is in blocks and used 1549 * internally and set later. 1550 */ 1551 raid_bdev->strip_size = 0; 1552 raid_bdev->strip_size_kb = strip_size; 1553 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1554 raid_bdev->level = level; 1555 raid_bdev->min_base_bdevs_operational = min_operational; 1556 raid_bdev->superblock_enabled = superblock_enabled; 1557 1558 raid_bdev_gen = &raid_bdev->bdev; 1559 1560 raid_bdev_gen->name = strdup(name); 1561 if (!raid_bdev_gen->name) { 1562 SPDK_ERRLOG("Unable to allocate name for raid\n"); 1563 raid_bdev_free(raid_bdev); 1564 return -ENOMEM; 1565 } 1566 1567 raid_bdev_gen->product_name = "Raid Volume"; 1568 raid_bdev_gen->ctxt = raid_bdev; 1569 raid_bdev_gen->fn_table = &g_raid_bdev_fn_table; 1570 raid_bdev_gen->module = &g_raid_if; 1571 raid_bdev_gen->write_cache = 0; 1572 spdk_uuid_copy(&raid_bdev_gen->uuid, uuid); 1573 1574 TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link); 1575 1576 *raid_bdev_out = raid_bdev; 1577 1578 return 0; 1579 } 1580 1581 /* 1582 * brief: 1583 * raid_bdev_create allocates raid bdev based on passed configuration 1584 * params: 1585 * name - name for raid bdev 1586 * strip_size - strip size in KB 1587 * num_base_bdevs - number of base bdevs 1588 * level - raid level 1589 * superblock_enabled - true if raid should have superblock 1590 * uuid - uuid to set for the bdev 1591 * raid_bdev_out - the created raid bdev 1592 * returns: 1593 * 0 - success 1594 * non zero - failure 1595 */ 1596 int 1597 raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs, 1598 enum raid_level level, bool superblock_enabled, const struct spdk_uuid *uuid, 1599 struct raid_bdev **raid_bdev_out) 1600 { 1601 struct raid_bdev *raid_bdev; 1602 int rc; 1603 1604 assert(uuid != NULL); 1605 1606 rc = _raid_bdev_create(name, strip_size, num_base_bdevs, level, superblock_enabled, uuid, 1607 &raid_bdev); 1608 if (rc != 0) { 1609 return rc; 1610 } 1611 1612 if (superblock_enabled && spdk_uuid_is_null(uuid)) { 1613 /* we need to have the uuid to store in the superblock before the bdev is registered */ 1614 spdk_uuid_generate(&raid_bdev->bdev.uuid); 1615 } 1616 1617 raid_bdev->num_base_bdevs_operational = num_base_bdevs; 1618 1619 *raid_bdev_out = raid_bdev; 1620 1621 return 0; 1622 } 1623 1624 static void 1625 _raid_bdev_unregistering_cont(void *ctx) 1626 { 1627 struct raid_bdev *raid_bdev = ctx; 1628 1629 spdk_bdev_close(raid_bdev->self_desc); 1630 raid_bdev->self_desc = NULL; 1631 } 1632 1633 static void 1634 raid_bdev_unregistering_cont(void *ctx) 1635 { 1636 spdk_thread_exec_msg(spdk_thread_get_app_thread(), _raid_bdev_unregistering_cont, ctx); 1637 } 1638 1639 static int 1640 raid_bdev_process_add_finish_action(struct raid_bdev_process *process, spdk_msg_fn cb, void *cb_ctx) 1641 { 1642 struct raid_process_finish_action *finish_action; 1643 1644 assert(spdk_get_thread() == process->thread); 1645 assert(process->state < RAID_PROCESS_STATE_STOPPED); 1646 1647 finish_action = calloc(1, sizeof(*finish_action)); 1648 if (finish_action == NULL) { 1649 return -ENOMEM; 1650 } 1651 1652 finish_action->cb = cb; 1653 finish_action->cb_ctx = cb_ctx; 1654 1655 TAILQ_INSERT_TAIL(&process->finish_actions, finish_action, link); 1656 1657 return 0; 1658 } 1659 1660 static void 1661 raid_bdev_unregistering_stop_process(void *ctx) 1662 { 1663 struct raid_bdev_process *process = ctx; 1664 struct raid_bdev *raid_bdev = process->raid_bdev; 1665 int rc; 1666 1667 process->state = RAID_PROCESS_STATE_STOPPING; 1668 if (process->status == 0) { 1669 process->status = -ECANCELED; 1670 } 1671 1672 rc = raid_bdev_process_add_finish_action(process, raid_bdev_unregistering_cont, raid_bdev); 1673 if (rc != 0) { 1674 SPDK_ERRLOG("Failed to add raid bdev '%s' process finish action: %s\n", 1675 raid_bdev->bdev.name, spdk_strerror(-rc)); 1676 } 1677 } 1678 1679 static void 1680 raid_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 1681 { 1682 struct raid_bdev *raid_bdev = event_ctx; 1683 1684 switch (type) { 1685 case SPDK_BDEV_EVENT_REMOVE: 1686 if (raid_bdev->process != NULL) { 1687 spdk_thread_send_msg(raid_bdev->process->thread, raid_bdev_unregistering_stop_process, 1688 raid_bdev->process); 1689 } else { 1690 raid_bdev_unregistering_cont(raid_bdev); 1691 } 1692 break; 1693 default: 1694 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 1695 break; 1696 } 1697 } 1698 1699 static void 1700 raid_bdev_configure_cont(struct raid_bdev *raid_bdev) 1701 { 1702 struct spdk_bdev *raid_bdev_gen = &raid_bdev->bdev; 1703 int rc; 1704 1705 raid_bdev->state = RAID_BDEV_STATE_ONLINE; 1706 SPDK_DEBUGLOG(bdev_raid, "io device register %p\n", raid_bdev); 1707 SPDK_DEBUGLOG(bdev_raid, "blockcnt %" PRIu64 ", blocklen %u\n", 1708 raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen); 1709 spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb, 1710 sizeof(struct raid_bdev_io_channel), 1711 raid_bdev_gen->name); 1712 rc = spdk_bdev_register(raid_bdev_gen); 1713 if (rc != 0) { 1714 SPDK_ERRLOG("Failed to register raid bdev '%s': %s\n", 1715 raid_bdev_gen->name, spdk_strerror(-rc)); 1716 goto err; 1717 } 1718 1719 /* 1720 * Open the bdev internally to delay unregistering if we need to stop a background process 1721 * first. The process may still need to unquiesce a range but it will fail because the 1722 * bdev's internal.spinlock is destroyed by the time the destruct callback is reached. 1723 * During application shutdown, bdevs automatically get unregistered by the bdev layer 1724 * so this is the only way currently to do this correctly. 1725 * TODO: try to handle this correctly in bdev layer instead. 1726 */ 1727 rc = spdk_bdev_open_ext(raid_bdev_gen->name, false, raid_bdev_event_cb, raid_bdev, 1728 &raid_bdev->self_desc); 1729 if (rc != 0) { 1730 SPDK_ERRLOG("Failed to open raid bdev '%s': %s\n", 1731 raid_bdev_gen->name, spdk_strerror(-rc)); 1732 spdk_bdev_unregister(raid_bdev_gen, NULL, NULL); 1733 goto err; 1734 } 1735 1736 SPDK_DEBUGLOG(bdev_raid, "raid bdev generic %p\n", raid_bdev_gen); 1737 SPDK_DEBUGLOG(bdev_raid, "raid bdev is created with name %s, raid_bdev %p\n", 1738 raid_bdev_gen->name, raid_bdev); 1739 return; 1740 err: 1741 if (raid_bdev->module->stop != NULL) { 1742 raid_bdev->module->stop(raid_bdev); 1743 } 1744 spdk_io_device_unregister(raid_bdev, NULL); 1745 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1746 } 1747 1748 static void 1749 raid_bdev_configure_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx) 1750 { 1751 if (status == 0) { 1752 raid_bdev_configure_cont(raid_bdev); 1753 } else { 1754 SPDK_ERRLOG("Failed to write raid bdev '%s' superblock: %s\n", 1755 raid_bdev->bdev.name, spdk_strerror(-status)); 1756 if (raid_bdev->module->stop != NULL) { 1757 raid_bdev->module->stop(raid_bdev); 1758 } 1759 } 1760 } 1761 1762 /* 1763 * brief: 1764 * If raid bdev config is complete, then only register the raid bdev to 1765 * bdev layer and remove this raid bdev from configuring list and 1766 * insert the raid bdev to configured list 1767 * params: 1768 * raid_bdev - pointer to raid bdev 1769 * returns: 1770 * 0 - success 1771 * non zero - failure 1772 */ 1773 static int 1774 raid_bdev_configure(struct raid_bdev *raid_bdev) 1775 { 1776 uint32_t data_block_size = spdk_bdev_get_data_block_size(&raid_bdev->bdev); 1777 int rc; 1778 1779 assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING); 1780 assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs_operational); 1781 assert(raid_bdev->bdev.blocklen > 0); 1782 1783 /* The strip_size_kb is read in from user in KB. Convert to blocks here for 1784 * internal use. 1785 */ 1786 raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / data_block_size; 1787 if (raid_bdev->strip_size == 0 && raid_bdev->level != RAID1) { 1788 SPDK_ERRLOG("Strip size cannot be smaller than the device block size\n"); 1789 return -EINVAL; 1790 } 1791 raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size); 1792 raid_bdev->blocklen_shift = spdk_u32log2(data_block_size); 1793 1794 rc = raid_bdev->module->start(raid_bdev); 1795 if (rc != 0) { 1796 SPDK_ERRLOG("raid module startup callback failed\n"); 1797 return rc; 1798 } 1799 1800 if (raid_bdev->superblock_enabled) { 1801 if (raid_bdev->sb == NULL) { 1802 rc = raid_bdev_alloc_superblock(raid_bdev, data_block_size); 1803 if (rc == 0) { 1804 raid_bdev_init_superblock(raid_bdev); 1805 } 1806 } else { 1807 assert(spdk_uuid_compare(&raid_bdev->sb->uuid, &raid_bdev->bdev.uuid) == 0); 1808 if (raid_bdev->sb->block_size != data_block_size) { 1809 SPDK_ERRLOG("blocklen does not match value in superblock\n"); 1810 rc = -EINVAL; 1811 } 1812 if (raid_bdev->sb->raid_size != raid_bdev->bdev.blockcnt) { 1813 SPDK_ERRLOG("blockcnt does not match value in superblock\n"); 1814 rc = -EINVAL; 1815 } 1816 } 1817 1818 if (rc != 0) { 1819 if (raid_bdev->module->stop != NULL) { 1820 raid_bdev->module->stop(raid_bdev); 1821 } 1822 return rc; 1823 } 1824 1825 raid_bdev_write_superblock(raid_bdev, raid_bdev_configure_write_sb_cb, NULL); 1826 } else { 1827 raid_bdev_configure_cont(raid_bdev); 1828 } 1829 1830 return 0; 1831 } 1832 1833 /* 1834 * brief: 1835 * If raid bdev is online and registered, change the bdev state to 1836 * configuring and unregister this raid device. Queue this raid device 1837 * in configuring list 1838 * params: 1839 * raid_bdev - pointer to raid bdev 1840 * cb_fn - callback function 1841 * cb_arg - argument to callback function 1842 * returns: 1843 * none 1844 */ 1845 static void 1846 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, 1847 void *cb_arg) 1848 { 1849 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 1850 if (cb_fn) { 1851 cb_fn(cb_arg, 0); 1852 } 1853 return; 1854 } 1855 1856 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 1857 assert(raid_bdev->num_base_bdevs_discovered); 1858 SPDK_DEBUGLOG(bdev_raid, "raid bdev state changing from online to offline\n"); 1859 1860 spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg); 1861 } 1862 1863 /* 1864 * brief: 1865 * raid_bdev_find_base_info_by_bdev function finds the base bdev info by bdev. 1866 * params: 1867 * base_bdev - pointer to base bdev 1868 * returns: 1869 * base bdev info if found, otherwise NULL. 1870 */ 1871 static struct raid_base_bdev_info * 1872 raid_bdev_find_base_info_by_bdev(struct spdk_bdev *base_bdev) 1873 { 1874 struct raid_bdev *raid_bdev; 1875 struct raid_base_bdev_info *base_info; 1876 1877 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 1878 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1879 if (base_info->desc != NULL && 1880 spdk_bdev_desc_get_bdev(base_info->desc) == base_bdev) { 1881 return base_info; 1882 } 1883 } 1884 } 1885 1886 return NULL; 1887 } 1888 1889 static void 1890 raid_bdev_remove_base_bdev_done(struct raid_base_bdev_info *base_info, int status) 1891 { 1892 assert(base_info->remove_scheduled); 1893 1894 base_info->remove_scheduled = false; 1895 if (base_info->remove_cb != NULL) { 1896 base_info->remove_cb(base_info->remove_cb_ctx, status); 1897 } 1898 } 1899 1900 static void 1901 raid_bdev_remove_base_bdev_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx) 1902 { 1903 struct raid_base_bdev_info *base_info = ctx; 1904 1905 if (status != 0) { 1906 SPDK_ERRLOG("Failed to write raid bdev '%s' superblock: %s\n", 1907 raid_bdev->bdev.name, spdk_strerror(-status)); 1908 } 1909 1910 raid_bdev_remove_base_bdev_done(base_info, status); 1911 } 1912 1913 static void 1914 raid_bdev_remove_base_bdev_on_unquiesced(void *ctx, int status) 1915 { 1916 struct raid_base_bdev_info *base_info = ctx; 1917 struct raid_bdev *raid_bdev = base_info->raid_bdev; 1918 1919 if (status != 0) { 1920 SPDK_ERRLOG("Failed to unquiesce raid bdev %s: %s\n", 1921 raid_bdev->bdev.name, spdk_strerror(-status)); 1922 goto out; 1923 } 1924 1925 spdk_spin_lock(&raid_bdev->base_bdev_lock); 1926 raid_bdev_free_base_bdev_resource(base_info); 1927 spdk_spin_unlock(&raid_bdev->base_bdev_lock); 1928 1929 if (raid_bdev->sb) { 1930 struct raid_bdev_superblock *sb = raid_bdev->sb; 1931 uint8_t slot = raid_bdev_base_bdev_slot(base_info); 1932 uint8_t i; 1933 1934 for (i = 0; i < sb->base_bdevs_size; i++) { 1935 struct raid_bdev_sb_base_bdev *sb_base_bdev = &sb->base_bdevs[i]; 1936 1937 if (sb_base_bdev->state == RAID_SB_BASE_BDEV_CONFIGURED && 1938 sb_base_bdev->slot == slot) { 1939 /* TODO: distinguish between failure and intentional removal */ 1940 sb_base_bdev->state = RAID_SB_BASE_BDEV_FAILED; 1941 1942 raid_bdev_write_superblock(raid_bdev, raid_bdev_remove_base_bdev_write_sb_cb, base_info); 1943 return; 1944 } 1945 } 1946 } 1947 out: 1948 raid_bdev_remove_base_bdev_done(base_info, status); 1949 } 1950 1951 static void 1952 raid_bdev_channel_remove_base_bdev(struct spdk_io_channel_iter *i) 1953 { 1954 struct raid_base_bdev_info *base_info = spdk_io_channel_iter_get_ctx(i); 1955 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 1956 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 1957 uint8_t idx = raid_bdev_base_bdev_slot(base_info); 1958 1959 SPDK_DEBUGLOG(bdev_raid, "slot: %u raid_ch: %p\n", idx, raid_ch); 1960 1961 if (raid_ch->base_channel[idx] != NULL) { 1962 spdk_put_io_channel(raid_ch->base_channel[idx]); 1963 raid_ch->base_channel[idx] = NULL; 1964 } 1965 1966 if (raid_ch->process.ch_processed != NULL) { 1967 raid_ch->process.ch_processed->base_channel[idx] = NULL; 1968 } 1969 1970 spdk_for_each_channel_continue(i, 0); 1971 } 1972 1973 static void 1974 raid_bdev_channels_remove_base_bdev_done(struct spdk_io_channel_iter *i, int status) 1975 { 1976 struct raid_base_bdev_info *base_info = spdk_io_channel_iter_get_ctx(i); 1977 struct raid_bdev *raid_bdev = base_info->raid_bdev; 1978 1979 spdk_bdev_unquiesce(&raid_bdev->bdev, &g_raid_if, raid_bdev_remove_base_bdev_on_unquiesced, 1980 base_info); 1981 } 1982 1983 static void 1984 raid_bdev_remove_base_bdev_on_quiesced(void *ctx, int status) 1985 { 1986 struct raid_base_bdev_info *base_info = ctx; 1987 struct raid_bdev *raid_bdev = base_info->raid_bdev; 1988 1989 if (status != 0) { 1990 SPDK_ERRLOG("Failed to quiesce raid bdev %s: %s\n", 1991 raid_bdev->bdev.name, spdk_strerror(-status)); 1992 raid_bdev_remove_base_bdev_done(base_info, status); 1993 return; 1994 } 1995 1996 spdk_for_each_channel(raid_bdev, raid_bdev_channel_remove_base_bdev, base_info, 1997 raid_bdev_channels_remove_base_bdev_done); 1998 } 1999 2000 static int 2001 raid_bdev_remove_base_bdev_quiesce(struct raid_base_bdev_info *base_info) 2002 { 2003 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2004 2005 return spdk_bdev_quiesce(&base_info->raid_bdev->bdev, &g_raid_if, 2006 raid_bdev_remove_base_bdev_on_quiesced, base_info); 2007 } 2008 2009 struct raid_bdev_process_base_bdev_remove_ctx { 2010 struct raid_bdev_process *process; 2011 struct raid_base_bdev_info *base_info; 2012 uint8_t num_base_bdevs_operational; 2013 }; 2014 2015 static void 2016 _raid_bdev_process_base_bdev_remove_cont(void *ctx) 2017 { 2018 struct raid_base_bdev_info *base_info = ctx; 2019 int ret; 2020 2021 ret = raid_bdev_remove_base_bdev_quiesce(base_info); 2022 if (ret != 0) { 2023 raid_bdev_remove_base_bdev_done(base_info, ret); 2024 } 2025 } 2026 2027 static void 2028 raid_bdev_process_base_bdev_remove_cont(void *_ctx) 2029 { 2030 struct raid_bdev_process_base_bdev_remove_ctx *ctx = _ctx; 2031 struct raid_base_bdev_info *base_info = ctx->base_info; 2032 2033 free(ctx); 2034 2035 spdk_thread_send_msg(spdk_thread_get_app_thread(), _raid_bdev_process_base_bdev_remove_cont, 2036 base_info); 2037 } 2038 2039 static void 2040 _raid_bdev_process_base_bdev_remove(void *_ctx) 2041 { 2042 struct raid_bdev_process_base_bdev_remove_ctx *ctx = _ctx; 2043 struct raid_bdev_process *process = ctx->process; 2044 int ret; 2045 2046 if (ctx->base_info != process->target && 2047 ctx->num_base_bdevs_operational > process->raid_bdev->min_base_bdevs_operational) { 2048 /* process doesn't need to be stopped */ 2049 raid_bdev_process_base_bdev_remove_cont(ctx); 2050 return; 2051 } 2052 2053 assert(process->state > RAID_PROCESS_STATE_INIT && 2054 process->state < RAID_PROCESS_STATE_STOPPED); 2055 2056 ret = raid_bdev_process_add_finish_action(process, raid_bdev_process_base_bdev_remove_cont, ctx); 2057 if (ret != 0) { 2058 raid_bdev_remove_base_bdev_done(ctx->base_info, ret); 2059 free(ctx); 2060 return; 2061 } 2062 2063 process->state = RAID_PROCESS_STATE_STOPPING; 2064 2065 if (process->status == 0) { 2066 process->status = -ENODEV; 2067 } 2068 } 2069 2070 static int 2071 raid_bdev_process_base_bdev_remove(struct raid_bdev_process *process, 2072 struct raid_base_bdev_info *base_info) 2073 { 2074 struct raid_bdev_process_base_bdev_remove_ctx *ctx; 2075 2076 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2077 2078 ctx = calloc(1, sizeof(*ctx)); 2079 if (ctx == NULL) { 2080 return -ENOMEM; 2081 } 2082 2083 /* 2084 * We have to send the process and num_base_bdevs_operational in the message ctx 2085 * because the process thread should not access raid_bdev's properties. Particularly, 2086 * raid_bdev->process may be cleared by the time the message is handled, but ctx->process 2087 * will still be valid until the process is fully stopped. 2088 */ 2089 ctx->base_info = base_info; 2090 ctx->process = process; 2091 ctx->num_base_bdevs_operational = process->raid_bdev->num_base_bdevs_operational; 2092 2093 spdk_thread_send_msg(process->thread, _raid_bdev_process_base_bdev_remove, ctx); 2094 2095 return 0; 2096 } 2097 2098 static int 2099 _raid_bdev_remove_base_bdev(struct raid_base_bdev_info *base_info, 2100 raid_base_bdev_cb cb_fn, void *cb_ctx) 2101 { 2102 struct raid_bdev *raid_bdev = base_info->raid_bdev; 2103 int ret = 0; 2104 2105 SPDK_DEBUGLOG(bdev_raid, "%s\n", base_info->name); 2106 2107 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2108 2109 if (base_info->remove_scheduled) { 2110 return -ENODEV; 2111 } 2112 2113 assert(base_info->desc); 2114 base_info->remove_scheduled = true; 2115 base_info->remove_cb = cb_fn; 2116 base_info->remove_cb_ctx = cb_ctx; 2117 2118 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 2119 /* 2120 * As raid bdev is not registered yet or already unregistered, 2121 * so cleanup should be done here itself. 2122 * 2123 * Removing a base bdev at this stage does not change the number of operational 2124 * base bdevs, only the number of discovered base bdevs. 2125 */ 2126 raid_bdev_free_base_bdev_resource(base_info); 2127 if (raid_bdev->num_base_bdevs_discovered == 0) { 2128 /* There is no base bdev for this raid, so free the raid device. */ 2129 raid_bdev_cleanup_and_free(raid_bdev); 2130 } 2131 } else if (raid_bdev->num_base_bdevs_operational-- == raid_bdev->min_base_bdevs_operational) { 2132 /* 2133 * After this base bdev is removed there will not be enough base bdevs 2134 * to keep the raid bdev operational. 2135 */ 2136 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_ctx); 2137 } else if (raid_bdev->process != NULL) { 2138 ret = raid_bdev_process_base_bdev_remove(raid_bdev->process, base_info); 2139 } else { 2140 ret = raid_bdev_remove_base_bdev_quiesce(base_info); 2141 } 2142 2143 if (ret != 0) { 2144 base_info->remove_scheduled = false; 2145 } 2146 return ret; 2147 } 2148 2149 /* 2150 * brief: 2151 * raid_bdev_remove_base_bdev function is called by below layers when base_bdev 2152 * is removed. This function checks if this base bdev is part of any raid bdev 2153 * or not. If yes, it takes necessary action on that particular raid bdev. 2154 * params: 2155 * base_bdev - pointer to base bdev which got removed 2156 * cb_fn - callback function 2157 * cb_arg - argument to callback function 2158 * returns: 2159 * 0 - success 2160 * non zero - failure 2161 */ 2162 int 2163 raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev, raid_base_bdev_cb cb_fn, void *cb_ctx) 2164 { 2165 struct raid_base_bdev_info *base_info; 2166 2167 /* Find the raid_bdev which has claimed this base_bdev */ 2168 base_info = raid_bdev_find_base_info_by_bdev(base_bdev); 2169 if (!base_info) { 2170 SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name); 2171 return -ENODEV; 2172 } 2173 2174 return _raid_bdev_remove_base_bdev(base_info, cb_fn, cb_ctx); 2175 } 2176 2177 /* 2178 * brief: 2179 * raid_bdev_resize_base_bdev function is called by below layers when base_bdev 2180 * is resized. This function checks if the smallest size of the base_bdevs is changed. 2181 * If yes, call module handler to resize the raid_bdev if implemented. 2182 * params: 2183 * base_bdev - pointer to base bdev which got resized. 2184 * returns: 2185 * none 2186 */ 2187 static void 2188 raid_bdev_resize_base_bdev(struct spdk_bdev *base_bdev) 2189 { 2190 struct raid_bdev *raid_bdev; 2191 struct raid_base_bdev_info *base_info; 2192 2193 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_resize_base_bdev\n"); 2194 2195 base_info = raid_bdev_find_base_info_by_bdev(base_bdev); 2196 2197 /* Find the raid_bdev which has claimed this base_bdev */ 2198 if (!base_info) { 2199 SPDK_ERRLOG("raid_bdev whose base_bdev '%s' not found\n", base_bdev->name); 2200 return; 2201 } 2202 raid_bdev = base_info->raid_bdev; 2203 2204 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2205 2206 SPDK_NOTICELOG("base_bdev '%s' was resized: old size %" PRIu64 ", new size %" PRIu64 "\n", 2207 base_bdev->name, base_info->blockcnt, base_bdev->blockcnt); 2208 2209 if (raid_bdev->module->resize) { 2210 raid_bdev->module->resize(raid_bdev); 2211 } 2212 } 2213 2214 /* 2215 * brief: 2216 * raid_bdev_event_base_bdev function is called by below layers when base_bdev 2217 * triggers asynchronous event. 2218 * params: 2219 * type - event details. 2220 * bdev - bdev that triggered event. 2221 * event_ctx - context for event. 2222 * returns: 2223 * none 2224 */ 2225 static void 2226 raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 2227 void *event_ctx) 2228 { 2229 int rc; 2230 2231 switch (type) { 2232 case SPDK_BDEV_EVENT_REMOVE: 2233 rc = raid_bdev_remove_base_bdev(bdev, NULL, NULL); 2234 if (rc != 0) { 2235 SPDK_ERRLOG("Failed to remove base bdev %s: %s\n", 2236 spdk_bdev_get_name(bdev), spdk_strerror(-rc)); 2237 } 2238 break; 2239 case SPDK_BDEV_EVENT_RESIZE: 2240 raid_bdev_resize_base_bdev(bdev); 2241 break; 2242 default: 2243 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 2244 break; 2245 } 2246 } 2247 2248 /* 2249 * brief: 2250 * Deletes the specified raid bdev 2251 * params: 2252 * raid_bdev - pointer to raid bdev 2253 * cb_fn - callback function 2254 * cb_arg - argument to callback function 2255 */ 2256 void 2257 raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_arg) 2258 { 2259 struct raid_base_bdev_info *base_info; 2260 2261 SPDK_DEBUGLOG(bdev_raid, "delete raid bdev: %s\n", raid_bdev->bdev.name); 2262 2263 if (raid_bdev->destroy_started) { 2264 SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n", 2265 raid_bdev->bdev.name); 2266 if (cb_fn) { 2267 cb_fn(cb_arg, -EALREADY); 2268 } 2269 return; 2270 } 2271 2272 raid_bdev->destroy_started = true; 2273 2274 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 2275 base_info->remove_scheduled = true; 2276 2277 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 2278 /* 2279 * As raid bdev is not registered yet or already unregistered, 2280 * so cleanup should be done here itself. 2281 */ 2282 raid_bdev_free_base_bdev_resource(base_info); 2283 } 2284 } 2285 2286 if (raid_bdev->num_base_bdevs_discovered == 0) { 2287 /* There is no base bdev for this raid, so free the raid device. */ 2288 raid_bdev_cleanup_and_free(raid_bdev); 2289 if (cb_fn) { 2290 cb_fn(cb_arg, 0); 2291 } 2292 } else { 2293 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg); 2294 } 2295 } 2296 2297 static void 2298 raid_bdev_process_finish_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx) 2299 { 2300 if (status != 0) { 2301 SPDK_ERRLOG("Failed to write raid bdev '%s' superblock after background process finished: %s\n", 2302 raid_bdev->bdev.name, spdk_strerror(-status)); 2303 } 2304 } 2305 2306 static void 2307 raid_bdev_process_finish_write_sb(void *ctx) 2308 { 2309 struct raid_bdev *raid_bdev = ctx; 2310 struct raid_bdev_superblock *sb = raid_bdev->sb; 2311 struct raid_bdev_sb_base_bdev *sb_base_bdev; 2312 struct raid_base_bdev_info *base_info; 2313 uint8_t i; 2314 2315 for (i = 0; i < sb->base_bdevs_size; i++) { 2316 sb_base_bdev = &sb->base_bdevs[i]; 2317 2318 if (sb_base_bdev->state != RAID_SB_BASE_BDEV_CONFIGURED && 2319 sb_base_bdev->slot < raid_bdev->num_base_bdevs) { 2320 base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot]; 2321 if (base_info->is_configured) { 2322 sb_base_bdev->state = RAID_SB_BASE_BDEV_CONFIGURED; 2323 spdk_uuid_copy(&sb_base_bdev->uuid, &base_info->uuid); 2324 } 2325 } 2326 } 2327 2328 raid_bdev_write_superblock(raid_bdev, raid_bdev_process_finish_write_sb_cb, NULL); 2329 } 2330 2331 static void raid_bdev_process_free(struct raid_bdev_process *process); 2332 2333 static void 2334 _raid_bdev_process_finish_done(void *ctx) 2335 { 2336 struct raid_bdev_process *process = ctx; 2337 struct raid_process_finish_action *finish_action; 2338 2339 while ((finish_action = TAILQ_FIRST(&process->finish_actions)) != NULL) { 2340 TAILQ_REMOVE(&process->finish_actions, finish_action, link); 2341 finish_action->cb(finish_action->cb_ctx); 2342 free(finish_action); 2343 } 2344 2345 raid_bdev_process_free(process); 2346 2347 spdk_thread_exit(spdk_get_thread()); 2348 } 2349 2350 static void 2351 raid_bdev_process_finish_target_removed(void *ctx, int status) 2352 { 2353 struct raid_bdev_process *process = ctx; 2354 2355 if (status != 0) { 2356 SPDK_ERRLOG("Failed to remove target bdev: %s\n", spdk_strerror(-status)); 2357 } 2358 2359 spdk_thread_send_msg(process->thread, _raid_bdev_process_finish_done, process); 2360 } 2361 2362 static void 2363 raid_bdev_process_finish_unquiesced(void *ctx, int status) 2364 { 2365 struct raid_bdev_process *process = ctx; 2366 2367 if (status != 0) { 2368 SPDK_ERRLOG("Failed to unquiesce bdev: %s\n", spdk_strerror(-status)); 2369 } 2370 2371 if (process->status != 0) { 2372 struct raid_base_bdev_info *target = process->target; 2373 2374 if (target->desc != NULL && target->remove_scheduled == false) { 2375 _raid_bdev_remove_base_bdev(target, raid_bdev_process_finish_target_removed, process); 2376 return; 2377 } 2378 } 2379 2380 spdk_thread_send_msg(process->thread, _raid_bdev_process_finish_done, process); 2381 } 2382 2383 static void 2384 raid_bdev_process_finish_unquiesce(void *ctx) 2385 { 2386 struct raid_bdev_process *process = ctx; 2387 int rc; 2388 2389 rc = spdk_bdev_unquiesce(&process->raid_bdev->bdev, &g_raid_if, 2390 raid_bdev_process_finish_unquiesced, process); 2391 if (rc != 0) { 2392 raid_bdev_process_finish_unquiesced(process, rc); 2393 } 2394 } 2395 2396 static void 2397 raid_bdev_process_finish_done(void *ctx) 2398 { 2399 struct raid_bdev_process *process = ctx; 2400 struct raid_bdev *raid_bdev = process->raid_bdev; 2401 2402 if (process->raid_ch != NULL) { 2403 spdk_put_io_channel(spdk_io_channel_from_ctx(process->raid_ch)); 2404 } 2405 2406 process->state = RAID_PROCESS_STATE_STOPPED; 2407 2408 if (process->status == 0) { 2409 SPDK_NOTICELOG("Finished %s on raid bdev %s\n", 2410 raid_bdev_process_to_str(process->type), 2411 raid_bdev->bdev.name); 2412 if (raid_bdev->superblock_enabled) { 2413 spdk_thread_send_msg(spdk_thread_get_app_thread(), 2414 raid_bdev_process_finish_write_sb, 2415 raid_bdev); 2416 } 2417 } else { 2418 SPDK_WARNLOG("Finished %s on raid bdev %s: %s\n", 2419 raid_bdev_process_to_str(process->type), 2420 raid_bdev->bdev.name, 2421 spdk_strerror(-process->status)); 2422 } 2423 2424 spdk_thread_send_msg(spdk_thread_get_app_thread(), raid_bdev_process_finish_unquiesce, 2425 process); 2426 } 2427 2428 static void 2429 __raid_bdev_process_finish(struct spdk_io_channel_iter *i, int status) 2430 { 2431 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2432 2433 spdk_thread_send_msg(process->thread, raid_bdev_process_finish_done, process); 2434 } 2435 2436 static void 2437 raid_bdev_channel_process_finish(struct spdk_io_channel_iter *i) 2438 { 2439 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2440 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2441 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2442 2443 if (process->status == 0) { 2444 uint8_t slot = raid_bdev_base_bdev_slot(process->target); 2445 2446 raid_ch->base_channel[slot] = raid_ch->process.target_ch; 2447 raid_ch->process.target_ch = NULL; 2448 } 2449 2450 raid_bdev_ch_process_cleanup(raid_ch); 2451 2452 spdk_for_each_channel_continue(i, 0); 2453 } 2454 2455 static void 2456 raid_bdev_process_finish_quiesced(void *ctx, int status) 2457 { 2458 struct raid_bdev_process *process = ctx; 2459 struct raid_bdev *raid_bdev = process->raid_bdev; 2460 2461 if (status != 0) { 2462 SPDK_ERRLOG("Failed to quiesce bdev: %s\n", spdk_strerror(-status)); 2463 return; 2464 } 2465 2466 raid_bdev->process = NULL; 2467 spdk_for_each_channel(process->raid_bdev, raid_bdev_channel_process_finish, process, 2468 __raid_bdev_process_finish); 2469 } 2470 2471 static void 2472 _raid_bdev_process_finish(void *ctx) 2473 { 2474 struct raid_bdev_process *process = ctx; 2475 int rc; 2476 2477 rc = spdk_bdev_quiesce(&process->raid_bdev->bdev, &g_raid_if, 2478 raid_bdev_process_finish_quiesced, process); 2479 if (rc != 0) { 2480 raid_bdev_process_finish_quiesced(ctx, rc); 2481 } 2482 } 2483 2484 static void 2485 raid_bdev_process_do_finish(struct raid_bdev_process *process) 2486 { 2487 spdk_thread_send_msg(spdk_thread_get_app_thread(), _raid_bdev_process_finish, process); 2488 } 2489 2490 static void raid_bdev_process_unlock_window_range(struct raid_bdev_process *process); 2491 static void raid_bdev_process_thread_run(struct raid_bdev_process *process); 2492 2493 static void 2494 raid_bdev_process_finish(struct raid_bdev_process *process, int status) 2495 { 2496 assert(spdk_get_thread() == process->thread); 2497 2498 if (process->status == 0) { 2499 process->status = status; 2500 } 2501 2502 if (process->state >= RAID_PROCESS_STATE_STOPPING) { 2503 return; 2504 } 2505 2506 assert(process->state == RAID_PROCESS_STATE_RUNNING); 2507 process->state = RAID_PROCESS_STATE_STOPPING; 2508 2509 if (process->window_range_locked) { 2510 raid_bdev_process_unlock_window_range(process); 2511 } else { 2512 raid_bdev_process_thread_run(process); 2513 } 2514 } 2515 2516 static void 2517 raid_bdev_process_window_range_unlocked(void *ctx, int status) 2518 { 2519 struct raid_bdev_process *process = ctx; 2520 2521 if (status != 0) { 2522 SPDK_ERRLOG("Failed to unlock LBA range: %s\n", spdk_strerror(-status)); 2523 raid_bdev_process_finish(process, status); 2524 return; 2525 } 2526 2527 process->window_range_locked = false; 2528 process->window_offset += process->window_size; 2529 2530 raid_bdev_process_thread_run(process); 2531 } 2532 2533 static void 2534 raid_bdev_process_unlock_window_range(struct raid_bdev_process *process) 2535 { 2536 int rc; 2537 2538 assert(process->window_range_locked == true); 2539 2540 rc = spdk_bdev_unquiesce_range(&process->raid_bdev->bdev, &g_raid_if, 2541 process->window_offset, process->max_window_size, 2542 raid_bdev_process_window_range_unlocked, process); 2543 if (rc != 0) { 2544 raid_bdev_process_window_range_unlocked(process, rc); 2545 } 2546 } 2547 2548 static void 2549 raid_bdev_process_channels_update_done(struct spdk_io_channel_iter *i, int status) 2550 { 2551 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2552 2553 raid_bdev_process_unlock_window_range(process); 2554 } 2555 2556 static void 2557 raid_bdev_process_channel_update(struct spdk_io_channel_iter *i) 2558 { 2559 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2560 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2561 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2562 2563 raid_ch->process.offset = process->window_offset + process->window_size; 2564 2565 spdk_for_each_channel_continue(i, 0); 2566 } 2567 2568 void 2569 raid_bdev_process_request_complete(struct raid_bdev_process_request *process_req, int status) 2570 { 2571 struct raid_bdev_process *process = process_req->process; 2572 2573 TAILQ_INSERT_TAIL(&process->requests, process_req, link); 2574 2575 assert(spdk_get_thread() == process->thread); 2576 assert(process->window_remaining >= process_req->num_blocks); 2577 2578 if (status != 0) { 2579 process->window_status = status; 2580 } 2581 2582 process->window_remaining -= process_req->num_blocks; 2583 if (process->window_remaining == 0) { 2584 if (process->window_status != 0) { 2585 raid_bdev_process_finish(process, process->window_status); 2586 return; 2587 } 2588 2589 spdk_for_each_channel(process->raid_bdev, raid_bdev_process_channel_update, process, 2590 raid_bdev_process_channels_update_done); 2591 } 2592 } 2593 2594 static int 2595 raid_bdev_submit_process_request(struct raid_bdev_process *process, uint64_t offset_blocks, 2596 uint32_t num_blocks) 2597 { 2598 struct raid_bdev *raid_bdev = process->raid_bdev; 2599 struct raid_bdev_process_request *process_req; 2600 int ret; 2601 2602 process_req = TAILQ_FIRST(&process->requests); 2603 if (process_req == NULL) { 2604 assert(process->window_remaining > 0); 2605 return 0; 2606 } 2607 2608 process_req->target = process->target; 2609 process_req->target_ch = process->raid_ch->process.target_ch; 2610 process_req->offset_blocks = offset_blocks; 2611 process_req->num_blocks = num_blocks; 2612 process_req->iov.iov_len = num_blocks * raid_bdev->bdev.blocklen; 2613 2614 ret = raid_bdev->module->submit_process_request(process_req, process->raid_ch); 2615 if (ret <= 0) { 2616 if (ret < 0) { 2617 SPDK_ERRLOG("Failed to submit process request on %s: %s\n", 2618 raid_bdev->bdev.name, spdk_strerror(-ret)); 2619 process->window_status = ret; 2620 } 2621 return ret; 2622 } 2623 2624 process_req->num_blocks = ret; 2625 TAILQ_REMOVE(&process->requests, process_req, link); 2626 2627 return ret; 2628 } 2629 2630 static void 2631 _raid_bdev_process_thread_run(struct raid_bdev_process *process) 2632 { 2633 struct raid_bdev *raid_bdev = process->raid_bdev; 2634 uint64_t offset = process->window_offset; 2635 const uint64_t offset_end = spdk_min(offset + process->max_window_size, raid_bdev->bdev.blockcnt); 2636 int ret; 2637 2638 while (offset < offset_end) { 2639 ret = raid_bdev_submit_process_request(process, offset, offset_end - offset); 2640 if (ret <= 0) { 2641 break; 2642 } 2643 2644 process->window_remaining += ret; 2645 offset += ret; 2646 } 2647 2648 if (process->window_remaining > 0) { 2649 process->window_size = process->window_remaining; 2650 } else { 2651 raid_bdev_process_finish(process, process->window_status); 2652 } 2653 } 2654 2655 static void 2656 raid_bdev_process_window_range_locked(void *ctx, int status) 2657 { 2658 struct raid_bdev_process *process = ctx; 2659 2660 if (status != 0) { 2661 SPDK_ERRLOG("Failed to lock LBA range: %s\n", spdk_strerror(-status)); 2662 raid_bdev_process_finish(process, status); 2663 return; 2664 } 2665 2666 process->window_range_locked = true; 2667 2668 if (process->state == RAID_PROCESS_STATE_STOPPING) { 2669 raid_bdev_process_unlock_window_range(process); 2670 return; 2671 } 2672 2673 _raid_bdev_process_thread_run(process); 2674 } 2675 2676 static void 2677 raid_bdev_process_thread_run(struct raid_bdev_process *process) 2678 { 2679 struct raid_bdev *raid_bdev = process->raid_bdev; 2680 int rc; 2681 2682 assert(spdk_get_thread() == process->thread); 2683 assert(process->window_remaining == 0); 2684 assert(process->window_range_locked == false); 2685 2686 if (process->state == RAID_PROCESS_STATE_STOPPING) { 2687 raid_bdev_process_do_finish(process); 2688 return; 2689 } 2690 2691 if (process->window_offset == raid_bdev->bdev.blockcnt) { 2692 SPDK_DEBUGLOG(bdev_raid, "process completed on %s\n", raid_bdev->bdev.name); 2693 raid_bdev_process_finish(process, 0); 2694 return; 2695 } 2696 2697 process->max_window_size = spdk_min(raid_bdev->bdev.blockcnt - process->window_offset, 2698 process->max_window_size); 2699 2700 rc = spdk_bdev_quiesce_range(&raid_bdev->bdev, &g_raid_if, 2701 process->window_offset, process->max_window_size, 2702 raid_bdev_process_window_range_locked, process); 2703 if (rc != 0) { 2704 raid_bdev_process_window_range_locked(process, rc); 2705 } 2706 } 2707 2708 static void 2709 raid_bdev_process_thread_init(void *ctx) 2710 { 2711 struct raid_bdev_process *process = ctx; 2712 struct raid_bdev *raid_bdev = process->raid_bdev; 2713 struct spdk_io_channel *ch; 2714 2715 process->thread = spdk_get_thread(); 2716 2717 ch = spdk_get_io_channel(raid_bdev); 2718 if (ch == NULL) { 2719 process->status = -ENOMEM; 2720 raid_bdev_process_do_finish(process); 2721 return; 2722 } 2723 2724 process->raid_ch = spdk_io_channel_get_ctx(ch); 2725 process->state = RAID_PROCESS_STATE_RUNNING; 2726 2727 SPDK_NOTICELOG("Started %s on raid bdev %s\n", 2728 raid_bdev_process_to_str(process->type), raid_bdev->bdev.name); 2729 2730 raid_bdev_process_thread_run(process); 2731 } 2732 2733 static void 2734 raid_bdev_channels_abort_start_process_done(struct spdk_io_channel_iter *i, int status) 2735 { 2736 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2737 2738 _raid_bdev_remove_base_bdev(process->target, NULL, NULL); 2739 raid_bdev_process_free(process); 2740 2741 /* TODO: update sb */ 2742 } 2743 2744 static void 2745 raid_bdev_channel_abort_start_process(struct spdk_io_channel_iter *i) 2746 { 2747 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2748 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2749 2750 raid_bdev_ch_process_cleanup(raid_ch); 2751 2752 spdk_for_each_channel_continue(i, 0); 2753 } 2754 2755 static void 2756 raid_bdev_channels_start_process_done(struct spdk_io_channel_iter *i, int status) 2757 { 2758 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2759 struct raid_bdev *raid_bdev = process->raid_bdev; 2760 struct spdk_thread *thread; 2761 char thread_name[RAID_BDEV_SB_NAME_SIZE + 16]; 2762 2763 if (status != 0) { 2764 SPDK_ERRLOG("Failed to start %s on %s: %s\n", 2765 raid_bdev_process_to_str(process->type), raid_bdev->bdev.name, 2766 spdk_strerror(-status)); 2767 goto err; 2768 } 2769 2770 /* TODO: we may need to abort if a base bdev was removed before we got here */ 2771 2772 snprintf(thread_name, sizeof(thread_name), "%s_%s", 2773 raid_bdev->bdev.name, raid_bdev_process_to_str(process->type)); 2774 2775 thread = spdk_thread_create(thread_name, NULL); 2776 if (thread == NULL) { 2777 SPDK_ERRLOG("Failed to create %s thread for %s\n", 2778 raid_bdev_process_to_str(process->type), raid_bdev->bdev.name); 2779 goto err; 2780 } 2781 2782 raid_bdev->process = process; 2783 2784 spdk_thread_send_msg(thread, raid_bdev_process_thread_init, process); 2785 2786 return; 2787 err: 2788 spdk_for_each_channel(process->raid_bdev, raid_bdev_channel_abort_start_process, process, 2789 raid_bdev_channels_abort_start_process_done); 2790 } 2791 2792 static void 2793 raid_bdev_channel_start_process(struct spdk_io_channel_iter *i) 2794 { 2795 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2796 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2797 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2798 int rc; 2799 2800 rc = raid_bdev_ch_process_setup(raid_ch, process); 2801 2802 spdk_for_each_channel_continue(i, rc); 2803 } 2804 2805 static void 2806 raid_bdev_process_start(struct raid_bdev_process *process) 2807 { 2808 struct raid_bdev *raid_bdev = process->raid_bdev; 2809 2810 assert(raid_bdev->module->submit_process_request != NULL); 2811 2812 spdk_for_each_channel(raid_bdev, raid_bdev_channel_start_process, process, 2813 raid_bdev_channels_start_process_done); 2814 } 2815 2816 static void 2817 raid_bdev_process_request_free(struct raid_bdev_process_request *process_req) 2818 { 2819 spdk_dma_free(process_req->iov.iov_base); 2820 spdk_dma_free(process_req->md_buf); 2821 free(process_req); 2822 } 2823 2824 static struct raid_bdev_process_request * 2825 raid_bdev_process_alloc_request(struct raid_bdev_process *process) 2826 { 2827 struct raid_bdev *raid_bdev = process->raid_bdev; 2828 struct raid_bdev_process_request *process_req; 2829 2830 process_req = calloc(1, sizeof(*process_req)); 2831 if (process_req == NULL) { 2832 return NULL; 2833 } 2834 2835 process_req->process = process; 2836 process_req->iov.iov_len = process->max_window_size * raid_bdev->bdev.blocklen; 2837 process_req->iov.iov_base = spdk_dma_malloc(process_req->iov.iov_len, 4096, 0); 2838 if (process_req->iov.iov_base == NULL) { 2839 free(process_req); 2840 return NULL; 2841 } 2842 if (spdk_bdev_is_md_separate(&raid_bdev->bdev)) { 2843 process_req->md_buf = spdk_dma_malloc(process->max_window_size * raid_bdev->bdev.md_len, 4096, 0); 2844 if (process_req->md_buf == NULL) { 2845 raid_bdev_process_request_free(process_req); 2846 return NULL; 2847 } 2848 } 2849 2850 return process_req; 2851 } 2852 2853 static void 2854 raid_bdev_process_free(struct raid_bdev_process *process) 2855 { 2856 struct raid_bdev_process_request *process_req; 2857 2858 while ((process_req = TAILQ_FIRST(&process->requests)) != NULL) { 2859 TAILQ_REMOVE(&process->requests, process_req, link); 2860 raid_bdev_process_request_free(process_req); 2861 } 2862 2863 free(process); 2864 } 2865 2866 static struct raid_bdev_process * 2867 raid_bdev_process_alloc(struct raid_bdev *raid_bdev, enum raid_process_type type, 2868 struct raid_base_bdev_info *target) 2869 { 2870 struct raid_bdev_process *process; 2871 struct raid_bdev_process_request *process_req; 2872 int i; 2873 2874 process = calloc(1, sizeof(*process)); 2875 if (process == NULL) { 2876 return NULL; 2877 } 2878 2879 process->raid_bdev = raid_bdev; 2880 process->type = type; 2881 process->target = target; 2882 process->max_window_size = spdk_max(spdk_divide_round_up(g_opts.process_window_size_kb * 1024UL, 2883 spdk_bdev_get_data_block_size(&raid_bdev->bdev)), 2884 raid_bdev->bdev.write_unit_size); 2885 TAILQ_INIT(&process->requests); 2886 TAILQ_INIT(&process->finish_actions); 2887 2888 for (i = 0; i < RAID_BDEV_PROCESS_MAX_QD; i++) { 2889 process_req = raid_bdev_process_alloc_request(process); 2890 if (process_req == NULL) { 2891 raid_bdev_process_free(process); 2892 return NULL; 2893 } 2894 2895 TAILQ_INSERT_TAIL(&process->requests, process_req, link); 2896 } 2897 2898 return process; 2899 } 2900 2901 static int 2902 raid_bdev_start_rebuild(struct raid_base_bdev_info *target) 2903 { 2904 struct raid_bdev_process *process; 2905 2906 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2907 2908 process = raid_bdev_process_alloc(target->raid_bdev, RAID_PROCESS_REBUILD, target); 2909 if (process == NULL) { 2910 return -ENOMEM; 2911 } 2912 2913 raid_bdev_process_start(process); 2914 2915 return 0; 2916 } 2917 2918 static void 2919 raid_bdev_configure_base_bdev_cont(struct raid_base_bdev_info *base_info) 2920 { 2921 struct raid_bdev *raid_bdev = base_info->raid_bdev; 2922 int rc; 2923 2924 /* TODO: defer if rebuild in progress on another base bdev */ 2925 assert(raid_bdev->process == NULL); 2926 2927 base_info->is_configured = true; 2928 2929 raid_bdev->num_base_bdevs_discovered++; 2930 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 2931 assert(raid_bdev->num_base_bdevs_operational <= raid_bdev->num_base_bdevs); 2932 assert(raid_bdev->num_base_bdevs_operational >= raid_bdev->min_base_bdevs_operational); 2933 2934 /* 2935 * Configure the raid bdev when the number of discovered base bdevs reaches the number 2936 * of base bdevs we know to be operational members of the array. Usually this is equal 2937 * to the total number of base bdevs (num_base_bdevs) but can be less - when the array is 2938 * degraded. 2939 */ 2940 if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs_operational) { 2941 rc = raid_bdev_configure(raid_bdev); 2942 if (rc != 0) { 2943 SPDK_ERRLOG("Failed to configure raid bdev: %s\n", spdk_strerror(-rc)); 2944 } 2945 } else if (raid_bdev->num_base_bdevs_discovered > raid_bdev->num_base_bdevs_operational) { 2946 assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE); 2947 raid_bdev->num_base_bdevs_operational++; 2948 rc = raid_bdev_start_rebuild(base_info); 2949 if (rc != 0) { 2950 SPDK_ERRLOG("Failed to start rebuild: %s\n", spdk_strerror(-rc)); 2951 _raid_bdev_remove_base_bdev(base_info, NULL, NULL); 2952 } 2953 } else { 2954 rc = 0; 2955 } 2956 2957 if (base_info->configure_cb != NULL) { 2958 base_info->configure_cb(base_info->configure_cb_ctx, rc); 2959 } 2960 } 2961 2962 static void 2963 raid_bdev_configure_base_bdev_check_sb_cb(const struct raid_bdev_superblock *sb, int status, 2964 void *ctx) 2965 { 2966 struct raid_base_bdev_info *base_info = ctx; 2967 2968 switch (status) { 2969 case 0: 2970 /* valid superblock found */ 2971 SPDK_ERRLOG("Existing raid superblock found on bdev %s\n", base_info->name); 2972 status = -EEXIST; 2973 raid_bdev_free_base_bdev_resource(base_info); 2974 break; 2975 case -EINVAL: 2976 /* no valid superblock */ 2977 raid_bdev_configure_base_bdev_cont(base_info); 2978 return; 2979 default: 2980 SPDK_ERRLOG("Failed to examine bdev %s: %s\n", 2981 base_info->name, spdk_strerror(-status)); 2982 break; 2983 } 2984 2985 if (base_info->configure_cb != NULL) { 2986 base_info->configure_cb(base_info->configure_cb_ctx, status); 2987 } 2988 } 2989 2990 static int 2991 raid_bdev_configure_base_bdev(struct raid_base_bdev_info *base_info, bool existing, 2992 raid_base_bdev_cb cb_fn, void *cb_ctx) 2993 { 2994 struct raid_bdev *raid_bdev = base_info->raid_bdev; 2995 struct spdk_bdev_desc *desc; 2996 struct spdk_bdev *bdev; 2997 const struct spdk_uuid *bdev_uuid; 2998 int rc; 2999 3000 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 3001 assert(base_info->desc == NULL); 3002 3003 /* 3004 * Base bdev can be added by name or uuid. Here we assure both properties are set and valid 3005 * before claiming the bdev. 3006 */ 3007 3008 if (!spdk_uuid_is_null(&base_info->uuid)) { 3009 char uuid_str[SPDK_UUID_STRING_LEN]; 3010 const char *bdev_name; 3011 3012 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid); 3013 3014 /* UUID of a bdev is registered as its alias */ 3015 bdev = spdk_bdev_get_by_name(uuid_str); 3016 if (bdev == NULL) { 3017 return -ENODEV; 3018 } 3019 3020 bdev_name = spdk_bdev_get_name(bdev); 3021 3022 if (base_info->name == NULL) { 3023 assert(existing == true); 3024 base_info->name = strdup(bdev_name); 3025 if (base_info->name == NULL) { 3026 return -ENOMEM; 3027 } 3028 } else if (strcmp(base_info->name, bdev_name) != 0) { 3029 SPDK_ERRLOG("Name mismatch for base bdev '%s' - expected '%s'\n", 3030 bdev_name, base_info->name); 3031 return -EINVAL; 3032 } 3033 } 3034 3035 assert(base_info->name != NULL); 3036 3037 rc = spdk_bdev_open_ext(base_info->name, true, raid_bdev_event_base_bdev, NULL, &desc); 3038 if (rc != 0) { 3039 if (rc != -ENODEV) { 3040 SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", base_info->name); 3041 } 3042 return rc; 3043 } 3044 3045 bdev = spdk_bdev_desc_get_bdev(desc); 3046 bdev_uuid = spdk_bdev_get_uuid(bdev); 3047 3048 if (spdk_uuid_is_null(&base_info->uuid)) { 3049 spdk_uuid_copy(&base_info->uuid, bdev_uuid); 3050 } else if (spdk_uuid_compare(&base_info->uuid, bdev_uuid) != 0) { 3051 SPDK_ERRLOG("UUID mismatch for base bdev '%s'\n", base_info->name); 3052 spdk_bdev_close(desc); 3053 return -EINVAL; 3054 } 3055 3056 rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if); 3057 if (rc != 0) { 3058 SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n"); 3059 spdk_bdev_close(desc); 3060 return rc; 3061 } 3062 3063 SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev->name); 3064 3065 base_info->app_thread_ch = spdk_bdev_get_io_channel(desc); 3066 if (base_info->app_thread_ch == NULL) { 3067 SPDK_ERRLOG("Failed to get io channel\n"); 3068 spdk_bdev_module_release_bdev(bdev); 3069 spdk_bdev_close(desc); 3070 return -ENOMEM; 3071 } 3072 3073 base_info->desc = desc; 3074 base_info->blockcnt = bdev->blockcnt; 3075 3076 if (raid_bdev->superblock_enabled) { 3077 uint64_t data_offset; 3078 3079 if (base_info->data_offset == 0) { 3080 assert((RAID_BDEV_MIN_DATA_OFFSET_SIZE % spdk_bdev_get_data_block_size(bdev)) == 0); 3081 data_offset = RAID_BDEV_MIN_DATA_OFFSET_SIZE / spdk_bdev_get_data_block_size(bdev); 3082 } else { 3083 data_offset = base_info->data_offset; 3084 } 3085 3086 if (bdev->optimal_io_boundary != 0) { 3087 data_offset = spdk_divide_round_up(data_offset, 3088 bdev->optimal_io_boundary) * bdev->optimal_io_boundary; 3089 if (base_info->data_offset != 0 && base_info->data_offset != data_offset) { 3090 SPDK_WARNLOG("Data offset %lu on bdev '%s' is different than optimal value %lu\n", 3091 base_info->data_offset, base_info->name, data_offset); 3092 data_offset = base_info->data_offset; 3093 } 3094 } 3095 3096 base_info->data_offset = data_offset; 3097 } 3098 3099 if (base_info->data_offset >= bdev->blockcnt) { 3100 SPDK_ERRLOG("Data offset %lu exceeds base bdev capacity %lu on bdev '%s'\n", 3101 base_info->data_offset, bdev->blockcnt, base_info->name); 3102 rc = -EINVAL; 3103 goto out; 3104 } 3105 3106 if (base_info->data_size == 0) { 3107 base_info->data_size = bdev->blockcnt - base_info->data_offset; 3108 } else if (base_info->data_offset + base_info->data_size > bdev->blockcnt) { 3109 SPDK_ERRLOG("Data offset and size exceeds base bdev capacity %lu on bdev '%s'\n", 3110 bdev->blockcnt, base_info->name); 3111 rc = -EINVAL; 3112 goto out; 3113 } 3114 3115 if (!raid_bdev->module->dif_supported && spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 3116 SPDK_ERRLOG("Base bdev '%s' has DIF or DIX enabled - unsupported RAID configuration\n", 3117 bdev->name); 3118 rc = -EINVAL; 3119 goto out; 3120 } 3121 3122 /* 3123 * Set the raid bdev properties if this is the first base bdev configured, 3124 * otherwise - verify. Assumption is that all the base bdevs for any raid bdev should 3125 * have the same blocklen and metadata format. 3126 */ 3127 if (raid_bdev->bdev.blocklen == 0) { 3128 raid_bdev->bdev.blocklen = bdev->blocklen; 3129 raid_bdev->bdev.md_len = spdk_bdev_get_md_size(bdev); 3130 raid_bdev->bdev.md_interleave = spdk_bdev_is_md_interleaved(bdev); 3131 raid_bdev->bdev.dif_type = spdk_bdev_get_dif_type(bdev); 3132 raid_bdev->bdev.dif_check_flags = bdev->dif_check_flags; 3133 raid_bdev->bdev.dif_is_head_of_md = spdk_bdev_is_dif_head_of_md(bdev); 3134 } else { 3135 if (raid_bdev->bdev.blocklen != bdev->blocklen) { 3136 SPDK_ERRLOG("Raid bdev '%s' blocklen %u differs from base bdev '%s' blocklen %u\n", 3137 raid_bdev->bdev.name, raid_bdev->bdev.blocklen, bdev->name, bdev->blocklen); 3138 rc = -EINVAL; 3139 goto out; 3140 } 3141 3142 if (raid_bdev->bdev.md_len != spdk_bdev_get_md_size(bdev) || 3143 raid_bdev->bdev.md_interleave != spdk_bdev_is_md_interleaved(bdev) || 3144 raid_bdev->bdev.dif_type != spdk_bdev_get_dif_type(bdev) || 3145 raid_bdev->bdev.dif_check_flags != bdev->dif_check_flags || 3146 raid_bdev->bdev.dif_is_head_of_md != spdk_bdev_is_dif_head_of_md(bdev)) { 3147 SPDK_ERRLOG("Raid bdev '%s' has different metadata format than base bdev '%s'\n", 3148 raid_bdev->bdev.name, bdev->name); 3149 rc = -EINVAL; 3150 goto out; 3151 } 3152 } 3153 3154 base_info->configure_cb = cb_fn; 3155 base_info->configure_cb_ctx = cb_ctx; 3156 3157 if (existing) { 3158 raid_bdev_configure_base_bdev_cont(base_info); 3159 } else { 3160 /* check for existing superblock when using a new bdev */ 3161 rc = raid_bdev_load_base_bdev_superblock(desc, base_info->app_thread_ch, 3162 raid_bdev_configure_base_bdev_check_sb_cb, base_info); 3163 if (rc) { 3164 SPDK_ERRLOG("Failed to read bdev %s superblock: %s\n", 3165 bdev->name, spdk_strerror(-rc)); 3166 } 3167 } 3168 out: 3169 if (rc != 0) { 3170 raid_bdev_free_base_bdev_resource(base_info); 3171 } 3172 return rc; 3173 } 3174 3175 static int 3176 _raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot, 3177 uint64_t data_offset, uint64_t data_size, 3178 raid_base_bdev_cb cb_fn, void *cb_ctx) 3179 { 3180 struct raid_base_bdev_info *base_info; 3181 3182 assert(name != NULL); 3183 3184 if (slot >= raid_bdev->num_base_bdevs) { 3185 return -EINVAL; 3186 } 3187 3188 base_info = &raid_bdev->base_bdev_info[slot]; 3189 3190 if (base_info->name != NULL) { 3191 SPDK_ERRLOG("Slot %u on raid bdev '%s' already assigned to bdev '%s'\n", 3192 slot, raid_bdev->bdev.name, base_info->name); 3193 return -EBUSY; 3194 } 3195 3196 if (!spdk_uuid_is_null(&base_info->uuid)) { 3197 char uuid_str[SPDK_UUID_STRING_LEN]; 3198 3199 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid); 3200 SPDK_ERRLOG("Slot %u on raid bdev '%s' already assigned to bdev with uuid %s\n", 3201 slot, raid_bdev->bdev.name, uuid_str); 3202 return -EBUSY; 3203 } 3204 3205 base_info->name = strdup(name); 3206 if (base_info->name == NULL) { 3207 return -ENOMEM; 3208 } 3209 3210 base_info->data_offset = data_offset; 3211 base_info->data_size = data_size; 3212 3213 return raid_bdev_configure_base_bdev(base_info, false, cb_fn, cb_ctx); 3214 } 3215 3216 int 3217 raid_bdev_attach_base_bdev(struct raid_bdev *raid_bdev, struct spdk_bdev *base_bdev, 3218 raid_base_bdev_cb cb_fn, void *cb_ctx) 3219 { 3220 struct raid_base_bdev_info *base_info = NULL, *iter; 3221 int rc; 3222 3223 SPDK_DEBUGLOG(bdev_raid, "attach_base_device: %s\n", base_bdev->name); 3224 3225 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 3226 3227 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 3228 SPDK_ERRLOG("raid bdev '%s' must be in online state to attach base bdev\n", 3229 raid_bdev->bdev.name); 3230 return -EINVAL; 3231 } 3232 3233 RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) { 3234 if (iter->desc == NULL) { 3235 base_info = iter; 3236 break; 3237 } 3238 } 3239 3240 if (base_info == NULL) { 3241 SPDK_ERRLOG("no empty slot found in raid bdev '%s' for new base bdev '%s'\n", 3242 raid_bdev->bdev.name, base_bdev->name); 3243 return -EINVAL; 3244 } 3245 3246 assert(base_info->is_configured == false); 3247 assert(base_info->data_size != 0); 3248 3249 spdk_spin_lock(&raid_bdev->base_bdev_lock); 3250 3251 rc = _raid_bdev_add_base_device(raid_bdev, base_bdev->name, 3252 raid_bdev_base_bdev_slot(base_info), 3253 base_info->data_offset, base_info->data_size, 3254 cb_fn, cb_ctx); 3255 if (rc != 0) { 3256 SPDK_ERRLOG("base bdev '%s' attach failed: %s\n", base_bdev->name, spdk_strerror(-rc)); 3257 raid_bdev_free_base_bdev_resource(base_info); 3258 } 3259 3260 spdk_spin_unlock(&raid_bdev->base_bdev_lock); 3261 3262 return rc; 3263 } 3264 3265 /* 3266 * brief: 3267 * raid_bdev_add_base_device function is the actual function which either adds 3268 * the nvme base device to existing raid bdev or create a new raid bdev. It also claims 3269 * the base device and keep the open descriptor. 3270 * params: 3271 * raid_bdev - pointer to raid bdev 3272 * name - name of the base bdev 3273 * slot - position to add base bdev 3274 * cb_fn - callback function 3275 * cb_ctx - argument to callback function 3276 * returns: 3277 * 0 - success 3278 * non zero - failure 3279 */ 3280 int 3281 raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot, 3282 raid_base_bdev_cb cb_fn, void *cb_ctx) 3283 { 3284 return _raid_bdev_add_base_device(raid_bdev, name, slot, 0, 0, cb_fn, cb_ctx); 3285 } 3286 3287 static int 3288 raid_bdev_create_from_sb(const struct raid_bdev_superblock *sb, struct raid_bdev **raid_bdev_out) 3289 { 3290 struct raid_bdev *raid_bdev; 3291 uint8_t i; 3292 int rc; 3293 3294 rc = _raid_bdev_create(sb->name, (sb->strip_size * sb->block_size) / 1024, sb->num_base_bdevs, 3295 sb->level, true, &sb->uuid, &raid_bdev); 3296 if (rc != 0) { 3297 return rc; 3298 } 3299 3300 rc = raid_bdev_alloc_superblock(raid_bdev, sb->block_size); 3301 if (rc != 0) { 3302 raid_bdev_free(raid_bdev); 3303 return rc; 3304 } 3305 3306 assert(sb->length <= RAID_BDEV_SB_MAX_LENGTH); 3307 memcpy(raid_bdev->sb, sb, sb->length); 3308 3309 for (i = 0; i < sb->base_bdevs_size; i++) { 3310 const struct raid_bdev_sb_base_bdev *sb_base_bdev = &sb->base_bdevs[i]; 3311 struct raid_base_bdev_info *base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot]; 3312 3313 if (sb_base_bdev->state == RAID_SB_BASE_BDEV_CONFIGURED) { 3314 spdk_uuid_copy(&base_info->uuid, &sb_base_bdev->uuid); 3315 raid_bdev->num_base_bdevs_operational++; 3316 } 3317 3318 base_info->data_offset = sb_base_bdev->data_offset; 3319 base_info->data_size = sb_base_bdev->data_size; 3320 } 3321 3322 *raid_bdev_out = raid_bdev; 3323 return 0; 3324 } 3325 3326 static void 3327 raid_bdev_examine_no_sb(struct spdk_bdev *bdev) 3328 { 3329 struct raid_bdev *raid_bdev; 3330 struct raid_base_bdev_info *base_info; 3331 3332 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 3333 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 3334 if (base_info->desc == NULL && base_info->name != NULL && 3335 strcmp(bdev->name, base_info->name) == 0) { 3336 raid_bdev_configure_base_bdev(base_info, true, NULL, NULL); 3337 break; 3338 } 3339 } 3340 } 3341 } 3342 3343 static void 3344 raid_bdev_examine_sb(const struct raid_bdev_superblock *sb, struct spdk_bdev *bdev) 3345 { 3346 const struct raid_bdev_sb_base_bdev *sb_base_bdev = NULL; 3347 struct raid_bdev *raid_bdev; 3348 struct raid_base_bdev_info *iter, *base_info; 3349 uint8_t i; 3350 int rc; 3351 3352 if (sb->block_size != spdk_bdev_get_data_block_size(bdev)) { 3353 SPDK_WARNLOG("Bdev %s block size (%u) does not match the value in superblock (%u)\n", 3354 bdev->name, sb->block_size, spdk_bdev_get_data_block_size(bdev)); 3355 return; 3356 } 3357 3358 if (spdk_uuid_is_null(&sb->uuid)) { 3359 SPDK_WARNLOG("NULL raid bdev UUID in superblock on bdev %s\n", bdev->name); 3360 return; 3361 } 3362 3363 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 3364 if (spdk_uuid_compare(&raid_bdev->bdev.uuid, &sb->uuid) == 0) { 3365 break; 3366 } 3367 } 3368 3369 if (raid_bdev) { 3370 if (sb->seq_number > raid_bdev->sb->seq_number) { 3371 SPDK_DEBUGLOG(bdev_raid, 3372 "raid superblock seq_number on bdev %s (%lu) greater than existing raid bdev %s (%lu)\n", 3373 bdev->name, sb->seq_number, raid_bdev->bdev.name, raid_bdev->sb->seq_number); 3374 3375 if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) { 3376 SPDK_WARNLOG("Newer version of raid bdev %s superblock found on bdev %s but raid bdev is not in configuring state.\n", 3377 raid_bdev->bdev.name, bdev->name); 3378 return; 3379 } 3380 3381 /* remove and then recreate the raid bdev using the newer superblock */ 3382 raid_bdev_delete(raid_bdev, NULL, NULL); 3383 raid_bdev = NULL; 3384 } else if (sb->seq_number < raid_bdev->sb->seq_number) { 3385 SPDK_DEBUGLOG(bdev_raid, 3386 "raid superblock seq_number on bdev %s (%lu) smaller than existing raid bdev %s (%lu)\n", 3387 bdev->name, sb->seq_number, raid_bdev->bdev.name, raid_bdev->sb->seq_number); 3388 /* use the current raid bdev superblock */ 3389 sb = raid_bdev->sb; 3390 } 3391 } 3392 3393 for (i = 0; i < sb->base_bdevs_size; i++) { 3394 sb_base_bdev = &sb->base_bdevs[i]; 3395 3396 assert(spdk_uuid_is_null(&sb_base_bdev->uuid) == false); 3397 3398 if (spdk_uuid_compare(&sb_base_bdev->uuid, spdk_bdev_get_uuid(bdev)) == 0) { 3399 break; 3400 } 3401 } 3402 3403 if (i == sb->base_bdevs_size) { 3404 SPDK_DEBUGLOG(bdev_raid, "raid superblock does not contain this bdev's uuid\n"); 3405 return; 3406 } 3407 3408 if (!raid_bdev) { 3409 rc = raid_bdev_create_from_sb(sb, &raid_bdev); 3410 if (rc != 0) { 3411 SPDK_ERRLOG("Failed to create raid bdev %s: %s\n", 3412 sb->name, spdk_strerror(-rc)); 3413 return; 3414 } 3415 } 3416 3417 if (sb_base_bdev->state != RAID_SB_BASE_BDEV_CONFIGURED) { 3418 SPDK_NOTICELOG("Bdev %s is not an active member of raid bdev %s. Ignoring.\n", 3419 bdev->name, raid_bdev->bdev.name); 3420 return; 3421 } 3422 3423 base_info = NULL; 3424 RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) { 3425 if (spdk_uuid_compare(&iter->uuid, spdk_bdev_get_uuid(bdev)) == 0) { 3426 base_info = iter; 3427 break; 3428 } 3429 } 3430 3431 if (base_info == NULL) { 3432 SPDK_ERRLOG("Bdev %s is not a member of raid bdev %s\n", 3433 bdev->name, raid_bdev->bdev.name); 3434 return; 3435 } 3436 3437 rc = raid_bdev_configure_base_bdev(base_info, true, NULL, NULL); 3438 if (rc != 0) { 3439 SPDK_ERRLOG("Failed to configure bdev %s as base bdev of raid %s: %s\n", 3440 bdev->name, raid_bdev->bdev.name, spdk_strerror(-rc)); 3441 } 3442 } 3443 3444 struct raid_bdev_examine_ctx { 3445 struct spdk_bdev_desc *desc; 3446 struct spdk_io_channel *ch; 3447 }; 3448 3449 static void 3450 raid_bdev_examine_ctx_free(struct raid_bdev_examine_ctx *ctx) 3451 { 3452 if (!ctx) { 3453 return; 3454 } 3455 3456 if (ctx->ch) { 3457 spdk_put_io_channel(ctx->ch); 3458 } 3459 3460 if (ctx->desc) { 3461 spdk_bdev_close(ctx->desc); 3462 } 3463 3464 free(ctx); 3465 } 3466 3467 static void 3468 raid_bdev_examine_load_sb_cb(const struct raid_bdev_superblock *sb, int status, void *_ctx) 3469 { 3470 struct raid_bdev_examine_ctx *ctx = _ctx; 3471 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(ctx->desc); 3472 3473 switch (status) { 3474 case 0: 3475 /* valid superblock found */ 3476 SPDK_DEBUGLOG(bdev_raid, "raid superblock found on bdev %s\n", bdev->name); 3477 raid_bdev_examine_sb(sb, bdev); 3478 break; 3479 case -EINVAL: 3480 /* no valid superblock, check if it can be claimed anyway */ 3481 raid_bdev_examine_no_sb(bdev); 3482 break; 3483 default: 3484 SPDK_ERRLOG("Failed to examine bdev %s: %s\n", 3485 bdev->name, spdk_strerror(-status)); 3486 break; 3487 } 3488 3489 raid_bdev_examine_ctx_free(ctx); 3490 spdk_bdev_module_examine_done(&g_raid_if); 3491 } 3492 3493 static void 3494 raid_bdev_examine_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 3495 { 3496 } 3497 3498 /* 3499 * brief: 3500 * raid_bdev_examine function is the examine function call by the below layers 3501 * like bdev_nvme layer. This function will check if this base bdev can be 3502 * claimed by this raid bdev or not. 3503 * params: 3504 * bdev - pointer to base bdev 3505 * returns: 3506 * none 3507 */ 3508 static void 3509 raid_bdev_examine(struct spdk_bdev *bdev) 3510 { 3511 struct raid_bdev_examine_ctx *ctx; 3512 int rc; 3513 3514 if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 3515 raid_bdev_examine_no_sb(bdev); 3516 spdk_bdev_module_examine_done(&g_raid_if); 3517 return; 3518 } 3519 3520 ctx = calloc(1, sizeof(*ctx)); 3521 if (!ctx) { 3522 SPDK_ERRLOG("Failed to examine bdev %s: %s\n", 3523 bdev->name, spdk_strerror(ENOMEM)); 3524 goto err; 3525 } 3526 3527 rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, raid_bdev_examine_event_cb, NULL, 3528 &ctx->desc); 3529 if (rc) { 3530 SPDK_ERRLOG("Failed to open bdev %s: %s\n", 3531 bdev->name, spdk_strerror(-rc)); 3532 goto err; 3533 } 3534 3535 ctx->ch = spdk_bdev_get_io_channel(ctx->desc); 3536 if (!ctx->ch) { 3537 SPDK_ERRLOG("Failed to get io channel for bdev %s\n", bdev->name); 3538 goto err; 3539 } 3540 3541 rc = raid_bdev_load_base_bdev_superblock(ctx->desc, ctx->ch, raid_bdev_examine_load_sb_cb, ctx); 3542 if (rc) { 3543 SPDK_ERRLOG("Failed to read bdev %s superblock: %s\n", 3544 bdev->name, spdk_strerror(-rc)); 3545 goto err; 3546 } 3547 3548 return; 3549 err: 3550 raid_bdev_examine_ctx_free(ctx); 3551 spdk_bdev_module_examine_done(&g_raid_if); 3552 } 3553 3554 /* Log component for bdev raid bdev module */ 3555 SPDK_LOG_REGISTER_COMPONENT(bdev_raid) 3556