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