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