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