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 assert(str != NULL); 882 883 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 884 if (strcasecmp(g_raid_level_names[i].name, str) == 0) { 885 return g_raid_level_names[i].value; 886 } 887 } 888 889 return INVALID_RAID_LEVEL; 890 } 891 892 const char * 893 raid_bdev_level_to_str(enum raid_level level) 894 { 895 unsigned int i; 896 897 for (i = 0; g_raid_level_names[i].name != NULL; i++) { 898 if (g_raid_level_names[i].value == level) { 899 return g_raid_level_names[i].name; 900 } 901 } 902 903 return ""; 904 } 905 906 /* 907 * brief: 908 * raid_bdev_parse_raid is used to parse the raid bdev from config file based on 909 * pre-defined raid bdev format in config file. 910 * Format of config file: 911 * [RAID1] 912 * Name raid1 913 * StripSize 64 914 * NumDevices 2 915 * RaidLevel 0 916 * Devices Nvme0n1 Nvme1n1 917 * 918 * [RAID2] 919 * Name raid2 920 * StripSize 64 921 * NumDevices 3 922 * RaidLevel 0 923 * Devices Nvme2n1 Nvme3n1 Nvme4n1 924 * 925 * params: 926 * conf_section - pointer to config section 927 * returns: 928 * 0 - success 929 * non zero - failure 930 */ 931 static int 932 raid_bdev_parse_raid(struct spdk_conf_section *conf_section) 933 { 934 const char *raid_name; 935 uint32_t strip_size; 936 uint8_t num_base_bdevs; 937 const char *raid_level_str; 938 enum raid_level level; 939 const char *base_bdev_name; 940 struct raid_bdev_config *raid_cfg; 941 int rc, i, val; 942 943 raid_name = spdk_conf_section_get_val(conf_section, "Name"); 944 if (raid_name == NULL) { 945 SPDK_ERRLOG("raid_name is null\n"); 946 return -EINVAL; 947 } 948 949 val = spdk_conf_section_get_intval(conf_section, "StripSize"); 950 if (val < 0) { 951 return -EINVAL; 952 } 953 strip_size = val; 954 955 val = spdk_conf_section_get_intval(conf_section, "NumDevices"); 956 if (val < 0) { 957 return -EINVAL; 958 } 959 num_base_bdevs = val; 960 961 raid_level_str = spdk_conf_section_get_val(conf_section, "RaidLevel"); 962 if (raid_level_str == NULL) { 963 SPDK_ERRLOG("Missing RaidLevel\n"); 964 return -EINVAL; 965 } 966 level = raid_bdev_parse_raid_level(raid_level_str); 967 if (level == INVALID_RAID_LEVEL) { 968 SPDK_ERRLOG("Invalid RaidLevel\n"); 969 return -EINVAL; 970 } 971 972 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "%s %" PRIu32 " %u %u\n", 973 raid_name, strip_size, num_base_bdevs, level); 974 975 rc = raid_bdev_config_add(raid_name, strip_size, num_base_bdevs, level, 976 &raid_cfg); 977 if (rc != 0) { 978 SPDK_ERRLOG("Failed to add raid bdev config\n"); 979 return rc; 980 } 981 982 for (i = 0; true; i++) { 983 base_bdev_name = spdk_conf_section_get_nmval(conf_section, "Devices", 0, i); 984 if (base_bdev_name == NULL) { 985 break; 986 } 987 if (i >= num_base_bdevs) { 988 raid_bdev_config_cleanup(raid_cfg); 989 SPDK_ERRLOG("Number of devices mentioned is more than count\n"); 990 return -EINVAL; 991 } 992 993 rc = raid_bdev_config_add_base_bdev(raid_cfg, base_bdev_name, i); 994 if (rc != 0) { 995 raid_bdev_config_cleanup(raid_cfg); 996 SPDK_ERRLOG("Failed to add base bdev to raid bdev config\n"); 997 return rc; 998 } 999 } 1000 1001 if (i != raid_cfg->num_base_bdevs) { 1002 raid_bdev_config_cleanup(raid_cfg); 1003 SPDK_ERRLOG("Number of devices mentioned is less than count\n"); 1004 return -EINVAL; 1005 } 1006 1007 rc = raid_bdev_create(raid_cfg); 1008 if (rc != 0) { 1009 raid_bdev_config_cleanup(raid_cfg); 1010 SPDK_ERRLOG("Failed to create raid bdev\n"); 1011 return rc; 1012 } 1013 1014 rc = raid_bdev_add_base_devices(raid_cfg); 1015 if (rc != 0) { 1016 SPDK_ERRLOG("Failed to add any base bdev to raid bdev\n"); 1017 /* Config is not removed in this case. */ 1018 } 1019 1020 return 0; 1021 } 1022 1023 /* 1024 * brief: 1025 * raid_bdev_parse_config is used to find the raid bdev config section and parse it 1026 * Format of config file: 1027 * params: 1028 * none 1029 * returns: 1030 * 0 - success 1031 * non zero - failure 1032 */ 1033 static int 1034 raid_bdev_parse_config(void) 1035 { 1036 int ret; 1037 struct spdk_conf_section *conf_section; 1038 1039 conf_section = spdk_conf_first_section(NULL); 1040 while (conf_section != NULL) { 1041 if (spdk_conf_section_match_prefix(conf_section, "RAID")) { 1042 ret = raid_bdev_parse_raid(conf_section); 1043 if (ret < 0) { 1044 SPDK_ERRLOG("Unable to parse raid bdev section\n"); 1045 return ret; 1046 } 1047 } 1048 conf_section = spdk_conf_next_section(conf_section); 1049 } 1050 1051 return 0; 1052 } 1053 1054 /* 1055 * brief: 1056 * raid_bdev_fini_start is called when bdev layer is starting the 1057 * shutdown process 1058 * params: 1059 * none 1060 * returns: 1061 * none 1062 */ 1063 static void 1064 raid_bdev_fini_start(void) 1065 { 1066 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_fini_start\n"); 1067 g_shutdown_started = true; 1068 } 1069 1070 /* 1071 * brief: 1072 * raid_bdev_exit is called on raid bdev module exit time by bdev layer 1073 * params: 1074 * none 1075 * returns: 1076 * none 1077 */ 1078 static void 1079 raid_bdev_exit(void) 1080 { 1081 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_exit\n"); 1082 raid_bdev_free(); 1083 } 1084 1085 /* 1086 * brief: 1087 * raid_bdev_get_ctx_size is used to return the context size of bdev_io for raid 1088 * module 1089 * params: 1090 * none 1091 * returns: 1092 * size of spdk_bdev_io context for raid 1093 */ 1094 static int 1095 raid_bdev_get_ctx_size(void) 1096 { 1097 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_get_ctx_size\n"); 1098 return sizeof(struct raid_bdev_io); 1099 } 1100 1101 /* 1102 * brief: 1103 * raid_bdev_get_running_config is used to get the configuration options. 1104 * 1105 * params: 1106 * fp - The pointer to a file that will be written to the configuration options. 1107 * returns: 1108 * none 1109 */ 1110 static void 1111 raid_bdev_get_running_config(FILE *fp) 1112 { 1113 struct raid_bdev *raid_bdev; 1114 struct raid_base_bdev_info *base_info; 1115 int index = 1; 1116 1117 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_configured_list, state_link) { 1118 fprintf(fp, 1119 "\n" 1120 "[RAID%d]\n" 1121 " Name %s\n" 1122 " StripSize %" PRIu32 "\n" 1123 " NumDevices %u\n" 1124 " RaidLevel %s\n", 1125 index, raid_bdev->bdev.name, raid_bdev->strip_size_kb, 1126 raid_bdev->num_base_bdevs, 1127 raid_bdev_level_to_str(raid_bdev->level)); 1128 fprintf(fp, 1129 " Devices "); 1130 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1131 if (base_info->bdev) { 1132 fprintf(fp, 1133 "%s ", 1134 base_info->bdev->name); 1135 } 1136 } 1137 fprintf(fp, 1138 "\n"); 1139 index++; 1140 } 1141 } 1142 1143 /* 1144 * brief: 1145 * raid_bdev_can_claim_bdev is the function to check if this base_bdev can be 1146 * claimed by raid bdev or not. 1147 * params: 1148 * bdev_name - represents base bdev name 1149 * _raid_cfg - pointer to raid bdev config parsed from config file 1150 * base_bdev_slot - if bdev can be claimed, it represents the base_bdev correct 1151 * slot. This field is only valid if return value of this function is true 1152 * returns: 1153 * true - if bdev can be claimed 1154 * false - if bdev can't be claimed 1155 */ 1156 static bool 1157 raid_bdev_can_claim_bdev(const char *bdev_name, struct raid_bdev_config **_raid_cfg, 1158 uint8_t *base_bdev_slot) 1159 { 1160 struct raid_bdev_config *raid_cfg; 1161 uint8_t i; 1162 1163 TAILQ_FOREACH(raid_cfg, &g_raid_config.raid_bdev_config_head, link) { 1164 for (i = 0; i < raid_cfg->num_base_bdevs; i++) { 1165 /* 1166 * Check if the base bdev name is part of raid bdev configuration. 1167 * If match is found then return true and the slot information where 1168 * this base bdev should be inserted in raid bdev 1169 */ 1170 if (!strcmp(bdev_name, raid_cfg->base_bdev[i].name)) { 1171 *_raid_cfg = raid_cfg; 1172 *base_bdev_slot = i; 1173 return true; 1174 } 1175 } 1176 } 1177 1178 return false; 1179 } 1180 1181 1182 static struct spdk_bdev_module g_raid_if = { 1183 .name = "raid", 1184 .module_init = raid_bdev_init, 1185 .fini_start = raid_bdev_fini_start, 1186 .module_fini = raid_bdev_exit, 1187 .get_ctx_size = raid_bdev_get_ctx_size, 1188 .examine_config = raid_bdev_examine, 1189 .config_text = raid_bdev_get_running_config, 1190 .async_init = false, 1191 .async_fini = false, 1192 }; 1193 SPDK_BDEV_MODULE_REGISTER(raid, &g_raid_if) 1194 1195 /* 1196 * brief: 1197 * raid_bdev_init is the initialization function for raid bdev module 1198 * params: 1199 * none 1200 * returns: 1201 * 0 - success 1202 * non zero - failure 1203 */ 1204 static int 1205 raid_bdev_init(void) 1206 { 1207 int ret; 1208 1209 /* Parse config file for raids */ 1210 ret = raid_bdev_parse_config(); 1211 if (ret < 0) { 1212 SPDK_ERRLOG("raid bdev init failed parsing\n"); 1213 raid_bdev_free(); 1214 return ret; 1215 } 1216 1217 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_init completed successfully\n"); 1218 1219 return 0; 1220 } 1221 1222 /* 1223 * brief: 1224 * raid_bdev_create allocates raid bdev based on passed configuration 1225 * params: 1226 * raid_cfg - configuration of raid bdev 1227 * returns: 1228 * 0 - success 1229 * non zero - failure 1230 */ 1231 int 1232 raid_bdev_create(struct raid_bdev_config *raid_cfg) 1233 { 1234 struct raid_bdev *raid_bdev; 1235 struct spdk_bdev *raid_bdev_gen; 1236 struct raid_bdev_module *module; 1237 1238 module = raid_bdev_module_find(raid_cfg->level); 1239 if (module == NULL) { 1240 SPDK_ERRLOG("Unsupported raid level '%d'\n", raid_cfg->level); 1241 return -EINVAL; 1242 } 1243 1244 assert(module->base_bdevs_min != 0); 1245 if (raid_cfg->num_base_bdevs < module->base_bdevs_min) { 1246 SPDK_ERRLOG("At least %u base devices required for %s\n", 1247 module->base_bdevs_min, 1248 raid_bdev_level_to_str(raid_cfg->level)); 1249 return -EINVAL; 1250 } 1251 1252 raid_bdev = calloc(1, sizeof(*raid_bdev)); 1253 if (!raid_bdev) { 1254 SPDK_ERRLOG("Unable to allocate memory for raid bdev\n"); 1255 return -ENOMEM; 1256 } 1257 1258 raid_bdev->module = module; 1259 raid_bdev->num_base_bdevs = raid_cfg->num_base_bdevs; 1260 raid_bdev->base_bdev_info = calloc(raid_bdev->num_base_bdevs, 1261 sizeof(struct raid_base_bdev_info)); 1262 if (!raid_bdev->base_bdev_info) { 1263 SPDK_ERRLOG("Unable able to allocate base bdev info\n"); 1264 free(raid_bdev); 1265 return -ENOMEM; 1266 } 1267 1268 /* strip_size_kb is from the rpc param. strip_size is in blocks and used 1269 * internally and set later. 1270 */ 1271 raid_bdev->strip_size = 0; 1272 raid_bdev->strip_size_kb = raid_cfg->strip_size; 1273 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1274 raid_bdev->config = raid_cfg; 1275 raid_bdev->level = raid_cfg->level; 1276 1277 raid_bdev_gen = &raid_bdev->bdev; 1278 1279 raid_bdev_gen->name = strdup(raid_cfg->name); 1280 if (!raid_bdev_gen->name) { 1281 SPDK_ERRLOG("Unable to allocate name for raid\n"); 1282 free(raid_bdev->base_bdev_info); 1283 free(raid_bdev); 1284 return -ENOMEM; 1285 } 1286 1287 raid_bdev_gen->product_name = "Raid Volume"; 1288 raid_bdev_gen->ctxt = raid_bdev; 1289 raid_bdev_gen->fn_table = &g_raid_bdev_fn_table; 1290 raid_bdev_gen->module = &g_raid_if; 1291 raid_bdev_gen->write_cache = 0; 1292 1293 TAILQ_INSERT_TAIL(&g_raid_bdev_configuring_list, raid_bdev, state_link); 1294 TAILQ_INSERT_TAIL(&g_raid_bdev_list, raid_bdev, global_link); 1295 1296 raid_cfg->raid_bdev = raid_bdev; 1297 1298 return 0; 1299 } 1300 1301 /* 1302 * brief 1303 * raid_bdev_alloc_base_bdev_resource allocates resource of base bdev. 1304 * params: 1305 * raid_bdev - pointer to raid bdev 1306 * bdev - pointer to base bdev 1307 * base_bdev_slot - position to add base bdev 1308 * returns: 1309 * 0 - success 1310 * non zero - failure 1311 */ 1312 static int 1313 raid_bdev_alloc_base_bdev_resource(struct raid_bdev *raid_bdev, struct spdk_bdev *bdev, 1314 uint8_t base_bdev_slot) 1315 { 1316 struct spdk_bdev_desc *desc; 1317 int rc; 1318 1319 rc = spdk_bdev_open(bdev, true, raid_bdev_remove_base_bdev, bdev, &desc); 1320 if (rc != 0) { 1321 SPDK_ERRLOG("Unable to create desc on bdev '%s'\n", bdev->name); 1322 return rc; 1323 } 1324 1325 rc = spdk_bdev_module_claim_bdev(bdev, NULL, &g_raid_if); 1326 if (rc != 0) { 1327 SPDK_ERRLOG("Unable to claim this bdev as it is already claimed\n"); 1328 spdk_bdev_close(desc); 1329 return rc; 1330 } 1331 1332 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s is claimed\n", bdev->name); 1333 1334 assert(raid_bdev->state != RAID_BDEV_STATE_ONLINE); 1335 assert(base_bdev_slot < raid_bdev->num_base_bdevs); 1336 1337 raid_bdev->base_bdev_info[base_bdev_slot].thread = spdk_get_thread(); 1338 raid_bdev->base_bdev_info[base_bdev_slot].bdev = bdev; 1339 raid_bdev->base_bdev_info[base_bdev_slot].desc = desc; 1340 raid_bdev->num_base_bdevs_discovered++; 1341 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 1342 1343 return 0; 1344 } 1345 1346 /* 1347 * brief: 1348 * If raid bdev config is complete, then only register the raid bdev to 1349 * bdev layer and remove this raid bdev from configuring list and 1350 * insert the raid bdev to configured list 1351 * params: 1352 * raid_bdev - pointer to raid bdev 1353 * returns: 1354 * 0 - success 1355 * non zero - failure 1356 */ 1357 static int 1358 raid_bdev_configure(struct raid_bdev *raid_bdev) 1359 { 1360 uint32_t blocklen = 0; 1361 struct spdk_bdev *raid_bdev_gen; 1362 struct raid_base_bdev_info *base_info; 1363 int rc = 0; 1364 1365 assert(raid_bdev->state == RAID_BDEV_STATE_CONFIGURING); 1366 assert(raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs); 1367 1368 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1369 /* Check blocklen for all base bdevs that it should be same */ 1370 if (blocklen == 0) { 1371 blocklen = base_info->bdev->blocklen; 1372 } else if (blocklen != base_info->bdev->blocklen) { 1373 /* 1374 * Assumption is that all the base bdevs for any raid bdev should 1375 * have same blocklen 1376 */ 1377 SPDK_ERRLOG("Blocklen of various bdevs not matching\n"); 1378 return -EINVAL; 1379 } 1380 } 1381 assert(blocklen > 0); 1382 1383 /* The strip_size_kb is read in from user in KB. Convert to blocks here for 1384 * internal use. 1385 */ 1386 raid_bdev->strip_size = (raid_bdev->strip_size_kb * 1024) / blocklen; 1387 raid_bdev->strip_size_shift = spdk_u32log2(raid_bdev->strip_size); 1388 raid_bdev->blocklen_shift = spdk_u32log2(blocklen); 1389 1390 raid_bdev_gen = &raid_bdev->bdev; 1391 raid_bdev_gen->blocklen = blocklen; 1392 1393 rc = raid_bdev->module->start(raid_bdev); 1394 if (rc != 0) { 1395 SPDK_ERRLOG("raid module startup callback failed\n"); 1396 return rc; 1397 } 1398 raid_bdev->state = RAID_BDEV_STATE_ONLINE; 1399 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "io device register %p\n", raid_bdev); 1400 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "blockcnt %lu, blocklen %u\n", 1401 raid_bdev_gen->blockcnt, raid_bdev_gen->blocklen); 1402 spdk_io_device_register(raid_bdev, raid_bdev_create_cb, raid_bdev_destroy_cb, 1403 sizeof(struct raid_bdev_io_channel), 1404 raid_bdev->bdev.name); 1405 rc = spdk_bdev_register(raid_bdev_gen); 1406 if (rc != 0) { 1407 SPDK_ERRLOG("Unable to register raid bdev and stay at configuring state\n"); 1408 if (raid_bdev->module->stop != NULL) { 1409 raid_bdev->module->stop(raid_bdev); 1410 } 1411 spdk_io_device_unregister(raid_bdev, NULL); 1412 raid_bdev->state = RAID_BDEV_STATE_CONFIGURING; 1413 return rc; 1414 } 1415 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev generic %p\n", raid_bdev_gen); 1416 TAILQ_REMOVE(&g_raid_bdev_configuring_list, raid_bdev, state_link); 1417 TAILQ_INSERT_TAIL(&g_raid_bdev_configured_list, raid_bdev, state_link); 1418 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev is created with name %s, raid_bdev %p\n", 1419 raid_bdev_gen->name, raid_bdev); 1420 1421 return 0; 1422 } 1423 1424 /* 1425 * brief: 1426 * If raid bdev is online and registered, change the bdev state to 1427 * configuring and unregister this raid device. Queue this raid device 1428 * in configuring list 1429 * params: 1430 * raid_bdev - pointer to raid bdev 1431 * cb_fn - callback function 1432 * cb_arg - argument to callback function 1433 * returns: 1434 * none 1435 */ 1436 static void 1437 raid_bdev_deconfigure(struct raid_bdev *raid_bdev, raid_bdev_destruct_cb cb_fn, 1438 void *cb_arg) 1439 { 1440 if (raid_bdev->state != RAID_BDEV_STATE_ONLINE) { 1441 if (cb_fn) { 1442 cb_fn(cb_arg, 0); 1443 } 1444 return; 1445 } 1446 1447 assert(raid_bdev->num_base_bdevs == raid_bdev->num_base_bdevs_discovered); 1448 TAILQ_REMOVE(&g_raid_bdev_configured_list, raid_bdev, state_link); 1449 if (raid_bdev->module->stop != NULL) { 1450 raid_bdev->module->stop(raid_bdev); 1451 } 1452 raid_bdev->state = RAID_BDEV_STATE_OFFLINE; 1453 assert(raid_bdev->num_base_bdevs_discovered); 1454 TAILQ_INSERT_TAIL(&g_raid_bdev_offline_list, raid_bdev, state_link); 1455 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev state chaning from online to offline\n"); 1456 1457 spdk_bdev_unregister(&raid_bdev->bdev, cb_fn, cb_arg); 1458 } 1459 1460 /* 1461 * brief: 1462 * raid_bdev_find_by_base_bdev function finds the raid bdev which has 1463 * claimed the base bdev. 1464 * params: 1465 * base_bdev - pointer to base bdev pointer 1466 * _raid_bdev - Reference to pointer to raid bdev 1467 * _base_info - Reference to the raid base bdev info. 1468 * returns: 1469 * true - if the raid bdev is found. 1470 * false - if the raid bdev is not found. 1471 */ 1472 static bool 1473 raid_bdev_find_by_base_bdev(struct spdk_bdev *base_bdev, struct raid_bdev **_raid_bdev, 1474 struct raid_base_bdev_info **_base_info) 1475 { 1476 struct raid_bdev *raid_bdev; 1477 struct raid_base_bdev_info *base_info; 1478 1479 TAILQ_FOREACH(raid_bdev, &g_raid_bdev_list, global_link) { 1480 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1481 if (base_info->bdev == base_bdev) { 1482 *_raid_bdev = raid_bdev; 1483 *_base_info = base_info; 1484 return true; 1485 } 1486 } 1487 } 1488 1489 return false; 1490 } 1491 1492 /* 1493 * brief: 1494 * raid_bdev_remove_base_bdev function is called by below layers when base_bdev 1495 * is removed. This function checks if this base bdev is part of any raid bdev 1496 * or not. If yes, it takes necessary action on that particular raid bdev. 1497 * params: 1498 * ctx - pointer to base bdev pointer which got removed 1499 * returns: 1500 * none 1501 */ 1502 static void 1503 raid_bdev_remove_base_bdev(void *ctx) 1504 { 1505 struct spdk_bdev *base_bdev = ctx; 1506 struct raid_bdev *raid_bdev = NULL; 1507 struct raid_base_bdev_info *base_info; 1508 1509 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_bdev\n"); 1510 1511 /* Find the raid_bdev which has claimed this base_bdev */ 1512 if (!raid_bdev_find_by_base_bdev(base_bdev, &raid_bdev, &base_info)) { 1513 SPDK_ERRLOG("bdev to remove '%s' not found\n", base_bdev->name); 1514 return; 1515 } 1516 1517 assert(base_info->desc); 1518 base_info->remove_scheduled = true; 1519 1520 if (raid_bdev->destruct_called == true || 1521 raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { 1522 /* 1523 * As raid bdev is not registered yet or already unregistered, 1524 * so cleanup should be done here itself. 1525 */ 1526 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 1527 if (raid_bdev->num_base_bdevs_discovered == 0) { 1528 /* There is no base bdev for this raid, so free the raid device. */ 1529 raid_bdev_cleanup(raid_bdev); 1530 return; 1531 } 1532 } 1533 1534 raid_bdev_deconfigure(raid_bdev, NULL, NULL); 1535 } 1536 1537 /* 1538 * brief: 1539 * Remove base bdevs from the raid bdev one by one. Skip any base bdev which 1540 * doesn't exist. 1541 * params: 1542 * raid_cfg - pointer to raid bdev config. 1543 * cb_fn - callback function 1544 * cb_ctx - argument to callback function 1545 */ 1546 void 1547 raid_bdev_remove_base_devices(struct raid_bdev_config *raid_cfg, 1548 raid_bdev_destruct_cb cb_fn, void *cb_arg) 1549 { 1550 struct raid_bdev *raid_bdev; 1551 struct raid_base_bdev_info *base_info; 1552 1553 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid_bdev_remove_base_devices\n"); 1554 1555 raid_bdev = raid_cfg->raid_bdev; 1556 if (raid_bdev == NULL) { 1557 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "raid bdev %s doesn't exist now\n", raid_cfg->name); 1558 if (cb_fn) { 1559 cb_fn(cb_arg, 0); 1560 } 1561 return; 1562 } 1563 1564 if (raid_bdev->destroy_started) { 1565 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "destroying raid bdev %s is already started\n", 1566 raid_cfg->name); 1567 if (cb_fn) { 1568 cb_fn(cb_arg, -EALREADY); 1569 } 1570 return; 1571 } 1572 1573 raid_bdev->destroy_started = true; 1574 1575 RAID_FOR_EACH_BASE_BDEV(raid_bdev, base_info) { 1576 if (base_info->bdev == NULL) { 1577 continue; 1578 } 1579 1580 assert(base_info->desc); 1581 base_info->remove_scheduled = true; 1582 1583 if (raid_bdev->destruct_called == true || 1584 raid_bdev->state == RAID_BDEV_STATE_CONFIGURING) { 1585 /* 1586 * As raid bdev is not registered yet or already unregistered, 1587 * so cleanup should be done here itself. 1588 */ 1589 raid_bdev_free_base_bdev_resource(raid_bdev, base_info); 1590 if (raid_bdev->num_base_bdevs_discovered == 0) { 1591 /* There is no base bdev for this raid, so free the raid device. */ 1592 raid_bdev_cleanup(raid_bdev); 1593 if (cb_fn) { 1594 cb_fn(cb_arg, 0); 1595 } 1596 return; 1597 } 1598 } 1599 } 1600 1601 raid_bdev_deconfigure(raid_bdev, cb_fn, cb_arg); 1602 } 1603 1604 /* 1605 * brief: 1606 * raid_bdev_add_base_device function is the actual function which either adds 1607 * the nvme base device to existing raid bdev or create a new raid bdev. It also claims 1608 * the base device and keep the open descriptor. 1609 * params: 1610 * raid_cfg - pointer to raid bdev config 1611 * bdev - pointer to base bdev 1612 * base_bdev_slot - position to add base bdev 1613 * returns: 1614 * 0 - success 1615 * non zero - failure 1616 */ 1617 static int 1618 raid_bdev_add_base_device(struct raid_bdev_config *raid_cfg, struct spdk_bdev *bdev, 1619 uint8_t base_bdev_slot) 1620 { 1621 struct raid_bdev *raid_bdev; 1622 int rc; 1623 1624 raid_bdev = raid_cfg->raid_bdev; 1625 if (!raid_bdev) { 1626 SPDK_ERRLOG("Raid bdev '%s' is not created yet\n", raid_cfg->name); 1627 return -ENODEV; 1628 } 1629 1630 rc = raid_bdev_alloc_base_bdev_resource(raid_bdev, bdev, base_bdev_slot); 1631 if (rc != 0) { 1632 SPDK_ERRLOG("Failed to allocate resource for bdev '%s'\n", bdev->name); 1633 return rc; 1634 } 1635 1636 assert(raid_bdev->num_base_bdevs_discovered <= raid_bdev->num_base_bdevs); 1637 1638 if (raid_bdev->num_base_bdevs_discovered == raid_bdev->num_base_bdevs) { 1639 rc = raid_bdev_configure(raid_bdev); 1640 if (rc != 0) { 1641 SPDK_ERRLOG("Failed to configure raid bdev\n"); 1642 return rc; 1643 } 1644 } 1645 1646 return 0; 1647 } 1648 1649 /* 1650 * brief: 1651 * Add base bdevs to the raid bdev one by one. Skip any base bdev which doesn't 1652 * exist or fails to add. If all base bdevs are successfully added, the raid bdev 1653 * moves to the configured state and becomes available. Otherwise, the raid bdev 1654 * stays at the configuring state with added base bdevs. 1655 * params: 1656 * raid_cfg - pointer to raid bdev config 1657 * returns: 1658 * 0 - The raid bdev moves to the configured state or stays at the configuring 1659 * state with added base bdevs due to any nonexistent base bdev. 1660 * non zero - Failed to add any base bdev and stays at the configuring state with 1661 * added base bdevs. 1662 */ 1663 int 1664 raid_bdev_add_base_devices(struct raid_bdev_config *raid_cfg) 1665 { 1666 struct spdk_bdev *base_bdev; 1667 uint8_t i; 1668 int rc = 0, _rc; 1669 1670 for (i = 0; i < raid_cfg->num_base_bdevs; i++) { 1671 base_bdev = spdk_bdev_get_by_name(raid_cfg->base_bdev[i].name); 1672 if (base_bdev == NULL) { 1673 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "base bdev %s doesn't exist now\n", 1674 raid_cfg->base_bdev[i].name); 1675 continue; 1676 } 1677 1678 _rc = raid_bdev_add_base_device(raid_cfg, base_bdev, i); 1679 if (_rc != 0) { 1680 SPDK_ERRLOG("Failed to add base bdev %s to RAID bdev %s: %s\n", 1681 raid_cfg->base_bdev[i].name, raid_cfg->name, 1682 spdk_strerror(-_rc)); 1683 if (rc == 0) { 1684 rc = _rc; 1685 } 1686 } 1687 } 1688 1689 return rc; 1690 } 1691 1692 /* 1693 * brief: 1694 * raid_bdev_examine function is the examine function call by the below layers 1695 * like bdev_nvme layer. This function will check if this base bdev can be 1696 * claimed by this raid bdev or not. 1697 * params: 1698 * bdev - pointer to base bdev 1699 * returns: 1700 * none 1701 */ 1702 static void 1703 raid_bdev_examine(struct spdk_bdev *bdev) 1704 { 1705 struct raid_bdev_config *raid_cfg; 1706 uint8_t base_bdev_slot; 1707 1708 if (raid_bdev_can_claim_bdev(bdev->name, &raid_cfg, &base_bdev_slot)) { 1709 raid_bdev_add_base_device(raid_cfg, bdev, base_bdev_slot); 1710 } else { 1711 SPDK_DEBUGLOG(SPDK_LOG_BDEV_RAID, "bdev %s can't be claimed\n", 1712 bdev->name); 1713 } 1714 1715 spdk_bdev_module_examine_done(&g_raid_if); 1716 } 1717 1718 /* Log component for bdev raid bdev module */ 1719 SPDK_LOG_REGISTER_COMPONENT("bdev_raid", SPDK_LOG_BDEV_RAID) 1720