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