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