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/string.h" 15 16 static bool g_shutdown_started = false; 17 18 /* List of all raid bdevs */ 19 struct raid_all_tailq g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list); 20 21 static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules); 22 23 static struct raid_bdev_module * 24 raid_bdev_module_find(enum raid_level level) 25 { 26 struct raid_bdev_module *raid_module; 27 28 TAILQ_FOREACH(raid_module, &g_raid_modules, link) { 29 if (raid_module->level == level) { 30 return raid_module; 31 } 32 } 33 34 return NULL; 35 } 36 37 void 38 raid_bdev_module_list_add(struct raid_bdev_module *raid_module) 39 { 40 if (raid_bdev_module_find(raid_module->level) != NULL) { 41 SPDK_ERRLOG("module for raid level '%s' already registered.\n", 42 raid_bdev_level_to_str(raid_module->level)); 43 assert(false); 44 } else { 45 TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link); 46 } 47 } 48 49 /* Function declarations */ 50 static void raid_bdev_examine(struct spdk_bdev *bdev); 51 static int raid_bdev_init(void); 52 static void raid_bdev_deconfigure(struct raid_bdev *raid_bdev, 53 raid_bdev_destruct_cb cb_fn, void *cb_arg); 54 55 /* 56 * brief: 57 * raid_bdev_create_cb function is a cb function for raid bdev which creates the 58 * hierarchy from raid bdev to base bdev io channels. It will be called per core 59 * params: 60 * io_device - pointer to raid bdev io device represented by raid_bdev 61 * ctx_buf - pointer to context buffer for raid bdev io channel 62 * returns: 63 * 0 - success 64 * non zero - failure 65 */ 66 static int 67 raid_bdev_create_cb(void *io_device, void *ctx_buf) 68 { 69 struct raid_bdev *raid_bdev = io_device; 70 struct raid_bdev_io_channel *raid_ch = ctx_buf; 71 uint8_t i; 72 int ret = 0; 73 74 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_create_cb, %p\n", raid_ch); 75 76 assert(raid_bdev != NULL); 77 assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE); 78 79 raid_ch->num_channels = raid_bdev->num_base_bdevs; 80 81 raid_ch->base_channel = calloc(raid_ch->num_channels, 82 sizeof(struct spdk_io_channel *)); 83 if (!raid_ch->base_channel) { 84 SPDK_ERRLOG("Unable to allocate base bdevs io channel\n"); 85 return -ENOMEM; 86 } 87 for (i = 0; i < raid_ch->num_channels; i++) { 88 /* 89 * Get the spdk_io_channel for all the base bdevs. This is used during 90 * split logic to send the respective child bdev ios to respective base 91 * bdev io channel. 92 */ 93 raid_ch->base_channel[i] = spdk_bdev_get_io_channel( 94 raid_bdev->base_bdev_info[i].desc); 95 if (!raid_ch->base_channel[i]) { 96 SPDK_ERRLOG("Unable to create io channel for base bdev\n"); 97 ret = -ENOMEM; 98 break; 99 } 100 } 101 102 if (!ret && raid_bdev->module->get_io_channel) { 103 raid_ch->module_channel = raid_bdev->module->get_io_channel(raid_bdev); 104 if (!raid_ch->module_channel) { 105 SPDK_ERRLOG("Unable to create io channel for raid module\n"); 106 ret = -ENOMEM; 107 } 108 } 109 110 if (ret) { 111 uint8_t j; 112 113 for (j = 0; j < i; j++) { 114 spdk_put_io_channel(raid_ch->base_channel[j]); 115 } 116 free(raid_ch->base_channel); 117 raid_ch->base_channel = NULL; 118 } 119 return ret; 120 } 121 122 /* 123 * brief: 124 * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the 125 * hierarchy from raid bdev to base bdev io channels. It will be called per core 126 * params: 127 * io_device - pointer to raid bdev io device represented by raid_bdev 128 * ctx_buf - pointer to context buffer for raid bdev io channel 129 * returns: 130 * none 131 */ 132 static void 133 raid_bdev_destroy_cb(void *io_device, void *ctx_buf) 134 { 135 struct raid_bdev_io_channel *raid_ch = ctx_buf; 136 uint8_t i; 137 138 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destroy_cb\n"); 139 140 assert(raid_ch != NULL); 141 assert(raid_ch->base_channel); 142 143 if (raid_ch->module_channel) { 144 spdk_put_io_channel(raid_ch->module_channel); 145 } 146 147 for (i = 0; i < raid_ch->num_channels; i++) { 148 /* Free base bdev channels */ 149 assert(raid_ch->base_channel[i] != NULL); 150 spdk_put_io_channel(raid_ch->base_channel[i]); 151 } 152 free(raid_ch->base_channel); 153 raid_ch->base_channel = NULL; 154 } 155 156 /* 157 * brief: 158 * raid_bdev_cleanup is used to cleanup raid_bdev related data 159 * structures. 160 * params: 161 * raid_bdev - pointer to raid_bdev 162 * returns: 163 * none 164 */ 165 static void 166 raid_bdev_cleanup(struct raid_bdev *raid_bdev) 167 { 168 struct raid_base_bdev_info *base_info; 169 170 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_cleanup, %p name %s, state %u\n", 171 raid_bdev, raid_bdev->bdev.name, raid_bdev->state); 172 assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE); 173 174 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 175 assert(base_info->bdev == NULL); 176 assert(base_info->desc == NULL); 177 free(base_info->name); 178 } 179 180 TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link); 181 free(raid_bdev->base_bdev_info); 182 } 183 184 static void 185 raid_bdev_free(struct raid_bdev *raid_bdev) 186 { 187 free(raid_bdev->bdev.name); 188 free(raid_bdev); 189 } 190 191 static void 192 raid_bdev_cleanup_and_free(struct raid_bdev *raid_bdev) 193 { 194 raid_bdev_cleanup(raid_bdev); 195 raid_bdev_free(raid_bdev); 196 } 197 198 /* 199 * brief: 200 * wrapper for the bdev close operation 201 * params: 202 * base_info - raid base bdev info 203 * returns: 204 */ 205 static void 206 _raid_bdev_free_base_bdev_resource(void *ctx) 207 { 208 struct spdk_bdev_desc *desc = ctx; 209 210 spdk_bdev_close(desc); 211 } 212 213 214 /* 215 * brief: 216 * free resource of base bdev for raid bdev 217 * params: 218 * raid_bdev - pointer to raid bdev 219 * base_info - raid base bdev info 220 * returns: 221 * 0 - success 222 * non zero - failure 223 */ 224 static void 225 raid_bdev_free_base_bdev_resource(struct raid_bdev *raid_bdev, 226 struct raid_base_bdev_info *base_info) 227 { 228 free(base_info->name); 229 base_info->name = NULL; 230 231 if (base_info->bdev == NULL) { 232 return; 233 } 234 235 assert(base_info->desc); 236 spdk_bdev_module_release_bdev(base_info->bdev); 237 if (base_info->thread && base_info->thread != spdk_get_thread()) { 238 spdk_thread_send_msg(base_info->thread, _raid_bdev_free_base_bdev_resource, base_info->desc); 239 } else { 240 spdk_bdev_close(base_info->desc); 241 } 242 base_info->desc = NULL; 243 base_info->bdev = NULL; 244 245 assert(raid_bdev->num_base_bdevs_discovered); 246 raid_bdev->num_base_bdevs_discovered--; 247 } 248 249 static void 250 raid_bdev_io_device_unregister_cb(void *io_device) 251 { 252 struct raid_bdev *raid_bdev = io_device; 253 254 if (raid_bdev->num_base_bdevs_discovered == 0) { 255 /* Free raid_bdev when there are no base bdevs left */ 256 SPDK_DEBUGLOG(bdev_raid, "raid bdev base bdevs is 0, going to free all in destruct\n"); 257 raid_bdev_cleanup(raid_bdev); 258 spdk_bdev_destruct_done(&raid_bdev->bdev, 0); 259 raid_bdev_free(raid_bdev); 260 } else { 261 spdk_bdev_destruct_done(&raid_bdev->bdev, 0); 262 } 263 } 264 265 void 266 raid_bdev_module_stop_done(struct raid_bdev *raid_bdev) 267 { 268 if (raid_bdev->state != RAID_BDEV_STATE_CONFIGURING) { 269 spdk_io_device_unregister(raid_bdev, raid_bdev_io_device_unregister_cb); 270 } 271 } 272 273 /* 274 * brief: 275 * raid_bdev_destruct is the destruct function table pointer for raid bdev 276 * params: 277 * ctxt - pointer to raid_bdev 278 * returns: 279 * 1 - success (deferred completion) 280 */ 281 static int 282 raid_bdev_destruct(void *ctxt) 283 { 284 struct raid_bdev *raid_bdev = ctxt; 285 struct raid_base_bdev_info *base_info; 286 287 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_destruct\n"); 288 289 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 290 /* 291 * Close all base bdev descriptors for which call has come from below 292 * layers. Also close the descriptors if we have started shutdown. 293 */ 294 if (g_shutdown_started || base_info->remove_scheduled == true) { 295 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 296 } 297 } 298 299 if (g_shutdown_started) { 300 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 301 } 302 303 if (raid_bdev->module->stop != NULL) { 304 if (raid_bdev->module->stop(raid_bdev) == false) { 305 return 1; 306 } 307 } 308 309 raid_bdev_module_stop_done(raid_bdev); 310 311 return 1; 312 } 313 314 void 315 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status) 316 { 317 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io); 318 319 spdk_bdev_io_complete(bdev_io, status); 320 } 321 322 /* 323 * brief: 324 * raid_bdev_io_complete_part - signal the completion of a part of the expected 325 * base bdev IOs and complete the raid_io if this is the final expected IO. 326 * The caller should first set raid_io->base_bdev_io_remaining. This function 327 * will decrement this counter by the value of the 'completed' parameter and 328 * complete the raid_io if the counter reaches 0. The caller is free to 329 * interpret the 'base_bdev_io_remaining' and 'completed' values as needed, 330 * it can represent e.g. blocks or IOs. 331 * params: 332 * raid_io - pointer to raid_bdev_io 333 * completed - the part of the raid_io that has been completed 334 * status - status of the base IO 335 * returns: 336 * true - if the raid_io is completed 337 * false - otherwise 338 */ 339 bool 340 raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, 341 enum spdk_bdev_io_status status) 342 { 343 assert(raid_io->base_bdev_io_remaining >= completed); 344 raid_io->base_bdev_io_remaining -= completed; 345 346 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 347 raid_io->base_bdev_io_status = status; 348 } 349 350 if (raid_io->base_bdev_io_remaining == 0) { 351 raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status); 352 return true; 353 } else { 354 return false; 355 } 356 } 357 358 /* 359 * brief: 360 * raid_bdev_queue_io_wait function processes the IO which failed to submit. 361 * It will try to queue the IOs after storing the context to bdev wait queue logic. 362 * params: 363 * raid_io - pointer to raid_bdev_io 364 * bdev - the block device that the IO is submitted to 365 * ch - io channel 366 * cb_fn - callback when the spdk_bdev_io for bdev becomes available 367 * returns: 368 * none 369 */ 370 void 371 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 372 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn) 373 { 374 raid_io->waitq_entry.bdev = bdev; 375 raid_io->waitq_entry.cb_fn = cb_fn; 376 raid_io->waitq_entry.cb_arg = raid_io; 377 spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry); 378 } 379 380 static void 381 raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 382 { 383 struct raid_bdev_io *raid_io = cb_arg; 384 385 spdk_bdev_free_io(bdev_io); 386 387 raid_bdev_io_complete_part(raid_io, 1, success ? 388 SPDK_BDEV_IO_STATUS_SUCCESS : 389 SPDK_BDEV_IO_STATUS_FAILED); 390 } 391 392 static void raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io); 393 394 static void 395 _raid_bdev_submit_reset_request(void *_raid_io) 396 { 397 struct raid_bdev_io *raid_io = _raid_io; 398 399 raid_bdev_submit_reset_request(raid_io); 400 } 401 402 /* 403 * brief: 404 * raid_bdev_submit_reset_request function submits reset requests 405 * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in 406 * which case it will queue it for later submission 407 * params: 408 * raid_io 409 * returns: 410 * none 411 */ 412 static void 413 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io) 414 { 415 struct raid_bdev *raid_bdev; 416 int ret; 417 uint8_t i; 418 struct raid_base_bdev_info *base_info; 419 struct spdk_io_channel *base_ch; 420 421 raid_bdev = raid_io->raid_bdev; 422 423 if (raid_io->base_bdev_io_remaining == 0) { 424 raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs; 425 } 426 427 while (raid_io->base_bdev_io_submitted < raid_bdev->num_base_bdevs) { 428 i = raid_io->base_bdev_io_submitted; 429 base_info = &raid_bdev->base_bdev_info[i]; 430 base_ch = raid_io->raid_ch->base_channel[i]; 431 ret = spdk_bdev_reset(base_info->desc, base_ch, 432 raid_base_bdev_reset_complete, raid_io); 433 if (ret == 0) { 434 raid_io->base_bdev_io_submitted++; 435 } else if (ret == -ENOMEM) { 436 raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, 437 _raid_bdev_submit_reset_request); 438 return; 439 } else { 440 SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); 441 assert(false); 442 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 443 return; 444 } 445 } 446 } 447 448 /* 449 * brief: 450 * Callback function to spdk_bdev_io_get_buf. 451 * params: 452 * ch - pointer to raid bdev io channel 453 * bdev_io - pointer to parent bdev_io on raid bdev device 454 * success - True if buffer is allocated or false otherwise. 455 * returns: 456 * none 457 */ 458 static void 459 raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 460 bool success) 461 { 462 struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; 463 464 if (!success) { 465 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 466 return; 467 } 468 469 raid_io->raid_bdev->module->submit_rw_request(raid_io); 470 } 471 472 /* 473 * brief: 474 * raid_bdev_submit_request function is the submit_request function pointer of 475 * raid bdev function table. This is used to submit the io on raid_bdev to below 476 * layers. 477 * params: 478 * ch - pointer to raid bdev io channel 479 * bdev_io - pointer to parent bdev_io on raid bdev device 480 * returns: 481 * none 482 */ 483 static void 484 raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 485 { 486 struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; 487 488 raid_io->raid_bdev = bdev_io->bdev->ctxt; 489 raid_io->raid_ch = spdk_io_channel_get_ctx(ch); 490 raid_io->base_bdev_io_remaining = 0; 491 raid_io->base_bdev_io_submitted = 0; 492 raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS; 493 494 switch (bdev_io->type) { 495 case SPDK_BDEV_IO_TYPE_READ: 496 spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb, 497 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 498 break; 499 case SPDK_BDEV_IO_TYPE_WRITE: 500 raid_io->raid_bdev->module->submit_rw_request(raid_io); 501 break; 502 503 case SPDK_BDEV_IO_TYPE_RESET: 504 raid_bdev_submit_reset_request(raid_io); 505 break; 506 507 case SPDK_BDEV_IO_TYPE_FLUSH: 508 case SPDK_BDEV_IO_TYPE_UNMAP: 509 raid_io->raid_bdev->module->submit_null_payload_request(raid_io); 510 break; 511 512 default: 513 SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type); 514 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 515 break; 516 } 517 } 518 519 /* 520 * brief: 521 * _raid_bdev_io_type_supported checks whether io_type is supported in 522 * all base bdev modules of raid bdev module. If anyone among the base_bdevs 523 * doesn't support, the raid device doesn't supports. 524 * 525 * params: 526 * raid_bdev - pointer to raid bdev context 527 * io_type - io type 528 * returns: 529 * true - io_type is supported 530 * false - io_type is not supported 531 */ 532 inline static bool 533 _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type) 534 { 535 struct raid_base_bdev_info *base_info; 536 537 if (io_type == SPDK_BDEV_IO_TYPE_FLUSH || 538 io_type == SPDK_BDEV_IO_TYPE_UNMAP) { 539 if (raid_bdev->module->submit_null_payload_request == NULL) { 540 return false; 541 } 542 } 543 544 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 545 if (base_info->bdev == NULL) { 546 assert(false); 547 continue; 548 } 549 550 if (spdk_bdev_io_type_supported(base_info->bdev, io_type) == false) { 551 return false; 552 } 553 } 554 555 return true; 556 } 557 558 /* 559 * brief: 560 * raid_bdev_io_type_supported is the io_supported function for bdev function 561 * table which returns whether the particular io type is supported or not by 562 * raid bdev module 563 * params: 564 * ctx - pointer to raid bdev context 565 * type - io type 566 * returns: 567 * true - io_type is supported 568 * false - io_type is not supported 569 */ 570 static bool 571 raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 572 { 573 switch (io_type) { 574 case SPDK_BDEV_IO_TYPE_READ: 575 case SPDK_BDEV_IO_TYPE_WRITE: 576 return true; 577 578 case SPDK_BDEV_IO_TYPE_FLUSH: 579 case SPDK_BDEV_IO_TYPE_RESET: 580 case SPDK_BDEV_IO_TYPE_UNMAP: 581 return _raid_bdev_io_type_supported(ctx, io_type); 582 583 default: 584 return false; 585 } 586 587 return false; 588 } 589 590 /* 591 * brief: 592 * raid_bdev_get_io_channel is the get_io_channel function table pointer for 593 * raid bdev. This is used to return the io channel for this raid bdev 594 * params: 595 * ctxt - pointer to raid_bdev 596 * returns: 597 * pointer to io channel for raid bdev 598 */ 599 static struct spdk_io_channel * 600 raid_bdev_get_io_channel(void *ctxt) 601 { 602 struct raid_bdev *raid_bdev = ctxt; 603 604 return spdk_get_io_channel(raid_bdev); 605 } 606 607 /* 608 * brief: 609 * raid_bdev_dump_info_json is the function table pointer for raid bdev 610 * params: 611 * ctx - pointer to raid_bdev 612 * w - pointer to json context 613 * returns: 614 * 0 - success 615 * non zero - failure 616 */ 617 static int 618 raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 619 { 620 struct raid_bdev *raid_bdev = ctx; 621 struct raid_base_bdev_info *base_info; 622 623 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_dump_config_json\n"); 624 assert(raid_bdev != NULL); 625 626 /* Dump the raid bdev configuration related information */ 627 spdk_json_write_named_object_begin(w, "raid"); 628 spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb); 629 spdk_json_write_named_uint32(w, "state", raid_bdev->state); 630 spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); 631 spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs); 632 spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered); 633 spdk_json_write_name(w, "base_bdevs_list"); 634 spdk_json_write_array_begin(w); 635 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 636 if (base_info->bdev) { 637 spdk_json_write_string(w, base_info->bdev->name); 638 } else { 639 spdk_json_write_null(w); 640 } 641 } 642 spdk_json_write_array_end(w); 643 spdk_json_write_object_end(w); 644 645 return 0; 646 } 647 648 /* 649 * brief: 650 * raid_bdev_write_config_json is the function table pointer for raid bdev 651 * params: 652 * bdev - pointer to spdk_bdev 653 * w - pointer to json context 654 * returns: 655 * none 656 */ 657 static void 658 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 659 { 660 struct raid_bdev *raid_bdev = bdev->ctxt; 661 struct raid_base_bdev_info *base_info; 662 663 spdk_json_write_object_begin(w); 664 665 spdk_json_write_named_string(w, "method", "bdev_raid_create"); 666 667 spdk_json_write_named_object_begin(w, "params"); 668 spdk_json_write_named_string(w, "name", bdev->name); 669 spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb); 670 spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); 671 672 spdk_json_write_named_array_begin(w, "base_bdevs"); 673 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 674 if (base_info->bdev) { 675 spdk_json_write_string(w, base_info->bdev->name); 676 } 677 } 678 spdk_json_write_array_end(w); 679 spdk_json_write_object_end(w); 680 681 spdk_json_write_object_end(w); 682 } 683 684 static int 685 raid_bdev_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size) 686 { 687 struct raid_bdev *raid_bdev = ctx; 688 struct spdk_bdev *base_bdev; 689 uint32_t i; 690 int domains_count = 0, rc; 691 692 /* First loop to get the number of memory domains */ 693 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 694 base_bdev = raid_bdev->base_bdev_info[i].bdev; 695 rc = spdk_bdev_get_memory_domains(base_bdev, NULL, 0); 696 if (rc < 0) { 697 return rc; 698 } 699 domains_count += rc; 700 } 701 702 if (!domains || array_size < domains_count) { 703 return domains_count; 704 } 705 706 for (i = 0; i < raid_bdev->num_base_bdevs; i++) { 707 base_bdev = raid_bdev->base_bdev_info[i].bdev; 708 rc = spdk_bdev_get_memory_domains(base_bdev, domains, array_size); 709 if (rc < 0) { 710 return rc; 711 } 712 domains += rc; 713 array_size -= rc; 714 } 715 716 return domains_count; 717 } 718 719 /* g_raid_bdev_fn_table is the function table for raid bdev */ 720 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = { 721 .destruct = raid_bdev_destruct, 722 .submit_request = raid_bdev_submit_request, 723 .io_type_supported = raid_bdev_io_type_supported, 724 .get_io_channel = raid_bdev_get_io_channel, 725 .dump_info_json = raid_bdev_dump_info_json, 726 .write_config_json = raid_bdev_write_config_json, 727 .get_memory_domains = raid_bdev_get_memory_domains, 728 }; 729 730 struct raid_bdev * 731 raid_bdev_find_by_name(const char *name) 732 { 733 struct raid_bdev *raid_bdev; 734 735 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 736 if (strcmp(raid_bdev->bdev.name, name) == 0) { 737 return raid_bdev; 738 } 739 } 740 741 return NULL; 742 } 743 744 static struct { 745 const char *name; 746 enum raid_level value; 747 } g_raid_level_names[] = { 748 { "raid0", RAID0 }, 749 { "0", RAID0 }, 750 { "raid5f", RAID5F }, 751 { "5f", RAID5F }, 752 { "concat", CONCAT }, 753 { } 754 }; 755 756 /* We have to use the typedef in the function declaration to appease astyle. */ 757 typedef enum raid_level raid_level_t; 758 759 raid_level_t 760 raid_bdev_parse_raid_level(const char *str) 761 { 762 unsigned int i; 763 764 assert(str != NULL); 765 766 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 767 if (strcasecmp(g_raid_level_names[i].name, str) == 0) { 768 return g_raid_level_names[i].value; 769 } 770 } 771 772 return INVALID_RAID_LEVEL; 773 } 774 775 const char * 776 raid_bdev_level_to_str(enum raid_level level) 777 { 778 unsigned int i; 779 780 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 781 if (g_raid_level_names[i].value == level) { 782 return g_raid_level_names[i].name; 783 } 784 } 785 786 return ""; 787 } 788 789 /* 790 * brief: 791 * raid_bdev_fini_start is called when bdev layer is starting the 792 * shutdown process 793 * params: 794 * none 795 * returns: 796 * none 797 */ 798 static void 799 raid_bdev_fini_start(void) 800 { 801 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_fini_start\n"); 802 g_shutdown_started = true; 803 } 804 805 /* 806 * brief: 807 * raid_bdev_exit is called on raid bdev module exit time by bdev layer 808 * params: 809 * none 810 * returns: 811 * none 812 */ 813 static void 814 raid_bdev_exit(void) 815 { 816 struct raid_bdev *raid_bdev, *tmp; 817 818 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_exit\n"); 819 820 TAILQ_FOREACH_SAFE(raid_bdev, &g_raid_bdev_list, global_link, tmp) { 821 raid_bdev_cleanup_and_free(raid_bdev); 822 } 823 } 824 825 /* 826 * brief: 827 * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid 828 * module 829 * params: 830 * none 831 * returns: 832 * size of spdk_bdev_io context for raid 833 */ 834 static int 835 raid_bdev_get_ctx_size(void) 836 { 837 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_get_ctx_size\n"); 838 return sizeof(struct raid_bdev_io); 839 } 840 841 static struct spdk_bdev_module g_raid_if = { 842 .name = "raid", 843 .module_init = raid_bdev_init, 844 .fini_start = raid_bdev_fini_start, 845 .module_fini = raid_bdev_exit, 846 .get_ctx_size = raid_bdev_get_ctx_size, 847 .examine_config = raid_bdev_examine, 848 .async_init = false, 849 .async_fini = false, 850 }; 851 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if) 852 853 /* 854 * brief: 855 * raid_bdev_init is the initialization function for raid bdev module 856 * params: 857 * none 858 * returns: 859 * 0 - success 860 * non zero - failure 861 */ 862 static int 863 raid_bdev_init(void) 864 { 865 return 0; 866 } 867 868 /* 869 * brief: 870 * raid_bdev_create allocates raid bdev based on passed configuration 871 * params: 872 * name - name for raid bdev 873 * strip_size - strip size in KB 874 * num_base_bdevs - number of base bdevs 875 * level - raid level 876 * raid_bdev_out - the created raid bdev 877 * returns: 878 * 0 - success 879 * non zero - failure 880 */ 881 int 882 raid_bdev_create(const char *name, uint32_t strip_size, uint8_t num_base_bdevs, 883 enum raid_level level, struct raid_bdev **raid_bdev_out) 884 { 885 struct raid_bdev *raid_bdev; 886 struct spdk_bdev *raid_bdev_gen; 887 struct raid_bdev_module *module; 888 889 if (raid_bdev_find_by_name(name) != NULL) { 890 SPDK_ERRLOG("Duplicate raid bdev name found: %s\n", name); 891 return -EEXIST; 892 } 893 894 if (spdk_u32_is_pow2(strip_size) == false) { 895 SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size); 896 return -EINVAL; 897 } 898 899 module = raid_bdev_module_find(level); 900 if (module == NULL) { 901 SPDK_ERRLOG("Unsupported raid level '%d'\n", level); 902 return -EINVAL; 903 } 904 905 assert(module->base_bdevs_min != 0); 906 if (num_base_bdevs < module->base_bdevs_min) { 907 SPDK_ERRLOG("At least %u base devices required for %s\n", 908 module->base_bdevs_min, 909 raid_bdev_level_to_str(level)); 910 return -EINVAL; 911 } 912 913 raid_bdev = calloc(1, sizeof(*raid_bdev)); 914 if (!raid_bdev) { 915 SPDK_ERRLOG("Unable to allocate memory for raid bdev\n"); 916 return -ENOMEM; 917 } 918 919 raid_bdev->module = module; 920 raid_bdev->num_base_bdevs = num_base_bdevs; 921 raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs, 922 sizeof(struct raid_base_bdev_info)); 923 if (!raid_bdev->base_bdev_info) { 924 SPDK_ERRLOG("Unable able to allocate base bdev info\n"); 925 free(raid_bdev); 926 return -ENOMEM; 927 } 928 929 /* strip_size_kb is from the rpc param. strip_size is in blocks and used 930 * internally and set later. 931 */ 932 raid_bdev->strip_size = 0; 933 raid_bdev->strip_size_kb = strip_size; 934 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 935 raid_bdev->level = level; 936 937 raid_bdev_gen = &raid_bdev->bdev; 938 939 raid_bdev_gen->name = strdup(name); 940 if (!raid_bdev_gen->name) { 941 SPDK_ERRLOG("Unable to allocate name for raid\n"); 942 free(raid_bdev->base_bdev_info); 943 free(raid_bdev); 944 return -ENOMEM; 945 } 946 947 raid_bdev_gen->product_name = "Raid Volume"; 948 raid_bdev_gen->ctxt = raid_bdev; 949 raid_bdev_gen->fn_table = &g_raid_bdev_fn_table; 950 raid_bdev_gen->module = &g_raid_if; 951 raid_bdev_gen->write_cache = 0; 952 953 TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link); 954 955 *raid_bdev_out = raid_bdev; 956 957 return 0; 958 } 959 960 /* 961 * brief: 962 * If raid bdev config is complete, then only register the raid bdev to 963 * bdev layer and remove this raid bdev from configuring list and 964 * insert the raid bdev to configured list 965 * params: 966 * raid_bdev - pointer to raid bdev 967 * returns: 968 * 0 - success 969 * non zero - failure 970 */ 971 static int 972 raid_bdev_configure(struct raid_bdev *raid_bdev) 973 { 974 uint32_t blocklen = 0; 975 struct spdk_bdev *raid_bdev_gen; 976 struct raid_base_bdev_info *base_info; 977 int rc = 0; 978 979 assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING); 980 assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs); 981 982 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 983 assert(base_info->bdev != NULL); 984 /* Check blocklen for all base bdevs that it should be same */ 985 if (blocklen == 0) { 986 blocklen = base_info->bdev->blocklen; 987 } else if (blocklen != base_info->bdev->blocklen) { 988 /* 989 * Assumption is that all the base bdevs for any raid bdev should 990 * have same blocklen 991 */ 992 SPDK_ERRLOG("Blocklen of various bdevs not matching\n"); 993 return -EINVAL; 994 } 995 } 996 assert(blocklen > 0); 997 998 /* The strip_size_kb is read in from user in KB. Convert to blocks here for 999 * internal use. 1000 */ 1001 raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen; 1002 raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size); 1003 raid_bdev->blocklen_shift = spdk_u32log2(blocklen); 1004 1005 raid_bdev_gen = &raid_bdev->bdev; 1006 raid_bdev_gen->blocklen = blocklen; 1007 1008 rc = raid_bdev->module->start(raid_bdev); 1009 if (rc != 0) { 1010 SPDK_ERRLOG("raid module startup callback failed\n"); 1011 return rc; 1012 } 1013 raid_bdev->state = RAID_BDEV_STATE_ONLINE; 1014 SPDK_DEBUGLOG(bdev_raid, "io device register %p\n", raid_bdev); 1015 SPDK_DEBUGLOG(bdev_raid, "blockcnt %" PRIu64 ", blocklen %u\n", 1016 raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen); 1017 spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb, 1018 sizeof(struct raid_bdev_io_channel), 1019 raid_bdev->bdev.name); 1020 rc = spdk_bdev_register(raid_bdev_gen); 1021 if (rc != 0) { 1022 SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n"); 1023 if (raid_bdev->module->stop != NULL) { 1024 raid_bdev->module->stop(raid_bdev); 1025 } 1026 spdk_io_device_unregister(raid_bdev, NULL); 1027 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1028 return rc; 1029 } 1030 SPDK_DEBUGLOG(bdev_raid, "raid bdev generic %p\n", raid_bdev_gen); 1031 SPDK_DEBUGLOG(bdev_raid, "raid bdev is created with name %s, raid_bdev %p\n", 1032 raid_bdev_gen->name, raid_bdev); 1033 1034 return 0; 1035 } 1036 1037 /* 1038 * brief: 1039 * If raid bdev is online and registered, change the bdev state to 1040 * configuring and unregister this raid device. Queue this raid device 1041 * in configuring list 1042 * params: 1043 * raid_bdev - pointer to raid bdev 1044 * cb_fn - callback function 1045 * cb_arg - argument to callback function 1046 * returns: 1047 * none 1048 */ 1049 static void 1050 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, 1051 void *cb_arg) 1052 { 1053 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 1054 if (cb_fn) { 1055 cb_fn(cb_arg, 0); 1056 } 1057 return; 1058 } 1059 1060 assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered); 1061 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 1062 assert(raid_bdev->num_base_bdevs_discovered); 1063 SPDK_DEBUGLOG(bdev_raid, "raid bdev state changing from online to offline\n"); 1064 1065 spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg); 1066 } 1067 1068 /* 1069 * brief: 1070 * raid_bdev_find_by_base_bdev function finds the raid bdev which has 1071 * claimed the base bdev. 1072 * params: 1073 * base_bdev - pointer to base bdev pointer 1074 * _raid_bdev - Reference to pointer to raid bdev 1075 * _base_info - Reference to the raid base bdev info. 1076 * returns: 1077 * true - if the raid bdev is found. 1078 * false - if the raid bdev is not found. 1079 */ 1080 static bool 1081 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev, 1082 struct raid_base_bdev_info **_base_info) 1083 { 1084 struct raid_bdev *raid_bdev; 1085 struct raid_base_bdev_info *base_info; 1086 1087 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 1088 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1089 if (base_info->bdev == base_bdev) { 1090 *_raid_bdev = raid_bdev; 1091 *_base_info = base_info; 1092 return true; 1093 } 1094 } 1095 } 1096 1097 return false; 1098 } 1099 1100 /* 1101 * brief: 1102 * raid_bdev_remove_base_bdev function is called by below layers when base_bdev 1103 * is removed. This function checks if this base bdev is part of any raid bdev 1104 * or not. If yes, it takes necessary action on that particular raid bdev. 1105 * params: 1106 * base_bdev - pointer to base bdev pointer which got removed 1107 * returns: 1108 * none 1109 */ 1110 static void 1111 raid_bdev_remove_base_bdev(struct spdk_bdev *base_bdev) 1112 { 1113 struct raid_bdev *raid_bdev = NULL; 1114 struct raid_base_bdev_info *base_info; 1115 1116 SPDK_DEBUGLOG(bdev_raid, "raid_bdev_remove_base_bdev\n"); 1117 1118 /* Find the raid_bdev which has claimed this base_bdev */ 1119 if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) { 1120 SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name); 1121 return; 1122 } 1123 1124 assert(base_info->desc); 1125 base_info->remove_scheduled = true; 1126 1127 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 1128 /* 1129 * As raid bdev is not registered yet or already unregistered, 1130 * so cleanup should be done here itself. 1131 */ 1132 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 1133 if (raid_bdev->num_base_bdevs_discovered == 0) { 1134 /* There is no base bdev for this raid, so free the raid device. */ 1135 raid_bdev_cleanup_and_free(raid_bdev); 1136 return; 1137 } 1138 } 1139 1140 raid_bdev_deconfigure(raid_bdev, NULL, NULL); 1141 } 1142 1143 /* 1144 * brief: 1145 * raid_bdev_event_base_bdev function is called by below layers when base_bdev 1146 * triggers asynchronous event. 1147 * params: 1148 * type - event details. 1149 * bdev - bdev that triggered event. 1150 * event_ctx - context for event. 1151 * returns: 1152 * none 1153 */ 1154 static void 1155 raid_bdev_event_base_bdev(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 1156 void *event_ctx) 1157 { 1158 switch (type) { 1159 case SPDK_BDEV_EVENT_REMOVE: 1160 raid_bdev_remove_base_bdev(bdev); 1161 break; 1162 default: 1163 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 1164 break; 1165 } 1166 } 1167 1168 /* 1169 * brief: 1170 * Deletes the specified raid bdev 1171 * params: 1172 * raid_bdev - pointer to raid bdev 1173 * cb_fn - callback function 1174 * cb_arg - argument to callback function 1175 */ 1176 void 1177 raid_bdev_delete(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, void *cb_arg) 1178 { 1179 struct raid_base_bdev_info *base_info; 1180 1181 SPDK_DEBUGLOG(bdev_raid, "delete raid bdev: %s\n", raid_bdev->bdev.name); 1182 1183 if (raid_bdev->destroy_started) { 1184 SPDK_DEBUGLOG(bdev_raid, "destroying raid bdev %s is already started\n", 1185 raid_bdev->bdev.name); 1186 if (cb_fn) { 1187 cb_fn(cb_arg, -EALREADY); 1188 } 1189 return; 1190 } 1191 1192 raid_bdev->destroy_started = true; 1193 1194 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1195 base_info->remove_scheduled = true; 1196 1197 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 1198 /* 1199 * As raid bdev is not registered yet or already unregistered, 1200 * so cleanup should be done here itself. 1201 */ 1202 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 1203 } 1204 } 1205 1206 if (raid_bdev->num_base_bdevs_discovered == 0) { 1207 /* There is no base bdev for this raid, so free the raid device. */ 1208 raid_bdev_cleanup_and_free(raid_bdev); 1209 if (cb_fn) { 1210 cb_fn(cb_arg, 0); 1211 } 1212 } else { 1213 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg); 1214 } 1215 } 1216 1217 static int 1218 raid_bdev_configure_base_bdev(struct raid_bdev *raid_bdev, struct raid_base_bdev_info *base_info) 1219 { 1220 struct spdk_bdev_desc *desc; 1221 struct spdk_bdev *bdev; 1222 int rc; 1223 1224 assert(base_info->name != NULL); 1225 assert(base_info->bdev == NULL); 1226 1227 rc = spdk_bdev_open_ext(base_info->name, true, raid_bdev_event_base_bdev, NULL, &desc); 1228 if (rc != 0) { 1229 if (rc != -ENODEV) { 1230 SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", base_info->name); 1231 } 1232 return rc; 1233 } 1234 1235 bdev = spdk_bdev_desc_get_bdev(desc); 1236 1237 rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if); 1238 if (rc != 0) { 1239 SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n"); 1240 spdk_bdev_close(desc); 1241 return rc; 1242 } 1243 1244 SPDK_DEBUGLOG(bdev_raid, "bdev %s is claimed\n", bdev->name); 1245 1246 assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE); 1247 1248 base_info->thread = spdk_get_thread(); 1249 base_info->bdev = bdev; 1250 base_info->desc = desc; 1251 raid_bdev->num_base_bdevs_discovered++; 1252 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 1253 1254 if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) { 1255 rc = raid_bdev_configure(raid_bdev); 1256 if (rc != 0) { 1257 SPDK_ERRLOG("Failed to configure raid bdev\n"); 1258 return rc; 1259 } 1260 } 1261 1262 return 0; 1263 } 1264 1265 /* 1266 * brief: 1267 * raid_bdev_add_base_device function is the actual function which either adds 1268 * the nvme base device to existing raid bdev or create a new raid bdev. It also claims 1269 * the base device and keep the open descriptor. 1270 * params: 1271 * raid_bdev - pointer to raid bdev 1272 * name - name of the base bdev 1273 * slot - position to add base bdev 1274 * returns: 1275 * 0 - success 1276 * non zero - failure 1277 */ 1278 int 1279 raid_bdev_add_base_device(struct raid_bdev *raid_bdev, const char *name, uint8_t slot) 1280 { 1281 struct raid_base_bdev_info *base_info; 1282 int rc; 1283 1284 if (slot >= raid_bdev->num_base_bdevs) { 1285 return -EINVAL; 1286 } 1287 1288 base_info = &raid_bdev->base_bdev_info[slot]; 1289 1290 if (base_info->name != NULL) { 1291 SPDK_ERRLOG("Slot %u on raid bdev '%s' already assigned to bdev '%s'\n", 1292 slot, raid_bdev->bdev.name, base_info->name); 1293 return -EBUSY; 1294 } 1295 1296 base_info->name = strdup(name); 1297 if (base_info->name == NULL) { 1298 return -ENOMEM; 1299 } 1300 1301 rc = raid_bdev_configure_base_bdev(raid_bdev, base_info); 1302 if (rc != 0) { 1303 if (rc != -ENODEV) { 1304 SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", name); 1305 } 1306 return rc; 1307 } 1308 1309 return 0; 1310 } 1311 1312 /* 1313 * brief: 1314 * raid_bdev_examine function is the examine function call by the below layers 1315 * like bdev_nvme layer. This function will check if this base bdev can be 1316 * claimed by this raid bdev or not. 1317 * params: 1318 * bdev - pointer to base bdev 1319 * returns: 1320 * none 1321 */ 1322 static void 1323 raid_bdev_examine(struct spdk_bdev *bdev) 1324 { 1325 struct raid_bdev *raid_bdev; 1326 struct raid_base_bdev_info *base_info; 1327 1328 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 1329 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1330 if (base_info->bdev == NULL && strcmp(bdev->name, base_info->name) == 0) { 1331 raid_bdev_configure_base_bdev(raid_bdev, base_info); 1332 break; 1333 } 1334 } 1335 } 1336 1337 spdk_bdev_module_examine_done(&g_raid_if); 1338 } 1339 1340 /* Log component for bdev raid bdev module */ 1341 SPDK_LOG_REGISTER_COMPONENT(bdev_raid) 1342