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