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