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