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