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/likely.h" 43 #include "spdk/util.h" 44 45 #include "spdk_internal/assert.h" 46 #include "spdk_internal/log.h" 47 48 #include "blobstore.h" 49 50 #define BLOB_CRC32C_INITIAL 0xffffffffUL 51 52 static int spdk_bs_register_md_thread(struct spdk_blob_store *bs); 53 static int spdk_bs_unregister_md_thread(struct spdk_blob_store *bs); 54 static void _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno); 55 static void _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 56 uint64_t cluster, spdk_blob_op_complete cb_fn, void *cb_arg); 57 58 static int _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 59 uint16_t value_len, bool internal); 60 static int _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 61 const void **value, size_t *value_len, bool internal); 62 static int _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal); 63 64 static void 65 _spdk_blob_verify_md_op(struct spdk_blob *blob) 66 { 67 assert(blob != NULL); 68 assert(spdk_get_thread() == blob->bs->md_thread); 69 assert(blob->state != SPDK_BLOB_STATE_LOADING); 70 } 71 72 static void 73 _spdk_bs_claim_cluster(struct spdk_blob_store *bs, uint32_t cluster_num) 74 { 75 assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters)); 76 assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == false); 77 assert(bs->num_free_clusters > 0); 78 79 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming cluster %u\n", cluster_num); 80 81 spdk_bit_array_set(bs->used_clusters, cluster_num); 82 bs->num_free_clusters--; 83 } 84 85 static int 86 _spdk_blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster) 87 { 88 uint64_t *cluster_lba = &blob->active.clusters[cluster_num]; 89 90 _spdk_blob_verify_md_op(blob); 91 92 if (*cluster_lba != 0) { 93 return -EEXIST; 94 } 95 96 *cluster_lba = _spdk_bs_cluster_to_lba(blob->bs, cluster); 97 return 0; 98 } 99 100 static int 101 _spdk_bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num, 102 uint64_t *lowest_free_cluster, bool update_map) 103 { 104 pthread_mutex_lock(&blob->bs->used_clusters_mutex); 105 *lowest_free_cluster = spdk_bit_array_find_first_clear(blob->bs->used_clusters, 106 *lowest_free_cluster); 107 if (*lowest_free_cluster == UINT32_MAX) { 108 /* No more free clusters. Cannot satisfy the request */ 109 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 110 return -ENOSPC; 111 } 112 113 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming cluster %lu for blob %lu\n", *lowest_free_cluster, blob->id); 114 _spdk_bs_claim_cluster(blob->bs, *lowest_free_cluster); 115 pthread_mutex_unlock(&blob->bs->used_clusters_mutex); 116 117 if (update_map) { 118 _spdk_blob_insert_cluster(blob, cluster_num, *lowest_free_cluster); 119 } 120 121 return 0; 122 } 123 124 static void 125 _spdk_bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num) 126 { 127 assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters)); 128 assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == true); 129 assert(bs->num_free_clusters < bs->total_clusters); 130 131 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Releasing cluster %u\n", cluster_num); 132 133 pthread_mutex_lock(&bs->used_clusters_mutex); 134 spdk_bit_array_clear(bs->used_clusters, cluster_num); 135 bs->num_free_clusters++; 136 pthread_mutex_unlock(&bs->used_clusters_mutex); 137 } 138 139 static void 140 _spdk_blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs) 141 { 142 xattrs->count = 0; 143 xattrs->names = NULL; 144 xattrs->ctx = NULL; 145 xattrs->get_value = NULL; 146 } 147 148 void 149 spdk_blob_opts_init(struct spdk_blob_opts *opts) 150 { 151 opts->num_clusters = 0; 152 opts->thin_provision = false; 153 _spdk_blob_xattrs_init(&opts->xattrs); 154 } 155 156 void 157 spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts) 158 { 159 opts->clear_method = BLOB_CLEAR_WITH_UNMAP; 160 } 161 162 static struct spdk_blob * 163 _spdk_blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id) 164 { 165 struct spdk_blob *blob; 166 167 blob = calloc(1, sizeof(*blob)); 168 if (!blob) { 169 return NULL; 170 } 171 172 blob->id = id; 173 blob->bs = bs; 174 175 blob->parent_id = SPDK_BLOBID_INVALID; 176 177 blob->state = SPDK_BLOB_STATE_DIRTY; 178 blob->active.num_pages = 1; 179 blob->active.pages = calloc(1, sizeof(*blob->active.pages)); 180 if (!blob->active.pages) { 181 free(blob); 182 return NULL; 183 } 184 185 blob->active.pages[0] = _spdk_bs_blobid_to_page(id); 186 187 TAILQ_INIT(&blob->xattrs); 188 TAILQ_INIT(&blob->xattrs_internal); 189 190 return blob; 191 } 192 193 static void 194 _spdk_xattrs_free(struct spdk_xattr_tailq *xattrs) 195 { 196 struct spdk_xattr *xattr, *xattr_tmp; 197 198 TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) { 199 TAILQ_REMOVE(xattrs, xattr, link); 200 free(xattr->name); 201 free(xattr->value); 202 free(xattr); 203 } 204 } 205 206 static void 207 _spdk_blob_free(struct spdk_blob *blob) 208 { 209 assert(blob != NULL); 210 211 free(blob->active.clusters); 212 free(blob->clean.clusters); 213 free(blob->active.pages); 214 free(blob->clean.pages); 215 216 _spdk_xattrs_free(&blob->xattrs); 217 _spdk_xattrs_free(&blob->xattrs_internal); 218 219 if (blob->back_bs_dev) { 220 blob->back_bs_dev->destroy(blob->back_bs_dev); 221 } 222 223 free(blob); 224 } 225 226 struct freeze_io_ctx { 227 struct spdk_bs_cpl cpl; 228 struct spdk_blob *blob; 229 }; 230 231 static void 232 _spdk_blob_io_sync(struct spdk_io_channel_iter *i) 233 { 234 spdk_for_each_channel_continue(i, 0); 235 } 236 237 static void 238 _spdk_blob_execute_queued_io(struct spdk_io_channel_iter *i) 239 { 240 struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i); 241 struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch); 242 struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 243 struct spdk_bs_request_set *set; 244 struct spdk_bs_user_op_args *args; 245 spdk_bs_user_op_t *op, *tmp; 246 247 TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) { 248 set = (struct spdk_bs_request_set *)op; 249 args = &set->u.user_op; 250 251 if (args->blob == ctx->blob) { 252 TAILQ_REMOVE(&ch->queued_io, op, link); 253 spdk_bs_user_op_execute(op); 254 } 255 } 256 257 spdk_for_each_channel_continue(i, 0); 258 } 259 260 static void 261 _spdk_blob_io_cpl(struct spdk_io_channel_iter *i, int status) 262 { 263 struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 264 265 ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0); 266 267 free(ctx); 268 } 269 270 static void 271 _spdk_blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 272 { 273 struct freeze_io_ctx *ctx; 274 275 ctx = calloc(1, sizeof(*ctx)); 276 if (!ctx) { 277 cb_fn(cb_arg, -ENOMEM); 278 return; 279 } 280 281 ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 282 ctx->cpl.u.blob_basic.cb_fn = cb_fn; 283 ctx->cpl.u.blob_basic.cb_arg = cb_arg; 284 ctx->blob = blob; 285 286 /* Freeze I/O on blob */ 287 blob->frozen_refcnt++; 288 289 if (blob->frozen_refcnt == 1) { 290 spdk_for_each_channel(blob->bs, _spdk_blob_io_sync, ctx, _spdk_blob_io_cpl); 291 } else { 292 cb_fn(cb_arg, 0); 293 free(ctx); 294 } 295 } 296 297 static void 298 _spdk_blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 299 { 300 struct freeze_io_ctx *ctx; 301 302 ctx = calloc(1, sizeof(*ctx)); 303 if (!ctx) { 304 cb_fn(cb_arg, -ENOMEM); 305 return; 306 } 307 308 ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 309 ctx->cpl.u.blob_basic.cb_fn = cb_fn; 310 ctx->cpl.u.blob_basic.cb_arg = cb_arg; 311 ctx->blob = blob; 312 313 assert(blob->frozen_refcnt > 0); 314 315 blob->frozen_refcnt--; 316 317 if (blob->frozen_refcnt == 0) { 318 spdk_for_each_channel(blob->bs, _spdk_blob_execute_queued_io, ctx, _spdk_blob_io_cpl); 319 } else { 320 cb_fn(cb_arg, 0); 321 free(ctx); 322 } 323 } 324 325 static int 326 _spdk_blob_mark_clean(struct spdk_blob *blob) 327 { 328 uint64_t *clusters = NULL; 329 uint32_t *pages = NULL; 330 331 assert(blob != NULL); 332 333 if (blob->active.num_clusters) { 334 assert(blob->active.clusters); 335 clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters)); 336 if (!clusters) { 337 return -ENOMEM; 338 } 339 memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*clusters)); 340 } 341 342 if (blob->active.num_pages) { 343 assert(blob->active.pages); 344 pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages)); 345 if (!pages) { 346 free(clusters); 347 return -ENOMEM; 348 } 349 memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*pages)); 350 } 351 352 free(blob->clean.clusters); 353 free(blob->clean.pages); 354 355 blob->clean.num_clusters = blob->active.num_clusters; 356 blob->clean.clusters = blob->active.clusters; 357 blob->clean.num_pages = blob->active.num_pages; 358 blob->clean.pages = blob->active.pages; 359 360 blob->active.clusters = clusters; 361 blob->active.pages = pages; 362 363 /* If the metadata was dirtied again while the metadata was being written to disk, 364 * we do not want to revert the DIRTY state back to CLEAN here. 365 */ 366 if (blob->state == SPDK_BLOB_STATE_LOADING) { 367 blob->state = SPDK_BLOB_STATE_CLEAN; 368 } 369 370 return 0; 371 } 372 373 static int 374 _spdk_blob_deserialize_xattr(struct spdk_blob *blob, 375 struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal) 376 { 377 struct spdk_xattr *xattr; 378 379 if (desc_xattr->length != sizeof(desc_xattr->name_length) + 380 sizeof(desc_xattr->value_length) + 381 desc_xattr->name_length + desc_xattr->value_length) { 382 return -EINVAL; 383 } 384 385 xattr = calloc(1, sizeof(*xattr)); 386 if (xattr == NULL) { 387 return -ENOMEM; 388 } 389 390 xattr->name = malloc(desc_xattr->name_length + 1); 391 if (xattr->name == NULL) { 392 free(xattr); 393 return -ENOMEM; 394 } 395 memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length); 396 xattr->name[desc_xattr->name_length] = '\0'; 397 398 xattr->value = malloc(desc_xattr->value_length); 399 if (xattr->value == NULL) { 400 free(xattr->name); 401 free(xattr); 402 return -ENOMEM; 403 } 404 xattr->value_len = desc_xattr->value_length; 405 memcpy(xattr->value, 406 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 407 desc_xattr->value_length); 408 409 TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link); 410 411 return 0; 412 } 413 414 415 static int 416 _spdk_blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob) 417 { 418 struct spdk_blob_md_descriptor *desc; 419 size_t cur_desc = 0; 420 void *tmp; 421 422 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 423 while (cur_desc < sizeof(page->descriptors)) { 424 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 425 if (desc->length == 0) { 426 /* If padding and length are 0, this terminates the page */ 427 break; 428 } 429 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 430 struct spdk_blob_md_descriptor_flags *desc_flags; 431 432 desc_flags = (struct spdk_blob_md_descriptor_flags *)desc; 433 434 if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) { 435 return -EINVAL; 436 } 437 438 if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) != 439 SPDK_BLOB_INVALID_FLAGS_MASK) { 440 return -EINVAL; 441 } 442 443 if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) != 444 SPDK_BLOB_DATA_RO_FLAGS_MASK) { 445 blob->data_ro = true; 446 blob->md_ro = true; 447 } 448 449 if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) != 450 SPDK_BLOB_MD_RO_FLAGS_MASK) { 451 blob->md_ro = true; 452 } 453 454 if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 455 blob->data_ro = true; 456 blob->md_ro = true; 457 } 458 459 blob->invalid_flags = desc_flags->invalid_flags; 460 blob->data_ro_flags = desc_flags->data_ro_flags; 461 blob->md_ro_flags = desc_flags->md_ro_flags; 462 463 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 464 struct spdk_blob_md_descriptor_extent *desc_extent; 465 unsigned int i, j; 466 unsigned int cluster_count = blob->active.num_clusters; 467 468 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 469 470 if (desc_extent->length == 0 || 471 (desc_extent->length % sizeof(desc_extent->extents[0]) != 0)) { 472 return -EINVAL; 473 } 474 475 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 476 for (j = 0; j < desc_extent->extents[i].length; j++) { 477 if (desc_extent->extents[i].cluster_idx != 0) { 478 if (!spdk_bit_array_get(blob->bs->used_clusters, 479 desc_extent->extents[i].cluster_idx + j)) { 480 return -EINVAL; 481 } 482 } 483 cluster_count++; 484 } 485 } 486 487 if (cluster_count == 0) { 488 return -EINVAL; 489 } 490 tmp = realloc(blob->active.clusters, cluster_count * sizeof(uint64_t)); 491 if (tmp == NULL) { 492 return -ENOMEM; 493 } 494 blob->active.clusters = tmp; 495 blob->active.cluster_array_size = cluster_count; 496 497 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 498 for (j = 0; j < desc_extent->extents[i].length; j++) { 499 if (desc_extent->extents[i].cluster_idx != 0) { 500 blob->active.clusters[blob->active.num_clusters++] = _spdk_bs_cluster_to_lba(blob->bs, 501 desc_extent->extents[i].cluster_idx + j); 502 } else if (spdk_blob_is_thin_provisioned(blob)) { 503 blob->active.clusters[blob->active.num_clusters++] = 0; 504 } else { 505 return -EINVAL; 506 } 507 } 508 } 509 510 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 511 int rc; 512 513 rc = _spdk_blob_deserialize_xattr(blob, 514 (struct spdk_blob_md_descriptor_xattr *) desc, false); 515 if (rc != 0) { 516 return rc; 517 } 518 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 519 int rc; 520 521 rc = _spdk_blob_deserialize_xattr(blob, 522 (struct spdk_blob_md_descriptor_xattr *) desc, true); 523 if (rc != 0) { 524 return rc; 525 } 526 } else { 527 /* Unrecognized descriptor type. Do not fail - just continue to the 528 * next descriptor. If this descriptor is associated with some feature 529 * defined in a newer version of blobstore, that version of blobstore 530 * should create and set an associated feature flag to specify if this 531 * blob can be loaded or not. 532 */ 533 } 534 535 /* Advance to the next descriptor */ 536 cur_desc += sizeof(*desc) + desc->length; 537 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 538 break; 539 } 540 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 541 } 542 543 return 0; 544 } 545 546 static int 547 _spdk_blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count, 548 struct spdk_blob *blob) 549 { 550 const struct spdk_blob_md_page *page; 551 uint32_t i; 552 int rc; 553 554 assert(page_count > 0); 555 assert(pages[0].sequence_num == 0); 556 assert(blob != NULL); 557 assert(blob->state == SPDK_BLOB_STATE_LOADING); 558 assert(blob->active.clusters == NULL); 559 560 /* The blobid provided doesn't match what's in the MD, this can 561 * happen for example if a bogus blobid is passed in through open. 562 */ 563 if (blob->id != pages[0].id) { 564 SPDK_ERRLOG("Blobid (%lu) doesn't match what's in metadata (%lu)\n", 565 blob->id, pages[0].id); 566 return -ENOENT; 567 } 568 569 for (i = 0; i < page_count; i++) { 570 page = &pages[i]; 571 572 assert(page->id == blob->id); 573 assert(page->sequence_num == i); 574 575 rc = _spdk_blob_parse_page(page, blob); 576 if (rc != 0) { 577 return rc; 578 } 579 } 580 581 return 0; 582 } 583 584 static int 585 _spdk_blob_serialize_add_page(const struct spdk_blob *blob, 586 struct spdk_blob_md_page **pages, 587 uint32_t *page_count, 588 struct spdk_blob_md_page **last_page) 589 { 590 struct spdk_blob_md_page *page; 591 592 assert(pages != NULL); 593 assert(page_count != NULL); 594 595 if (*page_count == 0) { 596 assert(*pages == NULL); 597 *page_count = 1; 598 *pages = spdk_dma_malloc(SPDK_BS_PAGE_SIZE, 599 SPDK_BS_PAGE_SIZE, 600 NULL); 601 } else { 602 assert(*pages != NULL); 603 (*page_count)++; 604 *pages = spdk_dma_realloc(*pages, 605 SPDK_BS_PAGE_SIZE * (*page_count), 606 SPDK_BS_PAGE_SIZE, 607 NULL); 608 } 609 610 if (*pages == NULL) { 611 *page_count = 0; 612 *last_page = NULL; 613 return -ENOMEM; 614 } 615 616 page = &(*pages)[*page_count - 1]; 617 memset(page, 0, sizeof(*page)); 618 page->id = blob->id; 619 page->sequence_num = *page_count - 1; 620 page->next = SPDK_INVALID_MD_PAGE; 621 *last_page = page; 622 623 return 0; 624 } 625 626 /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor. 627 * Update required_sz on both success and failure. 628 * 629 */ 630 static int 631 _spdk_blob_serialize_xattr(const struct spdk_xattr *xattr, 632 uint8_t *buf, size_t buf_sz, 633 size_t *required_sz, bool internal) 634 { 635 struct spdk_blob_md_descriptor_xattr *desc; 636 637 *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) + 638 strlen(xattr->name) + 639 xattr->value_len; 640 641 if (buf_sz < *required_sz) { 642 return -1; 643 } 644 645 desc = (struct spdk_blob_md_descriptor_xattr *)buf; 646 647 desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR; 648 desc->length = sizeof(desc->name_length) + 649 sizeof(desc->value_length) + 650 strlen(xattr->name) + 651 xattr->value_len; 652 desc->name_length = strlen(xattr->name); 653 desc->value_length = xattr->value_len; 654 655 memcpy(desc->name, xattr->name, desc->name_length); 656 memcpy((void *)((uintptr_t)desc->name + desc->name_length), 657 xattr->value, 658 desc->value_length); 659 660 return 0; 661 } 662 663 static void 664 _spdk_blob_serialize_extent(const struct spdk_blob *blob, 665 uint64_t start_cluster, uint64_t *next_cluster, 666 uint8_t *buf, size_t buf_sz) 667 { 668 struct spdk_blob_md_descriptor_extent *desc; 669 size_t cur_sz; 670 uint64_t i, extent_idx; 671 uint64_t lba, lba_per_cluster, lba_count; 672 673 /* The buffer must have room for at least one extent */ 674 cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->extents[0]); 675 if (buf_sz < cur_sz) { 676 *next_cluster = start_cluster; 677 return; 678 } 679 680 desc = (struct spdk_blob_md_descriptor_extent *)buf; 681 desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT; 682 683 lba_per_cluster = _spdk_bs_cluster_to_lba(blob->bs, 1); 684 685 lba = blob->active.clusters[start_cluster]; 686 lba_count = lba_per_cluster; 687 extent_idx = 0; 688 for (i = start_cluster + 1; i < blob->active.num_clusters; i++) { 689 if ((lba + lba_count) == blob->active.clusters[i]) { 690 lba_count += lba_per_cluster; 691 continue; 692 } else if (lba == 0 && blob->active.clusters[i] == 0) { 693 lba_count += lba_per_cluster; 694 continue; 695 } 696 desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster; 697 desc->extents[extent_idx].length = lba_count / lba_per_cluster; 698 extent_idx++; 699 700 cur_sz += sizeof(desc->extents[extent_idx]); 701 702 if (buf_sz < cur_sz) { 703 /* If we ran out of buffer space, return */ 704 desc->length = sizeof(desc->extents[0]) * extent_idx; 705 *next_cluster = i; 706 return; 707 } 708 709 lba = blob->active.clusters[i]; 710 lba_count = lba_per_cluster; 711 } 712 713 desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster; 714 desc->extents[extent_idx].length = lba_count / lba_per_cluster; 715 extent_idx++; 716 717 desc->length = sizeof(desc->extents[0]) * extent_idx; 718 *next_cluster = blob->active.num_clusters; 719 720 return; 721 } 722 723 static void 724 _spdk_blob_serialize_flags(const struct spdk_blob *blob, 725 uint8_t *buf, size_t *buf_sz) 726 { 727 struct spdk_blob_md_descriptor_flags *desc; 728 729 /* 730 * Flags get serialized first, so we should always have room for the flags 731 * descriptor. 732 */ 733 assert(*buf_sz >= sizeof(*desc)); 734 735 desc = (struct spdk_blob_md_descriptor_flags *)buf; 736 desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS; 737 desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor); 738 desc->invalid_flags = blob->invalid_flags; 739 desc->data_ro_flags = blob->data_ro_flags; 740 desc->md_ro_flags = blob->md_ro_flags; 741 742 *buf_sz -= sizeof(*desc); 743 } 744 745 static int 746 _spdk_blob_serialize_xattrs(const struct spdk_blob *blob, 747 const struct spdk_xattr_tailq *xattrs, bool internal, 748 struct spdk_blob_md_page **pages, 749 struct spdk_blob_md_page *cur_page, 750 uint32_t *page_count, uint8_t **buf, 751 size_t *remaining_sz) 752 { 753 const struct spdk_xattr *xattr; 754 int rc; 755 756 TAILQ_FOREACH(xattr, xattrs, link) { 757 size_t required_sz = 0; 758 759 rc = _spdk_blob_serialize_xattr(xattr, 760 *buf, *remaining_sz, 761 &required_sz, internal); 762 if (rc < 0) { 763 /* Need to add a new page to the chain */ 764 rc = _spdk_blob_serialize_add_page(blob, pages, page_count, 765 &cur_page); 766 if (rc < 0) { 767 spdk_dma_free(*pages); 768 *pages = NULL; 769 *page_count = 0; 770 return rc; 771 } 772 773 *buf = (uint8_t *)cur_page->descriptors; 774 *remaining_sz = sizeof(cur_page->descriptors); 775 776 /* Try again */ 777 required_sz = 0; 778 rc = _spdk_blob_serialize_xattr(xattr, 779 *buf, *remaining_sz, 780 &required_sz, internal); 781 782 if (rc < 0) { 783 spdk_dma_free(*pages); 784 *pages = NULL; 785 *page_count = 0; 786 return rc; 787 } 788 } 789 790 *remaining_sz -= required_sz; 791 *buf += required_sz; 792 } 793 794 return 0; 795 } 796 797 static int 798 _spdk_blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages, 799 uint32_t *page_count) 800 { 801 struct spdk_blob_md_page *cur_page; 802 int rc; 803 uint8_t *buf; 804 size_t remaining_sz; 805 uint64_t last_cluster; 806 807 assert(pages != NULL); 808 assert(page_count != NULL); 809 assert(blob != NULL); 810 assert(blob->state == SPDK_BLOB_STATE_DIRTY); 811 812 *pages = NULL; 813 *page_count = 0; 814 815 /* A blob always has at least 1 page, even if it has no descriptors */ 816 rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page); 817 if (rc < 0) { 818 return rc; 819 } 820 821 buf = (uint8_t *)cur_page->descriptors; 822 remaining_sz = sizeof(cur_page->descriptors); 823 824 /* Serialize flags */ 825 _spdk_blob_serialize_flags(blob, buf, &remaining_sz); 826 buf += sizeof(struct spdk_blob_md_descriptor_flags); 827 828 /* Serialize xattrs */ 829 rc = _spdk_blob_serialize_xattrs(blob, &blob->xattrs, false, 830 pages, cur_page, page_count, &buf, &remaining_sz); 831 if (rc < 0) { 832 return rc; 833 } 834 835 /* Serialize internal xattrs */ 836 rc = _spdk_blob_serialize_xattrs(blob, &blob->xattrs_internal, true, 837 pages, cur_page, page_count, &buf, &remaining_sz); 838 if (rc < 0) { 839 return rc; 840 } 841 842 /* Serialize extents */ 843 last_cluster = 0; 844 while (last_cluster < blob->active.num_clusters) { 845 _spdk_blob_serialize_extent(blob, last_cluster, &last_cluster, 846 buf, remaining_sz); 847 848 if (last_cluster == blob->active.num_clusters) { 849 break; 850 } 851 852 rc = _spdk_blob_serialize_add_page(blob, pages, page_count, 853 &cur_page); 854 if (rc < 0) { 855 return rc; 856 } 857 858 buf = (uint8_t *)cur_page->descriptors; 859 remaining_sz = sizeof(cur_page->descriptors); 860 } 861 862 return 0; 863 } 864 865 struct spdk_blob_load_ctx { 866 struct spdk_blob *blob; 867 868 struct spdk_blob_md_page *pages; 869 uint32_t num_pages; 870 spdk_bs_sequence_t *seq; 871 872 spdk_bs_sequence_cpl cb_fn; 873 void *cb_arg; 874 }; 875 876 static uint32_t 877 _spdk_blob_md_page_calc_crc(void *page) 878 { 879 uint32_t crc; 880 881 crc = BLOB_CRC32C_INITIAL; 882 crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc); 883 crc ^= BLOB_CRC32C_INITIAL; 884 885 return crc; 886 887 } 888 889 static void 890 _spdk_blob_load_final(void *cb_arg, int bserrno) 891 { 892 struct spdk_blob_load_ctx *ctx = cb_arg; 893 struct spdk_blob *blob = ctx->blob; 894 895 _spdk_blob_mark_clean(blob); 896 897 ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno); 898 899 /* Free the memory */ 900 spdk_dma_free(ctx->pages); 901 free(ctx); 902 } 903 904 static void 905 _spdk_blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno) 906 { 907 struct spdk_blob_load_ctx *ctx = cb_arg; 908 struct spdk_blob *blob = ctx->blob; 909 910 if (bserrno != 0) { 911 goto error; 912 } 913 914 blob->back_bs_dev = spdk_bs_create_blob_bs_dev(snapshot); 915 916 if (blob->back_bs_dev == NULL) { 917 bserrno = -ENOMEM; 918 goto error; 919 } 920 921 _spdk_blob_load_final(ctx, bserrno); 922 return; 923 924 error: 925 SPDK_ERRLOG("Snapshot fail\n"); 926 _spdk_blob_free(blob); 927 ctx->cb_fn(ctx->seq, NULL, bserrno); 928 spdk_dma_free(ctx->pages); 929 free(ctx); 930 } 931 932 static void 933 _spdk_blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 934 { 935 struct spdk_blob_load_ctx *ctx = cb_arg; 936 struct spdk_blob *blob = ctx->blob; 937 struct spdk_blob_md_page *page; 938 const void *value; 939 size_t len; 940 int rc; 941 uint32_t crc; 942 943 if (bserrno) { 944 SPDK_ERRLOG("Metadata page read failed: %d\n", bserrno); 945 _spdk_blob_free(blob); 946 ctx->cb_fn(seq, NULL, bserrno); 947 spdk_dma_free(ctx->pages); 948 free(ctx); 949 return; 950 } 951 952 page = &ctx->pages[ctx->num_pages - 1]; 953 crc = _spdk_blob_md_page_calc_crc(page); 954 if (crc != page->crc) { 955 SPDK_ERRLOG("Metadata page %d crc mismatch\n", ctx->num_pages); 956 _spdk_blob_free(blob); 957 ctx->cb_fn(seq, NULL, -EINVAL); 958 spdk_dma_free(ctx->pages); 959 free(ctx); 960 return; 961 } 962 963 if (page->next != SPDK_INVALID_MD_PAGE) { 964 uint32_t next_page = page->next; 965 uint64_t next_lba = _spdk_bs_page_to_lba(blob->bs, blob->bs->md_start + next_page); 966 967 968 assert(next_lba < (blob->bs->md_start + blob->bs->md_len)); 969 970 /* Read the next page */ 971 ctx->num_pages++; 972 ctx->pages = spdk_dma_realloc(ctx->pages, (sizeof(*page) * ctx->num_pages), 973 sizeof(*page), NULL); 974 if (ctx->pages == NULL) { 975 ctx->cb_fn(seq, ctx->cb_arg, -ENOMEM); 976 free(ctx); 977 return; 978 } 979 980 spdk_bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1], 981 next_lba, 982 _spdk_bs_byte_to_lba(blob->bs, sizeof(*page)), 983 _spdk_blob_load_cpl, ctx); 984 return; 985 } 986 987 /* Parse the pages */ 988 rc = _spdk_blob_parse(ctx->pages, ctx->num_pages, blob); 989 if (rc) { 990 _spdk_blob_free(blob); 991 ctx->cb_fn(seq, NULL, rc); 992 spdk_dma_free(ctx->pages); 993 free(ctx); 994 return; 995 } 996 ctx->seq = seq; 997 998 999 if (spdk_blob_is_thin_provisioned(blob)) { 1000 rc = _spdk_blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true); 1001 if (rc == 0) { 1002 if (len != sizeof(spdk_blob_id)) { 1003 _spdk_blob_free(blob); 1004 ctx->cb_fn(seq, NULL, -EINVAL); 1005 spdk_dma_free(ctx->pages); 1006 free(ctx); 1007 return; 1008 } 1009 /* open snapshot blob and continue in the callback function */ 1010 blob->parent_id = *(spdk_blob_id *)value; 1011 spdk_bs_open_blob(blob->bs, blob->parent_id, 1012 _spdk_blob_load_snapshot_cpl, ctx); 1013 return; 1014 } else { 1015 /* add zeroes_dev for thin provisioned blob */ 1016 blob->back_bs_dev = spdk_bs_create_zeroes_dev(); 1017 } 1018 } else { 1019 /* standard blob */ 1020 blob->back_bs_dev = NULL; 1021 } 1022 _spdk_blob_load_final(ctx, bserrno); 1023 } 1024 1025 /* Load a blob from disk given a blobid */ 1026 static void 1027 _spdk_blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 1028 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 1029 { 1030 struct spdk_blob_load_ctx *ctx; 1031 struct spdk_blob_store *bs; 1032 uint32_t page_num; 1033 uint64_t lba; 1034 1035 _spdk_blob_verify_md_op(blob); 1036 1037 bs = blob->bs; 1038 1039 ctx = calloc(1, sizeof(*ctx)); 1040 if (!ctx) { 1041 cb_fn(seq, cb_arg, -ENOMEM); 1042 return; 1043 } 1044 1045 ctx->blob = blob; 1046 ctx->pages = spdk_dma_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 1047 SPDK_BS_PAGE_SIZE, NULL); 1048 if (!ctx->pages) { 1049 free(ctx); 1050 cb_fn(seq, cb_arg, -ENOMEM); 1051 return; 1052 } 1053 ctx->num_pages = 1; 1054 ctx->cb_fn = cb_fn; 1055 ctx->cb_arg = cb_arg; 1056 1057 page_num = _spdk_bs_blobid_to_page(blob->id); 1058 lba = _spdk_bs_page_to_lba(blob->bs, bs->md_start + page_num); 1059 1060 blob->state = SPDK_BLOB_STATE_LOADING; 1061 1062 spdk_bs_sequence_read_dev(seq, &ctx->pages[0], lba, 1063 _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE), 1064 _spdk_blob_load_cpl, ctx); 1065 } 1066 1067 struct spdk_blob_persist_ctx { 1068 struct spdk_blob *blob; 1069 1070 struct spdk_bs_super_block *super; 1071 1072 struct spdk_blob_md_page *pages; 1073 1074 uint64_t idx; 1075 1076 spdk_bs_sequence_t *seq; 1077 spdk_bs_sequence_cpl cb_fn; 1078 void *cb_arg; 1079 }; 1080 1081 static void 1082 spdk_bs_batch_clear_dev(struct spdk_blob_persist_ctx *ctx, spdk_bs_batch_t *batch, uint64_t lba, 1083 uint32_t lba_count) 1084 { 1085 if (ctx->blob->clear_method == BLOB_CLEAR_WITH_DEFAULT || 1086 ctx->blob->clear_method == BLOB_CLEAR_WITH_UNMAP) { 1087 spdk_bs_batch_unmap_dev(batch, lba, lba_count); 1088 } else if (ctx->blob->clear_method == BLOB_CLEAR_WITH_WRITE_ZEROES) { 1089 spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count); 1090 } 1091 } 1092 1093 static void 1094 _spdk_blob_persist_complete(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1095 { 1096 struct spdk_blob_persist_ctx *ctx = cb_arg; 1097 struct spdk_blob *blob = ctx->blob; 1098 1099 if (bserrno == 0) { 1100 _spdk_blob_mark_clean(blob); 1101 } 1102 1103 /* Call user callback */ 1104 ctx->cb_fn(seq, ctx->cb_arg, bserrno); 1105 1106 /* Free the memory */ 1107 spdk_dma_free(ctx->pages); 1108 free(ctx); 1109 } 1110 1111 static void 1112 _spdk_blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1113 { 1114 struct spdk_blob_persist_ctx *ctx = cb_arg; 1115 struct spdk_blob *blob = ctx->blob; 1116 struct spdk_blob_store *bs = blob->bs; 1117 void *tmp; 1118 size_t i; 1119 1120 /* Release all clusters that were truncated */ 1121 for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) { 1122 uint32_t cluster_num = _spdk_bs_lba_to_cluster(bs, blob->active.clusters[i]); 1123 1124 /* Nothing to release if it was not allocated */ 1125 if (blob->active.clusters[i] != 0) { 1126 _spdk_bs_release_cluster(bs, cluster_num); 1127 } 1128 } 1129 1130 if (blob->active.num_clusters == 0) { 1131 free(blob->active.clusters); 1132 blob->active.clusters = NULL; 1133 blob->active.cluster_array_size = 0; 1134 } else { 1135 tmp = realloc(blob->active.clusters, sizeof(uint64_t) * blob->active.num_clusters); 1136 assert(tmp != NULL); 1137 blob->active.clusters = tmp; 1138 blob->active.cluster_array_size = blob->active.num_clusters; 1139 } 1140 1141 _spdk_blob_persist_complete(seq, ctx, bserrno); 1142 } 1143 1144 static void 1145 _spdk_blob_persist_clear_clusters(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1146 { 1147 struct spdk_blob_persist_ctx *ctx = cb_arg; 1148 struct spdk_blob *blob = ctx->blob; 1149 struct spdk_blob_store *bs = blob->bs; 1150 spdk_bs_batch_t *batch; 1151 size_t i; 1152 uint64_t lba; 1153 uint32_t lba_count; 1154 1155 /* Clusters don't move around in blobs. The list shrinks or grows 1156 * at the end, but no changes ever occur in the middle of the list. 1157 */ 1158 1159 batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_clear_clusters_cpl, ctx); 1160 1161 /* Clear all clusters that were truncated */ 1162 lba = 0; 1163 lba_count = 0; 1164 for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) { 1165 uint64_t next_lba = blob->active.clusters[i]; 1166 uint32_t next_lba_count = _spdk_bs_cluster_to_lba(bs, 1); 1167 1168 if (next_lba > 0 && (lba + lba_count) == next_lba) { 1169 /* This cluster is contiguous with the previous one. */ 1170 lba_count += next_lba_count; 1171 continue; 1172 } 1173 1174 /* This cluster is not contiguous with the previous one. */ 1175 1176 /* If a run of LBAs previously existing, clear them now */ 1177 if (lba_count > 0) { 1178 spdk_bs_batch_clear_dev(ctx, batch, lba, lba_count); 1179 } 1180 1181 /* Start building the next batch */ 1182 lba = next_lba; 1183 if (next_lba > 0) { 1184 lba_count = next_lba_count; 1185 } else { 1186 lba_count = 0; 1187 } 1188 } 1189 1190 /* If we ended with a contiguous set of LBAs, clear them now */ 1191 if (lba_count > 0) { 1192 spdk_bs_batch_clear_dev(ctx, batch, lba, lba_count); 1193 } 1194 1195 spdk_bs_batch_close(batch); 1196 } 1197 1198 static void 1199 _spdk_blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1200 { 1201 struct spdk_blob_persist_ctx *ctx = cb_arg; 1202 struct spdk_blob *blob = ctx->blob; 1203 struct spdk_blob_store *bs = blob->bs; 1204 size_t i; 1205 1206 /* This loop starts at 1 because the first page is special and handled 1207 * below. The pages (except the first) are never written in place, 1208 * so any pages in the clean list must be zeroed. 1209 */ 1210 for (i = 1; i < blob->clean.num_pages; i++) { 1211 spdk_bit_array_clear(bs->used_md_pages, blob->clean.pages[i]); 1212 } 1213 1214 if (blob->active.num_pages == 0) { 1215 uint32_t page_num; 1216 1217 page_num = _spdk_bs_blobid_to_page(blob->id); 1218 spdk_bit_array_clear(bs->used_md_pages, page_num); 1219 } 1220 1221 /* Move on to clearing clusters */ 1222 _spdk_blob_persist_clear_clusters(seq, ctx, 0); 1223 } 1224 1225 static void 1226 _spdk_blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1227 { 1228 struct spdk_blob_persist_ctx *ctx = cb_arg; 1229 struct spdk_blob *blob = ctx->blob; 1230 struct spdk_blob_store *bs = blob->bs; 1231 uint64_t lba; 1232 uint32_t lba_count; 1233 spdk_bs_batch_t *batch; 1234 size_t i; 1235 1236 batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_zero_pages_cpl, ctx); 1237 1238 lba_count = _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE); 1239 1240 /* This loop starts at 1 because the first page is special and handled 1241 * below. The pages (except the first) are never written in place, 1242 * so any pages in the clean list must be zeroed. 1243 */ 1244 for (i = 1; i < blob->clean.num_pages; i++) { 1245 lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->clean.pages[i]); 1246 1247 spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count); 1248 } 1249 1250 /* The first page will only be zeroed if this is a delete. */ 1251 if (blob->active.num_pages == 0) { 1252 uint32_t page_num; 1253 1254 /* The first page in the metadata goes where the blobid indicates */ 1255 page_num = _spdk_bs_blobid_to_page(blob->id); 1256 lba = _spdk_bs_page_to_lba(bs, bs->md_start + page_num); 1257 1258 spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count); 1259 } 1260 1261 spdk_bs_batch_close(batch); 1262 } 1263 1264 static void 1265 _spdk_blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1266 { 1267 struct spdk_blob_persist_ctx *ctx = cb_arg; 1268 struct spdk_blob *blob = ctx->blob; 1269 struct spdk_blob_store *bs = blob->bs; 1270 uint64_t lba; 1271 uint32_t lba_count; 1272 struct spdk_blob_md_page *page; 1273 1274 if (blob->active.num_pages == 0) { 1275 /* Move on to the next step */ 1276 _spdk_blob_persist_zero_pages(seq, ctx, 0); 1277 return; 1278 } 1279 1280 lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page)); 1281 1282 page = &ctx->pages[0]; 1283 /* The first page in the metadata goes where the blobid indicates */ 1284 lba = _spdk_bs_page_to_lba(bs, bs->md_start + _spdk_bs_blobid_to_page(blob->id)); 1285 1286 spdk_bs_sequence_write_dev(seq, page, lba, lba_count, 1287 _spdk_blob_persist_zero_pages, ctx); 1288 } 1289 1290 static void 1291 _spdk_blob_persist_write_page_chain(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1292 { 1293 struct spdk_blob_persist_ctx *ctx = cb_arg; 1294 struct spdk_blob *blob = ctx->blob; 1295 struct spdk_blob_store *bs = blob->bs; 1296 uint64_t lba; 1297 uint32_t lba_count; 1298 struct spdk_blob_md_page *page; 1299 spdk_bs_batch_t *batch; 1300 size_t i; 1301 1302 /* Clusters don't move around in blobs. The list shrinks or grows 1303 * at the end, but no changes ever occur in the middle of the list. 1304 */ 1305 1306 lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page)); 1307 1308 batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_write_page_root, ctx); 1309 1310 /* This starts at 1. The root page is not written until 1311 * all of the others are finished 1312 */ 1313 for (i = 1; i < blob->active.num_pages; i++) { 1314 page = &ctx->pages[i]; 1315 assert(page->sequence_num == i); 1316 1317 lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->active.pages[i]); 1318 1319 spdk_bs_batch_write_dev(batch, page, lba, lba_count); 1320 } 1321 1322 spdk_bs_batch_close(batch); 1323 } 1324 1325 static int 1326 _spdk_blob_resize(struct spdk_blob *blob, uint64_t sz) 1327 { 1328 uint64_t i; 1329 uint64_t *tmp; 1330 uint64_t lfc; /* lowest free cluster */ 1331 uint64_t num_clusters; 1332 struct spdk_blob_store *bs; 1333 1334 bs = blob->bs; 1335 1336 _spdk_blob_verify_md_op(blob); 1337 1338 if (blob->active.num_clusters == sz) { 1339 return 0; 1340 } 1341 1342 if (blob->active.num_clusters < blob->active.cluster_array_size) { 1343 /* If this blob was resized to be larger, then smaller, then 1344 * larger without syncing, then the cluster array already 1345 * contains spare assigned clusters we can use. 1346 */ 1347 num_clusters = spdk_min(blob->active.cluster_array_size, 1348 sz); 1349 } else { 1350 num_clusters = blob->active.num_clusters; 1351 } 1352 1353 /* Do two passes - one to verify that we can obtain enough clusters 1354 * and another to actually claim them. 1355 */ 1356 1357 if (spdk_blob_is_thin_provisioned(blob) == false) { 1358 lfc = 0; 1359 for (i = num_clusters; i < sz; i++) { 1360 lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc); 1361 if (lfc == UINT32_MAX) { 1362 /* No more free clusters. Cannot satisfy the request */ 1363 return -ENOSPC; 1364 } 1365 lfc++; 1366 } 1367 } 1368 1369 if (sz > num_clusters) { 1370 /* Expand the cluster array if necessary. 1371 * We only shrink the array when persisting. 1372 */ 1373 tmp = realloc(blob->active.clusters, sizeof(uint64_t) * sz); 1374 if (sz > 0 && tmp == NULL) { 1375 return -ENOMEM; 1376 } 1377 memset(tmp + blob->active.cluster_array_size, 0, 1378 sizeof(uint64_t) * (sz - blob->active.cluster_array_size)); 1379 blob->active.clusters = tmp; 1380 blob->active.cluster_array_size = sz; 1381 } 1382 1383 blob->state = SPDK_BLOB_STATE_DIRTY; 1384 1385 if (spdk_blob_is_thin_provisioned(blob) == false) { 1386 lfc = 0; 1387 for (i = num_clusters; i < sz; i++) { 1388 _spdk_bs_allocate_cluster(blob, i, &lfc, true); 1389 lfc++; 1390 } 1391 } 1392 1393 blob->active.num_clusters = sz; 1394 1395 return 0; 1396 } 1397 1398 static void 1399 _spdk_blob_persist_start(struct spdk_blob_persist_ctx *ctx) 1400 { 1401 spdk_bs_sequence_t *seq = ctx->seq; 1402 struct spdk_blob *blob = ctx->blob; 1403 struct spdk_blob_store *bs = blob->bs; 1404 uint64_t i; 1405 uint32_t page_num; 1406 void *tmp; 1407 int rc; 1408 1409 if (blob->active.num_pages == 0) { 1410 /* This is the signal that the blob should be deleted. 1411 * Immediately jump to the clean up routine. */ 1412 assert(blob->clean.num_pages > 0); 1413 ctx->idx = blob->clean.num_pages - 1; 1414 blob->state = SPDK_BLOB_STATE_CLEAN; 1415 _spdk_blob_persist_zero_pages(seq, ctx, 0); 1416 return; 1417 1418 } 1419 1420 /* Generate the new metadata */ 1421 rc = _spdk_blob_serialize(blob, &ctx->pages, &blob->active.num_pages); 1422 if (rc < 0) { 1423 _spdk_blob_persist_complete(seq, ctx, rc); 1424 return; 1425 } 1426 1427 assert(blob->active.num_pages >= 1); 1428 1429 /* Resize the cache of page indices */ 1430 tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages)); 1431 if (!tmp) { 1432 _spdk_blob_persist_complete(seq, ctx, -ENOMEM); 1433 return; 1434 } 1435 blob->active.pages = tmp; 1436 1437 /* Assign this metadata to pages. This requires two passes - 1438 * one to verify that there are enough pages and a second 1439 * to actually claim them. */ 1440 page_num = 0; 1441 /* Note that this loop starts at one. The first page location is fixed by the blobid. */ 1442 for (i = 1; i < blob->active.num_pages; i++) { 1443 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num); 1444 if (page_num == UINT32_MAX) { 1445 _spdk_blob_persist_complete(seq, ctx, -ENOMEM); 1446 return; 1447 } 1448 page_num++; 1449 } 1450 1451 page_num = 0; 1452 blob->active.pages[0] = _spdk_bs_blobid_to_page(blob->id); 1453 for (i = 1; i < blob->active.num_pages; i++) { 1454 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num); 1455 ctx->pages[i - 1].next = page_num; 1456 /* Now that previous metadata page is complete, calculate the crc for it. */ 1457 ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]); 1458 blob->active.pages[i] = page_num; 1459 spdk_bit_array_set(bs->used_md_pages, page_num); 1460 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming page %u for blob %lu\n", page_num, blob->id); 1461 page_num++; 1462 } 1463 ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]); 1464 /* Start writing the metadata from last page to first */ 1465 ctx->idx = blob->active.num_pages - 1; 1466 blob->state = SPDK_BLOB_STATE_CLEAN; 1467 _spdk_blob_persist_write_page_chain(seq, ctx, 0); 1468 } 1469 1470 static void 1471 _spdk_blob_persist_dirty_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1472 { 1473 struct spdk_blob_persist_ctx *ctx = cb_arg; 1474 1475 ctx->blob->bs->clean = 0; 1476 1477 spdk_dma_free(ctx->super); 1478 1479 _spdk_blob_persist_start(ctx); 1480 } 1481 1482 static void 1483 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 1484 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg); 1485 1486 1487 static void 1488 _spdk_blob_persist_dirty(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1489 { 1490 struct spdk_blob_persist_ctx *ctx = cb_arg; 1491 1492 ctx->super->clean = 0; 1493 if (ctx->super->size == 0) { 1494 ctx->super->size = ctx->blob->bs->dev->blockcnt * ctx->blob->bs->dev->blocklen; 1495 } 1496 1497 _spdk_bs_write_super(seq, ctx->blob->bs, ctx->super, _spdk_blob_persist_dirty_cpl, ctx); 1498 } 1499 1500 1501 /* Write a blob to disk */ 1502 static void 1503 _spdk_blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 1504 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 1505 { 1506 struct spdk_blob_persist_ctx *ctx; 1507 1508 _spdk_blob_verify_md_op(blob); 1509 1510 if (blob->state == SPDK_BLOB_STATE_CLEAN) { 1511 cb_fn(seq, cb_arg, 0); 1512 return; 1513 } 1514 1515 ctx = calloc(1, sizeof(*ctx)); 1516 if (!ctx) { 1517 cb_fn(seq, cb_arg, -ENOMEM); 1518 return; 1519 } 1520 ctx->blob = blob; 1521 ctx->seq = seq; 1522 ctx->cb_fn = cb_fn; 1523 ctx->cb_arg = cb_arg; 1524 1525 if (blob->bs->clean) { 1526 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 1527 if (!ctx->super) { 1528 cb_fn(seq, cb_arg, -ENOMEM); 1529 free(ctx); 1530 return; 1531 } 1532 1533 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(blob->bs, 0), 1534 _spdk_bs_byte_to_lba(blob->bs, sizeof(*ctx->super)), 1535 _spdk_blob_persist_dirty, ctx); 1536 } else { 1537 _spdk_blob_persist_start(ctx); 1538 } 1539 } 1540 1541 struct spdk_blob_copy_cluster_ctx { 1542 struct spdk_blob *blob; 1543 uint8_t *buf; 1544 uint64_t page; 1545 uint64_t new_cluster; 1546 spdk_bs_sequence_t *seq; 1547 }; 1548 1549 static void 1550 _spdk_blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno) 1551 { 1552 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 1553 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq; 1554 TAILQ_HEAD(, spdk_bs_request_set) requests; 1555 spdk_bs_user_op_t *op; 1556 1557 TAILQ_INIT(&requests); 1558 TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link); 1559 1560 while (!TAILQ_EMPTY(&requests)) { 1561 op = TAILQ_FIRST(&requests); 1562 TAILQ_REMOVE(&requests, op, link); 1563 if (bserrno == 0) { 1564 spdk_bs_user_op_execute(op); 1565 } else { 1566 spdk_bs_user_op_abort(op); 1567 } 1568 } 1569 1570 spdk_dma_free(ctx->buf); 1571 free(ctx); 1572 } 1573 1574 static void 1575 _spdk_blob_insert_cluster_cpl(void *cb_arg, int bserrno) 1576 { 1577 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 1578 1579 if (bserrno) { 1580 uint32_t cluster_number; 1581 1582 if (bserrno == -EEXIST) { 1583 /* The metadata insert failed because another thread 1584 * allocated the cluster first. Free our cluster 1585 * but continue without error. */ 1586 bserrno = 0; 1587 } 1588 1589 cluster_number = _spdk_bs_page_to_cluster(ctx->blob->bs, ctx->page); 1590 _spdk_bs_release_cluster(ctx->blob->bs, cluster_number); 1591 } 1592 1593 spdk_bs_sequence_finish(ctx->seq, bserrno); 1594 } 1595 1596 static void 1597 _spdk_blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1598 { 1599 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 1600 uint32_t cluster_number; 1601 1602 if (bserrno) { 1603 /* The write failed, so jump to the final completion handler */ 1604 spdk_bs_sequence_finish(seq, bserrno); 1605 return; 1606 } 1607 1608 cluster_number = _spdk_bs_page_to_cluster(ctx->blob->bs, ctx->page); 1609 1610 _spdk_blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster, 1611 _spdk_blob_insert_cluster_cpl, ctx); 1612 } 1613 1614 static void 1615 _spdk_blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1616 { 1617 struct spdk_blob_copy_cluster_ctx *ctx = cb_arg; 1618 1619 if (bserrno != 0) { 1620 /* The read failed, so jump to the final completion handler */ 1621 spdk_bs_sequence_finish(seq, bserrno); 1622 return; 1623 } 1624 1625 /* Write whole cluster */ 1626 spdk_bs_sequence_write_dev(seq, ctx->buf, 1627 _spdk_bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster), 1628 _spdk_bs_cluster_to_lba(ctx->blob->bs, 1), 1629 _spdk_blob_write_copy_cpl, ctx); 1630 } 1631 1632 static void 1633 _spdk_bs_allocate_and_copy_cluster(struct spdk_blob *blob, 1634 struct spdk_io_channel *_ch, 1635 uint64_t io_unit, spdk_bs_user_op_t *op) 1636 { 1637 struct spdk_bs_cpl cpl; 1638 struct spdk_bs_channel *ch; 1639 struct spdk_blob_copy_cluster_ctx *ctx; 1640 uint32_t cluster_start_page; 1641 uint32_t cluster_number; 1642 int rc; 1643 1644 ch = spdk_io_channel_get_ctx(_ch); 1645 1646 if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) { 1647 /* There are already operations pending. Queue this user op 1648 * and return because it will be re-executed when the outstanding 1649 * cluster allocation completes. */ 1650 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link); 1651 return; 1652 } 1653 1654 /* Round the io_unit offset down to the first page in the cluster */ 1655 cluster_start_page = _spdk_bs_io_unit_to_cluster_start(blob, io_unit); 1656 1657 /* Calculate which index in the metadata cluster array the corresponding 1658 * cluster is supposed to be at. */ 1659 cluster_number = _spdk_bs_io_unit_to_cluster_number(blob, io_unit); 1660 1661 ctx = calloc(1, sizeof(*ctx)); 1662 if (!ctx) { 1663 spdk_bs_user_op_abort(op); 1664 return; 1665 } 1666 1667 assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0); 1668 1669 ctx->blob = blob; 1670 ctx->page = cluster_start_page; 1671 1672 if (blob->parent_id != SPDK_BLOBID_INVALID) { 1673 ctx->buf = spdk_dma_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen, NULL); 1674 if (!ctx->buf) { 1675 SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n", 1676 blob->bs->cluster_sz); 1677 free(ctx); 1678 spdk_bs_user_op_abort(op); 1679 return; 1680 } 1681 } 1682 1683 rc = _spdk_bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, false); 1684 if (rc != 0) { 1685 spdk_dma_free(ctx->buf); 1686 free(ctx); 1687 spdk_bs_user_op_abort(op); 1688 return; 1689 } 1690 1691 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 1692 cpl.u.blob_basic.cb_fn = _spdk_blob_allocate_and_copy_cluster_cpl; 1693 cpl.u.blob_basic.cb_arg = ctx; 1694 1695 ctx->seq = spdk_bs_sequence_start(_ch, &cpl); 1696 if (!ctx->seq) { 1697 _spdk_bs_release_cluster(blob->bs, ctx->new_cluster); 1698 spdk_dma_free(ctx->buf); 1699 free(ctx); 1700 spdk_bs_user_op_abort(op); 1701 return; 1702 } 1703 1704 /* Queue the user op to block other incoming operations */ 1705 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link); 1706 1707 if (blob->parent_id != SPDK_BLOBID_INVALID) { 1708 /* Read cluster from backing device */ 1709 spdk_bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf, 1710 _spdk_bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page), 1711 _spdk_bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz), 1712 _spdk_blob_write_copy, ctx); 1713 } else { 1714 _spdk_blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster, 1715 _spdk_blob_insert_cluster_cpl, ctx); 1716 } 1717 } 1718 1719 static void 1720 _spdk_blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length, 1721 uint64_t *lba, uint32_t *lba_count) 1722 { 1723 *lba_count = length; 1724 1725 if (!_spdk_bs_io_unit_is_allocated(blob, io_unit)) { 1726 assert(blob->back_bs_dev != NULL); 1727 *lba = _spdk_bs_io_unit_to_back_dev_lba(blob, io_unit); 1728 *lba_count = _spdk_bs_io_unit_to_back_dev_lba(blob, *lba_count); 1729 } else { 1730 *lba = _spdk_bs_blob_io_unit_to_lba(blob, io_unit); 1731 } 1732 } 1733 1734 struct op_split_ctx { 1735 struct spdk_blob *blob; 1736 struct spdk_io_channel *channel; 1737 uint64_t io_unit_offset; 1738 uint64_t io_units_remaining; 1739 void *curr_payload; 1740 enum spdk_blob_op_type op_type; 1741 spdk_bs_sequence_t *seq; 1742 }; 1743 1744 static void 1745 _spdk_blob_request_submit_op_split_next(void *cb_arg, int bserrno) 1746 { 1747 struct op_split_ctx *ctx = cb_arg; 1748 struct spdk_blob *blob = ctx->blob; 1749 struct spdk_io_channel *ch = ctx->channel; 1750 enum spdk_blob_op_type op_type = ctx->op_type; 1751 uint8_t *buf = ctx->curr_payload; 1752 uint64_t offset = ctx->io_unit_offset; 1753 uint64_t length = ctx->io_units_remaining; 1754 uint64_t op_length; 1755 1756 if (bserrno != 0 || ctx->io_units_remaining == 0) { 1757 spdk_bs_sequence_finish(ctx->seq, bserrno); 1758 free(ctx); 1759 return; 1760 } 1761 1762 op_length = spdk_min(length, _spdk_bs_num_io_units_to_cluster_boundary(blob, 1763 offset)); 1764 1765 /* Update length and payload for next operation */ 1766 ctx->io_units_remaining -= op_length; 1767 ctx->io_unit_offset += op_length; 1768 if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) { 1769 ctx->curr_payload += op_length * blob->bs->io_unit_size; 1770 } 1771 1772 switch (op_type) { 1773 case SPDK_BLOB_READ: 1774 spdk_blob_io_read(blob, ch, buf, offset, op_length, 1775 _spdk_blob_request_submit_op_split_next, ctx); 1776 break; 1777 case SPDK_BLOB_WRITE: 1778 spdk_blob_io_write(blob, ch, buf, offset, op_length, 1779 _spdk_blob_request_submit_op_split_next, ctx); 1780 break; 1781 case SPDK_BLOB_UNMAP: 1782 spdk_blob_io_unmap(blob, ch, offset, op_length, 1783 _spdk_blob_request_submit_op_split_next, ctx); 1784 break; 1785 case SPDK_BLOB_WRITE_ZEROES: 1786 spdk_blob_io_write_zeroes(blob, ch, offset, op_length, 1787 _spdk_blob_request_submit_op_split_next, ctx); 1788 break; 1789 case SPDK_BLOB_READV: 1790 case SPDK_BLOB_WRITEV: 1791 SPDK_ERRLOG("readv/write not valid for %s\n", __func__); 1792 spdk_bs_sequence_finish(ctx->seq, -EINVAL); 1793 free(ctx); 1794 break; 1795 } 1796 } 1797 1798 static void 1799 _spdk_blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob, 1800 void *payload, uint64_t offset, uint64_t length, 1801 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 1802 { 1803 struct op_split_ctx *ctx; 1804 spdk_bs_sequence_t *seq; 1805 struct spdk_bs_cpl cpl; 1806 1807 assert(blob != NULL); 1808 1809 ctx = calloc(1, sizeof(struct op_split_ctx)); 1810 if (ctx == NULL) { 1811 cb_fn(cb_arg, -ENOMEM); 1812 return; 1813 } 1814 1815 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 1816 cpl.u.blob_basic.cb_fn = cb_fn; 1817 cpl.u.blob_basic.cb_arg = cb_arg; 1818 1819 seq = spdk_bs_sequence_start(ch, &cpl); 1820 if (!seq) { 1821 free(ctx); 1822 cb_fn(cb_arg, -ENOMEM); 1823 return; 1824 } 1825 1826 ctx->blob = blob; 1827 ctx->channel = ch; 1828 ctx->curr_payload = payload; 1829 ctx->io_unit_offset = offset; 1830 ctx->io_units_remaining = length; 1831 ctx->op_type = op_type; 1832 ctx->seq = seq; 1833 1834 _spdk_blob_request_submit_op_split_next(ctx, 0); 1835 } 1836 1837 static void 1838 _spdk_blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob, 1839 void *payload, uint64_t offset, uint64_t length, 1840 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 1841 { 1842 struct spdk_bs_cpl cpl; 1843 uint64_t lba; 1844 uint32_t lba_count; 1845 1846 assert(blob != NULL); 1847 1848 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 1849 cpl.u.blob_basic.cb_fn = cb_fn; 1850 cpl.u.blob_basic.cb_arg = cb_arg; 1851 1852 _spdk_blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count); 1853 1854 if (blob->frozen_refcnt) { 1855 /* This blob I/O is frozen */ 1856 spdk_bs_user_op_t *op; 1857 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch); 1858 1859 op = spdk_bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length); 1860 if (!op) { 1861 cb_fn(cb_arg, -ENOMEM); 1862 return; 1863 } 1864 1865 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link); 1866 1867 return; 1868 } 1869 1870 switch (op_type) { 1871 case SPDK_BLOB_READ: { 1872 spdk_bs_batch_t *batch; 1873 1874 batch = spdk_bs_batch_open(_ch, &cpl); 1875 if (!batch) { 1876 cb_fn(cb_arg, -ENOMEM); 1877 return; 1878 } 1879 1880 if (_spdk_bs_io_unit_is_allocated(blob, offset)) { 1881 /* Read from the blob */ 1882 spdk_bs_batch_read_dev(batch, payload, lba, lba_count); 1883 } else { 1884 /* Read from the backing block device */ 1885 spdk_bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count); 1886 } 1887 1888 spdk_bs_batch_close(batch); 1889 break; 1890 } 1891 case SPDK_BLOB_WRITE: 1892 case SPDK_BLOB_WRITE_ZEROES: { 1893 if (_spdk_bs_io_unit_is_allocated(blob, offset)) { 1894 /* Write to the blob */ 1895 spdk_bs_batch_t *batch; 1896 1897 if (lba_count == 0) { 1898 cb_fn(cb_arg, 0); 1899 return; 1900 } 1901 1902 batch = spdk_bs_batch_open(_ch, &cpl); 1903 if (!batch) { 1904 cb_fn(cb_arg, -ENOMEM); 1905 return; 1906 } 1907 1908 if (op_type == SPDK_BLOB_WRITE) { 1909 spdk_bs_batch_write_dev(batch, payload, lba, lba_count); 1910 } else { 1911 spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count); 1912 } 1913 1914 spdk_bs_batch_close(batch); 1915 } else { 1916 /* Queue this operation and allocate the cluster */ 1917 spdk_bs_user_op_t *op; 1918 1919 op = spdk_bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length); 1920 if (!op) { 1921 cb_fn(cb_arg, -ENOMEM); 1922 return; 1923 } 1924 1925 _spdk_bs_allocate_and_copy_cluster(blob, _ch, offset, op); 1926 } 1927 break; 1928 } 1929 case SPDK_BLOB_UNMAP: { 1930 spdk_bs_batch_t *batch; 1931 1932 batch = spdk_bs_batch_open(_ch, &cpl); 1933 if (!batch) { 1934 cb_fn(cb_arg, -ENOMEM); 1935 return; 1936 } 1937 1938 if (_spdk_bs_io_unit_is_allocated(blob, offset)) { 1939 spdk_bs_batch_unmap_dev(batch, lba, lba_count); 1940 } 1941 1942 spdk_bs_batch_close(batch); 1943 break; 1944 } 1945 case SPDK_BLOB_READV: 1946 case SPDK_BLOB_WRITEV: 1947 SPDK_ERRLOG("readv/write not valid\n"); 1948 cb_fn(cb_arg, -EINVAL); 1949 break; 1950 } 1951 } 1952 1953 static void 1954 _spdk_blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel, 1955 void *payload, uint64_t offset, uint64_t length, 1956 spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type) 1957 { 1958 assert(blob != NULL); 1959 1960 if (blob->data_ro && op_type != SPDK_BLOB_READ) { 1961 cb_fn(cb_arg, -EPERM); 1962 return; 1963 } 1964 1965 if (offset + length > _spdk_bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) { 1966 cb_fn(cb_arg, -EINVAL); 1967 return; 1968 } 1969 if (length <= _spdk_bs_num_io_units_to_cluster_boundary(blob, offset)) { 1970 _spdk_blob_request_submit_op_single(_channel, blob, payload, offset, length, 1971 cb_fn, cb_arg, op_type); 1972 } else { 1973 _spdk_blob_request_submit_op_split(_channel, blob, payload, offset, length, 1974 cb_fn, cb_arg, op_type); 1975 } 1976 } 1977 1978 struct rw_iov_ctx { 1979 struct spdk_blob *blob; 1980 struct spdk_io_channel *channel; 1981 spdk_blob_op_complete cb_fn; 1982 void *cb_arg; 1983 bool read; 1984 int iovcnt; 1985 struct iovec *orig_iov; 1986 uint64_t io_unit_offset; 1987 uint64_t io_units_remaining; 1988 uint64_t io_units_done; 1989 struct iovec iov[0]; 1990 }; 1991 1992 static void 1993 _spdk_rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1994 { 1995 assert(cb_arg == NULL); 1996 spdk_bs_sequence_finish(seq, bserrno); 1997 } 1998 1999 static void 2000 _spdk_rw_iov_split_next(void *cb_arg, int bserrno) 2001 { 2002 struct rw_iov_ctx *ctx = cb_arg; 2003 struct spdk_blob *blob = ctx->blob; 2004 struct iovec *iov, *orig_iov; 2005 int iovcnt; 2006 size_t orig_iovoff; 2007 uint64_t io_units_count, io_units_to_boundary, io_unit_offset; 2008 uint64_t byte_count; 2009 2010 if (bserrno != 0 || ctx->io_units_remaining == 0) { 2011 ctx->cb_fn(ctx->cb_arg, bserrno); 2012 free(ctx); 2013 return; 2014 } 2015 2016 io_unit_offset = ctx->io_unit_offset; 2017 io_units_to_boundary = _spdk_bs_num_io_units_to_cluster_boundary(blob, io_unit_offset); 2018 io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary); 2019 /* 2020 * Get index and offset into the original iov array for our current position in the I/O sequence. 2021 * byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will 2022 * point to the current position in the I/O sequence. 2023 */ 2024 byte_count = ctx->io_units_done * blob->bs->io_unit_size; 2025 orig_iov = &ctx->orig_iov[0]; 2026 orig_iovoff = 0; 2027 while (byte_count > 0) { 2028 if (byte_count >= orig_iov->iov_len) { 2029 byte_count -= orig_iov->iov_len; 2030 orig_iov++; 2031 } else { 2032 orig_iovoff = byte_count; 2033 byte_count = 0; 2034 } 2035 } 2036 2037 /* 2038 * Build an iov array for the next I/O in the sequence. byte_count will keep track of how many 2039 * bytes of this next I/O remain to be accounted for in the new iov array. 2040 */ 2041 byte_count = io_units_count * blob->bs->io_unit_size; 2042 iov = &ctx->iov[0]; 2043 iovcnt = 0; 2044 while (byte_count > 0) { 2045 assert(iovcnt < ctx->iovcnt); 2046 iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff); 2047 iov->iov_base = orig_iov->iov_base + orig_iovoff; 2048 byte_count -= iov->iov_len; 2049 orig_iovoff = 0; 2050 orig_iov++; 2051 iov++; 2052 iovcnt++; 2053 } 2054 2055 ctx->io_unit_offset += io_units_count; 2056 ctx->io_units_remaining -= io_units_count; 2057 ctx->io_units_done += io_units_count; 2058 iov = &ctx->iov[0]; 2059 2060 if (ctx->read) { 2061 spdk_blob_io_readv(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset, 2062 io_units_count, _spdk_rw_iov_split_next, ctx); 2063 } else { 2064 spdk_blob_io_writev(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset, 2065 io_units_count, _spdk_rw_iov_split_next, ctx); 2066 } 2067 } 2068 2069 static void 2070 _spdk_blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel, 2071 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 2072 spdk_blob_op_complete cb_fn, void *cb_arg, bool read) 2073 { 2074 struct spdk_bs_cpl cpl; 2075 2076 assert(blob != NULL); 2077 2078 if (!read && blob->data_ro) { 2079 cb_fn(cb_arg, -EPERM); 2080 return; 2081 } 2082 2083 if (length == 0) { 2084 cb_fn(cb_arg, 0); 2085 return; 2086 } 2087 2088 if (offset + length > _spdk_bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) { 2089 cb_fn(cb_arg, -EINVAL); 2090 return; 2091 } 2092 2093 /* 2094 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having 2095 * to split a request that spans a cluster boundary. For I/O that do not span a cluster boundary, 2096 * there will be no noticeable difference compared to using a batch. For I/O that do span a cluster 2097 * boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need 2098 * to allocate a separate iov array and split the I/O such that none of the resulting 2099 * smaller I/O cross a cluster boundary. These smaller I/O will be issued in sequence (not in parallel) 2100 * but since this case happens very infrequently, any performance impact will be negligible. 2101 * 2102 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs 2103 * for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them 2104 * in a batch. That would also require creating an intermediate spdk_bs_cpl that would get called 2105 * when the batch was completed, to allow for freeing the memory for the iov arrays. 2106 */ 2107 if (spdk_likely(length <= _spdk_bs_num_io_units_to_cluster_boundary(blob, offset))) { 2108 uint32_t lba_count; 2109 uint64_t lba; 2110 2111 _spdk_blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count); 2112 2113 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2114 cpl.u.blob_basic.cb_fn = cb_fn; 2115 cpl.u.blob_basic.cb_arg = cb_arg; 2116 if (blob->frozen_refcnt) { 2117 /* This blob I/O is frozen */ 2118 spdk_bs_user_op_t *op; 2119 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel); 2120 2121 op = spdk_bs_user_op_alloc(_channel, &cpl, read, blob, iov, iovcnt, offset, length); 2122 if (!op) { 2123 cb_fn(cb_arg, -ENOMEM); 2124 return; 2125 } 2126 2127 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link); 2128 2129 return; 2130 } 2131 2132 if (read) { 2133 spdk_bs_sequence_t *seq; 2134 2135 seq = spdk_bs_sequence_start(_channel, &cpl); 2136 if (!seq) { 2137 cb_fn(cb_arg, -ENOMEM); 2138 return; 2139 } 2140 2141 if (_spdk_bs_io_unit_is_allocated(blob, offset)) { 2142 spdk_bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL); 2143 } else { 2144 spdk_bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count, 2145 _spdk_rw_iov_done, NULL); 2146 } 2147 } else { 2148 if (_spdk_bs_io_unit_is_allocated(blob, offset)) { 2149 spdk_bs_sequence_t *seq; 2150 2151 seq = spdk_bs_sequence_start(_channel, &cpl); 2152 if (!seq) { 2153 cb_fn(cb_arg, -ENOMEM); 2154 return; 2155 } 2156 2157 spdk_bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL); 2158 } else { 2159 /* Queue this operation and allocate the cluster */ 2160 spdk_bs_user_op_t *op; 2161 2162 op = spdk_bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset, 2163 length); 2164 if (!op) { 2165 cb_fn(cb_arg, -ENOMEM); 2166 return; 2167 } 2168 2169 _spdk_bs_allocate_and_copy_cluster(blob, _channel, offset, op); 2170 } 2171 } 2172 } else { 2173 struct rw_iov_ctx *ctx; 2174 2175 ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec)); 2176 if (ctx == NULL) { 2177 cb_fn(cb_arg, -ENOMEM); 2178 return; 2179 } 2180 2181 ctx->blob = blob; 2182 ctx->channel = _channel; 2183 ctx->cb_fn = cb_fn; 2184 ctx->cb_arg = cb_arg; 2185 ctx->read = read; 2186 ctx->orig_iov = iov; 2187 ctx->iovcnt = iovcnt; 2188 ctx->io_unit_offset = offset; 2189 ctx->io_units_remaining = length; 2190 ctx->io_units_done = 0; 2191 2192 _spdk_rw_iov_split_next(ctx, 0); 2193 } 2194 } 2195 2196 static struct spdk_blob * 2197 _spdk_blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid) 2198 { 2199 struct spdk_blob *blob; 2200 2201 TAILQ_FOREACH(blob, &bs->blobs, link) { 2202 if (blob->id == blobid) { 2203 return blob; 2204 } 2205 } 2206 2207 return NULL; 2208 } 2209 2210 static int 2211 _spdk_bs_channel_create(void *io_device, void *ctx_buf) 2212 { 2213 struct spdk_blob_store *bs = io_device; 2214 struct spdk_bs_channel *channel = ctx_buf; 2215 struct spdk_bs_dev *dev; 2216 uint32_t max_ops = bs->max_channel_ops; 2217 uint32_t i; 2218 2219 dev = bs->dev; 2220 2221 channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set)); 2222 if (!channel->req_mem) { 2223 return -1; 2224 } 2225 2226 TAILQ_INIT(&channel->reqs); 2227 2228 for (i = 0; i < max_ops; i++) { 2229 TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link); 2230 } 2231 2232 channel->bs = bs; 2233 channel->dev = dev; 2234 channel->dev_channel = dev->create_channel(dev); 2235 2236 if (!channel->dev_channel) { 2237 SPDK_ERRLOG("Failed to create device channel.\n"); 2238 free(channel->req_mem); 2239 return -1; 2240 } 2241 2242 TAILQ_INIT(&channel->need_cluster_alloc); 2243 TAILQ_INIT(&channel->queued_io); 2244 2245 return 0; 2246 } 2247 2248 static void 2249 _spdk_bs_channel_destroy(void *io_device, void *ctx_buf) 2250 { 2251 struct spdk_bs_channel *channel = ctx_buf; 2252 spdk_bs_user_op_t *op; 2253 2254 while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) { 2255 op = TAILQ_FIRST(&channel->need_cluster_alloc); 2256 TAILQ_REMOVE(&channel->need_cluster_alloc, op, link); 2257 spdk_bs_user_op_abort(op); 2258 } 2259 2260 while (!TAILQ_EMPTY(&channel->queued_io)) { 2261 op = TAILQ_FIRST(&channel->queued_io); 2262 TAILQ_REMOVE(&channel->queued_io, op, link); 2263 spdk_bs_user_op_abort(op); 2264 } 2265 2266 free(channel->req_mem); 2267 channel->dev->destroy_channel(channel->dev, channel->dev_channel); 2268 } 2269 2270 static void 2271 _spdk_bs_dev_destroy(void *io_device) 2272 { 2273 struct spdk_blob_store *bs = io_device; 2274 struct spdk_blob *blob, *blob_tmp; 2275 2276 bs->dev->destroy(bs->dev); 2277 2278 TAILQ_FOREACH_SAFE(blob, &bs->blobs, link, blob_tmp) { 2279 TAILQ_REMOVE(&bs->blobs, blob, link); 2280 _spdk_blob_free(blob); 2281 } 2282 2283 pthread_mutex_destroy(&bs->used_clusters_mutex); 2284 2285 spdk_bit_array_free(&bs->used_blobids); 2286 spdk_bit_array_free(&bs->used_md_pages); 2287 spdk_bit_array_free(&bs->used_clusters); 2288 /* 2289 * If this function is called for any reason except a successful unload, 2290 * the unload_cpl type will be NONE and this will be a nop. 2291 */ 2292 spdk_bs_call_cpl(&bs->unload_cpl, bs->unload_err); 2293 2294 free(bs); 2295 } 2296 2297 static int 2298 _spdk_bs_blob_list_add(struct spdk_blob *blob) 2299 { 2300 spdk_blob_id snapshot_id; 2301 struct spdk_blob_list *snapshot_entry = NULL; 2302 struct spdk_blob_list *clone_entry = NULL; 2303 2304 assert(blob != NULL); 2305 2306 snapshot_id = blob->parent_id; 2307 if (snapshot_id == SPDK_BLOBID_INVALID) { 2308 return 0; 2309 } 2310 2311 TAILQ_FOREACH(snapshot_entry, &blob->bs->snapshots, link) { 2312 if (snapshot_entry->id == snapshot_id) { 2313 break; 2314 } 2315 } 2316 2317 if (snapshot_entry == NULL) { 2318 /* Snapshot not found */ 2319 snapshot_entry = calloc(1, sizeof(struct spdk_blob_list)); 2320 if (snapshot_entry == NULL) { 2321 return -ENOMEM; 2322 } 2323 snapshot_entry->id = snapshot_id; 2324 TAILQ_INIT(&snapshot_entry->clones); 2325 TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link); 2326 } else { 2327 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 2328 if (clone_entry->id == blob->id) { 2329 break; 2330 } 2331 } 2332 } 2333 2334 if (clone_entry == NULL) { 2335 /* Clone not found */ 2336 clone_entry = calloc(1, sizeof(struct spdk_blob_list)); 2337 if (clone_entry == NULL) { 2338 return -ENOMEM; 2339 } 2340 clone_entry->id = blob->id; 2341 TAILQ_INIT(&clone_entry->clones); 2342 TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link); 2343 snapshot_entry->clone_count++; 2344 } 2345 2346 return 0; 2347 } 2348 2349 static int 2350 _spdk_bs_blob_list_remove(struct spdk_blob *blob) 2351 { 2352 struct spdk_blob_list *snapshot_entry = NULL; 2353 struct spdk_blob_list *clone_entry = NULL; 2354 spdk_blob_id snapshot_id; 2355 2356 assert(blob != NULL); 2357 2358 snapshot_id = blob->parent_id; 2359 if (snapshot_id == SPDK_BLOBID_INVALID) { 2360 return 0; 2361 } 2362 2363 TAILQ_FOREACH(snapshot_entry, &blob->bs->snapshots, link) { 2364 if (snapshot_entry->id == snapshot_id) { 2365 break; 2366 } 2367 } 2368 2369 assert(snapshot_entry != NULL); 2370 2371 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 2372 if (clone_entry->id == blob->id) { 2373 break; 2374 } 2375 } 2376 2377 assert(clone_entry != NULL); 2378 2379 blob->parent_id = SPDK_BLOBID_INVALID; 2380 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 2381 free(clone_entry); 2382 2383 snapshot_entry->clone_count--; 2384 2385 return 0; 2386 } 2387 2388 static int 2389 _spdk_bs_blob_list_free(struct spdk_blob_store *bs) 2390 { 2391 struct spdk_blob_list *snapshot_entry; 2392 struct spdk_blob_list *snapshot_entry_tmp; 2393 struct spdk_blob_list *clone_entry; 2394 struct spdk_blob_list *clone_entry_tmp; 2395 2396 TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) { 2397 TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) { 2398 TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link); 2399 free(clone_entry); 2400 } 2401 TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link); 2402 free(snapshot_entry); 2403 } 2404 2405 return 0; 2406 } 2407 2408 static void 2409 _spdk_bs_free(struct spdk_blob_store *bs) 2410 { 2411 _spdk_bs_blob_list_free(bs); 2412 2413 spdk_bs_unregister_md_thread(bs); 2414 spdk_io_device_unregister(bs, _spdk_bs_dev_destroy); 2415 } 2416 2417 void 2418 spdk_bs_opts_init(struct spdk_bs_opts *opts) 2419 { 2420 opts->cluster_sz = SPDK_BLOB_OPTS_CLUSTER_SZ; 2421 opts->num_md_pages = SPDK_BLOB_OPTS_NUM_MD_PAGES; 2422 opts->max_md_ops = SPDK_BLOB_OPTS_MAX_MD_OPS; 2423 opts->max_channel_ops = SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS; 2424 memset(&opts->bstype, 0, sizeof(opts->bstype)); 2425 opts->iter_cb_fn = NULL; 2426 opts->iter_cb_arg = NULL; 2427 } 2428 2429 static int 2430 _spdk_bs_opts_verify(struct spdk_bs_opts *opts) 2431 { 2432 if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 || 2433 opts->max_channel_ops == 0) { 2434 SPDK_ERRLOG("Blobstore options cannot be set to 0\n"); 2435 return -1; 2436 } 2437 2438 return 0; 2439 } 2440 2441 static int 2442 _spdk_bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs) 2443 { 2444 struct spdk_blob_store *bs; 2445 uint64_t dev_size; 2446 int rc; 2447 2448 dev_size = dev->blocklen * dev->blockcnt; 2449 if (dev_size < opts->cluster_sz) { 2450 /* Device size cannot be smaller than cluster size of blobstore */ 2451 SPDK_INFOLOG(SPDK_LOG_BLOB, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n", 2452 dev_size, opts->cluster_sz); 2453 return -ENOSPC; 2454 } 2455 if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) { 2456 /* Cluster size cannot be smaller than page size */ 2457 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n", 2458 opts->cluster_sz, SPDK_BS_PAGE_SIZE); 2459 return -EINVAL; 2460 } 2461 bs = calloc(1, sizeof(struct spdk_blob_store)); 2462 if (!bs) { 2463 return -ENOMEM; 2464 } 2465 2466 TAILQ_INIT(&bs->blobs); 2467 TAILQ_INIT(&bs->snapshots); 2468 bs->dev = dev; 2469 bs->md_thread = spdk_get_thread(); 2470 assert(bs->md_thread != NULL); 2471 2472 /* 2473 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an 2474 * even multiple of the cluster size. 2475 */ 2476 bs->cluster_sz = opts->cluster_sz; 2477 bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen); 2478 bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE; 2479 bs->num_free_clusters = bs->total_clusters; 2480 bs->used_clusters = spdk_bit_array_create(bs->total_clusters); 2481 bs->io_unit_size = dev->blocklen; 2482 if (bs->used_clusters == NULL) { 2483 free(bs); 2484 return -ENOMEM; 2485 } 2486 2487 bs->max_channel_ops = opts->max_channel_ops; 2488 bs->super_blob = SPDK_BLOBID_INVALID; 2489 memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype)); 2490 2491 /* The metadata is assumed to be at least 1 page */ 2492 bs->used_md_pages = spdk_bit_array_create(1); 2493 bs->used_blobids = spdk_bit_array_create(0); 2494 2495 pthread_mutex_init(&bs->used_clusters_mutex, NULL); 2496 2497 spdk_io_device_register(bs, _spdk_bs_channel_create, _spdk_bs_channel_destroy, 2498 sizeof(struct spdk_bs_channel), "blobstore"); 2499 rc = spdk_bs_register_md_thread(bs); 2500 if (rc == -1) { 2501 spdk_io_device_unregister(bs, NULL); 2502 pthread_mutex_destroy(&bs->used_clusters_mutex); 2503 spdk_bit_array_free(&bs->used_blobids); 2504 spdk_bit_array_free(&bs->used_md_pages); 2505 spdk_bit_array_free(&bs->used_clusters); 2506 free(bs); 2507 /* FIXME: this is a lie but don't know how to get a proper error code here */ 2508 return -ENOMEM; 2509 } 2510 2511 *_bs = bs; 2512 return 0; 2513 } 2514 2515 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */ 2516 2517 struct spdk_bs_load_ctx { 2518 struct spdk_blob_store *bs; 2519 struct spdk_bs_super_block *super; 2520 2521 struct spdk_bs_md_mask *mask; 2522 bool in_page_chain; 2523 uint32_t page_index; 2524 uint32_t cur_page; 2525 struct spdk_blob_md_page *page; 2526 2527 spdk_bs_sequence_t *seq; 2528 spdk_blob_op_with_handle_complete iter_cb_fn; 2529 void *iter_cb_arg; 2530 }; 2531 2532 static void 2533 _spdk_bs_load_ctx_fail(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 2534 { 2535 assert(bserrno != 0); 2536 2537 spdk_dma_free(ctx->super); 2538 spdk_bs_sequence_finish(seq, bserrno); 2539 _spdk_bs_free(ctx->bs); 2540 free(ctx); 2541 } 2542 2543 static void 2544 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask) 2545 { 2546 uint32_t i = 0; 2547 2548 while (true) { 2549 i = spdk_bit_array_find_first_set(array, i); 2550 if (i >= mask->length) { 2551 break; 2552 } 2553 mask->mask[i / 8] |= 1U << (i % 8); 2554 i++; 2555 } 2556 } 2557 2558 static int 2559 _spdk_bs_load_mask(struct spdk_bit_array **array_ptr, struct spdk_bs_md_mask *mask) 2560 { 2561 struct spdk_bit_array *array; 2562 uint32_t i; 2563 2564 if (spdk_bit_array_resize(array_ptr, mask->length) < 0) { 2565 return -ENOMEM; 2566 } 2567 2568 array = *array_ptr; 2569 for (i = 0; i < mask->length; i++) { 2570 if (mask->mask[i / 8] & (1U << (i % 8))) { 2571 spdk_bit_array_set(array, i); 2572 } 2573 } 2574 2575 return 0; 2576 } 2577 2578 static void 2579 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 2580 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 2581 { 2582 /* Update the values in the super block */ 2583 super->super_blob = bs->super_blob; 2584 memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype)); 2585 super->crc = _spdk_blob_md_page_calc_crc(super); 2586 spdk_bs_sequence_write_dev(seq, super, _spdk_bs_page_to_lba(bs, 0), 2587 _spdk_bs_byte_to_lba(bs, sizeof(*super)), 2588 cb_fn, cb_arg); 2589 } 2590 2591 static void 2592 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 2593 { 2594 struct spdk_bs_load_ctx *ctx = arg; 2595 uint64_t mask_size, lba, lba_count; 2596 2597 /* Write out the used clusters mask */ 2598 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 2599 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2600 if (!ctx->mask) { 2601 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2602 return; 2603 } 2604 2605 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS; 2606 ctx->mask->length = ctx->bs->total_clusters; 2607 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters)); 2608 2609 _spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask); 2610 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 2611 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 2612 spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 2613 } 2614 2615 static void 2616 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 2617 { 2618 struct spdk_bs_load_ctx *ctx = arg; 2619 uint64_t mask_size, lba, lba_count; 2620 2621 if (seq->bserrno) { 2622 _spdk_bs_load_ctx_fail(seq, ctx, seq->bserrno); 2623 return; 2624 } 2625 2626 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 2627 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2628 if (!ctx->mask) { 2629 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2630 return; 2631 } 2632 2633 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES; 2634 ctx->mask->length = ctx->super->md_len; 2635 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages)); 2636 2637 _spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask); 2638 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 2639 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 2640 spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 2641 } 2642 2643 static void 2644 _spdk_bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 2645 { 2646 struct spdk_bs_load_ctx *ctx = arg; 2647 uint64_t mask_size, lba, lba_count; 2648 2649 if (ctx->super->used_blobid_mask_len == 0) { 2650 /* 2651 * This is a pre-v3 on-disk format where the blobid mask does not get 2652 * written to disk. 2653 */ 2654 cb_fn(seq, arg, 0); 2655 return; 2656 } 2657 2658 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 2659 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2660 if (!ctx->mask) { 2661 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2662 return; 2663 } 2664 2665 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS; 2666 ctx->mask->length = ctx->super->md_len; 2667 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids)); 2668 2669 _spdk_bs_set_mask(ctx->bs->used_blobids, ctx->mask); 2670 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 2671 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 2672 spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 2673 } 2674 2675 static void 2676 _spdk_bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno) 2677 { 2678 struct spdk_bs_load_ctx *ctx = arg; 2679 2680 if (bserrno == 0) { 2681 if (ctx->iter_cb_fn) { 2682 ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0); 2683 } 2684 _spdk_bs_blob_list_add(blob); 2685 spdk_bs_iter_next(ctx->bs, blob, _spdk_bs_load_iter, ctx); 2686 return; 2687 } 2688 2689 if (bserrno == -ENOENT) { 2690 bserrno = 0; 2691 } else { 2692 /* 2693 * This case needs to be looked at further. Same problem 2694 * exists with applications that rely on explicit blob 2695 * iteration. We should just skip the blob that failed 2696 * to load and continue on to the next one. 2697 */ 2698 SPDK_ERRLOG("Error in iterating blobs\n"); 2699 } 2700 2701 ctx->iter_cb_fn = NULL; 2702 2703 spdk_dma_free(ctx->super); 2704 spdk_dma_free(ctx->mask); 2705 spdk_bs_sequence_finish(ctx->seq, bserrno); 2706 free(ctx); 2707 } 2708 2709 static void 2710 _spdk_bs_load_complete(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 2711 { 2712 ctx->seq = seq; 2713 spdk_bs_iter_first(ctx->bs, _spdk_bs_load_iter, ctx); 2714 } 2715 2716 static void 2717 _spdk_bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2718 { 2719 struct spdk_bs_load_ctx *ctx = cb_arg; 2720 int rc; 2721 2722 /* The type must be correct */ 2723 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS); 2724 2725 /* The length of the mask (in bits) must not be greater than 2726 * the length of the buffer (converted to bits) */ 2727 assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8)); 2728 2729 /* The length of the mask must be exactly equal to the size 2730 * (in pages) of the metadata region */ 2731 assert(ctx->mask->length == ctx->super->md_len); 2732 2733 rc = _spdk_bs_load_mask(&ctx->bs->used_blobids, ctx->mask); 2734 if (rc < 0) { 2735 spdk_dma_free(ctx->mask); 2736 _spdk_bs_load_ctx_fail(seq, ctx, rc); 2737 return; 2738 } 2739 2740 _spdk_bs_load_complete(seq, ctx, bserrno); 2741 } 2742 2743 static void 2744 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2745 { 2746 struct spdk_bs_load_ctx *ctx = cb_arg; 2747 uint64_t lba, lba_count, mask_size; 2748 int rc; 2749 2750 /* The type must be correct */ 2751 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 2752 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 2753 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 2754 struct spdk_blob_md_page) * 8)); 2755 /* The length of the mask must be exactly equal to the total number of clusters */ 2756 assert(ctx->mask->length == ctx->bs->total_clusters); 2757 2758 rc = _spdk_bs_load_mask(&ctx->bs->used_clusters, ctx->mask); 2759 if (rc < 0) { 2760 spdk_dma_free(ctx->mask); 2761 _spdk_bs_load_ctx_fail(seq, ctx, rc); 2762 return; 2763 } 2764 2765 ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->bs->used_clusters); 2766 assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters); 2767 2768 spdk_dma_free(ctx->mask); 2769 2770 /* Read the used blobids mask */ 2771 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 2772 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2773 if (!ctx->mask) { 2774 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2775 return; 2776 } 2777 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 2778 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 2779 spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 2780 _spdk_bs_load_used_blobids_cpl, ctx); 2781 } 2782 2783 static void 2784 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2785 { 2786 struct spdk_bs_load_ctx *ctx = cb_arg; 2787 uint64_t lba, lba_count, mask_size; 2788 int rc; 2789 2790 /* The type must be correct */ 2791 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES); 2792 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 2793 assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE * 2794 8)); 2795 /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */ 2796 assert(ctx->mask->length == ctx->super->md_len); 2797 2798 rc = _spdk_bs_load_mask(&ctx->bs->used_md_pages, ctx->mask); 2799 if (rc < 0) { 2800 spdk_dma_free(ctx->mask); 2801 _spdk_bs_load_ctx_fail(seq, ctx, rc); 2802 return; 2803 } 2804 2805 spdk_dma_free(ctx->mask); 2806 2807 /* Read the used clusters mask */ 2808 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 2809 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2810 if (!ctx->mask) { 2811 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2812 return; 2813 } 2814 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 2815 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 2816 spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 2817 _spdk_bs_load_used_clusters_cpl, ctx); 2818 } 2819 2820 static void 2821 _spdk_bs_load_read_used_pages(spdk_bs_sequence_t *seq, void *cb_arg) 2822 { 2823 struct spdk_bs_load_ctx *ctx = cb_arg; 2824 uint64_t lba, lba_count, mask_size; 2825 2826 /* Read the used pages mask */ 2827 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 2828 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2829 if (!ctx->mask) { 2830 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2831 return; 2832 } 2833 2834 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 2835 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 2836 spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 2837 _spdk_bs_load_used_pages_cpl, ctx); 2838 } 2839 2840 static int 2841 _spdk_bs_load_replay_md_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob_store *bs) 2842 { 2843 struct spdk_blob_md_descriptor *desc; 2844 size_t cur_desc = 0; 2845 2846 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 2847 while (cur_desc < sizeof(page->descriptors)) { 2848 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 2849 if (desc->length == 0) { 2850 /* If padding and length are 0, this terminates the page */ 2851 break; 2852 } 2853 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 2854 struct spdk_blob_md_descriptor_extent *desc_extent; 2855 unsigned int i, j; 2856 unsigned int cluster_count = 0; 2857 uint32_t cluster_idx; 2858 2859 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 2860 2861 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 2862 for (j = 0; j < desc_extent->extents[i].length; j++) { 2863 cluster_idx = desc_extent->extents[i].cluster_idx; 2864 /* 2865 * cluster_idx = 0 means an unallocated cluster - don't mark that 2866 * in the used cluster map. 2867 */ 2868 if (cluster_idx != 0) { 2869 spdk_bit_array_set(bs->used_clusters, cluster_idx + j); 2870 if (bs->num_free_clusters == 0) { 2871 return -ENOSPC; 2872 } 2873 bs->num_free_clusters--; 2874 } 2875 cluster_count++; 2876 } 2877 } 2878 if (cluster_count == 0) { 2879 return -EINVAL; 2880 } 2881 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 2882 /* Skip this item */ 2883 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 2884 /* Skip this item */ 2885 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 2886 /* Skip this item */ 2887 } else { 2888 /* Error */ 2889 return -EINVAL; 2890 } 2891 /* Advance to the next descriptor */ 2892 cur_desc += sizeof(*desc) + desc->length; 2893 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 2894 break; 2895 } 2896 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 2897 } 2898 return 0; 2899 } 2900 2901 static bool _spdk_bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx) 2902 { 2903 uint32_t crc; 2904 2905 crc = _spdk_blob_md_page_calc_crc(ctx->page); 2906 if (crc != ctx->page->crc) { 2907 return false; 2908 } 2909 2910 if (_spdk_bs_page_to_blobid(ctx->cur_page) != ctx->page->id) { 2911 return false; 2912 } 2913 return true; 2914 } 2915 2916 static void 2917 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 2918 2919 static void 2920 _spdk_bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2921 { 2922 struct spdk_bs_load_ctx *ctx = cb_arg; 2923 2924 _spdk_bs_load_complete(seq, ctx, bserrno); 2925 } 2926 2927 static void 2928 _spdk_bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2929 { 2930 struct spdk_bs_load_ctx *ctx = cb_arg; 2931 2932 spdk_dma_free(ctx->mask); 2933 ctx->mask = NULL; 2934 2935 _spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_load_write_used_clusters_cpl); 2936 } 2937 2938 static void 2939 _spdk_bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2940 { 2941 struct spdk_bs_load_ctx *ctx = cb_arg; 2942 2943 spdk_dma_free(ctx->mask); 2944 ctx->mask = NULL; 2945 2946 _spdk_bs_write_used_blobids(seq, cb_arg, _spdk_bs_load_write_used_blobids_cpl); 2947 } 2948 2949 static void 2950 _spdk_bs_load_write_used_md(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2951 { 2952 _spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_load_write_used_pages_cpl); 2953 } 2954 2955 static void 2956 _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2957 { 2958 struct spdk_bs_load_ctx *ctx = cb_arg; 2959 uint64_t num_md_clusters; 2960 uint64_t i; 2961 uint32_t page_num; 2962 2963 if (bserrno != 0) { 2964 _spdk_bs_load_ctx_fail(seq, ctx, bserrno); 2965 return; 2966 } 2967 2968 page_num = ctx->cur_page; 2969 if (_spdk_bs_load_cur_md_page_valid(ctx) == true) { 2970 if (ctx->page->sequence_num == 0 || ctx->in_page_chain == true) { 2971 spdk_bit_array_set(ctx->bs->used_md_pages, page_num); 2972 if (ctx->page->sequence_num == 0) { 2973 spdk_bit_array_set(ctx->bs->used_blobids, page_num); 2974 } 2975 if (_spdk_bs_load_replay_md_parse_page(ctx->page, ctx->bs)) { 2976 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 2977 return; 2978 } 2979 if (ctx->page->next != SPDK_INVALID_MD_PAGE) { 2980 ctx->in_page_chain = true; 2981 ctx->cur_page = ctx->page->next; 2982 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 2983 return; 2984 } 2985 } 2986 } 2987 2988 ctx->in_page_chain = false; 2989 2990 do { 2991 ctx->page_index++; 2992 } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true); 2993 2994 if (ctx->page_index < ctx->super->md_len) { 2995 ctx->cur_page = ctx->page_index; 2996 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 2997 } else { 2998 /* Claim all of the clusters used by the metadata */ 2999 num_md_clusters = spdk_divide_round_up(ctx->super->md_len, ctx->bs->pages_per_cluster); 3000 for (i = 0; i < num_md_clusters; i++) { 3001 _spdk_bs_claim_cluster(ctx->bs, i); 3002 } 3003 spdk_dma_free(ctx->page); 3004 _spdk_bs_load_write_used_md(seq, ctx, bserrno); 3005 } 3006 } 3007 3008 static void 3009 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 3010 { 3011 struct spdk_bs_load_ctx *ctx = cb_arg; 3012 uint64_t lba; 3013 3014 assert(ctx->cur_page < ctx->super->md_len); 3015 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 3016 spdk_bs_sequence_read_dev(seq, ctx->page, lba, 3017 _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 3018 _spdk_bs_load_replay_md_cpl, ctx); 3019 } 3020 3021 static void 3022 _spdk_bs_load_replay_md(spdk_bs_sequence_t *seq, void *cb_arg) 3023 { 3024 struct spdk_bs_load_ctx *ctx = cb_arg; 3025 3026 ctx->page_index = 0; 3027 ctx->cur_page = 0; 3028 ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE, 3029 SPDK_BS_PAGE_SIZE, 3030 NULL); 3031 if (!ctx->page) { 3032 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3033 return; 3034 } 3035 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 3036 } 3037 3038 static void 3039 _spdk_bs_recover(spdk_bs_sequence_t *seq, void *cb_arg) 3040 { 3041 struct spdk_bs_load_ctx *ctx = cb_arg; 3042 int rc; 3043 3044 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); 3045 if (rc < 0) { 3046 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3047 return; 3048 } 3049 3050 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len); 3051 if (rc < 0) { 3052 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3053 return; 3054 } 3055 3056 rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); 3057 if (rc < 0) { 3058 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3059 return; 3060 } 3061 3062 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 3063 _spdk_bs_load_replay_md(seq, cb_arg); 3064 } 3065 3066 static void 3067 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3068 { 3069 struct spdk_bs_load_ctx *ctx = cb_arg; 3070 uint32_t crc; 3071 int rc; 3072 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 3073 3074 if (ctx->super->version > SPDK_BS_VERSION || 3075 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 3076 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3077 return; 3078 } 3079 3080 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 3081 sizeof(ctx->super->signature)) != 0) { 3082 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3083 return; 3084 } 3085 3086 crc = _spdk_blob_md_page_calc_crc(ctx->super); 3087 if (crc != ctx->super->crc) { 3088 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3089 return; 3090 } 3091 3092 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 3093 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype matched - loading blobstore\n"); 3094 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 3095 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n"); 3096 } else { 3097 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Unexpected bstype\n"); 3098 SPDK_LOGDUMP(SPDK_LOG_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 3099 SPDK_LOGDUMP(SPDK_LOG_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 3100 _spdk_bs_load_ctx_fail(seq, ctx, -ENXIO); 3101 return; 3102 } 3103 3104 if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) { 3105 SPDK_NOTICELOG("Size mismatch, dev size: %lu, blobstore size: %lu\n", 3106 ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size); 3107 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3108 return; 3109 } 3110 3111 if (ctx->super->size == 0) { 3112 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 3113 } 3114 3115 if (ctx->super->io_unit_size == 0) { 3116 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE; 3117 } 3118 3119 /* Parse the super block */ 3120 ctx->bs->clean = 1; 3121 ctx->bs->cluster_sz = ctx->super->cluster_size; 3122 ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size; 3123 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 3124 ctx->bs->io_unit_size = ctx->super->io_unit_size; 3125 rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); 3126 if (rc < 0) { 3127 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3128 return; 3129 } 3130 ctx->bs->md_start = ctx->super->md_start; 3131 ctx->bs->md_len = ctx->super->md_len; 3132 ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up( 3133 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 3134 ctx->bs->super_blob = ctx->super->super_blob; 3135 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 3136 3137 if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) { 3138 _spdk_bs_recover(seq, ctx); 3139 } else { 3140 _spdk_bs_load_read_used_pages(seq, ctx); 3141 } 3142 } 3143 3144 void 3145 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 3146 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 3147 { 3148 struct spdk_blob_store *bs; 3149 struct spdk_bs_cpl cpl; 3150 spdk_bs_sequence_t *seq; 3151 struct spdk_bs_load_ctx *ctx; 3152 struct spdk_bs_opts opts = {}; 3153 int err; 3154 3155 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Loading blobstore from dev %p\n", dev); 3156 3157 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 3158 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "unsupported dev block length of %d\n", dev->blocklen); 3159 dev->destroy(dev); 3160 cb_fn(cb_arg, NULL, -EINVAL); 3161 return; 3162 } 3163 3164 if (o) { 3165 opts = *o; 3166 } else { 3167 spdk_bs_opts_init(&opts); 3168 } 3169 3170 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 3171 dev->destroy(dev); 3172 cb_fn(cb_arg, NULL, -EINVAL); 3173 return; 3174 } 3175 3176 err = _spdk_bs_alloc(dev, &opts, &bs); 3177 if (err) { 3178 dev->destroy(dev); 3179 cb_fn(cb_arg, NULL, err); 3180 return; 3181 } 3182 3183 ctx = calloc(1, sizeof(*ctx)); 3184 if (!ctx) { 3185 _spdk_bs_free(bs); 3186 cb_fn(cb_arg, NULL, -ENOMEM); 3187 return; 3188 } 3189 3190 ctx->bs = bs; 3191 ctx->iter_cb_fn = opts.iter_cb_fn; 3192 ctx->iter_cb_arg = opts.iter_cb_arg; 3193 3194 /* Allocate memory for the super block */ 3195 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3196 if (!ctx->super) { 3197 free(ctx); 3198 _spdk_bs_free(bs); 3199 cb_fn(cb_arg, NULL, -ENOMEM); 3200 return; 3201 } 3202 3203 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 3204 cpl.u.bs_handle.cb_fn = cb_fn; 3205 cpl.u.bs_handle.cb_arg = cb_arg; 3206 cpl.u.bs_handle.bs = bs; 3207 3208 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3209 if (!seq) { 3210 spdk_dma_free(ctx->super); 3211 free(ctx); 3212 _spdk_bs_free(bs); 3213 cb_fn(cb_arg, NULL, -ENOMEM); 3214 return; 3215 } 3216 3217 /* Read the super block */ 3218 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3219 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3220 _spdk_bs_load_super_cpl, ctx); 3221 } 3222 3223 /* END spdk_bs_load */ 3224 3225 /* START spdk_bs_dump */ 3226 3227 struct spdk_bs_dump_ctx { 3228 struct spdk_blob_store *bs; 3229 struct spdk_bs_super_block *super; 3230 uint32_t cur_page; 3231 struct spdk_blob_md_page *page; 3232 spdk_bs_sequence_t *seq; 3233 FILE *fp; 3234 spdk_bs_dump_print_xattr print_xattr_fn; 3235 char xattr_name[4096]; 3236 }; 3237 3238 static void 3239 _spdk_bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_dump_ctx *ctx, int bserrno) 3240 { 3241 spdk_dma_free(ctx->super); 3242 3243 /* 3244 * We need to defer calling spdk_bs_call_cpl() until after 3245 * dev destruction, so tuck these away for later use. 3246 */ 3247 ctx->bs->unload_err = bserrno; 3248 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 3249 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 3250 3251 spdk_bs_sequence_finish(seq, 0); 3252 _spdk_bs_free(ctx->bs); 3253 free(ctx); 3254 } 3255 3256 static void _spdk_bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 3257 3258 static void 3259 _spdk_bs_dump_print_md_page(struct spdk_bs_dump_ctx *ctx) 3260 { 3261 uint32_t page_idx = ctx->cur_page; 3262 struct spdk_blob_md_page *page = ctx->page; 3263 struct spdk_blob_md_descriptor *desc; 3264 size_t cur_desc = 0; 3265 uint32_t crc; 3266 3267 fprintf(ctx->fp, "=========\n"); 3268 fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx); 3269 fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id); 3270 3271 crc = _spdk_blob_md_page_calc_crc(page); 3272 fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch"); 3273 3274 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 3275 while (cur_desc < sizeof(page->descriptors)) { 3276 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 3277 if (desc->length == 0) { 3278 /* If padding and length are 0, this terminates the page */ 3279 break; 3280 } 3281 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 3282 struct spdk_blob_md_descriptor_extent *desc_extent; 3283 unsigned int i; 3284 3285 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 3286 3287 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 3288 if (desc_extent->extents[i].cluster_idx != 0) { 3289 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 3290 desc_extent->extents[i].cluster_idx); 3291 } else { 3292 fprintf(ctx->fp, "Unallocated Extent - "); 3293 } 3294 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent->extents[i].length); 3295 fprintf(ctx->fp, "\n"); 3296 } 3297 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 3298 struct spdk_blob_md_descriptor_xattr *desc_xattr; 3299 uint32_t i; 3300 3301 desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc; 3302 3303 if (desc_xattr->length != 3304 sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) + 3305 desc_xattr->name_length + desc_xattr->value_length) { 3306 } 3307 3308 memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length); 3309 ctx->xattr_name[desc_xattr->name_length] = '\0'; 3310 fprintf(ctx->fp, "XATTR: name = \"%s\"\n", ctx->xattr_name); 3311 fprintf(ctx->fp, " value = \""); 3312 ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name, 3313 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 3314 desc_xattr->value_length); 3315 fprintf(ctx->fp, "\"\n"); 3316 for (i = 0; i < desc_xattr->value_length; i++) { 3317 if (i % 16 == 0) { 3318 fprintf(ctx->fp, " "); 3319 } 3320 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i)); 3321 if ((i + 1) % 16 == 0) { 3322 fprintf(ctx->fp, "\n"); 3323 } 3324 } 3325 if (i % 16 != 0) { 3326 fprintf(ctx->fp, "\n"); 3327 } 3328 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 3329 /* TODO */ 3330 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 3331 /* TODO */ 3332 } else { 3333 /* Error */ 3334 } 3335 /* Advance to the next descriptor */ 3336 cur_desc += sizeof(*desc) + desc->length; 3337 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 3338 break; 3339 } 3340 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 3341 } 3342 } 3343 3344 static void 3345 _spdk_bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3346 { 3347 struct spdk_bs_dump_ctx *ctx = cb_arg; 3348 3349 if (bserrno != 0) { 3350 _spdk_bs_dump_finish(seq, ctx, bserrno); 3351 return; 3352 } 3353 3354 if (ctx->page->id != 0) { 3355 _spdk_bs_dump_print_md_page(ctx); 3356 } 3357 3358 ctx->cur_page++; 3359 3360 if (ctx->cur_page < ctx->super->md_len) { 3361 _spdk_bs_dump_read_md_page(seq, cb_arg); 3362 } else { 3363 spdk_dma_free(ctx->page); 3364 _spdk_bs_dump_finish(seq, ctx, 0); 3365 } 3366 } 3367 3368 static void 3369 _spdk_bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 3370 { 3371 struct spdk_bs_dump_ctx *ctx = cb_arg; 3372 uint64_t lba; 3373 3374 assert(ctx->cur_page < ctx->super->md_len); 3375 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 3376 spdk_bs_sequence_read_dev(seq, ctx->page, lba, 3377 _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 3378 _spdk_bs_dump_read_md_page_cpl, ctx); 3379 } 3380 3381 static void 3382 _spdk_bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3383 { 3384 struct spdk_bs_dump_ctx *ctx = cb_arg; 3385 3386 fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature); 3387 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 3388 sizeof(ctx->super->signature)) != 0) { 3389 fprintf(ctx->fp, "(Mismatch)\n"); 3390 _spdk_bs_dump_finish(seq, ctx, bserrno); 3391 return; 3392 } else { 3393 fprintf(ctx->fp, "(OK)\n"); 3394 } 3395 fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version); 3396 fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc, 3397 (ctx->super->crc == _spdk_blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch"); 3398 fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype); 3399 fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size); 3400 fprintf(ctx->fp, "Super Blob ID: "); 3401 if (ctx->super->super_blob == SPDK_BLOBID_INVALID) { 3402 fprintf(ctx->fp, "(None)\n"); 3403 } else { 3404 fprintf(ctx->fp, "%" PRIu64 "\n", ctx->super->super_blob); 3405 } 3406 fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean); 3407 fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start); 3408 fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len); 3409 fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start); 3410 fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len); 3411 fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start); 3412 fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len); 3413 fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start); 3414 fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len); 3415 3416 ctx->cur_page = 0; 3417 ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE, 3418 SPDK_BS_PAGE_SIZE, 3419 NULL); 3420 if (!ctx->page) { 3421 _spdk_bs_dump_finish(seq, ctx, -ENOMEM); 3422 return; 3423 } 3424 _spdk_bs_dump_read_md_page(seq, cb_arg); 3425 } 3426 3427 void 3428 spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn, 3429 spdk_bs_op_complete cb_fn, void *cb_arg) 3430 { 3431 struct spdk_blob_store *bs; 3432 struct spdk_bs_cpl cpl; 3433 spdk_bs_sequence_t *seq; 3434 struct spdk_bs_dump_ctx *ctx; 3435 struct spdk_bs_opts opts = {}; 3436 int err; 3437 3438 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Dumping blobstore from dev %p\n", dev); 3439 3440 spdk_bs_opts_init(&opts); 3441 3442 err = _spdk_bs_alloc(dev, &opts, &bs); 3443 if (err) { 3444 dev->destroy(dev); 3445 cb_fn(cb_arg, err); 3446 return; 3447 } 3448 3449 ctx = calloc(1, sizeof(*ctx)); 3450 if (!ctx) { 3451 _spdk_bs_free(bs); 3452 cb_fn(cb_arg, -ENOMEM); 3453 return; 3454 } 3455 3456 ctx->bs = bs; 3457 ctx->fp = fp; 3458 ctx->print_xattr_fn = print_xattr_fn; 3459 3460 /* Allocate memory for the super block */ 3461 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3462 if (!ctx->super) { 3463 free(ctx); 3464 _spdk_bs_free(bs); 3465 cb_fn(cb_arg, -ENOMEM); 3466 return; 3467 } 3468 3469 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3470 cpl.u.bs_basic.cb_fn = cb_fn; 3471 cpl.u.bs_basic.cb_arg = cb_arg; 3472 3473 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3474 if (!seq) { 3475 spdk_dma_free(ctx->super); 3476 free(ctx); 3477 _spdk_bs_free(bs); 3478 cb_fn(cb_arg, -ENOMEM); 3479 return; 3480 } 3481 3482 /* Read the super block */ 3483 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3484 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3485 _spdk_bs_dump_super_cpl, ctx); 3486 } 3487 3488 /* END spdk_bs_dump */ 3489 3490 /* START spdk_bs_init */ 3491 3492 struct spdk_bs_init_ctx { 3493 struct spdk_blob_store *bs; 3494 struct spdk_bs_super_block *super; 3495 }; 3496 3497 static void 3498 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3499 { 3500 struct spdk_bs_init_ctx *ctx = cb_arg; 3501 3502 spdk_dma_free(ctx->super); 3503 free(ctx); 3504 3505 spdk_bs_sequence_finish(seq, bserrno); 3506 } 3507 3508 static void 3509 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3510 { 3511 struct spdk_bs_init_ctx *ctx = cb_arg; 3512 3513 /* Write super block */ 3514 spdk_bs_sequence_write_dev(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0), 3515 _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 3516 _spdk_bs_init_persist_super_cpl, ctx); 3517 } 3518 3519 void 3520 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 3521 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 3522 { 3523 struct spdk_bs_init_ctx *ctx; 3524 struct spdk_blob_store *bs; 3525 struct spdk_bs_cpl cpl; 3526 spdk_bs_sequence_t *seq; 3527 spdk_bs_batch_t *batch; 3528 uint64_t num_md_lba; 3529 uint64_t num_md_pages; 3530 uint64_t num_md_clusters; 3531 uint32_t i; 3532 struct spdk_bs_opts opts = {}; 3533 int rc; 3534 3535 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Initializing blobstore on dev %p\n", dev); 3536 3537 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 3538 SPDK_ERRLOG("unsupported dev block length of %d\n", 3539 dev->blocklen); 3540 dev->destroy(dev); 3541 cb_fn(cb_arg, NULL, -EINVAL); 3542 return; 3543 } 3544 3545 if (o) { 3546 opts = *o; 3547 } else { 3548 spdk_bs_opts_init(&opts); 3549 } 3550 3551 if (_spdk_bs_opts_verify(&opts) != 0) { 3552 dev->destroy(dev); 3553 cb_fn(cb_arg, NULL, -EINVAL); 3554 return; 3555 } 3556 3557 rc = _spdk_bs_alloc(dev, &opts, &bs); 3558 if (rc) { 3559 dev->destroy(dev); 3560 cb_fn(cb_arg, NULL, rc); 3561 return; 3562 } 3563 3564 if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) { 3565 /* By default, allocate 1 page per cluster. 3566 * Technically, this over-allocates metadata 3567 * because more metadata will reduce the number 3568 * of usable clusters. This can be addressed with 3569 * more complex math in the future. 3570 */ 3571 bs->md_len = bs->total_clusters; 3572 } else { 3573 bs->md_len = opts.num_md_pages; 3574 } 3575 rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len); 3576 if (rc < 0) { 3577 _spdk_bs_free(bs); 3578 cb_fn(cb_arg, NULL, -ENOMEM); 3579 return; 3580 } 3581 3582 rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len); 3583 if (rc < 0) { 3584 _spdk_bs_free(bs); 3585 cb_fn(cb_arg, NULL, -ENOMEM); 3586 return; 3587 } 3588 3589 ctx = calloc(1, sizeof(*ctx)); 3590 if (!ctx) { 3591 _spdk_bs_free(bs); 3592 cb_fn(cb_arg, NULL, -ENOMEM); 3593 return; 3594 } 3595 3596 ctx->bs = bs; 3597 3598 /* Allocate memory for the super block */ 3599 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3600 if (!ctx->super) { 3601 free(ctx); 3602 _spdk_bs_free(bs); 3603 cb_fn(cb_arg, NULL, -ENOMEM); 3604 return; 3605 } 3606 memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 3607 sizeof(ctx->super->signature)); 3608 ctx->super->version = SPDK_BS_VERSION; 3609 ctx->super->length = sizeof(*ctx->super); 3610 ctx->super->super_blob = bs->super_blob; 3611 ctx->super->clean = 0; 3612 ctx->super->cluster_size = bs->cluster_sz; 3613 ctx->super->io_unit_size = bs->io_unit_size; 3614 memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype)); 3615 3616 /* Calculate how many pages the metadata consumes at the front 3617 * of the disk. 3618 */ 3619 3620 /* The super block uses 1 page */ 3621 num_md_pages = 1; 3622 3623 /* The used_md_pages mask requires 1 bit per metadata page, rounded 3624 * up to the nearest page, plus a header. 3625 */ 3626 ctx->super->used_page_mask_start = num_md_pages; 3627 ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 3628 spdk_divide_round_up(bs->md_len, 8), 3629 SPDK_BS_PAGE_SIZE); 3630 num_md_pages += ctx->super->used_page_mask_len; 3631 3632 /* The used_clusters mask requires 1 bit per cluster, rounded 3633 * up to the nearest page, plus a header. 3634 */ 3635 ctx->super->used_cluster_mask_start = num_md_pages; 3636 ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 3637 spdk_divide_round_up(bs->total_clusters, 8), 3638 SPDK_BS_PAGE_SIZE); 3639 num_md_pages += ctx->super->used_cluster_mask_len; 3640 3641 /* The used_blobids mask requires 1 bit per metadata page, rounded 3642 * up to the nearest page, plus a header. 3643 */ 3644 ctx->super->used_blobid_mask_start = num_md_pages; 3645 ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 3646 spdk_divide_round_up(bs->md_len, 8), 3647 SPDK_BS_PAGE_SIZE); 3648 num_md_pages += ctx->super->used_blobid_mask_len; 3649 3650 /* The metadata region size was chosen above */ 3651 ctx->super->md_start = bs->md_start = num_md_pages; 3652 ctx->super->md_len = bs->md_len; 3653 num_md_pages += bs->md_len; 3654 3655 num_md_lba = _spdk_bs_page_to_lba(bs, num_md_pages); 3656 3657 ctx->super->size = dev->blockcnt * dev->blocklen; 3658 3659 ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super); 3660 3661 num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster); 3662 if (num_md_clusters > bs->total_clusters) { 3663 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, " 3664 "please decrease number of pages reserved for metadata " 3665 "or increase cluster size.\n"); 3666 spdk_dma_free(ctx->super); 3667 free(ctx); 3668 _spdk_bs_free(bs); 3669 cb_fn(cb_arg, NULL, -ENOMEM); 3670 return; 3671 } 3672 /* Claim all of the clusters used by the metadata */ 3673 for (i = 0; i < num_md_clusters; i++) { 3674 _spdk_bs_claim_cluster(bs, i); 3675 } 3676 3677 bs->total_data_clusters = bs->num_free_clusters; 3678 3679 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 3680 cpl.u.bs_handle.cb_fn = cb_fn; 3681 cpl.u.bs_handle.cb_arg = cb_arg; 3682 cpl.u.bs_handle.bs = bs; 3683 3684 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3685 if (!seq) { 3686 spdk_dma_free(ctx->super); 3687 free(ctx); 3688 _spdk_bs_free(bs); 3689 cb_fn(cb_arg, NULL, -ENOMEM); 3690 return; 3691 } 3692 3693 batch = spdk_bs_sequence_to_batch(seq, _spdk_bs_init_trim_cpl, ctx); 3694 3695 /* Clear metadata space */ 3696 spdk_bs_batch_write_zeroes_dev(batch, 0, num_md_lba); 3697 /* Trim data clusters */ 3698 spdk_bs_batch_unmap_dev(batch, num_md_lba, ctx->bs->dev->blockcnt - num_md_lba); 3699 3700 spdk_bs_batch_close(batch); 3701 } 3702 3703 /* END spdk_bs_init */ 3704 3705 /* START spdk_bs_destroy */ 3706 3707 static void 3708 _spdk_bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3709 { 3710 struct spdk_bs_init_ctx *ctx = cb_arg; 3711 struct spdk_blob_store *bs = ctx->bs; 3712 3713 /* 3714 * We need to defer calling spdk_bs_call_cpl() until after 3715 * dev destruction, so tuck these away for later use. 3716 */ 3717 bs->unload_err = bserrno; 3718 memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 3719 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 3720 3721 spdk_bs_sequence_finish(seq, bserrno); 3722 3723 _spdk_bs_free(bs); 3724 free(ctx); 3725 } 3726 3727 void 3728 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, 3729 void *cb_arg) 3730 { 3731 struct spdk_bs_cpl cpl; 3732 spdk_bs_sequence_t *seq; 3733 struct spdk_bs_init_ctx *ctx; 3734 3735 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Destroying blobstore\n"); 3736 3737 if (!TAILQ_EMPTY(&bs->blobs)) { 3738 SPDK_ERRLOG("Blobstore still has open blobs\n"); 3739 cb_fn(cb_arg, -EBUSY); 3740 return; 3741 } 3742 3743 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3744 cpl.u.bs_basic.cb_fn = cb_fn; 3745 cpl.u.bs_basic.cb_arg = cb_arg; 3746 3747 ctx = calloc(1, sizeof(*ctx)); 3748 if (!ctx) { 3749 cb_fn(cb_arg, -ENOMEM); 3750 return; 3751 } 3752 3753 ctx->bs = bs; 3754 3755 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3756 if (!seq) { 3757 free(ctx); 3758 cb_fn(cb_arg, -ENOMEM); 3759 return; 3760 } 3761 3762 /* Write zeroes to the super block */ 3763 spdk_bs_sequence_write_zeroes_dev(seq, 3764 _spdk_bs_page_to_lba(bs, 0), 3765 _spdk_bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)), 3766 _spdk_bs_destroy_trim_cpl, ctx); 3767 } 3768 3769 /* END spdk_bs_destroy */ 3770 3771 /* START spdk_bs_unload */ 3772 3773 static void 3774 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3775 { 3776 struct spdk_bs_load_ctx *ctx = cb_arg; 3777 3778 spdk_dma_free(ctx->super); 3779 3780 /* 3781 * We need to defer calling spdk_bs_call_cpl() until after 3782 * dev destruction, so tuck these away for later use. 3783 */ 3784 ctx->bs->unload_err = bserrno; 3785 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 3786 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 3787 3788 spdk_bs_sequence_finish(seq, bserrno); 3789 3790 _spdk_bs_free(ctx->bs); 3791 free(ctx); 3792 } 3793 3794 static void 3795 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3796 { 3797 struct spdk_bs_load_ctx *ctx = cb_arg; 3798 3799 spdk_dma_free(ctx->mask); 3800 ctx->super->clean = 1; 3801 3802 _spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx); 3803 } 3804 3805 static void 3806 _spdk_bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3807 { 3808 struct spdk_bs_load_ctx *ctx = cb_arg; 3809 3810 spdk_dma_free(ctx->mask); 3811 ctx->mask = NULL; 3812 3813 _spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_unload_write_used_clusters_cpl); 3814 } 3815 3816 static void 3817 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3818 { 3819 struct spdk_bs_load_ctx *ctx = cb_arg; 3820 3821 spdk_dma_free(ctx->mask); 3822 ctx->mask = NULL; 3823 3824 _spdk_bs_write_used_blobids(seq, cb_arg, _spdk_bs_unload_write_used_blobids_cpl); 3825 } 3826 3827 static void 3828 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3829 { 3830 _spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl); 3831 } 3832 3833 void 3834 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 3835 { 3836 struct spdk_bs_cpl cpl; 3837 spdk_bs_sequence_t *seq; 3838 struct spdk_bs_load_ctx *ctx; 3839 3840 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blobstore\n"); 3841 3842 if (!TAILQ_EMPTY(&bs->blobs)) { 3843 SPDK_ERRLOG("Blobstore still has open blobs\n"); 3844 cb_fn(cb_arg, -EBUSY); 3845 return; 3846 } 3847 3848 ctx = calloc(1, sizeof(*ctx)); 3849 if (!ctx) { 3850 cb_fn(cb_arg, -ENOMEM); 3851 return; 3852 } 3853 3854 ctx->bs = bs; 3855 3856 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3857 if (!ctx->super) { 3858 free(ctx); 3859 cb_fn(cb_arg, -ENOMEM); 3860 return; 3861 } 3862 3863 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3864 cpl.u.bs_basic.cb_fn = cb_fn; 3865 cpl.u.bs_basic.cb_arg = cb_arg; 3866 3867 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3868 if (!seq) { 3869 spdk_dma_free(ctx->super); 3870 free(ctx); 3871 cb_fn(cb_arg, -ENOMEM); 3872 return; 3873 } 3874 3875 /* Read super block */ 3876 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3877 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3878 _spdk_bs_unload_read_super_cpl, ctx); 3879 } 3880 3881 /* END spdk_bs_unload */ 3882 3883 /* START spdk_bs_set_super */ 3884 3885 struct spdk_bs_set_super_ctx { 3886 struct spdk_blob_store *bs; 3887 struct spdk_bs_super_block *super; 3888 }; 3889 3890 static void 3891 _spdk_bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3892 { 3893 struct spdk_bs_set_super_ctx *ctx = cb_arg; 3894 3895 if (bserrno != 0) { 3896 SPDK_ERRLOG("Unable to write to super block of blobstore\n"); 3897 } 3898 3899 spdk_dma_free(ctx->super); 3900 3901 spdk_bs_sequence_finish(seq, bserrno); 3902 3903 free(ctx); 3904 } 3905 3906 static void 3907 _spdk_bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3908 { 3909 struct spdk_bs_set_super_ctx *ctx = cb_arg; 3910 3911 if (bserrno != 0) { 3912 SPDK_ERRLOG("Unable to read super block of blobstore\n"); 3913 spdk_dma_free(ctx->super); 3914 spdk_bs_sequence_finish(seq, bserrno); 3915 free(ctx); 3916 return; 3917 } 3918 3919 _spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_set_super_write_cpl, ctx); 3920 } 3921 3922 void 3923 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 3924 spdk_bs_op_complete cb_fn, void *cb_arg) 3925 { 3926 struct spdk_bs_cpl cpl; 3927 spdk_bs_sequence_t *seq; 3928 struct spdk_bs_set_super_ctx *ctx; 3929 3930 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Setting super blob id on blobstore\n"); 3931 3932 ctx = calloc(1, sizeof(*ctx)); 3933 if (!ctx) { 3934 cb_fn(cb_arg, -ENOMEM); 3935 return; 3936 } 3937 3938 ctx->bs = bs; 3939 3940 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3941 if (!ctx->super) { 3942 free(ctx); 3943 cb_fn(cb_arg, -ENOMEM); 3944 return; 3945 } 3946 3947 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3948 cpl.u.bs_basic.cb_fn = cb_fn; 3949 cpl.u.bs_basic.cb_arg = cb_arg; 3950 3951 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3952 if (!seq) { 3953 spdk_dma_free(ctx->super); 3954 free(ctx); 3955 cb_fn(cb_arg, -ENOMEM); 3956 return; 3957 } 3958 3959 bs->super_blob = blobid; 3960 3961 /* Read super block */ 3962 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3963 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3964 _spdk_bs_set_super_read_cpl, ctx); 3965 } 3966 3967 /* END spdk_bs_set_super */ 3968 3969 void 3970 spdk_bs_get_super(struct spdk_blob_store *bs, 3971 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 3972 { 3973 if (bs->super_blob == SPDK_BLOBID_INVALID) { 3974 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT); 3975 } else { 3976 cb_fn(cb_arg, bs->super_blob, 0); 3977 } 3978 } 3979 3980 uint64_t 3981 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 3982 { 3983 return bs->cluster_sz; 3984 } 3985 3986 uint64_t 3987 spdk_bs_get_page_size(struct spdk_blob_store *bs) 3988 { 3989 return SPDK_BS_PAGE_SIZE; 3990 } 3991 3992 uint64_t 3993 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs) 3994 { 3995 return bs->io_unit_size; 3996 } 3997 3998 uint64_t 3999 spdk_bs_free_cluster_count(struct spdk_blob_store *bs) 4000 { 4001 return bs->num_free_clusters; 4002 } 4003 4004 uint64_t 4005 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs) 4006 { 4007 return bs->total_data_clusters; 4008 } 4009 4010 static int 4011 spdk_bs_register_md_thread(struct spdk_blob_store *bs) 4012 { 4013 bs->md_channel = spdk_get_io_channel(bs); 4014 if (!bs->md_channel) { 4015 SPDK_ERRLOG("Failed to get IO channel.\n"); 4016 return -1; 4017 } 4018 4019 return 0; 4020 } 4021 4022 static int 4023 spdk_bs_unregister_md_thread(struct spdk_blob_store *bs) 4024 { 4025 spdk_put_io_channel(bs->md_channel); 4026 4027 return 0; 4028 } 4029 4030 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob) 4031 { 4032 assert(blob != NULL); 4033 4034 return blob->id; 4035 } 4036 4037 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob) 4038 { 4039 assert(blob != NULL); 4040 4041 return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters); 4042 } 4043 4044 uint64_t spdk_blob_get_num_io_units(struct spdk_blob *blob) 4045 { 4046 assert(blob != NULL); 4047 4048 return spdk_blob_get_num_pages(blob) * _spdk_bs_io_unit_per_page(blob->bs); 4049 } 4050 4051 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob) 4052 { 4053 assert(blob != NULL); 4054 4055 return blob->active.num_clusters; 4056 } 4057 4058 /* START spdk_bs_create_blob */ 4059 4060 static void 4061 _spdk_bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4062 { 4063 struct spdk_blob *blob = cb_arg; 4064 4065 _spdk_blob_free(blob); 4066 4067 spdk_bs_sequence_finish(seq, bserrno); 4068 } 4069 4070 static int 4071 _spdk_blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs, 4072 bool internal) 4073 { 4074 uint64_t i; 4075 size_t value_len = 0; 4076 int rc; 4077 const void *value = NULL; 4078 if (xattrs->count > 0 && xattrs->get_value == NULL) { 4079 return -EINVAL; 4080 } 4081 for (i = 0; i < xattrs->count; i++) { 4082 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len); 4083 if (value == NULL || value_len == 0) { 4084 return -EINVAL; 4085 } 4086 rc = _spdk_blob_set_xattr(blob, xattrs->names[i], value, value_len, internal); 4087 if (rc < 0) { 4088 return rc; 4089 } 4090 } 4091 return 0; 4092 } 4093 4094 static void 4095 _spdk_blob_set_thin_provision(struct spdk_blob *blob) 4096 { 4097 _spdk_blob_verify_md_op(blob); 4098 blob->invalid_flags |= SPDK_BLOB_THIN_PROV; 4099 blob->state = SPDK_BLOB_STATE_DIRTY; 4100 } 4101 4102 static void 4103 _spdk_bs_create_blob(struct spdk_blob_store *bs, 4104 const struct spdk_blob_opts *opts, 4105 const struct spdk_blob_xattr_opts *internal_xattrs, 4106 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4107 { 4108 struct spdk_blob *blob; 4109 uint32_t page_idx; 4110 struct spdk_bs_cpl cpl; 4111 struct spdk_blob_opts opts_default; 4112 struct spdk_blob_xattr_opts internal_xattrs_default; 4113 spdk_bs_sequence_t *seq; 4114 spdk_blob_id id; 4115 int rc; 4116 4117 assert(spdk_get_thread() == bs->md_thread); 4118 4119 page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0); 4120 if (page_idx == UINT32_MAX) { 4121 cb_fn(cb_arg, 0, -ENOMEM); 4122 return; 4123 } 4124 spdk_bit_array_set(bs->used_blobids, page_idx); 4125 spdk_bit_array_set(bs->used_md_pages, page_idx); 4126 4127 id = _spdk_bs_page_to_blobid(page_idx); 4128 4129 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx); 4130 4131 blob = _spdk_blob_alloc(bs, id); 4132 if (!blob) { 4133 cb_fn(cb_arg, 0, -ENOMEM); 4134 return; 4135 } 4136 4137 if (!opts) { 4138 spdk_blob_opts_init(&opts_default); 4139 opts = &opts_default; 4140 } 4141 if (!internal_xattrs) { 4142 _spdk_blob_xattrs_init(&internal_xattrs_default); 4143 internal_xattrs = &internal_xattrs_default; 4144 } 4145 4146 rc = _spdk_blob_set_xattrs(blob, &opts->xattrs, false); 4147 if (rc < 0) { 4148 _spdk_blob_free(blob); 4149 cb_fn(cb_arg, 0, rc); 4150 return; 4151 } 4152 4153 rc = _spdk_blob_set_xattrs(blob, internal_xattrs, true); 4154 if (rc < 0) { 4155 _spdk_blob_free(blob); 4156 cb_fn(cb_arg, 0, rc); 4157 return; 4158 } 4159 4160 if (opts->thin_provision) { 4161 _spdk_blob_set_thin_provision(blob); 4162 } 4163 4164 rc = _spdk_blob_resize(blob, opts->num_clusters); 4165 if (rc < 0) { 4166 _spdk_blob_free(blob); 4167 cb_fn(cb_arg, 0, rc); 4168 return; 4169 } 4170 cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 4171 cpl.u.blobid.cb_fn = cb_fn; 4172 cpl.u.blobid.cb_arg = cb_arg; 4173 cpl.u.blobid.blobid = blob->id; 4174 4175 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 4176 if (!seq) { 4177 _spdk_blob_free(blob); 4178 cb_fn(cb_arg, 0, -ENOMEM); 4179 return; 4180 } 4181 4182 _spdk_blob_persist(seq, blob, _spdk_bs_create_blob_cpl, blob); 4183 } 4184 4185 void spdk_bs_create_blob(struct spdk_blob_store *bs, 4186 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4187 { 4188 _spdk_bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg); 4189 } 4190 4191 void spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts, 4192 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4193 { 4194 _spdk_bs_create_blob(bs, opts, NULL, cb_fn, cb_arg); 4195 } 4196 4197 /* END spdk_bs_create_blob */ 4198 4199 /* START blob_cleanup */ 4200 4201 struct spdk_clone_snapshot_ctx { 4202 struct spdk_bs_cpl cpl; 4203 int bserrno; 4204 bool frozen; 4205 4206 struct spdk_io_channel *channel; 4207 4208 /* Current cluster for inflate operation */ 4209 uint64_t cluster; 4210 4211 /* For inflation force allocation of all unallocated clusters and remove 4212 * thin-provisioning. Otherwise only decouple parent and keep clone thin. */ 4213 bool allocate_all; 4214 4215 struct { 4216 spdk_blob_id id; 4217 struct spdk_blob *blob; 4218 } original; 4219 struct { 4220 spdk_blob_id id; 4221 struct spdk_blob *blob; 4222 } new; 4223 4224 /* xattrs specified for snapshot/clones only. They have no impact on 4225 * the original blobs xattrs. */ 4226 const struct spdk_blob_xattr_opts *xattrs; 4227 }; 4228 4229 static void 4230 _spdk_bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno) 4231 { 4232 struct spdk_clone_snapshot_ctx *ctx = cb_arg; 4233 struct spdk_bs_cpl *cpl = &ctx->cpl; 4234 4235 if (bserrno != 0) { 4236 if (ctx->bserrno != 0) { 4237 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 4238 } else { 4239 ctx->bserrno = bserrno; 4240 } 4241 } 4242 4243 switch (cpl->type) { 4244 case SPDK_BS_CPL_TYPE_BLOBID: 4245 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno); 4246 break; 4247 case SPDK_BS_CPL_TYPE_BLOB_BASIC: 4248 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno); 4249 break; 4250 default: 4251 SPDK_UNREACHABLE(); 4252 break; 4253 } 4254 4255 free(ctx); 4256 } 4257 4258 static void 4259 _spdk_bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 4260 { 4261 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4262 struct spdk_blob *origblob = ctx->original.blob; 4263 4264 if (bserrno != 0) { 4265 if (ctx->bserrno != 0) { 4266 SPDK_ERRLOG("Unfreeze error %d\n", bserrno); 4267 } else { 4268 ctx->bserrno = bserrno; 4269 } 4270 } 4271 4272 ctx->original.id = origblob->id; 4273 spdk_blob_close(origblob, _spdk_bs_clone_snapshot_cleanup_finish, ctx); 4274 } 4275 4276 static void 4277 _spdk_bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno) 4278 { 4279 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4280 struct spdk_blob *origblob = ctx->original.blob; 4281 4282 if (bserrno != 0) { 4283 if (ctx->bserrno != 0) { 4284 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 4285 } else { 4286 ctx->bserrno = bserrno; 4287 } 4288 } 4289 4290 if (ctx->frozen) { 4291 /* Unfreeze any outstanding I/O */ 4292 _spdk_blob_unfreeze_io(origblob, _spdk_bs_snapshot_unfreeze_cpl, ctx); 4293 } else { 4294 _spdk_bs_snapshot_unfreeze_cpl(ctx, 0); 4295 } 4296 4297 } 4298 4299 static void 4300 _spdk_bs_clone_snapshot_newblob_cleanup(void *cb_arg, int bserrno) 4301 { 4302 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4303 struct spdk_blob *newblob = ctx->new.blob; 4304 4305 if (bserrno != 0) { 4306 if (ctx->bserrno != 0) { 4307 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 4308 } else { 4309 ctx->bserrno = bserrno; 4310 } 4311 } 4312 4313 ctx->new.id = newblob->id; 4314 spdk_blob_close(newblob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4315 } 4316 4317 /* END blob_cleanup */ 4318 4319 /* START spdk_bs_create_snapshot */ 4320 4321 static void 4322 _spdk_bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno) 4323 { 4324 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4325 struct spdk_blob *newblob = ctx->new.blob; 4326 4327 if (bserrno != 0) { 4328 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4329 return; 4330 } 4331 4332 /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */ 4333 bserrno = _spdk_blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true); 4334 if (bserrno != 0) { 4335 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4336 return; 4337 } 4338 4339 _spdk_bs_blob_list_add(ctx->original.blob); 4340 4341 spdk_blob_set_read_only(newblob); 4342 4343 /* sync snapshot metadata */ 4344 spdk_blob_sync_md(newblob, _spdk_bs_clone_snapshot_origblob_cleanup, cb_arg); 4345 } 4346 4347 static void 4348 _spdk_bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno) 4349 { 4350 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4351 struct spdk_blob *origblob = ctx->original.blob; 4352 struct spdk_blob *newblob = ctx->new.blob; 4353 4354 if (bserrno != 0) { 4355 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4356 return; 4357 } 4358 4359 /* Set internal xattr for snapshot id */ 4360 bserrno = _spdk_blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true); 4361 if (bserrno != 0) { 4362 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4363 return; 4364 } 4365 4366 _spdk_bs_blob_list_remove(origblob); 4367 origblob->parent_id = newblob->id; 4368 4369 /* Create new back_bs_dev for snapshot */ 4370 origblob->back_bs_dev = spdk_bs_create_blob_bs_dev(newblob); 4371 if (origblob->back_bs_dev == NULL) { 4372 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL); 4373 return; 4374 } 4375 4376 /* set clone blob as thin provisioned */ 4377 _spdk_blob_set_thin_provision(origblob); 4378 4379 _spdk_bs_blob_list_add(newblob); 4380 4381 /* Zero out origblob cluster map */ 4382 memset(origblob->active.clusters, 0, 4383 origblob->active.num_clusters * sizeof(origblob->active.clusters)); 4384 4385 /* sync clone metadata */ 4386 spdk_blob_sync_md(origblob, _spdk_bs_snapshot_origblob_sync_cpl, ctx); 4387 } 4388 4389 static void 4390 _spdk_bs_snapshot_freeze_cpl(void *cb_arg, int rc) 4391 { 4392 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4393 struct spdk_blob *origblob = ctx->original.blob; 4394 struct spdk_blob *newblob = ctx->new.blob; 4395 int bserrno; 4396 4397 if (rc != 0) { 4398 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, rc); 4399 return; 4400 } 4401 4402 ctx->frozen = true; 4403 4404 /* set new back_bs_dev for snapshot */ 4405 newblob->back_bs_dev = origblob->back_bs_dev; 4406 /* Set invalid flags from origblob */ 4407 newblob->invalid_flags = origblob->invalid_flags; 4408 4409 /* inherit parent from original blob if set */ 4410 newblob->parent_id = origblob->parent_id; 4411 if (origblob->parent_id != SPDK_BLOBID_INVALID) { 4412 /* Set internal xattr for snapshot id */ 4413 bserrno = _spdk_blob_set_xattr(newblob, BLOB_SNAPSHOT, 4414 &origblob->parent_id, sizeof(spdk_blob_id), true); 4415 if (bserrno != 0) { 4416 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4417 return; 4418 } 4419 } 4420 4421 /* Copy cluster map to snapshot */ 4422 memcpy(newblob->active.clusters, origblob->active.clusters, 4423 origblob->active.num_clusters * sizeof(origblob->active.clusters)); 4424 4425 /* sync snapshot metadata */ 4426 spdk_blob_sync_md(newblob, _spdk_bs_snapshot_newblob_sync_cpl, ctx); 4427 } 4428 4429 static void 4430 _spdk_bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4431 { 4432 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4433 struct spdk_blob *origblob = ctx->original.blob; 4434 struct spdk_blob *newblob = _blob; 4435 4436 if (bserrno != 0) { 4437 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4438 return; 4439 } 4440 4441 ctx->new.blob = newblob; 4442 4443 _spdk_blob_freeze_io(origblob, _spdk_bs_snapshot_freeze_cpl, ctx); 4444 } 4445 4446 static void 4447 _spdk_bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 4448 { 4449 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4450 struct spdk_blob *origblob = ctx->original.blob; 4451 4452 if (bserrno != 0) { 4453 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4454 return; 4455 } 4456 4457 ctx->new.id = blobid; 4458 ctx->cpl.u.blobid.blobid = blobid; 4459 4460 spdk_bs_open_blob(origblob->bs, ctx->new.id, _spdk_bs_snapshot_newblob_open_cpl, ctx); 4461 } 4462 4463 4464 static void 4465 _spdk_bs_xattr_snapshot(void *arg, const char *name, 4466 const void **value, size_t *value_len) 4467 { 4468 assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0); 4469 4470 struct spdk_blob *blob = (struct spdk_blob *)arg; 4471 *value = &blob->id; 4472 *value_len = sizeof(blob->id); 4473 } 4474 4475 static void 4476 _spdk_bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4477 { 4478 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4479 struct spdk_blob_opts opts; 4480 struct spdk_blob_xattr_opts internal_xattrs; 4481 char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS }; 4482 4483 if (bserrno != 0) { 4484 _spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno); 4485 return; 4486 } 4487 4488 ctx->original.blob = _blob; 4489 4490 if (_blob->data_ro || _blob->md_ro) { 4491 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot create snapshot from read only blob with id %lu\n", 4492 _blob->id); 4493 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 4494 return; 4495 } 4496 4497 spdk_blob_opts_init(&opts); 4498 _spdk_blob_xattrs_init(&internal_xattrs); 4499 4500 /* Change the size of new blob to the same as in original blob, 4501 * but do not allocate clusters */ 4502 opts.thin_provision = true; 4503 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 4504 4505 /* If there are any xattrs specified for snapshot, set them now */ 4506 if (ctx->xattrs) { 4507 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 4508 } 4509 /* Set internal xattr SNAPSHOT_IN_PROGRESS */ 4510 internal_xattrs.count = 1; 4511 internal_xattrs.ctx = _blob; 4512 internal_xattrs.names = xattrs_names; 4513 internal_xattrs.get_value = _spdk_bs_xattr_snapshot; 4514 4515 _spdk_bs_create_blob(_blob->bs, &opts, &internal_xattrs, 4516 _spdk_bs_snapshot_newblob_create_cpl, ctx); 4517 } 4518 4519 void spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid, 4520 const struct spdk_blob_xattr_opts *snapshot_xattrs, 4521 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4522 { 4523 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 4524 4525 if (!ctx) { 4526 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 4527 return; 4528 } 4529 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 4530 ctx->cpl.u.blobid.cb_fn = cb_fn; 4531 ctx->cpl.u.blobid.cb_arg = cb_arg; 4532 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 4533 ctx->bserrno = 0; 4534 ctx->frozen = false; 4535 ctx->original.id = blobid; 4536 ctx->xattrs = snapshot_xattrs; 4537 4538 spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_snapshot_origblob_open_cpl, ctx); 4539 } 4540 /* END spdk_bs_create_snapshot */ 4541 4542 /* START spdk_bs_create_clone */ 4543 4544 static void 4545 _spdk_bs_xattr_clone(void *arg, const char *name, 4546 const void **value, size_t *value_len) 4547 { 4548 assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0); 4549 4550 struct spdk_blob *blob = (struct spdk_blob *)arg; 4551 *value = &blob->id; 4552 *value_len = sizeof(blob->id); 4553 } 4554 4555 static void 4556 _spdk_bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4557 { 4558 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4559 struct spdk_blob *clone = _blob; 4560 4561 ctx->new.blob = clone; 4562 _spdk_bs_blob_list_add(clone); 4563 4564 spdk_blob_close(clone, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4565 } 4566 4567 static void 4568 _spdk_bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 4569 { 4570 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4571 4572 ctx->cpl.u.blobid.blobid = blobid; 4573 spdk_bs_open_blob(ctx->original.blob->bs, blobid, _spdk_bs_clone_newblob_open_cpl, ctx); 4574 } 4575 4576 static void 4577 _spdk_bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4578 { 4579 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4580 struct spdk_blob_opts opts; 4581 struct spdk_blob_xattr_opts internal_xattrs; 4582 char *xattr_names[] = { BLOB_SNAPSHOT }; 4583 4584 if (bserrno != 0) { 4585 _spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno); 4586 return; 4587 } 4588 4589 ctx->original.blob = _blob; 4590 4591 if (!_blob->data_ro || !_blob->md_ro) { 4592 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Clone not from read-only blob\n"); 4593 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 4594 return; 4595 } 4596 4597 spdk_blob_opts_init(&opts); 4598 _spdk_blob_xattrs_init(&internal_xattrs); 4599 4600 opts.thin_provision = true; 4601 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 4602 if (ctx->xattrs) { 4603 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 4604 } 4605 4606 /* Set internal xattr BLOB_SNAPSHOT */ 4607 internal_xattrs.count = 1; 4608 internal_xattrs.ctx = _blob; 4609 internal_xattrs.names = xattr_names; 4610 internal_xattrs.get_value = _spdk_bs_xattr_clone; 4611 4612 _spdk_bs_create_blob(_blob->bs, &opts, &internal_xattrs, 4613 _spdk_bs_clone_newblob_create_cpl, ctx); 4614 } 4615 4616 void spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid, 4617 const struct spdk_blob_xattr_opts *clone_xattrs, 4618 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4619 { 4620 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 4621 4622 if (!ctx) { 4623 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 4624 return; 4625 } 4626 4627 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 4628 ctx->cpl.u.blobid.cb_fn = cb_fn; 4629 ctx->cpl.u.blobid.cb_arg = cb_arg; 4630 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 4631 ctx->bserrno = 0; 4632 ctx->xattrs = clone_xattrs; 4633 ctx->original.id = blobid; 4634 4635 spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_clone_origblob_open_cpl, ctx); 4636 } 4637 4638 /* END spdk_bs_create_clone */ 4639 4640 /* START spdk_bs_inflate_blob */ 4641 4642 static void 4643 _spdk_bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno) 4644 { 4645 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4646 struct spdk_blob *_blob = ctx->original.blob; 4647 4648 if (bserrno != 0) { 4649 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4650 return; 4651 } 4652 4653 assert(_parent != NULL); 4654 4655 _spdk_bs_blob_list_remove(_blob); 4656 _blob->parent_id = _parent->id; 4657 _spdk_blob_set_xattr(_blob, BLOB_SNAPSHOT, &_blob->parent_id, 4658 sizeof(spdk_blob_id), true); 4659 4660 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 4661 _blob->back_bs_dev = spdk_bs_create_blob_bs_dev(_parent); 4662 _spdk_bs_blob_list_add(_blob); 4663 4664 spdk_blob_sync_md(_blob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4665 } 4666 4667 static void 4668 _spdk_bs_inflate_blob_done(void *cb_arg, int bserrno) 4669 { 4670 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4671 struct spdk_blob *_blob = ctx->original.blob; 4672 struct spdk_blob *_parent; 4673 4674 if (bserrno != 0) { 4675 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4676 return; 4677 } 4678 4679 if (ctx->allocate_all) { 4680 /* remove thin provisioning */ 4681 _spdk_bs_blob_list_remove(_blob); 4682 _spdk_blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 4683 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV; 4684 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 4685 _blob->back_bs_dev = NULL; 4686 _blob->parent_id = SPDK_BLOBID_INVALID; 4687 } else { 4688 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob; 4689 if (_parent->parent_id != SPDK_BLOBID_INVALID) { 4690 /* We must change the parent of the inflated blob */ 4691 spdk_bs_open_blob(_blob->bs, _parent->parent_id, 4692 _spdk_bs_inflate_blob_set_parent_cpl, ctx); 4693 return; 4694 } 4695 4696 _spdk_bs_blob_list_remove(_blob); 4697 _spdk_blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 4698 _blob->parent_id = SPDK_BLOBID_INVALID; 4699 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 4700 _blob->back_bs_dev = spdk_bs_create_zeroes_dev(); 4701 } 4702 4703 _blob->state = SPDK_BLOB_STATE_DIRTY; 4704 spdk_blob_sync_md(_blob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4705 } 4706 4707 /* Check if cluster needs allocation */ 4708 static inline bool 4709 _spdk_bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all) 4710 { 4711 struct spdk_blob_bs_dev *b; 4712 4713 assert(blob != NULL); 4714 4715 if (blob->active.clusters[cluster] != 0) { 4716 /* Cluster is already allocated */ 4717 return false; 4718 } 4719 4720 if (blob->parent_id == SPDK_BLOBID_INVALID) { 4721 /* Blob have no parent blob */ 4722 return allocate_all; 4723 } 4724 4725 b = (struct spdk_blob_bs_dev *)blob->back_bs_dev; 4726 return (allocate_all || b->blob->active.clusters[cluster] != 0); 4727 } 4728 4729 static void 4730 _spdk_bs_inflate_blob_touch_next(void *cb_arg, int bserrno) 4731 { 4732 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4733 struct spdk_blob *_blob = ctx->original.blob; 4734 uint64_t offset; 4735 4736 if (bserrno != 0) { 4737 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4738 return; 4739 } 4740 4741 for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) { 4742 if (_spdk_bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) { 4743 break; 4744 } 4745 } 4746 4747 if (ctx->cluster < _blob->active.num_clusters) { 4748 offset = _spdk_bs_cluster_to_lba(_blob->bs, ctx->cluster); 4749 4750 /* We may safely increment a cluster before write */ 4751 ctx->cluster++; 4752 4753 /* Use zero length write to touch a cluster */ 4754 spdk_blob_io_write(_blob, ctx->channel, NULL, offset, 0, 4755 _spdk_bs_inflate_blob_touch_next, ctx); 4756 } else { 4757 _spdk_bs_inflate_blob_done(cb_arg, bserrno); 4758 } 4759 } 4760 4761 static void 4762 _spdk_bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4763 { 4764 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4765 uint64_t lfc; /* lowest free cluster */ 4766 uint64_t i; 4767 4768 if (bserrno != 0) { 4769 _spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno); 4770 return; 4771 } 4772 ctx->original.blob = _blob; 4773 4774 if (!ctx->allocate_all && _blob->parent_id == SPDK_BLOBID_INVALID) { 4775 /* This blob have no parent, so we cannot decouple it. */ 4776 SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n"); 4777 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 4778 return; 4779 } 4780 4781 if (spdk_blob_is_thin_provisioned(_blob) == false) { 4782 /* This is not thin provisioned blob. No need to inflate. */ 4783 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, 0); 4784 return; 4785 } 4786 4787 /* Do two passes - one to verify that we can obtain enough clusters 4788 * and another to actually claim them. 4789 */ 4790 lfc = 0; 4791 for (i = 0; i < _blob->active.num_clusters; i++) { 4792 if (_spdk_bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) { 4793 lfc = spdk_bit_array_find_first_clear(_blob->bs->used_clusters, lfc); 4794 if (lfc == UINT32_MAX) { 4795 /* No more free clusters. Cannot satisfy the request */ 4796 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC); 4797 return; 4798 } 4799 lfc++; 4800 } 4801 } 4802 4803 ctx->cluster = 0; 4804 _spdk_bs_inflate_blob_touch_next(ctx, 0); 4805 } 4806 4807 static void 4808 _spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 4809 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg) 4810 { 4811 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 4812 4813 if (!ctx) { 4814 cb_fn(cb_arg, -ENOMEM); 4815 return; 4816 } 4817 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 4818 ctx->cpl.u.bs_basic.cb_fn = cb_fn; 4819 ctx->cpl.u.bs_basic.cb_arg = cb_arg; 4820 ctx->bserrno = 0; 4821 ctx->original.id = blobid; 4822 ctx->channel = channel; 4823 ctx->allocate_all = allocate_all; 4824 4825 spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_inflate_blob_open_cpl, ctx); 4826 } 4827 4828 void 4829 spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 4830 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 4831 { 4832 _spdk_bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg); 4833 } 4834 4835 void 4836 spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 4837 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 4838 { 4839 _spdk_bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg); 4840 } 4841 /* END spdk_bs_inflate_blob */ 4842 4843 /* START spdk_blob_resize */ 4844 struct spdk_bs_resize_ctx { 4845 spdk_blob_op_complete cb_fn; 4846 void *cb_arg; 4847 struct spdk_blob *blob; 4848 uint64_t sz; 4849 int rc; 4850 }; 4851 4852 static void 4853 _spdk_bs_resize_unfreeze_cpl(void *cb_arg, int rc) 4854 { 4855 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 4856 4857 if (rc != 0) { 4858 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc); 4859 } 4860 4861 if (ctx->rc != 0) { 4862 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc); 4863 rc = ctx->rc; 4864 } 4865 4866 ctx->blob->resize_in_progress = false; 4867 4868 ctx->cb_fn(ctx->cb_arg, rc); 4869 free(ctx); 4870 } 4871 4872 static void 4873 _spdk_bs_resize_freeze_cpl(void *cb_arg, int rc) 4874 { 4875 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 4876 4877 if (rc != 0) { 4878 ctx->blob->resize_in_progress = false; 4879 ctx->cb_fn(ctx->cb_arg, rc); 4880 free(ctx); 4881 return; 4882 } 4883 4884 ctx->rc = _spdk_blob_resize(ctx->blob, ctx->sz); 4885 4886 _spdk_blob_unfreeze_io(ctx->blob, _spdk_bs_resize_unfreeze_cpl, ctx); 4887 } 4888 4889 void 4890 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg) 4891 { 4892 struct spdk_bs_resize_ctx *ctx; 4893 4894 _spdk_blob_verify_md_op(blob); 4895 4896 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz); 4897 4898 if (blob->md_ro) { 4899 cb_fn(cb_arg, -EPERM); 4900 return; 4901 } 4902 4903 if (sz == blob->active.num_clusters) { 4904 cb_fn(cb_arg, 0); 4905 return; 4906 } 4907 4908 if (blob->resize_in_progress) { 4909 cb_fn(cb_arg, -EBUSY); 4910 return; 4911 } 4912 4913 ctx = calloc(1, sizeof(*ctx)); 4914 if (!ctx) { 4915 cb_fn(cb_arg, -ENOMEM); 4916 return; 4917 } 4918 4919 blob->resize_in_progress = true; 4920 ctx->cb_fn = cb_fn; 4921 ctx->cb_arg = cb_arg; 4922 ctx->blob = blob; 4923 ctx->sz = sz; 4924 _spdk_blob_freeze_io(blob, _spdk_bs_resize_freeze_cpl, ctx); 4925 } 4926 4927 /* END spdk_blob_resize */ 4928 4929 4930 /* START spdk_bs_delete_blob */ 4931 4932 static void 4933 _spdk_bs_delete_close_cpl(void *cb_arg, int bserrno) 4934 { 4935 spdk_bs_sequence_t *seq = cb_arg; 4936 4937 spdk_bs_sequence_finish(seq, bserrno); 4938 } 4939 4940 static void 4941 _spdk_bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4942 { 4943 struct spdk_blob *blob = cb_arg; 4944 4945 if (bserrno != 0) { 4946 /* 4947 * We already removed this blob from the blobstore tailq, so 4948 * we need to free it here since this is the last reference 4949 * to it. 4950 */ 4951 _spdk_blob_free(blob); 4952 _spdk_bs_delete_close_cpl(seq, bserrno); 4953 return; 4954 } 4955 4956 /* 4957 * This will immediately decrement the ref_count and call 4958 * the completion routine since the metadata state is clean. 4959 * By calling spdk_blob_close, we reduce the number of call 4960 * points into code that touches the blob->open_ref count 4961 * and the blobstore's blob list. 4962 */ 4963 spdk_blob_close(blob, _spdk_bs_delete_close_cpl, seq); 4964 } 4965 4966 static void 4967 _spdk_bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno) 4968 { 4969 spdk_bs_sequence_t *seq = cb_arg; 4970 struct spdk_blob_list *snapshot = NULL; 4971 uint32_t page_num; 4972 4973 if (bserrno != 0) { 4974 spdk_bs_sequence_finish(seq, bserrno); 4975 return; 4976 } 4977 4978 _spdk_blob_verify_md_op(blob); 4979 4980 if (blob->open_ref > 1) { 4981 /* 4982 * Someone has this blob open (besides this delete context). 4983 * Decrement the ref count directly and return -EBUSY. 4984 */ 4985 blob->open_ref--; 4986 spdk_bs_sequence_finish(seq, -EBUSY); 4987 return; 4988 } 4989 4990 bserrno = _spdk_bs_blob_list_remove(blob); 4991 if (bserrno != 0) { 4992 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Remove blob #%" PRIu64 " from a list\n", blob->id); 4993 spdk_bs_sequence_finish(seq, bserrno); 4994 return; 4995 } 4996 4997 /* 4998 * Remove the blob from the blob_store list now, to ensure it does not 4999 * get returned after this point by _spdk_blob_lookup(). 5000 */ 5001 TAILQ_REMOVE(&blob->bs->blobs, blob, link); 5002 5003 /* If blob is a snapshot then remove it from the list */ 5004 TAILQ_FOREACH(snapshot, &blob->bs->snapshots, link) { 5005 if (snapshot->id == blob->id) { 5006 TAILQ_REMOVE(&blob->bs->snapshots, snapshot, link); 5007 free(snapshot); 5008 break; 5009 } 5010 } 5011 5012 page_num = _spdk_bs_blobid_to_page(blob->id); 5013 spdk_bit_array_clear(blob->bs->used_blobids, page_num); 5014 blob->state = SPDK_BLOB_STATE_DIRTY; 5015 blob->active.num_pages = 0; 5016 _spdk_blob_resize(blob, 0); 5017 5018 _spdk_blob_persist(seq, blob, _spdk_bs_delete_persist_cpl, blob); 5019 } 5020 5021 void 5022 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 5023 spdk_blob_op_complete cb_fn, void *cb_arg) 5024 { 5025 struct spdk_bs_cpl cpl; 5026 spdk_bs_sequence_t *seq; 5027 struct spdk_blob_list *snapshot_entry = NULL; 5028 5029 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Deleting blob %lu\n", blobid); 5030 5031 assert(spdk_get_thread() == bs->md_thread); 5032 5033 /* Check if this is a snapshot with clones */ 5034 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 5035 if (snapshot_entry->id == blobid) { 5036 break; 5037 } 5038 } 5039 if (snapshot_entry != NULL) { 5040 /* If snapshot have clones, we cannot remove it */ 5041 if (!TAILQ_EMPTY(&snapshot_entry->clones)) { 5042 SPDK_ERRLOG("Cannot remove snapshot with clones\n"); 5043 cb_fn(cb_arg, -EBUSY); 5044 return; 5045 } 5046 } 5047 5048 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 5049 cpl.u.blob_basic.cb_fn = cb_fn; 5050 cpl.u.blob_basic.cb_arg = cb_arg; 5051 5052 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 5053 if (!seq) { 5054 cb_fn(cb_arg, -ENOMEM); 5055 return; 5056 } 5057 5058 spdk_bs_open_blob(bs, blobid, _spdk_bs_delete_open_cpl, seq); 5059 } 5060 5061 /* END spdk_bs_delete_blob */ 5062 5063 /* START spdk_bs_open_blob */ 5064 5065 static void 5066 _spdk_bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5067 { 5068 struct spdk_blob *blob = cb_arg; 5069 5070 /* If the blob have crc error, we just return NULL. */ 5071 if (blob == NULL) { 5072 seq->cpl.u.blob_handle.blob = NULL; 5073 spdk_bs_sequence_finish(seq, bserrno); 5074 return; 5075 } 5076 5077 blob->open_ref++; 5078 5079 TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link); 5080 5081 spdk_bs_sequence_finish(seq, bserrno); 5082 } 5083 5084 static void _spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 5085 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5086 { 5087 struct spdk_blob *blob; 5088 struct spdk_bs_cpl cpl; 5089 struct spdk_blob_open_opts opts_default; 5090 spdk_bs_sequence_t *seq; 5091 uint32_t page_num; 5092 5093 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Opening blob %lu\n", blobid); 5094 assert(spdk_get_thread() == bs->md_thread); 5095 5096 page_num = _spdk_bs_blobid_to_page(blobid); 5097 if (spdk_bit_array_get(bs->used_blobids, page_num) == false) { 5098 /* Invalid blobid */ 5099 cb_fn(cb_arg, NULL, -ENOENT); 5100 return; 5101 } 5102 5103 blob = _spdk_blob_lookup(bs, blobid); 5104 if (blob) { 5105 blob->open_ref++; 5106 cb_fn(cb_arg, blob, 0); 5107 return; 5108 } 5109 5110 blob = _spdk_blob_alloc(bs, blobid); 5111 if (!blob) { 5112 cb_fn(cb_arg, NULL, -ENOMEM); 5113 return; 5114 } 5115 5116 if (!opts) { 5117 spdk_blob_open_opts_init(&opts_default); 5118 opts = &opts_default; 5119 } 5120 5121 blob->clear_method = opts->clear_method; 5122 5123 cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE; 5124 cpl.u.blob_handle.cb_fn = cb_fn; 5125 cpl.u.blob_handle.cb_arg = cb_arg; 5126 cpl.u.blob_handle.blob = blob; 5127 5128 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 5129 if (!seq) { 5130 _spdk_blob_free(blob); 5131 cb_fn(cb_arg, NULL, -ENOMEM); 5132 return; 5133 } 5134 5135 _spdk_blob_load(seq, blob, _spdk_bs_open_blob_cpl, blob); 5136 } 5137 5138 void spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 5139 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5140 { 5141 _spdk_bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg); 5142 } 5143 5144 void spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid, 5145 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5146 { 5147 _spdk_bs_open_blob(bs, blobid, opts, cb_fn, cb_arg); 5148 } 5149 5150 /* END spdk_bs_open_blob */ 5151 5152 /* START spdk_blob_set_read_only */ 5153 int spdk_blob_set_read_only(struct spdk_blob *blob) 5154 { 5155 _spdk_blob_verify_md_op(blob); 5156 5157 blob->data_ro_flags |= SPDK_BLOB_READ_ONLY; 5158 5159 blob->state = SPDK_BLOB_STATE_DIRTY; 5160 return 0; 5161 } 5162 /* END spdk_blob_set_read_only */ 5163 5164 /* START spdk_blob_sync_md */ 5165 5166 static void 5167 _spdk_blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5168 { 5169 struct spdk_blob *blob = cb_arg; 5170 5171 if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 5172 blob->data_ro = true; 5173 blob->md_ro = true; 5174 } 5175 5176 spdk_bs_sequence_finish(seq, bserrno); 5177 } 5178 5179 static void 5180 _spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 5181 { 5182 struct spdk_bs_cpl cpl; 5183 spdk_bs_sequence_t *seq; 5184 5185 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 5186 cpl.u.blob_basic.cb_fn = cb_fn; 5187 cpl.u.blob_basic.cb_arg = cb_arg; 5188 5189 seq = spdk_bs_sequence_start(blob->bs->md_channel, &cpl); 5190 if (!seq) { 5191 cb_fn(cb_arg, -ENOMEM); 5192 return; 5193 } 5194 5195 _spdk_blob_persist(seq, blob, _spdk_blob_sync_md_cpl, blob); 5196 } 5197 5198 void 5199 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 5200 { 5201 _spdk_blob_verify_md_op(blob); 5202 5203 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blob %lu\n", blob->id); 5204 5205 if (blob->md_ro) { 5206 assert(blob->state == SPDK_BLOB_STATE_CLEAN); 5207 cb_fn(cb_arg, 0); 5208 return; 5209 } 5210 5211 _spdk_blob_sync_md(blob, cb_fn, cb_arg); 5212 } 5213 5214 /* END spdk_blob_sync_md */ 5215 5216 struct spdk_blob_insert_cluster_ctx { 5217 struct spdk_thread *thread; 5218 struct spdk_blob *blob; 5219 uint32_t cluster_num; /* cluster index in blob */ 5220 uint32_t cluster; /* cluster on disk */ 5221 int rc; 5222 spdk_blob_op_complete cb_fn; 5223 void *cb_arg; 5224 }; 5225 5226 static void 5227 _spdk_blob_insert_cluster_msg_cpl(void *arg) 5228 { 5229 struct spdk_blob_insert_cluster_ctx *ctx = arg; 5230 5231 ctx->cb_fn(ctx->cb_arg, ctx->rc); 5232 free(ctx); 5233 } 5234 5235 static void 5236 _spdk_blob_insert_cluster_msg_cb(void *arg, int bserrno) 5237 { 5238 struct spdk_blob_insert_cluster_ctx *ctx = arg; 5239 5240 ctx->rc = bserrno; 5241 spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx); 5242 } 5243 5244 static void 5245 _spdk_blob_insert_cluster_msg(void *arg) 5246 { 5247 struct spdk_blob_insert_cluster_ctx *ctx = arg; 5248 5249 ctx->rc = _spdk_blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster); 5250 if (ctx->rc != 0) { 5251 spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx); 5252 return; 5253 } 5254 5255 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 5256 _spdk_blob_sync_md(ctx->blob, _spdk_blob_insert_cluster_msg_cb, ctx); 5257 } 5258 5259 static void 5260 _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 5261 uint64_t cluster, spdk_blob_op_complete cb_fn, void *cb_arg) 5262 { 5263 struct spdk_blob_insert_cluster_ctx *ctx; 5264 5265 ctx = calloc(1, sizeof(*ctx)); 5266 if (ctx == NULL) { 5267 cb_fn(cb_arg, -ENOMEM); 5268 return; 5269 } 5270 5271 ctx->thread = spdk_get_thread(); 5272 ctx->blob = blob; 5273 ctx->cluster_num = cluster_num; 5274 ctx->cluster = cluster; 5275 ctx->cb_fn = cb_fn; 5276 ctx->cb_arg = cb_arg; 5277 5278 spdk_thread_send_msg(blob->bs->md_thread, _spdk_blob_insert_cluster_msg, ctx); 5279 } 5280 5281 /* START spdk_blob_close */ 5282 5283 static void 5284 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5285 { 5286 struct spdk_blob *blob = cb_arg; 5287 5288 if (bserrno == 0) { 5289 blob->open_ref--; 5290 if (blob->open_ref == 0) { 5291 /* 5292 * Blobs with active.num_pages == 0 are deleted blobs. 5293 * these blobs are removed from the blob_store list 5294 * when the deletion process starts - so don't try to 5295 * remove them again. 5296 */ 5297 if (blob->active.num_pages > 0) { 5298 TAILQ_REMOVE(&blob->bs->blobs, blob, link); 5299 } 5300 _spdk_blob_free(blob); 5301 } 5302 } 5303 5304 spdk_bs_sequence_finish(seq, bserrno); 5305 } 5306 5307 void spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 5308 { 5309 struct spdk_bs_cpl cpl; 5310 spdk_bs_sequence_t *seq; 5311 5312 _spdk_blob_verify_md_op(blob); 5313 5314 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Closing blob %lu\n", blob->id); 5315 5316 if (blob->open_ref == 0) { 5317 cb_fn(cb_arg, -EBADF); 5318 return; 5319 } 5320 5321 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 5322 cpl.u.blob_basic.cb_fn = cb_fn; 5323 cpl.u.blob_basic.cb_arg = cb_arg; 5324 5325 seq = spdk_bs_sequence_start(blob->bs->md_channel, &cpl); 5326 if (!seq) { 5327 cb_fn(cb_arg, -ENOMEM); 5328 return; 5329 } 5330 5331 /* Sync metadata */ 5332 _spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, blob); 5333 } 5334 5335 /* END spdk_blob_close */ 5336 5337 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 5338 { 5339 return spdk_get_io_channel(bs); 5340 } 5341 5342 void spdk_bs_free_io_channel(struct spdk_io_channel *channel) 5343 { 5344 spdk_put_io_channel(channel); 5345 } 5346 5347 void spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel, 5348 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 5349 { 5350 _spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 5351 SPDK_BLOB_UNMAP); 5352 } 5353 5354 void spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel, 5355 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 5356 { 5357 _spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 5358 SPDK_BLOB_WRITE_ZEROES); 5359 } 5360 5361 void spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel, 5362 void *payload, uint64_t offset, uint64_t length, 5363 spdk_blob_op_complete cb_fn, void *cb_arg) 5364 { 5365 _spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 5366 SPDK_BLOB_WRITE); 5367 } 5368 5369 void spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel, 5370 void *payload, uint64_t offset, uint64_t length, 5371 spdk_blob_op_complete cb_fn, void *cb_arg) 5372 { 5373 _spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 5374 SPDK_BLOB_READ); 5375 } 5376 5377 void spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel, 5378 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 5379 spdk_blob_op_complete cb_fn, void *cb_arg) 5380 { 5381 _spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false); 5382 } 5383 5384 void spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel, 5385 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 5386 spdk_blob_op_complete cb_fn, void *cb_arg) 5387 { 5388 _spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true); 5389 } 5390 5391 struct spdk_bs_iter_ctx { 5392 int64_t page_num; 5393 struct spdk_blob_store *bs; 5394 5395 spdk_blob_op_with_handle_complete cb_fn; 5396 void *cb_arg; 5397 }; 5398 5399 static void 5400 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5401 { 5402 struct spdk_bs_iter_ctx *ctx = cb_arg; 5403 struct spdk_blob_store *bs = ctx->bs; 5404 spdk_blob_id id; 5405 5406 if (bserrno == 0) { 5407 ctx->cb_fn(ctx->cb_arg, _blob, bserrno); 5408 free(ctx); 5409 return; 5410 } 5411 5412 ctx->page_num++; 5413 ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num); 5414 if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) { 5415 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT); 5416 free(ctx); 5417 return; 5418 } 5419 5420 id = _spdk_bs_page_to_blobid(ctx->page_num); 5421 5422 spdk_bs_open_blob(bs, id, _spdk_bs_iter_cpl, ctx); 5423 } 5424 5425 void 5426 spdk_bs_iter_first(struct spdk_blob_store *bs, 5427 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5428 { 5429 struct spdk_bs_iter_ctx *ctx; 5430 5431 ctx = calloc(1, sizeof(*ctx)); 5432 if (!ctx) { 5433 cb_fn(cb_arg, NULL, -ENOMEM); 5434 return; 5435 } 5436 5437 ctx->page_num = -1; 5438 ctx->bs = bs; 5439 ctx->cb_fn = cb_fn; 5440 ctx->cb_arg = cb_arg; 5441 5442 _spdk_bs_iter_cpl(ctx, NULL, -1); 5443 } 5444 5445 static void 5446 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno) 5447 { 5448 struct spdk_bs_iter_ctx *ctx = cb_arg; 5449 5450 _spdk_bs_iter_cpl(ctx, NULL, -1); 5451 } 5452 5453 void 5454 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob, 5455 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5456 { 5457 struct spdk_bs_iter_ctx *ctx; 5458 5459 assert(blob != NULL); 5460 5461 ctx = calloc(1, sizeof(*ctx)); 5462 if (!ctx) { 5463 cb_fn(cb_arg, NULL, -ENOMEM); 5464 return; 5465 } 5466 5467 ctx->page_num = _spdk_bs_blobid_to_page(blob->id); 5468 ctx->bs = bs; 5469 ctx->cb_fn = cb_fn; 5470 ctx->cb_arg = cb_arg; 5471 5472 /* Close the existing blob */ 5473 spdk_blob_close(blob, _spdk_bs_iter_close_cpl, ctx); 5474 } 5475 5476 static int 5477 _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 5478 uint16_t value_len, bool internal) 5479 { 5480 struct spdk_xattr_tailq *xattrs; 5481 struct spdk_xattr *xattr; 5482 5483 _spdk_blob_verify_md_op(blob); 5484 5485 if (blob->md_ro) { 5486 return -EPERM; 5487 } 5488 5489 if (internal) { 5490 xattrs = &blob->xattrs_internal; 5491 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR; 5492 } else { 5493 xattrs = &blob->xattrs; 5494 } 5495 5496 TAILQ_FOREACH(xattr, xattrs, link) { 5497 if (!strcmp(name, xattr->name)) { 5498 free(xattr->value); 5499 xattr->value_len = value_len; 5500 xattr->value = malloc(value_len); 5501 memcpy(xattr->value, value, value_len); 5502 5503 blob->state = SPDK_BLOB_STATE_DIRTY; 5504 5505 return 0; 5506 } 5507 } 5508 5509 xattr = calloc(1, sizeof(*xattr)); 5510 if (!xattr) { 5511 return -ENOMEM; 5512 } 5513 xattr->name = strdup(name); 5514 xattr->value_len = value_len; 5515 xattr->value = malloc(value_len); 5516 memcpy(xattr->value, value, value_len); 5517 TAILQ_INSERT_TAIL(xattrs, xattr, link); 5518 5519 blob->state = SPDK_BLOB_STATE_DIRTY; 5520 5521 return 0; 5522 } 5523 5524 int 5525 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 5526 uint16_t value_len) 5527 { 5528 return _spdk_blob_set_xattr(blob, name, value, value_len, false); 5529 } 5530 5531 static int 5532 _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal) 5533 { 5534 struct spdk_xattr_tailq *xattrs; 5535 struct spdk_xattr *xattr; 5536 5537 _spdk_blob_verify_md_op(blob); 5538 5539 if (blob->md_ro) { 5540 return -EPERM; 5541 } 5542 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 5543 5544 TAILQ_FOREACH(xattr, xattrs, link) { 5545 if (!strcmp(name, xattr->name)) { 5546 TAILQ_REMOVE(xattrs, xattr, link); 5547 free(xattr->value); 5548 free(xattr->name); 5549 free(xattr); 5550 5551 if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) { 5552 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR; 5553 } 5554 blob->state = SPDK_BLOB_STATE_DIRTY; 5555 5556 return 0; 5557 } 5558 } 5559 5560 return -ENOENT; 5561 } 5562 5563 int 5564 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name) 5565 { 5566 return _spdk_blob_remove_xattr(blob, name, false); 5567 } 5568 5569 static int 5570 _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 5571 const void **value, size_t *value_len, bool internal) 5572 { 5573 struct spdk_xattr *xattr; 5574 struct spdk_xattr_tailq *xattrs; 5575 5576 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 5577 5578 TAILQ_FOREACH(xattr, xattrs, link) { 5579 if (!strcmp(name, xattr->name)) { 5580 *value = xattr->value; 5581 *value_len = xattr->value_len; 5582 return 0; 5583 } 5584 } 5585 return -ENOENT; 5586 } 5587 5588 int 5589 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 5590 const void **value, size_t *value_len) 5591 { 5592 _spdk_blob_verify_md_op(blob); 5593 5594 return _spdk_blob_get_xattr_value(blob, name, value, value_len, false); 5595 } 5596 5597 struct spdk_xattr_names { 5598 uint32_t count; 5599 const char *names[0]; 5600 }; 5601 5602 static int 5603 _spdk_blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names) 5604 { 5605 struct spdk_xattr *xattr; 5606 int count = 0; 5607 5608 TAILQ_FOREACH(xattr, xattrs, link) { 5609 count++; 5610 } 5611 5612 *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *)); 5613 if (*names == NULL) { 5614 return -ENOMEM; 5615 } 5616 5617 TAILQ_FOREACH(xattr, xattrs, link) { 5618 (*names)->names[(*names)->count++] = xattr->name; 5619 } 5620 5621 return 0; 5622 } 5623 5624 int 5625 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names) 5626 { 5627 _spdk_blob_verify_md_op(blob); 5628 5629 return _spdk_blob_get_xattr_names(&blob->xattrs, names); 5630 } 5631 5632 uint32_t 5633 spdk_xattr_names_get_count(struct spdk_xattr_names *names) 5634 { 5635 assert(names != NULL); 5636 5637 return names->count; 5638 } 5639 5640 const char * 5641 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index) 5642 { 5643 if (index >= names->count) { 5644 return NULL; 5645 } 5646 5647 return names->names[index]; 5648 } 5649 5650 void 5651 spdk_xattr_names_free(struct spdk_xattr_names *names) 5652 { 5653 free(names); 5654 } 5655 5656 struct spdk_bs_type 5657 spdk_bs_get_bstype(struct spdk_blob_store *bs) 5658 { 5659 return bs->bstype; 5660 } 5661 5662 void 5663 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype) 5664 { 5665 memcpy(&bs->bstype, &bstype, sizeof(bstype)); 5666 } 5667 5668 bool 5669 spdk_blob_is_read_only(struct spdk_blob *blob) 5670 { 5671 assert(blob != NULL); 5672 return (blob->data_ro || blob->md_ro); 5673 } 5674 5675 bool 5676 spdk_blob_is_snapshot(struct spdk_blob *blob) 5677 { 5678 struct spdk_blob_list *snapshot_entry; 5679 5680 assert(blob != NULL); 5681 5682 TAILQ_FOREACH(snapshot_entry, &blob->bs->snapshots, link) { 5683 if (snapshot_entry->id == blob->id) { 5684 break; 5685 } 5686 } 5687 5688 if (snapshot_entry == NULL) { 5689 return false; 5690 } 5691 5692 return true; 5693 } 5694 5695 bool 5696 spdk_blob_is_clone(struct spdk_blob *blob) 5697 { 5698 assert(blob != NULL); 5699 5700 if (blob->parent_id != SPDK_BLOBID_INVALID) { 5701 assert(spdk_blob_is_thin_provisioned(blob)); 5702 return true; 5703 } 5704 5705 return false; 5706 } 5707 5708 bool 5709 spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 5710 { 5711 assert(blob != NULL); 5712 return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 5713 } 5714 5715 spdk_blob_id 5716 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id) 5717 { 5718 struct spdk_blob_list *snapshot_entry = NULL; 5719 struct spdk_blob_list *clone_entry = NULL; 5720 5721 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 5722 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 5723 if (clone_entry->id == blob_id) { 5724 return snapshot_entry->id; 5725 } 5726 } 5727 } 5728 5729 return SPDK_BLOBID_INVALID; 5730 } 5731 5732 int 5733 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids, 5734 size_t *count) 5735 { 5736 struct spdk_blob_list *snapshot_entry, *clone_entry; 5737 size_t n; 5738 5739 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 5740 if (snapshot_entry->id == blobid) { 5741 break; 5742 } 5743 } 5744 if (snapshot_entry == NULL) { 5745 *count = 0; 5746 return 0; 5747 } 5748 5749 if (ids == NULL || *count < snapshot_entry->clone_count) { 5750 *count = snapshot_entry->clone_count; 5751 return -ENOMEM; 5752 } 5753 *count = snapshot_entry->clone_count; 5754 5755 n = 0; 5756 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 5757 ids[n++] = clone_entry->id; 5758 } 5759 5760 return 0; 5761 } 5762 5763 SPDK_LOG_REGISTER_COMPONENT("blob", SPDK_LOG_BLOB) 5764