1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2017 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 bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 2222 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg); 2223 2224 2225 static void 2226 blob_persist_dirty(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2227 { 2228 struct spdk_blob_persist_ctx *ctx = cb_arg; 2229 2230 if (bserrno != 0) { 2231 spdk_free(ctx->super); 2232 blob_persist_complete(seq, ctx, bserrno); 2233 return; 2234 } 2235 2236 ctx->super->clean = 0; 2237 if (ctx->super->size == 0) { 2238 ctx->super->size = ctx->blob->bs->dev->blockcnt * ctx->blob->bs->dev->blocklen; 2239 } 2240 2241 bs_write_super(seq, ctx->blob->bs, ctx->super, blob_persist_dirty_cpl, ctx); 2242 } 2243 2244 static void 2245 blob_persist_check_dirty(struct spdk_blob_persist_ctx *ctx) 2246 { 2247 if (ctx->blob->bs->clean) { 2248 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 2249 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 2250 if (!ctx->super) { 2251 blob_persist_complete(ctx->seq, ctx, -ENOMEM); 2252 return; 2253 } 2254 2255 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->blob->bs, 0), 2256 bs_byte_to_lba(ctx->blob->bs, sizeof(*ctx->super)), 2257 blob_persist_dirty, ctx); 2258 } else { 2259 blob_persist_start(ctx); 2260 } 2261 } 2262 2263 /* Write a blob to disk */ 2264 static void 2265 blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 2266 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 2267 { 2268 struct spdk_blob_persist_ctx *ctx; 2269 2270 blob_verify_md_op(blob); 2271 2272 if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->persists_to_complete)) { 2273 cb_fn(seq, cb_arg, 0); 2274 return; 2275 } 2276 2277 ctx = calloc(1, sizeof(*ctx)); 2278 if (!ctx) { 2279 cb_fn(seq, cb_arg, -ENOMEM); 2280 return; 2281 } 2282 ctx->blob = blob; 2283 ctx->seq = seq; 2284 ctx->cb_fn = cb_fn; 2285 ctx->cb_arg = cb_arg; 2286 2287 /* Multiple blob persists can affect one another, via blob->state or 2288 * blob mutable data changes. To prevent it, queue up the persists. */ 2289 if (!TAILQ_EMPTY(&blob->persists_to_complete)) { 2290 TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link); 2291 return; 2292 } 2293 TAILQ_INSERT_HEAD(&blob->persists_to_complete, ctx, link); 2294 2295 blob_persist_check_dirty(ctx); 2296 } 2297 2298 struct spdk_blob_copy_cluster_ctx { 2299 struct spdk_blob *blob; 2300 uint8_t *buf; 2301 uint64_t page; 2302 uint64_t new_cluster; 2303 uint32_t new_extent_page; 2304 spdk_bs_sequence_t *seq; 2305 struct spdk_blob_md_page *new_cluster_page; 2306 }; 2307 2308 static void 2309 blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno) 2310 { 2311 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2312 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq; 2313 TAILQ_HEAD(, spdk_bs_request_set) requests; 2314 spdk_bs_user_op_t *op; 2315 2316 TAILQ_INIT(&requests); 2317 TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link); 2318 2319 while (!TAILQ_EMPTY(&requests)) { 2320 op = TAILQ_FIRST(&requests); 2321 TAILQ_REMOVE(&requests, op, link); 2322 if (bserrno == 0) { 2323 bs_user_op_execute(op); 2324 } else { 2325 bs_user_op_abort(op, bserrno); 2326 } 2327 } 2328 2329 spdk_free(ctx->buf); 2330 free(ctx); 2331 } 2332 2333 static void 2334 blob_insert_cluster_cpl(void *cb_arg, int bserrno) 2335 { 2336 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2337 2338 if (bserrno) { 2339 if (bserrno == -EEXIST) { 2340 /* The metadata insert failed because another thread 2341 * allocated the cluster first. Free our cluster 2342 * but continue without error. */ 2343 bserrno = 0; 2344 } 2345 pthread_mutex_lock(&ctx->blob->bs->used_clusters_mutex); 2346 bs_release_cluster(ctx->blob->bs, ctx->new_cluster); 2347 pthread_mutex_unlock(&ctx->blob->bs->used_clusters_mutex); 2348 if (ctx->new_extent_page != 0) { 2349 bs_release_md_page(ctx->blob->bs, ctx->new_extent_page); 2350 } 2351 } 2352 2353 bs_sequence_finish(ctx->seq, bserrno); 2354 } 2355 2356 static void 2357 blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2358 { 2359 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2360 uint32_t cluster_number; 2361 2362 if (bserrno) { 2363 /* The write failed, so jump to the final completion handler */ 2364 bs_sequence_finish(seq, bserrno); 2365 return; 2366 } 2367 2368 cluster_number = bs_page_to_cluster(ctx->blob->bs, ctx->page); 2369 2370 blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster, 2371 ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx); 2372 } 2373 2374 static void 2375 blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2376 { 2377 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 2378 2379 if (bserrno != 0) { 2380 /* The read failed, so jump to the final completion handler */ 2381 bs_sequence_finish(seq, bserrno); 2382 return; 2383 } 2384 2385 /* Write whole cluster */ 2386 bs_sequence_write_dev(seq, ctx->buf, 2387 bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster), 2388 bs_cluster_to_lba(ctx->blob->bs, 1), 2389 blob_write_copy_cpl, ctx); 2390 } 2391 2392 static bool 2393 blob_can_copy(struct spdk_blob *blob, uint32_t cluster_start_page, uint64_t *base_lba) 2394 { 2395 uint64_t lba = bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page); 2396 2397 return (blob->bs->dev->copy != NULL) && 2398 blob->back_bs_dev->translate_lba(blob->back_bs_dev, lba, base_lba); 2399 } 2400 2401 static void 2402 blob_copy(struct spdk_blob_copy_cluster_ctx *ctx, spdk_bs_user_op_t *op, uint64_t src_lba) 2403 { 2404 struct spdk_blob *blob = ctx->blob; 2405 uint64_t lba_count = bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz); 2406 2407 bs_sequence_copy_dev(ctx->seq, 2408 bs_cluster_to_lba(blob->bs, ctx->new_cluster), 2409 src_lba, 2410 lba_count, 2411 blob_write_copy_cpl, ctx); 2412 } 2413 2414 static void 2415 bs_allocate_and_copy_cluster(struct spdk_blob *blob, 2416 struct spdk_io_channel *_ch, 2417 uint64_t io_unit, spdk_bs_user_op_t *op) 2418 { 2419 struct spdk_bs_cpl cpl; 2420 struct spdk_bs_channel *ch; 2421 struct spdk_blob_copy_cluster_ctx *ctx; 2422 uint32_t cluster_start_page; 2423 uint32_t cluster_number; 2424 bool is_zeroes; 2425 bool can_copy; 2426 uint64_t copy_src_lba; 2427 int rc; 2428 2429 ch = spdk_io_channel_get_ctx(_ch); 2430 2431 if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) { 2432 /* There are already operations pending. Queue this user op 2433 * and return because it will be re-executed when the outstanding 2434 * cluster allocation completes. */ 2435 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link); 2436 return; 2437 } 2438 2439 /* Round the io_unit offset down to the first page in the cluster */ 2440 cluster_start_page = bs_io_unit_to_cluster_start(blob, io_unit); 2441 2442 /* Calculate which index in the metadata cluster array the corresponding 2443 * cluster is supposed to be at. */ 2444 cluster_number = bs_io_unit_to_cluster_number(blob, io_unit); 2445 2446 ctx = calloc(1, sizeof(*ctx)); 2447 if (!ctx) { 2448 bs_user_op_abort(op, -ENOMEM); 2449 return; 2450 } 2451 2452 assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0); 2453 2454 ctx->blob = blob; 2455 ctx->page = cluster_start_page; 2456 ctx->new_cluster_page = ch->new_cluster_page; 2457 memset(ctx->new_cluster_page, 0, SPDK_BS_PAGE_SIZE); 2458 can_copy = blob_can_copy(blob, cluster_start_page, ©_src_lba); 2459 2460 is_zeroes = blob->back_bs_dev->is_zeroes(blob->back_bs_dev, 2461 bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page), 2462 bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz)); 2463 if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes && !can_copy) { 2464 ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen, 2465 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 2466 if (!ctx->buf) { 2467 SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n", 2468 blob->bs->cluster_sz); 2469 free(ctx); 2470 bs_user_op_abort(op, -ENOMEM); 2471 return; 2472 } 2473 } 2474 2475 pthread_mutex_lock(&blob->bs->used_clusters_mutex); 2476 rc = bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page, 2477 false); 2478 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 2479 if (rc != 0) { 2480 spdk_free(ctx->buf); 2481 free(ctx); 2482 bs_user_op_abort(op, rc); 2483 return; 2484 } 2485 2486 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2487 cpl.u.blob_basic.cb_fn = blob_allocate_and_copy_cluster_cpl; 2488 cpl.u.blob_basic.cb_arg = ctx; 2489 2490 ctx->seq = bs_sequence_start(_ch, &cpl); 2491 if (!ctx->seq) { 2492 pthread_mutex_lock(&blob->bs->used_clusters_mutex); 2493 bs_release_cluster(blob->bs, ctx->new_cluster); 2494 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 2495 spdk_free(ctx->buf); 2496 free(ctx); 2497 bs_user_op_abort(op, -ENOMEM); 2498 return; 2499 } 2500 2501 /* Queue the user op to block other incoming operations */ 2502 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link); 2503 2504 if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes) { 2505 if (can_copy) { 2506 blob_copy(ctx, op, copy_src_lba); 2507 } else { 2508 /* Read cluster from backing device */ 2509 bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf, 2510 bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page), 2511 bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz), 2512 blob_write_copy, ctx); 2513 } 2514 2515 } else { 2516 blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster, 2517 ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx); 2518 } 2519 } 2520 2521 static inline bool 2522 blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length, 2523 uint64_t *lba, uint64_t *lba_count) 2524 { 2525 *lba_count = length; 2526 2527 if (!bs_io_unit_is_allocated(blob, io_unit)) { 2528 assert(blob->back_bs_dev != NULL); 2529 *lba = bs_io_unit_to_back_dev_lba(blob, io_unit); 2530 *lba_count = bs_io_unit_to_back_dev_lba(blob, *lba_count); 2531 return false; 2532 } else { 2533 *lba = bs_blob_io_unit_to_lba(blob, io_unit); 2534 return true; 2535 } 2536 } 2537 2538 struct op_split_ctx { 2539 struct spdk_blob *blob; 2540 struct spdk_io_channel *channel; 2541 uint64_t io_unit_offset; 2542 uint64_t io_units_remaining; 2543 void *curr_payload; 2544 enum spdk_blob_op_type op_type; 2545 spdk_bs_sequence_t *seq; 2546 bool in_submit_ctx; 2547 bool completed_in_submit_ctx; 2548 bool done; 2549 }; 2550 2551 static void 2552 blob_request_submit_op_split_next(void *cb_arg, int bserrno) 2553 { 2554 struct op_split_ctx *ctx = cb_arg; 2555 struct spdk_blob *blob = ctx->blob; 2556 struct spdk_io_channel *ch = ctx->channel; 2557 enum spdk_blob_op_type op_type = ctx->op_type; 2558 uint8_t *buf; 2559 uint64_t offset; 2560 uint64_t length; 2561 uint64_t op_length; 2562 2563 if (bserrno != 0 || ctx->io_units_remaining == 0) { 2564 bs_sequence_finish(ctx->seq, bserrno); 2565 if (ctx->in_submit_ctx) { 2566 /* Defer freeing of the ctx object, since it will be 2567 * accessed when this unwinds back to the submisison 2568 * context. 2569 */ 2570 ctx->done = true; 2571 } else { 2572 free(ctx); 2573 } 2574 return; 2575 } 2576 2577 if (ctx->in_submit_ctx) { 2578 /* If this split operation completed in the context 2579 * of its submission, mark the flag and return immediately 2580 * to avoid recursion. 2581 */ 2582 ctx->completed_in_submit_ctx = true; 2583 return; 2584 } 2585 2586 while (true) { 2587 ctx->completed_in_submit_ctx = false; 2588 2589 offset = ctx->io_unit_offset; 2590 length = ctx->io_units_remaining; 2591 buf = ctx->curr_payload; 2592 op_length = spdk_min(length, bs_num_io_units_to_cluster_boundary(blob, 2593 offset)); 2594 2595 /* Update length and payload for next operation */ 2596 ctx->io_units_remaining -= op_length; 2597 ctx->io_unit_offset += op_length; 2598 if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) { 2599 ctx->curr_payload += op_length * blob->bs->io_unit_size; 2600 } 2601 2602 assert(!ctx->in_submit_ctx); 2603 ctx->in_submit_ctx = true; 2604 2605 switch (op_type) { 2606 case SPDK_BLOB_READ: 2607 spdk_blob_io_read(blob, ch, buf, offset, op_length, 2608 blob_request_submit_op_split_next, ctx); 2609 break; 2610 case SPDK_BLOB_WRITE: 2611 spdk_blob_io_write(blob, ch, buf, offset, op_length, 2612 blob_request_submit_op_split_next, ctx); 2613 break; 2614 case SPDK_BLOB_UNMAP: 2615 spdk_blob_io_unmap(blob, ch, offset, op_length, 2616 blob_request_submit_op_split_next, ctx); 2617 break; 2618 case SPDK_BLOB_WRITE_ZEROES: 2619 spdk_blob_io_write_zeroes(blob, ch, offset, op_length, 2620 blob_request_submit_op_split_next, ctx); 2621 break; 2622 case SPDK_BLOB_READV: 2623 case SPDK_BLOB_WRITEV: 2624 SPDK_ERRLOG("readv/write not valid\n"); 2625 bs_sequence_finish(ctx->seq, -EINVAL); 2626 free(ctx); 2627 return; 2628 } 2629 2630 #ifndef __clang_analyzer__ 2631 /* scan-build reports a false positive around accessing the ctx here. It 2632 * forms a path that recursively calls this function, but then says 2633 * "assuming ctx->in_submit_ctx is false", when that isn't possible. 2634 * This path does free(ctx), returns to here, and reports a use-after-free 2635 * bug. Wrapping this bit of code so that scan-build doesn't see it 2636 * works around the scan-build bug. 2637 */ 2638 assert(ctx->in_submit_ctx); 2639 ctx->in_submit_ctx = false; 2640 2641 /* If the operation completed immediately, loop back and submit the 2642 * next operation. Otherwise we can return and the next split 2643 * operation will get submitted when this current operation is 2644 * later completed asynchronously. 2645 */ 2646 if (ctx->completed_in_submit_ctx) { 2647 continue; 2648 } else if (ctx->done) { 2649 free(ctx); 2650 } 2651 #endif 2652 break; 2653 } 2654 } 2655 2656 static void 2657 blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob, 2658 void *payload, uint64_t offset, uint64_t length, 2659 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 2660 { 2661 struct op_split_ctx *ctx; 2662 spdk_bs_sequence_t *seq; 2663 struct spdk_bs_cpl cpl; 2664 2665 assert(blob != NULL); 2666 2667 ctx = calloc(1, sizeof(struct op_split_ctx)); 2668 if (ctx == NULL) { 2669 cb_fn(cb_arg, -ENOMEM); 2670 return; 2671 } 2672 2673 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2674 cpl.u.blob_basic.cb_fn = cb_fn; 2675 cpl.u.blob_basic.cb_arg = cb_arg; 2676 2677 seq = bs_sequence_start(ch, &cpl); 2678 if (!seq) { 2679 free(ctx); 2680 cb_fn(cb_arg, -ENOMEM); 2681 return; 2682 } 2683 2684 ctx->blob = blob; 2685 ctx->channel = ch; 2686 ctx->curr_payload = payload; 2687 ctx->io_unit_offset = offset; 2688 ctx->io_units_remaining = length; 2689 ctx->op_type = op_type; 2690 ctx->seq = seq; 2691 2692 blob_request_submit_op_split_next(ctx, 0); 2693 } 2694 2695 static void 2696 blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob, 2697 void *payload, uint64_t offset, uint64_t length, 2698 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 2699 { 2700 struct spdk_bs_cpl cpl; 2701 uint64_t lba; 2702 uint64_t lba_count; 2703 bool is_allocated; 2704 2705 assert(blob != NULL); 2706 2707 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2708 cpl.u.blob_basic.cb_fn = cb_fn; 2709 cpl.u.blob_basic.cb_arg = cb_arg; 2710 2711 if (blob->frozen_refcnt) { 2712 /* This blob I/O is frozen */ 2713 spdk_bs_user_op_t *op; 2714 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch); 2715 2716 op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length); 2717 if (!op) { 2718 cb_fn(cb_arg, -ENOMEM); 2719 return; 2720 } 2721 2722 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link); 2723 2724 return; 2725 } 2726 2727 is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count); 2728 2729 switch (op_type) { 2730 case SPDK_BLOB_READ: { 2731 spdk_bs_batch_t *batch; 2732 2733 batch = bs_batch_open(_ch, &cpl); 2734 if (!batch) { 2735 cb_fn(cb_arg, -ENOMEM); 2736 return; 2737 } 2738 2739 if (is_allocated) { 2740 /* Read from the blob */ 2741 bs_batch_read_dev(batch, payload, lba, lba_count); 2742 } else { 2743 /* Read from the backing block device */ 2744 bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count); 2745 } 2746 2747 bs_batch_close(batch); 2748 break; 2749 } 2750 case SPDK_BLOB_WRITE: 2751 case SPDK_BLOB_WRITE_ZEROES: { 2752 if (is_allocated) { 2753 /* Write to the blob */ 2754 spdk_bs_batch_t *batch; 2755 2756 if (lba_count == 0) { 2757 cb_fn(cb_arg, 0); 2758 return; 2759 } 2760 2761 batch = bs_batch_open(_ch, &cpl); 2762 if (!batch) { 2763 cb_fn(cb_arg, -ENOMEM); 2764 return; 2765 } 2766 2767 if (op_type == SPDK_BLOB_WRITE) { 2768 bs_batch_write_dev(batch, payload, lba, lba_count); 2769 } else { 2770 bs_batch_write_zeroes_dev(batch, lba, lba_count); 2771 } 2772 2773 bs_batch_close(batch); 2774 } else { 2775 /* Queue this operation and allocate the cluster */ 2776 spdk_bs_user_op_t *op; 2777 2778 op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length); 2779 if (!op) { 2780 cb_fn(cb_arg, -ENOMEM); 2781 return; 2782 } 2783 2784 bs_allocate_and_copy_cluster(blob, _ch, offset, op); 2785 } 2786 break; 2787 } 2788 case SPDK_BLOB_UNMAP: { 2789 spdk_bs_batch_t *batch; 2790 2791 batch = bs_batch_open(_ch, &cpl); 2792 if (!batch) { 2793 cb_fn(cb_arg, -ENOMEM); 2794 return; 2795 } 2796 2797 if (is_allocated) { 2798 bs_batch_unmap_dev(batch, lba, lba_count); 2799 } 2800 2801 bs_batch_close(batch); 2802 break; 2803 } 2804 case SPDK_BLOB_READV: 2805 case SPDK_BLOB_WRITEV: 2806 SPDK_ERRLOG("readv/write not valid\n"); 2807 cb_fn(cb_arg, -EINVAL); 2808 break; 2809 } 2810 } 2811 2812 static void 2813 blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel, 2814 void *payload, uint64_t offset, uint64_t length, 2815 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 2816 { 2817 assert(blob != NULL); 2818 2819 if (blob->data_ro && op_type != SPDK_BLOB_READ) { 2820 cb_fn(cb_arg, -EPERM); 2821 return; 2822 } 2823 2824 if (length == 0) { 2825 cb_fn(cb_arg, 0); 2826 return; 2827 } 2828 2829 if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) { 2830 cb_fn(cb_arg, -EINVAL); 2831 return; 2832 } 2833 if (length <= bs_num_io_units_to_cluster_boundary(blob, offset)) { 2834 blob_request_submit_op_single(_channel, blob, payload, offset, length, 2835 cb_fn, cb_arg, op_type); 2836 } else { 2837 blob_request_submit_op_split(_channel, blob, payload, offset, length, 2838 cb_fn, cb_arg, op_type); 2839 } 2840 } 2841 2842 struct rw_iov_ctx { 2843 struct spdk_blob *blob; 2844 struct spdk_io_channel *channel; 2845 spdk_blob_op_complete cb_fn; 2846 void *cb_arg; 2847 bool read; 2848 int iovcnt; 2849 struct iovec *orig_iov; 2850 uint64_t io_unit_offset; 2851 uint64_t io_units_remaining; 2852 uint64_t io_units_done; 2853 struct spdk_blob_ext_io_opts *ext_io_opts; 2854 struct iovec iov[0]; 2855 }; 2856 2857 static void 2858 rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2859 { 2860 assert(cb_arg == NULL); 2861 bs_sequence_finish(seq, bserrno); 2862 } 2863 2864 static void 2865 rw_iov_split_next(void *cb_arg, int bserrno) 2866 { 2867 struct rw_iov_ctx *ctx = cb_arg; 2868 struct spdk_blob *blob = ctx->blob; 2869 struct iovec *iov, *orig_iov; 2870 int iovcnt; 2871 size_t orig_iovoff; 2872 uint64_t io_units_count, io_units_to_boundary, io_unit_offset; 2873 uint64_t byte_count; 2874 2875 if (bserrno != 0 || ctx->io_units_remaining == 0) { 2876 ctx->cb_fn(ctx->cb_arg, bserrno); 2877 free(ctx); 2878 return; 2879 } 2880 2881 io_unit_offset = ctx->io_unit_offset; 2882 io_units_to_boundary = bs_num_io_units_to_cluster_boundary(blob, io_unit_offset); 2883 io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary); 2884 /* 2885 * Get index and offset into the original iov array for our current position in the I/O sequence. 2886 * byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will 2887 * point to the current position in the I/O sequence. 2888 */ 2889 byte_count = ctx->io_units_done * blob->bs->io_unit_size; 2890 orig_iov = &ctx->orig_iov[0]; 2891 orig_iovoff = 0; 2892 while (byte_count > 0) { 2893 if (byte_count >= orig_iov->iov_len) { 2894 byte_count -= orig_iov->iov_len; 2895 orig_iov++; 2896 } else { 2897 orig_iovoff = byte_count; 2898 byte_count = 0; 2899 } 2900 } 2901 2902 /* 2903 * Build an iov array for the next I/O in the sequence. byte_count will keep track of how many 2904 * bytes of this next I/O remain to be accounted for in the new iov array. 2905 */ 2906 byte_count = io_units_count * blob->bs->io_unit_size; 2907 iov = &ctx->iov[0]; 2908 iovcnt = 0; 2909 while (byte_count > 0) { 2910 assert(iovcnt < ctx->iovcnt); 2911 iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff); 2912 iov->iov_base = orig_iov->iov_base + orig_iovoff; 2913 byte_count -= iov->iov_len; 2914 orig_iovoff = 0; 2915 orig_iov++; 2916 iov++; 2917 iovcnt++; 2918 } 2919 2920 ctx->io_unit_offset += io_units_count; 2921 ctx->io_units_remaining -= io_units_count; 2922 ctx->io_units_done += io_units_count; 2923 iov = &ctx->iov[0]; 2924 2925 if (ctx->read) { 2926 spdk_blob_io_readv_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset, 2927 io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts); 2928 } else { 2929 spdk_blob_io_writev_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset, 2930 io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts); 2931 } 2932 } 2933 2934 static void 2935 blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel, 2936 struct iovec *iov, int iovcnt, 2937 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg, bool read, 2938 struct spdk_blob_ext_io_opts *ext_io_opts) 2939 { 2940 struct spdk_bs_cpl cpl; 2941 2942 assert(blob != NULL); 2943 2944 if (!read && blob->data_ro) { 2945 cb_fn(cb_arg, -EPERM); 2946 return; 2947 } 2948 2949 if (length == 0) { 2950 cb_fn(cb_arg, 0); 2951 return; 2952 } 2953 2954 if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) { 2955 cb_fn(cb_arg, -EINVAL); 2956 return; 2957 } 2958 2959 /* 2960 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having 2961 * to split a request that spans a cluster boundary. For I/O that do not span a cluster boundary, 2962 * there will be no noticeable difference compared to using a batch. For I/O that do span a cluster 2963 * boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need 2964 * to allocate a separate iov array and split the I/O such that none of the resulting 2965 * smaller I/O cross a cluster boundary. These smaller I/O will be issued in sequence (not in parallel) 2966 * but since this case happens very infrequently, any performance impact will be negligible. 2967 * 2968 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs 2969 * for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them 2970 * in a batch. That would also require creating an intermediate spdk_bs_cpl that would get called 2971 * when the batch was completed, to allow for freeing the memory for the iov arrays. 2972 */ 2973 if (spdk_likely(length <= bs_num_io_units_to_cluster_boundary(blob, offset))) { 2974 uint64_t lba_count; 2975 uint64_t lba; 2976 bool is_allocated; 2977 2978 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2979 cpl.u.blob_basic.cb_fn = cb_fn; 2980 cpl.u.blob_basic.cb_arg = cb_arg; 2981 2982 if (blob->frozen_refcnt) { 2983 /* This blob I/O is frozen */ 2984 enum spdk_blob_op_type op_type; 2985 spdk_bs_user_op_t *op; 2986 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel); 2987 2988 op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV; 2989 op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length); 2990 if (!op) { 2991 cb_fn(cb_arg, -ENOMEM); 2992 return; 2993 } 2994 2995 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link); 2996 2997 return; 2998 } 2999 3000 is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count); 3001 3002 if (read) { 3003 spdk_bs_sequence_t *seq; 3004 3005 seq = bs_sequence_start(_channel, &cpl); 3006 if (!seq) { 3007 cb_fn(cb_arg, -ENOMEM); 3008 return; 3009 } 3010 3011 seq->ext_io_opts = ext_io_opts; 3012 3013 if (is_allocated) { 3014 bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL); 3015 } else { 3016 bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count, 3017 rw_iov_done, NULL); 3018 } 3019 } else { 3020 if (is_allocated) { 3021 spdk_bs_sequence_t *seq; 3022 3023 seq = bs_sequence_start(_channel, &cpl); 3024 if (!seq) { 3025 cb_fn(cb_arg, -ENOMEM); 3026 return; 3027 } 3028 3029 seq->ext_io_opts = ext_io_opts; 3030 3031 bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL); 3032 } else { 3033 /* Queue this operation and allocate the cluster */ 3034 spdk_bs_user_op_t *op; 3035 3036 op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset, 3037 length); 3038 if (!op) { 3039 cb_fn(cb_arg, -ENOMEM); 3040 return; 3041 } 3042 3043 op->ext_io_opts = ext_io_opts; 3044 3045 bs_allocate_and_copy_cluster(blob, _channel, offset, op); 3046 } 3047 } 3048 } else { 3049 struct rw_iov_ctx *ctx; 3050 3051 ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec)); 3052 if (ctx == NULL) { 3053 cb_fn(cb_arg, -ENOMEM); 3054 return; 3055 } 3056 3057 ctx->blob = blob; 3058 ctx->channel = _channel; 3059 ctx->cb_fn = cb_fn; 3060 ctx->cb_arg = cb_arg; 3061 ctx->read = read; 3062 ctx->orig_iov = iov; 3063 ctx->iovcnt = iovcnt; 3064 ctx->io_unit_offset = offset; 3065 ctx->io_units_remaining = length; 3066 ctx->io_units_done = 0; 3067 ctx->ext_io_opts = ext_io_opts; 3068 3069 rw_iov_split_next(ctx, 0); 3070 } 3071 } 3072 3073 static struct spdk_blob * 3074 blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid) 3075 { 3076 struct spdk_blob find; 3077 3078 if (spdk_bit_array_get(bs->open_blobids, blobid) == 0) { 3079 return NULL; 3080 } 3081 3082 find.id = blobid; 3083 return RB_FIND(spdk_blob_tree, &bs->open_blobs, &find); 3084 } 3085 3086 static void 3087 blob_get_snapshot_and_clone_entries(struct spdk_blob *blob, 3088 struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry) 3089 { 3090 assert(blob != NULL); 3091 *snapshot_entry = NULL; 3092 *clone_entry = NULL; 3093 3094 if (blob->parent_id == SPDK_BLOBID_INVALID) { 3095 return; 3096 } 3097 3098 TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) { 3099 if ((*snapshot_entry)->id == blob->parent_id) { 3100 break; 3101 } 3102 } 3103 3104 if (*snapshot_entry != NULL) { 3105 TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) { 3106 if ((*clone_entry)->id == blob->id) { 3107 break; 3108 } 3109 } 3110 3111 assert(*clone_entry != NULL); 3112 } 3113 } 3114 3115 static int 3116 bs_channel_create(void *io_device, void *ctx_buf) 3117 { 3118 struct spdk_blob_store *bs = io_device; 3119 struct spdk_bs_channel *channel = ctx_buf; 3120 struct spdk_bs_dev *dev; 3121 uint32_t max_ops = bs->max_channel_ops; 3122 uint32_t i; 3123 3124 dev = bs->dev; 3125 3126 channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set)); 3127 if (!channel->req_mem) { 3128 return -1; 3129 } 3130 3131 TAILQ_INIT(&channel->reqs); 3132 3133 for (i = 0; i < max_ops; i++) { 3134 TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link); 3135 } 3136 3137 channel->bs = bs; 3138 channel->dev = dev; 3139 channel->dev_channel = dev->create_channel(dev); 3140 3141 if (!channel->dev_channel) { 3142 SPDK_ERRLOG("Failed to create device channel.\n"); 3143 free(channel->req_mem); 3144 return -1; 3145 } 3146 3147 channel->new_cluster_page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY, 3148 SPDK_MALLOC_DMA); 3149 if (!channel->new_cluster_page) { 3150 SPDK_ERRLOG("Failed to allocate new cluster page\n"); 3151 free(channel->req_mem); 3152 channel->dev->destroy_channel(channel->dev, channel->dev_channel); 3153 return -1; 3154 } 3155 3156 TAILQ_INIT(&channel->need_cluster_alloc); 3157 TAILQ_INIT(&channel->queued_io); 3158 3159 return 0; 3160 } 3161 3162 static void 3163 bs_channel_destroy(void *io_device, void *ctx_buf) 3164 { 3165 struct spdk_bs_channel *channel = ctx_buf; 3166 spdk_bs_user_op_t *op; 3167 3168 while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) { 3169 op = TAILQ_FIRST(&channel->need_cluster_alloc); 3170 TAILQ_REMOVE(&channel->need_cluster_alloc, op, link); 3171 bs_user_op_abort(op, -EIO); 3172 } 3173 3174 while (!TAILQ_EMPTY(&channel->queued_io)) { 3175 op = TAILQ_FIRST(&channel->queued_io); 3176 TAILQ_REMOVE(&channel->queued_io, op, link); 3177 bs_user_op_abort(op, -EIO); 3178 } 3179 3180 free(channel->req_mem); 3181 spdk_free(channel->new_cluster_page); 3182 channel->dev->destroy_channel(channel->dev, channel->dev_channel); 3183 } 3184 3185 static void 3186 bs_dev_destroy(void *io_device) 3187 { 3188 struct spdk_blob_store *bs = io_device; 3189 struct spdk_blob *blob, *blob_tmp; 3190 3191 bs->dev->destroy(bs->dev); 3192 3193 RB_FOREACH_SAFE(blob, spdk_blob_tree, &bs->open_blobs, blob_tmp) { 3194 RB_REMOVE(spdk_blob_tree, &bs->open_blobs, blob); 3195 spdk_bit_array_clear(bs->open_blobids, blob->id); 3196 blob_free(blob); 3197 } 3198 3199 pthread_mutex_destroy(&bs->used_clusters_mutex); 3200 3201 spdk_bit_array_free(&bs->open_blobids); 3202 spdk_bit_array_free(&bs->used_blobids); 3203 spdk_bit_array_free(&bs->used_md_pages); 3204 spdk_bit_pool_free(&bs->used_clusters); 3205 /* 3206 * If this function is called for any reason except a successful unload, 3207 * the unload_cpl type will be NONE and this will be a nop. 3208 */ 3209 bs_call_cpl(&bs->unload_cpl, bs->unload_err); 3210 3211 free(bs); 3212 } 3213 3214 static int 3215 bs_blob_list_add(struct spdk_blob *blob) 3216 { 3217 spdk_blob_id snapshot_id; 3218 struct spdk_blob_list *snapshot_entry = NULL; 3219 struct spdk_blob_list *clone_entry = NULL; 3220 3221 assert(blob != NULL); 3222 3223 snapshot_id = blob->parent_id; 3224 if (snapshot_id == SPDK_BLOBID_INVALID) { 3225 return 0; 3226 } 3227 3228 snapshot_entry = bs_get_snapshot_entry(blob->bs, snapshot_id); 3229 if (snapshot_entry == NULL) { 3230 /* Snapshot not found */ 3231 snapshot_entry = calloc(1, sizeof(struct spdk_blob_list)); 3232 if (snapshot_entry == NULL) { 3233 return -ENOMEM; 3234 } 3235 snapshot_entry->id = snapshot_id; 3236 TAILQ_INIT(&snapshot_entry->clones); 3237 TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link); 3238 } else { 3239 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 3240 if (clone_entry->id == blob->id) { 3241 break; 3242 } 3243 } 3244 } 3245 3246 if (clone_entry == NULL) { 3247 /* Clone not found */ 3248 clone_entry = calloc(1, sizeof(struct spdk_blob_list)); 3249 if (clone_entry == NULL) { 3250 return -ENOMEM; 3251 } 3252 clone_entry->id = blob->id; 3253 TAILQ_INIT(&clone_entry->clones); 3254 TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link); 3255 snapshot_entry->clone_count++; 3256 } 3257 3258 return 0; 3259 } 3260 3261 static void 3262 bs_blob_list_remove(struct spdk_blob *blob) 3263 { 3264 struct spdk_blob_list *snapshot_entry = NULL; 3265 struct spdk_blob_list *clone_entry = NULL; 3266 3267 blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry); 3268 3269 if (snapshot_entry == NULL) { 3270 return; 3271 } 3272 3273 blob->parent_id = SPDK_BLOBID_INVALID; 3274 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 3275 free(clone_entry); 3276 3277 snapshot_entry->clone_count--; 3278 } 3279 3280 static int 3281 bs_blob_list_free(struct spdk_blob_store *bs) 3282 { 3283 struct spdk_blob_list *snapshot_entry; 3284 struct spdk_blob_list *snapshot_entry_tmp; 3285 struct spdk_blob_list *clone_entry; 3286 struct spdk_blob_list *clone_entry_tmp; 3287 3288 TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) { 3289 TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) { 3290 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 3291 free(clone_entry); 3292 } 3293 TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link); 3294 free(snapshot_entry); 3295 } 3296 3297 return 0; 3298 } 3299 3300 static void 3301 bs_free(struct spdk_blob_store *bs) 3302 { 3303 bs_blob_list_free(bs); 3304 3305 bs_unregister_md_thread(bs); 3306 spdk_io_device_unregister(bs, bs_dev_destroy); 3307 } 3308 3309 void 3310 spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size) 3311 { 3312 3313 if (!opts) { 3314 SPDK_ERRLOG("opts should not be NULL\n"); 3315 return; 3316 } 3317 3318 if (!opts_size) { 3319 SPDK_ERRLOG("opts_size should not be zero value\n"); 3320 return; 3321 } 3322 3323 memset(opts, 0, opts_size); 3324 opts->opts_size = opts_size; 3325 3326 #define FIELD_OK(field) \ 3327 offsetof(struct spdk_bs_opts, field) + sizeof(opts->field) <= opts_size 3328 3329 #define SET_FIELD(field, value) \ 3330 if (FIELD_OK(field)) { \ 3331 opts->field = value; \ 3332 } \ 3333 3334 SET_FIELD(cluster_sz, SPDK_BLOB_OPTS_CLUSTER_SZ); 3335 SET_FIELD(num_md_pages, SPDK_BLOB_OPTS_NUM_MD_PAGES); 3336 SET_FIELD(max_md_ops, SPDK_BLOB_OPTS_NUM_MD_PAGES); 3337 SET_FIELD(max_channel_ops, SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS); 3338 SET_FIELD(clear_method, BS_CLEAR_WITH_UNMAP); 3339 3340 if (FIELD_OK(bstype)) { 3341 memset(&opts->bstype, 0, sizeof(opts->bstype)); 3342 } 3343 3344 SET_FIELD(iter_cb_fn, NULL); 3345 SET_FIELD(iter_cb_arg, NULL); 3346 SET_FIELD(force_recover, false); 3347 3348 #undef FIELD_OK 3349 #undef SET_FIELD 3350 } 3351 3352 static int 3353 bs_opts_verify(struct spdk_bs_opts *opts) 3354 { 3355 if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 || 3356 opts->max_channel_ops == 0) { 3357 SPDK_ERRLOG("Blobstore options cannot be set to 0\n"); 3358 return -1; 3359 } 3360 3361 return 0; 3362 } 3363 3364 /* START spdk_bs_load */ 3365 3366 /* spdk_bs_load_ctx is used for init, load, unload and dump code paths. */ 3367 3368 struct spdk_bs_load_ctx { 3369 struct spdk_blob_store *bs; 3370 struct spdk_bs_super_block *super; 3371 3372 struct spdk_bs_md_mask *mask; 3373 bool in_page_chain; 3374 uint32_t page_index; 3375 uint32_t cur_page; 3376 struct spdk_blob_md_page *page; 3377 3378 uint64_t num_extent_pages; 3379 uint32_t *extent_page_num; 3380 struct spdk_blob_md_page *extent_pages; 3381 struct spdk_bit_array *used_clusters; 3382 3383 spdk_bs_sequence_t *seq; 3384 spdk_blob_op_with_handle_complete iter_cb_fn; 3385 void *iter_cb_arg; 3386 struct spdk_blob *blob; 3387 spdk_blob_id blobid; 3388 3389 bool force_recover; 3390 3391 /* These fields are used in the spdk_bs_dump path. */ 3392 bool dumping; 3393 FILE *fp; 3394 spdk_bs_dump_print_xattr print_xattr_fn; 3395 char xattr_name[4096]; 3396 }; 3397 3398 static int 3399 bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs, 3400 struct spdk_bs_load_ctx **_ctx) 3401 { 3402 struct spdk_blob_store *bs; 3403 struct spdk_bs_load_ctx *ctx; 3404 uint64_t dev_size; 3405 int rc; 3406 3407 dev_size = dev->blocklen * dev->blockcnt; 3408 if (dev_size < opts->cluster_sz) { 3409 /* Device size cannot be smaller than cluster size of blobstore */ 3410 SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n", 3411 dev_size, opts->cluster_sz); 3412 return -ENOSPC; 3413 } 3414 if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) { 3415 /* Cluster size cannot be smaller than page size */ 3416 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n", 3417 opts->cluster_sz, SPDK_BS_PAGE_SIZE); 3418 return -EINVAL; 3419 } 3420 bs = calloc(1, sizeof(struct spdk_blob_store)); 3421 if (!bs) { 3422 return -ENOMEM; 3423 } 3424 3425 ctx = calloc(1, sizeof(struct spdk_bs_load_ctx)); 3426 if (!ctx) { 3427 free(bs); 3428 return -ENOMEM; 3429 } 3430 3431 ctx->bs = bs; 3432 ctx->iter_cb_fn = opts->iter_cb_fn; 3433 ctx->iter_cb_arg = opts->iter_cb_arg; 3434 ctx->force_recover = opts->force_recover; 3435 3436 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 3437 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3438 if (!ctx->super) { 3439 free(ctx); 3440 free(bs); 3441 return -ENOMEM; 3442 } 3443 3444 RB_INIT(&bs->open_blobs); 3445 TAILQ_INIT(&bs->snapshots); 3446 bs->dev = dev; 3447 bs->md_thread = spdk_get_thread(); 3448 assert(bs->md_thread != NULL); 3449 3450 /* 3451 * Do not use bs_lba_to_cluster() here since blockcnt may not be an 3452 * even multiple of the cluster size. 3453 */ 3454 bs->cluster_sz = opts->cluster_sz; 3455 bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen); 3456 ctx->used_clusters = spdk_bit_array_create(bs->total_clusters); 3457 if (!ctx->used_clusters) { 3458 spdk_free(ctx->super); 3459 free(ctx); 3460 free(bs); 3461 return -ENOMEM; 3462 } 3463 3464 bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE; 3465 if (spdk_u32_is_pow2(bs->pages_per_cluster)) { 3466 bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster); 3467 } 3468 bs->num_free_clusters = bs->total_clusters; 3469 bs->io_unit_size = dev->blocklen; 3470 3471 bs->max_channel_ops = opts->max_channel_ops; 3472 bs->super_blob = SPDK_BLOBID_INVALID; 3473 memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype)); 3474 3475 /* The metadata is assumed to be at least 1 page */ 3476 bs->used_md_pages = spdk_bit_array_create(1); 3477 bs->used_blobids = spdk_bit_array_create(0); 3478 bs->open_blobids = spdk_bit_array_create(0); 3479 3480 pthread_mutex_init(&bs->used_clusters_mutex, NULL); 3481 3482 spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy, 3483 sizeof(struct spdk_bs_channel), "blobstore"); 3484 rc = bs_register_md_thread(bs); 3485 if (rc == -1) { 3486 spdk_io_device_unregister(bs, NULL); 3487 pthread_mutex_destroy(&bs->used_clusters_mutex); 3488 spdk_bit_array_free(&bs->open_blobids); 3489 spdk_bit_array_free(&bs->used_blobids); 3490 spdk_bit_array_free(&bs->used_md_pages); 3491 spdk_bit_array_free(&ctx->used_clusters); 3492 spdk_free(ctx->super); 3493 free(ctx); 3494 free(bs); 3495 /* FIXME: this is a lie but don't know how to get a proper error code here */ 3496 return -ENOMEM; 3497 } 3498 3499 *_ctx = ctx; 3500 *_bs = bs; 3501 return 0; 3502 } 3503 3504 static void 3505 bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno) 3506 { 3507 assert(bserrno != 0); 3508 3509 spdk_free(ctx->super); 3510 bs_sequence_finish(ctx->seq, bserrno); 3511 bs_free(ctx->bs); 3512 spdk_bit_array_free(&ctx->used_clusters); 3513 free(ctx); 3514 } 3515 3516 static void 3517 bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 3518 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 3519 { 3520 /* Update the values in the super block */ 3521 super->super_blob = bs->super_blob; 3522 memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype)); 3523 super->crc = blob_md_page_calc_crc(super); 3524 bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0), 3525 bs_byte_to_lba(bs, sizeof(*super)), 3526 cb_fn, cb_arg); 3527 } 3528 3529 static void 3530 bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3531 { 3532 struct spdk_bs_load_ctx *ctx = arg; 3533 uint64_t mask_size, lba, lba_count; 3534 3535 /* Write out the used clusters mask */ 3536 mask_size = ctx->super->used_cluster_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_CLUSTERS; 3545 ctx->mask->length = ctx->bs->total_clusters; 3546 /* We could get here through the normal unload path, or through dirty 3547 * shutdown recovery. For the normal unload path, we use the mask from 3548 * the bit pool. For dirty shutdown recovery, we don't have a bit pool yet - 3549 * only the bit array from the load ctx. 3550 */ 3551 if (ctx->bs->used_clusters) { 3552 assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters)); 3553 spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask); 3554 } else { 3555 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters)); 3556 spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask); 3557 } 3558 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 3559 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 3560 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3561 } 3562 3563 static void 3564 bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3565 { 3566 struct spdk_bs_load_ctx *ctx = arg; 3567 uint64_t mask_size, lba, lba_count; 3568 3569 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 3570 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3571 SPDK_ENV_SOCKET_ID_ANY, 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_PAGES; 3578 ctx->mask->length = ctx->super->md_len; 3579 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages)); 3580 3581 spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask); 3582 lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 3583 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 3584 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3585 } 3586 3587 static void 3588 bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 3589 { 3590 struct spdk_bs_load_ctx *ctx = arg; 3591 uint64_t mask_size, lba, lba_count; 3592 3593 if (ctx->super->used_blobid_mask_len == 0) { 3594 /* 3595 * This is a pre-v3 on-disk format where the blobid mask does not get 3596 * written to disk. 3597 */ 3598 cb_fn(seq, arg, 0); 3599 return; 3600 } 3601 3602 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 3603 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3604 SPDK_MALLOC_DMA); 3605 if (!ctx->mask) { 3606 bs_load_ctx_fail(ctx, -ENOMEM); 3607 return; 3608 } 3609 3610 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS; 3611 ctx->mask->length = ctx->super->md_len; 3612 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids)); 3613 3614 spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask); 3615 lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 3616 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 3617 bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 3618 } 3619 3620 static void 3621 blob_set_thin_provision(struct spdk_blob *blob) 3622 { 3623 blob_verify_md_op(blob); 3624 blob->invalid_flags |= SPDK_BLOB_THIN_PROV; 3625 blob->state = SPDK_BLOB_STATE_DIRTY; 3626 } 3627 3628 static void 3629 blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method) 3630 { 3631 blob_verify_md_op(blob); 3632 blob->clear_method = clear_method; 3633 blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT); 3634 blob->state = SPDK_BLOB_STATE_DIRTY; 3635 } 3636 3637 static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno); 3638 3639 static void 3640 bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno) 3641 { 3642 struct spdk_bs_load_ctx *ctx = cb_arg; 3643 spdk_blob_id id; 3644 int64_t page_num; 3645 3646 /* Iterate to next blob (we can't use spdk_bs_iter_next function as our 3647 * last blob has been removed */ 3648 page_num = bs_blobid_to_page(ctx->blobid); 3649 page_num++; 3650 page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num); 3651 if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) { 3652 bs_load_iter(ctx, NULL, -ENOENT); 3653 return; 3654 } 3655 3656 id = bs_page_to_blobid(page_num); 3657 3658 spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx); 3659 } 3660 3661 static void 3662 bs_delete_corrupted_close_cb(void *cb_arg, int bserrno) 3663 { 3664 struct spdk_bs_load_ctx *ctx = cb_arg; 3665 3666 if (bserrno != 0) { 3667 SPDK_ERRLOG("Failed to close corrupted blob\n"); 3668 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3669 return; 3670 } 3671 3672 spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx); 3673 } 3674 3675 static void 3676 bs_delete_corrupted_blob(void *cb_arg, int bserrno) 3677 { 3678 struct spdk_bs_load_ctx *ctx = cb_arg; 3679 uint64_t i; 3680 3681 if (bserrno != 0) { 3682 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n"); 3683 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3684 return; 3685 } 3686 3687 /* Snapshot and clone have the same copy of cluster map and extent pages 3688 * at this point. Let's clear both for snapshot now, 3689 * so that it won't be cleared for clone later when we remove snapshot. 3690 * Also set thin provision to pass data corruption check */ 3691 for (i = 0; i < ctx->blob->active.num_clusters; i++) { 3692 ctx->blob->active.clusters[i] = 0; 3693 } 3694 for (i = 0; i < ctx->blob->active.num_extent_pages; i++) { 3695 ctx->blob->active.extent_pages[i] = 0; 3696 } 3697 3698 ctx->blob->md_ro = false; 3699 3700 blob_set_thin_provision(ctx->blob); 3701 3702 ctx->blobid = ctx->blob->id; 3703 3704 spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx); 3705 } 3706 3707 static void 3708 bs_update_corrupted_blob(void *cb_arg, int bserrno) 3709 { 3710 struct spdk_bs_load_ctx *ctx = cb_arg; 3711 3712 if (bserrno != 0) { 3713 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n"); 3714 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3715 return; 3716 } 3717 3718 ctx->blob->md_ro = false; 3719 blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true); 3720 blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true); 3721 spdk_blob_set_read_only(ctx->blob); 3722 3723 if (ctx->iter_cb_fn) { 3724 ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0); 3725 } 3726 bs_blob_list_add(ctx->blob); 3727 3728 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3729 } 3730 3731 static void 3732 bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno) 3733 { 3734 struct spdk_bs_load_ctx *ctx = cb_arg; 3735 3736 if (bserrno != 0) { 3737 SPDK_ERRLOG("Failed to open clone of a corrupted blob\n"); 3738 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx); 3739 return; 3740 } 3741 3742 if (blob->parent_id == ctx->blob->id) { 3743 /* Power failure occurred before updating clone (snapshot delete case) 3744 * or after updating clone (creating snapshot case) - keep snapshot */ 3745 spdk_blob_close(blob, bs_update_corrupted_blob, ctx); 3746 } else { 3747 /* Power failure occurred after updating clone (snapshot delete case) 3748 * or before updating clone (creating snapshot case) - remove snapshot */ 3749 spdk_blob_close(blob, bs_delete_corrupted_blob, ctx); 3750 } 3751 } 3752 3753 static void 3754 bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno) 3755 { 3756 struct spdk_bs_load_ctx *ctx = arg; 3757 const void *value; 3758 size_t len; 3759 int rc = 0; 3760 3761 if (bserrno == 0) { 3762 /* Examine blob if it is corrupted after power failure. Fix 3763 * the ones that can be fixed and remove any other corrupted 3764 * ones. If it is not corrupted just process it */ 3765 rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true); 3766 if (rc != 0) { 3767 rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true); 3768 if (rc != 0) { 3769 /* Not corrupted - process it and continue with iterating through blobs */ 3770 if (ctx->iter_cb_fn) { 3771 ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0); 3772 } 3773 bs_blob_list_add(blob); 3774 spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx); 3775 return; 3776 } 3777 3778 } 3779 3780 assert(len == sizeof(spdk_blob_id)); 3781 3782 ctx->blob = blob; 3783 3784 /* Open clone to check if we are able to fix this blob or should we remove it */ 3785 spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx); 3786 return; 3787 } else if (bserrno == -ENOENT) { 3788 bserrno = 0; 3789 } else { 3790 /* 3791 * This case needs to be looked at further. Same problem 3792 * exists with applications that rely on explicit blob 3793 * iteration. We should just skip the blob that failed 3794 * to load and continue on to the next one. 3795 */ 3796 SPDK_ERRLOG("Error in iterating blobs\n"); 3797 } 3798 3799 ctx->iter_cb_fn = NULL; 3800 3801 spdk_free(ctx->super); 3802 spdk_free(ctx->mask); 3803 bs_sequence_finish(ctx->seq, bserrno); 3804 free(ctx); 3805 } 3806 3807 static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 3808 3809 static void 3810 bs_load_complete(struct spdk_bs_load_ctx *ctx) 3811 { 3812 ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters); 3813 if (ctx->dumping) { 3814 bs_dump_read_md_page(ctx->seq, ctx); 3815 return; 3816 } 3817 spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx); 3818 } 3819 3820 static void 3821 bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3822 { 3823 struct spdk_bs_load_ctx *ctx = cb_arg; 3824 int rc; 3825 3826 /* The type must be correct */ 3827 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS); 3828 3829 /* The length of the mask (in bits) must not be greater than 3830 * the length of the buffer (converted to bits) */ 3831 assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8)); 3832 3833 /* The length of the mask must be exactly equal to the size 3834 * (in pages) of the metadata region */ 3835 assert(ctx->mask->length == ctx->super->md_len); 3836 3837 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length); 3838 if (rc < 0) { 3839 spdk_free(ctx->mask); 3840 bs_load_ctx_fail(ctx, rc); 3841 return; 3842 } 3843 3844 spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask); 3845 bs_load_complete(ctx); 3846 } 3847 3848 static void 3849 bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3850 { 3851 struct spdk_bs_load_ctx *ctx = cb_arg; 3852 uint64_t lba, lba_count, mask_size; 3853 int rc; 3854 3855 if (bserrno != 0) { 3856 bs_load_ctx_fail(ctx, bserrno); 3857 return; 3858 } 3859 3860 /* The type must be correct */ 3861 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 3862 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 3863 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 3864 struct spdk_blob_md_page) * 8)); 3865 /* 3866 * The length of the mask must be equal to or larger than the total number of clusters. It may be 3867 * larger than the total number of clusters due to a failure spdk_bs_grow. 3868 */ 3869 assert(ctx->mask->length >= ctx->bs->total_clusters); 3870 if (ctx->mask->length > ctx->bs->total_clusters) { 3871 SPDK_WARNLOG("Shrink the used_custers mask length to total_clusters"); 3872 ctx->mask->length = ctx->bs->total_clusters; 3873 } 3874 3875 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length); 3876 if (rc < 0) { 3877 spdk_free(ctx->mask); 3878 bs_load_ctx_fail(ctx, rc); 3879 return; 3880 } 3881 3882 spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask); 3883 ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters); 3884 assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters); 3885 3886 spdk_free(ctx->mask); 3887 3888 /* Read the used blobids mask */ 3889 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 3890 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3891 SPDK_MALLOC_DMA); 3892 if (!ctx->mask) { 3893 bs_load_ctx_fail(ctx, -ENOMEM); 3894 return; 3895 } 3896 lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 3897 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 3898 bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 3899 bs_load_used_blobids_cpl, ctx); 3900 } 3901 3902 static void 3903 bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3904 { 3905 struct spdk_bs_load_ctx *ctx = cb_arg; 3906 uint64_t lba, lba_count, mask_size; 3907 int rc; 3908 3909 if (bserrno != 0) { 3910 bs_load_ctx_fail(ctx, bserrno); 3911 return; 3912 } 3913 3914 /* The type must be correct */ 3915 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES); 3916 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 3917 assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE * 3918 8)); 3919 /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */ 3920 if (ctx->mask->length != ctx->super->md_len) { 3921 SPDK_ERRLOG("mismatched md_len in used_pages mask: " 3922 "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n", 3923 ctx->mask->length, ctx->super->md_len); 3924 assert(false); 3925 } 3926 3927 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length); 3928 if (rc < 0) { 3929 spdk_free(ctx->mask); 3930 bs_load_ctx_fail(ctx, rc); 3931 return; 3932 } 3933 3934 spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask); 3935 spdk_free(ctx->mask); 3936 3937 /* Read the used clusters mask */ 3938 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 3939 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 3940 SPDK_MALLOC_DMA); 3941 if (!ctx->mask) { 3942 bs_load_ctx_fail(ctx, -ENOMEM); 3943 return; 3944 } 3945 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 3946 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 3947 bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 3948 bs_load_used_clusters_cpl, ctx); 3949 } 3950 3951 static void 3952 bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx) 3953 { 3954 uint64_t lba, lba_count, mask_size; 3955 3956 /* Read the used pages mask */ 3957 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 3958 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, 3959 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 3960 if (!ctx->mask) { 3961 bs_load_ctx_fail(ctx, -ENOMEM); 3962 return; 3963 } 3964 3965 lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 3966 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 3967 bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count, 3968 bs_load_used_pages_cpl, ctx); 3969 } 3970 3971 static int 3972 bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page) 3973 { 3974 struct spdk_blob_store *bs = ctx->bs; 3975 struct spdk_blob_md_descriptor *desc; 3976 size_t cur_desc = 0; 3977 3978 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 3979 while (cur_desc < sizeof(page->descriptors)) { 3980 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 3981 if (desc->length == 0) { 3982 /* If padding and length are 0, this terminates the page */ 3983 break; 3984 } 3985 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 3986 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 3987 unsigned int i, j; 3988 unsigned int cluster_count = 0; 3989 uint32_t cluster_idx; 3990 3991 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 3992 3993 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 3994 for (j = 0; j < desc_extent_rle->extents[i].length; j++) { 3995 cluster_idx = desc_extent_rle->extents[i].cluster_idx; 3996 /* 3997 * cluster_idx = 0 means an unallocated cluster - don't mark that 3998 * in the used cluster map. 3999 */ 4000 if (cluster_idx != 0) { 4001 SPDK_NOTICELOG("Recover: cluster %" PRIu32 "\n", cluster_idx + j); 4002 spdk_bit_array_set(ctx->used_clusters, cluster_idx + j); 4003 if (bs->num_free_clusters == 0) { 4004 return -ENOSPC; 4005 } 4006 bs->num_free_clusters--; 4007 } 4008 cluster_count++; 4009 } 4010 } 4011 if (cluster_count == 0) { 4012 return -EINVAL; 4013 } 4014 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4015 struct spdk_blob_md_descriptor_extent_page *desc_extent; 4016 uint32_t i; 4017 uint32_t cluster_count = 0; 4018 uint32_t cluster_idx; 4019 size_t cluster_idx_length; 4020 4021 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 4022 cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx); 4023 4024 if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) || 4025 (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) { 4026 return -EINVAL; 4027 } 4028 4029 for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) { 4030 cluster_idx = desc_extent->cluster_idx[i]; 4031 /* 4032 * cluster_idx = 0 means an unallocated cluster - don't mark that 4033 * in the used cluster map. 4034 */ 4035 if (cluster_idx != 0) { 4036 if (cluster_idx < desc_extent->start_cluster_idx && 4037 cluster_idx >= desc_extent->start_cluster_idx + cluster_count) { 4038 return -EINVAL; 4039 } 4040 spdk_bit_array_set(ctx->used_clusters, cluster_idx); 4041 if (bs->num_free_clusters == 0) { 4042 return -ENOSPC; 4043 } 4044 bs->num_free_clusters--; 4045 } 4046 cluster_count++; 4047 } 4048 4049 if (cluster_count == 0) { 4050 return -EINVAL; 4051 } 4052 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 4053 /* Skip this item */ 4054 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 4055 /* Skip this item */ 4056 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 4057 /* Skip this item */ 4058 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) { 4059 struct spdk_blob_md_descriptor_extent_table *desc_extent_table; 4060 uint32_t num_extent_pages = ctx->num_extent_pages; 4061 uint32_t i; 4062 size_t extent_pages_length; 4063 void *tmp; 4064 4065 desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc; 4066 extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters); 4067 4068 if (desc_extent_table->length == 0 || 4069 (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) { 4070 return -EINVAL; 4071 } 4072 4073 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 4074 if (desc_extent_table->extent_page[i].page_idx != 0) { 4075 if (desc_extent_table->extent_page[i].num_pages != 1) { 4076 return -EINVAL; 4077 } 4078 num_extent_pages += 1; 4079 } 4080 } 4081 4082 if (num_extent_pages > 0) { 4083 tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t)); 4084 if (tmp == NULL) { 4085 return -ENOMEM; 4086 } 4087 ctx->extent_page_num = tmp; 4088 4089 /* Extent table entries contain md page numbers for extent pages. 4090 * Zeroes represent unallocated extent pages, those are run-length-encoded. 4091 */ 4092 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) { 4093 if (desc_extent_table->extent_page[i].page_idx != 0) { 4094 ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx; 4095 ctx->num_extent_pages += 1; 4096 } 4097 } 4098 } 4099 } else { 4100 /* Error */ 4101 return -EINVAL; 4102 } 4103 /* Advance to the next descriptor */ 4104 cur_desc += sizeof(*desc) + desc->length; 4105 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 4106 break; 4107 } 4108 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 4109 } 4110 return 0; 4111 } 4112 4113 static bool 4114 bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page) 4115 { 4116 uint32_t crc; 4117 struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors; 4118 size_t desc_len; 4119 4120 crc = blob_md_page_calc_crc(page); 4121 if (crc != page->crc) { 4122 return false; 4123 } 4124 4125 /* Extent page should always be of sequence num 0. */ 4126 if (page->sequence_num != 0) { 4127 return false; 4128 } 4129 4130 /* Descriptor type must be EXTENT_PAGE. */ 4131 if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4132 return false; 4133 } 4134 4135 /* Descriptor length cannot exceed the page. */ 4136 desc_len = sizeof(*desc) + desc->length; 4137 if (desc_len > sizeof(page->descriptors)) { 4138 return false; 4139 } 4140 4141 /* It has to be the only descriptor in the page. */ 4142 if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) { 4143 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len); 4144 if (desc->length != 0) { 4145 return false; 4146 } 4147 } 4148 4149 return true; 4150 } 4151 4152 static bool 4153 bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx) 4154 { 4155 uint32_t crc; 4156 struct spdk_blob_md_page *page = ctx->page; 4157 4158 crc = blob_md_page_calc_crc(page); 4159 if (crc != page->crc) { 4160 return false; 4161 } 4162 4163 /* First page of a sequence should match the blobid. */ 4164 if (page->sequence_num == 0 && 4165 bs_page_to_blobid(ctx->cur_page) != page->id) { 4166 return false; 4167 } 4168 assert(bs_load_cur_extent_page_valid(page) == false); 4169 4170 return true; 4171 } 4172 4173 static void bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx); 4174 4175 static void 4176 bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4177 { 4178 struct spdk_bs_load_ctx *ctx = cb_arg; 4179 4180 if (bserrno != 0) { 4181 bs_load_ctx_fail(ctx, bserrno); 4182 return; 4183 } 4184 4185 bs_load_complete(ctx); 4186 } 4187 4188 static void 4189 bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4190 { 4191 struct spdk_bs_load_ctx *ctx = cb_arg; 4192 4193 spdk_free(ctx->mask); 4194 ctx->mask = NULL; 4195 4196 if (bserrno != 0) { 4197 bs_load_ctx_fail(ctx, bserrno); 4198 return; 4199 } 4200 4201 bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl); 4202 } 4203 4204 static void 4205 bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4206 { 4207 struct spdk_bs_load_ctx *ctx = cb_arg; 4208 4209 spdk_free(ctx->mask); 4210 ctx->mask = NULL; 4211 4212 if (bserrno != 0) { 4213 bs_load_ctx_fail(ctx, bserrno); 4214 return; 4215 } 4216 4217 bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl); 4218 } 4219 4220 static void 4221 bs_load_write_used_md(struct spdk_bs_load_ctx *ctx) 4222 { 4223 bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl); 4224 } 4225 4226 static void 4227 bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx) 4228 { 4229 uint64_t num_md_clusters; 4230 uint64_t i; 4231 4232 ctx->in_page_chain = false; 4233 4234 do { 4235 ctx->page_index++; 4236 } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true); 4237 4238 if (ctx->page_index < ctx->super->md_len) { 4239 ctx->cur_page = ctx->page_index; 4240 bs_load_replay_cur_md_page(ctx); 4241 } else { 4242 /* Claim all of the clusters used by the metadata */ 4243 num_md_clusters = spdk_divide_round_up( 4244 ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster); 4245 for (i = 0; i < num_md_clusters; i++) { 4246 spdk_bit_array_set(ctx->used_clusters, i); 4247 } 4248 ctx->bs->num_free_clusters -= num_md_clusters; 4249 spdk_free(ctx->page); 4250 bs_load_write_used_md(ctx); 4251 } 4252 } 4253 4254 static void 4255 bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4256 { 4257 struct spdk_bs_load_ctx *ctx = cb_arg; 4258 uint32_t page_num; 4259 uint64_t i; 4260 4261 if (bserrno != 0) { 4262 spdk_free(ctx->extent_pages); 4263 bs_load_ctx_fail(ctx, bserrno); 4264 return; 4265 } 4266 4267 for (i = 0; i < ctx->num_extent_pages; i++) { 4268 /* Extent pages are only read when present within in chain md. 4269 * Integrity of md is not right if that page was not a valid extent page. */ 4270 if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) { 4271 spdk_free(ctx->extent_pages); 4272 bs_load_ctx_fail(ctx, -EILSEQ); 4273 return; 4274 } 4275 4276 page_num = ctx->extent_page_num[i]; 4277 spdk_bit_array_set(ctx->bs->used_md_pages, page_num); 4278 if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) { 4279 spdk_free(ctx->extent_pages); 4280 bs_load_ctx_fail(ctx, -EILSEQ); 4281 return; 4282 } 4283 } 4284 4285 spdk_free(ctx->extent_pages); 4286 free(ctx->extent_page_num); 4287 ctx->extent_page_num = NULL; 4288 ctx->num_extent_pages = 0; 4289 4290 bs_load_replay_md_chain_cpl(ctx); 4291 } 4292 4293 static void 4294 bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx) 4295 { 4296 spdk_bs_batch_t *batch; 4297 uint32_t page; 4298 uint64_t lba; 4299 uint64_t i; 4300 4301 ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0, 4302 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4303 if (!ctx->extent_pages) { 4304 bs_load_ctx_fail(ctx, -ENOMEM); 4305 return; 4306 } 4307 4308 batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx); 4309 4310 for (i = 0; i < ctx->num_extent_pages; i++) { 4311 page = ctx->extent_page_num[i]; 4312 assert(page < ctx->super->md_len); 4313 lba = bs_md_page_to_lba(ctx->bs, page); 4314 bs_batch_read_dev(batch, &ctx->extent_pages[i], lba, 4315 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE)); 4316 } 4317 4318 bs_batch_close(batch); 4319 } 4320 4321 static void 4322 bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4323 { 4324 struct spdk_bs_load_ctx *ctx = cb_arg; 4325 uint32_t page_num; 4326 struct spdk_blob_md_page *page; 4327 4328 if (bserrno != 0) { 4329 bs_load_ctx_fail(ctx, bserrno); 4330 return; 4331 } 4332 4333 page_num = ctx->cur_page; 4334 page = ctx->page; 4335 if (bs_load_cur_md_page_valid(ctx) == true) { 4336 if (page->sequence_num == 0 || ctx->in_page_chain == true) { 4337 bs_claim_md_page(ctx->bs, page_num); 4338 if (page->sequence_num == 0) { 4339 SPDK_NOTICELOG("Recover: blob %" PRIu32 "\n", page_num); 4340 spdk_bit_array_set(ctx->bs->used_blobids, page_num); 4341 } 4342 if (bs_load_replay_md_parse_page(ctx, page)) { 4343 bs_load_ctx_fail(ctx, -EILSEQ); 4344 return; 4345 } 4346 if (page->next != SPDK_INVALID_MD_PAGE) { 4347 ctx->in_page_chain = true; 4348 ctx->cur_page = page->next; 4349 bs_load_replay_cur_md_page(ctx); 4350 return; 4351 } 4352 if (ctx->num_extent_pages != 0) { 4353 bs_load_replay_extent_pages(ctx); 4354 return; 4355 } 4356 } 4357 } 4358 bs_load_replay_md_chain_cpl(ctx); 4359 } 4360 4361 static void 4362 bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx) 4363 { 4364 uint64_t lba; 4365 4366 assert(ctx->cur_page < ctx->super->md_len); 4367 lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page); 4368 bs_sequence_read_dev(ctx->seq, ctx->page, lba, 4369 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 4370 bs_load_replay_md_cpl, ctx); 4371 } 4372 4373 static void 4374 bs_load_replay_md(struct spdk_bs_load_ctx *ctx) 4375 { 4376 ctx->page_index = 0; 4377 ctx->cur_page = 0; 4378 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 4379 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4380 if (!ctx->page) { 4381 bs_load_ctx_fail(ctx, -ENOMEM); 4382 return; 4383 } 4384 bs_load_replay_cur_md_page(ctx); 4385 } 4386 4387 static void 4388 bs_recover(struct spdk_bs_load_ctx *ctx) 4389 { 4390 int rc; 4391 4392 SPDK_NOTICELOG("Performing recovery on blobstore\n"); 4393 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); 4394 if (rc < 0) { 4395 bs_load_ctx_fail(ctx, -ENOMEM); 4396 return; 4397 } 4398 4399 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len); 4400 if (rc < 0) { 4401 bs_load_ctx_fail(ctx, -ENOMEM); 4402 return; 4403 } 4404 4405 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 4406 if (rc < 0) { 4407 bs_load_ctx_fail(ctx, -ENOMEM); 4408 return; 4409 } 4410 4411 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len); 4412 if (rc < 0) { 4413 bs_load_ctx_fail(ctx, -ENOMEM); 4414 return; 4415 } 4416 4417 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 4418 bs_load_replay_md(ctx); 4419 } 4420 4421 static int 4422 bs_parse_super(struct spdk_bs_load_ctx *ctx) 4423 { 4424 int rc; 4425 4426 if (ctx->super->size == 0) { 4427 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 4428 } 4429 4430 if (ctx->super->io_unit_size == 0) { 4431 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE; 4432 } 4433 4434 ctx->bs->clean = 1; 4435 ctx->bs->cluster_sz = ctx->super->cluster_size; 4436 ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size; 4437 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 4438 if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) { 4439 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster); 4440 } 4441 ctx->bs->io_unit_size = ctx->super->io_unit_size; 4442 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 4443 if (rc < 0) { 4444 return -ENOMEM; 4445 } 4446 ctx->bs->md_start = ctx->super->md_start; 4447 ctx->bs->md_len = ctx->super->md_len; 4448 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len); 4449 if (rc < 0) { 4450 return -ENOMEM; 4451 } 4452 4453 ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up( 4454 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 4455 ctx->bs->super_blob = ctx->super->super_blob; 4456 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 4457 4458 return 0; 4459 } 4460 4461 static void 4462 bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4463 { 4464 struct spdk_bs_load_ctx *ctx = cb_arg; 4465 uint32_t crc; 4466 int rc; 4467 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 4468 4469 if (ctx->super->version > SPDK_BS_VERSION || 4470 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 4471 bs_load_ctx_fail(ctx, -EILSEQ); 4472 return; 4473 } 4474 4475 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4476 sizeof(ctx->super->signature)) != 0) { 4477 bs_load_ctx_fail(ctx, -EILSEQ); 4478 return; 4479 } 4480 4481 crc = blob_md_page_calc_crc(ctx->super); 4482 if (crc != ctx->super->crc) { 4483 bs_load_ctx_fail(ctx, -EILSEQ); 4484 return; 4485 } 4486 4487 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 4488 SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n"); 4489 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 4490 SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n"); 4491 } else { 4492 SPDK_DEBUGLOG(blob, "Unexpected bstype\n"); 4493 SPDK_LOGDUMP(blob, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 4494 SPDK_LOGDUMP(blob, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 4495 bs_load_ctx_fail(ctx, -ENXIO); 4496 return; 4497 } 4498 4499 if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) { 4500 SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n", 4501 ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size); 4502 bs_load_ctx_fail(ctx, -EILSEQ); 4503 return; 4504 } 4505 4506 rc = bs_parse_super(ctx); 4507 if (rc < 0) { 4508 bs_load_ctx_fail(ctx, rc); 4509 return; 4510 } 4511 4512 if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0 || ctx->force_recover) { 4513 bs_recover(ctx); 4514 } else { 4515 bs_load_read_used_pages(ctx); 4516 } 4517 } 4518 4519 static inline int 4520 bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst) 4521 { 4522 4523 if (!src->opts_size) { 4524 SPDK_ERRLOG("opts_size should not be zero value\n"); 4525 return -1; 4526 } 4527 4528 #define FIELD_OK(field) \ 4529 offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size 4530 4531 #define SET_FIELD(field) \ 4532 if (FIELD_OK(field)) { \ 4533 dst->field = src->field; \ 4534 } \ 4535 4536 SET_FIELD(cluster_sz); 4537 SET_FIELD(num_md_pages); 4538 SET_FIELD(max_md_ops); 4539 SET_FIELD(max_channel_ops); 4540 SET_FIELD(clear_method); 4541 4542 if (FIELD_OK(bstype)) { 4543 memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype)); 4544 } 4545 SET_FIELD(iter_cb_fn); 4546 SET_FIELD(iter_cb_arg); 4547 SET_FIELD(force_recover); 4548 4549 dst->opts_size = src->opts_size; 4550 4551 /* You should not remove this statement, but need to update the assert statement 4552 * if you add a new field, and also add a corresponding SET_FIELD statement */ 4553 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 72, "Incorrect size"); 4554 4555 #undef FIELD_OK 4556 #undef SET_FIELD 4557 4558 return 0; 4559 } 4560 4561 void 4562 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 4563 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 4564 { 4565 struct spdk_blob_store *bs; 4566 struct spdk_bs_cpl cpl; 4567 struct spdk_bs_load_ctx *ctx; 4568 struct spdk_bs_opts opts = {}; 4569 int err; 4570 4571 SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev); 4572 4573 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 4574 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen); 4575 dev->destroy(dev); 4576 cb_fn(cb_arg, NULL, -EINVAL); 4577 return; 4578 } 4579 4580 spdk_bs_opts_init(&opts, sizeof(opts)); 4581 if (o) { 4582 if (bs_opts_copy(o, &opts)) { 4583 return; 4584 } 4585 } 4586 4587 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 4588 dev->destroy(dev); 4589 cb_fn(cb_arg, NULL, -EINVAL); 4590 return; 4591 } 4592 4593 err = bs_alloc(dev, &opts, &bs, &ctx); 4594 if (err) { 4595 dev->destroy(dev); 4596 cb_fn(cb_arg, NULL, err); 4597 return; 4598 } 4599 4600 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 4601 cpl.u.bs_handle.cb_fn = cb_fn; 4602 cpl.u.bs_handle.cb_arg = cb_arg; 4603 cpl.u.bs_handle.bs = bs; 4604 4605 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 4606 if (!ctx->seq) { 4607 spdk_free(ctx->super); 4608 free(ctx); 4609 bs_free(bs); 4610 cb_fn(cb_arg, NULL, -ENOMEM); 4611 return; 4612 } 4613 4614 /* Read the super block */ 4615 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 4616 bs_byte_to_lba(bs, sizeof(*ctx->super)), 4617 bs_load_super_cpl, ctx); 4618 } 4619 4620 /* END spdk_bs_load */ 4621 4622 /* START spdk_bs_dump */ 4623 4624 static void 4625 bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 4626 { 4627 spdk_free(ctx->super); 4628 4629 /* 4630 * We need to defer calling bs_call_cpl() until after 4631 * dev destruction, so tuck these away for later use. 4632 */ 4633 ctx->bs->unload_err = bserrno; 4634 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 4635 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 4636 4637 bs_sequence_finish(seq, 0); 4638 bs_free(ctx->bs); 4639 free(ctx); 4640 } 4641 4642 static void 4643 bs_dump_print_xattr(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc) 4644 { 4645 struct spdk_blob_md_descriptor_xattr *desc_xattr; 4646 uint32_t i; 4647 const char *type; 4648 4649 desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc; 4650 4651 if (desc_xattr->length != 4652 sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) + 4653 desc_xattr->name_length + desc_xattr->value_length) { 4654 } 4655 4656 memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length); 4657 ctx->xattr_name[desc_xattr->name_length] = '\0'; 4658 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 4659 type = "XATTR"; 4660 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 4661 type = "XATTR_INTERNAL"; 4662 } else { 4663 assert(false); 4664 type = "XATTR_?"; 4665 } 4666 fprintf(ctx->fp, "%s: name = \"%s\"\n", type, ctx->xattr_name); 4667 fprintf(ctx->fp, " value = \""); 4668 ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name, 4669 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 4670 desc_xattr->value_length); 4671 fprintf(ctx->fp, "\"\n"); 4672 for (i = 0; i < desc_xattr->value_length; i++) { 4673 if (i % 16 == 0) { 4674 fprintf(ctx->fp, " "); 4675 } 4676 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i)); 4677 if ((i + 1) % 16 == 0) { 4678 fprintf(ctx->fp, "\n"); 4679 } 4680 } 4681 if (i % 16 != 0) { 4682 fprintf(ctx->fp, "\n"); 4683 } 4684 } 4685 4686 struct type_flag_desc { 4687 uint64_t mask; 4688 uint64_t val; 4689 const char *name; 4690 }; 4691 4692 static void 4693 bs_dump_print_type_bits(struct spdk_bs_load_ctx *ctx, uint64_t flags, 4694 struct type_flag_desc *desc, size_t numflags) 4695 { 4696 uint64_t covered = 0; 4697 size_t i; 4698 4699 for (i = 0; i < numflags; i++) { 4700 if ((desc[i].mask & flags) != desc[i].val) { 4701 continue; 4702 } 4703 fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " %s", desc[i].val, desc[i].name); 4704 if (desc[i].mask != desc[i].val) { 4705 fprintf(ctx->fp, " (mask 0x%" PRIx64 " value 0x%" PRIx64 ")", 4706 desc[i].mask, desc[i].val); 4707 } 4708 fprintf(ctx->fp, "\n"); 4709 covered |= desc[i].mask; 4710 } 4711 if ((flags & ~covered) != 0) { 4712 fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " Unknown\n", flags & ~covered); 4713 } 4714 } 4715 4716 static void 4717 bs_dump_print_type_flags(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc) 4718 { 4719 struct spdk_blob_md_descriptor_flags *type_desc; 4720 #define ADD_FLAG(f) { f, f, #f } 4721 #define ADD_MASK_VAL(m, v) { m, v, #v } 4722 static struct type_flag_desc invalid[] = { 4723 ADD_FLAG(SPDK_BLOB_THIN_PROV), 4724 ADD_FLAG(SPDK_BLOB_INTERNAL_XATTR), 4725 ADD_FLAG(SPDK_BLOB_EXTENT_TABLE), 4726 }; 4727 static struct type_flag_desc data_ro[] = { 4728 ADD_FLAG(SPDK_BLOB_READ_ONLY), 4729 }; 4730 static struct type_flag_desc md_ro[] = { 4731 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_DEFAULT), 4732 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_NONE), 4733 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_UNMAP), 4734 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_WRITE_ZEROES), 4735 }; 4736 #undef ADD_FLAG 4737 #undef ADD_MASK_VAL 4738 4739 type_desc = (struct spdk_blob_md_descriptor_flags *)desc; 4740 fprintf(ctx->fp, "Flags:\n"); 4741 fprintf(ctx->fp, "\tinvalid: 0x%016" PRIx64 "\n", type_desc->invalid_flags); 4742 bs_dump_print_type_bits(ctx, type_desc->invalid_flags, invalid, 4743 SPDK_COUNTOF(invalid)); 4744 fprintf(ctx->fp, "\tdata_ro: 0x%016" PRIx64 "\n", type_desc->data_ro_flags); 4745 bs_dump_print_type_bits(ctx, type_desc->data_ro_flags, data_ro, 4746 SPDK_COUNTOF(data_ro)); 4747 fprintf(ctx->fp, "\t md_ro: 0x%016" PRIx64 "\n", type_desc->md_ro_flags); 4748 bs_dump_print_type_bits(ctx, type_desc->md_ro_flags, md_ro, 4749 SPDK_COUNTOF(md_ro)); 4750 } 4751 4752 static void 4753 bs_dump_print_extent_table(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc) 4754 { 4755 struct spdk_blob_md_descriptor_extent_table *et_desc; 4756 uint64_t num_extent_pages; 4757 uint32_t et_idx; 4758 4759 et_desc = (struct spdk_blob_md_descriptor_extent_table *)desc; 4760 num_extent_pages = (et_desc->length - sizeof(et_desc->num_clusters)) / 4761 sizeof(et_desc->extent_page[0]); 4762 4763 fprintf(ctx->fp, "Extent table:\n"); 4764 for (et_idx = 0; et_idx < num_extent_pages; et_idx++) { 4765 if (et_desc->extent_page[et_idx].page_idx == 0) { 4766 /* Zeroes represent unallocated extent pages. */ 4767 continue; 4768 } 4769 fprintf(ctx->fp, "\tExtent page: %5" PRIu32 " length %3" PRIu32 4770 " at LBA %" PRIu64 "\n", et_desc->extent_page[et_idx].page_idx, 4771 et_desc->extent_page[et_idx].num_pages, 4772 bs_md_page_to_lba(ctx->bs, et_desc->extent_page[et_idx].page_idx)); 4773 } 4774 } 4775 4776 static void 4777 bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx) 4778 { 4779 uint32_t page_idx = ctx->cur_page; 4780 struct spdk_blob_md_page *page = ctx->page; 4781 struct spdk_blob_md_descriptor *desc; 4782 size_t cur_desc = 0; 4783 uint32_t crc; 4784 4785 fprintf(ctx->fp, "=========\n"); 4786 fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx); 4787 fprintf(ctx->fp, "Start LBA: %" PRIu64 "\n", bs_md_page_to_lba(ctx->bs, page_idx)); 4788 fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id); 4789 fprintf(ctx->fp, "Sequence: %" PRIu32 "\n", page->sequence_num); 4790 if (page->next == SPDK_INVALID_MD_PAGE) { 4791 fprintf(ctx->fp, "Next: None\n"); 4792 } else { 4793 fprintf(ctx->fp, "Next: %" PRIu32 "\n", page->next); 4794 } 4795 fprintf(ctx->fp, "In used bit array%s:", ctx->super->clean ? "" : " (not clean: dubious)"); 4796 if (spdk_bit_array_get(ctx->bs->used_md_pages, page_idx)) { 4797 fprintf(ctx->fp, " md"); 4798 } 4799 if (spdk_bit_array_get(ctx->bs->used_blobids, page_idx)) { 4800 fprintf(ctx->fp, " blob"); 4801 } 4802 fprintf(ctx->fp, "\n"); 4803 4804 crc = blob_md_page_calc_crc(page); 4805 fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch"); 4806 4807 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 4808 while (cur_desc < sizeof(page->descriptors)) { 4809 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 4810 if (desc->length == 0) { 4811 /* If padding and length are 0, this terminates the page */ 4812 break; 4813 } 4814 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) { 4815 struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle; 4816 unsigned int i; 4817 4818 desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc; 4819 4820 for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) { 4821 if (desc_extent_rle->extents[i].cluster_idx != 0) { 4822 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 4823 desc_extent_rle->extents[i].cluster_idx); 4824 } else { 4825 fprintf(ctx->fp, "Unallocated Extent - "); 4826 } 4827 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length); 4828 fprintf(ctx->fp, "\n"); 4829 } 4830 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) { 4831 struct spdk_blob_md_descriptor_extent_page *desc_extent; 4832 unsigned int i; 4833 4834 desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc; 4835 4836 for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) { 4837 if (desc_extent->cluster_idx[i] != 0) { 4838 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 4839 desc_extent->cluster_idx[i]); 4840 } else { 4841 fprintf(ctx->fp, "Unallocated Extent"); 4842 } 4843 fprintf(ctx->fp, "\n"); 4844 } 4845 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 4846 bs_dump_print_xattr(ctx, desc); 4847 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 4848 bs_dump_print_xattr(ctx, desc); 4849 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 4850 bs_dump_print_type_flags(ctx, desc); 4851 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) { 4852 bs_dump_print_extent_table(ctx, desc); 4853 } else { 4854 /* Error */ 4855 fprintf(ctx->fp, "Unknown descriptor type %" PRIu8 "\n", desc->type); 4856 } 4857 /* Advance to the next descriptor */ 4858 cur_desc += sizeof(*desc) + desc->length; 4859 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 4860 break; 4861 } 4862 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 4863 } 4864 } 4865 4866 static void 4867 bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4868 { 4869 struct spdk_bs_load_ctx *ctx = cb_arg; 4870 4871 if (bserrno != 0) { 4872 bs_dump_finish(seq, ctx, bserrno); 4873 return; 4874 } 4875 4876 if (ctx->page->id != 0) { 4877 bs_dump_print_md_page(ctx); 4878 } 4879 4880 ctx->cur_page++; 4881 4882 if (ctx->cur_page < ctx->super->md_len) { 4883 bs_dump_read_md_page(seq, ctx); 4884 } else { 4885 spdk_free(ctx->page); 4886 bs_dump_finish(seq, ctx, 0); 4887 } 4888 } 4889 4890 static void 4891 bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 4892 { 4893 struct spdk_bs_load_ctx *ctx = cb_arg; 4894 uint64_t lba; 4895 4896 assert(ctx->cur_page < ctx->super->md_len); 4897 lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 4898 bs_sequence_read_dev(seq, ctx->page, lba, 4899 bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 4900 bs_dump_read_md_page_cpl, ctx); 4901 } 4902 4903 static void 4904 bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4905 { 4906 struct spdk_bs_load_ctx *ctx = cb_arg; 4907 int rc; 4908 4909 fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature); 4910 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 4911 sizeof(ctx->super->signature)) != 0) { 4912 fprintf(ctx->fp, "(Mismatch)\n"); 4913 bs_dump_finish(seq, ctx, bserrno); 4914 return; 4915 } else { 4916 fprintf(ctx->fp, "(OK)\n"); 4917 } 4918 fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version); 4919 fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc, 4920 (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch"); 4921 fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype); 4922 fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size); 4923 fprintf(ctx->fp, "Super Blob ID: "); 4924 if (ctx->super->super_blob == SPDK_BLOBID_INVALID) { 4925 fprintf(ctx->fp, "(None)\n"); 4926 } else { 4927 fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob); 4928 } 4929 fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean); 4930 fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start); 4931 fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len); 4932 fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start); 4933 fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len); 4934 fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start); 4935 fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len); 4936 fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start); 4937 fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len); 4938 4939 ctx->cur_page = 0; 4940 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, 4941 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 4942 if (!ctx->page) { 4943 bs_dump_finish(seq, ctx, -ENOMEM); 4944 return; 4945 } 4946 4947 rc = bs_parse_super(ctx); 4948 if (rc < 0) { 4949 bs_load_ctx_fail(ctx, rc); 4950 return; 4951 } 4952 4953 bs_load_read_used_pages(ctx); 4954 } 4955 4956 void 4957 spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn, 4958 spdk_bs_op_complete cb_fn, void *cb_arg) 4959 { 4960 struct spdk_blob_store *bs; 4961 struct spdk_bs_cpl cpl; 4962 struct spdk_bs_load_ctx *ctx; 4963 struct spdk_bs_opts opts = {}; 4964 int err; 4965 4966 SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev); 4967 4968 spdk_bs_opts_init(&opts, sizeof(opts)); 4969 4970 err = bs_alloc(dev, &opts, &bs, &ctx); 4971 if (err) { 4972 dev->destroy(dev); 4973 cb_fn(cb_arg, err); 4974 return; 4975 } 4976 4977 ctx->dumping = true; 4978 ctx->fp = fp; 4979 ctx->print_xattr_fn = print_xattr_fn; 4980 4981 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 4982 cpl.u.bs_basic.cb_fn = cb_fn; 4983 cpl.u.bs_basic.cb_arg = cb_arg; 4984 4985 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 4986 if (!ctx->seq) { 4987 spdk_free(ctx->super); 4988 free(ctx); 4989 bs_free(bs); 4990 cb_fn(cb_arg, -ENOMEM); 4991 return; 4992 } 4993 4994 /* Read the super block */ 4995 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 4996 bs_byte_to_lba(bs, sizeof(*ctx->super)), 4997 bs_dump_super_cpl, ctx); 4998 } 4999 5000 /* END spdk_bs_dump */ 5001 5002 /* START spdk_bs_init */ 5003 5004 static void 5005 bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5006 { 5007 struct spdk_bs_load_ctx *ctx = cb_arg; 5008 5009 ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters); 5010 spdk_free(ctx->super); 5011 free(ctx); 5012 5013 bs_sequence_finish(seq, bserrno); 5014 } 5015 5016 static void 5017 bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5018 { 5019 struct spdk_bs_load_ctx *ctx = cb_arg; 5020 5021 /* Write super block */ 5022 bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0), 5023 bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 5024 bs_init_persist_super_cpl, ctx); 5025 } 5026 5027 void 5028 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 5029 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 5030 { 5031 struct spdk_bs_load_ctx *ctx; 5032 struct spdk_blob_store *bs; 5033 struct spdk_bs_cpl cpl; 5034 spdk_bs_sequence_t *seq; 5035 spdk_bs_batch_t *batch; 5036 uint64_t num_md_lba; 5037 uint64_t num_md_pages; 5038 uint64_t num_md_clusters; 5039 uint64_t max_used_cluster_mask_len; 5040 uint32_t i; 5041 struct spdk_bs_opts opts = {}; 5042 int rc; 5043 uint64_t lba, lba_count; 5044 5045 SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev); 5046 5047 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 5048 SPDK_ERRLOG("unsupported dev block length of %d\n", 5049 dev->blocklen); 5050 dev->destroy(dev); 5051 cb_fn(cb_arg, NULL, -EINVAL); 5052 return; 5053 } 5054 5055 spdk_bs_opts_init(&opts, sizeof(opts)); 5056 if (o) { 5057 if (bs_opts_copy(o, &opts)) { 5058 return; 5059 } 5060 } 5061 5062 if (bs_opts_verify(&opts) != 0) { 5063 dev->destroy(dev); 5064 cb_fn(cb_arg, NULL, -EINVAL); 5065 return; 5066 } 5067 5068 rc = bs_alloc(dev, &opts, &bs, &ctx); 5069 if (rc) { 5070 dev->destroy(dev); 5071 cb_fn(cb_arg, NULL, rc); 5072 return; 5073 } 5074 5075 if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) { 5076 /* By default, allocate 1 page per cluster. 5077 * Technically, this over-allocates metadata 5078 * because more metadata will reduce the number 5079 * of usable clusters. This can be addressed with 5080 * more complex math in the future. 5081 */ 5082 bs->md_len = bs->total_clusters; 5083 } else { 5084 bs->md_len = opts.num_md_pages; 5085 } 5086 rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len); 5087 if (rc < 0) { 5088 spdk_free(ctx->super); 5089 free(ctx); 5090 bs_free(bs); 5091 cb_fn(cb_arg, NULL, -ENOMEM); 5092 return; 5093 } 5094 5095 rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len); 5096 if (rc < 0) { 5097 spdk_free(ctx->super); 5098 free(ctx); 5099 bs_free(bs); 5100 cb_fn(cb_arg, NULL, -ENOMEM); 5101 return; 5102 } 5103 5104 rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len); 5105 if (rc < 0) { 5106 spdk_free(ctx->super); 5107 free(ctx); 5108 bs_free(bs); 5109 cb_fn(cb_arg, NULL, -ENOMEM); 5110 return; 5111 } 5112 5113 memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 5114 sizeof(ctx->super->signature)); 5115 ctx->super->version = SPDK_BS_VERSION; 5116 ctx->super->length = sizeof(*ctx->super); 5117 ctx->super->super_blob = bs->super_blob; 5118 ctx->super->clean = 0; 5119 ctx->super->cluster_size = bs->cluster_sz; 5120 ctx->super->io_unit_size = bs->io_unit_size; 5121 memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype)); 5122 5123 /* Calculate how many pages the metadata consumes at the front 5124 * of the disk. 5125 */ 5126 5127 /* The super block uses 1 page */ 5128 num_md_pages = 1; 5129 5130 /* The used_md_pages mask requires 1 bit per metadata page, rounded 5131 * up to the nearest page, plus a header. 5132 */ 5133 ctx->super->used_page_mask_start = num_md_pages; 5134 ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 5135 spdk_divide_round_up(bs->md_len, 8), 5136 SPDK_BS_PAGE_SIZE); 5137 num_md_pages += ctx->super->used_page_mask_len; 5138 5139 /* The used_clusters mask requires 1 bit per cluster, rounded 5140 * up to the nearest page, plus a header. 5141 */ 5142 ctx->super->used_cluster_mask_start = num_md_pages; 5143 ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 5144 spdk_divide_round_up(bs->total_clusters, 8), 5145 SPDK_BS_PAGE_SIZE); 5146 /* The blobstore might be extended, then the used_cluster bitmap will need more space. 5147 * Here we calculate the max clusters we can support according to the 5148 * num_md_pages (bs->md_len). 5149 */ 5150 max_used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 5151 spdk_divide_round_up(bs->md_len, 8), 5152 SPDK_BS_PAGE_SIZE); 5153 max_used_cluster_mask_len = spdk_max(max_used_cluster_mask_len, 5154 ctx->super->used_cluster_mask_len); 5155 num_md_pages += max_used_cluster_mask_len; 5156 5157 /* The used_blobids mask requires 1 bit per metadata page, rounded 5158 * up to the nearest page, plus a header. 5159 */ 5160 ctx->super->used_blobid_mask_start = num_md_pages; 5161 ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 5162 spdk_divide_round_up(bs->md_len, 8), 5163 SPDK_BS_PAGE_SIZE); 5164 num_md_pages += ctx->super->used_blobid_mask_len; 5165 5166 /* The metadata region size was chosen above */ 5167 ctx->super->md_start = bs->md_start = num_md_pages; 5168 ctx->super->md_len = bs->md_len; 5169 num_md_pages += bs->md_len; 5170 5171 num_md_lba = bs_page_to_lba(bs, num_md_pages); 5172 5173 ctx->super->size = dev->blockcnt * dev->blocklen; 5174 5175 ctx->super->crc = blob_md_page_calc_crc(ctx->super); 5176 5177 num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster); 5178 if (num_md_clusters > bs->total_clusters) { 5179 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, " 5180 "please decrease number of pages reserved for metadata " 5181 "or increase cluster size.\n"); 5182 spdk_free(ctx->super); 5183 spdk_bit_array_free(&ctx->used_clusters); 5184 free(ctx); 5185 bs_free(bs); 5186 cb_fn(cb_arg, NULL, -ENOMEM); 5187 return; 5188 } 5189 /* Claim all of the clusters used by the metadata */ 5190 for (i = 0; i < num_md_clusters; i++) { 5191 spdk_bit_array_set(ctx->used_clusters, i); 5192 } 5193 5194 bs->num_free_clusters -= num_md_clusters; 5195 bs->total_data_clusters = bs->num_free_clusters; 5196 5197 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 5198 cpl.u.bs_handle.cb_fn = cb_fn; 5199 cpl.u.bs_handle.cb_arg = cb_arg; 5200 cpl.u.bs_handle.bs = bs; 5201 5202 seq = bs_sequence_start(bs->md_channel, &cpl); 5203 if (!seq) { 5204 spdk_free(ctx->super); 5205 free(ctx); 5206 bs_free(bs); 5207 cb_fn(cb_arg, NULL, -ENOMEM); 5208 return; 5209 } 5210 5211 batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx); 5212 5213 /* Clear metadata space */ 5214 bs_batch_write_zeroes_dev(batch, 0, num_md_lba); 5215 5216 lba = num_md_lba; 5217 lba_count = ctx->bs->dev->blockcnt - lba; 5218 switch (opts.clear_method) { 5219 case BS_CLEAR_WITH_UNMAP: 5220 /* Trim data clusters */ 5221 bs_batch_unmap_dev(batch, lba, lba_count); 5222 break; 5223 case BS_CLEAR_WITH_WRITE_ZEROES: 5224 /* Write_zeroes to data clusters */ 5225 bs_batch_write_zeroes_dev(batch, lba, lba_count); 5226 break; 5227 case BS_CLEAR_WITH_NONE: 5228 default: 5229 break; 5230 } 5231 5232 bs_batch_close(batch); 5233 } 5234 5235 /* END spdk_bs_init */ 5236 5237 /* START spdk_bs_destroy */ 5238 5239 static void 5240 bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5241 { 5242 struct spdk_bs_load_ctx *ctx = cb_arg; 5243 struct spdk_blob_store *bs = ctx->bs; 5244 5245 /* 5246 * We need to defer calling bs_call_cpl() until after 5247 * dev destruction, so tuck these away for later use. 5248 */ 5249 bs->unload_err = bserrno; 5250 memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 5251 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 5252 5253 bs_sequence_finish(seq, bserrno); 5254 5255 bs_free(bs); 5256 free(ctx); 5257 } 5258 5259 void 5260 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, 5261 void *cb_arg) 5262 { 5263 struct spdk_bs_cpl cpl; 5264 spdk_bs_sequence_t *seq; 5265 struct spdk_bs_load_ctx *ctx; 5266 5267 SPDK_DEBUGLOG(blob, "Destroying blobstore\n"); 5268 5269 if (!RB_EMPTY(&bs->open_blobs)) { 5270 SPDK_ERRLOG("Blobstore still has open blobs\n"); 5271 cb_fn(cb_arg, -EBUSY); 5272 return; 5273 } 5274 5275 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5276 cpl.u.bs_basic.cb_fn = cb_fn; 5277 cpl.u.bs_basic.cb_arg = cb_arg; 5278 5279 ctx = calloc(1, sizeof(*ctx)); 5280 if (!ctx) { 5281 cb_fn(cb_arg, -ENOMEM); 5282 return; 5283 } 5284 5285 ctx->bs = bs; 5286 5287 seq = bs_sequence_start(bs->md_channel, &cpl); 5288 if (!seq) { 5289 free(ctx); 5290 cb_fn(cb_arg, -ENOMEM); 5291 return; 5292 } 5293 5294 /* Write zeroes to the super block */ 5295 bs_sequence_write_zeroes_dev(seq, 5296 bs_page_to_lba(bs, 0), 5297 bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)), 5298 bs_destroy_trim_cpl, ctx); 5299 } 5300 5301 /* END spdk_bs_destroy */ 5302 5303 /* START spdk_bs_unload */ 5304 5305 static void 5306 bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno) 5307 { 5308 spdk_bs_sequence_t *seq = ctx->seq; 5309 5310 spdk_free(ctx->super); 5311 5312 /* 5313 * We need to defer calling bs_call_cpl() until after 5314 * dev destruction, so tuck these away for later use. 5315 */ 5316 ctx->bs->unload_err = bserrno; 5317 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 5318 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 5319 5320 bs_sequence_finish(seq, bserrno); 5321 5322 bs_free(ctx->bs); 5323 free(ctx); 5324 } 5325 5326 static void 5327 bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5328 { 5329 struct spdk_bs_load_ctx *ctx = cb_arg; 5330 5331 bs_unload_finish(ctx, bserrno); 5332 } 5333 5334 static void 5335 bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5336 { 5337 struct spdk_bs_load_ctx *ctx = cb_arg; 5338 5339 spdk_free(ctx->mask); 5340 5341 if (bserrno != 0) { 5342 bs_unload_finish(ctx, bserrno); 5343 return; 5344 } 5345 5346 ctx->super->clean = 1; 5347 5348 bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx); 5349 } 5350 5351 static void 5352 bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5353 { 5354 struct spdk_bs_load_ctx *ctx = cb_arg; 5355 5356 spdk_free(ctx->mask); 5357 ctx->mask = NULL; 5358 5359 if (bserrno != 0) { 5360 bs_unload_finish(ctx, bserrno); 5361 return; 5362 } 5363 5364 bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl); 5365 } 5366 5367 static void 5368 bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5369 { 5370 struct spdk_bs_load_ctx *ctx = cb_arg; 5371 5372 spdk_free(ctx->mask); 5373 ctx->mask = NULL; 5374 5375 if (bserrno != 0) { 5376 bs_unload_finish(ctx, bserrno); 5377 return; 5378 } 5379 5380 bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl); 5381 } 5382 5383 static void 5384 bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5385 { 5386 struct spdk_bs_load_ctx *ctx = cb_arg; 5387 5388 if (bserrno != 0) { 5389 bs_unload_finish(ctx, bserrno); 5390 return; 5391 } 5392 5393 bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl); 5394 } 5395 5396 void 5397 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 5398 { 5399 struct spdk_bs_cpl cpl; 5400 struct spdk_bs_load_ctx *ctx; 5401 5402 SPDK_DEBUGLOG(blob, "Syncing blobstore\n"); 5403 5404 if (!RB_EMPTY(&bs->open_blobs)) { 5405 SPDK_ERRLOG("Blobstore still has open blobs\n"); 5406 cb_fn(cb_arg, -EBUSY); 5407 return; 5408 } 5409 5410 ctx = calloc(1, sizeof(*ctx)); 5411 if (!ctx) { 5412 cb_fn(cb_arg, -ENOMEM); 5413 return; 5414 } 5415 5416 ctx->bs = bs; 5417 5418 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 5419 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 5420 if (!ctx->super) { 5421 free(ctx); 5422 cb_fn(cb_arg, -ENOMEM); 5423 return; 5424 } 5425 5426 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5427 cpl.u.bs_basic.cb_fn = cb_fn; 5428 cpl.u.bs_basic.cb_arg = cb_arg; 5429 5430 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 5431 if (!ctx->seq) { 5432 spdk_free(ctx->super); 5433 free(ctx); 5434 cb_fn(cb_arg, -ENOMEM); 5435 return; 5436 } 5437 5438 /* Read super block */ 5439 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 5440 bs_byte_to_lba(bs, sizeof(*ctx->super)), 5441 bs_unload_read_super_cpl, ctx); 5442 } 5443 5444 /* END spdk_bs_unload */ 5445 5446 /* START spdk_bs_set_super */ 5447 5448 struct spdk_bs_set_super_ctx { 5449 struct spdk_blob_store *bs; 5450 struct spdk_bs_super_block *super; 5451 }; 5452 5453 static void 5454 bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5455 { 5456 struct spdk_bs_set_super_ctx *ctx = cb_arg; 5457 5458 if (bserrno != 0) { 5459 SPDK_ERRLOG("Unable to write to super block of blobstore\n"); 5460 } 5461 5462 spdk_free(ctx->super); 5463 5464 bs_sequence_finish(seq, bserrno); 5465 5466 free(ctx); 5467 } 5468 5469 static void 5470 bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5471 { 5472 struct spdk_bs_set_super_ctx *ctx = cb_arg; 5473 5474 if (bserrno != 0) { 5475 SPDK_ERRLOG("Unable to read super block of blobstore\n"); 5476 spdk_free(ctx->super); 5477 bs_sequence_finish(seq, bserrno); 5478 free(ctx); 5479 return; 5480 } 5481 5482 bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx); 5483 } 5484 5485 void 5486 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 5487 spdk_bs_op_complete cb_fn, void *cb_arg) 5488 { 5489 struct spdk_bs_cpl cpl; 5490 spdk_bs_sequence_t *seq; 5491 struct spdk_bs_set_super_ctx *ctx; 5492 5493 SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n"); 5494 5495 ctx = calloc(1, sizeof(*ctx)); 5496 if (!ctx) { 5497 cb_fn(cb_arg, -ENOMEM); 5498 return; 5499 } 5500 5501 ctx->bs = bs; 5502 5503 ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL, 5504 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 5505 if (!ctx->super) { 5506 free(ctx); 5507 cb_fn(cb_arg, -ENOMEM); 5508 return; 5509 } 5510 5511 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 5512 cpl.u.bs_basic.cb_fn = cb_fn; 5513 cpl.u.bs_basic.cb_arg = cb_arg; 5514 5515 seq = bs_sequence_start(bs->md_channel, &cpl); 5516 if (!seq) { 5517 spdk_free(ctx->super); 5518 free(ctx); 5519 cb_fn(cb_arg, -ENOMEM); 5520 return; 5521 } 5522 5523 bs->super_blob = blobid; 5524 5525 /* Read super block */ 5526 bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0), 5527 bs_byte_to_lba(bs, sizeof(*ctx->super)), 5528 bs_set_super_read_cpl, ctx); 5529 } 5530 5531 /* END spdk_bs_set_super */ 5532 5533 void 5534 spdk_bs_get_super(struct spdk_blob_store *bs, 5535 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5536 { 5537 if (bs->super_blob == SPDK_BLOBID_INVALID) { 5538 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT); 5539 } else { 5540 cb_fn(cb_arg, bs->super_blob, 0); 5541 } 5542 } 5543 5544 uint64_t 5545 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 5546 { 5547 return bs->cluster_sz; 5548 } 5549 5550 uint64_t 5551 spdk_bs_get_page_size(struct spdk_blob_store *bs) 5552 { 5553 return SPDK_BS_PAGE_SIZE; 5554 } 5555 5556 uint64_t 5557 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs) 5558 { 5559 return bs->io_unit_size; 5560 } 5561 5562 uint64_t 5563 spdk_bs_free_cluster_count(struct spdk_blob_store *bs) 5564 { 5565 return bs->num_free_clusters; 5566 } 5567 5568 uint64_t 5569 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs) 5570 { 5571 return bs->total_data_clusters; 5572 } 5573 5574 static int 5575 bs_register_md_thread(struct spdk_blob_store *bs) 5576 { 5577 bs->md_channel = spdk_get_io_channel(bs); 5578 if (!bs->md_channel) { 5579 SPDK_ERRLOG("Failed to get IO channel.\n"); 5580 return -1; 5581 } 5582 5583 return 0; 5584 } 5585 5586 static int 5587 bs_unregister_md_thread(struct spdk_blob_store *bs) 5588 { 5589 spdk_put_io_channel(bs->md_channel); 5590 5591 return 0; 5592 } 5593 5594 spdk_blob_id 5595 spdk_blob_get_id(struct spdk_blob *blob) 5596 { 5597 assert(blob != NULL); 5598 5599 return blob->id; 5600 } 5601 5602 uint64_t 5603 spdk_blob_get_num_pages(struct spdk_blob *blob) 5604 { 5605 assert(blob != NULL); 5606 5607 return bs_cluster_to_page(blob->bs, blob->active.num_clusters); 5608 } 5609 5610 uint64_t 5611 spdk_blob_get_num_io_units(struct spdk_blob *blob) 5612 { 5613 assert(blob != NULL); 5614 5615 return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs); 5616 } 5617 5618 uint64_t 5619 spdk_blob_get_num_clusters(struct spdk_blob *blob) 5620 { 5621 assert(blob != NULL); 5622 5623 return blob->active.num_clusters; 5624 } 5625 5626 static uint64_t 5627 blob_find_io_unit(struct spdk_blob *blob, uint64_t offset, bool is_allocated) 5628 { 5629 uint64_t blob_io_unit_num = spdk_blob_get_num_io_units(blob); 5630 5631 while (offset < blob_io_unit_num) { 5632 if (bs_io_unit_is_allocated(blob, offset) == is_allocated) { 5633 return offset; 5634 } 5635 5636 offset += bs_num_io_units_to_cluster_boundary(blob, offset); 5637 } 5638 5639 return UINT64_MAX; 5640 } 5641 5642 uint64_t 5643 spdk_blob_get_next_allocated_io_unit(struct spdk_blob *blob, uint64_t offset) 5644 { 5645 return blob_find_io_unit(blob, offset, true); 5646 } 5647 5648 uint64_t 5649 spdk_blob_get_next_unallocated_io_unit(struct spdk_blob *blob, uint64_t offset) 5650 { 5651 return blob_find_io_unit(blob, offset, false); 5652 } 5653 5654 /* START spdk_bs_create_blob */ 5655 5656 static void 5657 bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5658 { 5659 struct spdk_blob *blob = cb_arg; 5660 uint32_t page_idx = bs_blobid_to_page(blob->id); 5661 5662 if (bserrno != 0) { 5663 spdk_bit_array_clear(blob->bs->used_blobids, page_idx); 5664 bs_release_md_page(blob->bs, page_idx); 5665 } 5666 5667 blob_free(blob); 5668 5669 bs_sequence_finish(seq, bserrno); 5670 } 5671 5672 static int 5673 blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs, 5674 bool internal) 5675 { 5676 uint64_t i; 5677 size_t value_len = 0; 5678 int rc; 5679 const void *value = NULL; 5680 if (xattrs->count > 0 && xattrs->get_value == NULL) { 5681 return -EINVAL; 5682 } 5683 for (i = 0; i < xattrs->count; i++) { 5684 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len); 5685 if (value == NULL || value_len == 0) { 5686 return -EINVAL; 5687 } 5688 rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal); 5689 if (rc < 0) { 5690 return rc; 5691 } 5692 } 5693 return 0; 5694 } 5695 5696 static void 5697 blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst) 5698 { 5699 #define FIELD_OK(field) \ 5700 offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size 5701 5702 #define SET_FIELD(field) \ 5703 if (FIELD_OK(field)) { \ 5704 dst->field = src->field; \ 5705 } \ 5706 5707 SET_FIELD(num_clusters); 5708 SET_FIELD(thin_provision); 5709 SET_FIELD(clear_method); 5710 5711 if (FIELD_OK(xattrs)) { 5712 memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs)); 5713 } 5714 5715 SET_FIELD(use_extent_table); 5716 5717 dst->opts_size = src->opts_size; 5718 5719 /* You should not remove this statement, but need to update the assert statement 5720 * if you add a new field, and also add a corresponding SET_FIELD statement */ 5721 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 64, "Incorrect size"); 5722 5723 #undef FIELD_OK 5724 #undef SET_FIELD 5725 } 5726 5727 static void 5728 bs_create_blob(struct spdk_blob_store *bs, 5729 const struct spdk_blob_opts *opts, 5730 const struct spdk_blob_xattr_opts *internal_xattrs, 5731 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5732 { 5733 struct spdk_blob *blob; 5734 uint32_t page_idx; 5735 struct spdk_bs_cpl cpl; 5736 struct spdk_blob_opts opts_local; 5737 struct spdk_blob_xattr_opts internal_xattrs_default; 5738 spdk_bs_sequence_t *seq; 5739 spdk_blob_id id; 5740 int rc; 5741 5742 assert(spdk_get_thread() == bs->md_thread); 5743 5744 page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0); 5745 if (page_idx == UINT32_MAX) { 5746 cb_fn(cb_arg, 0, -ENOMEM); 5747 return; 5748 } 5749 spdk_bit_array_set(bs->used_blobids, page_idx); 5750 bs_claim_md_page(bs, page_idx); 5751 5752 id = bs_page_to_blobid(page_idx); 5753 5754 SPDK_DEBUGLOG(blob, "Creating blob with id %" PRIu64 " at page %u\n", id, page_idx); 5755 5756 blob = blob_alloc(bs, id); 5757 if (!blob) { 5758 spdk_bit_array_clear(bs->used_blobids, page_idx); 5759 bs_release_md_page(bs, page_idx); 5760 cb_fn(cb_arg, 0, -ENOMEM); 5761 return; 5762 } 5763 5764 spdk_blob_opts_init(&opts_local, sizeof(opts_local)); 5765 if (opts) { 5766 blob_opts_copy(opts, &opts_local); 5767 } 5768 5769 blob->use_extent_table = opts_local.use_extent_table; 5770 if (blob->use_extent_table) { 5771 blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE; 5772 } 5773 5774 if (!internal_xattrs) { 5775 blob_xattrs_init(&internal_xattrs_default); 5776 internal_xattrs = &internal_xattrs_default; 5777 } 5778 5779 rc = blob_set_xattrs(blob, &opts_local.xattrs, false); 5780 if (rc < 0) { 5781 blob_free(blob); 5782 spdk_bit_array_clear(bs->used_blobids, page_idx); 5783 bs_release_md_page(bs, page_idx); 5784 cb_fn(cb_arg, 0, rc); 5785 return; 5786 } 5787 5788 rc = blob_set_xattrs(blob, internal_xattrs, true); 5789 if (rc < 0) { 5790 blob_free(blob); 5791 spdk_bit_array_clear(bs->used_blobids, page_idx); 5792 bs_release_md_page(bs, page_idx); 5793 cb_fn(cb_arg, 0, rc); 5794 return; 5795 } 5796 5797 if (opts_local.thin_provision) { 5798 blob_set_thin_provision(blob); 5799 } 5800 5801 blob_set_clear_method(blob, opts_local.clear_method); 5802 5803 rc = blob_resize(blob, opts_local.num_clusters); 5804 if (rc < 0) { 5805 blob_free(blob); 5806 spdk_bit_array_clear(bs->used_blobids, page_idx); 5807 bs_release_md_page(bs, page_idx); 5808 cb_fn(cb_arg, 0, rc); 5809 return; 5810 } 5811 cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 5812 cpl.u.blobid.cb_fn = cb_fn; 5813 cpl.u.blobid.cb_arg = cb_arg; 5814 cpl.u.blobid.blobid = blob->id; 5815 5816 seq = bs_sequence_start(bs->md_channel, &cpl); 5817 if (!seq) { 5818 blob_free(blob); 5819 spdk_bit_array_clear(bs->used_blobids, page_idx); 5820 bs_release_md_page(bs, page_idx); 5821 cb_fn(cb_arg, 0, -ENOMEM); 5822 return; 5823 } 5824 5825 blob_persist(seq, blob, bs_create_blob_cpl, blob); 5826 } 5827 5828 void 5829 spdk_bs_create_blob(struct spdk_blob_store *bs, 5830 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5831 { 5832 bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg); 5833 } 5834 5835 void 5836 spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts, 5837 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 5838 { 5839 bs_create_blob(bs, opts, NULL, cb_fn, cb_arg); 5840 } 5841 5842 /* END spdk_bs_create_blob */ 5843 5844 /* START blob_cleanup */ 5845 5846 struct spdk_clone_snapshot_ctx { 5847 struct spdk_bs_cpl cpl; 5848 int bserrno; 5849 bool frozen; 5850 5851 struct spdk_io_channel *channel; 5852 5853 /* Current cluster for inflate operation */ 5854 uint64_t cluster; 5855 5856 /* For inflation force allocation of all unallocated clusters and remove 5857 * thin-provisioning. Otherwise only decouple parent and keep clone thin. */ 5858 bool allocate_all; 5859 5860 struct { 5861 spdk_blob_id id; 5862 struct spdk_blob *blob; 5863 bool md_ro; 5864 } original; 5865 struct { 5866 spdk_blob_id id; 5867 struct spdk_blob *blob; 5868 } new; 5869 5870 /* xattrs specified for snapshot/clones only. They have no impact on 5871 * the original blobs xattrs. */ 5872 const struct spdk_blob_xattr_opts *xattrs; 5873 }; 5874 5875 static void 5876 bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno) 5877 { 5878 struct spdk_clone_snapshot_ctx *ctx = cb_arg; 5879 struct spdk_bs_cpl *cpl = &ctx->cpl; 5880 5881 if (bserrno != 0) { 5882 if (ctx->bserrno != 0) { 5883 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5884 } else { 5885 ctx->bserrno = bserrno; 5886 } 5887 } 5888 5889 switch (cpl->type) { 5890 case SPDK_BS_CPL_TYPE_BLOBID: 5891 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno); 5892 break; 5893 case SPDK_BS_CPL_TYPE_BLOB_BASIC: 5894 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno); 5895 break; 5896 default: 5897 SPDK_UNREACHABLE(); 5898 break; 5899 } 5900 5901 free(ctx); 5902 } 5903 5904 static void 5905 bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 5906 { 5907 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5908 struct spdk_blob *origblob = ctx->original.blob; 5909 5910 if (bserrno != 0) { 5911 if (ctx->bserrno != 0) { 5912 SPDK_ERRLOG("Unfreeze error %d\n", bserrno); 5913 } else { 5914 ctx->bserrno = bserrno; 5915 } 5916 } 5917 5918 ctx->original.id = origblob->id; 5919 origblob->locked_operation_in_progress = false; 5920 5921 /* Revert md_ro to original state */ 5922 origblob->md_ro = ctx->original.md_ro; 5923 5924 spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx); 5925 } 5926 5927 static void 5928 bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno) 5929 { 5930 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5931 struct spdk_blob *origblob = ctx->original.blob; 5932 5933 if (bserrno != 0) { 5934 if (ctx->bserrno != 0) { 5935 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5936 } else { 5937 ctx->bserrno = bserrno; 5938 } 5939 } 5940 5941 if (ctx->frozen) { 5942 /* Unfreeze any outstanding I/O */ 5943 blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx); 5944 } else { 5945 bs_snapshot_unfreeze_cpl(ctx, 0); 5946 } 5947 5948 } 5949 5950 static void 5951 bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno) 5952 { 5953 struct spdk_blob *newblob = ctx->new.blob; 5954 5955 if (bserrno != 0) { 5956 if (ctx->bserrno != 0) { 5957 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 5958 } else { 5959 ctx->bserrno = bserrno; 5960 } 5961 } 5962 5963 ctx->new.id = newblob->id; 5964 spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx); 5965 } 5966 5967 /* END blob_cleanup */ 5968 5969 /* START spdk_bs_create_snapshot */ 5970 5971 static void 5972 bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2) 5973 { 5974 uint64_t *cluster_temp; 5975 uint32_t *extent_page_temp; 5976 5977 cluster_temp = blob1->active.clusters; 5978 blob1->active.clusters = blob2->active.clusters; 5979 blob2->active.clusters = cluster_temp; 5980 5981 extent_page_temp = blob1->active.extent_pages; 5982 blob1->active.extent_pages = blob2->active.extent_pages; 5983 blob2->active.extent_pages = extent_page_temp; 5984 } 5985 5986 static void 5987 bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno) 5988 { 5989 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 5990 struct spdk_blob *origblob = ctx->original.blob; 5991 struct spdk_blob *newblob = ctx->new.blob; 5992 5993 if (bserrno != 0) { 5994 bs_snapshot_swap_cluster_maps(newblob, origblob); 5995 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 5996 return; 5997 } 5998 5999 /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */ 6000 bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true); 6001 if (bserrno != 0) { 6002 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6003 return; 6004 } 6005 6006 bs_blob_list_add(ctx->original.blob); 6007 6008 spdk_blob_set_read_only(newblob); 6009 6010 /* sync snapshot metadata */ 6011 spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx); 6012 } 6013 6014 static void 6015 bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno) 6016 { 6017 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6018 struct spdk_blob *origblob = ctx->original.blob; 6019 struct spdk_blob *newblob = ctx->new.blob; 6020 6021 if (bserrno != 0) { 6022 /* return cluster map back to original */ 6023 bs_snapshot_swap_cluster_maps(newblob, origblob); 6024 6025 /* Newblob md sync failed. Valid clusters are only present in origblob. 6026 * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred. 6027 * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */ 6028 blob_set_thin_provision(newblob); 6029 assert(spdk_mem_all_zero(newblob->active.clusters, 6030 newblob->active.num_clusters * sizeof(*newblob->active.clusters))); 6031 assert(spdk_mem_all_zero(newblob->active.extent_pages, 6032 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages))); 6033 6034 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 6035 return; 6036 } 6037 6038 /* Set internal xattr for snapshot id */ 6039 bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true); 6040 if (bserrno != 0) { 6041 /* return cluster map back to original */ 6042 bs_snapshot_swap_cluster_maps(newblob, origblob); 6043 blob_set_thin_provision(newblob); 6044 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 6045 return; 6046 } 6047 6048 /* Create new back_bs_dev for snapshot */ 6049 origblob->back_bs_dev = bs_create_blob_bs_dev(newblob); 6050 if (origblob->back_bs_dev == NULL) { 6051 /* return cluster map back to original */ 6052 bs_snapshot_swap_cluster_maps(newblob, origblob); 6053 blob_set_thin_provision(newblob); 6054 bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL); 6055 return; 6056 } 6057 6058 bs_blob_list_remove(origblob); 6059 origblob->parent_id = newblob->id; 6060 /* set clone blob as thin provisioned */ 6061 blob_set_thin_provision(origblob); 6062 6063 bs_blob_list_add(newblob); 6064 6065 /* sync clone metadata */ 6066 spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx); 6067 } 6068 6069 static void 6070 bs_snapshot_freeze_cpl(void *cb_arg, int rc) 6071 { 6072 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6073 struct spdk_blob *origblob = ctx->original.blob; 6074 struct spdk_blob *newblob = ctx->new.blob; 6075 int bserrno; 6076 6077 if (rc != 0) { 6078 bs_clone_snapshot_newblob_cleanup(ctx, rc); 6079 return; 6080 } 6081 6082 ctx->frozen = true; 6083 6084 if (newblob->back_bs_dev) { 6085 newblob->back_bs_dev->destroy(newblob->back_bs_dev); 6086 } 6087 /* set new back_bs_dev for snapshot */ 6088 newblob->back_bs_dev = origblob->back_bs_dev; 6089 /* Set invalid flags from origblob */ 6090 newblob->invalid_flags = origblob->invalid_flags; 6091 6092 /* inherit parent from original blob if set */ 6093 newblob->parent_id = origblob->parent_id; 6094 if (origblob->parent_id != SPDK_BLOBID_INVALID) { 6095 /* Set internal xattr for snapshot id */ 6096 bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT, 6097 &origblob->parent_id, sizeof(spdk_blob_id), true); 6098 if (bserrno != 0) { 6099 bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 6100 return; 6101 } 6102 } 6103 6104 /* swap cluster maps */ 6105 bs_snapshot_swap_cluster_maps(newblob, origblob); 6106 6107 /* Set the clear method on the new blob to match the original. */ 6108 blob_set_clear_method(newblob, origblob->clear_method); 6109 6110 /* sync snapshot metadata */ 6111 spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx); 6112 } 6113 6114 static void 6115 bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6116 { 6117 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6118 struct spdk_blob *origblob = ctx->original.blob; 6119 struct spdk_blob *newblob = _blob; 6120 6121 if (bserrno != 0) { 6122 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6123 return; 6124 } 6125 6126 ctx->new.blob = newblob; 6127 assert(spdk_blob_is_thin_provisioned(newblob)); 6128 assert(spdk_mem_all_zero(newblob->active.clusters, 6129 newblob->active.num_clusters * sizeof(*newblob->active.clusters))); 6130 assert(spdk_mem_all_zero(newblob->active.extent_pages, 6131 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages))); 6132 6133 blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx); 6134 } 6135 6136 static void 6137 bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 6138 { 6139 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6140 struct spdk_blob *origblob = ctx->original.blob; 6141 6142 if (bserrno != 0) { 6143 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6144 return; 6145 } 6146 6147 ctx->new.id = blobid; 6148 ctx->cpl.u.blobid.blobid = blobid; 6149 6150 spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx); 6151 } 6152 6153 6154 static void 6155 bs_xattr_snapshot(void *arg, const char *name, 6156 const void **value, size_t *value_len) 6157 { 6158 assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0); 6159 6160 struct spdk_blob *blob = (struct spdk_blob *)arg; 6161 *value = &blob->id; 6162 *value_len = sizeof(blob->id); 6163 } 6164 6165 static void 6166 bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6167 { 6168 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6169 struct spdk_blob_opts opts; 6170 struct spdk_blob_xattr_opts internal_xattrs; 6171 char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS }; 6172 6173 if (bserrno != 0) { 6174 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 6175 return; 6176 } 6177 6178 ctx->original.blob = _blob; 6179 6180 if (_blob->data_ro || _blob->md_ro) { 6181 SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id %" PRIu64 "\n", 6182 _blob->id); 6183 ctx->bserrno = -EINVAL; 6184 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6185 return; 6186 } 6187 6188 if (_blob->locked_operation_in_progress) { 6189 SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n"); 6190 ctx->bserrno = -EBUSY; 6191 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6192 return; 6193 } 6194 6195 _blob->locked_operation_in_progress = true; 6196 6197 spdk_blob_opts_init(&opts, sizeof(opts)); 6198 blob_xattrs_init(&internal_xattrs); 6199 6200 /* Change the size of new blob to the same as in original blob, 6201 * but do not allocate clusters */ 6202 opts.thin_provision = true; 6203 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 6204 opts.use_extent_table = _blob->use_extent_table; 6205 6206 /* If there are any xattrs specified for snapshot, set them now */ 6207 if (ctx->xattrs) { 6208 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 6209 } 6210 /* Set internal xattr SNAPSHOT_IN_PROGRESS */ 6211 internal_xattrs.count = 1; 6212 internal_xattrs.ctx = _blob; 6213 internal_xattrs.names = xattrs_names; 6214 internal_xattrs.get_value = bs_xattr_snapshot; 6215 6216 bs_create_blob(_blob->bs, &opts, &internal_xattrs, 6217 bs_snapshot_newblob_create_cpl, ctx); 6218 } 6219 6220 void 6221 spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid, 6222 const struct spdk_blob_xattr_opts *snapshot_xattrs, 6223 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 6224 { 6225 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6226 6227 if (!ctx) { 6228 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 6229 return; 6230 } 6231 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 6232 ctx->cpl.u.blobid.cb_fn = cb_fn; 6233 ctx->cpl.u.blobid.cb_arg = cb_arg; 6234 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 6235 ctx->bserrno = 0; 6236 ctx->frozen = false; 6237 ctx->original.id = blobid; 6238 ctx->xattrs = snapshot_xattrs; 6239 6240 spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx); 6241 } 6242 /* END spdk_bs_create_snapshot */ 6243 6244 /* START spdk_bs_create_clone */ 6245 6246 static void 6247 bs_xattr_clone(void *arg, const char *name, 6248 const void **value, size_t *value_len) 6249 { 6250 assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0); 6251 6252 struct spdk_blob *blob = (struct spdk_blob *)arg; 6253 *value = &blob->id; 6254 *value_len = sizeof(blob->id); 6255 } 6256 6257 static void 6258 bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6259 { 6260 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6261 struct spdk_blob *clone = _blob; 6262 6263 ctx->new.blob = clone; 6264 bs_blob_list_add(clone); 6265 6266 spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx); 6267 } 6268 6269 static void 6270 bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 6271 { 6272 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6273 6274 ctx->cpl.u.blobid.blobid = blobid; 6275 spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx); 6276 } 6277 6278 static void 6279 bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6280 { 6281 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6282 struct spdk_blob_opts opts; 6283 struct spdk_blob_xattr_opts internal_xattrs; 6284 char *xattr_names[] = { BLOB_SNAPSHOT }; 6285 6286 if (bserrno != 0) { 6287 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 6288 return; 6289 } 6290 6291 ctx->original.blob = _blob; 6292 ctx->original.md_ro = _blob->md_ro; 6293 6294 if (!_blob->data_ro || !_blob->md_ro) { 6295 SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n"); 6296 ctx->bserrno = -EINVAL; 6297 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6298 return; 6299 } 6300 6301 if (_blob->locked_operation_in_progress) { 6302 SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n"); 6303 ctx->bserrno = -EBUSY; 6304 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6305 return; 6306 } 6307 6308 _blob->locked_operation_in_progress = true; 6309 6310 spdk_blob_opts_init(&opts, sizeof(opts)); 6311 blob_xattrs_init(&internal_xattrs); 6312 6313 opts.thin_provision = true; 6314 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 6315 opts.use_extent_table = _blob->use_extent_table; 6316 if (ctx->xattrs) { 6317 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 6318 } 6319 6320 /* Set internal xattr BLOB_SNAPSHOT */ 6321 internal_xattrs.count = 1; 6322 internal_xattrs.ctx = _blob; 6323 internal_xattrs.names = xattr_names; 6324 internal_xattrs.get_value = bs_xattr_clone; 6325 6326 bs_create_blob(_blob->bs, &opts, &internal_xattrs, 6327 bs_clone_newblob_create_cpl, ctx); 6328 } 6329 6330 void 6331 spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid, 6332 const struct spdk_blob_xattr_opts *clone_xattrs, 6333 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 6334 { 6335 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6336 6337 if (!ctx) { 6338 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 6339 return; 6340 } 6341 6342 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 6343 ctx->cpl.u.blobid.cb_fn = cb_fn; 6344 ctx->cpl.u.blobid.cb_arg = cb_arg; 6345 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 6346 ctx->bserrno = 0; 6347 ctx->xattrs = clone_xattrs; 6348 ctx->original.id = blobid; 6349 6350 spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx); 6351 } 6352 6353 /* END spdk_bs_create_clone */ 6354 6355 /* START spdk_bs_inflate_blob */ 6356 6357 static void 6358 bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno) 6359 { 6360 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6361 struct spdk_blob *_blob = ctx->original.blob; 6362 6363 if (bserrno != 0) { 6364 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6365 return; 6366 } 6367 6368 /* Temporarily override md_ro flag for MD modification */ 6369 _blob->md_ro = false; 6370 6371 bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true); 6372 if (bserrno != 0) { 6373 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6374 return; 6375 } 6376 6377 assert(_parent != NULL); 6378 6379 bs_blob_list_remove(_blob); 6380 _blob->parent_id = _parent->id; 6381 6382 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6383 _blob->back_bs_dev = bs_create_blob_bs_dev(_parent); 6384 bs_blob_list_add(_blob); 6385 6386 spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx); 6387 } 6388 6389 static void 6390 bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx) 6391 { 6392 struct spdk_blob *_blob = ctx->original.blob; 6393 struct spdk_blob *_parent; 6394 6395 if (ctx->allocate_all) { 6396 /* remove thin provisioning */ 6397 bs_blob_list_remove(_blob); 6398 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV; 6399 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6400 _blob->back_bs_dev = NULL; 6401 _blob->parent_id = SPDK_BLOBID_INVALID; 6402 } else { 6403 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob; 6404 if (_parent->parent_id != SPDK_BLOBID_INVALID) { 6405 /* We must change the parent of the inflated blob */ 6406 spdk_bs_open_blob(_blob->bs, _parent->parent_id, 6407 bs_inflate_blob_set_parent_cpl, ctx); 6408 return; 6409 } 6410 6411 bs_blob_list_remove(_blob); 6412 _blob->parent_id = SPDK_BLOBID_INVALID; 6413 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 6414 _blob->back_bs_dev = bs_create_zeroes_dev(); 6415 } 6416 6417 /* Temporarily override md_ro flag for MD modification */ 6418 _blob->md_ro = false; 6419 blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 6420 _blob->state = SPDK_BLOB_STATE_DIRTY; 6421 6422 spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx); 6423 } 6424 6425 /* Check if cluster needs allocation */ 6426 static inline bool 6427 bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all) 6428 { 6429 struct spdk_blob_bs_dev *b; 6430 6431 assert(blob != NULL); 6432 6433 if (blob->active.clusters[cluster] != 0) { 6434 /* Cluster is already allocated */ 6435 return false; 6436 } 6437 6438 if (blob->parent_id == SPDK_BLOBID_INVALID) { 6439 /* Blob have no parent blob */ 6440 return allocate_all; 6441 } 6442 6443 b = (struct spdk_blob_bs_dev *)blob->back_bs_dev; 6444 return (allocate_all || b->blob->active.clusters[cluster] != 0); 6445 } 6446 6447 static void 6448 bs_inflate_blob_touch_next(void *cb_arg, int bserrno) 6449 { 6450 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6451 struct spdk_blob *_blob = ctx->original.blob; 6452 struct spdk_bs_cpl cpl; 6453 spdk_bs_user_op_t *op; 6454 uint64_t offset; 6455 6456 if (bserrno != 0) { 6457 bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 6458 return; 6459 } 6460 6461 for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) { 6462 if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) { 6463 break; 6464 } 6465 } 6466 6467 if (ctx->cluster < _blob->active.num_clusters) { 6468 offset = bs_cluster_to_lba(_blob->bs, ctx->cluster); 6469 6470 /* We may safely increment a cluster before copying */ 6471 ctx->cluster++; 6472 6473 /* Use a dummy 0B read as a context for cluster copy */ 6474 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6475 cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next; 6476 cpl.u.blob_basic.cb_arg = ctx; 6477 6478 op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob, 6479 NULL, 0, offset, 0); 6480 if (!op) { 6481 bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM); 6482 return; 6483 } 6484 6485 bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op); 6486 } else { 6487 bs_inflate_blob_done(ctx); 6488 } 6489 } 6490 6491 static void 6492 bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 6493 { 6494 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 6495 uint64_t clusters_needed; 6496 uint64_t i; 6497 6498 if (bserrno != 0) { 6499 bs_clone_snapshot_cleanup_finish(ctx, bserrno); 6500 return; 6501 } 6502 6503 ctx->original.blob = _blob; 6504 ctx->original.md_ro = _blob->md_ro; 6505 6506 if (_blob->locked_operation_in_progress) { 6507 SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n"); 6508 ctx->bserrno = -EBUSY; 6509 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx); 6510 return; 6511 } 6512 6513 _blob->locked_operation_in_progress = true; 6514 6515 if (!ctx->allocate_all && _blob->parent_id == SPDK_BLOBID_INVALID) { 6516 /* This blob have no parent, so we cannot decouple it. */ 6517 SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n"); 6518 bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 6519 return; 6520 } 6521 6522 if (spdk_blob_is_thin_provisioned(_blob) == false) { 6523 /* This is not thin provisioned blob. No need to inflate. */ 6524 bs_clone_snapshot_origblob_cleanup(ctx, 0); 6525 return; 6526 } 6527 6528 /* Do two passes - one to verify that we can obtain enough clusters 6529 * and another to actually claim them. 6530 */ 6531 clusters_needed = 0; 6532 for (i = 0; i < _blob->active.num_clusters; i++) { 6533 if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) { 6534 clusters_needed++; 6535 } 6536 } 6537 6538 if (clusters_needed > _blob->bs->num_free_clusters) { 6539 /* Not enough free clusters. Cannot satisfy the request. */ 6540 bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC); 6541 return; 6542 } 6543 6544 ctx->cluster = 0; 6545 bs_inflate_blob_touch_next(ctx, 0); 6546 } 6547 6548 static void 6549 bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6550 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg) 6551 { 6552 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 6553 6554 if (!ctx) { 6555 cb_fn(cb_arg, -ENOMEM); 6556 return; 6557 } 6558 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 6559 ctx->cpl.u.bs_basic.cb_fn = cb_fn; 6560 ctx->cpl.u.bs_basic.cb_arg = cb_arg; 6561 ctx->bserrno = 0; 6562 ctx->original.id = blobid; 6563 ctx->channel = channel; 6564 ctx->allocate_all = allocate_all; 6565 6566 spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx); 6567 } 6568 6569 void 6570 spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6571 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 6572 { 6573 bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg); 6574 } 6575 6576 void 6577 spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 6578 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 6579 { 6580 bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg); 6581 } 6582 /* END spdk_bs_inflate_blob */ 6583 6584 /* START spdk_blob_resize */ 6585 struct spdk_bs_resize_ctx { 6586 spdk_blob_op_complete cb_fn; 6587 void *cb_arg; 6588 struct spdk_blob *blob; 6589 uint64_t sz; 6590 int rc; 6591 }; 6592 6593 static void 6594 bs_resize_unfreeze_cpl(void *cb_arg, int rc) 6595 { 6596 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 6597 6598 if (rc != 0) { 6599 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc); 6600 } 6601 6602 if (ctx->rc != 0) { 6603 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc); 6604 rc = ctx->rc; 6605 } 6606 6607 ctx->blob->locked_operation_in_progress = false; 6608 6609 ctx->cb_fn(ctx->cb_arg, rc); 6610 free(ctx); 6611 } 6612 6613 static void 6614 bs_resize_freeze_cpl(void *cb_arg, int rc) 6615 { 6616 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 6617 6618 if (rc != 0) { 6619 ctx->blob->locked_operation_in_progress = false; 6620 ctx->cb_fn(ctx->cb_arg, rc); 6621 free(ctx); 6622 return; 6623 } 6624 6625 ctx->rc = blob_resize(ctx->blob, ctx->sz); 6626 6627 blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx); 6628 } 6629 6630 void 6631 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg) 6632 { 6633 struct spdk_bs_resize_ctx *ctx; 6634 6635 blob_verify_md_op(blob); 6636 6637 SPDK_DEBUGLOG(blob, "Resizing blob %" PRIu64 " to %" PRIu64 " clusters\n", blob->id, sz); 6638 6639 if (blob->md_ro) { 6640 cb_fn(cb_arg, -EPERM); 6641 return; 6642 } 6643 6644 if (sz == blob->active.num_clusters) { 6645 cb_fn(cb_arg, 0); 6646 return; 6647 } 6648 6649 if (blob->locked_operation_in_progress) { 6650 cb_fn(cb_arg, -EBUSY); 6651 return; 6652 } 6653 6654 ctx = calloc(1, sizeof(*ctx)); 6655 if (!ctx) { 6656 cb_fn(cb_arg, -ENOMEM); 6657 return; 6658 } 6659 6660 blob->locked_operation_in_progress = true; 6661 ctx->cb_fn = cb_fn; 6662 ctx->cb_arg = cb_arg; 6663 ctx->blob = blob; 6664 ctx->sz = sz; 6665 blob_freeze_io(blob, bs_resize_freeze_cpl, ctx); 6666 } 6667 6668 /* END spdk_blob_resize */ 6669 6670 6671 /* START spdk_bs_delete_blob */ 6672 6673 static void 6674 bs_delete_close_cpl(void *cb_arg, int bserrno) 6675 { 6676 spdk_bs_sequence_t *seq = cb_arg; 6677 6678 bs_sequence_finish(seq, bserrno); 6679 } 6680 6681 static void 6682 bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 6683 { 6684 struct spdk_blob *blob = cb_arg; 6685 6686 if (bserrno != 0) { 6687 /* 6688 * We already removed this blob from the blobstore tailq, so 6689 * we need to free it here since this is the last reference 6690 * to it. 6691 */ 6692 blob_free(blob); 6693 bs_delete_close_cpl(seq, bserrno); 6694 return; 6695 } 6696 6697 /* 6698 * This will immediately decrement the ref_count and call 6699 * the completion routine since the metadata state is clean. 6700 * By calling spdk_blob_close, we reduce the number of call 6701 * points into code that touches the blob->open_ref count 6702 * and the blobstore's blob list. 6703 */ 6704 spdk_blob_close(blob, bs_delete_close_cpl, seq); 6705 } 6706 6707 struct delete_snapshot_ctx { 6708 struct spdk_blob_list *parent_snapshot_entry; 6709 struct spdk_blob *snapshot; 6710 struct spdk_blob_md_page *page; 6711 bool snapshot_md_ro; 6712 struct spdk_blob *clone; 6713 bool clone_md_ro; 6714 spdk_blob_op_with_handle_complete cb_fn; 6715 void *cb_arg; 6716 int bserrno; 6717 uint32_t next_extent_page; 6718 }; 6719 6720 static void 6721 delete_blob_cleanup_finish(void *cb_arg, int bserrno) 6722 { 6723 struct delete_snapshot_ctx *ctx = cb_arg; 6724 6725 if (bserrno != 0) { 6726 SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno); 6727 } 6728 6729 assert(ctx != NULL); 6730 6731 if (bserrno != 0 && ctx->bserrno == 0) { 6732 ctx->bserrno = bserrno; 6733 } 6734 6735 ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno); 6736 spdk_free(ctx->page); 6737 free(ctx); 6738 } 6739 6740 static void 6741 delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno) 6742 { 6743 struct delete_snapshot_ctx *ctx = cb_arg; 6744 6745 if (bserrno != 0) { 6746 ctx->bserrno = bserrno; 6747 SPDK_ERRLOG("Clone cleanup error %d\n", bserrno); 6748 } 6749 6750 if (ctx->bserrno != 0) { 6751 assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL); 6752 RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot); 6753 spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id); 6754 } 6755 6756 ctx->snapshot->locked_operation_in_progress = false; 6757 ctx->snapshot->md_ro = ctx->snapshot_md_ro; 6758 6759 spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx); 6760 } 6761 6762 static void 6763 delete_snapshot_cleanup_clone(void *cb_arg, int bserrno) 6764 { 6765 struct delete_snapshot_ctx *ctx = cb_arg; 6766 6767 ctx->clone->locked_operation_in_progress = false; 6768 ctx->clone->md_ro = ctx->clone_md_ro; 6769 6770 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx); 6771 } 6772 6773 static void 6774 delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 6775 { 6776 struct delete_snapshot_ctx *ctx = cb_arg; 6777 6778 if (bserrno) { 6779 ctx->bserrno = bserrno; 6780 delete_snapshot_cleanup_clone(ctx, 0); 6781 return; 6782 } 6783 6784 ctx->clone->locked_operation_in_progress = false; 6785 spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx); 6786 } 6787 6788 static void 6789 delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno) 6790 { 6791 struct delete_snapshot_ctx *ctx = cb_arg; 6792 struct spdk_blob_list *parent_snapshot_entry = NULL; 6793 struct spdk_blob_list *snapshot_entry = NULL; 6794 struct spdk_blob_list *clone_entry = NULL; 6795 struct spdk_blob_list *snapshot_clone_entry = NULL; 6796 6797 if (bserrno) { 6798 SPDK_ERRLOG("Failed to sync MD on blob\n"); 6799 ctx->bserrno = bserrno; 6800 delete_snapshot_cleanup_clone(ctx, 0); 6801 return; 6802 } 6803 6804 /* Get snapshot entry for the snapshot we want to remove */ 6805 snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id); 6806 6807 assert(snapshot_entry != NULL); 6808 6809 /* Remove clone entry in this snapshot (at this point there can be only one clone) */ 6810 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 6811 assert(clone_entry != NULL); 6812 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 6813 snapshot_entry->clone_count--; 6814 assert(TAILQ_EMPTY(&snapshot_entry->clones)); 6815 6816 if (ctx->snapshot->parent_id != SPDK_BLOBID_INVALID) { 6817 /* This snapshot is at the same time a clone of another snapshot - we need to 6818 * update parent snapshot (remove current clone, add new one inherited from 6819 * the snapshot that is being removed) */ 6820 6821 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for 6822 * snapshot that we are removing */ 6823 blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry, 6824 &snapshot_clone_entry); 6825 6826 /* Switch clone entry in parent snapshot */ 6827 TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link); 6828 TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link); 6829 free(snapshot_clone_entry); 6830 } else { 6831 /* No parent snapshot - just remove clone entry */ 6832 free(clone_entry); 6833 } 6834 6835 /* Restore md_ro flags */ 6836 ctx->clone->md_ro = ctx->clone_md_ro; 6837 ctx->snapshot->md_ro = ctx->snapshot_md_ro; 6838 6839 blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx); 6840 } 6841 6842 static void 6843 delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno) 6844 { 6845 struct delete_snapshot_ctx *ctx = cb_arg; 6846 uint64_t i; 6847 6848 ctx->snapshot->md_ro = false; 6849 6850 if (bserrno) { 6851 SPDK_ERRLOG("Failed to sync MD on clone\n"); 6852 ctx->bserrno = bserrno; 6853 6854 /* Restore snapshot to previous state */ 6855 bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true); 6856 if (bserrno != 0) { 6857 delete_snapshot_cleanup_clone(ctx, bserrno); 6858 return; 6859 } 6860 6861 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx); 6862 return; 6863 } 6864 6865 /* Clear cluster map entries for snapshot */ 6866 for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) { 6867 if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) { 6868 ctx->snapshot->active.clusters[i] = 0; 6869 } 6870 } 6871 for (i = 0; i < ctx->snapshot->active.num_extent_pages && 6872 i < ctx->clone->active.num_extent_pages; i++) { 6873 if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) { 6874 ctx->snapshot->active.extent_pages[i] = 0; 6875 } 6876 } 6877 6878 blob_set_thin_provision(ctx->snapshot); 6879 ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY; 6880 6881 if (ctx->parent_snapshot_entry != NULL) { 6882 ctx->snapshot->back_bs_dev = NULL; 6883 } 6884 6885 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx); 6886 } 6887 6888 static void 6889 delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx) 6890 { 6891 /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */ 6892 ctx->clone->back_bs_dev->destroy(ctx->clone->back_bs_dev); 6893 6894 /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */ 6895 if (ctx->parent_snapshot_entry != NULL) { 6896 /* ...to parent snapshot */ 6897 ctx->clone->parent_id = ctx->parent_snapshot_entry->id; 6898 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev; 6899 blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id, 6900 sizeof(spdk_blob_id), 6901 true); 6902 } else { 6903 /* ...to blobid invalid and zeroes dev */ 6904 ctx->clone->parent_id = SPDK_BLOBID_INVALID; 6905 ctx->clone->back_bs_dev = bs_create_zeroes_dev(); 6906 blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true); 6907 } 6908 6909 spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx); 6910 } 6911 6912 static void 6913 delete_snapshot_update_extent_pages(void *cb_arg, int bserrno) 6914 { 6915 struct delete_snapshot_ctx *ctx = cb_arg; 6916 uint32_t *extent_page; 6917 uint64_t i; 6918 6919 for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages && 6920 i < ctx->clone->active.num_extent_pages; i++) { 6921 if (ctx->snapshot->active.extent_pages[i] == 0) { 6922 /* No extent page to use from snapshot */ 6923 continue; 6924 } 6925 6926 extent_page = &ctx->clone->active.extent_pages[i]; 6927 if (*extent_page == 0) { 6928 /* Copy extent page from snapshot when clone did not have a matching one */ 6929 *extent_page = ctx->snapshot->active.extent_pages[i]; 6930 continue; 6931 } 6932 6933 /* Clone and snapshot both contain partially filled matching extent pages. 6934 * Update the clone extent page in place with cluster map containing the mix of both. */ 6935 ctx->next_extent_page = i + 1; 6936 memset(ctx->page, 0, SPDK_BS_PAGE_SIZE); 6937 6938 blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, ctx->page, 6939 delete_snapshot_update_extent_pages, ctx); 6940 return; 6941 } 6942 delete_snapshot_update_extent_pages_cpl(ctx); 6943 } 6944 6945 static void 6946 delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno) 6947 { 6948 struct delete_snapshot_ctx *ctx = cb_arg; 6949 uint64_t i; 6950 6951 /* Temporarily override md_ro flag for clone for MD modification */ 6952 ctx->clone_md_ro = ctx->clone->md_ro; 6953 ctx->clone->md_ro = false; 6954 6955 if (bserrno) { 6956 SPDK_ERRLOG("Failed to sync MD with xattr on blob\n"); 6957 ctx->bserrno = bserrno; 6958 delete_snapshot_cleanup_clone(ctx, 0); 6959 return; 6960 } 6961 6962 /* Copy snapshot map to clone map (only unallocated clusters in clone) */ 6963 for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) { 6964 if (ctx->clone->active.clusters[i] == 0) { 6965 ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i]; 6966 } 6967 } 6968 ctx->next_extent_page = 0; 6969 delete_snapshot_update_extent_pages(ctx, 0); 6970 } 6971 6972 static void 6973 delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno) 6974 { 6975 struct delete_snapshot_ctx *ctx = cb_arg; 6976 6977 if (bserrno) { 6978 SPDK_ERRLOG("Failed to freeze I/O on clone\n"); 6979 ctx->bserrno = bserrno; 6980 delete_snapshot_cleanup_clone(ctx, 0); 6981 return; 6982 } 6983 6984 /* Temporarily override md_ro flag for snapshot for MD modification */ 6985 ctx->snapshot_md_ro = ctx->snapshot->md_ro; 6986 ctx->snapshot->md_ro = false; 6987 6988 /* Mark blob as pending for removal for power failure safety, use clone id for recovery */ 6989 ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id, 6990 sizeof(spdk_blob_id), true); 6991 if (ctx->bserrno != 0) { 6992 delete_snapshot_cleanup_clone(ctx, 0); 6993 return; 6994 } 6995 6996 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx); 6997 } 6998 6999 static void 7000 delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno) 7001 { 7002 struct delete_snapshot_ctx *ctx = cb_arg; 7003 7004 if (bserrno) { 7005 SPDK_ERRLOG("Failed to open clone\n"); 7006 ctx->bserrno = bserrno; 7007 delete_snapshot_cleanup_snapshot(ctx, 0); 7008 return; 7009 } 7010 7011 ctx->clone = clone; 7012 7013 if (clone->locked_operation_in_progress) { 7014 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n"); 7015 ctx->bserrno = -EBUSY; 7016 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx); 7017 return; 7018 } 7019 7020 clone->locked_operation_in_progress = true; 7021 7022 blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx); 7023 } 7024 7025 static void 7026 update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx) 7027 { 7028 struct spdk_blob_list *snapshot_entry = NULL; 7029 struct spdk_blob_list *clone_entry = NULL; 7030 struct spdk_blob_list *snapshot_clone_entry = NULL; 7031 7032 /* Get snapshot entry for the snapshot we want to remove */ 7033 snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id); 7034 7035 assert(snapshot_entry != NULL); 7036 7037 /* Get clone of the snapshot (at this point there can be only one clone) */ 7038 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 7039 assert(snapshot_entry->clone_count == 1); 7040 assert(clone_entry != NULL); 7041 7042 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for 7043 * snapshot that we are removing */ 7044 blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry, 7045 &snapshot_clone_entry); 7046 7047 spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx); 7048 } 7049 7050 static void 7051 bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno) 7052 { 7053 spdk_bs_sequence_t *seq = cb_arg; 7054 struct spdk_blob_list *snapshot_entry = NULL; 7055 uint32_t page_num; 7056 7057 if (bserrno) { 7058 SPDK_ERRLOG("Failed to remove blob\n"); 7059 bs_sequence_finish(seq, bserrno); 7060 return; 7061 } 7062 7063 /* Remove snapshot from the list */ 7064 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 7065 if (snapshot_entry != NULL) { 7066 TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link); 7067 free(snapshot_entry); 7068 } 7069 7070 page_num = bs_blobid_to_page(blob->id); 7071 spdk_bit_array_clear(blob->bs->used_blobids, page_num); 7072 blob->state = SPDK_BLOB_STATE_DIRTY; 7073 blob->active.num_pages = 0; 7074 blob_resize(blob, 0); 7075 7076 blob_persist(seq, blob, bs_delete_persist_cpl, blob); 7077 } 7078 7079 static int 7080 bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone) 7081 { 7082 struct spdk_blob_list *snapshot_entry = NULL; 7083 struct spdk_blob_list *clone_entry = NULL; 7084 struct spdk_blob *clone = NULL; 7085 bool has_one_clone = false; 7086 7087 /* Check if this is a snapshot with clones */ 7088 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 7089 if (snapshot_entry != NULL) { 7090 if (snapshot_entry->clone_count > 1) { 7091 SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n"); 7092 return -EBUSY; 7093 } else if (snapshot_entry->clone_count == 1) { 7094 has_one_clone = true; 7095 } 7096 } 7097 7098 /* Check if someone has this blob open (besides this delete context): 7099 * - open_ref = 1 - only this context opened blob, so it is ok to remove it 7100 * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot 7101 * and that is ok, because we will update it accordingly */ 7102 if (blob->open_ref <= 2 && has_one_clone) { 7103 clone_entry = TAILQ_FIRST(&snapshot_entry->clones); 7104 assert(clone_entry != NULL); 7105 clone = blob_lookup(blob->bs, clone_entry->id); 7106 7107 if (blob->open_ref == 2 && clone == NULL) { 7108 /* Clone is closed and someone else opened this blob */ 7109 SPDK_ERRLOG("Cannot remove snapshot because it is open\n"); 7110 return -EBUSY; 7111 } 7112 7113 *update_clone = true; 7114 return 0; 7115 } 7116 7117 if (blob->open_ref > 1) { 7118 SPDK_ERRLOG("Cannot remove snapshot because it is open\n"); 7119 return -EBUSY; 7120 } 7121 7122 assert(has_one_clone == false); 7123 *update_clone = false; 7124 return 0; 7125 } 7126 7127 static void 7128 bs_delete_enomem_close_cpl(void *cb_arg, int bserrno) 7129 { 7130 spdk_bs_sequence_t *seq = cb_arg; 7131 7132 bs_sequence_finish(seq, -ENOMEM); 7133 } 7134 7135 static void 7136 bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno) 7137 { 7138 spdk_bs_sequence_t *seq = cb_arg; 7139 struct delete_snapshot_ctx *ctx; 7140 bool update_clone = false; 7141 7142 if (bserrno != 0) { 7143 bs_sequence_finish(seq, bserrno); 7144 return; 7145 } 7146 7147 blob_verify_md_op(blob); 7148 7149 ctx = calloc(1, sizeof(*ctx)); 7150 if (ctx == NULL) { 7151 spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq); 7152 return; 7153 } 7154 7155 ctx->snapshot = blob; 7156 ctx->cb_fn = bs_delete_blob_finish; 7157 ctx->cb_arg = seq; 7158 7159 /* Check if blob can be removed and if it is a snapshot with clone on top of it */ 7160 ctx->bserrno = bs_is_blob_deletable(blob, &update_clone); 7161 if (ctx->bserrno) { 7162 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 7163 return; 7164 } 7165 7166 if (blob->locked_operation_in_progress) { 7167 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n"); 7168 ctx->bserrno = -EBUSY; 7169 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 7170 return; 7171 } 7172 7173 blob->locked_operation_in_progress = true; 7174 7175 /* 7176 * Remove the blob from the blob_store list now, to ensure it does not 7177 * get returned after this point by blob_lookup(). 7178 */ 7179 spdk_bit_array_clear(blob->bs->open_blobids, blob->id); 7180 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob); 7181 7182 if (update_clone) { 7183 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA); 7184 if (!ctx->page) { 7185 ctx->bserrno = -ENOMEM; 7186 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx); 7187 return; 7188 } 7189 /* This blob is a snapshot with active clone - update clone first */ 7190 update_clone_on_snapshot_deletion(blob, ctx); 7191 } else { 7192 /* This blob does not have any clones - just remove it */ 7193 bs_blob_list_remove(blob); 7194 bs_delete_blob_finish(seq, blob, 0); 7195 free(ctx); 7196 } 7197 } 7198 7199 void 7200 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 7201 spdk_blob_op_complete cb_fn, void *cb_arg) 7202 { 7203 struct spdk_bs_cpl cpl; 7204 spdk_bs_sequence_t *seq; 7205 7206 SPDK_DEBUGLOG(blob, "Deleting blob %" PRIu64 "\n", blobid); 7207 7208 assert(spdk_get_thread() == bs->md_thread); 7209 7210 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7211 cpl.u.blob_basic.cb_fn = cb_fn; 7212 cpl.u.blob_basic.cb_arg = cb_arg; 7213 7214 seq = bs_sequence_start(bs->md_channel, &cpl); 7215 if (!seq) { 7216 cb_fn(cb_arg, -ENOMEM); 7217 return; 7218 } 7219 7220 spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq); 7221 } 7222 7223 /* END spdk_bs_delete_blob */ 7224 7225 /* START spdk_bs_open_blob */ 7226 7227 static void 7228 bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7229 { 7230 struct spdk_blob *blob = cb_arg; 7231 struct spdk_blob *existing; 7232 7233 if (bserrno != 0) { 7234 blob_free(blob); 7235 seq->cpl.u.blob_handle.blob = NULL; 7236 bs_sequence_finish(seq, bserrno); 7237 return; 7238 } 7239 7240 existing = blob_lookup(blob->bs, blob->id); 7241 if (existing) { 7242 blob_free(blob); 7243 existing->open_ref++; 7244 seq->cpl.u.blob_handle.blob = existing; 7245 bs_sequence_finish(seq, 0); 7246 return; 7247 } 7248 7249 blob->open_ref++; 7250 7251 spdk_bit_array_set(blob->bs->open_blobids, blob->id); 7252 RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob); 7253 7254 bs_sequence_finish(seq, bserrno); 7255 } 7256 7257 static inline void 7258 blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst) 7259 { 7260 #define FIELD_OK(field) \ 7261 offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size 7262 7263 #define SET_FIELD(field) \ 7264 if (FIELD_OK(field)) { \ 7265 dst->field = src->field; \ 7266 } \ 7267 7268 SET_FIELD(clear_method); 7269 7270 dst->opts_size = src->opts_size; 7271 7272 /* You should not remove this statement, but need to update the assert statement 7273 * if you add a new field, and also add a corresponding SET_FIELD statement */ 7274 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 16, "Incorrect size"); 7275 7276 #undef FIELD_OK 7277 #undef SET_FIELD 7278 } 7279 7280 static void 7281 bs_open_blob(struct spdk_blob_store *bs, 7282 spdk_blob_id blobid, 7283 struct spdk_blob_open_opts *opts, 7284 spdk_blob_op_with_handle_complete cb_fn, 7285 void *cb_arg) 7286 { 7287 struct spdk_blob *blob; 7288 struct spdk_bs_cpl cpl; 7289 struct spdk_blob_open_opts opts_local; 7290 spdk_bs_sequence_t *seq; 7291 uint32_t page_num; 7292 7293 SPDK_DEBUGLOG(blob, "Opening blob %" PRIu64 "\n", blobid); 7294 assert(spdk_get_thread() == bs->md_thread); 7295 7296 page_num = bs_blobid_to_page(blobid); 7297 if (spdk_bit_array_get(bs->used_blobids, page_num) == false) { 7298 /* Invalid blobid */ 7299 cb_fn(cb_arg, NULL, -ENOENT); 7300 return; 7301 } 7302 7303 blob = blob_lookup(bs, blobid); 7304 if (blob) { 7305 blob->open_ref++; 7306 cb_fn(cb_arg, blob, 0); 7307 return; 7308 } 7309 7310 blob = blob_alloc(bs, blobid); 7311 if (!blob) { 7312 cb_fn(cb_arg, NULL, -ENOMEM); 7313 return; 7314 } 7315 7316 spdk_blob_open_opts_init(&opts_local, sizeof(opts_local)); 7317 if (opts) { 7318 blob_open_opts_copy(opts, &opts_local); 7319 } 7320 7321 blob->clear_method = opts_local.clear_method; 7322 7323 cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE; 7324 cpl.u.blob_handle.cb_fn = cb_fn; 7325 cpl.u.blob_handle.cb_arg = cb_arg; 7326 cpl.u.blob_handle.blob = blob; 7327 7328 seq = bs_sequence_start(bs->md_channel, &cpl); 7329 if (!seq) { 7330 blob_free(blob); 7331 cb_fn(cb_arg, NULL, -ENOMEM); 7332 return; 7333 } 7334 7335 blob_load(seq, blob, bs_open_blob_cpl, blob); 7336 } 7337 7338 void 7339 spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 7340 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7341 { 7342 bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg); 7343 } 7344 7345 void 7346 spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid, 7347 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7348 { 7349 bs_open_blob(bs, blobid, opts, cb_fn, cb_arg); 7350 } 7351 7352 /* END spdk_bs_open_blob */ 7353 7354 /* START spdk_blob_set_read_only */ 7355 int 7356 spdk_blob_set_read_only(struct spdk_blob *blob) 7357 { 7358 blob_verify_md_op(blob); 7359 7360 blob->data_ro_flags |= SPDK_BLOB_READ_ONLY; 7361 7362 blob->state = SPDK_BLOB_STATE_DIRTY; 7363 return 0; 7364 } 7365 /* END spdk_blob_set_read_only */ 7366 7367 /* START spdk_blob_sync_md */ 7368 7369 static void 7370 blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7371 { 7372 struct spdk_blob *blob = cb_arg; 7373 7374 if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 7375 blob->data_ro = true; 7376 blob->md_ro = true; 7377 } 7378 7379 bs_sequence_finish(seq, bserrno); 7380 } 7381 7382 static void 7383 blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7384 { 7385 struct spdk_bs_cpl cpl; 7386 spdk_bs_sequence_t *seq; 7387 7388 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7389 cpl.u.blob_basic.cb_fn = cb_fn; 7390 cpl.u.blob_basic.cb_arg = cb_arg; 7391 7392 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7393 if (!seq) { 7394 cb_fn(cb_arg, -ENOMEM); 7395 return; 7396 } 7397 7398 blob_persist(seq, blob, blob_sync_md_cpl, blob); 7399 } 7400 7401 void 7402 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7403 { 7404 blob_verify_md_op(blob); 7405 7406 SPDK_DEBUGLOG(blob, "Syncing blob %" PRIu64 "\n", blob->id); 7407 7408 if (blob->md_ro) { 7409 assert(blob->state == SPDK_BLOB_STATE_CLEAN); 7410 cb_fn(cb_arg, 0); 7411 return; 7412 } 7413 7414 blob_sync_md(blob, cb_fn, cb_arg); 7415 } 7416 7417 /* END spdk_blob_sync_md */ 7418 7419 struct spdk_blob_insert_cluster_ctx { 7420 struct spdk_thread *thread; 7421 struct spdk_blob *blob; 7422 uint32_t cluster_num; /* cluster index in blob */ 7423 uint32_t cluster; /* cluster on disk */ 7424 uint32_t extent_page; /* extent page on disk */ 7425 struct spdk_blob_md_page *page; /* preallocated extent page */ 7426 int rc; 7427 spdk_blob_op_complete cb_fn; 7428 void *cb_arg; 7429 }; 7430 7431 static void 7432 blob_insert_cluster_msg_cpl(void *arg) 7433 { 7434 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7435 7436 ctx->cb_fn(ctx->cb_arg, ctx->rc); 7437 free(ctx); 7438 } 7439 7440 static void 7441 blob_insert_cluster_msg_cb(void *arg, int bserrno) 7442 { 7443 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7444 7445 ctx->rc = bserrno; 7446 spdk_thread_send_msg(ctx->thread, blob_insert_cluster_msg_cpl, ctx); 7447 } 7448 7449 static void 7450 blob_insert_new_ep_cb(void *arg, int bserrno) 7451 { 7452 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7453 uint32_t *extent_page; 7454 7455 extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num); 7456 *extent_page = ctx->extent_page; 7457 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 7458 blob_sync_md(ctx->blob, blob_insert_cluster_msg_cb, ctx); 7459 } 7460 7461 static void 7462 blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7463 { 7464 bs_sequence_finish(seq, bserrno); 7465 } 7466 7467 static void 7468 blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num, 7469 struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg) 7470 { 7471 spdk_bs_sequence_t *seq; 7472 struct spdk_bs_cpl cpl; 7473 7474 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7475 cpl.u.blob_basic.cb_fn = cb_fn; 7476 cpl.u.blob_basic.cb_arg = cb_arg; 7477 7478 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7479 if (!seq) { 7480 cb_fn(cb_arg, -ENOMEM); 7481 return; 7482 } 7483 7484 assert(page); 7485 page->next = SPDK_INVALID_MD_PAGE; 7486 page->id = blob->id; 7487 page->sequence_num = 0; 7488 7489 blob_serialize_extent_page(blob, cluster_num, page); 7490 7491 page->crc = blob_md_page_calc_crc(page); 7492 7493 assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true); 7494 7495 bs_sequence_write_dev(seq, page, bs_md_page_to_lba(blob->bs, extent), 7496 bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE), 7497 blob_persist_extent_page_cpl, page); 7498 } 7499 7500 static void 7501 blob_insert_cluster_msg(void *arg) 7502 { 7503 struct spdk_blob_insert_cluster_ctx *ctx = arg; 7504 uint32_t *extent_page; 7505 7506 ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster); 7507 if (ctx->rc != 0) { 7508 spdk_thread_send_msg(ctx->thread, blob_insert_cluster_msg_cpl, ctx); 7509 return; 7510 } 7511 7512 if (ctx->blob->use_extent_table == false) { 7513 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */ 7514 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 7515 blob_sync_md(ctx->blob, blob_insert_cluster_msg_cb, ctx); 7516 return; 7517 } 7518 7519 extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num); 7520 if (*extent_page == 0) { 7521 /* Extent page requires allocation. 7522 * It was already claimed in the used_md_pages map and placed in ctx. */ 7523 assert(ctx->extent_page != 0); 7524 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true); 7525 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page, 7526 blob_insert_new_ep_cb, ctx); 7527 } else { 7528 /* It is possible for original thread to allocate extent page for 7529 * different cluster in the same extent page. In such case proceed with 7530 * updating the existing extent page, but release the additional one. */ 7531 if (ctx->extent_page != 0) { 7532 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true); 7533 bs_release_md_page(ctx->blob->bs, ctx->extent_page); 7534 ctx->extent_page = 0; 7535 } 7536 /* Extent page already allocated. 7537 * Every cluster allocation, requires just an update of single extent page. */ 7538 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page, 7539 blob_insert_cluster_msg_cb, ctx); 7540 } 7541 } 7542 7543 static void 7544 blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 7545 uint64_t cluster, uint32_t extent_page, struct spdk_blob_md_page *page, 7546 spdk_blob_op_complete cb_fn, void *cb_arg) 7547 { 7548 struct spdk_blob_insert_cluster_ctx *ctx; 7549 7550 ctx = calloc(1, sizeof(*ctx)); 7551 if (ctx == NULL) { 7552 cb_fn(cb_arg, -ENOMEM); 7553 return; 7554 } 7555 7556 ctx->thread = spdk_get_thread(); 7557 ctx->blob = blob; 7558 ctx->cluster_num = cluster_num; 7559 ctx->cluster = cluster; 7560 ctx->extent_page = extent_page; 7561 ctx->page = page; 7562 ctx->cb_fn = cb_fn; 7563 ctx->cb_arg = cb_arg; 7564 7565 spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx); 7566 } 7567 7568 /* START spdk_blob_close */ 7569 7570 static void 7571 blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 7572 { 7573 struct spdk_blob *blob = cb_arg; 7574 7575 if (bserrno == 0) { 7576 blob->open_ref--; 7577 if (blob->open_ref == 0) { 7578 /* 7579 * Blobs with active.num_pages == 0 are deleted blobs. 7580 * these blobs are removed from the blob_store list 7581 * when the deletion process starts - so don't try to 7582 * remove them again. 7583 */ 7584 if (blob->active.num_pages > 0) { 7585 spdk_bit_array_clear(blob->bs->open_blobids, blob->id); 7586 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob); 7587 } 7588 blob_free(blob); 7589 } 7590 } 7591 7592 bs_sequence_finish(seq, bserrno); 7593 } 7594 7595 void 7596 spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 7597 { 7598 struct spdk_bs_cpl cpl; 7599 spdk_bs_sequence_t *seq; 7600 7601 blob_verify_md_op(blob); 7602 7603 SPDK_DEBUGLOG(blob, "Closing blob %" PRIu64 "\n", blob->id); 7604 7605 if (blob->open_ref == 0) { 7606 cb_fn(cb_arg, -EBADF); 7607 return; 7608 } 7609 7610 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 7611 cpl.u.blob_basic.cb_fn = cb_fn; 7612 cpl.u.blob_basic.cb_arg = cb_arg; 7613 7614 seq = bs_sequence_start(blob->bs->md_channel, &cpl); 7615 if (!seq) { 7616 cb_fn(cb_arg, -ENOMEM); 7617 return; 7618 } 7619 7620 /* Sync metadata */ 7621 blob_persist(seq, blob, blob_close_cpl, blob); 7622 } 7623 7624 /* END spdk_blob_close */ 7625 7626 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 7627 { 7628 return spdk_get_io_channel(bs); 7629 } 7630 7631 void 7632 spdk_bs_free_io_channel(struct spdk_io_channel *channel) 7633 { 7634 spdk_put_io_channel(channel); 7635 } 7636 7637 void 7638 spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel, 7639 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 7640 { 7641 blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 7642 SPDK_BLOB_UNMAP); 7643 } 7644 7645 void 7646 spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel, 7647 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 7648 { 7649 blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 7650 SPDK_BLOB_WRITE_ZEROES); 7651 } 7652 7653 void 7654 spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel, 7655 void *payload, uint64_t offset, uint64_t length, 7656 spdk_blob_op_complete cb_fn, void *cb_arg) 7657 { 7658 blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 7659 SPDK_BLOB_WRITE); 7660 } 7661 7662 void 7663 spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel, 7664 void *payload, uint64_t offset, uint64_t length, 7665 spdk_blob_op_complete cb_fn, void *cb_arg) 7666 { 7667 blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 7668 SPDK_BLOB_READ); 7669 } 7670 7671 void 7672 spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel, 7673 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7674 spdk_blob_op_complete cb_fn, void *cb_arg) 7675 { 7676 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false, NULL); 7677 } 7678 7679 void 7680 spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel, 7681 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7682 spdk_blob_op_complete cb_fn, void *cb_arg) 7683 { 7684 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true, NULL); 7685 } 7686 7687 void 7688 spdk_blob_io_writev_ext(struct spdk_blob *blob, struct spdk_io_channel *channel, 7689 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7690 spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts) 7691 { 7692 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false, 7693 io_opts); 7694 } 7695 7696 void 7697 spdk_blob_io_readv_ext(struct spdk_blob *blob, struct spdk_io_channel *channel, 7698 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 7699 spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts) 7700 { 7701 blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true, 7702 io_opts); 7703 } 7704 7705 struct spdk_bs_iter_ctx { 7706 int64_t page_num; 7707 struct spdk_blob_store *bs; 7708 7709 spdk_blob_op_with_handle_complete cb_fn; 7710 void *cb_arg; 7711 }; 7712 7713 static void 7714 bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 7715 { 7716 struct spdk_bs_iter_ctx *ctx = cb_arg; 7717 struct spdk_blob_store *bs = ctx->bs; 7718 spdk_blob_id id; 7719 7720 if (bserrno == 0) { 7721 ctx->cb_fn(ctx->cb_arg, _blob, bserrno); 7722 free(ctx); 7723 return; 7724 } 7725 7726 ctx->page_num++; 7727 ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num); 7728 if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) { 7729 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT); 7730 free(ctx); 7731 return; 7732 } 7733 7734 id = bs_page_to_blobid(ctx->page_num); 7735 7736 spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx); 7737 } 7738 7739 void 7740 spdk_bs_iter_first(struct spdk_blob_store *bs, 7741 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7742 { 7743 struct spdk_bs_iter_ctx *ctx; 7744 7745 ctx = calloc(1, sizeof(*ctx)); 7746 if (!ctx) { 7747 cb_fn(cb_arg, NULL, -ENOMEM); 7748 return; 7749 } 7750 7751 ctx->page_num = -1; 7752 ctx->bs = bs; 7753 ctx->cb_fn = cb_fn; 7754 ctx->cb_arg = cb_arg; 7755 7756 bs_iter_cpl(ctx, NULL, -1); 7757 } 7758 7759 static void 7760 bs_iter_close_cpl(void *cb_arg, int bserrno) 7761 { 7762 struct spdk_bs_iter_ctx *ctx = cb_arg; 7763 7764 bs_iter_cpl(ctx, NULL, -1); 7765 } 7766 7767 void 7768 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob, 7769 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 7770 { 7771 struct spdk_bs_iter_ctx *ctx; 7772 7773 assert(blob != NULL); 7774 7775 ctx = calloc(1, sizeof(*ctx)); 7776 if (!ctx) { 7777 cb_fn(cb_arg, NULL, -ENOMEM); 7778 return; 7779 } 7780 7781 ctx->page_num = bs_blobid_to_page(blob->id); 7782 ctx->bs = bs; 7783 ctx->cb_fn = cb_fn; 7784 ctx->cb_arg = cb_arg; 7785 7786 /* Close the existing blob */ 7787 spdk_blob_close(blob, bs_iter_close_cpl, ctx); 7788 } 7789 7790 static int 7791 blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 7792 uint16_t value_len, bool internal) 7793 { 7794 struct spdk_xattr_tailq *xattrs; 7795 struct spdk_xattr *xattr; 7796 size_t desc_size; 7797 void *tmp; 7798 7799 blob_verify_md_op(blob); 7800 7801 if (blob->md_ro) { 7802 return -EPERM; 7803 } 7804 7805 desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len; 7806 if (desc_size > SPDK_BS_MAX_DESC_SIZE) { 7807 SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name, 7808 desc_size, SPDK_BS_MAX_DESC_SIZE); 7809 return -ENOMEM; 7810 } 7811 7812 if (internal) { 7813 xattrs = &blob->xattrs_internal; 7814 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR; 7815 } else { 7816 xattrs = &blob->xattrs; 7817 } 7818 7819 TAILQ_FOREACH(xattr, xattrs, link) { 7820 if (!strcmp(name, xattr->name)) { 7821 tmp = malloc(value_len); 7822 if (!tmp) { 7823 return -ENOMEM; 7824 } 7825 7826 free(xattr->value); 7827 xattr->value_len = value_len; 7828 xattr->value = tmp; 7829 memcpy(xattr->value, value, value_len); 7830 7831 blob->state = SPDK_BLOB_STATE_DIRTY; 7832 7833 return 0; 7834 } 7835 } 7836 7837 xattr = calloc(1, sizeof(*xattr)); 7838 if (!xattr) { 7839 return -ENOMEM; 7840 } 7841 7842 xattr->name = strdup(name); 7843 if (!xattr->name) { 7844 free(xattr); 7845 return -ENOMEM; 7846 } 7847 7848 xattr->value_len = value_len; 7849 xattr->value = malloc(value_len); 7850 if (!xattr->value) { 7851 free(xattr->name); 7852 free(xattr); 7853 return -ENOMEM; 7854 } 7855 memcpy(xattr->value, value, value_len); 7856 TAILQ_INSERT_TAIL(xattrs, xattr, link); 7857 7858 blob->state = SPDK_BLOB_STATE_DIRTY; 7859 7860 return 0; 7861 } 7862 7863 int 7864 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 7865 uint16_t value_len) 7866 { 7867 return blob_set_xattr(blob, name, value, value_len, false); 7868 } 7869 7870 static int 7871 blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal) 7872 { 7873 struct spdk_xattr_tailq *xattrs; 7874 struct spdk_xattr *xattr; 7875 7876 blob_verify_md_op(blob); 7877 7878 if (blob->md_ro) { 7879 return -EPERM; 7880 } 7881 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 7882 7883 TAILQ_FOREACH(xattr, xattrs, link) { 7884 if (!strcmp(name, xattr->name)) { 7885 TAILQ_REMOVE(xattrs, xattr, link); 7886 free(xattr->value); 7887 free(xattr->name); 7888 free(xattr); 7889 7890 if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) { 7891 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR; 7892 } 7893 blob->state = SPDK_BLOB_STATE_DIRTY; 7894 7895 return 0; 7896 } 7897 } 7898 7899 return -ENOENT; 7900 } 7901 7902 int 7903 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name) 7904 { 7905 return blob_remove_xattr(blob, name, false); 7906 } 7907 7908 static int 7909 blob_get_xattr_value(struct spdk_blob *blob, const char *name, 7910 const void **value, size_t *value_len, bool internal) 7911 { 7912 struct spdk_xattr *xattr; 7913 struct spdk_xattr_tailq *xattrs; 7914 7915 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 7916 7917 TAILQ_FOREACH(xattr, xattrs, link) { 7918 if (!strcmp(name, xattr->name)) { 7919 *value = xattr->value; 7920 *value_len = xattr->value_len; 7921 return 0; 7922 } 7923 } 7924 return -ENOENT; 7925 } 7926 7927 int 7928 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 7929 const void **value, size_t *value_len) 7930 { 7931 blob_verify_md_op(blob); 7932 7933 return blob_get_xattr_value(blob, name, value, value_len, false); 7934 } 7935 7936 struct spdk_xattr_names { 7937 uint32_t count; 7938 const char *names[0]; 7939 }; 7940 7941 static int 7942 blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names) 7943 { 7944 struct spdk_xattr *xattr; 7945 int count = 0; 7946 7947 TAILQ_FOREACH(xattr, xattrs, link) { 7948 count++; 7949 } 7950 7951 *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *)); 7952 if (*names == NULL) { 7953 return -ENOMEM; 7954 } 7955 7956 TAILQ_FOREACH(xattr, xattrs, link) { 7957 (*names)->names[(*names)->count++] = xattr->name; 7958 } 7959 7960 return 0; 7961 } 7962 7963 int 7964 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names) 7965 { 7966 blob_verify_md_op(blob); 7967 7968 return blob_get_xattr_names(&blob->xattrs, names); 7969 } 7970 7971 uint32_t 7972 spdk_xattr_names_get_count(struct spdk_xattr_names *names) 7973 { 7974 assert(names != NULL); 7975 7976 return names->count; 7977 } 7978 7979 const char * 7980 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index) 7981 { 7982 if (index >= names->count) { 7983 return NULL; 7984 } 7985 7986 return names->names[index]; 7987 } 7988 7989 void 7990 spdk_xattr_names_free(struct spdk_xattr_names *names) 7991 { 7992 free(names); 7993 } 7994 7995 struct spdk_bs_type 7996 spdk_bs_get_bstype(struct spdk_blob_store *bs) 7997 { 7998 return bs->bstype; 7999 } 8000 8001 void 8002 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype) 8003 { 8004 memcpy(&bs->bstype, &bstype, sizeof(bstype)); 8005 } 8006 8007 bool 8008 spdk_blob_is_read_only(struct spdk_blob *blob) 8009 { 8010 assert(blob != NULL); 8011 return (blob->data_ro || blob->md_ro); 8012 } 8013 8014 bool 8015 spdk_blob_is_snapshot(struct spdk_blob *blob) 8016 { 8017 struct spdk_blob_list *snapshot_entry; 8018 8019 assert(blob != NULL); 8020 8021 snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id); 8022 if (snapshot_entry == NULL) { 8023 return false; 8024 } 8025 8026 return true; 8027 } 8028 8029 bool 8030 spdk_blob_is_clone(struct spdk_blob *blob) 8031 { 8032 assert(blob != NULL); 8033 8034 if (blob->parent_id != SPDK_BLOBID_INVALID) { 8035 assert(spdk_blob_is_thin_provisioned(blob)); 8036 return true; 8037 } 8038 8039 return false; 8040 } 8041 8042 bool 8043 spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 8044 { 8045 assert(blob != NULL); 8046 return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 8047 } 8048 8049 static void 8050 blob_update_clear_method(struct spdk_blob *blob) 8051 { 8052 enum blob_clear_method stored_cm; 8053 8054 assert(blob != NULL); 8055 8056 /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored 8057 * in metadata previously. If something other than the default was 8058 * specified, ignore stored value and used what was passed in. 8059 */ 8060 stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT); 8061 8062 if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) { 8063 blob->clear_method = stored_cm; 8064 } else if (blob->clear_method != stored_cm) { 8065 SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n", 8066 blob->clear_method, stored_cm); 8067 } 8068 } 8069 8070 spdk_blob_id 8071 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id) 8072 { 8073 struct spdk_blob_list *snapshot_entry = NULL; 8074 struct spdk_blob_list *clone_entry = NULL; 8075 8076 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 8077 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 8078 if (clone_entry->id == blob_id) { 8079 return snapshot_entry->id; 8080 } 8081 } 8082 } 8083 8084 return SPDK_BLOBID_INVALID; 8085 } 8086 8087 int 8088 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids, 8089 size_t *count) 8090 { 8091 struct spdk_blob_list *snapshot_entry, *clone_entry; 8092 size_t n; 8093 8094 snapshot_entry = bs_get_snapshot_entry(bs, blobid); 8095 if (snapshot_entry == NULL) { 8096 *count = 0; 8097 return 0; 8098 } 8099 8100 if (ids == NULL || *count < snapshot_entry->clone_count) { 8101 *count = snapshot_entry->clone_count; 8102 return -ENOMEM; 8103 } 8104 *count = snapshot_entry->clone_count; 8105 8106 n = 0; 8107 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 8108 ids[n++] = clone_entry->id; 8109 } 8110 8111 return 0; 8112 } 8113 8114 static void 8115 bs_load_grow_continue(struct spdk_bs_load_ctx *ctx) 8116 { 8117 int rc; 8118 8119 if (ctx->super->size == 0) { 8120 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 8121 } 8122 8123 if (ctx->super->io_unit_size == 0) { 8124 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE; 8125 } 8126 8127 /* Parse the super block */ 8128 ctx->bs->clean = 1; 8129 ctx->bs->cluster_sz = ctx->super->cluster_size; 8130 ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size; 8131 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 8132 if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) { 8133 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster); 8134 } 8135 ctx->bs->io_unit_size = ctx->super->io_unit_size; 8136 rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters); 8137 if (rc < 0) { 8138 bs_load_ctx_fail(ctx, -ENOMEM); 8139 return; 8140 } 8141 ctx->bs->md_start = ctx->super->md_start; 8142 ctx->bs->md_len = ctx->super->md_len; 8143 rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len); 8144 if (rc < 0) { 8145 bs_load_ctx_fail(ctx, -ENOMEM); 8146 return; 8147 } 8148 8149 ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up( 8150 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 8151 ctx->bs->super_blob = ctx->super->super_blob; 8152 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 8153 8154 if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) { 8155 SPDK_ERRLOG("Can not grow an unclean blobstore, please load it normally to clean it.\n"); 8156 bs_load_ctx_fail(ctx, -EIO); 8157 return; 8158 } else { 8159 bs_load_read_used_pages(ctx); 8160 } 8161 } 8162 8163 static void 8164 bs_load_grow_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 8165 { 8166 struct spdk_bs_load_ctx *ctx = cb_arg; 8167 8168 if (bserrno != 0) { 8169 bs_load_ctx_fail(ctx, bserrno); 8170 return; 8171 } 8172 bs_load_grow_continue(ctx); 8173 } 8174 8175 static void 8176 bs_load_grow_used_clusters_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 8177 { 8178 struct spdk_bs_load_ctx *ctx = cb_arg; 8179 8180 if (bserrno != 0) { 8181 bs_load_ctx_fail(ctx, bserrno); 8182 return; 8183 } 8184 8185 spdk_free(ctx->mask); 8186 8187 bs_sequence_write_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->bs, 0), 8188 bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 8189 bs_load_grow_super_write_cpl, ctx); 8190 } 8191 8192 static void 8193 bs_load_grow_used_clusters_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 8194 { 8195 struct spdk_bs_load_ctx *ctx = cb_arg; 8196 uint64_t lba, lba_count; 8197 uint64_t dev_size; 8198 uint64_t total_clusters; 8199 8200 if (bserrno != 0) { 8201 bs_load_ctx_fail(ctx, bserrno); 8202 return; 8203 } 8204 8205 /* The type must be correct */ 8206 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 8207 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 8208 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 8209 struct spdk_blob_md_page) * 8)); 8210 dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 8211 total_clusters = dev_size / ctx->super->cluster_size; 8212 ctx->mask->length = total_clusters; 8213 8214 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 8215 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 8216 bs_sequence_write_dev(ctx->seq, ctx->mask, lba, lba_count, 8217 bs_load_grow_used_clusters_write_cpl, ctx); 8218 } 8219 8220 static void 8221 bs_load_try_to_grow(struct spdk_bs_load_ctx *ctx) 8222 { 8223 uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask; 8224 uint64_t lba, lba_count, mask_size; 8225 8226 dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 8227 total_clusters = dev_size / ctx->super->cluster_size; 8228 used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 8229 spdk_divide_round_up(total_clusters, 8), 8230 SPDK_BS_PAGE_SIZE); 8231 max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start; 8232 /* No necessary to grow or no space to grow */ 8233 if (ctx->super->size >= dev_size || used_cluster_mask_len > max_used_cluster_mask) { 8234 SPDK_DEBUGLOG(blob, "No grow\n"); 8235 bs_load_grow_continue(ctx); 8236 return; 8237 } 8238 8239 SPDK_DEBUGLOG(blob, "Resize blobstore\n"); 8240 8241 ctx->super->size = dev_size; 8242 ctx->super->used_cluster_mask_len = used_cluster_mask_len; 8243 ctx->super->crc = blob_md_page_calc_crc(ctx->super); 8244 8245 mask_size = used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 8246 ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY, 8247 SPDK_MALLOC_DMA); 8248 if (!ctx->mask) { 8249 bs_load_ctx_fail(ctx, -ENOMEM); 8250 return; 8251 } 8252 lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 8253 lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 8254 bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count, 8255 bs_load_grow_used_clusters_read_cpl, ctx); 8256 } 8257 8258 static void 8259 bs_grow_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 8260 { 8261 struct spdk_bs_load_ctx *ctx = cb_arg; 8262 uint32_t crc; 8263 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 8264 8265 if (ctx->super->version > SPDK_BS_VERSION || 8266 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 8267 bs_load_ctx_fail(ctx, -EILSEQ); 8268 return; 8269 } 8270 8271 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 8272 sizeof(ctx->super->signature)) != 0) { 8273 bs_load_ctx_fail(ctx, -EILSEQ); 8274 return; 8275 } 8276 8277 crc = blob_md_page_calc_crc(ctx->super); 8278 if (crc != ctx->super->crc) { 8279 bs_load_ctx_fail(ctx, -EILSEQ); 8280 return; 8281 } 8282 8283 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 8284 SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n"); 8285 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 8286 SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n"); 8287 } else { 8288 SPDK_DEBUGLOG(blob, "Unexpected bstype\n"); 8289 SPDK_LOGDUMP(blob, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 8290 SPDK_LOGDUMP(blob, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 8291 bs_load_ctx_fail(ctx, -ENXIO); 8292 return; 8293 } 8294 8295 if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) { 8296 SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n", 8297 ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size); 8298 bs_load_ctx_fail(ctx, -EILSEQ); 8299 return; 8300 } 8301 8302 bs_load_try_to_grow(ctx); 8303 8304 } 8305 8306 void 8307 spdk_bs_grow(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 8308 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 8309 { 8310 struct spdk_blob_store *bs; 8311 struct spdk_bs_cpl cpl; 8312 struct spdk_bs_load_ctx *ctx; 8313 struct spdk_bs_opts opts = {}; 8314 int err; 8315 8316 SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev); 8317 8318 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 8319 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen); 8320 dev->destroy(dev); 8321 cb_fn(cb_arg, NULL, -EINVAL); 8322 return; 8323 } 8324 8325 spdk_bs_opts_init(&opts, sizeof(opts)); 8326 if (o) { 8327 if (bs_opts_copy(o, &opts)) { 8328 return; 8329 } 8330 } 8331 8332 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 8333 dev->destroy(dev); 8334 cb_fn(cb_arg, NULL, -EINVAL); 8335 return; 8336 } 8337 8338 err = bs_alloc(dev, &opts, &bs, &ctx); 8339 if (err) { 8340 dev->destroy(dev); 8341 cb_fn(cb_arg, NULL, err); 8342 return; 8343 } 8344 8345 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 8346 cpl.u.bs_handle.cb_fn = cb_fn; 8347 cpl.u.bs_handle.cb_arg = cb_arg; 8348 cpl.u.bs_handle.bs = bs; 8349 8350 ctx->seq = bs_sequence_start(bs->md_channel, &cpl); 8351 if (!ctx->seq) { 8352 spdk_free(ctx->super); 8353 free(ctx); 8354 bs_free(bs); 8355 cb_fn(cb_arg, NULL, -ENOMEM); 8356 return; 8357 } 8358 8359 /* Read the super block */ 8360 bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0), 8361 bs_byte_to_lba(bs, sizeof(*ctx->super)), 8362 bs_grow_load_super_cpl, ctx); 8363 } 8364 8365 SPDK_LOG_REGISTER_COMPONENT(blob) 8366