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 opts->clear_method = BS_CLEAR_WITH_UNMAP; 2425 memset(&opts->bstype, 0, sizeof(opts->bstype)); 2426 opts->iter_cb_fn = NULL; 2427 opts->iter_cb_arg = NULL; 2428 } 2429 2430 static int 2431 _spdk_bs_opts_verify(struct spdk_bs_opts *opts) 2432 { 2433 if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 || 2434 opts->max_channel_ops == 0) { 2435 SPDK_ERRLOG("Blobstore options cannot be set to 0\n"); 2436 return -1; 2437 } 2438 2439 return 0; 2440 } 2441 2442 static int 2443 _spdk_bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs) 2444 { 2445 struct spdk_blob_store *bs; 2446 uint64_t dev_size; 2447 int rc; 2448 2449 dev_size = dev->blocklen * dev->blockcnt; 2450 if (dev_size < opts->cluster_sz) { 2451 /* Device size cannot be smaller than cluster size of blobstore */ 2452 SPDK_INFOLOG(SPDK_LOG_BLOB, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n", 2453 dev_size, opts->cluster_sz); 2454 return -ENOSPC; 2455 } 2456 if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) { 2457 /* Cluster size cannot be smaller than page size */ 2458 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n", 2459 opts->cluster_sz, SPDK_BS_PAGE_SIZE); 2460 return -EINVAL; 2461 } 2462 bs = calloc(1, sizeof(struct spdk_blob_store)); 2463 if (!bs) { 2464 return -ENOMEM; 2465 } 2466 2467 TAILQ_INIT(&bs->blobs); 2468 TAILQ_INIT(&bs->snapshots); 2469 bs->dev = dev; 2470 bs->md_thread = spdk_get_thread(); 2471 assert(bs->md_thread != NULL); 2472 2473 /* 2474 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an 2475 * even multiple of the cluster size. 2476 */ 2477 bs->cluster_sz = opts->cluster_sz; 2478 bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen); 2479 bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE; 2480 bs->num_free_clusters = bs->total_clusters; 2481 bs->used_clusters = spdk_bit_array_create(bs->total_clusters); 2482 bs->io_unit_size = dev->blocklen; 2483 if (bs->used_clusters == NULL) { 2484 free(bs); 2485 return -ENOMEM; 2486 } 2487 2488 bs->max_channel_ops = opts->max_channel_ops; 2489 bs->super_blob = SPDK_BLOBID_INVALID; 2490 memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype)); 2491 2492 /* The metadata is assumed to be at least 1 page */ 2493 bs->used_md_pages = spdk_bit_array_create(1); 2494 bs->used_blobids = spdk_bit_array_create(0); 2495 2496 pthread_mutex_init(&bs->used_clusters_mutex, NULL); 2497 2498 spdk_io_device_register(bs, _spdk_bs_channel_create, _spdk_bs_channel_destroy, 2499 sizeof(struct spdk_bs_channel), "blobstore"); 2500 rc = spdk_bs_register_md_thread(bs); 2501 if (rc == -1) { 2502 spdk_io_device_unregister(bs, NULL); 2503 pthread_mutex_destroy(&bs->used_clusters_mutex); 2504 spdk_bit_array_free(&bs->used_blobids); 2505 spdk_bit_array_free(&bs->used_md_pages); 2506 spdk_bit_array_free(&bs->used_clusters); 2507 free(bs); 2508 /* FIXME: this is a lie but don't know how to get a proper error code here */ 2509 return -ENOMEM; 2510 } 2511 2512 *_bs = bs; 2513 return 0; 2514 } 2515 2516 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */ 2517 2518 struct spdk_bs_load_ctx { 2519 struct spdk_blob_store *bs; 2520 struct spdk_bs_super_block *super; 2521 2522 struct spdk_bs_md_mask *mask; 2523 bool in_page_chain; 2524 uint32_t page_index; 2525 uint32_t cur_page; 2526 struct spdk_blob_md_page *page; 2527 2528 spdk_bs_sequence_t *seq; 2529 spdk_blob_op_with_handle_complete iter_cb_fn; 2530 void *iter_cb_arg; 2531 }; 2532 2533 static void 2534 _spdk_bs_load_ctx_fail(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 2535 { 2536 assert(bserrno != 0); 2537 2538 spdk_dma_free(ctx->super); 2539 spdk_bs_sequence_finish(seq, bserrno); 2540 _spdk_bs_free(ctx->bs); 2541 free(ctx); 2542 } 2543 2544 static void 2545 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask) 2546 { 2547 uint32_t i = 0; 2548 2549 while (true) { 2550 i = spdk_bit_array_find_first_set(array, i); 2551 if (i >= mask->length) { 2552 break; 2553 } 2554 mask->mask[i / 8] |= 1U << (i % 8); 2555 i++; 2556 } 2557 } 2558 2559 static int 2560 _spdk_bs_load_mask(struct spdk_bit_array **array_ptr, struct spdk_bs_md_mask *mask) 2561 { 2562 struct spdk_bit_array *array; 2563 uint32_t i; 2564 2565 if (spdk_bit_array_resize(array_ptr, mask->length) < 0) { 2566 return -ENOMEM; 2567 } 2568 2569 array = *array_ptr; 2570 for (i = 0; i < mask->length; i++) { 2571 if (mask->mask[i / 8] & (1U << (i % 8))) { 2572 spdk_bit_array_set(array, i); 2573 } 2574 } 2575 2576 return 0; 2577 } 2578 2579 static void 2580 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 2581 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 2582 { 2583 /* Update the values in the super block */ 2584 super->super_blob = bs->super_blob; 2585 memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype)); 2586 super->crc = _spdk_blob_md_page_calc_crc(super); 2587 spdk_bs_sequence_write_dev(seq, super, _spdk_bs_page_to_lba(bs, 0), 2588 _spdk_bs_byte_to_lba(bs, sizeof(*super)), 2589 cb_fn, cb_arg); 2590 } 2591 2592 static void 2593 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 2594 { 2595 struct spdk_bs_load_ctx *ctx = arg; 2596 uint64_t mask_size, lba, lba_count; 2597 2598 /* Write out the used clusters mask */ 2599 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 2600 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2601 if (!ctx->mask) { 2602 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2603 return; 2604 } 2605 2606 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS; 2607 ctx->mask->length = ctx->bs->total_clusters; 2608 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters)); 2609 2610 _spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask); 2611 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 2612 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 2613 spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 2614 } 2615 2616 static void 2617 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 2618 { 2619 struct spdk_bs_load_ctx *ctx = arg; 2620 uint64_t mask_size, lba, lba_count; 2621 2622 if (seq->bserrno) { 2623 _spdk_bs_load_ctx_fail(seq, ctx, seq->bserrno); 2624 return; 2625 } 2626 2627 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 2628 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2629 if (!ctx->mask) { 2630 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2631 return; 2632 } 2633 2634 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES; 2635 ctx->mask->length = ctx->super->md_len; 2636 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages)); 2637 2638 _spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask); 2639 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 2640 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 2641 spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 2642 } 2643 2644 static void 2645 _spdk_bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 2646 { 2647 struct spdk_bs_load_ctx *ctx = arg; 2648 uint64_t mask_size, lba, lba_count; 2649 2650 if (ctx->super->used_blobid_mask_len == 0) { 2651 /* 2652 * This is a pre-v3 on-disk format where the blobid mask does not get 2653 * written to disk. 2654 */ 2655 cb_fn(seq, arg, 0); 2656 return; 2657 } 2658 2659 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 2660 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2661 if (!ctx->mask) { 2662 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2663 return; 2664 } 2665 2666 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS; 2667 ctx->mask->length = ctx->super->md_len; 2668 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids)); 2669 2670 _spdk_bs_set_mask(ctx->bs->used_blobids, ctx->mask); 2671 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 2672 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 2673 spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg); 2674 } 2675 2676 static void 2677 _spdk_bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno) 2678 { 2679 struct spdk_bs_load_ctx *ctx = arg; 2680 2681 if (bserrno == 0) { 2682 if (ctx->iter_cb_fn) { 2683 ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0); 2684 } 2685 _spdk_bs_blob_list_add(blob); 2686 spdk_bs_iter_next(ctx->bs, blob, _spdk_bs_load_iter, ctx); 2687 return; 2688 } 2689 2690 if (bserrno == -ENOENT) { 2691 bserrno = 0; 2692 } else { 2693 /* 2694 * This case needs to be looked at further. Same problem 2695 * exists with applications that rely on explicit blob 2696 * iteration. We should just skip the blob that failed 2697 * to load and continue on to the next one. 2698 */ 2699 SPDK_ERRLOG("Error in iterating blobs\n"); 2700 } 2701 2702 ctx->iter_cb_fn = NULL; 2703 2704 spdk_dma_free(ctx->super); 2705 spdk_dma_free(ctx->mask); 2706 spdk_bs_sequence_finish(ctx->seq, bserrno); 2707 free(ctx); 2708 } 2709 2710 static void 2711 _spdk_bs_load_complete(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno) 2712 { 2713 ctx->seq = seq; 2714 spdk_bs_iter_first(ctx->bs, _spdk_bs_load_iter, ctx); 2715 } 2716 2717 static void 2718 _spdk_bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2719 { 2720 struct spdk_bs_load_ctx *ctx = cb_arg; 2721 int rc; 2722 2723 /* The type must be correct */ 2724 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS); 2725 2726 /* The length of the mask (in bits) must not be greater than 2727 * the length of the buffer (converted to bits) */ 2728 assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8)); 2729 2730 /* The length of the mask must be exactly equal to the size 2731 * (in pages) of the metadata region */ 2732 assert(ctx->mask->length == ctx->super->md_len); 2733 2734 rc = _spdk_bs_load_mask(&ctx->bs->used_blobids, ctx->mask); 2735 if (rc < 0) { 2736 spdk_dma_free(ctx->mask); 2737 _spdk_bs_load_ctx_fail(seq, ctx, rc); 2738 return; 2739 } 2740 2741 _spdk_bs_load_complete(seq, ctx, bserrno); 2742 } 2743 2744 static void 2745 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2746 { 2747 struct spdk_bs_load_ctx *ctx = cb_arg; 2748 uint64_t lba, lba_count, mask_size; 2749 int rc; 2750 2751 /* The type must be correct */ 2752 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 2753 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 2754 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 2755 struct spdk_blob_md_page) * 8)); 2756 /* The length of the mask must be exactly equal to the total number of clusters */ 2757 assert(ctx->mask->length == ctx->bs->total_clusters); 2758 2759 rc = _spdk_bs_load_mask(&ctx->bs->used_clusters, ctx->mask); 2760 if (rc < 0) { 2761 spdk_dma_free(ctx->mask); 2762 _spdk_bs_load_ctx_fail(seq, ctx, rc); 2763 return; 2764 } 2765 2766 ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->bs->used_clusters); 2767 assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters); 2768 2769 spdk_dma_free(ctx->mask); 2770 2771 /* Read the used blobids mask */ 2772 mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE; 2773 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2774 if (!ctx->mask) { 2775 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2776 return; 2777 } 2778 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start); 2779 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len); 2780 spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 2781 _spdk_bs_load_used_blobids_cpl, ctx); 2782 } 2783 2784 static void 2785 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2786 { 2787 struct spdk_bs_load_ctx *ctx = cb_arg; 2788 uint64_t lba, lba_count, mask_size; 2789 int rc; 2790 2791 /* The type must be correct */ 2792 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES); 2793 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 2794 assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE * 2795 8)); 2796 /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */ 2797 assert(ctx->mask->length == ctx->super->md_len); 2798 2799 rc = _spdk_bs_load_mask(&ctx->bs->used_md_pages, ctx->mask); 2800 if (rc < 0) { 2801 spdk_dma_free(ctx->mask); 2802 _spdk_bs_load_ctx_fail(seq, ctx, rc); 2803 return; 2804 } 2805 2806 spdk_dma_free(ctx->mask); 2807 2808 /* Read the used clusters mask */ 2809 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 2810 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2811 if (!ctx->mask) { 2812 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2813 return; 2814 } 2815 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 2816 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 2817 spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 2818 _spdk_bs_load_used_clusters_cpl, ctx); 2819 } 2820 2821 static void 2822 _spdk_bs_load_read_used_pages(spdk_bs_sequence_t *seq, void *cb_arg) 2823 { 2824 struct spdk_bs_load_ctx *ctx = cb_arg; 2825 uint64_t lba, lba_count, mask_size; 2826 2827 /* Read the used pages mask */ 2828 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 2829 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 2830 if (!ctx->mask) { 2831 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 2832 return; 2833 } 2834 2835 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 2836 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 2837 spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count, 2838 _spdk_bs_load_used_pages_cpl, ctx); 2839 } 2840 2841 static int 2842 _spdk_bs_load_replay_md_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob_store *bs) 2843 { 2844 struct spdk_blob_md_descriptor *desc; 2845 size_t cur_desc = 0; 2846 2847 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 2848 while (cur_desc < sizeof(page->descriptors)) { 2849 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 2850 if (desc->length == 0) { 2851 /* If padding and length are 0, this terminates the page */ 2852 break; 2853 } 2854 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 2855 struct spdk_blob_md_descriptor_extent *desc_extent; 2856 unsigned int i, j; 2857 unsigned int cluster_count = 0; 2858 uint32_t cluster_idx; 2859 2860 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 2861 2862 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 2863 for (j = 0; j < desc_extent->extents[i].length; j++) { 2864 cluster_idx = desc_extent->extents[i].cluster_idx; 2865 /* 2866 * cluster_idx = 0 means an unallocated cluster - don't mark that 2867 * in the used cluster map. 2868 */ 2869 if (cluster_idx != 0) { 2870 spdk_bit_array_set(bs->used_clusters, cluster_idx + j); 2871 if (bs->num_free_clusters == 0) { 2872 return -ENOSPC; 2873 } 2874 bs->num_free_clusters--; 2875 } 2876 cluster_count++; 2877 } 2878 } 2879 if (cluster_count == 0) { 2880 return -EINVAL; 2881 } 2882 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 2883 /* Skip this item */ 2884 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 2885 /* Skip this item */ 2886 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 2887 /* Skip this item */ 2888 } else { 2889 /* Error */ 2890 return -EINVAL; 2891 } 2892 /* Advance to the next descriptor */ 2893 cur_desc += sizeof(*desc) + desc->length; 2894 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 2895 break; 2896 } 2897 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 2898 } 2899 return 0; 2900 } 2901 2902 static bool _spdk_bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx) 2903 { 2904 uint32_t crc; 2905 2906 crc = _spdk_blob_md_page_calc_crc(ctx->page); 2907 if (crc != ctx->page->crc) { 2908 return false; 2909 } 2910 2911 if (_spdk_bs_page_to_blobid(ctx->cur_page) != ctx->page->id) { 2912 return false; 2913 } 2914 return true; 2915 } 2916 2917 static void 2918 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 2919 2920 static void 2921 _spdk_bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2922 { 2923 struct spdk_bs_load_ctx *ctx = cb_arg; 2924 2925 _spdk_bs_load_complete(seq, ctx, bserrno); 2926 } 2927 2928 static void 2929 _spdk_bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2930 { 2931 struct spdk_bs_load_ctx *ctx = cb_arg; 2932 2933 spdk_dma_free(ctx->mask); 2934 ctx->mask = NULL; 2935 2936 _spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_load_write_used_clusters_cpl); 2937 } 2938 2939 static void 2940 _spdk_bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2941 { 2942 struct spdk_bs_load_ctx *ctx = cb_arg; 2943 2944 spdk_dma_free(ctx->mask); 2945 ctx->mask = NULL; 2946 2947 _spdk_bs_write_used_blobids(seq, cb_arg, _spdk_bs_load_write_used_blobids_cpl); 2948 } 2949 2950 static void 2951 _spdk_bs_load_write_used_md(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2952 { 2953 _spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_load_write_used_pages_cpl); 2954 } 2955 2956 static void 2957 _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2958 { 2959 struct spdk_bs_load_ctx *ctx = cb_arg; 2960 uint64_t num_md_clusters; 2961 uint64_t i; 2962 uint32_t page_num; 2963 2964 if (bserrno != 0) { 2965 _spdk_bs_load_ctx_fail(seq, ctx, bserrno); 2966 return; 2967 } 2968 2969 page_num = ctx->cur_page; 2970 if (_spdk_bs_load_cur_md_page_valid(ctx) == true) { 2971 if (ctx->page->sequence_num == 0 || ctx->in_page_chain == true) { 2972 spdk_bit_array_set(ctx->bs->used_md_pages, page_num); 2973 if (ctx->page->sequence_num == 0) { 2974 spdk_bit_array_set(ctx->bs->used_blobids, page_num); 2975 } 2976 if (_spdk_bs_load_replay_md_parse_page(ctx->page, ctx->bs)) { 2977 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 2978 return; 2979 } 2980 if (ctx->page->next != SPDK_INVALID_MD_PAGE) { 2981 ctx->in_page_chain = true; 2982 ctx->cur_page = ctx->page->next; 2983 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 2984 return; 2985 } 2986 } 2987 } 2988 2989 ctx->in_page_chain = false; 2990 2991 do { 2992 ctx->page_index++; 2993 } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true); 2994 2995 if (ctx->page_index < ctx->super->md_len) { 2996 ctx->cur_page = ctx->page_index; 2997 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 2998 } else { 2999 /* Claim all of the clusters used by the metadata */ 3000 num_md_clusters = spdk_divide_round_up(ctx->super->md_len, ctx->bs->pages_per_cluster); 3001 for (i = 0; i < num_md_clusters; i++) { 3002 _spdk_bs_claim_cluster(ctx->bs, i); 3003 } 3004 spdk_dma_free(ctx->page); 3005 _spdk_bs_load_write_used_md(seq, ctx, bserrno); 3006 } 3007 } 3008 3009 static void 3010 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 3011 { 3012 struct spdk_bs_load_ctx *ctx = cb_arg; 3013 uint64_t lba; 3014 3015 assert(ctx->cur_page < ctx->super->md_len); 3016 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 3017 spdk_bs_sequence_read_dev(seq, ctx->page, lba, 3018 _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 3019 _spdk_bs_load_replay_md_cpl, ctx); 3020 } 3021 3022 static void 3023 _spdk_bs_load_replay_md(spdk_bs_sequence_t *seq, void *cb_arg) 3024 { 3025 struct spdk_bs_load_ctx *ctx = cb_arg; 3026 3027 ctx->page_index = 0; 3028 ctx->cur_page = 0; 3029 ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE, 3030 SPDK_BS_PAGE_SIZE, 3031 NULL); 3032 if (!ctx->page) { 3033 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3034 return; 3035 } 3036 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 3037 } 3038 3039 static void 3040 _spdk_bs_recover(spdk_bs_sequence_t *seq, void *cb_arg) 3041 { 3042 struct spdk_bs_load_ctx *ctx = cb_arg; 3043 int rc; 3044 3045 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); 3046 if (rc < 0) { 3047 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3048 return; 3049 } 3050 3051 rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len); 3052 if (rc < 0) { 3053 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3054 return; 3055 } 3056 3057 rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); 3058 if (rc < 0) { 3059 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3060 return; 3061 } 3062 3063 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 3064 _spdk_bs_load_replay_md(seq, cb_arg); 3065 } 3066 3067 static void 3068 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3069 { 3070 struct spdk_bs_load_ctx *ctx = cb_arg; 3071 uint32_t crc; 3072 int rc; 3073 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 3074 3075 if (ctx->super->version > SPDK_BS_VERSION || 3076 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 3077 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3078 return; 3079 } 3080 3081 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 3082 sizeof(ctx->super->signature)) != 0) { 3083 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3084 return; 3085 } 3086 3087 crc = _spdk_blob_md_page_calc_crc(ctx->super); 3088 if (crc != ctx->super->crc) { 3089 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3090 return; 3091 } 3092 3093 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 3094 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype matched - loading blobstore\n"); 3095 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 3096 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n"); 3097 } else { 3098 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Unexpected bstype\n"); 3099 SPDK_LOGDUMP(SPDK_LOG_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 3100 SPDK_LOGDUMP(SPDK_LOG_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 3101 _spdk_bs_load_ctx_fail(seq, ctx, -ENXIO); 3102 return; 3103 } 3104 3105 if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) { 3106 SPDK_NOTICELOG("Size mismatch, dev size: %lu, blobstore size: %lu\n", 3107 ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size); 3108 _spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ); 3109 return; 3110 } 3111 3112 if (ctx->super->size == 0) { 3113 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen; 3114 } 3115 3116 if (ctx->super->io_unit_size == 0) { 3117 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE; 3118 } 3119 3120 /* Parse the super block */ 3121 ctx->bs->clean = 1; 3122 ctx->bs->cluster_sz = ctx->super->cluster_size; 3123 ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size; 3124 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 3125 ctx->bs->io_unit_size = ctx->super->io_unit_size; 3126 rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); 3127 if (rc < 0) { 3128 _spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM); 3129 return; 3130 } 3131 ctx->bs->md_start = ctx->super->md_start; 3132 ctx->bs->md_len = ctx->super->md_len; 3133 ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up( 3134 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 3135 ctx->bs->super_blob = ctx->super->super_blob; 3136 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 3137 3138 if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) { 3139 _spdk_bs_recover(seq, ctx); 3140 } else { 3141 _spdk_bs_load_read_used_pages(seq, ctx); 3142 } 3143 } 3144 3145 void 3146 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 3147 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 3148 { 3149 struct spdk_blob_store *bs; 3150 struct spdk_bs_cpl cpl; 3151 spdk_bs_sequence_t *seq; 3152 struct spdk_bs_load_ctx *ctx; 3153 struct spdk_bs_opts opts = {}; 3154 int err; 3155 3156 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Loading blobstore from dev %p\n", dev); 3157 3158 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 3159 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "unsupported dev block length of %d\n", dev->blocklen); 3160 dev->destroy(dev); 3161 cb_fn(cb_arg, NULL, -EINVAL); 3162 return; 3163 } 3164 3165 if (o) { 3166 opts = *o; 3167 } else { 3168 spdk_bs_opts_init(&opts); 3169 } 3170 3171 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 3172 dev->destroy(dev); 3173 cb_fn(cb_arg, NULL, -EINVAL); 3174 return; 3175 } 3176 3177 err = _spdk_bs_alloc(dev, &opts, &bs); 3178 if (err) { 3179 dev->destroy(dev); 3180 cb_fn(cb_arg, NULL, err); 3181 return; 3182 } 3183 3184 ctx = calloc(1, sizeof(*ctx)); 3185 if (!ctx) { 3186 _spdk_bs_free(bs); 3187 cb_fn(cb_arg, NULL, -ENOMEM); 3188 return; 3189 } 3190 3191 ctx->bs = bs; 3192 ctx->iter_cb_fn = opts.iter_cb_fn; 3193 ctx->iter_cb_arg = opts.iter_cb_arg; 3194 3195 /* Allocate memory for the super block */ 3196 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3197 if (!ctx->super) { 3198 free(ctx); 3199 _spdk_bs_free(bs); 3200 cb_fn(cb_arg, NULL, -ENOMEM); 3201 return; 3202 } 3203 3204 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 3205 cpl.u.bs_handle.cb_fn = cb_fn; 3206 cpl.u.bs_handle.cb_arg = cb_arg; 3207 cpl.u.bs_handle.bs = bs; 3208 3209 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3210 if (!seq) { 3211 spdk_dma_free(ctx->super); 3212 free(ctx); 3213 _spdk_bs_free(bs); 3214 cb_fn(cb_arg, NULL, -ENOMEM); 3215 return; 3216 } 3217 3218 /* Read the super block */ 3219 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3220 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3221 _spdk_bs_load_super_cpl, ctx); 3222 } 3223 3224 /* END spdk_bs_load */ 3225 3226 /* START spdk_bs_dump */ 3227 3228 struct spdk_bs_dump_ctx { 3229 struct spdk_blob_store *bs; 3230 struct spdk_bs_super_block *super; 3231 uint32_t cur_page; 3232 struct spdk_blob_md_page *page; 3233 spdk_bs_sequence_t *seq; 3234 FILE *fp; 3235 spdk_bs_dump_print_xattr print_xattr_fn; 3236 char xattr_name[4096]; 3237 }; 3238 3239 static void 3240 _spdk_bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_dump_ctx *ctx, int bserrno) 3241 { 3242 spdk_dma_free(ctx->super); 3243 3244 /* 3245 * We need to defer calling spdk_bs_call_cpl() until after 3246 * dev destruction, so tuck these away for later use. 3247 */ 3248 ctx->bs->unload_err = bserrno; 3249 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 3250 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 3251 3252 spdk_bs_sequence_finish(seq, 0); 3253 _spdk_bs_free(ctx->bs); 3254 free(ctx); 3255 } 3256 3257 static void _spdk_bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 3258 3259 static void 3260 _spdk_bs_dump_print_md_page(struct spdk_bs_dump_ctx *ctx) 3261 { 3262 uint32_t page_idx = ctx->cur_page; 3263 struct spdk_blob_md_page *page = ctx->page; 3264 struct spdk_blob_md_descriptor *desc; 3265 size_t cur_desc = 0; 3266 uint32_t crc; 3267 3268 fprintf(ctx->fp, "=========\n"); 3269 fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx); 3270 fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id); 3271 3272 crc = _spdk_blob_md_page_calc_crc(page); 3273 fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch"); 3274 3275 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 3276 while (cur_desc < sizeof(page->descriptors)) { 3277 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 3278 if (desc->length == 0) { 3279 /* If padding and length are 0, this terminates the page */ 3280 break; 3281 } 3282 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 3283 struct spdk_blob_md_descriptor_extent *desc_extent; 3284 unsigned int i; 3285 3286 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 3287 3288 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 3289 if (desc_extent->extents[i].cluster_idx != 0) { 3290 fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32, 3291 desc_extent->extents[i].cluster_idx); 3292 } else { 3293 fprintf(ctx->fp, "Unallocated Extent - "); 3294 } 3295 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent->extents[i].length); 3296 fprintf(ctx->fp, "\n"); 3297 } 3298 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 3299 struct spdk_blob_md_descriptor_xattr *desc_xattr; 3300 uint32_t i; 3301 3302 desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc; 3303 3304 if (desc_xattr->length != 3305 sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) + 3306 desc_xattr->name_length + desc_xattr->value_length) { 3307 } 3308 3309 memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length); 3310 ctx->xattr_name[desc_xattr->name_length] = '\0'; 3311 fprintf(ctx->fp, "XATTR: name = \"%s\"\n", ctx->xattr_name); 3312 fprintf(ctx->fp, " value = \""); 3313 ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name, 3314 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 3315 desc_xattr->value_length); 3316 fprintf(ctx->fp, "\"\n"); 3317 for (i = 0; i < desc_xattr->value_length; i++) { 3318 if (i % 16 == 0) { 3319 fprintf(ctx->fp, " "); 3320 } 3321 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i)); 3322 if ((i + 1) % 16 == 0) { 3323 fprintf(ctx->fp, "\n"); 3324 } 3325 } 3326 if (i % 16 != 0) { 3327 fprintf(ctx->fp, "\n"); 3328 } 3329 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) { 3330 /* TODO */ 3331 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) { 3332 /* TODO */ 3333 } else { 3334 /* Error */ 3335 } 3336 /* Advance to the next descriptor */ 3337 cur_desc += sizeof(*desc) + desc->length; 3338 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 3339 break; 3340 } 3341 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 3342 } 3343 } 3344 3345 static void 3346 _spdk_bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3347 { 3348 struct spdk_bs_dump_ctx *ctx = cb_arg; 3349 3350 if (bserrno != 0) { 3351 _spdk_bs_dump_finish(seq, ctx, bserrno); 3352 return; 3353 } 3354 3355 if (ctx->page->id != 0) { 3356 _spdk_bs_dump_print_md_page(ctx); 3357 } 3358 3359 ctx->cur_page++; 3360 3361 if (ctx->cur_page < ctx->super->md_len) { 3362 _spdk_bs_dump_read_md_page(seq, cb_arg); 3363 } else { 3364 spdk_dma_free(ctx->page); 3365 _spdk_bs_dump_finish(seq, ctx, 0); 3366 } 3367 } 3368 3369 static void 3370 _spdk_bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 3371 { 3372 struct spdk_bs_dump_ctx *ctx = cb_arg; 3373 uint64_t lba; 3374 3375 assert(ctx->cur_page < ctx->super->md_len); 3376 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 3377 spdk_bs_sequence_read_dev(seq, ctx->page, lba, 3378 _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 3379 _spdk_bs_dump_read_md_page_cpl, ctx); 3380 } 3381 3382 static void 3383 _spdk_bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3384 { 3385 struct spdk_bs_dump_ctx *ctx = cb_arg; 3386 3387 fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature); 3388 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 3389 sizeof(ctx->super->signature)) != 0) { 3390 fprintf(ctx->fp, "(Mismatch)\n"); 3391 _spdk_bs_dump_finish(seq, ctx, bserrno); 3392 return; 3393 } else { 3394 fprintf(ctx->fp, "(OK)\n"); 3395 } 3396 fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version); 3397 fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc, 3398 (ctx->super->crc == _spdk_blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch"); 3399 fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype); 3400 fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size); 3401 fprintf(ctx->fp, "Super Blob ID: "); 3402 if (ctx->super->super_blob == SPDK_BLOBID_INVALID) { 3403 fprintf(ctx->fp, "(None)\n"); 3404 } else { 3405 fprintf(ctx->fp, "%" PRIu64 "\n", ctx->super->super_blob); 3406 } 3407 fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean); 3408 fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start); 3409 fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len); 3410 fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start); 3411 fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len); 3412 fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start); 3413 fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len); 3414 fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start); 3415 fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len); 3416 3417 ctx->cur_page = 0; 3418 ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE, 3419 SPDK_BS_PAGE_SIZE, 3420 NULL); 3421 if (!ctx->page) { 3422 _spdk_bs_dump_finish(seq, ctx, -ENOMEM); 3423 return; 3424 } 3425 _spdk_bs_dump_read_md_page(seq, cb_arg); 3426 } 3427 3428 void 3429 spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn, 3430 spdk_bs_op_complete cb_fn, void *cb_arg) 3431 { 3432 struct spdk_blob_store *bs; 3433 struct spdk_bs_cpl cpl; 3434 spdk_bs_sequence_t *seq; 3435 struct spdk_bs_dump_ctx *ctx; 3436 struct spdk_bs_opts opts = {}; 3437 int err; 3438 3439 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Dumping blobstore from dev %p\n", dev); 3440 3441 spdk_bs_opts_init(&opts); 3442 3443 err = _spdk_bs_alloc(dev, &opts, &bs); 3444 if (err) { 3445 dev->destroy(dev); 3446 cb_fn(cb_arg, err); 3447 return; 3448 } 3449 3450 ctx = calloc(1, sizeof(*ctx)); 3451 if (!ctx) { 3452 _spdk_bs_free(bs); 3453 cb_fn(cb_arg, -ENOMEM); 3454 return; 3455 } 3456 3457 ctx->bs = bs; 3458 ctx->fp = fp; 3459 ctx->print_xattr_fn = print_xattr_fn; 3460 3461 /* Allocate memory for the super block */ 3462 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3463 if (!ctx->super) { 3464 free(ctx); 3465 _spdk_bs_free(bs); 3466 cb_fn(cb_arg, -ENOMEM); 3467 return; 3468 } 3469 3470 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3471 cpl.u.bs_basic.cb_fn = cb_fn; 3472 cpl.u.bs_basic.cb_arg = cb_arg; 3473 3474 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3475 if (!seq) { 3476 spdk_dma_free(ctx->super); 3477 free(ctx); 3478 _spdk_bs_free(bs); 3479 cb_fn(cb_arg, -ENOMEM); 3480 return; 3481 } 3482 3483 /* Read the super block */ 3484 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3485 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3486 _spdk_bs_dump_super_cpl, ctx); 3487 } 3488 3489 /* END spdk_bs_dump */ 3490 3491 /* START spdk_bs_init */ 3492 3493 struct spdk_bs_init_ctx { 3494 struct spdk_blob_store *bs; 3495 struct spdk_bs_super_block *super; 3496 }; 3497 3498 static void 3499 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3500 { 3501 struct spdk_bs_init_ctx *ctx = cb_arg; 3502 3503 spdk_dma_free(ctx->super); 3504 free(ctx); 3505 3506 spdk_bs_sequence_finish(seq, bserrno); 3507 } 3508 3509 static void 3510 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3511 { 3512 struct spdk_bs_init_ctx *ctx = cb_arg; 3513 3514 /* Write super block */ 3515 spdk_bs_sequence_write_dev(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0), 3516 _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 3517 _spdk_bs_init_persist_super_cpl, ctx); 3518 } 3519 3520 void 3521 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 3522 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 3523 { 3524 struct spdk_bs_init_ctx *ctx; 3525 struct spdk_blob_store *bs; 3526 struct spdk_bs_cpl cpl; 3527 spdk_bs_sequence_t *seq; 3528 spdk_bs_batch_t *batch; 3529 uint64_t num_md_lba; 3530 uint64_t num_md_pages; 3531 uint64_t num_md_clusters; 3532 uint32_t i; 3533 struct spdk_bs_opts opts = {}; 3534 int rc; 3535 3536 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Initializing blobstore on dev %p\n", dev); 3537 3538 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 3539 SPDK_ERRLOG("unsupported dev block length of %d\n", 3540 dev->blocklen); 3541 dev->destroy(dev); 3542 cb_fn(cb_arg, NULL, -EINVAL); 3543 return; 3544 } 3545 3546 if (o) { 3547 opts = *o; 3548 } else { 3549 spdk_bs_opts_init(&opts); 3550 } 3551 3552 if (_spdk_bs_opts_verify(&opts) != 0) { 3553 dev->destroy(dev); 3554 cb_fn(cb_arg, NULL, -EINVAL); 3555 return; 3556 } 3557 3558 rc = _spdk_bs_alloc(dev, &opts, &bs); 3559 if (rc) { 3560 dev->destroy(dev); 3561 cb_fn(cb_arg, NULL, rc); 3562 return; 3563 } 3564 3565 if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) { 3566 /* By default, allocate 1 page per cluster. 3567 * Technically, this over-allocates metadata 3568 * because more metadata will reduce the number 3569 * of usable clusters. This can be addressed with 3570 * more complex math in the future. 3571 */ 3572 bs->md_len = bs->total_clusters; 3573 } else { 3574 bs->md_len = opts.num_md_pages; 3575 } 3576 rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len); 3577 if (rc < 0) { 3578 _spdk_bs_free(bs); 3579 cb_fn(cb_arg, NULL, -ENOMEM); 3580 return; 3581 } 3582 3583 rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len); 3584 if (rc < 0) { 3585 _spdk_bs_free(bs); 3586 cb_fn(cb_arg, NULL, -ENOMEM); 3587 return; 3588 } 3589 3590 ctx = calloc(1, sizeof(*ctx)); 3591 if (!ctx) { 3592 _spdk_bs_free(bs); 3593 cb_fn(cb_arg, NULL, -ENOMEM); 3594 return; 3595 } 3596 3597 ctx->bs = bs; 3598 3599 /* Allocate memory for the super block */ 3600 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3601 if (!ctx->super) { 3602 free(ctx); 3603 _spdk_bs_free(bs); 3604 cb_fn(cb_arg, NULL, -ENOMEM); 3605 return; 3606 } 3607 memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 3608 sizeof(ctx->super->signature)); 3609 ctx->super->version = SPDK_BS_VERSION; 3610 ctx->super->length = sizeof(*ctx->super); 3611 ctx->super->super_blob = bs->super_blob; 3612 ctx->super->clean = 0; 3613 ctx->super->cluster_size = bs->cluster_sz; 3614 ctx->super->io_unit_size = bs->io_unit_size; 3615 memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype)); 3616 3617 /* Calculate how many pages the metadata consumes at the front 3618 * of the disk. 3619 */ 3620 3621 /* The super block uses 1 page */ 3622 num_md_pages = 1; 3623 3624 /* The used_md_pages mask requires 1 bit per metadata page, rounded 3625 * up to the nearest page, plus a header. 3626 */ 3627 ctx->super->used_page_mask_start = num_md_pages; 3628 ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 3629 spdk_divide_round_up(bs->md_len, 8), 3630 SPDK_BS_PAGE_SIZE); 3631 num_md_pages += ctx->super->used_page_mask_len; 3632 3633 /* The used_clusters mask requires 1 bit per cluster, rounded 3634 * up to the nearest page, plus a header. 3635 */ 3636 ctx->super->used_cluster_mask_start = num_md_pages; 3637 ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 3638 spdk_divide_round_up(bs->total_clusters, 8), 3639 SPDK_BS_PAGE_SIZE); 3640 num_md_pages += ctx->super->used_cluster_mask_len; 3641 3642 /* The used_blobids mask requires 1 bit per metadata page, rounded 3643 * up to the nearest page, plus a header. 3644 */ 3645 ctx->super->used_blobid_mask_start = num_md_pages; 3646 ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) + 3647 spdk_divide_round_up(bs->md_len, 8), 3648 SPDK_BS_PAGE_SIZE); 3649 num_md_pages += ctx->super->used_blobid_mask_len; 3650 3651 /* The metadata region size was chosen above */ 3652 ctx->super->md_start = bs->md_start = num_md_pages; 3653 ctx->super->md_len = bs->md_len; 3654 num_md_pages += bs->md_len; 3655 3656 num_md_lba = _spdk_bs_page_to_lba(bs, num_md_pages); 3657 3658 ctx->super->size = dev->blockcnt * dev->blocklen; 3659 3660 ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super); 3661 3662 num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster); 3663 if (num_md_clusters > bs->total_clusters) { 3664 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, " 3665 "please decrease number of pages reserved for metadata " 3666 "or increase cluster size.\n"); 3667 spdk_dma_free(ctx->super); 3668 free(ctx); 3669 _spdk_bs_free(bs); 3670 cb_fn(cb_arg, NULL, -ENOMEM); 3671 return; 3672 } 3673 /* Claim all of the clusters used by the metadata */ 3674 for (i = 0; i < num_md_clusters; i++) { 3675 _spdk_bs_claim_cluster(bs, i); 3676 } 3677 3678 bs->total_data_clusters = bs->num_free_clusters; 3679 3680 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 3681 cpl.u.bs_handle.cb_fn = cb_fn; 3682 cpl.u.bs_handle.cb_arg = cb_arg; 3683 cpl.u.bs_handle.bs = bs; 3684 3685 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3686 if (!seq) { 3687 spdk_dma_free(ctx->super); 3688 free(ctx); 3689 _spdk_bs_free(bs); 3690 cb_fn(cb_arg, NULL, -ENOMEM); 3691 return; 3692 } 3693 3694 batch = spdk_bs_sequence_to_batch(seq, _spdk_bs_init_trim_cpl, ctx); 3695 3696 /* Clear metadata space */ 3697 spdk_bs_batch_write_zeroes_dev(batch, 0, num_md_lba); 3698 3699 if (opts.clear_method == BS_CLEAR_WITH_UNMAP) { 3700 /* Trim data clusters */ 3701 spdk_bs_batch_unmap_dev(batch, num_md_lba, ctx->bs->dev->blockcnt - num_md_lba); 3702 } else if (opts.clear_method == BS_CLEAR_WITH_WRITE_ZEROES) { 3703 /* Write_zeroes to data clusters */ 3704 spdk_bs_batch_write_zeroes_dev(batch, num_md_lba, ctx->bs->dev->blockcnt - num_md_lba); 3705 } 3706 3707 spdk_bs_batch_close(batch); 3708 } 3709 3710 /* END spdk_bs_init */ 3711 3712 /* START spdk_bs_destroy */ 3713 3714 static void 3715 _spdk_bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3716 { 3717 struct spdk_bs_init_ctx *ctx = cb_arg; 3718 struct spdk_blob_store *bs = ctx->bs; 3719 3720 /* 3721 * We need to defer calling spdk_bs_call_cpl() until after 3722 * dev destruction, so tuck these away for later use. 3723 */ 3724 bs->unload_err = bserrno; 3725 memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 3726 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 3727 3728 spdk_bs_sequence_finish(seq, bserrno); 3729 3730 _spdk_bs_free(bs); 3731 free(ctx); 3732 } 3733 3734 void 3735 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, 3736 void *cb_arg) 3737 { 3738 struct spdk_bs_cpl cpl; 3739 spdk_bs_sequence_t *seq; 3740 struct spdk_bs_init_ctx *ctx; 3741 3742 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Destroying blobstore\n"); 3743 3744 if (!TAILQ_EMPTY(&bs->blobs)) { 3745 SPDK_ERRLOG("Blobstore still has open blobs\n"); 3746 cb_fn(cb_arg, -EBUSY); 3747 return; 3748 } 3749 3750 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3751 cpl.u.bs_basic.cb_fn = cb_fn; 3752 cpl.u.bs_basic.cb_arg = cb_arg; 3753 3754 ctx = calloc(1, sizeof(*ctx)); 3755 if (!ctx) { 3756 cb_fn(cb_arg, -ENOMEM); 3757 return; 3758 } 3759 3760 ctx->bs = bs; 3761 3762 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3763 if (!seq) { 3764 free(ctx); 3765 cb_fn(cb_arg, -ENOMEM); 3766 return; 3767 } 3768 3769 /* Write zeroes to the super block */ 3770 spdk_bs_sequence_write_zeroes_dev(seq, 3771 _spdk_bs_page_to_lba(bs, 0), 3772 _spdk_bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)), 3773 _spdk_bs_destroy_trim_cpl, ctx); 3774 } 3775 3776 /* END spdk_bs_destroy */ 3777 3778 /* START spdk_bs_unload */ 3779 3780 static void 3781 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3782 { 3783 struct spdk_bs_load_ctx *ctx = cb_arg; 3784 3785 spdk_dma_free(ctx->super); 3786 3787 /* 3788 * We need to defer calling spdk_bs_call_cpl() until after 3789 * dev destruction, so tuck these away for later use. 3790 */ 3791 ctx->bs->unload_err = bserrno; 3792 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 3793 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 3794 3795 spdk_bs_sequence_finish(seq, bserrno); 3796 3797 _spdk_bs_free(ctx->bs); 3798 free(ctx); 3799 } 3800 3801 static void 3802 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3803 { 3804 struct spdk_bs_load_ctx *ctx = cb_arg; 3805 3806 spdk_dma_free(ctx->mask); 3807 ctx->super->clean = 1; 3808 3809 _spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx); 3810 } 3811 3812 static void 3813 _spdk_bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3814 { 3815 struct spdk_bs_load_ctx *ctx = cb_arg; 3816 3817 spdk_dma_free(ctx->mask); 3818 ctx->mask = NULL; 3819 3820 _spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_unload_write_used_clusters_cpl); 3821 } 3822 3823 static void 3824 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3825 { 3826 struct spdk_bs_load_ctx *ctx = cb_arg; 3827 3828 spdk_dma_free(ctx->mask); 3829 ctx->mask = NULL; 3830 3831 _spdk_bs_write_used_blobids(seq, cb_arg, _spdk_bs_unload_write_used_blobids_cpl); 3832 } 3833 3834 static void 3835 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3836 { 3837 _spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl); 3838 } 3839 3840 void 3841 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 3842 { 3843 struct spdk_bs_cpl cpl; 3844 spdk_bs_sequence_t *seq; 3845 struct spdk_bs_load_ctx *ctx; 3846 3847 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blobstore\n"); 3848 3849 if (!TAILQ_EMPTY(&bs->blobs)) { 3850 SPDK_ERRLOG("Blobstore still has open blobs\n"); 3851 cb_fn(cb_arg, -EBUSY); 3852 return; 3853 } 3854 3855 ctx = calloc(1, sizeof(*ctx)); 3856 if (!ctx) { 3857 cb_fn(cb_arg, -ENOMEM); 3858 return; 3859 } 3860 3861 ctx->bs = bs; 3862 3863 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3864 if (!ctx->super) { 3865 free(ctx); 3866 cb_fn(cb_arg, -ENOMEM); 3867 return; 3868 } 3869 3870 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3871 cpl.u.bs_basic.cb_fn = cb_fn; 3872 cpl.u.bs_basic.cb_arg = cb_arg; 3873 3874 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3875 if (!seq) { 3876 spdk_dma_free(ctx->super); 3877 free(ctx); 3878 cb_fn(cb_arg, -ENOMEM); 3879 return; 3880 } 3881 3882 /* Read super block */ 3883 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3884 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3885 _spdk_bs_unload_read_super_cpl, ctx); 3886 } 3887 3888 /* END spdk_bs_unload */ 3889 3890 /* START spdk_bs_set_super */ 3891 3892 struct spdk_bs_set_super_ctx { 3893 struct spdk_blob_store *bs; 3894 struct spdk_bs_super_block *super; 3895 }; 3896 3897 static void 3898 _spdk_bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3899 { 3900 struct spdk_bs_set_super_ctx *ctx = cb_arg; 3901 3902 if (bserrno != 0) { 3903 SPDK_ERRLOG("Unable to write to super block of blobstore\n"); 3904 } 3905 3906 spdk_dma_free(ctx->super); 3907 3908 spdk_bs_sequence_finish(seq, bserrno); 3909 3910 free(ctx); 3911 } 3912 3913 static void 3914 _spdk_bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 3915 { 3916 struct spdk_bs_set_super_ctx *ctx = cb_arg; 3917 3918 if (bserrno != 0) { 3919 SPDK_ERRLOG("Unable to read super block of blobstore\n"); 3920 spdk_dma_free(ctx->super); 3921 spdk_bs_sequence_finish(seq, bserrno); 3922 free(ctx); 3923 return; 3924 } 3925 3926 _spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_set_super_write_cpl, ctx); 3927 } 3928 3929 void 3930 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 3931 spdk_bs_op_complete cb_fn, void *cb_arg) 3932 { 3933 struct spdk_bs_cpl cpl; 3934 spdk_bs_sequence_t *seq; 3935 struct spdk_bs_set_super_ctx *ctx; 3936 3937 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Setting super blob id on blobstore\n"); 3938 3939 ctx = calloc(1, sizeof(*ctx)); 3940 if (!ctx) { 3941 cb_fn(cb_arg, -ENOMEM); 3942 return; 3943 } 3944 3945 ctx->bs = bs; 3946 3947 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 3948 if (!ctx->super) { 3949 free(ctx); 3950 cb_fn(cb_arg, -ENOMEM); 3951 return; 3952 } 3953 3954 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 3955 cpl.u.bs_basic.cb_fn = cb_fn; 3956 cpl.u.bs_basic.cb_arg = cb_arg; 3957 3958 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 3959 if (!seq) { 3960 spdk_dma_free(ctx->super); 3961 free(ctx); 3962 cb_fn(cb_arg, -ENOMEM); 3963 return; 3964 } 3965 3966 bs->super_blob = blobid; 3967 3968 /* Read super block */ 3969 spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 3970 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 3971 _spdk_bs_set_super_read_cpl, ctx); 3972 } 3973 3974 /* END spdk_bs_set_super */ 3975 3976 void 3977 spdk_bs_get_super(struct spdk_blob_store *bs, 3978 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 3979 { 3980 if (bs->super_blob == SPDK_BLOBID_INVALID) { 3981 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT); 3982 } else { 3983 cb_fn(cb_arg, bs->super_blob, 0); 3984 } 3985 } 3986 3987 uint64_t 3988 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 3989 { 3990 return bs->cluster_sz; 3991 } 3992 3993 uint64_t 3994 spdk_bs_get_page_size(struct spdk_blob_store *bs) 3995 { 3996 return SPDK_BS_PAGE_SIZE; 3997 } 3998 3999 uint64_t 4000 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs) 4001 { 4002 return bs->io_unit_size; 4003 } 4004 4005 uint64_t 4006 spdk_bs_free_cluster_count(struct spdk_blob_store *bs) 4007 { 4008 return bs->num_free_clusters; 4009 } 4010 4011 uint64_t 4012 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs) 4013 { 4014 return bs->total_data_clusters; 4015 } 4016 4017 static int 4018 spdk_bs_register_md_thread(struct spdk_blob_store *bs) 4019 { 4020 bs->md_channel = spdk_get_io_channel(bs); 4021 if (!bs->md_channel) { 4022 SPDK_ERRLOG("Failed to get IO channel.\n"); 4023 return -1; 4024 } 4025 4026 return 0; 4027 } 4028 4029 static int 4030 spdk_bs_unregister_md_thread(struct spdk_blob_store *bs) 4031 { 4032 spdk_put_io_channel(bs->md_channel); 4033 4034 return 0; 4035 } 4036 4037 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob) 4038 { 4039 assert(blob != NULL); 4040 4041 return blob->id; 4042 } 4043 4044 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob) 4045 { 4046 assert(blob != NULL); 4047 4048 return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters); 4049 } 4050 4051 uint64_t spdk_blob_get_num_io_units(struct spdk_blob *blob) 4052 { 4053 assert(blob != NULL); 4054 4055 return spdk_blob_get_num_pages(blob) * _spdk_bs_io_unit_per_page(blob->bs); 4056 } 4057 4058 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob) 4059 { 4060 assert(blob != NULL); 4061 4062 return blob->active.num_clusters; 4063 } 4064 4065 /* START spdk_bs_create_blob */ 4066 4067 static void 4068 _spdk_bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4069 { 4070 struct spdk_blob *blob = cb_arg; 4071 4072 _spdk_blob_free(blob); 4073 4074 spdk_bs_sequence_finish(seq, bserrno); 4075 } 4076 4077 static int 4078 _spdk_blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs, 4079 bool internal) 4080 { 4081 uint64_t i; 4082 size_t value_len = 0; 4083 int rc; 4084 const void *value = NULL; 4085 if (xattrs->count > 0 && xattrs->get_value == NULL) { 4086 return -EINVAL; 4087 } 4088 for (i = 0; i < xattrs->count; i++) { 4089 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len); 4090 if (value == NULL || value_len == 0) { 4091 return -EINVAL; 4092 } 4093 rc = _spdk_blob_set_xattr(blob, xattrs->names[i], value, value_len, internal); 4094 if (rc < 0) { 4095 return rc; 4096 } 4097 } 4098 return 0; 4099 } 4100 4101 static void 4102 _spdk_blob_set_thin_provision(struct spdk_blob *blob) 4103 { 4104 _spdk_blob_verify_md_op(blob); 4105 blob->invalid_flags |= SPDK_BLOB_THIN_PROV; 4106 blob->state = SPDK_BLOB_STATE_DIRTY; 4107 } 4108 4109 static void 4110 _spdk_bs_create_blob(struct spdk_blob_store *bs, 4111 const struct spdk_blob_opts *opts, 4112 const struct spdk_blob_xattr_opts *internal_xattrs, 4113 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4114 { 4115 struct spdk_blob *blob; 4116 uint32_t page_idx; 4117 struct spdk_bs_cpl cpl; 4118 struct spdk_blob_opts opts_default; 4119 struct spdk_blob_xattr_opts internal_xattrs_default; 4120 spdk_bs_sequence_t *seq; 4121 spdk_blob_id id; 4122 int rc; 4123 4124 assert(spdk_get_thread() == bs->md_thread); 4125 4126 page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0); 4127 if (page_idx == UINT32_MAX) { 4128 cb_fn(cb_arg, 0, -ENOMEM); 4129 return; 4130 } 4131 spdk_bit_array_set(bs->used_blobids, page_idx); 4132 spdk_bit_array_set(bs->used_md_pages, page_idx); 4133 4134 id = _spdk_bs_page_to_blobid(page_idx); 4135 4136 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx); 4137 4138 blob = _spdk_blob_alloc(bs, id); 4139 if (!blob) { 4140 cb_fn(cb_arg, 0, -ENOMEM); 4141 return; 4142 } 4143 4144 if (!opts) { 4145 spdk_blob_opts_init(&opts_default); 4146 opts = &opts_default; 4147 } 4148 if (!internal_xattrs) { 4149 _spdk_blob_xattrs_init(&internal_xattrs_default); 4150 internal_xattrs = &internal_xattrs_default; 4151 } 4152 4153 rc = _spdk_blob_set_xattrs(blob, &opts->xattrs, false); 4154 if (rc < 0) { 4155 _spdk_blob_free(blob); 4156 cb_fn(cb_arg, 0, rc); 4157 return; 4158 } 4159 4160 rc = _spdk_blob_set_xattrs(blob, internal_xattrs, true); 4161 if (rc < 0) { 4162 _spdk_blob_free(blob); 4163 cb_fn(cb_arg, 0, rc); 4164 return; 4165 } 4166 4167 if (opts->thin_provision) { 4168 _spdk_blob_set_thin_provision(blob); 4169 } 4170 4171 rc = _spdk_blob_resize(blob, opts->num_clusters); 4172 if (rc < 0) { 4173 _spdk_blob_free(blob); 4174 cb_fn(cb_arg, 0, rc); 4175 return; 4176 } 4177 cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 4178 cpl.u.blobid.cb_fn = cb_fn; 4179 cpl.u.blobid.cb_arg = cb_arg; 4180 cpl.u.blobid.blobid = blob->id; 4181 4182 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 4183 if (!seq) { 4184 _spdk_blob_free(blob); 4185 cb_fn(cb_arg, 0, -ENOMEM); 4186 return; 4187 } 4188 4189 _spdk_blob_persist(seq, blob, _spdk_bs_create_blob_cpl, blob); 4190 } 4191 4192 void spdk_bs_create_blob(struct spdk_blob_store *bs, 4193 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4194 { 4195 _spdk_bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg); 4196 } 4197 4198 void spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts, 4199 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4200 { 4201 _spdk_bs_create_blob(bs, opts, NULL, cb_fn, cb_arg); 4202 } 4203 4204 /* END spdk_bs_create_blob */ 4205 4206 /* START blob_cleanup */ 4207 4208 struct spdk_clone_snapshot_ctx { 4209 struct spdk_bs_cpl cpl; 4210 int bserrno; 4211 bool frozen; 4212 4213 struct spdk_io_channel *channel; 4214 4215 /* Current cluster for inflate operation */ 4216 uint64_t cluster; 4217 4218 /* For inflation force allocation of all unallocated clusters and remove 4219 * thin-provisioning. Otherwise only decouple parent and keep clone thin. */ 4220 bool allocate_all; 4221 4222 struct { 4223 spdk_blob_id id; 4224 struct spdk_blob *blob; 4225 } original; 4226 struct { 4227 spdk_blob_id id; 4228 struct spdk_blob *blob; 4229 } new; 4230 4231 /* xattrs specified for snapshot/clones only. They have no impact on 4232 * the original blobs xattrs. */ 4233 const struct spdk_blob_xattr_opts *xattrs; 4234 }; 4235 4236 static void 4237 _spdk_bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno) 4238 { 4239 struct spdk_clone_snapshot_ctx *ctx = cb_arg; 4240 struct spdk_bs_cpl *cpl = &ctx->cpl; 4241 4242 if (bserrno != 0) { 4243 if (ctx->bserrno != 0) { 4244 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 4245 } else { 4246 ctx->bserrno = bserrno; 4247 } 4248 } 4249 4250 switch (cpl->type) { 4251 case SPDK_BS_CPL_TYPE_BLOBID: 4252 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno); 4253 break; 4254 case SPDK_BS_CPL_TYPE_BLOB_BASIC: 4255 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno); 4256 break; 4257 default: 4258 SPDK_UNREACHABLE(); 4259 break; 4260 } 4261 4262 free(ctx); 4263 } 4264 4265 static void 4266 _spdk_bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno) 4267 { 4268 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4269 struct spdk_blob *origblob = ctx->original.blob; 4270 4271 if (bserrno != 0) { 4272 if (ctx->bserrno != 0) { 4273 SPDK_ERRLOG("Unfreeze error %d\n", bserrno); 4274 } else { 4275 ctx->bserrno = bserrno; 4276 } 4277 } 4278 4279 ctx->original.id = origblob->id; 4280 spdk_blob_close(origblob, _spdk_bs_clone_snapshot_cleanup_finish, ctx); 4281 } 4282 4283 static void 4284 _spdk_bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno) 4285 { 4286 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4287 struct spdk_blob *origblob = ctx->original.blob; 4288 4289 if (bserrno != 0) { 4290 if (ctx->bserrno != 0) { 4291 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 4292 } else { 4293 ctx->bserrno = bserrno; 4294 } 4295 } 4296 4297 if (ctx->frozen) { 4298 /* Unfreeze any outstanding I/O */ 4299 _spdk_blob_unfreeze_io(origblob, _spdk_bs_snapshot_unfreeze_cpl, ctx); 4300 } else { 4301 _spdk_bs_snapshot_unfreeze_cpl(ctx, 0); 4302 } 4303 4304 } 4305 4306 static void 4307 _spdk_bs_clone_snapshot_newblob_cleanup(void *cb_arg, int bserrno) 4308 { 4309 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4310 struct spdk_blob *newblob = ctx->new.blob; 4311 4312 if (bserrno != 0) { 4313 if (ctx->bserrno != 0) { 4314 SPDK_ERRLOG("Cleanup error %d\n", bserrno); 4315 } else { 4316 ctx->bserrno = bserrno; 4317 } 4318 } 4319 4320 ctx->new.id = newblob->id; 4321 spdk_blob_close(newblob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4322 } 4323 4324 /* END blob_cleanup */ 4325 4326 /* START spdk_bs_create_snapshot */ 4327 4328 static void 4329 _spdk_bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno) 4330 { 4331 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4332 struct spdk_blob *newblob = ctx->new.blob; 4333 4334 if (bserrno != 0) { 4335 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4336 return; 4337 } 4338 4339 /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */ 4340 bserrno = _spdk_blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true); 4341 if (bserrno != 0) { 4342 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4343 return; 4344 } 4345 4346 _spdk_bs_blob_list_add(ctx->original.blob); 4347 4348 spdk_blob_set_read_only(newblob); 4349 4350 /* sync snapshot metadata */ 4351 spdk_blob_sync_md(newblob, _spdk_bs_clone_snapshot_origblob_cleanup, cb_arg); 4352 } 4353 4354 static void 4355 _spdk_bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno) 4356 { 4357 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4358 struct spdk_blob *origblob = ctx->original.blob; 4359 struct spdk_blob *newblob = ctx->new.blob; 4360 4361 if (bserrno != 0) { 4362 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4363 return; 4364 } 4365 4366 /* Set internal xattr for snapshot id */ 4367 bserrno = _spdk_blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true); 4368 if (bserrno != 0) { 4369 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4370 return; 4371 } 4372 4373 _spdk_bs_blob_list_remove(origblob); 4374 origblob->parent_id = newblob->id; 4375 4376 /* Create new back_bs_dev for snapshot */ 4377 origblob->back_bs_dev = spdk_bs_create_blob_bs_dev(newblob); 4378 if (origblob->back_bs_dev == NULL) { 4379 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL); 4380 return; 4381 } 4382 4383 /* set clone blob as thin provisioned */ 4384 _spdk_blob_set_thin_provision(origblob); 4385 4386 _spdk_bs_blob_list_add(newblob); 4387 4388 /* Zero out origblob cluster map */ 4389 memset(origblob->active.clusters, 0, 4390 origblob->active.num_clusters * sizeof(origblob->active.clusters)); 4391 4392 /* sync clone metadata */ 4393 spdk_blob_sync_md(origblob, _spdk_bs_snapshot_origblob_sync_cpl, ctx); 4394 } 4395 4396 static void 4397 _spdk_bs_snapshot_freeze_cpl(void *cb_arg, int rc) 4398 { 4399 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4400 struct spdk_blob *origblob = ctx->original.blob; 4401 struct spdk_blob *newblob = ctx->new.blob; 4402 int bserrno; 4403 4404 if (rc != 0) { 4405 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, rc); 4406 return; 4407 } 4408 4409 ctx->frozen = true; 4410 4411 /* set new back_bs_dev for snapshot */ 4412 newblob->back_bs_dev = origblob->back_bs_dev; 4413 /* Set invalid flags from origblob */ 4414 newblob->invalid_flags = origblob->invalid_flags; 4415 4416 /* inherit parent from original blob if set */ 4417 newblob->parent_id = origblob->parent_id; 4418 if (origblob->parent_id != SPDK_BLOBID_INVALID) { 4419 /* Set internal xattr for snapshot id */ 4420 bserrno = _spdk_blob_set_xattr(newblob, BLOB_SNAPSHOT, 4421 &origblob->parent_id, sizeof(spdk_blob_id), true); 4422 if (bserrno != 0) { 4423 _spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno); 4424 return; 4425 } 4426 } 4427 4428 /* Copy cluster map to snapshot */ 4429 memcpy(newblob->active.clusters, origblob->active.clusters, 4430 origblob->active.num_clusters * sizeof(origblob->active.clusters)); 4431 4432 /* sync snapshot metadata */ 4433 spdk_blob_sync_md(newblob, _spdk_bs_snapshot_newblob_sync_cpl, ctx); 4434 } 4435 4436 static void 4437 _spdk_bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4438 { 4439 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4440 struct spdk_blob *origblob = ctx->original.blob; 4441 struct spdk_blob *newblob = _blob; 4442 4443 if (bserrno != 0) { 4444 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4445 return; 4446 } 4447 4448 ctx->new.blob = newblob; 4449 4450 _spdk_blob_freeze_io(origblob, _spdk_bs_snapshot_freeze_cpl, ctx); 4451 } 4452 4453 static void 4454 _spdk_bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 4455 { 4456 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4457 struct spdk_blob *origblob = ctx->original.blob; 4458 4459 if (bserrno != 0) { 4460 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4461 return; 4462 } 4463 4464 ctx->new.id = blobid; 4465 ctx->cpl.u.blobid.blobid = blobid; 4466 4467 spdk_bs_open_blob(origblob->bs, ctx->new.id, _spdk_bs_snapshot_newblob_open_cpl, ctx); 4468 } 4469 4470 4471 static void 4472 _spdk_bs_xattr_snapshot(void *arg, const char *name, 4473 const void **value, size_t *value_len) 4474 { 4475 assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0); 4476 4477 struct spdk_blob *blob = (struct spdk_blob *)arg; 4478 *value = &blob->id; 4479 *value_len = sizeof(blob->id); 4480 } 4481 4482 static void 4483 _spdk_bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4484 { 4485 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4486 struct spdk_blob_opts opts; 4487 struct spdk_blob_xattr_opts internal_xattrs; 4488 char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS }; 4489 4490 if (bserrno != 0) { 4491 _spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno); 4492 return; 4493 } 4494 4495 ctx->original.blob = _blob; 4496 4497 if (_blob->data_ro || _blob->md_ro) { 4498 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot create snapshot from read only blob with id %lu\n", 4499 _blob->id); 4500 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 4501 return; 4502 } 4503 4504 spdk_blob_opts_init(&opts); 4505 _spdk_blob_xattrs_init(&internal_xattrs); 4506 4507 /* Change the size of new blob to the same as in original blob, 4508 * but do not allocate clusters */ 4509 opts.thin_provision = true; 4510 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 4511 4512 /* If there are any xattrs specified for snapshot, set them now */ 4513 if (ctx->xattrs) { 4514 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 4515 } 4516 /* Set internal xattr SNAPSHOT_IN_PROGRESS */ 4517 internal_xattrs.count = 1; 4518 internal_xattrs.ctx = _blob; 4519 internal_xattrs.names = xattrs_names; 4520 internal_xattrs.get_value = _spdk_bs_xattr_snapshot; 4521 4522 _spdk_bs_create_blob(_blob->bs, &opts, &internal_xattrs, 4523 _spdk_bs_snapshot_newblob_create_cpl, ctx); 4524 } 4525 4526 void spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid, 4527 const struct spdk_blob_xattr_opts *snapshot_xattrs, 4528 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4529 { 4530 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 4531 4532 if (!ctx) { 4533 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 4534 return; 4535 } 4536 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 4537 ctx->cpl.u.blobid.cb_fn = cb_fn; 4538 ctx->cpl.u.blobid.cb_arg = cb_arg; 4539 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 4540 ctx->bserrno = 0; 4541 ctx->frozen = false; 4542 ctx->original.id = blobid; 4543 ctx->xattrs = snapshot_xattrs; 4544 4545 spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_snapshot_origblob_open_cpl, ctx); 4546 } 4547 /* END spdk_bs_create_snapshot */ 4548 4549 /* START spdk_bs_create_clone */ 4550 4551 static void 4552 _spdk_bs_xattr_clone(void *arg, const char *name, 4553 const void **value, size_t *value_len) 4554 { 4555 assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0); 4556 4557 struct spdk_blob *blob = (struct spdk_blob *)arg; 4558 *value = &blob->id; 4559 *value_len = sizeof(blob->id); 4560 } 4561 4562 static void 4563 _spdk_bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4564 { 4565 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4566 struct spdk_blob *clone = _blob; 4567 4568 ctx->new.blob = clone; 4569 _spdk_bs_blob_list_add(clone); 4570 4571 spdk_blob_close(clone, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4572 } 4573 4574 static void 4575 _spdk_bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno) 4576 { 4577 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4578 4579 ctx->cpl.u.blobid.blobid = blobid; 4580 spdk_bs_open_blob(ctx->original.blob->bs, blobid, _spdk_bs_clone_newblob_open_cpl, ctx); 4581 } 4582 4583 static void 4584 _spdk_bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4585 { 4586 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4587 struct spdk_blob_opts opts; 4588 struct spdk_blob_xattr_opts internal_xattrs; 4589 char *xattr_names[] = { BLOB_SNAPSHOT }; 4590 4591 if (bserrno != 0) { 4592 _spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno); 4593 return; 4594 } 4595 4596 ctx->original.blob = _blob; 4597 4598 if (!_blob->data_ro || !_blob->md_ro) { 4599 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Clone not from read-only blob\n"); 4600 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 4601 return; 4602 } 4603 4604 spdk_blob_opts_init(&opts); 4605 _spdk_blob_xattrs_init(&internal_xattrs); 4606 4607 opts.thin_provision = true; 4608 opts.num_clusters = spdk_blob_get_num_clusters(_blob); 4609 if (ctx->xattrs) { 4610 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs)); 4611 } 4612 4613 /* Set internal xattr BLOB_SNAPSHOT */ 4614 internal_xattrs.count = 1; 4615 internal_xattrs.ctx = _blob; 4616 internal_xattrs.names = xattr_names; 4617 internal_xattrs.get_value = _spdk_bs_xattr_clone; 4618 4619 _spdk_bs_create_blob(_blob->bs, &opts, &internal_xattrs, 4620 _spdk_bs_clone_newblob_create_cpl, ctx); 4621 } 4622 4623 void spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid, 4624 const struct spdk_blob_xattr_opts *clone_xattrs, 4625 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 4626 { 4627 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 4628 4629 if (!ctx) { 4630 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM); 4631 return; 4632 } 4633 4634 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 4635 ctx->cpl.u.blobid.cb_fn = cb_fn; 4636 ctx->cpl.u.blobid.cb_arg = cb_arg; 4637 ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID; 4638 ctx->bserrno = 0; 4639 ctx->xattrs = clone_xattrs; 4640 ctx->original.id = blobid; 4641 4642 spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_clone_origblob_open_cpl, ctx); 4643 } 4644 4645 /* END spdk_bs_create_clone */ 4646 4647 /* START spdk_bs_inflate_blob */ 4648 4649 static void 4650 _spdk_bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno) 4651 { 4652 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4653 struct spdk_blob *_blob = ctx->original.blob; 4654 4655 if (bserrno != 0) { 4656 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4657 return; 4658 } 4659 4660 assert(_parent != NULL); 4661 4662 _spdk_bs_blob_list_remove(_blob); 4663 _blob->parent_id = _parent->id; 4664 _spdk_blob_set_xattr(_blob, BLOB_SNAPSHOT, &_blob->parent_id, 4665 sizeof(spdk_blob_id), true); 4666 4667 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 4668 _blob->back_bs_dev = spdk_bs_create_blob_bs_dev(_parent); 4669 _spdk_bs_blob_list_add(_blob); 4670 4671 spdk_blob_sync_md(_blob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4672 } 4673 4674 static void 4675 _spdk_bs_inflate_blob_done(void *cb_arg, int bserrno) 4676 { 4677 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4678 struct spdk_blob *_blob = ctx->original.blob; 4679 struct spdk_blob *_parent; 4680 4681 if (bserrno != 0) { 4682 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4683 return; 4684 } 4685 4686 if (ctx->allocate_all) { 4687 /* remove thin provisioning */ 4688 _spdk_bs_blob_list_remove(_blob); 4689 _spdk_blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 4690 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV; 4691 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 4692 _blob->back_bs_dev = NULL; 4693 _blob->parent_id = SPDK_BLOBID_INVALID; 4694 } else { 4695 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob; 4696 if (_parent->parent_id != SPDK_BLOBID_INVALID) { 4697 /* We must change the parent of the inflated blob */ 4698 spdk_bs_open_blob(_blob->bs, _parent->parent_id, 4699 _spdk_bs_inflate_blob_set_parent_cpl, ctx); 4700 return; 4701 } 4702 4703 _spdk_bs_blob_list_remove(_blob); 4704 _spdk_blob_remove_xattr(_blob, BLOB_SNAPSHOT, true); 4705 _blob->parent_id = SPDK_BLOBID_INVALID; 4706 _blob->back_bs_dev->destroy(_blob->back_bs_dev); 4707 _blob->back_bs_dev = spdk_bs_create_zeroes_dev(); 4708 } 4709 4710 _blob->state = SPDK_BLOB_STATE_DIRTY; 4711 spdk_blob_sync_md(_blob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx); 4712 } 4713 4714 /* Check if cluster needs allocation */ 4715 static inline bool 4716 _spdk_bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all) 4717 { 4718 struct spdk_blob_bs_dev *b; 4719 4720 assert(blob != NULL); 4721 4722 if (blob->active.clusters[cluster] != 0) { 4723 /* Cluster is already allocated */ 4724 return false; 4725 } 4726 4727 if (blob->parent_id == SPDK_BLOBID_INVALID) { 4728 /* Blob have no parent blob */ 4729 return allocate_all; 4730 } 4731 4732 b = (struct spdk_blob_bs_dev *)blob->back_bs_dev; 4733 return (allocate_all || b->blob->active.clusters[cluster] != 0); 4734 } 4735 4736 static void 4737 _spdk_bs_inflate_blob_touch_next(void *cb_arg, int bserrno) 4738 { 4739 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4740 struct spdk_blob *_blob = ctx->original.blob; 4741 uint64_t offset; 4742 4743 if (bserrno != 0) { 4744 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno); 4745 return; 4746 } 4747 4748 for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) { 4749 if (_spdk_bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) { 4750 break; 4751 } 4752 } 4753 4754 if (ctx->cluster < _blob->active.num_clusters) { 4755 offset = _spdk_bs_cluster_to_lba(_blob->bs, ctx->cluster); 4756 4757 /* We may safely increment a cluster before write */ 4758 ctx->cluster++; 4759 4760 /* Use zero length write to touch a cluster */ 4761 spdk_blob_io_write(_blob, ctx->channel, NULL, offset, 0, 4762 _spdk_bs_inflate_blob_touch_next, ctx); 4763 } else { 4764 _spdk_bs_inflate_blob_done(cb_arg, bserrno); 4765 } 4766 } 4767 4768 static void 4769 _spdk_bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 4770 { 4771 struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg; 4772 uint64_t lfc; /* lowest free cluster */ 4773 uint64_t i; 4774 4775 if (bserrno != 0) { 4776 _spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno); 4777 return; 4778 } 4779 ctx->original.blob = _blob; 4780 4781 if (!ctx->allocate_all && _blob->parent_id == SPDK_BLOBID_INVALID) { 4782 /* This blob have no parent, so we cannot decouple it. */ 4783 SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n"); 4784 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL); 4785 return; 4786 } 4787 4788 if (spdk_blob_is_thin_provisioned(_blob) == false) { 4789 /* This is not thin provisioned blob. No need to inflate. */ 4790 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, 0); 4791 return; 4792 } 4793 4794 /* Do two passes - one to verify that we can obtain enough clusters 4795 * and another to actually claim them. 4796 */ 4797 lfc = 0; 4798 for (i = 0; i < _blob->active.num_clusters; i++) { 4799 if (_spdk_bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) { 4800 lfc = spdk_bit_array_find_first_clear(_blob->bs->used_clusters, lfc); 4801 if (lfc == UINT32_MAX) { 4802 /* No more free clusters. Cannot satisfy the request */ 4803 _spdk_bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC); 4804 return; 4805 } 4806 lfc++; 4807 } 4808 } 4809 4810 ctx->cluster = 0; 4811 _spdk_bs_inflate_blob_touch_next(ctx, 0); 4812 } 4813 4814 static void 4815 _spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 4816 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg) 4817 { 4818 struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx)); 4819 4820 if (!ctx) { 4821 cb_fn(cb_arg, -ENOMEM); 4822 return; 4823 } 4824 ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 4825 ctx->cpl.u.bs_basic.cb_fn = cb_fn; 4826 ctx->cpl.u.bs_basic.cb_arg = cb_arg; 4827 ctx->bserrno = 0; 4828 ctx->original.id = blobid; 4829 ctx->channel = channel; 4830 ctx->allocate_all = allocate_all; 4831 4832 spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_inflate_blob_open_cpl, ctx); 4833 } 4834 4835 void 4836 spdk_bs_inflate_blob(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, true, cb_fn, cb_arg); 4840 } 4841 4842 void 4843 spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel, 4844 spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg) 4845 { 4846 _spdk_bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg); 4847 } 4848 /* END spdk_bs_inflate_blob */ 4849 4850 /* START spdk_blob_resize */ 4851 struct spdk_bs_resize_ctx { 4852 spdk_blob_op_complete cb_fn; 4853 void *cb_arg; 4854 struct spdk_blob *blob; 4855 uint64_t sz; 4856 int rc; 4857 }; 4858 4859 static void 4860 _spdk_bs_resize_unfreeze_cpl(void *cb_arg, int rc) 4861 { 4862 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 4863 4864 if (rc != 0) { 4865 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc); 4866 } 4867 4868 if (ctx->rc != 0) { 4869 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc); 4870 rc = ctx->rc; 4871 } 4872 4873 ctx->blob->resize_in_progress = false; 4874 4875 ctx->cb_fn(ctx->cb_arg, rc); 4876 free(ctx); 4877 } 4878 4879 static void 4880 _spdk_bs_resize_freeze_cpl(void *cb_arg, int rc) 4881 { 4882 struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg; 4883 4884 if (rc != 0) { 4885 ctx->blob->resize_in_progress = false; 4886 ctx->cb_fn(ctx->cb_arg, rc); 4887 free(ctx); 4888 return; 4889 } 4890 4891 ctx->rc = _spdk_blob_resize(ctx->blob, ctx->sz); 4892 4893 _spdk_blob_unfreeze_io(ctx->blob, _spdk_bs_resize_unfreeze_cpl, ctx); 4894 } 4895 4896 void 4897 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg) 4898 { 4899 struct spdk_bs_resize_ctx *ctx; 4900 4901 _spdk_blob_verify_md_op(blob); 4902 4903 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz); 4904 4905 if (blob->md_ro) { 4906 cb_fn(cb_arg, -EPERM); 4907 return; 4908 } 4909 4910 if (sz == blob->active.num_clusters) { 4911 cb_fn(cb_arg, 0); 4912 return; 4913 } 4914 4915 if (blob->resize_in_progress) { 4916 cb_fn(cb_arg, -EBUSY); 4917 return; 4918 } 4919 4920 ctx = calloc(1, sizeof(*ctx)); 4921 if (!ctx) { 4922 cb_fn(cb_arg, -ENOMEM); 4923 return; 4924 } 4925 4926 blob->resize_in_progress = true; 4927 ctx->cb_fn = cb_fn; 4928 ctx->cb_arg = cb_arg; 4929 ctx->blob = blob; 4930 ctx->sz = sz; 4931 _spdk_blob_freeze_io(blob, _spdk_bs_resize_freeze_cpl, ctx); 4932 } 4933 4934 /* END spdk_blob_resize */ 4935 4936 4937 /* START spdk_bs_delete_blob */ 4938 4939 static void 4940 _spdk_bs_delete_close_cpl(void *cb_arg, int bserrno) 4941 { 4942 spdk_bs_sequence_t *seq = cb_arg; 4943 4944 spdk_bs_sequence_finish(seq, bserrno); 4945 } 4946 4947 static void 4948 _spdk_bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 4949 { 4950 struct spdk_blob *blob = cb_arg; 4951 4952 if (bserrno != 0) { 4953 /* 4954 * We already removed this blob from the blobstore tailq, so 4955 * we need to free it here since this is the last reference 4956 * to it. 4957 */ 4958 _spdk_blob_free(blob); 4959 _spdk_bs_delete_close_cpl(seq, bserrno); 4960 return; 4961 } 4962 4963 /* 4964 * This will immediately decrement the ref_count and call 4965 * the completion routine since the metadata state is clean. 4966 * By calling spdk_blob_close, we reduce the number of call 4967 * points into code that touches the blob->open_ref count 4968 * and the blobstore's blob list. 4969 */ 4970 spdk_blob_close(blob, _spdk_bs_delete_close_cpl, seq); 4971 } 4972 4973 static void 4974 _spdk_bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno) 4975 { 4976 spdk_bs_sequence_t *seq = cb_arg; 4977 struct spdk_blob_list *snapshot = NULL; 4978 uint32_t page_num; 4979 4980 if (bserrno != 0) { 4981 spdk_bs_sequence_finish(seq, bserrno); 4982 return; 4983 } 4984 4985 _spdk_blob_verify_md_op(blob); 4986 4987 if (blob->open_ref > 1) { 4988 /* 4989 * Someone has this blob open (besides this delete context). 4990 * Decrement the ref count directly and return -EBUSY. 4991 */ 4992 blob->open_ref--; 4993 spdk_bs_sequence_finish(seq, -EBUSY); 4994 return; 4995 } 4996 4997 bserrno = _spdk_bs_blob_list_remove(blob); 4998 if (bserrno != 0) { 4999 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Remove blob #%" PRIu64 " from a list\n", blob->id); 5000 spdk_bs_sequence_finish(seq, bserrno); 5001 return; 5002 } 5003 5004 /* 5005 * Remove the blob from the blob_store list now, to ensure it does not 5006 * get returned after this point by _spdk_blob_lookup(). 5007 */ 5008 TAILQ_REMOVE(&blob->bs->blobs, blob, link); 5009 5010 /* If blob is a snapshot then remove it from the list */ 5011 TAILQ_FOREACH(snapshot, &blob->bs->snapshots, link) { 5012 if (snapshot->id == blob->id) { 5013 TAILQ_REMOVE(&blob->bs->snapshots, snapshot, link); 5014 free(snapshot); 5015 break; 5016 } 5017 } 5018 5019 page_num = _spdk_bs_blobid_to_page(blob->id); 5020 spdk_bit_array_clear(blob->bs->used_blobids, page_num); 5021 blob->state = SPDK_BLOB_STATE_DIRTY; 5022 blob->active.num_pages = 0; 5023 _spdk_blob_resize(blob, 0); 5024 5025 _spdk_blob_persist(seq, blob, _spdk_bs_delete_persist_cpl, blob); 5026 } 5027 5028 void 5029 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 5030 spdk_blob_op_complete cb_fn, void *cb_arg) 5031 { 5032 struct spdk_bs_cpl cpl; 5033 spdk_bs_sequence_t *seq; 5034 struct spdk_blob_list *snapshot_entry = NULL; 5035 5036 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Deleting blob %lu\n", blobid); 5037 5038 assert(spdk_get_thread() == bs->md_thread); 5039 5040 /* Check if this is a snapshot with clones */ 5041 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 5042 if (snapshot_entry->id == blobid) { 5043 break; 5044 } 5045 } 5046 if (snapshot_entry != NULL) { 5047 /* If snapshot have clones, we cannot remove it */ 5048 if (!TAILQ_EMPTY(&snapshot_entry->clones)) { 5049 SPDK_ERRLOG("Cannot remove snapshot with clones\n"); 5050 cb_fn(cb_arg, -EBUSY); 5051 return; 5052 } 5053 } 5054 5055 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 5056 cpl.u.blob_basic.cb_fn = cb_fn; 5057 cpl.u.blob_basic.cb_arg = cb_arg; 5058 5059 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 5060 if (!seq) { 5061 cb_fn(cb_arg, -ENOMEM); 5062 return; 5063 } 5064 5065 spdk_bs_open_blob(bs, blobid, _spdk_bs_delete_open_cpl, seq); 5066 } 5067 5068 /* END spdk_bs_delete_blob */ 5069 5070 /* START spdk_bs_open_blob */ 5071 5072 static void 5073 _spdk_bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5074 { 5075 struct spdk_blob *blob = cb_arg; 5076 5077 /* If the blob have crc error, we just return NULL. */ 5078 if (blob == NULL) { 5079 seq->cpl.u.blob_handle.blob = NULL; 5080 spdk_bs_sequence_finish(seq, bserrno); 5081 return; 5082 } 5083 5084 blob->open_ref++; 5085 5086 TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link); 5087 5088 spdk_bs_sequence_finish(seq, bserrno); 5089 } 5090 5091 static void _spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 5092 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5093 { 5094 struct spdk_blob *blob; 5095 struct spdk_bs_cpl cpl; 5096 struct spdk_blob_open_opts opts_default; 5097 spdk_bs_sequence_t *seq; 5098 uint32_t page_num; 5099 5100 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Opening blob %lu\n", blobid); 5101 assert(spdk_get_thread() == bs->md_thread); 5102 5103 page_num = _spdk_bs_blobid_to_page(blobid); 5104 if (spdk_bit_array_get(bs->used_blobids, page_num) == false) { 5105 /* Invalid blobid */ 5106 cb_fn(cb_arg, NULL, -ENOENT); 5107 return; 5108 } 5109 5110 blob = _spdk_blob_lookup(bs, blobid); 5111 if (blob) { 5112 blob->open_ref++; 5113 cb_fn(cb_arg, blob, 0); 5114 return; 5115 } 5116 5117 blob = _spdk_blob_alloc(bs, blobid); 5118 if (!blob) { 5119 cb_fn(cb_arg, NULL, -ENOMEM); 5120 return; 5121 } 5122 5123 if (!opts) { 5124 spdk_blob_open_opts_init(&opts_default); 5125 opts = &opts_default; 5126 } 5127 5128 blob->clear_method = opts->clear_method; 5129 5130 cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE; 5131 cpl.u.blob_handle.cb_fn = cb_fn; 5132 cpl.u.blob_handle.cb_arg = cb_arg; 5133 cpl.u.blob_handle.blob = blob; 5134 5135 seq = spdk_bs_sequence_start(bs->md_channel, &cpl); 5136 if (!seq) { 5137 _spdk_blob_free(blob); 5138 cb_fn(cb_arg, NULL, -ENOMEM); 5139 return; 5140 } 5141 5142 _spdk_blob_load(seq, blob, _spdk_bs_open_blob_cpl, blob); 5143 } 5144 5145 void spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 5146 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5147 { 5148 _spdk_bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg); 5149 } 5150 5151 void spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid, 5152 struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5153 { 5154 _spdk_bs_open_blob(bs, blobid, opts, cb_fn, cb_arg); 5155 } 5156 5157 /* END spdk_bs_open_blob */ 5158 5159 /* START spdk_blob_set_read_only */ 5160 int spdk_blob_set_read_only(struct spdk_blob *blob) 5161 { 5162 _spdk_blob_verify_md_op(blob); 5163 5164 blob->data_ro_flags |= SPDK_BLOB_READ_ONLY; 5165 5166 blob->state = SPDK_BLOB_STATE_DIRTY; 5167 return 0; 5168 } 5169 /* END spdk_blob_set_read_only */ 5170 5171 /* START spdk_blob_sync_md */ 5172 5173 static void 5174 _spdk_blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5175 { 5176 struct spdk_blob *blob = cb_arg; 5177 5178 if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) { 5179 blob->data_ro = true; 5180 blob->md_ro = true; 5181 } 5182 5183 spdk_bs_sequence_finish(seq, bserrno); 5184 } 5185 5186 static void 5187 _spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 5188 { 5189 struct spdk_bs_cpl cpl; 5190 spdk_bs_sequence_t *seq; 5191 5192 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 5193 cpl.u.blob_basic.cb_fn = cb_fn; 5194 cpl.u.blob_basic.cb_arg = cb_arg; 5195 5196 seq = spdk_bs_sequence_start(blob->bs->md_channel, &cpl); 5197 if (!seq) { 5198 cb_fn(cb_arg, -ENOMEM); 5199 return; 5200 } 5201 5202 _spdk_blob_persist(seq, blob, _spdk_blob_sync_md_cpl, blob); 5203 } 5204 5205 void 5206 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 5207 { 5208 _spdk_blob_verify_md_op(blob); 5209 5210 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blob %lu\n", blob->id); 5211 5212 if (blob->md_ro) { 5213 assert(blob->state == SPDK_BLOB_STATE_CLEAN); 5214 cb_fn(cb_arg, 0); 5215 return; 5216 } 5217 5218 _spdk_blob_sync_md(blob, cb_fn, cb_arg); 5219 } 5220 5221 /* END spdk_blob_sync_md */ 5222 5223 struct spdk_blob_insert_cluster_ctx { 5224 struct spdk_thread *thread; 5225 struct spdk_blob *blob; 5226 uint32_t cluster_num; /* cluster index in blob */ 5227 uint32_t cluster; /* cluster on disk */ 5228 int rc; 5229 spdk_blob_op_complete cb_fn; 5230 void *cb_arg; 5231 }; 5232 5233 static void 5234 _spdk_blob_insert_cluster_msg_cpl(void *arg) 5235 { 5236 struct spdk_blob_insert_cluster_ctx *ctx = arg; 5237 5238 ctx->cb_fn(ctx->cb_arg, ctx->rc); 5239 free(ctx); 5240 } 5241 5242 static void 5243 _spdk_blob_insert_cluster_msg_cb(void *arg, int bserrno) 5244 { 5245 struct spdk_blob_insert_cluster_ctx *ctx = arg; 5246 5247 ctx->rc = bserrno; 5248 spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx); 5249 } 5250 5251 static void 5252 _spdk_blob_insert_cluster_msg(void *arg) 5253 { 5254 struct spdk_blob_insert_cluster_ctx *ctx = arg; 5255 5256 ctx->rc = _spdk_blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster); 5257 if (ctx->rc != 0) { 5258 spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx); 5259 return; 5260 } 5261 5262 ctx->blob->state = SPDK_BLOB_STATE_DIRTY; 5263 _spdk_blob_sync_md(ctx->blob, _spdk_blob_insert_cluster_msg_cb, ctx); 5264 } 5265 5266 static void 5267 _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, 5268 uint64_t cluster, spdk_blob_op_complete cb_fn, void *cb_arg) 5269 { 5270 struct spdk_blob_insert_cluster_ctx *ctx; 5271 5272 ctx = calloc(1, sizeof(*ctx)); 5273 if (ctx == NULL) { 5274 cb_fn(cb_arg, -ENOMEM); 5275 return; 5276 } 5277 5278 ctx->thread = spdk_get_thread(); 5279 ctx->blob = blob; 5280 ctx->cluster_num = cluster_num; 5281 ctx->cluster = cluster; 5282 ctx->cb_fn = cb_fn; 5283 ctx->cb_arg = cb_arg; 5284 5285 spdk_thread_send_msg(blob->bs->md_thread, _spdk_blob_insert_cluster_msg, ctx); 5286 } 5287 5288 /* START spdk_blob_close */ 5289 5290 static void 5291 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 5292 { 5293 struct spdk_blob *blob = cb_arg; 5294 5295 if (bserrno == 0) { 5296 blob->open_ref--; 5297 if (blob->open_ref == 0) { 5298 /* 5299 * Blobs with active.num_pages == 0 are deleted blobs. 5300 * these blobs are removed from the blob_store list 5301 * when the deletion process starts - so don't try to 5302 * remove them again. 5303 */ 5304 if (blob->active.num_pages > 0) { 5305 TAILQ_REMOVE(&blob->bs->blobs, blob, link); 5306 } 5307 _spdk_blob_free(blob); 5308 } 5309 } 5310 5311 spdk_bs_sequence_finish(seq, bserrno); 5312 } 5313 5314 void spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg) 5315 { 5316 struct spdk_bs_cpl cpl; 5317 spdk_bs_sequence_t *seq; 5318 5319 _spdk_blob_verify_md_op(blob); 5320 5321 SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Closing blob %lu\n", blob->id); 5322 5323 if (blob->open_ref == 0) { 5324 cb_fn(cb_arg, -EBADF); 5325 return; 5326 } 5327 5328 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 5329 cpl.u.blob_basic.cb_fn = cb_fn; 5330 cpl.u.blob_basic.cb_arg = cb_arg; 5331 5332 seq = spdk_bs_sequence_start(blob->bs->md_channel, &cpl); 5333 if (!seq) { 5334 cb_fn(cb_arg, -ENOMEM); 5335 return; 5336 } 5337 5338 /* Sync metadata */ 5339 _spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, blob); 5340 } 5341 5342 /* END spdk_blob_close */ 5343 5344 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 5345 { 5346 return spdk_get_io_channel(bs); 5347 } 5348 5349 void spdk_bs_free_io_channel(struct spdk_io_channel *channel) 5350 { 5351 spdk_put_io_channel(channel); 5352 } 5353 5354 void spdk_blob_io_unmap(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_UNMAP); 5359 } 5360 5361 void spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel, 5362 uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg) 5363 { 5364 _spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg, 5365 SPDK_BLOB_WRITE_ZEROES); 5366 } 5367 5368 void spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel, 5369 void *payload, uint64_t offset, uint64_t length, 5370 spdk_blob_op_complete cb_fn, void *cb_arg) 5371 { 5372 _spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 5373 SPDK_BLOB_WRITE); 5374 } 5375 5376 void spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel, 5377 void *payload, uint64_t offset, uint64_t length, 5378 spdk_blob_op_complete cb_fn, void *cb_arg) 5379 { 5380 _spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg, 5381 SPDK_BLOB_READ); 5382 } 5383 5384 void spdk_blob_io_writev(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, false); 5389 } 5390 5391 void spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel, 5392 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 5393 spdk_blob_op_complete cb_fn, void *cb_arg) 5394 { 5395 _spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true); 5396 } 5397 5398 struct spdk_bs_iter_ctx { 5399 int64_t page_num; 5400 struct spdk_blob_store *bs; 5401 5402 spdk_blob_op_with_handle_complete cb_fn; 5403 void *cb_arg; 5404 }; 5405 5406 static void 5407 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno) 5408 { 5409 struct spdk_bs_iter_ctx *ctx = cb_arg; 5410 struct spdk_blob_store *bs = ctx->bs; 5411 spdk_blob_id id; 5412 5413 if (bserrno == 0) { 5414 ctx->cb_fn(ctx->cb_arg, _blob, bserrno); 5415 free(ctx); 5416 return; 5417 } 5418 5419 ctx->page_num++; 5420 ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num); 5421 if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) { 5422 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT); 5423 free(ctx); 5424 return; 5425 } 5426 5427 id = _spdk_bs_page_to_blobid(ctx->page_num); 5428 5429 spdk_bs_open_blob(bs, id, _spdk_bs_iter_cpl, ctx); 5430 } 5431 5432 void 5433 spdk_bs_iter_first(struct spdk_blob_store *bs, 5434 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5435 { 5436 struct spdk_bs_iter_ctx *ctx; 5437 5438 ctx = calloc(1, sizeof(*ctx)); 5439 if (!ctx) { 5440 cb_fn(cb_arg, NULL, -ENOMEM); 5441 return; 5442 } 5443 5444 ctx->page_num = -1; 5445 ctx->bs = bs; 5446 ctx->cb_fn = cb_fn; 5447 ctx->cb_arg = cb_arg; 5448 5449 _spdk_bs_iter_cpl(ctx, NULL, -1); 5450 } 5451 5452 static void 5453 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno) 5454 { 5455 struct spdk_bs_iter_ctx *ctx = cb_arg; 5456 5457 _spdk_bs_iter_cpl(ctx, NULL, -1); 5458 } 5459 5460 void 5461 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob, 5462 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 5463 { 5464 struct spdk_bs_iter_ctx *ctx; 5465 5466 assert(blob != NULL); 5467 5468 ctx = calloc(1, sizeof(*ctx)); 5469 if (!ctx) { 5470 cb_fn(cb_arg, NULL, -ENOMEM); 5471 return; 5472 } 5473 5474 ctx->page_num = _spdk_bs_blobid_to_page(blob->id); 5475 ctx->bs = bs; 5476 ctx->cb_fn = cb_fn; 5477 ctx->cb_arg = cb_arg; 5478 5479 /* Close the existing blob */ 5480 spdk_blob_close(blob, _spdk_bs_iter_close_cpl, ctx); 5481 } 5482 5483 static int 5484 _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 5485 uint16_t value_len, bool internal) 5486 { 5487 struct spdk_xattr_tailq *xattrs; 5488 struct spdk_xattr *xattr; 5489 5490 _spdk_blob_verify_md_op(blob); 5491 5492 if (blob->md_ro) { 5493 return -EPERM; 5494 } 5495 5496 if (internal) { 5497 xattrs = &blob->xattrs_internal; 5498 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR; 5499 } else { 5500 xattrs = &blob->xattrs; 5501 } 5502 5503 TAILQ_FOREACH(xattr, xattrs, link) { 5504 if (!strcmp(name, xattr->name)) { 5505 free(xattr->value); 5506 xattr->value_len = value_len; 5507 xattr->value = malloc(value_len); 5508 memcpy(xattr->value, value, value_len); 5509 5510 blob->state = SPDK_BLOB_STATE_DIRTY; 5511 5512 return 0; 5513 } 5514 } 5515 5516 xattr = calloc(1, sizeof(*xattr)); 5517 if (!xattr) { 5518 return -ENOMEM; 5519 } 5520 xattr->name = strdup(name); 5521 xattr->value_len = value_len; 5522 xattr->value = malloc(value_len); 5523 memcpy(xattr->value, value, value_len); 5524 TAILQ_INSERT_TAIL(xattrs, xattr, link); 5525 5526 blob->state = SPDK_BLOB_STATE_DIRTY; 5527 5528 return 0; 5529 } 5530 5531 int 5532 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 5533 uint16_t value_len) 5534 { 5535 return _spdk_blob_set_xattr(blob, name, value, value_len, false); 5536 } 5537 5538 static int 5539 _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal) 5540 { 5541 struct spdk_xattr_tailq *xattrs; 5542 struct spdk_xattr *xattr; 5543 5544 _spdk_blob_verify_md_op(blob); 5545 5546 if (blob->md_ro) { 5547 return -EPERM; 5548 } 5549 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 5550 5551 TAILQ_FOREACH(xattr, xattrs, link) { 5552 if (!strcmp(name, xattr->name)) { 5553 TAILQ_REMOVE(xattrs, xattr, link); 5554 free(xattr->value); 5555 free(xattr->name); 5556 free(xattr); 5557 5558 if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) { 5559 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR; 5560 } 5561 blob->state = SPDK_BLOB_STATE_DIRTY; 5562 5563 return 0; 5564 } 5565 } 5566 5567 return -ENOENT; 5568 } 5569 5570 int 5571 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name) 5572 { 5573 return _spdk_blob_remove_xattr(blob, name, false); 5574 } 5575 5576 static int 5577 _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 5578 const void **value, size_t *value_len, bool internal) 5579 { 5580 struct spdk_xattr *xattr; 5581 struct spdk_xattr_tailq *xattrs; 5582 5583 xattrs = internal ? &blob->xattrs_internal : &blob->xattrs; 5584 5585 TAILQ_FOREACH(xattr, xattrs, link) { 5586 if (!strcmp(name, xattr->name)) { 5587 *value = xattr->value; 5588 *value_len = xattr->value_len; 5589 return 0; 5590 } 5591 } 5592 return -ENOENT; 5593 } 5594 5595 int 5596 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, 5597 const void **value, size_t *value_len) 5598 { 5599 _spdk_blob_verify_md_op(blob); 5600 5601 return _spdk_blob_get_xattr_value(blob, name, value, value_len, false); 5602 } 5603 5604 struct spdk_xattr_names { 5605 uint32_t count; 5606 const char *names[0]; 5607 }; 5608 5609 static int 5610 _spdk_blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names) 5611 { 5612 struct spdk_xattr *xattr; 5613 int count = 0; 5614 5615 TAILQ_FOREACH(xattr, xattrs, link) { 5616 count++; 5617 } 5618 5619 *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *)); 5620 if (*names == NULL) { 5621 return -ENOMEM; 5622 } 5623 5624 TAILQ_FOREACH(xattr, xattrs, link) { 5625 (*names)->names[(*names)->count++] = xattr->name; 5626 } 5627 5628 return 0; 5629 } 5630 5631 int 5632 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names) 5633 { 5634 _spdk_blob_verify_md_op(blob); 5635 5636 return _spdk_blob_get_xattr_names(&blob->xattrs, names); 5637 } 5638 5639 uint32_t 5640 spdk_xattr_names_get_count(struct spdk_xattr_names *names) 5641 { 5642 assert(names != NULL); 5643 5644 return names->count; 5645 } 5646 5647 const char * 5648 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index) 5649 { 5650 if (index >= names->count) { 5651 return NULL; 5652 } 5653 5654 return names->names[index]; 5655 } 5656 5657 void 5658 spdk_xattr_names_free(struct spdk_xattr_names *names) 5659 { 5660 free(names); 5661 } 5662 5663 struct spdk_bs_type 5664 spdk_bs_get_bstype(struct spdk_blob_store *bs) 5665 { 5666 return bs->bstype; 5667 } 5668 5669 void 5670 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype) 5671 { 5672 memcpy(&bs->bstype, &bstype, sizeof(bstype)); 5673 } 5674 5675 bool 5676 spdk_blob_is_read_only(struct spdk_blob *blob) 5677 { 5678 assert(blob != NULL); 5679 return (blob->data_ro || blob->md_ro); 5680 } 5681 5682 bool 5683 spdk_blob_is_snapshot(struct spdk_blob *blob) 5684 { 5685 struct spdk_blob_list *snapshot_entry; 5686 5687 assert(blob != NULL); 5688 5689 TAILQ_FOREACH(snapshot_entry, &blob->bs->snapshots, link) { 5690 if (snapshot_entry->id == blob->id) { 5691 break; 5692 } 5693 } 5694 5695 if (snapshot_entry == NULL) { 5696 return false; 5697 } 5698 5699 return true; 5700 } 5701 5702 bool 5703 spdk_blob_is_clone(struct spdk_blob *blob) 5704 { 5705 assert(blob != NULL); 5706 5707 if (blob->parent_id != SPDK_BLOBID_INVALID) { 5708 assert(spdk_blob_is_thin_provisioned(blob)); 5709 return true; 5710 } 5711 5712 return false; 5713 } 5714 5715 bool 5716 spdk_blob_is_thin_provisioned(struct spdk_blob *blob) 5717 { 5718 assert(blob != NULL); 5719 return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV); 5720 } 5721 5722 spdk_blob_id 5723 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id) 5724 { 5725 struct spdk_blob_list *snapshot_entry = NULL; 5726 struct spdk_blob_list *clone_entry = NULL; 5727 5728 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 5729 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 5730 if (clone_entry->id == blob_id) { 5731 return snapshot_entry->id; 5732 } 5733 } 5734 } 5735 5736 return SPDK_BLOBID_INVALID; 5737 } 5738 5739 int 5740 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids, 5741 size_t *count) 5742 { 5743 struct spdk_blob_list *snapshot_entry, *clone_entry; 5744 size_t n; 5745 5746 TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) { 5747 if (snapshot_entry->id == blobid) { 5748 break; 5749 } 5750 } 5751 if (snapshot_entry == NULL) { 5752 *count = 0; 5753 return 0; 5754 } 5755 5756 if (ids == NULL || *count < snapshot_entry->clone_count) { 5757 *count = snapshot_entry->clone_count; 5758 return -ENOMEM; 5759 } 5760 *count = snapshot_entry->clone_count; 5761 5762 n = 0; 5763 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) { 5764 ids[n++] = clone_entry->id; 5765 } 5766 5767 return 0; 5768 } 5769 5770 SPDK_LOG_REGISTER_COMPONENT("blob", SPDK_LOG_BLOB) 5771