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