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