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