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 "spdk_cunit.h" 35 #include "spdk/string.h" 36 37 #include "bdev/lvol/vbdev_lvol.c" 38 39 #define SPDK_BS_PAGE_SIZE 0x1000 40 41 int g_lvolerrno; 42 int g_lvserrno; 43 int g_cluster_size; 44 int g_registered_bdevs; 45 int g_num_lvols = 0; 46 struct spdk_lvol_store *g_lvs = NULL; 47 struct spdk_lvol *g_lvol = NULL; 48 struct lvol_store_bdev *g_lvs_bdev = NULL; 49 struct spdk_bdev *g_base_bdev = NULL; 50 struct spdk_bdev_io *g_io = NULL; 51 struct spdk_io_channel *g_ch = NULL; 52 struct lvol_task *g_task = NULL; 53 54 static struct spdk_bdev g_bdev = {}; 55 static struct spdk_lvol_store *g_lvol_store = NULL; 56 bool lvol_store_initialize_fail = false; 57 bool lvol_store_initialize_cb_fail = false; 58 bool lvol_already_opened = false; 59 bool g_examine_done = false; 60 bool g_bdev_alias_already_exists = false; 61 bool g_lvs_with_name_already_exists = false; 62 bool g_lvol_deletable = true; 63 64 int 65 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias) 66 { 67 struct spdk_bdev_alias *tmp; 68 69 CU_ASSERT(alias != NULL); 70 CU_ASSERT(bdev != NULL); 71 if (g_bdev_alias_already_exists) { 72 return -EEXIST; 73 } 74 75 tmp = calloc(1, sizeof(*tmp)); 76 SPDK_CU_ASSERT_FATAL(tmp != NULL); 77 78 tmp->alias = strdup(alias); 79 SPDK_CU_ASSERT_FATAL(tmp->alias != NULL); 80 81 TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq); 82 83 return 0; 84 } 85 86 int 87 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias) 88 { 89 struct spdk_bdev_alias *tmp; 90 91 CU_ASSERT(alias != NULL); 92 CU_ASSERT(bdev != NULL); 93 94 TAILQ_FOREACH(tmp, &bdev->aliases, tailq) { 95 if (strncmp(alias, tmp->alias, SPDK_LVOL_NAME_MAX) == 0) { 96 TAILQ_REMOVE(&bdev->aliases, tmp, tailq); 97 free(tmp->alias); 98 free(tmp); 99 return 0; 100 } 101 } 102 103 return -ENOENT; 104 } 105 106 void 107 spdk_bdev_alias_del_all(struct spdk_bdev *bdev) 108 { 109 struct spdk_bdev_alias *p, *tmp; 110 111 TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) { 112 TAILQ_REMOVE(&bdev->aliases, p, tailq); 113 free(p->alias); 114 free(p); 115 } 116 } 117 118 void 119 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno) 120 { 121 } 122 123 void 124 spdk_lvs_rename(struct spdk_lvol_store *lvs, const char *new_name, 125 spdk_lvs_op_complete cb_fn, void *cb_arg) 126 { 127 if (g_lvs_with_name_already_exists) { 128 g_lvolerrno = -EEXIST; 129 } else { 130 snprintf(lvs->name, sizeof(lvs->name), "%s", new_name); 131 g_lvolerrno = 0; 132 } 133 134 cb_fn(cb_arg, g_lvolerrno); 135 } 136 137 void 138 spdk_lvol_rename(struct spdk_lvol *lvol, const char *new_name, 139 spdk_lvol_op_complete cb_fn, void *cb_arg) 140 { 141 struct spdk_lvol *tmp; 142 143 if (strncmp(lvol->name, new_name, SPDK_LVOL_NAME_MAX) == 0) { 144 cb_fn(cb_arg, 0); 145 return; 146 } 147 148 TAILQ_FOREACH(tmp, &lvol->lvol_store->lvols, link) { 149 if (strncmp(tmp->name, new_name, SPDK_LVOL_NAME_MAX) == 0) { 150 SPDK_ERRLOG("Lvol %s already exists in lvol store %s\n", new_name, lvol->lvol_store->name); 151 cb_fn(cb_arg, -EEXIST); 152 return; 153 } 154 } 155 156 snprintf(lvol->name, sizeof(lvol->name), "%s", new_name); 157 158 cb_fn(cb_arg, g_lvolerrno); 159 } 160 161 void 162 spdk_lvol_open(struct spdk_lvol *lvol, spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg) 163 { 164 cb_fn(cb_arg, lvol, g_lvolerrno); 165 } 166 167 uint64_t 168 spdk_blob_get_num_clusters(struct spdk_blob *b) 169 { 170 return 0; 171 } 172 173 int 174 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids, 175 size_t *count) 176 { 177 *count = 0; 178 return 0; 179 } 180 181 spdk_blob_id 182 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid) 183 { 184 return 0; 185 } 186 187 bool g_blob_is_read_only = false; 188 189 bool 190 spdk_blob_is_read_only(struct spdk_blob *blob) 191 { 192 return g_blob_is_read_only; 193 } 194 195 bool 196 spdk_blob_is_snapshot(struct spdk_blob *blob) 197 { 198 return false; 199 } 200 201 bool 202 spdk_blob_is_clone(struct spdk_blob *blob) 203 { 204 return false; 205 } 206 207 bool 208 spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 209 { 210 return false; 211 } 212 213 static struct spdk_lvol *_lvol_create(struct spdk_lvol_store *lvs); 214 215 void 216 spdk_lvs_load(struct spdk_bs_dev *dev, 217 spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg) 218 { 219 struct spdk_lvol_store *lvs = NULL; 220 int i; 221 int lvserrno = g_lvserrno; 222 223 if (lvserrno != 0) { 224 /* On error blobstore destroys bs_dev itself, 225 * by puttin back io channels. 226 * This operation is asynchronous, and completed 227 * after calling the callback for lvol. */ 228 cb_fn(cb_arg, g_lvol_store, lvserrno); 229 dev->destroy(dev); 230 return; 231 } 232 233 lvs = calloc(1, sizeof(*lvs)); 234 SPDK_CU_ASSERT_FATAL(lvs != NULL); 235 TAILQ_INIT(&lvs->lvols); 236 TAILQ_INIT(&lvs->pending_lvols); 237 spdk_uuid_generate(&lvs->uuid); 238 lvs->bs_dev = dev; 239 for (i = 0; i < g_num_lvols; i++) { 240 _lvol_create(lvs); 241 } 242 243 cb_fn(cb_arg, lvs, lvserrno); 244 } 245 246 int 247 spdk_bs_bdev_claim(struct spdk_bs_dev *bs_dev, struct spdk_bdev_module *module) 248 { 249 if (lvol_already_opened == true) { 250 return -1; 251 } 252 253 lvol_already_opened = true; 254 255 return 0; 256 } 257 258 void 259 spdk_bdev_unregister(struct spdk_bdev *vbdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg) 260 { 261 int rc; 262 263 SPDK_CU_ASSERT_FATAL(vbdev != NULL); 264 rc = vbdev->fn_table->destruct(vbdev->ctxt); 265 266 SPDK_CU_ASSERT_FATAL(cb_fn != NULL); 267 cb_fn(cb_arg, rc); 268 } 269 270 void 271 spdk_bdev_module_finish_done(void) 272 { 273 return; 274 } 275 276 uint64_t 277 spdk_bs_get_page_size(struct spdk_blob_store *bs) 278 { 279 return SPDK_BS_PAGE_SIZE; 280 } 281 282 uint64_t 283 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs) 284 { 285 return SPDK_BS_PAGE_SIZE; 286 } 287 288 static void 289 bdev_blob_destroy(struct spdk_bs_dev *bs_dev) 290 { 291 CU_ASSERT(bs_dev != NULL); 292 free(bs_dev); 293 lvol_already_opened = false; 294 } 295 296 struct spdk_bs_dev * 297 spdk_bdev_create_bs_dev(struct spdk_bdev *bdev, spdk_bdev_remove_cb_t remove_cb, void *remove_ctx) 298 { 299 struct spdk_bs_dev *bs_dev; 300 301 if (lvol_already_opened == true || bdev == NULL) { 302 return NULL; 303 } 304 305 bs_dev = calloc(1, sizeof(*bs_dev)); 306 SPDK_CU_ASSERT_FATAL(bs_dev != NULL); 307 bs_dev->destroy = bdev_blob_destroy; 308 309 return bs_dev; 310 } 311 312 void 313 spdk_lvs_opts_init(struct spdk_lvs_opts *opts) 314 { 315 } 316 317 int 318 spdk_lvs_init(struct spdk_bs_dev *bs_dev, struct spdk_lvs_opts *o, 319 spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg) 320 { 321 struct spdk_lvol_store *lvs; 322 int error = 0; 323 324 if (lvol_store_initialize_fail) { 325 return -1; 326 } 327 328 if (lvol_store_initialize_cb_fail) { 329 bs_dev->destroy(bs_dev); 330 lvs = NULL; 331 error = -1; 332 } else { 333 lvs = calloc(1, sizeof(*lvs)); 334 SPDK_CU_ASSERT_FATAL(lvs != NULL); 335 TAILQ_INIT(&lvs->lvols); 336 TAILQ_INIT(&lvs->pending_lvols); 337 spdk_uuid_generate(&lvs->uuid); 338 snprintf(lvs->name, sizeof(lvs->name), "%s", o->name); 339 lvs->bs_dev = bs_dev; 340 error = 0; 341 } 342 cb_fn(cb_arg, lvs, error); 343 344 return 0; 345 } 346 347 int 348 spdk_lvs_unload(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn, void *cb_arg) 349 { 350 struct spdk_lvol *lvol, *tmp; 351 352 TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) { 353 TAILQ_REMOVE(&lvs->lvols, lvol, link); 354 free(lvol->unique_id); 355 free(lvol); 356 } 357 g_lvol_store = NULL; 358 359 lvs->bs_dev->destroy(lvs->bs_dev); 360 free(lvs); 361 362 if (cb_fn != NULL) { 363 cb_fn(cb_arg, 0); 364 } 365 366 return 0; 367 } 368 369 int 370 spdk_lvs_destroy(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn, 371 void *cb_arg) 372 { 373 struct spdk_lvol *lvol, *tmp; 374 char *alias; 375 376 TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) { 377 TAILQ_REMOVE(&lvs->lvols, lvol, link); 378 379 alias = spdk_sprintf_alloc("%s/%s", lvs->name, lvol->name); 380 if (alias == NULL) { 381 SPDK_ERRLOG("Cannot alloc memory for alias\n"); 382 return -1; 383 } 384 spdk_bdev_alias_del(lvol->bdev, alias); 385 386 free(alias); 387 free(lvol->unique_id); 388 free(lvol); 389 } 390 g_lvol_store = NULL; 391 392 lvs->bs_dev->destroy(lvs->bs_dev); 393 free(lvs); 394 395 if (cb_fn != NULL) { 396 cb_fn(cb_arg, 0); 397 } 398 399 return 0; 400 } 401 402 void 403 spdk_lvol_resize(struct spdk_lvol *lvol, size_t sz, spdk_lvol_op_complete cb_fn, void *cb_arg) 404 { 405 cb_fn(cb_arg, 0); 406 } 407 408 int 409 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size) 410 { 411 bdev->blockcnt = size; 412 return 0; 413 } 414 415 uint64_t 416 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 417 { 418 return g_cluster_size; 419 } 420 421 struct spdk_bdev * 422 spdk_bdev_get_by_name(const char *bdev_name) 423 { 424 if (!strcmp(g_base_bdev->name, bdev_name)) { 425 return g_base_bdev; 426 } 427 428 return NULL; 429 } 430 431 void 432 spdk_lvol_close(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg) 433 { 434 lvol->ref_count--; 435 436 SPDK_CU_ASSERT_FATAL(cb_fn != NULL); 437 cb_fn(cb_arg, 0); 438 } 439 440 bool 441 spdk_lvol_deletable(struct spdk_lvol *lvol) 442 { 443 return g_lvol_deletable; 444 } 445 446 void 447 spdk_lvol_destroy(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg) 448 { 449 if (lvol->ref_count != 0) { 450 cb_fn(cb_arg, -ENODEV); 451 } 452 453 TAILQ_REMOVE(&lvol->lvol_store->lvols, lvol, link); 454 455 SPDK_CU_ASSERT_FATAL(cb_fn != NULL); 456 cb_fn(cb_arg, 0); 457 458 g_lvol = NULL; 459 free(lvol->unique_id); 460 free(lvol); 461 } 462 463 void 464 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status) 465 { 466 } 467 468 struct spdk_io_channel *spdk_lvol_get_io_channel(struct spdk_lvol *lvol) 469 { 470 CU_ASSERT(lvol == g_lvol); 471 return g_ch; 472 } 473 474 void 475 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len) 476 { 477 CU_ASSERT(cb == lvol_read); 478 } 479 480 void 481 spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel, 482 void *payload, uint64_t offset, uint64_t length, 483 spdk_blob_op_complete cb_fn, void *cb_arg) 484 { 485 } 486 487 void 488 spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel, 489 void *payload, uint64_t offset, uint64_t length, 490 spdk_blob_op_complete cb_fn, void *cb_arg) 491 { 492 } 493 494 void 495 spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel, 496 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 497 { 498 CU_ASSERT(blob == NULL); 499 CU_ASSERT(channel == g_ch); 500 CU_ASSERT(offset == g_io->u.bdev.offset_blocks); 501 CU_ASSERT(length == g_io->u.bdev.num_blocks); 502 } 503 504 void 505 spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel, 506 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 507 { 508 CU_ASSERT(blob == NULL); 509 CU_ASSERT(channel == g_ch); 510 CU_ASSERT(offset == g_io->u.bdev.offset_blocks); 511 CU_ASSERT(length == g_io->u.bdev.num_blocks); 512 } 513 514 void 515 spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel, 516 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 517 spdk_blob_op_complete cb_fn, void *cb_arg) 518 { 519 CU_ASSERT(blob == NULL); 520 CU_ASSERT(channel == g_ch); 521 CU_ASSERT(offset == g_io->u.bdev.offset_blocks); 522 CU_ASSERT(length == g_io->u.bdev.num_blocks); 523 } 524 525 void 526 spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel, 527 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 528 spdk_blob_op_complete cb_fn, void *cb_arg) 529 { 530 CU_ASSERT(blob == NULL); 531 CU_ASSERT(channel == g_ch); 532 CU_ASSERT(offset == g_io->u.bdev.offset_blocks); 533 CU_ASSERT(length == g_io->u.bdev.num_blocks); 534 } 535 536 void 537 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module) 538 { 539 } 540 541 int 542 spdk_json_write_name(struct spdk_json_write_ctx *w, const char *name) 543 { 544 return 0; 545 } 546 547 int 548 spdk_json_write_array_begin(struct spdk_json_write_ctx *w) 549 { 550 return 0; 551 } 552 553 int 554 spdk_json_write_array_end(struct spdk_json_write_ctx *w) 555 { 556 return 0; 557 } 558 559 int 560 spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val) 561 { 562 return 0; 563 } 564 565 int 566 spdk_json_write_bool(struct spdk_json_write_ctx *w, bool val) 567 { 568 return 0; 569 } 570 571 int 572 spdk_json_write_object_begin(struct spdk_json_write_ctx *w) 573 { 574 return 0; 575 } 576 577 int 578 spdk_json_write_object_end(struct spdk_json_write_ctx *w) 579 { 580 return 0; 581 } 582 583 const char * 584 spdk_bdev_get_name(const struct spdk_bdev *bdev) 585 { 586 return "test"; 587 } 588 589 int 590 spdk_vbdev_register(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, int base_bdev_count) 591 { 592 TAILQ_INIT(&vbdev->aliases); 593 594 g_registered_bdevs++; 595 return 0; 596 } 597 598 void 599 spdk_bdev_module_examine_done(struct spdk_bdev_module *module) 600 { 601 SPDK_CU_ASSERT_FATAL(g_examine_done != true); 602 g_examine_done = true; 603 } 604 605 static struct spdk_lvol * 606 _lvol_create(struct spdk_lvol_store *lvs) 607 { 608 struct spdk_lvol *lvol = calloc(1, sizeof(*lvol)); 609 610 SPDK_CU_ASSERT_FATAL(lvol != NULL); 611 612 lvol->lvol_store = lvs; 613 lvol->ref_count++; 614 lvol->unique_id = spdk_sprintf_alloc("%s", "UNIT_TEST_UUID"); 615 SPDK_CU_ASSERT_FATAL(lvol->unique_id != NULL); 616 617 TAILQ_INSERT_TAIL(&lvol->lvol_store->lvols, lvol, link); 618 619 return lvol; 620 } 621 622 int 623 spdk_lvol_create(struct spdk_lvol_store *lvs, const char *name, size_t sz, 624 bool thin_provision, spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg) 625 { 626 struct spdk_lvol *lvol; 627 628 lvol = _lvol_create(lvs); 629 snprintf(lvol->name, sizeof(lvol->name), "%s", name); 630 cb_fn(cb_arg, lvol, 0); 631 632 return 0; 633 } 634 635 void 636 spdk_lvol_create_snapshot(struct spdk_lvol *lvol, const char *snapshot_name, 637 spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg) 638 { 639 struct spdk_lvol *snap; 640 641 snap = _lvol_create(lvol->lvol_store); 642 snprintf(snap->name, sizeof(snap->name), "%s", snapshot_name); 643 cb_fn(cb_arg, snap, 0); 644 } 645 646 void 647 spdk_lvol_create_clone(struct spdk_lvol *lvol, const char *clone_name, 648 spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg) 649 { 650 struct spdk_lvol *clone; 651 652 clone = _lvol_create(lvol->lvol_store); 653 snprintf(clone->name, sizeof(clone->name), "%s", clone_name); 654 cb_fn(cb_arg, clone, 0); 655 } 656 657 static void 658 lvol_store_op_complete(void *cb_arg, int lvserrno) 659 { 660 g_lvserrno = lvserrno; 661 return; 662 } 663 664 static void 665 lvol_store_op_with_handle_complete(void *cb_arg, struct spdk_lvol_store *lvs, int lvserrno) 666 { 667 g_lvserrno = lvserrno; 668 g_lvol_store = lvs; 669 return; 670 } 671 672 static void 673 vbdev_lvol_create_complete(void *cb_arg, struct spdk_lvol *lvol, int lvolerrno) 674 { 675 g_lvolerrno = lvolerrno; 676 g_lvol = lvol; 677 } 678 679 static void 680 vbdev_lvol_resize_complete(void *cb_arg, int lvolerrno) 681 { 682 g_lvolerrno = lvolerrno; 683 } 684 685 static void 686 vbdev_lvol_rename_complete(void *cb_arg, int lvolerrno) 687 { 688 g_lvolerrno = lvolerrno; 689 } 690 691 static void 692 ut_lvs_destroy(void) 693 { 694 int rc = 0; 695 int sz = 10; 696 struct spdk_lvol_store *lvs; 697 698 /* Lvol store is successfully created */ 699 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 700 CU_ASSERT(rc == 0); 701 CU_ASSERT(g_lvserrno == 0); 702 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 703 CU_ASSERT(g_lvol_store->bs_dev != NULL); 704 705 lvs = g_lvol_store; 706 g_lvol_store = NULL; 707 708 spdk_uuid_generate(&lvs->uuid); 709 710 /* Successfully create lvol, which should be unloaded with lvs later */ 711 g_lvolerrno = -1; 712 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 713 CU_ASSERT(rc == 0); 714 CU_ASSERT(g_lvolerrno == 0); 715 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 716 717 /* Unload lvol store */ 718 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 719 CU_ASSERT(g_lvserrno == 0); 720 CU_ASSERT(g_lvol_store == NULL); 721 } 722 723 static void 724 ut_lvol_init(void) 725 { 726 struct spdk_lvol_store *lvs; 727 int sz = 10; 728 int rc; 729 730 /* Lvol store is successfully created */ 731 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 732 CU_ASSERT(rc == 0); 733 CU_ASSERT(g_lvserrno == 0); 734 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 735 CU_ASSERT(g_lvol_store->bs_dev != NULL); 736 lvs = g_lvol_store; 737 738 /* Successful lvol create */ 739 g_lvolerrno = -1; 740 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 741 SPDK_CU_ASSERT_FATAL(rc == 0); 742 CU_ASSERT(g_lvol != NULL); 743 CU_ASSERT(g_lvolerrno == 0); 744 745 /* Successful lvol destroy */ 746 vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL); 747 CU_ASSERT(g_lvol == NULL); 748 749 /* Destroy lvol store */ 750 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 751 CU_ASSERT(g_lvserrno == 0); 752 CU_ASSERT(g_lvol_store == NULL); 753 } 754 755 static void 756 ut_lvol_snapshot(void) 757 { 758 struct spdk_lvol_store *lvs; 759 int sz = 10; 760 int rc; 761 struct spdk_lvol *lvol = NULL; 762 763 /* Lvol store is successfully created */ 764 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 765 CU_ASSERT(rc == 0); 766 CU_ASSERT(g_lvserrno == 0); 767 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 768 CU_ASSERT(g_lvol_store->bs_dev != NULL); 769 lvs = g_lvol_store; 770 771 /* Successful lvol create */ 772 g_lvolerrno = -1; 773 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 774 SPDK_CU_ASSERT_FATAL(rc == 0); 775 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 776 CU_ASSERT(g_lvolerrno == 0); 777 778 lvol = g_lvol; 779 780 /* Successful snap create */ 781 vbdev_lvol_create_snapshot(lvol, "snap", vbdev_lvol_create_complete, NULL); 782 SPDK_CU_ASSERT_FATAL(rc == 0); 783 CU_ASSERT(g_lvol != NULL); 784 CU_ASSERT(g_lvolerrno == 0); 785 786 /* Successful lvol destroy */ 787 vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL); 788 CU_ASSERT(g_lvol == NULL); 789 790 /* Successful snap destroy */ 791 g_lvol = lvol; 792 vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL); 793 CU_ASSERT(g_lvol == NULL); 794 795 /* Destroy lvol store */ 796 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 797 CU_ASSERT(g_lvserrno == 0); 798 CU_ASSERT(g_lvol_store == NULL); 799 } 800 801 static void 802 ut_lvol_clone(void) 803 { 804 struct spdk_lvol_store *lvs; 805 int sz = 10; 806 int rc; 807 struct spdk_lvol *lvol = NULL; 808 struct spdk_lvol *snap = NULL; 809 struct spdk_lvol *clone = NULL; 810 811 /* Lvol store is successfully created */ 812 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 813 CU_ASSERT(rc == 0); 814 CU_ASSERT(g_lvserrno == 0); 815 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 816 CU_ASSERT(g_lvol_store->bs_dev != NULL); 817 lvs = g_lvol_store; 818 819 /* Successful lvol create */ 820 g_lvolerrno = -1; 821 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 822 SPDK_CU_ASSERT_FATAL(rc == 0); 823 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 824 CU_ASSERT(g_lvolerrno == 0); 825 826 lvol = g_lvol; 827 828 /* Successful snap create */ 829 vbdev_lvol_create_snapshot(lvol, "snap", vbdev_lvol_create_complete, NULL); 830 SPDK_CU_ASSERT_FATAL(rc == 0); 831 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 832 CU_ASSERT(g_lvolerrno == 0); 833 834 snap = g_lvol; 835 836 /* Successful clone create */ 837 vbdev_lvol_create_clone(snap, "clone", vbdev_lvol_create_complete, NULL); 838 839 SPDK_CU_ASSERT_FATAL(rc == 0); 840 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 841 CU_ASSERT(g_lvolerrno == 0); 842 843 clone = g_lvol; 844 845 /* Successful lvol destroy */ 846 g_lvol = lvol; 847 vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL); 848 CU_ASSERT(g_lvol == NULL); 849 850 /* Successful clone destroy */ 851 g_lvol = clone; 852 vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL); 853 CU_ASSERT(g_lvol == NULL); 854 855 /* Successful lvol destroy */ 856 g_lvol = snap; 857 vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL); 858 CU_ASSERT(g_lvol == NULL); 859 860 /* Destroy lvol store */ 861 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 862 CU_ASSERT(g_lvserrno == 0); 863 CU_ASSERT(g_lvol_store == NULL); 864 } 865 866 static void 867 ut_lvol_hotremove(void) 868 { 869 int rc = 0; 870 871 lvol_store_initialize_fail = false; 872 lvol_store_initialize_cb_fail = false; 873 lvol_already_opened = false; 874 875 /* Lvol store is successfully created */ 876 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 877 CU_ASSERT(rc == 0); 878 CU_ASSERT(g_lvserrno == 0); 879 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 880 CU_ASSERT(g_lvol_store->bs_dev != NULL); 881 882 /* Hot remove callback with NULL - stability check */ 883 vbdev_lvs_hotremove_cb(NULL); 884 885 /* Hot remove lvs on bdev removal */ 886 vbdev_lvs_hotremove_cb(&g_bdev); 887 888 CU_ASSERT(g_lvol_store == NULL); 889 CU_ASSERT(TAILQ_EMPTY(&g_spdk_lvol_pairs)); 890 891 } 892 893 static void 894 ut_lvs_examine_check(bool success) 895 { 896 struct lvol_store_bdev *lvs_bdev; 897 898 /* Examine was finished regardless of result */ 899 CU_ASSERT(g_examine_done == true); 900 g_examine_done = false; 901 902 if (success) { 903 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_spdk_lvol_pairs)); 904 lvs_bdev = TAILQ_FIRST(&g_spdk_lvol_pairs); 905 SPDK_CU_ASSERT_FATAL(lvs_bdev != NULL); 906 g_lvol_store = lvs_bdev->lvs; 907 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 908 CU_ASSERT(g_lvol_store->bs_dev != NULL); 909 } else { 910 SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&g_spdk_lvol_pairs)); 911 g_lvol_store = NULL; 912 } 913 } 914 915 static void 916 ut_lvol_examine(void) 917 { 918 /* Examine unsuccessfully - bdev already opened */ 919 g_lvserrno = -1; 920 lvol_already_opened = true; 921 vbdev_lvs_examine(&g_bdev); 922 ut_lvs_examine_check(false); 923 924 /* Examine unsuccessfully - fail on lvol store */ 925 g_lvserrno = -1; 926 lvol_already_opened = false; 927 vbdev_lvs_examine(&g_bdev); 928 ut_lvs_examine_check(false); 929 930 /* Examine successfully 931 * - one lvol fails to load 932 * - lvs is loaded with no lvols present */ 933 g_lvserrno = 0; 934 g_lvolerrno = -1; 935 g_num_lvols = 1; 936 lvol_already_opened = false; 937 g_registered_bdevs = 0; 938 vbdev_lvs_examine(&g_bdev); 939 ut_lvs_examine_check(true); 940 CU_ASSERT(g_registered_bdevs == 0); 941 CU_ASSERT(TAILQ_EMPTY(&g_lvol_store->lvols)); 942 vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL); 943 CU_ASSERT(g_lvserrno == 0); 944 CU_ASSERT(g_lvol_store == NULL); 945 946 /* Examine successfully */ 947 g_lvserrno = 0; 948 g_lvolerrno = 0; 949 g_registered_bdevs = 0; 950 lvol_already_opened = false; 951 vbdev_lvs_examine(&g_bdev); 952 ut_lvs_examine_check(true); 953 CU_ASSERT(g_registered_bdevs != 0); 954 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_lvol_store->lvols)); 955 vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL); 956 CU_ASSERT(g_lvserrno == 0); 957 } 958 959 static void 960 ut_lvol_rename(void) 961 { 962 struct spdk_lvol_store *lvs; 963 struct spdk_lvol *lvol; 964 struct spdk_lvol *lvol2; 965 int sz = 10; 966 int rc; 967 968 /* Lvol store is successfully created */ 969 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 970 CU_ASSERT(rc == 0); 971 CU_ASSERT(g_lvserrno == 0); 972 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 973 CU_ASSERT(g_lvol_store->bs_dev != NULL); 974 lvs = g_lvol_store; 975 976 /* Successful lvols create */ 977 g_lvolerrno = -1; 978 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 979 SPDK_CU_ASSERT_FATAL(rc == 0); 980 CU_ASSERT(g_lvol != NULL); 981 CU_ASSERT(g_lvolerrno == 0); 982 lvol = g_lvol; 983 984 g_lvolerrno = -1; 985 rc = vbdev_lvol_create(lvs, "lvol2", sz, false, vbdev_lvol_create_complete, NULL); 986 SPDK_CU_ASSERT_FATAL(rc == 0); 987 CU_ASSERT(g_lvol != NULL); 988 CU_ASSERT(g_lvolerrno == 0); 989 lvol2 = g_lvol; 990 991 /* Successful rename lvol */ 992 vbdev_lvol_rename(lvol, "new_lvol_name", vbdev_lvol_rename_complete, NULL); 993 SPDK_CU_ASSERT_FATAL(g_lvolerrno == 0); 994 CU_ASSERT_STRING_EQUAL(lvol->name, "new_lvol_name"); 995 996 /* Renaming lvol with name already existing */ 997 g_bdev_alias_already_exists = true; 998 vbdev_lvol_rename(lvol2, "new_lvol_name", vbdev_lvol_rename_complete, NULL); 999 g_bdev_alias_already_exists = false; 1000 SPDK_CU_ASSERT_FATAL(g_lvolerrno != 0); 1001 CU_ASSERT_STRING_NOT_EQUAL(lvol2->name, "new_lvol_name"); 1002 1003 /* Renaming lvol with it's own name */ 1004 vbdev_lvol_rename(lvol, "new_lvol_name", vbdev_lvol_rename_complete, NULL); 1005 SPDK_CU_ASSERT_FATAL(g_lvolerrno == 0); 1006 CU_ASSERT_STRING_EQUAL(lvol->name, "new_lvol_name"); 1007 1008 /* Successful lvols destroy */ 1009 vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL); 1010 CU_ASSERT(g_lvol == NULL); 1011 1012 vbdev_lvol_destroy(lvol2, lvol_store_op_complete, NULL); 1013 CU_ASSERT(g_lvol == NULL); 1014 1015 /* Destroy lvol store */ 1016 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 1017 CU_ASSERT(g_lvserrno == 0); 1018 CU_ASSERT(g_lvol_store == NULL); 1019 } 1020 1021 static void 1022 ut_lvol_destroy(void) 1023 { 1024 struct spdk_lvol_store *lvs; 1025 struct spdk_lvol *lvol; 1026 struct spdk_lvol *lvol2; 1027 int sz = 10; 1028 int rc; 1029 1030 /* Lvol store is successfully created */ 1031 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1032 CU_ASSERT(rc == 0); 1033 CU_ASSERT(g_lvserrno == 0); 1034 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1035 CU_ASSERT(g_lvol_store->bs_dev != NULL); 1036 lvs = g_lvol_store; 1037 1038 /* Successful lvols create */ 1039 g_lvolerrno = -1; 1040 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 1041 SPDK_CU_ASSERT_FATAL(rc == 0); 1042 CU_ASSERT(g_lvol != NULL); 1043 CU_ASSERT(g_lvolerrno == 0); 1044 lvol = g_lvol; 1045 1046 g_lvolerrno = -1; 1047 rc = vbdev_lvol_create(lvs, "lvol2", sz, false, vbdev_lvol_create_complete, NULL); 1048 SPDK_CU_ASSERT_FATAL(rc == 0); 1049 CU_ASSERT(g_lvol != NULL); 1050 CU_ASSERT(g_lvolerrno == 0); 1051 lvol2 = g_lvol; 1052 1053 /* Unsuccessful lvols destroy */ 1054 g_lvol_deletable = false; 1055 vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL); 1056 CU_ASSERT(g_lvol != NULL); 1057 CU_ASSERT(g_lvserrno == -EPERM); 1058 1059 g_lvol_deletable = true; 1060 /* Successful lvols destroy */ 1061 vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL); 1062 CU_ASSERT(g_lvol == NULL); 1063 CU_ASSERT(g_lvolerrno == 0); 1064 1065 /* Hot remove lvol bdev */ 1066 vbdev_lvol_unregister(lvol2); 1067 1068 /* Unload lvol store */ 1069 vbdev_lvs_unload(lvs, lvol_store_op_complete, NULL); 1070 CU_ASSERT(g_lvserrno == 0); 1071 CU_ASSERT(g_lvol_store == NULL); 1072 } 1073 1074 static void 1075 ut_lvol_resize(void) 1076 { 1077 struct spdk_lvol_store *lvs; 1078 struct spdk_lvol *lvol; 1079 int sz = 10; 1080 int rc = 0; 1081 1082 /* Lvol store is successfully created */ 1083 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1084 CU_ASSERT(rc == 0); 1085 CU_ASSERT(g_lvserrno == 0); 1086 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1087 CU_ASSERT(g_lvol_store->bs_dev != NULL); 1088 lvs = g_lvol_store; 1089 1090 /* Successful lvol create */ 1091 g_lvolerrno = -1; 1092 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 1093 CU_ASSERT(rc == 0); 1094 CU_ASSERT(g_lvolerrno == 0); 1095 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1096 lvol = g_lvol; 1097 1098 /* Successful lvol resize */ 1099 g_lvolerrno = -1; 1100 vbdev_lvol_resize(lvol, 20, vbdev_lvol_resize_complete, NULL); 1101 CU_ASSERT(g_lvolerrno == 0); 1102 CU_ASSERT(lvol->bdev->blockcnt == 20 * g_cluster_size / lvol->bdev->blocklen); 1103 1104 /* Resize with NULL lvol */ 1105 vbdev_lvol_resize(NULL, 20, vbdev_lvol_resize_complete, NULL); 1106 CU_ASSERT(g_lvolerrno != 0); 1107 1108 /* Successful lvol destroy */ 1109 vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL); 1110 CU_ASSERT(g_lvol == NULL); 1111 1112 /* Destroy lvol store */ 1113 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 1114 CU_ASSERT(g_lvserrno == 0); 1115 CU_ASSERT(g_lvol_store == NULL); 1116 } 1117 1118 static void 1119 ut_lvs_unload(void) 1120 { 1121 int rc = 0; 1122 int sz = 10; 1123 struct spdk_lvol_store *lvs; 1124 1125 /* Lvol store is successfully created */ 1126 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1127 CU_ASSERT(rc == 0); 1128 CU_ASSERT(g_lvserrno == 0); 1129 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1130 CU_ASSERT(g_lvol_store->bs_dev != NULL); 1131 1132 lvs = g_lvol_store; 1133 g_lvol_store = NULL; 1134 1135 spdk_uuid_generate(&lvs->uuid); 1136 1137 /* Successfully create lvol, which should be destroyed with lvs later */ 1138 g_lvolerrno = -1; 1139 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 1140 CU_ASSERT(rc == 0); 1141 CU_ASSERT(g_lvolerrno == 0); 1142 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1143 1144 /* Unload lvol store */ 1145 vbdev_lvs_unload(lvs, lvol_store_op_complete, NULL); 1146 CU_ASSERT(g_lvserrno == 0); 1147 CU_ASSERT(g_lvol_store == NULL); 1148 CU_ASSERT(g_lvol != NULL); 1149 } 1150 1151 static void 1152 ut_lvs_init(void) 1153 { 1154 int rc = 0; 1155 struct spdk_lvol_store *lvs; 1156 1157 /* spdk_lvs_init() fails */ 1158 lvol_store_initialize_fail = true; 1159 1160 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1161 CU_ASSERT(rc != 0); 1162 CU_ASSERT(g_lvserrno == 0); 1163 CU_ASSERT(g_lvol_store == NULL); 1164 1165 lvol_store_initialize_fail = false; 1166 1167 /* spdk_lvs_init_cb() fails */ 1168 lvol_store_initialize_cb_fail = true; 1169 1170 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1171 CU_ASSERT(rc == 0); 1172 CU_ASSERT(g_lvserrno != 0); 1173 CU_ASSERT(g_lvol_store == NULL); 1174 1175 lvol_store_initialize_cb_fail = false; 1176 1177 /* Lvol store is successfully created */ 1178 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1179 CU_ASSERT(rc == 0); 1180 CU_ASSERT(g_lvserrno == 0); 1181 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1182 CU_ASSERT(g_lvol_store->bs_dev != NULL); 1183 1184 lvs = g_lvol_store; 1185 g_lvol_store = NULL; 1186 1187 /* Bdev with lvol store already claimed */ 1188 rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL); 1189 CU_ASSERT(rc != 0); 1190 CU_ASSERT(g_lvserrno == 0); 1191 CU_ASSERT(g_lvol_store == NULL); 1192 1193 /* Destruct lvol store */ 1194 vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL); 1195 CU_ASSERT(g_lvserrno == 0); 1196 CU_ASSERT(g_lvol_store == NULL); 1197 } 1198 1199 static void 1200 ut_vbdev_lvol_get_io_channel(void) 1201 { 1202 struct spdk_io_channel *ch; 1203 1204 g_lvol = calloc(1, sizeof(struct spdk_lvol)); 1205 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1206 1207 ch = vbdev_lvol_get_io_channel(g_lvol); 1208 CU_ASSERT(ch == g_ch); 1209 1210 free(g_lvol); 1211 } 1212 1213 static void 1214 ut_vbdev_lvol_io_type_supported(void) 1215 { 1216 struct spdk_lvol *lvol; 1217 bool ret; 1218 1219 lvol = calloc(1, sizeof(struct spdk_lvol)); 1220 SPDK_CU_ASSERT_FATAL(lvol != NULL); 1221 1222 g_blob_is_read_only = false; 1223 1224 /* Supported types */ 1225 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_READ); 1226 CU_ASSERT(ret == true); 1227 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE); 1228 CU_ASSERT(ret == true); 1229 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_RESET); 1230 CU_ASSERT(ret == true); 1231 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_UNMAP); 1232 CU_ASSERT(ret == true); 1233 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE_ZEROES); 1234 CU_ASSERT(ret == true); 1235 1236 /* Unsupported types */ 1237 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_FLUSH); 1238 CU_ASSERT(ret == false); 1239 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_ADMIN); 1240 CU_ASSERT(ret == false); 1241 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_IO); 1242 CU_ASSERT(ret == false); 1243 1244 g_blob_is_read_only = true; 1245 1246 /* Supported types */ 1247 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_READ); 1248 CU_ASSERT(ret == true); 1249 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_RESET); 1250 CU_ASSERT(ret == true); 1251 1252 /* Unsupported types */ 1253 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE); 1254 CU_ASSERT(ret == false); 1255 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_UNMAP); 1256 CU_ASSERT(ret == false); 1257 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE_ZEROES); 1258 CU_ASSERT(ret == false); 1259 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_FLUSH); 1260 CU_ASSERT(ret == false); 1261 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_ADMIN); 1262 CU_ASSERT(ret == false); 1263 ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_IO); 1264 CU_ASSERT(ret == false); 1265 1266 free(lvol); 1267 } 1268 1269 static void 1270 ut_lvol_read_write(void) 1271 { 1272 g_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct lvol_task)); 1273 SPDK_CU_ASSERT_FATAL(g_io != NULL); 1274 g_base_bdev = calloc(1, sizeof(struct spdk_bdev)); 1275 SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL); 1276 g_lvol = calloc(1, sizeof(struct spdk_lvol)); 1277 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1278 1279 g_task = (struct lvol_task *)g_io->driver_ctx; 1280 g_io->bdev = g_base_bdev; 1281 g_io->bdev->ctxt = g_lvol; 1282 g_io->u.bdev.offset_blocks = 20; 1283 g_io->u.bdev.num_blocks = 20; 1284 1285 lvol_read(g_ch, g_io); 1286 CU_ASSERT(g_task->status == SPDK_BDEV_IO_STATUS_SUCCESS); 1287 1288 lvol_write(g_lvol, g_ch, g_io); 1289 CU_ASSERT(g_task->status == SPDK_BDEV_IO_STATUS_SUCCESS); 1290 1291 free(g_io); 1292 free(g_base_bdev); 1293 free(g_lvol); 1294 } 1295 1296 static void 1297 ut_vbdev_lvol_submit_request(void) 1298 { 1299 struct spdk_lvol request_lvol = {}; 1300 g_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct lvol_task)); 1301 SPDK_CU_ASSERT_FATAL(g_io != NULL); 1302 g_base_bdev = calloc(1, sizeof(struct spdk_bdev)); 1303 SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL); 1304 g_task = (struct lvol_task *)g_io->driver_ctx; 1305 g_io->bdev = g_base_bdev; 1306 1307 g_io->type = SPDK_BDEV_IO_TYPE_READ; 1308 g_base_bdev->ctxt = &request_lvol; 1309 vbdev_lvol_submit_request(g_ch, g_io); 1310 1311 free(g_io); 1312 free(g_base_bdev); 1313 } 1314 1315 static void 1316 ut_lvs_rename(void) 1317 { 1318 int rc = 0; 1319 int sz = 10; 1320 struct spdk_lvol_store *lvs; 1321 1322 /* Lvol store is successfully created */ 1323 rc = vbdev_lvs_create(&g_bdev, "old_lvs_name", 0, lvol_store_op_with_handle_complete, NULL); 1324 CU_ASSERT(rc == 0); 1325 CU_ASSERT(g_lvserrno == 0); 1326 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1327 CU_ASSERT(g_lvol_store->bs_dev != NULL); 1328 1329 lvs = g_lvol_store; 1330 g_lvol_store = NULL; 1331 1332 g_base_bdev = calloc(1, sizeof(*g_base_bdev)); 1333 SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL); 1334 1335 /* Successfully create lvol, which should be destroyed with lvs later */ 1336 g_lvolerrno = -1; 1337 rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL); 1338 CU_ASSERT(rc == 0); 1339 CU_ASSERT(g_lvolerrno == 0); 1340 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1341 1342 /* Trying to rename lvs with lvols created */ 1343 vbdev_lvs_rename(lvs, "new_lvs_name", lvol_store_op_complete, NULL); 1344 CU_ASSERT(g_lvserrno == 0); 1345 CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name"); 1346 CU_ASSERT_STRING_EQUAL(TAILQ_FIRST(&g_lvol->bdev->aliases)->alias, "new_lvs_name/lvol"); 1347 1348 /* Trying to rename lvs with name already used by another lvs */ 1349 /* This is a bdev_lvol test, so g_lvs_with_name_already_exists simulates 1350 * existing lvs with name 'another_new_lvs_name' and this name in fact is not compared */ 1351 g_lvs_with_name_already_exists = true; 1352 vbdev_lvs_rename(lvs, "another_new_lvs_name", lvol_store_op_complete, NULL); 1353 CU_ASSERT(g_lvserrno == -EEXIST); 1354 CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name"); 1355 CU_ASSERT_STRING_EQUAL(TAILQ_FIRST(&g_lvol->bdev->aliases)->alias, "new_lvs_name/lvol"); 1356 g_lvs_with_name_already_exists = false; 1357 1358 /* Unload lvol store */ 1359 g_lvol_store = lvs; 1360 vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL); 1361 CU_ASSERT(g_lvserrno == 0); 1362 CU_ASSERT(g_lvol_store == NULL); 1363 1364 free(g_base_bdev->name); 1365 free(g_base_bdev); 1366 } 1367 1368 int main(int argc, char **argv) 1369 { 1370 CU_pSuite suite = NULL; 1371 unsigned int num_failures; 1372 1373 if (CU_initialize_registry() != CUE_SUCCESS) { 1374 return CU_get_error(); 1375 } 1376 1377 suite = CU_add_suite("lvol", NULL, NULL); 1378 if (suite == NULL) { 1379 CU_cleanup_registry(); 1380 return CU_get_error(); 1381 } 1382 1383 if ( 1384 CU_add_test(suite, "ut_lvs_init", ut_lvs_init) == NULL || 1385 CU_add_test(suite, "ut_lvol_init", ut_lvol_init) == NULL || 1386 CU_add_test(suite, "ut_lvol_snapshot", ut_lvol_snapshot) == NULL || 1387 CU_add_test(suite, "ut_lvol_clone", ut_lvol_clone) == NULL || 1388 CU_add_test(suite, "ut_lvs_destroy", ut_lvs_destroy) == NULL || 1389 CU_add_test(suite, "ut_lvs_unload", ut_lvs_unload) == NULL || 1390 CU_add_test(suite, "ut_lvol_resize", ut_lvol_resize) == NULL || 1391 CU_add_test(suite, "lvol_hotremove", ut_lvol_hotremove) == NULL || 1392 CU_add_test(suite, "ut_vbdev_lvol_get_io_channel", ut_vbdev_lvol_get_io_channel) == NULL || 1393 CU_add_test(suite, "ut_vbdev_lvol_io_type_supported", ut_vbdev_lvol_io_type_supported) == NULL || 1394 CU_add_test(suite, "ut_lvol_read_write", ut_lvol_read_write) == NULL || 1395 CU_add_test(suite, "ut_vbdev_lvol_submit_request", ut_vbdev_lvol_submit_request) == NULL || 1396 CU_add_test(suite, "lvol_examine", ut_lvol_examine) == NULL || 1397 CU_add_test(suite, "ut_lvol_rename", ut_lvol_rename) == NULL || 1398 CU_add_test(suite, "ut_lvol_destroy", ut_lvol_destroy) == NULL || 1399 CU_add_test(suite, "ut_lvs_rename", ut_lvs_rename) == NULL 1400 ) { 1401 CU_cleanup_registry(); 1402 return CU_get_error(); 1403 } 1404 1405 CU_basic_set_mode(CU_BRM_VERBOSE); 1406 CU_basic_run_tests(); 1407 num_failures = CU_get_number_of_failures(); 1408 CU_cleanup_registry(); 1409 return num_failures; 1410 } 1411