1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "bdev_raid.h" 35 #include "spdk/env.h" 36 #include "spdk/thread.h" 37 #include "spdk/conf.h" 38 #include "spdk_internal/log.h" 39 #include "spdk/string.h" 40 #include "spdk/util.h" 41 #include "spdk/json.h" 42 #include "spdk/string.h" 43 44 static bool g_shutdown_started = false; 45 46 /* raid bdev config as read from config file */ 47 struct raid_config g_raid_config = { 48 .raid_bdev_config_head = TAILQ_HEAD_INITIALIZER(g_raid_config.raid_bdev_config_head), 49 }; 50 51 /* 52 * List of raid bdev in configured list, these raid bdevs are registered with 53 * bdev layer 54 */ 55 struct raid_configured_tailq g_raid_bdev_configured_list = TAILQ_HEAD_INITIALIZER( 56 g_raid_bdev_configured_list); 57 58 /* List of raid bdev in configuring list */ 59 struct raid_configuring_tailq g_raid_bdev_configuring_list = TAILQ_HEAD_INITIALIZER( 60 g_raid_bdev_configuring_list); 61 62 /* List of all raid bdevs */ 63 struct raid_all_tailq g_raid_bdev_list = TAILQ_HEAD_INITIALIZER(g_raid_bdev_list); 64 65 /* List of all raid bdevs that are offline */ 66 struct raid_offline_tailq g_raid_bdev_offline_list = TAILQ_HEAD_INITIALIZER( 67 g_raid_bdev_offline_list); 68 69 static TAILQ_HEAD(, raid_bdev_module) g_raid_modules = TAILQ_HEAD_INITIALIZER(g_raid_modules); 70 71 static struct raid_bdev_module *raid_bdev_module_find(enum raid_level level) 72 { 73 struct raid_bdev_module *raid_module; 74 75 TAILQ_FOREACH(raid_module, &g_raid_modules, link) { 76 if (raid_module->level == level) { 77 return raid_module; 78 } 79 } 80 81 return NULL; 82 } 83 84 void raid_bdev_module_list_add(struct raid_bdev_module *raid_module) 85 { 86 if (raid_bdev_module_find(raid_module->level) != NULL) { 87 SPDK_ERRLOG("module for raid level '%s' already registered.\n", 88 raid_bdev_level_to_str(raid_module->level)); 89 assert(false); 90 } else { 91 TAILQ_INSERT_TAIL(&g_raid_modules, raid_module, link); 92 } 93 } 94 95 /* Function declarations */ 96 static void raid_bdev_examine(struct spdk_bdev *bdev); 97 static int raid_bdev_init(void); 98 static void raid_bdev_deconfigure(struct raid_bdev *raid_bdev, 99 raid_bdev_destruct_cb cb_fn, void *cb_arg); 100 static void raid_bdev_remove_base_bdev(void *ctx); 101 102 /* 103 * brief: 104 * raid_bdev_create_cb function is a cb function for raid bdev which creates the 105 * hierarchy from raid bdev to base bdev io channels. It will be called per core 106 * params: 107 * io_device - pointer to raid bdev io device represented by raid_bdev 108 * ctx_buf - pointer to context buffer for raid bdev io channel 109 * returns: 110 * 0 - success 111 * non zero - failure 112 */ 113 static int 114 raid_bdev_create_cb(void *io_device, void *ctx_buf) 115 { 116 struct raid_bdev *raid_bdev = io_device; 117 struct raid_bdev_io_channel *raid_ch = ctx_buf; 118 uint8_t i; 119 120 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_create_cb, %p\n", raid_ch); 121 122 assert(raid_bdev != NULL); 123 assert(raid_bdev->state == RAID_BDEV_STATE_ONLINE); 124 125 raid_ch->num_channels = raid_bdev->num_base_bdevs; 126 127 raid_ch->base_channel = calloc(raid_ch->num_channels, 128 sizeof(struct spdk_io_channel *)); 129 if (!raid_ch->base_channel) { 130 SPDK_ERRLOG("Unable to allocate base bdevs io channel\n"); 131 return -ENOMEM; 132 } 133 for (i = 0; i < raid_ch->num_channels; i++) { 134 /* 135 * Get the spdk_io_channel for all the base bdevs. This is used during 136 * split logic to send the respective child bdev ios to respective base 137 * bdev io channel. 138 */ 139 raid_ch->base_channel[i] = spdk_bdev_get_io_channel( 140 raid_bdev->base_bdev_info[i].desc); 141 if (!raid_ch->base_channel[i]) { 142 uint8_t j; 143 144 for (j = 0; j < i; j++) { 145 spdk_put_io_channel(raid_ch->base_channel[j]); 146 } 147 free(raid_ch->base_channel); 148 raid_ch->base_channel = NULL; 149 SPDK_ERRLOG("Unable to create io channel for base bdev\n"); 150 return -ENOMEM; 151 } 152 } 153 154 return 0; 155 } 156 157 /* 158 * brief: 159 * raid_bdev_destroy_cb function is a cb function for raid bdev which deletes the 160 * hierarchy from raid bdev to base bdev io channels. It will be called per core 161 * params: 162 * io_device - pointer to raid bdev io device represented by raid_bdev 163 * ctx_buf - pointer to context buffer for raid bdev io channel 164 * returns: 165 * none 166 */ 167 static void 168 raid_bdev_destroy_cb(void *io_device, void *ctx_buf) 169 { 170 struct raid_bdev_io_channel *raid_ch = ctx_buf; 171 uint8_t i; 172 173 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_destroy_cb\n"); 174 175 assert(raid_ch != NULL); 176 assert(raid_ch->base_channel); 177 for (i = 0; i < raid_ch->num_channels; i++) { 178 /* Free base bdev channels */ 179 assert(raid_ch->base_channel[i] != NULL); 180 spdk_put_io_channel(raid_ch->base_channel[i]); 181 } 182 free(raid_ch->base_channel); 183 raid_ch->base_channel = NULL; 184 } 185 186 /* 187 * brief: 188 * raid_bdev_cleanup is used to cleanup and free raid_bdev related data 189 * structures. 190 * params: 191 * raid_bdev - pointer to raid_bdev 192 * returns: 193 * none 194 */ 195 static void 196 raid_bdev_cleanup(struct raid_bdev *raid_bdev) 197 { 198 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_cleanup, %p name %s, state %u, config %p\n", 199 raid_bdev, 200 raid_bdev->bdev.name, raid_bdev->state, raid_bdev->config); 201 if (raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { 202 TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link); 203 } else if (raid_bdev->state == RAID_BDEV_STATE_OFFLINE) { 204 TAILQ_REMOVE(&g_raid_bdev_offline_list, raid_bdev, state_link); 205 } else { 206 assert(0); 207 } 208 TAILQ_REMOVE(&g_raid_bdev_list, raid_bdev, global_link); 209 free(raid_bdev->bdev.name); 210 free(raid_bdev->base_bdev_info); 211 if (raid_bdev->config) { 212 raid_bdev->config->raid_bdev = NULL; 213 } 214 free(raid_bdev); 215 } 216 217 /* 218 * brief: 219 * wrapper for the bdev close operation 220 * params: 221 * base_info - raid base bdev info 222 * returns: 223 */ 224 static void 225 _raid_bdev_free_base_bdev_resource(void *ctx) 226 { 227 struct spdk_bdev_desc *desc = ctx; 228 229 spdk_bdev_close(desc); 230 } 231 232 233 /* 234 * brief: 235 * free resource of base bdev for raid bdev 236 * params: 237 * raid_bdev - pointer to raid bdev 238 * base_info - raid base bdev info 239 * returns: 240 * 0 - success 241 * non zero - failure 242 */ 243 static void 244 raid_bdev_free_base_bdev_resource(struct raid_bdev *raid_bdev, 245 struct raid_base_bdev_info *base_info) 246 { 247 spdk_bdev_module_release_bdev(base_info->bdev); 248 if (base_info->thread && base_info->thread != spdk_get_thread()) { 249 spdk_thread_send_msg(base_info->thread, _raid_bdev_free_base_bdev_resource, base_info->desc); 250 } else { 251 spdk_bdev_close(base_info->desc); 252 } 253 base_info->desc = NULL; 254 base_info->bdev = NULL; 255 256 assert(raid_bdev->num_base_bdevs_discovered); 257 raid_bdev->num_base_bdevs_discovered--; 258 } 259 260 /* 261 * brief: 262 * raid_bdev_destruct is the destruct function table pointer for raid bdev 263 * params: 264 * ctxt - pointer to raid_bdev 265 * returns: 266 * 0 - success 267 * non zero - failure 268 */ 269 static int 270 raid_bdev_destruct(void *ctxt) 271 { 272 struct raid_bdev *raid_bdev = ctxt; 273 struct raid_base_bdev_info *base_info; 274 275 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_destruct\n"); 276 277 raid_bdev->destruct_called = true; 278 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 279 /* 280 * Close all base bdev descriptors for which call has come from below 281 * layers. Also close the descriptors if we have started shutdown. 282 */ 283 if (g_shutdown_started || 284 ((base_info->remove_scheduled == true) && 285 (base_info->bdev != NULL))) { 286 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 287 } 288 } 289 290 if (g_shutdown_started) { 291 TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link); 292 if (raid_bdev->module->stop != NULL) { 293 raid_bdev->module->stop(raid_bdev); 294 } 295 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 296 TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link); 297 } 298 299 spdk_io_device_unregister(raid_bdev, NULL); 300 301 if (raid_bdev->num_base_bdevs_discovered == 0) { 302 /* Free raid_bdev when there are no base bdevs left */ 303 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev base bdevs is 0, going to free all in destruct\n"); 304 raid_bdev_cleanup(raid_bdev); 305 } 306 307 return 0; 308 } 309 310 void 311 raid_bdev_io_complete(struct raid_bdev_io *raid_io, enum spdk_bdev_io_status status) 312 { 313 struct spdk_bdev_io *bdev_io = spdk_bdev_io_from_ctx(raid_io); 314 315 spdk_bdev_io_complete(bdev_io, status); 316 } 317 318 /* 319 * brief: 320 * raid_bdev_io_complete_part - signal the completion of a part of the expected 321 * base bdev IOs and complete the raid_io if this is the final expected IO. 322 * The caller should first set raid_io->base_bdev_io_remaining. This function 323 * will decrement this counter by the value of the 'completed' parameter and 324 * complete the raid_io if the counter reaches 0. The caller is free to 325 * interpret the 'base_bdev_io_remaining' and 'completed' values as needed, 326 * it can represent e.g. blocks or IOs. 327 * params: 328 * raid_io - pointer to raid_bdev_io 329 * completed - the part of the raid_io that has been completed 330 * status - status of the base IO 331 * returns: 332 * true - if the raid_io is completed 333 * false - otherwise 334 */ 335 bool 336 raid_bdev_io_complete_part(struct raid_bdev_io *raid_io, uint64_t completed, 337 enum spdk_bdev_io_status status) 338 { 339 assert(raid_io->base_bdev_io_remaining >= completed); 340 raid_io->base_bdev_io_remaining -= completed; 341 342 if (status != SPDK_BDEV_IO_STATUS_SUCCESS) { 343 raid_io->base_bdev_io_status = status; 344 } 345 346 if (raid_io->base_bdev_io_remaining == 0) { 347 raid_bdev_io_complete(raid_io, raid_io->base_bdev_io_status); 348 return true; 349 } else { 350 return false; 351 } 352 } 353 354 /* 355 * brief: 356 * raid_bdev_queue_io_wait function processes the IO which failed to submit. 357 * It will try to queue the IOs after storing the context to bdev wait queue logic. 358 * params: 359 * raid_io - pointer to raid_bdev_io 360 * bdev - the block device that the IO is submitted to 361 * ch - io channel 362 * cb_fn - callback when the spdk_bdev_io for bdev becomes available 363 * returns: 364 * none 365 */ 366 void 367 raid_bdev_queue_io_wait(struct raid_bdev_io *raid_io, struct spdk_bdev *bdev, 368 struct spdk_io_channel *ch, spdk_bdev_io_wait_cb cb_fn) 369 { 370 raid_io->waitq_entry.bdev = bdev; 371 raid_io->waitq_entry.cb_fn = cb_fn; 372 raid_io->waitq_entry.cb_arg = raid_io; 373 spdk_bdev_queue_io_wait(bdev, ch, &raid_io->waitq_entry); 374 } 375 376 static void 377 raid_base_bdev_reset_complete(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 378 { 379 struct raid_bdev_io *raid_io = cb_arg; 380 381 spdk_bdev_free_io(bdev_io); 382 383 raid_bdev_io_complete_part(raid_io, 1, success ? 384 SPDK_BDEV_IO_STATUS_SUCCESS : 385 SPDK_BDEV_IO_STATUS_FAILED); 386 } 387 388 static void 389 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io); 390 391 static void 392 _raid_bdev_submit_reset_request(void *_raid_io) 393 { 394 struct raid_bdev_io *raid_io = _raid_io; 395 396 raid_bdev_submit_reset_request(raid_io); 397 } 398 399 /* 400 * brief: 401 * raid_bdev_submit_reset_request function submits reset requests 402 * to member disks; it will submit as many as possible unless a reset fails with -ENOMEM, in 403 * which case it will queue it for later submission 404 * params: 405 * raid_io 406 * returns: 407 * none 408 */ 409 static void 410 raid_bdev_submit_reset_request(struct raid_bdev_io *raid_io) 411 { 412 struct raid_bdev *raid_bdev; 413 int ret; 414 uint8_t i; 415 struct raid_base_bdev_info *base_info; 416 struct spdk_io_channel *base_ch; 417 418 raid_bdev = raid_io->raid_bdev; 419 420 if (raid_io->base_bdev_io_remaining == 0) { 421 raid_io->base_bdev_io_remaining = raid_bdev->num_base_bdevs; 422 } 423 424 while (raid_io->base_bdev_io_submitted < raid_bdev->num_base_bdevs) { 425 i = raid_io->base_bdev_io_submitted; 426 base_info = &raid_bdev->base_bdev_info[i]; 427 base_ch = raid_io->raid_ch->base_channel[i]; 428 ret = spdk_bdev_reset(base_info->desc, base_ch, 429 raid_base_bdev_reset_complete, raid_io); 430 if (ret == 0) { 431 raid_io->base_bdev_io_submitted++; 432 } else if (ret == -ENOMEM) { 433 raid_bdev_queue_io_wait(raid_io, base_info->bdev, base_ch, 434 _raid_bdev_submit_reset_request); 435 return; 436 } else { 437 SPDK_ERRLOG("bdev io submit error not due to ENOMEM, it should not happen\n"); 438 assert(false); 439 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 440 return; 441 } 442 } 443 } 444 445 /* 446 * brief: 447 * Callback function to spdk_bdev_io_get_buf. 448 * params: 449 * ch - pointer to raid bdev io channel 450 * bdev_io - pointer to parent bdev_io on raid bdev device 451 * success - True if buffer is allocated or false otherwise. 452 * returns: 453 * none 454 */ 455 static void 456 raid_bdev_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 457 bool success) 458 { 459 struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; 460 461 if (!success) { 462 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 463 return; 464 } 465 466 raid_io->raid_bdev->module->submit_rw_request(raid_io); 467 } 468 469 /* 470 * brief: 471 * raid_bdev_submit_request function is the submit_request function pointer of 472 * raid bdev function table. This is used to submit the io on raid_bdev to below 473 * layers. 474 * params: 475 * ch - pointer to raid bdev io channel 476 * bdev_io - pointer to parent bdev_io on raid bdev device 477 * returns: 478 * none 479 */ 480 static void 481 raid_bdev_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 482 { 483 struct raid_bdev_io *raid_io = (struct raid_bdev_io *)bdev_io->driver_ctx; 484 485 raid_io->raid_bdev = bdev_io->bdev->ctxt; 486 raid_io->raid_ch = spdk_io_channel_get_ctx(ch); 487 raid_io->base_bdev_io_remaining = 0; 488 raid_io->base_bdev_io_submitted = 0; 489 raid_io->base_bdev_io_status = SPDK_BDEV_IO_STATUS_SUCCESS; 490 491 switch (bdev_io->type) { 492 case SPDK_BDEV_IO_TYPE_READ: 493 spdk_bdev_io_get_buf(bdev_io, raid_bdev_get_buf_cb, 494 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 495 break; 496 case SPDK_BDEV_IO_TYPE_WRITE: 497 raid_io->raid_bdev->module->submit_rw_request(raid_io); 498 break; 499 500 case SPDK_BDEV_IO_TYPE_RESET: 501 raid_bdev_submit_reset_request(raid_io); 502 break; 503 504 case SPDK_BDEV_IO_TYPE_FLUSH: 505 case SPDK_BDEV_IO_TYPE_UNMAP: 506 raid_io->raid_bdev->module->submit_null_payload_request(raid_io); 507 break; 508 509 default: 510 SPDK_ERRLOG("submit request, invalid io type %u\n", bdev_io->type); 511 raid_bdev_io_complete(raid_io, SPDK_BDEV_IO_STATUS_FAILED); 512 break; 513 } 514 } 515 516 /* 517 * brief: 518 * _raid_bdev_io_type_supported checks whether io_type is supported in 519 * all base bdev modules of raid bdev module. If anyone among the base_bdevs 520 * doesn't support, the raid device doesn't supports. 521 * 522 * params: 523 * raid_bdev - pointer to raid bdev context 524 * io_type - io type 525 * returns: 526 * true - io_type is supported 527 * false - io_type is not supported 528 */ 529 inline static bool 530 _raid_bdev_io_type_supported(struct raid_bdev *raid_bdev, enum spdk_bdev_io_type io_type) 531 { 532 struct raid_base_bdev_info *base_info; 533 534 if (io_type == SPDK_BDEV_IO_TYPE_FLUSH || 535 io_type == SPDK_BDEV_IO_TYPE_UNMAP) { 536 if (raid_bdev->module->submit_null_payload_request == NULL) { 537 return false; 538 } 539 } 540 541 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 542 if (base_info->bdev == NULL) { 543 assert(false); 544 continue; 545 } 546 547 if (spdk_bdev_io_type_supported(base_info->bdev, io_type) == false) { 548 return false; 549 } 550 } 551 552 return true; 553 } 554 555 /* 556 * brief: 557 * raid_bdev_io_type_supported is the io_supported function for bdev function 558 * table which returns whether the particular io type is supported or not by 559 * raid bdev module 560 * params: 561 * ctx - pointer to raid bdev context 562 * type - io type 563 * returns: 564 * true - io_type is supported 565 * false - io_type is not supported 566 */ 567 static bool 568 raid_bdev_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 569 { 570 switch (io_type) { 571 case SPDK_BDEV_IO_TYPE_READ: 572 case SPDK_BDEV_IO_TYPE_WRITE: 573 return true; 574 575 case SPDK_BDEV_IO_TYPE_FLUSH: 576 case SPDK_BDEV_IO_TYPE_RESET: 577 case SPDK_BDEV_IO_TYPE_UNMAP: 578 return _raid_bdev_io_type_supported(ctx, io_type); 579 580 default: 581 return false; 582 } 583 584 return false; 585 } 586 587 /* 588 * brief: 589 * raid_bdev_get_io_channel is the get_io_channel function table pointer for 590 * raid bdev. This is used to return the io channel for this raid bdev 591 * params: 592 * ctxt - pointer to raid_bdev 593 * returns: 594 * pointer to io channel for raid bdev 595 */ 596 static struct spdk_io_channel * 597 raid_bdev_get_io_channel(void *ctxt) 598 { 599 struct raid_bdev *raid_bdev = ctxt; 600 601 return spdk_get_io_channel(raid_bdev); 602 } 603 604 /* 605 * brief: 606 * raid_bdev_dump_info_json is the function table pointer for raid bdev 607 * params: 608 * ctx - pointer to raid_bdev 609 * w - pointer to json context 610 * returns: 611 * 0 - success 612 * non zero - failure 613 */ 614 static int 615 raid_bdev_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 616 { 617 struct raid_bdev *raid_bdev = ctx; 618 struct raid_base_bdev_info *base_info; 619 620 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_dump_config_json\n"); 621 assert(raid_bdev != NULL); 622 623 /* Dump the raid bdev configuration related information */ 624 spdk_json_write_named_object_begin(w, "raid"); 625 spdk_json_write_named_uint32(w, "strip_size", raid_bdev->strip_size); 626 spdk_json_write_named_uint32(w, "strip_size_kb", raid_bdev->strip_size_kb); 627 spdk_json_write_named_uint32(w, "state", raid_bdev->state); 628 spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); 629 spdk_json_write_named_uint32(w, "destruct_called", raid_bdev->destruct_called); 630 spdk_json_write_named_uint32(w, "num_base_bdevs", raid_bdev->num_base_bdevs); 631 spdk_json_write_named_uint32(w, "num_base_bdevs_discovered", raid_bdev->num_base_bdevs_discovered); 632 spdk_json_write_name(w, "base_bdevs_list"); 633 spdk_json_write_array_begin(w); 634 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 635 if (base_info->bdev) { 636 spdk_json_write_string(w, base_info->bdev->name); 637 } else { 638 spdk_json_write_null(w); 639 } 640 } 641 spdk_json_write_array_end(w); 642 spdk_json_write_object_end(w); 643 644 return 0; 645 } 646 647 /* 648 * brief: 649 * raid_bdev_write_config_json is the function table pointer for raid bdev 650 * params: 651 * bdev - pointer to spdk_bdev 652 * w - pointer to json context 653 * returns: 654 * none 655 */ 656 static void 657 raid_bdev_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 658 { 659 struct raid_bdev *raid_bdev = bdev->ctxt; 660 struct raid_base_bdev_info *base_info; 661 662 spdk_json_write_object_begin(w); 663 664 spdk_json_write_named_string(w, "method", "bdev_raid_create"); 665 666 spdk_json_write_named_object_begin(w, "params"); 667 spdk_json_write_named_string(w, "name", bdev->name); 668 spdk_json_write_named_uint32(w, "strip_size", raid_bdev->strip_size_kb); 669 spdk_json_write_named_string(w, "raid_level", raid_bdev_level_to_str(raid_bdev->level)); 670 671 spdk_json_write_named_array_begin(w, "base_bdevs"); 672 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 673 if (base_info->bdev) { 674 spdk_json_write_string(w, base_info->bdev->name); 675 } 676 } 677 spdk_json_write_array_end(w); 678 spdk_json_write_object_end(w); 679 680 spdk_json_write_object_end(w); 681 } 682 683 /* g_raid_bdev_fn_table is the function table for raid bdev */ 684 static const struct spdk_bdev_fn_table g_raid_bdev_fn_table = { 685 .destruct = raid_bdev_destruct, 686 .submit_request = raid_bdev_submit_request, 687 .io_type_supported = raid_bdev_io_type_supported, 688 .get_io_channel = raid_bdev_get_io_channel, 689 .dump_info_json = raid_bdev_dump_info_json, 690 .write_config_json = raid_bdev_write_config_json, 691 }; 692 693 /* 694 * brief: 695 * raid_bdev_config_cleanup function is used to free memory for one raid_bdev in configuration 696 * params: 697 * raid_cfg - pointer to raid_bdev_config structure 698 * returns: 699 * none 700 */ 701 void 702 raid_bdev_config_cleanup(struct raid_bdev_config *raid_cfg) 703 { 704 uint8_t i; 705 706 TAILQ_REMOVE(&g_raid_config.raid_bdev_config_head, raid_cfg, link); 707 g_raid_config.total_raid_bdev--; 708 709 if (raid_cfg->base_bdev) { 710 for (i = 0; i < raid_cfg->num_base_bdevs; i++) { 711 free(raid_cfg->base_bdev[i].name); 712 } 713 free(raid_cfg->base_bdev); 714 } 715 free(raid_cfg->name); 716 free(raid_cfg); 717 } 718 719 /* 720 * brief: 721 * raid_bdev_free is the raid bdev function table function pointer. This is 722 * called on bdev free path 723 * params: 724 * none 725 * returns: 726 * none 727 */ 728 static void 729 raid_bdev_free(void) 730 { 731 struct raid_bdev_config *raid_cfg, *tmp; 732 733 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_free\n"); 734 TAILQ_FOREACH_SAFE(raid_cfg, &g_raid_config.raid_bdev_config_head, link, tmp) { 735 raid_bdev_config_cleanup(raid_cfg); 736 } 737 } 738 739 /* brief 740 * raid_bdev_config_find_by_name is a helper function to find raid bdev config 741 * by name as key. 742 * 743 * params: 744 * raid_name - name for raid bdev. 745 */ 746 struct raid_bdev_config * 747 raid_bdev_config_find_by_name(const char *raid_name) 748 { 749 struct raid_bdev_config *raid_cfg; 750 751 TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) { 752 if (!strcmp(raid_cfg->name, raid_name)) { 753 return raid_cfg; 754 } 755 } 756 757 return raid_cfg; 758 } 759 760 /* 761 * brief 762 * raid_bdev_config_add function adds config for newly created raid bdev. 763 * 764 * params: 765 * raid_name - name for raid bdev. 766 * strip_size - strip size in KB 767 * num_base_bdevs - number of base bdevs. 768 * level - raid level. 769 * _raid_cfg - Pointer to newly added configuration 770 */ 771 int 772 raid_bdev_config_add(const char *raid_name, uint32_t strip_size, uint8_t num_base_bdevs, 773 enum raid_level level, struct raid_bdev_config **_raid_cfg) 774 { 775 struct raid_bdev_config *raid_cfg; 776 777 raid_cfg = raid_bdev_config_find_by_name(raid_name); 778 if (raid_cfg != NULL) { 779 SPDK_ERRLOG("Duplicate raid bdev name found in config file %s\n", 780 raid_name); 781 return -EEXIST; 782 } 783 784 if (spdk_u32_is_pow2(strip_size) == false) { 785 SPDK_ERRLOG("Invalid strip size %" PRIu32 "\n", strip_size); 786 return -EINVAL; 787 } 788 789 if (num_base_bdevs == 0) { 790 SPDK_ERRLOG("Invalid base device count %u\n", num_base_bdevs); 791 return -EINVAL; 792 } 793 794 raid_cfg = calloc(1, sizeof(*raid_cfg)); 795 if (raid_cfg == NULL) { 796 SPDK_ERRLOG("unable to allocate memory\n"); 797 return -ENOMEM; 798 } 799 800 raid_cfg->name = strdup(raid_name); 801 if (!raid_cfg->name) { 802 free(raid_cfg); 803 SPDK_ERRLOG("unable to allocate memory\n"); 804 return -ENOMEM; 805 } 806 raid_cfg->strip_size = strip_size; 807 raid_cfg->num_base_bdevs = num_base_bdevs; 808 raid_cfg->level = level; 809 810 raid_cfg->base_bdev = calloc(num_base_bdevs, sizeof(*raid_cfg->base_bdev)); 811 if (raid_cfg->base_bdev == NULL) { 812 free(raid_cfg->name); 813 free(raid_cfg); 814 SPDK_ERRLOG("unable to allocate memory\n"); 815 return -ENOMEM; 816 } 817 818 TAILQ_INSERT_TAIL(&g_raid_config.raid_bdev_config_head, raid_cfg, link); 819 g_raid_config.total_raid_bdev++; 820 821 *_raid_cfg = raid_cfg; 822 return 0; 823 } 824 825 /* 826 * brief: 827 * raid_bdev_config_add_base_bdev function add base bdev to raid bdev config. 828 * 829 * params: 830 * raid_cfg - pointer to raid bdev configuration 831 * base_bdev_name - name of base bdev 832 * slot - Position to add base bdev 833 */ 834 int 835 raid_bdev_config_add_base_bdev(struct raid_bdev_config *raid_cfg, const char *base_bdev_name, 836 uint8_t slot) 837 { 838 uint8_t i; 839 struct raid_bdev_config *tmp; 840 841 if (slot >= raid_cfg->num_base_bdevs) { 842 return -EINVAL; 843 } 844 845 TAILQ_FOREACH(tmp, &g_raid_config.raid_bdev_config_head, link) { 846 for (i = 0; i < tmp->num_base_bdevs; i++) { 847 if (tmp->base_bdev[i].name != NULL) { 848 if (!strcmp(tmp->base_bdev[i].name, base_bdev_name)) { 849 SPDK_ERRLOG("duplicate base bdev name %s mentioned\n", 850 base_bdev_name); 851 return -EEXIST; 852 } 853 } 854 } 855 } 856 857 raid_cfg->base_bdev[slot].name = strdup(base_bdev_name); 858 if (raid_cfg->base_bdev[slot].name == NULL) { 859 SPDK_ERRLOG("unable to allocate memory\n"); 860 return -ENOMEM; 861 } 862 863 return 0; 864 } 865 866 static struct { 867 const char *name; 868 enum raid_level value; 869 } g_raid_level_names[] = { 870 { "raid0", RAID0 }, 871 { "0", RAID0 }, 872 { "raid5", RAID5 }, 873 { "5", RAID5 }, 874 { } 875 }; 876 877 enum raid_level raid_bdev_parse_raid_level(const char *str) 878 { 879 unsigned int i; 880 881 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 882 if (strcasecmp(g_raid_level_names[i].name, str) == 0) { 883 return g_raid_level_names[i].value; 884 } 885 } 886 887 return INVALID_RAID_LEVEL; 888 } 889 890 const char * 891 raid_bdev_level_to_str(enum raid_level level) 892 { 893 unsigned int i; 894 895 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 896 if (g_raid_level_names[i].value == level) { 897 return g_raid_level_names[i].name; 898 } 899 } 900 901 return ""; 902 } 903 904 /* 905 * brief: 906 * raid_bdev_parse_raid is used to parse the raid bdev from config file based on 907 * pre-defined raid bdev format in config file. 908 * Format of config file: 909 * [RAID1] 910 * Name raid1 911 * StripSize 64 912 * NumDevices 2 913 * RaidLevel 0 914 * Devices Nvme0n1 Nvme1n1 915 * 916 * [RAID2] 917 * Name raid2 918 * StripSize 64 919 * NumDevices 3 920 * RaidLevel 0 921 * Devices Nvme2n1 Nvme3n1 Nvme4n1 922 * 923 * params: 924 * conf_section - pointer to config section 925 * returns: 926 * 0 - success 927 * non zero - failure 928 */ 929 static int 930 raid_bdev_parse_raid(struct spdk_conf_section *conf_section) 931 { 932 const char *raid_name; 933 uint32_t strip_size; 934 uint8_t num_base_bdevs; 935 const char *raid_level_str; 936 enum raid_level level; 937 const char *base_bdev_name; 938 struct raid_bdev_config *raid_cfg; 939 int rc, i, val; 940 941 raid_name = spdk_conf_section_get_val(conf_section, "Name"); 942 if (raid_name == NULL) { 943 SPDK_ERRLOG("raid_name is null\n"); 944 return -EINVAL; 945 } 946 947 val = spdk_conf_section_get_intval(conf_section, "StripSize"); 948 if (val < 0) { 949 return -EINVAL; 950 } 951 strip_size = val; 952 953 val = spdk_conf_section_get_intval(conf_section, "NumDevices"); 954 if (val < 0) { 955 return -EINVAL; 956 } 957 num_base_bdevs = val; 958 959 raid_level_str = spdk_conf_section_get_val(conf_section, "RaidLevel"); 960 if (raid_level_str == NULL) { 961 SPDK_ERRLOG("Missing RaidLevel\n"); 962 return -EINVAL; 963 } 964 level = raid_bdev_parse_raid_level(raid_level_str); 965 if (level == INVALID_RAID_LEVEL) { 966 SPDK_ERRLOG("Invalid RaidLevel\n"); 967 return -EINVAL; 968 } 969 970 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "%s %" PRIu32 " %u %u\n", 971 raid_name, strip_size, num_base_bdevs, level); 972 973 rc = raid_bdev_config_add(raid_name, strip_size, num_base_bdevs, level, 974 &raid_cfg); 975 if (rc != 0) { 976 SPDK_ERRLOG("Failed to add raid bdev config\n"); 977 return rc; 978 } 979 980 for (i = 0; true; i++) { 981 base_bdev_name = spdk_conf_section_get_nmval(conf_section, "Devices", 0, i); 982 if (base_bdev_name == NULL) { 983 break; 984 } 985 if (i >= num_base_bdevs) { 986 raid_bdev_config_cleanup(raid_cfg); 987 SPDK_ERRLOG("Number of devices mentioned is more than count\n"); 988 return -EINVAL; 989 } 990 991 rc = raid_bdev_config_add_base_bdev(raid_cfg, base_bdev_name, i); 992 if (rc != 0) { 993 raid_bdev_config_cleanup(raid_cfg); 994 SPDK_ERRLOG("Failed to add base bdev to raid bdev config\n"); 995 return rc; 996 } 997 } 998 999 if (i != raid_cfg->num_base_bdevs) { 1000 raid_bdev_config_cleanup(raid_cfg); 1001 SPDK_ERRLOG("Number of devices mentioned is less than count\n"); 1002 return -EINVAL; 1003 } 1004 1005 rc = raid_bdev_create(raid_cfg); 1006 if (rc != 0) { 1007 raid_bdev_config_cleanup(raid_cfg); 1008 SPDK_ERRLOG("Failed to create raid bdev\n"); 1009 return rc; 1010 } 1011 1012 rc = raid_bdev_add_base_devices(raid_cfg); 1013 if (rc != 0) { 1014 SPDK_ERRLOG("Failed to add any base bdev to raid bdev\n"); 1015 /* Config is not removed in this case. */ 1016 } 1017 1018 return 0; 1019 } 1020 1021 /* 1022 * brief: 1023 * raid_bdev_parse_config is used to find the raid bdev config section and parse it 1024 * Format of config file: 1025 * params: 1026 * none 1027 * returns: 1028 * 0 - success 1029 * non zero - failure 1030 */ 1031 static int 1032 raid_bdev_parse_config(void) 1033 { 1034 int ret; 1035 struct spdk_conf_section *conf_section; 1036 1037 conf_section = spdk_conf_first_section(NULL); 1038 while (conf_section != NULL) { 1039 if (spdk_conf_section_match_prefix(conf_section, "RAID")) { 1040 ret = raid_bdev_parse_raid(conf_section); 1041 if (ret < 0) { 1042 SPDK_ERRLOG("Unable to parse raid bdev section\n"); 1043 return ret; 1044 } 1045 } 1046 conf_section = spdk_conf_next_section(conf_section); 1047 } 1048 1049 return 0; 1050 } 1051 1052 /* 1053 * brief: 1054 * raid_bdev_fini_start is called when bdev layer is starting the 1055 * shutdown process 1056 * params: 1057 * none 1058 * returns: 1059 * none 1060 */ 1061 static void 1062 raid_bdev_fini_start(void) 1063 { 1064 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_fini_start\n"); 1065 g_shutdown_started = true; 1066 } 1067 1068 /* 1069 * brief: 1070 * raid_bdev_exit is called on raid bdev module exit time by bdev layer 1071 * params: 1072 * none 1073 * returns: 1074 * none 1075 */ 1076 static void 1077 raid_bdev_exit(void) 1078 { 1079 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_exit\n"); 1080 raid_bdev_free(); 1081 } 1082 1083 /* 1084 * brief: 1085 * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid 1086 * module 1087 * params: 1088 * none 1089 * returns: 1090 * size of spdk_bdev_io context for raid 1091 */ 1092 static int 1093 raid_bdev_get_ctx_size(void) 1094 { 1095 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_get_ctx_size\n"); 1096 return sizeof(struct raid_bdev_io); 1097 } 1098 1099 /* 1100 * brief: 1101 * raid_bdev_get_running_config is used to get the configuration options. 1102 * 1103 * params: 1104 * fp - The pointer to a file that will be written to the configuration options. 1105 * returns: 1106 * none 1107 */ 1108 static void 1109 raid_bdev_get_running_config(FILE *fp) 1110 { 1111 struct raid_bdev *raid_bdev; 1112 struct raid_base_bdev_info *base_info; 1113 int index = 1; 1114 1115 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configured_list, state_link) { 1116 fprintf(fp, 1117 "\n" 1118 "[RAID%d]\n" 1119 " Name %s\n" 1120 " StripSize %" PRIu32 "\n" 1121 " NumDevices %u\n" 1122 " RaidLevel %s\n", 1123 index, raid_bdev->bdev.name, raid_bdev->strip_size_kb, 1124 raid_bdev->num_base_bdevs, 1125 raid_bdev_level_to_str(raid_bdev->level)); 1126 fprintf(fp, 1127 " Devices "); 1128 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1129 if (base_info->bdev) { 1130 fprintf(fp, 1131 "%s ", 1132 base_info->bdev->name); 1133 } 1134 } 1135 fprintf(fp, 1136 "\n"); 1137 index++; 1138 } 1139 } 1140 1141 /* 1142 * brief: 1143 * raid_bdev_can_claim_bdev is the function to check if this base_bdev can be 1144 * claimed by raid bdev or not. 1145 * params: 1146 * bdev_name - represents base bdev name 1147 * _raid_cfg - pointer to raid bdev config parsed from config file 1148 * base_bdev_slot - if bdev can be claimed, it represents the base_bdev correct 1149 * slot. This field is only valid if return value of this function is true 1150 * returns: 1151 * true - if bdev can be claimed 1152 * false - if bdev can't be claimed 1153 */ 1154 static bool 1155 raid_bdev_can_claim_bdev(const char *bdev_name, struct raid_bdev_config **_raid_cfg, 1156 uint8_t *base_bdev_slot) 1157 { 1158 struct raid_bdev_config *raid_cfg; 1159 uint8_t i; 1160 1161 TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) { 1162 for (i = 0; i < raid_cfg->num_base_bdevs; i++) { 1163 /* 1164 * Check if the base bdev name is part of raid bdev configuration. 1165 * If match is found then return true and the slot information where 1166 * this base bdev should be inserted in raid bdev 1167 */ 1168 if (!strcmp(bdev_name, raid_cfg->base_bdev[i].name)) { 1169 *_raid_cfg = raid_cfg; 1170 *base_bdev_slot = i; 1171 return true; 1172 } 1173 } 1174 } 1175 1176 return false; 1177 } 1178 1179 1180 static struct spdk_bdev_module g_raid_if = { 1181 .name = "raid", 1182 .module_init = raid_bdev_init, 1183 .fini_start = raid_bdev_fini_start, 1184 .module_fini = raid_bdev_exit, 1185 .get_ctx_size = raid_bdev_get_ctx_size, 1186 .examine_config = raid_bdev_examine, 1187 .config_text = raid_bdev_get_running_config, 1188 .async_init = false, 1189 .async_fini = false, 1190 }; 1191 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if) 1192 1193 /* 1194 * brief: 1195 * raid_bdev_init is the initialization function for raid bdev module 1196 * params: 1197 * none 1198 * returns: 1199 * 0 - success 1200 * non zero - failure 1201 */ 1202 static int 1203 raid_bdev_init(void) 1204 { 1205 int ret; 1206 1207 /* Parse config file for raids */ 1208 ret = raid_bdev_parse_config(); 1209 if (ret < 0) { 1210 SPDK_ERRLOG("raid bdev init failed parsing\n"); 1211 raid_bdev_free(); 1212 return ret; 1213 } 1214 1215 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_init completed successfully\n"); 1216 1217 return 0; 1218 } 1219 1220 /* 1221 * brief: 1222 * raid_bdev_create allocates raid bdev based on passed configuration 1223 * params: 1224 * raid_cfg - configuration of raid bdev 1225 * returns: 1226 * 0 - success 1227 * non zero - failure 1228 */ 1229 int 1230 raid_bdev_create(struct raid_bdev_config *raid_cfg) 1231 { 1232 struct raid_bdev *raid_bdev; 1233 struct spdk_bdev *raid_bdev_gen; 1234 struct raid_bdev_module *module; 1235 1236 module = raid_bdev_module_find(raid_cfg->level); 1237 if (module == NULL) { 1238 SPDK_ERRLOG("Unsupported raid level '%d'\n", raid_cfg->level); 1239 return -EINVAL; 1240 } 1241 1242 assert(module->base_bdevs_min != 0); 1243 if (raid_cfg->num_base_bdevs < module->base_bdevs_min) { 1244 SPDK_ERRLOG("At least %u base devices required for %s\n", 1245 module->base_bdevs_min, 1246 raid_bdev_level_to_str(raid_cfg->level)); 1247 return -EINVAL; 1248 } 1249 1250 raid_bdev = calloc(1, sizeof(*raid_bdev)); 1251 if (!raid_bdev) { 1252 SPDK_ERRLOG("Unable to allocate memory for raid bdev\n"); 1253 return -ENOMEM; 1254 } 1255 1256 raid_bdev->module = module; 1257 raid_bdev->num_base_bdevs = raid_cfg->num_base_bdevs; 1258 raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs, 1259 sizeof(struct raid_base_bdev_info)); 1260 if (!raid_bdev->base_bdev_info) { 1261 SPDK_ERRLOG("Unable able to allocate base bdev info\n"); 1262 free(raid_bdev); 1263 return -ENOMEM; 1264 } 1265 1266 /* strip_size_kb is from the rpc param. strip_size is in blocks and used 1267 * internally and set later. 1268 */ 1269 raid_bdev->strip_size = 0; 1270 raid_bdev->strip_size_kb = raid_cfg->strip_size; 1271 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1272 raid_bdev->config = raid_cfg; 1273 raid_bdev->level = raid_cfg->level; 1274 1275 raid_bdev_gen = &raid_bdev->bdev; 1276 1277 raid_bdev_gen->name = strdup(raid_cfg->name); 1278 if (!raid_bdev_gen->name) { 1279 SPDK_ERRLOG("Unable to allocate name for raid\n"); 1280 free(raid_bdev->base_bdev_info); 1281 free(raid_bdev); 1282 return -ENOMEM; 1283 } 1284 1285 raid_bdev_gen->product_name = "Raid Volume"; 1286 raid_bdev_gen->ctxt = raid_bdev; 1287 raid_bdev_gen->fn_table = &g_raid_bdev_fn_table; 1288 raid_bdev_gen->module = &g_raid_if; 1289 raid_bdev_gen->write_cache = 0; 1290 1291 TAILQ_INSERT_TAIL(&g_raid_bdev_configuring_list, raid_bdev, state_link); 1292 TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link); 1293 1294 raid_cfg->raid_bdev = raid_bdev; 1295 1296 return 0; 1297 } 1298 1299 /* 1300 * brief 1301 * raid_bdev_alloc_base_bdev_resource allocates resource of base bdev. 1302 * params: 1303 * raid_bdev - pointer to raid bdev 1304 * bdev - pointer to base bdev 1305 * base_bdev_slot - position to add base bdev 1306 * returns: 1307 * 0 - success 1308 * non zero - failure 1309 */ 1310 static int 1311 raid_bdev_alloc_base_bdev_resource(struct raid_bdev *raid_bdev, struct spdk_bdev *bdev, 1312 uint8_t base_bdev_slot) 1313 { 1314 struct spdk_bdev_desc *desc; 1315 int rc; 1316 1317 rc = spdk_bdev_open(bdev, true, raid_bdev_remove_base_bdev, bdev, &desc); 1318 if (rc != 0) { 1319 SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", bdev->name); 1320 return rc; 1321 } 1322 1323 rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if); 1324 if (rc != 0) { 1325 SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n"); 1326 spdk_bdev_close(desc); 1327 return rc; 1328 } 1329 1330 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s is claimed\n", bdev->name); 1331 1332 assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE); 1333 assert(base_bdev_slot < raid_bdev->num_base_bdevs); 1334 1335 raid_bdev->base_bdev_info[base_bdev_slot].thread = spdk_get_thread(); 1336 raid_bdev->base_bdev_info[base_bdev_slot].bdev = bdev; 1337 raid_bdev->base_bdev_info[base_bdev_slot].desc = desc; 1338 raid_bdev->num_base_bdevs_discovered++; 1339 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 1340 1341 return 0; 1342 } 1343 1344 /* 1345 * brief: 1346 * If raid bdev config is complete, then only register the raid bdev to 1347 * bdev layer and remove this raid bdev from configuring list and 1348 * insert the raid bdev to configured list 1349 * params: 1350 * raid_bdev - pointer to raid bdev 1351 * returns: 1352 * 0 - success 1353 * non zero - failure 1354 */ 1355 static int 1356 raid_bdev_configure(struct raid_bdev *raid_bdev) 1357 { 1358 uint32_t blocklen = 0; 1359 struct spdk_bdev *raid_bdev_gen; 1360 struct raid_base_bdev_info *base_info; 1361 int rc = 0; 1362 1363 assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING); 1364 assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs); 1365 1366 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1367 /* Check blocklen for all base bdevs that it should be same */ 1368 if (blocklen == 0) { 1369 blocklen = base_info->bdev->blocklen; 1370 } else if (blocklen != base_info->bdev->blocklen) { 1371 /* 1372 * Assumption is that all the base bdevs for any raid bdev should 1373 * have same blocklen 1374 */ 1375 SPDK_ERRLOG("Blocklen of various bdevs not matching\n"); 1376 return -EINVAL; 1377 } 1378 } 1379 assert(blocklen > 0); 1380 1381 /* The strip_size_kb is read in from user in KB. Convert to blocks here for 1382 * internal use. 1383 */ 1384 raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen; 1385 raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size); 1386 raid_bdev->blocklen_shift = spdk_u32log2(blocklen); 1387 1388 raid_bdev_gen = &raid_bdev->bdev; 1389 raid_bdev_gen->blocklen = blocklen; 1390 1391 rc = raid_bdev->module->start(raid_bdev); 1392 if (rc != 0) { 1393 SPDK_ERRLOG("raid module startup callback failed\n"); 1394 return rc; 1395 } 1396 raid_bdev->state = RAID_BDEV_STATE_ONLINE; 1397 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "io device register %p\n", raid_bdev); 1398 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "blockcnt %lu, blocklen %u\n", 1399 raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen); 1400 spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb, 1401 sizeof(struct raid_bdev_io_channel), 1402 raid_bdev->bdev.name); 1403 rc = spdk_bdev_register(raid_bdev_gen); 1404 if (rc != 0) { 1405 SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n"); 1406 if (raid_bdev->module->stop != NULL) { 1407 raid_bdev->module->stop(raid_bdev); 1408 } 1409 spdk_io_device_unregister(raid_bdev, NULL); 1410 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1411 return rc; 1412 } 1413 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev generic %p\n", raid_bdev_gen); 1414 TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link); 1415 TAILQ_INSERT_TAIL(&g_raid_bdev_configured_list, raid_bdev, state_link); 1416 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev is created with name %s, raid_bdev %p\n", 1417 raid_bdev_gen->name, raid_bdev); 1418 1419 return 0; 1420 } 1421 1422 /* 1423 * brief: 1424 * If raid bdev is online and registered, change the bdev state to 1425 * configuring and unregister this raid device. Queue this raid device 1426 * in configuring list 1427 * params: 1428 * raid_bdev - pointer to raid bdev 1429 * cb_fn - callback function 1430 * cb_arg - argument to callback function 1431 * returns: 1432 * none 1433 */ 1434 static void 1435 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, 1436 void *cb_arg) 1437 { 1438 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 1439 if (cb_fn) { 1440 cb_fn(cb_arg, 0); 1441 } 1442 return; 1443 } 1444 1445 assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered); 1446 TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link); 1447 if (raid_bdev->module->stop != NULL) { 1448 raid_bdev->module->stop(raid_bdev); 1449 } 1450 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 1451 assert(raid_bdev->num_base_bdevs_discovered); 1452 TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link); 1453 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev state chaning from online to offline\n"); 1454 1455 spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg); 1456 } 1457 1458 /* 1459 * brief: 1460 * raid_bdev_find_by_base_bdev function finds the raid bdev which has 1461 * claimed the base bdev. 1462 * params: 1463 * base_bdev - pointer to base bdev pointer 1464 * _raid_bdev - Reference to pointer to raid bdev 1465 * _base_info - Reference to the raid base bdev info. 1466 * returns: 1467 * true - if the raid bdev is found. 1468 * false - if the raid bdev is not found. 1469 */ 1470 static bool 1471 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev, 1472 struct raid_base_bdev_info **_base_info) 1473 { 1474 struct raid_bdev *raid_bdev; 1475 struct raid_base_bdev_info *base_info; 1476 1477 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 1478 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1479 if (base_info->bdev == base_bdev) { 1480 *_raid_bdev = raid_bdev; 1481 *_base_info = base_info; 1482 return true; 1483 } 1484 } 1485 } 1486 1487 return false; 1488 } 1489 1490 /* 1491 * brief: 1492 * raid_bdev_remove_base_bdev function is called by below layers when base_bdev 1493 * is removed. This function checks if this base bdev is part of any raid bdev 1494 * or not. If yes, it takes necessary action on that particular raid bdev. 1495 * params: 1496 * ctx - pointer to base bdev pointer which got removed 1497 * returns: 1498 * none 1499 */ 1500 static void 1501 raid_bdev_remove_base_bdev(void *ctx) 1502 { 1503 struct spdk_bdev *base_bdev = ctx; 1504 struct raid_bdev *raid_bdev = NULL; 1505 struct raid_base_bdev_info *base_info; 1506 1507 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_bdev\n"); 1508 1509 /* Find the raid_bdev which has claimed this base_bdev */ 1510 if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) { 1511 SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name); 1512 return; 1513 } 1514 1515 assert(base_info->desc); 1516 base_info->remove_scheduled = true; 1517 1518 if (raid_bdev->destruct_called == true || 1519 raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { 1520 /* 1521 * As raid bdev is not registered yet or already unregistered, 1522 * so cleanup should be done here itself. 1523 */ 1524 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 1525 if (raid_bdev->num_base_bdevs_discovered == 0) { 1526 /* There is no base bdev for this raid, so free the raid device. */ 1527 raid_bdev_cleanup(raid_bdev); 1528 return; 1529 } 1530 } 1531 1532 raid_bdev_deconfigure(raid_bdev, NULL, NULL); 1533 } 1534 1535 /* 1536 * brief: 1537 * Remove base bdevs from the raid bdev one by one. Skip any base bdev which 1538 * doesn't exist. 1539 * params: 1540 * raid_cfg - pointer to raid bdev config. 1541 * cb_fn - callback function 1542 * cb_ctx - argument to callback function 1543 */ 1544 void 1545 raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg, 1546 raid_bdev_destruct_cb cb_fn, void *cb_arg) 1547 { 1548 struct raid_bdev *raid_bdev; 1549 struct raid_base_bdev_info *base_info; 1550 1551 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_devices\n"); 1552 1553 raid_bdev = raid_cfg->raid_bdev; 1554 if (raid_bdev == NULL) { 1555 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev %s doesn't exist now\n", raid_cfg->name); 1556 if (cb_fn) { 1557 cb_fn(cb_arg, 0); 1558 } 1559 return; 1560 } 1561 1562 if (raid_bdev->destroy_started) { 1563 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "destroying raid bdev %s is already started\n", 1564 raid_cfg->name); 1565 if (cb_fn) { 1566 cb_fn(cb_arg, -EALREADY); 1567 } 1568 return; 1569 } 1570 1571 raid_bdev->destroy_started = true; 1572 1573 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1574 if (base_info->bdev == NULL) { 1575 continue; 1576 } 1577 1578 assert(base_info->desc); 1579 base_info->remove_scheduled = true; 1580 1581 if (raid_bdev->destruct_called == true || 1582 raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { 1583 /* 1584 * As raid bdev is not registered yet or already unregistered, 1585 * so cleanup should be done here itself. 1586 */ 1587 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 1588 if (raid_bdev->num_base_bdevs_discovered == 0) { 1589 /* There is no base bdev for this raid, so free the raid device. */ 1590 raid_bdev_cleanup(raid_bdev); 1591 if (cb_fn) { 1592 cb_fn(cb_arg, 0); 1593 } 1594 return; 1595 } 1596 } 1597 } 1598 1599 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg); 1600 } 1601 1602 /* 1603 * brief: 1604 * raid_bdev_add_base_device function is the actual function which either adds 1605 * the nvme base device to existing raid bdev or create a new raid bdev. It also claims 1606 * the base device and keep the open descriptor. 1607 * params: 1608 * raid_cfg - pointer to raid bdev config 1609 * bdev - pointer to base bdev 1610 * base_bdev_slot - position to add base bdev 1611 * returns: 1612 * 0 - success 1613 * non zero - failure 1614 */ 1615 static int 1616 raid_bdev_add_base_device(struct raid_bdev_config *raid_cfg, struct spdk_bdev *bdev, 1617 uint8_t base_bdev_slot) 1618 { 1619 struct raid_bdev *raid_bdev; 1620 int rc; 1621 1622 raid_bdev = raid_cfg->raid_bdev; 1623 if (!raid_bdev) { 1624 SPDK_ERRLOG("Raid bdev '%s' is not created yet\n", raid_cfg->name); 1625 return -ENODEV; 1626 } 1627 1628 rc = raid_bdev_alloc_base_bdev_resource(raid_bdev, bdev, base_bdev_slot); 1629 if (rc != 0) { 1630 SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", bdev->name); 1631 return rc; 1632 } 1633 1634 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 1635 1636 if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) { 1637 rc = raid_bdev_configure(raid_bdev); 1638 if (rc != 0) { 1639 SPDK_ERRLOG("Failed to configure raid bdev\n"); 1640 return rc; 1641 } 1642 } 1643 1644 return 0; 1645 } 1646 1647 /* 1648 * brief: 1649 * Add base bdevs to the raid bdev one by one. Skip any base bdev which doesn't 1650 * exist or fails to add. If all base bdevs are successfully added, the raid bdev 1651 * moves to the configured state and becomes available. Otherwise, the raid bdev 1652 * stays at the configuring state with added base bdevs. 1653 * params: 1654 * raid_cfg - pointer to raid bdev config 1655 * returns: 1656 * 0 - The raid bdev moves to the configured state or stays at the configuring 1657 * state with added base bdevs due to any nonexistent base bdev. 1658 * non zero - Failed to add any base bdev and stays at the configuring state with 1659 * added base bdevs. 1660 */ 1661 int 1662 raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg) 1663 { 1664 struct spdk_bdev *base_bdev; 1665 uint8_t i; 1666 int rc = 0, _rc; 1667 1668 for (i = 0; i < raid_cfg->num_base_bdevs; i++) { 1669 base_bdev = spdk_bdev_get_by_name(raid_cfg->base_bdev[i].name); 1670 if (base_bdev == NULL) { 1671 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "base bdev %s doesn't exist now\n", 1672 raid_cfg->base_bdev[i].name); 1673 continue; 1674 } 1675 1676 _rc = raid_bdev_add_base_device(raid_cfg, base_bdev, i); 1677 if (_rc != 0) { 1678 SPDK_ERRLOG("Failed to add base bdev %s to RAID bdev %s: %s\n", 1679 raid_cfg->base_bdev[i].name, raid_cfg->name, 1680 spdk_strerror(-_rc)); 1681 if (rc == 0) { 1682 rc = _rc; 1683 } 1684 } 1685 } 1686 1687 return rc; 1688 } 1689 1690 /* 1691 * brief: 1692 * raid_bdev_examine function is the examine function call by the below layers 1693 * like bdev_nvme layer. This function will check if this base bdev can be 1694 * claimed by this raid bdev or not. 1695 * params: 1696 * bdev - pointer to base bdev 1697 * returns: 1698 * none 1699 */ 1700 static void 1701 raid_bdev_examine(struct spdk_bdev *bdev) 1702 { 1703 struct raid_bdev_config *raid_cfg; 1704 uint8_t base_bdev_slot; 1705 1706 if (raid_bdev_can_claim_bdev(bdev->name, &raid_cfg, &base_bdev_slot)) { 1707 raid_bdev_add_base_device(raid_cfg, bdev, base_bdev_slot); 1708 } else { 1709 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s can't be claimed\n", 1710 bdev->name); 1711 } 1712 1713 spdk_bdev_module_examine_done(&g_raid_if); 1714 } 1715 1716 /* Log component for bdev raid bdev module */ 1717 SPDK_LOG_REGISTER_COMPONENT("bdev_raid", SPDK_LOG_BDEV_RAID) 1718