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