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