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