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 bool dumping; 3307 FILE *fp; 3308 spdk_bs_dump_print_xattr print_xattr_fn; 3309 char xattr_name[4096]; 3310 }; 3311 3312 static int 3313 bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs, 3314 struct spdk_bs_load_ctx **_ctx) 3315 { 3316 struct spdk_blob_store *bs; 3317 struct spdk_bs_load_ctx *ctx; 3318 uint64_t dev_size; 3319 int rc; 3320 3321 dev_size = dev->blocklen * dev->blockcnt; 3322 if (dev_size < opts->cluster_sz) { 3323 /* Device size cannot be smaller than cluster size of blobstore */ 3324 SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n", 3325 dev_size, opts->cluster_sz); 3326 return -ENOSPC; 3327 } 3328 if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) { 3329 /* Cluster size cannot be smaller than page size */ 3330 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n", 3331 opts->cluster_sz, SPDK_BS_PAGE_SIZE); 3332 return -EINVAL; 3333 } 3334 bs = calloc(1, sizeof(struct spdk_blob_store)); 3335 if (!bs) { 3336 return -ENOMEM; 3337 } 3338 3339 ctx = calloc(1, sizeof(struct spdk_bs_load_ctx)); 3340 if (!ctx) { 3341 free(bs); 3342 return -ENOMEM; 3343 } 3344 3345 ctx->bs = bs; 3346 ctx->iter_cb_fn = opts->iter_cb_fn; 3347 ctx->iter_cb_arg = opts->iter_cb_arg; 3348 3349 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 3350 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3351 if (!ctx->super) { 3352 free(ctx); 3353 free(bs); 3354 return -ENOMEM; 3355 } 3356 3357 RB_INIT(&bs->open_blobs); 3358 TAILQ_INIT(&bs->snapshots); 3359 bs->dev = dev; 3360 bs->md_thread = spdk_get_thread(); 3361 assert(bs->md_thread != NULL); 3362 3363 /* 3364 * Do not use bs_lba_to_cluster() here since blockcnt may not be an 3365 * even multiple of the cluster size. 3366 */ 3367 bs->cluster_sz = opts->cluster_sz; 3368 bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen); 3369 ctx->used_clusters = spdk_bit_array_create(bs->total_clusters); 3370 if (!ctx->used_clusters) { 3371 spdk_free(ctx->super); 3372 free(ctx); 3373 free(bs); 3374 return -ENOMEM; 3375 } 3376 3377 bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE; 3378 if (spdk_u32_is_pow2(bs->pages_per_cluster)) { 3379 bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster); 3380 } 3381 bs->num_free_clusters = bs->total_clusters; 3382 bs->io_unit_size = dev->blocklen; 3383 3384 bs->max_channel_ops = opts->max_channel_ops; 3385 bs->super_blob = SPDK_BLOBID_INVALID; 3386 memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype)); 3387 3388 /* The metadata is assumed to be at least 1 page */ 3389 bs->used_md_pages = spdk_bit_array_create(1); 3390 bs->used_blobids = spdk_bit_array_create(0); 3391 bs->open_blobids = spdk_bit_array_create(0); 3392 3393 pthread_mutex_init(&bs->used_clusters_mutex, NULL); 3394 3395 spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy, 3396 sizeof(struct spdk_bs_channel), "blobstore"); 3397 rc = bs_register_md_thread(bs); 3398 if (rc == -1) { 3399 spdk_io_device_unregister(bs, NULL); 3400 pthread_mutex_destroy(&bs->used_clusters_mutex); 3401 spdk_bit_array_free(&bs->open_blobids); 3402 spdk_bit_array_free(&bs->used_blobids); 3403 spdk_bit_array_free(&bs->used_md_pages); 3404 spdk_bit_array_free(&ctx->used_clusters); 3405 spdk_free(ctx->super); 3406 free(ctx); 3407 free(bs); 3408 /* FIXME: this is a lie but don't know how to get a proper error code here */ 3409 return -ENOMEM; 3410 } 3411 3412 *_ctx = ctx; 3413 *_bs = bs; 3414 return 0; 3415 } 3416 3417 static void 3418 bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno) 3419 { 3420 assert(bserrno != 0); 3421 3422 spdk_free(ctx->super); 3423 bs_sequence_finish(ctx->seq, bserrno); 3424 bs_free(ctx->bs); 3425 spdk_bit_array_free(&ctx->used_clusters); 3426 free(ctx); 3427 } 3428 3429 static void 3430 bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 3431 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 3432 { 3433 /* Update the values in the super block */ 3434 super->super_blob = bs->super_blob; 3435 memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype)); 3436 super->crc = blob_md_page_calc_crc(super); 3437 bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0), 3438 bs_byte_to_lba(bs, sizeof(*super)), 3439 cb_fn, cb_arg); 3440 } 3441 3442 static void 3443 bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3444 { 3445 struct spdk_bs_load_ctx *ctx = arg; 3446 uint64_t mask_size, lba, lba_count; 3447 3448 /* Write out the used clusters mask */ 3449 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 3450 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3451 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3452 if (!ctx->mask) { 3453 bs_load_ctx_fail(ctx, -ENOMEM); 3454 return; 3455 } 3456 3457 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS; 3458 ctx->mask->length = ctx->bs->total_clusters; 3459 /* We could get here through the normal unload path, or through dirty 3460 * shutdown recovery. For the normal unload path, we use the mask from 3461 * the bit pool. For dirty shutdown recovery, we don't have a bit pool yet - 3462 * only the bit array from the load ctx. 3463 */ 3464 if (ctx->bs->used_clusters) { 3465 assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters)); 3466 spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask); 3467 } else { 3468 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters)); 3469 spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask); 3470 } 3471 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 3472 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 3473 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3474 } 3475 3476 static void 3477 bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3478 { 3479 struct spdk_bs_load_ctx *ctx = arg; 3480 uint64_t mask_size, lba, lba_count; 3481 3482 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 3483 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3484 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3485 if (!ctx->mask) { 3486 bs_load_ctx_fail(ctx, -ENOMEM); 3487 return; 3488 } 3489 3490 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES; 3491 ctx->mask->length = ctx->super->md_len; 3492 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages)); 3493 3494 spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask); 3495 lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 3496 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 3497 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3498 } 3499 3500 static void 3501 bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3502 { 3503 struct spdk_bs_load_ctx *ctx = arg; 3504 uint64_t mask_size, lba, lba_count; 3505 3506 if (ctx->super->used_blobid_mask_len == 0) { 3507 /* 3508 * This is a pre-v3 on-disk format where the blobid mask does not get 3509 * written to disk. 3510 */ 3511 cb_fn(seq, arg, 0); 3512 return; 3513 } 3514 3515 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 3516 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3517 SPDK_MALLOC_DMA); 3518 if (!ctx->mask) { 3519 bs_load_ctx_fail(ctx, -ENOMEM); 3520 return; 3521 } 3522 3523 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS; 3524 ctx->mask->length = ctx->super->md_len; 3525 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids)); 3526 3527 spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask); 3528 lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 3529 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 3530 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3531 } 3532 3533 static void 3534 blob_set_thin_provision(struct spdk_blob *blob) 3535 { 3536 blob_verify_md_op(blob); 3537 blob->invalid_flags |= SPDK_BLOB_THIN_PROV; 3538 blob->state = SPDK_BLOB_STATE_DIRTY; 3539 } 3540 3541 static void 3542 blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method) 3543 { 3544 blob_verify_md_op(blob); 3545 blob->clear_method = clear_method; 3546 blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT); 3547 blob->state = SPDK_BLOB_STATE_DIRTY; 3548 } 3549 3550 static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno); 3551 3552 static void 3553 bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno) 3554 { 3555 struct spdk_bs_load_ctx *ctx = cb_arg; 3556 spdk_blob_id id; 3557 int64_t page_num; 3558 3559 /* Iterate to next blob (we can't use spdk_bs_iter_next function as our 3560 * last blob has been removed */ 3561 page_num = bs_blobid_to_page(ctx->blobid); 3562 page_num++; 3563 page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num); 3564 if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) { 3565 bs_load_iter(ctx, NULL, -ENOENT); 3566 return; 3567 } 3568 3569 id = bs_page_to_blobid(page_num); 3570 3571 spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx); 3572 } 3573 3574 static void 3575 bs_delete_corrupted_close_cb(void *cb_arg, int bserrno) 3576 { 3577 struct spdk_bs_load_ctx *ctx = cb_arg; 3578 3579 if (bserrno != 0) { 3580 SPDK_ERRLOG("Failed to close corrupted blob\n"); 3581 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3582 return; 3583 } 3584 3585 spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx); 3586 } 3587 3588 static void 3589 bs_delete_corrupted_blob(void *cb_arg, int bserrno) 3590 { 3591 struct spdk_bs_load_ctx *ctx = cb_arg; 3592 uint64_t i; 3593 3594 if (bserrno != 0) { 3595 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n"); 3596 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3597 return; 3598 } 3599 3600 /* Snapshot and clone have the same copy of cluster map and extent pages 3601 * at this point. Let's clear both for snapshot now, 3602 * so that it won't be cleared for clone later when we remove snapshot. 3603 * Also set thin provision to pass data corruption check */ 3604 for (i = 0; i < ctx->blob->active.num_clusters; i++) { 3605 ctx->blob->active.clusters[i] = 0; 3606 } 3607 for (i = 0; i < ctx->blob->active.num_extent_pages; i++) { 3608 ctx->blob->active.extent_pages[i] = 0; 3609 } 3610 3611 ctx->blob->md_ro = false; 3612 3613 blob_set_thin_provision(ctx->blob); 3614 3615 ctx->blobid = ctx->blob->id; 3616 3617 spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx); 3618 } 3619 3620 static void 3621 bs_update_corrupted_blob(void *cb_arg, int bserrno) 3622 { 3623 struct spdk_bs_load_ctx *ctx = cb_arg; 3624 3625 if (bserrno != 0) { 3626 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n"); 3627 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3628 return; 3629 } 3630 3631 ctx->blob->md_ro = false; 3632 blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true); 3633 blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true); 3634 spdk_blob_set_read_only(ctx->blob); 3635 3636 if (ctx->iter_cb_fn) { 3637 ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0); 3638 } 3639 bs_blob_list_add(ctx->blob); 3640 3641 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3642 } 3643 3644 static void 3645 bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno) 3646 { 3647 struct spdk_bs_load_ctx *ctx = cb_arg; 3648 3649 if (bserrno != 0) { 3650 SPDK_ERRLOG("Failed to open clone of a corrupted blob\n"); 3651 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3652 return; 3653 } 3654 3655 if (blob->parent_id == ctx->blob->id) { 3656 /* Power failure occurred before updating clone (snapshot delete case) 3657 * or after updating clone (creating snapshot case) - keep snapshot */ 3658 spdk_blob_close(blob, bs_update_corrupted_blob, ctx); 3659 } else { 3660 /* Power failure occurred after updating clone (snapshot delete case) 3661 * or before updating clone (creating snapshot case) - remove snapshot */ 3662 spdk_blob_close(blob, bs_delete_corrupted_blob, ctx); 3663 } 3664 } 3665 3666 static void 3667 bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno) 3668 { 3669 struct spdk_bs_load_ctx *ctx = arg; 3670 const void *value; 3671 size_t len; 3672 int rc = 0; 3673 3674 if (bserrno == 0) { 3675 /* Examine blob if it is corrupted after power failure. Fix 3676 * the ones that can be fixed and remove any other corrupted 3677 * ones. If it is not corrupted just process it */ 3678 rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true); 3679 if (rc != 0) { 3680 rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true); 3681 if (rc != 0) { 3682 /* Not corrupted - process it and continue with iterating through blobs */ 3683 if (ctx->iter_cb_fn) { 3684 ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0); 3685 } 3686 bs_blob_list_add(blob); 3687 spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx); 3688 return; 3689 } 3690 3691 } 3692 3693 assert(len == sizeof(spdk_blob_id)); 3694 3695 ctx->blob = blob; 3696 3697 /* Open clone to check if we are able to fix this blob or should we remove it */ 3698 spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx); 3699 return; 3700 } else if (bserrno == -ENOENT) { 3701 bserrno = 0; 3702 } else { 3703 /* 3704 * This case needs to be looked at further. Same problem 3705 * exists with applications that rely on explicit blob 3706 * iteration. We should just skip the blob that failed 3707 * to load and continue on to the next one. 3708 */ 3709 SPDK_ERRLOG("Error in iterating blobs\n"); 3710 } 3711 3712 ctx->iter_cb_fn = NULL; 3713 3714 spdk_free(ctx->super); 3715 spdk_free(ctx->mask); 3716 bs_sequence_finish(ctx->seq, bserrno); 3717 free(ctx); 3718 } 3719 3720 static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 3721 3722 static void 3723 bs_load_complete(struct spdk_bs_load_ctx *ctx) 3724 { 3725 ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters); 3726 if (ctx->dumping) { 3727 bs_dump_read_md_page(ctx->seq, ctx); 3728 return; 3729 } 3730 spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx); 3731 } 3732 3733 static void 3734 bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3735 { 3736 struct spdk_bs_load_ctx *ctx = cb_arg; 3737 int rc; 3738 3739 /* The type must be correct */ 3740 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS); 3741 3742 /* The length of the mask (in bits) must not be greater than 3743 * the length of the buffer (converted to bits) */ 3744 assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8)); 3745 3746 /* The length of the mask must be exactly equal to the size 3747 * (in pages) of the metadata region */ 3748 assert(ctx->mask->length == ctx->super->md_len); 3749 3750 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length); 3751 if (rc < 0) { 3752 spdk_free(ctx->mask); 3753 bs_load_ctx_fail(ctx, rc); 3754 return; 3755 } 3756 3757 spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask); 3758 bs_load_complete(ctx); 3759 } 3760 3761 static void 3762 bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3763 { 3764 struct spdk_bs_load_ctx *ctx = cb_arg; 3765 uint64_t lba, lba_count, mask_size; 3766 int rc; 3767 3768 if (bserrno != 0) { 3769 bs_load_ctx_fail(ctx, bserrno); 3770 return; 3771 } 3772 3773 /* The type must be correct */ 3774 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 3775 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 3776 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 3777 struct spdk_blob_md_page) * 8)); 3778 /* The length of the mask must be exactly equal to the total number of clusters */ 3779 assert(ctx->mask->length == ctx->bs->total_clusters); 3780 3781 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length); 3782 if (rc < 0) { 3783 spdk_free(ctx->mask); 3784 bs_load_ctx_fail(ctx, rc); 3785 return; 3786 } 3787 3788 spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask); 3789 ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters); 3790 assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters); 3791 3792 spdk_free(ctx->mask); 3793 3794 /* Read the used blobids mask */ 3795 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 3796 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3797 SPDK_MALLOC_DMA); 3798 if (!ctx->mask) { 3799 bs_load_ctx_fail(ctx, -ENOMEM); 3800 return; 3801 } 3802 lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 3803 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 3804 bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 3805 bs_load_used_blobids_cpl, ctx); 3806 } 3807 3808 static void 3809 bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3810 { 3811 struct spdk_bs_load_ctx *ctx = cb_arg; 3812 uint64_t lba, lba_count, mask_size; 3813 int rc; 3814 3815 if (bserrno != 0) { 3816 bs_load_ctx_fail(ctx, bserrno); 3817 return; 3818 } 3819 3820 /* The type must be correct */ 3821 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES); 3822 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 3823 assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE * 3824 8)); 3825 /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */ 3826 if (ctx->mask->length != ctx->super->md_len) { 3827 SPDK_ERRLOG("mismatched md_len in used_pages mask: " 3828 "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n", 3829 ctx->mask->length, ctx->super->md_len); 3830 assert(false); 3831 } 3832 3833 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length); 3834 if (rc < 0) { 3835 spdk_free(ctx->mask); 3836 bs_load_ctx_fail(ctx, rc); 3837 return; 3838 } 3839 3840 spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask); 3841 spdk_free(ctx->mask); 3842 3843 /* Read the used clusters mask */ 3844 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 3845 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3846 SPDK_MALLOC_DMA); 3847 if (!ctx->mask) { 3848 bs_load_ctx_fail(ctx, -ENOMEM); 3849 return; 3850 } 3851 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 3852 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 3853 bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 3854 bs_load_used_clusters_cpl, ctx); 3855 } 3856 3857 static void 3858 bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx) 3859 { 3860 uint64_t lba, lba_count, mask_size; 3861 3862 /* Read the used pages mask */ 3863 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 3864 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3865 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3866 if (!ctx->mask) { 3867 bs_load_ctx_fail(ctx, -ENOMEM); 3868 return; 3869 } 3870 3871 lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 3872 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 3873 bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count, 3874 bs_load_used_pages_cpl, ctx); 3875 } 3876 3877 static int 3878 bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page) 3879 { 3880 struct spdk_blob_store *bs = ctx->bs; 3881 struct spdk_blob_md_descriptor *desc; 3882 size_t cur_desc = 0; 3883 3884 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 3885 while (cur_desc < sizeof(page->descriptors)) { 3886 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 3887 if (desc->length == 0) { 3888 /* If padding and length are 0, this terminates the page */ 3889 break; 3890 } 3891 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 3892 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 3893 unsigned int i, j; 3894 unsigned int cluster_count = 0; 3895 uint32_t cluster_idx; 3896 3897 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 3898 3899 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 3900 for (j = 0; j < desc_extent_rle->extents[i].length; j++) { 3901 cluster_idx = desc_extent_rle->extents[i].cluster_idx; 3902 /* 3903 * cluster_idx = 0 means an unallocated cluster - don't mark that 3904 * in the used cluster map. 3905 */ 3906 if (cluster_idx != 0) { 3907 spdk_bit_array_set(ctx->used_clusters, cluster_idx + j); 3908 if (bs->num_free_clusters == 0) { 3909 return -ENOSPC; 3910 } 3911 bs->num_free_clusters--; 3912 } 3913 cluster_count++; 3914 } 3915 } 3916 if (cluster_count == 0) { 3917 return -EINVAL; 3918 } 3919 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 3920 struct spdk_blob_md_descriptor_extent_page *desc_extent; 3921 uint32_t i; 3922 uint32_t cluster_count = 0; 3923 uint32_t cluster_idx; 3924 size_t cluster_idx_length; 3925 3926 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 3927 cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx); 3928 3929 if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) || 3930 (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) { 3931 return -EINVAL; 3932 } 3933 3934 for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) { 3935 cluster_idx = desc_extent->cluster_idx[i]; 3936 /* 3937 * cluster_idx = 0 means an unallocated cluster - don't mark that 3938 * in the used cluster map. 3939 */ 3940 if (cluster_idx != 0) { 3941 if (cluster_idx < desc_extent->start_cluster_idx && 3942 cluster_idx >= desc_extent->start_cluster_idx + cluster_count) { 3943 return -EINVAL; 3944 } 3945 spdk_bit_array_set(ctx->used_clusters, cluster_idx); 3946 if (bs->num_free_clusters == 0) { 3947 return -ENOSPC; 3948 } 3949 bs->num_free_clusters--; 3950 } 3951 cluster_count++; 3952 } 3953 3954 if (cluster_count == 0) { 3955 return -EINVAL; 3956 } 3957 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 3958 /* Skip this item */ 3959 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 3960 /* Skip this item */ 3961 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 3962 /* Skip this item */ 3963 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) { 3964 struct spdk_blob_md_descriptor_extent_table *desc_extent_table; 3965 uint32_t num_extent_pages = ctx->num_extent_pages; 3966 uint32_t i; 3967 size_t extent_pages_length; 3968 void *tmp; 3969 3970 desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc; 3971 extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters); 3972 3973 if (desc_extent_table->length == 0 || 3974 (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) { 3975 return -EINVAL; 3976 } 3977 3978 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 3979 if (desc_extent_table->extent_page[i].page_idx != 0) { 3980 if (desc_extent_table->extent_page[i].num_pages != 1) { 3981 return -EINVAL; 3982 } 3983 num_extent_pages += 1; 3984 } 3985 } 3986 3987 if (num_extent_pages > 0) { 3988 tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t)); 3989 if (tmp == NULL) { 3990 return -ENOMEM; 3991 } 3992 ctx->extent_page_num = tmp; 3993 3994 /* Extent table entries contain md page numbers for extent pages. 3995 * Zeroes represent unallocated extent pages, those are run-length-encoded. 3996 */ 3997 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 3998 if (desc_extent_table->extent_page[i].page_idx != 0) { 3999 ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx; 4000 ctx->num_extent_pages += 1; 4001 } 4002 } 4003 } 4004 } else { 4005 /* Error */ 4006 return -EINVAL; 4007 } 4008 /* Advance to the next descriptor */ 4009 cur_desc += sizeof(*desc) + desc->length; 4010 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 4011 break; 4012 } 4013 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 4014 } 4015 return 0; 4016 } 4017 4018 static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page) 4019 { 4020 uint32_t crc; 4021 struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors; 4022 size_t desc_len; 4023 4024 crc = blob_md_page_calc_crc(page); 4025 if (crc != page->crc) { 4026 return false; 4027 } 4028 4029 /* Extent page should always be of sequence num 0. */ 4030 if (page->sequence_num != 0) { 4031 return false; 4032 } 4033 4034 /* Descriptor type must be EXTENT_PAGE. */ 4035 if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4036 return false; 4037 } 4038 4039 /* Descriptor length cannot exceed the page. */ 4040 desc_len = sizeof(*desc) + desc->length; 4041 if (desc_len > sizeof(page->descriptors)) { 4042 return false; 4043 } 4044 4045 /* It has to be the only descriptor in the page. */ 4046 if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) { 4047 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len); 4048 if (desc->length != 0) { 4049 return false; 4050 } 4051 } 4052 4053 return true; 4054 } 4055 4056 static bool bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx) 4057 { 4058 uint32_t crc; 4059 struct spdk_blob_md_page *page = ctx->page; 4060 4061 crc = blob_md_page_calc_crc(page); 4062 if (crc != page->crc) { 4063 return false; 4064 } 4065 4066 /* First page of a sequence should match the blobid. */ 4067 if (page->sequence_num == 0 && 4068 bs_page_to_blobid(ctx->cur_page) != page->id) { 4069 return false; 4070 } 4071 assert(bs_load_cur_extent_page_valid(page) == false); 4072 4073 return true; 4074 } 4075 4076 static void 4077 bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx); 4078 4079 static void 4080 bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4081 { 4082 struct spdk_bs_load_ctx *ctx = cb_arg; 4083 4084 if (bserrno != 0) { 4085 bs_load_ctx_fail(ctx, bserrno); 4086 return; 4087 } 4088 4089 bs_load_complete(ctx); 4090 } 4091 4092 static void 4093 bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4094 { 4095 struct spdk_bs_load_ctx *ctx = cb_arg; 4096 4097 spdk_free(ctx->mask); 4098 ctx->mask = NULL; 4099 4100 if (bserrno != 0) { 4101 bs_load_ctx_fail(ctx, bserrno); 4102 return; 4103 } 4104 4105 bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl); 4106 } 4107 4108 static void 4109 bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4110 { 4111 struct spdk_bs_load_ctx *ctx = cb_arg; 4112 4113 spdk_free(ctx->mask); 4114 ctx->mask = NULL; 4115 4116 if (bserrno != 0) { 4117 bs_load_ctx_fail(ctx, bserrno); 4118 return; 4119 } 4120 4121 bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl); 4122 } 4123 4124 static void 4125 bs_load_write_used_md(struct spdk_bs_load_ctx *ctx) 4126 { 4127 bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl); 4128 } 4129 4130 static void 4131 bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx) 4132 { 4133 uint64_t num_md_clusters; 4134 uint64_t i; 4135 4136 ctx->in_page_chain = false; 4137 4138 do { 4139 ctx->page_index++; 4140 } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true); 4141 4142 if (ctx->page_index < ctx->super->md_len) { 4143 ctx->cur_page = ctx->page_index; 4144 bs_load_replay_cur_md_page(ctx); 4145 } else { 4146 /* Claim all of the clusters used by the metadata */ 4147 num_md_clusters = spdk_divide_round_up( 4148 ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster); 4149 for (i = 0; i < num_md_clusters; i++) { 4150 spdk_bit_array_set(ctx->used_clusters, i); 4151 } 4152 ctx->bs->num_free_clusters -= num_md_clusters; 4153 spdk_free(ctx->page); 4154 bs_load_write_used_md(ctx); 4155 } 4156 } 4157 4158 static void 4159 bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4160 { 4161 struct spdk_bs_load_ctx *ctx = cb_arg; 4162 uint32_t page_num; 4163 uint64_t i; 4164 4165 if (bserrno != 0) { 4166 spdk_free(ctx->extent_pages); 4167 bs_load_ctx_fail(ctx, bserrno); 4168 return; 4169 } 4170 4171 for (i = 0; i < ctx->num_extent_pages; i++) { 4172 /* Extent pages are only read when present within in chain md. 4173 * Integrity of md is not right if that page was not a valid extent page. */ 4174 if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) { 4175 spdk_free(ctx->extent_pages); 4176 bs_load_ctx_fail(ctx, -EILSEQ); 4177 return; 4178 } 4179 4180 page_num = ctx->extent_page_num[i]; 4181 spdk_bit_array_set(ctx->bs->used_md_pages, page_num); 4182 if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) { 4183 spdk_free(ctx->extent_pages); 4184 bs_load_ctx_fail(ctx, -EILSEQ); 4185 return; 4186 } 4187 } 4188 4189 spdk_free(ctx->extent_pages); 4190 free(ctx->extent_page_num); 4191 ctx->extent_page_num = NULL; 4192 ctx->num_extent_pages = 0; 4193 4194 bs_load_replay_md_chain_cpl(ctx); 4195 } 4196 4197 static void 4198 bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx) 4199 { 4200 spdk_bs_batch_t *batch; 4201 uint32_t page; 4202 uint64_t lba; 4203 uint64_t i; 4204 4205 ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0, 4206 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4207 if (!ctx->extent_pages) { 4208 bs_load_ctx_fail(ctx, -ENOMEM); 4209 return; 4210 } 4211 4212 batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx); 4213 4214 for (i = 0; i < ctx->num_extent_pages; i++) { 4215 page = ctx->extent_page_num[i]; 4216 assert(page < ctx->super->md_len); 4217 lba = bs_md_page_to_lba(ctx->bs, page); 4218 bs_batch_read_dev(batch, &ctx->extent_pages[i], lba, 4219 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE)); 4220 } 4221 4222 bs_batch_close(batch); 4223 } 4224 4225 static void 4226 bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4227 { 4228 struct spdk_bs_load_ctx *ctx = cb_arg; 4229 uint32_t page_num; 4230 struct spdk_blob_md_page *page; 4231 4232 if (bserrno != 0) { 4233 bs_load_ctx_fail(ctx, bserrno); 4234 return; 4235 } 4236 4237 page_num = ctx->cur_page; 4238 page = ctx->page; 4239 if (bs_load_cur_md_page_valid(ctx) == true) { 4240 if (page->sequence_num == 0 || ctx->in_page_chain == true) { 4241 bs_claim_md_page(ctx->bs, page_num); 4242 if (page->sequence_num == 0) { 4243 spdk_bit_array_set(ctx->bs->used_blobids, page_num); 4244 } 4245 if (bs_load_replay_md_parse_page(ctx, page)) { 4246 bs_load_ctx_fail(ctx, -EILSEQ); 4247 return; 4248 } 4249 if (page->next != SPDK_INVALID_MD_PAGE) { 4250 ctx->in_page_chain = true; 4251 ctx->cur_page = page->next; 4252 bs_load_replay_cur_md_page(ctx); 4253 return; 4254 } 4255 if (ctx->num_extent_pages != 0) { 4256 bs_load_replay_extent_pages(ctx); 4257 return; 4258 } 4259 } 4260 } 4261 bs_load_replay_md_chain_cpl(ctx); 4262 } 4263 4264 static void 4265 bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx) 4266 { 4267 uint64_t lba; 4268 4269 assert(ctx->cur_page < ctx->super->md_len); 4270 lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page); 4271 bs_sequence_read_dev(ctx->seq, ctx->page, lba, 4272 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 4273 bs_load_replay_md_cpl, ctx); 4274 } 4275 4276 static void 4277 bs_load_replay_md(struct spdk_bs_load_ctx *ctx) 4278 { 4279 ctx->page_index = 0; 4280 ctx->cur_page = 0; 4281 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 4282 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4283 if (!ctx->page) { 4284 bs_load_ctx_fail(ctx, -ENOMEM); 4285 return; 4286 } 4287 bs_load_replay_cur_md_page(ctx); 4288 } 4289 4290 static void 4291 bs_recover(struct spdk_bs_load_ctx *ctx) 4292 { 4293 int rc; 4294 4295 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); 4296 if (rc < 0) { 4297 bs_load_ctx_fail(ctx, -ENOMEM); 4298 return; 4299 } 4300 4301 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len); 4302 if (rc < 0) { 4303 bs_load_ctx_fail(ctx, -ENOMEM); 4304 return; 4305 } 4306 4307 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 4308 if (rc < 0) { 4309 bs_load_ctx_fail(ctx, -ENOMEM); 4310 return; 4311 } 4312 4313 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len); 4314 if (rc < 0) { 4315 bs_load_ctx_fail(ctx, -ENOMEM); 4316 return; 4317 } 4318 4319 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 4320 bs_load_replay_md(ctx); 4321 } 4322 4323 static int 4324 bs_parse_super(struct spdk_bs_load_ctx *ctx) 4325 { 4326 int rc; 4327 4328 if (ctx->super->size == 0) { 4329 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 4330 } 4331 4332 if (ctx->super->io_unit_size == 0) { 4333 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE; 4334 } 4335 4336 ctx->bs->clean = 1; 4337 ctx->bs->cluster_sz = ctx->super->cluster_size; 4338 ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size; 4339 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 4340 if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) { 4341 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster); 4342 } 4343 ctx->bs->io_unit_size = ctx->super->io_unit_size; 4344 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 4345 if (rc < 0) { 4346 return -ENOMEM; 4347 } 4348 ctx->bs->md_start = ctx->super->md_start; 4349 ctx->bs->md_len = ctx->super->md_len; 4350 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len); 4351 if (rc < 0) { 4352 return -ENOMEM; 4353 } 4354 4355 ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up( 4356 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 4357 ctx->bs->super_blob = ctx->super->super_blob; 4358 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 4359 4360 return 0; 4361 } 4362 4363 static void 4364 bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4365 { 4366 struct spdk_bs_load_ctx *ctx = cb_arg; 4367 uint32_t crc; 4368 int rc; 4369 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 4370 4371 if (ctx->super->version > SPDK_BS_VERSION || 4372 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 4373 bs_load_ctx_fail(ctx, -EILSEQ); 4374 return; 4375 } 4376 4377 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4378 sizeof(ctx->super->signature)) != 0) { 4379 bs_load_ctx_fail(ctx, -EILSEQ); 4380 return; 4381 } 4382 4383 crc = blob_md_page_calc_crc(ctx->super); 4384 if (crc != ctx->super->crc) { 4385 bs_load_ctx_fail(ctx, -EILSEQ); 4386 return; 4387 } 4388 4389 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 4390 SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n"); 4391 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 4392 SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n"); 4393 } else { 4394 SPDK_DEBUGLOG(blob, "Unexpected bstype\n"); 4395 SPDK_LOGDUMP(blob, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 4396 SPDK_LOGDUMP(blob, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 4397 bs_load_ctx_fail(ctx, -ENXIO); 4398 return; 4399 } 4400 4401 if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) { 4402 SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n", 4403 ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size); 4404 bs_load_ctx_fail(ctx, -EILSEQ); 4405 return; 4406 } 4407 4408 rc = bs_parse_super(ctx); 4409 if (rc < 0) { 4410 bs_load_ctx_fail(ctx, rc); 4411 return; 4412 } 4413 4414 if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) { 4415 bs_recover(ctx); 4416 } else { 4417 bs_load_read_used_pages(ctx); 4418 } 4419 } 4420 4421 static inline int 4422 bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst) 4423 { 4424 4425 if (!src->opts_size) { 4426 SPDK_ERRLOG("opts_size should not be zero value\n"); 4427 return -1; 4428 } 4429 4430 #define FIELD_OK(field) \ 4431 offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size 4432 4433 #define SET_FIELD(field) \ 4434 if (FIELD_OK(field)) { \ 4435 dst->field = src->field; \ 4436 } \ 4437 4438 SET_FIELD(cluster_sz); 4439 SET_FIELD(num_md_pages); 4440 SET_FIELD(max_md_ops); 4441 SET_FIELD(max_channel_ops); 4442 SET_FIELD(clear_method); 4443 4444 if (FIELD_OK(bstype)) { 4445 memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype)); 4446 } 4447 SET_FIELD(iter_cb_fn); 4448 SET_FIELD(iter_cb_arg); 4449 4450 dst->opts_size = src->opts_size; 4451 4452 /* You should not remove this statement, but need to update the assert statement 4453 * if you add a new field, and also add a corresponding SET_FIELD statement */ 4454 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 64, "Incorrect size"); 4455 4456 #undef FIELD_OK 4457 #undef SET_FIELD 4458 4459 return 0; 4460 } 4461 4462 void 4463 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 4464 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 4465 { 4466 struct spdk_blob_store *bs; 4467 struct spdk_bs_cpl cpl; 4468 struct spdk_bs_load_ctx *ctx; 4469 struct spdk_bs_opts opts = {}; 4470 int err; 4471 4472 SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev); 4473 4474 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 4475 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen); 4476 dev->destroy(dev); 4477 cb_fn(cb_arg, NULL, -EINVAL); 4478 return; 4479 } 4480 4481 spdk_bs_opts_init(&opts, sizeof(opts)); 4482 if (o) { 4483 if (bs_opts_copy(o, &opts)) { 4484 return; 4485 } 4486 } 4487 4488 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 4489 dev->destroy(dev); 4490 cb_fn(cb_arg, NULL, -EINVAL); 4491 return; 4492 } 4493 4494 err = bs_alloc(dev, &opts, &bs, &ctx); 4495 if (err) { 4496 dev->destroy(dev); 4497 cb_fn(cb_arg, NULL, err); 4498 return; 4499 } 4500 4501 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 4502 cpl.u.bs_handle.cb_fn = cb_fn; 4503 cpl.u.bs_handle.cb_arg = cb_arg; 4504 cpl.u.bs_handle.bs = bs; 4505 4506 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 4507 if (!ctx->seq) { 4508 spdk_free(ctx->super); 4509 free(ctx); 4510 bs_free(bs); 4511 cb_fn(cb_arg, NULL, -ENOMEM); 4512 return; 4513 } 4514 4515 /* Read the super block */ 4516 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 4517 bs_byte_to_lba(bs, sizeof(*ctx->super)), 4518 bs_load_super_cpl, ctx); 4519 } 4520 4521 /* END spdk_bs_load */ 4522 4523 /* START spdk_bs_dump */ 4524 4525 static void 4526 bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 4527 { 4528 spdk_free(ctx->super); 4529 4530 /* 4531 * We need to defer calling bs_call_cpl() until after 4532 * dev destruction, so tuck these away for later use. 4533 */ 4534 ctx->bs->unload_err = bserrno; 4535 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 4536 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 4537 4538 bs_sequence_finish(seq, 0); 4539 bs_free(ctx->bs); 4540 free(ctx); 4541 } 4542 4543 static void 4544 bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx) 4545 { 4546 uint32_t page_idx = ctx->cur_page; 4547 struct spdk_blob_md_page *page = ctx->page; 4548 struct spdk_blob_md_descriptor *desc; 4549 size_t cur_desc = 0; 4550 uint32_t crc; 4551 4552 fprintf(ctx->fp, "=========\n"); 4553 fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx); 4554 fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id); 4555 fprintf(ctx->fp, "In used bit array%s:", ctx->super->clean ? "" : " (not clean: dubious)"); 4556 if (spdk_bit_array_get(ctx->bs->used_md_pages, page_idx)) { 4557 fprintf(ctx->fp, " md"); 4558 } 4559 if (spdk_bit_array_get(ctx->bs->used_blobids, page_idx)) { 4560 fprintf(ctx->fp, " blob"); 4561 } 4562 fprintf(ctx->fp, "\n"); 4563 4564 crc = blob_md_page_calc_crc(page); 4565 fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch"); 4566 4567 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 4568 while (cur_desc < sizeof(page->descriptors)) { 4569 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 4570 if (desc->length == 0) { 4571 /* If padding and length are 0, this terminates the page */ 4572 break; 4573 } 4574 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 4575 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 4576 unsigned int i; 4577 4578 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 4579 4580 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 4581 if (desc_extent_rle->extents[i].cluster_idx != 0) { 4582 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 4583 desc_extent_rle->extents[i].cluster_idx); 4584 } else { 4585 fprintf(ctx->fp, "Unallocated Extent - "); 4586 } 4587 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length); 4588 fprintf(ctx->fp, "\n"); 4589 } 4590 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4591 struct spdk_blob_md_descriptor_extent_page *desc_extent; 4592 unsigned int i; 4593 4594 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 4595 4596 for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) { 4597 if (desc_extent->cluster_idx[i] != 0) { 4598 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 4599 desc_extent->cluster_idx[i]); 4600 } else { 4601 fprintf(ctx->fp, "Unallocated Extent"); 4602 } 4603 fprintf(ctx->fp, "\n"); 4604 } 4605 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 4606 struct spdk_blob_md_descriptor_xattr *desc_xattr; 4607 uint32_t i; 4608 4609 desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc; 4610 4611 if (desc_xattr->length != 4612 sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) + 4613 desc_xattr->name_length + desc_xattr->value_length) { 4614 } 4615 4616 memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length); 4617 ctx->xattr_name[desc_xattr->name_length] = '\0'; 4618 fprintf(ctx->fp, "XATTR: name = \"%s\"\n", ctx->xattr_name); 4619 fprintf(ctx->fp, " value = \""); 4620 ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name, 4621 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 4622 desc_xattr->value_length); 4623 fprintf(ctx->fp, "\"\n"); 4624 for (i = 0; i < desc_xattr->value_length; i++) { 4625 if (i % 16 == 0) { 4626 fprintf(ctx->fp, " "); 4627 } 4628 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i)); 4629 if ((i + 1) % 16 == 0) { 4630 fprintf(ctx->fp, "\n"); 4631 } 4632 } 4633 if (i % 16 != 0) { 4634 fprintf(ctx->fp, "\n"); 4635 } 4636 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 4637 /* TODO */ 4638 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 4639 /* TODO */ 4640 } else { 4641 /* Error */ 4642 } 4643 /* Advance to the next descriptor */ 4644 cur_desc += sizeof(*desc) + desc->length; 4645 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 4646 break; 4647 } 4648 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 4649 } 4650 } 4651 4652 static void 4653 bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4654 { 4655 struct spdk_bs_load_ctx *ctx = cb_arg; 4656 4657 if (bserrno != 0) { 4658 bs_dump_finish(seq, ctx, bserrno); 4659 return; 4660 } 4661 4662 if (ctx->page->id != 0) { 4663 bs_dump_print_md_page(ctx); 4664 } 4665 4666 ctx->cur_page++; 4667 4668 if (ctx->cur_page < ctx->super->md_len) { 4669 bs_dump_read_md_page(seq, ctx); 4670 } else { 4671 spdk_free(ctx->page); 4672 bs_dump_finish(seq, ctx, 0); 4673 } 4674 } 4675 4676 static void 4677 bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 4678 { 4679 struct spdk_bs_load_ctx *ctx = cb_arg; 4680 uint64_t lba; 4681 4682 assert(ctx->cur_page < ctx->super->md_len); 4683 lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 4684 bs_sequence_read_dev(seq, ctx->page, lba, 4685 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 4686 bs_dump_read_md_page_cpl, ctx); 4687 } 4688 4689 static void 4690 bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4691 { 4692 struct spdk_bs_load_ctx *ctx = cb_arg; 4693 int rc; 4694 4695 fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature); 4696 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4697 sizeof(ctx->super->signature)) != 0) { 4698 fprintf(ctx->fp, "(Mismatch)\n"); 4699 bs_dump_finish(seq, ctx, bserrno); 4700 return; 4701 } else { 4702 fprintf(ctx->fp, "(OK)\n"); 4703 } 4704 fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version); 4705 fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc, 4706 (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch"); 4707 fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype); 4708 fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size); 4709 fprintf(ctx->fp, "Super Blob ID: "); 4710 if (ctx->super->super_blob == SPDK_BLOBID_INVALID) { 4711 fprintf(ctx->fp, "(None)\n"); 4712 } else { 4713 fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob); 4714 } 4715 fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean); 4716 fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start); 4717 fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len); 4718 fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start); 4719 fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len); 4720 fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start); 4721 fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len); 4722 fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start); 4723 fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len); 4724 4725 ctx->cur_page = 0; 4726 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 4727 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4728 if (!ctx->page) { 4729 bs_dump_finish(seq, ctx, -ENOMEM); 4730 return; 4731 } 4732 4733 rc = bs_parse_super(ctx); 4734 if (rc < 0) { 4735 bs_load_ctx_fail(ctx, rc); 4736 return; 4737 } 4738 4739 bs_load_read_used_pages(ctx); 4740 } 4741 4742 void 4743 spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn, 4744 spdk_bs_op_complete cb_fn, void *cb_arg) 4745 { 4746 struct spdk_blob_store *bs; 4747 struct spdk_bs_cpl cpl; 4748 struct spdk_bs_load_ctx *ctx; 4749 struct spdk_bs_opts opts = {}; 4750 int err; 4751 4752 SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev); 4753 4754 spdk_bs_opts_init(&opts, sizeof(opts)); 4755 4756 err = bs_alloc(dev, &opts, &bs, &ctx); 4757 if (err) { 4758 dev->destroy(dev); 4759 cb_fn(cb_arg, err); 4760 return; 4761 } 4762 4763 ctx->dumping = true; 4764 ctx->fp = fp; 4765 ctx->print_xattr_fn = print_xattr_fn; 4766 4767 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 4768 cpl.u.bs_basic.cb_fn = cb_fn; 4769 cpl.u.bs_basic.cb_arg = cb_arg; 4770 4771 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 4772 if (!ctx->seq) { 4773 spdk_free(ctx->super); 4774 free(ctx); 4775 bs_free(bs); 4776 cb_fn(cb_arg, -ENOMEM); 4777 return; 4778 } 4779 4780 /* Read the super block */ 4781 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 4782 bs_byte_to_lba(bs, sizeof(*ctx->super)), 4783 bs_dump_super_cpl, ctx); 4784 } 4785 4786 /* END spdk_bs_dump */ 4787 4788 /* START spdk_bs_init */ 4789 4790 static void 4791 bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4792 { 4793 struct spdk_bs_load_ctx *ctx = cb_arg; 4794 4795 ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters); 4796 spdk_free(ctx->super); 4797 free(ctx); 4798 4799 bs_sequence_finish(seq, bserrno); 4800 } 4801 4802 static void 4803 bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4804 { 4805 struct spdk_bs_load_ctx *ctx = cb_arg; 4806 4807 /* Write super block */ 4808 bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0), 4809 bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 4810 bs_init_persist_super_cpl, ctx); 4811 } 4812 4813 void 4814 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 4815 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 4816 { 4817 struct spdk_bs_load_ctx *ctx; 4818 struct spdk_blob_store *bs; 4819 struct spdk_bs_cpl cpl; 4820 spdk_bs_sequence_t *seq; 4821 spdk_bs_batch_t *batch; 4822 uint64_t num_md_lba; 4823 uint64_t num_md_pages; 4824 uint64_t num_md_clusters; 4825 uint32_t i; 4826 struct spdk_bs_opts opts = {}; 4827 int rc; 4828 uint64_t lba, lba_count; 4829 4830 SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev); 4831 4832 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 4833 SPDK_ERRLOG("unsupported dev block length of %d\n", 4834 dev->blocklen); 4835 dev->destroy(dev); 4836 cb_fn(cb_arg, NULL, -EINVAL); 4837 return; 4838 } 4839 4840 spdk_bs_opts_init(&opts, sizeof(opts)); 4841 if (o) { 4842 if (bs_opts_copy(o, &opts)) { 4843 return; 4844 } 4845 } 4846 4847 if (bs_opts_verify(&opts) != 0) { 4848 dev->destroy(dev); 4849 cb_fn(cb_arg, NULL, -EINVAL); 4850 return; 4851 } 4852 4853 rc = bs_alloc(dev, &opts, &bs, &ctx); 4854 if (rc) { 4855 dev->destroy(dev); 4856 cb_fn(cb_arg, NULL, rc); 4857 return; 4858 } 4859 4860 if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) { 4861 /* By default, allocate 1 page per cluster. 4862 * Technically, this over-allocates metadata 4863 * because more metadata will reduce the number 4864 * of usable clusters. This can be addressed with 4865 * more complex math in the future. 4866 */ 4867 bs->md_len = bs->total_clusters; 4868 } else { 4869 bs->md_len = opts.num_md_pages; 4870 } 4871 rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len); 4872 if (rc < 0) { 4873 spdk_free(ctx->super); 4874 free(ctx); 4875 bs_free(bs); 4876 cb_fn(cb_arg, NULL, -ENOMEM); 4877 return; 4878 } 4879 4880 rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len); 4881 if (rc < 0) { 4882 spdk_free(ctx->super); 4883 free(ctx); 4884 bs_free(bs); 4885 cb_fn(cb_arg, NULL, -ENOMEM); 4886 return; 4887 } 4888 4889 rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len); 4890 if (rc < 0) { 4891 spdk_free(ctx->super); 4892 free(ctx); 4893 bs_free(bs); 4894 cb_fn(cb_arg, NULL, -ENOMEM); 4895 return; 4896 } 4897 4898 memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4899 sizeof(ctx->super->signature)); 4900 ctx->super->version = SPDK_BS_VERSION; 4901 ctx->super->length = sizeof(*ctx->super); 4902 ctx->super->super_blob = bs->super_blob; 4903 ctx->super->clean = 0; 4904 ctx->super->cluster_size = bs->cluster_sz; 4905 ctx->super->io_unit_size = bs->io_unit_size; 4906 memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype)); 4907 4908 /* Calculate how many pages the metadata consumes at the front 4909 * of the disk. 4910 */ 4911 4912 /* The super block uses 1 page */ 4913 num_md_pages = 1; 4914 4915 /* The used_md_pages mask requires 1 bit per metadata page, rounded 4916 * up to the nearest page, plus a header. 4917 */ 4918 ctx->super->used_page_mask_start = num_md_pages; 4919 ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 4920 spdk_divide_round_up(bs->md_len, 8), 4921 SPDK_BS_PAGE_SIZE); 4922 num_md_pages += ctx->super->used_page_mask_len; 4923 4924 /* The used_clusters mask requires 1 bit per cluster, rounded 4925 * up to the nearest page, plus a header. 4926 */ 4927 ctx->super->used_cluster_mask_start = num_md_pages; 4928 ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 4929 spdk_divide_round_up(bs->total_clusters, 8), 4930 SPDK_BS_PAGE_SIZE); 4931 num_md_pages += ctx->super->used_cluster_mask_len; 4932 4933 /* The used_blobids mask requires 1 bit per metadata page, rounded 4934 * up to the nearest page, plus a header. 4935 */ 4936 ctx->super->used_blobid_mask_start = num_md_pages; 4937 ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 4938 spdk_divide_round_up(bs->md_len, 8), 4939 SPDK_BS_PAGE_SIZE); 4940 num_md_pages += ctx->super->used_blobid_mask_len; 4941 4942 /* The metadata region size was chosen above */ 4943 ctx->super->md_start = bs->md_start = num_md_pages; 4944 ctx->super->md_len = bs->md_len; 4945 num_md_pages += bs->md_len; 4946 4947 num_md_lba = bs_page_to_lba(bs, num_md_pages); 4948 4949 ctx->super->size = dev->blockcnt * dev->blocklen; 4950 4951 ctx->super->crc = blob_md_page_calc_crc(ctx->super); 4952 4953 num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster); 4954 if (num_md_clusters > bs->total_clusters) { 4955 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, " 4956 "please decrease number of pages reserved for metadata " 4957 "or increase cluster size.\n"); 4958 spdk_free(ctx->super); 4959 spdk_bit_array_free(&ctx->used_clusters); 4960 free(ctx); 4961 bs_free(bs); 4962 cb_fn(cb_arg, NULL, -ENOMEM); 4963 return; 4964 } 4965 /* Claim all of the clusters used by the metadata */ 4966 for (i = 0; i < num_md_clusters; i++) { 4967 spdk_bit_array_set(ctx->used_clusters, i); 4968 } 4969 4970 bs->num_free_clusters -= num_md_clusters; 4971 bs->total_data_clusters = bs->num_free_clusters; 4972 4973 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 4974 cpl.u.bs_handle.cb_fn = cb_fn; 4975 cpl.u.bs_handle.cb_arg = cb_arg; 4976 cpl.u.bs_handle.bs = bs; 4977 4978 seq = bs_sequence_start(bs->md_channel, &cpl); 4979 if (!seq) { 4980 spdk_free(ctx->super); 4981 free(ctx); 4982 bs_free(bs); 4983 cb_fn(cb_arg, NULL, -ENOMEM); 4984 return; 4985 } 4986 4987 batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx); 4988 4989 /* Clear metadata space */ 4990 bs_batch_write_zeroes_dev(batch, 0, num_md_lba); 4991 4992 lba = num_md_lba; 4993 lba_count = ctx->bs->dev->blockcnt - lba; 4994 switch (opts.clear_method) { 4995 case BS_CLEAR_WITH_UNMAP: 4996 /* Trim data clusters */ 4997 bs_batch_unmap_dev(batch, lba, lba_count); 4998 break; 4999 case BS_CLEAR_WITH_WRITE_ZEROES: 5000 /* Write_zeroes to data clusters */ 5001 bs_batch_write_zeroes_dev(batch, lba, lba_count); 5002 break; 5003 case BS_CLEAR_WITH_NONE: 5004 default: 5005 break; 5006 } 5007 5008 bs_batch_close(batch); 5009 } 5010 5011 /* END spdk_bs_init */ 5012 5013 /* START spdk_bs_destroy */ 5014 5015 static void 5016 bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5017 { 5018 struct spdk_bs_load_ctx *ctx = cb_arg; 5019 struct spdk_blob_store *bs = ctx->bs; 5020 5021 /* 5022 * We need to defer calling bs_call_cpl() until after 5023 * dev destruction, so tuck these away for later use. 5024 */ 5025 bs->unload_err = bserrno; 5026 memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 5027 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 5028 5029 bs_sequence_finish(seq, bserrno); 5030 5031 bs_free(bs); 5032 free(ctx); 5033 } 5034 5035 void 5036 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, 5037 void *cb_arg) 5038 { 5039 struct spdk_bs_cpl cpl; 5040 spdk_bs_sequence_t *seq; 5041 struct spdk_bs_load_ctx *ctx; 5042 5043 SPDK_DEBUGLOG(blob, "Destroying blobstore\n"); 5044 5045 if (!RB_EMPTY(&bs->open_blobs)) { 5046 SPDK_ERRLOG("Blobstore still has open blobs\n"); 5047 cb_fn(cb_arg, -EBUSY); 5048 return; 5049 } 5050 5051 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5052 cpl.u.bs_basic.cb_fn = cb_fn; 5053 cpl.u.bs_basic.cb_arg = cb_arg; 5054 5055 ctx = calloc(1, sizeof(*ctx)); 5056 if (!ctx) { 5057 cb_fn(cb_arg, -ENOMEM); 5058 return; 5059 } 5060 5061 ctx->bs = bs; 5062 5063 seq = bs_sequence_start(bs->md_channel, &cpl); 5064 if (!seq) { 5065 free(ctx); 5066 cb_fn(cb_arg, -ENOMEM); 5067 return; 5068 } 5069 5070 /* Write zeroes to the super block */ 5071 bs_sequence_write_zeroes_dev(seq, 5072 bs_page_to_lba(bs, 0), 5073 bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)), 5074 bs_destroy_trim_cpl, ctx); 5075 } 5076 5077 /* END spdk_bs_destroy */ 5078 5079 /* START spdk_bs_unload */ 5080 5081 static void 5082 bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno) 5083 { 5084 spdk_bs_sequence_t *seq = ctx->seq; 5085 5086 spdk_free(ctx->super); 5087 5088 /* 5089 * We need to defer calling bs_call_cpl() until after 5090 * dev destruction, so tuck these away for later use. 5091 */ 5092 ctx->bs->unload_err = bserrno; 5093 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 5094 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 5095 5096 bs_sequence_finish(seq, bserrno); 5097 5098 bs_free(ctx->bs); 5099 free(ctx); 5100 } 5101 5102 static void 5103 bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5104 { 5105 struct spdk_bs_load_ctx *ctx = cb_arg; 5106 5107 bs_unload_finish(ctx, bserrno); 5108 } 5109 5110 static void 5111 bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5112 { 5113 struct spdk_bs_load_ctx *ctx = cb_arg; 5114 5115 spdk_free(ctx->mask); 5116 5117 if (bserrno != 0) { 5118 bs_unload_finish(ctx, bserrno); 5119 return; 5120 } 5121 5122 ctx->super->clean = 1; 5123 5124 bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx); 5125 } 5126 5127 static void 5128 bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5129 { 5130 struct spdk_bs_load_ctx *ctx = cb_arg; 5131 5132 spdk_free(ctx->mask); 5133 ctx->mask = NULL; 5134 5135 if (bserrno != 0) { 5136 bs_unload_finish(ctx, bserrno); 5137 return; 5138 } 5139 5140 bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl); 5141 } 5142 5143 static void 5144 bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5145 { 5146 struct spdk_bs_load_ctx *ctx = cb_arg; 5147 5148 spdk_free(ctx->mask); 5149 ctx->mask = NULL; 5150 5151 if (bserrno != 0) { 5152 bs_unload_finish(ctx, bserrno); 5153 return; 5154 } 5155 5156 bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl); 5157 } 5158 5159 static void 5160 bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5161 { 5162 struct spdk_bs_load_ctx *ctx = cb_arg; 5163 5164 if (bserrno != 0) { 5165 bs_unload_finish(ctx, bserrno); 5166 return; 5167 } 5168 5169 bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl); 5170 } 5171 5172 void 5173 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 5174 { 5175 struct spdk_bs_cpl cpl; 5176 struct spdk_bs_load_ctx *ctx; 5177 5178 SPDK_DEBUGLOG(blob, "Syncing blobstore\n"); 5179 5180 if (!RB_EMPTY(&bs->open_blobs)) { 5181 SPDK_ERRLOG("Blobstore still has open blobs\n"); 5182 cb_fn(cb_arg, -EBUSY); 5183 return; 5184 } 5185 5186 ctx = calloc(1, sizeof(*ctx)); 5187 if (!ctx) { 5188 cb_fn(cb_arg, -ENOMEM); 5189 return; 5190 } 5191 5192 ctx->bs = bs; 5193 5194 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 5195 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 5196 if (!ctx->super) { 5197 free(ctx); 5198 cb_fn(cb_arg, -ENOMEM); 5199 return; 5200 } 5201 5202 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5203 cpl.u.bs_basic.cb_fn = cb_fn; 5204 cpl.u.bs_basic.cb_arg = cb_arg; 5205 5206 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 5207 if (!ctx->seq) { 5208 spdk_free(ctx->super); 5209 free(ctx); 5210 cb_fn(cb_arg, -ENOMEM); 5211 return; 5212 } 5213 5214 /* Read super block */ 5215 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 5216 bs_byte_to_lba(bs, sizeof(*ctx->super)), 5217 bs_unload_read_super_cpl, ctx); 5218 } 5219 5220 /* END spdk_bs_unload */ 5221 5222 /* START spdk_bs_set_super */ 5223 5224 struct spdk_bs_set_super_ctx { 5225 struct spdk_blob_store *bs; 5226 struct spdk_bs_super_block *super; 5227 }; 5228 5229 static void 5230 bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5231 { 5232 struct spdk_bs_set_super_ctx *ctx = cb_arg; 5233 5234 if (bserrno != 0) { 5235 SPDK_ERRLOG("Unable to write to super block of blobstore\n"); 5236 } 5237 5238 spdk_free(ctx->super); 5239 5240 bs_sequence_finish(seq, bserrno); 5241 5242 free(ctx); 5243 } 5244 5245 static void 5246 bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5247 { 5248 struct spdk_bs_set_super_ctx *ctx = cb_arg; 5249 5250 if (bserrno != 0) { 5251 SPDK_ERRLOG("Unable to read super block of blobstore\n"); 5252 spdk_free(ctx->super); 5253 bs_sequence_finish(seq, bserrno); 5254 free(ctx); 5255 return; 5256 } 5257 5258 bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx); 5259 } 5260 5261 void 5262 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 5263 spdk_bs_op_complete cb_fn, void *cb_arg) 5264 { 5265 struct spdk_bs_cpl cpl; 5266 spdk_bs_sequence_t *seq; 5267 struct spdk_bs_set_super_ctx *ctx; 5268 5269 SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n"); 5270 5271 ctx = calloc(1, sizeof(*ctx)); 5272 if (!ctx) { 5273 cb_fn(cb_arg, -ENOMEM); 5274 return; 5275 } 5276 5277 ctx->bs = bs; 5278 5279 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 5280 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 5281 if (!ctx->super) { 5282 free(ctx); 5283 cb_fn(cb_arg, -ENOMEM); 5284 return; 5285 } 5286 5287 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5288 cpl.u.bs_basic.cb_fn = cb_fn; 5289 cpl.u.bs_basic.cb_arg = cb_arg; 5290 5291 seq = bs_sequence_start(bs->md_channel, &cpl); 5292 if (!seq) { 5293 spdk_free(ctx->super); 5294 free(ctx); 5295 cb_fn(cb_arg, -ENOMEM); 5296 return; 5297 } 5298 5299 bs->super_blob = blobid; 5300 5301 /* Read super block */ 5302 bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0), 5303 bs_byte_to_lba(bs, sizeof(*ctx->super)), 5304 bs_set_super_read_cpl, ctx); 5305 } 5306 5307 /* END spdk_bs_set_super */ 5308 5309 void 5310 spdk_bs_get_super(struct spdk_blob_store *bs, 5311 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5312 { 5313 if (bs->super_blob == SPDK_BLOBID_INVALID) { 5314 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT); 5315 } else { 5316 cb_fn(cb_arg, bs->super_blob, 0); 5317 } 5318 } 5319 5320 uint64_t 5321 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 5322 { 5323 return bs->cluster_sz; 5324 } 5325 5326 uint64_t 5327 spdk_bs_get_page_size(struct spdk_blob_store *bs) 5328 { 5329 return SPDK_BS_PAGE_SIZE; 5330 } 5331 5332 uint64_t 5333 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs) 5334 { 5335 return bs->io_unit_size; 5336 } 5337 5338 uint64_t 5339 spdk_bs_free_cluster_count(struct spdk_blob_store *bs) 5340 { 5341 return bs->num_free_clusters; 5342 } 5343 5344 uint64_t 5345 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs) 5346 { 5347 return bs->total_data_clusters; 5348 } 5349 5350 static int 5351 bs_register_md_thread(struct spdk_blob_store *bs) 5352 { 5353 bs->md_channel = spdk_get_io_channel(bs); 5354 if (!bs->md_channel) { 5355 SPDK_ERRLOG("Failed to get IO channel.\n"); 5356 return -1; 5357 } 5358 5359 return 0; 5360 } 5361 5362 static int 5363 bs_unregister_md_thread(struct spdk_blob_store *bs) 5364 { 5365 spdk_put_io_channel(bs->md_channel); 5366 5367 return 0; 5368 } 5369 5370 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob) 5371 { 5372 assert(blob != NULL); 5373 5374 return blob->id; 5375 } 5376 5377 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob) 5378 { 5379 assert(blob != NULL); 5380 5381 return bs_cluster_to_page(blob->bs, blob->active.num_clusters); 5382 } 5383 5384 uint64_t spdk_blob_get_num_io_units(struct spdk_blob *blob) 5385 { 5386 assert(blob != NULL); 5387 5388 return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs); 5389 } 5390 5391 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob) 5392 { 5393 assert(blob != NULL); 5394 5395 return blob->active.num_clusters; 5396 } 5397 5398 /* START spdk_bs_create_blob */ 5399 5400 static void 5401 bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5402 { 5403 struct spdk_blob *blob = cb_arg; 5404 uint32_t page_idx = bs_blobid_to_page(blob->id); 5405 5406 if (bserrno != 0) { 5407 spdk_bit_array_clear(blob->bs->used_blobids, page_idx); 5408 bs_release_md_page(blob->bs, page_idx); 5409 } 5410 5411 blob_free(blob); 5412 5413 bs_sequence_finish(seq, bserrno); 5414 } 5415 5416 static int 5417 blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs, 5418 bool internal) 5419 { 5420 uint64_t i; 5421 size_t value_len = 0; 5422 int rc; 5423 const void *value = NULL; 5424 if (xattrs->count > 0 && xattrs->get_value == NULL) { 5425 return -EINVAL; 5426 } 5427 for (i = 0; i < xattrs->count; i++) { 5428 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len); 5429 if (value == NULL || value_len == 0) { 5430 return -EINVAL; 5431 } 5432 rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal); 5433 if (rc < 0) { 5434 return rc; 5435 } 5436 } 5437 return 0; 5438 } 5439 5440 static void 5441 blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst) 5442 { 5443 #define FIELD_OK(field) \ 5444 offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size 5445 5446 #define SET_FIELD(field) \ 5447 if (FIELD_OK(field)) { \ 5448 dst->field = src->field; \ 5449 } \ 5450 5451 SET_FIELD(num_clusters); 5452 SET_FIELD(thin_provision); 5453 SET_FIELD(clear_method); 5454 5455 if (FIELD_OK(xattrs)) { 5456 memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs)); 5457 } 5458 5459 SET_FIELD(use_extent_table); 5460 5461 dst->opts_size = src->opts_size; 5462 5463 /* You should not remove this statement, but need to update the assert statement 5464 * if you add a new field, and also add a corresponding SET_FIELD statement */ 5465 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 64, "Incorrect size"); 5466 5467 #undef FIELD_OK 5468 #undef SET_FIELD 5469 } 5470 5471 static void 5472 bs_create_blob(struct spdk_blob_store *bs, 5473 const struct spdk_blob_opts *opts, 5474 const struct spdk_blob_xattr_opts *internal_xattrs, 5475 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5476 { 5477 struct spdk_blob *blob; 5478 uint32_t page_idx; 5479 struct spdk_bs_cpl cpl; 5480 struct spdk_blob_opts opts_local; 5481 struct spdk_blob_xattr_opts internal_xattrs_default; 5482 spdk_bs_sequence_t *seq; 5483 spdk_blob_id id; 5484 int rc; 5485 5486 assert(spdk_get_thread() == bs->md_thread); 5487 5488 page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0); 5489 if (page_idx == UINT32_MAX) { 5490 cb_fn(cb_arg, 0, -ENOMEM); 5491 return; 5492 } 5493 spdk_bit_array_set(bs->used_blobids, page_idx); 5494 bs_claim_md_page(bs, page_idx); 5495 5496 id = bs_page_to_blobid(page_idx); 5497 5498 SPDK_DEBUGLOG(blob, "Creating blob with id %" PRIu64 " at page %u\n", id, page_idx); 5499 5500 blob = blob_alloc(bs, id); 5501 if (!blob) { 5502 spdk_bit_array_clear(bs->used_blobids, page_idx); 5503 bs_release_md_page(bs, page_idx); 5504 cb_fn(cb_arg, 0, -ENOMEM); 5505 return; 5506 } 5507 5508 spdk_blob_opts_init(&opts_local, sizeof(opts_local)); 5509 if (opts) { 5510 blob_opts_copy(opts, &opts_local); 5511 } 5512 5513 blob->use_extent_table = opts_local.use_extent_table; 5514 if (blob->use_extent_table) { 5515 blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE; 5516 } 5517 5518 if (!internal_xattrs) { 5519 blob_xattrs_init(&internal_xattrs_default); 5520 internal_xattrs = &internal_xattrs_default; 5521 } 5522 5523 rc = blob_set_xattrs(blob, &opts_local.xattrs, false); 5524 if (rc < 0) { 5525 blob_free(blob); 5526 spdk_bit_array_clear(bs->used_blobids, page_idx); 5527 bs_release_md_page(bs, page_idx); 5528 cb_fn(cb_arg, 0, rc); 5529 return; 5530 } 5531 5532 rc = blob_set_xattrs(blob, internal_xattrs, true); 5533 if (rc < 0) { 5534 blob_free(blob); 5535 spdk_bit_array_clear(bs->used_blobids, page_idx); 5536 bs_release_md_page(bs, page_idx); 5537 cb_fn(cb_arg, 0, rc); 5538 return; 5539 } 5540 5541 if (opts_local.thin_provision) { 5542 blob_set_thin_provision(blob); 5543 } 5544 5545 blob_set_clear_method(blob, opts_local.clear_method); 5546 5547 rc = blob_resize(blob, opts_local.num_clusters); 5548 if (rc < 0) { 5549 blob_free(blob); 5550 spdk_bit_array_clear(bs->used_blobids, page_idx); 5551 bs_release_md_page(bs, page_idx); 5552 cb_fn(cb_arg, 0, rc); 5553 return; 5554 } 5555 cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 5556 cpl.u.blobid.cb_fn = cb_fn; 5557 cpl.u.blobid.cb_arg = cb_arg; 5558 cpl.u.blobid.blobid = blob->id; 5559 5560 seq = bs_sequence_start(bs->md_channel, &cpl); 5561 if (!seq) { 5562 blob_free(blob); 5563 spdk_bit_array_clear(bs->used_blobids, page_idx); 5564 bs_release_md_page(bs, page_idx); 5565 cb_fn(cb_arg, 0, -ENOMEM); 5566 return; 5567 } 5568 5569 blob_persist(seq, blob, bs_create_blob_cpl, blob); 5570 } 5571 5572 void spdk_bs_create_blob(struct spdk_blob_store *bs, 5573 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5574 { 5575 bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg); 5576 } 5577 5578 void spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts, 5579 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5580 { 5581 bs_create_blob(bs, opts, NULL, cb_fn, cb_arg); 5582 } 5583 5584 /* END spdk_bs_create_blob */ 5585 5586 /* START blob_cleanup */ 5587 5588 struct spdk_clone_snapshot_ctx { 5589 struct spdk_bs_cpl cpl; 5590 int bserrno; 5591 bool frozen; 5592 5593 struct spdk_io_channel *channel; 5594 5595 /* Current cluster for inflate operation */ 5596 uint64_t cluster; 5597 5598 /* For inflation force allocation of all unallocated clusters and remove 5599 * thin-provisioning. Otherwise only decouple parent and keep clone thin. */ 5600 bool allocate_all; 5601 5602 struct { 5603 spdk_blob_id id; 5604 struct spdk_blob *blob; 5605 bool md_ro; 5606 } original; 5607 struct { 5608 spdk_blob_id id; 5609 struct spdk_blob *blob; 5610 } new; 5611 5612 /* xattrs specified for snapshot/clones only. They have no impact on 5613 * the original blobs xattrs. */ 5614 const struct spdk_blob_xattr_opts *xattrs; 5615 }; 5616 5617 static void 5618 bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno) 5619 { 5620 struct spdk_clone_snapshot_ctx *ctx = cb_arg; 5621 struct spdk_bs_cpl *cpl = &ctx->cpl; 5622 5623 if (bserrno != 0) { 5624 if (ctx->bserrno != 0) { 5625 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5626 } else { 5627 ctx->bserrno = bserrno; 5628 } 5629 } 5630 5631 switch (cpl->type) { 5632 case SPDK_BS_CPL_TYPE_BLOBID: 5633 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno); 5634 break; 5635 case SPDK_BS_CPL_TYPE_BLOB_BASIC: 5636 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno); 5637 break; 5638 default: 5639 SPDK_UNREACHABLE(); 5640 break; 5641 } 5642 5643 free(ctx); 5644 } 5645 5646 static void 5647 bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 5648 { 5649 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5650 struct spdk_blob *origblob = ctx->original.blob; 5651 5652 if (bserrno != 0) { 5653 if (ctx->bserrno != 0) { 5654 SPDK_ERRLOG("Unfreeze error %d\n", bserrno); 5655 } else { 5656 ctx->bserrno = bserrno; 5657 } 5658 } 5659 5660 ctx->original.id = origblob->id; 5661 origblob->locked_operation_in_progress = false; 5662 5663 /* Revert md_ro to original state */ 5664 origblob->md_ro = ctx->original.md_ro; 5665 5666 spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx); 5667 } 5668 5669 static void 5670 bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno) 5671 { 5672 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5673 struct spdk_blob *origblob = ctx->original.blob; 5674 5675 if (bserrno != 0) { 5676 if (ctx->bserrno != 0) { 5677 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5678 } else { 5679 ctx->bserrno = bserrno; 5680 } 5681 } 5682 5683 if (ctx->frozen) { 5684 /* Unfreeze any outstanding I/O */ 5685 blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx); 5686 } else { 5687 bs_snapshot_unfreeze_cpl(ctx, 0); 5688 } 5689 5690 } 5691 5692 static void 5693 bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno) 5694 { 5695 struct spdk_blob *newblob = ctx->new.blob; 5696 5697 if (bserrno != 0) { 5698 if (ctx->bserrno != 0) { 5699 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5700 } else { 5701 ctx->bserrno = bserrno; 5702 } 5703 } 5704 5705 ctx->new.id = newblob->id; 5706 spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx); 5707 } 5708 5709 /* END blob_cleanup */ 5710 5711 /* START spdk_bs_create_snapshot */ 5712 5713 static void 5714 bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2) 5715 { 5716 uint64_t *cluster_temp; 5717 uint32_t *extent_page_temp; 5718 5719 cluster_temp = blob1->active.clusters; 5720 blob1->active.clusters = blob2->active.clusters; 5721 blob2->active.clusters = cluster_temp; 5722 5723 extent_page_temp = blob1->active.extent_pages; 5724 blob1->active.extent_pages = blob2->active.extent_pages; 5725 blob2->active.extent_pages = extent_page_temp; 5726 } 5727 5728 static void 5729 bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno) 5730 { 5731 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5732 struct spdk_blob *origblob = ctx->original.blob; 5733 struct spdk_blob *newblob = ctx->new.blob; 5734 5735 if (bserrno != 0) { 5736 bs_snapshot_swap_cluster_maps(newblob, origblob); 5737 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5738 return; 5739 } 5740 5741 /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */ 5742 bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true); 5743 if (bserrno != 0) { 5744 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5745 return; 5746 } 5747 5748 bs_blob_list_add(ctx->original.blob); 5749 5750 spdk_blob_set_read_only(newblob); 5751 5752 /* sync snapshot metadata */ 5753 spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx); 5754 } 5755 5756 static void 5757 bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno) 5758 { 5759 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5760 struct spdk_blob *origblob = ctx->original.blob; 5761 struct spdk_blob *newblob = ctx->new.blob; 5762 5763 if (bserrno != 0) { 5764 /* return cluster map back to original */ 5765 bs_snapshot_swap_cluster_maps(newblob, origblob); 5766 5767 /* Newblob md sync failed. Valid clusters are only present in origblob. 5768 * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred. 5769 * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */ 5770 blob_set_thin_provision(newblob); 5771 assert(spdk_mem_all_zero(newblob->active.clusters, 5772 newblob->active.num_clusters * sizeof(*newblob->active.clusters))); 5773 assert(spdk_mem_all_zero(newblob->active.extent_pages, 5774 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages))); 5775 5776 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 5777 return; 5778 } 5779 5780 /* Set internal xattr for snapshot id */ 5781 bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true); 5782 if (bserrno != 0) { 5783 /* return cluster map back to original */ 5784 bs_snapshot_swap_cluster_maps(newblob, origblob); 5785 blob_set_thin_provision(newblob); 5786 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 5787 return; 5788 } 5789 5790 /* Create new back_bs_dev for snapshot */ 5791 origblob->back_bs_dev = bs_create_blob_bs_dev(newblob); 5792 if (origblob->back_bs_dev == NULL) { 5793 /* return cluster map back to original */ 5794 bs_snapshot_swap_cluster_maps(newblob, origblob); 5795 blob_set_thin_provision(newblob); 5796 bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL); 5797 return; 5798 } 5799 5800 bs_blob_list_remove(origblob); 5801 origblob->parent_id = newblob->id; 5802 /* set clone blob as thin provisioned */ 5803 blob_set_thin_provision(origblob); 5804 5805 bs_blob_list_add(newblob); 5806 5807 /* sync clone metadata */ 5808 spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx); 5809 } 5810 5811 static void 5812 bs_snapshot_freeze_cpl(void *cb_arg, int rc) 5813 { 5814 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5815 struct spdk_blob *origblob = ctx->original.blob; 5816 struct spdk_blob *newblob = ctx->new.blob; 5817 int bserrno; 5818 5819 if (rc != 0) { 5820 bs_clone_snapshot_newblob_cleanup(ctx, rc); 5821 return; 5822 } 5823 5824 ctx->frozen = true; 5825 5826 /* set new back_bs_dev for snapshot */ 5827 newblob->back_bs_dev = origblob->back_bs_dev; 5828 /* Set invalid flags from origblob */ 5829 newblob->invalid_flags = origblob->invalid_flags; 5830 5831 /* inherit parent from original blob if set */ 5832 newblob->parent_id = origblob->parent_id; 5833 if (origblob->parent_id != SPDK_BLOBID_INVALID) { 5834 /* Set internal xattr for snapshot id */ 5835 bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT, 5836 &origblob->parent_id, sizeof(spdk_blob_id), true); 5837 if (bserrno != 0) { 5838 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 5839 return; 5840 } 5841 } 5842 5843 /* swap cluster maps */ 5844 bs_snapshot_swap_cluster_maps(newblob, origblob); 5845 5846 /* Set the clear method on the new blob to match the original. */ 5847 blob_set_clear_method(newblob, origblob->clear_method); 5848 5849 /* sync snapshot metadata */ 5850 spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx); 5851 } 5852 5853 static void 5854 bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5855 { 5856 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5857 struct spdk_blob *origblob = ctx->original.blob; 5858 struct spdk_blob *newblob = _blob; 5859 5860 if (bserrno != 0) { 5861 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5862 return; 5863 } 5864 5865 ctx->new.blob = newblob; 5866 assert(spdk_blob_is_thin_provisioned(newblob)); 5867 assert(spdk_mem_all_zero(newblob->active.clusters, 5868 newblob->active.num_clusters * sizeof(*newblob->active.clusters))); 5869 assert(spdk_mem_all_zero(newblob->active.extent_pages, 5870 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages))); 5871 5872 blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx); 5873 } 5874 5875 static void 5876 bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 5877 { 5878 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5879 struct spdk_blob *origblob = ctx->original.blob; 5880 5881 if (bserrno != 0) { 5882 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5883 return; 5884 } 5885 5886 ctx->new.id = blobid; 5887 ctx->cpl.u.blobid.blobid = blobid; 5888 5889 spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx); 5890 } 5891 5892 5893 static void 5894 bs_xattr_snapshot(void *arg, const char *name, 5895 const void **value, size_t *value_len) 5896 { 5897 assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0); 5898 5899 struct spdk_blob *blob = (struct spdk_blob *)arg; 5900 *value = &blob->id; 5901 *value_len = sizeof(blob->id); 5902 } 5903 5904 static void 5905 bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5906 { 5907 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5908 struct spdk_blob_opts opts; 5909 struct spdk_blob_xattr_opts internal_xattrs; 5910 char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS }; 5911 5912 if (bserrno != 0) { 5913 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 5914 return; 5915 } 5916 5917 ctx->original.blob = _blob; 5918 5919 if (_blob->data_ro || _blob->md_ro) { 5920 SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id %" PRIu64 "\n", 5921 _blob->id); 5922 ctx->bserrno = -EINVAL; 5923 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 5924 return; 5925 } 5926 5927 if (_blob->locked_operation_in_progress) { 5928 SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n"); 5929 ctx->bserrno = -EBUSY; 5930 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 5931 return; 5932 } 5933 5934 _blob->locked_operation_in_progress = true; 5935 5936 spdk_blob_opts_init(&opts, sizeof(opts)); 5937 blob_xattrs_init(&internal_xattrs); 5938 5939 /* Change the size of new blob to the same as in original blob, 5940 * but do not allocate clusters */ 5941 opts.thin_provision = true; 5942 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 5943 opts.use_extent_table = _blob->use_extent_table; 5944 5945 /* If there are any xattrs specified for snapshot, set them now */ 5946 if (ctx->xattrs) { 5947 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 5948 } 5949 /* Set internal xattr SNAPSHOT_IN_PROGRESS */ 5950 internal_xattrs.count = 1; 5951 internal_xattrs.ctx = _blob; 5952 internal_xattrs.names = xattrs_names; 5953 internal_xattrs.get_value = bs_xattr_snapshot; 5954 5955 bs_create_blob(_blob->bs, &opts, &internal_xattrs, 5956 bs_snapshot_newblob_create_cpl, ctx); 5957 } 5958 5959 void spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid, 5960 const struct spdk_blob_xattr_opts *snapshot_xattrs, 5961 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5962 { 5963 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 5964 5965 if (!ctx) { 5966 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 5967 return; 5968 } 5969 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 5970 ctx->cpl.u.blobid.cb_fn = cb_fn; 5971 ctx->cpl.u.blobid.cb_arg = cb_arg; 5972 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 5973 ctx->bserrno = 0; 5974 ctx->frozen = false; 5975 ctx->original.id = blobid; 5976 ctx->xattrs = snapshot_xattrs; 5977 5978 spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx); 5979 } 5980 /* END spdk_bs_create_snapshot */ 5981 5982 /* START spdk_bs_create_clone */ 5983 5984 static void 5985 bs_xattr_clone(void *arg, const char *name, 5986 const void **value, size_t *value_len) 5987 { 5988 assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0); 5989 5990 struct spdk_blob *blob = (struct spdk_blob *)arg; 5991 *value = &blob->id; 5992 *value_len = sizeof(blob->id); 5993 } 5994 5995 static void 5996 bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5997 { 5998 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5999 struct spdk_blob *clone = _blob; 6000 6001 ctx->new.blob = clone; 6002 bs_blob_list_add(clone); 6003 6004 spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx); 6005 } 6006 6007 static void 6008 bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 6009 { 6010 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6011 6012 ctx->cpl.u.blobid.blobid = blobid; 6013 spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx); 6014 } 6015 6016 static void 6017 bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6018 { 6019 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6020 struct spdk_blob_opts opts; 6021 struct spdk_blob_xattr_opts internal_xattrs; 6022 char *xattr_names[] = { BLOB_SNAPSHOT }; 6023 6024 if (bserrno != 0) { 6025 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 6026 return; 6027 } 6028 6029 ctx->original.blob = _blob; 6030 ctx->original.md_ro = _blob->md_ro; 6031 6032 if (!_blob->data_ro || !_blob->md_ro) { 6033 SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n"); 6034 ctx->bserrno = -EINVAL; 6035 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6036 return; 6037 } 6038 6039 if (_blob->locked_operation_in_progress) { 6040 SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n"); 6041 ctx->bserrno = -EBUSY; 6042 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6043 return; 6044 } 6045 6046 _blob->locked_operation_in_progress = true; 6047 6048 spdk_blob_opts_init(&opts, sizeof(opts)); 6049 blob_xattrs_init(&internal_xattrs); 6050 6051 opts.thin_provision = true; 6052 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 6053 opts.use_extent_table = _blob->use_extent_table; 6054 if (ctx->xattrs) { 6055 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 6056 } 6057 6058 /* Set internal xattr BLOB_SNAPSHOT */ 6059 internal_xattrs.count = 1; 6060 internal_xattrs.ctx = _blob; 6061 internal_xattrs.names = xattr_names; 6062 internal_xattrs.get_value = bs_xattr_clone; 6063 6064 bs_create_blob(_blob->bs, &opts, &internal_xattrs, 6065 bs_clone_newblob_create_cpl, ctx); 6066 } 6067 6068 void spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid, 6069 const struct spdk_blob_xattr_opts *clone_xattrs, 6070 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 6071 { 6072 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6073 6074 if (!ctx) { 6075 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 6076 return; 6077 } 6078 6079 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 6080 ctx->cpl.u.blobid.cb_fn = cb_fn; 6081 ctx->cpl.u.blobid.cb_arg = cb_arg; 6082 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 6083 ctx->bserrno = 0; 6084 ctx->xattrs = clone_xattrs; 6085 ctx->original.id = blobid; 6086 6087 spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx); 6088 } 6089 6090 /* END spdk_bs_create_clone */ 6091 6092 /* START spdk_bs_inflate_blob */ 6093 6094 static void 6095 bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno) 6096 { 6097 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6098 struct spdk_blob *_blob = ctx->original.blob; 6099 6100 if (bserrno != 0) { 6101 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6102 return; 6103 } 6104 6105 /* Temporarily override md_ro flag for MD modification */ 6106 _blob->md_ro = false; 6107 6108 bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true); 6109 if (bserrno != 0) { 6110 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6111 return; 6112 } 6113 6114 assert(_parent != NULL); 6115 6116 bs_blob_list_remove(_blob); 6117 _blob->parent_id = _parent->id; 6118 6119 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6120 _blob->back_bs_dev = bs_create_blob_bs_dev(_parent); 6121 bs_blob_list_add(_blob); 6122 6123 spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx); 6124 } 6125 6126 static void 6127 bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx) 6128 { 6129 struct spdk_blob *_blob = ctx->original.blob; 6130 struct spdk_blob *_parent; 6131 6132 if (ctx->allocate_all) { 6133 /* remove thin provisioning */ 6134 bs_blob_list_remove(_blob); 6135 blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 6136 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV; 6137 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6138 _blob->back_bs_dev = NULL; 6139 _blob->parent_id = SPDK_BLOBID_INVALID; 6140 } else { 6141 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob; 6142 if (_parent->parent_id != SPDK_BLOBID_INVALID) { 6143 /* We must change the parent of the inflated blob */ 6144 spdk_bs_open_blob(_blob->bs, _parent->parent_id, 6145 bs_inflate_blob_set_parent_cpl, ctx); 6146 return; 6147 } 6148 6149 bs_blob_list_remove(_blob); 6150 blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 6151 _blob->parent_id = SPDK_BLOBID_INVALID; 6152 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6153 _blob->back_bs_dev = bs_create_zeroes_dev(); 6154 } 6155 6156 /* Temporarily override md_ro flag for MD modification */ 6157 _blob->md_ro = false; 6158 _blob->state = SPDK_BLOB_STATE_DIRTY; 6159 6160 spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx); 6161 } 6162 6163 /* Check if cluster needs allocation */ 6164 static inline bool 6165 bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all) 6166 { 6167 struct spdk_blob_bs_dev *b; 6168 6169 assert(blob != NULL); 6170 6171 if (blob->active.clusters[cluster] != 0) { 6172 /* Cluster is already allocated */ 6173 return false; 6174 } 6175 6176 if (blob->parent_id == SPDK_BLOBID_INVALID) { 6177 /* Blob have no parent blob */ 6178 return allocate_all; 6179 } 6180 6181 b = (struct spdk_blob_bs_dev *)blob->back_bs_dev; 6182 return (allocate_all || b->blob->active.clusters[cluster] != 0); 6183 } 6184 6185 static void 6186 bs_inflate_blob_touch_next(void *cb_arg, int bserrno) 6187 { 6188 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6189 struct spdk_blob *_blob = ctx->original.blob; 6190 struct spdk_bs_cpl cpl; 6191 spdk_bs_user_op_t *op; 6192 uint64_t offset; 6193 6194 if (bserrno != 0) { 6195 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6196 return; 6197 } 6198 6199 for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) { 6200 if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) { 6201 break; 6202 } 6203 } 6204 6205 if (ctx->cluster < _blob->active.num_clusters) { 6206 offset = bs_cluster_to_lba(_blob->bs, ctx->cluster); 6207 6208 /* We may safely increment a cluster before copying */ 6209 ctx->cluster++; 6210 6211 /* Use a dummy 0B read as a context for cluster copy */ 6212 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6213 cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next; 6214 cpl.u.blob_basic.cb_arg = ctx; 6215 6216 op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob, 6217 NULL, 0, offset, 0); 6218 if (!op) { 6219 bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM); 6220 return; 6221 } 6222 6223 bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op); 6224 } else { 6225 bs_inflate_blob_done(ctx); 6226 } 6227 } 6228 6229 static void 6230 bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6231 { 6232 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6233 uint64_t clusters_needed; 6234 uint64_t i; 6235 6236 if (bserrno != 0) { 6237 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 6238 return; 6239 } 6240 6241 ctx->original.blob = _blob; 6242 ctx->original.md_ro = _blob->md_ro; 6243 6244 if (_blob->locked_operation_in_progress) { 6245 SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n"); 6246 ctx->bserrno = -EBUSY; 6247 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6248 return; 6249 } 6250 6251 _blob->locked_operation_in_progress = true; 6252 6253 if (!ctx->allocate_all && _blob->parent_id == SPDK_BLOBID_INVALID) { 6254 /* This blob have no parent, so we cannot decouple it. */ 6255 SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n"); 6256 bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 6257 return; 6258 } 6259 6260 if (spdk_blob_is_thin_provisioned(_blob) == false) { 6261 /* This is not thin provisioned blob. No need to inflate. */ 6262 bs_clone_snapshot_origblob_cleanup(ctx, 0); 6263 return; 6264 } 6265 6266 /* Do two passes - one to verify that we can obtain enough clusters 6267 * and another to actually claim them. 6268 */ 6269 clusters_needed = 0; 6270 for (i = 0; i < _blob->active.num_clusters; i++) { 6271 if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) { 6272 clusters_needed++; 6273 } 6274 } 6275 6276 if (clusters_needed > _blob->bs->num_free_clusters) { 6277 /* Not enough free clusters. Cannot satisfy the request. */ 6278 bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC); 6279 return; 6280 } 6281 6282 ctx->cluster = 0; 6283 bs_inflate_blob_touch_next(ctx, 0); 6284 } 6285 6286 static void 6287 bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6288 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg) 6289 { 6290 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6291 6292 if (!ctx) { 6293 cb_fn(cb_arg, -ENOMEM); 6294 return; 6295 } 6296 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6297 ctx->cpl.u.bs_basic.cb_fn = cb_fn; 6298 ctx->cpl.u.bs_basic.cb_arg = cb_arg; 6299 ctx->bserrno = 0; 6300 ctx->original.id = blobid; 6301 ctx->channel = channel; 6302 ctx->allocate_all = allocate_all; 6303 6304 spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx); 6305 } 6306 6307 void 6308 spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6309 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 6310 { 6311 bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg); 6312 } 6313 6314 void 6315 spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6316 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 6317 { 6318 bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg); 6319 } 6320 /* END spdk_bs_inflate_blob */ 6321 6322 /* START spdk_blob_resize */ 6323 struct spdk_bs_resize_ctx { 6324 spdk_blob_op_complete cb_fn; 6325 void *cb_arg; 6326 struct spdk_blob *blob; 6327 uint64_t sz; 6328 int rc; 6329 }; 6330 6331 static void 6332 bs_resize_unfreeze_cpl(void *cb_arg, int rc) 6333 { 6334 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 6335 6336 if (rc != 0) { 6337 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc); 6338 } 6339 6340 if (ctx->rc != 0) { 6341 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc); 6342 rc = ctx->rc; 6343 } 6344 6345 ctx->blob->locked_operation_in_progress = false; 6346 6347 ctx->cb_fn(ctx->cb_arg, rc); 6348 free(ctx); 6349 } 6350 6351 static void 6352 bs_resize_freeze_cpl(void *cb_arg, int rc) 6353 { 6354 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 6355 6356 if (rc != 0) { 6357 ctx->blob->locked_operation_in_progress = false; 6358 ctx->cb_fn(ctx->cb_arg, rc); 6359 free(ctx); 6360 return; 6361 } 6362 6363 ctx->rc = blob_resize(ctx->blob, ctx->sz); 6364 6365 blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx); 6366 } 6367 6368 void 6369 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg) 6370 { 6371 struct spdk_bs_resize_ctx *ctx; 6372 6373 blob_verify_md_op(blob); 6374 6375 SPDK_DEBUGLOG(blob, "Resizing blob %" PRIu64 " to %" PRIu64 " clusters\n", blob->id, sz); 6376 6377 if (blob->md_ro) { 6378 cb_fn(cb_arg, -EPERM); 6379 return; 6380 } 6381 6382 if (sz == blob->active.num_clusters) { 6383 cb_fn(cb_arg, 0); 6384 return; 6385 } 6386 6387 if (blob->locked_operation_in_progress) { 6388 cb_fn(cb_arg, -EBUSY); 6389 return; 6390 } 6391 6392 ctx = calloc(1, sizeof(*ctx)); 6393 if (!ctx) { 6394 cb_fn(cb_arg, -ENOMEM); 6395 return; 6396 } 6397 6398 blob->locked_operation_in_progress = true; 6399 ctx->cb_fn = cb_fn; 6400 ctx->cb_arg = cb_arg; 6401 ctx->blob = blob; 6402 ctx->sz = sz; 6403 blob_freeze_io(blob, bs_resize_freeze_cpl, ctx); 6404 } 6405 6406 /* END spdk_blob_resize */ 6407 6408 6409 /* START spdk_bs_delete_blob */ 6410 6411 static void 6412 bs_delete_close_cpl(void *cb_arg, int bserrno) 6413 { 6414 spdk_bs_sequence_t *seq = cb_arg; 6415 6416 bs_sequence_finish(seq, bserrno); 6417 } 6418 6419 static void 6420 bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 6421 { 6422 struct spdk_blob *blob = cb_arg; 6423 6424 if (bserrno != 0) { 6425 /* 6426 * We already removed this blob from the blobstore tailq, so 6427 * we need to free it here since this is the last reference 6428 * to it. 6429 */ 6430 blob_free(blob); 6431 bs_delete_close_cpl(seq, bserrno); 6432 return; 6433 } 6434 6435 /* 6436 * This will immediately decrement the ref_count and call 6437 * the completion routine since the metadata state is clean. 6438 * By calling spdk_blob_close, we reduce the number of call 6439 * points into code that touches the blob->open_ref count 6440 * and the blobstore's blob list. 6441 */ 6442 spdk_blob_close(blob, bs_delete_close_cpl, seq); 6443 } 6444 6445 struct delete_snapshot_ctx { 6446 struct spdk_blob_list *parent_snapshot_entry; 6447 struct spdk_blob *snapshot; 6448 bool snapshot_md_ro; 6449 struct spdk_blob *clone; 6450 bool clone_md_ro; 6451 spdk_blob_op_with_handle_complete cb_fn; 6452 void *cb_arg; 6453 int bserrno; 6454 uint32_t next_extent_page; 6455 }; 6456 6457 static void 6458 delete_blob_cleanup_finish(void *cb_arg, int bserrno) 6459 { 6460 struct delete_snapshot_ctx *ctx = cb_arg; 6461 6462 if (bserrno != 0) { 6463 SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno); 6464 } 6465 6466 assert(ctx != NULL); 6467 6468 if (bserrno != 0 && ctx->bserrno == 0) { 6469 ctx->bserrno = bserrno; 6470 } 6471 6472 ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno); 6473 free(ctx); 6474 } 6475 6476 static void 6477 delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno) 6478 { 6479 struct delete_snapshot_ctx *ctx = cb_arg; 6480 6481 if (bserrno != 0) { 6482 ctx->bserrno = bserrno; 6483 SPDK_ERRLOG("Clone cleanup error %d\n", bserrno); 6484 } 6485 6486 if (ctx->bserrno != 0) { 6487 assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL); 6488 RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot); 6489 spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id); 6490 } 6491 6492 ctx->snapshot->locked_operation_in_progress = false; 6493 ctx->snapshot->md_ro = ctx->snapshot_md_ro; 6494 6495 spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx); 6496 } 6497 6498 static void 6499 delete_snapshot_cleanup_clone(void *cb_arg, int bserrno) 6500 { 6501 struct delete_snapshot_ctx *ctx = cb_arg; 6502 6503 ctx->clone->locked_operation_in_progress = false; 6504 ctx->clone->md_ro = ctx->clone_md_ro; 6505 6506 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx); 6507 } 6508 6509 static void 6510 delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 6511 { 6512 struct delete_snapshot_ctx *ctx = cb_arg; 6513 6514 if (bserrno) { 6515 ctx->bserrno = bserrno; 6516 delete_snapshot_cleanup_clone(ctx, 0); 6517 return; 6518 } 6519 6520 ctx->clone->locked_operation_in_progress = false; 6521 spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx); 6522 } 6523 6524 static void 6525 delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno) 6526 { 6527 struct delete_snapshot_ctx *ctx = cb_arg; 6528 struct spdk_blob_list *parent_snapshot_entry = NULL; 6529 struct spdk_blob_list *snapshot_entry = NULL; 6530 struct spdk_blob_list *clone_entry = NULL; 6531 struct spdk_blob_list *snapshot_clone_entry = NULL; 6532 6533 if (bserrno) { 6534 SPDK_ERRLOG("Failed to sync MD on blob\n"); 6535 ctx->bserrno = bserrno; 6536 delete_snapshot_cleanup_clone(ctx, 0); 6537 return; 6538 } 6539 6540 /* Get snapshot entry for the snapshot we want to remove */ 6541 snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id); 6542 6543 assert(snapshot_entry != NULL); 6544 6545 /* Remove clone entry in this snapshot (at this point there can be only one clone) */ 6546 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6547 assert(clone_entry != NULL); 6548 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 6549 snapshot_entry->clone_count--; 6550 assert(TAILQ_EMPTY(&snapshot_entry->clones)); 6551 6552 if (ctx->snapshot->parent_id != SPDK_BLOBID_INVALID) { 6553 /* This snapshot is at the same time a clone of another snapshot - we need to 6554 * update parent snapshot (remove current clone, add new one inherited from 6555 * the snapshot that is being removed) */ 6556 6557 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for 6558 * snapshot that we are removing */ 6559 blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry, 6560 &snapshot_clone_entry); 6561 6562 /* Switch clone entry in parent snapshot */ 6563 TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link); 6564 TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link); 6565 free(snapshot_clone_entry); 6566 } else { 6567 /* No parent snapshot - just remove clone entry */ 6568 free(clone_entry); 6569 } 6570 6571 /* Restore md_ro flags */ 6572 ctx->clone->md_ro = ctx->clone_md_ro; 6573 ctx->snapshot->md_ro = ctx->snapshot_md_ro; 6574 6575 blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx); 6576 } 6577 6578 static void 6579 delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno) 6580 { 6581 struct delete_snapshot_ctx *ctx = cb_arg; 6582 uint64_t i; 6583 6584 ctx->snapshot->md_ro = false; 6585 6586 if (bserrno) { 6587 SPDK_ERRLOG("Failed to sync MD on clone\n"); 6588 ctx->bserrno = bserrno; 6589 6590 /* Restore snapshot to previous state */ 6591 bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true); 6592 if (bserrno != 0) { 6593 delete_snapshot_cleanup_clone(ctx, bserrno); 6594 return; 6595 } 6596 6597 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx); 6598 return; 6599 } 6600 6601 /* Clear cluster map entries for snapshot */ 6602 for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) { 6603 if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) { 6604 ctx->snapshot->active.clusters[i] = 0; 6605 } 6606 } 6607 for (i = 0; i < ctx->snapshot->active.num_extent_pages && 6608 i < ctx->clone->active.num_extent_pages; i++) { 6609 if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) { 6610 ctx->snapshot->active.extent_pages[i] = 0; 6611 } 6612 } 6613 6614 blob_set_thin_provision(ctx->snapshot); 6615 ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY; 6616 6617 if (ctx->parent_snapshot_entry != NULL) { 6618 ctx->snapshot->back_bs_dev = NULL; 6619 } 6620 6621 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx); 6622 } 6623 6624 static void 6625 delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx) 6626 { 6627 /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */ 6628 ctx->clone->back_bs_dev->destroy(ctx->clone->back_bs_dev); 6629 6630 /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */ 6631 if (ctx->parent_snapshot_entry != NULL) { 6632 /* ...to parent snapshot */ 6633 ctx->clone->parent_id = ctx->parent_snapshot_entry->id; 6634 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev; 6635 blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id, 6636 sizeof(spdk_blob_id), 6637 true); 6638 } else { 6639 /* ...to blobid invalid and zeroes dev */ 6640 ctx->clone->parent_id = SPDK_BLOBID_INVALID; 6641 ctx->clone->back_bs_dev = bs_create_zeroes_dev(); 6642 blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true); 6643 } 6644 6645 spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx); 6646 } 6647 6648 static void 6649 delete_snapshot_update_extent_pages(void *cb_arg, int bserrno) 6650 { 6651 struct delete_snapshot_ctx *ctx = cb_arg; 6652 uint32_t *extent_page; 6653 uint64_t i; 6654 6655 for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages && 6656 i < ctx->clone->active.num_extent_pages; i++) { 6657 if (ctx->snapshot->active.extent_pages[i] == 0) { 6658 /* No extent page to use from snapshot */ 6659 continue; 6660 } 6661 6662 extent_page = &ctx->clone->active.extent_pages[i]; 6663 if (*extent_page == 0) { 6664 /* Copy extent page from snapshot when clone did not have a matching one */ 6665 *extent_page = ctx->snapshot->active.extent_pages[i]; 6666 continue; 6667 } 6668 6669 /* Clone and snapshot both contain partially filled matching extent pages. 6670 * Update the clone extent page in place with cluster map containing the mix of both. */ 6671 ctx->next_extent_page = i + 1; 6672 6673 blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, 6674 delete_snapshot_update_extent_pages, ctx); 6675 return; 6676 } 6677 delete_snapshot_update_extent_pages_cpl(ctx); 6678 } 6679 6680 static void 6681 delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno) 6682 { 6683 struct delete_snapshot_ctx *ctx = cb_arg; 6684 uint64_t i; 6685 6686 /* Temporarily override md_ro flag for clone for MD modification */ 6687 ctx->clone_md_ro = ctx->clone->md_ro; 6688 ctx->clone->md_ro = false; 6689 6690 if (bserrno) { 6691 SPDK_ERRLOG("Failed to sync MD with xattr on blob\n"); 6692 ctx->bserrno = bserrno; 6693 delete_snapshot_cleanup_clone(ctx, 0); 6694 return; 6695 } 6696 6697 /* Copy snapshot map to clone map (only unallocated clusters in clone) */ 6698 for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) { 6699 if (ctx->clone->active.clusters[i] == 0) { 6700 ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i]; 6701 } 6702 } 6703 ctx->next_extent_page = 0; 6704 delete_snapshot_update_extent_pages(ctx, 0); 6705 } 6706 6707 static void 6708 delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno) 6709 { 6710 struct delete_snapshot_ctx *ctx = cb_arg; 6711 6712 if (bserrno) { 6713 SPDK_ERRLOG("Failed to freeze I/O on clone\n"); 6714 ctx->bserrno = bserrno; 6715 delete_snapshot_cleanup_clone(ctx, 0); 6716 return; 6717 } 6718 6719 /* Temporarily override md_ro flag for snapshot for MD modification */ 6720 ctx->snapshot_md_ro = ctx->snapshot->md_ro; 6721 ctx->snapshot->md_ro = false; 6722 6723 /* Mark blob as pending for removal for power failure safety, use clone id for recovery */ 6724 ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id, 6725 sizeof(spdk_blob_id), true); 6726 if (ctx->bserrno != 0) { 6727 delete_snapshot_cleanup_clone(ctx, 0); 6728 return; 6729 } 6730 6731 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx); 6732 } 6733 6734 static void 6735 delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno) 6736 { 6737 struct delete_snapshot_ctx *ctx = cb_arg; 6738 6739 if (bserrno) { 6740 SPDK_ERRLOG("Failed to open clone\n"); 6741 ctx->bserrno = bserrno; 6742 delete_snapshot_cleanup_snapshot(ctx, 0); 6743 return; 6744 } 6745 6746 ctx->clone = clone; 6747 6748 if (clone->locked_operation_in_progress) { 6749 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n"); 6750 ctx->bserrno = -EBUSY; 6751 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx); 6752 return; 6753 } 6754 6755 clone->locked_operation_in_progress = true; 6756 6757 blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx); 6758 } 6759 6760 static void 6761 update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx) 6762 { 6763 struct spdk_blob_list *snapshot_entry = NULL; 6764 struct spdk_blob_list *clone_entry = NULL; 6765 struct spdk_blob_list *snapshot_clone_entry = NULL; 6766 6767 /* Get snapshot entry for the snapshot we want to remove */ 6768 snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id); 6769 6770 assert(snapshot_entry != NULL); 6771 6772 /* Get clone of the snapshot (at this point there can be only one clone) */ 6773 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6774 assert(snapshot_entry->clone_count == 1); 6775 assert(clone_entry != NULL); 6776 6777 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for 6778 * snapshot that we are removing */ 6779 blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry, 6780 &snapshot_clone_entry); 6781 6782 spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx); 6783 } 6784 6785 static void 6786 bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno) 6787 { 6788 spdk_bs_sequence_t *seq = cb_arg; 6789 struct spdk_blob_list *snapshot_entry = NULL; 6790 uint32_t page_num; 6791 6792 if (bserrno) { 6793 SPDK_ERRLOG("Failed to remove blob\n"); 6794 bs_sequence_finish(seq, bserrno); 6795 return; 6796 } 6797 6798 /* Remove snapshot from the list */ 6799 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 6800 if (snapshot_entry != NULL) { 6801 TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link); 6802 free(snapshot_entry); 6803 } 6804 6805 page_num = bs_blobid_to_page(blob->id); 6806 spdk_bit_array_clear(blob->bs->used_blobids, page_num); 6807 blob->state = SPDK_BLOB_STATE_DIRTY; 6808 blob->active.num_pages = 0; 6809 blob_resize(blob, 0); 6810 6811 blob_persist(seq, blob, bs_delete_persist_cpl, blob); 6812 } 6813 6814 static int 6815 bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone) 6816 { 6817 struct spdk_blob_list *snapshot_entry = NULL; 6818 struct spdk_blob_list *clone_entry = NULL; 6819 struct spdk_blob *clone = NULL; 6820 bool has_one_clone = false; 6821 6822 /* Check if this is a snapshot with clones */ 6823 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 6824 if (snapshot_entry != NULL) { 6825 if (snapshot_entry->clone_count > 1) { 6826 SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n"); 6827 return -EBUSY; 6828 } else if (snapshot_entry->clone_count == 1) { 6829 has_one_clone = true; 6830 } 6831 } 6832 6833 /* Check if someone has this blob open (besides this delete context): 6834 * - open_ref = 1 - only this context opened blob, so it is ok to remove it 6835 * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot 6836 * and that is ok, because we will update it accordingly */ 6837 if (blob->open_ref <= 2 && has_one_clone) { 6838 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6839 assert(clone_entry != NULL); 6840 clone = blob_lookup(blob->bs, clone_entry->id); 6841 6842 if (blob->open_ref == 2 && clone == NULL) { 6843 /* Clone is closed and someone else opened this blob */ 6844 SPDK_ERRLOG("Cannot remove snapshot because it is open\n"); 6845 return -EBUSY; 6846 } 6847 6848 *update_clone = true; 6849 return 0; 6850 } 6851 6852 if (blob->open_ref > 1) { 6853 SPDK_ERRLOG("Cannot remove snapshot because it is open\n"); 6854 return -EBUSY; 6855 } 6856 6857 assert(has_one_clone == false); 6858 *update_clone = false; 6859 return 0; 6860 } 6861 6862 static void 6863 bs_delete_enomem_close_cpl(void *cb_arg, int bserrno) 6864 { 6865 spdk_bs_sequence_t *seq = cb_arg; 6866 6867 bs_sequence_finish(seq, -ENOMEM); 6868 } 6869 6870 static void 6871 bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno) 6872 { 6873 spdk_bs_sequence_t *seq = cb_arg; 6874 struct delete_snapshot_ctx *ctx; 6875 bool update_clone = false; 6876 6877 if (bserrno != 0) { 6878 bs_sequence_finish(seq, bserrno); 6879 return; 6880 } 6881 6882 blob_verify_md_op(blob); 6883 6884 ctx = calloc(1, sizeof(*ctx)); 6885 if (ctx == NULL) { 6886 spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq); 6887 return; 6888 } 6889 6890 ctx->snapshot = blob; 6891 ctx->cb_fn = bs_delete_blob_finish; 6892 ctx->cb_arg = seq; 6893 6894 /* Check if blob can be removed and if it is a snapshot with clone on top of it */ 6895 ctx->bserrno = bs_is_blob_deletable(blob, &update_clone); 6896 if (ctx->bserrno) { 6897 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 6898 return; 6899 } 6900 6901 if (blob->locked_operation_in_progress) { 6902 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n"); 6903 ctx->bserrno = -EBUSY; 6904 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 6905 return; 6906 } 6907 6908 blob->locked_operation_in_progress = true; 6909 6910 /* 6911 * Remove the blob from the blob_store list now, to ensure it does not 6912 * get returned after this point by blob_lookup(). 6913 */ 6914 spdk_bit_array_clear(blob->bs->open_blobids, blob->id); 6915 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob); 6916 6917 if (update_clone) { 6918 /* This blob is a snapshot with active clone - update clone first */ 6919 update_clone_on_snapshot_deletion(blob, ctx); 6920 } else { 6921 /* This blob does not have any clones - just remove it */ 6922 bs_blob_list_remove(blob); 6923 bs_delete_blob_finish(seq, blob, 0); 6924 free(ctx); 6925 } 6926 } 6927 6928 void 6929 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 6930 spdk_blob_op_complete cb_fn, void *cb_arg) 6931 { 6932 struct spdk_bs_cpl cpl; 6933 spdk_bs_sequence_t *seq; 6934 6935 SPDK_DEBUGLOG(blob, "Deleting blob %" PRIu64 "\n", blobid); 6936 6937 assert(spdk_get_thread() == bs->md_thread); 6938 6939 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6940 cpl.u.blob_basic.cb_fn = cb_fn; 6941 cpl.u.blob_basic.cb_arg = cb_arg; 6942 6943 seq = bs_sequence_start(bs->md_channel, &cpl); 6944 if (!seq) { 6945 cb_fn(cb_arg, -ENOMEM); 6946 return; 6947 } 6948 6949 spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq); 6950 } 6951 6952 /* END spdk_bs_delete_blob */ 6953 6954 /* START spdk_bs_open_blob */ 6955 6956 static void 6957 bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 6958 { 6959 struct spdk_blob *blob = cb_arg; 6960 struct spdk_blob *existing; 6961 6962 if (bserrno != 0) { 6963 blob_free(blob); 6964 seq->cpl.u.blob_handle.blob = NULL; 6965 bs_sequence_finish(seq, bserrno); 6966 return; 6967 } 6968 6969 existing = blob_lookup(blob->bs, blob->id); 6970 if (existing) { 6971 blob_free(blob); 6972 existing->open_ref++; 6973 seq->cpl.u.blob_handle.blob = existing; 6974 bs_sequence_finish(seq, 0); 6975 return; 6976 } 6977 6978 blob->open_ref++; 6979 6980 spdk_bit_array_set(blob->bs->open_blobids, blob->id); 6981 RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob); 6982 6983 bs_sequence_finish(seq, bserrno); 6984 } 6985 6986 static inline void 6987 blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst) 6988 { 6989 #define FIELD_OK(field) \ 6990 offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size 6991 6992 #define SET_FIELD(field) \ 6993 if (FIELD_OK(field)) { \ 6994 dst->field = src->field; \ 6995 } \ 6996 6997 SET_FIELD(clear_method); 6998 6999 dst->opts_size = src->opts_size; 7000 7001 /* You should not remove this statement, but need to update the assert statement 7002 * if you add a new field, and also add a corresponding SET_FIELD statement */ 7003 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 16, "Incorrect size"); 7004 7005 #undef FIELD_OK 7006 #undef SET_FIELD 7007 } 7008 7009 static void 7010 bs_open_blob(struct spdk_blob_store *bs, 7011 spdk_blob_id blobid, 7012 struct spdk_blob_open_opts *opts, 7013 spdk_blob_op_with_handle_complete cb_fn, 7014 void *cb_arg) 7015 { 7016 struct spdk_blob *blob; 7017 struct spdk_bs_cpl cpl; 7018 struct spdk_blob_open_opts opts_local; 7019 spdk_bs_sequence_t *seq; 7020 uint32_t page_num; 7021 7022 SPDK_DEBUGLOG(blob, "Opening blob %" PRIu64 "\n", blobid); 7023 assert(spdk_get_thread() == bs->md_thread); 7024 7025 page_num = bs_blobid_to_page(blobid); 7026 if (spdk_bit_array_get(bs->used_blobids, page_num) == false) { 7027 /* Invalid blobid */ 7028 cb_fn(cb_arg, NULL, -ENOENT); 7029 return; 7030 } 7031 7032 blob = blob_lookup(bs, blobid); 7033 if (blob) { 7034 blob->open_ref++; 7035 cb_fn(cb_arg, blob, 0); 7036 return; 7037 } 7038 7039 blob = blob_alloc(bs, blobid); 7040 if (!blob) { 7041 cb_fn(cb_arg, NULL, -ENOMEM); 7042 return; 7043 } 7044 7045 spdk_blob_open_opts_init(&opts_local, sizeof(opts_local)); 7046 if (opts) { 7047 blob_open_opts_copy(opts, &opts_local); 7048 } 7049 7050 blob->clear_method = opts_local.clear_method; 7051 7052 cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE; 7053 cpl.u.blob_handle.cb_fn = cb_fn; 7054 cpl.u.blob_handle.cb_arg = cb_arg; 7055 cpl.u.blob_handle.blob = blob; 7056 7057 seq = bs_sequence_start(bs->md_channel, &cpl); 7058 if (!seq) { 7059 blob_free(blob); 7060 cb_fn(cb_arg, NULL, -ENOMEM); 7061 return; 7062 } 7063 7064 blob_load(seq, blob, bs_open_blob_cpl, blob); 7065 } 7066 7067 void spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 7068 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7069 { 7070 bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg); 7071 } 7072 7073 void spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid, 7074 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7075 { 7076 bs_open_blob(bs, blobid, opts, cb_fn, cb_arg); 7077 } 7078 7079 /* END spdk_bs_open_blob */ 7080 7081 /* START spdk_blob_set_read_only */ 7082 int spdk_blob_set_read_only(struct spdk_blob *blob) 7083 { 7084 blob_verify_md_op(blob); 7085 7086 blob->data_ro_flags |= SPDK_BLOB_READ_ONLY; 7087 7088 blob->state = SPDK_BLOB_STATE_DIRTY; 7089 return 0; 7090 } 7091 /* END spdk_blob_set_read_only */ 7092 7093 /* START spdk_blob_sync_md */ 7094 7095 static void 7096 blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7097 { 7098 struct spdk_blob *blob = cb_arg; 7099 7100 if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 7101 blob->data_ro = true; 7102 blob->md_ro = true; 7103 } 7104 7105 bs_sequence_finish(seq, bserrno); 7106 } 7107 7108 static void 7109 blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7110 { 7111 struct spdk_bs_cpl cpl; 7112 spdk_bs_sequence_t *seq; 7113 7114 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7115 cpl.u.blob_basic.cb_fn = cb_fn; 7116 cpl.u.blob_basic.cb_arg = cb_arg; 7117 7118 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7119 if (!seq) { 7120 cb_fn(cb_arg, -ENOMEM); 7121 return; 7122 } 7123 7124 blob_persist(seq, blob, blob_sync_md_cpl, blob); 7125 } 7126 7127 void 7128 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7129 { 7130 blob_verify_md_op(blob); 7131 7132 SPDK_DEBUGLOG(blob, "Syncing blob %" PRIu64 "\n", blob->id); 7133 7134 if (blob->md_ro) { 7135 assert(blob->state == SPDK_BLOB_STATE_CLEAN); 7136 cb_fn(cb_arg, 0); 7137 return; 7138 } 7139 7140 blob_sync_md(blob, cb_fn, cb_arg); 7141 } 7142 7143 /* END spdk_blob_sync_md */ 7144 7145 struct spdk_blob_insert_cluster_ctx { 7146 struct spdk_thread *thread; 7147 struct spdk_blob *blob; 7148 uint32_t cluster_num; /* cluster index in blob */ 7149 uint32_t cluster; /* cluster on disk */ 7150 uint32_t extent_page; /* extent page on disk */ 7151 int rc; 7152 spdk_blob_op_complete cb_fn; 7153 void *cb_arg; 7154 }; 7155 7156 static void 7157 blob_insert_cluster_msg_cpl(void *arg) 7158 { 7159 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7160 7161 ctx->cb_fn(ctx->cb_arg, ctx->rc); 7162 free(ctx); 7163 } 7164 7165 static void 7166 blob_insert_cluster_msg_cb(void *arg, int bserrno) 7167 { 7168 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7169 7170 ctx->rc = bserrno; 7171 spdk_thread_send_msg(ctx->thread, blob_insert_cluster_msg_cpl, ctx); 7172 } 7173 7174 static void 7175 blob_insert_new_ep_cb(void *arg, int bserrno) 7176 { 7177 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7178 uint32_t *extent_page; 7179 7180 extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num); 7181 *extent_page = ctx->extent_page; 7182 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 7183 blob_sync_md(ctx->blob, blob_insert_cluster_msg_cb, ctx); 7184 } 7185 7186 static void 7187 blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7188 { 7189 struct spdk_blob_md_page *page = cb_arg; 7190 7191 bs_sequence_finish(seq, bserrno); 7192 spdk_free(page); 7193 } 7194 7195 static void 7196 blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num, 7197 spdk_blob_op_complete cb_fn, void *cb_arg) 7198 { 7199 spdk_bs_sequence_t *seq; 7200 struct spdk_bs_cpl cpl; 7201 struct spdk_blob_md_page *page = NULL; 7202 uint32_t page_count = 0; 7203 int rc; 7204 7205 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7206 cpl.u.blob_basic.cb_fn = cb_fn; 7207 cpl.u.blob_basic.cb_arg = cb_arg; 7208 7209 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7210 if (!seq) { 7211 cb_fn(cb_arg, -ENOMEM); 7212 return; 7213 } 7214 rc = blob_serialize_add_page(blob, &page, &page_count, &page); 7215 if (rc < 0) { 7216 bs_sequence_finish(seq, rc); 7217 return; 7218 } 7219 7220 blob_serialize_extent_page(blob, cluster_num, page); 7221 7222 page->crc = blob_md_page_calc_crc(page); 7223 7224 assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true); 7225 7226 bs_sequence_write_dev(seq, page, bs_md_page_to_lba(blob->bs, extent), 7227 bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE), 7228 blob_persist_extent_page_cpl, page); 7229 } 7230 7231 static void 7232 blob_insert_cluster_msg(void *arg) 7233 { 7234 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7235 uint32_t *extent_page; 7236 7237 ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster); 7238 if (ctx->rc != 0) { 7239 spdk_thread_send_msg(ctx->thread, blob_insert_cluster_msg_cpl, ctx); 7240 return; 7241 } 7242 7243 if (ctx->blob->use_extent_table == false) { 7244 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */ 7245 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 7246 blob_sync_md(ctx->blob, blob_insert_cluster_msg_cb, ctx); 7247 return; 7248 } 7249 7250 extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num); 7251 if (*extent_page == 0) { 7252 /* Extent page requires allocation. 7253 * It was already claimed in the used_md_pages map and placed in ctx. */ 7254 assert(ctx->extent_page != 0); 7255 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true); 7256 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, 7257 blob_insert_new_ep_cb, ctx); 7258 } else { 7259 /* It is possible for original thread to allocate extent page for 7260 * different cluster in the same extent page. In such case proceed with 7261 * updating the existing extent page, but release the additional one. */ 7262 if (ctx->extent_page != 0) { 7263 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true); 7264 bs_release_md_page(ctx->blob->bs, ctx->extent_page); 7265 ctx->extent_page = 0; 7266 } 7267 /* Extent page already allocated. 7268 * Every cluster allocation, requires just an update of single extent page. */ 7269 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, 7270 blob_insert_cluster_msg_cb, ctx); 7271 } 7272 } 7273 7274 static void 7275 blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 7276 uint64_t cluster, uint32_t extent_page, spdk_blob_op_complete cb_fn, void *cb_arg) 7277 { 7278 struct spdk_blob_insert_cluster_ctx *ctx; 7279 7280 ctx = calloc(1, sizeof(*ctx)); 7281 if (ctx == NULL) { 7282 cb_fn(cb_arg, -ENOMEM); 7283 return; 7284 } 7285 7286 ctx->thread = spdk_get_thread(); 7287 ctx->blob = blob; 7288 ctx->cluster_num = cluster_num; 7289 ctx->cluster = cluster; 7290 ctx->extent_page = extent_page; 7291 ctx->cb_fn = cb_fn; 7292 ctx->cb_arg = cb_arg; 7293 7294 spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx); 7295 } 7296 7297 /* START spdk_blob_close */ 7298 7299 static void 7300 blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7301 { 7302 struct spdk_blob *blob = cb_arg; 7303 7304 if (bserrno == 0) { 7305 blob->open_ref--; 7306 if (blob->open_ref == 0) { 7307 /* 7308 * Blobs with active.num_pages == 0 are deleted blobs. 7309 * these blobs are removed from the blob_store list 7310 * when the deletion process starts - so don't try to 7311 * remove them again. 7312 */ 7313 if (blob->active.num_pages > 0) { 7314 spdk_bit_array_clear(blob->bs->open_blobids, blob->id); 7315 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob); 7316 } 7317 blob_free(blob); 7318 } 7319 } 7320 7321 bs_sequence_finish(seq, bserrno); 7322 } 7323 7324 void spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7325 { 7326 struct spdk_bs_cpl cpl; 7327 spdk_bs_sequence_t *seq; 7328 7329 blob_verify_md_op(blob); 7330 7331 SPDK_DEBUGLOG(blob, "Closing blob %" PRIu64 "\n", blob->id); 7332 7333 if (blob->open_ref == 0) { 7334 cb_fn(cb_arg, -EBADF); 7335 return; 7336 } 7337 7338 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7339 cpl.u.blob_basic.cb_fn = cb_fn; 7340 cpl.u.blob_basic.cb_arg = cb_arg; 7341 7342 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7343 if (!seq) { 7344 cb_fn(cb_arg, -ENOMEM); 7345 return; 7346 } 7347 7348 /* Sync metadata */ 7349 blob_persist(seq, blob, blob_close_cpl, blob); 7350 } 7351 7352 /* END spdk_blob_close */ 7353 7354 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 7355 { 7356 return spdk_get_io_channel(bs); 7357 } 7358 7359 void spdk_bs_free_io_channel(struct spdk_io_channel *channel) 7360 { 7361 spdk_put_io_channel(channel); 7362 } 7363 7364 void spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel, 7365 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 7366 { 7367 blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 7368 SPDK_BLOB_UNMAP); 7369 } 7370 7371 void spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel, 7372 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 7373 { 7374 blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 7375 SPDK_BLOB_WRITE_ZEROES); 7376 } 7377 7378 void spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel, 7379 void *payload, uint64_t offset, uint64_t length, 7380 spdk_blob_op_complete cb_fn, void *cb_arg) 7381 { 7382 blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 7383 SPDK_BLOB_WRITE); 7384 } 7385 7386 void spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel, 7387 void *payload, uint64_t offset, uint64_t length, 7388 spdk_blob_op_complete cb_fn, void *cb_arg) 7389 { 7390 blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 7391 SPDK_BLOB_READ); 7392 } 7393 7394 void spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel, 7395 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7396 spdk_blob_op_complete cb_fn, void *cb_arg) 7397 { 7398 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false); 7399 } 7400 7401 void spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel, 7402 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7403 spdk_blob_op_complete cb_fn, void *cb_arg) 7404 { 7405 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true); 7406 } 7407 7408 struct spdk_bs_iter_ctx { 7409 int64_t page_num; 7410 struct spdk_blob_store *bs; 7411 7412 spdk_blob_op_with_handle_complete cb_fn; 7413 void *cb_arg; 7414 }; 7415 7416 static void 7417 bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 7418 { 7419 struct spdk_bs_iter_ctx *ctx = cb_arg; 7420 struct spdk_blob_store *bs = ctx->bs; 7421 spdk_blob_id id; 7422 7423 if (bserrno == 0) { 7424 ctx->cb_fn(ctx->cb_arg, _blob, bserrno); 7425 free(ctx); 7426 return; 7427 } 7428 7429 ctx->page_num++; 7430 ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num); 7431 if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) { 7432 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT); 7433 free(ctx); 7434 return; 7435 } 7436 7437 id = bs_page_to_blobid(ctx->page_num); 7438 7439 spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx); 7440 } 7441 7442 void 7443 spdk_bs_iter_first(struct spdk_blob_store *bs, 7444 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7445 { 7446 struct spdk_bs_iter_ctx *ctx; 7447 7448 ctx = calloc(1, sizeof(*ctx)); 7449 if (!ctx) { 7450 cb_fn(cb_arg, NULL, -ENOMEM); 7451 return; 7452 } 7453 7454 ctx->page_num = -1; 7455 ctx->bs = bs; 7456 ctx->cb_fn = cb_fn; 7457 ctx->cb_arg = cb_arg; 7458 7459 bs_iter_cpl(ctx, NULL, -1); 7460 } 7461 7462 static void 7463 bs_iter_close_cpl(void *cb_arg, int bserrno) 7464 { 7465 struct spdk_bs_iter_ctx *ctx = cb_arg; 7466 7467 bs_iter_cpl(ctx, NULL, -1); 7468 } 7469 7470 void 7471 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob, 7472 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7473 { 7474 struct spdk_bs_iter_ctx *ctx; 7475 7476 assert(blob != NULL); 7477 7478 ctx = calloc(1, sizeof(*ctx)); 7479 if (!ctx) { 7480 cb_fn(cb_arg, NULL, -ENOMEM); 7481 return; 7482 } 7483 7484 ctx->page_num = bs_blobid_to_page(blob->id); 7485 ctx->bs = bs; 7486 ctx->cb_fn = cb_fn; 7487 ctx->cb_arg = cb_arg; 7488 7489 /* Close the existing blob */ 7490 spdk_blob_close(blob, bs_iter_close_cpl, ctx); 7491 } 7492 7493 static int 7494 blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 7495 uint16_t value_len, bool internal) 7496 { 7497 struct spdk_xattr_tailq *xattrs; 7498 struct spdk_xattr *xattr; 7499 size_t desc_size; 7500 void *tmp; 7501 7502 blob_verify_md_op(blob); 7503 7504 if (blob->md_ro) { 7505 return -EPERM; 7506 } 7507 7508 desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len; 7509 if (desc_size > SPDK_BS_MAX_DESC_SIZE) { 7510 SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name, 7511 desc_size, SPDK_BS_MAX_DESC_SIZE); 7512 return -ENOMEM; 7513 } 7514 7515 if (internal) { 7516 xattrs = &blob->xattrs_internal; 7517 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR; 7518 } else { 7519 xattrs = &blob->xattrs; 7520 } 7521 7522 TAILQ_FOREACH(xattr, xattrs, link) { 7523 if (!strcmp(name, xattr->name)) { 7524 tmp = malloc(value_len); 7525 if (!tmp) { 7526 return -ENOMEM; 7527 } 7528 7529 free(xattr->value); 7530 xattr->value_len = value_len; 7531 xattr->value = tmp; 7532 memcpy(xattr->value, value, value_len); 7533 7534 blob->state = SPDK_BLOB_STATE_DIRTY; 7535 7536 return 0; 7537 } 7538 } 7539 7540 xattr = calloc(1, sizeof(*xattr)); 7541 if (!xattr) { 7542 return -ENOMEM; 7543 } 7544 7545 xattr->name = strdup(name); 7546 if (!xattr->name) { 7547 free(xattr); 7548 return -ENOMEM; 7549 } 7550 7551 xattr->value_len = value_len; 7552 xattr->value = malloc(value_len); 7553 if (!xattr->value) { 7554 free(xattr->name); 7555 free(xattr); 7556 return -ENOMEM; 7557 } 7558 memcpy(xattr->value, value, value_len); 7559 TAILQ_INSERT_TAIL(xattrs, xattr, link); 7560 7561 blob->state = SPDK_BLOB_STATE_DIRTY; 7562 7563 return 0; 7564 } 7565 7566 int 7567 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 7568 uint16_t value_len) 7569 { 7570 return blob_set_xattr(blob, name, value, value_len, false); 7571 } 7572 7573 static int 7574 blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal) 7575 { 7576 struct spdk_xattr_tailq *xattrs; 7577 struct spdk_xattr *xattr; 7578 7579 blob_verify_md_op(blob); 7580 7581 if (blob->md_ro) { 7582 return -EPERM; 7583 } 7584 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 7585 7586 TAILQ_FOREACH(xattr, xattrs, link) { 7587 if (!strcmp(name, xattr->name)) { 7588 TAILQ_REMOVE(xattrs, xattr, link); 7589 free(xattr->value); 7590 free(xattr->name); 7591 free(xattr); 7592 7593 if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) { 7594 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR; 7595 } 7596 blob->state = SPDK_BLOB_STATE_DIRTY; 7597 7598 return 0; 7599 } 7600 } 7601 7602 return -ENOENT; 7603 } 7604 7605 int 7606 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name) 7607 { 7608 return blob_remove_xattr(blob, name, false); 7609 } 7610 7611 static int 7612 blob_get_xattr_value(struct spdk_blob *blob, const char *name, 7613 const void **value, size_t *value_len, bool internal) 7614 { 7615 struct spdk_xattr *xattr; 7616 struct spdk_xattr_tailq *xattrs; 7617 7618 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 7619 7620 TAILQ_FOREACH(xattr, xattrs, link) { 7621 if (!strcmp(name, xattr->name)) { 7622 *value = xattr->value; 7623 *value_len = xattr->value_len; 7624 return 0; 7625 } 7626 } 7627 return -ENOENT; 7628 } 7629 7630 int 7631 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 7632 const void **value, size_t *value_len) 7633 { 7634 blob_verify_md_op(blob); 7635 7636 return blob_get_xattr_value(blob, name, value, value_len, false); 7637 } 7638 7639 struct spdk_xattr_names { 7640 uint32_t count; 7641 const char *names[0]; 7642 }; 7643 7644 static int 7645 blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names) 7646 { 7647 struct spdk_xattr *xattr; 7648 int count = 0; 7649 7650 TAILQ_FOREACH(xattr, xattrs, link) { 7651 count++; 7652 } 7653 7654 *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *)); 7655 if (*names == NULL) { 7656 return -ENOMEM; 7657 } 7658 7659 TAILQ_FOREACH(xattr, xattrs, link) { 7660 (*names)->names[(*names)->count++] = xattr->name; 7661 } 7662 7663 return 0; 7664 } 7665 7666 int 7667 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names) 7668 { 7669 blob_verify_md_op(blob); 7670 7671 return blob_get_xattr_names(&blob->xattrs, names); 7672 } 7673 7674 uint32_t 7675 spdk_xattr_names_get_count(struct spdk_xattr_names *names) 7676 { 7677 assert(names != NULL); 7678 7679 return names->count; 7680 } 7681 7682 const char * 7683 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index) 7684 { 7685 if (index >= names->count) { 7686 return NULL; 7687 } 7688 7689 return names->names[index]; 7690 } 7691 7692 void 7693 spdk_xattr_names_free(struct spdk_xattr_names *names) 7694 { 7695 free(names); 7696 } 7697 7698 struct spdk_bs_type 7699 spdk_bs_get_bstype(struct spdk_blob_store *bs) 7700 { 7701 return bs->bstype; 7702 } 7703 7704 void 7705 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype) 7706 { 7707 memcpy(&bs->bstype, &bstype, sizeof(bstype)); 7708 } 7709 7710 bool 7711 spdk_blob_is_read_only(struct spdk_blob *blob) 7712 { 7713 assert(blob != NULL); 7714 return (blob->data_ro || blob->md_ro); 7715 } 7716 7717 bool 7718 spdk_blob_is_snapshot(struct spdk_blob *blob) 7719 { 7720 struct spdk_blob_list *snapshot_entry; 7721 7722 assert(blob != NULL); 7723 7724 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 7725 if (snapshot_entry == NULL) { 7726 return false; 7727 } 7728 7729 return true; 7730 } 7731 7732 bool 7733 spdk_blob_is_clone(struct spdk_blob *blob) 7734 { 7735 assert(blob != NULL); 7736 7737 if (blob->parent_id != SPDK_BLOBID_INVALID) { 7738 assert(spdk_blob_is_thin_provisioned(blob)); 7739 return true; 7740 } 7741 7742 return false; 7743 } 7744 7745 bool 7746 spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 7747 { 7748 assert(blob != NULL); 7749 return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 7750 } 7751 7752 static void 7753 blob_update_clear_method(struct spdk_blob *blob) 7754 { 7755 enum blob_clear_method stored_cm; 7756 7757 assert(blob != NULL); 7758 7759 /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored 7760 * in metadata previously. If something other than the default was 7761 * specified, ignore stored value and used what was passed in. 7762 */ 7763 stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT); 7764 7765 if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) { 7766 blob->clear_method = stored_cm; 7767 } else if (blob->clear_method != stored_cm) { 7768 SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n", 7769 blob->clear_method, stored_cm); 7770 } 7771 } 7772 7773 spdk_blob_id 7774 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id) 7775 { 7776 struct spdk_blob_list *snapshot_entry = NULL; 7777 struct spdk_blob_list *clone_entry = NULL; 7778 7779 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 7780 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 7781 if (clone_entry->id == blob_id) { 7782 return snapshot_entry->id; 7783 } 7784 } 7785 } 7786 7787 return SPDK_BLOBID_INVALID; 7788 } 7789 7790 int 7791 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids, 7792 size_t *count) 7793 { 7794 struct spdk_blob_list *snapshot_entry, *clone_entry; 7795 size_t n; 7796 7797 snapshot_entry = bs_get_snapshot_entry(bs, blobid); 7798 if (snapshot_entry == NULL) { 7799 *count = 0; 7800 return 0; 7801 } 7802 7803 if (ids == NULL || *count < snapshot_entry->clone_count) { 7804 *count = snapshot_entry->clone_count; 7805 return -ENOMEM; 7806 } 7807 *count = snapshot_entry->clone_count; 7808 7809 n = 0; 7810 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 7811 ids[n++] = clone_entry->id; 7812 } 7813 7814 return 0; 7815 } 7816 7817 SPDK_LOG_REGISTER_COMPONENT(blob) 7818