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/blob.h" 36 #include "spdk/util.h" 37 38 #include "spdk/bdev_module.h" 39 #include "thread/thread_internal.h" 40 41 #include "common/lib/ut_multithread.c" 42 43 #include "lvol/lvol.c" 44 45 #define DEV_BUFFER_SIZE (64 * 1024 * 1024) 46 #define DEV_BUFFER_BLOCKLEN (4096) 47 #define DEV_BUFFER_BLOCKCNT (DEV_BUFFER_SIZE / DEV_BUFFER_BLOCKLEN) 48 #define BS_CLUSTER_SIZE (1024 * 1024) 49 #define BS_FREE_CLUSTERS (DEV_BUFFER_SIZE / BS_CLUSTER_SIZE) 50 #define BS_PAGE_SIZE (4096) 51 52 #define SPDK_BLOB_OPTS_CLUSTER_SZ (1024 * 1024) 53 #define SPDK_BLOB_OPTS_NUM_MD_PAGES UINT32_MAX 54 #define SPDK_BLOB_OPTS_MAX_MD_OPS 32 55 #define SPDK_BLOB_OPTS_MAX_CHANNEL_OPS 512 56 57 #define SPDK_BLOB_THIN_PROV (1ULL << 0) 58 59 const char *uuid = "828d9766-ae50-11e7-bd8d-001e67edf350"; 60 61 struct spdk_blob { 62 spdk_blob_id id; 63 uint32_t ref; 64 struct spdk_blob_store *bs; 65 int close_status; 66 int open_status; 67 int load_status; 68 TAILQ_ENTRY(spdk_blob) link; 69 char uuid[SPDK_UUID_STRING_LEN]; 70 char name[SPDK_LVS_NAME_MAX]; 71 bool thin_provisioned; 72 }; 73 74 int g_lvserrno; 75 int g_close_super_status; 76 int g_resize_rc; 77 int g_inflate_rc; 78 int g_remove_rc; 79 bool g_lvs_rename_blob_open_error = false; 80 struct spdk_lvol_store *g_lvol_store; 81 struct spdk_lvol *g_lvol; 82 spdk_blob_id g_blobid = 1; 83 struct spdk_io_channel *g_io_channel; 84 85 struct spdk_blob_store { 86 struct spdk_bs_opts bs_opts; 87 spdk_blob_id super_blobid; 88 TAILQ_HEAD(, spdk_blob) blobs; 89 int get_super_status; 90 }; 91 92 struct lvol_ut_bs_dev { 93 struct spdk_bs_dev bs_dev; 94 int init_status; 95 int load_status; 96 struct spdk_blob_store *bs; 97 }; 98 99 void spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 100 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 101 { 102 cb_fn(cb_arg, g_inflate_rc); 103 } 104 105 void spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 106 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 107 { 108 cb_fn(cb_arg, g_inflate_rc); 109 } 110 111 void 112 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *b, 113 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 114 { 115 struct spdk_blob *next; 116 int _errno = 0; 117 118 next = TAILQ_NEXT(b, link); 119 if (next == NULL) { 120 _errno = -ENOENT; 121 } else if (next->load_status != 0) { 122 _errno = next->load_status; 123 } 124 125 cb_fn(cb_arg, next, _errno); 126 } 127 128 void 129 spdk_bs_iter_first(struct spdk_blob_store *bs, 130 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 131 { 132 struct spdk_blob *first; 133 int _errno = 0; 134 135 first = TAILQ_FIRST(&bs->blobs); 136 if (first == NULL) { 137 _errno = -ENOENT; 138 } else if (first->load_status != 0) { 139 _errno = first->load_status; 140 } 141 142 cb_fn(cb_arg, first, _errno); 143 } 144 145 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob) 146 { 147 return 0; 148 } 149 150 void 151 spdk_bs_get_super(struct spdk_blob_store *bs, 152 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 153 { 154 if (bs->get_super_status != 0) { 155 cb_fn(cb_arg, 0, bs->get_super_status); 156 } else { 157 cb_fn(cb_arg, bs->super_blobid, 0); 158 } 159 } 160 161 void 162 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 163 spdk_bs_op_complete cb_fn, void *cb_arg) 164 { 165 bs->super_blobid = blobid; 166 cb_fn(cb_arg, 0); 167 } 168 169 void 170 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, 171 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 172 { 173 struct lvol_ut_bs_dev *ut_dev = SPDK_CONTAINEROF(dev, struct lvol_ut_bs_dev, bs_dev); 174 struct spdk_blob_store *bs = NULL; 175 176 if (ut_dev->load_status == 0) { 177 bs = ut_dev->bs; 178 } 179 180 cb_fn(cb_arg, bs, ut_dev->load_status); 181 } 182 183 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 184 { 185 if (g_io_channel == NULL) { 186 g_io_channel = calloc(1, sizeof(struct spdk_io_channel)); 187 SPDK_CU_ASSERT_FATAL(g_io_channel != NULL); 188 } 189 g_io_channel->ref++; 190 return g_io_channel; 191 } 192 193 void spdk_bs_free_io_channel(struct spdk_io_channel *channel) 194 { 195 g_io_channel->ref--; 196 if (g_io_channel->ref == 0) { 197 free(g_io_channel); 198 g_io_channel = NULL; 199 } 200 return; 201 } 202 203 int 204 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 205 uint16_t value_len) 206 { 207 if (!strcmp(name, "uuid")) { 208 CU_ASSERT(value_len == SPDK_UUID_STRING_LEN); 209 memcpy(blob->uuid, value, SPDK_UUID_STRING_LEN); 210 } else if (!strcmp(name, "name")) { 211 CU_ASSERT(value_len <= SPDK_LVS_NAME_MAX); 212 memcpy(blob->name, value, value_len); 213 } 214 215 return 0; 216 } 217 218 int 219 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 220 const void **value, size_t *value_len) 221 { 222 if (!strcmp(name, "uuid") && strnlen(blob->uuid, SPDK_UUID_STRING_LEN) != 0) { 223 CU_ASSERT(strnlen(blob->uuid, SPDK_UUID_STRING_LEN) == (SPDK_UUID_STRING_LEN - 1)); 224 *value = blob->uuid; 225 *value_len = SPDK_UUID_STRING_LEN; 226 return 0; 227 } else if (!strcmp(name, "name") && strnlen(blob->name, SPDK_LVS_NAME_MAX) != 0) { 228 *value = blob->name; 229 *value_len = strnlen(blob->name, SPDK_LVS_NAME_MAX) + 1; 230 return 0; 231 } 232 233 return -ENOENT; 234 } 235 236 bool spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 237 { 238 return blob->thin_provisioned; 239 } 240 241 DEFINE_STUB(spdk_blob_get_clones, int, (struct spdk_blob_store *bs, spdk_blob_id blobid, 242 spdk_blob_id *ids, size_t *count), 0); 243 DEFINE_STUB(spdk_bs_get_page_size, uint64_t, (struct spdk_blob_store *bs), BS_PAGE_SIZE); 244 245 int 246 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size) 247 { 248 bdev->blockcnt = size; 249 return 0; 250 } 251 252 static void 253 init_dev(struct lvol_ut_bs_dev *dev) 254 { 255 memset(dev, 0, sizeof(*dev)); 256 dev->bs_dev.blockcnt = DEV_BUFFER_BLOCKCNT; 257 dev->bs_dev.blocklen = DEV_BUFFER_BLOCKLEN; 258 } 259 260 static void 261 free_dev(struct lvol_ut_bs_dev *dev) 262 { 263 struct spdk_blob_store *bs = dev->bs; 264 struct spdk_blob *blob, *tmp; 265 266 if (bs == NULL) { 267 return; 268 } 269 270 TAILQ_FOREACH_SAFE(blob, &bs->blobs, link, tmp) { 271 TAILQ_REMOVE(&bs->blobs, blob, link); 272 free(blob); 273 } 274 275 free(bs); 276 dev->bs = NULL; 277 } 278 279 void 280 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 281 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 282 { 283 struct lvol_ut_bs_dev *ut_dev = SPDK_CONTAINEROF(dev, struct lvol_ut_bs_dev, bs_dev); 284 struct spdk_blob_store *bs; 285 286 bs = calloc(1, sizeof(*bs)); 287 SPDK_CU_ASSERT_FATAL(bs != NULL); 288 289 TAILQ_INIT(&bs->blobs); 290 291 ut_dev->bs = bs; 292 293 memcpy(&bs->bs_opts, o, sizeof(struct spdk_bs_opts)); 294 295 cb_fn(cb_arg, bs, 0); 296 } 297 298 void 299 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 300 { 301 cb_fn(cb_arg, 0); 302 } 303 304 void 305 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, 306 void *cb_arg) 307 { 308 free(bs); 309 310 cb_fn(cb_arg, 0); 311 } 312 313 void 314 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 315 spdk_blob_op_complete cb_fn, void *cb_arg) 316 { 317 struct spdk_blob *blob; 318 319 TAILQ_FOREACH(blob, &bs->blobs, link) { 320 if (blob->id == blobid) { 321 TAILQ_REMOVE(&bs->blobs, blob, link); 322 free(blob); 323 break; 324 } 325 } 326 327 cb_fn(cb_arg, g_remove_rc); 328 } 329 330 spdk_blob_id 331 spdk_blob_get_id(struct spdk_blob *blob) 332 { 333 return blob->id; 334 } 335 336 void 337 spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size) 338 { 339 opts->opts_size = opts_size; 340 opts->cluster_sz = SPDK_BLOB_OPTS_CLUSTER_SZ; 341 opts->num_md_pages = SPDK_BLOB_OPTS_NUM_MD_PAGES; 342 opts->max_md_ops = SPDK_BLOB_OPTS_MAX_MD_OPS; 343 opts->max_channel_ops = SPDK_BLOB_OPTS_MAX_CHANNEL_OPS; 344 memset(&opts->bstype, 0, sizeof(opts->bstype)); 345 } 346 347 DEFINE_STUB(spdk_bs_get_cluster_size, uint64_t, (struct spdk_blob_store *bs), BS_CLUSTER_SIZE); 348 349 void spdk_blob_close(struct spdk_blob *b, spdk_blob_op_complete cb_fn, void *cb_arg) 350 { 351 b->ref--; 352 353 cb_fn(cb_arg, b->close_status); 354 } 355 356 void 357 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg) 358 { 359 if (g_resize_rc != 0) { 360 return cb_fn(cb_arg, g_resize_rc); 361 } else if (sz > DEV_BUFFER_SIZE / BS_CLUSTER_SIZE) { 362 return cb_fn(cb_arg, -ENOMEM); 363 } 364 cb_fn(cb_arg, 0); 365 } 366 367 DEFINE_STUB(spdk_blob_set_read_only, int, (struct spdk_blob *blob), 0); 368 369 void 370 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 371 { 372 cb_fn(cb_arg, 0); 373 } 374 375 void 376 spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid, 377 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 378 { 379 spdk_bs_open_blob(bs, blobid, cb_fn, cb_arg); 380 } 381 382 void 383 spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 384 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 385 { 386 struct spdk_blob *blob; 387 388 if (!g_lvs_rename_blob_open_error) { 389 TAILQ_FOREACH(blob, &bs->blobs, link) { 390 if (blob->id == blobid) { 391 blob->ref++; 392 cb_fn(cb_arg, blob, blob->open_status); 393 return; 394 } 395 } 396 } 397 398 cb_fn(cb_arg, NULL, -ENOENT); 399 } 400 401 DEFINE_STUB(spdk_bs_free_cluster_count, uint64_t, (struct spdk_blob_store *bs), BS_FREE_CLUSTERS); 402 403 void 404 spdk_blob_opts_init(struct spdk_blob_opts *opts, size_t opts_size) 405 { 406 opts->opts_size = opts_size; 407 opts->num_clusters = 0; 408 opts->thin_provision = false; 409 opts->xattrs.count = 0; 410 opts->xattrs.names = NULL; 411 opts->xattrs.ctx = NULL; 412 opts->xattrs.get_value = NULL; 413 } 414 415 void 416 spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts, size_t opts_size) 417 { 418 opts->opts_size = opts_size; 419 opts->clear_method = BLOB_CLEAR_WITH_DEFAULT; 420 } 421 422 void 423 spdk_bs_create_blob(struct spdk_blob_store *bs, 424 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 425 { 426 spdk_bs_create_blob_ext(bs, NULL, cb_fn, cb_arg); 427 } 428 429 void 430 spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts, 431 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 432 { 433 struct spdk_blob *b; 434 435 if (opts && opts->num_clusters > DEV_BUFFER_SIZE / BS_CLUSTER_SIZE) { 436 cb_fn(cb_arg, 0, -1); 437 return; 438 } 439 440 b = calloc(1, sizeof(*b)); 441 SPDK_CU_ASSERT_FATAL(b != NULL); 442 443 b->id = g_blobid++; 444 if (opts != NULL && opts->thin_provision) { 445 b->thin_provisioned = true; 446 } 447 b->bs = bs; 448 449 TAILQ_INSERT_TAIL(&bs->blobs, b, link); 450 cb_fn(cb_arg, b->id, 0); 451 } 452 453 void 454 spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid, 455 const struct spdk_blob_xattr_opts *snapshot_xattrs, 456 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 457 { 458 spdk_bs_create_blob_ext(bs, NULL, cb_fn, cb_arg); 459 } 460 461 void 462 spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid, 463 const struct spdk_blob_xattr_opts *clone_xattrs, 464 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 465 { 466 spdk_bs_create_blob_ext(bs, NULL, cb_fn, cb_arg); 467 } 468 469 static void 470 lvol_store_op_with_handle_complete(void *cb_arg, struct spdk_lvol_store *lvol_store, int lvserrno) 471 { 472 g_lvol_store = lvol_store; 473 g_lvserrno = lvserrno; 474 } 475 476 static void 477 lvol_op_with_handle_complete(void *cb_arg, struct spdk_lvol *lvol, int lvserrno) 478 { 479 g_lvol = lvol; 480 g_lvserrno = lvserrno; 481 } 482 483 static void 484 op_complete(void *cb_arg, int lvserrno) 485 { 486 g_lvserrno = lvserrno; 487 } 488 489 static void 490 lvs_init_unload_success(void) 491 { 492 struct lvol_ut_bs_dev dev; 493 struct spdk_lvs_opts opts; 494 int rc = 0; 495 496 init_dev(&dev); 497 498 spdk_lvs_opts_init(&opts); 499 snprintf(opts.name, sizeof(opts.name), "lvs"); 500 501 g_lvserrno = -1; 502 503 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 504 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 505 CU_ASSERT(rc == 0); 506 CU_ASSERT(g_lvserrno == 0); 507 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 508 CU_ASSERT(!TAILQ_EMPTY(&g_lvol_stores)); 509 510 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 511 lvol_op_with_handle_complete, NULL); 512 CU_ASSERT(g_lvserrno == 0); 513 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 514 515 /* Lvol store has an open lvol, this unload should fail. */ 516 g_lvserrno = -1; 517 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 518 CU_ASSERT(rc == -EBUSY); 519 CU_ASSERT(g_lvserrno == -EBUSY); 520 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 521 CU_ASSERT(!TAILQ_EMPTY(&g_lvol_stores)); 522 523 /* Lvol has to be closed (or destroyed) before unloading lvol store. */ 524 spdk_lvol_close(g_lvol, op_complete, NULL); 525 CU_ASSERT(g_lvserrno == 0); 526 527 g_lvserrno = -1; 528 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 529 CU_ASSERT(rc == 0); 530 CU_ASSERT(g_lvserrno == 0); 531 g_lvol_store = NULL; 532 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 533 534 free_dev(&dev); 535 } 536 537 static void 538 lvs_init_destroy_success(void) 539 { 540 struct lvol_ut_bs_dev dev; 541 struct spdk_lvs_opts opts; 542 int rc = 0; 543 544 init_dev(&dev); 545 546 spdk_lvs_opts_init(&opts); 547 snprintf(opts.name, sizeof(opts.name), "lvs"); 548 549 g_lvserrno = -1; 550 551 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 552 CU_ASSERT(rc == 0); 553 CU_ASSERT(g_lvserrno == 0); 554 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 555 556 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 557 lvol_op_with_handle_complete, NULL); 558 CU_ASSERT(g_lvserrno == 0); 559 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 560 561 /* Lvol store contains one lvol, this destroy should fail. */ 562 g_lvserrno = -1; 563 rc = spdk_lvs_destroy(g_lvol_store, op_complete, NULL); 564 CU_ASSERT(rc == -EBUSY); 565 CU_ASSERT(g_lvserrno == -EBUSY); 566 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 567 568 spdk_lvol_close(g_lvol, op_complete, NULL); 569 CU_ASSERT(g_lvserrno == 0); 570 571 spdk_lvol_destroy(g_lvol, op_complete, NULL); 572 573 g_lvserrno = -1; 574 rc = spdk_lvs_destroy(g_lvol_store, op_complete, NULL); 575 CU_ASSERT(rc == 0); 576 CU_ASSERT(g_lvserrno == 0); 577 g_lvol_store = NULL; 578 } 579 580 static void 581 lvs_init_opts_success(void) 582 { 583 struct lvol_ut_bs_dev dev; 584 struct spdk_lvs_opts opts; 585 int rc = 0; 586 587 init_dev(&dev); 588 589 g_lvserrno = -1; 590 591 spdk_lvs_opts_init(&opts); 592 snprintf(opts.name, sizeof(opts.name), "lvs"); 593 opts.cluster_sz = 8192; 594 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 595 CU_ASSERT(rc == 0); 596 CU_ASSERT(g_lvserrno == 0); 597 CU_ASSERT(dev.bs->bs_opts.cluster_sz == opts.cluster_sz); 598 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 599 600 g_lvserrno = -1; 601 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 602 CU_ASSERT(rc == 0); 603 CU_ASSERT(g_lvserrno == 0); 604 g_lvol_store = NULL; 605 606 free_dev(&dev); 607 } 608 609 static void 610 lvs_unload_lvs_is_null_fail(void) 611 { 612 int rc = 0; 613 614 g_lvserrno = -1; 615 rc = spdk_lvs_unload(NULL, op_complete, NULL); 616 CU_ASSERT(rc == -ENODEV); 617 CU_ASSERT(g_lvserrno == -1); 618 } 619 620 static void 621 lvs_names(void) 622 { 623 struct lvol_ut_bs_dev dev_x, dev_y, dev_x2; 624 struct spdk_lvs_opts opts_none, opts_x, opts_y, opts_full; 625 struct spdk_lvol_store *lvs_x, *lvs_y, *lvs_x2; 626 int rc = 0; 627 628 init_dev(&dev_x); 629 init_dev(&dev_y); 630 init_dev(&dev_x2); 631 632 spdk_lvs_opts_init(&opts_none); 633 spdk_lvs_opts_init(&opts_x); 634 opts_x.name[0] = 'x'; 635 spdk_lvs_opts_init(&opts_y); 636 opts_y.name[0] = 'y'; 637 spdk_lvs_opts_init(&opts_full); 638 memset(opts_full.name, 'a', sizeof(opts_full.name)); 639 640 /* Test that opts with no name fails spdk_lvs_init(). */ 641 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 642 rc = spdk_lvs_init(&dev_x.bs_dev, &opts_none, lvol_store_op_with_handle_complete, NULL); 643 CU_ASSERT(rc != 0); 644 CU_ASSERT(g_lvol_store == NULL); 645 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 646 647 /* Test that opts with no null terminator for name fails spdk_lvs_init(). */ 648 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 649 rc = spdk_lvs_init(&dev_x.bs_dev, &opts_full, lvol_store_op_with_handle_complete, NULL); 650 CU_ASSERT(rc != 0); 651 CU_ASSERT(g_lvol_store == NULL); 652 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 653 654 /* Test that we can create an lvolstore with name 'x'. */ 655 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 656 g_lvol_store = NULL; 657 rc = spdk_lvs_init(&dev_x.bs_dev, &opts_x, lvol_store_op_with_handle_complete, NULL); 658 CU_ASSERT(rc == 0); 659 CU_ASSERT(!TAILQ_EMPTY(&g_lvol_stores)); 660 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 661 lvs_x = g_lvol_store; 662 663 /* Test that we can create an lvolstore with name 'y'. */ 664 g_lvol_store = NULL; 665 rc = spdk_lvs_init(&dev_y.bs_dev, &opts_y, lvol_store_op_with_handle_complete, NULL); 666 CU_ASSERT(rc == 0); 667 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 668 lvs_y = g_lvol_store; 669 670 /* Test that we cannot create another lvolstore with name 'x'. */ 671 rc = spdk_lvs_init(&dev_x2.bs_dev, &opts_x, lvol_store_op_with_handle_complete, NULL); 672 CU_ASSERT(rc == -EEXIST); 673 674 /* Now destroy lvolstore 'x' and then confirm we can create a new lvolstore with name 'x'. */ 675 g_lvserrno = -1; 676 rc = spdk_lvs_destroy(lvs_x, op_complete, NULL); 677 CU_ASSERT(rc == 0); 678 CU_ASSERT(g_lvserrno == 0); 679 g_lvol_store = NULL; 680 rc = spdk_lvs_init(&dev_x.bs_dev, &opts_x, lvol_store_op_with_handle_complete, NULL); 681 CU_ASSERT(rc == 0); 682 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 683 lvs_x = g_lvol_store; 684 685 /* 686 * Unload lvolstore 'x'. Then we should be able to create another lvolstore with name 'x'. 687 */ 688 g_lvserrno = -1; 689 rc = spdk_lvs_unload(lvs_x, op_complete, NULL); 690 CU_ASSERT(rc == 0); 691 CU_ASSERT(g_lvserrno == 0); 692 g_lvol_store = NULL; 693 rc = spdk_lvs_init(&dev_x2.bs_dev, &opts_x, lvol_store_op_with_handle_complete, NULL); 694 CU_ASSERT(rc == 0); 695 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 696 lvs_x2 = g_lvol_store; 697 698 /* Confirm that we cannot load the first lvolstore 'x'. */ 699 g_lvserrno = 0; 700 spdk_lvs_load(&dev_x.bs_dev, lvol_store_op_with_handle_complete, NULL); 701 CU_ASSERT(g_lvserrno != 0); 702 703 /* Destroy the second lvolstore 'x'. Then we should be able to load the first lvolstore 'x'. */ 704 g_lvserrno = -1; 705 rc = spdk_lvs_destroy(lvs_x2, op_complete, NULL); 706 CU_ASSERT(rc == 0); 707 CU_ASSERT(g_lvserrno == 0); 708 g_lvserrno = -1; 709 spdk_lvs_load(&dev_x.bs_dev, lvol_store_op_with_handle_complete, NULL); 710 CU_ASSERT(g_lvserrno == 0); 711 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 712 lvs_x = g_lvol_store; 713 714 g_lvserrno = -1; 715 rc = spdk_lvs_destroy(lvs_x, op_complete, NULL); 716 CU_ASSERT(rc == 0); 717 CU_ASSERT(g_lvserrno == 0); 718 719 g_lvserrno = -1; 720 rc = spdk_lvs_destroy(lvs_y, op_complete, NULL); 721 CU_ASSERT(rc == 0); 722 CU_ASSERT(g_lvserrno == 0); 723 } 724 725 static void 726 lvol_create_destroy_success(void) 727 { 728 struct lvol_ut_bs_dev dev; 729 struct spdk_lvs_opts opts; 730 int rc = 0; 731 732 init_dev(&dev); 733 734 spdk_lvs_opts_init(&opts); 735 snprintf(opts.name, sizeof(opts.name), "lvs"); 736 737 g_lvserrno = -1; 738 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 739 CU_ASSERT(rc == 0); 740 CU_ASSERT(g_lvserrno == 0); 741 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 742 743 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 744 lvol_op_with_handle_complete, NULL); 745 CU_ASSERT(g_lvserrno == 0); 746 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 747 748 spdk_lvol_close(g_lvol, op_complete, NULL); 749 CU_ASSERT(g_lvserrno == 0); 750 spdk_lvol_destroy(g_lvol, op_complete, NULL); 751 CU_ASSERT(g_lvserrno == 0); 752 753 g_lvserrno = -1; 754 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 755 CU_ASSERT(rc == 0); 756 CU_ASSERT(g_lvserrno == 0); 757 g_lvol_store = NULL; 758 759 free_dev(&dev); 760 } 761 762 static void 763 lvol_create_fail(void) 764 { 765 struct lvol_ut_bs_dev dev; 766 struct spdk_lvs_opts opts; 767 int rc = 0; 768 769 init_dev(&dev); 770 771 spdk_lvs_opts_init(&opts); 772 snprintf(opts.name, sizeof(opts.name), "lvs"); 773 774 g_lvol_store = NULL; 775 g_lvserrno = 0; 776 rc = spdk_lvs_init(NULL, &opts, lvol_store_op_with_handle_complete, NULL); 777 CU_ASSERT(rc != 0); 778 CU_ASSERT(g_lvol_store == NULL); 779 780 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 781 CU_ASSERT(rc == 0); 782 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 783 784 g_lvol = NULL; 785 rc = spdk_lvol_create(NULL, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 786 lvol_op_with_handle_complete, NULL); 787 CU_ASSERT(rc != 0); 788 CU_ASSERT(g_lvol == NULL); 789 790 g_lvol = NULL; 791 rc = spdk_lvol_create(g_lvol_store, "lvol", DEV_BUFFER_SIZE + 1, false, LVOL_CLEAR_WITH_DEFAULT, 792 lvol_op_with_handle_complete, NULL); 793 CU_ASSERT(rc == 0); 794 CU_ASSERT(g_lvserrno != 0); 795 CU_ASSERT(g_lvol == NULL); 796 797 g_lvserrno = -1; 798 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 799 CU_ASSERT(rc == 0); 800 CU_ASSERT(g_lvserrno == 0); 801 g_lvol_store = NULL; 802 803 free_dev(&dev); 804 } 805 806 static void 807 lvol_destroy_fail(void) 808 { 809 struct lvol_ut_bs_dev dev; 810 struct spdk_lvs_opts opts; 811 int rc = 0; 812 813 init_dev(&dev); 814 815 spdk_lvs_opts_init(&opts); 816 snprintf(opts.name, sizeof(opts.name), "lvs"); 817 818 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 819 CU_ASSERT(rc == 0); 820 CU_ASSERT(g_lvserrno == 0); 821 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 822 823 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 824 lvol_op_with_handle_complete, NULL); 825 CU_ASSERT(g_lvserrno == 0); 826 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 827 828 spdk_lvol_close(g_lvol, op_complete, NULL); 829 CU_ASSERT(g_lvserrno == 0); 830 spdk_lvol_destroy(g_lvol, op_complete, NULL); 831 CU_ASSERT(g_lvserrno == 0); 832 833 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 834 lvol_op_with_handle_complete, NULL); 835 CU_ASSERT(g_lvserrno == 0); 836 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 837 838 spdk_lvol_close(g_lvol, op_complete, NULL); 839 CU_ASSERT(g_lvserrno == 0); 840 841 g_remove_rc = -1; 842 spdk_lvol_destroy(g_lvol, op_complete, NULL); 843 CU_ASSERT(g_lvserrno != 0); 844 CU_ASSERT(TAILQ_EMPTY(&g_lvol_store->lvols)); 845 g_remove_rc = 0; 846 847 g_lvserrno = -1; 848 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 849 CU_ASSERT(rc == 0); 850 CU_ASSERT(g_lvserrno == 0); 851 g_lvol_store = NULL; 852 853 free_dev(&dev); 854 } 855 856 static void 857 lvol_close_fail(void) 858 { 859 struct lvol_ut_bs_dev dev; 860 struct spdk_lvs_opts opts; 861 int rc = 0; 862 863 init_dev(&dev); 864 865 spdk_lvs_opts_init(&opts); 866 snprintf(opts.name, sizeof(opts.name), "lvs"); 867 868 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 869 CU_ASSERT(rc == 0); 870 CU_ASSERT(g_lvserrno == 0); 871 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 872 873 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 874 lvol_op_with_handle_complete, NULL); 875 CU_ASSERT(g_lvserrno == 0); 876 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 877 878 spdk_lvol_close(g_lvol, op_complete, NULL); 879 CU_ASSERT(g_lvserrno == 0); 880 881 g_lvserrno = -1; 882 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 883 CU_ASSERT(rc == 0); 884 CU_ASSERT(g_lvserrno == 0); 885 g_lvol_store = NULL; 886 887 free_dev(&dev); 888 } 889 890 static void 891 lvol_close_success(void) 892 { 893 struct lvol_ut_bs_dev dev; 894 struct spdk_lvs_opts opts; 895 int rc = 0; 896 897 init_dev(&dev); 898 899 spdk_lvs_opts_init(&opts); 900 snprintf(opts.name, sizeof(opts.name), "lvs"); 901 902 g_lvserrno = -1; 903 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 904 CU_ASSERT(rc == 0); 905 CU_ASSERT(g_lvserrno == 0); 906 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 907 908 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 909 lvol_op_with_handle_complete, NULL); 910 CU_ASSERT(g_lvserrno == 0); 911 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 912 913 spdk_lvol_close(g_lvol, op_complete, NULL); 914 CU_ASSERT(g_lvserrno == 0); 915 916 g_lvserrno = -1; 917 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 918 CU_ASSERT(rc == 0); 919 CU_ASSERT(g_lvserrno == 0); 920 g_lvol_store = NULL; 921 922 free_dev(&dev); 923 } 924 925 static void 926 lvol_resize(void) 927 { 928 struct lvol_ut_bs_dev dev; 929 struct spdk_lvs_opts opts; 930 int rc = 0; 931 932 init_dev(&dev); 933 934 spdk_lvs_opts_init(&opts); 935 snprintf(opts.name, sizeof(opts.name), "lvs"); 936 937 g_resize_rc = 0; 938 g_lvserrno = -1; 939 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 940 CU_ASSERT(rc == 0); 941 CU_ASSERT(g_lvserrno == 0); 942 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 943 944 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 945 lvol_op_with_handle_complete, NULL); 946 CU_ASSERT(g_lvserrno == 0); 947 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 948 949 /* Resize to same size */ 950 spdk_lvol_resize(g_lvol, 10, op_complete, NULL); 951 CU_ASSERT(g_lvserrno == 0); 952 953 /* Resize to smaller size */ 954 spdk_lvol_resize(g_lvol, 5, op_complete, NULL); 955 CU_ASSERT(g_lvserrno == 0); 956 957 /* Resize to bigger size */ 958 spdk_lvol_resize(g_lvol, 15, op_complete, NULL); 959 CU_ASSERT(g_lvserrno == 0); 960 961 /* Resize to size = 0 */ 962 spdk_lvol_resize(g_lvol, 0, op_complete, NULL); 963 CU_ASSERT(g_lvserrno == 0); 964 965 /* Resize to bigger size than available */ 966 g_lvserrno = 0; 967 spdk_lvol_resize(g_lvol, 0xFFFFFFFF, op_complete, NULL); 968 CU_ASSERT(g_lvserrno != 0); 969 970 /* Fail resize */ 971 g_resize_rc = -1; 972 g_lvserrno = 0; 973 spdk_lvol_resize(g_lvol, 10, op_complete, NULL); 974 CU_ASSERT(g_lvserrno != 0); 975 g_resize_rc = 0; 976 977 g_resize_rc = 0; 978 spdk_lvol_close(g_lvol, op_complete, NULL); 979 CU_ASSERT(g_lvserrno == 0); 980 spdk_lvol_destroy(g_lvol, op_complete, NULL); 981 CU_ASSERT(g_lvserrno == 0); 982 983 g_lvserrno = -1; 984 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 985 CU_ASSERT(rc == 0); 986 CU_ASSERT(g_lvserrno == 0); 987 g_lvol_store = NULL; 988 989 free_dev(&dev); 990 } 991 992 static void 993 lvol_set_read_only(void) 994 { 995 struct lvol_ut_bs_dev dev; 996 struct spdk_lvs_opts opts; 997 int rc = 0; 998 struct spdk_lvol *lvol, *clone; 999 1000 init_dev(&dev); 1001 1002 spdk_lvs_opts_init(&opts); 1003 snprintf(opts.name, sizeof(opts.name), "lvs"); 1004 1005 g_lvserrno = -1; 1006 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1007 CU_ASSERT(rc == 0); 1008 CU_ASSERT(g_lvserrno == 0); 1009 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1010 1011 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 1012 lvol_op_with_handle_complete, NULL); 1013 CU_ASSERT(g_lvserrno == 0); 1014 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1015 lvol = g_lvol; 1016 1017 /* Set lvol as read only */ 1018 spdk_lvol_set_read_only(lvol, op_complete, NULL); 1019 CU_ASSERT(g_lvserrno == 0); 1020 1021 /* Create lvol clone from read only lvol */ 1022 spdk_lvol_create_clone(lvol, "clone", lvol_op_with_handle_complete, NULL); 1023 CU_ASSERT(g_lvserrno == 0); 1024 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1025 CU_ASSERT_STRING_EQUAL(g_lvol->name, "clone"); 1026 clone = g_lvol; 1027 1028 spdk_lvol_close(lvol, op_complete, NULL); 1029 CU_ASSERT(g_lvserrno == 0); 1030 spdk_lvol_close(clone, op_complete, NULL); 1031 CU_ASSERT(g_lvserrno == 0); 1032 1033 g_lvserrno = -1; 1034 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1035 CU_ASSERT(rc == 0); 1036 CU_ASSERT(g_lvserrno == 0); 1037 g_lvol_store = NULL; 1038 1039 free_dev(&dev); 1040 } 1041 1042 static void 1043 null_cb(void *ctx, struct spdk_blob_store *bs, int bserrno) 1044 { 1045 SPDK_CU_ASSERT_FATAL(bs != NULL); 1046 } 1047 1048 static void 1049 lvs_load(void) 1050 { 1051 int rc = -1; 1052 struct lvol_ut_bs_dev dev; 1053 struct spdk_lvs_with_handle_req *req; 1054 struct spdk_bs_opts bs_opts = {}; 1055 struct spdk_blob *super_blob; 1056 1057 req = calloc(1, sizeof(*req)); 1058 SPDK_CU_ASSERT_FATAL(req != NULL); 1059 1060 init_dev(&dev); 1061 spdk_bs_opts_init(&bs_opts, sizeof(bs_opts)); 1062 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "LVOLSTORE"); 1063 spdk_bs_init(&dev.bs_dev, &bs_opts, null_cb, NULL); 1064 SPDK_CU_ASSERT_FATAL(dev.bs != NULL); 1065 1066 /* Fail on bs load */ 1067 dev.load_status = -1; 1068 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1069 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1070 CU_ASSERT(g_lvserrno != 0); 1071 CU_ASSERT(g_lvol_store == NULL); 1072 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1073 1074 /* Fail on getting super blob */ 1075 dev.load_status = 0; 1076 dev.bs->get_super_status = -1; 1077 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1078 CU_ASSERT(g_lvserrno == -ENODEV); 1079 CU_ASSERT(g_lvol_store == NULL); 1080 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1081 1082 /* Fail on opening super blob */ 1083 g_lvserrno = 0; 1084 super_blob = calloc(1, sizeof(*super_blob)); 1085 super_blob->id = 0x100; 1086 super_blob->open_status = -1; 1087 TAILQ_INSERT_TAIL(&dev.bs->blobs, super_blob, link); 1088 dev.bs->super_blobid = 0x100; 1089 dev.bs->get_super_status = 0; 1090 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1091 CU_ASSERT(g_lvserrno == -ENODEV); 1092 CU_ASSERT(g_lvol_store == NULL); 1093 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1094 1095 /* Fail on getting uuid */ 1096 g_lvserrno = 0; 1097 super_blob->open_status = 0; 1098 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1099 CU_ASSERT(g_lvserrno == -EINVAL); 1100 CU_ASSERT(g_lvol_store == NULL); 1101 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1102 1103 /* Fail on getting name */ 1104 g_lvserrno = 0; 1105 spdk_blob_set_xattr(super_blob, "uuid", uuid, SPDK_UUID_STRING_LEN); 1106 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1107 CU_ASSERT(g_lvserrno == -EINVAL); 1108 CU_ASSERT(g_lvol_store == NULL); 1109 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1110 1111 /* Fail on closing super blob */ 1112 g_lvserrno = 0; 1113 spdk_blob_set_xattr(super_blob, "name", "lvs", strnlen("lvs", SPDK_LVS_NAME_MAX) + 1); 1114 super_blob->close_status = -1; 1115 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1116 CU_ASSERT(g_lvserrno == -ENODEV); 1117 CU_ASSERT(g_lvol_store == NULL); 1118 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1119 1120 /* Load successfully */ 1121 g_lvserrno = 0; 1122 super_blob->close_status = 0; 1123 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1124 CU_ASSERT(g_lvserrno == 0); 1125 CU_ASSERT(g_lvol_store != NULL); 1126 CU_ASSERT(!TAILQ_EMPTY(&g_lvol_stores)); 1127 1128 g_lvserrno = -1; 1129 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1130 CU_ASSERT(rc == 0); 1131 CU_ASSERT(g_lvserrno == 0); 1132 CU_ASSERT(TAILQ_EMPTY(&g_lvol_stores)); 1133 1134 free(req); 1135 free_dev(&dev); 1136 } 1137 1138 static void 1139 lvols_load(void) 1140 { 1141 int rc = -1; 1142 struct lvol_ut_bs_dev dev; 1143 struct spdk_lvs_with_handle_req *req; 1144 struct spdk_bs_opts bs_opts; 1145 struct spdk_blob *super_blob, *blob1, *blob2, *blob3; 1146 1147 req = calloc(1, sizeof(*req)); 1148 SPDK_CU_ASSERT_FATAL(req != NULL); 1149 1150 init_dev(&dev); 1151 spdk_bs_opts_init(&bs_opts, sizeof(bs_opts)); 1152 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "LVOLSTORE"); 1153 spdk_bs_init(&dev.bs_dev, &bs_opts, null_cb, NULL); 1154 super_blob = calloc(1, sizeof(*super_blob)); 1155 SPDK_CU_ASSERT_FATAL(super_blob != NULL); 1156 super_blob->id = 0x100; 1157 spdk_blob_set_xattr(super_blob, "uuid", uuid, SPDK_UUID_STRING_LEN); 1158 spdk_blob_set_xattr(super_blob, "name", "lvs", strnlen("lvs", SPDK_LVS_NAME_MAX) + 1); 1159 TAILQ_INSERT_TAIL(&dev.bs->blobs, super_blob, link); 1160 dev.bs->super_blobid = 0x100; 1161 1162 /* 1163 * Create 3 blobs, write different char values to the last char in the UUID 1164 * to make sure they are unique. 1165 */ 1166 blob1 = calloc(1, sizeof(*blob1)); 1167 SPDK_CU_ASSERT_FATAL(blob1 != NULL); 1168 blob1->id = 0x1; 1169 spdk_blob_set_xattr(blob1, "uuid", uuid, SPDK_UUID_STRING_LEN); 1170 spdk_blob_set_xattr(blob1, "name", "lvol1", strnlen("lvol1", SPDK_LVOL_NAME_MAX) + 1); 1171 blob1->uuid[SPDK_UUID_STRING_LEN - 2] = '1'; 1172 1173 blob2 = calloc(1, sizeof(*blob2)); 1174 SPDK_CU_ASSERT_FATAL(blob2 != NULL); 1175 blob2->id = 0x2; 1176 spdk_blob_set_xattr(blob2, "uuid", uuid, SPDK_UUID_STRING_LEN); 1177 spdk_blob_set_xattr(blob2, "name", "lvol2", strnlen("lvol2", SPDK_LVOL_NAME_MAX) + 1); 1178 blob2->uuid[SPDK_UUID_STRING_LEN - 2] = '2'; 1179 1180 blob3 = calloc(1, sizeof(*blob3)); 1181 SPDK_CU_ASSERT_FATAL(blob3 != NULL); 1182 blob3->id = 0x2; 1183 spdk_blob_set_xattr(blob3, "uuid", uuid, SPDK_UUID_STRING_LEN); 1184 spdk_blob_set_xattr(blob3, "name", "lvol3", strnlen("lvol3", SPDK_LVOL_NAME_MAX) + 1); 1185 blob3->uuid[SPDK_UUID_STRING_LEN - 2] = '3'; 1186 1187 /* Load lvs with 0 blobs */ 1188 g_lvserrno = 0; 1189 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1190 CU_ASSERT(g_lvserrno == 0); 1191 CU_ASSERT(g_lvol_store != NULL); 1192 CU_ASSERT(g_lvserrno == 0); 1193 1194 g_lvserrno = -1; 1195 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1196 CU_ASSERT(rc == 0); 1197 CU_ASSERT(g_lvserrno == 0); 1198 1199 TAILQ_INSERT_TAIL(&dev.bs->blobs, blob1, link); 1200 TAILQ_INSERT_TAIL(&dev.bs->blobs, blob2, link); 1201 TAILQ_INSERT_TAIL(&dev.bs->blobs, blob3, link); 1202 1203 /* Load lvs again with 3 blobs, but fail on 1st one */ 1204 g_lvol_store = NULL; 1205 g_lvserrno = 0; 1206 blob1->load_status = -1; 1207 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1208 CU_ASSERT(g_lvserrno != 0); 1209 CU_ASSERT(g_lvol_store == NULL); 1210 1211 /* Load lvs again with 3 blobs, but fail on 3rd one */ 1212 g_lvol_store = NULL; 1213 g_lvserrno = 0; 1214 blob1->load_status = 0; 1215 blob2->load_status = 0; 1216 blob3->load_status = -1; 1217 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1218 CU_ASSERT(g_lvserrno != 0); 1219 CU_ASSERT(g_lvol_store == NULL); 1220 1221 /* Load lvs again with 3 blobs, with success */ 1222 g_lvol_store = NULL; 1223 g_lvserrno = 0; 1224 blob1->load_status = 0; 1225 blob2->load_status = 0; 1226 blob3->load_status = 0; 1227 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1228 CU_ASSERT(g_lvserrno == 0); 1229 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1230 CU_ASSERT(!TAILQ_EMPTY(&g_lvol_store->lvols)); 1231 1232 g_lvserrno = -1; 1233 /* rc = */ spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1234 /* 1235 * Disable these two asserts for now. lvolstore should allow unload as long 1236 * as the lvols were not opened - but this is coming a future patch. 1237 */ 1238 /* CU_ASSERT(rc == 0); */ 1239 /* CU_ASSERT(g_lvserrno == 0); */ 1240 1241 free(req); 1242 free_dev(&dev); 1243 } 1244 1245 static void 1246 lvol_open(void) 1247 { 1248 struct lvol_ut_bs_dev dev; 1249 struct spdk_lvs_with_handle_req *req; 1250 struct spdk_bs_opts bs_opts; 1251 struct spdk_blob *super_blob, *blob1, *blob2, *blob3; 1252 struct spdk_lvol *lvol, *tmp; 1253 1254 req = calloc(1, sizeof(*req)); 1255 SPDK_CU_ASSERT_FATAL(req != NULL); 1256 1257 init_dev(&dev); 1258 spdk_bs_opts_init(&bs_opts, sizeof(bs_opts)); 1259 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "LVOLSTORE"); 1260 spdk_bs_init(&dev.bs_dev, &bs_opts, null_cb, NULL); 1261 super_blob = calloc(1, sizeof(*super_blob)); 1262 SPDK_CU_ASSERT_FATAL(super_blob != NULL); 1263 super_blob->id = 0x100; 1264 spdk_blob_set_xattr(super_blob, "uuid", uuid, SPDK_UUID_STRING_LEN); 1265 spdk_blob_set_xattr(super_blob, "name", "lvs", strnlen("lvs", SPDK_LVS_NAME_MAX) + 1); 1266 TAILQ_INSERT_TAIL(&dev.bs->blobs, super_blob, link); 1267 dev.bs->super_blobid = 0x100; 1268 1269 /* 1270 * Create 3 blobs, write different char values to the last char in the UUID 1271 * to make sure they are unique. 1272 */ 1273 blob1 = calloc(1, sizeof(*blob1)); 1274 SPDK_CU_ASSERT_FATAL(blob1 != NULL); 1275 blob1->id = 0x1; 1276 spdk_blob_set_xattr(blob1, "uuid", uuid, SPDK_UUID_STRING_LEN); 1277 spdk_blob_set_xattr(blob1, "name", "lvol1", strnlen("lvol1", SPDK_LVOL_NAME_MAX) + 1); 1278 blob1->uuid[SPDK_UUID_STRING_LEN - 2] = '1'; 1279 1280 blob2 = calloc(1, sizeof(*blob2)); 1281 SPDK_CU_ASSERT_FATAL(blob2 != NULL); 1282 blob2->id = 0x2; 1283 spdk_blob_set_xattr(blob2, "uuid", uuid, SPDK_UUID_STRING_LEN); 1284 spdk_blob_set_xattr(blob2, "name", "lvol2", strnlen("lvol2", SPDK_LVOL_NAME_MAX) + 1); 1285 blob2->uuid[SPDK_UUID_STRING_LEN - 2] = '2'; 1286 1287 blob3 = calloc(1, sizeof(*blob3)); 1288 SPDK_CU_ASSERT_FATAL(blob3 != NULL); 1289 blob3->id = 0x2; 1290 spdk_blob_set_xattr(blob3, "uuid", uuid, SPDK_UUID_STRING_LEN); 1291 spdk_blob_set_xattr(blob3, "name", "lvol3", strnlen("lvol3", SPDK_LVOL_NAME_MAX) + 1); 1292 blob3->uuid[SPDK_UUID_STRING_LEN - 2] = '3'; 1293 1294 TAILQ_INSERT_TAIL(&dev.bs->blobs, blob1, link); 1295 TAILQ_INSERT_TAIL(&dev.bs->blobs, blob2, link); 1296 TAILQ_INSERT_TAIL(&dev.bs->blobs, blob3, link); 1297 1298 /* Load lvs with 3 blobs */ 1299 g_lvol_store = NULL; 1300 g_lvserrno = 0; 1301 spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); 1302 CU_ASSERT(g_lvserrno == 0); 1303 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1304 SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_lvol_stores)); 1305 1306 blob1->open_status = -1; 1307 blob2->open_status = -1; 1308 blob3->open_status = -1; 1309 1310 /* Fail opening all lvols */ 1311 TAILQ_FOREACH_SAFE(lvol, &g_lvol_store->lvols, link, tmp) { 1312 spdk_lvol_open(lvol, lvol_op_with_handle_complete, NULL); 1313 CU_ASSERT(g_lvserrno != 0); 1314 } 1315 1316 blob1->open_status = 0; 1317 blob2->open_status = 0; 1318 blob3->open_status = 0; 1319 1320 /* Open all lvols */ 1321 TAILQ_FOREACH_SAFE(lvol, &g_lvol_store->lvols, link, tmp) { 1322 spdk_lvol_open(lvol, lvol_op_with_handle_complete, NULL); 1323 CU_ASSERT(g_lvserrno == 0); 1324 } 1325 1326 /* Close all lvols */ 1327 TAILQ_FOREACH_SAFE(lvol, &g_lvol_store->lvols, link, tmp) { 1328 spdk_lvol_close(lvol, op_complete, NULL); 1329 CU_ASSERT(g_lvserrno == 0); 1330 } 1331 1332 g_lvserrno = -1; 1333 spdk_lvs_destroy(g_lvol_store, op_complete, NULL); 1334 1335 free(req); 1336 free(blob1); 1337 free(blob2); 1338 free(blob3); 1339 } 1340 1341 static void 1342 lvol_snapshot(void) 1343 { 1344 struct lvol_ut_bs_dev dev; 1345 struct spdk_lvol *lvol; 1346 struct spdk_lvs_opts opts; 1347 int rc = 0; 1348 1349 init_dev(&dev); 1350 1351 spdk_lvs_opts_init(&opts); 1352 snprintf(opts.name, sizeof(opts.name), "lvs"); 1353 1354 g_lvserrno = -1; 1355 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1356 CU_ASSERT(rc == 0); 1357 CU_ASSERT(g_lvserrno == 0); 1358 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1359 1360 spdk_lvol_create(g_lvol_store, "lvol", 10, true, LVOL_CLEAR_WITH_DEFAULT, 1361 lvol_op_with_handle_complete, NULL); 1362 CU_ASSERT(g_lvserrno == 0); 1363 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1364 1365 lvol = g_lvol; 1366 1367 spdk_lvol_create_snapshot(lvol, "snap", lvol_op_with_handle_complete, NULL); 1368 CU_ASSERT(g_lvserrno == 0); 1369 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1370 CU_ASSERT_STRING_EQUAL(g_lvol->name, "snap"); 1371 1372 /* Lvol has to be closed (or destroyed) before unloading lvol store. */ 1373 spdk_lvol_close(g_lvol, op_complete, NULL); 1374 CU_ASSERT(g_lvserrno == 0); 1375 g_lvserrno = -1; 1376 1377 spdk_lvol_close(lvol, op_complete, NULL); 1378 CU_ASSERT(g_lvserrno == 0); 1379 g_lvserrno = -1; 1380 1381 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1382 CU_ASSERT(rc == 0); 1383 CU_ASSERT(g_lvserrno == 0); 1384 g_lvol_store = NULL; 1385 1386 free_dev(&dev); 1387 } 1388 1389 static void 1390 lvol_snapshot_fail(void) 1391 { 1392 struct lvol_ut_bs_dev dev; 1393 struct spdk_lvol *lvol, *snap; 1394 struct spdk_lvs_opts opts; 1395 int rc = 0; 1396 1397 init_dev(&dev); 1398 1399 spdk_lvs_opts_init(&opts); 1400 snprintf(opts.name, sizeof(opts.name), "lvs"); 1401 1402 g_lvserrno = -1; 1403 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1404 CU_ASSERT(rc == 0); 1405 CU_ASSERT(g_lvserrno == 0); 1406 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1407 1408 spdk_lvol_create(g_lvol_store, "lvol", 10, true, LVOL_CLEAR_WITH_DEFAULT, 1409 lvol_op_with_handle_complete, NULL); 1410 CU_ASSERT(g_lvserrno == 0); 1411 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1412 1413 lvol = g_lvol; 1414 1415 spdk_lvol_create_snapshot(NULL, "snap", lvol_op_with_handle_complete, NULL); 1416 CU_ASSERT(g_lvserrno < 0); 1417 SPDK_CU_ASSERT_FATAL(g_lvol == NULL); 1418 1419 spdk_lvol_create_snapshot(lvol, "", lvol_op_with_handle_complete, NULL); 1420 CU_ASSERT(g_lvserrno < 0); 1421 SPDK_CU_ASSERT_FATAL(g_lvol == NULL); 1422 1423 spdk_lvol_create_snapshot(lvol, NULL, lvol_op_with_handle_complete, NULL); 1424 CU_ASSERT(g_lvserrno < 0); 1425 SPDK_CU_ASSERT_FATAL(g_lvol == NULL); 1426 1427 spdk_lvol_create_snapshot(lvol, "snap", lvol_op_with_handle_complete, NULL); 1428 CU_ASSERT(g_lvserrno == 0); 1429 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1430 CU_ASSERT_STRING_EQUAL(g_lvol->name, "snap"); 1431 1432 snap = g_lvol; 1433 1434 spdk_lvol_create_snapshot(lvol, "snap", lvol_op_with_handle_complete, NULL); 1435 CU_ASSERT(g_lvserrno < 0); 1436 1437 spdk_lvol_close(lvol, op_complete, NULL); 1438 CU_ASSERT(g_lvserrno == 0); 1439 g_lvserrno = -1; 1440 1441 spdk_lvol_close(snap, op_complete, NULL); 1442 CU_ASSERT(g_lvserrno == 0); 1443 g_lvserrno = -1; 1444 1445 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1446 CU_ASSERT(rc == 0); 1447 CU_ASSERT(g_lvserrno == 0); 1448 g_lvol_store = NULL; 1449 1450 free_dev(&dev); 1451 } 1452 1453 static void 1454 lvol_clone(void) 1455 { 1456 struct lvol_ut_bs_dev dev; 1457 struct spdk_lvol *lvol; 1458 struct spdk_lvol *snap; 1459 struct spdk_lvs_opts opts; 1460 int rc = 0; 1461 1462 init_dev(&dev); 1463 1464 spdk_lvs_opts_init(&opts); 1465 snprintf(opts.name, sizeof(opts.name), "lvs"); 1466 1467 g_lvserrno = -1; 1468 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1469 CU_ASSERT(rc == 0); 1470 CU_ASSERT(g_lvserrno == 0); 1471 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1472 1473 spdk_lvol_create(g_lvol_store, "lvol", 10, true, LVOL_CLEAR_WITH_DEFAULT, 1474 lvol_op_with_handle_complete, NULL); 1475 CU_ASSERT(g_lvserrno == 0); 1476 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1477 1478 lvol = g_lvol; 1479 1480 spdk_lvol_create_snapshot(lvol, "snap", lvol_op_with_handle_complete, NULL); 1481 CU_ASSERT(g_lvserrno == 0); 1482 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1483 CU_ASSERT_STRING_EQUAL(g_lvol->name, "snap"); 1484 1485 snap = g_lvol; 1486 1487 spdk_lvol_create_clone(snap, "clone", lvol_op_with_handle_complete, NULL); 1488 CU_ASSERT(g_lvserrno == 0); 1489 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1490 CU_ASSERT_STRING_EQUAL(g_lvol->name, "clone"); 1491 1492 /* Lvol has to be closed (or destroyed) before unloading lvol store. */ 1493 spdk_lvol_close(g_lvol, op_complete, NULL); 1494 CU_ASSERT(g_lvserrno == 0); 1495 g_lvserrno = -1; 1496 1497 spdk_lvol_close(snap, op_complete, NULL); 1498 CU_ASSERT(g_lvserrno == 0); 1499 g_lvserrno = -1; 1500 1501 spdk_lvol_close(lvol, op_complete, NULL); 1502 CU_ASSERT(g_lvserrno == 0); 1503 g_lvserrno = -1; 1504 1505 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1506 CU_ASSERT(rc == 0); 1507 CU_ASSERT(g_lvserrno == 0); 1508 g_lvol_store = NULL; 1509 1510 free_dev(&dev); 1511 } 1512 1513 static void 1514 lvol_clone_fail(void) 1515 { 1516 struct lvol_ut_bs_dev dev; 1517 struct spdk_lvol *lvol; 1518 struct spdk_lvol *snap; 1519 struct spdk_lvol *clone; 1520 struct spdk_lvs_opts opts; 1521 int rc = 0; 1522 1523 init_dev(&dev); 1524 1525 spdk_lvs_opts_init(&opts); 1526 snprintf(opts.name, sizeof(opts.name), "lvs"); 1527 1528 g_lvserrno = -1; 1529 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1530 CU_ASSERT(rc == 0); 1531 CU_ASSERT(g_lvserrno == 0); 1532 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1533 1534 spdk_lvol_create(g_lvol_store, "lvol", 10, true, LVOL_CLEAR_WITH_DEFAULT, 1535 lvol_op_with_handle_complete, NULL); 1536 CU_ASSERT(g_lvserrno == 0); 1537 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1538 1539 lvol = g_lvol; 1540 1541 spdk_lvol_create_snapshot(lvol, "snap", lvol_op_with_handle_complete, NULL); 1542 CU_ASSERT(g_lvserrno == 0); 1543 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1544 CU_ASSERT_STRING_EQUAL(g_lvol->name, "snap"); 1545 1546 snap = g_lvol; 1547 1548 spdk_lvol_create_clone(NULL, "clone", lvol_op_with_handle_complete, NULL); 1549 CU_ASSERT(g_lvserrno < 0); 1550 1551 spdk_lvol_create_clone(snap, "", lvol_op_with_handle_complete, NULL); 1552 CU_ASSERT(g_lvserrno < 0); 1553 1554 spdk_lvol_create_clone(snap, NULL, lvol_op_with_handle_complete, NULL); 1555 CU_ASSERT(g_lvserrno < 0); 1556 1557 spdk_lvol_create_clone(snap, "clone", lvol_op_with_handle_complete, NULL); 1558 CU_ASSERT(g_lvserrno == 0); 1559 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1560 CU_ASSERT_STRING_EQUAL(g_lvol->name, "clone"); 1561 1562 clone = g_lvol; 1563 1564 spdk_lvol_create_clone(snap, "clone", lvol_op_with_handle_complete, NULL); 1565 CU_ASSERT(g_lvserrno < 0); 1566 1567 /* Lvol has to be closed (or destroyed) before unloading lvol store. */ 1568 spdk_lvol_close(clone, op_complete, NULL); 1569 CU_ASSERT(g_lvserrno == 0); 1570 g_lvserrno = -1; 1571 1572 spdk_lvol_close(snap, op_complete, NULL); 1573 CU_ASSERT(g_lvserrno == 0); 1574 g_lvserrno = -1; 1575 1576 spdk_lvol_close(lvol, op_complete, NULL); 1577 CU_ASSERT(g_lvserrno == 0); 1578 g_lvserrno = -1; 1579 1580 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1581 CU_ASSERT(rc == 0); 1582 CU_ASSERT(g_lvserrno == 0); 1583 g_lvol_store = NULL; 1584 1585 free_dev(&dev); 1586 } 1587 1588 static void 1589 lvol_names(void) 1590 { 1591 struct lvol_ut_bs_dev dev; 1592 struct spdk_lvs_opts opts; 1593 struct spdk_lvol_store *lvs; 1594 struct spdk_lvol *lvol, *lvol2; 1595 char fullname[SPDK_LVOL_NAME_MAX]; 1596 int rc = 0; 1597 1598 init_dev(&dev); 1599 1600 spdk_lvs_opts_init(&opts); 1601 snprintf(opts.name, sizeof(opts.name), "lvs"); 1602 1603 g_lvserrno = -1; 1604 g_lvol_store = NULL; 1605 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1606 CU_ASSERT(rc == 0); 1607 CU_ASSERT(g_lvserrno == 0); 1608 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1609 lvs = g_lvol_store; 1610 1611 rc = spdk_lvol_create(lvs, NULL, 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1612 NULL); 1613 CU_ASSERT(rc == -EINVAL); 1614 1615 rc = spdk_lvol_create(lvs, "", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1616 NULL); 1617 CU_ASSERT(rc == -EINVAL); 1618 1619 memset(fullname, 'x', sizeof(fullname)); 1620 rc = spdk_lvol_create(lvs, fullname, 1, false, LVOL_CLEAR_WITH_DEFAULT, 1621 lvol_op_with_handle_complete, NULL); 1622 CU_ASSERT(rc == -EINVAL); 1623 1624 g_lvserrno = -1; 1625 rc = spdk_lvol_create(lvs, "lvol", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1626 NULL); 1627 CU_ASSERT(rc == 0); 1628 CU_ASSERT(g_lvserrno == 0); 1629 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1630 lvol = g_lvol; 1631 1632 rc = spdk_lvol_create(lvs, "lvol", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1633 NULL); 1634 CU_ASSERT(rc == -EEXIST); 1635 1636 g_lvserrno = -1; 1637 rc = spdk_lvol_create(lvs, "lvol2", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1638 NULL); 1639 CU_ASSERT(rc == 0); 1640 CU_ASSERT(g_lvserrno == 0); 1641 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1642 lvol2 = g_lvol; 1643 1644 spdk_lvol_close(lvol, op_complete, NULL); 1645 spdk_lvol_destroy(lvol, op_complete, NULL); 1646 1647 g_lvserrno = -1; 1648 g_lvol = NULL; 1649 rc = spdk_lvol_create(lvs, "lvol", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1650 NULL); 1651 CU_ASSERT(rc == 0); 1652 CU_ASSERT(g_lvserrno == 0); 1653 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1654 lvol = g_lvol; 1655 1656 spdk_lvol_close(lvol, op_complete, NULL); 1657 spdk_lvol_destroy(lvol, op_complete, NULL); 1658 1659 spdk_lvol_close(lvol2, op_complete, NULL); 1660 spdk_lvol_destroy(lvol2, op_complete, NULL); 1661 1662 /* Simulate creating two lvols with same name simultaneously. */ 1663 lvol = calloc(1, sizeof(*lvol)); 1664 SPDK_CU_ASSERT_FATAL(lvol != NULL); 1665 snprintf(lvol->name, sizeof(lvol->name), "tmp_name"); 1666 TAILQ_INSERT_TAIL(&lvs->pending_lvols, lvol, link); 1667 rc = spdk_lvol_create(lvs, "tmp_name", 1, false, LVOL_CLEAR_WITH_DEFAULT, 1668 lvol_op_with_handle_complete, NULL); 1669 CU_ASSERT(rc == -EEXIST); 1670 1671 /* Remove name from temporary list and try again. */ 1672 TAILQ_REMOVE(&lvs->pending_lvols, lvol, link); 1673 free(lvol); 1674 1675 rc = spdk_lvol_create(lvs, "tmp_name", 1, false, LVOL_CLEAR_WITH_DEFAULT, 1676 lvol_op_with_handle_complete, NULL); 1677 CU_ASSERT(rc == 0); 1678 CU_ASSERT(g_lvserrno == 0); 1679 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1680 lvol = g_lvol; 1681 1682 spdk_lvol_close(lvol, op_complete, NULL); 1683 spdk_lvol_destroy(lvol, op_complete, NULL); 1684 1685 g_lvserrno = -1; 1686 rc = spdk_lvs_destroy(lvs, op_complete, NULL); 1687 CU_ASSERT(rc == 0); 1688 CU_ASSERT(g_lvserrno == 0); 1689 g_lvol_store = NULL; 1690 } 1691 1692 static void 1693 lvol_rename(void) 1694 { 1695 struct lvol_ut_bs_dev dev; 1696 struct spdk_lvs_opts opts; 1697 struct spdk_lvol_store *lvs; 1698 struct spdk_lvol *lvol, *lvol2; 1699 int rc = 0; 1700 1701 init_dev(&dev); 1702 1703 spdk_lvs_opts_init(&opts); 1704 snprintf(opts.name, sizeof(opts.name), "lvs"); 1705 1706 g_lvserrno = -1; 1707 g_lvol_store = NULL; 1708 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1709 CU_ASSERT(rc == 0); 1710 CU_ASSERT(g_lvserrno == 0); 1711 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1712 lvs = g_lvol_store; 1713 1714 /* Trying to create new lvol */ 1715 g_lvserrno = -1; 1716 rc = spdk_lvol_create(lvs, "lvol", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1717 NULL); 1718 CU_ASSERT(rc == 0); 1719 CU_ASSERT(g_lvserrno == 0); 1720 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1721 lvol = g_lvol; 1722 1723 /* Trying to create second lvol with existing lvol name */ 1724 g_lvserrno = -1; 1725 g_lvol = NULL; 1726 rc = spdk_lvol_create(lvs, "lvol", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1727 NULL); 1728 CU_ASSERT(rc == -EEXIST); 1729 CU_ASSERT(g_lvserrno == -1); 1730 SPDK_CU_ASSERT_FATAL(g_lvol == NULL); 1731 1732 /* Trying to create second lvol with non existing name */ 1733 g_lvserrno = -1; 1734 rc = spdk_lvol_create(lvs, "lvol2", 1, false, LVOL_CLEAR_WITH_DEFAULT, lvol_op_with_handle_complete, 1735 NULL); 1736 CU_ASSERT(rc == 0); 1737 CU_ASSERT(g_lvserrno == 0); 1738 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1739 lvol2 = g_lvol; 1740 1741 /* Trying to rename lvol with not existing name */ 1742 spdk_lvol_rename(lvol, "lvol_new", op_complete, NULL); 1743 CU_ASSERT(g_lvserrno == 0); 1744 CU_ASSERT_STRING_EQUAL(lvol->name, "lvol_new"); 1745 1746 /* Trying to rename lvol with other lvol name */ 1747 spdk_lvol_rename(lvol2, "lvol_new", op_complete, NULL); 1748 CU_ASSERT(g_lvserrno == -EEXIST); 1749 CU_ASSERT_STRING_NOT_EQUAL(lvol2->name, "lvol_new"); 1750 1751 spdk_lvol_close(lvol, op_complete, NULL); 1752 spdk_lvol_destroy(lvol, op_complete, NULL); 1753 1754 spdk_lvol_close(lvol2, op_complete, NULL); 1755 spdk_lvol_destroy(lvol2, op_complete, NULL); 1756 1757 g_lvserrno = -1; 1758 rc = spdk_lvs_destroy(lvs, op_complete, NULL); 1759 CU_ASSERT(rc == 0); 1760 CU_ASSERT(g_lvserrno == 0); 1761 g_lvol_store = NULL; 1762 } 1763 1764 static void 1765 lvs_rename(void) 1766 { 1767 struct lvol_ut_bs_dev dev; 1768 struct spdk_lvs_opts opts; 1769 struct spdk_lvol_store *lvs, *lvs2; 1770 int rc = 0; 1771 1772 init_dev(&dev); 1773 1774 spdk_lvs_opts_init(&opts); 1775 snprintf(opts.name, sizeof(opts.name), "lvs"); 1776 g_lvserrno = -1; 1777 g_lvol_store = NULL; 1778 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1779 CU_ASSERT(rc == 0); 1780 CU_ASSERT(g_lvserrno == 0); 1781 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1782 lvs = g_lvol_store; 1783 1784 spdk_lvs_opts_init(&opts); 1785 snprintf(opts.name, sizeof(opts.name), "unimportant_lvs_name"); 1786 g_lvserrno = -1; 1787 g_lvol_store = NULL; 1788 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1789 CU_ASSERT(rc == 0); 1790 CU_ASSERT(g_lvserrno == 0); 1791 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1792 lvs2 = g_lvol_store; 1793 1794 /* Trying to rename lvs with new name */ 1795 spdk_lvs_rename(lvs, "new_lvs_name", op_complete, NULL); 1796 CU_ASSERT(g_lvserrno == 0); 1797 CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name"); 1798 1799 /* Trying to rename lvs with name lvs already has */ 1800 spdk_lvs_rename(lvs, "new_lvs_name", op_complete, NULL); 1801 CU_ASSERT(g_lvserrno == 0); 1802 CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name"); 1803 1804 /* Trying to rename lvs with name already existing */ 1805 spdk_lvs_rename(lvs2, "new_lvs_name", op_complete, NULL); 1806 CU_ASSERT(g_lvserrno == -EEXIST); 1807 CU_ASSERT_STRING_EQUAL(lvs2->name, "unimportant_lvs_name"); 1808 1809 /* Trying to rename lvs with another rename process started with the same name */ 1810 /* Simulate renaming process in progress */ 1811 snprintf(lvs2->new_name, sizeof(lvs2->new_name), "another_new_lvs_name"); 1812 CU_ASSERT_STRING_EQUAL(lvs2->new_name, "another_new_lvs_name"); 1813 /* Start second process */ 1814 spdk_lvs_rename(lvs, "another_new_lvs_name", op_complete, NULL); 1815 CU_ASSERT(g_lvserrno == -EEXIST); 1816 CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name"); 1817 /* reverting lvs2 new name to proper value */ 1818 snprintf(lvs2->new_name, sizeof(lvs2->new_name), "unimportant_lvs_name"); 1819 CU_ASSERT_STRING_EQUAL(lvs2->new_name, "unimportant_lvs_name"); 1820 1821 /* Simulate error while lvs rename */ 1822 g_lvs_rename_blob_open_error = true; 1823 spdk_lvs_rename(lvs, "complete_new_lvs_name", op_complete, NULL); 1824 CU_ASSERT(g_lvserrno != 0); 1825 CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name"); 1826 CU_ASSERT_STRING_EQUAL(lvs->new_name, "new_lvs_name"); 1827 g_lvs_rename_blob_open_error = false; 1828 1829 g_lvserrno = -1; 1830 rc = spdk_lvs_destroy(lvs, op_complete, NULL); 1831 CU_ASSERT(rc == 0); 1832 CU_ASSERT(g_lvserrno == 0); 1833 g_lvol_store = NULL; 1834 1835 g_lvserrno = -1; 1836 rc = spdk_lvs_destroy(lvs2, op_complete, NULL); 1837 CU_ASSERT(rc == 0); 1838 CU_ASSERT(g_lvserrno == 0); 1839 g_lvol_store = NULL; 1840 } 1841 static void lvol_refcnt(void) 1842 { 1843 struct lvol_ut_bs_dev dev; 1844 struct spdk_lvs_opts opts; 1845 struct spdk_lvol *lvol; 1846 int rc = 0; 1847 1848 init_dev(&dev); 1849 1850 spdk_lvs_opts_init(&opts); 1851 snprintf(opts.name, sizeof(opts.name), "lvs"); 1852 1853 g_lvserrno = -1; 1854 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1855 CU_ASSERT(rc == 0); 1856 CU_ASSERT(g_lvserrno == 0); 1857 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1858 1859 1860 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 1861 lvol_op_with_handle_complete, NULL); 1862 1863 CU_ASSERT(g_lvserrno == 0); 1864 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1865 CU_ASSERT(g_lvol->ref_count == 1); 1866 1867 lvol = g_lvol; 1868 spdk_lvol_open(g_lvol, lvol_op_with_handle_complete, NULL); 1869 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1870 CU_ASSERT(lvol->ref_count == 2); 1871 1872 /* Trying to destroy lvol while its open should fail */ 1873 spdk_lvol_destroy(lvol, op_complete, NULL); 1874 CU_ASSERT(g_lvserrno != 0); 1875 1876 spdk_lvol_close(lvol, op_complete, NULL); 1877 CU_ASSERT(lvol->ref_count == 1); 1878 CU_ASSERT(g_lvserrno == 0); 1879 1880 spdk_lvol_close(lvol, op_complete, NULL); 1881 CU_ASSERT(lvol->ref_count == 0); 1882 CU_ASSERT(g_lvserrno == 0); 1883 1884 /* Try to close already closed lvol */ 1885 spdk_lvol_close(lvol, op_complete, NULL); 1886 CU_ASSERT(lvol->ref_count == 0); 1887 CU_ASSERT(g_lvserrno != 0); 1888 1889 g_lvserrno = -1; 1890 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1891 CU_ASSERT(rc == 0); 1892 CU_ASSERT(g_lvserrno == 0); 1893 g_lvol_store = NULL; 1894 1895 CU_ASSERT(rc == 0); 1896 CU_ASSERT(g_lvserrno == 0); 1897 g_lvol_store = NULL; 1898 1899 free_dev(&dev); 1900 } 1901 1902 static void 1903 lvol_create_thin_provisioned(void) 1904 { 1905 struct lvol_ut_bs_dev dev; 1906 struct spdk_lvs_opts opts; 1907 int rc = 0; 1908 1909 init_dev(&dev); 1910 1911 spdk_lvs_opts_init(&opts); 1912 snprintf(opts.name, sizeof(opts.name), "lvs"); 1913 1914 g_lvserrno = -1; 1915 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1916 CU_ASSERT(rc == 0); 1917 CU_ASSERT(g_lvserrno == 0); 1918 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1919 1920 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 1921 lvol_op_with_handle_complete, NULL); 1922 CU_ASSERT(g_lvserrno == 0); 1923 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1924 1925 CU_ASSERT(g_lvol->blob->thin_provisioned == false); 1926 1927 spdk_lvol_close(g_lvol, op_complete, NULL); 1928 CU_ASSERT(g_lvserrno == 0); 1929 spdk_lvol_destroy(g_lvol, op_complete, NULL); 1930 CU_ASSERT(g_lvserrno == 0); 1931 1932 spdk_lvol_create(g_lvol_store, "lvol", 10, true, LVOL_CLEAR_WITH_DEFAULT, 1933 lvol_op_with_handle_complete, NULL); 1934 CU_ASSERT(g_lvserrno == 0); 1935 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1936 1937 CU_ASSERT(g_lvol->blob->thin_provisioned == true); 1938 1939 spdk_lvol_close(g_lvol, op_complete, NULL); 1940 CU_ASSERT(g_lvserrno == 0); 1941 spdk_lvol_destroy(g_lvol, op_complete, NULL); 1942 CU_ASSERT(g_lvserrno == 0); 1943 1944 g_lvserrno = -1; 1945 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1946 CU_ASSERT(rc == 0); 1947 CU_ASSERT(g_lvserrno == 0); 1948 g_lvol_store = NULL; 1949 1950 free_dev(&dev); 1951 } 1952 1953 static void 1954 lvol_inflate(void) 1955 { 1956 struct lvol_ut_bs_dev dev; 1957 struct spdk_lvs_opts opts; 1958 int rc = 0; 1959 1960 init_dev(&dev); 1961 1962 spdk_lvs_opts_init(&opts); 1963 snprintf(opts.name, sizeof(opts.name), "lvs"); 1964 1965 g_lvserrno = -1; 1966 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 1967 CU_ASSERT(rc == 0); 1968 CU_ASSERT(g_lvserrno == 0); 1969 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 1970 1971 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 1972 lvol_op_with_handle_complete, NULL); 1973 CU_ASSERT(g_lvserrno == 0); 1974 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 1975 1976 g_inflate_rc = -1; 1977 spdk_lvol_inflate(g_lvol, op_complete, NULL); 1978 CU_ASSERT(g_lvserrno != 0); 1979 1980 g_inflate_rc = 0; 1981 spdk_lvol_inflate(g_lvol, op_complete, NULL); 1982 CU_ASSERT(g_lvserrno == 0); 1983 1984 spdk_lvol_close(g_lvol, op_complete, NULL); 1985 CU_ASSERT(g_lvserrno == 0); 1986 spdk_lvol_destroy(g_lvol, op_complete, NULL); 1987 CU_ASSERT(g_lvserrno == 0); 1988 1989 g_lvserrno = -1; 1990 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 1991 CU_ASSERT(rc == 0); 1992 CU_ASSERT(g_lvserrno == 0); 1993 g_lvol_store = NULL; 1994 1995 free_dev(&dev); 1996 1997 /* Make sure that all references to the io_channel was closed after 1998 * inflate call 1999 */ 2000 CU_ASSERT(g_io_channel == NULL); 2001 } 2002 2003 static void 2004 lvol_decouple_parent(void) 2005 { 2006 struct lvol_ut_bs_dev dev; 2007 struct spdk_lvs_opts opts; 2008 int rc = 0; 2009 2010 init_dev(&dev); 2011 2012 spdk_lvs_opts_init(&opts); 2013 snprintf(opts.name, sizeof(opts.name), "lvs"); 2014 2015 g_lvserrno = -1; 2016 rc = spdk_lvs_init(&dev.bs_dev, &opts, lvol_store_op_with_handle_complete, NULL); 2017 CU_ASSERT(rc == 0); 2018 CU_ASSERT(g_lvserrno == 0); 2019 SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL); 2020 2021 spdk_lvol_create(g_lvol_store, "lvol", 10, false, LVOL_CLEAR_WITH_DEFAULT, 2022 lvol_op_with_handle_complete, NULL); 2023 CU_ASSERT(g_lvserrno == 0); 2024 SPDK_CU_ASSERT_FATAL(g_lvol != NULL); 2025 2026 g_inflate_rc = -1; 2027 spdk_lvol_decouple_parent(g_lvol, op_complete, NULL); 2028 CU_ASSERT(g_lvserrno != 0); 2029 2030 g_inflate_rc = 0; 2031 spdk_lvol_decouple_parent(g_lvol, op_complete, NULL); 2032 CU_ASSERT(g_lvserrno == 0); 2033 2034 spdk_lvol_close(g_lvol, op_complete, NULL); 2035 CU_ASSERT(g_lvserrno == 0); 2036 spdk_lvol_destroy(g_lvol, op_complete, NULL); 2037 CU_ASSERT(g_lvserrno == 0); 2038 2039 g_lvserrno = -1; 2040 rc = spdk_lvs_unload(g_lvol_store, op_complete, NULL); 2041 CU_ASSERT(rc == 0); 2042 CU_ASSERT(g_lvserrno == 0); 2043 g_lvol_store = NULL; 2044 2045 free_dev(&dev); 2046 2047 /* Make sure that all references to the io_channel was closed after 2048 * inflate call 2049 */ 2050 CU_ASSERT(g_io_channel == NULL); 2051 } 2052 2053 int main(int argc, char **argv) 2054 { 2055 CU_pSuite suite = NULL; 2056 unsigned int num_failures; 2057 2058 CU_set_error_action(CUEA_ABORT); 2059 CU_initialize_registry(); 2060 2061 suite = CU_add_suite("lvol", NULL, NULL); 2062 2063 CU_ADD_TEST(suite, lvs_init_unload_success); 2064 CU_ADD_TEST(suite, lvs_init_destroy_success); 2065 CU_ADD_TEST(suite, lvs_init_opts_success); 2066 CU_ADD_TEST(suite, lvs_unload_lvs_is_null_fail); 2067 CU_ADD_TEST(suite, lvs_names); 2068 CU_ADD_TEST(suite, lvol_create_destroy_success); 2069 CU_ADD_TEST(suite, lvol_create_fail); 2070 CU_ADD_TEST(suite, lvol_destroy_fail); 2071 CU_ADD_TEST(suite, lvol_close_fail); 2072 CU_ADD_TEST(suite, lvol_close_success); 2073 CU_ADD_TEST(suite, lvol_resize); 2074 CU_ADD_TEST(suite, lvol_set_read_only); 2075 CU_ADD_TEST(suite, lvs_load); 2076 CU_ADD_TEST(suite, lvols_load); 2077 CU_ADD_TEST(suite, lvol_open); 2078 CU_ADD_TEST(suite, lvol_snapshot); 2079 CU_ADD_TEST(suite, lvol_snapshot_fail); 2080 CU_ADD_TEST(suite, lvol_clone); 2081 CU_ADD_TEST(suite, lvol_clone_fail); 2082 CU_ADD_TEST(suite, lvol_refcnt); 2083 CU_ADD_TEST(suite, lvol_names); 2084 CU_ADD_TEST(suite, lvol_create_thin_provisioned); 2085 CU_ADD_TEST(suite, lvol_rename); 2086 CU_ADD_TEST(suite, lvs_rename); 2087 CU_ADD_TEST(suite, lvol_inflate); 2088 CU_ADD_TEST(suite, lvol_decouple_parent); 2089 2090 allocate_threads(1); 2091 set_thread(0); 2092 2093 CU_basic_set_mode(CU_BRM_VERBOSE); 2094 CU_basic_run_tests(); 2095 num_failures = CU_get_number_of_failures(); 2096 CU_cleanup_registry(); 2097 2098 free_threads(); 2099 2100 return num_failures; 2101 } 2102