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/stdinc.h" 35 36 #include "spdk_cunit.h" 37 #include "spdk/blob.h" 38 #include "spdk/string.h" 39 40 #include "common/lib/test_env.c" 41 #include "../bs_dev_common.c" 42 #include "blob/blobstore.c" 43 #include "blob/request.c" 44 #include "blob/zeroes.c" 45 #include "blob/blob_bs_dev.c" 46 47 struct spdk_blob_store *g_bs; 48 spdk_blob_id g_blobid; 49 struct spdk_blob *g_blob; 50 int g_bserrno; 51 struct spdk_xattr_names *g_names; 52 int g_done; 53 char *g_xattr_names[] = {"first", "second", "third"}; 54 char *g_xattr_values[] = {"one", "two", "three"}; 55 uint64_t g_ctx = 1729; 56 57 struct spdk_bs_super_block_ver1 { 58 uint8_t signature[8]; 59 uint32_t version; 60 uint32_t length; 61 uint32_t clean; /* If there was a clean shutdown, this is 1. */ 62 spdk_blob_id super_blob; 63 64 uint32_t cluster_size; /* In bytes */ 65 66 uint32_t used_page_mask_start; /* Offset from beginning of disk, in pages */ 67 uint32_t used_page_mask_len; /* Count, in pages */ 68 69 uint32_t used_cluster_mask_start; /* Offset from beginning of disk, in pages */ 70 uint32_t used_cluster_mask_len; /* Count, in pages */ 71 72 uint32_t md_start; /* Offset from beginning of disk, in pages */ 73 uint32_t md_len; /* Count, in pages */ 74 75 uint8_t reserved[4036]; 76 uint32_t crc; 77 } __attribute__((packed)); 78 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block_ver1) == 0x1000, "Invalid super block size"); 79 80 81 static void 82 _get_xattr_value(void *arg, const char *name, 83 const void **value, size_t *value_len) 84 { 85 uint64_t i; 86 87 SPDK_CU_ASSERT_FATAL(value_len != NULL); 88 SPDK_CU_ASSERT_FATAL(value != NULL); 89 CU_ASSERT(arg == &g_ctx) 90 91 for (i = 0; i < sizeof(g_xattr_names); i++) { 92 if (!strcmp(name, g_xattr_names[i])) { 93 *value_len = strlen(g_xattr_values[i]); 94 *value = g_xattr_values[i]; 95 break; 96 } 97 } 98 } 99 100 static void 101 _get_xattr_value_null(void *arg, const char *name, 102 const void **value, size_t *value_len) 103 { 104 SPDK_CU_ASSERT_FATAL(value_len != NULL); 105 SPDK_CU_ASSERT_FATAL(value != NULL); 106 CU_ASSERT(arg == NULL) 107 108 *value_len = 0; 109 *value = NULL; 110 } 111 112 113 114 static void 115 bs_op_complete(void *cb_arg, int bserrno) 116 { 117 g_bserrno = bserrno; 118 } 119 120 static void 121 bs_op_with_handle_complete(void *cb_arg, struct spdk_blob_store *bs, 122 int bserrno) 123 { 124 g_bs = bs; 125 g_bserrno = bserrno; 126 } 127 128 static void 129 blob_op_complete(void *cb_arg, int bserrno) 130 { 131 g_bserrno = bserrno; 132 } 133 134 static void 135 blob_op_with_id_complete(void *cb_arg, spdk_blob_id blobid, int bserrno) 136 { 137 g_blobid = blobid; 138 g_bserrno = bserrno; 139 } 140 141 static void 142 blob_op_with_handle_complete(void *cb_arg, struct spdk_blob *blb, int bserrno) 143 { 144 g_blob = blb; 145 g_bserrno = bserrno; 146 } 147 148 static void 149 blob_init(void) 150 { 151 struct spdk_bs_dev *dev; 152 153 dev = init_dev(); 154 155 /* should fail for an unsupported blocklen */ 156 dev->blocklen = 500; 157 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 158 CU_ASSERT(g_bserrno == -EINVAL); 159 160 dev = init_dev(); 161 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 162 CU_ASSERT(g_bserrno == 0); 163 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 164 165 spdk_bs_unload(g_bs, bs_op_complete, NULL); 166 CU_ASSERT(g_bserrno == 0); 167 g_bs = NULL; 168 } 169 170 static void 171 blob_super(void) 172 { 173 struct spdk_blob_store *bs; 174 struct spdk_bs_dev *dev; 175 spdk_blob_id blobid; 176 177 dev = init_dev(); 178 179 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 180 CU_ASSERT(g_bserrno == 0); 181 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 182 bs = g_bs; 183 184 /* Get the super blob without having set one */ 185 spdk_bs_get_super(bs, blob_op_with_id_complete, NULL); 186 CU_ASSERT(g_bserrno == -ENOENT); 187 CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID); 188 189 /* Create a blob */ 190 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 191 CU_ASSERT(g_bserrno == 0); 192 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 193 blobid = g_blobid; 194 195 /* Set the blob as the super blob */ 196 spdk_bs_set_super(bs, blobid, blob_op_complete, NULL); 197 CU_ASSERT(g_bserrno == 0); 198 199 /* Get the super blob */ 200 spdk_bs_get_super(bs, blob_op_with_id_complete, NULL); 201 CU_ASSERT(g_bserrno == 0); 202 CU_ASSERT(blobid == g_blobid); 203 204 spdk_bs_unload(g_bs, bs_op_complete, NULL); 205 CU_ASSERT(g_bserrno == 0); 206 g_bs = NULL; 207 } 208 209 static void 210 blob_open(void) 211 { 212 struct spdk_blob_store *bs; 213 struct spdk_bs_dev *dev; 214 struct spdk_blob *blob; 215 spdk_blob_id blobid, blobid2; 216 217 dev = init_dev(); 218 219 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 220 CU_ASSERT(g_bserrno == 0); 221 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 222 bs = g_bs; 223 224 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 225 CU_ASSERT(g_bserrno == 0); 226 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 227 blobid = g_blobid; 228 229 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 230 CU_ASSERT(g_bserrno == 0); 231 CU_ASSERT(g_blob != NULL); 232 blob = g_blob; 233 234 blobid2 = spdk_blob_get_id(blob); 235 CU_ASSERT(blobid == blobid2); 236 237 /* Try to open file again. It should return success. */ 238 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 239 CU_ASSERT(g_bserrno == 0); 240 CU_ASSERT(blob == g_blob); 241 242 spdk_blob_close(blob, blob_op_complete, NULL); 243 CU_ASSERT(g_bserrno == 0); 244 245 /* 246 * Close the file a second time, releasing the second reference. This 247 * should succeed. 248 */ 249 blob = g_blob; 250 spdk_blob_close(blob, blob_op_complete, NULL); 251 CU_ASSERT(g_bserrno == 0); 252 253 /* 254 * Try to open file again. It should succeed. This tests the case 255 * where the file is opened, closed, then re-opened again. 256 */ 257 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 258 CU_ASSERT(g_bserrno == 0); 259 CU_ASSERT(g_blob != NULL); 260 blob = g_blob; 261 262 spdk_blob_close(blob, blob_op_complete, NULL); 263 CU_ASSERT(g_bserrno == 0); 264 265 spdk_bs_unload(g_bs, bs_op_complete, NULL); 266 CU_ASSERT(g_bserrno == 0); 267 g_bs = NULL; 268 } 269 270 static void 271 blob_create(void) 272 { 273 struct spdk_blob_store *bs; 274 struct spdk_bs_dev *dev; 275 struct spdk_blob *blob; 276 struct spdk_blob_opts opts; 277 spdk_blob_id blobid; 278 279 dev = init_dev(); 280 281 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 282 CU_ASSERT(g_bserrno == 0); 283 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 284 bs = g_bs; 285 286 /* Create blob with 10 clusters */ 287 288 spdk_blob_opts_init(&opts); 289 opts.num_clusters = 10; 290 291 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 292 CU_ASSERT(g_bserrno == 0); 293 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 294 blobid = g_blobid; 295 296 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 297 CU_ASSERT(g_bserrno == 0); 298 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 299 blob = g_blob; 300 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 301 302 spdk_blob_close(blob, blob_op_complete, NULL); 303 CU_ASSERT(g_bserrno == 0); 304 305 /* Create blob with 0 clusters */ 306 307 spdk_blob_opts_init(&opts); 308 opts.num_clusters = 0; 309 310 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 311 CU_ASSERT(g_bserrno == 0); 312 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 313 blobid = g_blobid; 314 315 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 316 CU_ASSERT(g_bserrno == 0); 317 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 318 blob = g_blob; 319 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0) 320 321 spdk_blob_close(blob, blob_op_complete, NULL); 322 CU_ASSERT(g_bserrno == 0); 323 324 /* Create blob with default options (opts == NULL) */ 325 326 spdk_bs_create_blob_ext(bs, NULL, blob_op_with_id_complete, NULL); 327 CU_ASSERT(g_bserrno == 0); 328 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 329 blobid = g_blobid; 330 331 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 332 CU_ASSERT(g_bserrno == 0); 333 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 334 blob = g_blob; 335 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0) 336 337 spdk_blob_close(blob, blob_op_complete, NULL); 338 CU_ASSERT(g_bserrno == 0); 339 340 /* Try to create blob with size larger than blobstore */ 341 342 spdk_blob_opts_init(&opts); 343 opts.num_clusters = bs->total_clusters + 1; 344 345 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 346 CU_ASSERT(g_bserrno == -ENOSPC); 347 348 spdk_bs_unload(g_bs, bs_op_complete, NULL); 349 CU_ASSERT(g_bserrno == 0); 350 g_bs = NULL; 351 352 } 353 354 static void 355 blob_create_internal(void) 356 { 357 struct spdk_blob_store *bs; 358 struct spdk_bs_dev *dev; 359 struct spdk_blob *blob; 360 struct spdk_blob_opts opts; 361 struct spdk_blob_xattr_opts internal_xattrs; 362 const void *value; 363 size_t value_len; 364 spdk_blob_id blobid; 365 int rc; 366 367 dev = init_dev(); 368 369 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 370 CU_ASSERT(g_bserrno == 0); 371 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 372 bs = g_bs; 373 374 /* Create blob with custom xattrs */ 375 376 spdk_blob_opts_init(&opts); 377 _spdk_blob_xattrs_init(&internal_xattrs); 378 internal_xattrs.count = 3; 379 internal_xattrs.names = g_xattr_names; 380 internal_xattrs.get_value = _get_xattr_value; 381 internal_xattrs.ctx = &g_ctx; 382 383 _spdk_bs_create_blob(bs, &opts, &internal_xattrs, blob_op_with_id_complete, NULL); 384 CU_ASSERT(g_bserrno == 0); 385 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 386 blobid = g_blobid; 387 388 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 389 CU_ASSERT(g_bserrno == 0); 390 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 391 blob = g_blob; 392 393 rc = _spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len, true); 394 CU_ASSERT(rc == 0); 395 SPDK_CU_ASSERT_FATAL(value != NULL); 396 CU_ASSERT(value_len == strlen(g_xattr_values[0])); 397 CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len); 398 399 rc = _spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len, true); 400 CU_ASSERT(rc == 0); 401 SPDK_CU_ASSERT_FATAL(value != NULL); 402 CU_ASSERT(value_len == strlen(g_xattr_values[1])); 403 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len); 404 405 rc = _spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len, true); 406 CU_ASSERT(rc == 0); 407 SPDK_CU_ASSERT_FATAL(value != NULL); 408 CU_ASSERT(value_len == strlen(g_xattr_values[2])); 409 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len); 410 411 rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len); 412 CU_ASSERT(rc != 0); 413 414 rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len); 415 CU_ASSERT(rc != 0); 416 417 rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len); 418 CU_ASSERT(rc != 0); 419 420 spdk_blob_close(blob, blob_op_complete, NULL); 421 CU_ASSERT(g_bserrno == 0); 422 423 /* Create blob with NULL internal options */ 424 425 _spdk_bs_create_blob(bs, NULL, NULL, blob_op_with_id_complete, NULL); 426 CU_ASSERT(g_bserrno == 0); 427 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 428 blobid = g_blobid; 429 430 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 431 CU_ASSERT(g_bserrno == 0); 432 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 433 CU_ASSERT(TAILQ_FIRST(&g_blob->xattrs_internal) == NULL); 434 435 blob = g_blob; 436 437 spdk_blob_close(blob, blob_op_complete, NULL); 438 CU_ASSERT(g_bserrno == 0); 439 440 spdk_bs_unload(g_bs, bs_op_complete, NULL); 441 CU_ASSERT(g_bserrno == 0); 442 g_bs = NULL; 443 444 } 445 446 static void 447 blob_thin_provision(void) 448 { 449 struct spdk_blob_store *bs; 450 struct spdk_bs_dev *dev; 451 struct spdk_blob *blob; 452 struct spdk_blob_opts opts; 453 struct spdk_bs_opts bs_opts; 454 spdk_blob_id blobid; 455 456 dev = init_dev(); 457 spdk_bs_opts_init(&bs_opts); 458 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE"); 459 460 /* Initialize a new blob store */ 461 spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL); 462 CU_ASSERT(g_bserrno == 0); 463 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 464 465 bs = g_bs; 466 467 /* Create blob with thin provisioning enabled */ 468 469 spdk_blob_opts_init(&opts); 470 opts.thin_provision = true; 471 opts.num_clusters = 10; 472 473 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 474 CU_ASSERT(g_bserrno == 0); 475 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 476 blobid = g_blobid; 477 478 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 479 CU_ASSERT(g_bserrno == 0); 480 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 481 blob = g_blob; 482 CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 483 484 spdk_blob_close(blob, blob_op_complete, NULL); 485 CU_ASSERT(g_bserrno == 0); 486 487 /* Do not shut down cleanly. This makes sure that when we load again 488 * and try to recover a valid used_cluster map, that blobstore will 489 * ignore clusters with index 0 since these are unallocated clusters. 490 */ 491 492 /* Load an existing blob store and check if invalid_flags is set */ 493 dev = init_dev(); 494 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE"); 495 spdk_bs_load(dev, &bs_opts, bs_op_with_handle_complete, NULL); 496 CU_ASSERT(g_bserrno == 0); 497 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 498 499 bs = g_bs; 500 501 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 502 CU_ASSERT(g_bserrno == 0); 503 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 504 blob = g_blob; 505 CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 506 507 spdk_blob_close(blob, blob_op_complete, NULL); 508 CU_ASSERT(g_bserrno == 0); 509 510 spdk_bs_unload(g_bs, bs_op_complete, NULL); 511 CU_ASSERT(g_bserrno == 0); 512 g_bs = NULL; 513 } 514 515 static void 516 blob_snapshot(void) 517 { 518 struct spdk_blob_store *bs; 519 struct spdk_bs_dev *dev; 520 struct spdk_blob *blob; 521 struct spdk_blob *snapshot, *snapshot2; 522 struct spdk_blob_bs_dev *blob_bs_dev; 523 struct spdk_blob_opts opts; 524 struct spdk_blob_xattr_opts xattrs; 525 spdk_blob_id blobid; 526 spdk_blob_id snapshotid; 527 const void *value; 528 size_t value_len; 529 int rc; 530 531 dev = init_dev(); 532 533 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 534 CU_ASSERT(g_bserrno == 0); 535 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 536 bs = g_bs; 537 538 /* Create blob with 10 clusters */ 539 spdk_blob_opts_init(&opts); 540 opts.num_clusters = 10; 541 542 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 543 CU_ASSERT(g_bserrno == 0); 544 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 545 blobid = g_blobid; 546 547 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 548 CU_ASSERT(g_bserrno == 0); 549 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 550 blob = g_blob; 551 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 552 553 /* Create snapshot from blob */ 554 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 555 CU_ASSERT(g_bserrno == 0); 556 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 557 snapshotid = g_blobid; 558 559 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 560 CU_ASSERT(g_bserrno == 0); 561 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 562 snapshot = g_blob; 563 CU_ASSERT(snapshot->data_ro == true) 564 CU_ASSERT(snapshot->md_ro == true) 565 CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10) 566 567 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 568 CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 569 CU_ASSERT(spdk_mem_all_zero(blob->active.clusters, 570 blob->active.num_clusters * sizeof(blob->active.clusters[0]))); 571 572 /* Try to create snapshot from clone with xattrs */ 573 xattrs.names = g_xattr_names; 574 xattrs.get_value = _get_xattr_value; 575 xattrs.count = 3; 576 xattrs.ctx = &g_ctx; 577 spdk_bs_create_snapshot(bs, blobid, &xattrs, blob_op_with_id_complete, NULL); 578 CU_ASSERT(g_bserrno == 0); 579 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 580 blobid = g_blobid; 581 582 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 583 CU_ASSERT(g_bserrno == 0); 584 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 585 snapshot2 = g_blob; 586 CU_ASSERT(snapshot2->data_ro == true) 587 CU_ASSERT(snapshot2->md_ro == true) 588 CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 10) 589 590 /* Confirm that blob is backed by snapshot2 and snapshot2 is backed by snapshot */ 591 CU_ASSERT(snapshot->back_bs_dev == NULL); 592 SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL); 593 SPDK_CU_ASSERT_FATAL(snapshot2->back_bs_dev != NULL); 594 595 blob_bs_dev = (struct spdk_blob_bs_dev *)blob->back_bs_dev; 596 CU_ASSERT(blob_bs_dev->blob == snapshot2); 597 598 blob_bs_dev = (struct spdk_blob_bs_dev *)snapshot2->back_bs_dev; 599 CU_ASSERT(blob_bs_dev->blob == snapshot); 600 601 rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[0], &value, &value_len); 602 CU_ASSERT(rc == 0); 603 SPDK_CU_ASSERT_FATAL(value != NULL); 604 CU_ASSERT(value_len == strlen(g_xattr_values[0])); 605 CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len); 606 607 rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[1], &value, &value_len); 608 CU_ASSERT(rc == 0); 609 SPDK_CU_ASSERT_FATAL(value != NULL); 610 CU_ASSERT(value_len == strlen(g_xattr_values[1])); 611 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len); 612 613 rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[2], &value, &value_len); 614 CU_ASSERT(rc == 0); 615 SPDK_CU_ASSERT_FATAL(value != NULL); 616 CU_ASSERT(value_len == strlen(g_xattr_values[2])); 617 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len); 618 619 /* Try to create snapshot from snapshot */ 620 spdk_bs_create_snapshot(bs, snapshotid, NULL, blob_op_with_id_complete, NULL); 621 CU_ASSERT(g_bserrno == -EINVAL); 622 CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID); 623 624 spdk_blob_close(blob, blob_op_complete, NULL); 625 CU_ASSERT(g_bserrno == 0); 626 627 spdk_blob_close(snapshot, blob_op_complete, NULL); 628 CU_ASSERT(g_bserrno == 0); 629 630 spdk_blob_close(snapshot2, blob_op_complete, NULL); 631 CU_ASSERT(g_bserrno == 0); 632 633 spdk_bs_unload(g_bs, bs_op_complete, NULL); 634 CU_ASSERT(g_bserrno == 0); 635 g_bs = NULL; 636 } 637 638 static void 639 blob_snapshot_freeze_io(void) 640 { 641 struct spdk_io_channel *channel; 642 struct spdk_bs_channel *bs_channel; 643 struct spdk_blob_store *bs; 644 struct spdk_bs_dev *dev; 645 struct spdk_blob *blob; 646 struct spdk_blob_opts opts; 647 spdk_blob_id blobid; 648 uint32_t num_of_pages = 10; 649 uint8_t payload_read[num_of_pages * SPDK_BS_PAGE_SIZE]; 650 uint8_t payload_write[num_of_pages * SPDK_BS_PAGE_SIZE]; 651 uint8_t payload_zero[num_of_pages * SPDK_BS_PAGE_SIZE]; 652 653 memset(payload_write, 0xE5, sizeof(payload_write)); 654 memset(payload_read, 0x00, sizeof(payload_read)); 655 memset(payload_zero, 0x00, sizeof(payload_zero)); 656 657 dev = init_dev(); 658 memset(g_dev_buffer, 0, DEV_BUFFER_SIZE); 659 660 /* Test freeze I/O during snapshot */ 661 662 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 663 CU_ASSERT(g_bserrno == 0); 664 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 665 bs = g_bs; 666 667 channel = spdk_bs_alloc_io_channel(bs); 668 bs_channel = spdk_io_channel_get_ctx(channel); 669 670 /* Create blob with 10 clusters */ 671 spdk_blob_opts_init(&opts); 672 opts.num_clusters = 10; 673 opts.thin_provision = false; 674 675 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 676 CU_ASSERT(g_bserrno == 0); 677 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 678 blobid = g_blobid; 679 680 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 681 CU_ASSERT(g_bserrno == 0); 682 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 683 blob = g_blob; 684 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10); 685 686 /* Enable explicitly calling callbacks. On each read/write to back device 687 * execution will stop and wait until _bs_flush_scheduler is called */ 688 g_scheduler_delay = true; 689 690 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 691 692 /* This is implementation specific. 693 * Flag 'frozen_io' is set in _spdk_bs_snapshot_freeze_cpl callback. 694 * Four async I/O operations happen before that. */ 695 696 _bs_flush_scheduler(4); 697 698 CU_ASSERT(TAILQ_EMPTY(&bs_channel->queued_io)); 699 700 /* Blob I/O should be frozen here */ 701 CU_ASSERT(blob->frozen_refcnt == 1); 702 703 /* Write to the blob */ 704 spdk_blob_io_write(blob, channel, payload_write, 0, num_of_pages, blob_op_complete, NULL); 705 706 /* Verify that I/O is queued */ 707 CU_ASSERT(!TAILQ_EMPTY(&bs_channel->queued_io)); 708 /* Verify that payload is not written to disk */ 709 CU_ASSERT(memcmp(payload_zero, &g_dev_buffer[blob->active.clusters[0]*SPDK_BS_PAGE_SIZE], 710 SPDK_BS_PAGE_SIZE) == 0); 711 712 /* Disable scheduler delay. 713 * Finish all operations including spdk_bs_create_snapshot */ 714 g_scheduler_delay = false; 715 _bs_flush_scheduler(1); 716 717 /* Verify snapshot */ 718 CU_ASSERT(g_bserrno == 0); 719 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 720 721 /* Verify that blob has unset frozen_io */ 722 CU_ASSERT(blob->frozen_refcnt == 0); 723 724 /* Verify that postponed I/O completed successfully by comparing payload */ 725 spdk_blob_io_read(blob, channel, payload_read, 0, num_of_pages, blob_op_complete, NULL); 726 CU_ASSERT(g_bserrno == 0); 727 CU_ASSERT(memcmp(payload_write, payload_read, num_of_pages * SPDK_BS_PAGE_SIZE) == 0); 728 729 spdk_blob_close(blob, blob_op_complete, NULL); 730 CU_ASSERT(g_bserrno == 0); 731 732 spdk_bs_free_io_channel(channel); 733 734 spdk_bs_unload(g_bs, bs_op_complete, NULL); 735 CU_ASSERT(g_bserrno == 0); 736 g_bs = NULL; 737 } 738 739 static void 740 blob_clone(void) 741 { 742 struct spdk_blob_store *bs; 743 struct spdk_bs_dev *dev; 744 struct spdk_blob_opts opts; 745 struct spdk_blob *blob, *snapshot, *clone; 746 spdk_blob_id blobid, cloneid, snapshotid; 747 struct spdk_blob_xattr_opts xattrs; 748 const void *value; 749 size_t value_len; 750 int rc; 751 752 dev = init_dev(); 753 754 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 755 CU_ASSERT(g_bserrno == 0); 756 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 757 bs = g_bs; 758 759 /* Create blob with 10 clusters */ 760 761 spdk_blob_opts_init(&opts); 762 opts.num_clusters = 10; 763 764 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 765 CU_ASSERT(g_bserrno == 0); 766 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 767 blobid = g_blobid; 768 769 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 770 CU_ASSERT(g_bserrno == 0); 771 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 772 blob = g_blob; 773 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 774 775 /* Create snapshot */ 776 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 777 CU_ASSERT(g_bserrno == 0); 778 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 779 snapshotid = g_blobid; 780 781 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 782 CU_ASSERT(g_bserrno == 0); 783 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 784 snapshot = g_blob; 785 CU_ASSERT(snapshot->data_ro == true) 786 CU_ASSERT(snapshot->md_ro == true) 787 CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10); 788 789 spdk_blob_close(snapshot, blob_op_complete, NULL); 790 CU_ASSERT(g_bserrno == 0); 791 792 /* Create clone from snapshot with xattrs */ 793 xattrs.names = g_xattr_names; 794 xattrs.get_value = _get_xattr_value; 795 xattrs.count = 3; 796 xattrs.ctx = &g_ctx; 797 798 spdk_bs_create_clone(bs, snapshotid, &xattrs, blob_op_with_id_complete, NULL); 799 CU_ASSERT(g_bserrno == 0); 800 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 801 cloneid = g_blobid; 802 803 spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL); 804 CU_ASSERT(g_bserrno == 0); 805 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 806 clone = g_blob; 807 CU_ASSERT(clone->data_ro == false) 808 CU_ASSERT(clone->md_ro == false) 809 CU_ASSERT(spdk_blob_get_num_clusters(clone) == 10); 810 811 rc = spdk_blob_get_xattr_value(clone, g_xattr_names[0], &value, &value_len); 812 CU_ASSERT(rc == 0); 813 SPDK_CU_ASSERT_FATAL(value != NULL); 814 CU_ASSERT(value_len == strlen(g_xattr_values[0])); 815 CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len); 816 817 rc = spdk_blob_get_xattr_value(clone, g_xattr_names[1], &value, &value_len); 818 CU_ASSERT(rc == 0); 819 SPDK_CU_ASSERT_FATAL(value != NULL); 820 CU_ASSERT(value_len == strlen(g_xattr_values[1])); 821 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len); 822 823 rc = spdk_blob_get_xattr_value(clone, g_xattr_names[2], &value, &value_len); 824 CU_ASSERT(rc == 0); 825 SPDK_CU_ASSERT_FATAL(value != NULL); 826 CU_ASSERT(value_len == strlen(g_xattr_values[2])); 827 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len); 828 829 830 spdk_blob_close(clone, blob_op_complete, NULL); 831 CU_ASSERT(g_bserrno == 0); 832 833 /* Try to create clone from not read only blob */ 834 spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL); 835 CU_ASSERT(g_bserrno == -EINVAL); 836 CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID); 837 838 /* Mark blob as read only */ 839 spdk_blob_set_read_only(blob); 840 spdk_blob_sync_md(blob, blob_op_complete, NULL); 841 CU_ASSERT(g_bserrno == 0); 842 843 /* Create clone from read only blob */ 844 spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL); 845 CU_ASSERT(g_bserrno == 0); 846 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 847 cloneid = g_blobid; 848 849 spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL); 850 CU_ASSERT(g_bserrno == 0); 851 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 852 clone = g_blob; 853 CU_ASSERT(clone->data_ro == false) 854 CU_ASSERT(clone->md_ro == false) 855 CU_ASSERT(spdk_blob_get_num_clusters(clone) == 10); 856 857 spdk_blob_close(clone, blob_op_complete, NULL); 858 CU_ASSERT(g_bserrno == 0); 859 860 spdk_blob_close(blob, blob_op_complete, NULL); 861 CU_ASSERT(g_bserrno == 0); 862 863 spdk_bs_unload(g_bs, bs_op_complete, NULL); 864 CU_ASSERT(g_bserrno == 0); 865 g_bs = NULL; 866 867 } 868 869 static void 870 _blob_inflate(bool decouple_parent) 871 { 872 struct spdk_blob_store *bs; 873 struct spdk_bs_dev *dev; 874 struct spdk_blob_opts opts; 875 struct spdk_blob *blob, *snapshot; 876 spdk_blob_id blobid, snapshotid; 877 struct spdk_io_channel *channel; 878 uint64_t free_clusters; 879 880 dev = init_dev(); 881 882 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 883 CU_ASSERT(g_bserrno == 0); 884 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 885 bs = g_bs; 886 887 channel = spdk_bs_alloc_io_channel(bs); 888 SPDK_CU_ASSERT_FATAL(channel != NULL); 889 890 /* Create blob with 10 clusters */ 891 892 spdk_blob_opts_init(&opts); 893 opts.num_clusters = 10; 894 opts.thin_provision = true; 895 896 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 897 CU_ASSERT(g_bserrno == 0); 898 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 899 blobid = g_blobid; 900 901 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 902 CU_ASSERT(g_bserrno == 0); 903 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 904 blob = g_blob; 905 906 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 907 CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == true); 908 909 /* 1) Blob with no parent */ 910 if (decouple_parent) { 911 /* Decouple parent of blob with no parent (should fail) */ 912 spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL); 913 CU_ASSERT(g_bserrno != 0); 914 } else { 915 /* Inflate of thin blob with no parent should made it thick */ 916 spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL); 917 CU_ASSERT(g_bserrno == 0); 918 CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == false); 919 } 920 921 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 922 CU_ASSERT(g_bserrno == 0); 923 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 924 snapshotid = g_blobid; 925 926 CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == true); 927 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 928 929 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 930 CU_ASSERT(g_bserrno == 0); 931 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 932 snapshot = g_blob; 933 CU_ASSERT(snapshot->data_ro == true) 934 CU_ASSERT(snapshot->md_ro == true) 935 CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10); 936 937 spdk_blob_close(snapshot, blob_op_complete, NULL); 938 CU_ASSERT(g_bserrno == 0); 939 940 free_clusters = spdk_bs_free_cluster_count(bs); 941 942 /* 2) Blob with parent */ 943 if (!decouple_parent) { 944 /* Do full blob inflation */ 945 spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL); 946 CU_ASSERT(g_bserrno == 0); 947 /* all 10 clusters should be allocated */ 948 CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 10); 949 } else { 950 /* Decouple parent of blob */ 951 spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL); 952 CU_ASSERT(g_bserrno == 0); 953 /* when only parent is removed, none of the clusters should be allocated */ 954 CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters); 955 } 956 957 /* Now, it should be possible to delete snapshot */ 958 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 959 CU_ASSERT(g_bserrno == 0); 960 961 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10) 962 CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == decouple_parent); 963 964 spdk_blob_close(blob, blob_op_complete, NULL); 965 CU_ASSERT(g_bserrno == 0); 966 967 spdk_bs_unload(g_bs, bs_op_complete, NULL); 968 CU_ASSERT(g_bserrno == 0); 969 g_bs = NULL; 970 971 spdk_bs_free_io_channel(channel); 972 } 973 974 static void 975 blob_inflate(void) 976 { 977 _blob_inflate(false); 978 _blob_inflate(true); 979 } 980 981 static void 982 blob_delete(void) 983 { 984 struct spdk_blob_store *bs; 985 struct spdk_bs_dev *dev; 986 spdk_blob_id blobid; 987 988 dev = init_dev(); 989 990 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 991 CU_ASSERT(g_bserrno == 0); 992 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 993 bs = g_bs; 994 995 /* Create a blob and then delete it. */ 996 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 997 CU_ASSERT(g_bserrno == 0); 998 CU_ASSERT(g_blobid > 0); 999 blobid = g_blobid; 1000 1001 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 1002 CU_ASSERT(g_bserrno == 0); 1003 1004 /* Try to open the blob */ 1005 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1006 CU_ASSERT(g_bserrno == -ENOENT); 1007 1008 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1009 CU_ASSERT(g_bserrno == 0); 1010 g_bs = NULL; 1011 } 1012 1013 static void 1014 blob_resize(void) 1015 { 1016 struct spdk_blob_store *bs; 1017 struct spdk_bs_dev *dev; 1018 struct spdk_blob *blob; 1019 spdk_blob_id blobid; 1020 uint64_t free_clusters; 1021 1022 dev = init_dev(); 1023 1024 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1025 CU_ASSERT(g_bserrno == 0); 1026 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1027 bs = g_bs; 1028 free_clusters = spdk_bs_free_cluster_count(bs); 1029 1030 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1031 CU_ASSERT(g_bserrno == 0); 1032 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1033 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 1034 blobid = g_blobid; 1035 1036 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1037 CU_ASSERT(g_bserrno == 0); 1038 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1039 blob = g_blob; 1040 1041 /* Confirm that resize fails if blob is marked read-only. */ 1042 blob->md_ro = true; 1043 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 1044 CU_ASSERT(g_bserrno == -EPERM); 1045 blob->md_ro = false; 1046 1047 /* The blob started at 0 clusters. Resize it to be 5. */ 1048 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 1049 CU_ASSERT(g_bserrno == 0); 1050 CU_ASSERT((free_clusters - 5) == spdk_bs_free_cluster_count(bs)); 1051 1052 /* Shrink the blob to 3 clusters. This will not actually release 1053 * the old clusters until the blob is synced. 1054 */ 1055 spdk_blob_resize(blob, 3, blob_op_complete, NULL); 1056 CU_ASSERT(g_bserrno == 0); 1057 /* Verify there are still 5 clusters in use */ 1058 CU_ASSERT((free_clusters - 5) == spdk_bs_free_cluster_count(bs)); 1059 1060 spdk_blob_sync_md(blob, blob_op_complete, NULL); 1061 CU_ASSERT(g_bserrno == 0); 1062 /* Now there are only 3 clusters in use */ 1063 CU_ASSERT((free_clusters - 3) == spdk_bs_free_cluster_count(bs)); 1064 1065 /* Resize the blob to be 10 clusters. Growth takes effect immediately. */ 1066 spdk_blob_resize(blob, 10, blob_op_complete, NULL); 1067 CU_ASSERT(g_bserrno == 0); 1068 CU_ASSERT((free_clusters - 10) == spdk_bs_free_cluster_count(bs)); 1069 1070 /* Try to resize the blob to size larger than blobstore. */ 1071 spdk_blob_resize(blob, bs->total_clusters + 1, blob_op_complete, NULL); 1072 CU_ASSERT(g_bserrno == -ENOSPC); 1073 1074 spdk_blob_close(blob, blob_op_complete, NULL); 1075 CU_ASSERT(g_bserrno == 0); 1076 1077 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 1078 CU_ASSERT(g_bserrno == 0); 1079 1080 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1081 CU_ASSERT(g_bserrno == 0); 1082 g_bs = NULL; 1083 } 1084 1085 static void 1086 blob_read_only(void) 1087 { 1088 struct spdk_blob_store *bs; 1089 struct spdk_bs_dev *dev; 1090 struct spdk_blob *blob; 1091 struct spdk_bs_opts opts; 1092 spdk_blob_id blobid; 1093 int rc; 1094 1095 dev = init_dev(); 1096 spdk_bs_opts_init(&opts); 1097 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 1098 1099 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 1100 CU_ASSERT(g_bserrno == 0); 1101 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1102 bs = g_bs; 1103 1104 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1105 CU_ASSERT(g_bserrno == 0); 1106 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1107 blobid = g_blobid; 1108 1109 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1110 CU_ASSERT(g_bserrno == 0); 1111 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1112 blob = g_blob; 1113 1114 rc = spdk_blob_set_read_only(blob); 1115 CU_ASSERT(rc == 0); 1116 1117 CU_ASSERT(blob->data_ro == false); 1118 CU_ASSERT(blob->md_ro == false); 1119 1120 spdk_blob_sync_md(blob, bs_op_complete, NULL); 1121 1122 CU_ASSERT(blob->data_ro == true); 1123 CU_ASSERT(blob->md_ro == true); 1124 CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY); 1125 1126 spdk_blob_close(blob, blob_op_complete, NULL); 1127 CU_ASSERT(g_bserrno == 0); 1128 1129 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1130 CU_ASSERT(g_bserrno == 0); 1131 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1132 blob = g_blob; 1133 1134 CU_ASSERT(blob->data_ro == true); 1135 CU_ASSERT(blob->md_ro == true); 1136 CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY); 1137 1138 spdk_blob_close(blob, blob_op_complete, NULL); 1139 CU_ASSERT(g_bserrno == 0); 1140 1141 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1142 CU_ASSERT(g_bserrno == 0); 1143 g_bs = NULL; 1144 g_blob = NULL; 1145 g_blobid = 0; 1146 1147 /* Load an existing blob store */ 1148 dev = init_dev(); 1149 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 1150 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 1151 CU_ASSERT(g_bserrno == 0); 1152 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1153 1154 spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL); 1155 CU_ASSERT(g_bserrno == 0); 1156 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1157 blob = g_blob; 1158 1159 CU_ASSERT(blob->data_ro == true); 1160 CU_ASSERT(blob->md_ro == true); 1161 CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY); 1162 1163 spdk_blob_close(blob, blob_op_complete, NULL); 1164 CU_ASSERT(g_bserrno == 0); 1165 1166 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1167 CU_ASSERT(g_bserrno == 0); 1168 1169 } 1170 1171 static void 1172 channel_ops(void) 1173 { 1174 struct spdk_blob_store *bs; 1175 struct spdk_bs_dev *dev; 1176 struct spdk_io_channel *channel; 1177 1178 dev = init_dev(); 1179 1180 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1181 CU_ASSERT(g_bserrno == 0); 1182 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1183 bs = g_bs; 1184 1185 channel = spdk_bs_alloc_io_channel(bs); 1186 CU_ASSERT(channel != NULL); 1187 1188 spdk_bs_free_io_channel(channel); 1189 1190 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1191 CU_ASSERT(g_bserrno == 0); 1192 g_bs = NULL; 1193 } 1194 1195 static void 1196 blob_write(void) 1197 { 1198 struct spdk_blob_store *bs; 1199 struct spdk_bs_dev *dev; 1200 struct spdk_blob *blob; 1201 struct spdk_io_channel *channel; 1202 spdk_blob_id blobid; 1203 uint64_t pages_per_cluster; 1204 uint8_t payload[10 * 4096]; 1205 1206 dev = init_dev(); 1207 1208 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1209 CU_ASSERT(g_bserrno == 0); 1210 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1211 bs = g_bs; 1212 1213 pages_per_cluster = spdk_bs_get_cluster_size(bs) / spdk_bs_get_page_size(bs); 1214 1215 channel = spdk_bs_alloc_io_channel(bs); 1216 CU_ASSERT(channel != NULL); 1217 1218 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1219 CU_ASSERT(g_bserrno == 0); 1220 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1221 blobid = g_blobid; 1222 1223 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1224 CU_ASSERT(g_bserrno == 0); 1225 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1226 blob = g_blob; 1227 1228 /* Write to a blob with 0 size */ 1229 spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL); 1230 CU_ASSERT(g_bserrno == -EINVAL); 1231 1232 /* Resize the blob */ 1233 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 1234 CU_ASSERT(g_bserrno == 0); 1235 1236 /* Confirm that write fails if blob is marked read-only. */ 1237 blob->data_ro = true; 1238 spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL); 1239 CU_ASSERT(g_bserrno == -EPERM); 1240 blob->data_ro = false; 1241 1242 /* Write to the blob */ 1243 spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL); 1244 CU_ASSERT(g_bserrno == 0); 1245 1246 /* Write starting beyond the end */ 1247 spdk_blob_io_write(blob, channel, payload, 5 * pages_per_cluster, 1, blob_op_complete, 1248 NULL); 1249 CU_ASSERT(g_bserrno == -EINVAL); 1250 1251 /* Write starting at a valid location but going off the end */ 1252 spdk_blob_io_write(blob, channel, payload, 4 * pages_per_cluster, pages_per_cluster + 1, 1253 blob_op_complete, NULL); 1254 CU_ASSERT(g_bserrno == -EINVAL); 1255 1256 spdk_blob_close(blob, blob_op_complete, NULL); 1257 CU_ASSERT(g_bserrno == 0); 1258 1259 spdk_bs_free_io_channel(channel); 1260 1261 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1262 CU_ASSERT(g_bserrno == 0); 1263 g_bs = NULL; 1264 } 1265 1266 static void 1267 blob_read(void) 1268 { 1269 struct spdk_blob_store *bs; 1270 struct spdk_bs_dev *dev; 1271 struct spdk_blob *blob; 1272 struct spdk_io_channel *channel; 1273 spdk_blob_id blobid; 1274 uint64_t pages_per_cluster; 1275 uint8_t payload[10 * 4096]; 1276 1277 dev = init_dev(); 1278 1279 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1280 CU_ASSERT(g_bserrno == 0); 1281 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1282 bs = g_bs; 1283 1284 pages_per_cluster = spdk_bs_get_cluster_size(bs) / spdk_bs_get_page_size(bs); 1285 1286 channel = spdk_bs_alloc_io_channel(bs); 1287 CU_ASSERT(channel != NULL); 1288 1289 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1290 CU_ASSERT(g_bserrno == 0); 1291 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1292 blobid = g_blobid; 1293 1294 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1295 CU_ASSERT(g_bserrno == 0); 1296 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1297 blob = g_blob; 1298 1299 /* Read from a blob with 0 size */ 1300 spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL); 1301 CU_ASSERT(g_bserrno == -EINVAL); 1302 1303 /* Resize the blob */ 1304 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 1305 CU_ASSERT(g_bserrno == 0); 1306 1307 /* Confirm that read passes if blob is marked read-only. */ 1308 blob->data_ro = true; 1309 spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL); 1310 CU_ASSERT(g_bserrno == 0); 1311 blob->data_ro = false; 1312 1313 /* Read from the blob */ 1314 spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL); 1315 CU_ASSERT(g_bserrno == 0); 1316 1317 /* Read starting beyond the end */ 1318 spdk_blob_io_read(blob, channel, payload, 5 * pages_per_cluster, 1, blob_op_complete, 1319 NULL); 1320 CU_ASSERT(g_bserrno == -EINVAL); 1321 1322 /* Read starting at a valid location but going off the end */ 1323 spdk_blob_io_read(blob, channel, payload, 4 * pages_per_cluster, pages_per_cluster + 1, 1324 blob_op_complete, NULL); 1325 CU_ASSERT(g_bserrno == -EINVAL); 1326 1327 spdk_blob_close(blob, blob_op_complete, NULL); 1328 CU_ASSERT(g_bserrno == 0); 1329 1330 spdk_bs_free_io_channel(channel); 1331 1332 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1333 CU_ASSERT(g_bserrno == 0); 1334 g_bs = NULL; 1335 } 1336 1337 static void 1338 blob_rw_verify(void) 1339 { 1340 struct spdk_blob_store *bs; 1341 struct spdk_bs_dev *dev; 1342 struct spdk_blob *blob; 1343 struct spdk_io_channel *channel; 1344 spdk_blob_id blobid; 1345 uint8_t payload_read[10 * 4096]; 1346 uint8_t payload_write[10 * 4096]; 1347 1348 dev = init_dev(); 1349 1350 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1351 CU_ASSERT(g_bserrno == 0); 1352 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1353 bs = g_bs; 1354 1355 channel = spdk_bs_alloc_io_channel(bs); 1356 CU_ASSERT(channel != NULL); 1357 1358 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1359 CU_ASSERT(g_bserrno == 0); 1360 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1361 blobid = g_blobid; 1362 1363 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1364 CU_ASSERT(g_bserrno == 0); 1365 CU_ASSERT(g_blob != NULL); 1366 blob = g_blob; 1367 1368 spdk_blob_resize(blob, 32, blob_op_complete, NULL); 1369 CU_ASSERT(g_bserrno == 0); 1370 1371 memset(payload_write, 0xE5, sizeof(payload_write)); 1372 spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL); 1373 CU_ASSERT(g_bserrno == 0); 1374 1375 memset(payload_read, 0x00, sizeof(payload_read)); 1376 spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL); 1377 CU_ASSERT(g_bserrno == 0); 1378 CU_ASSERT(memcmp(payload_write, payload_read, 4 * 4096) == 0); 1379 1380 spdk_blob_close(blob, blob_op_complete, NULL); 1381 CU_ASSERT(g_bserrno == 0); 1382 1383 spdk_bs_free_io_channel(channel); 1384 1385 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1386 CU_ASSERT(g_bserrno == 0); 1387 g_bs = NULL; 1388 } 1389 1390 static void 1391 blob_rw_verify_iov(void) 1392 { 1393 struct spdk_blob_store *bs; 1394 struct spdk_bs_dev *dev; 1395 struct spdk_blob *blob; 1396 struct spdk_io_channel *channel; 1397 spdk_blob_id blobid; 1398 uint8_t payload_read[10 * 4096]; 1399 uint8_t payload_write[10 * 4096]; 1400 struct iovec iov_read[3]; 1401 struct iovec iov_write[3]; 1402 void *buf; 1403 1404 dev = init_dev(); 1405 memset(g_dev_buffer, 0, DEV_BUFFER_SIZE); 1406 1407 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1408 CU_ASSERT(g_bserrno == 0); 1409 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1410 bs = g_bs; 1411 1412 channel = spdk_bs_alloc_io_channel(bs); 1413 CU_ASSERT(channel != NULL); 1414 1415 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1416 CU_ASSERT(g_bserrno == 0); 1417 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1418 blobid = g_blobid; 1419 1420 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1421 CU_ASSERT(g_bserrno == 0); 1422 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1423 blob = g_blob; 1424 1425 spdk_blob_resize(blob, 2, blob_op_complete, NULL); 1426 CU_ASSERT(g_bserrno == 0); 1427 1428 /* 1429 * Manually adjust the offset of the blob's second cluster. This allows 1430 * us to make sure that the readv/write code correctly accounts for I/O 1431 * that cross cluster boundaries. Start by asserting that the allocated 1432 * clusters are where we expect before modifying the second cluster. 1433 */ 1434 CU_ASSERT(blob->active.clusters[0] == 1 * 256); 1435 CU_ASSERT(blob->active.clusters[1] == 2 * 256); 1436 blob->active.clusters[1] = 3 * 256; 1437 1438 memset(payload_write, 0xE5, sizeof(payload_write)); 1439 iov_write[0].iov_base = payload_write; 1440 iov_write[0].iov_len = 1 * 4096; 1441 iov_write[1].iov_base = payload_write + 1 * 4096; 1442 iov_write[1].iov_len = 5 * 4096; 1443 iov_write[2].iov_base = payload_write + 6 * 4096; 1444 iov_write[2].iov_len = 4 * 4096; 1445 /* 1446 * Choose a page offset just before the cluster boundary. The first 6 pages of payload 1447 * will get written to the first cluster, the last 4 to the second cluster. 1448 */ 1449 spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL); 1450 CU_ASSERT(g_bserrno == 0); 1451 1452 memset(payload_read, 0xAA, sizeof(payload_read)); 1453 iov_read[0].iov_base = payload_read; 1454 iov_read[0].iov_len = 3 * 4096; 1455 iov_read[1].iov_base = payload_read + 3 * 4096; 1456 iov_read[1].iov_len = 4 * 4096; 1457 iov_read[2].iov_base = payload_read + 7 * 4096; 1458 iov_read[2].iov_len = 3 * 4096; 1459 spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL); 1460 CU_ASSERT(g_bserrno == 0); 1461 CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0); 1462 1463 buf = calloc(1, 256 * 4096); 1464 SPDK_CU_ASSERT_FATAL(buf != NULL); 1465 /* Check that cluster 2 on "disk" was not modified. */ 1466 CU_ASSERT(memcmp(buf, &g_dev_buffer[512 * 4096], 256 * 4096) == 0); 1467 free(buf); 1468 1469 spdk_blob_close(blob, blob_op_complete, NULL); 1470 CU_ASSERT(g_bserrno == 0); 1471 1472 spdk_bs_free_io_channel(channel); 1473 1474 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1475 CU_ASSERT(g_bserrno == 0); 1476 g_bs = NULL; 1477 } 1478 1479 static uint32_t 1480 bs_channel_get_req_count(struct spdk_io_channel *_channel) 1481 { 1482 struct spdk_bs_channel *channel = spdk_io_channel_get_ctx(_channel); 1483 struct spdk_bs_request_set *set; 1484 uint32_t count = 0; 1485 1486 TAILQ_FOREACH(set, &channel->reqs, link) { 1487 count++; 1488 } 1489 1490 return count; 1491 } 1492 1493 static void 1494 blob_rw_verify_iov_nomem(void) 1495 { 1496 struct spdk_blob_store *bs; 1497 struct spdk_bs_dev *dev; 1498 struct spdk_blob *blob; 1499 struct spdk_io_channel *channel; 1500 spdk_blob_id blobid; 1501 uint8_t payload_write[10 * 4096]; 1502 struct iovec iov_write[3]; 1503 uint32_t req_count; 1504 1505 dev = init_dev(); 1506 memset(g_dev_buffer, 0, DEV_BUFFER_SIZE); 1507 1508 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1509 CU_ASSERT(g_bserrno == 0); 1510 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1511 bs = g_bs; 1512 1513 channel = spdk_bs_alloc_io_channel(bs); 1514 CU_ASSERT(channel != NULL); 1515 1516 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1517 CU_ASSERT(g_bserrno == 0); 1518 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1519 blobid = g_blobid; 1520 1521 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1522 CU_ASSERT(g_bserrno == 0); 1523 CU_ASSERT(g_blob != NULL); 1524 blob = g_blob; 1525 1526 spdk_blob_resize(blob, 2, blob_op_complete, NULL); 1527 CU_ASSERT(g_bserrno == 0); 1528 1529 /* 1530 * Choose a page offset just before the cluster boundary. The first 6 pages of payload 1531 * will get written to the first cluster, the last 4 to the second cluster. 1532 */ 1533 iov_write[0].iov_base = payload_write; 1534 iov_write[0].iov_len = 1 * 4096; 1535 iov_write[1].iov_base = payload_write + 1 * 4096; 1536 iov_write[1].iov_len = 5 * 4096; 1537 iov_write[2].iov_base = payload_write + 6 * 4096; 1538 iov_write[2].iov_len = 4 * 4096; 1539 MOCK_SET(calloc, void *, NULL); 1540 req_count = bs_channel_get_req_count(channel); 1541 spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL); 1542 CU_ASSERT(g_bserrno = -ENOMEM); 1543 CU_ASSERT(req_count == bs_channel_get_req_count(channel)); 1544 MOCK_SET(calloc, void *, (void *)MOCK_PASS_THRU); 1545 1546 spdk_blob_close(blob, blob_op_complete, NULL); 1547 CU_ASSERT(g_bserrno == 0); 1548 1549 spdk_bs_free_io_channel(channel); 1550 1551 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1552 CU_ASSERT(g_bserrno == 0); 1553 g_bs = NULL; 1554 } 1555 1556 static void 1557 blob_rw_iov_read_only(void) 1558 { 1559 struct spdk_blob_store *bs; 1560 struct spdk_bs_dev *dev; 1561 struct spdk_blob *blob; 1562 struct spdk_io_channel *channel; 1563 spdk_blob_id blobid; 1564 uint8_t payload_read[4096]; 1565 uint8_t payload_write[4096]; 1566 struct iovec iov_read; 1567 struct iovec iov_write; 1568 1569 dev = init_dev(); 1570 memset(g_dev_buffer, 0, DEV_BUFFER_SIZE); 1571 1572 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1573 CU_ASSERT(g_bserrno == 0); 1574 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1575 bs = g_bs; 1576 1577 channel = spdk_bs_alloc_io_channel(bs); 1578 CU_ASSERT(channel != NULL); 1579 1580 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1581 CU_ASSERT(g_bserrno == 0); 1582 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1583 blobid = g_blobid; 1584 1585 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1586 CU_ASSERT(g_bserrno == 0); 1587 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1588 blob = g_blob; 1589 1590 spdk_blob_resize(blob, 2, blob_op_complete, NULL); 1591 CU_ASSERT(g_bserrno == 0); 1592 1593 /* Verify that writev failed if read_only flag is set. */ 1594 blob->data_ro = true; 1595 iov_write.iov_base = payload_write; 1596 iov_write.iov_len = sizeof(payload_write); 1597 spdk_blob_io_writev(blob, channel, &iov_write, 1, 0, 1, blob_op_complete, NULL); 1598 CU_ASSERT(g_bserrno == -EPERM); 1599 1600 /* Verify that reads pass if data_ro flag is set. */ 1601 iov_read.iov_base = payload_read; 1602 iov_read.iov_len = sizeof(payload_read); 1603 spdk_blob_io_readv(blob, channel, &iov_read, 1, 0, 1, blob_op_complete, NULL); 1604 CU_ASSERT(g_bserrno == 0); 1605 1606 spdk_blob_close(blob, blob_op_complete, NULL); 1607 CU_ASSERT(g_bserrno == 0); 1608 1609 spdk_bs_free_io_channel(channel); 1610 1611 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1612 CU_ASSERT(g_bserrno == 0); 1613 g_bs = NULL; 1614 } 1615 1616 static void 1617 _blob_io_read_no_split(struct spdk_blob *blob, struct spdk_io_channel *channel, 1618 uint8_t *payload, uint64_t offset, uint64_t length, 1619 spdk_blob_op_complete cb_fn, void *cb_arg) 1620 { 1621 uint64_t i; 1622 uint8_t *buf; 1623 uint64_t page_size = spdk_bs_get_page_size(blob->bs); 1624 1625 /* To be sure that operation is NOT splitted, read one page at the time */ 1626 buf = payload; 1627 for (i = 0; i < length; i++) { 1628 spdk_blob_io_read(blob, channel, buf, i + offset, 1, blob_op_complete, NULL); 1629 if (g_bserrno != 0) { 1630 /* Pass the error code up */ 1631 break; 1632 } 1633 buf += page_size; 1634 } 1635 1636 cb_fn(cb_arg, g_bserrno); 1637 } 1638 1639 static void 1640 _blob_io_write_no_split(struct spdk_blob *blob, struct spdk_io_channel *channel, 1641 uint8_t *payload, uint64_t offset, uint64_t length, 1642 spdk_blob_op_complete cb_fn, void *cb_arg) 1643 { 1644 uint64_t i; 1645 uint8_t *buf; 1646 uint64_t page_size = spdk_bs_get_page_size(blob->bs); 1647 1648 /* To be sure that operation is NOT splitted, write one page at the time */ 1649 buf = payload; 1650 for (i = 0; i < length; i++) { 1651 spdk_blob_io_write(blob, channel, buf, i + offset, 1, blob_op_complete, NULL); 1652 if (g_bserrno != 0) { 1653 /* Pass the error code up */ 1654 break; 1655 } 1656 buf += page_size; 1657 } 1658 1659 cb_fn(cb_arg, g_bserrno); 1660 } 1661 1662 static void 1663 blob_operation_split_rw(void) 1664 { 1665 struct spdk_blob_store *bs; 1666 struct spdk_bs_dev *dev; 1667 struct spdk_blob *blob; 1668 struct spdk_io_channel *channel; 1669 struct spdk_blob_opts opts; 1670 spdk_blob_id blobid; 1671 uint64_t cluster_size; 1672 1673 uint64_t payload_size; 1674 uint8_t *payload_read; 1675 uint8_t *payload_write; 1676 uint8_t *payload_pattern; 1677 1678 uint64_t page_size; 1679 uint64_t pages_per_cluster; 1680 uint64_t pages_per_payload; 1681 1682 uint64_t i; 1683 1684 dev = init_dev(); 1685 1686 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1687 CU_ASSERT(g_bserrno == 0); 1688 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1689 bs = g_bs; 1690 1691 cluster_size = spdk_bs_get_cluster_size(bs); 1692 page_size = spdk_bs_get_page_size(bs); 1693 pages_per_cluster = cluster_size / page_size; 1694 pages_per_payload = pages_per_cluster * 5; 1695 payload_size = cluster_size * 5; 1696 1697 payload_read = malloc(payload_size); 1698 SPDK_CU_ASSERT_FATAL(payload_read != NULL); 1699 1700 payload_write = malloc(payload_size); 1701 SPDK_CU_ASSERT_FATAL(payload_write != NULL); 1702 1703 payload_pattern = malloc(payload_size); 1704 SPDK_CU_ASSERT_FATAL(payload_pattern != NULL); 1705 1706 /* Prepare random pattern to write */ 1707 memset(payload_pattern, 0xFF, payload_size); 1708 for (i = 0; i < pages_per_payload; i++) { 1709 *((uint64_t *)(payload_pattern + page_size * i)) = (i + 1); 1710 } 1711 1712 channel = spdk_bs_alloc_io_channel(bs); 1713 SPDK_CU_ASSERT_FATAL(channel != NULL); 1714 1715 /* Create blob */ 1716 spdk_blob_opts_init(&opts); 1717 opts.thin_provision = false; 1718 opts.num_clusters = 5; 1719 1720 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 1721 CU_ASSERT(g_bserrno == 0); 1722 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1723 blobid = g_blobid; 1724 1725 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1726 CU_ASSERT(g_bserrno == 0); 1727 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1728 blob = g_blob; 1729 1730 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5); 1731 1732 /* Initial read should return zeroed payload */ 1733 memset(payload_read, 0xFF, payload_size); 1734 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL); 1735 CU_ASSERT(g_bserrno == 0); 1736 CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size)); 1737 1738 /* Fill whole blob except last page */ 1739 spdk_blob_io_write(blob, channel, payload_pattern, 0, pages_per_payload - 1, 1740 blob_op_complete, NULL); 1741 CU_ASSERT(g_bserrno == 0); 1742 1743 /* Write last page with a pattern */ 1744 spdk_blob_io_write(blob, channel, payload_pattern, pages_per_payload - 1, 1, 1745 blob_op_complete, NULL); 1746 CU_ASSERT(g_bserrno == 0); 1747 1748 /* Read whole blob and check consistency */ 1749 memset(payload_read, 0xFF, payload_size); 1750 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL); 1751 CU_ASSERT(g_bserrno == 0); 1752 CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size - page_size) == 0); 1753 CU_ASSERT(memcmp(payload_pattern, payload_read + payload_size - page_size, page_size) == 0); 1754 1755 /* Fill whole blob except first page */ 1756 spdk_blob_io_write(blob, channel, payload_pattern, 1, pages_per_payload - 1, 1757 blob_op_complete, NULL); 1758 CU_ASSERT(g_bserrno == 0); 1759 1760 /* Write first page with a pattern */ 1761 spdk_blob_io_write(blob, channel, payload_pattern, 0, 1, 1762 blob_op_complete, NULL); 1763 CU_ASSERT(g_bserrno == 0); 1764 1765 /* Read whole blob and check consistency */ 1766 memset(payload_read, 0xFF, payload_size); 1767 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL); 1768 CU_ASSERT(g_bserrno == 0); 1769 CU_ASSERT(memcmp(payload_pattern, payload_read + page_size, payload_size - page_size) == 0); 1770 CU_ASSERT(memcmp(payload_pattern, payload_read, page_size) == 0); 1771 1772 1773 /* Fill whole blob with a pattern (5 clusters) */ 1774 1775 /* 1. Read test. */ 1776 _blob_io_write_no_split(blob, channel, payload_pattern, 0, pages_per_payload, 1777 blob_op_complete, NULL); 1778 CU_ASSERT(g_bserrno == 0); 1779 1780 memset(payload_read, 0xFF, payload_size); 1781 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL); 1782 CU_ASSERT(g_bserrno == 0); 1783 CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0); 1784 1785 /* 2. Write test. */ 1786 spdk_blob_io_write(blob, channel, payload_pattern, 0, pages_per_payload, 1787 blob_op_complete, NULL); 1788 CU_ASSERT(g_bserrno == 0); 1789 1790 memset(payload_read, 0xFF, payload_size); 1791 _blob_io_read_no_split(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL); 1792 CU_ASSERT(g_bserrno == 0); 1793 CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0); 1794 1795 spdk_blob_close(blob, blob_op_complete, NULL); 1796 CU_ASSERT(g_bserrno == 0); 1797 1798 spdk_bs_free_io_channel(channel); 1799 1800 /* Unload the blob store */ 1801 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1802 CU_ASSERT(g_bserrno == 0); 1803 g_bs = NULL; 1804 g_blob = NULL; 1805 g_blobid = 0; 1806 1807 free(payload_read); 1808 free(payload_write); 1809 free(payload_pattern); 1810 } 1811 1812 static void 1813 blob_unmap(void) 1814 { 1815 struct spdk_blob_store *bs; 1816 struct spdk_bs_dev *dev; 1817 struct spdk_blob *blob; 1818 struct spdk_io_channel *channel; 1819 spdk_blob_id blobid; 1820 struct spdk_blob_opts opts; 1821 uint8_t payload[4096]; 1822 int i; 1823 1824 dev = init_dev(); 1825 1826 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1827 CU_ASSERT(g_bserrno == 0); 1828 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1829 bs = g_bs; 1830 1831 channel = spdk_bs_alloc_io_channel(bs); 1832 CU_ASSERT(channel != NULL); 1833 1834 spdk_blob_opts_init(&opts); 1835 opts.num_clusters = 10; 1836 1837 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 1838 CU_ASSERT(g_bserrno == 0); 1839 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1840 blobid = g_blobid; 1841 1842 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1843 CU_ASSERT(g_bserrno == 0); 1844 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1845 blob = g_blob; 1846 1847 spdk_blob_resize(blob, 10, blob_op_complete, NULL); 1848 CU_ASSERT(g_bserrno == 0); 1849 1850 memset(payload, 0, sizeof(payload)); 1851 payload[0] = 0xFF; 1852 1853 /* 1854 * Set first byte of every cluster to 0xFF. 1855 * First cluster on device is reserved so let's start from cluster number 1 1856 */ 1857 for (i = 1; i < 11; i++) { 1858 g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] = 0xFF; 1859 } 1860 1861 /* Confirm writes */ 1862 for (i = 0; i < 10; i++) { 1863 payload[0] = 0; 1864 spdk_blob_io_read(blob, channel, &payload, i * SPDK_BLOB_OPTS_CLUSTER_SZ / 4096, 1, 1865 blob_op_complete, NULL); 1866 CU_ASSERT(g_bserrno == 0); 1867 CU_ASSERT(payload[0] == 0xFF); 1868 } 1869 1870 /* Mark some clusters as unallocated */ 1871 blob->active.clusters[1] = 0; 1872 blob->active.clusters[2] = 0; 1873 blob->active.clusters[3] = 0; 1874 blob->active.clusters[6] = 0; 1875 blob->active.clusters[8] = 0; 1876 1877 /* Unmap clusters by resizing to 0 */ 1878 spdk_blob_resize(blob, 0, blob_op_complete, NULL); 1879 CU_ASSERT(g_bserrno == 0); 1880 1881 spdk_blob_sync_md(blob, blob_op_complete, NULL); 1882 CU_ASSERT(g_bserrno == 0); 1883 1884 /* Confirm that only 'allocated' clusters were unmaped */ 1885 for (i = 1; i < 11; i++) { 1886 switch (i) { 1887 case 2: 1888 case 3: 1889 case 4: 1890 case 7: 1891 case 9: 1892 CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0xFF); 1893 break; 1894 default: 1895 CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0); 1896 break; 1897 } 1898 } 1899 1900 spdk_blob_close(blob, blob_op_complete, NULL); 1901 CU_ASSERT(g_bserrno == 0); 1902 1903 spdk_bs_free_io_channel(channel); 1904 1905 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1906 CU_ASSERT(g_bserrno == 0); 1907 g_bs = NULL; 1908 } 1909 1910 1911 static void 1912 blob_iter(void) 1913 { 1914 struct spdk_blob_store *bs; 1915 struct spdk_bs_dev *dev; 1916 struct spdk_blob *blob; 1917 spdk_blob_id blobid; 1918 1919 dev = init_dev(); 1920 1921 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1922 CU_ASSERT(g_bserrno == 0); 1923 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1924 bs = g_bs; 1925 1926 spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL); 1927 CU_ASSERT(g_blob == NULL); 1928 CU_ASSERT(g_bserrno == -ENOENT); 1929 1930 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1931 CU_ASSERT(g_bserrno == 0); 1932 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1933 blobid = g_blobid; 1934 1935 spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL); 1936 CU_ASSERT(g_blob != NULL); 1937 CU_ASSERT(g_bserrno == 0); 1938 blob = g_blob; 1939 CU_ASSERT(spdk_blob_get_id(blob) == blobid); 1940 1941 spdk_bs_iter_next(bs, blob, blob_op_with_handle_complete, NULL); 1942 CU_ASSERT(g_blob == NULL); 1943 CU_ASSERT(g_bserrno == -ENOENT); 1944 1945 spdk_bs_unload(g_bs, bs_op_complete, NULL); 1946 CU_ASSERT(g_bserrno == 0); 1947 g_bs = NULL; 1948 } 1949 1950 static void 1951 blob_xattr(void) 1952 { 1953 struct spdk_blob_store *bs; 1954 struct spdk_bs_dev *dev; 1955 struct spdk_blob *blob; 1956 spdk_blob_id blobid; 1957 uint64_t length; 1958 int rc; 1959 const char *name1, *name2; 1960 const void *value; 1961 size_t value_len; 1962 struct spdk_xattr_names *names; 1963 1964 dev = init_dev(); 1965 1966 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 1967 CU_ASSERT(g_bserrno == 0); 1968 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 1969 bs = g_bs; 1970 1971 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 1972 CU_ASSERT(g_bserrno == 0); 1973 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 1974 blobid = g_blobid; 1975 1976 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 1977 CU_ASSERT(g_bserrno == 0); 1978 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 1979 blob = g_blob; 1980 1981 /* Test that set_xattr fails if md_ro flag is set. */ 1982 blob->md_ro = true; 1983 rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1); 1984 CU_ASSERT(rc == -EPERM); 1985 1986 blob->md_ro = false; 1987 rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1); 1988 CU_ASSERT(rc == 0); 1989 1990 length = 2345; 1991 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 1992 CU_ASSERT(rc == 0); 1993 1994 /* Overwrite "length" xattr. */ 1995 length = 3456; 1996 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 1997 CU_ASSERT(rc == 0); 1998 1999 /* get_xattr should still work even if md_ro flag is set. */ 2000 value = NULL; 2001 blob->md_ro = true; 2002 rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len); 2003 CU_ASSERT(rc == 0); 2004 SPDK_CU_ASSERT_FATAL(value != NULL); 2005 CU_ASSERT(*(uint64_t *)value == length); 2006 CU_ASSERT(value_len == 8); 2007 blob->md_ro = false; 2008 2009 rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len); 2010 CU_ASSERT(rc == -ENOENT); 2011 2012 names = NULL; 2013 rc = spdk_blob_get_xattr_names(blob, &names); 2014 CU_ASSERT(rc == 0); 2015 SPDK_CU_ASSERT_FATAL(names != NULL); 2016 CU_ASSERT(spdk_xattr_names_get_count(names) == 2); 2017 name1 = spdk_xattr_names_get_name(names, 0); 2018 SPDK_CU_ASSERT_FATAL(name1 != NULL); 2019 CU_ASSERT(!strcmp(name1, "name") || !strcmp(name1, "length")); 2020 name2 = spdk_xattr_names_get_name(names, 1); 2021 SPDK_CU_ASSERT_FATAL(name2 != NULL); 2022 CU_ASSERT(!strcmp(name2, "name") || !strcmp(name2, "length")); 2023 CU_ASSERT(strcmp(name1, name2)); 2024 spdk_xattr_names_free(names); 2025 2026 /* Confirm that remove_xattr fails if md_ro is set to true. */ 2027 blob->md_ro = true; 2028 rc = spdk_blob_remove_xattr(blob, "name"); 2029 CU_ASSERT(rc == -EPERM); 2030 2031 blob->md_ro = false; 2032 rc = spdk_blob_remove_xattr(blob, "name"); 2033 CU_ASSERT(rc == 0); 2034 2035 rc = spdk_blob_remove_xattr(blob, "foobar"); 2036 CU_ASSERT(rc == -ENOENT); 2037 2038 /* Set internal xattr */ 2039 length = 7898; 2040 rc = _spdk_blob_set_xattr(blob, "internal", &length, sizeof(length), true); 2041 CU_ASSERT(rc == 0); 2042 rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, true); 2043 CU_ASSERT(rc == 0); 2044 CU_ASSERT(*(uint64_t *)value == length); 2045 /* try to get public xattr with same name */ 2046 rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len); 2047 CU_ASSERT(rc != 0); 2048 rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, false); 2049 CU_ASSERT(rc != 0); 2050 /* Check if SPDK_BLOB_INTERNAL_XATTR is set */ 2051 CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) == 2052 SPDK_BLOB_INTERNAL_XATTR) 2053 2054 spdk_blob_close(blob, blob_op_complete, NULL); 2055 2056 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2057 2058 /* Check if xattrs are persisted */ 2059 dev = init_dev(); 2060 2061 spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL); 2062 CU_ASSERT(g_bserrno == 0); 2063 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2064 2065 bs = g_bs; 2066 2067 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 2068 CU_ASSERT(g_bserrno == 0); 2069 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 2070 blob = g_blob; 2071 2072 rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, true); 2073 CU_ASSERT(rc == 0); 2074 CU_ASSERT(*(uint64_t *)value == length); 2075 2076 /* try to get internal xattr trough public call */ 2077 rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len); 2078 CU_ASSERT(rc != 0); 2079 2080 rc = _spdk_blob_remove_xattr(blob, "internal", true); 2081 CU_ASSERT(rc == 0); 2082 2083 CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) == 0); 2084 2085 CU_ASSERT(g_bserrno == 0); 2086 g_bs = NULL; 2087 } 2088 2089 static void 2090 bs_load(void) 2091 { 2092 struct spdk_bs_dev *dev; 2093 spdk_blob_id blobid; 2094 struct spdk_blob *blob; 2095 struct spdk_bs_super_block *super_block; 2096 uint64_t length; 2097 int rc; 2098 const void *value; 2099 size_t value_len; 2100 struct spdk_bs_opts opts; 2101 2102 dev = init_dev(); 2103 spdk_bs_opts_init(&opts); 2104 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2105 2106 /* Initialize a new blob store */ 2107 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2108 CU_ASSERT(g_bserrno == 0); 2109 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2110 2111 /* Try to open a blobid that does not exist */ 2112 spdk_bs_open_blob(g_bs, 0, blob_op_with_handle_complete, NULL); 2113 CU_ASSERT(g_bserrno == -ENOENT); 2114 CU_ASSERT(g_blob == NULL); 2115 2116 /* Create a blob */ 2117 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 2118 CU_ASSERT(g_bserrno == 0); 2119 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2120 blobid = g_blobid; 2121 2122 spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL); 2123 CU_ASSERT(g_bserrno == 0); 2124 CU_ASSERT(g_blob != NULL); 2125 blob = g_blob; 2126 2127 /* Try again to open valid blob but without the upper bit set */ 2128 spdk_bs_open_blob(g_bs, blobid & 0xFFFFFFFF, blob_op_with_handle_complete, NULL); 2129 CU_ASSERT(g_bserrno == -ENOENT); 2130 CU_ASSERT(g_blob == NULL); 2131 2132 /* Set some xattrs */ 2133 rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1); 2134 CU_ASSERT(rc == 0); 2135 2136 length = 2345; 2137 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 2138 CU_ASSERT(rc == 0); 2139 2140 /* Resize the blob */ 2141 spdk_blob_resize(blob, 10, blob_op_complete, NULL); 2142 CU_ASSERT(g_bserrno == 0); 2143 2144 spdk_blob_close(blob, blob_op_complete, NULL); 2145 CU_ASSERT(g_bserrno == 0); 2146 blob = NULL; 2147 g_blob = NULL; 2148 g_blobid = SPDK_BLOBID_INVALID; 2149 2150 /* Unload the blob store */ 2151 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2152 CU_ASSERT(g_bserrno == 0); 2153 g_bs = NULL; 2154 g_blob = NULL; 2155 g_blobid = 0; 2156 2157 super_block = (struct spdk_bs_super_block *)g_dev_buffer; 2158 CU_ASSERT(super_block->clean == 1); 2159 2160 /* Load should fail for device with an unsupported blocklen */ 2161 dev = init_dev(); 2162 dev->blocklen = SPDK_BS_PAGE_SIZE * 2; 2163 spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL); 2164 CU_ASSERT(g_bserrno == -EINVAL); 2165 2166 /* Load should when max_md_ops is set to zero */ 2167 dev = init_dev(); 2168 spdk_bs_opts_init(&opts); 2169 opts.max_md_ops = 0; 2170 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2171 CU_ASSERT(g_bserrno == -EINVAL); 2172 2173 /* Load should when max_channel_ops is set to zero */ 2174 dev = init_dev(); 2175 spdk_bs_opts_init(&opts); 2176 opts.max_channel_ops = 0; 2177 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2178 CU_ASSERT(g_bserrno == -EINVAL); 2179 2180 /* Load an existing blob store */ 2181 dev = init_dev(); 2182 spdk_bs_opts_init(&opts); 2183 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2184 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2185 CU_ASSERT(g_bserrno == 0); 2186 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2187 2188 super_block = (struct spdk_bs_super_block *)g_dev_buffer; 2189 CU_ASSERT(super_block->clean == 1); 2190 CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen); 2191 2192 spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL); 2193 CU_ASSERT(g_bserrno == 0); 2194 CU_ASSERT(g_blob != NULL); 2195 blob = g_blob; 2196 2197 /* Verify that blobstore is marked dirty after first metadata sync */ 2198 spdk_blob_sync_md(blob, blob_op_complete, NULL); 2199 CU_ASSERT(super_block->clean == 1); 2200 2201 /* Get the xattrs */ 2202 value = NULL; 2203 rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len); 2204 CU_ASSERT(rc == 0); 2205 SPDK_CU_ASSERT_FATAL(value != NULL); 2206 CU_ASSERT(*(uint64_t *)value == length); 2207 CU_ASSERT(value_len == 8); 2208 2209 rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len); 2210 CU_ASSERT(rc == -ENOENT); 2211 2212 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10); 2213 2214 spdk_blob_close(blob, blob_op_complete, NULL); 2215 CU_ASSERT(g_bserrno == 0); 2216 blob = NULL; 2217 g_blob = NULL; 2218 2219 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2220 CU_ASSERT(g_bserrno == 0); 2221 g_bs = NULL; 2222 2223 /* Load should fail: bdev size < saved size */ 2224 dev = init_dev(); 2225 dev->blockcnt /= 2; 2226 2227 spdk_bs_opts_init(&opts); 2228 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2229 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2230 2231 CU_ASSERT(g_bserrno == -EILSEQ); 2232 2233 /* Load should succeed: bdev size > saved size */ 2234 dev = init_dev(); 2235 dev->blockcnt *= 4; 2236 2237 spdk_bs_opts_init(&opts); 2238 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2239 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2240 2241 CU_ASSERT(g_bserrno == 0); 2242 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2243 2244 2245 /* Test compatibility mode */ 2246 2247 dev = init_dev(); 2248 super_block->size = 0; 2249 super_block->crc = _spdk_blob_md_page_calc_crc(super_block); 2250 2251 spdk_bs_opts_init(&opts); 2252 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2253 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2254 CU_ASSERT(g_bserrno == 0); 2255 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2256 2257 /* Create a blob */ 2258 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 2259 CU_ASSERT(g_bserrno == 0); 2260 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2261 2262 /* Blobstore should update number of blocks in super_block */ 2263 CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen); 2264 CU_ASSERT(super_block->clean == 0); 2265 2266 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2267 CU_ASSERT(g_bserrno == 0); 2268 CU_ASSERT(super_block->clean == 1); 2269 g_bs = NULL; 2270 2271 } 2272 2273 static void 2274 bs_type(void) 2275 { 2276 struct spdk_bs_dev *dev; 2277 struct spdk_bs_opts opts; 2278 2279 dev = init_dev(); 2280 spdk_bs_opts_init(&opts); 2281 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2282 2283 /* Initialize a new blob store */ 2284 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2285 CU_ASSERT(g_bserrno == 0); 2286 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2287 2288 /* Unload the blob store */ 2289 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2290 CU_ASSERT(g_bserrno == 0); 2291 g_bs = NULL; 2292 g_blob = NULL; 2293 g_blobid = 0; 2294 2295 /* Load non existing blobstore type */ 2296 dev = init_dev(); 2297 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING"); 2298 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2299 CU_ASSERT(g_bserrno != 0); 2300 2301 /* Load with empty blobstore type */ 2302 dev = init_dev(); 2303 memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype)); 2304 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2305 CU_ASSERT(g_bserrno == 0); 2306 2307 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2308 CU_ASSERT(g_bserrno == 0); 2309 g_bs = NULL; 2310 2311 /* Initialize a new blob store with empty bstype */ 2312 dev = init_dev(); 2313 memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype)); 2314 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 2315 CU_ASSERT(g_bserrno == 0); 2316 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2317 2318 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2319 CU_ASSERT(g_bserrno == 0); 2320 g_bs = NULL; 2321 2322 /* Load non existing blobstore type */ 2323 dev = init_dev(); 2324 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING"); 2325 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2326 CU_ASSERT(g_bserrno != 0); 2327 2328 /* Load with empty blobstore type */ 2329 dev = init_dev(); 2330 memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype)); 2331 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2332 CU_ASSERT(g_bserrno == 0); 2333 2334 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2335 CU_ASSERT(g_bserrno == 0); 2336 g_bs = NULL; 2337 } 2338 2339 static void 2340 bs_super_block(void) 2341 { 2342 struct spdk_bs_dev *dev; 2343 struct spdk_bs_super_block *super_block; 2344 struct spdk_bs_opts opts; 2345 struct spdk_bs_super_block_ver1 super_block_v1; 2346 2347 dev = init_dev(); 2348 spdk_bs_opts_init(&opts); 2349 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 2350 2351 /* Initialize a new blob store */ 2352 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2353 CU_ASSERT(g_bserrno == 0); 2354 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2355 2356 /* Unload the blob store */ 2357 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2358 CU_ASSERT(g_bserrno == 0); 2359 g_bs = NULL; 2360 g_blob = NULL; 2361 g_blobid = 0; 2362 2363 /* Load an existing blob store with version newer than supported */ 2364 super_block = (struct spdk_bs_super_block *)g_dev_buffer; 2365 super_block->version++; 2366 2367 dev = init_dev(); 2368 memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype)); 2369 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2370 CU_ASSERT(g_bserrno != 0); 2371 2372 /* Create a new blob store with super block version 1 */ 2373 dev = init_dev(); 2374 super_block_v1.version = 1; 2375 memcpy(super_block_v1.signature, "SPDKBLOB", sizeof(super_block_v1.signature)); 2376 super_block_v1.length = 0x1000; 2377 super_block_v1.clean = 1; 2378 super_block_v1.super_blob = 0xFFFFFFFFFFFFFFFF; 2379 super_block_v1.cluster_size = 0x100000; 2380 super_block_v1.used_page_mask_start = 0x01; 2381 super_block_v1.used_page_mask_len = 0x01; 2382 super_block_v1.used_cluster_mask_start = 0x02; 2383 super_block_v1.used_cluster_mask_len = 0x01; 2384 super_block_v1.md_start = 0x03; 2385 super_block_v1.md_len = 0x40; 2386 memset(super_block_v1.reserved, 0, 4036); 2387 super_block_v1.crc = _spdk_blob_md_page_calc_crc(&super_block_v1); 2388 memcpy(g_dev_buffer, &super_block_v1, sizeof(struct spdk_bs_super_block_ver1)); 2389 2390 memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype)); 2391 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2392 CU_ASSERT(g_bserrno == 0); 2393 2394 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2395 CU_ASSERT(g_bserrno == 0); 2396 g_bs = NULL; 2397 } 2398 2399 /* 2400 * Create a blobstore and then unload it. 2401 */ 2402 static void 2403 bs_unload(void) 2404 { 2405 struct spdk_bs_dev *dev; 2406 struct spdk_blob_store *bs; 2407 spdk_blob_id blobid; 2408 struct spdk_blob *blob; 2409 2410 dev = init_dev(); 2411 2412 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 2413 CU_ASSERT(g_bserrno == 0); 2414 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2415 bs = g_bs; 2416 2417 /* Create a blob and open it. */ 2418 g_bserrno = -1; 2419 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 2420 CU_ASSERT(g_bserrno == 0); 2421 CU_ASSERT(g_blobid > 0); 2422 blobid = g_blobid; 2423 2424 g_bserrno = -1; 2425 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 2426 CU_ASSERT(g_bserrno == 0); 2427 CU_ASSERT(g_blob != NULL); 2428 blob = g_blob; 2429 2430 /* Try to unload blobstore, should fail with open blob */ 2431 g_bserrno = -1; 2432 spdk_bs_unload(bs, bs_op_complete, NULL); 2433 CU_ASSERT(g_bserrno == -EBUSY); 2434 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2435 2436 /* Close the blob, then successfully unload blobstore */ 2437 g_bserrno = -1; 2438 spdk_blob_close(blob, blob_op_complete, NULL); 2439 CU_ASSERT(g_bserrno == 0); 2440 2441 g_bserrno = -1; 2442 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2443 CU_ASSERT(g_bserrno == 0); 2444 g_bs = NULL; 2445 } 2446 2447 /* 2448 * Create a blobstore with a cluster size different than the default, and ensure it is 2449 * persisted. 2450 */ 2451 static void 2452 bs_cluster_sz(void) 2453 { 2454 struct spdk_bs_dev *dev; 2455 struct spdk_bs_opts opts; 2456 uint32_t cluster_sz; 2457 2458 /* Set cluster size to zero */ 2459 dev = init_dev(); 2460 spdk_bs_opts_init(&opts); 2461 opts.cluster_sz = 0; 2462 2463 /* Initialize a new blob store */ 2464 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2465 CU_ASSERT(g_bserrno == -EINVAL); 2466 SPDK_CU_ASSERT_FATAL(g_bs == NULL); 2467 2468 /* 2469 * Set cluster size to blobstore page size, 2470 * to work it is required to be at least twice the blobstore page size. 2471 */ 2472 dev = init_dev(); 2473 spdk_bs_opts_init(&opts); 2474 opts.cluster_sz = SPDK_BS_PAGE_SIZE; 2475 2476 /* Initialize a new blob store */ 2477 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2478 CU_ASSERT(g_bserrno == -ENOMEM); 2479 SPDK_CU_ASSERT_FATAL(g_bs == NULL); 2480 2481 /* 2482 * Set cluster size to lower than page size, 2483 * to work it is required to be at least twice the blobstore page size. 2484 */ 2485 dev = init_dev(); 2486 spdk_bs_opts_init(&opts); 2487 opts.cluster_sz = SPDK_BS_PAGE_SIZE - 1; 2488 2489 /* Initialize a new blob store */ 2490 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2491 CU_ASSERT(g_bserrno == -EINVAL); 2492 SPDK_CU_ASSERT_FATAL(g_bs == NULL); 2493 2494 /* Set cluster size to twice the default */ 2495 dev = init_dev(); 2496 spdk_bs_opts_init(&opts); 2497 opts.cluster_sz *= 2; 2498 cluster_sz = opts.cluster_sz; 2499 2500 /* Initialize a new blob store */ 2501 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2502 CU_ASSERT(g_bserrno == 0); 2503 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2504 2505 CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz); 2506 2507 /* Unload the blob store */ 2508 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2509 CU_ASSERT(g_bserrno == 0); 2510 g_bs = NULL; 2511 g_blob = NULL; 2512 g_blobid = 0; 2513 2514 dev = init_dev(); 2515 /* Load an existing blob store */ 2516 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2517 CU_ASSERT(g_bserrno == 0); 2518 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2519 2520 CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz); 2521 2522 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2523 CU_ASSERT(g_bserrno == 0); 2524 g_bs = NULL; 2525 } 2526 2527 /* 2528 * Create a blobstore, reload it and ensure total usable cluster count 2529 * stays the same. 2530 */ 2531 static void 2532 bs_usable_clusters(void) 2533 { 2534 struct spdk_bs_dev *dev; 2535 struct spdk_bs_opts opts; 2536 uint32_t clusters; 2537 int i; 2538 2539 /* Init blobstore */ 2540 dev = init_dev(); 2541 spdk_bs_opts_init(&opts); 2542 2543 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2544 CU_ASSERT(g_bserrno == 0); 2545 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2546 2547 clusters = spdk_bs_total_data_cluster_count(g_bs); 2548 2549 /* Unload the blob store */ 2550 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2551 CU_ASSERT(g_bserrno == 0); 2552 g_bs = NULL; 2553 2554 dev = init_dev(); 2555 /* Load an existing blob store */ 2556 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2557 CU_ASSERT(g_bserrno == 0); 2558 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2559 2560 CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters); 2561 2562 /* Create and resize blobs to make sure that useable cluster count won't change */ 2563 for (i = 0; i < 4; i++) { 2564 g_bserrno = -1; 2565 g_blobid = SPDK_BLOBID_INVALID; 2566 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 2567 CU_ASSERT(g_bserrno == 0); 2568 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2569 2570 g_bserrno = -1; 2571 g_blob = NULL; 2572 spdk_bs_open_blob(g_bs, g_blobid, blob_op_with_handle_complete, NULL); 2573 CU_ASSERT(g_bserrno == 0); 2574 CU_ASSERT(g_blob != NULL); 2575 2576 spdk_blob_resize(g_blob, 10, blob_op_complete, NULL); 2577 CU_ASSERT(g_bserrno == 0); 2578 2579 g_bserrno = -1; 2580 spdk_blob_close(g_blob, blob_op_complete, NULL); 2581 CU_ASSERT(g_bserrno == 0); 2582 2583 CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters); 2584 } 2585 2586 /* Reload the blob store to make sure that nothing changed */ 2587 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2588 CU_ASSERT(g_bserrno == 0); 2589 g_bs = NULL; 2590 2591 dev = init_dev(); 2592 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2593 CU_ASSERT(g_bserrno == 0); 2594 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2595 2596 CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters); 2597 2598 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2599 CU_ASSERT(g_bserrno == 0); 2600 g_bs = NULL; 2601 } 2602 2603 /* 2604 * Test resizing of the metadata blob. This requires creating enough blobs 2605 * so that one cluster is not enough to fit the metadata for those blobs. 2606 * To induce this condition to happen more quickly, we reduce the cluster 2607 * size to 16KB, which means only 4 4KB blob metadata pages can fit. 2608 */ 2609 static void 2610 bs_resize_md(void) 2611 { 2612 const int CLUSTER_PAGE_COUNT = 4; 2613 const int NUM_BLOBS = CLUSTER_PAGE_COUNT * 4; 2614 struct spdk_bs_dev *dev; 2615 struct spdk_bs_opts opts; 2616 uint32_t cluster_sz; 2617 spdk_blob_id blobids[NUM_BLOBS]; 2618 int i; 2619 2620 2621 dev = init_dev(); 2622 spdk_bs_opts_init(&opts); 2623 opts.cluster_sz = CLUSTER_PAGE_COUNT * 4096; 2624 cluster_sz = opts.cluster_sz; 2625 2626 /* Initialize a new blob store */ 2627 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2628 CU_ASSERT(g_bserrno == 0); 2629 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2630 2631 CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz); 2632 2633 for (i = 0; i < NUM_BLOBS; i++) { 2634 g_bserrno = -1; 2635 g_blobid = SPDK_BLOBID_INVALID; 2636 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 2637 CU_ASSERT(g_bserrno == 0); 2638 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2639 blobids[i] = g_blobid; 2640 } 2641 2642 /* Unload the blob store */ 2643 g_bserrno = -1; 2644 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2645 CU_ASSERT(g_bserrno == 0); 2646 2647 /* Load an existing blob store */ 2648 g_bserrno = -1; 2649 g_bs = NULL; 2650 dev = init_dev(); 2651 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2652 CU_ASSERT(g_bserrno == 0); 2653 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2654 2655 CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz); 2656 2657 for (i = 0; i < NUM_BLOBS; i++) { 2658 g_bserrno = -1; 2659 g_blob = NULL; 2660 spdk_bs_open_blob(g_bs, blobids[i], blob_op_with_handle_complete, NULL); 2661 CU_ASSERT(g_bserrno == 0); 2662 CU_ASSERT(g_blob != NULL); 2663 g_bserrno = -1; 2664 spdk_blob_close(g_blob, blob_op_complete, NULL); 2665 CU_ASSERT(g_bserrno == 0); 2666 } 2667 2668 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2669 CU_ASSERT(g_bserrno == 0); 2670 g_bs = NULL; 2671 } 2672 2673 static void 2674 bs_destroy(void) 2675 { 2676 struct spdk_bs_dev *dev; 2677 struct spdk_bs_opts opts; 2678 2679 /* Initialize a new blob store */ 2680 dev = init_dev(); 2681 spdk_bs_opts_init(&opts); 2682 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2683 CU_ASSERT(g_bserrno == 0); 2684 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2685 2686 /* Destroy the blob store */ 2687 g_bserrno = -1; 2688 spdk_bs_destroy(g_bs, bs_op_complete, NULL); 2689 CU_ASSERT(g_bserrno == 0); 2690 2691 /* Loading an non-existent blob store should fail. */ 2692 g_bs = NULL; 2693 dev = init_dev(); 2694 2695 g_bserrno = 0; 2696 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2697 CU_ASSERT(g_bserrno != 0); 2698 } 2699 2700 /* Try to hit all of the corner cases associated with serializing 2701 * a blob to disk 2702 */ 2703 static void 2704 blob_serialize(void) 2705 { 2706 struct spdk_bs_dev *dev; 2707 struct spdk_bs_opts opts; 2708 struct spdk_blob_store *bs; 2709 spdk_blob_id blobid[2]; 2710 struct spdk_blob *blob[2]; 2711 uint64_t i; 2712 char *value; 2713 int rc; 2714 2715 dev = init_dev(); 2716 2717 /* Initialize a new blobstore with very small clusters */ 2718 spdk_bs_opts_init(&opts); 2719 opts.cluster_sz = dev->blocklen * 8; 2720 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 2721 CU_ASSERT(g_bserrno == 0); 2722 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2723 bs = g_bs; 2724 2725 /* Create and open two blobs */ 2726 for (i = 0; i < 2; i++) { 2727 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 2728 CU_ASSERT(g_bserrno == 0); 2729 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2730 blobid[i] = g_blobid; 2731 2732 /* Open a blob */ 2733 spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL); 2734 CU_ASSERT(g_bserrno == 0); 2735 CU_ASSERT(g_blob != NULL); 2736 blob[i] = g_blob; 2737 2738 /* Set a fairly large xattr on both blobs to eat up 2739 * metadata space 2740 */ 2741 value = calloc(dev->blocklen - 64, sizeof(char)); 2742 SPDK_CU_ASSERT_FATAL(value != NULL); 2743 memset(value, i, dev->blocklen / 2); 2744 rc = spdk_blob_set_xattr(blob[i], "name", value, dev->blocklen - 64); 2745 CU_ASSERT(rc == 0); 2746 free(value); 2747 } 2748 2749 /* Resize the blobs, alternating 1 cluster at a time. 2750 * This thwarts run length encoding and will cause spill 2751 * over of the extents. 2752 */ 2753 for (i = 0; i < 6; i++) { 2754 spdk_blob_resize(blob[i % 2], (i / 2) + 1, blob_op_complete, NULL); 2755 CU_ASSERT(g_bserrno == 0); 2756 } 2757 2758 for (i = 0; i < 2; i++) { 2759 spdk_blob_sync_md(blob[i], blob_op_complete, NULL); 2760 CU_ASSERT(g_bserrno == 0); 2761 } 2762 2763 /* Close the blobs */ 2764 for (i = 0; i < 2; i++) { 2765 spdk_blob_close(blob[i], blob_op_complete, NULL); 2766 CU_ASSERT(g_bserrno == 0); 2767 } 2768 2769 /* Unload the blobstore */ 2770 spdk_bs_unload(bs, bs_op_complete, NULL); 2771 CU_ASSERT(g_bserrno == 0); 2772 g_bs = NULL; 2773 g_blob = NULL; 2774 g_blobid = 0; 2775 bs = NULL; 2776 2777 dev = init_dev(); 2778 /* Load an existing blob store */ 2779 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2780 CU_ASSERT(g_bserrno == 0); 2781 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2782 bs = g_bs; 2783 2784 for (i = 0; i < 2; i++) { 2785 blob[i] = NULL; 2786 2787 spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL); 2788 CU_ASSERT(g_bserrno == 0); 2789 CU_ASSERT(g_blob != NULL); 2790 blob[i] = g_blob; 2791 2792 CU_ASSERT(spdk_blob_get_num_clusters(blob[i]) == 3); 2793 2794 spdk_blob_close(blob[i], blob_op_complete, NULL); 2795 CU_ASSERT(g_bserrno == 0); 2796 } 2797 2798 spdk_bs_unload(bs, bs_op_complete, NULL); 2799 CU_ASSERT(g_bserrno == 0); 2800 g_bs = NULL; 2801 } 2802 2803 static void 2804 blob_crc(void) 2805 { 2806 struct spdk_blob_store *bs; 2807 struct spdk_bs_dev *dev; 2808 struct spdk_blob *blob; 2809 spdk_blob_id blobid; 2810 uint32_t page_num; 2811 int index; 2812 struct spdk_blob_md_page *page; 2813 2814 dev = init_dev(); 2815 2816 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 2817 CU_ASSERT(g_bserrno == 0); 2818 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2819 bs = g_bs; 2820 2821 spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL); 2822 CU_ASSERT(g_bserrno == 0); 2823 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2824 blobid = g_blobid; 2825 2826 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 2827 CU_ASSERT(g_bserrno == 0); 2828 CU_ASSERT(g_blob != NULL); 2829 blob = g_blob; 2830 2831 spdk_blob_close(blob, blob_op_complete, NULL); 2832 CU_ASSERT(g_bserrno == 0); 2833 2834 page_num = _spdk_bs_blobid_to_page(blobid); 2835 index = DEV_BUFFER_BLOCKLEN * (bs->md_start + page_num); 2836 page = (struct spdk_blob_md_page *)&g_dev_buffer[index]; 2837 page->crc = 0; 2838 2839 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 2840 CU_ASSERT(g_bserrno == -EINVAL); 2841 CU_ASSERT(g_blob == NULL); 2842 g_bserrno = 0; 2843 2844 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 2845 CU_ASSERT(g_bserrno == -EINVAL); 2846 2847 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2848 CU_ASSERT(g_bserrno == 0); 2849 g_bs = NULL; 2850 } 2851 2852 static void 2853 super_block_crc(void) 2854 { 2855 struct spdk_bs_dev *dev; 2856 struct spdk_bs_super_block *super_block; 2857 struct spdk_bs_opts opts; 2858 2859 dev = init_dev(); 2860 spdk_bs_opts_init(&opts); 2861 2862 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 2863 CU_ASSERT(g_bserrno == 0); 2864 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2865 2866 spdk_bs_unload(g_bs, bs_op_complete, NULL); 2867 CU_ASSERT(g_bserrno == 0); 2868 g_bs = NULL; 2869 2870 super_block = (struct spdk_bs_super_block *)g_dev_buffer; 2871 super_block->crc = 0; 2872 dev = init_dev(); 2873 2874 /* Load an existing blob store */ 2875 g_bserrno = 0; 2876 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2877 CU_ASSERT(g_bserrno == -EILSEQ); 2878 } 2879 2880 /* For blob dirty shutdown test case we do the following sub-test cases: 2881 * 1 Initialize new blob store and create 1 super blob with some xattrs, then we 2882 * dirty shutdown and reload the blob store and verify the xattrs. 2883 * 2 Resize the blob from 10 clusters to 20 clusters and then dirty shutdown, 2884 * reload the blob store and verify the clusters number. 2885 * 3 Create the second blob and then dirty shutdown, reload the blob store 2886 * and verify the second blob. 2887 * 4 Delete the second blob and then dirty shutdown, reload the blob store 2888 * and verify the second blob is invalid. 2889 * 5 Create the second blob again and also create the third blob, modify the 2890 * md of second blob which makes the md invalid, and then dirty shutdown, 2891 * reload the blob store verify the second blob, it should invalid and also 2892 * verify the third blob, it should correct. 2893 */ 2894 static void 2895 blob_dirty_shutdown(void) 2896 { 2897 int rc; 2898 int index; 2899 struct spdk_bs_dev *dev; 2900 spdk_blob_id blobid1, blobid2, blobid3; 2901 struct spdk_blob *blob; 2902 uint64_t length; 2903 uint64_t free_clusters; 2904 const void *value; 2905 size_t value_len; 2906 uint32_t page_num; 2907 struct spdk_blob_md_page *page; 2908 struct spdk_bs_opts opts; 2909 2910 dev = init_dev(); 2911 spdk_bs_opts_init(&opts); 2912 /* Initialize a new blob store */ 2913 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 2914 CU_ASSERT(g_bserrno == 0); 2915 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 2916 2917 /* Create first blob */ 2918 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 2919 CU_ASSERT(g_bserrno == 0); 2920 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 2921 blobid1 = g_blobid; 2922 2923 spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL); 2924 CU_ASSERT(g_bserrno == 0); 2925 CU_ASSERT(g_blob != NULL); 2926 blob = g_blob; 2927 2928 /* Set some xattrs */ 2929 rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1); 2930 CU_ASSERT(rc == 0); 2931 2932 length = 2345; 2933 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 2934 CU_ASSERT(rc == 0); 2935 2936 /* Resize the blob */ 2937 spdk_blob_resize(blob, 10, blob_op_complete, NULL); 2938 CU_ASSERT(g_bserrno == 0); 2939 2940 /* Set the blob as the super blob */ 2941 spdk_bs_set_super(g_bs, blobid1, blob_op_complete, NULL); 2942 CU_ASSERT(g_bserrno == 0); 2943 2944 free_clusters = spdk_bs_free_cluster_count(g_bs); 2945 2946 spdk_blob_close(blob, blob_op_complete, NULL); 2947 blob = NULL; 2948 g_blob = NULL; 2949 g_blobid = SPDK_BLOBID_INVALID; 2950 2951 /* Dirty shutdown */ 2952 _spdk_bs_free(g_bs); 2953 2954 /* reload blobstore */ 2955 dev = init_dev(); 2956 spdk_bs_opts_init(&opts); 2957 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 2958 CU_ASSERT(g_bserrno == 0); 2959 2960 /* Get the super blob */ 2961 spdk_bs_get_super(g_bs, blob_op_with_id_complete, NULL); 2962 CU_ASSERT(g_bserrno == 0); 2963 CU_ASSERT(blobid1 == g_blobid); 2964 2965 spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL); 2966 CU_ASSERT(g_bserrno == 0); 2967 CU_ASSERT(g_blob != NULL); 2968 blob = g_blob; 2969 2970 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs)); 2971 2972 /* Get the xattrs */ 2973 value = NULL; 2974 rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len); 2975 CU_ASSERT(rc == 0); 2976 SPDK_CU_ASSERT_FATAL(value != NULL); 2977 CU_ASSERT(*(uint64_t *)value == length); 2978 CU_ASSERT(value_len == 8); 2979 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10); 2980 2981 /* Resize the blob */ 2982 spdk_blob_resize(blob, 20, blob_op_complete, NULL); 2983 CU_ASSERT(g_bserrno == 0); 2984 2985 free_clusters = spdk_bs_free_cluster_count(g_bs); 2986 2987 spdk_blob_close(blob, blob_op_complete, NULL); 2988 CU_ASSERT(g_bserrno == 0); 2989 blob = NULL; 2990 g_blob = NULL; 2991 g_blobid = SPDK_BLOBID_INVALID; 2992 2993 /* Dirty shutdown */ 2994 _spdk_bs_free(g_bs); 2995 2996 /* reload the blobstore */ 2997 dev = init_dev(); 2998 spdk_bs_opts_init(&opts); 2999 /* Load an existing blob store */ 3000 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3001 CU_ASSERT(g_bserrno == 0); 3002 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3003 spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL); 3004 CU_ASSERT(g_bserrno == 0); 3005 CU_ASSERT(g_blob != NULL); 3006 blob = g_blob; 3007 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 20); 3008 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs)); 3009 3010 spdk_blob_close(blob, blob_op_complete, NULL); 3011 CU_ASSERT(g_bserrno == 0); 3012 blob = NULL; 3013 g_blob = NULL; 3014 g_blobid = SPDK_BLOBID_INVALID; 3015 3016 /* Create second blob */ 3017 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3018 CU_ASSERT(g_bserrno == 0); 3019 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3020 blobid2 = g_blobid; 3021 3022 spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL); 3023 CU_ASSERT(g_bserrno == 0); 3024 CU_ASSERT(g_blob != NULL); 3025 blob = g_blob; 3026 3027 /* Set some xattrs */ 3028 rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1); 3029 CU_ASSERT(rc == 0); 3030 3031 length = 5432; 3032 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 3033 CU_ASSERT(rc == 0); 3034 3035 /* Resize the blob */ 3036 spdk_blob_resize(blob, 10, blob_op_complete, NULL); 3037 CU_ASSERT(g_bserrno == 0); 3038 3039 free_clusters = spdk_bs_free_cluster_count(g_bs); 3040 3041 spdk_blob_close(blob, blob_op_complete, NULL); 3042 blob = NULL; 3043 g_blob = NULL; 3044 g_blobid = SPDK_BLOBID_INVALID; 3045 3046 /* Dirty shutdown */ 3047 _spdk_bs_free(g_bs); 3048 3049 /* reload the blobstore */ 3050 dev = init_dev(); 3051 spdk_bs_opts_init(&opts); 3052 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3053 CU_ASSERT(g_bserrno == 0); 3054 3055 spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL); 3056 CU_ASSERT(g_bserrno == 0); 3057 CU_ASSERT(g_blob != NULL); 3058 blob = g_blob; 3059 3060 /* Get the xattrs */ 3061 value = NULL; 3062 rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len); 3063 CU_ASSERT(rc == 0); 3064 SPDK_CU_ASSERT_FATAL(value != NULL); 3065 CU_ASSERT(*(uint64_t *)value == length); 3066 CU_ASSERT(value_len == 8); 3067 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10); 3068 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs)); 3069 3070 spdk_blob_close(blob, blob_op_complete, NULL); 3071 CU_ASSERT(g_bserrno == 0); 3072 spdk_bs_delete_blob(g_bs, blobid2, blob_op_complete, NULL); 3073 CU_ASSERT(g_bserrno == 0); 3074 3075 free_clusters = spdk_bs_free_cluster_count(g_bs); 3076 3077 /* Dirty shutdown */ 3078 _spdk_bs_free(g_bs); 3079 /* reload the blobstore */ 3080 dev = init_dev(); 3081 spdk_bs_opts_init(&opts); 3082 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3083 CU_ASSERT(g_bserrno == 0); 3084 3085 spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL); 3086 CU_ASSERT(g_bserrno != 0); 3087 CU_ASSERT(g_blob == NULL); 3088 3089 spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL); 3090 CU_ASSERT(g_bserrno == 0); 3091 CU_ASSERT(g_blob != NULL); 3092 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs)); 3093 spdk_blob_close(g_blob, blob_op_complete, NULL); 3094 CU_ASSERT(g_bserrno == 0); 3095 3096 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3097 CU_ASSERT(g_bserrno == 0); 3098 g_bs = NULL; 3099 3100 /* reload the blobstore */ 3101 dev = init_dev(); 3102 spdk_bs_opts_init(&opts); 3103 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3104 CU_ASSERT(g_bserrno == 0); 3105 3106 /* Create second blob */ 3107 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3108 CU_ASSERT(g_bserrno == 0); 3109 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3110 blobid2 = g_blobid; 3111 3112 /* Create third blob */ 3113 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3114 CU_ASSERT(g_bserrno == 0); 3115 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3116 blobid3 = g_blobid; 3117 3118 spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL); 3119 CU_ASSERT(g_bserrno == 0); 3120 CU_ASSERT(g_blob != NULL); 3121 blob = g_blob; 3122 3123 /* Set some xattrs for second blob */ 3124 rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1); 3125 CU_ASSERT(rc == 0); 3126 3127 length = 5432; 3128 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 3129 CU_ASSERT(rc == 0); 3130 3131 spdk_blob_close(blob, blob_op_complete, NULL); 3132 blob = NULL; 3133 g_blob = NULL; 3134 g_blobid = SPDK_BLOBID_INVALID; 3135 3136 spdk_bs_open_blob(g_bs, blobid3, blob_op_with_handle_complete, NULL); 3137 CU_ASSERT(g_bserrno == 0); 3138 CU_ASSERT(g_blob != NULL); 3139 blob = g_blob; 3140 3141 /* Set some xattrs for third blob */ 3142 rc = spdk_blob_set_xattr(blob, "name", "log2.txt", strlen("log2.txt") + 1); 3143 CU_ASSERT(rc == 0); 3144 3145 length = 5432; 3146 rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length)); 3147 CU_ASSERT(rc == 0); 3148 3149 spdk_blob_close(blob, blob_op_complete, NULL); 3150 blob = NULL; 3151 g_blob = NULL; 3152 g_blobid = SPDK_BLOBID_INVALID; 3153 3154 /* Mark second blob as invalid */ 3155 page_num = _spdk_bs_blobid_to_page(blobid2); 3156 3157 index = DEV_BUFFER_BLOCKLEN * (g_bs->md_start + page_num); 3158 page = (struct spdk_blob_md_page *)&g_dev_buffer[index]; 3159 page->sequence_num = 1; 3160 page->crc = _spdk_blob_md_page_calc_crc(page); 3161 3162 free_clusters = spdk_bs_free_cluster_count(g_bs); 3163 3164 /* Dirty shutdown */ 3165 _spdk_bs_free(g_bs); 3166 /* reload the blobstore */ 3167 dev = init_dev(); 3168 spdk_bs_opts_init(&opts); 3169 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3170 CU_ASSERT(g_bserrno == 0); 3171 3172 spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL); 3173 CU_ASSERT(g_bserrno != 0); 3174 CU_ASSERT(g_blob == NULL); 3175 3176 spdk_bs_open_blob(g_bs, blobid3, blob_op_with_handle_complete, NULL); 3177 CU_ASSERT(g_bserrno == 0); 3178 CU_ASSERT(g_blob != NULL); 3179 blob = g_blob; 3180 3181 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs)); 3182 3183 spdk_blob_close(blob, blob_op_complete, NULL); 3184 blob = NULL; 3185 g_blob = NULL; 3186 g_blobid = SPDK_BLOBID_INVALID; 3187 3188 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3189 CU_ASSERT(g_bserrno == 0); 3190 g_bs = NULL; 3191 } 3192 3193 static void 3194 blob_flags(void) 3195 { 3196 struct spdk_bs_dev *dev; 3197 spdk_blob_id blobid_invalid, blobid_data_ro, blobid_md_ro; 3198 struct spdk_blob *blob_invalid, *blob_data_ro, *blob_md_ro; 3199 struct spdk_bs_opts opts; 3200 int rc; 3201 3202 dev = init_dev(); 3203 spdk_bs_opts_init(&opts); 3204 3205 /* Initialize a new blob store */ 3206 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 3207 CU_ASSERT(g_bserrno == 0); 3208 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3209 3210 /* Create three blobs - one each for testing invalid, data_ro and md_ro flags. */ 3211 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3212 CU_ASSERT(g_bserrno == 0); 3213 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3214 blobid_invalid = g_blobid; 3215 3216 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3217 CU_ASSERT(g_bserrno == 0); 3218 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3219 blobid_data_ro = g_blobid; 3220 3221 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3222 CU_ASSERT(g_bserrno == 0); 3223 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3224 blobid_md_ro = g_blobid; 3225 3226 spdk_bs_open_blob(g_bs, blobid_invalid, blob_op_with_handle_complete, NULL); 3227 CU_ASSERT(g_bserrno == 0); 3228 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3229 blob_invalid = g_blob; 3230 3231 spdk_bs_open_blob(g_bs, blobid_data_ro, blob_op_with_handle_complete, NULL); 3232 CU_ASSERT(g_bserrno == 0); 3233 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3234 blob_data_ro = g_blob; 3235 3236 spdk_bs_open_blob(g_bs, blobid_md_ro, blob_op_with_handle_complete, NULL); 3237 CU_ASSERT(g_bserrno == 0); 3238 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3239 blob_md_ro = g_blob; 3240 3241 /* Change the size of blob_data_ro to check if flags are serialized 3242 * when blob has non zero number of extents */ 3243 spdk_blob_resize(blob_data_ro, 10, blob_op_complete, NULL); 3244 CU_ASSERT(g_bserrno == 0); 3245 3246 /* Set the xattr to check if flags are serialized 3247 * when blob has non zero number of xattrs */ 3248 rc = spdk_blob_set_xattr(blob_md_ro, "name", "log.txt", strlen("log.txt") + 1); 3249 CU_ASSERT(rc == 0); 3250 3251 blob_invalid->invalid_flags = (1ULL << 63); 3252 blob_invalid->state = SPDK_BLOB_STATE_DIRTY; 3253 blob_data_ro->data_ro_flags = (1ULL << 62); 3254 blob_data_ro->state = SPDK_BLOB_STATE_DIRTY; 3255 blob_md_ro->md_ro_flags = (1ULL << 61); 3256 blob_md_ro->state = SPDK_BLOB_STATE_DIRTY; 3257 3258 g_bserrno = -1; 3259 spdk_blob_sync_md(blob_invalid, blob_op_complete, NULL); 3260 CU_ASSERT(g_bserrno == 0); 3261 g_bserrno = -1; 3262 spdk_blob_sync_md(blob_data_ro, blob_op_complete, NULL); 3263 CU_ASSERT(g_bserrno == 0); 3264 g_bserrno = -1; 3265 spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL); 3266 CU_ASSERT(g_bserrno == 0); 3267 3268 g_bserrno = -1; 3269 spdk_blob_close(blob_invalid, blob_op_complete, NULL); 3270 CU_ASSERT(g_bserrno == 0); 3271 blob_invalid = NULL; 3272 g_bserrno = -1; 3273 spdk_blob_close(blob_data_ro, blob_op_complete, NULL); 3274 CU_ASSERT(g_bserrno == 0); 3275 blob_data_ro = NULL; 3276 g_bserrno = -1; 3277 spdk_blob_close(blob_md_ro, blob_op_complete, NULL); 3278 CU_ASSERT(g_bserrno == 0); 3279 blob_md_ro = NULL; 3280 3281 g_blob = NULL; 3282 g_blobid = SPDK_BLOBID_INVALID; 3283 3284 /* Unload the blob store */ 3285 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3286 CU_ASSERT(g_bserrno == 0); 3287 g_bs = NULL; 3288 3289 /* Load an existing blob store */ 3290 dev = init_dev(); 3291 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3292 CU_ASSERT(g_bserrno == 0); 3293 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3294 3295 g_blob = NULL; 3296 g_bserrno = 0; 3297 spdk_bs_open_blob(g_bs, blobid_invalid, blob_op_with_handle_complete, NULL); 3298 CU_ASSERT(g_bserrno != 0); 3299 CU_ASSERT(g_blob == NULL); 3300 3301 g_blob = NULL; 3302 g_bserrno = -1; 3303 spdk_bs_open_blob(g_bs, blobid_data_ro, blob_op_with_handle_complete, NULL); 3304 CU_ASSERT(g_bserrno == 0); 3305 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3306 blob_data_ro = g_blob; 3307 /* If an unknown data_ro flag was found, the blob should be marked both data and md read-only. */ 3308 CU_ASSERT(blob_data_ro->data_ro == true); 3309 CU_ASSERT(blob_data_ro->md_ro == true); 3310 CU_ASSERT(spdk_blob_get_num_clusters(blob_data_ro) == 10); 3311 3312 g_blob = NULL; 3313 g_bserrno = -1; 3314 spdk_bs_open_blob(g_bs, blobid_md_ro, blob_op_with_handle_complete, NULL); 3315 CU_ASSERT(g_bserrno == 0); 3316 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3317 blob_md_ro = g_blob; 3318 CU_ASSERT(blob_md_ro->data_ro == false); 3319 CU_ASSERT(blob_md_ro->md_ro == true); 3320 3321 g_bserrno = -1; 3322 spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL); 3323 CU_ASSERT(g_bserrno == 0); 3324 3325 spdk_blob_close(blob_data_ro, blob_op_complete, NULL); 3326 CU_ASSERT(g_bserrno == 0); 3327 spdk_blob_close(blob_md_ro, blob_op_complete, NULL); 3328 CU_ASSERT(g_bserrno == 0); 3329 3330 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3331 CU_ASSERT(g_bserrno == 0); 3332 } 3333 3334 static void 3335 bs_version(void) 3336 { 3337 struct spdk_bs_super_block *super; 3338 struct spdk_bs_dev *dev; 3339 struct spdk_bs_opts opts; 3340 spdk_blob_id blobid; 3341 3342 dev = init_dev(); 3343 spdk_bs_opts_init(&opts); 3344 3345 /* Initialize a new blob store */ 3346 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 3347 CU_ASSERT(g_bserrno == 0); 3348 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3349 3350 /* Unload the blob store */ 3351 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3352 CU_ASSERT(g_bserrno == 0); 3353 g_bs = NULL; 3354 3355 /* 3356 * Change the bs version on disk. This will allow us to 3357 * test that the version does not get modified automatically 3358 * when loading and unloading the blobstore. 3359 */ 3360 super = (struct spdk_bs_super_block *)&g_dev_buffer[0]; 3361 CU_ASSERT(super->version == SPDK_BS_VERSION); 3362 CU_ASSERT(super->clean == 1); 3363 super->version = 2; 3364 /* 3365 * Version 2 metadata does not have a used blobid mask, so clear 3366 * those fields in the super block and zero the corresponding 3367 * region on "disk". We will use this to ensure blob IDs are 3368 * correctly reconstructed. 3369 */ 3370 memset(&g_dev_buffer[super->used_blobid_mask_start * SPDK_BS_PAGE_SIZE], 0, 3371 super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE); 3372 super->used_blobid_mask_start = 0; 3373 super->used_blobid_mask_len = 0; 3374 super->crc = _spdk_blob_md_page_calc_crc(super); 3375 3376 /* Load an existing blob store */ 3377 dev = init_dev(); 3378 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3379 CU_ASSERT(g_bserrno == 0); 3380 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3381 CU_ASSERT(super->clean == 1); 3382 3383 /* 3384 * Create a blob - just to make sure that when we unload it 3385 * results in writing the super block (since metadata pages 3386 * were allocated. 3387 */ 3388 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3389 CU_ASSERT(g_bserrno == 0); 3390 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3391 blobid = g_blobid; 3392 3393 /* Unload the blob store */ 3394 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3395 CU_ASSERT(g_bserrno == 0); 3396 g_bs = NULL; 3397 CU_ASSERT(super->version == 2); 3398 CU_ASSERT(super->used_blobid_mask_start == 0); 3399 CU_ASSERT(super->used_blobid_mask_len == 0); 3400 3401 dev = init_dev(); 3402 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3403 CU_ASSERT(g_bserrno == 0); 3404 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3405 3406 g_blob = NULL; 3407 spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL); 3408 CU_ASSERT(g_bserrno == 0); 3409 CU_ASSERT(g_blob != NULL); 3410 3411 spdk_blob_close(g_blob, blob_op_complete, NULL); 3412 CU_ASSERT(g_bserrno == 0); 3413 3414 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3415 CU_ASSERT(g_bserrno == 0); 3416 g_bs = NULL; 3417 CU_ASSERT(super->version == 2); 3418 CU_ASSERT(super->used_blobid_mask_start == 0); 3419 CU_ASSERT(super->used_blobid_mask_len == 0); 3420 } 3421 3422 static void 3423 blob_set_xattrs(void) 3424 { 3425 struct spdk_blob_store *bs; 3426 struct spdk_bs_dev *dev; 3427 struct spdk_blob *blob; 3428 struct spdk_blob_opts opts; 3429 spdk_blob_id blobid; 3430 const void *value; 3431 size_t value_len; 3432 int rc; 3433 3434 dev = init_dev(); 3435 3436 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 3437 CU_ASSERT(g_bserrno == 0); 3438 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3439 bs = g_bs; 3440 3441 /* Create blob with extra attributes */ 3442 spdk_blob_opts_init(&opts); 3443 3444 opts.xattrs.names = g_xattr_names; 3445 opts.xattrs.get_value = _get_xattr_value; 3446 opts.xattrs.count = 3; 3447 opts.xattrs.ctx = &g_ctx; 3448 3449 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3450 CU_ASSERT(g_bserrno == 0); 3451 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3452 blobid = g_blobid; 3453 3454 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 3455 CU_ASSERT(g_bserrno == 0); 3456 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3457 blob = g_blob; 3458 3459 /* Get the xattrs */ 3460 value = NULL; 3461 3462 rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len); 3463 CU_ASSERT(rc == 0); 3464 SPDK_CU_ASSERT_FATAL(value != NULL); 3465 CU_ASSERT(value_len == strlen(g_xattr_values[0])); 3466 CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len); 3467 3468 rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len); 3469 CU_ASSERT(rc == 0); 3470 SPDK_CU_ASSERT_FATAL(value != NULL); 3471 CU_ASSERT(value_len == strlen(g_xattr_values[1])); 3472 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len); 3473 3474 rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len); 3475 CU_ASSERT(rc == 0); 3476 SPDK_CU_ASSERT_FATAL(value != NULL); 3477 CU_ASSERT(value_len == strlen(g_xattr_values[2])); 3478 CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len); 3479 3480 /* Try to get non existing attribute */ 3481 3482 rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len); 3483 CU_ASSERT(rc == -ENOENT); 3484 3485 spdk_blob_close(blob, blob_op_complete, NULL); 3486 CU_ASSERT(g_bserrno == 0); 3487 blob = NULL; 3488 g_blob = NULL; 3489 g_blobid = SPDK_BLOBID_INVALID; 3490 3491 /* NULL callback */ 3492 spdk_blob_opts_init(&opts); 3493 opts.xattrs.names = g_xattr_names; 3494 opts.xattrs.get_value = NULL; 3495 opts.xattrs.count = 1; 3496 opts.xattrs.ctx = &g_ctx; 3497 3498 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3499 CU_ASSERT(g_bserrno == -EINVAL); 3500 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3501 3502 /* NULL values */ 3503 spdk_blob_opts_init(&opts); 3504 opts.xattrs.names = g_xattr_names; 3505 opts.xattrs.get_value = _get_xattr_value_null; 3506 opts.xattrs.count = 1; 3507 opts.xattrs.ctx = NULL; 3508 3509 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3510 CU_ASSERT(g_bserrno == -EINVAL); 3511 3512 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3513 CU_ASSERT(g_bserrno == 0); 3514 g_bs = NULL; 3515 3516 } 3517 3518 static void 3519 blob_thin_prov_alloc(void) 3520 { 3521 struct spdk_blob_store *bs; 3522 struct spdk_bs_dev *dev; 3523 struct spdk_blob *blob; 3524 struct spdk_blob_opts opts; 3525 spdk_blob_id blobid; 3526 uint64_t free_clusters; 3527 3528 dev = init_dev(); 3529 3530 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 3531 CU_ASSERT(g_bserrno == 0); 3532 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3533 bs = g_bs; 3534 free_clusters = spdk_bs_free_cluster_count(bs); 3535 3536 /* Set blob as thin provisioned */ 3537 spdk_blob_opts_init(&opts); 3538 opts.thin_provision = true; 3539 3540 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3541 CU_ASSERT(g_bserrno == 0); 3542 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3543 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3544 blobid = g_blobid; 3545 3546 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 3547 CU_ASSERT(g_bserrno == 0); 3548 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3549 blob = g_blob; 3550 3551 CU_ASSERT(blob->active.num_clusters == 0); 3552 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0); 3553 3554 /* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */ 3555 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 3556 CU_ASSERT(g_bserrno == 0); 3557 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3558 CU_ASSERT(blob->active.num_clusters == 5); 3559 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5); 3560 3561 /* Grow it to 1TB - still unallocated */ 3562 spdk_blob_resize(blob, 262144, blob_op_complete, NULL); 3563 CU_ASSERT(g_bserrno == 0); 3564 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3565 CU_ASSERT(blob->active.num_clusters == 262144); 3566 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 262144); 3567 3568 spdk_blob_sync_md(blob, blob_op_complete, NULL); 3569 CU_ASSERT(g_bserrno == 0); 3570 /* Sync must not change anything */ 3571 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3572 CU_ASSERT(blob->active.num_clusters == 262144); 3573 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 262144); 3574 /* Since clusters are not allocated, 3575 * number of metadata pages is expected to be minimal. 3576 */ 3577 CU_ASSERT(blob->active.num_pages == 1); 3578 3579 /* Shrink the blob to 3 clusters - still unallocated */ 3580 spdk_blob_resize(blob, 3, blob_op_complete, NULL); 3581 CU_ASSERT(g_bserrno == 0); 3582 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3583 CU_ASSERT(blob->active.num_clusters == 3); 3584 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3); 3585 3586 spdk_blob_sync_md(blob, blob_op_complete, NULL); 3587 CU_ASSERT(g_bserrno == 0); 3588 /* Sync must not change anything */ 3589 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3590 CU_ASSERT(blob->active.num_clusters == 3); 3591 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3); 3592 3593 spdk_blob_close(blob, blob_op_complete, NULL); 3594 CU_ASSERT(g_bserrno == 0); 3595 3596 /* Unload the blob store */ 3597 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3598 CU_ASSERT(g_bserrno == 0); 3599 g_bs = NULL; 3600 g_blob = NULL; 3601 g_blobid = 0; 3602 3603 /* Load an existing blob store */ 3604 dev = init_dev(); 3605 spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL); 3606 CU_ASSERT(g_bserrno == 0); 3607 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3608 3609 bs = g_bs; 3610 3611 spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL); 3612 CU_ASSERT(g_bserrno == 0); 3613 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3614 blob = g_blob; 3615 3616 /* Check that clusters allocation and size is still the same */ 3617 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3618 CU_ASSERT(blob->active.num_clusters == 3); 3619 3620 spdk_blob_close(blob, blob_op_complete, NULL); 3621 CU_ASSERT(g_bserrno == 0); 3622 3623 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 3624 CU_ASSERT(g_bserrno == 0); 3625 3626 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3627 CU_ASSERT(g_bserrno == 0); 3628 g_bs = NULL; 3629 } 3630 3631 static void 3632 blob_insert_cluster_msg(void) 3633 { 3634 struct spdk_blob_store *bs; 3635 struct spdk_bs_dev *dev; 3636 struct spdk_blob *blob; 3637 struct spdk_blob_opts opts; 3638 spdk_blob_id blobid; 3639 uint64_t free_clusters; 3640 3641 dev = init_dev(); 3642 3643 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 3644 CU_ASSERT(g_bserrno == 0); 3645 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3646 bs = g_bs; 3647 free_clusters = spdk_bs_free_cluster_count(bs); 3648 3649 /* Set blob as thin provisioned */ 3650 spdk_blob_opts_init(&opts); 3651 opts.thin_provision = true; 3652 opts.num_clusters = 4; 3653 3654 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3655 CU_ASSERT(g_bserrno == 0); 3656 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3657 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3658 blobid = g_blobid; 3659 3660 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 3661 CU_ASSERT(g_bserrno == 0); 3662 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3663 blob = g_blob; 3664 3665 CU_ASSERT(blob->active.num_clusters == 4); 3666 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 4); 3667 CU_ASSERT(blob->active.clusters[1] == 0); 3668 3669 _spdk_bs_claim_cluster(bs, 0xF); 3670 _spdk_blob_insert_cluster_on_md_thread(blob, 1, 0xF, blob_op_complete, NULL); 3671 3672 CU_ASSERT(blob->active.clusters[1] != 0); 3673 3674 spdk_blob_close(blob, blob_op_complete, NULL); 3675 CU_ASSERT(g_bserrno == 0); 3676 3677 /* Unload the blob store */ 3678 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3679 CU_ASSERT(g_bserrno == 0); 3680 g_bs = NULL; 3681 g_blob = NULL; 3682 g_blobid = 0; 3683 3684 /* Load an existing blob store */ 3685 dev = init_dev(); 3686 spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL); 3687 CU_ASSERT(g_bserrno == 0); 3688 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3689 3690 bs = g_bs; 3691 3692 spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL); 3693 CU_ASSERT(g_bserrno == 0); 3694 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3695 blob = g_blob; 3696 3697 CU_ASSERT(blob->active.clusters[1] != 0); 3698 3699 spdk_blob_close(blob, blob_op_complete, NULL); 3700 CU_ASSERT(g_bserrno == 0); 3701 3702 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 3703 CU_ASSERT(g_bserrno == 0); 3704 3705 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3706 CU_ASSERT(g_bserrno == 0); 3707 g_bs = NULL; 3708 } 3709 3710 static void 3711 blob_thin_prov_rw(void) 3712 { 3713 static const uint8_t zero[10 * 4096] = { 0 }; 3714 struct spdk_blob_store *bs; 3715 struct spdk_bs_dev *dev; 3716 struct spdk_blob *blob; 3717 struct spdk_io_channel *channel; 3718 struct spdk_blob_opts opts; 3719 spdk_blob_id blobid; 3720 uint64_t free_clusters; 3721 uint64_t page_size; 3722 uint8_t payload_read[10 * 4096]; 3723 uint8_t payload_write[10 * 4096]; 3724 uint64_t write_bytes; 3725 uint64_t read_bytes; 3726 3727 dev = init_dev(); 3728 3729 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 3730 CU_ASSERT(g_bserrno == 0); 3731 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3732 bs = g_bs; 3733 free_clusters = spdk_bs_free_cluster_count(bs); 3734 page_size = spdk_bs_get_page_size(bs); 3735 3736 channel = spdk_bs_alloc_io_channel(bs); 3737 CU_ASSERT(channel != NULL); 3738 3739 spdk_blob_opts_init(&opts); 3740 opts.thin_provision = true; 3741 3742 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3743 CU_ASSERT(g_bserrno == 0); 3744 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3745 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3746 blobid = g_blobid; 3747 3748 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 3749 CU_ASSERT(g_bserrno == 0); 3750 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3751 blob = g_blob; 3752 3753 CU_ASSERT(blob->active.num_clusters == 0); 3754 3755 /* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */ 3756 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 3757 CU_ASSERT(g_bserrno == 0); 3758 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3759 CU_ASSERT(blob->active.num_clusters == 5); 3760 3761 spdk_blob_sync_md(blob, blob_op_complete, NULL); 3762 CU_ASSERT(g_bserrno == 0); 3763 /* Sync must not change anything */ 3764 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3765 CU_ASSERT(blob->active.num_clusters == 5); 3766 3767 /* Payload should be all zeros from unallocated clusters */ 3768 memset(payload_read, 0xFF, sizeof(payload_read)); 3769 spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL); 3770 CU_ASSERT(g_bserrno == 0); 3771 CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0); 3772 3773 write_bytes = g_dev_write_bytes; 3774 read_bytes = g_dev_read_bytes; 3775 3776 memset(payload_write, 0xE5, sizeof(payload_write)); 3777 spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL); 3778 CU_ASSERT(g_bserrno == 0); 3779 CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs)); 3780 /* For thin-provisioned blob we need to write 10 pages plus one page metadata and 3781 * read 0 bytes */ 3782 CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 11); 3783 CU_ASSERT(g_dev_read_bytes - read_bytes == 0); 3784 3785 spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL); 3786 CU_ASSERT(g_bserrno == 0); 3787 CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0); 3788 3789 spdk_blob_close(blob, blob_op_complete, NULL); 3790 CU_ASSERT(g_bserrno == 0); 3791 3792 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 3793 CU_ASSERT(g_bserrno == 0); 3794 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3795 3796 spdk_bs_free_io_channel(channel); 3797 3798 /* Unload the blob store */ 3799 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3800 CU_ASSERT(g_bserrno == 0); 3801 g_bs = NULL; 3802 g_blob = NULL; 3803 g_blobid = 0; 3804 } 3805 3806 static void 3807 blob_thin_prov_rw_iov(void) 3808 { 3809 static const uint8_t zero[10 * 4096] = { 0 }; 3810 struct spdk_blob_store *bs; 3811 struct spdk_bs_dev *dev; 3812 struct spdk_blob *blob; 3813 struct spdk_io_channel *channel; 3814 struct spdk_blob_opts opts; 3815 spdk_blob_id blobid; 3816 uint64_t free_clusters; 3817 uint8_t payload_read[10 * 4096]; 3818 uint8_t payload_write[10 * 4096]; 3819 struct iovec iov_read[3]; 3820 struct iovec iov_write[3]; 3821 3822 dev = init_dev(); 3823 3824 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 3825 CU_ASSERT(g_bserrno == 0); 3826 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3827 bs = g_bs; 3828 free_clusters = spdk_bs_free_cluster_count(bs); 3829 3830 channel = spdk_bs_alloc_io_channel(bs); 3831 CU_ASSERT(channel != NULL); 3832 3833 spdk_blob_opts_init(&opts); 3834 opts.thin_provision = true; 3835 3836 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 3837 CU_ASSERT(g_bserrno == 0); 3838 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3839 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3840 blobid = g_blobid; 3841 3842 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 3843 CU_ASSERT(g_bserrno == 0); 3844 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 3845 blob = g_blob; 3846 3847 CU_ASSERT(blob->active.num_clusters == 0); 3848 3849 /* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */ 3850 spdk_blob_resize(blob, 5, blob_op_complete, NULL); 3851 CU_ASSERT(g_bserrno == 0); 3852 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3853 CU_ASSERT(blob->active.num_clusters == 5); 3854 3855 spdk_blob_sync_md(blob, blob_op_complete, NULL); 3856 CU_ASSERT(g_bserrno == 0); 3857 /* Sync must not change anything */ 3858 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 3859 CU_ASSERT(blob->active.num_clusters == 5); 3860 3861 /* Payload should be all zeros from unallocated clusters */ 3862 memset(payload_read, 0xAA, sizeof(payload_read)); 3863 iov_read[0].iov_base = payload_read; 3864 iov_read[0].iov_len = 3 * 4096; 3865 iov_read[1].iov_base = payload_read + 3 * 4096; 3866 iov_read[1].iov_len = 4 * 4096; 3867 iov_read[2].iov_base = payload_read + 7 * 4096; 3868 iov_read[2].iov_len = 3 * 4096; 3869 spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL); 3870 CU_ASSERT(g_bserrno == 0); 3871 CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0); 3872 3873 memset(payload_write, 0xE5, sizeof(payload_write)); 3874 iov_write[0].iov_base = payload_write; 3875 iov_write[0].iov_len = 1 * 4096; 3876 iov_write[1].iov_base = payload_write + 1 * 4096; 3877 iov_write[1].iov_len = 5 * 4096; 3878 iov_write[2].iov_base = payload_write + 6 * 4096; 3879 iov_write[2].iov_len = 4 * 4096; 3880 3881 spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL); 3882 CU_ASSERT(g_bserrno == 0); 3883 3884 memset(payload_read, 0xAA, sizeof(payload_read)); 3885 iov_read[0].iov_base = payload_read; 3886 iov_read[0].iov_len = 3 * 4096; 3887 iov_read[1].iov_base = payload_read + 3 * 4096; 3888 iov_read[1].iov_len = 4 * 4096; 3889 iov_read[2].iov_base = payload_read + 7 * 4096; 3890 iov_read[2].iov_len = 3 * 4096; 3891 spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL); 3892 CU_ASSERT(g_bserrno == 0); 3893 CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0); 3894 3895 spdk_blob_close(blob, blob_op_complete, NULL); 3896 CU_ASSERT(g_bserrno == 0); 3897 3898 spdk_bs_free_io_channel(channel); 3899 3900 /* Unload the blob store */ 3901 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3902 CU_ASSERT(g_bserrno == 0); 3903 g_bs = NULL; 3904 g_blob = NULL; 3905 g_blobid = 0; 3906 } 3907 3908 struct iter_ctx { 3909 int current_iter; 3910 spdk_blob_id blobid[4]; 3911 }; 3912 3913 static void 3914 test_iter(void *arg, struct spdk_blob *blob, int bserrno) 3915 { 3916 struct iter_ctx *iter_ctx = arg; 3917 spdk_blob_id blobid; 3918 3919 CU_ASSERT(bserrno == 0); 3920 blobid = spdk_blob_get_id(blob); 3921 CU_ASSERT(blobid == iter_ctx->blobid[iter_ctx->current_iter++]); 3922 } 3923 3924 static void 3925 bs_load_iter(void) 3926 { 3927 struct spdk_bs_dev *dev; 3928 struct iter_ctx iter_ctx = { 0 }; 3929 struct spdk_blob *blob; 3930 int i, rc; 3931 struct spdk_bs_opts opts; 3932 3933 dev = init_dev(); 3934 spdk_bs_opts_init(&opts); 3935 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 3936 3937 /* Initialize a new blob store */ 3938 spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL); 3939 CU_ASSERT(g_bserrno == 0); 3940 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3941 3942 for (i = 0; i < 4; i++) { 3943 g_bserrno = -1; 3944 g_blobid = SPDK_BLOBID_INVALID; 3945 spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL); 3946 CU_ASSERT(g_bserrno == 0); 3947 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 3948 iter_ctx.blobid[i] = g_blobid; 3949 3950 g_bserrno = -1; 3951 g_blob = NULL; 3952 spdk_bs_open_blob(g_bs, g_blobid, blob_op_with_handle_complete, NULL); 3953 CU_ASSERT(g_bserrno == 0); 3954 CU_ASSERT(g_blob != NULL); 3955 blob = g_blob; 3956 3957 /* Just save the blobid as an xattr for testing purposes. */ 3958 rc = spdk_blob_set_xattr(blob, "blobid", &g_blobid, sizeof(g_blobid)); 3959 CU_ASSERT(rc == 0); 3960 3961 /* Resize the blob */ 3962 spdk_blob_resize(blob, i, blob_op_complete, NULL); 3963 CU_ASSERT(g_bserrno == 0); 3964 3965 spdk_blob_close(blob, blob_op_complete, NULL); 3966 CU_ASSERT(g_bserrno == 0); 3967 } 3968 3969 g_bserrno = -1; 3970 spdk_bs_unload(g_bs, bs_op_complete, NULL); 3971 CU_ASSERT(g_bserrno == 0); 3972 3973 dev = init_dev(); 3974 spdk_bs_opts_init(&opts); 3975 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 3976 opts.iter_cb_fn = test_iter; 3977 opts.iter_cb_arg = &iter_ctx; 3978 3979 /* Test blob iteration during load after a clean shutdown. */ 3980 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3981 CU_ASSERT(g_bserrno == 0); 3982 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3983 3984 /* Dirty shutdown */ 3985 _spdk_bs_free(g_bs); 3986 3987 dev = init_dev(); 3988 spdk_bs_opts_init(&opts); 3989 snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE"); 3990 opts.iter_cb_fn = test_iter; 3991 iter_ctx.current_iter = 0; 3992 opts.iter_cb_arg = &iter_ctx; 3993 3994 /* Test blob iteration during load after a dirty shutdown. */ 3995 spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL); 3996 CU_ASSERT(g_bserrno == 0); 3997 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 3998 3999 spdk_bs_unload(g_bs, bs_op_complete, NULL); 4000 CU_ASSERT(g_bserrno == 0); 4001 g_bs = NULL; 4002 } 4003 4004 static void 4005 blob_snapshot_rw(void) 4006 { 4007 static const uint8_t zero[10 * 4096] = { 0 }; 4008 struct spdk_blob_store *bs; 4009 struct spdk_bs_dev *dev; 4010 struct spdk_blob *blob, *snapshot; 4011 struct spdk_io_channel *channel; 4012 struct spdk_blob_opts opts; 4013 spdk_blob_id blobid, snapshotid; 4014 uint64_t free_clusters; 4015 uint64_t cluster_size; 4016 uint64_t page_size; 4017 uint8_t payload_read[10 * 4096]; 4018 uint8_t payload_write[10 * 4096]; 4019 uint64_t write_bytes; 4020 uint64_t read_bytes; 4021 4022 dev = init_dev(); 4023 4024 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 4025 CU_ASSERT(g_bserrno == 0); 4026 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 4027 bs = g_bs; 4028 free_clusters = spdk_bs_free_cluster_count(bs); 4029 cluster_size = spdk_bs_get_cluster_size(bs); 4030 page_size = spdk_bs_get_page_size(bs); 4031 4032 channel = spdk_bs_alloc_io_channel(bs); 4033 CU_ASSERT(channel != NULL); 4034 4035 spdk_blob_opts_init(&opts); 4036 opts.thin_provision = true; 4037 opts.num_clusters = 5; 4038 4039 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 4040 CU_ASSERT(g_bserrno == 0); 4041 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4042 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 4043 blobid = g_blobid; 4044 4045 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 4046 CU_ASSERT(g_bserrno == 0); 4047 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4048 blob = g_blob; 4049 4050 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5); 4051 4052 memset(payload_read, 0xFF, sizeof(payload_read)); 4053 spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL); 4054 CU_ASSERT(g_bserrno == 0); 4055 CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0); 4056 4057 memset(payload_write, 0xE5, sizeof(payload_write)); 4058 spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL); 4059 CU_ASSERT(g_bserrno == 0); 4060 CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs)); 4061 4062 /* Create snapshot from blob */ 4063 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 4064 CU_ASSERT(g_bserrno == 0); 4065 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4066 snapshotid = g_blobid; 4067 4068 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 4069 CU_ASSERT(g_bserrno == 0); 4070 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4071 snapshot = g_blob; 4072 CU_ASSERT(snapshot->data_ro == true) 4073 CU_ASSERT(snapshot->md_ro == true) 4074 4075 CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5) 4076 4077 write_bytes = g_dev_write_bytes; 4078 read_bytes = g_dev_read_bytes; 4079 4080 memset(payload_write, 0xAA, sizeof(payload_write)); 4081 spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL); 4082 CU_ASSERT(g_bserrno == 0); 4083 CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs)); 4084 4085 /* For a clone we need to allocate and copy one cluster, update one page of metadata 4086 * and then write 10 pages of payload. 4087 */ 4088 CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 11 + cluster_size); 4089 CU_ASSERT(g_dev_read_bytes - read_bytes == cluster_size); 4090 4091 spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL); 4092 CU_ASSERT(g_bserrno == 0); 4093 CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0); 4094 4095 /* Data on snapshot should not change after write to clone */ 4096 memset(payload_write, 0xE5, sizeof(payload_write)); 4097 spdk_blob_io_read(snapshot, channel, payload_read, 4, 10, blob_op_complete, NULL); 4098 CU_ASSERT(g_bserrno == 0); 4099 CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0); 4100 4101 spdk_blob_close(blob, blob_op_complete, NULL); 4102 CU_ASSERT(g_bserrno == 0); 4103 4104 spdk_blob_close(snapshot, blob_op_complete, NULL); 4105 CU_ASSERT(g_bserrno == 0); 4106 4107 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 4108 CU_ASSERT(g_bserrno == 0); 4109 4110 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 4111 CU_ASSERT(g_bserrno == 0); 4112 4113 spdk_bs_free_io_channel(channel); 4114 4115 /* Unload the blob store */ 4116 spdk_bs_unload(g_bs, bs_op_complete, NULL); 4117 CU_ASSERT(g_bserrno == 0); 4118 g_bs = NULL; 4119 g_blob = NULL; 4120 g_blobid = 0; 4121 } 4122 4123 static void 4124 blob_snapshot_rw_iov(void) 4125 { 4126 static const uint8_t zero[10 * 4096] = { 0 }; 4127 struct spdk_blob_store *bs; 4128 struct spdk_bs_dev *dev; 4129 struct spdk_blob *blob, *snapshot; 4130 struct spdk_io_channel *channel; 4131 struct spdk_blob_opts opts; 4132 spdk_blob_id blobid, snapshotid; 4133 uint64_t free_clusters; 4134 uint8_t payload_read[10 * 4096]; 4135 uint8_t payload_write[10 * 4096]; 4136 struct iovec iov_read[3]; 4137 struct iovec iov_write[3]; 4138 4139 dev = init_dev(); 4140 4141 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 4142 CU_ASSERT(g_bserrno == 0); 4143 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 4144 bs = g_bs; 4145 free_clusters = spdk_bs_free_cluster_count(bs); 4146 4147 channel = spdk_bs_alloc_io_channel(bs); 4148 CU_ASSERT(channel != NULL); 4149 4150 spdk_blob_opts_init(&opts); 4151 opts.thin_provision = true; 4152 opts.num_clusters = 5; 4153 4154 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 4155 CU_ASSERT(g_bserrno == 0); 4156 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4157 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 4158 blobid = g_blobid; 4159 4160 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 4161 CU_ASSERT(g_bserrno == 0); 4162 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4163 blob = g_blob; 4164 4165 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5); 4166 4167 /* Create snapshot from blob */ 4168 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 4169 CU_ASSERT(g_bserrno == 0); 4170 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4171 snapshotid = g_blobid; 4172 4173 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 4174 CU_ASSERT(g_bserrno == 0); 4175 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4176 snapshot = g_blob; 4177 CU_ASSERT(snapshot->data_ro == true) 4178 CU_ASSERT(snapshot->md_ro == true) 4179 CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5); 4180 4181 /* Payload should be all zeros from unallocated clusters */ 4182 memset(payload_read, 0xAA, sizeof(payload_read)); 4183 iov_read[0].iov_base = payload_read; 4184 iov_read[0].iov_len = 3 * 4096; 4185 iov_read[1].iov_base = payload_read + 3 * 4096; 4186 iov_read[1].iov_len = 4 * 4096; 4187 iov_read[2].iov_base = payload_read + 7 * 4096; 4188 iov_read[2].iov_len = 3 * 4096; 4189 spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL); 4190 CU_ASSERT(g_bserrno == 0); 4191 CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0); 4192 4193 memset(payload_write, 0xE5, sizeof(payload_write)); 4194 iov_write[0].iov_base = payload_write; 4195 iov_write[0].iov_len = 1 * 4096; 4196 iov_write[1].iov_base = payload_write + 1 * 4096; 4197 iov_write[1].iov_len = 5 * 4096; 4198 iov_write[2].iov_base = payload_write + 6 * 4096; 4199 iov_write[2].iov_len = 4 * 4096; 4200 4201 spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL); 4202 CU_ASSERT(g_bserrno == 0); 4203 4204 memset(payload_read, 0xAA, sizeof(payload_read)); 4205 iov_read[0].iov_base = payload_read; 4206 iov_read[0].iov_len = 3 * 4096; 4207 iov_read[1].iov_base = payload_read + 3 * 4096; 4208 iov_read[1].iov_len = 4 * 4096; 4209 iov_read[2].iov_base = payload_read + 7 * 4096; 4210 iov_read[2].iov_len = 3 * 4096; 4211 spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL); 4212 CU_ASSERT(g_bserrno == 0); 4213 CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0); 4214 4215 spdk_blob_close(blob, blob_op_complete, NULL); 4216 CU_ASSERT(g_bserrno == 0); 4217 4218 spdk_blob_close(snapshot, blob_op_complete, NULL); 4219 CU_ASSERT(g_bserrno == 0); 4220 4221 spdk_bs_free_io_channel(channel); 4222 4223 /* Unload the blob store */ 4224 spdk_bs_unload(g_bs, bs_op_complete, NULL); 4225 CU_ASSERT(g_bserrno == 0); 4226 g_bs = NULL; 4227 g_blob = NULL; 4228 g_blobid = 0; 4229 } 4230 4231 /** 4232 * Inflate / decouple parent rw unit tests. 4233 * 4234 * -------------- 4235 * original blob: 0 1 2 3 4 4236 * ,---------+---------+---------+---------+---------. 4237 * snapshot |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx| - | 4238 * +---------+---------+---------+---------+---------+ 4239 * snapshot2 | - |yyyyyyyyy| - |yyyyyyyyy| - | 4240 * +---------+---------+---------+---------+---------+ 4241 * blob | - |zzzzzzzzz| - | - | - | 4242 * '---------+---------+---------+---------+---------' 4243 * . . . . . . 4244 * -------- . . . . . . 4245 * inflate: . . . . . . 4246 * ,---------+---------+---------+---------+---------. 4247 * blob |xxxxxxxxx|zzzzzzzzz|xxxxxxxxx|yyyyyyyyy|000000000| 4248 * '---------+---------+---------+---------+---------' 4249 * 4250 * NOTE: needs to allocate 4 clusters, thin provisioning removed, dependency 4251 * on snapshot2 and snapshot removed . . . 4252 * . . . . . . 4253 * ---------------- . . . . . . 4254 * decouple parent: . . . . . . 4255 * ,---------+---------+---------+---------+---------. 4256 * snapshot |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx| - | 4257 * +---------+---------+---------+---------+---------+ 4258 * blob | - |zzzzzzzzz| - |yyyyyyyyy| - | 4259 * '---------+---------+---------+---------+---------' 4260 * 4261 * NOTE: needs to allocate 1 cluster, 3 clusters unallocated, dependency 4262 * on snapshot2 removed and on snapshot still exists. Snapshot2 4263 * should remain a clone of snapshot. 4264 */ 4265 static void 4266 _blob_inflate_rw(bool decouple_parent) 4267 { 4268 struct spdk_blob_store *bs; 4269 struct spdk_bs_dev *dev; 4270 struct spdk_blob *blob, *snapshot, *snapshot2; 4271 struct spdk_io_channel *channel; 4272 struct spdk_blob_opts opts; 4273 spdk_blob_id blobid, snapshotid, snapshot2id; 4274 uint64_t free_clusters; 4275 uint64_t cluster_size; 4276 4277 uint64_t payload_size; 4278 uint8_t *payload_read; 4279 uint8_t *payload_write; 4280 uint8_t *payload_clone; 4281 4282 uint64_t pages_per_cluster; 4283 uint64_t pages_per_payload; 4284 4285 int i; 4286 spdk_blob_id ids[2]; 4287 size_t count; 4288 4289 dev = init_dev(); 4290 4291 spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL); 4292 CU_ASSERT(g_bserrno == 0); 4293 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 4294 bs = g_bs; 4295 4296 free_clusters = spdk_bs_free_cluster_count(bs); 4297 cluster_size = spdk_bs_get_cluster_size(bs); 4298 pages_per_cluster = cluster_size / spdk_bs_get_page_size(bs); 4299 pages_per_payload = pages_per_cluster * 5; 4300 4301 payload_size = cluster_size * 5; 4302 4303 payload_read = malloc(payload_size); 4304 SPDK_CU_ASSERT_FATAL(payload_read != NULL); 4305 4306 payload_write = malloc(payload_size); 4307 SPDK_CU_ASSERT_FATAL(payload_write != NULL); 4308 4309 payload_clone = malloc(payload_size); 4310 SPDK_CU_ASSERT_FATAL(payload_clone != NULL); 4311 4312 channel = spdk_bs_alloc_io_channel(bs); 4313 SPDK_CU_ASSERT_FATAL(channel != NULL); 4314 4315 /* Create blob */ 4316 spdk_blob_opts_init(&opts); 4317 opts.thin_provision = true; 4318 opts.num_clusters = 5; 4319 4320 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 4321 CU_ASSERT(g_bserrno == 0); 4322 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4323 CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs)); 4324 blobid = g_blobid; 4325 4326 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 4327 CU_ASSERT(g_bserrno == 0); 4328 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4329 blob = g_blob; 4330 4331 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5); 4332 4333 /* 1) Initial read should return zeroed payload */ 4334 memset(payload_read, 0xFF, payload_size); 4335 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, 4336 blob_op_complete, NULL); 4337 CU_ASSERT(g_bserrno == 0); 4338 CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size)); 4339 4340 /* Fill whole blob with a pattern, except last cluster (to be sure it 4341 * isn't allocated) */ 4342 memset(payload_write, 0xE5, payload_size - cluster_size); 4343 spdk_blob_io_write(blob, channel, payload_write, 0, pages_per_payload - 4344 pages_per_cluster, blob_op_complete, NULL); 4345 CU_ASSERT(g_bserrno == 0); 4346 CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs)); 4347 4348 /* 2) Create snapshot from blob (first level) */ 4349 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 4350 CU_ASSERT(g_bserrno == 0); 4351 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4352 snapshotid = g_blobid; 4353 4354 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 4355 CU_ASSERT(g_bserrno == 0); 4356 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4357 snapshot = g_blob; 4358 CU_ASSERT(snapshot->data_ro == true) 4359 CU_ASSERT(snapshot->md_ro == true) 4360 4361 CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5) 4362 4363 /* Write every second cluster with a pattern. 4364 * 4365 * Last cluster shouldn't be written, to be sure that snapshot nor clone 4366 * doesn't allocate it. 4367 * 4368 * payload_clone stores expected result on "blob" read at the time and 4369 * is used only to check data consistency on clone before and after 4370 * inflation. Initially we fill it with a backing snapshots pattern 4371 * used before. 4372 */ 4373 memset(payload_clone, 0xE5, payload_size - cluster_size); 4374 memset(payload_clone + payload_size - cluster_size, 0x00, cluster_size); 4375 memset(payload_write, 0xAA, payload_size); 4376 for (i = 1; i < 5; i += 2) { 4377 spdk_blob_io_write(blob, channel, payload_write, i * pages_per_cluster, 4378 pages_per_cluster, blob_op_complete, NULL); 4379 CU_ASSERT(g_bserrno == 0); 4380 4381 /* Update expected result */ 4382 memcpy(payload_clone + (cluster_size * i), payload_write, 4383 cluster_size); 4384 } 4385 CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs)); 4386 4387 /* Check data consistency on clone */ 4388 memset(payload_read, 0xFF, payload_size); 4389 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, 4390 blob_op_complete, NULL); 4391 CU_ASSERT(g_bserrno == 0); 4392 CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0); 4393 4394 /* 3) Create second levels snapshot from blob */ 4395 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 4396 CU_ASSERT(g_bserrno == 0); 4397 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4398 snapshot2id = g_blobid; 4399 4400 spdk_bs_open_blob(bs, snapshot2id, blob_op_with_handle_complete, NULL); 4401 CU_ASSERT(g_bserrno == 0); 4402 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4403 snapshot2 = g_blob; 4404 CU_ASSERT(snapshot2->data_ro == true) 4405 CU_ASSERT(snapshot2->md_ro == true) 4406 4407 CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 5) 4408 4409 CU_ASSERT(snapshot2->parent_id == snapshotid); 4410 4411 /* Write one cluster on the top level blob. This cluster (1) covers 4412 * already allocated cluster in the snapshot2, so shouldn't be inflated 4413 * at all */ 4414 spdk_blob_io_write(blob, channel, payload_write, pages_per_cluster, 4415 pages_per_cluster, blob_op_complete, NULL); 4416 CU_ASSERT(g_bserrno == 0); 4417 4418 /* Update expected result */ 4419 memcpy(payload_clone + cluster_size, payload_write, cluster_size); 4420 4421 /* Check data consistency on clone */ 4422 memset(payload_read, 0xFF, payload_size); 4423 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, 4424 blob_op_complete, NULL); 4425 CU_ASSERT(g_bserrno == 0); 4426 CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0); 4427 4428 4429 /* Close all blobs */ 4430 spdk_blob_close(blob, blob_op_complete, NULL); 4431 CU_ASSERT(g_bserrno == 0); 4432 4433 spdk_blob_close(snapshot2, blob_op_complete, NULL); 4434 CU_ASSERT(g_bserrno == 0); 4435 4436 spdk_blob_close(snapshot, blob_op_complete, NULL); 4437 CU_ASSERT(g_bserrno == 0); 4438 4439 /* Check snapshot-clone relations */ 4440 count = 2; 4441 CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0); 4442 CU_ASSERT(count == 1); 4443 CU_ASSERT(ids[0] == snapshot2id); 4444 4445 count = 2; 4446 CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0); 4447 CU_ASSERT(count == 1); 4448 CU_ASSERT(ids[0] == blobid); 4449 4450 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshot2id); 4451 4452 free_clusters = spdk_bs_free_cluster_count(bs); 4453 if (!decouple_parent) { 4454 /* Do full blob inflation */ 4455 spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL); 4456 CU_ASSERT(g_bserrno == 0); 4457 4458 /* All clusters should be inflated (except one already allocated 4459 * in a top level blob) */ 4460 CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 4); 4461 4462 /* Check if relation tree updated correctly */ 4463 count = 2; 4464 CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0); 4465 4466 /* snapshotid have one clone */ 4467 CU_ASSERT(count == 1); 4468 CU_ASSERT(ids[0] == snapshot2id); 4469 4470 /* snapshot2id have no clones */ 4471 count = 2; 4472 CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0); 4473 CU_ASSERT(count == 0); 4474 4475 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID); 4476 } else { 4477 /* Decouple parent of blob */ 4478 spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL); 4479 CU_ASSERT(g_bserrno == 0); 4480 4481 /* Only one cluster from a parent should be inflated (second one 4482 * is covered by a cluster written on a top level blob, and 4483 * already allocated) */ 4484 CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 1); 4485 4486 /* Check if relation tree updated correctly */ 4487 count = 2; 4488 CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0); 4489 4490 /* snapshotid have two clones now */ 4491 CU_ASSERT(count == 2); 4492 CU_ASSERT(ids[0] == blobid || ids[1] == blobid); 4493 CU_ASSERT(ids[0] == snapshot2id || ids[1] == snapshot2id); 4494 4495 /* snapshot2id have no clones */ 4496 count = 2; 4497 CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0); 4498 CU_ASSERT(count == 0); 4499 4500 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid); 4501 } 4502 4503 /* Try to delete snapshot2 (should pass) */ 4504 spdk_bs_delete_blob(bs, snapshot2id, blob_op_complete, NULL); 4505 CU_ASSERT(g_bserrno == 0); 4506 4507 /* Try to delete base snapshot (for decouple_parent should fail while 4508 * dependency still exists) */ 4509 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 4510 CU_ASSERT(decouple_parent || g_bserrno == 0); 4511 CU_ASSERT(!decouple_parent || g_bserrno != 0); 4512 4513 /* Reopen blob after snapshot deletion */ 4514 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 4515 CU_ASSERT(g_bserrno == 0); 4516 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4517 blob = g_blob; 4518 4519 CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5); 4520 4521 /* Check data consistency on inflated blob */ 4522 memset(payload_read, 0xFF, payload_size); 4523 spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, 4524 blob_op_complete, NULL); 4525 CU_ASSERT(g_bserrno == 0); 4526 CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0); 4527 4528 spdk_blob_close(blob, blob_op_complete, NULL); 4529 CU_ASSERT(g_bserrno == 0); 4530 4531 spdk_bs_free_io_channel(channel); 4532 4533 /* Unload the blob store */ 4534 spdk_bs_unload(g_bs, bs_op_complete, NULL); 4535 CU_ASSERT(g_bserrno == 0); 4536 g_bs = NULL; 4537 g_blob = NULL; 4538 g_blobid = 0; 4539 4540 free(payload_read); 4541 free(payload_write); 4542 free(payload_clone); 4543 } 4544 4545 static void 4546 blob_inflate_rw(void) 4547 { 4548 _blob_inflate_rw(false); 4549 _blob_inflate_rw(true); 4550 } 4551 4552 /** 4553 * Snapshot-clones relation test 4554 * 4555 * snapshot 4556 * | 4557 * +-----+-----+ 4558 * | | 4559 * blob(ro) snapshot2 4560 * | | 4561 * clone2 clone 4562 */ 4563 static void 4564 blob_relations(void) 4565 { 4566 struct spdk_blob_store *bs; 4567 struct spdk_bs_dev *dev; 4568 struct spdk_bs_opts bs_opts; 4569 struct spdk_blob_opts opts; 4570 struct spdk_blob *blob, *snapshot, *snapshot2, *clone, *clone2; 4571 spdk_blob_id blobid, cloneid, snapshotid, cloneid2, snapshotid2; 4572 int rc; 4573 size_t count; 4574 spdk_blob_id ids[10]; 4575 4576 dev = init_dev(); 4577 spdk_bs_opts_init(&bs_opts); 4578 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE"); 4579 4580 spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL); 4581 CU_ASSERT(g_bserrno == 0); 4582 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 4583 bs = g_bs; 4584 4585 /* 1. Create blob with 10 clusters */ 4586 4587 spdk_blob_opts_init(&opts); 4588 opts.num_clusters = 10; 4589 4590 spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL); 4591 CU_ASSERT(g_bserrno == 0); 4592 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4593 blobid = g_blobid; 4594 4595 spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL); 4596 CU_ASSERT(g_bserrno == 0); 4597 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4598 blob = g_blob; 4599 4600 CU_ASSERT(!spdk_blob_is_read_only(blob)); 4601 CU_ASSERT(!spdk_blob_is_snapshot(blob)); 4602 CU_ASSERT(!spdk_blob_is_clone(blob)); 4603 CU_ASSERT(!spdk_blob_is_thin_provisioned(blob)); 4604 4605 /* blob should not have underlying snapshot nor clones */ 4606 CU_ASSERT(blob->parent_id == SPDK_BLOBID_INVALID); 4607 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID); 4608 count = SPDK_COUNTOF(ids); 4609 rc = spdk_blob_get_clones(bs, blobid, ids, &count); 4610 CU_ASSERT(rc == 0); 4611 CU_ASSERT(count == 0); 4612 4613 4614 /* 2. Create snapshot */ 4615 4616 spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL); 4617 CU_ASSERT(g_bserrno == 0); 4618 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4619 snapshotid = g_blobid; 4620 4621 spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL); 4622 CU_ASSERT(g_bserrno == 0); 4623 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4624 snapshot = g_blob; 4625 4626 CU_ASSERT(spdk_blob_is_read_only(snapshot)); 4627 CU_ASSERT(spdk_blob_is_snapshot(snapshot)); 4628 CU_ASSERT(!spdk_blob_is_clone(snapshot)); 4629 CU_ASSERT(snapshot->parent_id == SPDK_BLOBID_INVALID); 4630 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID); 4631 4632 /* Check if original blob is converted to the clone of snapshot */ 4633 CU_ASSERT(!spdk_blob_is_read_only(blob)); 4634 CU_ASSERT(!spdk_blob_is_snapshot(blob)); 4635 CU_ASSERT(spdk_blob_is_clone(blob)); 4636 CU_ASSERT(spdk_blob_is_thin_provisioned(blob)); 4637 CU_ASSERT(blob->parent_id == snapshotid); 4638 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid); 4639 4640 count = SPDK_COUNTOF(ids); 4641 rc = spdk_blob_get_clones(bs, snapshotid, ids, &count); 4642 CU_ASSERT(rc == 0); 4643 CU_ASSERT(count == 1); 4644 CU_ASSERT(ids[0] == blobid); 4645 4646 4647 /* 3. Create clone from snapshot */ 4648 4649 spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL); 4650 CU_ASSERT(g_bserrno == 0); 4651 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4652 cloneid = g_blobid; 4653 4654 spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL); 4655 CU_ASSERT(g_bserrno == 0); 4656 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4657 clone = g_blob; 4658 4659 CU_ASSERT(!spdk_blob_is_read_only(clone)); 4660 CU_ASSERT(!spdk_blob_is_snapshot(clone)); 4661 CU_ASSERT(spdk_blob_is_clone(clone)); 4662 CU_ASSERT(spdk_blob_is_thin_provisioned(clone)); 4663 CU_ASSERT(clone->parent_id == snapshotid); 4664 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid); 4665 4666 count = SPDK_COUNTOF(ids); 4667 rc = spdk_blob_get_clones(bs, cloneid, ids, &count); 4668 CU_ASSERT(rc == 0); 4669 CU_ASSERT(count == 0); 4670 4671 /* Check if clone is on the snapshot's list */ 4672 count = SPDK_COUNTOF(ids); 4673 rc = spdk_blob_get_clones(bs, snapshotid, ids, &count); 4674 CU_ASSERT(rc == 0); 4675 CU_ASSERT(ids[0] == blobid || ids[1] == blobid); 4676 CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid); 4677 4678 4679 /* 4. Create snapshot of the clone */ 4680 4681 spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL); 4682 CU_ASSERT(g_bserrno == 0); 4683 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4684 snapshotid2 = g_blobid; 4685 4686 spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL); 4687 CU_ASSERT(g_bserrno == 0); 4688 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4689 snapshot2 = g_blob; 4690 4691 CU_ASSERT(spdk_blob_is_read_only(snapshot2)); 4692 CU_ASSERT(spdk_blob_is_snapshot(snapshot2)); 4693 CU_ASSERT(spdk_blob_is_clone(snapshot2)); 4694 CU_ASSERT(snapshot2->parent_id == snapshotid); 4695 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid); 4696 4697 /* Check if clone is converted to the clone of snapshot2 and snapshot2 4698 * is a child of snapshot */ 4699 CU_ASSERT(!spdk_blob_is_read_only(clone)); 4700 CU_ASSERT(!spdk_blob_is_snapshot(clone)); 4701 CU_ASSERT(spdk_blob_is_clone(clone)); 4702 CU_ASSERT(spdk_blob_is_thin_provisioned(clone)); 4703 CU_ASSERT(clone->parent_id == snapshotid2); 4704 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2); 4705 4706 count = SPDK_COUNTOF(ids); 4707 rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count); 4708 CU_ASSERT(rc == 0); 4709 CU_ASSERT(count == 1); 4710 CU_ASSERT(ids[0] == cloneid); 4711 4712 4713 /* 5. Try to create clone from read only blob */ 4714 4715 /* Mark blob as read only */ 4716 spdk_blob_set_read_only(blob); 4717 spdk_blob_sync_md(blob, blob_op_complete, NULL); 4718 CU_ASSERT(g_bserrno == 0); 4719 4720 /* Check if previously created blob is read only clone */ 4721 CU_ASSERT(spdk_blob_is_read_only(blob)); 4722 CU_ASSERT(!spdk_blob_is_snapshot(blob)); 4723 CU_ASSERT(spdk_blob_is_clone(blob)); 4724 CU_ASSERT(spdk_blob_is_thin_provisioned(blob)); 4725 4726 /* Create clone from read only blob */ 4727 spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL); 4728 CU_ASSERT(g_bserrno == 0); 4729 CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID); 4730 cloneid2 = g_blobid; 4731 4732 spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL); 4733 CU_ASSERT(g_bserrno == 0); 4734 SPDK_CU_ASSERT_FATAL(g_blob != NULL); 4735 clone2 = g_blob; 4736 4737 CU_ASSERT(!spdk_blob_is_read_only(clone2)); 4738 CU_ASSERT(!spdk_blob_is_snapshot(clone2)); 4739 CU_ASSERT(spdk_blob_is_clone(clone2)); 4740 CU_ASSERT(spdk_blob_is_thin_provisioned(clone2)); 4741 4742 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid); 4743 4744 count = SPDK_COUNTOF(ids); 4745 rc = spdk_blob_get_clones(bs, blobid, ids, &count); 4746 CU_ASSERT(rc == 0); 4747 4748 CU_ASSERT(count == 1); 4749 CU_ASSERT(ids[0] == cloneid2); 4750 4751 /* Close blobs */ 4752 4753 spdk_blob_close(clone2, blob_op_complete, NULL); 4754 CU_ASSERT(g_bserrno == 0); 4755 4756 spdk_blob_close(blob, blob_op_complete, NULL); 4757 CU_ASSERT(g_bserrno == 0); 4758 4759 spdk_blob_close(clone, blob_op_complete, NULL); 4760 CU_ASSERT(g_bserrno == 0); 4761 4762 spdk_blob_close(snapshot, blob_op_complete, NULL); 4763 CU_ASSERT(g_bserrno == 0); 4764 4765 spdk_blob_close(snapshot2, blob_op_complete, NULL); 4766 CU_ASSERT(g_bserrno == 0); 4767 4768 /* Try to delete snapshot with created clones */ 4769 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 4770 CU_ASSERT(g_bserrno != 0); 4771 4772 spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL); 4773 CU_ASSERT(g_bserrno != 0); 4774 4775 spdk_bs_unload(bs, bs_op_complete, NULL); 4776 CU_ASSERT(g_bserrno == 0); 4777 g_bs = NULL; 4778 4779 /* Load an existing blob store */ 4780 dev = init_dev(); 4781 snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE"); 4782 4783 spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL); 4784 CU_ASSERT(g_bserrno == 0); 4785 SPDK_CU_ASSERT_FATAL(g_bs != NULL); 4786 bs = g_bs; 4787 4788 4789 /* NULL ids array should return number of clones in count */ 4790 count = SPDK_COUNTOF(ids); 4791 rc = spdk_blob_get_clones(bs, snapshotid, NULL, &count); 4792 CU_ASSERT(rc == -ENOMEM); 4793 CU_ASSERT(count == 2); 4794 4795 /* incorrect array size */ 4796 count = 1; 4797 rc = spdk_blob_get_clones(bs, snapshotid, ids, &count); 4798 CU_ASSERT(rc == -ENOMEM); 4799 CU_ASSERT(count == 2); 4800 4801 4802 /* Verify structure of loaded blob store */ 4803 4804 /* snapshot */ 4805 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID); 4806 4807 count = SPDK_COUNTOF(ids); 4808 rc = spdk_blob_get_clones(bs, snapshotid, ids, &count); 4809 CU_ASSERT(rc == 0); 4810 CU_ASSERT(count == 2); 4811 CU_ASSERT(ids[0] == blobid || ids[1] == blobid); 4812 CU_ASSERT(ids[0] == snapshotid2 || ids[1] == snapshotid2); 4813 4814 /* blob */ 4815 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid); 4816 count = SPDK_COUNTOF(ids); 4817 rc = spdk_blob_get_clones(bs, blobid, ids, &count); 4818 CU_ASSERT(rc == 0); 4819 CU_ASSERT(count == 1); 4820 CU_ASSERT(ids[0] == cloneid2); 4821 4822 /* clone */ 4823 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2); 4824 count = SPDK_COUNTOF(ids); 4825 rc = spdk_blob_get_clones(bs, cloneid, ids, &count); 4826 CU_ASSERT(rc == 0); 4827 CU_ASSERT(count == 0); 4828 4829 /* snapshot2 */ 4830 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid); 4831 count = SPDK_COUNTOF(ids); 4832 rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count); 4833 CU_ASSERT(rc == 0); 4834 CU_ASSERT(count == 1); 4835 CU_ASSERT(ids[0] == cloneid); 4836 4837 /* clone2 */ 4838 CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid); 4839 count = SPDK_COUNTOF(ids); 4840 rc = spdk_blob_get_clones(bs, cloneid2, ids, &count); 4841 CU_ASSERT(rc == 0); 4842 CU_ASSERT(count == 0); 4843 4844 /* Try to delete all blobs in the worse possible order */ 4845 4846 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 4847 CU_ASSERT(g_bserrno != 0); 4848 4849 spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL); 4850 CU_ASSERT(g_bserrno != 0); 4851 4852 spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL); 4853 CU_ASSERT(g_bserrno == 0); 4854 4855 spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL); 4856 CU_ASSERT(g_bserrno == 0); 4857 4858 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 4859 CU_ASSERT(g_bserrno != 0); 4860 4861 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 4862 CU_ASSERT(g_bserrno != 0); 4863 4864 spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL); 4865 CU_ASSERT(g_bserrno == 0); 4866 4867 spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL); 4868 CU_ASSERT(g_bserrno == 0); 4869 4870 spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL); 4871 CU_ASSERT(g_bserrno == 0); 4872 4873 spdk_bs_unload(bs, bs_op_complete, NULL); 4874 CU_ASSERT(g_bserrno == 0); 4875 4876 g_bs = NULL; 4877 } 4878 4879 int main(int argc, char **argv) 4880 { 4881 CU_pSuite suite = NULL; 4882 unsigned int num_failures; 4883 4884 if (CU_initialize_registry() != CUE_SUCCESS) { 4885 return CU_get_error(); 4886 } 4887 4888 suite = CU_add_suite("blob", NULL, NULL); 4889 if (suite == NULL) { 4890 CU_cleanup_registry(); 4891 return CU_get_error(); 4892 } 4893 4894 if ( 4895 CU_add_test(suite, "blob_init", blob_init) == NULL || 4896 CU_add_test(suite, "blob_open", blob_open) == NULL || 4897 CU_add_test(suite, "blob_create", blob_create) == NULL || 4898 CU_add_test(suite, "blob_create_internal", blob_create_internal) == NULL || 4899 CU_add_test(suite, "blob_thin_provision", blob_thin_provision) == NULL || 4900 CU_add_test(suite, "blob_snapshot", blob_snapshot) == NULL || 4901 CU_add_test(suite, "blob_clone", blob_clone) == NULL || 4902 CU_add_test(suite, "blob_inflate", blob_inflate) == NULL || 4903 CU_add_test(suite, "blob_delete", blob_delete) == NULL || 4904 CU_add_test(suite, "blob_resize", blob_resize) == NULL || 4905 CU_add_test(suite, "blob_read_only", blob_read_only) == NULL || 4906 CU_add_test(suite, "channel_ops", channel_ops) == NULL || 4907 CU_add_test(suite, "blob_super", blob_super) == NULL || 4908 CU_add_test(suite, "blob_write", blob_write) == NULL || 4909 CU_add_test(suite, "blob_read", blob_read) == NULL || 4910 CU_add_test(suite, "blob_rw_verify", blob_rw_verify) == NULL || 4911 CU_add_test(suite, "blob_rw_verify_iov", blob_rw_verify_iov) == NULL || 4912 CU_add_test(suite, "blob_rw_verify_iov_nomem", blob_rw_verify_iov_nomem) == NULL || 4913 CU_add_test(suite, "blob_rw_iov_read_only", blob_rw_iov_read_only) == NULL || 4914 CU_add_test(suite, "blob_unmap", blob_unmap) == NULL || 4915 CU_add_test(suite, "blob_iter", blob_iter) == NULL || 4916 CU_add_test(suite, "blob_xattr", blob_xattr) == NULL || 4917 CU_add_test(suite, "bs_load", bs_load) == NULL || 4918 CU_add_test(suite, "bs_unload", bs_unload) == NULL || 4919 CU_add_test(suite, "bs_cluster_sz", bs_cluster_sz) == NULL || 4920 CU_add_test(suite, "bs_usable_clusters", bs_usable_clusters) == NULL || 4921 CU_add_test(suite, "bs_resize_md", bs_resize_md) == NULL || 4922 CU_add_test(suite, "bs_destroy", bs_destroy) == NULL || 4923 CU_add_test(suite, "bs_type", bs_type) == NULL || 4924 CU_add_test(suite, "bs_super_block", bs_super_block) == NULL || 4925 CU_add_test(suite, "blob_serialize", blob_serialize) == NULL || 4926 CU_add_test(suite, "blob_crc", blob_crc) == NULL || 4927 CU_add_test(suite, "super_block_crc", super_block_crc) == NULL || 4928 CU_add_test(suite, "blob_dirty_shutdown", blob_dirty_shutdown) == NULL || 4929 CU_add_test(suite, "blob_flags", blob_flags) == NULL || 4930 CU_add_test(suite, "bs_version", bs_version) == NULL || 4931 CU_add_test(suite, "blob_set_xattrs", blob_set_xattrs) == NULL || 4932 CU_add_test(suite, "blob_thin_prov_alloc", blob_thin_prov_alloc) == NULL || 4933 CU_add_test(suite, "blob_insert_cluster_msg", blob_insert_cluster_msg) == NULL || 4934 CU_add_test(suite, "blob_thin_prov_rw", blob_thin_prov_rw) == NULL || 4935 CU_add_test(suite, "blob_thin_prov_rw_iov", blob_thin_prov_rw_iov) == NULL || 4936 CU_add_test(suite, "bs_load_iter", bs_load_iter) == NULL || 4937 CU_add_test(suite, "blob_snapshot_rw", blob_snapshot_rw) == NULL || 4938 CU_add_test(suite, "blob_snapshot_rw_iov", blob_snapshot_rw_iov) == NULL || 4939 CU_add_test(suite, "blob_relations", blob_relations) == NULL || 4940 CU_add_test(suite, "blob_inflate_rw", blob_inflate_rw) == NULL || 4941 CU_add_test(suite, "blob_snapshot_freeze_io", blob_snapshot_freeze_io) == NULL || 4942 CU_add_test(suite, "blob_operation_split_rw", blob_operation_split_rw) == NULL 4943 ) { 4944 CU_cleanup_registry(); 4945 return CU_get_error(); 4946 } 4947 4948 g_dev_buffer = calloc(1, DEV_BUFFER_SIZE); 4949 spdk_allocate_thread(_bs_send_msg, NULL, NULL, NULL, "thread0"); 4950 CU_basic_set_mode(CU_BRM_VERBOSE); 4951 CU_basic_run_tests(); 4952 num_failures = CU_get_number_of_failures(); 4953 CU_cleanup_registry(); 4954 spdk_free_thread(); 4955 free(g_dev_buffer); 4956 return num_failures; 4957 } 4958