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