1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "spdk/stdinc.h" 36 37 #include "spdk/blob.h" 38 #include "spdk/crc32.h" 39 #include "spdk/env.h" 40 #include "spdk/queue.h" 41 #include "spdk/thread.h" 42 #include "spdk/bit_array.h" 43 #include "spdk/bit_pool.h" 44 #include "spdk/likely.h" 45 #include "spdk/util.h" 46 #include "spdk/string.h" 47 48 #include "spdk_internal/assert.h" 49 #include "spdk/log.h" 50 51 #include "blobstore.h" 52 53 #define BLOB_CRC32C_INITIAL 0xffffffffUL 54 55 static int bs_register_md_thread(struct spdk_blob_store *bs); 56 static int bs_unregister_md_thread(struct spdk_blob_store *bs); 57 static void blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno); 58 static void blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 59 uint64_t cluster, uint32_t extent, spdk_blob_op_complete cb_fn, void *cb_arg); 60 61 static int blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 62 uint16_t value_len, bool internal); 63 static int blob_get_xattr_value(struct spdk_blob *blob, const char *name, 64 const void **value, size_t *value_len, bool internal); 65 static int blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal); 66 67 static void blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num, 68 spdk_blob_op_complete cb_fn, void *cb_arg); 69 70 static int 71 blob_id_cmp(struct spdk_blob *blob1, struct spdk_blob *blob2) 72 { 73 return (blob1->id < blob2->id ? -1 : blob1->id > blob2->id); 74 } 75 76 RB_GENERATE_STATIC(spdk_blob_tree, spdk_blob, link, blob_id_cmp); 77 78 static void 79 blob_verify_md_op(struct spdk_blob *blob) 80 { 81 assert(blob != NULL); 82 assert(spdk_get_thread() == blob->bs->md_thread); 83 assert(blob->state != SPDK_BLOB_STATE_LOADING); 84 } 85 86 static struct spdk_blob_list * 87 bs_get_snapshot_entry(struct spdk_blob_store *bs, spdk_blob_id blobid) 88 { 89 struct spdk_blob_list *snapshot_entry = NULL; 90 91 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 92 if (snapshot_entry->id == blobid) { 93 break; 94 } 95 } 96 97 return snapshot_entry; 98 } 99 100 static void 101 bs_claim_md_page(struct spdk_blob_store *bs, uint32_t page) 102 { 103 assert(page < spdk_bit_array_capacity(bs->used_md_pages)); 104 assert(spdk_bit_array_get(bs->used_md_pages, page) == false); 105 106 spdk_bit_array_set(bs->used_md_pages, page); 107 } 108 109 static void 110 bs_release_md_page(struct spdk_blob_store *bs, uint32_t page) 111 { 112 assert(page < spdk_bit_array_capacity(bs->used_md_pages)); 113 assert(spdk_bit_array_get(bs->used_md_pages, page) == true); 114 115 spdk_bit_array_clear(bs->used_md_pages, page); 116 } 117 118 static uint32_t 119 bs_claim_cluster(struct spdk_blob_store *bs) 120 { 121 uint32_t cluster_num; 122 123 cluster_num = spdk_bit_pool_allocate_bit(bs->used_clusters); 124 if (cluster_num == UINT32_MAX) { 125 return UINT32_MAX; 126 } 127 128 SPDK_DEBUGLOG(blob, "Claiming cluster %u\n", cluster_num); 129 bs->num_free_clusters--; 130 131 return cluster_num; 132 } 133 134 static void 135 bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num) 136 { 137 assert(cluster_num < spdk_bit_pool_capacity(bs->used_clusters)); 138 assert(spdk_bit_pool_is_allocated(bs->used_clusters, cluster_num) == true); 139 assert(bs->num_free_clusters < bs->total_clusters); 140 141 SPDK_DEBUGLOG(blob, "Releasing cluster %u\n", cluster_num); 142 143 spdk_bit_pool_free_bit(bs->used_clusters, cluster_num); 144 bs->num_free_clusters++; 145 } 146 147 static int 148 blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster) 149 { 150 uint64_t *cluster_lba = &blob->active.clusters[cluster_num]; 151 152 blob_verify_md_op(blob); 153 154 if (*cluster_lba != 0) { 155 return -EEXIST; 156 } 157 158 *cluster_lba = bs_cluster_to_lba(blob->bs, cluster); 159 return 0; 160 } 161 162 static int 163 bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num, 164 uint64_t *cluster, uint32_t *lowest_free_md_page, bool update_map) 165 { 166 uint32_t *extent_page = 0; 167 168 *cluster = bs_claim_cluster(blob->bs); 169 if (*cluster == UINT32_MAX) { 170 /* No more free clusters. Cannot satisfy the request */ 171 return -ENOSPC; 172 } 173 174 if (blob->use_extent_table) { 175 extent_page = bs_cluster_to_extent_page(blob, cluster_num); 176 if (*extent_page == 0) { 177 /* Extent page shall never occupy md_page so start the search from 1 */ 178 if (*lowest_free_md_page == 0) { 179 *lowest_free_md_page = 1; 180 } 181 /* No extent_page is allocated for the cluster */ 182 *lowest_free_md_page = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, 183 *lowest_free_md_page); 184 if (*lowest_free_md_page == UINT32_MAX) { 185 /* No more free md pages. Cannot satisfy the request */ 186 bs_release_cluster(blob->bs, *cluster); 187 return -ENOSPC; 188 } 189 bs_claim_md_page(blob->bs, *lowest_free_md_page); 190 } 191 } 192 193 SPDK_DEBUGLOG(blob, "Claiming cluster %" PRIu64 " for blob %" PRIu64 "\n", *cluster, blob->id); 194 195 if (update_map) { 196 blob_insert_cluster(blob, cluster_num, *cluster); 197 if (blob->use_extent_table && *extent_page == 0) { 198 *extent_page = *lowest_free_md_page; 199 } 200 } 201 202 return 0; 203 } 204 205 static void 206 blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs) 207 { 208 xattrs->count = 0; 209 xattrs->names = NULL; 210 xattrs->ctx = NULL; 211 xattrs->get_value = NULL; 212 } 213 214 void 215 spdk_blob_opts_init(struct spdk_blob_opts *opts, size_t opts_size) 216 { 217 if (!opts) { 218 SPDK_ERRLOG("opts should not be NULL\n"); 219 return; 220 } 221 222 if (!opts_size) { 223 SPDK_ERRLOG("opts_size should not be zero value\n"); 224 return; 225 } 226 227 memset(opts, 0, opts_size); 228 opts->opts_size = opts_size; 229 230 #define FIELD_OK(field) \ 231 offsetof(struct spdk_blob_opts, field) + sizeof(opts->field) <= opts_size 232 233 #define SET_FIELD(field, value) \ 234 if (FIELD_OK(field)) { \ 235 opts->field = value; \ 236 } \ 237 238 SET_FIELD(num_clusters, 0); 239 SET_FIELD(thin_provision, false); 240 SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT); 241 242 if (FIELD_OK(xattrs)) { 243 blob_xattrs_init(&opts->xattrs); 244 } 245 246 SET_FIELD(use_extent_table, true); 247 248 #undef FIELD_OK 249 #undef SET_FIELD 250 } 251 252 void 253 spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts, size_t opts_size) 254 { 255 if (!opts) { 256 SPDK_ERRLOG("opts should not be NULL\n"); 257 return; 258 } 259 260 if (!opts_size) { 261 SPDK_ERRLOG("opts_size should not be zero value\n"); 262 return; 263 } 264 265 memset(opts, 0, opts_size); 266 opts->opts_size = opts_size; 267 268 #define FIELD_OK(field) \ 269 offsetof(struct spdk_blob_open_opts, field) + sizeof(opts->field) <= opts_size 270 271 #define SET_FIELD(field, value) \ 272 if (FIELD_OK(field)) { \ 273 opts->field = value; \ 274 } \ 275 276 SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT); 277 278 #undef FIELD_OK 279 #undef SET_FILED 280 } 281 282 static struct spdk_blob * 283 blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id) 284 { 285 struct spdk_blob *blob; 286 287 blob = calloc(1, sizeof(*blob)); 288 if (!blob) { 289 return NULL; 290 } 291 292 blob->id = id; 293 blob->bs = bs; 294 295 blob->parent_id = SPDK_BLOBID_INVALID; 296 297 blob->state = SPDK_BLOB_STATE_DIRTY; 298 blob->extent_rle_found = false; 299 blob->extent_table_found = false; 300 blob->active.num_pages = 1; 301 blob->active.pages = calloc(1, sizeof(*blob->active.pages)); 302 if (!blob->active.pages) { 303 free(blob); 304 return NULL; 305 } 306 307 blob->active.pages[0] = bs_blobid_to_page(id); 308 309 TAILQ_INIT(&blob->xattrs); 310 TAILQ_INIT(&blob->xattrs_internal); 311 TAILQ_INIT(&blob->pending_persists); 312 TAILQ_INIT(&blob->persists_to_complete); 313 314 return blob; 315 } 316 317 static void 318 xattrs_free(struct spdk_xattr_tailq *xattrs) 319 { 320 struct spdk_xattr *xattr, *xattr_tmp; 321 322 TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) { 323 TAILQ_REMOVE(xattrs, xattr, link); 324 free(xattr->name); 325 free(xattr->value); 326 free(xattr); 327 } 328 } 329 330 static void 331 blob_free(struct spdk_blob *blob) 332 { 333 assert(blob != NULL); 334 assert(TAILQ_EMPTY(&blob->pending_persists)); 335 assert(TAILQ_EMPTY(&blob->persists_to_complete)); 336 337 free(blob->active.extent_pages); 338 free(blob->clean.extent_pages); 339 free(blob->active.clusters); 340 free(blob->clean.clusters); 341 free(blob->active.pages); 342 free(blob->clean.pages); 343 344 xattrs_free(&blob->xattrs); 345 xattrs_free(&blob->xattrs_internal); 346 347 if (blob->back_bs_dev) { 348 blob->back_bs_dev->destroy(blob->back_bs_dev); 349 } 350 351 free(blob); 352 } 353 354 struct freeze_io_ctx { 355 struct spdk_bs_cpl cpl; 356 struct spdk_blob *blob; 357 }; 358 359 static void 360 blob_io_sync(struct spdk_io_channel_iter *i) 361 { 362 spdk_for_each_channel_continue(i, 0); 363 } 364 365 static void 366 blob_execute_queued_io(struct spdk_io_channel_iter *i) 367 { 368 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 369 struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch); 370 struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 371 struct spdk_bs_request_set *set; 372 struct spdk_bs_user_op_args *args; 373 spdk_bs_user_op_t *op, *tmp; 374 375 TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) { 376 set = (struct spdk_bs_request_set *)op; 377 args = &set->u.user_op; 378 379 if (args->blob == ctx->blob) { 380 TAILQ_REMOVE(&ch->queued_io, op, link); 381 bs_user_op_execute(op); 382 } 383 } 384 385 spdk_for_each_channel_continue(i, 0); 386 } 387 388 static void 389 blob_io_cpl(struct spdk_io_channel_iter *i, int status) 390 { 391 struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 392 393 ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0); 394 395 free(ctx); 396 } 397 398 static void 399 blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 400 { 401 struct freeze_io_ctx *ctx; 402 403 ctx = calloc(1, sizeof(*ctx)); 404 if (!ctx) { 405 cb_fn(cb_arg, -ENOMEM); 406 return; 407 } 408 409 ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 410 ctx->cpl.u.blob_basic.cb_fn = cb_fn; 411 ctx->cpl.u.blob_basic.cb_arg = cb_arg; 412 ctx->blob = blob; 413 414 /* Freeze I/O on blob */ 415 blob->frozen_refcnt++; 416 417 if (blob->frozen_refcnt == 1) { 418 spdk_for_each_channel(blob->bs, blob_io_sync, ctx, blob_io_cpl); 419 } else { 420 cb_fn(cb_arg, 0); 421 free(ctx); 422 } 423 } 424 425 static void 426 blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 427 { 428 struct freeze_io_ctx *ctx; 429 430 ctx = calloc(1, sizeof(*ctx)); 431 if (!ctx) { 432 cb_fn(cb_arg, -ENOMEM); 433 return; 434 } 435 436 ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 437 ctx->cpl.u.blob_basic.cb_fn = cb_fn; 438 ctx->cpl.u.blob_basic.cb_arg = cb_arg; 439 ctx->blob = blob; 440 441 assert(blob->frozen_refcnt > 0); 442 443 blob->frozen_refcnt--; 444 445 if (blob->frozen_refcnt == 0) { 446 spdk_for_each_channel(blob->bs, blob_execute_queued_io, ctx, blob_io_cpl); 447 } else { 448 cb_fn(cb_arg, 0); 449 free(ctx); 450 } 451 } 452 453 static int 454 blob_mark_clean(struct spdk_blob *blob) 455 { 456 uint32_t *extent_pages = NULL; 457 uint64_t *clusters = NULL; 458 uint32_t *pages = NULL; 459 460 assert(blob != NULL); 461 462 if (blob->active.num_extent_pages) { 463 assert(blob->active.extent_pages); 464 extent_pages = calloc(blob->active.num_extent_pages, sizeof(*blob->active.extent_pages)); 465 if (!extent_pages) { 466 return -ENOMEM; 467 } 468 memcpy(extent_pages, blob->active.extent_pages, 469 blob->active.num_extent_pages * sizeof(*extent_pages)); 470 } 471 472 if (blob->active.num_clusters) { 473 assert(blob->active.clusters); 474 clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters)); 475 if (!clusters) { 476 free(extent_pages); 477 return -ENOMEM; 478 } 479 memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters)); 480 } 481 482 if (blob->active.num_pages) { 483 assert(blob->active.pages); 484 pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages)); 485 if (!pages) { 486 free(extent_pages); 487 free(clusters); 488 return -ENOMEM; 489 } 490 memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages)); 491 } 492 493 free(blob->clean.extent_pages); 494 free(blob->clean.clusters); 495 free(blob->clean.pages); 496 497 blob->clean.num_extent_pages = blob->active.num_extent_pages; 498 blob->clean.extent_pages = blob->active.extent_pages; 499 blob->clean.num_clusters = blob->active.num_clusters; 500 blob->clean.clusters = blob->active.clusters; 501 blob->clean.num_pages = blob->active.num_pages; 502 blob->clean.pages = blob->active.pages; 503 504 blob->active.extent_pages = extent_pages; 505 blob->active.clusters = clusters; 506 blob->active.pages = pages; 507 508 /* If the metadata was dirtied again while the metadata was being written to disk, 509 * we do not want to revert the DIRTY state back to CLEAN here. 510 */ 511 if (blob->state == SPDK_BLOB_STATE_LOADING) { 512 blob->state = SPDK_BLOB_STATE_CLEAN; 513 } 514 515 return 0; 516 } 517 518 static int 519 blob_deserialize_xattr(struct spdk_blob *blob, 520 struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal) 521 { 522 struct spdk_xattr *xattr; 523 524 if (desc_xattr->length != sizeof(desc_xattr->name_length) + 525 sizeof(desc_xattr->value_length) + 526 desc_xattr->name_length + desc_xattr->value_length) { 527 return -EINVAL; 528 } 529 530 xattr = calloc(1, sizeof(*xattr)); 531 if (xattr == NULL) { 532 return -ENOMEM; 533 } 534 535 xattr->name = malloc(desc_xattr->name_length + 1); 536 if (xattr->name == NULL) { 537 free(xattr); 538 return -ENOMEM; 539 } 540 memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length); 541 xattr->name[desc_xattr->name_length] = '\0'; 542 543 xattr->value = malloc(desc_xattr->value_length); 544 if (xattr->value == NULL) { 545 free(xattr->name); 546 free(xattr); 547 return -ENOMEM; 548 } 549 xattr->value_len = desc_xattr->value_length; 550 memcpy(xattr->value, 551 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 552 desc_xattr->value_length); 553 554 TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link); 555 556 return 0; 557 } 558 559 560 static int 561 blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob) 562 { 563 struct spdk_blob_md_descriptor *desc; 564 size_t cur_desc = 0; 565 void *tmp; 566 567 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 568 while (cur_desc < sizeof(page->descriptors)) { 569 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 570 if (desc->length == 0) { 571 /* If padding and length are 0, this terminates the page */ 572 break; 573 } 574 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 575 struct spdk_blob_md_descriptor_flags *desc_flags; 576 577 desc_flags = (struct spdk_blob_md_descriptor_flags *)desc; 578 579 if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) { 580 return -EINVAL; 581 } 582 583 if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) != 584 SPDK_BLOB_INVALID_FLAGS_MASK) { 585 return -EINVAL; 586 } 587 588 if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) != 589 SPDK_BLOB_DATA_RO_FLAGS_MASK) { 590 blob->data_ro = true; 591 blob->md_ro = true; 592 } 593 594 if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) != 595 SPDK_BLOB_MD_RO_FLAGS_MASK) { 596 blob->md_ro = true; 597 } 598 599 if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 600 blob->data_ro = true; 601 blob->md_ro = true; 602 } 603 604 blob->invalid_flags = desc_flags->invalid_flags; 605 blob->data_ro_flags = desc_flags->data_ro_flags; 606 blob->md_ro_flags = desc_flags->md_ro_flags; 607 608 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 609 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 610 unsigned int i, j; 611 unsigned int cluster_count = blob->active.num_clusters; 612 613 if (blob->extent_table_found) { 614 /* Extent Table already present in the md, 615 * both descriptors should never be at the same time. */ 616 return -EINVAL; 617 } 618 blob->extent_rle_found = true; 619 620 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 621 622 if (desc_extent_rle->length == 0 || 623 (desc_extent_rle->length % sizeof(desc_extent_rle->extents[0]) != 0)) { 624 return -EINVAL; 625 } 626 627 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 628 for (j = 0; j < desc_extent_rle->extents[i].length; j++) { 629 if (desc_extent_rle->extents[i].cluster_idx != 0) { 630 if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters, 631 desc_extent_rle->extents[i].cluster_idx + j)) { 632 return -EINVAL; 633 } 634 } 635 cluster_count++; 636 } 637 } 638 639 if (cluster_count == 0) { 640 return -EINVAL; 641 } 642 tmp = realloc(blob->active.clusters, cluster_count * sizeof(*blob->active.clusters)); 643 if (tmp == NULL) { 644 return -ENOMEM; 645 } 646 blob->active.clusters = tmp; 647 blob->active.cluster_array_size = cluster_count; 648 649 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 650 for (j = 0; j < desc_extent_rle->extents[i].length; j++) { 651 if (desc_extent_rle->extents[i].cluster_idx != 0) { 652 blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs, 653 desc_extent_rle->extents[i].cluster_idx + j); 654 } else if (spdk_blob_is_thin_provisioned(blob)) { 655 blob->active.clusters[blob->active.num_clusters++] = 0; 656 } else { 657 return -EINVAL; 658 } 659 } 660 } 661 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) { 662 struct spdk_blob_md_descriptor_extent_table *desc_extent_table; 663 uint32_t num_extent_pages = blob->active.num_extent_pages; 664 uint32_t i, j; 665 size_t extent_pages_length; 666 667 desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc; 668 extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters); 669 670 if (blob->extent_rle_found) { 671 /* This means that Extent RLE is present in MD, 672 * both should never be at the same time. */ 673 return -EINVAL; 674 } else if (blob->extent_table_found && 675 desc_extent_table->num_clusters != blob->remaining_clusters_in_et) { 676 /* Number of clusters in this ET does not match number 677 * from previously read EXTENT_TABLE. */ 678 return -EINVAL; 679 } 680 681 if (desc_extent_table->length == 0 || 682 (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) { 683 return -EINVAL; 684 } 685 686 blob->extent_table_found = true; 687 688 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 689 num_extent_pages += desc_extent_table->extent_page[i].num_pages; 690 } 691 692 if (num_extent_pages > 0) { 693 tmp = realloc(blob->active.extent_pages, num_extent_pages * sizeof(uint32_t)); 694 if (tmp == NULL) { 695 return -ENOMEM; 696 } 697 blob->active.extent_pages = tmp; 698 } 699 blob->active.extent_pages_array_size = num_extent_pages; 700 701 blob->remaining_clusters_in_et = desc_extent_table->num_clusters; 702 703 /* Extent table entries contain md page numbers for extent pages. 704 * Zeroes represent unallocated extent pages, those are run-length-encoded. 705 */ 706 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 707 if (desc_extent_table->extent_page[i].page_idx != 0) { 708 assert(desc_extent_table->extent_page[i].num_pages == 1); 709 blob->active.extent_pages[blob->active.num_extent_pages++] = 710 desc_extent_table->extent_page[i].page_idx; 711 } else if (spdk_blob_is_thin_provisioned(blob)) { 712 for (j = 0; j < desc_extent_table->extent_page[i].num_pages; j++) { 713 blob->active.extent_pages[blob->active.num_extent_pages++] = 0; 714 } 715 } else { 716 return -EINVAL; 717 } 718 } 719 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 720 struct spdk_blob_md_descriptor_extent_page *desc_extent; 721 unsigned int i; 722 unsigned int cluster_count = 0; 723 size_t cluster_idx_length; 724 725 if (blob->extent_rle_found) { 726 /* This means that Extent RLE is present in MD, 727 * both should never be at the same time. */ 728 return -EINVAL; 729 } 730 731 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 732 cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx); 733 734 if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) || 735 (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) { 736 return -EINVAL; 737 } 738 739 for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) { 740 if (desc_extent->cluster_idx[i] != 0) { 741 if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters, desc_extent->cluster_idx[i])) { 742 return -EINVAL; 743 } 744 } 745 cluster_count++; 746 } 747 748 if (cluster_count == 0) { 749 return -EINVAL; 750 } 751 752 /* When reading extent pages sequentially starting cluster idx should match 753 * current size of a blob. 754 * If changed to batch reading, this check shall be removed. */ 755 if (desc_extent->start_cluster_idx != blob->active.num_clusters) { 756 return -EINVAL; 757 } 758 759 tmp = realloc(blob->active.clusters, 760 (cluster_count + blob->active.num_clusters) * sizeof(*blob->active.clusters)); 761 if (tmp == NULL) { 762 return -ENOMEM; 763 } 764 blob->active.clusters = tmp; 765 blob->active.cluster_array_size = (cluster_count + blob->active.num_clusters); 766 767 for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) { 768 if (desc_extent->cluster_idx[i] != 0) { 769 blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs, 770 desc_extent->cluster_idx[i]); 771 } else if (spdk_blob_is_thin_provisioned(blob)) { 772 blob->active.clusters[blob->active.num_clusters++] = 0; 773 } else { 774 return -EINVAL; 775 } 776 } 777 assert(desc_extent->start_cluster_idx + cluster_count == blob->active.num_clusters); 778 assert(blob->remaining_clusters_in_et >= cluster_count); 779 blob->remaining_clusters_in_et -= cluster_count; 780 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 781 int rc; 782 783 rc = blob_deserialize_xattr(blob, 784 (struct spdk_blob_md_descriptor_xattr *) desc, false); 785 if (rc != 0) { 786 return rc; 787 } 788 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 789 int rc; 790 791 rc = blob_deserialize_xattr(blob, 792 (struct spdk_blob_md_descriptor_xattr *) desc, true); 793 if (rc != 0) { 794 return rc; 795 } 796 } else { 797 /* Unrecognized descriptor type. Do not fail - just continue to the 798 * next descriptor. If this descriptor is associated with some feature 799 * defined in a newer version of blobstore, that version of blobstore 800 * should create and set an associated feature flag to specify if this 801 * blob can be loaded or not. 802 */ 803 } 804 805 /* Advance to the next descriptor */ 806 cur_desc += sizeof(*desc) + desc->length; 807 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 808 break; 809 } 810 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 811 } 812 813 return 0; 814 } 815 816 static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page); 817 818 static int 819 blob_parse_extent_page(struct spdk_blob_md_page *extent_page, struct spdk_blob *blob) 820 { 821 assert(blob != NULL); 822 assert(blob->state == SPDK_BLOB_STATE_LOADING); 823 824 if (bs_load_cur_extent_page_valid(extent_page) == false) { 825 return -ENOENT; 826 } 827 828 return blob_parse_page(extent_page, blob); 829 } 830 831 static int 832 blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count, 833 struct spdk_blob *blob) 834 { 835 const struct spdk_blob_md_page *page; 836 uint32_t i; 837 int rc; 838 void *tmp; 839 840 assert(page_count > 0); 841 assert(pages[0].sequence_num == 0); 842 assert(blob != NULL); 843 assert(blob->state == SPDK_BLOB_STATE_LOADING); 844 assert(blob->active.clusters == NULL); 845 846 /* The blobid provided doesn't match what's in the MD, this can 847 * happen for example if a bogus blobid is passed in through open. 848 */ 849 if (blob->id != pages[0].id) { 850 SPDK_ERRLOG("Blobid (%" PRIu64 ") doesn't match what's in metadata (%" PRIu64 ")\n", 851 blob->id, pages[0].id); 852 return -ENOENT; 853 } 854 855 tmp = realloc(blob->active.pages, page_count * sizeof(*blob->active.pages)); 856 if (!tmp) { 857 return -ENOMEM; 858 } 859 blob->active.pages = tmp; 860 861 blob->active.pages[0] = pages[0].id; 862 863 for (i = 1; i < page_count; i++) { 864 assert(spdk_bit_array_get(blob->bs->used_md_pages, pages[i - 1].next)); 865 blob->active.pages[i] = pages[i - 1].next; 866 } 867 blob->active.num_pages = page_count; 868 869 for (i = 0; i < page_count; i++) { 870 page = &pages[i]; 871 872 assert(page->id == blob->id); 873 assert(page->sequence_num == i); 874 875 rc = blob_parse_page(page, blob); 876 if (rc != 0) { 877 return rc; 878 } 879 } 880 881 return 0; 882 } 883 884 static int 885 blob_serialize_add_page(const struct spdk_blob *blob, 886 struct spdk_blob_md_page **pages, 887 uint32_t *page_count, 888 struct spdk_blob_md_page **last_page) 889 { 890 struct spdk_blob_md_page *page, *tmp_pages; 891 892 assert(pages != NULL); 893 assert(page_count != NULL); 894 895 *last_page = NULL; 896 if (*page_count == 0) { 897 assert(*pages == NULL); 898 *pages = spdk_malloc(SPDK_BS_PAGE_SIZE, 0, 899 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 900 if (*pages == NULL) { 901 return -ENOMEM; 902 } 903 *page_count = 1; 904 } else { 905 assert(*pages != NULL); 906 tmp_pages = spdk_realloc(*pages, SPDK_BS_PAGE_SIZE * (*page_count + 1), 0); 907 if (tmp_pages == NULL) { 908 return -ENOMEM; 909 } 910 (*page_count)++; 911 *pages = tmp_pages; 912 } 913 914 page = &(*pages)[*page_count - 1]; 915 memset(page, 0, sizeof(*page)); 916 page->id = blob->id; 917 page->sequence_num = *page_count - 1; 918 page->next = SPDK_INVALID_MD_PAGE; 919 *last_page = page; 920 921 return 0; 922 } 923 924 /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor. 925 * Update required_sz on both success and failure. 926 * 927 */ 928 static int 929 blob_serialize_xattr(const struct spdk_xattr *xattr, 930 uint8_t *buf, size_t buf_sz, 931 size_t *required_sz, bool internal) 932 { 933 struct spdk_blob_md_descriptor_xattr *desc; 934 935 *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) + 936 strlen(xattr->name) + 937 xattr->value_len; 938 939 if (buf_sz < *required_sz) { 940 return -1; 941 } 942 943 desc = (struct spdk_blob_md_descriptor_xattr *)buf; 944 945 desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR; 946 desc->length = sizeof(desc->name_length) + 947 sizeof(desc->value_length) + 948 strlen(xattr->name) + 949 xattr->value_len; 950 desc->name_length = strlen(xattr->name); 951 desc->value_length = xattr->value_len; 952 953 memcpy(desc->name, xattr->name, desc->name_length); 954 memcpy((void *)((uintptr_t)desc->name + desc->name_length), 955 xattr->value, 956 desc->value_length); 957 958 return 0; 959 } 960 961 static void 962 blob_serialize_extent_table_entry(const struct spdk_blob *blob, 963 uint64_t start_ep, uint64_t *next_ep, 964 uint8_t **buf, size_t *remaining_sz) 965 { 966 struct spdk_blob_md_descriptor_extent_table *desc; 967 size_t cur_sz; 968 uint64_t i, et_idx; 969 uint32_t extent_page, ep_len; 970 971 /* The buffer must have room for at least num_clusters entry */ 972 cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->num_clusters); 973 if (*remaining_sz < cur_sz) { 974 *next_ep = start_ep; 975 return; 976 } 977 978 desc = (struct spdk_blob_md_descriptor_extent_table *)*buf; 979 desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE; 980 981 desc->num_clusters = blob->active.num_clusters; 982 983 ep_len = 1; 984 et_idx = 0; 985 for (i = start_ep; i < blob->active.num_extent_pages; i++) { 986 if (*remaining_sz < cur_sz + sizeof(desc->extent_page[0])) { 987 /* If we ran out of buffer space, return */ 988 break; 989 } 990 991 extent_page = blob->active.extent_pages[i]; 992 /* Verify that next extent_page is unallocated */ 993 if (extent_page == 0 && 994 (i + 1 < blob->active.num_extent_pages && blob->active.extent_pages[i + 1] == 0)) { 995 ep_len++; 996 continue; 997 } 998 desc->extent_page[et_idx].page_idx = extent_page; 999 desc->extent_page[et_idx].num_pages = ep_len; 1000 et_idx++; 1001 1002 ep_len = 1; 1003 cur_sz += sizeof(desc->extent_page[et_idx]); 1004 } 1005 *next_ep = i; 1006 1007 desc->length = sizeof(desc->num_clusters) + sizeof(desc->extent_page[0]) * et_idx; 1008 *remaining_sz -= sizeof(struct spdk_blob_md_descriptor) + desc->length; 1009 *buf += sizeof(struct spdk_blob_md_descriptor) + desc->length; 1010 } 1011 1012 static int 1013 blob_serialize_extent_table(const struct spdk_blob *blob, 1014 struct spdk_blob_md_page **pages, 1015 struct spdk_blob_md_page *cur_page, 1016 uint32_t *page_count, uint8_t **buf, 1017 size_t *remaining_sz) 1018 { 1019 uint64_t last_extent_page; 1020 int rc; 1021 1022 last_extent_page = 0; 1023 /* At least single extent table entry has to be always persisted. 1024 * Such case occurs with num_extent_pages == 0. */ 1025 while (last_extent_page <= blob->active.num_extent_pages) { 1026 blob_serialize_extent_table_entry(blob, last_extent_page, &last_extent_page, buf, 1027 remaining_sz); 1028 1029 if (last_extent_page == blob->active.num_extent_pages) { 1030 break; 1031 } 1032 1033 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page); 1034 if (rc < 0) { 1035 return rc; 1036 } 1037 1038 *buf = (uint8_t *)cur_page->descriptors; 1039 *remaining_sz = sizeof(cur_page->descriptors); 1040 } 1041 1042 return 0; 1043 } 1044 1045 static void 1046 blob_serialize_extent_rle(const struct spdk_blob *blob, 1047 uint64_t start_cluster, uint64_t *next_cluster, 1048 uint8_t **buf, size_t *buf_sz) 1049 { 1050 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 1051 size_t cur_sz; 1052 uint64_t i, extent_idx; 1053 uint64_t lba, lba_per_cluster, lba_count; 1054 1055 /* The buffer must have room for at least one extent */ 1056 cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc_extent_rle->extents[0]); 1057 if (*buf_sz < cur_sz) { 1058 *next_cluster = start_cluster; 1059 return; 1060 } 1061 1062 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)*buf; 1063 desc_extent_rle->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE; 1064 1065 lba_per_cluster = bs_cluster_to_lba(blob->bs, 1); 1066 1067 lba = blob->active.clusters[start_cluster]; 1068 lba_count = lba_per_cluster; 1069 extent_idx = 0; 1070 for (i = start_cluster + 1; i < blob->active.num_clusters; i++) { 1071 if ((lba + lba_count) == blob->active.clusters[i] && lba != 0) { 1072 /* Run-length encode sequential non-zero LBA */ 1073 lba_count += lba_per_cluster; 1074 continue; 1075 } else if (lba == 0 && blob->active.clusters[i] == 0) { 1076 /* Run-length encode unallocated clusters */ 1077 lba_count += lba_per_cluster; 1078 continue; 1079 } 1080 desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster; 1081 desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster; 1082 extent_idx++; 1083 1084 cur_sz += sizeof(desc_extent_rle->extents[extent_idx]); 1085 1086 if (*buf_sz < cur_sz) { 1087 /* If we ran out of buffer space, return */ 1088 *next_cluster = i; 1089 break; 1090 } 1091 1092 lba = blob->active.clusters[i]; 1093 lba_count = lba_per_cluster; 1094 } 1095 1096 if (*buf_sz >= cur_sz) { 1097 desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster; 1098 desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster; 1099 extent_idx++; 1100 1101 *next_cluster = blob->active.num_clusters; 1102 } 1103 1104 desc_extent_rle->length = sizeof(desc_extent_rle->extents[0]) * extent_idx; 1105 *buf_sz -= sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length; 1106 *buf += sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length; 1107 } 1108 1109 static int 1110 blob_serialize_extents_rle(const struct spdk_blob *blob, 1111 struct spdk_blob_md_page **pages, 1112 struct spdk_blob_md_page *cur_page, 1113 uint32_t *page_count, uint8_t **buf, 1114 size_t *remaining_sz) 1115 { 1116 uint64_t last_cluster; 1117 int rc; 1118 1119 last_cluster = 0; 1120 while (last_cluster < blob->active.num_clusters) { 1121 blob_serialize_extent_rle(blob, last_cluster, &last_cluster, buf, remaining_sz); 1122 1123 if (last_cluster == blob->active.num_clusters) { 1124 break; 1125 } 1126 1127 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page); 1128 if (rc < 0) { 1129 return rc; 1130 } 1131 1132 *buf = (uint8_t *)cur_page->descriptors; 1133 *remaining_sz = sizeof(cur_page->descriptors); 1134 } 1135 1136 return 0; 1137 } 1138 1139 static void 1140 blob_serialize_extent_page(const struct spdk_blob *blob, 1141 uint64_t cluster, struct spdk_blob_md_page *page) 1142 { 1143 struct spdk_blob_md_descriptor_extent_page *desc_extent; 1144 uint64_t i, extent_idx; 1145 uint64_t lba, lba_per_cluster; 1146 uint64_t start_cluster_idx = (cluster / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP; 1147 1148 desc_extent = (struct spdk_blob_md_descriptor_extent_page *) page->descriptors; 1149 desc_extent->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE; 1150 1151 lba_per_cluster = bs_cluster_to_lba(blob->bs, 1); 1152 1153 desc_extent->start_cluster_idx = start_cluster_idx; 1154 extent_idx = 0; 1155 for (i = start_cluster_idx; i < blob->active.num_clusters; i++) { 1156 lba = blob->active.clusters[i]; 1157 desc_extent->cluster_idx[extent_idx++] = lba / lba_per_cluster; 1158 if (extent_idx >= SPDK_EXTENTS_PER_EP) { 1159 break; 1160 } 1161 } 1162 desc_extent->length = sizeof(desc_extent->start_cluster_idx) + 1163 sizeof(desc_extent->cluster_idx[0]) * extent_idx; 1164 } 1165 1166 static void 1167 blob_serialize_flags(const struct spdk_blob *blob, 1168 uint8_t *buf, size_t *buf_sz) 1169 { 1170 struct spdk_blob_md_descriptor_flags *desc; 1171 1172 /* 1173 * Flags get serialized first, so we should always have room for the flags 1174 * descriptor. 1175 */ 1176 assert(*buf_sz >= sizeof(*desc)); 1177 1178 desc = (struct spdk_blob_md_descriptor_flags *)buf; 1179 desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS; 1180 desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor); 1181 desc->invalid_flags = blob->invalid_flags; 1182 desc->data_ro_flags = blob->data_ro_flags; 1183 desc->md_ro_flags = blob->md_ro_flags; 1184 1185 *buf_sz -= sizeof(*desc); 1186 } 1187 1188 static int 1189 blob_serialize_xattrs(const struct spdk_blob *blob, 1190 const struct spdk_xattr_tailq *xattrs, bool internal, 1191 struct spdk_blob_md_page **pages, 1192 struct spdk_blob_md_page *cur_page, 1193 uint32_t *page_count, uint8_t **buf, 1194 size_t *remaining_sz) 1195 { 1196 const struct spdk_xattr *xattr; 1197 int rc; 1198 1199 TAILQ_FOREACH(xattr, xattrs, link) { 1200 size_t required_sz = 0; 1201 1202 rc = blob_serialize_xattr(xattr, 1203 *buf, *remaining_sz, 1204 &required_sz, internal); 1205 if (rc < 0) { 1206 /* Need to add a new page to the chain */ 1207 rc = blob_serialize_add_page(blob, pages, page_count, 1208 &cur_page); 1209 if (rc < 0) { 1210 spdk_free(*pages); 1211 *pages = NULL; 1212 *page_count = 0; 1213 return rc; 1214 } 1215 1216 *buf = (uint8_t *)cur_page->descriptors; 1217 *remaining_sz = sizeof(cur_page->descriptors); 1218 1219 /* Try again */ 1220 required_sz = 0; 1221 rc = blob_serialize_xattr(xattr, 1222 *buf, *remaining_sz, 1223 &required_sz, internal); 1224 1225 if (rc < 0) { 1226 spdk_free(*pages); 1227 *pages = NULL; 1228 *page_count = 0; 1229 return rc; 1230 } 1231 } 1232 1233 *remaining_sz -= required_sz; 1234 *buf += required_sz; 1235 } 1236 1237 return 0; 1238 } 1239 1240 static int 1241 blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages, 1242 uint32_t *page_count) 1243 { 1244 struct spdk_blob_md_page *cur_page; 1245 int rc; 1246 uint8_t *buf; 1247 size_t remaining_sz; 1248 1249 assert(pages != NULL); 1250 assert(page_count != NULL); 1251 assert(blob != NULL); 1252 assert(blob->state == SPDK_BLOB_STATE_DIRTY); 1253 1254 *pages = NULL; 1255 *page_count = 0; 1256 1257 /* A blob always has at least 1 page, even if it has no descriptors */ 1258 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page); 1259 if (rc < 0) { 1260 return rc; 1261 } 1262 1263 buf = (uint8_t *)cur_page->descriptors; 1264 remaining_sz = sizeof(cur_page->descriptors); 1265 1266 /* Serialize flags */ 1267 blob_serialize_flags(blob, buf, &remaining_sz); 1268 buf += sizeof(struct spdk_blob_md_descriptor_flags); 1269 1270 /* Serialize xattrs */ 1271 rc = blob_serialize_xattrs(blob, &blob->xattrs, false, 1272 pages, cur_page, page_count, &buf, &remaining_sz); 1273 if (rc < 0) { 1274 return rc; 1275 } 1276 1277 /* Serialize internal xattrs */ 1278 rc = blob_serialize_xattrs(blob, &blob->xattrs_internal, true, 1279 pages, cur_page, page_count, &buf, &remaining_sz); 1280 if (rc < 0) { 1281 return rc; 1282 } 1283 1284 if (blob->use_extent_table) { 1285 /* Serialize extent table */ 1286 rc = blob_serialize_extent_table(blob, pages, cur_page, page_count, &buf, &remaining_sz); 1287 } else { 1288 /* Serialize extents */ 1289 rc = blob_serialize_extents_rle(blob, pages, cur_page, page_count, &buf, &remaining_sz); 1290 } 1291 1292 return rc; 1293 } 1294 1295 struct spdk_blob_load_ctx { 1296 struct spdk_blob *blob; 1297 1298 struct spdk_blob_md_page *pages; 1299 uint32_t num_pages; 1300 uint32_t next_extent_page; 1301 spdk_bs_sequence_t *seq; 1302 1303 spdk_bs_sequence_cpl cb_fn; 1304 void *cb_arg; 1305 }; 1306 1307 static uint32_t 1308 blob_md_page_calc_crc(void *page) 1309 { 1310 uint32_t crc; 1311 1312 crc = BLOB_CRC32C_INITIAL; 1313 crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc); 1314 crc ^= BLOB_CRC32C_INITIAL; 1315 1316 return crc; 1317 1318 } 1319 1320 static void 1321 blob_load_final(struct spdk_blob_load_ctx *ctx, int bserrno) 1322 { 1323 struct spdk_blob *blob = ctx->blob; 1324 1325 if (bserrno == 0) { 1326 blob_mark_clean(blob); 1327 } 1328 1329 ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno); 1330 1331 /* Free the memory */ 1332 spdk_free(ctx->pages); 1333 free(ctx); 1334 } 1335 1336 static void 1337 blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno) 1338 { 1339 struct spdk_blob_load_ctx *ctx = cb_arg; 1340 struct spdk_blob *blob = ctx->blob; 1341 1342 if (bserrno == 0) { 1343 blob->back_bs_dev = bs_create_blob_bs_dev(snapshot); 1344 if (blob->back_bs_dev == NULL) { 1345 bserrno = -ENOMEM; 1346 } 1347 } 1348 if (bserrno != 0) { 1349 SPDK_ERRLOG("Snapshot fail\n"); 1350 } 1351 1352 blob_load_final(ctx, bserrno); 1353 } 1354 1355 static void blob_update_clear_method(struct spdk_blob *blob); 1356 1357 static void 1358 blob_load_backing_dev(void *cb_arg) 1359 { 1360 struct spdk_blob_load_ctx *ctx = cb_arg; 1361 struct spdk_blob *blob = ctx->blob; 1362 const void *value; 1363 size_t len; 1364 int rc; 1365 1366 if (spdk_blob_is_thin_provisioned(blob)) { 1367 rc = blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true); 1368 if (rc == 0) { 1369 if (len != sizeof(spdk_blob_id)) { 1370 blob_load_final(ctx, -EINVAL); 1371 return; 1372 } 1373 /* open snapshot blob and continue in the callback function */ 1374 blob->parent_id = *(spdk_blob_id *)value; 1375 spdk_bs_open_blob(blob->bs, blob->parent_id, 1376 blob_load_snapshot_cpl, ctx); 1377 return; 1378 } else { 1379 /* add zeroes_dev for thin provisioned blob */ 1380 blob->back_bs_dev = bs_create_zeroes_dev(); 1381 } 1382 } else { 1383 /* standard blob */ 1384 blob->back_bs_dev = NULL; 1385 } 1386 blob_load_final(ctx, 0); 1387 } 1388 1389 static void 1390 blob_load_cpl_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1391 { 1392 struct spdk_blob_load_ctx *ctx = cb_arg; 1393 struct spdk_blob *blob = ctx->blob; 1394 struct spdk_blob_md_page *page; 1395 uint64_t i; 1396 uint32_t crc; 1397 uint64_t lba; 1398 void *tmp; 1399 uint64_t sz; 1400 1401 if (bserrno) { 1402 SPDK_ERRLOG("Extent page read failed: %d\n", bserrno); 1403 blob_load_final(ctx, bserrno); 1404 return; 1405 } 1406 1407 if (ctx->pages == NULL) { 1408 /* First iteration of this function, allocate buffer for single EXTENT_PAGE */ 1409 ctx->pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 1410 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 1411 if (!ctx->pages) { 1412 blob_load_final(ctx, -ENOMEM); 1413 return; 1414 } 1415 ctx->num_pages = 1; 1416 ctx->next_extent_page = 0; 1417 } else { 1418 page = &ctx->pages[0]; 1419 crc = blob_md_page_calc_crc(page); 1420 if (crc != page->crc) { 1421 blob_load_final(ctx, -EINVAL); 1422 return; 1423 } 1424 1425 if (page->next != SPDK_INVALID_MD_PAGE) { 1426 blob_load_final(ctx, -EINVAL); 1427 return; 1428 } 1429 1430 bserrno = blob_parse_extent_page(page, blob); 1431 if (bserrno) { 1432 blob_load_final(ctx, bserrno); 1433 return; 1434 } 1435 } 1436 1437 for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) { 1438 if (blob->active.extent_pages[i] != 0) { 1439 /* Extent page was allocated, read and parse it. */ 1440 lba = bs_md_page_to_lba(blob->bs, blob->active.extent_pages[i]); 1441 ctx->next_extent_page = i + 1; 1442 1443 bs_sequence_read_dev(seq, &ctx->pages[0], lba, 1444 bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE), 1445 blob_load_cpl_extents_cpl, ctx); 1446 return; 1447 } else { 1448 /* Thin provisioned blobs can point to unallocated extent pages. 1449 * In this case blob size should be increased by up to the amount left in remaining_clusters_in_et. */ 1450 1451 sz = spdk_min(blob->remaining_clusters_in_et, SPDK_EXTENTS_PER_EP); 1452 blob->active.num_clusters += sz; 1453 blob->remaining_clusters_in_et -= sz; 1454 1455 assert(spdk_blob_is_thin_provisioned(blob)); 1456 assert(i + 1 < blob->active.num_extent_pages || blob->remaining_clusters_in_et == 0); 1457 1458 tmp = realloc(blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters)); 1459 if (tmp == NULL) { 1460 blob_load_final(ctx, -ENOMEM); 1461 return; 1462 } 1463 memset(tmp + sizeof(*blob->active.clusters) * blob->active.cluster_array_size, 0, 1464 sizeof(*blob->active.clusters) * (blob->active.num_clusters - blob->active.cluster_array_size)); 1465 blob->active.clusters = tmp; 1466 blob->active.cluster_array_size = blob->active.num_clusters; 1467 } 1468 } 1469 1470 blob_load_backing_dev(ctx); 1471 } 1472 1473 static void 1474 blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1475 { 1476 struct spdk_blob_load_ctx *ctx = cb_arg; 1477 struct spdk_blob *blob = ctx->blob; 1478 struct spdk_blob_md_page *page; 1479 int rc; 1480 uint32_t crc; 1481 uint32_t current_page; 1482 1483 if (ctx->num_pages == 1) { 1484 current_page = bs_blobid_to_page(blob->id); 1485 } else { 1486 assert(ctx->num_pages != 0); 1487 page = &ctx->pages[ctx->num_pages - 2]; 1488 current_page = page->next; 1489 } 1490 1491 if (bserrno) { 1492 SPDK_ERRLOG("Metadata page %d read failed for blobid %" PRIu64 ": %d\n", 1493 current_page, blob->id, bserrno); 1494 blob_load_final(ctx, bserrno); 1495 return; 1496 } 1497 1498 page = &ctx->pages[ctx->num_pages - 1]; 1499 crc = blob_md_page_calc_crc(page); 1500 if (crc != page->crc) { 1501 SPDK_ERRLOG("Metadata page %d crc mismatch for blobid %" PRIu64 "\n", 1502 current_page, blob->id); 1503 blob_load_final(ctx, -EINVAL); 1504 return; 1505 } 1506 1507 if (page->next != SPDK_INVALID_MD_PAGE) { 1508 struct spdk_blob_md_page *tmp_pages; 1509 uint32_t next_page = page->next; 1510 uint64_t next_lba = bs_md_page_to_lba(blob->bs, next_page); 1511 1512 /* Read the next page */ 1513 tmp_pages = spdk_realloc(ctx->pages, (sizeof(*page) * (ctx->num_pages + 1)), 0); 1514 if (tmp_pages == NULL) { 1515 blob_load_final(ctx, -ENOMEM); 1516 return; 1517 } 1518 ctx->num_pages++; 1519 ctx->pages = tmp_pages; 1520 1521 bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1], 1522 next_lba, 1523 bs_byte_to_lba(blob->bs, sizeof(*page)), 1524 blob_load_cpl, ctx); 1525 return; 1526 } 1527 1528 /* Parse the pages */ 1529 rc = blob_parse(ctx->pages, ctx->num_pages, blob); 1530 if (rc) { 1531 blob_load_final(ctx, rc); 1532 return; 1533 } 1534 1535 if (blob->extent_table_found == true) { 1536 /* If EXTENT_TABLE was found, that means support for it should be enabled. */ 1537 assert(blob->extent_rle_found == false); 1538 blob->use_extent_table = true; 1539 } else { 1540 /* If EXTENT_RLE or no extent_* descriptor was found disable support 1541 * for extent table. No extent_* descriptors means that blob has length of 0 1542 * and no extent_rle descriptors were persisted for it. 1543 * EXTENT_TABLE if used, is always present in metadata regardless of length. */ 1544 blob->use_extent_table = false; 1545 } 1546 1547 /* Check the clear_method stored in metadata vs what may have been passed 1548 * via spdk_bs_open_blob_ext() and update accordingly. 1549 */ 1550 blob_update_clear_method(blob); 1551 1552 spdk_free(ctx->pages); 1553 ctx->pages = NULL; 1554 1555 if (blob->extent_table_found) { 1556 blob_load_cpl_extents_cpl(seq, ctx, 0); 1557 } else { 1558 blob_load_backing_dev(ctx); 1559 } 1560 } 1561 1562 /* Load a blob from disk given a blobid */ 1563 static void 1564 blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 1565 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 1566 { 1567 struct spdk_blob_load_ctx *ctx; 1568 struct spdk_blob_store *bs; 1569 uint32_t page_num; 1570 uint64_t lba; 1571 1572 blob_verify_md_op(blob); 1573 1574 bs = blob->bs; 1575 1576 ctx = calloc(1, sizeof(*ctx)); 1577 if (!ctx) { 1578 cb_fn(seq, cb_arg, -ENOMEM); 1579 return; 1580 } 1581 1582 ctx->blob = blob; 1583 ctx->pages = spdk_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 0); 1584 if (!ctx->pages) { 1585 free(ctx); 1586 cb_fn(seq, cb_arg, -ENOMEM); 1587 return; 1588 } 1589 ctx->num_pages = 1; 1590 ctx->cb_fn = cb_fn; 1591 ctx->cb_arg = cb_arg; 1592 ctx->seq = seq; 1593 1594 page_num = bs_blobid_to_page(blob->id); 1595 lba = bs_md_page_to_lba(blob->bs, page_num); 1596 1597 blob->state = SPDK_BLOB_STATE_LOADING; 1598 1599 bs_sequence_read_dev(seq, &ctx->pages[0], lba, 1600 bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE), 1601 blob_load_cpl, ctx); 1602 } 1603 1604 struct spdk_blob_persist_ctx { 1605 struct spdk_blob *blob; 1606 1607 struct spdk_bs_super_block *super; 1608 1609 struct spdk_blob_md_page *pages; 1610 uint32_t next_extent_page; 1611 struct spdk_blob_md_page *extent_page; 1612 1613 spdk_bs_sequence_t *seq; 1614 spdk_bs_sequence_cpl cb_fn; 1615 void *cb_arg; 1616 TAILQ_ENTRY(spdk_blob_persist_ctx) link; 1617 }; 1618 1619 static void 1620 bs_batch_clear_dev(struct spdk_blob_persist_ctx *ctx, spdk_bs_batch_t *batch, uint64_t lba, 1621 uint64_t lba_count) 1622 { 1623 switch (ctx->blob->clear_method) { 1624 case BLOB_CLEAR_WITH_DEFAULT: 1625 case BLOB_CLEAR_WITH_UNMAP: 1626 bs_batch_unmap_dev(batch, lba, lba_count); 1627 break; 1628 case BLOB_CLEAR_WITH_WRITE_ZEROES: 1629 bs_batch_write_zeroes_dev(batch, lba, lba_count); 1630 break; 1631 case BLOB_CLEAR_WITH_NONE: 1632 default: 1633 break; 1634 } 1635 } 1636 1637 static void blob_persist_check_dirty(struct spdk_blob_persist_ctx *ctx); 1638 1639 static void 1640 blob_persist_complete_cb(void *arg) 1641 { 1642 struct spdk_blob_persist_ctx *ctx = arg; 1643 1644 /* Call user callback */ 1645 ctx->cb_fn(ctx->seq, ctx->cb_arg, 0); 1646 1647 /* Free the memory */ 1648 spdk_free(ctx->pages); 1649 free(ctx); 1650 } 1651 1652 static void 1653 blob_persist_complete(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx, int bserrno) 1654 { 1655 struct spdk_blob_persist_ctx *next_persist, *tmp; 1656 struct spdk_blob *blob = ctx->blob; 1657 1658 if (bserrno == 0) { 1659 blob_mark_clean(blob); 1660 } 1661 1662 assert(ctx == TAILQ_FIRST(&blob->persists_to_complete)); 1663 1664 /* Complete all persists that were pending when the current persist started */ 1665 TAILQ_FOREACH_SAFE(next_persist, &blob->persists_to_complete, link, tmp) { 1666 TAILQ_REMOVE(&blob->persists_to_complete, next_persist, link); 1667 spdk_thread_send_msg(spdk_get_thread(), blob_persist_complete_cb, next_persist); 1668 } 1669 1670 if (TAILQ_EMPTY(&blob->pending_persists)) { 1671 return; 1672 } 1673 1674 /* Queue up all pending persists for completion and start blob persist with first one */ 1675 TAILQ_SWAP(&blob->persists_to_complete, &blob->pending_persists, spdk_blob_persist_ctx, link); 1676 next_persist = TAILQ_FIRST(&blob->persists_to_complete); 1677 1678 blob->state = SPDK_BLOB_STATE_DIRTY; 1679 blob_persist_check_dirty(next_persist); 1680 } 1681 1682 static void 1683 blob_persist_clear_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1684 { 1685 struct spdk_blob_persist_ctx *ctx = cb_arg; 1686 struct spdk_blob *blob = ctx->blob; 1687 struct spdk_blob_store *bs = blob->bs; 1688 size_t i; 1689 1690 if (bserrno != 0) { 1691 blob_persist_complete(seq, ctx, bserrno); 1692 return; 1693 } 1694 1695 /* Release all extent_pages that were truncated */ 1696 for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) { 1697 /* Nothing to release if it was not allocated */ 1698 if (blob->active.extent_pages[i] != 0) { 1699 bs_release_md_page(bs, blob->active.extent_pages[i]); 1700 } 1701 } 1702 1703 if (blob->active.num_extent_pages == 0) { 1704 free(blob->active.extent_pages); 1705 blob->active.extent_pages = NULL; 1706 blob->active.extent_pages_array_size = 0; 1707 } else if (blob->active.num_extent_pages != blob->active.extent_pages_array_size) { 1708 #ifndef __clang_analyzer__ 1709 void *tmp; 1710 1711 /* scan-build really can't figure reallocs, workaround it */ 1712 tmp = realloc(blob->active.extent_pages, sizeof(uint32_t) * blob->active.num_extent_pages); 1713 assert(tmp != NULL); 1714 blob->active.extent_pages = tmp; 1715 #endif 1716 blob->active.extent_pages_array_size = blob->active.num_extent_pages; 1717 } 1718 1719 blob_persist_complete(seq, ctx, bserrno); 1720 } 1721 1722 static void 1723 blob_persist_clear_extents(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx) 1724 { 1725 struct spdk_blob *blob = ctx->blob; 1726 struct spdk_blob_store *bs = blob->bs; 1727 size_t i; 1728 uint64_t lba; 1729 uint64_t lba_count; 1730 spdk_bs_batch_t *batch; 1731 1732 batch = bs_sequence_to_batch(seq, blob_persist_clear_extents_cpl, ctx); 1733 lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE); 1734 1735 /* Clear all extent_pages that were truncated */ 1736 for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) { 1737 /* Nothing to clear if it was not allocated */ 1738 if (blob->active.extent_pages[i] != 0) { 1739 lba = bs_md_page_to_lba(bs, blob->active.extent_pages[i]); 1740 bs_batch_write_zeroes_dev(batch, lba, lba_count); 1741 } 1742 } 1743 1744 bs_batch_close(batch); 1745 } 1746 1747 static void 1748 blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1749 { 1750 struct spdk_blob_persist_ctx *ctx = cb_arg; 1751 struct spdk_blob *blob = ctx->blob; 1752 struct spdk_blob_store *bs = blob->bs; 1753 size_t i; 1754 1755 if (bserrno != 0) { 1756 blob_persist_complete(seq, ctx, bserrno); 1757 return; 1758 } 1759 1760 pthread_mutex_lock(&bs->used_clusters_mutex); 1761 /* Release all clusters that were truncated */ 1762 for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) { 1763 uint32_t cluster_num = bs_lba_to_cluster(bs, blob->active.clusters[i]); 1764 1765 /* Nothing to release if it was not allocated */ 1766 if (blob->active.clusters[i] != 0) { 1767 bs_release_cluster(bs, cluster_num); 1768 } 1769 } 1770 pthread_mutex_unlock(&bs->used_clusters_mutex); 1771 1772 if (blob->active.num_clusters == 0) { 1773 free(blob->active.clusters); 1774 blob->active.clusters = NULL; 1775 blob->active.cluster_array_size = 0; 1776 } else if (blob->active.num_clusters != blob->active.cluster_array_size) { 1777 #ifndef __clang_analyzer__ 1778 void *tmp; 1779 1780 /* scan-build really can't figure reallocs, workaround it */ 1781 tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * blob->active.num_clusters); 1782 assert(tmp != NULL); 1783 blob->active.clusters = tmp; 1784 1785 #endif 1786 blob->active.cluster_array_size = blob->active.num_clusters; 1787 } 1788 1789 /* Move on to clearing extent pages */ 1790 blob_persist_clear_extents(seq, ctx); 1791 } 1792 1793 static void 1794 blob_persist_clear_clusters(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx) 1795 { 1796 struct spdk_blob *blob = ctx->blob; 1797 struct spdk_blob_store *bs = blob->bs; 1798 spdk_bs_batch_t *batch; 1799 size_t i; 1800 uint64_t lba; 1801 uint64_t lba_count; 1802 1803 /* Clusters don't move around in blobs. The list shrinks or grows 1804 * at the end, but no changes ever occur in the middle of the list. 1805 */ 1806 1807 batch = bs_sequence_to_batch(seq, blob_persist_clear_clusters_cpl, ctx); 1808 1809 /* Clear all clusters that were truncated */ 1810 lba = 0; 1811 lba_count = 0; 1812 for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) { 1813 uint64_t next_lba = blob->active.clusters[i]; 1814 uint64_t next_lba_count = bs_cluster_to_lba(bs, 1); 1815 1816 if (next_lba > 0 && (lba + lba_count) == next_lba) { 1817 /* This cluster is contiguous with the previous one. */ 1818 lba_count += next_lba_count; 1819 continue; 1820 } else if (next_lba == 0) { 1821 continue; 1822 } 1823 1824 /* This cluster is not contiguous with the previous one. */ 1825 1826 /* If a run of LBAs previously existing, clear them now */ 1827 if (lba_count > 0) { 1828 bs_batch_clear_dev(ctx, batch, lba, lba_count); 1829 } 1830 1831 /* Start building the next batch */ 1832 lba = next_lba; 1833 if (next_lba > 0) { 1834 lba_count = next_lba_count; 1835 } else { 1836 lba_count = 0; 1837 } 1838 } 1839 1840 /* If we ended with a contiguous set of LBAs, clear them now */ 1841 if (lba_count > 0) { 1842 bs_batch_clear_dev(ctx, batch, lba, lba_count); 1843 } 1844 1845 bs_batch_close(batch); 1846 } 1847 1848 static void 1849 blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1850 { 1851 struct spdk_blob_persist_ctx *ctx = cb_arg; 1852 struct spdk_blob *blob = ctx->blob; 1853 struct spdk_blob_store *bs = blob->bs; 1854 size_t i; 1855 1856 if (bserrno != 0) { 1857 blob_persist_complete(seq, ctx, bserrno); 1858 return; 1859 } 1860 1861 /* This loop starts at 1 because the first page is special and handled 1862 * below. The pages (except the first) are never written in place, 1863 * so any pages in the clean list must be zeroed. 1864 */ 1865 for (i = 1; i < blob->clean.num_pages; i++) { 1866 bs_release_md_page(bs, blob->clean.pages[i]); 1867 } 1868 1869 if (blob->active.num_pages == 0) { 1870 uint32_t page_num; 1871 1872 page_num = bs_blobid_to_page(blob->id); 1873 bs_release_md_page(bs, page_num); 1874 } 1875 1876 /* Move on to clearing clusters */ 1877 blob_persist_clear_clusters(seq, ctx); 1878 } 1879 1880 static void 1881 blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1882 { 1883 struct spdk_blob_persist_ctx *ctx = cb_arg; 1884 struct spdk_blob *blob = ctx->blob; 1885 struct spdk_blob_store *bs = blob->bs; 1886 uint64_t lba; 1887 uint64_t lba_count; 1888 spdk_bs_batch_t *batch; 1889 size_t i; 1890 1891 if (bserrno != 0) { 1892 blob_persist_complete(seq, ctx, bserrno); 1893 return; 1894 } 1895 1896 batch = bs_sequence_to_batch(seq, blob_persist_zero_pages_cpl, ctx); 1897 1898 lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE); 1899 1900 /* This loop starts at 1 because the first page is special and handled 1901 * below. The pages (except the first) are never written in place, 1902 * so any pages in the clean list must be zeroed. 1903 */ 1904 for (i = 1; i < blob->clean.num_pages; i++) { 1905 lba = bs_md_page_to_lba(bs, blob->clean.pages[i]); 1906 1907 bs_batch_write_zeroes_dev(batch, lba, lba_count); 1908 } 1909 1910 /* The first page will only be zeroed if this is a delete. */ 1911 if (blob->active.num_pages == 0) { 1912 uint32_t page_num; 1913 1914 /* The first page in the metadata goes where the blobid indicates */ 1915 page_num = bs_blobid_to_page(blob->id); 1916 lba = bs_md_page_to_lba(bs, page_num); 1917 1918 bs_batch_write_zeroes_dev(batch, lba, lba_count); 1919 } 1920 1921 bs_batch_close(batch); 1922 } 1923 1924 static void 1925 blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1926 { 1927 struct spdk_blob_persist_ctx *ctx = cb_arg; 1928 struct spdk_blob *blob = ctx->blob; 1929 struct spdk_blob_store *bs = blob->bs; 1930 uint64_t lba; 1931 uint32_t lba_count; 1932 struct spdk_blob_md_page *page; 1933 1934 if (bserrno != 0) { 1935 blob_persist_complete(seq, ctx, bserrno); 1936 return; 1937 } 1938 1939 if (blob->active.num_pages == 0) { 1940 /* Move on to the next step */ 1941 blob_persist_zero_pages(seq, ctx, 0); 1942 return; 1943 } 1944 1945 lba_count = bs_byte_to_lba(bs, sizeof(*page)); 1946 1947 page = &ctx->pages[0]; 1948 /* The first page in the metadata goes where the blobid indicates */ 1949 lba = bs_md_page_to_lba(bs, bs_blobid_to_page(blob->id)); 1950 1951 bs_sequence_write_dev(seq, page, lba, lba_count, 1952 blob_persist_zero_pages, ctx); 1953 } 1954 1955 static void 1956 blob_persist_write_page_chain(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx) 1957 { 1958 struct spdk_blob *blob = ctx->blob; 1959 struct spdk_blob_store *bs = blob->bs; 1960 uint64_t lba; 1961 uint32_t lba_count; 1962 struct spdk_blob_md_page *page; 1963 spdk_bs_batch_t *batch; 1964 size_t i; 1965 1966 /* Clusters don't move around in blobs. The list shrinks or grows 1967 * at the end, but no changes ever occur in the middle of the list. 1968 */ 1969 1970 lba_count = bs_byte_to_lba(bs, sizeof(*page)); 1971 1972 batch = bs_sequence_to_batch(seq, blob_persist_write_page_root, ctx); 1973 1974 /* This starts at 1. The root page is not written until 1975 * all of the others are finished 1976 */ 1977 for (i = 1; i < blob->active.num_pages; i++) { 1978 page = &ctx->pages[i]; 1979 assert(page->sequence_num == i); 1980 1981 lba = bs_md_page_to_lba(bs, blob->active.pages[i]); 1982 1983 bs_batch_write_dev(batch, page, lba, lba_count); 1984 } 1985 1986 bs_batch_close(batch); 1987 } 1988 1989 static int 1990 blob_resize(struct spdk_blob *blob, uint64_t sz) 1991 { 1992 uint64_t i; 1993 uint64_t *tmp; 1994 uint64_t cluster; 1995 uint32_t lfmd; /* lowest free md page */ 1996 uint64_t num_clusters; 1997 uint32_t *ep_tmp; 1998 uint64_t new_num_ep = 0, current_num_ep = 0; 1999 struct spdk_blob_store *bs; 2000 2001 bs = blob->bs; 2002 2003 blob_verify_md_op(blob); 2004 2005 if (blob->active.num_clusters == sz) { 2006 return 0; 2007 } 2008 2009 if (blob->active.num_clusters < blob->active.cluster_array_size) { 2010 /* If this blob was resized to be larger, then smaller, then 2011 * larger without syncing, then the cluster array already 2012 * contains spare assigned clusters we can use. 2013 */ 2014 num_clusters = spdk_min(blob->active.cluster_array_size, 2015 sz); 2016 } else { 2017 num_clusters = blob->active.num_clusters; 2018 } 2019 2020 if (blob->use_extent_table) { 2021 /* Round up since every cluster beyond current Extent Table size, 2022 * requires new extent page. */ 2023 new_num_ep = spdk_divide_round_up(sz, SPDK_EXTENTS_PER_EP); 2024 current_num_ep = spdk_divide_round_up(num_clusters, SPDK_EXTENTS_PER_EP); 2025 } 2026 2027 /* Check first that we have enough clusters and md pages before we start claiming them. */ 2028 if (sz > num_clusters && spdk_blob_is_thin_provisioned(blob) == false) { 2029 if ((sz - num_clusters) > bs->num_free_clusters) { 2030 return -ENOSPC; 2031 } 2032 lfmd = 0; 2033 for (i = current_num_ep; i < new_num_ep ; i++) { 2034 lfmd = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, lfmd); 2035 if (lfmd == UINT32_MAX) { 2036 /* No more free md pages. Cannot satisfy the request */ 2037 return -ENOSPC; 2038 } 2039 } 2040 } 2041 2042 if (sz > num_clusters) { 2043 /* Expand the cluster array if necessary. 2044 * We only shrink the array when persisting. 2045 */ 2046 tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * sz); 2047 if (sz > 0 && tmp == NULL) { 2048 return -ENOMEM; 2049 } 2050 memset(tmp + blob->active.cluster_array_size, 0, 2051 sizeof(*blob->active.clusters) * (sz - blob->active.cluster_array_size)); 2052 blob->active.clusters = tmp; 2053 blob->active.cluster_array_size = sz; 2054 2055 /* Expand the extents table, only if enough clusters were added */ 2056 if (new_num_ep > current_num_ep && blob->use_extent_table) { 2057 ep_tmp = realloc(blob->active.extent_pages, sizeof(*blob->active.extent_pages) * new_num_ep); 2058 if (new_num_ep > 0 && ep_tmp == NULL) { 2059 return -ENOMEM; 2060 } 2061 memset(ep_tmp + blob->active.extent_pages_array_size, 0, 2062 sizeof(*blob->active.extent_pages) * (new_num_ep - blob->active.extent_pages_array_size)); 2063 blob->active.extent_pages = ep_tmp; 2064 blob->active.extent_pages_array_size = new_num_ep; 2065 } 2066 } 2067 2068 blob->state = SPDK_BLOB_STATE_DIRTY; 2069 2070 if (spdk_blob_is_thin_provisioned(blob) == false) { 2071 cluster = 0; 2072 lfmd = 0; 2073 pthread_mutex_lock(&blob->bs->used_clusters_mutex); 2074 for (i = num_clusters; i < sz; i++) { 2075 bs_allocate_cluster(blob, i, &cluster, &lfmd, true); 2076 lfmd++; 2077 } 2078 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 2079 } 2080 2081 blob->active.num_clusters = sz; 2082 blob->active.num_extent_pages = new_num_ep; 2083 2084 return 0; 2085 } 2086 2087 static void 2088 blob_persist_generate_new_md(struct spdk_blob_persist_ctx *ctx) 2089 { 2090 spdk_bs_sequence_t *seq = ctx->seq; 2091 struct spdk_blob *blob = ctx->blob; 2092 struct spdk_blob_store *bs = blob->bs; 2093 uint64_t i; 2094 uint32_t page_num; 2095 void *tmp; 2096 int rc; 2097 2098 /* Generate the new metadata */ 2099 rc = blob_serialize(blob, &ctx->pages, &blob->active.num_pages); 2100 if (rc < 0) { 2101 blob_persist_complete(seq, ctx, rc); 2102 return; 2103 } 2104 2105 assert(blob->active.num_pages >= 1); 2106 2107 /* Resize the cache of page indices */ 2108 tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages)); 2109 if (!tmp) { 2110 blob_persist_complete(seq, ctx, -ENOMEM); 2111 return; 2112 } 2113 blob->active.pages = tmp; 2114 2115 /* Assign this metadata to pages. This requires two passes - 2116 * one to verify that there are enough pages and a second 2117 * to actually claim them. */ 2118 page_num = 0; 2119 /* Note that this loop starts at one. The first page location is fixed by the blobid. */ 2120 for (i = 1; i < blob->active.num_pages; i++) { 2121 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num); 2122 if (page_num == UINT32_MAX) { 2123 blob_persist_complete(seq, ctx, -ENOMEM); 2124 return; 2125 } 2126 page_num++; 2127 } 2128 2129 page_num = 0; 2130 blob->active.pages[0] = bs_blobid_to_page(blob->id); 2131 for (i = 1; i < blob->active.num_pages; i++) { 2132 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num); 2133 ctx->pages[i - 1].next = page_num; 2134 /* Now that previous metadata page is complete, calculate the crc for it. */ 2135 ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]); 2136 blob->active.pages[i] = page_num; 2137 bs_claim_md_page(bs, page_num); 2138 SPDK_DEBUGLOG(blob, "Claiming page %u for blob %" PRIu64 "\n", page_num, blob->id); 2139 page_num++; 2140 } 2141 ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]); 2142 /* Start writing the metadata from last page to first */ 2143 blob->state = SPDK_BLOB_STATE_CLEAN; 2144 blob_persist_write_page_chain(seq, ctx); 2145 } 2146 2147 static void 2148 blob_persist_write_extent_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2149 { 2150 struct spdk_blob_persist_ctx *ctx = cb_arg; 2151 struct spdk_blob *blob = ctx->blob; 2152 size_t i; 2153 uint32_t extent_page_id; 2154 uint32_t page_count = 0; 2155 int rc; 2156 2157 if (ctx->extent_page != NULL) { 2158 spdk_free(ctx->extent_page); 2159 ctx->extent_page = NULL; 2160 } 2161 2162 if (bserrno != 0) { 2163 blob_persist_complete(seq, ctx, bserrno); 2164 return; 2165 } 2166 2167 /* Only write out Extent Pages when blob was resized. */ 2168 for (i = ctx->next_extent_page; i < blob->active.extent_pages_array_size; i++) { 2169 extent_page_id = blob->active.extent_pages[i]; 2170 if (extent_page_id == 0) { 2171 /* No Extent Page to persist */ 2172 assert(spdk_blob_is_thin_provisioned(blob)); 2173 continue; 2174 } 2175 assert(spdk_bit_array_get(blob->bs->used_md_pages, extent_page_id)); 2176 ctx->next_extent_page = i + 1; 2177 rc = blob_serialize_add_page(ctx->blob, &ctx->extent_page, &page_count, &ctx->extent_page); 2178 if (rc < 0) { 2179 blob_persist_complete(seq, ctx, rc); 2180 return; 2181 } 2182 2183 blob->state = SPDK_BLOB_STATE_DIRTY; 2184 blob_serialize_extent_page(blob, i * SPDK_EXTENTS_PER_EP, ctx->extent_page); 2185 2186 ctx->extent_page->crc = blob_md_page_calc_crc(ctx->extent_page); 2187 2188 bs_sequence_write_dev(seq, ctx->extent_page, bs_md_page_to_lba(blob->bs, extent_page_id), 2189 bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE), 2190 blob_persist_write_extent_pages, ctx); 2191 return; 2192 } 2193 2194 blob_persist_generate_new_md(ctx); 2195 } 2196 2197 static void 2198 blob_persist_start(struct spdk_blob_persist_ctx *ctx) 2199 { 2200 spdk_bs_sequence_t *seq = ctx->seq; 2201 struct spdk_blob *blob = ctx->blob; 2202 2203 if (blob->active.num_pages == 0) { 2204 /* This is the signal that the blob should be deleted. 2205 * Immediately jump to the clean up routine. */ 2206 assert(blob->clean.num_pages > 0); 2207 blob->state = SPDK_BLOB_STATE_CLEAN; 2208 blob_persist_zero_pages(seq, ctx, 0); 2209 return; 2210 2211 } 2212 2213 if (blob->clean.num_clusters < blob->active.num_clusters) { 2214 /* Blob was resized up */ 2215 assert(blob->clean.num_extent_pages <= blob->active.num_extent_pages); 2216 ctx->next_extent_page = spdk_max(1, blob->clean.num_extent_pages) - 1; 2217 } else if (blob->active.num_clusters < blob->active.cluster_array_size) { 2218 /* Blob was resized down */ 2219 assert(blob->clean.num_extent_pages >= blob->active.num_extent_pages); 2220 ctx->next_extent_page = spdk_max(1, blob->active.num_extent_pages) - 1; 2221 } else { 2222 /* No change in size occurred */ 2223 blob_persist_generate_new_md(ctx); 2224 return; 2225 } 2226 2227 blob_persist_write_extent_pages(seq, ctx, 0); 2228 } 2229 2230 static void 2231 blob_persist_dirty_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2232 { 2233 struct spdk_blob_persist_ctx *ctx = cb_arg; 2234 2235 spdk_free(ctx->super); 2236 2237 if (bserrno != 0) { 2238 blob_persist_complete(seq, ctx, bserrno); 2239 return; 2240 } 2241 2242 ctx->blob->bs->clean = 0; 2243 2244 blob_persist_start(ctx); 2245 } 2246 2247 static void 2248 bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 2249 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg); 2250 2251 2252 static void 2253 blob_persist_dirty(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2254 { 2255 struct spdk_blob_persist_ctx *ctx = cb_arg; 2256 2257 if (bserrno != 0) { 2258 spdk_free(ctx->super); 2259 blob_persist_complete(seq, ctx, bserrno); 2260 return; 2261 } 2262 2263 ctx->super->clean = 0; 2264 if (ctx->super->size == 0) { 2265 ctx->super->size = ctx->blob->bs->dev->blockcnt * ctx->blob->bs->dev->blocklen; 2266 } 2267 2268 bs_write_super(seq, ctx->blob->bs, ctx->super, blob_persist_dirty_cpl, ctx); 2269 } 2270 2271 static void 2272 blob_persist_check_dirty(struct spdk_blob_persist_ctx *ctx) 2273 { 2274 if (ctx->blob->bs->clean) { 2275 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 2276 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 2277 if (!ctx->super) { 2278 blob_persist_complete(ctx->seq, ctx, -ENOMEM); 2279 return; 2280 } 2281 2282 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->blob->bs, 0), 2283 bs_byte_to_lba(ctx->blob->bs, sizeof(*ctx->super)), 2284 blob_persist_dirty, ctx); 2285 } else { 2286 blob_persist_start(ctx); 2287 } 2288 } 2289 2290 /* Write a blob to disk */ 2291 static void 2292 blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 2293 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 2294 { 2295 struct spdk_blob_persist_ctx *ctx; 2296 2297 blob_verify_md_op(blob); 2298 2299 if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->persists_to_complete)) { 2300 cb_fn(seq, cb_arg, 0); 2301 return; 2302 } 2303 2304 ctx = calloc(1, sizeof(*ctx)); 2305 if (!ctx) { 2306 cb_fn(seq, cb_arg, -ENOMEM); 2307 return; 2308 } 2309 ctx->blob = blob; 2310 ctx->seq = seq; 2311 ctx->cb_fn = cb_fn; 2312 ctx->cb_arg = cb_arg; 2313 2314 /* Multiple blob persists can affect one another, via blob->state or 2315 * blob mutable data changes. To prevent it, queue up the persists. */ 2316 if (!TAILQ_EMPTY(&blob->persists_to_complete)) { 2317 TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link); 2318 return; 2319 } 2320 TAILQ_INSERT_HEAD(&blob->persists_to_complete, ctx, link); 2321 2322 blob_persist_check_dirty(ctx); 2323 } 2324 2325 struct spdk_blob_copy_cluster_ctx { 2326 struct spdk_blob *blob; 2327 uint8_t *buf; 2328 uint64_t page; 2329 uint64_t new_cluster; 2330 uint32_t new_extent_page; 2331 spdk_bs_sequence_t *seq; 2332 }; 2333 2334 static void 2335 blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno) 2336 { 2337 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2338 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq; 2339 TAILQ_HEAD(, spdk_bs_request_set) requests; 2340 spdk_bs_user_op_t *op; 2341 2342 TAILQ_INIT(&requests); 2343 TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link); 2344 2345 while (!TAILQ_EMPTY(&requests)) { 2346 op = TAILQ_FIRST(&requests); 2347 TAILQ_REMOVE(&requests, op, link); 2348 if (bserrno == 0) { 2349 bs_user_op_execute(op); 2350 } else { 2351 bs_user_op_abort(op); 2352 } 2353 } 2354 2355 spdk_free(ctx->buf); 2356 free(ctx); 2357 } 2358 2359 static void 2360 blob_insert_cluster_cpl(void *cb_arg, int bserrno) 2361 { 2362 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2363 2364 if (bserrno) { 2365 if (bserrno == -EEXIST) { 2366 /* The metadata insert failed because another thread 2367 * allocated the cluster first. Free our cluster 2368 * but continue without error. */ 2369 bserrno = 0; 2370 } 2371 pthread_mutex_lock(&ctx->blob->bs->used_clusters_mutex); 2372 bs_release_cluster(ctx->blob->bs, ctx->new_cluster); 2373 pthread_mutex_unlock(&ctx->blob->bs->used_clusters_mutex); 2374 if (ctx->new_extent_page != 0) { 2375 bs_release_md_page(ctx->blob->bs, ctx->new_extent_page); 2376 } 2377 } 2378 2379 bs_sequence_finish(ctx->seq, bserrno); 2380 } 2381 2382 static void 2383 blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2384 { 2385 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2386 uint32_t cluster_number; 2387 2388 if (bserrno) { 2389 /* The write failed, so jump to the final completion handler */ 2390 bs_sequence_finish(seq, bserrno); 2391 return; 2392 } 2393 2394 cluster_number = bs_page_to_cluster(ctx->blob->bs, ctx->page); 2395 2396 blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster, 2397 ctx->new_extent_page, blob_insert_cluster_cpl, ctx); 2398 } 2399 2400 static void 2401 blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2402 { 2403 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2404 2405 if (bserrno != 0) { 2406 /* The read failed, so jump to the final completion handler */ 2407 bs_sequence_finish(seq, bserrno); 2408 return; 2409 } 2410 2411 /* Write whole cluster */ 2412 bs_sequence_write_dev(seq, ctx->buf, 2413 bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster), 2414 bs_cluster_to_lba(ctx->blob->bs, 1), 2415 blob_write_copy_cpl, ctx); 2416 } 2417 2418 static void 2419 bs_allocate_and_copy_cluster(struct spdk_blob *blob, 2420 struct spdk_io_channel *_ch, 2421 uint64_t io_unit, spdk_bs_user_op_t *op) 2422 { 2423 struct spdk_bs_cpl cpl; 2424 struct spdk_bs_channel *ch; 2425 struct spdk_blob_copy_cluster_ctx *ctx; 2426 uint32_t cluster_start_page; 2427 uint32_t cluster_number; 2428 int rc; 2429 2430 ch = spdk_io_channel_get_ctx(_ch); 2431 2432 if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) { 2433 /* There are already operations pending. Queue this user op 2434 * and return because it will be re-executed when the outstanding 2435 * cluster allocation completes. */ 2436 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link); 2437 return; 2438 } 2439 2440 /* Round the io_unit offset down to the first page in the cluster */ 2441 cluster_start_page = bs_io_unit_to_cluster_start(blob, io_unit); 2442 2443 /* Calculate which index in the metadata cluster array the corresponding 2444 * cluster is supposed to be at. */ 2445 cluster_number = bs_io_unit_to_cluster_number(blob, io_unit); 2446 2447 ctx = calloc(1, sizeof(*ctx)); 2448 if (!ctx) { 2449 bs_user_op_abort(op); 2450 return; 2451 } 2452 2453 assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0); 2454 2455 ctx->blob = blob; 2456 ctx->page = cluster_start_page; 2457 2458 if (blob->parent_id != SPDK_BLOBID_INVALID) { 2459 ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen, 2460 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 2461 if (!ctx->buf) { 2462 SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n", 2463 blob->bs->cluster_sz); 2464 free(ctx); 2465 bs_user_op_abort(op); 2466 return; 2467 } 2468 } 2469 2470 pthread_mutex_lock(&blob->bs->used_clusters_mutex); 2471 rc = bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page, 2472 false); 2473 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 2474 if (rc != 0) { 2475 spdk_free(ctx->buf); 2476 free(ctx); 2477 bs_user_op_abort(op); 2478 return; 2479 } 2480 2481 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2482 cpl.u.blob_basic.cb_fn = blob_allocate_and_copy_cluster_cpl; 2483 cpl.u.blob_basic.cb_arg = ctx; 2484 2485 ctx->seq = bs_sequence_start(_ch, &cpl); 2486 if (!ctx->seq) { 2487 pthread_mutex_lock(&blob->bs->used_clusters_mutex); 2488 bs_release_cluster(blob->bs, ctx->new_cluster); 2489 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 2490 spdk_free(ctx->buf); 2491 free(ctx); 2492 bs_user_op_abort(op); 2493 return; 2494 } 2495 2496 /* Queue the user op to block other incoming operations */ 2497 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link); 2498 2499 if (blob->parent_id != SPDK_BLOBID_INVALID) { 2500 /* Read cluster from backing device */ 2501 bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf, 2502 bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page), 2503 bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz), 2504 blob_write_copy, ctx); 2505 } else { 2506 blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster, 2507 ctx->new_extent_page, blob_insert_cluster_cpl, ctx); 2508 } 2509 } 2510 2511 static inline bool 2512 blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length, 2513 uint64_t *lba, uint64_t *lba_count) 2514 { 2515 *lba_count = length; 2516 2517 if (!bs_io_unit_is_allocated(blob, io_unit)) { 2518 assert(blob->back_bs_dev != NULL); 2519 *lba = bs_io_unit_to_back_dev_lba(blob, io_unit); 2520 *lba_count = bs_io_unit_to_back_dev_lba(blob, *lba_count); 2521 return false; 2522 } else { 2523 *lba = bs_blob_io_unit_to_lba(blob, io_unit); 2524 return true; 2525 } 2526 } 2527 2528 struct op_split_ctx { 2529 struct spdk_blob *blob; 2530 struct spdk_io_channel *channel; 2531 uint64_t io_unit_offset; 2532 uint64_t io_units_remaining; 2533 void *curr_payload; 2534 enum spdk_blob_op_type op_type; 2535 spdk_bs_sequence_t *seq; 2536 }; 2537 2538 static void 2539 blob_request_submit_op_split_next(void *cb_arg, int bserrno) 2540 { 2541 struct op_split_ctx *ctx = cb_arg; 2542 struct spdk_blob *blob = ctx->blob; 2543 struct spdk_io_channel *ch = ctx->channel; 2544 enum spdk_blob_op_type op_type = ctx->op_type; 2545 uint8_t *buf = ctx->curr_payload; 2546 uint64_t offset = ctx->io_unit_offset; 2547 uint64_t length = ctx->io_units_remaining; 2548 uint64_t op_length; 2549 2550 if (bserrno != 0 || ctx->io_units_remaining == 0) { 2551 bs_sequence_finish(ctx->seq, bserrno); 2552 free(ctx); 2553 return; 2554 } 2555 2556 op_length = spdk_min(length, bs_num_io_units_to_cluster_boundary(blob, 2557 offset)); 2558 2559 /* Update length and payload for next operation */ 2560 ctx->io_units_remaining -= op_length; 2561 ctx->io_unit_offset += op_length; 2562 if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) { 2563 ctx->curr_payload += op_length * blob->bs->io_unit_size; 2564 } 2565 2566 switch (op_type) { 2567 case SPDK_BLOB_READ: 2568 spdk_blob_io_read(blob, ch, buf, offset, op_length, 2569 blob_request_submit_op_split_next, ctx); 2570 break; 2571 case SPDK_BLOB_WRITE: 2572 spdk_blob_io_write(blob, ch, buf, offset, op_length, 2573 blob_request_submit_op_split_next, ctx); 2574 break; 2575 case SPDK_BLOB_UNMAP: 2576 spdk_blob_io_unmap(blob, ch, offset, op_length, 2577 blob_request_submit_op_split_next, ctx); 2578 break; 2579 case SPDK_BLOB_WRITE_ZEROES: 2580 spdk_blob_io_write_zeroes(blob, ch, offset, op_length, 2581 blob_request_submit_op_split_next, ctx); 2582 break; 2583 case SPDK_BLOB_READV: 2584 case SPDK_BLOB_WRITEV: 2585 SPDK_ERRLOG("readv/write not valid\n"); 2586 bs_sequence_finish(ctx->seq, -EINVAL); 2587 free(ctx); 2588 break; 2589 } 2590 } 2591 2592 static void 2593 blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob, 2594 void *payload, uint64_t offset, uint64_t length, 2595 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 2596 { 2597 struct op_split_ctx *ctx; 2598 spdk_bs_sequence_t *seq; 2599 struct spdk_bs_cpl cpl; 2600 2601 assert(blob != NULL); 2602 2603 ctx = calloc(1, sizeof(struct op_split_ctx)); 2604 if (ctx == NULL) { 2605 cb_fn(cb_arg, -ENOMEM); 2606 return; 2607 } 2608 2609 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2610 cpl.u.blob_basic.cb_fn = cb_fn; 2611 cpl.u.blob_basic.cb_arg = cb_arg; 2612 2613 seq = bs_sequence_start(ch, &cpl); 2614 if (!seq) { 2615 free(ctx); 2616 cb_fn(cb_arg, -ENOMEM); 2617 return; 2618 } 2619 2620 ctx->blob = blob; 2621 ctx->channel = ch; 2622 ctx->curr_payload = payload; 2623 ctx->io_unit_offset = offset; 2624 ctx->io_units_remaining = length; 2625 ctx->op_type = op_type; 2626 ctx->seq = seq; 2627 2628 blob_request_submit_op_split_next(ctx, 0); 2629 } 2630 2631 static void 2632 blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob, 2633 void *payload, uint64_t offset, uint64_t length, 2634 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 2635 { 2636 struct spdk_bs_cpl cpl; 2637 uint64_t lba; 2638 uint64_t lba_count; 2639 bool is_allocated; 2640 2641 assert(blob != NULL); 2642 2643 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2644 cpl.u.blob_basic.cb_fn = cb_fn; 2645 cpl.u.blob_basic.cb_arg = cb_arg; 2646 2647 if (blob->frozen_refcnt) { 2648 /* This blob I/O is frozen */ 2649 spdk_bs_user_op_t *op; 2650 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch); 2651 2652 op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length); 2653 if (!op) { 2654 cb_fn(cb_arg, -ENOMEM); 2655 return; 2656 } 2657 2658 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link); 2659 2660 return; 2661 } 2662 2663 is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count); 2664 2665 switch (op_type) { 2666 case SPDK_BLOB_READ: { 2667 spdk_bs_batch_t *batch; 2668 2669 batch = bs_batch_open(_ch, &cpl); 2670 if (!batch) { 2671 cb_fn(cb_arg, -ENOMEM); 2672 return; 2673 } 2674 2675 if (is_allocated) { 2676 /* Read from the blob */ 2677 bs_batch_read_dev(batch, payload, lba, lba_count); 2678 } else { 2679 /* Read from the backing block device */ 2680 bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count); 2681 } 2682 2683 bs_batch_close(batch); 2684 break; 2685 } 2686 case SPDK_BLOB_WRITE: 2687 case SPDK_BLOB_WRITE_ZEROES: { 2688 if (is_allocated) { 2689 /* Write to the blob */ 2690 spdk_bs_batch_t *batch; 2691 2692 if (lba_count == 0) { 2693 cb_fn(cb_arg, 0); 2694 return; 2695 } 2696 2697 batch = bs_batch_open(_ch, &cpl); 2698 if (!batch) { 2699 cb_fn(cb_arg, -ENOMEM); 2700 return; 2701 } 2702 2703 if (op_type == SPDK_BLOB_WRITE) { 2704 bs_batch_write_dev(batch, payload, lba, lba_count); 2705 } else { 2706 bs_batch_write_zeroes_dev(batch, lba, lba_count); 2707 } 2708 2709 bs_batch_close(batch); 2710 } else { 2711 /* Queue this operation and allocate the cluster */ 2712 spdk_bs_user_op_t *op; 2713 2714 op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length); 2715 if (!op) { 2716 cb_fn(cb_arg, -ENOMEM); 2717 return; 2718 } 2719 2720 bs_allocate_and_copy_cluster(blob, _ch, offset, op); 2721 } 2722 break; 2723 } 2724 case SPDK_BLOB_UNMAP: { 2725 spdk_bs_batch_t *batch; 2726 2727 batch = bs_batch_open(_ch, &cpl); 2728 if (!batch) { 2729 cb_fn(cb_arg, -ENOMEM); 2730 return; 2731 } 2732 2733 if (is_allocated) { 2734 bs_batch_unmap_dev(batch, lba, lba_count); 2735 } 2736 2737 bs_batch_close(batch); 2738 break; 2739 } 2740 case SPDK_BLOB_READV: 2741 case SPDK_BLOB_WRITEV: 2742 SPDK_ERRLOG("readv/write not valid\n"); 2743 cb_fn(cb_arg, -EINVAL); 2744 break; 2745 } 2746 } 2747 2748 static void 2749 blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel, 2750 void *payload, uint64_t offset, uint64_t length, 2751 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 2752 { 2753 assert(blob != NULL); 2754 2755 if (blob->data_ro && op_type != SPDK_BLOB_READ) { 2756 cb_fn(cb_arg, -EPERM); 2757 return; 2758 } 2759 2760 if (length == 0) { 2761 cb_fn(cb_arg, 0); 2762 return; 2763 } 2764 2765 if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) { 2766 cb_fn(cb_arg, -EINVAL); 2767 return; 2768 } 2769 if (length <= bs_num_io_units_to_cluster_boundary(blob, offset)) { 2770 blob_request_submit_op_single(_channel, blob, payload, offset, length, 2771 cb_fn, cb_arg, op_type); 2772 } else { 2773 blob_request_submit_op_split(_channel, blob, payload, offset, length, 2774 cb_fn, cb_arg, op_type); 2775 } 2776 } 2777 2778 struct rw_iov_ctx { 2779 struct spdk_blob *blob; 2780 struct spdk_io_channel *channel; 2781 spdk_blob_op_complete cb_fn; 2782 void *cb_arg; 2783 bool read; 2784 int iovcnt; 2785 struct iovec *orig_iov; 2786 uint64_t io_unit_offset; 2787 uint64_t io_units_remaining; 2788 uint64_t io_units_done; 2789 struct iovec iov[0]; 2790 }; 2791 2792 static void 2793 rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2794 { 2795 assert(cb_arg == NULL); 2796 bs_sequence_finish(seq, bserrno); 2797 } 2798 2799 static void 2800 rw_iov_split_next(void *cb_arg, int bserrno) 2801 { 2802 struct rw_iov_ctx *ctx = cb_arg; 2803 struct spdk_blob *blob = ctx->blob; 2804 struct iovec *iov, *orig_iov; 2805 int iovcnt; 2806 size_t orig_iovoff; 2807 uint64_t io_units_count, io_units_to_boundary, io_unit_offset; 2808 uint64_t byte_count; 2809 2810 if (bserrno != 0 || ctx->io_units_remaining == 0) { 2811 ctx->cb_fn(ctx->cb_arg, bserrno); 2812 free(ctx); 2813 return; 2814 } 2815 2816 io_unit_offset = ctx->io_unit_offset; 2817 io_units_to_boundary = bs_num_io_units_to_cluster_boundary(blob, io_unit_offset); 2818 io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary); 2819 /* 2820 * Get index and offset into the original iov array for our current position in the I/O sequence. 2821 * byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will 2822 * point to the current position in the I/O sequence. 2823 */ 2824 byte_count = ctx->io_units_done * blob->bs->io_unit_size; 2825 orig_iov = &ctx->orig_iov[0]; 2826 orig_iovoff = 0; 2827 while (byte_count > 0) { 2828 if (byte_count >= orig_iov->iov_len) { 2829 byte_count -= orig_iov->iov_len; 2830 orig_iov++; 2831 } else { 2832 orig_iovoff = byte_count; 2833 byte_count = 0; 2834 } 2835 } 2836 2837 /* 2838 * Build an iov array for the next I/O in the sequence. byte_count will keep track of how many 2839 * bytes of this next I/O remain to be accounted for in the new iov array. 2840 */ 2841 byte_count = io_units_count * blob->bs->io_unit_size; 2842 iov = &ctx->iov[0]; 2843 iovcnt = 0; 2844 while (byte_count > 0) { 2845 assert(iovcnt < ctx->iovcnt); 2846 iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff); 2847 iov->iov_base = orig_iov->iov_base + orig_iovoff; 2848 byte_count -= iov->iov_len; 2849 orig_iovoff = 0; 2850 orig_iov++; 2851 iov++; 2852 iovcnt++; 2853 } 2854 2855 ctx->io_unit_offset += io_units_count; 2856 ctx->io_units_remaining -= io_units_count; 2857 ctx->io_units_done += io_units_count; 2858 iov = &ctx->iov[0]; 2859 2860 if (ctx->read) { 2861 spdk_blob_io_readv(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset, 2862 io_units_count, rw_iov_split_next, ctx); 2863 } else { 2864 spdk_blob_io_writev(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset, 2865 io_units_count, rw_iov_split_next, ctx); 2866 } 2867 } 2868 2869 static void 2870 blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel, 2871 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 2872 spdk_blob_op_complete cb_fn, void *cb_arg, bool read) 2873 { 2874 struct spdk_bs_cpl cpl; 2875 2876 assert(blob != NULL); 2877 2878 if (!read && blob->data_ro) { 2879 cb_fn(cb_arg, -EPERM); 2880 return; 2881 } 2882 2883 if (length == 0) { 2884 cb_fn(cb_arg, 0); 2885 return; 2886 } 2887 2888 if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) { 2889 cb_fn(cb_arg, -EINVAL); 2890 return; 2891 } 2892 2893 /* 2894 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having 2895 * to split a request that spans a cluster boundary. For I/O that do not span a cluster boundary, 2896 * there will be no noticeable difference compared to using a batch. For I/O that do span a cluster 2897 * boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need 2898 * to allocate a separate iov array and split the I/O such that none of the resulting 2899 * smaller I/O cross a cluster boundary. These smaller I/O will be issued in sequence (not in parallel) 2900 * but since this case happens very infrequently, any performance impact will be negligible. 2901 * 2902 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs 2903 * for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them 2904 * in a batch. That would also require creating an intermediate spdk_bs_cpl that would get called 2905 * when the batch was completed, to allow for freeing the memory for the iov arrays. 2906 */ 2907 if (spdk_likely(length <= bs_num_io_units_to_cluster_boundary(blob, offset))) { 2908 uint64_t lba_count; 2909 uint64_t lba; 2910 bool is_allocated; 2911 2912 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2913 cpl.u.blob_basic.cb_fn = cb_fn; 2914 cpl.u.blob_basic.cb_arg = cb_arg; 2915 2916 if (blob->frozen_refcnt) { 2917 /* This blob I/O is frozen */ 2918 enum spdk_blob_op_type op_type; 2919 spdk_bs_user_op_t *op; 2920 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel); 2921 2922 op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV; 2923 op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length); 2924 if (!op) { 2925 cb_fn(cb_arg, -ENOMEM); 2926 return; 2927 } 2928 2929 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link); 2930 2931 return; 2932 } 2933 2934 is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count); 2935 2936 if (read) { 2937 spdk_bs_sequence_t *seq; 2938 2939 seq = bs_sequence_start(_channel, &cpl); 2940 if (!seq) { 2941 cb_fn(cb_arg, -ENOMEM); 2942 return; 2943 } 2944 2945 if (is_allocated) { 2946 bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL); 2947 } else { 2948 bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count, 2949 rw_iov_done, NULL); 2950 } 2951 } else { 2952 if (is_allocated) { 2953 spdk_bs_sequence_t *seq; 2954 2955 seq = bs_sequence_start(_channel, &cpl); 2956 if (!seq) { 2957 cb_fn(cb_arg, -ENOMEM); 2958 return; 2959 } 2960 2961 bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL); 2962 } else { 2963 /* Queue this operation and allocate the cluster */ 2964 spdk_bs_user_op_t *op; 2965 2966 op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset, 2967 length); 2968 if (!op) { 2969 cb_fn(cb_arg, -ENOMEM); 2970 return; 2971 } 2972 2973 bs_allocate_and_copy_cluster(blob, _channel, offset, op); 2974 } 2975 } 2976 } else { 2977 struct rw_iov_ctx *ctx; 2978 2979 ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec)); 2980 if (ctx == NULL) { 2981 cb_fn(cb_arg, -ENOMEM); 2982 return; 2983 } 2984 2985 ctx->blob = blob; 2986 ctx->channel = _channel; 2987 ctx->cb_fn = cb_fn; 2988 ctx->cb_arg = cb_arg; 2989 ctx->read = read; 2990 ctx->orig_iov = iov; 2991 ctx->iovcnt = iovcnt; 2992 ctx->io_unit_offset = offset; 2993 ctx->io_units_remaining = length; 2994 ctx->io_units_done = 0; 2995 2996 rw_iov_split_next(ctx, 0); 2997 } 2998 } 2999 3000 static struct spdk_blob * 3001 blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid) 3002 { 3003 struct spdk_blob find; 3004 3005 if (spdk_bit_array_get(bs->open_blobids, blobid) == 0) { 3006 return NULL; 3007 } 3008 3009 find.id = blobid; 3010 return RB_FIND(spdk_blob_tree, &bs->open_blobs, &find); 3011 } 3012 3013 static void 3014 blob_get_snapshot_and_clone_entries(struct spdk_blob *blob, 3015 struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry) 3016 { 3017 assert(blob != NULL); 3018 *snapshot_entry = NULL; 3019 *clone_entry = NULL; 3020 3021 if (blob->parent_id == SPDK_BLOBID_INVALID) { 3022 return; 3023 } 3024 3025 TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) { 3026 if ((*snapshot_entry)->id == blob->parent_id) { 3027 break; 3028 } 3029 } 3030 3031 if (*snapshot_entry != NULL) { 3032 TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) { 3033 if ((*clone_entry)->id == blob->id) { 3034 break; 3035 } 3036 } 3037 3038 assert(*clone_entry != NULL); 3039 } 3040 } 3041 3042 static int 3043 bs_channel_create(void *io_device, void *ctx_buf) 3044 { 3045 struct spdk_blob_store *bs = io_device; 3046 struct spdk_bs_channel *channel = ctx_buf; 3047 struct spdk_bs_dev *dev; 3048 uint32_t max_ops = bs->max_channel_ops; 3049 uint32_t i; 3050 3051 dev = bs->dev; 3052 3053 channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set)); 3054 if (!channel->req_mem) { 3055 return -1; 3056 } 3057 3058 TAILQ_INIT(&channel->reqs); 3059 3060 for (i = 0; i < max_ops; i++) { 3061 TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link); 3062 } 3063 3064 channel->bs = bs; 3065 channel->dev = dev; 3066 channel->dev_channel = dev->create_channel(dev); 3067 3068 if (!channel->dev_channel) { 3069 SPDK_ERRLOG("Failed to create device channel.\n"); 3070 free(channel->req_mem); 3071 return -1; 3072 } 3073 3074 TAILQ_INIT(&channel->need_cluster_alloc); 3075 TAILQ_INIT(&channel->queued_io); 3076 3077 return 0; 3078 } 3079 3080 static void 3081 bs_channel_destroy(void *io_device, void *ctx_buf) 3082 { 3083 struct spdk_bs_channel *channel = ctx_buf; 3084 spdk_bs_user_op_t *op; 3085 3086 while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) { 3087 op = TAILQ_FIRST(&channel->need_cluster_alloc); 3088 TAILQ_REMOVE(&channel->need_cluster_alloc, op, link); 3089 bs_user_op_abort(op); 3090 } 3091 3092 while (!TAILQ_EMPTY(&channel->queued_io)) { 3093 op = TAILQ_FIRST(&channel->queued_io); 3094 TAILQ_REMOVE(&channel->queued_io, op, link); 3095 bs_user_op_abort(op); 3096 } 3097 3098 free(channel->req_mem); 3099 channel->dev->destroy_channel(channel->dev, channel->dev_channel); 3100 } 3101 3102 static void 3103 bs_dev_destroy(void *io_device) 3104 { 3105 struct spdk_blob_store *bs = io_device; 3106 struct spdk_blob *blob, *blob_tmp; 3107 3108 bs->dev->destroy(bs->dev); 3109 3110 RB_FOREACH_SAFE(blob, spdk_blob_tree, &bs->open_blobs, blob_tmp) { 3111 RB_REMOVE(spdk_blob_tree, &bs->open_blobs, blob); 3112 spdk_bit_array_clear(bs->open_blobids, blob->id); 3113 blob_free(blob); 3114 } 3115 3116 pthread_mutex_destroy(&bs->used_clusters_mutex); 3117 3118 spdk_bit_array_free(&bs->open_blobids); 3119 spdk_bit_array_free(&bs->used_blobids); 3120 spdk_bit_array_free(&bs->used_md_pages); 3121 spdk_bit_pool_free(&bs->used_clusters); 3122 /* 3123 * If this function is called for any reason except a successful unload, 3124 * the unload_cpl type will be NONE and this will be a nop. 3125 */ 3126 bs_call_cpl(&bs->unload_cpl, bs->unload_err); 3127 3128 free(bs); 3129 } 3130 3131 static int 3132 bs_blob_list_add(struct spdk_blob *blob) 3133 { 3134 spdk_blob_id snapshot_id; 3135 struct spdk_blob_list *snapshot_entry = NULL; 3136 struct spdk_blob_list *clone_entry = NULL; 3137 3138 assert(blob != NULL); 3139 3140 snapshot_id = blob->parent_id; 3141 if (snapshot_id == SPDK_BLOBID_INVALID) { 3142 return 0; 3143 } 3144 3145 snapshot_entry = bs_get_snapshot_entry(blob->bs, snapshot_id); 3146 if (snapshot_entry == NULL) { 3147 /* Snapshot not found */ 3148 snapshot_entry = calloc(1, sizeof(struct spdk_blob_list)); 3149 if (snapshot_entry == NULL) { 3150 return -ENOMEM; 3151 } 3152 snapshot_entry->id = snapshot_id; 3153 TAILQ_INIT(&snapshot_entry->clones); 3154 TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link); 3155 } else { 3156 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 3157 if (clone_entry->id == blob->id) { 3158 break; 3159 } 3160 } 3161 } 3162 3163 if (clone_entry == NULL) { 3164 /* Clone not found */ 3165 clone_entry = calloc(1, sizeof(struct spdk_blob_list)); 3166 if (clone_entry == NULL) { 3167 return -ENOMEM; 3168 } 3169 clone_entry->id = blob->id; 3170 TAILQ_INIT(&clone_entry->clones); 3171 TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link); 3172 snapshot_entry->clone_count++; 3173 } 3174 3175 return 0; 3176 } 3177 3178 static void 3179 bs_blob_list_remove(struct spdk_blob *blob) 3180 { 3181 struct spdk_blob_list *snapshot_entry = NULL; 3182 struct spdk_blob_list *clone_entry = NULL; 3183 3184 blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry); 3185 3186 if (snapshot_entry == NULL) { 3187 return; 3188 } 3189 3190 blob->parent_id = SPDK_BLOBID_INVALID; 3191 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 3192 free(clone_entry); 3193 3194 snapshot_entry->clone_count--; 3195 } 3196 3197 static int 3198 bs_blob_list_free(struct spdk_blob_store *bs) 3199 { 3200 struct spdk_blob_list *snapshot_entry; 3201 struct spdk_blob_list *snapshot_entry_tmp; 3202 struct spdk_blob_list *clone_entry; 3203 struct spdk_blob_list *clone_entry_tmp; 3204 3205 TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) { 3206 TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) { 3207 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 3208 free(clone_entry); 3209 } 3210 TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link); 3211 free(snapshot_entry); 3212 } 3213 3214 return 0; 3215 } 3216 3217 static void 3218 bs_free(struct spdk_blob_store *bs) 3219 { 3220 bs_blob_list_free(bs); 3221 3222 bs_unregister_md_thread(bs); 3223 spdk_io_device_unregister(bs, bs_dev_destroy); 3224 } 3225 3226 void 3227 spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size) 3228 { 3229 3230 if (!opts) { 3231 SPDK_ERRLOG("opts should not be NULL\n"); 3232 return; 3233 } 3234 3235 if (!opts_size) { 3236 SPDK_ERRLOG("opts_size should not be zero value\n"); 3237 return; 3238 } 3239 3240 memset(opts, 0, opts_size); 3241 opts->opts_size = opts_size; 3242 3243 #define FIELD_OK(field) \ 3244 offsetof(struct spdk_bs_opts, field) + sizeof(opts->field) <= opts_size 3245 3246 #define SET_FIELD(field, value) \ 3247 if (FIELD_OK(field)) { \ 3248 opts->field = value; \ 3249 } \ 3250 3251 SET_FIELD(cluster_sz, SPDK_BLOB_OPTS_CLUSTER_SZ); 3252 SET_FIELD(num_md_pages, SPDK_BLOB_OPTS_NUM_MD_PAGES); 3253 SET_FIELD(max_md_ops, SPDK_BLOB_OPTS_NUM_MD_PAGES); 3254 SET_FIELD(max_channel_ops, SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS); 3255 SET_FIELD(clear_method, BS_CLEAR_WITH_UNMAP); 3256 3257 if (FIELD_OK(bstype)) { 3258 memset(&opts->bstype, 0, sizeof(opts->bstype)); 3259 } 3260 3261 SET_FIELD(iter_cb_fn, NULL); 3262 SET_FIELD(iter_cb_arg, NULL); 3263 3264 #undef FIELD_OK 3265 #undef SET_FIELD 3266 } 3267 3268 static int 3269 bs_opts_verify(struct spdk_bs_opts *opts) 3270 { 3271 if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 || 3272 opts->max_channel_ops == 0) { 3273 SPDK_ERRLOG("Blobstore options cannot be set to 0\n"); 3274 return -1; 3275 } 3276 3277 return 0; 3278 } 3279 3280 /* START spdk_bs_load */ 3281 3282 /* spdk_bs_load_ctx is used for init, load, unload and dump code paths. */ 3283 3284 struct spdk_bs_load_ctx { 3285 struct spdk_blob_store *bs; 3286 struct spdk_bs_super_block *super; 3287 3288 struct spdk_bs_md_mask *mask; 3289 bool in_page_chain; 3290 uint32_t page_index; 3291 uint32_t cur_page; 3292 struct spdk_blob_md_page *page; 3293 3294 uint64_t num_extent_pages; 3295 uint32_t *extent_page_num; 3296 struct spdk_blob_md_page *extent_pages; 3297 struct spdk_bit_array *used_clusters; 3298 3299 spdk_bs_sequence_t *seq; 3300 spdk_blob_op_with_handle_complete iter_cb_fn; 3301 void *iter_cb_arg; 3302 struct spdk_blob *blob; 3303 spdk_blob_id blobid; 3304 3305 /* These fields are used in the spdk_bs_dump path. */ 3306 FILE *fp; 3307 spdk_bs_dump_print_xattr print_xattr_fn; 3308 char xattr_name[4096]; 3309 }; 3310 3311 static int 3312 bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs, 3313 struct spdk_bs_load_ctx **_ctx) 3314 { 3315 struct spdk_blob_store *bs; 3316 struct spdk_bs_load_ctx *ctx; 3317 uint64_t dev_size; 3318 int rc; 3319 3320 dev_size = dev->blocklen * dev->blockcnt; 3321 if (dev_size < opts->cluster_sz) { 3322 /* Device size cannot be smaller than cluster size of blobstore */ 3323 SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n", 3324 dev_size, opts->cluster_sz); 3325 return -ENOSPC; 3326 } 3327 if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) { 3328 /* Cluster size cannot be smaller than page size */ 3329 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n", 3330 opts->cluster_sz, SPDK_BS_PAGE_SIZE); 3331 return -EINVAL; 3332 } 3333 bs = calloc(1, sizeof(struct spdk_blob_store)); 3334 if (!bs) { 3335 return -ENOMEM; 3336 } 3337 3338 ctx = calloc(1, sizeof(struct spdk_bs_load_ctx)); 3339 if (!ctx) { 3340 free(bs); 3341 return -ENOMEM; 3342 } 3343 3344 ctx->bs = bs; 3345 ctx->iter_cb_fn = opts->iter_cb_fn; 3346 ctx->iter_cb_arg = opts->iter_cb_arg; 3347 3348 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 3349 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3350 if (!ctx->super) { 3351 free(ctx); 3352 free(bs); 3353 return -ENOMEM; 3354 } 3355 3356 RB_INIT(&bs->open_blobs); 3357 TAILQ_INIT(&bs->snapshots); 3358 bs->dev = dev; 3359 bs->md_thread = spdk_get_thread(); 3360 assert(bs->md_thread != NULL); 3361 3362 /* 3363 * Do not use bs_lba_to_cluster() here since blockcnt may not be an 3364 * even multiple of the cluster size. 3365 */ 3366 bs->cluster_sz = opts->cluster_sz; 3367 bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen); 3368 ctx->used_clusters = spdk_bit_array_create(bs->total_clusters); 3369 if (!ctx->used_clusters) { 3370 spdk_free(ctx->super); 3371 free(ctx); 3372 free(bs); 3373 return -ENOMEM; 3374 } 3375 3376 bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE; 3377 if (spdk_u32_is_pow2(bs->pages_per_cluster)) { 3378 bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster); 3379 } 3380 bs->num_free_clusters = bs->total_clusters; 3381 bs->io_unit_size = dev->blocklen; 3382 3383 bs->max_channel_ops = opts->max_channel_ops; 3384 bs->super_blob = SPDK_BLOBID_INVALID; 3385 memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype)); 3386 3387 /* The metadata is assumed to be at least 1 page */ 3388 bs->used_md_pages = spdk_bit_array_create(1); 3389 bs->used_blobids = spdk_bit_array_create(0); 3390 bs->open_blobids = spdk_bit_array_create(0); 3391 3392 pthread_mutex_init(&bs->used_clusters_mutex, NULL); 3393 3394 spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy, 3395 sizeof(struct spdk_bs_channel), "blobstore"); 3396 rc = bs_register_md_thread(bs); 3397 if (rc == -1) { 3398 spdk_io_device_unregister(bs, NULL); 3399 pthread_mutex_destroy(&bs->used_clusters_mutex); 3400 spdk_bit_array_free(&bs->open_blobids); 3401 spdk_bit_array_free(&bs->used_blobids); 3402 spdk_bit_array_free(&bs->used_md_pages); 3403 spdk_bit_array_free(&ctx->used_clusters); 3404 spdk_free(ctx->super); 3405 free(ctx); 3406 free(bs); 3407 /* FIXME: this is a lie but don't know how to get a proper error code here */ 3408 return -ENOMEM; 3409 } 3410 3411 *_ctx = ctx; 3412 *_bs = bs; 3413 return 0; 3414 } 3415 3416 static void 3417 bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno) 3418 { 3419 assert(bserrno != 0); 3420 3421 spdk_free(ctx->super); 3422 bs_sequence_finish(ctx->seq, bserrno); 3423 bs_free(ctx->bs); 3424 spdk_bit_array_free(&ctx->used_clusters); 3425 free(ctx); 3426 } 3427 3428 static void 3429 bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 3430 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 3431 { 3432 /* Update the values in the super block */ 3433 super->super_blob = bs->super_blob; 3434 memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype)); 3435 super->crc = blob_md_page_calc_crc(super); 3436 bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0), 3437 bs_byte_to_lba(bs, sizeof(*super)), 3438 cb_fn, cb_arg); 3439 } 3440 3441 static void 3442 bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3443 { 3444 struct spdk_bs_load_ctx *ctx = arg; 3445 uint64_t mask_size, lba, lba_count; 3446 3447 /* Write out the used clusters mask */ 3448 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 3449 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3450 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3451 if (!ctx->mask) { 3452 bs_load_ctx_fail(ctx, -ENOMEM); 3453 return; 3454 } 3455 3456 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS; 3457 ctx->mask->length = ctx->bs->total_clusters; 3458 /* We could get here through the normal unload path, or through dirty 3459 * shutdown recovery. For the normal unload path, we use the mask from 3460 * the bit pool. For dirty shutdown recovery, we don't have a bit pool yet - 3461 * only the bit array from the load ctx. 3462 */ 3463 if (ctx->bs->used_clusters) { 3464 assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters)); 3465 spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask); 3466 } else { 3467 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters)); 3468 spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask); 3469 } 3470 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 3471 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 3472 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3473 } 3474 3475 static void 3476 bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3477 { 3478 struct spdk_bs_load_ctx *ctx = arg; 3479 uint64_t mask_size, lba, lba_count; 3480 3481 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 3482 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3483 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3484 if (!ctx->mask) { 3485 bs_load_ctx_fail(ctx, -ENOMEM); 3486 return; 3487 } 3488 3489 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES; 3490 ctx->mask->length = ctx->super->md_len; 3491 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages)); 3492 3493 spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask); 3494 lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 3495 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 3496 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3497 } 3498 3499 static void 3500 bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3501 { 3502 struct spdk_bs_load_ctx *ctx = arg; 3503 uint64_t mask_size, lba, lba_count; 3504 3505 if (ctx->super->used_blobid_mask_len == 0) { 3506 /* 3507 * This is a pre-v3 on-disk format where the blobid mask does not get 3508 * written to disk. 3509 */ 3510 cb_fn(seq, arg, 0); 3511 return; 3512 } 3513 3514 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 3515 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3516 SPDK_MALLOC_DMA); 3517 if (!ctx->mask) { 3518 bs_load_ctx_fail(ctx, -ENOMEM); 3519 return; 3520 } 3521 3522 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS; 3523 ctx->mask->length = ctx->super->md_len; 3524 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids)); 3525 3526 spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask); 3527 lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 3528 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 3529 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3530 } 3531 3532 static void 3533 blob_set_thin_provision(struct spdk_blob *blob) 3534 { 3535 blob_verify_md_op(blob); 3536 blob->invalid_flags |= SPDK_BLOB_THIN_PROV; 3537 blob->state = SPDK_BLOB_STATE_DIRTY; 3538 } 3539 3540 static void 3541 blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method) 3542 { 3543 blob_verify_md_op(blob); 3544 blob->clear_method = clear_method; 3545 blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT); 3546 blob->state = SPDK_BLOB_STATE_DIRTY; 3547 } 3548 3549 static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno); 3550 3551 static void 3552 bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno) 3553 { 3554 struct spdk_bs_load_ctx *ctx = cb_arg; 3555 spdk_blob_id id; 3556 int64_t page_num; 3557 3558 /* Iterate to next blob (we can't use spdk_bs_iter_next function as our 3559 * last blob has been removed */ 3560 page_num = bs_blobid_to_page(ctx->blobid); 3561 page_num++; 3562 page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num); 3563 if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) { 3564 bs_load_iter(ctx, NULL, -ENOENT); 3565 return; 3566 } 3567 3568 id = bs_page_to_blobid(page_num); 3569 3570 spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx); 3571 } 3572 3573 static void 3574 bs_delete_corrupted_close_cb(void *cb_arg, int bserrno) 3575 { 3576 struct spdk_bs_load_ctx *ctx = cb_arg; 3577 3578 if (bserrno != 0) { 3579 SPDK_ERRLOG("Failed to close corrupted blob\n"); 3580 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3581 return; 3582 } 3583 3584 spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx); 3585 } 3586 3587 static void 3588 bs_delete_corrupted_blob(void *cb_arg, int bserrno) 3589 { 3590 struct spdk_bs_load_ctx *ctx = cb_arg; 3591 uint64_t i; 3592 3593 if (bserrno != 0) { 3594 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n"); 3595 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3596 return; 3597 } 3598 3599 /* Snapshot and clone have the same copy of cluster map and extent pages 3600 * at this point. Let's clear both for snapshot now, 3601 * so that it won't be cleared for clone later when we remove snapshot. 3602 * Also set thin provision to pass data corruption check */ 3603 for (i = 0; i < ctx->blob->active.num_clusters; i++) { 3604 ctx->blob->active.clusters[i] = 0; 3605 } 3606 for (i = 0; i < ctx->blob->active.num_extent_pages; i++) { 3607 ctx->blob->active.extent_pages[i] = 0; 3608 } 3609 3610 ctx->blob->md_ro = false; 3611 3612 blob_set_thin_provision(ctx->blob); 3613 3614 ctx->blobid = ctx->blob->id; 3615 3616 spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx); 3617 } 3618 3619 static void 3620 bs_update_corrupted_blob(void *cb_arg, int bserrno) 3621 { 3622 struct spdk_bs_load_ctx *ctx = cb_arg; 3623 3624 if (bserrno != 0) { 3625 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n"); 3626 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3627 return; 3628 } 3629 3630 ctx->blob->md_ro = false; 3631 blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true); 3632 blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true); 3633 spdk_blob_set_read_only(ctx->blob); 3634 3635 if (ctx->iter_cb_fn) { 3636 ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0); 3637 } 3638 bs_blob_list_add(ctx->blob); 3639 3640 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3641 } 3642 3643 static void 3644 bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno) 3645 { 3646 struct spdk_bs_load_ctx *ctx = cb_arg; 3647 3648 if (bserrno != 0) { 3649 SPDK_ERRLOG("Failed to open clone of a corrupted blob\n"); 3650 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3651 return; 3652 } 3653 3654 if (blob->parent_id == ctx->blob->id) { 3655 /* Power failure occurred before updating clone (snapshot delete case) 3656 * or after updating clone (creating snapshot case) - keep snapshot */ 3657 spdk_blob_close(blob, bs_update_corrupted_blob, ctx); 3658 } else { 3659 /* Power failure occurred after updating clone (snapshot delete case) 3660 * or before updating clone (creating snapshot case) - remove snapshot */ 3661 spdk_blob_close(blob, bs_delete_corrupted_blob, ctx); 3662 } 3663 } 3664 3665 static void 3666 bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno) 3667 { 3668 struct spdk_bs_load_ctx *ctx = arg; 3669 const void *value; 3670 size_t len; 3671 int rc = 0; 3672 3673 if (bserrno == 0) { 3674 /* Examine blob if it is corrupted after power failure. Fix 3675 * the ones that can be fixed and remove any other corrupted 3676 * ones. If it is not corrupted just process it */ 3677 rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true); 3678 if (rc != 0) { 3679 rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true); 3680 if (rc != 0) { 3681 /* Not corrupted - process it and continue with iterating through blobs */ 3682 if (ctx->iter_cb_fn) { 3683 ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0); 3684 } 3685 bs_blob_list_add(blob); 3686 spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx); 3687 return; 3688 } 3689 3690 } 3691 3692 assert(len == sizeof(spdk_blob_id)); 3693 3694 ctx->blob = blob; 3695 3696 /* Open clone to check if we are able to fix this blob or should we remove it */ 3697 spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx); 3698 return; 3699 } else if (bserrno == -ENOENT) { 3700 bserrno = 0; 3701 } else { 3702 /* 3703 * This case needs to be looked at further. Same problem 3704 * exists with applications that rely on explicit blob 3705 * iteration. We should just skip the blob that failed 3706 * to load and continue on to the next one. 3707 */ 3708 SPDK_ERRLOG("Error in iterating blobs\n"); 3709 } 3710 3711 ctx->iter_cb_fn = NULL; 3712 3713 spdk_free(ctx->super); 3714 spdk_free(ctx->mask); 3715 bs_sequence_finish(ctx->seq, bserrno); 3716 free(ctx); 3717 } 3718 3719 static void 3720 bs_load_complete(struct spdk_bs_load_ctx *ctx) 3721 { 3722 ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters); 3723 spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx); 3724 } 3725 3726 static void 3727 bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3728 { 3729 struct spdk_bs_load_ctx *ctx = cb_arg; 3730 int rc; 3731 3732 /* The type must be correct */ 3733 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS); 3734 3735 /* The length of the mask (in bits) must not be greater than 3736 * the length of the buffer (converted to bits) */ 3737 assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8)); 3738 3739 /* The length of the mask must be exactly equal to the size 3740 * (in pages) of the metadata region */ 3741 assert(ctx->mask->length == ctx->super->md_len); 3742 3743 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length); 3744 if (rc < 0) { 3745 spdk_free(ctx->mask); 3746 bs_load_ctx_fail(ctx, rc); 3747 return; 3748 } 3749 3750 spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask); 3751 bs_load_complete(ctx); 3752 } 3753 3754 static void 3755 bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3756 { 3757 struct spdk_bs_load_ctx *ctx = cb_arg; 3758 uint64_t lba, lba_count, mask_size; 3759 int rc; 3760 3761 if (bserrno != 0) { 3762 bs_load_ctx_fail(ctx, bserrno); 3763 return; 3764 } 3765 3766 /* The type must be correct */ 3767 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 3768 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 3769 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 3770 struct spdk_blob_md_page) * 8)); 3771 /* The length of the mask must be exactly equal to the total number of clusters */ 3772 assert(ctx->mask->length == ctx->bs->total_clusters); 3773 3774 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length); 3775 if (rc < 0) { 3776 spdk_free(ctx->mask); 3777 bs_load_ctx_fail(ctx, rc); 3778 return; 3779 } 3780 3781 spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask); 3782 ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters); 3783 assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters); 3784 3785 spdk_free(ctx->mask); 3786 3787 /* Read the used blobids mask */ 3788 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 3789 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3790 SPDK_MALLOC_DMA); 3791 if (!ctx->mask) { 3792 bs_load_ctx_fail(ctx, -ENOMEM); 3793 return; 3794 } 3795 lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 3796 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 3797 bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 3798 bs_load_used_blobids_cpl, ctx); 3799 } 3800 3801 static void 3802 bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3803 { 3804 struct spdk_bs_load_ctx *ctx = cb_arg; 3805 uint64_t lba, lba_count, mask_size; 3806 int rc; 3807 3808 if (bserrno != 0) { 3809 bs_load_ctx_fail(ctx, bserrno); 3810 return; 3811 } 3812 3813 /* The type must be correct */ 3814 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES); 3815 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 3816 assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE * 3817 8)); 3818 /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */ 3819 if (ctx->mask->length != ctx->super->md_len) { 3820 SPDK_ERRLOG("mismatched md_len in used_pages mask: " 3821 "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n", 3822 ctx->mask->length, ctx->super->md_len); 3823 assert(false); 3824 } 3825 3826 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length); 3827 if (rc < 0) { 3828 spdk_free(ctx->mask); 3829 bs_load_ctx_fail(ctx, rc); 3830 return; 3831 } 3832 3833 spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask); 3834 spdk_free(ctx->mask); 3835 3836 /* Read the used clusters mask */ 3837 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 3838 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3839 SPDK_MALLOC_DMA); 3840 if (!ctx->mask) { 3841 bs_load_ctx_fail(ctx, -ENOMEM); 3842 return; 3843 } 3844 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 3845 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 3846 bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 3847 bs_load_used_clusters_cpl, ctx); 3848 } 3849 3850 static void 3851 bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx) 3852 { 3853 uint64_t lba, lba_count, mask_size; 3854 3855 /* Read the used pages mask */ 3856 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 3857 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3858 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3859 if (!ctx->mask) { 3860 bs_load_ctx_fail(ctx, -ENOMEM); 3861 return; 3862 } 3863 3864 lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 3865 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 3866 bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count, 3867 bs_load_used_pages_cpl, ctx); 3868 } 3869 3870 static int 3871 bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page) 3872 { 3873 struct spdk_blob_store *bs = ctx->bs; 3874 struct spdk_blob_md_descriptor *desc; 3875 size_t cur_desc = 0; 3876 3877 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 3878 while (cur_desc < sizeof(page->descriptors)) { 3879 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 3880 if (desc->length == 0) { 3881 /* If padding and length are 0, this terminates the page */ 3882 break; 3883 } 3884 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 3885 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 3886 unsigned int i, j; 3887 unsigned int cluster_count = 0; 3888 uint32_t cluster_idx; 3889 3890 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 3891 3892 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 3893 for (j = 0; j < desc_extent_rle->extents[i].length; j++) { 3894 cluster_idx = desc_extent_rle->extents[i].cluster_idx; 3895 /* 3896 * cluster_idx = 0 means an unallocated cluster - don't mark that 3897 * in the used cluster map. 3898 */ 3899 if (cluster_idx != 0) { 3900 spdk_bit_array_set(ctx->used_clusters, cluster_idx + j); 3901 if (bs->num_free_clusters == 0) { 3902 return -ENOSPC; 3903 } 3904 bs->num_free_clusters--; 3905 } 3906 cluster_count++; 3907 } 3908 } 3909 if (cluster_count == 0) { 3910 return -EINVAL; 3911 } 3912 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 3913 struct spdk_blob_md_descriptor_extent_page *desc_extent; 3914 uint32_t i; 3915 uint32_t cluster_count = 0; 3916 uint32_t cluster_idx; 3917 size_t cluster_idx_length; 3918 3919 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 3920 cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx); 3921 3922 if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) || 3923 (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) { 3924 return -EINVAL; 3925 } 3926 3927 for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) { 3928 cluster_idx = desc_extent->cluster_idx[i]; 3929 /* 3930 * cluster_idx = 0 means an unallocated cluster - don't mark that 3931 * in the used cluster map. 3932 */ 3933 if (cluster_idx != 0) { 3934 if (cluster_idx < desc_extent->start_cluster_idx && 3935 cluster_idx >= desc_extent->start_cluster_idx + cluster_count) { 3936 return -EINVAL; 3937 } 3938 spdk_bit_array_set(ctx->used_clusters, cluster_idx); 3939 if (bs->num_free_clusters == 0) { 3940 return -ENOSPC; 3941 } 3942 bs->num_free_clusters--; 3943 } 3944 cluster_count++; 3945 } 3946 3947 if (cluster_count == 0) { 3948 return -EINVAL; 3949 } 3950 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 3951 /* Skip this item */ 3952 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 3953 /* Skip this item */ 3954 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 3955 /* Skip this item */ 3956 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) { 3957 struct spdk_blob_md_descriptor_extent_table *desc_extent_table; 3958 uint32_t num_extent_pages = ctx->num_extent_pages; 3959 uint32_t i; 3960 size_t extent_pages_length; 3961 void *tmp; 3962 3963 desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc; 3964 extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters); 3965 3966 if (desc_extent_table->length == 0 || 3967 (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) { 3968 return -EINVAL; 3969 } 3970 3971 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 3972 if (desc_extent_table->extent_page[i].page_idx != 0) { 3973 if (desc_extent_table->extent_page[i].num_pages != 1) { 3974 return -EINVAL; 3975 } 3976 num_extent_pages += 1; 3977 } 3978 } 3979 3980 if (num_extent_pages > 0) { 3981 tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t)); 3982 if (tmp == NULL) { 3983 return -ENOMEM; 3984 } 3985 ctx->extent_page_num = tmp; 3986 3987 /* Extent table entries contain md page numbers for extent pages. 3988 * Zeroes represent unallocated extent pages, those are run-length-encoded. 3989 */ 3990 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 3991 if (desc_extent_table->extent_page[i].page_idx != 0) { 3992 ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx; 3993 ctx->num_extent_pages += 1; 3994 } 3995 } 3996 } 3997 } else { 3998 /* Error */ 3999 return -EINVAL; 4000 } 4001 /* Advance to the next descriptor */ 4002 cur_desc += sizeof(*desc) + desc->length; 4003 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 4004 break; 4005 } 4006 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 4007 } 4008 return 0; 4009 } 4010 4011 static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page) 4012 { 4013 uint32_t crc; 4014 struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors; 4015 size_t desc_len; 4016 4017 crc = blob_md_page_calc_crc(page); 4018 if (crc != page->crc) { 4019 return false; 4020 } 4021 4022 /* Extent page should always be of sequence num 0. */ 4023 if (page->sequence_num != 0) { 4024 return false; 4025 } 4026 4027 /* Descriptor type must be EXTENT_PAGE. */ 4028 if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4029 return false; 4030 } 4031 4032 /* Descriptor length cannot exceed the page. */ 4033 desc_len = sizeof(*desc) + desc->length; 4034 if (desc_len > sizeof(page->descriptors)) { 4035 return false; 4036 } 4037 4038 /* It has to be the only descriptor in the page. */ 4039 if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) { 4040 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len); 4041 if (desc->length != 0) { 4042 return false; 4043 } 4044 } 4045 4046 return true; 4047 } 4048 4049 static bool bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx) 4050 { 4051 uint32_t crc; 4052 struct spdk_blob_md_page *page = ctx->page; 4053 4054 crc = blob_md_page_calc_crc(page); 4055 if (crc != page->crc) { 4056 return false; 4057 } 4058 4059 /* First page of a sequence should match the blobid. */ 4060 if (page->sequence_num == 0 && 4061 bs_page_to_blobid(ctx->cur_page) != page->id) { 4062 return false; 4063 } 4064 assert(bs_load_cur_extent_page_valid(page) == false); 4065 4066 return true; 4067 } 4068 4069 static void 4070 bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx); 4071 4072 static void 4073 bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4074 { 4075 struct spdk_bs_load_ctx *ctx = cb_arg; 4076 4077 if (bserrno != 0) { 4078 bs_load_ctx_fail(ctx, bserrno); 4079 return; 4080 } 4081 4082 bs_load_complete(ctx); 4083 } 4084 4085 static void 4086 bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4087 { 4088 struct spdk_bs_load_ctx *ctx = cb_arg; 4089 4090 spdk_free(ctx->mask); 4091 ctx->mask = NULL; 4092 4093 if (bserrno != 0) { 4094 bs_load_ctx_fail(ctx, bserrno); 4095 return; 4096 } 4097 4098 bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl); 4099 } 4100 4101 static void 4102 bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4103 { 4104 struct spdk_bs_load_ctx *ctx = cb_arg; 4105 4106 spdk_free(ctx->mask); 4107 ctx->mask = NULL; 4108 4109 if (bserrno != 0) { 4110 bs_load_ctx_fail(ctx, bserrno); 4111 return; 4112 } 4113 4114 bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl); 4115 } 4116 4117 static void 4118 bs_load_write_used_md(struct spdk_bs_load_ctx *ctx) 4119 { 4120 bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl); 4121 } 4122 4123 static void 4124 bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx) 4125 { 4126 uint64_t num_md_clusters; 4127 uint64_t i; 4128 4129 ctx->in_page_chain = false; 4130 4131 do { 4132 ctx->page_index++; 4133 } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true); 4134 4135 if (ctx->page_index < ctx->super->md_len) { 4136 ctx->cur_page = ctx->page_index; 4137 bs_load_replay_cur_md_page(ctx); 4138 } else { 4139 /* Claim all of the clusters used by the metadata */ 4140 num_md_clusters = spdk_divide_round_up( 4141 ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster); 4142 for (i = 0; i < num_md_clusters; i++) { 4143 spdk_bit_array_set(ctx->used_clusters, i); 4144 } 4145 ctx->bs->num_free_clusters -= num_md_clusters; 4146 spdk_free(ctx->page); 4147 bs_load_write_used_md(ctx); 4148 } 4149 } 4150 4151 static void 4152 bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4153 { 4154 struct spdk_bs_load_ctx *ctx = cb_arg; 4155 uint32_t page_num; 4156 uint64_t i; 4157 4158 if (bserrno != 0) { 4159 spdk_free(ctx->extent_pages); 4160 bs_load_ctx_fail(ctx, bserrno); 4161 return; 4162 } 4163 4164 for (i = 0; i < ctx->num_extent_pages; i++) { 4165 /* Extent pages are only read when present within in chain md. 4166 * Integrity of md is not right if that page was not a valid extent page. */ 4167 if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) { 4168 spdk_free(ctx->extent_pages); 4169 bs_load_ctx_fail(ctx, -EILSEQ); 4170 return; 4171 } 4172 4173 page_num = ctx->extent_page_num[i]; 4174 spdk_bit_array_set(ctx->bs->used_md_pages, page_num); 4175 if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) { 4176 spdk_free(ctx->extent_pages); 4177 bs_load_ctx_fail(ctx, -EILSEQ); 4178 return; 4179 } 4180 } 4181 4182 spdk_free(ctx->extent_pages); 4183 free(ctx->extent_page_num); 4184 ctx->extent_page_num = NULL; 4185 ctx->num_extent_pages = 0; 4186 4187 bs_load_replay_md_chain_cpl(ctx); 4188 } 4189 4190 static void 4191 bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx) 4192 { 4193 spdk_bs_batch_t *batch; 4194 uint32_t page; 4195 uint64_t lba; 4196 uint64_t i; 4197 4198 ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0, 4199 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4200 if (!ctx->extent_pages) { 4201 bs_load_ctx_fail(ctx, -ENOMEM); 4202 return; 4203 } 4204 4205 batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx); 4206 4207 for (i = 0; i < ctx->num_extent_pages; i++) { 4208 page = ctx->extent_page_num[i]; 4209 assert(page < ctx->super->md_len); 4210 lba = bs_md_page_to_lba(ctx->bs, page); 4211 bs_batch_read_dev(batch, &ctx->extent_pages[i], lba, 4212 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE)); 4213 } 4214 4215 bs_batch_close(batch); 4216 } 4217 4218 static void 4219 bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4220 { 4221 struct spdk_bs_load_ctx *ctx = cb_arg; 4222 uint32_t page_num; 4223 struct spdk_blob_md_page *page; 4224 4225 if (bserrno != 0) { 4226 bs_load_ctx_fail(ctx, bserrno); 4227 return; 4228 } 4229 4230 page_num = ctx->cur_page; 4231 page = ctx->page; 4232 if (bs_load_cur_md_page_valid(ctx) == true) { 4233 if (page->sequence_num == 0 || ctx->in_page_chain == true) { 4234 bs_claim_md_page(ctx->bs, page_num); 4235 if (page->sequence_num == 0) { 4236 spdk_bit_array_set(ctx->bs->used_blobids, page_num); 4237 } 4238 if (bs_load_replay_md_parse_page(ctx, page)) { 4239 bs_load_ctx_fail(ctx, -EILSEQ); 4240 return; 4241 } 4242 if (page->next != SPDK_INVALID_MD_PAGE) { 4243 ctx->in_page_chain = true; 4244 ctx->cur_page = page->next; 4245 bs_load_replay_cur_md_page(ctx); 4246 return; 4247 } 4248 if (ctx->num_extent_pages != 0) { 4249 bs_load_replay_extent_pages(ctx); 4250 return; 4251 } 4252 } 4253 } 4254 bs_load_replay_md_chain_cpl(ctx); 4255 } 4256 4257 static void 4258 bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx) 4259 { 4260 uint64_t lba; 4261 4262 assert(ctx->cur_page < ctx->super->md_len); 4263 lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page); 4264 bs_sequence_read_dev(ctx->seq, ctx->page, lba, 4265 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 4266 bs_load_replay_md_cpl, ctx); 4267 } 4268 4269 static void 4270 bs_load_replay_md(struct spdk_bs_load_ctx *ctx) 4271 { 4272 ctx->page_index = 0; 4273 ctx->cur_page = 0; 4274 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 4275 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4276 if (!ctx->page) { 4277 bs_load_ctx_fail(ctx, -ENOMEM); 4278 return; 4279 } 4280 bs_load_replay_cur_md_page(ctx); 4281 } 4282 4283 static void 4284 bs_recover(struct spdk_bs_load_ctx *ctx) 4285 { 4286 int rc; 4287 4288 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); 4289 if (rc < 0) { 4290 bs_load_ctx_fail(ctx, -ENOMEM); 4291 return; 4292 } 4293 4294 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len); 4295 if (rc < 0) { 4296 bs_load_ctx_fail(ctx, -ENOMEM); 4297 return; 4298 } 4299 4300 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 4301 if (rc < 0) { 4302 bs_load_ctx_fail(ctx, -ENOMEM); 4303 return; 4304 } 4305 4306 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len); 4307 if (rc < 0) { 4308 bs_load_ctx_fail(ctx, -ENOMEM); 4309 return; 4310 } 4311 4312 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 4313 bs_load_replay_md(ctx); 4314 } 4315 4316 static void 4317 bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4318 { 4319 struct spdk_bs_load_ctx *ctx = cb_arg; 4320 uint32_t crc; 4321 int rc; 4322 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 4323 4324 if (ctx->super->version > SPDK_BS_VERSION || 4325 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 4326 bs_load_ctx_fail(ctx, -EILSEQ); 4327 return; 4328 } 4329 4330 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4331 sizeof(ctx->super->signature)) != 0) { 4332 bs_load_ctx_fail(ctx, -EILSEQ); 4333 return; 4334 } 4335 4336 crc = blob_md_page_calc_crc(ctx->super); 4337 if (crc != ctx->super->crc) { 4338 bs_load_ctx_fail(ctx, -EILSEQ); 4339 return; 4340 } 4341 4342 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 4343 SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n"); 4344 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 4345 SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n"); 4346 } else { 4347 SPDK_DEBUGLOG(blob, "Unexpected bstype\n"); 4348 SPDK_LOGDUMP(blob, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 4349 SPDK_LOGDUMP(blob, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 4350 bs_load_ctx_fail(ctx, -ENXIO); 4351 return; 4352 } 4353 4354 if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) { 4355 SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n", 4356 ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size); 4357 bs_load_ctx_fail(ctx, -EILSEQ); 4358 return; 4359 } 4360 4361 if (ctx->super->size == 0) { 4362 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 4363 } 4364 4365 if (ctx->super->io_unit_size == 0) { 4366 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE; 4367 } 4368 4369 /* Parse the super block */ 4370 ctx->bs->clean = 1; 4371 ctx->bs->cluster_sz = ctx->super->cluster_size; 4372 ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size; 4373 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 4374 if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) { 4375 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster); 4376 } 4377 ctx->bs->io_unit_size = ctx->super->io_unit_size; 4378 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 4379 if (rc < 0) { 4380 bs_load_ctx_fail(ctx, -ENOMEM); 4381 return; 4382 } 4383 ctx->bs->md_start = ctx->super->md_start; 4384 ctx->bs->md_len = ctx->super->md_len; 4385 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len); 4386 if (rc < 0) { 4387 bs_load_ctx_fail(ctx, -ENOMEM); 4388 return; 4389 } 4390 4391 ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up( 4392 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 4393 ctx->bs->super_blob = ctx->super->super_blob; 4394 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 4395 4396 if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) { 4397 bs_recover(ctx); 4398 } else { 4399 bs_load_read_used_pages(ctx); 4400 } 4401 } 4402 4403 static inline int 4404 bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst) 4405 { 4406 4407 if (!src->opts_size) { 4408 SPDK_ERRLOG("opts_size should not be zero value\n"); 4409 return -1; 4410 } 4411 4412 #define FIELD_OK(field) \ 4413 offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size 4414 4415 #define SET_FIELD(field) \ 4416 if (FIELD_OK(field)) { \ 4417 dst->field = src->field; \ 4418 } \ 4419 4420 SET_FIELD(cluster_sz); 4421 SET_FIELD(num_md_pages); 4422 SET_FIELD(max_md_ops); 4423 SET_FIELD(max_channel_ops); 4424 SET_FIELD(clear_method); 4425 4426 if (FIELD_OK(bstype)) { 4427 memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype)); 4428 } 4429 SET_FIELD(iter_cb_fn); 4430 SET_FIELD(iter_cb_arg); 4431 4432 dst->opts_size = src->opts_size; 4433 4434 /* You should not remove this statement, but need to update the assert statement 4435 * if you add a new field, and also add a corresponding SET_FIELD statement */ 4436 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 64, "Incorrect size"); 4437 4438 #undef FIELD_OK 4439 #undef SET_FIELD 4440 4441 return 0; 4442 } 4443 4444 void 4445 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 4446 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 4447 { 4448 struct spdk_blob_store *bs; 4449 struct spdk_bs_cpl cpl; 4450 struct spdk_bs_load_ctx *ctx; 4451 struct spdk_bs_opts opts = {}; 4452 int err; 4453 4454 SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev); 4455 4456 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 4457 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen); 4458 dev->destroy(dev); 4459 cb_fn(cb_arg, NULL, -EINVAL); 4460 return; 4461 } 4462 4463 spdk_bs_opts_init(&opts, sizeof(opts)); 4464 if (o) { 4465 if (bs_opts_copy(o, &opts)) { 4466 return; 4467 } 4468 } 4469 4470 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 4471 dev->destroy(dev); 4472 cb_fn(cb_arg, NULL, -EINVAL); 4473 return; 4474 } 4475 4476 err = bs_alloc(dev, &opts, &bs, &ctx); 4477 if (err) { 4478 dev->destroy(dev); 4479 cb_fn(cb_arg, NULL, err); 4480 return; 4481 } 4482 4483 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 4484 cpl.u.bs_handle.cb_fn = cb_fn; 4485 cpl.u.bs_handle.cb_arg = cb_arg; 4486 cpl.u.bs_handle.bs = bs; 4487 4488 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 4489 if (!ctx->seq) { 4490 spdk_free(ctx->super); 4491 free(ctx); 4492 bs_free(bs); 4493 cb_fn(cb_arg, NULL, -ENOMEM); 4494 return; 4495 } 4496 4497 /* Read the super block */ 4498 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 4499 bs_byte_to_lba(bs, sizeof(*ctx->super)), 4500 bs_load_super_cpl, ctx); 4501 } 4502 4503 /* END spdk_bs_load */ 4504 4505 /* START spdk_bs_dump */ 4506 4507 static void 4508 bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 4509 { 4510 spdk_free(ctx->super); 4511 4512 /* 4513 * We need to defer calling bs_call_cpl() until after 4514 * dev destruction, so tuck these away for later use. 4515 */ 4516 ctx->bs->unload_err = bserrno; 4517 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 4518 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 4519 4520 bs_sequence_finish(seq, 0); 4521 bs_free(ctx->bs); 4522 free(ctx); 4523 } 4524 4525 static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 4526 4527 static void 4528 bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx) 4529 { 4530 uint32_t page_idx = ctx->cur_page; 4531 struct spdk_blob_md_page *page = ctx->page; 4532 struct spdk_blob_md_descriptor *desc; 4533 size_t cur_desc = 0; 4534 uint32_t crc; 4535 4536 fprintf(ctx->fp, "=========\n"); 4537 fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx); 4538 fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id); 4539 4540 crc = blob_md_page_calc_crc(page); 4541 fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch"); 4542 4543 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 4544 while (cur_desc < sizeof(page->descriptors)) { 4545 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 4546 if (desc->length == 0) { 4547 /* If padding and length are 0, this terminates the page */ 4548 break; 4549 } 4550 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 4551 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 4552 unsigned int i; 4553 4554 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 4555 4556 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 4557 if (desc_extent_rle->extents[i].cluster_idx != 0) { 4558 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 4559 desc_extent_rle->extents[i].cluster_idx); 4560 } else { 4561 fprintf(ctx->fp, "Unallocated Extent - "); 4562 } 4563 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length); 4564 fprintf(ctx->fp, "\n"); 4565 } 4566 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4567 struct spdk_blob_md_descriptor_extent_page *desc_extent; 4568 unsigned int i; 4569 4570 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 4571 4572 for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) { 4573 if (desc_extent->cluster_idx[i] != 0) { 4574 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 4575 desc_extent->cluster_idx[i]); 4576 } else { 4577 fprintf(ctx->fp, "Unallocated Extent"); 4578 } 4579 fprintf(ctx->fp, "\n"); 4580 } 4581 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 4582 struct spdk_blob_md_descriptor_xattr *desc_xattr; 4583 uint32_t i; 4584 4585 desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc; 4586 4587 if (desc_xattr->length != 4588 sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) + 4589 desc_xattr->name_length + desc_xattr->value_length) { 4590 } 4591 4592 memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length); 4593 ctx->xattr_name[desc_xattr->name_length] = '\0'; 4594 fprintf(ctx->fp, "XATTR: name = \"%s\"\n", ctx->xattr_name); 4595 fprintf(ctx->fp, " value = \""); 4596 ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name, 4597 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 4598 desc_xattr->value_length); 4599 fprintf(ctx->fp, "\"\n"); 4600 for (i = 0; i < desc_xattr->value_length; i++) { 4601 if (i % 16 == 0) { 4602 fprintf(ctx->fp, " "); 4603 } 4604 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i)); 4605 if ((i + 1) % 16 == 0) { 4606 fprintf(ctx->fp, "\n"); 4607 } 4608 } 4609 if (i % 16 != 0) { 4610 fprintf(ctx->fp, "\n"); 4611 } 4612 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 4613 /* TODO */ 4614 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 4615 /* TODO */ 4616 } else { 4617 /* Error */ 4618 } 4619 /* Advance to the next descriptor */ 4620 cur_desc += sizeof(*desc) + desc->length; 4621 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 4622 break; 4623 } 4624 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 4625 } 4626 } 4627 4628 static void 4629 bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4630 { 4631 struct spdk_bs_load_ctx *ctx = cb_arg; 4632 4633 if (bserrno != 0) { 4634 bs_dump_finish(seq, ctx, bserrno); 4635 return; 4636 } 4637 4638 if (ctx->page->id != 0) { 4639 bs_dump_print_md_page(ctx); 4640 } 4641 4642 ctx->cur_page++; 4643 4644 if (ctx->cur_page < ctx->super->md_len) { 4645 bs_dump_read_md_page(seq, ctx); 4646 } else { 4647 spdk_free(ctx->page); 4648 bs_dump_finish(seq, ctx, 0); 4649 } 4650 } 4651 4652 static void 4653 bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 4654 { 4655 struct spdk_bs_load_ctx *ctx = cb_arg; 4656 uint64_t lba; 4657 4658 assert(ctx->cur_page < ctx->super->md_len); 4659 lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 4660 bs_sequence_read_dev(seq, ctx->page, lba, 4661 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 4662 bs_dump_read_md_page_cpl, ctx); 4663 } 4664 4665 static void 4666 bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4667 { 4668 struct spdk_bs_load_ctx *ctx = cb_arg; 4669 4670 fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature); 4671 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4672 sizeof(ctx->super->signature)) != 0) { 4673 fprintf(ctx->fp, "(Mismatch)\n"); 4674 bs_dump_finish(seq, ctx, bserrno); 4675 return; 4676 } else { 4677 fprintf(ctx->fp, "(OK)\n"); 4678 } 4679 fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version); 4680 fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc, 4681 (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch"); 4682 fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype); 4683 fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size); 4684 fprintf(ctx->fp, "Super Blob ID: "); 4685 if (ctx->super->super_blob == SPDK_BLOBID_INVALID) { 4686 fprintf(ctx->fp, "(None)\n"); 4687 } else { 4688 fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob); 4689 } 4690 fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean); 4691 fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start); 4692 fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len); 4693 fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start); 4694 fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len); 4695 fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start); 4696 fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len); 4697 fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start); 4698 fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len); 4699 4700 ctx->cur_page = 0; 4701 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 4702 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4703 if (!ctx->page) { 4704 bs_dump_finish(seq, ctx, -ENOMEM); 4705 return; 4706 } 4707 bs_dump_read_md_page(seq, ctx); 4708 } 4709 4710 void 4711 spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn, 4712 spdk_bs_op_complete cb_fn, void *cb_arg) 4713 { 4714 struct spdk_blob_store *bs; 4715 struct spdk_bs_cpl cpl; 4716 spdk_bs_sequence_t *seq; 4717 struct spdk_bs_load_ctx *ctx; 4718 struct spdk_bs_opts opts = {}; 4719 int err; 4720 4721 SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev); 4722 4723 spdk_bs_opts_init(&opts, sizeof(opts)); 4724 4725 err = bs_alloc(dev, &opts, &bs, &ctx); 4726 if (err) { 4727 dev->destroy(dev); 4728 cb_fn(cb_arg, err); 4729 return; 4730 } 4731 4732 ctx->fp = fp; 4733 ctx->print_xattr_fn = print_xattr_fn; 4734 4735 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 4736 cpl.u.bs_basic.cb_fn = cb_fn; 4737 cpl.u.bs_basic.cb_arg = cb_arg; 4738 4739 seq = bs_sequence_start(bs->md_channel, &cpl); 4740 if (!seq) { 4741 spdk_free(ctx->super); 4742 free(ctx); 4743 bs_free(bs); 4744 cb_fn(cb_arg, -ENOMEM); 4745 return; 4746 } 4747 4748 /* Read the super block */ 4749 bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0), 4750 bs_byte_to_lba(bs, sizeof(*ctx->super)), 4751 bs_dump_super_cpl, ctx); 4752 } 4753 4754 /* END spdk_bs_dump */ 4755 4756 /* START spdk_bs_init */ 4757 4758 static void 4759 bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4760 { 4761 struct spdk_bs_load_ctx *ctx = cb_arg; 4762 4763 ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters); 4764 spdk_free(ctx->super); 4765 free(ctx); 4766 4767 bs_sequence_finish(seq, bserrno); 4768 } 4769 4770 static void 4771 bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4772 { 4773 struct spdk_bs_load_ctx *ctx = cb_arg; 4774 4775 /* Write super block */ 4776 bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0), 4777 bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 4778 bs_init_persist_super_cpl, ctx); 4779 } 4780 4781 void 4782 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 4783 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 4784 { 4785 struct spdk_bs_load_ctx *ctx; 4786 struct spdk_blob_store *bs; 4787 struct spdk_bs_cpl cpl; 4788 spdk_bs_sequence_t *seq; 4789 spdk_bs_batch_t *batch; 4790 uint64_t num_md_lba; 4791 uint64_t num_md_pages; 4792 uint64_t num_md_clusters; 4793 uint32_t i; 4794 struct spdk_bs_opts opts = {}; 4795 int rc; 4796 uint64_t lba, lba_count; 4797 4798 SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev); 4799 4800 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 4801 SPDK_ERRLOG("unsupported dev block length of %d\n", 4802 dev->blocklen); 4803 dev->destroy(dev); 4804 cb_fn(cb_arg, NULL, -EINVAL); 4805 return; 4806 } 4807 4808 spdk_bs_opts_init(&opts, sizeof(opts)); 4809 if (o) { 4810 if (bs_opts_copy(o, &opts)) { 4811 return; 4812 } 4813 } 4814 4815 if (bs_opts_verify(&opts) != 0) { 4816 dev->destroy(dev); 4817 cb_fn(cb_arg, NULL, -EINVAL); 4818 return; 4819 } 4820 4821 rc = bs_alloc(dev, &opts, &bs, &ctx); 4822 if (rc) { 4823 dev->destroy(dev); 4824 cb_fn(cb_arg, NULL, rc); 4825 return; 4826 } 4827 4828 if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) { 4829 /* By default, allocate 1 page per cluster. 4830 * Technically, this over-allocates metadata 4831 * because more metadata will reduce the number 4832 * of usable clusters. This can be addressed with 4833 * more complex math in the future. 4834 */ 4835 bs->md_len = bs->total_clusters; 4836 } else { 4837 bs->md_len = opts.num_md_pages; 4838 } 4839 rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len); 4840 if (rc < 0) { 4841 spdk_free(ctx->super); 4842 free(ctx); 4843 bs_free(bs); 4844 cb_fn(cb_arg, NULL, -ENOMEM); 4845 return; 4846 } 4847 4848 rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len); 4849 if (rc < 0) { 4850 spdk_free(ctx->super); 4851 free(ctx); 4852 bs_free(bs); 4853 cb_fn(cb_arg, NULL, -ENOMEM); 4854 return; 4855 } 4856 4857 rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len); 4858 if (rc < 0) { 4859 spdk_free(ctx->super); 4860 free(ctx); 4861 bs_free(bs); 4862 cb_fn(cb_arg, NULL, -ENOMEM); 4863 return; 4864 } 4865 4866 memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4867 sizeof(ctx->super->signature)); 4868 ctx->super->version = SPDK_BS_VERSION; 4869 ctx->super->length = sizeof(*ctx->super); 4870 ctx->super->super_blob = bs->super_blob; 4871 ctx->super->clean = 0; 4872 ctx->super->cluster_size = bs->cluster_sz; 4873 ctx->super->io_unit_size = bs->io_unit_size; 4874 memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype)); 4875 4876 /* Calculate how many pages the metadata consumes at the front 4877 * of the disk. 4878 */ 4879 4880 /* The super block uses 1 page */ 4881 num_md_pages = 1; 4882 4883 /* The used_md_pages mask requires 1 bit per metadata page, rounded 4884 * up to the nearest page, plus a header. 4885 */ 4886 ctx->super->used_page_mask_start = num_md_pages; 4887 ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 4888 spdk_divide_round_up(bs->md_len, 8), 4889 SPDK_BS_PAGE_SIZE); 4890 num_md_pages += ctx->super->used_page_mask_len; 4891 4892 /* The used_clusters mask requires 1 bit per cluster, rounded 4893 * up to the nearest page, plus a header. 4894 */ 4895 ctx->super->used_cluster_mask_start = num_md_pages; 4896 ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 4897 spdk_divide_round_up(bs->total_clusters, 8), 4898 SPDK_BS_PAGE_SIZE); 4899 num_md_pages += ctx->super->used_cluster_mask_len; 4900 4901 /* The used_blobids mask requires 1 bit per metadata page, rounded 4902 * up to the nearest page, plus a header. 4903 */ 4904 ctx->super->used_blobid_mask_start = num_md_pages; 4905 ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 4906 spdk_divide_round_up(bs->md_len, 8), 4907 SPDK_BS_PAGE_SIZE); 4908 num_md_pages += ctx->super->used_blobid_mask_len; 4909 4910 /* The metadata region size was chosen above */ 4911 ctx->super->md_start = bs->md_start = num_md_pages; 4912 ctx->super->md_len = bs->md_len; 4913 num_md_pages += bs->md_len; 4914 4915 num_md_lba = bs_page_to_lba(bs, num_md_pages); 4916 4917 ctx->super->size = dev->blockcnt * dev->blocklen; 4918 4919 ctx->super->crc = blob_md_page_calc_crc(ctx->super); 4920 4921 num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster); 4922 if (num_md_clusters > bs->total_clusters) { 4923 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, " 4924 "please decrease number of pages reserved for metadata " 4925 "or increase cluster size.\n"); 4926 spdk_free(ctx->super); 4927 spdk_bit_array_free(&ctx->used_clusters); 4928 free(ctx); 4929 bs_free(bs); 4930 cb_fn(cb_arg, NULL, -ENOMEM); 4931 return; 4932 } 4933 /* Claim all of the clusters used by the metadata */ 4934 for (i = 0; i < num_md_clusters; i++) { 4935 spdk_bit_array_set(ctx->used_clusters, i); 4936 } 4937 4938 bs->num_free_clusters -= num_md_clusters; 4939 bs->total_data_clusters = bs->num_free_clusters; 4940 4941 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 4942 cpl.u.bs_handle.cb_fn = cb_fn; 4943 cpl.u.bs_handle.cb_arg = cb_arg; 4944 cpl.u.bs_handle.bs = bs; 4945 4946 seq = bs_sequence_start(bs->md_channel, &cpl); 4947 if (!seq) { 4948 spdk_free(ctx->super); 4949 free(ctx); 4950 bs_free(bs); 4951 cb_fn(cb_arg, NULL, -ENOMEM); 4952 return; 4953 } 4954 4955 batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx); 4956 4957 /* Clear metadata space */ 4958 bs_batch_write_zeroes_dev(batch, 0, num_md_lba); 4959 4960 lba = num_md_lba; 4961 lba_count = ctx->bs->dev->blockcnt - lba; 4962 switch (opts.clear_method) { 4963 case BS_CLEAR_WITH_UNMAP: 4964 /* Trim data clusters */ 4965 bs_batch_unmap_dev(batch, lba, lba_count); 4966 break; 4967 case BS_CLEAR_WITH_WRITE_ZEROES: 4968 /* Write_zeroes to data clusters */ 4969 bs_batch_write_zeroes_dev(batch, lba, lba_count); 4970 break; 4971 case BS_CLEAR_WITH_NONE: 4972 default: 4973 break; 4974 } 4975 4976 bs_batch_close(batch); 4977 } 4978 4979 /* END spdk_bs_init */ 4980 4981 /* START spdk_bs_destroy */ 4982 4983 static void 4984 bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4985 { 4986 struct spdk_bs_load_ctx *ctx = cb_arg; 4987 struct spdk_blob_store *bs = ctx->bs; 4988 4989 /* 4990 * We need to defer calling bs_call_cpl() until after 4991 * dev destruction, so tuck these away for later use. 4992 */ 4993 bs->unload_err = bserrno; 4994 memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 4995 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 4996 4997 bs_sequence_finish(seq, bserrno); 4998 4999 bs_free(bs); 5000 free(ctx); 5001 } 5002 5003 void 5004 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, 5005 void *cb_arg) 5006 { 5007 struct spdk_bs_cpl cpl; 5008 spdk_bs_sequence_t *seq; 5009 struct spdk_bs_load_ctx *ctx; 5010 5011 SPDK_DEBUGLOG(blob, "Destroying blobstore\n"); 5012 5013 if (!RB_EMPTY(&bs->open_blobs)) { 5014 SPDK_ERRLOG("Blobstore still has open blobs\n"); 5015 cb_fn(cb_arg, -EBUSY); 5016 return; 5017 } 5018 5019 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5020 cpl.u.bs_basic.cb_fn = cb_fn; 5021 cpl.u.bs_basic.cb_arg = cb_arg; 5022 5023 ctx = calloc(1, sizeof(*ctx)); 5024 if (!ctx) { 5025 cb_fn(cb_arg, -ENOMEM); 5026 return; 5027 } 5028 5029 ctx->bs = bs; 5030 5031 seq = bs_sequence_start(bs->md_channel, &cpl); 5032 if (!seq) { 5033 free(ctx); 5034 cb_fn(cb_arg, -ENOMEM); 5035 return; 5036 } 5037 5038 /* Write zeroes to the super block */ 5039 bs_sequence_write_zeroes_dev(seq, 5040 bs_page_to_lba(bs, 0), 5041 bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)), 5042 bs_destroy_trim_cpl, ctx); 5043 } 5044 5045 /* END spdk_bs_destroy */ 5046 5047 /* START spdk_bs_unload */ 5048 5049 static void 5050 bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno) 5051 { 5052 spdk_bs_sequence_t *seq = ctx->seq; 5053 5054 spdk_free(ctx->super); 5055 5056 /* 5057 * We need to defer calling bs_call_cpl() until after 5058 * dev destruction, so tuck these away for later use. 5059 */ 5060 ctx->bs->unload_err = bserrno; 5061 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 5062 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 5063 5064 bs_sequence_finish(seq, bserrno); 5065 5066 bs_free(ctx->bs); 5067 free(ctx); 5068 } 5069 5070 static void 5071 bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5072 { 5073 struct spdk_bs_load_ctx *ctx = cb_arg; 5074 5075 bs_unload_finish(ctx, bserrno); 5076 } 5077 5078 static void 5079 bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5080 { 5081 struct spdk_bs_load_ctx *ctx = cb_arg; 5082 5083 spdk_free(ctx->mask); 5084 5085 if (bserrno != 0) { 5086 bs_unload_finish(ctx, bserrno); 5087 return; 5088 } 5089 5090 ctx->super->clean = 1; 5091 5092 bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx); 5093 } 5094 5095 static void 5096 bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5097 { 5098 struct spdk_bs_load_ctx *ctx = cb_arg; 5099 5100 spdk_free(ctx->mask); 5101 ctx->mask = NULL; 5102 5103 if (bserrno != 0) { 5104 bs_unload_finish(ctx, bserrno); 5105 return; 5106 } 5107 5108 bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl); 5109 } 5110 5111 static void 5112 bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5113 { 5114 struct spdk_bs_load_ctx *ctx = cb_arg; 5115 5116 spdk_free(ctx->mask); 5117 ctx->mask = NULL; 5118 5119 if (bserrno != 0) { 5120 bs_unload_finish(ctx, bserrno); 5121 return; 5122 } 5123 5124 bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl); 5125 } 5126 5127 static void 5128 bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5129 { 5130 struct spdk_bs_load_ctx *ctx = cb_arg; 5131 5132 if (bserrno != 0) { 5133 bs_unload_finish(ctx, bserrno); 5134 return; 5135 } 5136 5137 bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl); 5138 } 5139 5140 void 5141 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 5142 { 5143 struct spdk_bs_cpl cpl; 5144 struct spdk_bs_load_ctx *ctx; 5145 5146 SPDK_DEBUGLOG(blob, "Syncing blobstore\n"); 5147 5148 if (!RB_EMPTY(&bs->open_blobs)) { 5149 SPDK_ERRLOG("Blobstore still has open blobs\n"); 5150 cb_fn(cb_arg, -EBUSY); 5151 return; 5152 } 5153 5154 ctx = calloc(1, sizeof(*ctx)); 5155 if (!ctx) { 5156 cb_fn(cb_arg, -ENOMEM); 5157 return; 5158 } 5159 5160 ctx->bs = bs; 5161 5162 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 5163 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 5164 if (!ctx->super) { 5165 free(ctx); 5166 cb_fn(cb_arg, -ENOMEM); 5167 return; 5168 } 5169 5170 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5171 cpl.u.bs_basic.cb_fn = cb_fn; 5172 cpl.u.bs_basic.cb_arg = cb_arg; 5173 5174 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 5175 if (!ctx->seq) { 5176 spdk_free(ctx->super); 5177 free(ctx); 5178 cb_fn(cb_arg, -ENOMEM); 5179 return; 5180 } 5181 5182 /* Read super block */ 5183 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 5184 bs_byte_to_lba(bs, sizeof(*ctx->super)), 5185 bs_unload_read_super_cpl, ctx); 5186 } 5187 5188 /* END spdk_bs_unload */ 5189 5190 /* START spdk_bs_set_super */ 5191 5192 struct spdk_bs_set_super_ctx { 5193 struct spdk_blob_store *bs; 5194 struct spdk_bs_super_block *super; 5195 }; 5196 5197 static void 5198 bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5199 { 5200 struct spdk_bs_set_super_ctx *ctx = cb_arg; 5201 5202 if (bserrno != 0) { 5203 SPDK_ERRLOG("Unable to write to super block of blobstore\n"); 5204 } 5205 5206 spdk_free(ctx->super); 5207 5208 bs_sequence_finish(seq, bserrno); 5209 5210 free(ctx); 5211 } 5212 5213 static void 5214 bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5215 { 5216 struct spdk_bs_set_super_ctx *ctx = cb_arg; 5217 5218 if (bserrno != 0) { 5219 SPDK_ERRLOG("Unable to read super block of blobstore\n"); 5220 spdk_free(ctx->super); 5221 bs_sequence_finish(seq, bserrno); 5222 free(ctx); 5223 return; 5224 } 5225 5226 bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx); 5227 } 5228 5229 void 5230 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 5231 spdk_bs_op_complete cb_fn, void *cb_arg) 5232 { 5233 struct spdk_bs_cpl cpl; 5234 spdk_bs_sequence_t *seq; 5235 struct spdk_bs_set_super_ctx *ctx; 5236 5237 SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n"); 5238 5239 ctx = calloc(1, sizeof(*ctx)); 5240 if (!ctx) { 5241 cb_fn(cb_arg, -ENOMEM); 5242 return; 5243 } 5244 5245 ctx->bs = bs; 5246 5247 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 5248 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 5249 if (!ctx->super) { 5250 free(ctx); 5251 cb_fn(cb_arg, -ENOMEM); 5252 return; 5253 } 5254 5255 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5256 cpl.u.bs_basic.cb_fn = cb_fn; 5257 cpl.u.bs_basic.cb_arg = cb_arg; 5258 5259 seq = bs_sequence_start(bs->md_channel, &cpl); 5260 if (!seq) { 5261 spdk_free(ctx->super); 5262 free(ctx); 5263 cb_fn(cb_arg, -ENOMEM); 5264 return; 5265 } 5266 5267 bs->super_blob = blobid; 5268 5269 /* Read super block */ 5270 bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0), 5271 bs_byte_to_lba(bs, sizeof(*ctx->super)), 5272 bs_set_super_read_cpl, ctx); 5273 } 5274 5275 /* END spdk_bs_set_super */ 5276 5277 void 5278 spdk_bs_get_super(struct spdk_blob_store *bs, 5279 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5280 { 5281 if (bs->super_blob == SPDK_BLOBID_INVALID) { 5282 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT); 5283 } else { 5284 cb_fn(cb_arg, bs->super_blob, 0); 5285 } 5286 } 5287 5288 uint64_t 5289 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 5290 { 5291 return bs->cluster_sz; 5292 } 5293 5294 uint64_t 5295 spdk_bs_get_page_size(struct spdk_blob_store *bs) 5296 { 5297 return SPDK_BS_PAGE_SIZE; 5298 } 5299 5300 uint64_t 5301 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs) 5302 { 5303 return bs->io_unit_size; 5304 } 5305 5306 uint64_t 5307 spdk_bs_free_cluster_count(struct spdk_blob_store *bs) 5308 { 5309 return bs->num_free_clusters; 5310 } 5311 5312 uint64_t 5313 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs) 5314 { 5315 return bs->total_data_clusters; 5316 } 5317 5318 static int 5319 bs_register_md_thread(struct spdk_blob_store *bs) 5320 { 5321 bs->md_channel = spdk_get_io_channel(bs); 5322 if (!bs->md_channel) { 5323 SPDK_ERRLOG("Failed to get IO channel.\n"); 5324 return -1; 5325 } 5326 5327 return 0; 5328 } 5329 5330 static int 5331 bs_unregister_md_thread(struct spdk_blob_store *bs) 5332 { 5333 spdk_put_io_channel(bs->md_channel); 5334 5335 return 0; 5336 } 5337 5338 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob) 5339 { 5340 assert(blob != NULL); 5341 5342 return blob->id; 5343 } 5344 5345 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob) 5346 { 5347 assert(blob != NULL); 5348 5349 return bs_cluster_to_page(blob->bs, blob->active.num_clusters); 5350 } 5351 5352 uint64_t spdk_blob_get_num_io_units(struct spdk_blob *blob) 5353 { 5354 assert(blob != NULL); 5355 5356 return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs); 5357 } 5358 5359 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob) 5360 { 5361 assert(blob != NULL); 5362 5363 return blob->active.num_clusters; 5364 } 5365 5366 /* START spdk_bs_create_blob */ 5367 5368 static void 5369 bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5370 { 5371 struct spdk_blob *blob = cb_arg; 5372 uint32_t page_idx = bs_blobid_to_page(blob->id); 5373 5374 if (bserrno != 0) { 5375 spdk_bit_array_clear(blob->bs->used_blobids, page_idx); 5376 bs_release_md_page(blob->bs, page_idx); 5377 } 5378 5379 blob_free(blob); 5380 5381 bs_sequence_finish(seq, bserrno); 5382 } 5383 5384 static int 5385 blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs, 5386 bool internal) 5387 { 5388 uint64_t i; 5389 size_t value_len = 0; 5390 int rc; 5391 const void *value = NULL; 5392 if (xattrs->count > 0 && xattrs->get_value == NULL) { 5393 return -EINVAL; 5394 } 5395 for (i = 0; i < xattrs->count; i++) { 5396 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len); 5397 if (value == NULL || value_len == 0) { 5398 return -EINVAL; 5399 } 5400 rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal); 5401 if (rc < 0) { 5402 return rc; 5403 } 5404 } 5405 return 0; 5406 } 5407 5408 static void 5409 blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst) 5410 { 5411 #define FIELD_OK(field) \ 5412 offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size 5413 5414 #define SET_FIELD(field) \ 5415 if (FIELD_OK(field)) { \ 5416 dst->field = src->field; \ 5417 } \ 5418 5419 SET_FIELD(num_clusters); 5420 SET_FIELD(thin_provision); 5421 SET_FIELD(clear_method); 5422 5423 if (FIELD_OK(xattrs)) { 5424 memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs)); 5425 } 5426 5427 SET_FIELD(use_extent_table); 5428 5429 dst->opts_size = src->opts_size; 5430 5431 /* You should not remove this statement, but need to update the assert statement 5432 * if you add a new field, and also add a corresponding SET_FIELD statement */ 5433 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 64, "Incorrect size"); 5434 5435 #undef FIELD_OK 5436 #undef SET_FIELD 5437 } 5438 5439 static void 5440 bs_create_blob(struct spdk_blob_store *bs, 5441 const struct spdk_blob_opts *opts, 5442 const struct spdk_blob_xattr_opts *internal_xattrs, 5443 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5444 { 5445 struct spdk_blob *blob; 5446 uint32_t page_idx; 5447 struct spdk_bs_cpl cpl; 5448 struct spdk_blob_opts opts_local; 5449 struct spdk_blob_xattr_opts internal_xattrs_default; 5450 spdk_bs_sequence_t *seq; 5451 spdk_blob_id id; 5452 int rc; 5453 5454 assert(spdk_get_thread() == bs->md_thread); 5455 5456 page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0); 5457 if (page_idx == UINT32_MAX) { 5458 cb_fn(cb_arg, 0, -ENOMEM); 5459 return; 5460 } 5461 spdk_bit_array_set(bs->used_blobids, page_idx); 5462 bs_claim_md_page(bs, page_idx); 5463 5464 id = bs_page_to_blobid(page_idx); 5465 5466 SPDK_DEBUGLOG(blob, "Creating blob with id %" PRIu64 " at page %u\n", id, page_idx); 5467 5468 blob = blob_alloc(bs, id); 5469 if (!blob) { 5470 spdk_bit_array_clear(bs->used_blobids, page_idx); 5471 bs_release_md_page(bs, page_idx); 5472 cb_fn(cb_arg, 0, -ENOMEM); 5473 return; 5474 } 5475 5476 spdk_blob_opts_init(&opts_local, sizeof(opts_local)); 5477 if (opts) { 5478 blob_opts_copy(opts, &opts_local); 5479 } 5480 5481 blob->use_extent_table = opts_local.use_extent_table; 5482 if (blob->use_extent_table) { 5483 blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE; 5484 } 5485 5486 if (!internal_xattrs) { 5487 blob_xattrs_init(&internal_xattrs_default); 5488 internal_xattrs = &internal_xattrs_default; 5489 } 5490 5491 rc = blob_set_xattrs(blob, &opts_local.xattrs, false); 5492 if (rc < 0) { 5493 blob_free(blob); 5494 spdk_bit_array_clear(bs->used_blobids, page_idx); 5495 bs_release_md_page(bs, page_idx); 5496 cb_fn(cb_arg, 0, rc); 5497 return; 5498 } 5499 5500 rc = blob_set_xattrs(blob, internal_xattrs, true); 5501 if (rc < 0) { 5502 blob_free(blob); 5503 spdk_bit_array_clear(bs->used_blobids, page_idx); 5504 bs_release_md_page(bs, page_idx); 5505 cb_fn(cb_arg, 0, rc); 5506 return; 5507 } 5508 5509 if (opts_local.thin_provision) { 5510 blob_set_thin_provision(blob); 5511 } 5512 5513 blob_set_clear_method(blob, opts_local.clear_method); 5514 5515 rc = blob_resize(blob, opts_local.num_clusters); 5516 if (rc < 0) { 5517 blob_free(blob); 5518 spdk_bit_array_clear(bs->used_blobids, page_idx); 5519 bs_release_md_page(bs, page_idx); 5520 cb_fn(cb_arg, 0, rc); 5521 return; 5522 } 5523 cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 5524 cpl.u.blobid.cb_fn = cb_fn; 5525 cpl.u.blobid.cb_arg = cb_arg; 5526 cpl.u.blobid.blobid = blob->id; 5527 5528 seq = bs_sequence_start(bs->md_channel, &cpl); 5529 if (!seq) { 5530 blob_free(blob); 5531 spdk_bit_array_clear(bs->used_blobids, page_idx); 5532 bs_release_md_page(bs, page_idx); 5533 cb_fn(cb_arg, 0, -ENOMEM); 5534 return; 5535 } 5536 5537 blob_persist(seq, blob, bs_create_blob_cpl, blob); 5538 } 5539 5540 void spdk_bs_create_blob(struct spdk_blob_store *bs, 5541 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5542 { 5543 bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg); 5544 } 5545 5546 void spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts, 5547 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5548 { 5549 bs_create_blob(bs, opts, NULL, cb_fn, cb_arg); 5550 } 5551 5552 /* END spdk_bs_create_blob */ 5553 5554 /* START blob_cleanup */ 5555 5556 struct spdk_clone_snapshot_ctx { 5557 struct spdk_bs_cpl cpl; 5558 int bserrno; 5559 bool frozen; 5560 5561 struct spdk_io_channel *channel; 5562 5563 /* Current cluster for inflate operation */ 5564 uint64_t cluster; 5565 5566 /* For inflation force allocation of all unallocated clusters and remove 5567 * thin-provisioning. Otherwise only decouple parent and keep clone thin. */ 5568 bool allocate_all; 5569 5570 struct { 5571 spdk_blob_id id; 5572 struct spdk_blob *blob; 5573 bool md_ro; 5574 } original; 5575 struct { 5576 spdk_blob_id id; 5577 struct spdk_blob *blob; 5578 } new; 5579 5580 /* xattrs specified for snapshot/clones only. They have no impact on 5581 * the original blobs xattrs. */ 5582 const struct spdk_blob_xattr_opts *xattrs; 5583 }; 5584 5585 static void 5586 bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno) 5587 { 5588 struct spdk_clone_snapshot_ctx *ctx = cb_arg; 5589 struct spdk_bs_cpl *cpl = &ctx->cpl; 5590 5591 if (bserrno != 0) { 5592 if (ctx->bserrno != 0) { 5593 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5594 } else { 5595 ctx->bserrno = bserrno; 5596 } 5597 } 5598 5599 switch (cpl->type) { 5600 case SPDK_BS_CPL_TYPE_BLOBID: 5601 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno); 5602 break; 5603 case SPDK_BS_CPL_TYPE_BLOB_BASIC: 5604 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno); 5605 break; 5606 default: 5607 SPDK_UNREACHABLE(); 5608 break; 5609 } 5610 5611 free(ctx); 5612 } 5613 5614 static void 5615 bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 5616 { 5617 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5618 struct spdk_blob *origblob = ctx->original.blob; 5619 5620 if (bserrno != 0) { 5621 if (ctx->bserrno != 0) { 5622 SPDK_ERRLOG("Unfreeze error %d\n", bserrno); 5623 } else { 5624 ctx->bserrno = bserrno; 5625 } 5626 } 5627 5628 ctx->original.id = origblob->id; 5629 origblob->locked_operation_in_progress = false; 5630 5631 /* Revert md_ro to original state */ 5632 origblob->md_ro = ctx->original.md_ro; 5633 5634 spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx); 5635 } 5636 5637 static void 5638 bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno) 5639 { 5640 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5641 struct spdk_blob *origblob = ctx->original.blob; 5642 5643 if (bserrno != 0) { 5644 if (ctx->bserrno != 0) { 5645 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5646 } else { 5647 ctx->bserrno = bserrno; 5648 } 5649 } 5650 5651 if (ctx->frozen) { 5652 /* Unfreeze any outstanding I/O */ 5653 blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx); 5654 } else { 5655 bs_snapshot_unfreeze_cpl(ctx, 0); 5656 } 5657 5658 } 5659 5660 static void 5661 bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno) 5662 { 5663 struct spdk_blob *newblob = ctx->new.blob; 5664 5665 if (bserrno != 0) { 5666 if (ctx->bserrno != 0) { 5667 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5668 } else { 5669 ctx->bserrno = bserrno; 5670 } 5671 } 5672 5673 ctx->new.id = newblob->id; 5674 spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx); 5675 } 5676 5677 /* END blob_cleanup */ 5678 5679 /* START spdk_bs_create_snapshot */ 5680 5681 static void 5682 bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2) 5683 { 5684 uint64_t *cluster_temp; 5685 uint32_t *extent_page_temp; 5686 5687 cluster_temp = blob1->active.clusters; 5688 blob1->active.clusters = blob2->active.clusters; 5689 blob2->active.clusters = cluster_temp; 5690 5691 extent_page_temp = blob1->active.extent_pages; 5692 blob1->active.extent_pages = blob2->active.extent_pages; 5693 blob2->active.extent_pages = extent_page_temp; 5694 } 5695 5696 static void 5697 bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno) 5698 { 5699 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5700 struct spdk_blob *origblob = ctx->original.blob; 5701 struct spdk_blob *newblob = ctx->new.blob; 5702 5703 if (bserrno != 0) { 5704 bs_snapshot_swap_cluster_maps(newblob, origblob); 5705 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5706 return; 5707 } 5708 5709 /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */ 5710 bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true); 5711 if (bserrno != 0) { 5712 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5713 return; 5714 } 5715 5716 bs_blob_list_add(ctx->original.blob); 5717 5718 spdk_blob_set_read_only(newblob); 5719 5720 /* sync snapshot metadata */ 5721 spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx); 5722 } 5723 5724 static void 5725 bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno) 5726 { 5727 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5728 struct spdk_blob *origblob = ctx->original.blob; 5729 struct spdk_blob *newblob = ctx->new.blob; 5730 5731 if (bserrno != 0) { 5732 /* return cluster map back to original */ 5733 bs_snapshot_swap_cluster_maps(newblob, origblob); 5734 5735 /* Newblob md sync failed. Valid clusters are only present in origblob. 5736 * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred. 5737 * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */ 5738 blob_set_thin_provision(newblob); 5739 assert(spdk_mem_all_zero(newblob->active.clusters, 5740 newblob->active.num_clusters * sizeof(*newblob->active.clusters))); 5741 assert(spdk_mem_all_zero(newblob->active.extent_pages, 5742 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages))); 5743 5744 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 5745 return; 5746 } 5747 5748 /* Set internal xattr for snapshot id */ 5749 bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true); 5750 if (bserrno != 0) { 5751 /* return cluster map back to original */ 5752 bs_snapshot_swap_cluster_maps(newblob, origblob); 5753 blob_set_thin_provision(newblob); 5754 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 5755 return; 5756 } 5757 5758 /* Create new back_bs_dev for snapshot */ 5759 origblob->back_bs_dev = bs_create_blob_bs_dev(newblob); 5760 if (origblob->back_bs_dev == NULL) { 5761 /* return cluster map back to original */ 5762 bs_snapshot_swap_cluster_maps(newblob, origblob); 5763 blob_set_thin_provision(newblob); 5764 bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL); 5765 return; 5766 } 5767 5768 bs_blob_list_remove(origblob); 5769 origblob->parent_id = newblob->id; 5770 /* set clone blob as thin provisioned */ 5771 blob_set_thin_provision(origblob); 5772 5773 bs_blob_list_add(newblob); 5774 5775 /* sync clone metadata */ 5776 spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx); 5777 } 5778 5779 static void 5780 bs_snapshot_freeze_cpl(void *cb_arg, int rc) 5781 { 5782 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5783 struct spdk_blob *origblob = ctx->original.blob; 5784 struct spdk_blob *newblob = ctx->new.blob; 5785 int bserrno; 5786 5787 if (rc != 0) { 5788 bs_clone_snapshot_newblob_cleanup(ctx, rc); 5789 return; 5790 } 5791 5792 ctx->frozen = true; 5793 5794 /* set new back_bs_dev for snapshot */ 5795 newblob->back_bs_dev = origblob->back_bs_dev; 5796 /* Set invalid flags from origblob */ 5797 newblob->invalid_flags = origblob->invalid_flags; 5798 5799 /* inherit parent from original blob if set */ 5800 newblob->parent_id = origblob->parent_id; 5801 if (origblob->parent_id != SPDK_BLOBID_INVALID) { 5802 /* Set internal xattr for snapshot id */ 5803 bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT, 5804 &origblob->parent_id, sizeof(spdk_blob_id), true); 5805 if (bserrno != 0) { 5806 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 5807 return; 5808 } 5809 } 5810 5811 /* swap cluster maps */ 5812 bs_snapshot_swap_cluster_maps(newblob, origblob); 5813 5814 /* Set the clear method on the new blob to match the original. */ 5815 blob_set_clear_method(newblob, origblob->clear_method); 5816 5817 /* sync snapshot metadata */ 5818 spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx); 5819 } 5820 5821 static void 5822 bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5823 { 5824 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5825 struct spdk_blob *origblob = ctx->original.blob; 5826 struct spdk_blob *newblob = _blob; 5827 5828 if (bserrno != 0) { 5829 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5830 return; 5831 } 5832 5833 ctx->new.blob = newblob; 5834 assert(spdk_blob_is_thin_provisioned(newblob)); 5835 assert(spdk_mem_all_zero(newblob->active.clusters, 5836 newblob->active.num_clusters * sizeof(*newblob->active.clusters))); 5837 assert(spdk_mem_all_zero(newblob->active.extent_pages, 5838 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages))); 5839 5840 blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx); 5841 } 5842 5843 static void 5844 bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 5845 { 5846 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5847 struct spdk_blob *origblob = ctx->original.blob; 5848 5849 if (bserrno != 0) { 5850 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5851 return; 5852 } 5853 5854 ctx->new.id = blobid; 5855 ctx->cpl.u.blobid.blobid = blobid; 5856 5857 spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx); 5858 } 5859 5860 5861 static void 5862 bs_xattr_snapshot(void *arg, const char *name, 5863 const void **value, size_t *value_len) 5864 { 5865 assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0); 5866 5867 struct spdk_blob *blob = (struct spdk_blob *)arg; 5868 *value = &blob->id; 5869 *value_len = sizeof(blob->id); 5870 } 5871 5872 static void 5873 bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5874 { 5875 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5876 struct spdk_blob_opts opts; 5877 struct spdk_blob_xattr_opts internal_xattrs; 5878 char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS }; 5879 5880 if (bserrno != 0) { 5881 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 5882 return; 5883 } 5884 5885 ctx->original.blob = _blob; 5886 5887 if (_blob->data_ro || _blob->md_ro) { 5888 SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id %" PRIu64 "\n", 5889 _blob->id); 5890 ctx->bserrno = -EINVAL; 5891 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 5892 return; 5893 } 5894 5895 if (_blob->locked_operation_in_progress) { 5896 SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n"); 5897 ctx->bserrno = -EBUSY; 5898 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 5899 return; 5900 } 5901 5902 _blob->locked_operation_in_progress = true; 5903 5904 spdk_blob_opts_init(&opts, sizeof(opts)); 5905 blob_xattrs_init(&internal_xattrs); 5906 5907 /* Change the size of new blob to the same as in original blob, 5908 * but do not allocate clusters */ 5909 opts.thin_provision = true; 5910 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 5911 opts.use_extent_table = _blob->use_extent_table; 5912 5913 /* If there are any xattrs specified for snapshot, set them now */ 5914 if (ctx->xattrs) { 5915 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 5916 } 5917 /* Set internal xattr SNAPSHOT_IN_PROGRESS */ 5918 internal_xattrs.count = 1; 5919 internal_xattrs.ctx = _blob; 5920 internal_xattrs.names = xattrs_names; 5921 internal_xattrs.get_value = bs_xattr_snapshot; 5922 5923 bs_create_blob(_blob->bs, &opts, &internal_xattrs, 5924 bs_snapshot_newblob_create_cpl, ctx); 5925 } 5926 5927 void spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid, 5928 const struct spdk_blob_xattr_opts *snapshot_xattrs, 5929 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5930 { 5931 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 5932 5933 if (!ctx) { 5934 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 5935 return; 5936 } 5937 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 5938 ctx->cpl.u.blobid.cb_fn = cb_fn; 5939 ctx->cpl.u.blobid.cb_arg = cb_arg; 5940 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 5941 ctx->bserrno = 0; 5942 ctx->frozen = false; 5943 ctx->original.id = blobid; 5944 ctx->xattrs = snapshot_xattrs; 5945 5946 spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx); 5947 } 5948 /* END spdk_bs_create_snapshot */ 5949 5950 /* START spdk_bs_create_clone */ 5951 5952 static void 5953 bs_xattr_clone(void *arg, const char *name, 5954 const void **value, size_t *value_len) 5955 { 5956 assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0); 5957 5958 struct spdk_blob *blob = (struct spdk_blob *)arg; 5959 *value = &blob->id; 5960 *value_len = sizeof(blob->id); 5961 } 5962 5963 static void 5964 bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5965 { 5966 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5967 struct spdk_blob *clone = _blob; 5968 5969 ctx->new.blob = clone; 5970 bs_blob_list_add(clone); 5971 5972 spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx); 5973 } 5974 5975 static void 5976 bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 5977 { 5978 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5979 5980 ctx->cpl.u.blobid.blobid = blobid; 5981 spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx); 5982 } 5983 5984 static void 5985 bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5986 { 5987 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5988 struct spdk_blob_opts opts; 5989 struct spdk_blob_xattr_opts internal_xattrs; 5990 char *xattr_names[] = { BLOB_SNAPSHOT }; 5991 5992 if (bserrno != 0) { 5993 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 5994 return; 5995 } 5996 5997 ctx->original.blob = _blob; 5998 ctx->original.md_ro = _blob->md_ro; 5999 6000 if (!_blob->data_ro || !_blob->md_ro) { 6001 SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n"); 6002 ctx->bserrno = -EINVAL; 6003 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6004 return; 6005 } 6006 6007 if (_blob->locked_operation_in_progress) { 6008 SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n"); 6009 ctx->bserrno = -EBUSY; 6010 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6011 return; 6012 } 6013 6014 _blob->locked_operation_in_progress = true; 6015 6016 spdk_blob_opts_init(&opts, sizeof(opts)); 6017 blob_xattrs_init(&internal_xattrs); 6018 6019 opts.thin_provision = true; 6020 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 6021 opts.use_extent_table = _blob->use_extent_table; 6022 if (ctx->xattrs) { 6023 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 6024 } 6025 6026 /* Set internal xattr BLOB_SNAPSHOT */ 6027 internal_xattrs.count = 1; 6028 internal_xattrs.ctx = _blob; 6029 internal_xattrs.names = xattr_names; 6030 internal_xattrs.get_value = bs_xattr_clone; 6031 6032 bs_create_blob(_blob->bs, &opts, &internal_xattrs, 6033 bs_clone_newblob_create_cpl, ctx); 6034 } 6035 6036 void spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid, 6037 const struct spdk_blob_xattr_opts *clone_xattrs, 6038 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 6039 { 6040 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6041 6042 if (!ctx) { 6043 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 6044 return; 6045 } 6046 6047 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 6048 ctx->cpl.u.blobid.cb_fn = cb_fn; 6049 ctx->cpl.u.blobid.cb_arg = cb_arg; 6050 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 6051 ctx->bserrno = 0; 6052 ctx->xattrs = clone_xattrs; 6053 ctx->original.id = blobid; 6054 6055 spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx); 6056 } 6057 6058 /* END spdk_bs_create_clone */ 6059 6060 /* START spdk_bs_inflate_blob */ 6061 6062 static void 6063 bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno) 6064 { 6065 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6066 struct spdk_blob *_blob = ctx->original.blob; 6067 6068 if (bserrno != 0) { 6069 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6070 return; 6071 } 6072 6073 /* Temporarily override md_ro flag for MD modification */ 6074 _blob->md_ro = false; 6075 6076 bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true); 6077 if (bserrno != 0) { 6078 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6079 return; 6080 } 6081 6082 assert(_parent != NULL); 6083 6084 bs_blob_list_remove(_blob); 6085 _blob->parent_id = _parent->id; 6086 6087 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6088 _blob->back_bs_dev = bs_create_blob_bs_dev(_parent); 6089 bs_blob_list_add(_blob); 6090 6091 spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx); 6092 } 6093 6094 static void 6095 bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx) 6096 { 6097 struct spdk_blob *_blob = ctx->original.blob; 6098 struct spdk_blob *_parent; 6099 6100 if (ctx->allocate_all) { 6101 /* remove thin provisioning */ 6102 bs_blob_list_remove(_blob); 6103 blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 6104 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV; 6105 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6106 _blob->back_bs_dev = NULL; 6107 _blob->parent_id = SPDK_BLOBID_INVALID; 6108 } else { 6109 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob; 6110 if (_parent->parent_id != SPDK_BLOBID_INVALID) { 6111 /* We must change the parent of the inflated blob */ 6112 spdk_bs_open_blob(_blob->bs, _parent->parent_id, 6113 bs_inflate_blob_set_parent_cpl, ctx); 6114 return; 6115 } 6116 6117 bs_blob_list_remove(_blob); 6118 blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 6119 _blob->parent_id = SPDK_BLOBID_INVALID; 6120 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6121 _blob->back_bs_dev = bs_create_zeroes_dev(); 6122 } 6123 6124 /* Temporarily override md_ro flag for MD modification */ 6125 _blob->md_ro = false; 6126 _blob->state = SPDK_BLOB_STATE_DIRTY; 6127 6128 spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx); 6129 } 6130 6131 /* Check if cluster needs allocation */ 6132 static inline bool 6133 bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all) 6134 { 6135 struct spdk_blob_bs_dev *b; 6136 6137 assert(blob != NULL); 6138 6139 if (blob->active.clusters[cluster] != 0) { 6140 /* Cluster is already allocated */ 6141 return false; 6142 } 6143 6144 if (blob->parent_id == SPDK_BLOBID_INVALID) { 6145 /* Blob have no parent blob */ 6146 return allocate_all; 6147 } 6148 6149 b = (struct spdk_blob_bs_dev *)blob->back_bs_dev; 6150 return (allocate_all || b->blob->active.clusters[cluster] != 0); 6151 } 6152 6153 static void 6154 bs_inflate_blob_touch_next(void *cb_arg, int bserrno) 6155 { 6156 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6157 struct spdk_blob *_blob = ctx->original.blob; 6158 struct spdk_bs_cpl cpl; 6159 spdk_bs_user_op_t *op; 6160 uint64_t offset; 6161 6162 if (bserrno != 0) { 6163 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6164 return; 6165 } 6166 6167 for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) { 6168 if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) { 6169 break; 6170 } 6171 } 6172 6173 if (ctx->cluster < _blob->active.num_clusters) { 6174 offset = bs_cluster_to_lba(_blob->bs, ctx->cluster); 6175 6176 /* We may safely increment a cluster before copying */ 6177 ctx->cluster++; 6178 6179 /* Use a dummy 0B read as a context for cluster copy */ 6180 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6181 cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next; 6182 cpl.u.blob_basic.cb_arg = ctx; 6183 6184 op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob, 6185 NULL, 0, offset, 0); 6186 if (!op) { 6187 bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM); 6188 return; 6189 } 6190 6191 bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op); 6192 } else { 6193 bs_inflate_blob_done(ctx); 6194 } 6195 } 6196 6197 static void 6198 bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6199 { 6200 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6201 uint64_t clusters_needed; 6202 uint64_t i; 6203 6204 if (bserrno != 0) { 6205 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 6206 return; 6207 } 6208 6209 ctx->original.blob = _blob; 6210 ctx->original.md_ro = _blob->md_ro; 6211 6212 if (_blob->locked_operation_in_progress) { 6213 SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n"); 6214 ctx->bserrno = -EBUSY; 6215 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6216 return; 6217 } 6218 6219 _blob->locked_operation_in_progress = true; 6220 6221 if (!ctx->allocate_all && _blob->parent_id == SPDK_BLOBID_INVALID) { 6222 /* This blob have no parent, so we cannot decouple it. */ 6223 SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n"); 6224 bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 6225 return; 6226 } 6227 6228 if (spdk_blob_is_thin_provisioned(_blob) == false) { 6229 /* This is not thin provisioned blob. No need to inflate. */ 6230 bs_clone_snapshot_origblob_cleanup(ctx, 0); 6231 return; 6232 } 6233 6234 /* Do two passes - one to verify that we can obtain enough clusters 6235 * and another to actually claim them. 6236 */ 6237 clusters_needed = 0; 6238 for (i = 0; i < _blob->active.num_clusters; i++) { 6239 if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) { 6240 clusters_needed++; 6241 } 6242 } 6243 6244 if (clusters_needed > _blob->bs->num_free_clusters) { 6245 /* Not enough free clusters. Cannot satisfy the request. */ 6246 bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC); 6247 return; 6248 } 6249 6250 ctx->cluster = 0; 6251 bs_inflate_blob_touch_next(ctx, 0); 6252 } 6253 6254 static void 6255 bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6256 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg) 6257 { 6258 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6259 6260 if (!ctx) { 6261 cb_fn(cb_arg, -ENOMEM); 6262 return; 6263 } 6264 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6265 ctx->cpl.u.bs_basic.cb_fn = cb_fn; 6266 ctx->cpl.u.bs_basic.cb_arg = cb_arg; 6267 ctx->bserrno = 0; 6268 ctx->original.id = blobid; 6269 ctx->channel = channel; 6270 ctx->allocate_all = allocate_all; 6271 6272 spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx); 6273 } 6274 6275 void 6276 spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6277 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 6278 { 6279 bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg); 6280 } 6281 6282 void 6283 spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6284 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 6285 { 6286 bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg); 6287 } 6288 /* END spdk_bs_inflate_blob */ 6289 6290 /* START spdk_blob_resize */ 6291 struct spdk_bs_resize_ctx { 6292 spdk_blob_op_complete cb_fn; 6293 void *cb_arg; 6294 struct spdk_blob *blob; 6295 uint64_t sz; 6296 int rc; 6297 }; 6298 6299 static void 6300 bs_resize_unfreeze_cpl(void *cb_arg, int rc) 6301 { 6302 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 6303 6304 if (rc != 0) { 6305 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc); 6306 } 6307 6308 if (ctx->rc != 0) { 6309 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc); 6310 rc = ctx->rc; 6311 } 6312 6313 ctx->blob->locked_operation_in_progress = false; 6314 6315 ctx->cb_fn(ctx->cb_arg, rc); 6316 free(ctx); 6317 } 6318 6319 static void 6320 bs_resize_freeze_cpl(void *cb_arg, int rc) 6321 { 6322 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 6323 6324 if (rc != 0) { 6325 ctx->blob->locked_operation_in_progress = false; 6326 ctx->cb_fn(ctx->cb_arg, rc); 6327 free(ctx); 6328 return; 6329 } 6330 6331 ctx->rc = blob_resize(ctx->blob, ctx->sz); 6332 6333 blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx); 6334 } 6335 6336 void 6337 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg) 6338 { 6339 struct spdk_bs_resize_ctx *ctx; 6340 6341 blob_verify_md_op(blob); 6342 6343 SPDK_DEBUGLOG(blob, "Resizing blob %" PRIu64 " to %" PRIu64 " clusters\n", blob->id, sz); 6344 6345 if (blob->md_ro) { 6346 cb_fn(cb_arg, -EPERM); 6347 return; 6348 } 6349 6350 if (sz == blob->active.num_clusters) { 6351 cb_fn(cb_arg, 0); 6352 return; 6353 } 6354 6355 if (blob->locked_operation_in_progress) { 6356 cb_fn(cb_arg, -EBUSY); 6357 return; 6358 } 6359 6360 ctx = calloc(1, sizeof(*ctx)); 6361 if (!ctx) { 6362 cb_fn(cb_arg, -ENOMEM); 6363 return; 6364 } 6365 6366 blob->locked_operation_in_progress = true; 6367 ctx->cb_fn = cb_fn; 6368 ctx->cb_arg = cb_arg; 6369 ctx->blob = blob; 6370 ctx->sz = sz; 6371 blob_freeze_io(blob, bs_resize_freeze_cpl, ctx); 6372 } 6373 6374 /* END spdk_blob_resize */ 6375 6376 6377 /* START spdk_bs_delete_blob */ 6378 6379 static void 6380 bs_delete_close_cpl(void *cb_arg, int bserrno) 6381 { 6382 spdk_bs_sequence_t *seq = cb_arg; 6383 6384 bs_sequence_finish(seq, bserrno); 6385 } 6386 6387 static void 6388 bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 6389 { 6390 struct spdk_blob *blob = cb_arg; 6391 6392 if (bserrno != 0) { 6393 /* 6394 * We already removed this blob from the blobstore tailq, so 6395 * we need to free it here since this is the last reference 6396 * to it. 6397 */ 6398 blob_free(blob); 6399 bs_delete_close_cpl(seq, bserrno); 6400 return; 6401 } 6402 6403 /* 6404 * This will immediately decrement the ref_count and call 6405 * the completion routine since the metadata state is clean. 6406 * By calling spdk_blob_close, we reduce the number of call 6407 * points into code that touches the blob->open_ref count 6408 * and the blobstore's blob list. 6409 */ 6410 spdk_blob_close(blob, bs_delete_close_cpl, seq); 6411 } 6412 6413 struct delete_snapshot_ctx { 6414 struct spdk_blob_list *parent_snapshot_entry; 6415 struct spdk_blob *snapshot; 6416 bool snapshot_md_ro; 6417 struct spdk_blob *clone; 6418 bool clone_md_ro; 6419 spdk_blob_op_with_handle_complete cb_fn; 6420 void *cb_arg; 6421 int bserrno; 6422 uint32_t next_extent_page; 6423 }; 6424 6425 static void 6426 delete_blob_cleanup_finish(void *cb_arg, int bserrno) 6427 { 6428 struct delete_snapshot_ctx *ctx = cb_arg; 6429 6430 if (bserrno != 0) { 6431 SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno); 6432 } 6433 6434 assert(ctx != NULL); 6435 6436 if (bserrno != 0 && ctx->bserrno == 0) { 6437 ctx->bserrno = bserrno; 6438 } 6439 6440 ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno); 6441 free(ctx); 6442 } 6443 6444 static void 6445 delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno) 6446 { 6447 struct delete_snapshot_ctx *ctx = cb_arg; 6448 6449 if (bserrno != 0) { 6450 ctx->bserrno = bserrno; 6451 SPDK_ERRLOG("Clone cleanup error %d\n", bserrno); 6452 } 6453 6454 if (ctx->bserrno != 0) { 6455 assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL); 6456 RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot); 6457 spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id); 6458 } 6459 6460 ctx->snapshot->locked_operation_in_progress = false; 6461 ctx->snapshot->md_ro = ctx->snapshot_md_ro; 6462 6463 spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx); 6464 } 6465 6466 static void 6467 delete_snapshot_cleanup_clone(void *cb_arg, int bserrno) 6468 { 6469 struct delete_snapshot_ctx *ctx = cb_arg; 6470 6471 ctx->clone->locked_operation_in_progress = false; 6472 ctx->clone->md_ro = ctx->clone_md_ro; 6473 6474 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx); 6475 } 6476 6477 static void 6478 delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 6479 { 6480 struct delete_snapshot_ctx *ctx = cb_arg; 6481 6482 if (bserrno) { 6483 ctx->bserrno = bserrno; 6484 delete_snapshot_cleanup_clone(ctx, 0); 6485 return; 6486 } 6487 6488 ctx->clone->locked_operation_in_progress = false; 6489 spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx); 6490 } 6491 6492 static void 6493 delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno) 6494 { 6495 struct delete_snapshot_ctx *ctx = cb_arg; 6496 struct spdk_blob_list *parent_snapshot_entry = NULL; 6497 struct spdk_blob_list *snapshot_entry = NULL; 6498 struct spdk_blob_list *clone_entry = NULL; 6499 struct spdk_blob_list *snapshot_clone_entry = NULL; 6500 6501 if (bserrno) { 6502 SPDK_ERRLOG("Failed to sync MD on blob\n"); 6503 ctx->bserrno = bserrno; 6504 delete_snapshot_cleanup_clone(ctx, 0); 6505 return; 6506 } 6507 6508 /* Get snapshot entry for the snapshot we want to remove */ 6509 snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id); 6510 6511 assert(snapshot_entry != NULL); 6512 6513 /* Remove clone entry in this snapshot (at this point there can be only one clone) */ 6514 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6515 assert(clone_entry != NULL); 6516 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 6517 snapshot_entry->clone_count--; 6518 assert(TAILQ_EMPTY(&snapshot_entry->clones)); 6519 6520 if (ctx->snapshot->parent_id != SPDK_BLOBID_INVALID) { 6521 /* This snapshot is at the same time a clone of another snapshot - we need to 6522 * update parent snapshot (remove current clone, add new one inherited from 6523 * the snapshot that is being removed) */ 6524 6525 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for 6526 * snapshot that we are removing */ 6527 blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry, 6528 &snapshot_clone_entry); 6529 6530 /* Switch clone entry in parent snapshot */ 6531 TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link); 6532 TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link); 6533 free(snapshot_clone_entry); 6534 } else { 6535 /* No parent snapshot - just remove clone entry */ 6536 free(clone_entry); 6537 } 6538 6539 /* Restore md_ro flags */ 6540 ctx->clone->md_ro = ctx->clone_md_ro; 6541 ctx->snapshot->md_ro = ctx->snapshot_md_ro; 6542 6543 blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx); 6544 } 6545 6546 static void 6547 delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno) 6548 { 6549 struct delete_snapshot_ctx *ctx = cb_arg; 6550 uint64_t i; 6551 6552 ctx->snapshot->md_ro = false; 6553 6554 if (bserrno) { 6555 SPDK_ERRLOG("Failed to sync MD on clone\n"); 6556 ctx->bserrno = bserrno; 6557 6558 /* Restore snapshot to previous state */ 6559 bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true); 6560 if (bserrno != 0) { 6561 delete_snapshot_cleanup_clone(ctx, bserrno); 6562 return; 6563 } 6564 6565 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx); 6566 return; 6567 } 6568 6569 /* Clear cluster map entries for snapshot */ 6570 for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) { 6571 if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) { 6572 ctx->snapshot->active.clusters[i] = 0; 6573 } 6574 } 6575 for (i = 0; i < ctx->snapshot->active.num_extent_pages && 6576 i < ctx->clone->active.num_extent_pages; i++) { 6577 if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) { 6578 ctx->snapshot->active.extent_pages[i] = 0; 6579 } 6580 } 6581 6582 blob_set_thin_provision(ctx->snapshot); 6583 ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY; 6584 6585 if (ctx->parent_snapshot_entry != NULL) { 6586 ctx->snapshot->back_bs_dev = NULL; 6587 } 6588 6589 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx); 6590 } 6591 6592 static void 6593 delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx) 6594 { 6595 /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */ 6596 ctx->clone->back_bs_dev->destroy(ctx->clone->back_bs_dev); 6597 6598 /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */ 6599 if (ctx->parent_snapshot_entry != NULL) { 6600 /* ...to parent snapshot */ 6601 ctx->clone->parent_id = ctx->parent_snapshot_entry->id; 6602 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev; 6603 blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id, 6604 sizeof(spdk_blob_id), 6605 true); 6606 } else { 6607 /* ...to blobid invalid and zeroes dev */ 6608 ctx->clone->parent_id = SPDK_BLOBID_INVALID; 6609 ctx->clone->back_bs_dev = bs_create_zeroes_dev(); 6610 blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true); 6611 } 6612 6613 spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx); 6614 } 6615 6616 static void 6617 delete_snapshot_update_extent_pages(void *cb_arg, int bserrno) 6618 { 6619 struct delete_snapshot_ctx *ctx = cb_arg; 6620 uint32_t *extent_page; 6621 uint64_t i; 6622 6623 for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages && 6624 i < ctx->clone->active.num_extent_pages; i++) { 6625 if (ctx->snapshot->active.extent_pages[i] == 0) { 6626 /* No extent page to use from snapshot */ 6627 continue; 6628 } 6629 6630 extent_page = &ctx->clone->active.extent_pages[i]; 6631 if (*extent_page == 0) { 6632 /* Copy extent page from snapshot when clone did not have a matching one */ 6633 *extent_page = ctx->snapshot->active.extent_pages[i]; 6634 continue; 6635 } 6636 6637 /* Clone and snapshot both contain partially filled matching extent pages. 6638 * Update the clone extent page in place with cluster map containing the mix of both. */ 6639 ctx->next_extent_page = i + 1; 6640 6641 blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, 6642 delete_snapshot_update_extent_pages, ctx); 6643 return; 6644 } 6645 delete_snapshot_update_extent_pages_cpl(ctx); 6646 } 6647 6648 static void 6649 delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno) 6650 { 6651 struct delete_snapshot_ctx *ctx = cb_arg; 6652 uint64_t i; 6653 6654 /* Temporarily override md_ro flag for clone for MD modification */ 6655 ctx->clone_md_ro = ctx->clone->md_ro; 6656 ctx->clone->md_ro = false; 6657 6658 if (bserrno) { 6659 SPDK_ERRLOG("Failed to sync MD with xattr on blob\n"); 6660 ctx->bserrno = bserrno; 6661 delete_snapshot_cleanup_clone(ctx, 0); 6662 return; 6663 } 6664 6665 /* Copy snapshot map to clone map (only unallocated clusters in clone) */ 6666 for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) { 6667 if (ctx->clone->active.clusters[i] == 0) { 6668 ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i]; 6669 } 6670 } 6671 ctx->next_extent_page = 0; 6672 delete_snapshot_update_extent_pages(ctx, 0); 6673 } 6674 6675 static void 6676 delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno) 6677 { 6678 struct delete_snapshot_ctx *ctx = cb_arg; 6679 6680 if (bserrno) { 6681 SPDK_ERRLOG("Failed to freeze I/O on clone\n"); 6682 ctx->bserrno = bserrno; 6683 delete_snapshot_cleanup_clone(ctx, 0); 6684 return; 6685 } 6686 6687 /* Temporarily override md_ro flag for snapshot for MD modification */ 6688 ctx->snapshot_md_ro = ctx->snapshot->md_ro; 6689 ctx->snapshot->md_ro = false; 6690 6691 /* Mark blob as pending for removal for power failure safety, use clone id for recovery */ 6692 ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id, 6693 sizeof(spdk_blob_id), true); 6694 if (ctx->bserrno != 0) { 6695 delete_snapshot_cleanup_clone(ctx, 0); 6696 return; 6697 } 6698 6699 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx); 6700 } 6701 6702 static void 6703 delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno) 6704 { 6705 struct delete_snapshot_ctx *ctx = cb_arg; 6706 6707 if (bserrno) { 6708 SPDK_ERRLOG("Failed to open clone\n"); 6709 ctx->bserrno = bserrno; 6710 delete_snapshot_cleanup_snapshot(ctx, 0); 6711 return; 6712 } 6713 6714 ctx->clone = clone; 6715 6716 if (clone->locked_operation_in_progress) { 6717 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n"); 6718 ctx->bserrno = -EBUSY; 6719 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx); 6720 return; 6721 } 6722 6723 clone->locked_operation_in_progress = true; 6724 6725 blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx); 6726 } 6727 6728 static void 6729 update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx) 6730 { 6731 struct spdk_blob_list *snapshot_entry = NULL; 6732 struct spdk_blob_list *clone_entry = NULL; 6733 struct spdk_blob_list *snapshot_clone_entry = NULL; 6734 6735 /* Get snapshot entry for the snapshot we want to remove */ 6736 snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id); 6737 6738 assert(snapshot_entry != NULL); 6739 6740 /* Get clone of the snapshot (at this point there can be only one clone) */ 6741 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6742 assert(snapshot_entry->clone_count == 1); 6743 assert(clone_entry != NULL); 6744 6745 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for 6746 * snapshot that we are removing */ 6747 blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry, 6748 &snapshot_clone_entry); 6749 6750 spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx); 6751 } 6752 6753 static void 6754 bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno) 6755 { 6756 spdk_bs_sequence_t *seq = cb_arg; 6757 struct spdk_blob_list *snapshot_entry = NULL; 6758 uint32_t page_num; 6759 6760 if (bserrno) { 6761 SPDK_ERRLOG("Failed to remove blob\n"); 6762 bs_sequence_finish(seq, bserrno); 6763 return; 6764 } 6765 6766 /* Remove snapshot from the list */ 6767 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 6768 if (snapshot_entry != NULL) { 6769 TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link); 6770 free(snapshot_entry); 6771 } 6772 6773 page_num = bs_blobid_to_page(blob->id); 6774 spdk_bit_array_clear(blob->bs->used_blobids, page_num); 6775 blob->state = SPDK_BLOB_STATE_DIRTY; 6776 blob->active.num_pages = 0; 6777 blob_resize(blob, 0); 6778 6779 blob_persist(seq, blob, bs_delete_persist_cpl, blob); 6780 } 6781 6782 static int 6783 bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone) 6784 { 6785 struct spdk_blob_list *snapshot_entry = NULL; 6786 struct spdk_blob_list *clone_entry = NULL; 6787 struct spdk_blob *clone = NULL; 6788 bool has_one_clone = false; 6789 6790 /* Check if this is a snapshot with clones */ 6791 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 6792 if (snapshot_entry != NULL) { 6793 if (snapshot_entry->clone_count > 1) { 6794 SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n"); 6795 return -EBUSY; 6796 } else if (snapshot_entry->clone_count == 1) { 6797 has_one_clone = true; 6798 } 6799 } 6800 6801 /* Check if someone has this blob open (besides this delete context): 6802 * - open_ref = 1 - only this context opened blob, so it is ok to remove it 6803 * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot 6804 * and that is ok, because we will update it accordingly */ 6805 if (blob->open_ref <= 2 && has_one_clone) { 6806 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6807 assert(clone_entry != NULL); 6808 clone = blob_lookup(blob->bs, clone_entry->id); 6809 6810 if (blob->open_ref == 2 && clone == NULL) { 6811 /* Clone is closed and someone else opened this blob */ 6812 SPDK_ERRLOG("Cannot remove snapshot because it is open\n"); 6813 return -EBUSY; 6814 } 6815 6816 *update_clone = true; 6817 return 0; 6818 } 6819 6820 if (blob->open_ref > 1) { 6821 SPDK_ERRLOG("Cannot remove snapshot because it is open\n"); 6822 return -EBUSY; 6823 } 6824 6825 assert(has_one_clone == false); 6826 *update_clone = false; 6827 return 0; 6828 } 6829 6830 static void 6831 bs_delete_enomem_close_cpl(void *cb_arg, int bserrno) 6832 { 6833 spdk_bs_sequence_t *seq = cb_arg; 6834 6835 bs_sequence_finish(seq, -ENOMEM); 6836 } 6837 6838 static void 6839 bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno) 6840 { 6841 spdk_bs_sequence_t *seq = cb_arg; 6842 struct delete_snapshot_ctx *ctx; 6843 bool update_clone = false; 6844 6845 if (bserrno != 0) { 6846 bs_sequence_finish(seq, bserrno); 6847 return; 6848 } 6849 6850 blob_verify_md_op(blob); 6851 6852 ctx = calloc(1, sizeof(*ctx)); 6853 if (ctx == NULL) { 6854 spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq); 6855 return; 6856 } 6857 6858 ctx->snapshot = blob; 6859 ctx->cb_fn = bs_delete_blob_finish; 6860 ctx->cb_arg = seq; 6861 6862 /* Check if blob can be removed and if it is a snapshot with clone on top of it */ 6863 ctx->bserrno = bs_is_blob_deletable(blob, &update_clone); 6864 if (ctx->bserrno) { 6865 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 6866 return; 6867 } 6868 6869 if (blob->locked_operation_in_progress) { 6870 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n"); 6871 ctx->bserrno = -EBUSY; 6872 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 6873 return; 6874 } 6875 6876 blob->locked_operation_in_progress = true; 6877 6878 /* 6879 * Remove the blob from the blob_store list now, to ensure it does not 6880 * get returned after this point by blob_lookup(). 6881 */ 6882 spdk_bit_array_clear(blob->bs->open_blobids, blob->id); 6883 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob); 6884 6885 if (update_clone) { 6886 /* This blob is a snapshot with active clone - update clone first */ 6887 update_clone_on_snapshot_deletion(blob, ctx); 6888 } else { 6889 /* This blob does not have any clones - just remove it */ 6890 bs_blob_list_remove(blob); 6891 bs_delete_blob_finish(seq, blob, 0); 6892 free(ctx); 6893 } 6894 } 6895 6896 void 6897 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 6898 spdk_blob_op_complete cb_fn, void *cb_arg) 6899 { 6900 struct spdk_bs_cpl cpl; 6901 spdk_bs_sequence_t *seq; 6902 6903 SPDK_DEBUGLOG(blob, "Deleting blob %" PRIu64 "\n", blobid); 6904 6905 assert(spdk_get_thread() == bs->md_thread); 6906 6907 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6908 cpl.u.blob_basic.cb_fn = cb_fn; 6909 cpl.u.blob_basic.cb_arg = cb_arg; 6910 6911 seq = bs_sequence_start(bs->md_channel, &cpl); 6912 if (!seq) { 6913 cb_fn(cb_arg, -ENOMEM); 6914 return; 6915 } 6916 6917 spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq); 6918 } 6919 6920 /* END spdk_bs_delete_blob */ 6921 6922 /* START spdk_bs_open_blob */ 6923 6924 static void 6925 bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 6926 { 6927 struct spdk_blob *blob = cb_arg; 6928 struct spdk_blob *existing; 6929 6930 if (bserrno != 0) { 6931 blob_free(blob); 6932 seq->cpl.u.blob_handle.blob = NULL; 6933 bs_sequence_finish(seq, bserrno); 6934 return; 6935 } 6936 6937 existing = blob_lookup(blob->bs, blob->id); 6938 if (existing) { 6939 blob_free(blob); 6940 existing->open_ref++; 6941 seq->cpl.u.blob_handle.blob = existing; 6942 bs_sequence_finish(seq, 0); 6943 return; 6944 } 6945 6946 blob->open_ref++; 6947 6948 spdk_bit_array_set(blob->bs->open_blobids, blob->id); 6949 RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob); 6950 6951 bs_sequence_finish(seq, bserrno); 6952 } 6953 6954 static inline void 6955 blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst) 6956 { 6957 #define FIELD_OK(field) \ 6958 offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size 6959 6960 #define SET_FIELD(field) \ 6961 if (FIELD_OK(field)) { \ 6962 dst->field = src->field; \ 6963 } \ 6964 6965 SET_FIELD(clear_method); 6966 6967 dst->opts_size = src->opts_size; 6968 6969 /* You should not remove this statement, but need to update the assert statement 6970 * if you add a new field, and also add a corresponding SET_FIELD statement */ 6971 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 16, "Incorrect size"); 6972 6973 #undef FIELD_OK 6974 #undef SET_FIELD 6975 } 6976 6977 static void 6978 bs_open_blob(struct spdk_blob_store *bs, 6979 spdk_blob_id blobid, 6980 struct spdk_blob_open_opts *opts, 6981 spdk_blob_op_with_handle_complete cb_fn, 6982 void *cb_arg) 6983 { 6984 struct spdk_blob *blob; 6985 struct spdk_bs_cpl cpl; 6986 struct spdk_blob_open_opts opts_local; 6987 spdk_bs_sequence_t *seq; 6988 uint32_t page_num; 6989 6990 SPDK_DEBUGLOG(blob, "Opening blob %" PRIu64 "\n", blobid); 6991 assert(spdk_get_thread() == bs->md_thread); 6992 6993 page_num = bs_blobid_to_page(blobid); 6994 if (spdk_bit_array_get(bs->used_blobids, page_num) == false) { 6995 /* Invalid blobid */ 6996 cb_fn(cb_arg, NULL, -ENOENT); 6997 return; 6998 } 6999 7000 blob = blob_lookup(bs, blobid); 7001 if (blob) { 7002 blob->open_ref++; 7003 cb_fn(cb_arg, blob, 0); 7004 return; 7005 } 7006 7007 blob = blob_alloc(bs, blobid); 7008 if (!blob) { 7009 cb_fn(cb_arg, NULL, -ENOMEM); 7010 return; 7011 } 7012 7013 spdk_blob_open_opts_init(&opts_local, sizeof(opts_local)); 7014 if (opts) { 7015 blob_open_opts_copy(opts, &opts_local); 7016 } 7017 7018 blob->clear_method = opts_local.clear_method; 7019 7020 cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE; 7021 cpl.u.blob_handle.cb_fn = cb_fn; 7022 cpl.u.blob_handle.cb_arg = cb_arg; 7023 cpl.u.blob_handle.blob = blob; 7024 7025 seq = bs_sequence_start(bs->md_channel, &cpl); 7026 if (!seq) { 7027 blob_free(blob); 7028 cb_fn(cb_arg, NULL, -ENOMEM); 7029 return; 7030 } 7031 7032 blob_load(seq, blob, bs_open_blob_cpl, blob); 7033 } 7034 7035 void spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 7036 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7037 { 7038 bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg); 7039 } 7040 7041 void spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid, 7042 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7043 { 7044 bs_open_blob(bs, blobid, opts, cb_fn, cb_arg); 7045 } 7046 7047 /* END spdk_bs_open_blob */ 7048 7049 /* START spdk_blob_set_read_only */ 7050 int spdk_blob_set_read_only(struct spdk_blob *blob) 7051 { 7052 blob_verify_md_op(blob); 7053 7054 blob->data_ro_flags |= SPDK_BLOB_READ_ONLY; 7055 7056 blob->state = SPDK_BLOB_STATE_DIRTY; 7057 return 0; 7058 } 7059 /* END spdk_blob_set_read_only */ 7060 7061 /* START spdk_blob_sync_md */ 7062 7063 static void 7064 blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7065 { 7066 struct spdk_blob *blob = cb_arg; 7067 7068 if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 7069 blob->data_ro = true; 7070 blob->md_ro = true; 7071 } 7072 7073 bs_sequence_finish(seq, bserrno); 7074 } 7075 7076 static void 7077 blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7078 { 7079 struct spdk_bs_cpl cpl; 7080 spdk_bs_sequence_t *seq; 7081 7082 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7083 cpl.u.blob_basic.cb_fn = cb_fn; 7084 cpl.u.blob_basic.cb_arg = cb_arg; 7085 7086 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7087 if (!seq) { 7088 cb_fn(cb_arg, -ENOMEM); 7089 return; 7090 } 7091 7092 blob_persist(seq, blob, blob_sync_md_cpl, blob); 7093 } 7094 7095 void 7096 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7097 { 7098 blob_verify_md_op(blob); 7099 7100 SPDK_DEBUGLOG(blob, "Syncing blob %" PRIu64 "\n", blob->id); 7101 7102 if (blob->md_ro) { 7103 assert(blob->state == SPDK_BLOB_STATE_CLEAN); 7104 cb_fn(cb_arg, 0); 7105 return; 7106 } 7107 7108 blob_sync_md(blob, cb_fn, cb_arg); 7109 } 7110 7111 /* END spdk_blob_sync_md */ 7112 7113 struct spdk_blob_insert_cluster_ctx { 7114 struct spdk_thread *thread; 7115 struct spdk_blob *blob; 7116 uint32_t cluster_num; /* cluster index in blob */ 7117 uint32_t cluster; /* cluster on disk */ 7118 uint32_t extent_page; /* extent page on disk */ 7119 int rc; 7120 spdk_blob_op_complete cb_fn; 7121 void *cb_arg; 7122 }; 7123 7124 static void 7125 blob_insert_cluster_msg_cpl(void *arg) 7126 { 7127 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7128 7129 ctx->cb_fn(ctx->cb_arg, ctx->rc); 7130 free(ctx); 7131 } 7132 7133 static void 7134 blob_insert_cluster_msg_cb(void *arg, int bserrno) 7135 { 7136 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7137 7138 ctx->rc = bserrno; 7139 spdk_thread_send_msg(ctx->thread, blob_insert_cluster_msg_cpl, ctx); 7140 } 7141 7142 static void 7143 blob_insert_new_ep_cb(void *arg, int bserrno) 7144 { 7145 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7146 uint32_t *extent_page; 7147 7148 extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num); 7149 *extent_page = ctx->extent_page; 7150 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 7151 blob_sync_md(ctx->blob, blob_insert_cluster_msg_cb, ctx); 7152 } 7153 7154 static void 7155 blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7156 { 7157 struct spdk_blob_md_page *page = cb_arg; 7158 7159 bs_sequence_finish(seq, bserrno); 7160 spdk_free(page); 7161 } 7162 7163 static void 7164 blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num, 7165 spdk_blob_op_complete cb_fn, void *cb_arg) 7166 { 7167 spdk_bs_sequence_t *seq; 7168 struct spdk_bs_cpl cpl; 7169 struct spdk_blob_md_page *page = NULL; 7170 uint32_t page_count = 0; 7171 int rc; 7172 7173 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7174 cpl.u.blob_basic.cb_fn = cb_fn; 7175 cpl.u.blob_basic.cb_arg = cb_arg; 7176 7177 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7178 if (!seq) { 7179 cb_fn(cb_arg, -ENOMEM); 7180 return; 7181 } 7182 rc = blob_serialize_add_page(blob, &page, &page_count, &page); 7183 if (rc < 0) { 7184 bs_sequence_finish(seq, rc); 7185 return; 7186 } 7187 7188 blob_serialize_extent_page(blob, cluster_num, page); 7189 7190 page->crc = blob_md_page_calc_crc(page); 7191 7192 assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true); 7193 7194 bs_sequence_write_dev(seq, page, bs_md_page_to_lba(blob->bs, extent), 7195 bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE), 7196 blob_persist_extent_page_cpl, page); 7197 } 7198 7199 static void 7200 blob_insert_cluster_msg(void *arg) 7201 { 7202 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7203 uint32_t *extent_page; 7204 7205 ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster); 7206 if (ctx->rc != 0) { 7207 spdk_thread_send_msg(ctx->thread, blob_insert_cluster_msg_cpl, ctx); 7208 return; 7209 } 7210 7211 if (ctx->blob->use_extent_table == false) { 7212 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */ 7213 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 7214 blob_sync_md(ctx->blob, blob_insert_cluster_msg_cb, ctx); 7215 return; 7216 } 7217 7218 extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num); 7219 if (*extent_page == 0) { 7220 /* Extent page requires allocation. 7221 * It was already claimed in the used_md_pages map and placed in ctx. */ 7222 assert(ctx->extent_page != 0); 7223 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true); 7224 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, 7225 blob_insert_new_ep_cb, ctx); 7226 } else { 7227 /* It is possible for original thread to allocate extent page for 7228 * different cluster in the same extent page. In such case proceed with 7229 * updating the existing extent page, but release the additional one. */ 7230 if (ctx->extent_page != 0) { 7231 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true); 7232 bs_release_md_page(ctx->blob->bs, ctx->extent_page); 7233 ctx->extent_page = 0; 7234 } 7235 /* Extent page already allocated. 7236 * Every cluster allocation, requires just an update of single extent page. */ 7237 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, 7238 blob_insert_cluster_msg_cb, ctx); 7239 } 7240 } 7241 7242 static void 7243 blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 7244 uint64_t cluster, uint32_t extent_page, spdk_blob_op_complete cb_fn, void *cb_arg) 7245 { 7246 struct spdk_blob_insert_cluster_ctx *ctx; 7247 7248 ctx = calloc(1, sizeof(*ctx)); 7249 if (ctx == NULL) { 7250 cb_fn(cb_arg, -ENOMEM); 7251 return; 7252 } 7253 7254 ctx->thread = spdk_get_thread(); 7255 ctx->blob = blob; 7256 ctx->cluster_num = cluster_num; 7257 ctx->cluster = cluster; 7258 ctx->extent_page = extent_page; 7259 ctx->cb_fn = cb_fn; 7260 ctx->cb_arg = cb_arg; 7261 7262 spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx); 7263 } 7264 7265 /* START spdk_blob_close */ 7266 7267 static void 7268 blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7269 { 7270 struct spdk_blob *blob = cb_arg; 7271 7272 if (bserrno == 0) { 7273 blob->open_ref--; 7274 if (blob->open_ref == 0) { 7275 /* 7276 * Blobs with active.num_pages == 0 are deleted blobs. 7277 * these blobs are removed from the blob_store list 7278 * when the deletion process starts - so don't try to 7279 * remove them again. 7280 */ 7281 if (blob->active.num_pages > 0) { 7282 spdk_bit_array_clear(blob->bs->open_blobids, blob->id); 7283 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob); 7284 } 7285 blob_free(blob); 7286 } 7287 } 7288 7289 bs_sequence_finish(seq, bserrno); 7290 } 7291 7292 void spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7293 { 7294 struct spdk_bs_cpl cpl; 7295 spdk_bs_sequence_t *seq; 7296 7297 blob_verify_md_op(blob); 7298 7299 SPDK_DEBUGLOG(blob, "Closing blob %" PRIu64 "\n", blob->id); 7300 7301 if (blob->open_ref == 0) { 7302 cb_fn(cb_arg, -EBADF); 7303 return; 7304 } 7305 7306 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7307 cpl.u.blob_basic.cb_fn = cb_fn; 7308 cpl.u.blob_basic.cb_arg = cb_arg; 7309 7310 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7311 if (!seq) { 7312 cb_fn(cb_arg, -ENOMEM); 7313 return; 7314 } 7315 7316 /* Sync metadata */ 7317 blob_persist(seq, blob, blob_close_cpl, blob); 7318 } 7319 7320 /* END spdk_blob_close */ 7321 7322 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 7323 { 7324 return spdk_get_io_channel(bs); 7325 } 7326 7327 void spdk_bs_free_io_channel(struct spdk_io_channel *channel) 7328 { 7329 spdk_put_io_channel(channel); 7330 } 7331 7332 void spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel, 7333 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 7334 { 7335 blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 7336 SPDK_BLOB_UNMAP); 7337 } 7338 7339 void spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel, 7340 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 7341 { 7342 blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 7343 SPDK_BLOB_WRITE_ZEROES); 7344 } 7345 7346 void spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel, 7347 void *payload, uint64_t offset, uint64_t length, 7348 spdk_blob_op_complete cb_fn, void *cb_arg) 7349 { 7350 blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 7351 SPDK_BLOB_WRITE); 7352 } 7353 7354 void spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel, 7355 void *payload, uint64_t offset, uint64_t length, 7356 spdk_blob_op_complete cb_fn, void *cb_arg) 7357 { 7358 blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 7359 SPDK_BLOB_READ); 7360 } 7361 7362 void spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel, 7363 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7364 spdk_blob_op_complete cb_fn, void *cb_arg) 7365 { 7366 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false); 7367 } 7368 7369 void spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel, 7370 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7371 spdk_blob_op_complete cb_fn, void *cb_arg) 7372 { 7373 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true); 7374 } 7375 7376 struct spdk_bs_iter_ctx { 7377 int64_t page_num; 7378 struct spdk_blob_store *bs; 7379 7380 spdk_blob_op_with_handle_complete cb_fn; 7381 void *cb_arg; 7382 }; 7383 7384 static void 7385 bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 7386 { 7387 struct spdk_bs_iter_ctx *ctx = cb_arg; 7388 struct spdk_blob_store *bs = ctx->bs; 7389 spdk_blob_id id; 7390 7391 if (bserrno == 0) { 7392 ctx->cb_fn(ctx->cb_arg, _blob, bserrno); 7393 free(ctx); 7394 return; 7395 } 7396 7397 ctx->page_num++; 7398 ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num); 7399 if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) { 7400 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT); 7401 free(ctx); 7402 return; 7403 } 7404 7405 id = bs_page_to_blobid(ctx->page_num); 7406 7407 spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx); 7408 } 7409 7410 void 7411 spdk_bs_iter_first(struct spdk_blob_store *bs, 7412 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7413 { 7414 struct spdk_bs_iter_ctx *ctx; 7415 7416 ctx = calloc(1, sizeof(*ctx)); 7417 if (!ctx) { 7418 cb_fn(cb_arg, NULL, -ENOMEM); 7419 return; 7420 } 7421 7422 ctx->page_num = -1; 7423 ctx->bs = bs; 7424 ctx->cb_fn = cb_fn; 7425 ctx->cb_arg = cb_arg; 7426 7427 bs_iter_cpl(ctx, NULL, -1); 7428 } 7429 7430 static void 7431 bs_iter_close_cpl(void *cb_arg, int bserrno) 7432 { 7433 struct spdk_bs_iter_ctx *ctx = cb_arg; 7434 7435 bs_iter_cpl(ctx, NULL, -1); 7436 } 7437 7438 void 7439 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob, 7440 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7441 { 7442 struct spdk_bs_iter_ctx *ctx; 7443 7444 assert(blob != NULL); 7445 7446 ctx = calloc(1, sizeof(*ctx)); 7447 if (!ctx) { 7448 cb_fn(cb_arg, NULL, -ENOMEM); 7449 return; 7450 } 7451 7452 ctx->page_num = bs_blobid_to_page(blob->id); 7453 ctx->bs = bs; 7454 ctx->cb_fn = cb_fn; 7455 ctx->cb_arg = cb_arg; 7456 7457 /* Close the existing blob */ 7458 spdk_blob_close(blob, bs_iter_close_cpl, ctx); 7459 } 7460 7461 static int 7462 blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 7463 uint16_t value_len, bool internal) 7464 { 7465 struct spdk_xattr_tailq *xattrs; 7466 struct spdk_xattr *xattr; 7467 size_t desc_size; 7468 void *tmp; 7469 7470 blob_verify_md_op(blob); 7471 7472 if (blob->md_ro) { 7473 return -EPERM; 7474 } 7475 7476 desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len; 7477 if (desc_size > SPDK_BS_MAX_DESC_SIZE) { 7478 SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name, 7479 desc_size, SPDK_BS_MAX_DESC_SIZE); 7480 return -ENOMEM; 7481 } 7482 7483 if (internal) { 7484 xattrs = &blob->xattrs_internal; 7485 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR; 7486 } else { 7487 xattrs = &blob->xattrs; 7488 } 7489 7490 TAILQ_FOREACH(xattr, xattrs, link) { 7491 if (!strcmp(name, xattr->name)) { 7492 tmp = malloc(value_len); 7493 if (!tmp) { 7494 return -ENOMEM; 7495 } 7496 7497 free(xattr->value); 7498 xattr->value_len = value_len; 7499 xattr->value = tmp; 7500 memcpy(xattr->value, value, value_len); 7501 7502 blob->state = SPDK_BLOB_STATE_DIRTY; 7503 7504 return 0; 7505 } 7506 } 7507 7508 xattr = calloc(1, sizeof(*xattr)); 7509 if (!xattr) { 7510 return -ENOMEM; 7511 } 7512 7513 xattr->name = strdup(name); 7514 if (!xattr->name) { 7515 free(xattr); 7516 return -ENOMEM; 7517 } 7518 7519 xattr->value_len = value_len; 7520 xattr->value = malloc(value_len); 7521 if (!xattr->value) { 7522 free(xattr->name); 7523 free(xattr); 7524 return -ENOMEM; 7525 } 7526 memcpy(xattr->value, value, value_len); 7527 TAILQ_INSERT_TAIL(xattrs, xattr, link); 7528 7529 blob->state = SPDK_BLOB_STATE_DIRTY; 7530 7531 return 0; 7532 } 7533 7534 int 7535 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 7536 uint16_t value_len) 7537 { 7538 return blob_set_xattr(blob, name, value, value_len, false); 7539 } 7540 7541 static int 7542 blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal) 7543 { 7544 struct spdk_xattr_tailq *xattrs; 7545 struct spdk_xattr *xattr; 7546 7547 blob_verify_md_op(blob); 7548 7549 if (blob->md_ro) { 7550 return -EPERM; 7551 } 7552 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 7553 7554 TAILQ_FOREACH(xattr, xattrs, link) { 7555 if (!strcmp(name, xattr->name)) { 7556 TAILQ_REMOVE(xattrs, xattr, link); 7557 free(xattr->value); 7558 free(xattr->name); 7559 free(xattr); 7560 7561 if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) { 7562 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR; 7563 } 7564 blob->state = SPDK_BLOB_STATE_DIRTY; 7565 7566 return 0; 7567 } 7568 } 7569 7570 return -ENOENT; 7571 } 7572 7573 int 7574 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name) 7575 { 7576 return blob_remove_xattr(blob, name, false); 7577 } 7578 7579 static int 7580 blob_get_xattr_value(struct spdk_blob *blob, const char *name, 7581 const void **value, size_t *value_len, bool internal) 7582 { 7583 struct spdk_xattr *xattr; 7584 struct spdk_xattr_tailq *xattrs; 7585 7586 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 7587 7588 TAILQ_FOREACH(xattr, xattrs, link) { 7589 if (!strcmp(name, xattr->name)) { 7590 *value = xattr->value; 7591 *value_len = xattr->value_len; 7592 return 0; 7593 } 7594 } 7595 return -ENOENT; 7596 } 7597 7598 int 7599 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 7600 const void **value, size_t *value_len) 7601 { 7602 blob_verify_md_op(blob); 7603 7604 return blob_get_xattr_value(blob, name, value, value_len, false); 7605 } 7606 7607 struct spdk_xattr_names { 7608 uint32_t count; 7609 const char *names[0]; 7610 }; 7611 7612 static int 7613 blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names) 7614 { 7615 struct spdk_xattr *xattr; 7616 int count = 0; 7617 7618 TAILQ_FOREACH(xattr, xattrs, link) { 7619 count++; 7620 } 7621 7622 *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *)); 7623 if (*names == NULL) { 7624 return -ENOMEM; 7625 } 7626 7627 TAILQ_FOREACH(xattr, xattrs, link) { 7628 (*names)->names[(*names)->count++] = xattr->name; 7629 } 7630 7631 return 0; 7632 } 7633 7634 int 7635 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names) 7636 { 7637 blob_verify_md_op(blob); 7638 7639 return blob_get_xattr_names(&blob->xattrs, names); 7640 } 7641 7642 uint32_t 7643 spdk_xattr_names_get_count(struct spdk_xattr_names *names) 7644 { 7645 assert(names != NULL); 7646 7647 return names->count; 7648 } 7649 7650 const char * 7651 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index) 7652 { 7653 if (index >= names->count) { 7654 return NULL; 7655 } 7656 7657 return names->names[index]; 7658 } 7659 7660 void 7661 spdk_xattr_names_free(struct spdk_xattr_names *names) 7662 { 7663 free(names); 7664 } 7665 7666 struct spdk_bs_type 7667 spdk_bs_get_bstype(struct spdk_blob_store *bs) 7668 { 7669 return bs->bstype; 7670 } 7671 7672 void 7673 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype) 7674 { 7675 memcpy(&bs->bstype, &bstype, sizeof(bstype)); 7676 } 7677 7678 bool 7679 spdk_blob_is_read_only(struct spdk_blob *blob) 7680 { 7681 assert(blob != NULL); 7682 return (blob->data_ro || blob->md_ro); 7683 } 7684 7685 bool 7686 spdk_blob_is_snapshot(struct spdk_blob *blob) 7687 { 7688 struct spdk_blob_list *snapshot_entry; 7689 7690 assert(blob != NULL); 7691 7692 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 7693 if (snapshot_entry == NULL) { 7694 return false; 7695 } 7696 7697 return true; 7698 } 7699 7700 bool 7701 spdk_blob_is_clone(struct spdk_blob *blob) 7702 { 7703 assert(blob != NULL); 7704 7705 if (blob->parent_id != SPDK_BLOBID_INVALID) { 7706 assert(spdk_blob_is_thin_provisioned(blob)); 7707 return true; 7708 } 7709 7710 return false; 7711 } 7712 7713 bool 7714 spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 7715 { 7716 assert(blob != NULL); 7717 return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 7718 } 7719 7720 static void 7721 blob_update_clear_method(struct spdk_blob *blob) 7722 { 7723 enum blob_clear_method stored_cm; 7724 7725 assert(blob != NULL); 7726 7727 /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored 7728 * in metadata previously. If something other than the default was 7729 * specified, ignore stored value and used what was passed in. 7730 */ 7731 stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT); 7732 7733 if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) { 7734 blob->clear_method = stored_cm; 7735 } else if (blob->clear_method != stored_cm) { 7736 SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n", 7737 blob->clear_method, stored_cm); 7738 } 7739 } 7740 7741 spdk_blob_id 7742 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id) 7743 { 7744 struct spdk_blob_list *snapshot_entry = NULL; 7745 struct spdk_blob_list *clone_entry = NULL; 7746 7747 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 7748 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 7749 if (clone_entry->id == blob_id) { 7750 return snapshot_entry->id; 7751 } 7752 } 7753 } 7754 7755 return SPDK_BLOBID_INVALID; 7756 } 7757 7758 int 7759 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids, 7760 size_t *count) 7761 { 7762 struct spdk_blob_list *snapshot_entry, *clone_entry; 7763 size_t n; 7764 7765 snapshot_entry = bs_get_snapshot_entry(bs, blobid); 7766 if (snapshot_entry == NULL) { 7767 *count = 0; 7768 return 0; 7769 } 7770 7771 if (ids == NULL || *count < snapshot_entry->clone_count) { 7772 *count = snapshot_entry->clone_count; 7773 return -ENOMEM; 7774 } 7775 *count = snapshot_entry->clone_count; 7776 7777 n = 0; 7778 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 7779 ids[n++] = clone_entry->id; 7780 } 7781 7782 return 0; 7783 } 7784 7785 SPDK_LOG_REGISTER_COMPONENT(blob) 7786