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