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