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