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 if (cb_fn != NULL) { 2132 cb_fn(cb_ctx, 0); 2133 } 2134 } else if (raid_bdev->num_base_bdevs_operational-- == raid_bdev->min_base_bdevs_operational) { 2135 /* 2136 * After this base bdev is removed there will not be enough base bdevs 2137 * to keep the raid bdev operational. 2138 */ 2139 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_ctx); 2140 } else if (raid_bdev->process != NULL) { 2141 ret = raid_bdev_process_base_bdev_remove(raid_bdev->process, base_info); 2142 } else { 2143 ret = raid_bdev_remove_base_bdev_quiesce(base_info); 2144 } 2145 2146 if (ret != 0) { 2147 base_info->remove_scheduled = false; 2148 } 2149 return ret; 2150 } 2151 2152 /* 2153 * brief: 2154 * raid_bdev_remove_base_bdev function is called by below layers when base_bdev 2155 * is removed. This function checks if this base bdev is part of any raid bdev 2156 * or not. If yes, it takes necessary action on that particular raid bdev. 2157 * params: 2158 * base_bdev - pointer to base bdev which got removed 2159 * cb_fn - callback function 2160 * cb_arg - argument to callback function 2161 * returns: 2162 * 0 - success 2163 * non zero - failure 2164 */ 2165 int 2166 raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev, raid_base_bdev_cb cb_fn, void *cb_ctx) 2167 { 2168 struct raid_base_bdev_info *base_info; 2169 2170 /* Find the raid_bdev which has claimed this base_bdev */ 2171 base_info = raid_bdev_find_base_info_by_bdev(base_bdev); 2172 if (!base_info) { 2173 SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name); 2174 return -ENODEV; 2175 } 2176 2177 return _raid_bdev_remove_base_bdev(base_info, cb_fn, cb_ctx); 2178 } 2179 2180 /* 2181 * brief: 2182 * raid_bdev_resize_base_bdev function is called by below layers when base_bdev 2183 * is resized. This function checks if the smallest size of the base_bdevs is changed. 2184 * If yes, call module handler to resize the raid_bdev if implemented. 2185 * params: 2186 * base_bdev - pointer to base bdev which got resized. 2187 * returns: 2188 * none 2189 */ 2190 static void 2191 raid_bdev_resize_base_bdev(struct spdk_bdev *base_bdev) 2192 { 2193 struct raid_bdev *raid_bdev; 2194 struct raid_base_bdev_info *base_info; 2195 2196 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_resize_base_bdev\n"); 2197 2198 base_info = raid_bdev_find_base_info_by_bdev(base_bdev); 2199 2200 /* Find the raid_bdev which has claimed this base_bdev */ 2201 if (!base_info) { 2202 SPDK_ERRLOG("raid_bdev whose base_bdev '%s' not found\n", base_bdev->name); 2203 return; 2204 } 2205 raid_bdev = base_info->raid_bdev; 2206 2207 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2208 2209 SPDK_NOTICELOG("base_bdev '%s' was resized: old size %" PRIu64 ", new size %" PRIu64 "\n", 2210 base_bdev->name, base_info->blockcnt, base_bdev->blockcnt); 2211 2212 if (raid_bdev->module->resize) { 2213 raid_bdev->module->resize(raid_bdev); 2214 } 2215 } 2216 2217 /* 2218 * brief: 2219 * raid_bdev_event_base_bdev function is called by below layers when base_bdev 2220 * triggers asynchronous event. 2221 * params: 2222 * type - event details. 2223 * bdev - bdev that triggered event. 2224 * event_ctx - context for event. 2225 * returns: 2226 * none 2227 */ 2228 static void 2229 raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 2230 void *event_ctx) 2231 { 2232 int rc; 2233 2234 switch (type) { 2235 case SPDK_BDEV_EVENT_REMOVE: 2236 rc = raid_bdev_remove_base_bdev(bdev, NULL, NULL); 2237 if (rc != 0) { 2238 SPDK_ERRLOG("Failed to remove base bdev %s: %s\n", 2239 spdk_bdev_get_name(bdev), spdk_strerror(-rc)); 2240 } 2241 break; 2242 case SPDK_BDEV_EVENT_RESIZE: 2243 raid_bdev_resize_base_bdev(bdev); 2244 break; 2245 default: 2246 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 2247 break; 2248 } 2249 } 2250 2251 /* 2252 * brief: 2253 * Deletes the specified raid bdev 2254 * params: 2255 * raid_bdev - pointer to raid bdev 2256 * cb_fn - callback function 2257 * cb_arg - argument to callback function 2258 */ 2259 void 2260 raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_arg) 2261 { 2262 struct raid_base_bdev_info *base_info; 2263 2264 SPDK_DEBUGLOG(bdev_raid, "delete raid bdev: %s\n", raid_bdev->bdev.name); 2265 2266 if (raid_bdev->destroy_started) { 2267 SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n", 2268 raid_bdev->bdev.name); 2269 if (cb_fn) { 2270 cb_fn(cb_arg, -EALREADY); 2271 } 2272 return; 2273 } 2274 2275 raid_bdev->destroy_started = true; 2276 2277 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 2278 base_info->remove_scheduled = true; 2279 2280 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 2281 /* 2282 * As raid bdev is not registered yet or already unregistered, 2283 * so cleanup should be done here itself. 2284 */ 2285 raid_bdev_free_base_bdev_resource(base_info); 2286 } 2287 } 2288 2289 if (raid_bdev->num_base_bdevs_discovered == 0) { 2290 /* There is no base bdev for this raid, so free the raid device. */ 2291 raid_bdev_cleanup_and_free(raid_bdev); 2292 if (cb_fn) { 2293 cb_fn(cb_arg, 0); 2294 } 2295 } else { 2296 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg); 2297 } 2298 } 2299 2300 static void 2301 raid_bdev_process_finish_write_sb_cb(int status, struct raid_bdev *raid_bdev, void *ctx) 2302 { 2303 if (status != 0) { 2304 SPDK_ERRLOG("Failed to write raid bdev '%s' superblock after background process finished: %s\n", 2305 raid_bdev->bdev.name, spdk_strerror(-status)); 2306 } 2307 } 2308 2309 static void 2310 raid_bdev_process_finish_write_sb(void *ctx) 2311 { 2312 struct raid_bdev *raid_bdev = ctx; 2313 struct raid_bdev_superblock *sb = raid_bdev->sb; 2314 struct raid_bdev_sb_base_bdev *sb_base_bdev; 2315 struct raid_base_bdev_info *base_info; 2316 uint8_t i; 2317 2318 for (i = 0; i < sb->base_bdevs_size; i++) { 2319 sb_base_bdev = &sb->base_bdevs[i]; 2320 2321 if (sb_base_bdev->state != RAID_SB_BASE_BDEV_CONFIGURED && 2322 sb_base_bdev->slot < raid_bdev->num_base_bdevs) { 2323 base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot]; 2324 if (base_info->is_configured) { 2325 sb_base_bdev->state = RAID_SB_BASE_BDEV_CONFIGURED; 2326 spdk_uuid_copy(&sb_base_bdev->uuid, &base_info->uuid); 2327 } 2328 } 2329 } 2330 2331 raid_bdev_write_superblock(raid_bdev, raid_bdev_process_finish_write_sb_cb, NULL); 2332 } 2333 2334 static void raid_bdev_process_free(struct raid_bdev_process *process); 2335 2336 static void 2337 _raid_bdev_process_finish_done(void *ctx) 2338 { 2339 struct raid_bdev_process *process = ctx; 2340 struct raid_process_finish_action *finish_action; 2341 2342 while ((finish_action = TAILQ_FIRST(&process->finish_actions)) != NULL) { 2343 TAILQ_REMOVE(&process->finish_actions, finish_action, link); 2344 finish_action->cb(finish_action->cb_ctx); 2345 free(finish_action); 2346 } 2347 2348 raid_bdev_process_free(process); 2349 2350 spdk_thread_exit(spdk_get_thread()); 2351 } 2352 2353 static void 2354 raid_bdev_process_finish_target_removed(void *ctx, int status) 2355 { 2356 struct raid_bdev_process *process = ctx; 2357 2358 if (status != 0) { 2359 SPDK_ERRLOG("Failed to remove target bdev: %s\n", spdk_strerror(-status)); 2360 } 2361 2362 spdk_thread_send_msg(process->thread, _raid_bdev_process_finish_done, process); 2363 } 2364 2365 static void 2366 raid_bdev_process_finish_unquiesced(void *ctx, int status) 2367 { 2368 struct raid_bdev_process *process = ctx; 2369 2370 if (status != 0) { 2371 SPDK_ERRLOG("Failed to unquiesce bdev: %s\n", spdk_strerror(-status)); 2372 } 2373 2374 if (process->status != 0) { 2375 struct raid_base_bdev_info *target = process->target; 2376 2377 if (target->desc != NULL && target->remove_scheduled == false) { 2378 _raid_bdev_remove_base_bdev(target, raid_bdev_process_finish_target_removed, process); 2379 return; 2380 } 2381 } 2382 2383 spdk_thread_send_msg(process->thread, _raid_bdev_process_finish_done, process); 2384 } 2385 2386 static void 2387 raid_bdev_process_finish_unquiesce(void *ctx) 2388 { 2389 struct raid_bdev_process *process = ctx; 2390 int rc; 2391 2392 rc = spdk_bdev_unquiesce(&process->raid_bdev->bdev, &g_raid_if, 2393 raid_bdev_process_finish_unquiesced, process); 2394 if (rc != 0) { 2395 raid_bdev_process_finish_unquiesced(process, rc); 2396 } 2397 } 2398 2399 static void 2400 raid_bdev_process_finish_done(void *ctx) 2401 { 2402 struct raid_bdev_process *process = ctx; 2403 struct raid_bdev *raid_bdev = process->raid_bdev; 2404 2405 if (process->raid_ch != NULL) { 2406 spdk_put_io_channel(spdk_io_channel_from_ctx(process->raid_ch)); 2407 } 2408 2409 process->state = RAID_PROCESS_STATE_STOPPED; 2410 2411 if (process->status == 0) { 2412 SPDK_NOTICELOG("Finished %s on raid bdev %s\n", 2413 raid_bdev_process_to_str(process->type), 2414 raid_bdev->bdev.name); 2415 if (raid_bdev->superblock_enabled) { 2416 spdk_thread_send_msg(spdk_thread_get_app_thread(), 2417 raid_bdev_process_finish_write_sb, 2418 raid_bdev); 2419 } 2420 } else { 2421 SPDK_WARNLOG("Finished %s on raid bdev %s: %s\n", 2422 raid_bdev_process_to_str(process->type), 2423 raid_bdev->bdev.name, 2424 spdk_strerror(-process->status)); 2425 } 2426 2427 spdk_thread_send_msg(spdk_thread_get_app_thread(), raid_bdev_process_finish_unquiesce, 2428 process); 2429 } 2430 2431 static void 2432 __raid_bdev_process_finish(struct spdk_io_channel_iter *i, int status) 2433 { 2434 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2435 2436 spdk_thread_send_msg(process->thread, raid_bdev_process_finish_done, process); 2437 } 2438 2439 static void 2440 raid_bdev_channel_process_finish(struct spdk_io_channel_iter *i) 2441 { 2442 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2443 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2444 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2445 2446 if (process->status == 0) { 2447 uint8_t slot = raid_bdev_base_bdev_slot(process->target); 2448 2449 raid_ch->base_channel[slot] = raid_ch->process.target_ch; 2450 raid_ch->process.target_ch = NULL; 2451 } 2452 2453 raid_bdev_ch_process_cleanup(raid_ch); 2454 2455 spdk_for_each_channel_continue(i, 0); 2456 } 2457 2458 static void 2459 raid_bdev_process_finish_quiesced(void *ctx, int status) 2460 { 2461 struct raid_bdev_process *process = ctx; 2462 struct raid_bdev *raid_bdev = process->raid_bdev; 2463 2464 if (status != 0) { 2465 SPDK_ERRLOG("Failed to quiesce bdev: %s\n", spdk_strerror(-status)); 2466 return; 2467 } 2468 2469 raid_bdev->process = NULL; 2470 spdk_for_each_channel(process->raid_bdev, raid_bdev_channel_process_finish, process, 2471 __raid_bdev_process_finish); 2472 } 2473 2474 static void 2475 _raid_bdev_process_finish(void *ctx) 2476 { 2477 struct raid_bdev_process *process = ctx; 2478 int rc; 2479 2480 rc = spdk_bdev_quiesce(&process->raid_bdev->bdev, &g_raid_if, 2481 raid_bdev_process_finish_quiesced, process); 2482 if (rc != 0) { 2483 raid_bdev_process_finish_quiesced(ctx, rc); 2484 } 2485 } 2486 2487 static void 2488 raid_bdev_process_do_finish(struct raid_bdev_process *process) 2489 { 2490 spdk_thread_send_msg(spdk_thread_get_app_thread(), _raid_bdev_process_finish, process); 2491 } 2492 2493 static void raid_bdev_process_unlock_window_range(struct raid_bdev_process *process); 2494 static void raid_bdev_process_thread_run(struct raid_bdev_process *process); 2495 2496 static void 2497 raid_bdev_process_finish(struct raid_bdev_process *process, int status) 2498 { 2499 assert(spdk_get_thread() == process->thread); 2500 2501 if (process->status == 0) { 2502 process->status = status; 2503 } 2504 2505 if (process->state >= RAID_PROCESS_STATE_STOPPING) { 2506 return; 2507 } 2508 2509 assert(process->state == RAID_PROCESS_STATE_RUNNING); 2510 process->state = RAID_PROCESS_STATE_STOPPING; 2511 2512 if (process->window_range_locked) { 2513 raid_bdev_process_unlock_window_range(process); 2514 } else { 2515 raid_bdev_process_thread_run(process); 2516 } 2517 } 2518 2519 static void 2520 raid_bdev_process_window_range_unlocked(void *ctx, int status) 2521 { 2522 struct raid_bdev_process *process = ctx; 2523 2524 if (status != 0) { 2525 SPDK_ERRLOG("Failed to unlock LBA range: %s\n", spdk_strerror(-status)); 2526 raid_bdev_process_finish(process, status); 2527 return; 2528 } 2529 2530 process->window_range_locked = false; 2531 process->window_offset += process->window_size; 2532 2533 raid_bdev_process_thread_run(process); 2534 } 2535 2536 static void 2537 raid_bdev_process_unlock_window_range(struct raid_bdev_process *process) 2538 { 2539 int rc; 2540 2541 assert(process->window_range_locked == true); 2542 2543 rc = spdk_bdev_unquiesce_range(&process->raid_bdev->bdev, &g_raid_if, 2544 process->window_offset, process->max_window_size, 2545 raid_bdev_process_window_range_unlocked, process); 2546 if (rc != 0) { 2547 raid_bdev_process_window_range_unlocked(process, rc); 2548 } 2549 } 2550 2551 static void 2552 raid_bdev_process_channels_update_done(struct spdk_io_channel_iter *i, int status) 2553 { 2554 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2555 2556 raid_bdev_process_unlock_window_range(process); 2557 } 2558 2559 static void 2560 raid_bdev_process_channel_update(struct spdk_io_channel_iter *i) 2561 { 2562 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2563 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2564 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2565 2566 raid_ch->process.offset = process->window_offset + process->window_size; 2567 2568 spdk_for_each_channel_continue(i, 0); 2569 } 2570 2571 void 2572 raid_bdev_process_request_complete(struct raid_bdev_process_request *process_req, int status) 2573 { 2574 struct raid_bdev_process *process = process_req->process; 2575 2576 TAILQ_INSERT_TAIL(&process->requests, process_req, link); 2577 2578 assert(spdk_get_thread() == process->thread); 2579 assert(process->window_remaining >= process_req->num_blocks); 2580 2581 if (status != 0) { 2582 process->window_status = status; 2583 } 2584 2585 process->window_remaining -= process_req->num_blocks; 2586 if (process->window_remaining == 0) { 2587 if (process->window_status != 0) { 2588 raid_bdev_process_finish(process, process->window_status); 2589 return; 2590 } 2591 2592 spdk_for_each_channel(process->raid_bdev, raid_bdev_process_channel_update, process, 2593 raid_bdev_process_channels_update_done); 2594 } 2595 } 2596 2597 static int 2598 raid_bdev_submit_process_request(struct raid_bdev_process *process, uint64_t offset_blocks, 2599 uint32_t num_blocks) 2600 { 2601 struct raid_bdev *raid_bdev = process->raid_bdev; 2602 struct raid_bdev_process_request *process_req; 2603 int ret; 2604 2605 process_req = TAILQ_FIRST(&process->requests); 2606 if (process_req == NULL) { 2607 assert(process->window_remaining > 0); 2608 return 0; 2609 } 2610 2611 process_req->target = process->target; 2612 process_req->target_ch = process->raid_ch->process.target_ch; 2613 process_req->offset_blocks = offset_blocks; 2614 process_req->num_blocks = num_blocks; 2615 process_req->iov.iov_len = num_blocks * raid_bdev->bdev.blocklen; 2616 2617 ret = raid_bdev->module->submit_process_request(process_req, process->raid_ch); 2618 if (ret <= 0) { 2619 if (ret < 0) { 2620 SPDK_ERRLOG("Failed to submit process request on %s: %s\n", 2621 raid_bdev->bdev.name, spdk_strerror(-ret)); 2622 process->window_status = ret; 2623 } 2624 return ret; 2625 } 2626 2627 process_req->num_blocks = ret; 2628 TAILQ_REMOVE(&process->requests, process_req, link); 2629 2630 return ret; 2631 } 2632 2633 static void 2634 _raid_bdev_process_thread_run(struct raid_bdev_process *process) 2635 { 2636 struct raid_bdev *raid_bdev = process->raid_bdev; 2637 uint64_t offset = process->window_offset; 2638 const uint64_t offset_end = spdk_min(offset + process->max_window_size, raid_bdev->bdev.blockcnt); 2639 int ret; 2640 2641 while (offset < offset_end) { 2642 ret = raid_bdev_submit_process_request(process, offset, offset_end - offset); 2643 if (ret <= 0) { 2644 break; 2645 } 2646 2647 process->window_remaining += ret; 2648 offset += ret; 2649 } 2650 2651 if (process->window_remaining > 0) { 2652 process->window_size = process->window_remaining; 2653 } else { 2654 raid_bdev_process_finish(process, process->window_status); 2655 } 2656 } 2657 2658 static void 2659 raid_bdev_process_window_range_locked(void *ctx, int status) 2660 { 2661 struct raid_bdev_process *process = ctx; 2662 2663 if (status != 0) { 2664 SPDK_ERRLOG("Failed to lock LBA range: %s\n", spdk_strerror(-status)); 2665 raid_bdev_process_finish(process, status); 2666 return; 2667 } 2668 2669 process->window_range_locked = true; 2670 2671 if (process->state == RAID_PROCESS_STATE_STOPPING) { 2672 raid_bdev_process_unlock_window_range(process); 2673 return; 2674 } 2675 2676 _raid_bdev_process_thread_run(process); 2677 } 2678 2679 static void 2680 raid_bdev_process_thread_run(struct raid_bdev_process *process) 2681 { 2682 struct raid_bdev *raid_bdev = process->raid_bdev; 2683 int rc; 2684 2685 assert(spdk_get_thread() == process->thread); 2686 assert(process->window_remaining == 0); 2687 assert(process->window_range_locked == false); 2688 2689 if (process->state == RAID_PROCESS_STATE_STOPPING) { 2690 raid_bdev_process_do_finish(process); 2691 return; 2692 } 2693 2694 if (process->window_offset == raid_bdev->bdev.blockcnt) { 2695 SPDK_DEBUGLOG(bdev_raid, "process completed on %s\n", raid_bdev->bdev.name); 2696 raid_bdev_process_finish(process, 0); 2697 return; 2698 } 2699 2700 process->max_window_size = spdk_min(raid_bdev->bdev.blockcnt - process->window_offset, 2701 process->max_window_size); 2702 2703 rc = spdk_bdev_quiesce_range(&raid_bdev->bdev, &g_raid_if, 2704 process->window_offset, process->max_window_size, 2705 raid_bdev_process_window_range_locked, process); 2706 if (rc != 0) { 2707 raid_bdev_process_window_range_locked(process, rc); 2708 } 2709 } 2710 2711 static void 2712 raid_bdev_process_thread_init(void *ctx) 2713 { 2714 struct raid_bdev_process *process = ctx; 2715 struct raid_bdev *raid_bdev = process->raid_bdev; 2716 struct spdk_io_channel *ch; 2717 2718 process->thread = spdk_get_thread(); 2719 2720 ch = spdk_get_io_channel(raid_bdev); 2721 if (ch == NULL) { 2722 process->status = -ENOMEM; 2723 raid_bdev_process_do_finish(process); 2724 return; 2725 } 2726 2727 process->raid_ch = spdk_io_channel_get_ctx(ch); 2728 process->state = RAID_PROCESS_STATE_RUNNING; 2729 2730 SPDK_NOTICELOG("Started %s on raid bdev %s\n", 2731 raid_bdev_process_to_str(process->type), raid_bdev->bdev.name); 2732 2733 raid_bdev_process_thread_run(process); 2734 } 2735 2736 static void 2737 raid_bdev_channels_abort_start_process_done(struct spdk_io_channel_iter *i, int status) 2738 { 2739 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2740 2741 _raid_bdev_remove_base_bdev(process->target, NULL, NULL); 2742 raid_bdev_process_free(process); 2743 2744 /* TODO: update sb */ 2745 } 2746 2747 static void 2748 raid_bdev_channel_abort_start_process(struct spdk_io_channel_iter *i) 2749 { 2750 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2751 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2752 2753 raid_bdev_ch_process_cleanup(raid_ch); 2754 2755 spdk_for_each_channel_continue(i, 0); 2756 } 2757 2758 static void 2759 raid_bdev_channels_start_process_done(struct spdk_io_channel_iter *i, int status) 2760 { 2761 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2762 struct raid_bdev *raid_bdev = process->raid_bdev; 2763 struct spdk_thread *thread; 2764 char thread_name[RAID_BDEV_SB_NAME_SIZE + 16]; 2765 2766 if (status != 0) { 2767 SPDK_ERRLOG("Failed to start %s on %s: %s\n", 2768 raid_bdev_process_to_str(process->type), raid_bdev->bdev.name, 2769 spdk_strerror(-status)); 2770 goto err; 2771 } 2772 2773 /* TODO: we may need to abort if a base bdev was removed before we got here */ 2774 2775 snprintf(thread_name, sizeof(thread_name), "%s_%s", 2776 raid_bdev->bdev.name, raid_bdev_process_to_str(process->type)); 2777 2778 thread = spdk_thread_create(thread_name, NULL); 2779 if (thread == NULL) { 2780 SPDK_ERRLOG("Failed to create %s thread for %s\n", 2781 raid_bdev_process_to_str(process->type), raid_bdev->bdev.name); 2782 goto err; 2783 } 2784 2785 raid_bdev->process = process; 2786 2787 spdk_thread_send_msg(thread, raid_bdev_process_thread_init, process); 2788 2789 return; 2790 err: 2791 spdk_for_each_channel(process->raid_bdev, raid_bdev_channel_abort_start_process, process, 2792 raid_bdev_channels_abort_start_process_done); 2793 } 2794 2795 static void 2796 raid_bdev_channel_start_process(struct spdk_io_channel_iter *i) 2797 { 2798 struct raid_bdev_process *process = spdk_io_channel_iter_get_ctx(i); 2799 struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i); 2800 struct raid_bdev_io_channel *raid_ch = spdk_io_channel_get_ctx(ch); 2801 int rc; 2802 2803 rc = raid_bdev_ch_process_setup(raid_ch, process); 2804 2805 spdk_for_each_channel_continue(i, rc); 2806 } 2807 2808 static void 2809 raid_bdev_process_start(struct raid_bdev_process *process) 2810 { 2811 struct raid_bdev *raid_bdev = process->raid_bdev; 2812 2813 assert(raid_bdev->module->submit_process_request != NULL); 2814 2815 spdk_for_each_channel(raid_bdev, raid_bdev_channel_start_process, process, 2816 raid_bdev_channels_start_process_done); 2817 } 2818 2819 static void 2820 raid_bdev_process_request_free(struct raid_bdev_process_request *process_req) 2821 { 2822 spdk_dma_free(process_req->iov.iov_base); 2823 spdk_dma_free(process_req->md_buf); 2824 free(process_req); 2825 } 2826 2827 static struct raid_bdev_process_request * 2828 raid_bdev_process_alloc_request(struct raid_bdev_process *process) 2829 { 2830 struct raid_bdev *raid_bdev = process->raid_bdev; 2831 struct raid_bdev_process_request *process_req; 2832 2833 process_req = calloc(1, sizeof(*process_req)); 2834 if (process_req == NULL) { 2835 return NULL; 2836 } 2837 2838 process_req->process = process; 2839 process_req->iov.iov_len = process->max_window_size * raid_bdev->bdev.blocklen; 2840 process_req->iov.iov_base = spdk_dma_malloc(process_req->iov.iov_len, 4096, 0); 2841 if (process_req->iov.iov_base == NULL) { 2842 free(process_req); 2843 return NULL; 2844 } 2845 if (spdk_bdev_is_md_separate(&raid_bdev->bdev)) { 2846 process_req->md_buf = spdk_dma_malloc(process->max_window_size * raid_bdev->bdev.md_len, 4096, 0); 2847 if (process_req->md_buf == NULL) { 2848 raid_bdev_process_request_free(process_req); 2849 return NULL; 2850 } 2851 } 2852 2853 return process_req; 2854 } 2855 2856 static void 2857 raid_bdev_process_free(struct raid_bdev_process *process) 2858 { 2859 struct raid_bdev_process_request *process_req; 2860 2861 while ((process_req = TAILQ_FIRST(&process->requests)) != NULL) { 2862 TAILQ_REMOVE(&process->requests, process_req, link); 2863 raid_bdev_process_request_free(process_req); 2864 } 2865 2866 free(process); 2867 } 2868 2869 static struct raid_bdev_process * 2870 raid_bdev_process_alloc(struct raid_bdev *raid_bdev, enum raid_process_type type, 2871 struct raid_base_bdev_info *target) 2872 { 2873 struct raid_bdev_process *process; 2874 struct raid_bdev_process_request *process_req; 2875 int i; 2876 2877 process = calloc(1, sizeof(*process)); 2878 if (process == NULL) { 2879 return NULL; 2880 } 2881 2882 process->raid_bdev = raid_bdev; 2883 process->type = type; 2884 process->target = target; 2885 process->max_window_size = spdk_max(spdk_divide_round_up(g_opts.process_window_size_kb * 1024UL, 2886 spdk_bdev_get_data_block_size(&raid_bdev->bdev)), 2887 raid_bdev->bdev.write_unit_size); 2888 TAILQ_INIT(&process->requests); 2889 TAILQ_INIT(&process->finish_actions); 2890 2891 for (i = 0; i < RAID_BDEV_PROCESS_MAX_QD; i++) { 2892 process_req = raid_bdev_process_alloc_request(process); 2893 if (process_req == NULL) { 2894 raid_bdev_process_free(process); 2895 return NULL; 2896 } 2897 2898 TAILQ_INSERT_TAIL(&process->requests, process_req, link); 2899 } 2900 2901 return process; 2902 } 2903 2904 static int 2905 raid_bdev_start_rebuild(struct raid_base_bdev_info *target) 2906 { 2907 struct raid_bdev_process *process; 2908 2909 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 2910 2911 process = raid_bdev_process_alloc(target->raid_bdev, RAID_PROCESS_REBUILD, target); 2912 if (process == NULL) { 2913 return -ENOMEM; 2914 } 2915 2916 raid_bdev_process_start(process); 2917 2918 return 0; 2919 } 2920 2921 static void 2922 raid_bdev_configure_base_bdev_cont(struct raid_base_bdev_info *base_info) 2923 { 2924 struct raid_bdev *raid_bdev = base_info->raid_bdev; 2925 int rc; 2926 2927 /* TODO: defer if rebuild in progress on another base bdev */ 2928 assert(raid_bdev->process == NULL); 2929 2930 base_info->is_configured = true; 2931 2932 raid_bdev->num_base_bdevs_discovered++; 2933 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 2934 assert(raid_bdev->num_base_bdevs_operational <= raid_bdev->num_base_bdevs); 2935 assert(raid_bdev->num_base_bdevs_operational >= raid_bdev->min_base_bdevs_operational); 2936 2937 /* 2938 * Configure the raid bdev when the number of discovered base bdevs reaches the number 2939 * of base bdevs we know to be operational members of the array. Usually this is equal 2940 * to the total number of base bdevs (num_base_bdevs) but can be less - when the array is 2941 * degraded. 2942 */ 2943 if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs_operational) { 2944 rc = raid_bdev_configure(raid_bdev); 2945 if (rc != 0) { 2946 SPDK_ERRLOG("Failed to configure raid bdev: %s\n", spdk_strerror(-rc)); 2947 } 2948 } else if (raid_bdev->num_base_bdevs_discovered > raid_bdev->num_base_bdevs_operational) { 2949 assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE); 2950 raid_bdev->num_base_bdevs_operational++; 2951 rc = raid_bdev_start_rebuild(base_info); 2952 if (rc != 0) { 2953 SPDK_ERRLOG("Failed to start rebuild: %s\n", spdk_strerror(-rc)); 2954 _raid_bdev_remove_base_bdev(base_info, NULL, NULL); 2955 } 2956 } else { 2957 rc = 0; 2958 } 2959 2960 if (base_info->configure_cb != NULL) { 2961 base_info->configure_cb(base_info->configure_cb_ctx, rc); 2962 } 2963 } 2964 2965 static void 2966 raid_bdev_configure_base_bdev_check_sb_cb(const struct raid_bdev_superblock *sb, int status, 2967 void *ctx) 2968 { 2969 struct raid_base_bdev_info *base_info = ctx; 2970 2971 switch (status) { 2972 case 0: 2973 /* valid superblock found */ 2974 SPDK_ERRLOG("Existing raid superblock found on bdev %s\n", base_info->name); 2975 status = -EEXIST; 2976 raid_bdev_free_base_bdev_resource(base_info); 2977 break; 2978 case -EINVAL: 2979 /* no valid superblock */ 2980 raid_bdev_configure_base_bdev_cont(base_info); 2981 return; 2982 default: 2983 SPDK_ERRLOG("Failed to examine bdev %s: %s\n", 2984 base_info->name, spdk_strerror(-status)); 2985 break; 2986 } 2987 2988 if (base_info->configure_cb != NULL) { 2989 base_info->configure_cb(base_info->configure_cb_ctx, status); 2990 } 2991 } 2992 2993 static int 2994 raid_bdev_configure_base_bdev(struct raid_base_bdev_info *base_info, bool existing, 2995 raid_base_bdev_cb cb_fn, void *cb_ctx) 2996 { 2997 struct raid_bdev *raid_bdev = base_info->raid_bdev; 2998 struct spdk_bdev_desc *desc; 2999 struct spdk_bdev *bdev; 3000 const struct spdk_uuid *bdev_uuid; 3001 int rc; 3002 3003 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 3004 assert(base_info->desc == NULL); 3005 3006 /* 3007 * Base bdev can be added by name or uuid. Here we assure both properties are set and valid 3008 * before claiming the bdev. 3009 */ 3010 3011 if (!spdk_uuid_is_null(&base_info->uuid)) { 3012 char uuid_str[SPDK_UUID_STRING_LEN]; 3013 const char *bdev_name; 3014 3015 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid); 3016 3017 /* UUID of a bdev is registered as its alias */ 3018 bdev = spdk_bdev_get_by_name(uuid_str); 3019 if (bdev == NULL) { 3020 return -ENODEV; 3021 } 3022 3023 bdev_name = spdk_bdev_get_name(bdev); 3024 3025 if (base_info->name == NULL) { 3026 assert(existing == true); 3027 base_info->name = strdup(bdev_name); 3028 if (base_info->name == NULL) { 3029 return -ENOMEM; 3030 } 3031 } else if (strcmp(base_info->name, bdev_name) != 0) { 3032 SPDK_ERRLOG("Name mismatch for base bdev '%s' - expected '%s'\n", 3033 bdev_name, base_info->name); 3034 return -EINVAL; 3035 } 3036 } 3037 3038 assert(base_info->name != NULL); 3039 3040 rc = spdk_bdev_open_ext(base_info->name, true, raid_bdev_event_base_bdev, NULL, &desc); 3041 if (rc != 0) { 3042 if (rc != -ENODEV) { 3043 SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", base_info->name); 3044 } 3045 return rc; 3046 } 3047 3048 bdev = spdk_bdev_desc_get_bdev(desc); 3049 bdev_uuid = spdk_bdev_get_uuid(bdev); 3050 3051 if (spdk_uuid_is_null(&base_info->uuid)) { 3052 spdk_uuid_copy(&base_info->uuid, bdev_uuid); 3053 } else if (spdk_uuid_compare(&base_info->uuid, bdev_uuid) != 0) { 3054 SPDK_ERRLOG("UUID mismatch for base bdev '%s'\n", base_info->name); 3055 spdk_bdev_close(desc); 3056 return -EINVAL; 3057 } 3058 3059 rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if); 3060 if (rc != 0) { 3061 SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n"); 3062 spdk_bdev_close(desc); 3063 return rc; 3064 } 3065 3066 SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev->name); 3067 3068 base_info->app_thread_ch = spdk_bdev_get_io_channel(desc); 3069 if (base_info->app_thread_ch == NULL) { 3070 SPDK_ERRLOG("Failed to get io channel\n"); 3071 spdk_bdev_module_release_bdev(bdev); 3072 spdk_bdev_close(desc); 3073 return -ENOMEM; 3074 } 3075 3076 base_info->desc = desc; 3077 base_info->blockcnt = bdev->blockcnt; 3078 3079 if (raid_bdev->superblock_enabled) { 3080 uint64_t data_offset; 3081 3082 if (base_info->data_offset == 0) { 3083 assert((RAID_BDEV_MIN_DATA_OFFSET_SIZE % spdk_bdev_get_data_block_size(bdev)) == 0); 3084 data_offset = RAID_BDEV_MIN_DATA_OFFSET_SIZE / spdk_bdev_get_data_block_size(bdev); 3085 } else { 3086 data_offset = base_info->data_offset; 3087 } 3088 3089 if (bdev->optimal_io_boundary != 0) { 3090 data_offset = spdk_divide_round_up(data_offset, 3091 bdev->optimal_io_boundary) * bdev->optimal_io_boundary; 3092 if (base_info->data_offset != 0 && base_info->data_offset != data_offset) { 3093 SPDK_WARNLOG("Data offset %lu on bdev '%s' is different than optimal value %lu\n", 3094 base_info->data_offset, base_info->name, data_offset); 3095 data_offset = base_info->data_offset; 3096 } 3097 } 3098 3099 base_info->data_offset = data_offset; 3100 } 3101 3102 if (base_info->data_offset >= bdev->blockcnt) { 3103 SPDK_ERRLOG("Data offset %lu exceeds base bdev capacity %lu on bdev '%s'\n", 3104 base_info->data_offset, bdev->blockcnt, base_info->name); 3105 rc = -EINVAL; 3106 goto out; 3107 } 3108 3109 if (base_info->data_size == 0) { 3110 base_info->data_size = bdev->blockcnt - base_info->data_offset; 3111 } else if (base_info->data_offset + base_info->data_size > bdev->blockcnt) { 3112 SPDK_ERRLOG("Data offset and size exceeds base bdev capacity %lu on bdev '%s'\n", 3113 bdev->blockcnt, base_info->name); 3114 rc = -EINVAL; 3115 goto out; 3116 } 3117 3118 if (!raid_bdev->module->dif_supported && spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 3119 SPDK_ERRLOG("Base bdev '%s' has DIF or DIX enabled - unsupported RAID configuration\n", 3120 bdev->name); 3121 rc = -EINVAL; 3122 goto out; 3123 } 3124 3125 /* 3126 * Set the raid bdev properties if this is the first base bdev configured, 3127 * otherwise - verify. Assumption is that all the base bdevs for any raid bdev should 3128 * have the same blocklen and metadata format. 3129 */ 3130 if (raid_bdev->bdev.blocklen == 0) { 3131 raid_bdev->bdev.blocklen = bdev->blocklen; 3132 raid_bdev->bdev.md_len = spdk_bdev_get_md_size(bdev); 3133 raid_bdev->bdev.md_interleave = spdk_bdev_is_md_interleaved(bdev); 3134 raid_bdev->bdev.dif_type = spdk_bdev_get_dif_type(bdev); 3135 raid_bdev->bdev.dif_check_flags = bdev->dif_check_flags; 3136 raid_bdev->bdev.dif_is_head_of_md = spdk_bdev_is_dif_head_of_md(bdev); 3137 } else { 3138 if (raid_bdev->bdev.blocklen != bdev->blocklen) { 3139 SPDK_ERRLOG("Raid bdev '%s' blocklen %u differs from base bdev '%s' blocklen %u\n", 3140 raid_bdev->bdev.name, raid_bdev->bdev.blocklen, bdev->name, bdev->blocklen); 3141 rc = -EINVAL; 3142 goto out; 3143 } 3144 3145 if (raid_bdev->bdev.md_len != spdk_bdev_get_md_size(bdev) || 3146 raid_bdev->bdev.md_interleave != spdk_bdev_is_md_interleaved(bdev) || 3147 raid_bdev->bdev.dif_type != spdk_bdev_get_dif_type(bdev) || 3148 raid_bdev->bdev.dif_check_flags != bdev->dif_check_flags || 3149 raid_bdev->bdev.dif_is_head_of_md != spdk_bdev_is_dif_head_of_md(bdev)) { 3150 SPDK_ERRLOG("Raid bdev '%s' has different metadata format than base bdev '%s'\n", 3151 raid_bdev->bdev.name, bdev->name); 3152 rc = -EINVAL; 3153 goto out; 3154 } 3155 } 3156 3157 base_info->configure_cb = cb_fn; 3158 base_info->configure_cb_ctx = cb_ctx; 3159 3160 if (existing) { 3161 raid_bdev_configure_base_bdev_cont(base_info); 3162 } else { 3163 /* check for existing superblock when using a new bdev */ 3164 rc = raid_bdev_load_base_bdev_superblock(desc, base_info->app_thread_ch, 3165 raid_bdev_configure_base_bdev_check_sb_cb, base_info); 3166 if (rc) { 3167 SPDK_ERRLOG("Failed to read bdev %s superblock: %s\n", 3168 bdev->name, spdk_strerror(-rc)); 3169 } 3170 } 3171 out: 3172 if (rc != 0) { 3173 raid_bdev_free_base_bdev_resource(base_info); 3174 } 3175 return rc; 3176 } 3177 3178 static int 3179 _raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot, 3180 uint64_t data_offset, uint64_t data_size, 3181 raid_base_bdev_cb cb_fn, void *cb_ctx) 3182 { 3183 struct raid_base_bdev_info *base_info; 3184 3185 assert(name != NULL); 3186 3187 if (slot >= raid_bdev->num_base_bdevs) { 3188 return -EINVAL; 3189 } 3190 3191 base_info = &raid_bdev->base_bdev_info[slot]; 3192 3193 if (base_info->name != NULL) { 3194 SPDK_ERRLOG("Slot %u on raid bdev '%s' already assigned to bdev '%s'\n", 3195 slot, raid_bdev->bdev.name, base_info->name); 3196 return -EBUSY; 3197 } 3198 3199 if (!spdk_uuid_is_null(&base_info->uuid)) { 3200 char uuid_str[SPDK_UUID_STRING_LEN]; 3201 3202 spdk_uuid_fmt_lower(uuid_str, sizeof(uuid_str), &base_info->uuid); 3203 SPDK_ERRLOG("Slot %u on raid bdev '%s' already assigned to bdev with uuid %s\n", 3204 slot, raid_bdev->bdev.name, uuid_str); 3205 return -EBUSY; 3206 } 3207 3208 base_info->name = strdup(name); 3209 if (base_info->name == NULL) { 3210 return -ENOMEM; 3211 } 3212 3213 base_info->data_offset = data_offset; 3214 base_info->data_size = data_size; 3215 3216 return raid_bdev_configure_base_bdev(base_info, false, cb_fn, cb_ctx); 3217 } 3218 3219 int 3220 raid_bdev_attach_base_bdev(struct raid_bdev *raid_bdev, struct spdk_bdev *base_bdev, 3221 raid_base_bdev_cb cb_fn, void *cb_ctx) 3222 { 3223 struct raid_base_bdev_info *base_info = NULL, *iter; 3224 int rc; 3225 3226 SPDK_DEBUGLOG(bdev_raid, "attach_base_device: %s\n", base_bdev->name); 3227 3228 assert(spdk_get_thread() == spdk_thread_get_app_thread()); 3229 3230 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 3231 SPDK_ERRLOG("raid bdev '%s' must be in online state to attach base bdev\n", 3232 raid_bdev->bdev.name); 3233 return -EINVAL; 3234 } 3235 3236 RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) { 3237 if (iter->desc == NULL) { 3238 base_info = iter; 3239 break; 3240 } 3241 } 3242 3243 if (base_info == NULL) { 3244 SPDK_ERRLOG("no empty slot found in raid bdev '%s' for new base bdev '%s'\n", 3245 raid_bdev->bdev.name, base_bdev->name); 3246 return -EINVAL; 3247 } 3248 3249 assert(base_info->is_configured == false); 3250 assert(base_info->data_size != 0); 3251 3252 spdk_spin_lock(&raid_bdev->base_bdev_lock); 3253 3254 rc = _raid_bdev_add_base_device(raid_bdev, base_bdev->name, 3255 raid_bdev_base_bdev_slot(base_info), 3256 base_info->data_offset, base_info->data_size, 3257 cb_fn, cb_ctx); 3258 if (rc != 0) { 3259 SPDK_ERRLOG("base bdev '%s' attach failed: %s\n", base_bdev->name, spdk_strerror(-rc)); 3260 raid_bdev_free_base_bdev_resource(base_info); 3261 } 3262 3263 spdk_spin_unlock(&raid_bdev->base_bdev_lock); 3264 3265 return rc; 3266 } 3267 3268 /* 3269 * brief: 3270 * raid_bdev_add_base_device function is the actual function which either adds 3271 * the nvme base device to existing raid bdev or create a new raid bdev. It also claims 3272 * the base device and keep the open descriptor. 3273 * params: 3274 * raid_bdev - pointer to raid bdev 3275 * name - name of the base bdev 3276 * slot - position to add base bdev 3277 * cb_fn - callback function 3278 * cb_ctx - argument to callback function 3279 * returns: 3280 * 0 - success 3281 * non zero - failure 3282 */ 3283 int 3284 raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot, 3285 raid_base_bdev_cb cb_fn, void *cb_ctx) 3286 { 3287 return _raid_bdev_add_base_device(raid_bdev, name, slot, 0, 0, cb_fn, cb_ctx); 3288 } 3289 3290 static int 3291 raid_bdev_create_from_sb(const struct raid_bdev_superblock *sb, struct raid_bdev **raid_bdev_out) 3292 { 3293 struct raid_bdev *raid_bdev; 3294 uint8_t i; 3295 int rc; 3296 3297 rc = _raid_bdev_create(sb->name, (sb->strip_size * sb->block_size) / 1024, sb->num_base_bdevs, 3298 sb->level, true, &sb->uuid, &raid_bdev); 3299 if (rc != 0) { 3300 return rc; 3301 } 3302 3303 rc = raid_bdev_alloc_superblock(raid_bdev, sb->block_size); 3304 if (rc != 0) { 3305 raid_bdev_free(raid_bdev); 3306 return rc; 3307 } 3308 3309 assert(sb->length <= RAID_BDEV_SB_MAX_LENGTH); 3310 memcpy(raid_bdev->sb, sb, sb->length); 3311 3312 for (i = 0; i < sb->base_bdevs_size; i++) { 3313 const struct raid_bdev_sb_base_bdev *sb_base_bdev = &sb->base_bdevs[i]; 3314 struct raid_base_bdev_info *base_info = &raid_bdev->base_bdev_info[sb_base_bdev->slot]; 3315 3316 if (sb_base_bdev->state == RAID_SB_BASE_BDEV_CONFIGURED) { 3317 spdk_uuid_copy(&base_info->uuid, &sb_base_bdev->uuid); 3318 raid_bdev->num_base_bdevs_operational++; 3319 } 3320 3321 base_info->data_offset = sb_base_bdev->data_offset; 3322 base_info->data_size = sb_base_bdev->data_size; 3323 } 3324 3325 *raid_bdev_out = raid_bdev; 3326 return 0; 3327 } 3328 3329 static void 3330 raid_bdev_examine_no_sb(struct spdk_bdev *bdev) 3331 { 3332 struct raid_bdev *raid_bdev; 3333 struct raid_base_bdev_info *base_info; 3334 3335 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 3336 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 3337 if (base_info->desc == NULL && base_info->name != NULL && 3338 strcmp(bdev->name, base_info->name) == 0) { 3339 raid_bdev_configure_base_bdev(base_info, true, NULL, NULL); 3340 break; 3341 } 3342 } 3343 } 3344 } 3345 3346 static void 3347 raid_bdev_examine_sb(const struct raid_bdev_superblock *sb, struct spdk_bdev *bdev) 3348 { 3349 const struct raid_bdev_sb_base_bdev *sb_base_bdev = NULL; 3350 struct raid_bdev *raid_bdev; 3351 struct raid_base_bdev_info *iter, *base_info; 3352 uint8_t i; 3353 int rc; 3354 3355 if (sb->block_size != spdk_bdev_get_data_block_size(bdev)) { 3356 SPDK_WARNLOG("Bdev %s block size (%u) does not match the value in superblock (%u)\n", 3357 bdev->name, sb->block_size, spdk_bdev_get_data_block_size(bdev)); 3358 return; 3359 } 3360 3361 if (spdk_uuid_is_null(&sb->uuid)) { 3362 SPDK_WARNLOG("NULL raid bdev UUID in superblock on bdev %s\n", bdev->name); 3363 return; 3364 } 3365 3366 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 3367 if (spdk_uuid_compare(&raid_bdev->bdev.uuid, &sb->uuid) == 0) { 3368 break; 3369 } 3370 } 3371 3372 if (raid_bdev) { 3373 if (sb->seq_number > raid_bdev->sb->seq_number) { 3374 SPDK_DEBUGLOG(bdev_raid, 3375 "raid superblock seq_number on bdev %s (%lu) greater than existing raid bdev %s (%lu)\n", 3376 bdev->name, sb->seq_number, raid_bdev->bdev.name, raid_bdev->sb->seq_number); 3377 3378 if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) { 3379 SPDK_WARNLOG("Newer version of raid bdev %s superblock found on bdev %s but raid bdev is not in configuring state.\n", 3380 raid_bdev->bdev.name, bdev->name); 3381 return; 3382 } 3383 3384 /* remove and then recreate the raid bdev using the newer superblock */ 3385 raid_bdev_delete(raid_bdev, NULL, NULL); 3386 raid_bdev = NULL; 3387 } else if (sb->seq_number < raid_bdev->sb->seq_number) { 3388 SPDK_DEBUGLOG(bdev_raid, 3389 "raid superblock seq_number on bdev %s (%lu) smaller than existing raid bdev %s (%lu)\n", 3390 bdev->name, sb->seq_number, raid_bdev->bdev.name, raid_bdev->sb->seq_number); 3391 /* use the current raid bdev superblock */ 3392 sb = raid_bdev->sb; 3393 } 3394 } 3395 3396 for (i = 0; i < sb->base_bdevs_size; i++) { 3397 sb_base_bdev = &sb->base_bdevs[i]; 3398 3399 assert(spdk_uuid_is_null(&sb_base_bdev->uuid) == false); 3400 3401 if (spdk_uuid_compare(&sb_base_bdev->uuid, spdk_bdev_get_uuid(bdev)) == 0) { 3402 break; 3403 } 3404 } 3405 3406 if (i == sb->base_bdevs_size) { 3407 SPDK_DEBUGLOG(bdev_raid, "raid superblock does not contain this bdev's uuid\n"); 3408 return; 3409 } 3410 3411 if (!raid_bdev) { 3412 rc = raid_bdev_create_from_sb(sb, &raid_bdev); 3413 if (rc != 0) { 3414 SPDK_ERRLOG("Failed to create raid bdev %s: %s\n", 3415 sb->name, spdk_strerror(-rc)); 3416 return; 3417 } 3418 } 3419 3420 if (sb_base_bdev->state != RAID_SB_BASE_BDEV_CONFIGURED) { 3421 SPDK_NOTICELOG("Bdev %s is not an active member of raid bdev %s. Ignoring.\n", 3422 bdev->name, raid_bdev->bdev.name); 3423 return; 3424 } 3425 3426 base_info = NULL; 3427 RAID_FOR_EACH_BASE_BDEV(raid_bdev, iter) { 3428 if (spdk_uuid_compare(&iter->uuid, spdk_bdev_get_uuid(bdev)) == 0) { 3429 base_info = iter; 3430 break; 3431 } 3432 } 3433 3434 if (base_info == NULL) { 3435 SPDK_ERRLOG("Bdev %s is not a member of raid bdev %s\n", 3436 bdev->name, raid_bdev->bdev.name); 3437 return; 3438 } 3439 3440 rc = raid_bdev_configure_base_bdev(base_info, true, NULL, NULL); 3441 if (rc != 0) { 3442 SPDK_ERRLOG("Failed to configure bdev %s as base bdev of raid %s: %s\n", 3443 bdev->name, raid_bdev->bdev.name, spdk_strerror(-rc)); 3444 } 3445 } 3446 3447 struct raid_bdev_examine_ctx { 3448 struct spdk_bdev_desc *desc; 3449 struct spdk_io_channel *ch; 3450 }; 3451 3452 static void 3453 raid_bdev_examine_ctx_free(struct raid_bdev_examine_ctx *ctx) 3454 { 3455 if (!ctx) { 3456 return; 3457 } 3458 3459 if (ctx->ch) { 3460 spdk_put_io_channel(ctx->ch); 3461 } 3462 3463 if (ctx->desc) { 3464 spdk_bdev_close(ctx->desc); 3465 } 3466 3467 free(ctx); 3468 } 3469 3470 static void 3471 raid_bdev_examine_load_sb_cb(const struct raid_bdev_superblock *sb, int status, void *_ctx) 3472 { 3473 struct raid_bdev_examine_ctx *ctx = _ctx; 3474 struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(ctx->desc); 3475 3476 switch (status) { 3477 case 0: 3478 /* valid superblock found */ 3479 SPDK_DEBUGLOG(bdev_raid, "raid superblock found on bdev %s\n", bdev->name); 3480 raid_bdev_examine_sb(sb, bdev); 3481 break; 3482 case -EINVAL: 3483 /* no valid superblock, check if it can be claimed anyway */ 3484 raid_bdev_examine_no_sb(bdev); 3485 break; 3486 default: 3487 SPDK_ERRLOG("Failed to examine bdev %s: %s\n", 3488 bdev->name, spdk_strerror(-status)); 3489 break; 3490 } 3491 3492 raid_bdev_examine_ctx_free(ctx); 3493 spdk_bdev_module_examine_done(&g_raid_if); 3494 } 3495 3496 static void 3497 raid_bdev_examine_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *event_ctx) 3498 { 3499 } 3500 3501 /* 3502 * brief: 3503 * raid_bdev_examine function is the examine function call by the below layers 3504 * like bdev_nvme layer. This function will check if this base bdev can be 3505 * claimed by this raid bdev or not. 3506 * params: 3507 * bdev - pointer to base bdev 3508 * returns: 3509 * none 3510 */ 3511 static void 3512 raid_bdev_examine(struct spdk_bdev *bdev) 3513 { 3514 struct raid_bdev_examine_ctx *ctx; 3515 int rc; 3516 3517 if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) { 3518 raid_bdev_examine_no_sb(bdev); 3519 spdk_bdev_module_examine_done(&g_raid_if); 3520 return; 3521 } 3522 3523 ctx = calloc(1, sizeof(*ctx)); 3524 if (!ctx) { 3525 SPDK_ERRLOG("Failed to examine bdev %s: %s\n", 3526 bdev->name, spdk_strerror(ENOMEM)); 3527 goto err; 3528 } 3529 3530 rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, raid_bdev_examine_event_cb, NULL, 3531 &ctx->desc); 3532 if (rc) { 3533 SPDK_ERRLOG("Failed to open bdev %s: %s\n", 3534 bdev->name, spdk_strerror(-rc)); 3535 goto err; 3536 } 3537 3538 ctx->ch = spdk_bdev_get_io_channel(ctx->desc); 3539 if (!ctx->ch) { 3540 SPDK_ERRLOG("Failed to get io channel for bdev %s\n", bdev->name); 3541 goto err; 3542 } 3543 3544 rc = raid_bdev_load_base_bdev_superblock(ctx->desc, ctx->ch, raid_bdev_examine_load_sb_cb, ctx); 3545 if (rc) { 3546 SPDK_ERRLOG("Failed to read bdev %s superblock: %s\n", 3547 bdev->name, spdk_strerror(-rc)); 3548 goto err; 3549 } 3550 3551 return; 3552 err: 3553 raid_bdev_examine_ctx_free(ctx); 3554 spdk_bdev_module_examine_done(&g_raid_if); 3555 } 3556 3557 /* Log component for bdev raid bdev module */ 3558 SPDK_LOG_REGISTER_COMPONENT(bdev_raid) 3559