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 <ocf/ocf.h> 35 #include <ocf/ocf_types.h> 36 #include <ocf/ocf_mngt.h> 37 38 #include "ctx.h" 39 #include "data.h" 40 #include "volume.h" 41 #include "utils.h" 42 #include "vbdev_ocf.h" 43 44 #include "spdk/bdev_module.h" 45 #include "spdk/thread.h" 46 #include "spdk/string.h" 47 #include "spdk/log.h" 48 #include "spdk/cpuset.h" 49 50 static struct spdk_bdev_module ocf_if; 51 52 static TAILQ_HEAD(, vbdev_ocf) g_ocf_vbdev_head 53 = TAILQ_HEAD_INITIALIZER(g_ocf_vbdev_head); 54 55 static TAILQ_HEAD(, examining_bdev) g_ocf_examining_bdevs_head 56 = TAILQ_HEAD_INITIALIZER(g_ocf_examining_bdevs_head); 57 58 bool g_fini_started = false; 59 60 /* Structure for keeping list of bdevs that are claimed but not used yet */ 61 struct examining_bdev { 62 struct spdk_bdev *bdev; 63 TAILQ_ENTRY(examining_bdev) tailq; 64 }; 65 66 /* Add bdev to list of claimed */ 67 static void 68 examine_start(struct spdk_bdev *bdev) 69 { 70 struct examining_bdev *entry = malloc(sizeof(*entry)); 71 72 assert(entry); 73 entry->bdev = bdev; 74 TAILQ_INSERT_TAIL(&g_ocf_examining_bdevs_head, entry, tailq); 75 } 76 77 /* Find bdev on list of claimed bdevs, then remove it, 78 * if it was the last one on list then report examine done */ 79 static void 80 examine_done(int status, struct vbdev_ocf *vbdev, void *cb_arg) 81 { 82 struct spdk_bdev *bdev = cb_arg; 83 struct examining_bdev *entry, *safe, *found = NULL; 84 85 TAILQ_FOREACH_SAFE(entry, &g_ocf_examining_bdevs_head, tailq, safe) { 86 if (entry->bdev == bdev) { 87 if (found) { 88 goto remove; 89 } else { 90 found = entry; 91 } 92 } 93 } 94 95 assert(found); 96 spdk_bdev_module_examine_done(&ocf_if); 97 98 remove: 99 TAILQ_REMOVE(&g_ocf_examining_bdevs_head, found, tailq); 100 free(found); 101 } 102 103 /* Free allocated strings and structure itself 104 * Used at shutdown only */ 105 static void 106 free_vbdev(struct vbdev_ocf *vbdev) 107 { 108 if (!vbdev) { 109 return; 110 } 111 112 free(vbdev->name); 113 free(vbdev->cache.name); 114 free(vbdev->core.name); 115 free(vbdev); 116 } 117 118 /* Get existing cache base 119 * that is attached to other vbdev */ 120 static struct vbdev_ocf_base * 121 get_other_cache_base(struct vbdev_ocf_base *base) 122 { 123 struct vbdev_ocf *vbdev; 124 125 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 126 if (&vbdev->cache == base || !vbdev->cache.attached) { 127 continue; 128 } 129 if (!strcmp(vbdev->cache.name, base->name)) { 130 return &vbdev->cache; 131 } 132 } 133 134 return NULL; 135 } 136 137 static bool 138 is_ocf_cache_running(struct vbdev_ocf *vbdev) 139 { 140 if (vbdev->cache.attached && vbdev->ocf_cache) { 141 return ocf_cache_is_running(vbdev->ocf_cache); 142 } 143 return false; 144 } 145 146 /* Get existing OCF cache instance 147 * that is started by other vbdev */ 148 static ocf_cache_t 149 get_other_cache_instance(struct vbdev_ocf *vbdev) 150 { 151 struct vbdev_ocf *cmp; 152 153 TAILQ_FOREACH(cmp, &g_ocf_vbdev_head, tailq) { 154 if (cmp->state.doing_finish || cmp == vbdev) { 155 continue; 156 } 157 if (strcmp(cmp->cache.name, vbdev->cache.name)) { 158 continue; 159 } 160 if (is_ocf_cache_running(cmp)) { 161 return cmp->ocf_cache; 162 } 163 } 164 165 return NULL; 166 } 167 168 static void 169 _remove_base_bdev(void *ctx) 170 { 171 struct spdk_bdev_desc *desc = ctx; 172 173 spdk_bdev_close(desc); 174 } 175 176 /* Close and unclaim base bdev */ 177 static void 178 remove_base_bdev(struct vbdev_ocf_base *base) 179 { 180 if (base->attached) { 181 if (base->management_channel) { 182 spdk_put_io_channel(base->management_channel); 183 } 184 185 spdk_bdev_module_release_bdev(base->bdev); 186 /* Close the underlying bdev on its same opened thread. */ 187 if (base->thread && base->thread != spdk_get_thread()) { 188 spdk_thread_send_msg(base->thread, _remove_base_bdev, base->desc); 189 } else { 190 spdk_bdev_close(base->desc); 191 } 192 base->attached = false; 193 } 194 } 195 196 /* Finish unregister operation */ 197 static void 198 unregister_finish(struct vbdev_ocf *vbdev) 199 { 200 spdk_bdev_destruct_done(&vbdev->exp_bdev, vbdev->state.stop_status); 201 ocf_mngt_cache_put(vbdev->ocf_cache); 202 vbdev_ocf_cache_ctx_put(vbdev->cache_ctx); 203 vbdev_ocf_mngt_continue(vbdev, 0); 204 } 205 206 static void 207 close_core_bdev(struct vbdev_ocf *vbdev) 208 { 209 remove_base_bdev(&vbdev->core); 210 vbdev_ocf_mngt_continue(vbdev, 0); 211 } 212 213 static void 214 remove_core_cmpl(void *priv, int error) 215 { 216 struct vbdev_ocf *vbdev = priv; 217 218 ocf_mngt_cache_unlock(vbdev->ocf_cache); 219 vbdev_ocf_mngt_continue(vbdev, error); 220 } 221 222 /* Try to lock cache, then remove core */ 223 static void 224 remove_core_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error) 225 { 226 struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv; 227 228 if (error) { 229 SPDK_ERRLOG("Error %d, can not lock cache instance %s\n", 230 error, vbdev->name); 231 vbdev_ocf_mngt_continue(vbdev, error); 232 return; 233 } 234 235 ocf_mngt_cache_remove_core(vbdev->ocf_core, remove_core_cmpl, vbdev); 236 } 237 238 /* Detach core base */ 239 static void 240 detach_core(struct vbdev_ocf *vbdev) 241 { 242 if (is_ocf_cache_running(vbdev)) { 243 ocf_mngt_cache_lock(vbdev->ocf_cache, remove_core_cache_lock_cmpl, vbdev); 244 } else { 245 vbdev_ocf_mngt_continue(vbdev, 0); 246 } 247 } 248 249 static void 250 close_cache_bdev(struct vbdev_ocf *vbdev) 251 { 252 remove_base_bdev(&vbdev->cache); 253 vbdev_ocf_mngt_continue(vbdev, 0); 254 } 255 256 /* Detach cache base */ 257 static void 258 detach_cache(struct vbdev_ocf *vbdev) 259 { 260 vbdev->state.stop_status = vbdev->mngt_ctx.status; 261 262 /* If some other vbdev references this cache bdev, 263 * we detach this only by changing the flag, without actual close */ 264 if (get_other_cache_base(&vbdev->cache)) { 265 vbdev->cache.attached = false; 266 } 267 268 vbdev_ocf_mngt_continue(vbdev, 0); 269 } 270 271 static void 272 stop_vbdev_cmpl(ocf_cache_t cache, void *priv, int error) 273 { 274 struct vbdev_ocf *vbdev = priv; 275 276 vbdev_ocf_queue_put(vbdev->cache_ctx->mngt_queue); 277 ocf_mngt_cache_unlock(cache); 278 279 vbdev_ocf_mngt_continue(vbdev, error); 280 } 281 282 /* Try to lock cache, then stop it */ 283 static void 284 stop_vbdev_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error) 285 { 286 struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv; 287 288 if (error) { 289 SPDK_ERRLOG("Error %d, can not lock cache instance %s\n", 290 error, vbdev->name); 291 vbdev_ocf_mngt_continue(vbdev, error); 292 return; 293 } 294 295 ocf_mngt_cache_stop(vbdev->ocf_cache, stop_vbdev_cmpl, vbdev); 296 } 297 298 /* Stop OCF cache object 299 * vbdev_ocf is not operational after this */ 300 static void 301 stop_vbdev(struct vbdev_ocf *vbdev) 302 { 303 if (!is_ocf_cache_running(vbdev)) { 304 vbdev_ocf_mngt_continue(vbdev, 0); 305 return; 306 } 307 308 if (!g_fini_started && get_other_cache_instance(vbdev)) { 309 SPDK_NOTICELOG("Not stopping cache instance '%s'" 310 " because it is referenced by other OCF bdev\n", 311 vbdev->cache.name); 312 vbdev_ocf_mngt_continue(vbdev, 0); 313 return; 314 } 315 316 ocf_mngt_cache_lock(vbdev->ocf_cache, stop_vbdev_cache_lock_cmpl, vbdev); 317 } 318 319 static void 320 flush_vbdev_cmpl(ocf_cache_t cache, void *priv, int error) 321 { 322 struct vbdev_ocf *vbdev = priv; 323 324 ocf_mngt_cache_unlock(cache); 325 vbdev_ocf_mngt_continue(vbdev, error); 326 } 327 328 static void 329 flush_vbdev_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error) 330 { 331 struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv; 332 333 if (error) { 334 SPDK_ERRLOG("Error %d, can not lock cache instance %s\n", 335 error, vbdev->name); 336 vbdev_ocf_mngt_continue(vbdev, error); 337 return; 338 } 339 340 ocf_mngt_cache_flush(vbdev->ocf_cache, flush_vbdev_cmpl, vbdev); 341 } 342 343 static void 344 flush_vbdev(struct vbdev_ocf *vbdev) 345 { 346 if (!is_ocf_cache_running(vbdev)) { 347 vbdev_ocf_mngt_continue(vbdev, -EINVAL); 348 return; 349 } 350 351 ocf_mngt_cache_lock(vbdev->ocf_cache, flush_vbdev_cache_lock_cmpl, vbdev); 352 } 353 354 /* Procedures called during dirty unregister */ 355 vbdev_ocf_mngt_fn unregister_path_dirty[] = { 356 flush_vbdev, 357 stop_vbdev, 358 detach_cache, 359 close_cache_bdev, 360 detach_core, 361 close_core_bdev, 362 unregister_finish, 363 NULL 364 }; 365 366 /* Procedures called during clean unregister */ 367 vbdev_ocf_mngt_fn unregister_path_clean[] = { 368 flush_vbdev, 369 detach_core, 370 close_core_bdev, 371 stop_vbdev, 372 detach_cache, 373 close_cache_bdev, 374 unregister_finish, 375 NULL 376 }; 377 378 /* Start asynchronous management operation using unregister_path */ 379 static void 380 unregister_cb(void *opaque) 381 { 382 struct vbdev_ocf *vbdev = opaque; 383 vbdev_ocf_mngt_fn *unregister_path; 384 int rc; 385 386 unregister_path = vbdev->state.doing_clean_delete ? 387 unregister_path_clean : unregister_path_dirty; 388 389 rc = vbdev_ocf_mngt_start(vbdev, unregister_path, NULL, NULL); 390 if (rc) { 391 SPDK_ERRLOG("Unable to unregister OCF bdev: %d\n", rc); 392 spdk_bdev_destruct_done(&vbdev->exp_bdev, rc); 393 } 394 } 395 396 /* Clean remove case - remove core and then cache, this order 397 * will remove instance permanently */ 398 static void 399 _vbdev_ocf_destruct_clean(struct vbdev_ocf *vbdev) 400 { 401 if (vbdev->core.attached) { 402 detach_core(vbdev); 403 close_core_bdev(vbdev); 404 } 405 406 if (vbdev->cache.attached) { 407 detach_cache(vbdev); 408 close_cache_bdev(vbdev); 409 } 410 } 411 412 /* Dirty shutdown/hot remove case - remove cache and then core, this order 413 * will allow us to recover this instance in the future */ 414 static void 415 _vbdev_ocf_destruct_dirty(struct vbdev_ocf *vbdev) 416 { 417 if (vbdev->cache.attached) { 418 detach_cache(vbdev); 419 close_cache_bdev(vbdev); 420 } 421 422 if (vbdev->core.attached) { 423 detach_core(vbdev); 424 close_core_bdev(vbdev); 425 } 426 } 427 428 /* Unregister io device with callback to unregister_cb 429 * This function is called during spdk_bdev_unregister */ 430 static int 431 vbdev_ocf_destruct(void *opaque) 432 { 433 struct vbdev_ocf *vbdev = opaque; 434 435 if (vbdev->state.doing_finish) { 436 return -EALREADY; 437 } 438 439 if (vbdev->state.starting && !vbdev->state.started) { 440 /* Prevent before detach cache/core during register path of 441 this bdev */ 442 return -EBUSY; 443 } 444 445 vbdev->state.doing_finish = true; 446 447 if (vbdev->state.started) { 448 spdk_io_device_unregister(vbdev, unregister_cb); 449 /* Return 1 because unregister is delayed */ 450 return 1; 451 } 452 453 if (vbdev->state.doing_clean_delete) { 454 _vbdev_ocf_destruct_clean(vbdev); 455 } else { 456 _vbdev_ocf_destruct_dirty(vbdev); 457 } 458 459 return 0; 460 } 461 462 /* Stop OCF cache and unregister SPDK bdev */ 463 int 464 vbdev_ocf_delete(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg) 465 { 466 int rc = 0; 467 468 if (vbdev->state.started) { 469 spdk_bdev_unregister(&vbdev->exp_bdev, cb, cb_arg); 470 } else { 471 rc = vbdev_ocf_destruct(vbdev); 472 if (rc == 0 && cb) { 473 cb(cb_arg, 0); 474 } 475 } 476 477 return rc; 478 } 479 480 /* Remove cores permanently and then stop OCF cache and unregister SPDK bdev */ 481 int 482 vbdev_ocf_delete_clean(struct vbdev_ocf *vbdev, void (*cb)(void *, int), 483 void *cb_arg) 484 { 485 vbdev->state.doing_clean_delete = true; 486 487 return vbdev_ocf_delete(vbdev, cb, cb_arg); 488 } 489 490 491 /* If vbdev is online, return its object */ 492 struct vbdev_ocf * 493 vbdev_ocf_get_by_name(const char *name) 494 { 495 struct vbdev_ocf *vbdev; 496 497 if (name == NULL) { 498 assert(false); 499 return NULL; 500 } 501 502 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 503 if (vbdev->name == NULL || vbdev->state.doing_finish) { 504 continue; 505 } 506 if (strcmp(vbdev->name, name) == 0) { 507 return vbdev; 508 } 509 } 510 return NULL; 511 } 512 513 /* Return matching base if parent vbdev is online */ 514 struct vbdev_ocf_base * 515 vbdev_ocf_get_base_by_name(const char *name) 516 { 517 struct vbdev_ocf *vbdev; 518 519 if (name == NULL) { 520 assert(false); 521 return NULL; 522 } 523 524 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 525 if (vbdev->state.doing_finish) { 526 continue; 527 } 528 529 if (vbdev->cache.name && strcmp(vbdev->cache.name, name) == 0) { 530 return &vbdev->cache; 531 } 532 if (vbdev->core.name && strcmp(vbdev->core.name, name) == 0) { 533 return &vbdev->core; 534 } 535 } 536 return NULL; 537 } 538 539 /* Execute fn for each OCF device that is online or waits for base devices */ 540 void 541 vbdev_ocf_foreach(vbdev_ocf_foreach_fn fn, void *ctx) 542 { 543 struct vbdev_ocf *vbdev; 544 545 assert(fn != NULL); 546 547 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 548 if (!vbdev->state.doing_finish) { 549 fn(vbdev, ctx); 550 } 551 } 552 } 553 554 /* Called from OCF when SPDK_IO is completed */ 555 static void 556 vbdev_ocf_io_submit_cb(struct ocf_io *io, int error) 557 { 558 struct spdk_bdev_io *bdev_io = io->priv1; 559 560 if (error == 0) { 561 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS); 562 } else if (error == -ENOMEM) { 563 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM); 564 } else { 565 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 566 } 567 568 ocf_io_put(io); 569 } 570 571 /* Configure io parameters and send it to OCF */ 572 static int 573 io_submit_to_ocf(struct spdk_bdev_io *bdev_io, struct ocf_io *io) 574 { 575 switch (bdev_io->type) { 576 case SPDK_BDEV_IO_TYPE_WRITE: 577 case SPDK_BDEV_IO_TYPE_READ: 578 ocf_core_submit_io(io); 579 return 0; 580 case SPDK_BDEV_IO_TYPE_FLUSH: 581 ocf_core_submit_flush(io); 582 return 0; 583 case SPDK_BDEV_IO_TYPE_UNMAP: 584 ocf_core_submit_discard(io); 585 return 0; 586 case SPDK_BDEV_IO_TYPE_RESET: 587 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 588 default: 589 SPDK_ERRLOG("Unsupported IO type: %d\n", bdev_io->type); 590 return -EINVAL; 591 } 592 } 593 594 /* Submit SPDK-IO to OCF */ 595 static void 596 io_handle(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 597 { 598 struct vbdev_ocf *vbdev = bdev_io->bdev->ctxt; 599 struct ocf_io *io = NULL; 600 struct bdev_ocf_data *data = NULL; 601 struct vbdev_ocf_qctx *qctx = spdk_io_channel_get_ctx(ch); 602 uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen; 603 uint64_t offset = bdev_io->u.bdev.offset_blocks * bdev_io->bdev->blocklen; 604 int dir, flags = 0; 605 int err; 606 607 switch (bdev_io->type) { 608 case SPDK_BDEV_IO_TYPE_READ: 609 dir = OCF_READ; 610 break; 611 case SPDK_BDEV_IO_TYPE_WRITE: 612 dir = OCF_WRITE; 613 break; 614 case SPDK_BDEV_IO_TYPE_FLUSH: 615 dir = OCF_WRITE; 616 break; 617 case SPDK_BDEV_IO_TYPE_UNMAP: 618 dir = OCF_WRITE; 619 break; 620 default: 621 err = -EINVAL; 622 goto fail; 623 } 624 625 if (bdev_io->type == SPDK_BDEV_IO_TYPE_FLUSH) { 626 flags = OCF_WRITE_FLUSH; 627 } 628 629 io = ocf_core_new_io(vbdev->ocf_core, qctx->queue, offset, len, dir, 0, flags); 630 if (!io) { 631 err = -ENOMEM; 632 goto fail; 633 } 634 635 data = vbdev_ocf_data_from_spdk_io(bdev_io); 636 if (!data) { 637 err = -ENOMEM; 638 goto fail; 639 } 640 641 err = ocf_io_set_data(io, data, 0); 642 if (err) { 643 goto fail; 644 } 645 646 ocf_io_set_cmpl(io, bdev_io, NULL, vbdev_ocf_io_submit_cb); 647 648 err = io_submit_to_ocf(bdev_io, io); 649 if (err) { 650 goto fail; 651 } 652 653 return; 654 655 fail: 656 if (io) { 657 ocf_io_put(io); 658 } 659 660 if (err == -ENOMEM) { 661 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM); 662 } else { 663 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 664 } 665 } 666 667 static void 668 vbdev_ocf_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, 669 bool success) 670 { 671 if (!success) { 672 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 673 return; 674 } 675 676 io_handle(ch, bdev_io); 677 } 678 679 /* Called from bdev layer when an io to Cache vbdev is submitted */ 680 static void 681 vbdev_ocf_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 682 { 683 switch (bdev_io->type) { 684 case SPDK_BDEV_IO_TYPE_READ: 685 /* User does not have to allocate io vectors for the request, 686 * so in case they are not allocated, we allocate them here */ 687 spdk_bdev_io_get_buf(bdev_io, vbdev_ocf_get_buf_cb, 688 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 689 break; 690 case SPDK_BDEV_IO_TYPE_WRITE: 691 case SPDK_BDEV_IO_TYPE_FLUSH: 692 case SPDK_BDEV_IO_TYPE_UNMAP: 693 io_handle(ch, bdev_io); 694 break; 695 case SPDK_BDEV_IO_TYPE_RESET: 696 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 697 default: 698 SPDK_ERRLOG("Unknown I/O type %d\n", bdev_io->type); 699 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 700 break; 701 } 702 } 703 704 /* Called from bdev layer */ 705 static bool 706 vbdev_ocf_io_type_supported(void *opaque, enum spdk_bdev_io_type io_type) 707 { 708 struct vbdev_ocf *vbdev = opaque; 709 710 switch (io_type) { 711 case SPDK_BDEV_IO_TYPE_READ: 712 case SPDK_BDEV_IO_TYPE_WRITE: 713 case SPDK_BDEV_IO_TYPE_FLUSH: 714 case SPDK_BDEV_IO_TYPE_UNMAP: 715 return spdk_bdev_io_type_supported(vbdev->core.bdev, io_type); 716 case SPDK_BDEV_IO_TYPE_RESET: 717 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 718 default: 719 return false; 720 } 721 } 722 723 /* Called from bdev layer */ 724 static struct spdk_io_channel * 725 vbdev_ocf_get_io_channel(void *opaque) 726 { 727 struct vbdev_ocf *bdev = opaque; 728 729 return spdk_get_io_channel(bdev); 730 } 731 732 static int 733 vbdev_ocf_dump_info_json(void *opaque, struct spdk_json_write_ctx *w) 734 { 735 struct vbdev_ocf *vbdev = opaque; 736 737 spdk_json_write_named_string(w, "cache_device", vbdev->cache.name); 738 spdk_json_write_named_string(w, "core_device", vbdev->core.name); 739 740 spdk_json_write_named_string(w, "mode", 741 ocf_get_cache_modename(ocf_cache_get_mode(vbdev->ocf_cache))); 742 spdk_json_write_named_uint32(w, "cache_line_size", 743 ocf_cache_get_line_size(vbdev->ocf_cache)); 744 spdk_json_write_named_bool(w, "metadata_volatile", 745 vbdev->cfg.cache.metadata_volatile); 746 747 return 0; 748 } 749 750 static void 751 vbdev_ocf_write_json_config(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 752 { 753 struct vbdev_ocf *vbdev = bdev->ctxt; 754 755 spdk_json_write_object_begin(w); 756 757 spdk_json_write_named_string(w, "method", "bdev_ocf_create"); 758 759 spdk_json_write_named_object_begin(w, "params"); 760 spdk_json_write_named_string(w, "name", vbdev->name); 761 spdk_json_write_named_string(w, "mode", 762 ocf_get_cache_modename(ocf_cache_get_mode(vbdev->ocf_cache))); 763 spdk_json_write_named_uint32(w, "cache_line_size", 764 ocf_cache_get_line_size(vbdev->ocf_cache)); 765 spdk_json_write_named_string(w, "cache_bdev_name", vbdev->cache.name); 766 spdk_json_write_named_string(w, "core_bdev_name", vbdev->core.name); 767 spdk_json_write_object_end(w); 768 769 spdk_json_write_object_end(w); 770 } 771 772 /* Cache vbdev function table 773 * Used by bdev layer */ 774 static struct spdk_bdev_fn_table cache_dev_fn_table = { 775 .destruct = vbdev_ocf_destruct, 776 .io_type_supported = vbdev_ocf_io_type_supported, 777 .submit_request = vbdev_ocf_submit_request, 778 .get_io_channel = vbdev_ocf_get_io_channel, 779 .write_config_json = vbdev_ocf_write_json_config, 780 .dump_info_json = vbdev_ocf_dump_info_json, 781 }; 782 783 /* Poller function for the OCF queue 784 * We execute OCF requests here synchronously */ 785 static int 786 queue_poll(void *opaque) 787 { 788 struct vbdev_ocf_qctx *qctx = opaque; 789 uint32_t iono = ocf_queue_pending_io(qctx->queue); 790 int i, max = spdk_min(32, iono); 791 792 for (i = 0; i < max; i++) { 793 ocf_queue_run_single(qctx->queue); 794 } 795 796 if (iono > 0) { 797 return SPDK_POLLER_BUSY; 798 } else { 799 return SPDK_POLLER_IDLE; 800 } 801 } 802 803 /* Called during ocf_submit_io, ocf_purge* 804 * and any other requests that need to submit io */ 805 static void 806 vbdev_ocf_ctx_queue_kick(ocf_queue_t q) 807 { 808 } 809 810 /* OCF queue deinitialization 811 * Called at ocf_cache_stop */ 812 static void 813 vbdev_ocf_ctx_queue_stop(ocf_queue_t q) 814 { 815 struct vbdev_ocf_qctx *qctx = ocf_queue_get_priv(q); 816 817 if (qctx) { 818 spdk_put_io_channel(qctx->cache_ch); 819 spdk_put_io_channel(qctx->core_ch); 820 spdk_poller_unregister(&qctx->poller); 821 if (qctx->allocated) { 822 free(qctx); 823 } 824 } 825 } 826 827 /* Queue ops is an interface for running queue thread 828 * stop() operation in called just before queue gets destroyed */ 829 const struct ocf_queue_ops queue_ops = { 830 .kick_sync = vbdev_ocf_ctx_queue_kick, 831 .kick = vbdev_ocf_ctx_queue_kick, 832 .stop = vbdev_ocf_ctx_queue_stop, 833 }; 834 835 /* Called on cache vbdev creation at every thread 836 * We allocate OCF queues here and SPDK poller for it */ 837 static int 838 io_device_create_cb(void *io_device, void *ctx_buf) 839 { 840 struct vbdev_ocf *vbdev = io_device; 841 struct vbdev_ocf_qctx *qctx = ctx_buf; 842 int rc; 843 844 rc = vbdev_ocf_queue_create(vbdev->ocf_cache, &qctx->queue, &queue_ops); 845 if (rc) { 846 return rc; 847 } 848 849 ocf_queue_set_priv(qctx->queue, qctx); 850 851 qctx->vbdev = vbdev; 852 qctx->cache_ch = spdk_bdev_get_io_channel(vbdev->cache.desc); 853 qctx->core_ch = spdk_bdev_get_io_channel(vbdev->core.desc); 854 qctx->poller = SPDK_POLLER_REGISTER(queue_poll, qctx, 0); 855 856 return rc; 857 } 858 859 /* Called per thread 860 * Put OCF queue and relaunch poller with new context to finish pending requests */ 861 static void 862 io_device_destroy_cb(void *io_device, void *ctx_buf) 863 { 864 /* Making a copy of context to use it after io channel will be destroyed */ 865 struct vbdev_ocf_qctx *copy = malloc(sizeof(*copy)); 866 struct vbdev_ocf_qctx *qctx = ctx_buf; 867 868 if (copy) { 869 ocf_queue_set_priv(qctx->queue, copy); 870 memcpy(copy, qctx, sizeof(*copy)); 871 spdk_poller_unregister(&qctx->poller); 872 copy->poller = SPDK_POLLER_REGISTER(queue_poll, copy, 0); 873 copy->allocated = true; 874 } else { 875 SPDK_ERRLOG("Unable to stop OCF queue properly: %s\n", 876 spdk_strerror(ENOMEM)); 877 } 878 879 vbdev_ocf_queue_put(qctx->queue); 880 } 881 882 /* OCF management queue deinitialization */ 883 static void 884 vbdev_ocf_ctx_mngt_queue_stop(ocf_queue_t q) 885 { 886 struct spdk_poller *poller = ocf_queue_get_priv(q); 887 888 if (poller) { 889 spdk_poller_unregister(&poller); 890 } 891 } 892 893 static int 894 mngt_queue_poll(void *opaque) 895 { 896 ocf_queue_t q = opaque; 897 uint32_t iono = ocf_queue_pending_io(q); 898 int i, max = spdk_min(32, iono); 899 900 for (i = 0; i < max; i++) { 901 ocf_queue_run_single(q); 902 } 903 904 if (iono > 0) { 905 return SPDK_POLLER_BUSY; 906 } else { 907 return SPDK_POLLER_IDLE; 908 } 909 } 910 911 static void 912 vbdev_ocf_ctx_mngt_queue_kick(ocf_queue_t q) 913 { 914 } 915 916 /* Queue ops is an interface for running queue thread 917 * stop() operation in called just before queue gets destroyed */ 918 const struct ocf_queue_ops mngt_queue_ops = { 919 .kick_sync = NULL, 920 .kick = vbdev_ocf_ctx_mngt_queue_kick, 921 .stop = vbdev_ocf_ctx_mngt_queue_stop, 922 }; 923 924 static void 925 vbdev_ocf_mngt_exit(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *rollback_path, int rc) 926 { 927 vbdev->state.starting = false; 928 vbdev_ocf_mngt_stop(vbdev, rollback_path, rc); 929 } 930 931 /* Create exported spdk object */ 932 static void 933 finish_register(struct vbdev_ocf *vbdev) 934 { 935 int result; 936 937 /* Copy properties of the base bdev */ 938 vbdev->exp_bdev.blocklen = vbdev->core.bdev->blocklen; 939 vbdev->exp_bdev.write_cache = vbdev->core.bdev->write_cache; 940 vbdev->exp_bdev.required_alignment = vbdev->core.bdev->required_alignment; 941 942 vbdev->exp_bdev.name = vbdev->name; 943 vbdev->exp_bdev.product_name = "SPDK OCF"; 944 945 vbdev->exp_bdev.blockcnt = vbdev->core.bdev->blockcnt; 946 vbdev->exp_bdev.ctxt = vbdev; 947 vbdev->exp_bdev.fn_table = &cache_dev_fn_table; 948 vbdev->exp_bdev.module = &ocf_if; 949 950 /* Finally register vbdev in SPDK */ 951 spdk_io_device_register(vbdev, io_device_create_cb, io_device_destroy_cb, 952 sizeof(struct vbdev_ocf_qctx), vbdev->name); 953 result = spdk_bdev_register(&vbdev->exp_bdev); 954 if (result) { 955 SPDK_ERRLOG("Could not register exposed bdev %s\n", 956 vbdev->name); 957 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, result); 958 return; 959 } else { 960 vbdev->state.started = true; 961 } 962 963 vbdev_ocf_mngt_continue(vbdev, result); 964 } 965 966 static void 967 add_core_cmpl(ocf_cache_t cache, ocf_core_t core, void *priv, int error) 968 { 969 struct vbdev_ocf *vbdev = priv; 970 971 ocf_mngt_cache_unlock(cache); 972 973 if (error) { 974 SPDK_ERRLOG("Error %d, failed to add core device to cache instance %s," 975 "starting rollback\n", error, vbdev->name); 976 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, error); 977 return; 978 } else { 979 vbdev->ocf_core = core; 980 } 981 982 vbdev_ocf_mngt_continue(vbdev, error); 983 } 984 985 /* Try to lock cache, then add core */ 986 static void 987 add_core_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error) 988 { 989 struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv; 990 991 if (error) { 992 SPDK_ERRLOG("Error %d, can not lock cache instance %s," 993 "starting rollback\n", error, vbdev->name); 994 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, error); 995 } 996 ocf_mngt_cache_add_core(vbdev->ocf_cache, &vbdev->cfg.core, add_core_cmpl, vbdev); 997 } 998 999 /* Add core for existing OCF cache instance */ 1000 static void 1001 add_core(struct vbdev_ocf *vbdev) 1002 { 1003 ocf_mngt_cache_lock(vbdev->ocf_cache, add_core_cache_lock_cmpl, vbdev); 1004 } 1005 1006 static void 1007 start_cache_cmpl(ocf_cache_t cache, void *priv, int error) 1008 { 1009 struct vbdev_ocf *vbdev = priv; 1010 1011 ocf_mngt_cache_unlock(cache); 1012 1013 if (error) { 1014 SPDK_ERRLOG("Error %d during start cache %s, starting rollback\n", 1015 error, vbdev->name); 1016 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, error); 1017 return; 1018 } 1019 1020 vbdev_ocf_mngt_continue(vbdev, error); 1021 } 1022 1023 static int 1024 create_management_queue(struct vbdev_ocf *vbdev) 1025 { 1026 struct spdk_poller *mngt_poller; 1027 int rc; 1028 1029 rc = vbdev_ocf_queue_create(vbdev->ocf_cache, &vbdev->cache_ctx->mngt_queue, &mngt_queue_ops); 1030 if (rc) { 1031 SPDK_ERRLOG("Unable to create mngt_queue: %d\n", rc); 1032 return rc; 1033 } 1034 1035 mngt_poller = SPDK_POLLER_REGISTER(mngt_queue_poll, vbdev->cache_ctx->mngt_queue, 100); 1036 if (mngt_poller == NULL) { 1037 SPDK_ERRLOG("Unable to initiate mngt request: %s", spdk_strerror(ENOMEM)); 1038 return -ENOMEM; 1039 } 1040 1041 ocf_queue_set_priv(vbdev->cache_ctx->mngt_queue, mngt_poller); 1042 ocf_mngt_cache_set_mngt_queue(vbdev->ocf_cache, vbdev->cache_ctx->mngt_queue); 1043 1044 return 0; 1045 } 1046 1047 /* Start OCF cache, attach caching device */ 1048 static void 1049 start_cache(struct vbdev_ocf *vbdev) 1050 { 1051 ocf_cache_t existing; 1052 int rc; 1053 1054 if (is_ocf_cache_running(vbdev)) { 1055 vbdev_ocf_mngt_stop(vbdev, NULL, -EALREADY); 1056 return; 1057 } 1058 1059 existing = get_other_cache_instance(vbdev); 1060 if (existing) { 1061 SPDK_NOTICELOG("OCF bdev %s connects to existing cache device %s\n", 1062 vbdev->name, vbdev->cache.name); 1063 vbdev->ocf_cache = existing; 1064 ocf_mngt_cache_get(vbdev->ocf_cache); 1065 vbdev->cache_ctx = ocf_cache_get_priv(existing); 1066 vbdev_ocf_cache_ctx_get(vbdev->cache_ctx); 1067 vbdev_ocf_mngt_continue(vbdev, 0); 1068 return; 1069 } 1070 1071 vbdev->cache_ctx = calloc(1, sizeof(struct vbdev_ocf_cache_ctx)); 1072 if (vbdev->cache_ctx == NULL) { 1073 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, -ENOMEM); 1074 return; 1075 } 1076 1077 vbdev_ocf_cache_ctx_get(vbdev->cache_ctx); 1078 pthread_mutex_init(&vbdev->cache_ctx->lock, NULL); 1079 1080 rc = ocf_mngt_cache_start(vbdev_ocf_ctx, &vbdev->ocf_cache, &vbdev->cfg.cache); 1081 if (rc) { 1082 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, rc); 1083 return; 1084 } 1085 ocf_mngt_cache_get(vbdev->ocf_cache); 1086 1087 ocf_cache_set_priv(vbdev->ocf_cache, vbdev->cache_ctx); 1088 1089 rc = create_management_queue(vbdev); 1090 if (rc) { 1091 SPDK_ERRLOG("Unable to create mngt_queue: %d\n", rc); 1092 vbdev_ocf_mngt_exit(vbdev, unregister_path_dirty, rc); 1093 return; 1094 } 1095 1096 if (vbdev->cfg.loadq) { 1097 ocf_mngt_cache_load(vbdev->ocf_cache, &vbdev->cfg.device, start_cache_cmpl, vbdev); 1098 } else { 1099 ocf_mngt_cache_attach(vbdev->ocf_cache, &vbdev->cfg.device, start_cache_cmpl, vbdev); 1100 } 1101 } 1102 1103 /* Procedures called during register operation */ 1104 vbdev_ocf_mngt_fn register_path[] = { 1105 start_cache, 1106 add_core, 1107 finish_register, 1108 NULL 1109 }; 1110 1111 /* Start cache instance and register OCF bdev */ 1112 static void 1113 register_vbdev(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_callback cb, void *cb_arg) 1114 { 1115 int rc; 1116 1117 if (!(vbdev->core.attached && vbdev->cache.attached) || vbdev->state.started) { 1118 cb(-EPERM, vbdev, cb_arg); 1119 return; 1120 } 1121 1122 vbdev->state.starting = true; 1123 rc = vbdev_ocf_mngt_start(vbdev, register_path, cb, cb_arg); 1124 if (rc) { 1125 cb(rc, vbdev, cb_arg); 1126 } 1127 } 1128 1129 /* Init OCF configuration options 1130 * for core and cache devices */ 1131 static void 1132 init_vbdev_config(struct vbdev_ocf *vbdev) 1133 { 1134 struct vbdev_ocf_config *cfg = &vbdev->cfg; 1135 1136 snprintf(cfg->cache.name, sizeof(cfg->cache.name), "%s", vbdev->name); 1137 snprintf(cfg->core.name, sizeof(cfg->core.name), "%s", vbdev->core.name); 1138 1139 /* TODO [metadata]: make configurable with persistent 1140 * metadata support */ 1141 cfg->cache.metadata_volatile = false; 1142 1143 /* This are suggested values that 1144 * should be sufficient for most use cases */ 1145 cfg->cache.backfill.max_queue_size = 65536; 1146 cfg->cache.backfill.queue_unblock_size = 60000; 1147 1148 cfg->device.perform_test = false; 1149 cfg->device.discard_on_start = false; 1150 1151 vbdev->cfg.cache.locked = true; 1152 1153 cfg->core.volume_type = SPDK_OBJECT; 1154 cfg->device.volume_type = SPDK_OBJECT; 1155 1156 if (vbdev->cfg.loadq) { 1157 /* When doing cache_load(), we need to set try_add to true, 1158 * otherwise OCF will interpret this core as new 1159 * instead of the inactive one */ 1160 vbdev->cfg.core.try_add = true; 1161 } else { 1162 /* When cache is initialized as new, set force flag to true, 1163 * to ignore warnings about existing metadata */ 1164 cfg->device.force = true; 1165 } 1166 1167 /* Serialize bdev names in OCF UUID to interpret on future loads 1168 * Core UUID is a triple of (core name, vbdev name, cache name) 1169 * Cache UUID is cache bdev name */ 1170 cfg->device.uuid.size = strlen(vbdev->cache.name) + 1; 1171 cfg->device.uuid.data = vbdev->cache.name; 1172 1173 snprintf(vbdev->uuid, VBDEV_OCF_MD_MAX_LEN, "%s %s %s", 1174 vbdev->core.name, vbdev->name, vbdev->cache.name); 1175 cfg->core.uuid.size = strlen(vbdev->uuid) + 1; 1176 cfg->core.uuid.data = vbdev->uuid; 1177 vbdev->uuid[strlen(vbdev->core.name)] = 0; 1178 vbdev->uuid[strlen(vbdev->core.name) + 1 + strlen(vbdev->name)] = 0; 1179 } 1180 1181 /* Allocate vbdev structure object and add it to the global list */ 1182 static int 1183 init_vbdev(const char *vbdev_name, 1184 const char *cache_mode_name, 1185 const uint64_t cache_line_size, 1186 const char *cache_name, 1187 const char *core_name, 1188 bool loadq) 1189 { 1190 struct vbdev_ocf *vbdev; 1191 int rc = 0; 1192 1193 if (spdk_bdev_get_by_name(vbdev_name) || vbdev_ocf_get_by_name(vbdev_name)) { 1194 SPDK_ERRLOG("Device with name '%s' already exists\n", vbdev_name); 1195 return -EPERM; 1196 } 1197 1198 vbdev = calloc(1, sizeof(*vbdev)); 1199 if (!vbdev) { 1200 goto error_mem; 1201 } 1202 1203 vbdev->cache.parent = vbdev; 1204 vbdev->core.parent = vbdev; 1205 vbdev->cache.is_cache = true; 1206 vbdev->core.is_cache = false; 1207 1208 if (cache_mode_name) { 1209 vbdev->cfg.cache.cache_mode 1210 = ocf_get_cache_mode(cache_mode_name); 1211 } else if (!loadq) { /* In load path it is OK to pass NULL as cache mode */ 1212 SPDK_ERRLOG("No cache mode specified\n"); 1213 rc = -EINVAL; 1214 goto error_free; 1215 } 1216 if (vbdev->cfg.cache.cache_mode < 0) { 1217 SPDK_ERRLOG("Incorrect cache mode '%s'\n", cache_mode_name); 1218 rc = -EINVAL; 1219 goto error_free; 1220 } 1221 1222 ocf_cache_line_size_t set_cache_line_size = cache_line_size ? 1223 (ocf_cache_line_size_t)cache_line_size * KiB : 1224 ocf_cache_line_size_default; 1225 if (set_cache_line_size == 0) { 1226 SPDK_ERRLOG("Cache line size should be non-zero.\n"); 1227 rc = -EINVAL; 1228 goto error_free; 1229 } 1230 vbdev->cfg.device.cache_line_size = set_cache_line_size; 1231 vbdev->cfg.cache.cache_line_size = set_cache_line_size; 1232 1233 vbdev->name = strdup(vbdev_name); 1234 if (!vbdev->name) { 1235 goto error_mem; 1236 } 1237 1238 vbdev->cache.name = strdup(cache_name); 1239 if (!vbdev->cache.name) { 1240 goto error_mem; 1241 } 1242 1243 vbdev->core.name = strdup(core_name); 1244 if (!vbdev->core.name) { 1245 goto error_mem; 1246 } 1247 1248 vbdev->cfg.loadq = loadq; 1249 init_vbdev_config(vbdev); 1250 TAILQ_INSERT_TAIL(&g_ocf_vbdev_head, vbdev, tailq); 1251 return rc; 1252 1253 error_mem: 1254 rc = -ENOMEM; 1255 error_free: 1256 free_vbdev(vbdev); 1257 return rc; 1258 } 1259 1260 /* Read configuration file at the start of SPDK application 1261 * This adds vbdevs to global list if some mentioned in config */ 1262 static int 1263 vbdev_ocf_init(void) 1264 { 1265 int status; 1266 1267 status = vbdev_ocf_ctx_init(); 1268 if (status) { 1269 SPDK_ERRLOG("OCF ctx initialization failed with=%d\n", status); 1270 return status; 1271 } 1272 1273 status = vbdev_ocf_volume_init(); 1274 if (status) { 1275 vbdev_ocf_ctx_cleanup(); 1276 SPDK_ERRLOG("OCF volume initialization failed with=%d\n", status); 1277 return status; 1278 } 1279 1280 return status; 1281 } 1282 1283 /* Called after application shutdown started 1284 * Release memory of allocated structures here */ 1285 static void 1286 vbdev_ocf_module_fini(void) 1287 { 1288 struct vbdev_ocf *vbdev; 1289 1290 while ((vbdev = TAILQ_FIRST(&g_ocf_vbdev_head))) { 1291 TAILQ_REMOVE(&g_ocf_vbdev_head, vbdev, tailq); 1292 free_vbdev(vbdev); 1293 } 1294 1295 vbdev_ocf_volume_cleanup(); 1296 vbdev_ocf_ctx_cleanup(); 1297 } 1298 1299 /* When base device gets unpluged this is called 1300 * We will unregister cache vbdev here 1301 * When cache device is removed, we delete every OCF bdev that used it */ 1302 static void 1303 hotremove_cb(struct vbdev_ocf_base *base) 1304 { 1305 struct vbdev_ocf *vbdev; 1306 1307 if (!base->is_cache) { 1308 if (base->parent->state.doing_finish) { 1309 return; 1310 } 1311 1312 SPDK_NOTICELOG("Deinitializing '%s' because its core device '%s' was removed\n", 1313 base->parent->name, base->name); 1314 vbdev_ocf_delete(base->parent, NULL, NULL); 1315 return; 1316 } 1317 1318 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 1319 if (vbdev->state.doing_finish) { 1320 continue; 1321 } 1322 if (strcmp(base->name, vbdev->cache.name) == 0) { 1323 SPDK_NOTICELOG("Deinitializing '%s' because" 1324 " its cache device '%s' was removed\n", 1325 vbdev->name, base->name); 1326 vbdev_ocf_delete(vbdev, NULL, NULL); 1327 } 1328 } 1329 } 1330 1331 static void 1332 base_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 1333 void *event_ctx) 1334 { 1335 switch (type) { 1336 case SPDK_BDEV_EVENT_REMOVE: 1337 if (event_ctx) { 1338 hotremove_cb(event_ctx); 1339 } 1340 break; 1341 default: 1342 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 1343 break; 1344 } 1345 } 1346 1347 /* Open base SPDK bdev and claim it */ 1348 static int 1349 attach_base(struct vbdev_ocf_base *base) 1350 { 1351 int status; 1352 1353 if (base->attached) { 1354 return -EALREADY; 1355 } 1356 1357 /* If base cache bdev was already opened by other vbdev, 1358 * we just copy its descriptor here */ 1359 if (base->is_cache) { 1360 struct vbdev_ocf_base *existing = get_other_cache_base(base); 1361 if (existing) { 1362 base->desc = existing->desc; 1363 base->management_channel = existing->management_channel; 1364 base->attached = true; 1365 return 0; 1366 } 1367 } 1368 1369 status = spdk_bdev_open_ext(base->name, true, base_bdev_event_cb, base, &base->desc); 1370 if (status) { 1371 SPDK_ERRLOG("Unable to open device '%s' for writing\n", base->name); 1372 return status; 1373 } 1374 1375 status = spdk_bdev_module_claim_bdev(base->bdev, base->desc, 1376 &ocf_if); 1377 if (status) { 1378 SPDK_ERRLOG("Unable to claim device '%s'\n", base->name); 1379 spdk_bdev_close(base->desc); 1380 return status; 1381 } 1382 1383 base->management_channel = spdk_bdev_get_io_channel(base->desc); 1384 if (!base->management_channel) { 1385 SPDK_ERRLOG("Unable to get io channel '%s'\n", base->name); 1386 spdk_bdev_module_release_bdev(base->bdev); 1387 spdk_bdev_close(base->desc); 1388 return -ENOMEM; 1389 } 1390 1391 /* Save the thread where the base device is opened */ 1392 base->thread = spdk_get_thread(); 1393 1394 base->attached = true; 1395 return status; 1396 } 1397 1398 /* Attach base bdevs */ 1399 static int 1400 attach_base_bdevs(struct vbdev_ocf *vbdev, 1401 struct spdk_bdev *cache_bdev, 1402 struct spdk_bdev *core_bdev) 1403 { 1404 int rc = 0; 1405 1406 if (cache_bdev) { 1407 vbdev->cache.bdev = cache_bdev; 1408 rc |= attach_base(&vbdev->cache); 1409 } 1410 1411 if (core_bdev) { 1412 vbdev->core.bdev = core_bdev; 1413 rc |= attach_base(&vbdev->core); 1414 } 1415 1416 return rc; 1417 } 1418 1419 /* Init and then start vbdev if all base devices are present */ 1420 void 1421 vbdev_ocf_construct(const char *vbdev_name, 1422 const char *cache_mode_name, 1423 const uint64_t cache_line_size, 1424 const char *cache_name, 1425 const char *core_name, 1426 bool loadq, 1427 void (*cb)(int, struct vbdev_ocf *, void *), 1428 void *cb_arg) 1429 { 1430 int rc; 1431 struct spdk_bdev *cache_bdev = spdk_bdev_get_by_name(cache_name); 1432 struct spdk_bdev *core_bdev = spdk_bdev_get_by_name(core_name); 1433 struct vbdev_ocf *vbdev; 1434 1435 rc = init_vbdev(vbdev_name, cache_mode_name, cache_line_size, cache_name, core_name, loadq); 1436 if (rc) { 1437 cb(rc, NULL, cb_arg); 1438 return; 1439 } 1440 1441 vbdev = vbdev_ocf_get_by_name(vbdev_name); 1442 if (vbdev == NULL) { 1443 cb(-ENODEV, NULL, cb_arg); 1444 return; 1445 } 1446 1447 if (cache_bdev == NULL) { 1448 SPDK_NOTICELOG("OCF bdev '%s' is waiting for cache device '%s' to connect\n", 1449 vbdev->name, cache_name); 1450 } 1451 if (core_bdev == NULL) { 1452 SPDK_NOTICELOG("OCF bdev '%s' is waiting for core device '%s' to connect\n", 1453 vbdev->name, core_name); 1454 } 1455 1456 rc = attach_base_bdevs(vbdev, cache_bdev, core_bdev); 1457 if (rc) { 1458 cb(rc, vbdev, cb_arg); 1459 return; 1460 } 1461 1462 if (core_bdev && cache_bdev) { 1463 register_vbdev(vbdev, cb, cb_arg); 1464 } else { 1465 cb(0, vbdev, cb_arg); 1466 } 1467 } 1468 1469 /* This called if new device is created in SPDK application 1470 * If that device named as one of base bdevs of OCF vbdev, 1471 * claim and open them */ 1472 static void 1473 vbdev_ocf_examine(struct spdk_bdev *bdev) 1474 { 1475 const char *bdev_name = spdk_bdev_get_name(bdev); 1476 struct vbdev_ocf *vbdev; 1477 1478 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 1479 if (vbdev->state.doing_finish) { 1480 continue; 1481 } 1482 1483 if (!strcmp(bdev_name, vbdev->cache.name)) { 1484 attach_base_bdevs(vbdev, bdev, NULL); 1485 continue; 1486 } 1487 if (!strcmp(bdev_name, vbdev->core.name)) { 1488 attach_base_bdevs(vbdev, NULL, bdev); 1489 break; 1490 } 1491 } 1492 spdk_bdev_module_examine_done(&ocf_if); 1493 } 1494 1495 struct metadata_probe_ctx { 1496 struct vbdev_ocf_base base; 1497 ocf_volume_t volume; 1498 1499 struct ocf_volume_uuid *core_uuids; 1500 unsigned int uuid_count; 1501 1502 int result; 1503 int refcnt; 1504 }; 1505 1506 static void 1507 _examine_ctx_put(void *ctx) 1508 { 1509 struct spdk_bdev_desc *desc = ctx; 1510 1511 spdk_bdev_close(desc); 1512 } 1513 1514 static void 1515 examine_ctx_put(struct metadata_probe_ctx *ctx) 1516 { 1517 unsigned int i; 1518 1519 ctx->refcnt--; 1520 if (ctx->refcnt > 0) { 1521 return; 1522 } 1523 1524 if (ctx->result) { 1525 SPDK_ERRLOG("OCF metadata probe for bdev '%s' failed with %d\n", 1526 spdk_bdev_get_name(ctx->base.bdev), ctx->result); 1527 } 1528 1529 if (ctx->base.desc) { 1530 /* Close the underlying bdev on its same opened thread. */ 1531 if (ctx->base.thread && ctx->base.thread != spdk_get_thread()) { 1532 spdk_thread_send_msg(ctx->base.thread, _examine_ctx_put, ctx->base.desc); 1533 } else { 1534 spdk_bdev_close(ctx->base.desc); 1535 } 1536 } 1537 1538 if (ctx->volume) { 1539 ocf_volume_destroy(ctx->volume); 1540 } 1541 1542 if (ctx->core_uuids) { 1543 for (i = 0; i < ctx->uuid_count; i++) { 1544 free(ctx->core_uuids[i].data); 1545 } 1546 } 1547 free(ctx->core_uuids); 1548 1549 examine_done(ctx->result, NULL, ctx->base.bdev); 1550 free(ctx); 1551 } 1552 1553 static void 1554 metadata_probe_construct_cb(int rc, struct vbdev_ocf *vbdev, void *vctx) 1555 { 1556 struct metadata_probe_ctx *ctx = vctx; 1557 1558 examine_ctx_put(ctx); 1559 } 1560 1561 /* This is second callback for ocf_metadata_probe_cores() 1562 * Here we create vbdev configurations based on UUIDs */ 1563 static void 1564 metadata_probe_cores_construct(void *priv, int error, unsigned int num_cores) 1565 { 1566 struct metadata_probe_ctx *ctx = priv; 1567 const char *vbdev_name; 1568 const char *core_name; 1569 const char *cache_name; 1570 unsigned int i; 1571 1572 if (error) { 1573 ctx->result = error; 1574 examine_ctx_put(ctx); 1575 return; 1576 } 1577 1578 for (i = 0; i < num_cores; i++) { 1579 core_name = ocf_uuid_to_str(&ctx->core_uuids[i]); 1580 vbdev_name = core_name + strlen(core_name) + 1; 1581 cache_name = vbdev_name + strlen(vbdev_name) + 1; 1582 1583 if (strcmp(ctx->base.bdev->name, cache_name)) { 1584 SPDK_NOTICELOG("OCF metadata found on %s belongs to bdev named '%s'\n", 1585 ctx->base.bdev->name, cache_name); 1586 } 1587 1588 ctx->refcnt++; 1589 vbdev_ocf_construct(vbdev_name, NULL, 0, cache_name, core_name, true, 1590 metadata_probe_construct_cb, ctx); 1591 } 1592 1593 examine_ctx_put(ctx); 1594 } 1595 1596 /* This callback is called after OCF reads cores UUIDs from cache metadata 1597 * Here we allocate memory for those UUIDs and call ocf_metadata_probe_cores() again */ 1598 static void 1599 metadata_probe_cores_get_num(void *priv, int error, unsigned int num_cores) 1600 { 1601 struct metadata_probe_ctx *ctx = priv; 1602 unsigned int i; 1603 1604 if (error) { 1605 ctx->result = error; 1606 examine_ctx_put(ctx); 1607 return; 1608 } 1609 1610 ctx->uuid_count = num_cores; 1611 ctx->core_uuids = calloc(num_cores, sizeof(struct ocf_volume_uuid)); 1612 if (!ctx->core_uuids) { 1613 ctx->result = -ENOMEM; 1614 examine_ctx_put(ctx); 1615 return; 1616 } 1617 1618 for (i = 0; i < ctx->uuid_count; i++) { 1619 ctx->core_uuids[i].size = OCF_VOLUME_UUID_MAX_SIZE; 1620 ctx->core_uuids[i].data = malloc(OCF_VOLUME_UUID_MAX_SIZE); 1621 if (!ctx->core_uuids[i].data) { 1622 ctx->result = -ENOMEM; 1623 examine_ctx_put(ctx); 1624 return; 1625 } 1626 } 1627 1628 ocf_metadata_probe_cores(vbdev_ocf_ctx, ctx->volume, ctx->core_uuids, ctx->uuid_count, 1629 metadata_probe_cores_construct, ctx); 1630 } 1631 1632 static void 1633 metadata_probe_cb(void *priv, int rc, 1634 struct ocf_metadata_probe_status *status) 1635 { 1636 struct metadata_probe_ctx *ctx = priv; 1637 1638 if (rc) { 1639 /* -ENODATA means device does not have cache metadata on it */ 1640 if (rc != -OCF_ERR_NO_METADATA) { 1641 ctx->result = rc; 1642 } 1643 examine_ctx_put(ctx); 1644 return; 1645 } 1646 1647 ocf_metadata_probe_cores(vbdev_ocf_ctx, ctx->volume, NULL, 0, 1648 metadata_probe_cores_get_num, ctx); 1649 } 1650 1651 /* This is called after vbdev_ocf_examine 1652 * It allows to delay application initialization 1653 * until all OCF bdevs get registered 1654 * If vbdev has all of its base devices it starts asynchronously here 1655 * We first check if bdev appears in configuration, 1656 * if not we do metadata_probe() to create its configuration from bdev metadata */ 1657 static void 1658 vbdev_ocf_examine_disk(struct spdk_bdev *bdev) 1659 { 1660 const char *bdev_name = spdk_bdev_get_name(bdev); 1661 struct vbdev_ocf *vbdev; 1662 struct metadata_probe_ctx *ctx; 1663 bool created_from_config = false; 1664 int rc; 1665 1666 examine_start(bdev); 1667 1668 TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) { 1669 if (vbdev->state.doing_finish || vbdev->state.started) { 1670 continue; 1671 } 1672 1673 if (!strcmp(bdev_name, vbdev->cache.name)) { 1674 examine_start(bdev); 1675 register_vbdev(vbdev, examine_done, bdev); 1676 created_from_config = true; 1677 continue; 1678 } 1679 if (!strcmp(bdev_name, vbdev->core.name)) { 1680 examine_start(bdev); 1681 register_vbdev(vbdev, examine_done, bdev); 1682 examine_done(0, NULL, bdev); 1683 return; 1684 } 1685 } 1686 1687 /* If devices is discovered during config we do not check for metadata */ 1688 if (created_from_config) { 1689 examine_done(0, NULL, bdev); 1690 return; 1691 } 1692 1693 /* Metadata probe path 1694 * We create temporary OCF volume and a temporary base structure 1695 * to use them for ocf_metadata_probe() and for bottom adapter IOs 1696 * Then we get UUIDs of core devices an create configurations based on them */ 1697 ctx = calloc(1, sizeof(*ctx)); 1698 if (!ctx) { 1699 examine_done(-ENOMEM, NULL, bdev); 1700 return; 1701 } 1702 1703 ctx->base.bdev = bdev; 1704 ctx->refcnt = 1; 1705 1706 rc = spdk_bdev_open_ext(bdev_name, true, base_bdev_event_cb, NULL, &ctx->base.desc); 1707 if (rc) { 1708 ctx->result = rc; 1709 examine_ctx_put(ctx); 1710 return; 1711 } 1712 1713 rc = ocf_ctx_volume_create(vbdev_ocf_ctx, &ctx->volume, NULL, SPDK_OBJECT); 1714 if (rc) { 1715 ctx->result = rc; 1716 examine_ctx_put(ctx); 1717 return; 1718 } 1719 1720 rc = ocf_volume_open(ctx->volume, &ctx->base); 1721 if (rc) { 1722 ctx->result = rc; 1723 examine_ctx_put(ctx); 1724 return; 1725 } 1726 1727 /* Save the thread where the base device is opened */ 1728 ctx->base.thread = spdk_get_thread(); 1729 1730 ocf_metadata_probe(vbdev_ocf_ctx, ctx->volume, metadata_probe_cb, ctx); 1731 } 1732 1733 static int 1734 vbdev_ocf_get_ctx_size(void) 1735 { 1736 return sizeof(struct bdev_ocf_data); 1737 } 1738 1739 static void 1740 fini_start(void) 1741 { 1742 g_fini_started = true; 1743 } 1744 1745 /* Module-global function table 1746 * Does not relate to vbdev instances */ 1747 static struct spdk_bdev_module ocf_if = { 1748 .name = "ocf", 1749 .module_init = vbdev_ocf_init, 1750 .fini_start = fini_start, 1751 .module_fini = vbdev_ocf_module_fini, 1752 .get_ctx_size = vbdev_ocf_get_ctx_size, 1753 .examine_config = vbdev_ocf_examine, 1754 .examine_disk = vbdev_ocf_examine_disk, 1755 }; 1756 SPDK_BDEV_MODULE_REGISTER(ocf, &ocf_if); 1757