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/io_channel.h" 41 #include "spdk/bit_array.h" 42 #include "spdk/likely.h" 43 44 #include "spdk_internal/log.h" 45 46 #include "blobstore.h" 47 48 #define BLOB_CRC32C_INITIAL 0xffffffffUL 49 50 static inline size_t 51 divide_round_up(size_t num, size_t divisor) 52 { 53 return (num + divisor - 1) / divisor; 54 } 55 56 static void 57 _spdk_bs_claim_cluster(struct spdk_blob_store *bs, uint32_t cluster_num) 58 { 59 assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters)); 60 assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == false); 61 assert(bs->num_free_clusters > 0); 62 63 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Claiming cluster %u\n", cluster_num); 64 65 spdk_bit_array_set(bs->used_clusters, cluster_num); 66 bs->num_free_clusters--; 67 } 68 69 static void 70 _spdk_bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num) 71 { 72 assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters)); 73 assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == true); 74 assert(bs->num_free_clusters < bs->total_clusters); 75 76 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Releasing cluster %u\n", cluster_num); 77 78 spdk_bit_array_clear(bs->used_clusters, cluster_num); 79 bs->num_free_clusters++; 80 } 81 82 static struct spdk_blob * 83 _spdk_blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id) 84 { 85 struct spdk_blob *blob; 86 87 blob = calloc(1, sizeof(*blob)); 88 if (!blob) { 89 return NULL; 90 } 91 92 blob->id = id; 93 blob->bs = bs; 94 95 blob->state = SPDK_BLOB_STATE_DIRTY; 96 blob->active.num_pages = 1; 97 blob->active.pages = calloc(1, sizeof(*blob->active.pages)); 98 if (!blob->active.pages) { 99 free(blob); 100 return NULL; 101 } 102 103 blob->active.pages[0] = _spdk_bs_blobid_to_page(id); 104 105 TAILQ_INIT(&blob->xattrs); 106 107 return blob; 108 } 109 110 static void 111 _spdk_blob_free(struct spdk_blob *blob) 112 { 113 struct spdk_xattr *xattr, *xattr_tmp; 114 115 assert(blob != NULL); 116 117 free(blob->active.clusters); 118 free(blob->clean.clusters); 119 free(blob->active.pages); 120 free(blob->clean.pages); 121 122 TAILQ_FOREACH_SAFE(xattr, &blob->xattrs, link, xattr_tmp) { 123 TAILQ_REMOVE(&blob->xattrs, xattr, link); 124 free(xattr->name); 125 free(xattr->value); 126 free(xattr); 127 } 128 129 free(blob); 130 } 131 132 static int 133 _spdk_blob_mark_clean(struct spdk_blob *blob) 134 { 135 uint64_t *clusters = NULL; 136 uint32_t *pages = NULL; 137 138 assert(blob != NULL); 139 assert(blob->state == SPDK_BLOB_STATE_LOADING || 140 blob->state == SPDK_BLOB_STATE_SYNCING); 141 142 if (blob->active.num_clusters) { 143 assert(blob->active.clusters); 144 clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters)); 145 if (!clusters) { 146 return -1; 147 } 148 memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*clusters)); 149 } 150 151 if (blob->active.num_pages) { 152 assert(blob->active.pages); 153 pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages)); 154 if (!pages) { 155 free(clusters); 156 return -1; 157 } 158 memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*pages)); 159 } 160 161 free(blob->clean.clusters); 162 free(blob->clean.pages); 163 164 blob->clean.num_clusters = blob->active.num_clusters; 165 blob->clean.clusters = blob->active.clusters; 166 blob->clean.num_pages = blob->active.num_pages; 167 blob->clean.pages = blob->active.pages; 168 169 blob->active.clusters = clusters; 170 blob->active.pages = pages; 171 172 blob->state = SPDK_BLOB_STATE_CLEAN; 173 174 return 0; 175 } 176 177 static void 178 _spdk_blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob) 179 { 180 struct spdk_blob_md_descriptor *desc; 181 size_t cur_desc = 0; 182 void *tmp; 183 184 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 185 while (cur_desc < sizeof(page->descriptors)) { 186 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 187 if (desc->length == 0) { 188 /* If padding and length are 0, this terminates the page */ 189 break; 190 } 191 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 192 struct spdk_blob_md_descriptor_extent *desc_extent; 193 unsigned int i, j; 194 unsigned int cluster_count = blob->active.num_clusters; 195 196 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 197 198 assert(desc_extent->length > 0); 199 assert(desc_extent->length % sizeof(desc_extent->extents[0]) == 0); 200 201 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 202 for (j = 0; j < desc_extent->extents[i].length; j++) { 203 assert(spdk_bit_array_get(blob->bs->used_clusters, desc_extent->extents[i].cluster_idx + j)); 204 cluster_count++; 205 } 206 } 207 208 assert(cluster_count > 0); 209 tmp = realloc(blob->active.clusters, cluster_count * sizeof(uint64_t)); 210 assert(tmp != NULL); 211 blob->active.clusters = tmp; 212 blob->active.cluster_array_size = cluster_count; 213 214 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 215 for (j = 0; j < desc_extent->extents[i].length; j++) { 216 blob->active.clusters[blob->active.num_clusters++] = _spdk_bs_cluster_to_lba(blob->bs, 217 desc_extent->extents[i].cluster_idx + j); 218 } 219 } 220 221 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 222 struct spdk_blob_md_descriptor_xattr *desc_xattr; 223 struct spdk_xattr *xattr; 224 225 desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc; 226 227 assert(desc_xattr->length == sizeof(desc_xattr->name_length) + 228 sizeof(desc_xattr->value_length) + 229 desc_xattr->name_length + desc_xattr->value_length); 230 231 xattr = calloc(1, sizeof(*xattr)); 232 assert(xattr != NULL); 233 234 xattr->name = malloc(desc_xattr->name_length + 1); 235 assert(xattr->name); 236 strncpy(xattr->name, desc_xattr->name, desc_xattr->name_length); 237 xattr->name[desc_xattr->name_length] = '\0'; 238 239 xattr->value = malloc(desc_xattr->value_length); 240 assert(xattr->value != NULL); 241 xattr->value_len = desc_xattr->value_length; 242 memcpy(xattr->value, 243 (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length), 244 desc_xattr->value_length); 245 246 TAILQ_INSERT_TAIL(&blob->xattrs, xattr, link); 247 } else { 248 /* Error */ 249 break; 250 } 251 252 /* Advance to the next descriptor */ 253 cur_desc += sizeof(*desc) + desc->length; 254 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 255 break; 256 } 257 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 258 } 259 } 260 261 static int 262 _spdk_blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count, 263 struct spdk_blob *blob) 264 { 265 const struct spdk_blob_md_page *page; 266 uint32_t i; 267 268 assert(page_count > 0); 269 assert(pages[0].sequence_num == 0); 270 assert(blob != NULL); 271 assert(blob->state == SPDK_BLOB_STATE_LOADING); 272 assert(blob->active.clusters == NULL); 273 assert(blob->state == SPDK_BLOB_STATE_LOADING); 274 275 /* The blobid provided doesn't match what's in the MD, this can 276 * happen for example if a bogus blobid is passed in through open. 277 */ 278 if (blob->id != pages[0].id) { 279 SPDK_ERRLOG("Blobid (%lu) doesn't match what's in metadata (%lu)\n", 280 blob->id, pages[0].id); 281 return -ENOENT; 282 } 283 284 for (i = 0; i < page_count; i++) { 285 page = &pages[i]; 286 287 assert(page->id == blob->id); 288 assert(page->sequence_num == i); 289 290 _spdk_blob_parse_page(page, blob); 291 } 292 293 return 0; 294 } 295 296 static int 297 _spdk_blob_serialize_add_page(const struct spdk_blob *blob, 298 struct spdk_blob_md_page **pages, 299 uint32_t *page_count, 300 struct spdk_blob_md_page **last_page) 301 { 302 struct spdk_blob_md_page *page; 303 304 assert(pages != NULL); 305 assert(page_count != NULL); 306 307 if (*page_count == 0) { 308 assert(*pages == NULL); 309 *page_count = 1; 310 *pages = spdk_dma_malloc(SPDK_BS_PAGE_SIZE, 311 SPDK_BS_PAGE_SIZE, 312 NULL); 313 } else { 314 assert(*pages != NULL); 315 (*page_count)++; 316 *pages = spdk_dma_realloc(*pages, 317 SPDK_BS_PAGE_SIZE * (*page_count), 318 SPDK_BS_PAGE_SIZE, 319 NULL); 320 } 321 322 if (*pages == NULL) { 323 *page_count = 0; 324 *last_page = NULL; 325 return -ENOMEM; 326 } 327 328 page = &(*pages)[*page_count - 1]; 329 memset(page, 0, sizeof(*page)); 330 page->id = blob->id; 331 page->sequence_num = *page_count - 1; 332 page->next = SPDK_INVALID_MD_PAGE; 333 *last_page = page; 334 335 return 0; 336 } 337 338 /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor. 339 * Update required_sz on both success and failure. 340 * 341 */ 342 static int 343 _spdk_blob_serialize_xattr(const struct spdk_xattr *xattr, 344 uint8_t *buf, size_t buf_sz, 345 size_t *required_sz) 346 { 347 struct spdk_blob_md_descriptor_xattr *desc; 348 349 *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) + 350 strlen(xattr->name) + 351 xattr->value_len; 352 353 if (buf_sz < *required_sz) { 354 return -1; 355 } 356 357 desc = (struct spdk_blob_md_descriptor_xattr *)buf; 358 359 desc->type = SPDK_MD_DESCRIPTOR_TYPE_XATTR; 360 desc->length = sizeof(desc->name_length) + 361 sizeof(desc->value_length) + 362 strlen(xattr->name) + 363 xattr->value_len; 364 desc->name_length = strlen(xattr->name); 365 desc->value_length = xattr->value_len; 366 367 memcpy(desc->name, xattr->name, desc->name_length); 368 memcpy((void *)((uintptr_t)desc->name + desc->name_length), 369 xattr->value, 370 desc->value_length); 371 372 return 0; 373 } 374 375 static void 376 _spdk_blob_serialize_extent(const struct spdk_blob *blob, 377 uint64_t start_cluster, uint64_t *next_cluster, 378 uint8_t *buf, size_t buf_sz) 379 { 380 struct spdk_blob_md_descriptor_extent *desc; 381 size_t cur_sz; 382 uint64_t i, extent_idx; 383 uint32_t lba, lba_per_cluster, lba_count; 384 385 /* The buffer must have room for at least one extent */ 386 cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->extents[0]); 387 if (buf_sz < cur_sz) { 388 *next_cluster = start_cluster; 389 return; 390 } 391 392 desc = (struct spdk_blob_md_descriptor_extent *)buf; 393 desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT; 394 395 lba_per_cluster = _spdk_bs_cluster_to_lba(blob->bs, 1); 396 397 lba = blob->active.clusters[start_cluster]; 398 lba_count = lba_per_cluster; 399 extent_idx = 0; 400 for (i = start_cluster + 1; i < blob->active.num_clusters; i++) { 401 if ((lba + lba_count) == blob->active.clusters[i]) { 402 lba_count += lba_per_cluster; 403 continue; 404 } 405 desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster; 406 desc->extents[extent_idx].length = lba_count / lba_per_cluster; 407 extent_idx++; 408 409 cur_sz += sizeof(desc->extents[extent_idx]); 410 411 if (buf_sz < cur_sz) { 412 /* If we ran out of buffer space, return */ 413 desc->length = sizeof(desc->extents[0]) * extent_idx; 414 *next_cluster = i; 415 return; 416 } 417 418 lba = blob->active.clusters[i]; 419 lba_count = lba_per_cluster; 420 } 421 422 desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster; 423 desc->extents[extent_idx].length = lba_count / lba_per_cluster; 424 extent_idx++; 425 426 desc->length = sizeof(desc->extents[0]) * extent_idx; 427 *next_cluster = blob->active.num_clusters; 428 429 return; 430 } 431 432 static int 433 _spdk_blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages, 434 uint32_t *page_count) 435 { 436 struct spdk_blob_md_page *cur_page; 437 const struct spdk_xattr *xattr; 438 int rc; 439 uint8_t *buf; 440 size_t remaining_sz; 441 uint64_t last_cluster; 442 443 assert(pages != NULL); 444 assert(page_count != NULL); 445 assert(blob != NULL); 446 assert(blob->state == SPDK_BLOB_STATE_SYNCING); 447 448 *pages = NULL; 449 *page_count = 0; 450 451 /* A blob always has at least 1 page, even if it has no descriptors */ 452 rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page); 453 if (rc < 0) { 454 return rc; 455 } 456 457 buf = (uint8_t *)cur_page->descriptors; 458 remaining_sz = sizeof(cur_page->descriptors); 459 460 /* Serialize xattrs */ 461 TAILQ_FOREACH(xattr, &blob->xattrs, link) { 462 size_t required_sz = 0; 463 rc = _spdk_blob_serialize_xattr(xattr, 464 buf, remaining_sz, 465 &required_sz); 466 if (rc < 0) { 467 /* Need to add a new page to the chain */ 468 rc = _spdk_blob_serialize_add_page(blob, pages, page_count, 469 &cur_page); 470 if (rc < 0) { 471 spdk_dma_free(*pages); 472 *pages = NULL; 473 *page_count = 0; 474 return rc; 475 } 476 477 buf = (uint8_t *)cur_page->descriptors; 478 remaining_sz = sizeof(cur_page->descriptors); 479 480 /* Try again */ 481 required_sz = 0; 482 rc = _spdk_blob_serialize_xattr(xattr, 483 buf, remaining_sz, 484 &required_sz); 485 486 if (rc < 0) { 487 spdk_dma_free(*pages); 488 *pages = NULL; 489 *page_count = 0; 490 return -1; 491 } 492 } 493 494 remaining_sz -= required_sz; 495 buf += required_sz; 496 } 497 498 /* Serialize extents */ 499 last_cluster = 0; 500 while (last_cluster < blob->active.num_clusters) { 501 _spdk_blob_serialize_extent(blob, last_cluster, &last_cluster, 502 buf, remaining_sz); 503 504 if (last_cluster == blob->active.num_clusters) { 505 break; 506 } 507 508 rc = _spdk_blob_serialize_add_page(blob, pages, page_count, 509 &cur_page); 510 if (rc < 0) { 511 return rc; 512 } 513 514 buf = (uint8_t *)cur_page->descriptors; 515 remaining_sz = sizeof(cur_page->descriptors); 516 } 517 518 return 0; 519 } 520 521 struct spdk_blob_load_ctx { 522 struct spdk_blob *blob; 523 524 struct spdk_blob_md_page *pages; 525 uint32_t num_pages; 526 527 spdk_bs_sequence_cpl cb_fn; 528 void *cb_arg; 529 }; 530 531 static uint32_t 532 _spdk_blob_md_page_calc_crc(void *page) 533 { 534 uint32_t crc; 535 536 crc = BLOB_CRC32C_INITIAL; 537 crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc); 538 crc ^= BLOB_CRC32C_INITIAL; 539 540 return crc; 541 542 } 543 544 static void 545 _spdk_blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 546 { 547 struct spdk_blob_load_ctx *ctx = cb_arg; 548 struct spdk_blob *blob = ctx->blob; 549 struct spdk_blob_md_page *page; 550 int rc; 551 uint32_t crc; 552 553 page = &ctx->pages[ctx->num_pages - 1]; 554 crc = _spdk_blob_md_page_calc_crc(page); 555 if (crc != page->crc) { 556 SPDK_ERRLOG("Metadata page %d crc mismatch\n", ctx->num_pages); 557 _spdk_blob_free(blob); 558 ctx->cb_fn(seq, NULL, -EINVAL); 559 spdk_dma_free(ctx->pages); 560 free(ctx); 561 return; 562 } 563 564 if (page->next != SPDK_INVALID_MD_PAGE) { 565 uint32_t next_page = page->next; 566 uint64_t next_lba = _spdk_bs_page_to_lba(blob->bs, blob->bs->md_start + next_page); 567 568 569 assert(next_lba < (blob->bs->md_start + blob->bs->md_len)); 570 571 /* Read the next page */ 572 ctx->num_pages++; 573 ctx->pages = spdk_dma_realloc(ctx->pages, (sizeof(*page) * ctx->num_pages), 574 sizeof(*page), NULL); 575 if (ctx->pages == NULL) { 576 ctx->cb_fn(seq, ctx->cb_arg, -ENOMEM); 577 free(ctx); 578 return; 579 } 580 581 spdk_bs_sequence_read(seq, &ctx->pages[ctx->num_pages - 1], 582 next_lba, 583 _spdk_bs_byte_to_lba(blob->bs, sizeof(*page)), 584 _spdk_blob_load_cpl, ctx); 585 return; 586 } 587 588 /* Parse the pages */ 589 rc = _spdk_blob_parse(ctx->pages, ctx->num_pages, blob); 590 if (rc) { 591 _spdk_blob_free(blob); 592 ctx->cb_fn(seq, NULL, rc); 593 spdk_dma_free(ctx->pages); 594 free(ctx); 595 return; 596 } 597 598 _spdk_blob_mark_clean(blob); 599 600 ctx->cb_fn(seq, ctx->cb_arg, rc); 601 602 /* Free the memory */ 603 spdk_dma_free(ctx->pages); 604 free(ctx); 605 } 606 607 /* Load a blob from disk given a blobid */ 608 static void 609 _spdk_blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 610 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 611 { 612 struct spdk_blob_load_ctx *ctx; 613 struct spdk_blob_store *bs; 614 uint32_t page_num; 615 uint64_t lba; 616 617 assert(blob != NULL); 618 assert(blob->state == SPDK_BLOB_STATE_CLEAN || 619 blob->state == SPDK_BLOB_STATE_DIRTY); 620 621 bs = blob->bs; 622 623 ctx = calloc(1, sizeof(*ctx)); 624 if (!ctx) { 625 cb_fn(seq, cb_arg, -ENOMEM); 626 return; 627 } 628 629 ctx->blob = blob; 630 ctx->pages = spdk_dma_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 631 SPDK_BS_PAGE_SIZE, NULL); 632 if (!ctx->pages) { 633 free(ctx); 634 cb_fn(seq, cb_arg, -ENOMEM); 635 return; 636 } 637 ctx->num_pages = 1; 638 ctx->cb_fn = cb_fn; 639 ctx->cb_arg = cb_arg; 640 641 page_num = _spdk_bs_blobid_to_page(blob->id); 642 lba = _spdk_bs_page_to_lba(blob->bs, bs->md_start + page_num); 643 644 blob->state = SPDK_BLOB_STATE_LOADING; 645 646 spdk_bs_sequence_read(seq, &ctx->pages[0], lba, 647 _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE), 648 _spdk_blob_load_cpl, ctx); 649 } 650 651 struct spdk_blob_persist_ctx { 652 struct spdk_blob *blob; 653 654 struct spdk_blob_md_page *pages; 655 656 uint64_t idx; 657 658 spdk_bs_sequence_cpl cb_fn; 659 void *cb_arg; 660 }; 661 662 static void 663 _spdk_blob_persist_complete(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 664 { 665 struct spdk_blob_persist_ctx *ctx = cb_arg; 666 struct spdk_blob *blob = ctx->blob; 667 668 if (bserrno == 0) { 669 _spdk_blob_mark_clean(blob); 670 } 671 672 /* Call user callback */ 673 ctx->cb_fn(seq, ctx->cb_arg, bserrno); 674 675 /* Free the memory */ 676 spdk_dma_free(ctx->pages); 677 free(ctx); 678 } 679 680 static void 681 _spdk_blob_persist_unmap_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 682 { 683 struct spdk_blob_persist_ctx *ctx = cb_arg; 684 struct spdk_blob *blob = ctx->blob; 685 struct spdk_blob_store *bs = blob->bs; 686 void *tmp; 687 size_t i; 688 689 /* Release all clusters that were truncated */ 690 for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) { 691 uint32_t cluster_num = _spdk_bs_lba_to_cluster(bs, blob->active.clusters[i]); 692 693 _spdk_bs_release_cluster(bs, cluster_num); 694 } 695 696 if (blob->active.num_clusters == 0) { 697 free(blob->active.clusters); 698 blob->active.clusters = NULL; 699 blob->active.cluster_array_size = 0; 700 } else { 701 tmp = realloc(blob->active.clusters, sizeof(uint64_t) * blob->active.num_clusters); 702 assert(tmp != NULL); 703 blob->active.clusters = tmp; 704 blob->active.cluster_array_size = blob->active.num_clusters; 705 } 706 707 _spdk_blob_persist_complete(seq, ctx, bserrno); 708 } 709 710 static void 711 _spdk_blob_persist_unmap_clusters(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 712 { 713 struct spdk_blob_persist_ctx *ctx = cb_arg; 714 struct spdk_blob *blob = ctx->blob; 715 struct spdk_blob_store *bs = blob->bs; 716 spdk_bs_batch_t *batch; 717 size_t i; 718 uint64_t lba; 719 uint32_t lba_count; 720 721 /* Clusters don't move around in blobs. The list shrinks or grows 722 * at the end, but no changes ever occur in the middle of the list. 723 */ 724 725 batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_unmap_clusters_cpl, ctx); 726 727 /* Unmap all clusters that were truncated */ 728 lba = 0; 729 lba_count = 0; 730 for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) { 731 uint64_t next_lba = blob->active.clusters[i]; 732 uint32_t next_lba_count = _spdk_bs_cluster_to_lba(bs, 1); 733 734 if ((lba + lba_count) == next_lba) { 735 /* This cluster is contiguous with the previous one. */ 736 lba_count += next_lba_count; 737 continue; 738 } 739 740 /* This cluster is not contiguous with the previous one. */ 741 742 /* If a run of LBAs previously existing, send them 743 * as an unmap. 744 */ 745 if (lba_count > 0) { 746 spdk_bs_batch_unmap(batch, lba, lba_count); 747 } 748 749 /* Start building the next batch */ 750 lba = next_lba; 751 lba_count = next_lba_count; 752 } 753 754 /* If we ended with a contiguous set of LBAs, send the unmap now */ 755 if (lba_count > 0) { 756 spdk_bs_batch_unmap(batch, lba, lba_count); 757 } 758 759 spdk_bs_batch_close(batch); 760 } 761 762 static void 763 _spdk_blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 764 { 765 struct spdk_blob_persist_ctx *ctx = cb_arg; 766 struct spdk_blob *blob = ctx->blob; 767 struct spdk_blob_store *bs = blob->bs; 768 size_t i; 769 770 /* This loop starts at 1 because the first page is special and handled 771 * below. The pages (except the first) are never written in place, 772 * so any pages in the clean list must be zeroed. 773 */ 774 for (i = 1; i < blob->clean.num_pages; i++) { 775 spdk_bit_array_clear(bs->used_md_pages, blob->clean.pages[i]); 776 } 777 778 if (blob->active.num_pages == 0) { 779 uint32_t page_num; 780 781 page_num = _spdk_bs_blobid_to_page(blob->id); 782 spdk_bit_array_clear(bs->used_md_pages, page_num); 783 } 784 785 /* Move on to unmapping clusters */ 786 _spdk_blob_persist_unmap_clusters(seq, ctx, 0); 787 } 788 789 static void 790 _spdk_blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 791 { 792 struct spdk_blob_persist_ctx *ctx = cb_arg; 793 struct spdk_blob *blob = ctx->blob; 794 struct spdk_blob_store *bs = blob->bs; 795 uint64_t lba; 796 uint32_t lba_count; 797 spdk_bs_batch_t *batch; 798 size_t i; 799 800 batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_zero_pages_cpl, ctx); 801 802 lba_count = _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE); 803 804 /* This loop starts at 1 because the first page is special and handled 805 * below. The pages (except the first) are never written in place, 806 * so any pages in the clean list must be zeroed. 807 */ 808 for (i = 1; i < blob->clean.num_pages; i++) { 809 lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->clean.pages[i]); 810 811 spdk_bs_batch_write_zeroes(batch, lba, lba_count); 812 } 813 814 /* The first page will only be zeroed if this is a delete. */ 815 if (blob->active.num_pages == 0) { 816 uint32_t page_num; 817 818 /* The first page in the metadata goes where the blobid indicates */ 819 page_num = _spdk_bs_blobid_to_page(blob->id); 820 lba = _spdk_bs_page_to_lba(bs, bs->md_start + page_num); 821 822 spdk_bs_batch_write_zeroes(batch, lba, lba_count); 823 } 824 825 spdk_bs_batch_close(batch); 826 } 827 828 static void 829 _spdk_blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 830 { 831 struct spdk_blob_persist_ctx *ctx = cb_arg; 832 struct spdk_blob *blob = ctx->blob; 833 struct spdk_blob_store *bs = blob->bs; 834 uint64_t lba; 835 uint32_t lba_count; 836 struct spdk_blob_md_page *page; 837 838 if (blob->active.num_pages == 0) { 839 /* Move on to the next step */ 840 _spdk_blob_persist_zero_pages(seq, ctx, 0); 841 return; 842 } 843 844 lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page)); 845 846 page = &ctx->pages[0]; 847 /* The first page in the metadata goes where the blobid indicates */ 848 lba = _spdk_bs_page_to_lba(bs, bs->md_start + _spdk_bs_blobid_to_page(blob->id)); 849 850 spdk_bs_sequence_write(seq, page, lba, lba_count, 851 _spdk_blob_persist_zero_pages, ctx); 852 } 853 854 static void 855 _spdk_blob_persist_write_page_chain(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 856 { 857 struct spdk_blob_persist_ctx *ctx = cb_arg; 858 struct spdk_blob *blob = ctx->blob; 859 struct spdk_blob_store *bs = blob->bs; 860 uint64_t lba; 861 uint32_t lba_count; 862 struct spdk_blob_md_page *page; 863 spdk_bs_batch_t *batch; 864 size_t i; 865 866 /* Clusters don't move around in blobs. The list shrinks or grows 867 * at the end, but no changes ever occur in the middle of the list. 868 */ 869 870 lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page)); 871 872 batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_write_page_root, ctx); 873 874 /* This starts at 1. The root page is not written until 875 * all of the others are finished 876 */ 877 for (i = 1; i < blob->active.num_pages; i++) { 878 page = &ctx->pages[i]; 879 assert(page->sequence_num == i); 880 881 lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->active.pages[i]); 882 883 spdk_bs_batch_write(batch, page, lba, lba_count); 884 } 885 886 spdk_bs_batch_close(batch); 887 } 888 889 static int 890 _spdk_resize_blob(struct spdk_blob *blob, uint64_t sz) 891 { 892 uint64_t i; 893 uint64_t *tmp; 894 uint64_t lfc; /* lowest free cluster */ 895 struct spdk_blob_store *bs; 896 897 bs = blob->bs; 898 899 assert(blob->state != SPDK_BLOB_STATE_LOADING && 900 blob->state != SPDK_BLOB_STATE_SYNCING); 901 902 if (blob->active.num_clusters == sz) { 903 return 0; 904 } 905 906 if (blob->active.num_clusters < blob->active.cluster_array_size) { 907 /* If this blob was resized to be larger, then smaller, then 908 * larger without syncing, then the cluster array already 909 * contains spare assigned clusters we can use. 910 */ 911 blob->active.num_clusters = spdk_min(blob->active.cluster_array_size, 912 sz); 913 } 914 915 blob->state = SPDK_BLOB_STATE_DIRTY; 916 917 /* Do two passes - one to verify that we can obtain enough clusters 918 * and another to actually claim them. 919 */ 920 921 lfc = 0; 922 for (i = blob->active.num_clusters; i < sz; i++) { 923 lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc); 924 if (lfc >= bs->total_clusters) { 925 /* No more free clusters. Cannot satisfy the request */ 926 assert(false); 927 return -1; 928 } 929 lfc++; 930 } 931 932 if (sz > blob->active.num_clusters) { 933 /* Expand the cluster array if necessary. 934 * We only shrink the array when persisting. 935 */ 936 tmp = realloc(blob->active.clusters, sizeof(uint64_t) * sz); 937 if (sz > 0 && tmp == NULL) { 938 assert(false); 939 return -1; 940 } 941 blob->active.clusters = tmp; 942 blob->active.cluster_array_size = sz; 943 } 944 945 lfc = 0; 946 for (i = blob->active.num_clusters; i < sz; i++) { 947 lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc); 948 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Claiming cluster %lu for blob %lu\n", lfc, blob->id); 949 _spdk_bs_claim_cluster(bs, lfc); 950 blob->active.clusters[i] = _spdk_bs_cluster_to_lba(bs, lfc); 951 lfc++; 952 } 953 954 blob->active.num_clusters = sz; 955 956 return 0; 957 } 958 959 /* Write a blob to disk */ 960 static void 961 _spdk_blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob, 962 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 963 { 964 struct spdk_blob_persist_ctx *ctx; 965 int rc; 966 uint64_t i; 967 uint32_t page_num; 968 struct spdk_blob_store *bs; 969 970 assert(blob != NULL); 971 assert(blob->state == SPDK_BLOB_STATE_CLEAN || 972 blob->state == SPDK_BLOB_STATE_DIRTY); 973 974 if (blob->state == SPDK_BLOB_STATE_CLEAN) { 975 cb_fn(seq, cb_arg, 0); 976 return; 977 } 978 979 bs = blob->bs; 980 981 ctx = calloc(1, sizeof(*ctx)); 982 if (!ctx) { 983 cb_fn(seq, cb_arg, -ENOMEM); 984 return; 985 } 986 ctx->blob = blob; 987 ctx->cb_fn = cb_fn; 988 ctx->cb_arg = cb_arg; 989 990 blob->state = SPDK_BLOB_STATE_SYNCING; 991 992 if (blob->active.num_pages == 0) { 993 /* This is the signal that the blob should be deleted. 994 * Immediately jump to the clean up routine. */ 995 assert(blob->clean.num_pages > 0); 996 ctx->idx = blob->clean.num_pages - 1; 997 _spdk_blob_persist_zero_pages(seq, ctx, 0); 998 return; 999 1000 } 1001 1002 /* Generate the new metadata */ 1003 rc = _spdk_blob_serialize(blob, &ctx->pages, &blob->active.num_pages); 1004 if (rc < 0) { 1005 free(ctx); 1006 cb_fn(seq, cb_arg, rc); 1007 return; 1008 } 1009 1010 assert(blob->active.num_pages >= 1); 1011 1012 /* Resize the cache of page indices */ 1013 blob->active.pages = realloc(blob->active.pages, 1014 blob->active.num_pages * sizeof(*blob->active.pages)); 1015 if (!blob->active.pages) { 1016 free(ctx); 1017 cb_fn(seq, cb_arg, -ENOMEM); 1018 return; 1019 } 1020 1021 /* Assign this metadata to pages. This requires two passes - 1022 * one to verify that there are enough pages and a second 1023 * to actually claim them. */ 1024 page_num = 0; 1025 /* Note that this loop starts at one. The first page location is fixed by the blobid. */ 1026 for (i = 1; i < blob->active.num_pages; i++) { 1027 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num); 1028 if (page_num >= spdk_bit_array_capacity(bs->used_md_pages)) { 1029 spdk_dma_free(ctx->pages); 1030 free(ctx); 1031 blob->state = SPDK_BLOB_STATE_DIRTY; 1032 cb_fn(seq, cb_arg, -ENOMEM); 1033 return; 1034 } 1035 page_num++; 1036 } 1037 1038 page_num = 0; 1039 blob->active.pages[0] = _spdk_bs_blobid_to_page(blob->id); 1040 for (i = 1; i < blob->active.num_pages; i++) { 1041 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num); 1042 ctx->pages[i - 1].next = page_num; 1043 /* Now that previous metadata page is complete, calculate the crc for it. */ 1044 ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]); 1045 blob->active.pages[i] = page_num; 1046 spdk_bit_array_set(bs->used_md_pages, page_num); 1047 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Claiming page %u for blob %lu\n", page_num, blob->id); 1048 page_num++; 1049 } 1050 ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]); 1051 /* Start writing the metadata from last page to first */ 1052 ctx->idx = blob->active.num_pages - 1; 1053 _spdk_blob_persist_write_page_chain(seq, ctx, 0); 1054 } 1055 1056 static void 1057 _spdk_blob_request_submit_rw(struct spdk_blob *blob, struct spdk_io_channel *_channel, 1058 void *payload, uint64_t offset, uint64_t length, 1059 spdk_blob_op_complete cb_fn, void *cb_arg, bool read) 1060 { 1061 spdk_bs_batch_t *batch; 1062 struct spdk_bs_cpl cpl; 1063 uint64_t lba; 1064 uint32_t lba_count; 1065 uint8_t *buf; 1066 uint64_t page; 1067 1068 assert(blob != NULL); 1069 1070 if (offset + length > blob->active.num_clusters * blob->bs->pages_per_cluster) { 1071 cb_fn(cb_arg, -EINVAL); 1072 return; 1073 } 1074 1075 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 1076 cpl.u.blob_basic.cb_fn = cb_fn; 1077 cpl.u.blob_basic.cb_arg = cb_arg; 1078 1079 batch = spdk_bs_batch_open(_channel, &cpl); 1080 if (!batch) { 1081 cb_fn(cb_arg, -ENOMEM); 1082 return; 1083 } 1084 1085 length = _spdk_bs_page_to_lba(blob->bs, length); 1086 page = offset; 1087 buf = payload; 1088 while (length > 0) { 1089 lba = _spdk_bs_blob_page_to_lba(blob, page); 1090 lba_count = spdk_min(length, 1091 _spdk_bs_page_to_lba(blob->bs, 1092 _spdk_bs_num_pages_to_cluster_boundary(blob, page))); 1093 1094 if (read) { 1095 spdk_bs_batch_read(batch, buf, lba, lba_count); 1096 } else { 1097 spdk_bs_batch_write(batch, buf, lba, lba_count); 1098 } 1099 1100 length -= lba_count; 1101 buf += _spdk_bs_lba_to_byte(blob->bs, lba_count); 1102 page += _spdk_bs_lba_to_page(blob->bs, lba_count); 1103 } 1104 1105 spdk_bs_batch_close(batch); 1106 } 1107 1108 struct rw_iov_ctx { 1109 struct spdk_blob *blob; 1110 bool read; 1111 int iovcnt; 1112 struct iovec *orig_iov; 1113 uint64_t page_offset; 1114 uint64_t pages_remaining; 1115 uint64_t pages_done; 1116 struct iovec iov[0]; 1117 }; 1118 1119 static void 1120 _spdk_rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1121 { 1122 assert(cb_arg == NULL); 1123 spdk_bs_sequence_finish(seq, bserrno); 1124 } 1125 1126 static void 1127 _spdk_rw_iov_split_next(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1128 { 1129 struct rw_iov_ctx *ctx = cb_arg; 1130 struct iovec *iov, *orig_iov; 1131 int iovcnt; 1132 size_t orig_iovoff; 1133 uint64_t lba; 1134 uint64_t page_count, pages_to_boundary; 1135 uint32_t lba_count; 1136 uint64_t byte_count; 1137 1138 if (bserrno != 0 || ctx->pages_remaining == 0) { 1139 free(ctx); 1140 spdk_bs_sequence_finish(seq, bserrno); 1141 return; 1142 } 1143 1144 pages_to_boundary = _spdk_bs_num_pages_to_cluster_boundary(ctx->blob, ctx->page_offset); 1145 page_count = spdk_min(ctx->pages_remaining, pages_to_boundary); 1146 lba = _spdk_bs_blob_page_to_lba(ctx->blob, ctx->page_offset); 1147 lba_count = _spdk_bs_page_to_lba(ctx->blob->bs, page_count); 1148 1149 /* 1150 * Get index and offset into the original iov array for our current position in the I/O sequence. 1151 * byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will 1152 * point to the current position in the I/O sequence. 1153 */ 1154 byte_count = ctx->pages_done * sizeof(struct spdk_blob_md_page); 1155 orig_iov = &ctx->orig_iov[0]; 1156 orig_iovoff = 0; 1157 while (byte_count > 0) { 1158 if (byte_count >= orig_iov->iov_len) { 1159 byte_count -= orig_iov->iov_len; 1160 orig_iov++; 1161 } else { 1162 orig_iovoff = byte_count; 1163 byte_count = 0; 1164 } 1165 } 1166 1167 /* 1168 * Build an iov array for the next I/O in the sequence. byte_count will keep track of how many 1169 * bytes of this next I/O remain to be accounted for in the new iov array. 1170 */ 1171 byte_count = page_count * sizeof(struct spdk_blob_md_page); 1172 iov = &ctx->iov[0]; 1173 iovcnt = 0; 1174 while (byte_count > 0) { 1175 iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff); 1176 iov->iov_base = orig_iov->iov_base + orig_iovoff; 1177 byte_count -= iov->iov_len; 1178 orig_iovoff = 0; 1179 orig_iov++; 1180 iov++; 1181 iovcnt++; 1182 } 1183 1184 ctx->page_offset += page_count; 1185 ctx->pages_done += page_count; 1186 ctx->pages_remaining -= page_count; 1187 iov = &ctx->iov[0]; 1188 1189 if (ctx->read) { 1190 spdk_bs_sequence_readv(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_split_next, ctx); 1191 } else { 1192 spdk_bs_sequence_writev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_split_next, ctx); 1193 } 1194 } 1195 1196 static void 1197 _spdk_blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel, 1198 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 1199 spdk_blob_op_complete cb_fn, void *cb_arg, bool read) 1200 { 1201 spdk_bs_sequence_t *seq; 1202 struct spdk_bs_cpl cpl; 1203 1204 assert(blob != NULL); 1205 1206 if (length == 0) { 1207 cb_fn(cb_arg, 0); 1208 return; 1209 } 1210 1211 if (offset + length > blob->active.num_clusters * blob->bs->pages_per_cluster) { 1212 cb_fn(cb_arg, -EINVAL); 1213 return; 1214 } 1215 1216 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 1217 cpl.u.blob_basic.cb_fn = cb_fn; 1218 cpl.u.blob_basic.cb_arg = cb_arg; 1219 1220 /* 1221 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having 1222 * to split a request that spans a cluster boundary. For I/O that do not span a cluster boundary, 1223 * there will be no noticeable difference compared to using a batch. For I/O that do span a cluster 1224 * boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need 1225 * to allocate a separate iov array and split the I/O such that none of the resulting 1226 * smaller I/O cross a cluster boundary. These smaller I/O will be issued in sequence (not in parallel) 1227 * but since this case happens very infrequently, any performance impact will be negligible. 1228 * 1229 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs 1230 * for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them 1231 * in a batch. That would also require creating an intermediate spdk_bs_cpl that would get called 1232 * when the batch was completed, to allow for freeing the memory for the iov arrays. 1233 */ 1234 seq = spdk_bs_sequence_start(_channel, &cpl); 1235 if (!seq) { 1236 cb_fn(cb_arg, -ENOMEM); 1237 return; 1238 } 1239 1240 if (spdk_likely(length <= _spdk_bs_num_pages_to_cluster_boundary(blob, offset))) { 1241 uint64_t lba = _spdk_bs_blob_page_to_lba(blob, offset); 1242 uint32_t lba_count = _spdk_bs_page_to_lba(blob->bs, length); 1243 1244 if (read) { 1245 spdk_bs_sequence_readv(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL); 1246 } else { 1247 spdk_bs_sequence_writev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL); 1248 } 1249 } else { 1250 struct rw_iov_ctx *ctx; 1251 1252 ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec)); 1253 if (ctx == NULL) { 1254 spdk_bs_sequence_finish(seq, -ENOMEM); 1255 return; 1256 } 1257 1258 ctx->blob = blob; 1259 ctx->read = read; 1260 ctx->orig_iov = iov; 1261 ctx->iovcnt = iovcnt; 1262 ctx->page_offset = offset; 1263 ctx->pages_remaining = length; 1264 ctx->pages_done = 0; 1265 1266 _spdk_rw_iov_split_next(seq, ctx, 0); 1267 } 1268 } 1269 1270 static struct spdk_blob * 1271 _spdk_blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid) 1272 { 1273 struct spdk_blob *blob; 1274 1275 TAILQ_FOREACH(blob, &bs->blobs, link) { 1276 if (blob->id == blobid) { 1277 return blob; 1278 } 1279 } 1280 1281 return NULL; 1282 } 1283 1284 static int 1285 _spdk_bs_channel_create(struct spdk_blob_store *bs, struct spdk_bs_channel *channel, 1286 uint32_t max_ops) 1287 { 1288 struct spdk_bs_dev *dev; 1289 uint32_t i; 1290 1291 dev = bs->dev; 1292 1293 channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set)); 1294 if (!channel->req_mem) { 1295 return -1; 1296 } 1297 1298 TAILQ_INIT(&channel->reqs); 1299 1300 for (i = 0; i < max_ops; i++) { 1301 TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link); 1302 } 1303 1304 channel->bs = bs; 1305 channel->dev = dev; 1306 channel->dev_channel = dev->create_channel(dev); 1307 1308 if (!channel->dev_channel) { 1309 SPDK_ERRLOG("Failed to create device channel.\n"); 1310 free(channel->req_mem); 1311 return -1; 1312 } 1313 1314 return 0; 1315 } 1316 1317 static int 1318 _spdk_bs_md_channel_create(void *io_device, void *ctx_buf) 1319 { 1320 struct spdk_blob_store *bs; 1321 struct spdk_bs_channel *channel = ctx_buf; 1322 1323 bs = SPDK_CONTAINEROF(io_device, struct spdk_blob_store, md_target); 1324 1325 return _spdk_bs_channel_create(bs, channel, bs->md_target.max_md_ops); 1326 } 1327 1328 static int 1329 _spdk_bs_io_channel_create(void *io_device, void *ctx_buf) 1330 { 1331 struct spdk_blob_store *bs; 1332 struct spdk_bs_channel *channel = ctx_buf; 1333 1334 bs = SPDK_CONTAINEROF(io_device, struct spdk_blob_store, io_target); 1335 1336 return _spdk_bs_channel_create(bs, channel, bs->io_target.max_channel_ops); 1337 } 1338 1339 1340 static void 1341 _spdk_bs_channel_destroy(void *io_device, void *ctx_buf) 1342 { 1343 struct spdk_bs_channel *channel = ctx_buf; 1344 1345 free(channel->req_mem); 1346 channel->dev->destroy_channel(channel->dev, channel->dev_channel); 1347 } 1348 1349 static void 1350 _spdk_bs_dev_destroy(void *io_device) 1351 { 1352 struct spdk_blob_store *bs; 1353 struct spdk_blob *blob, *blob_tmp; 1354 1355 bs = SPDK_CONTAINEROF(io_device, struct spdk_blob_store, md_target); 1356 bs->dev->destroy(bs->dev); 1357 1358 TAILQ_FOREACH_SAFE(blob, &bs->blobs, link, blob_tmp) { 1359 TAILQ_REMOVE(&bs->blobs, blob, link); 1360 _spdk_blob_free(blob); 1361 } 1362 1363 spdk_bit_array_free(&bs->used_md_pages); 1364 spdk_bit_array_free(&bs->used_clusters); 1365 /* 1366 * If this function is called for any reason except a successful unload, 1367 * the unload_cpl type will be NONE and this will be a nop. 1368 */ 1369 spdk_bs_call_cpl(&bs->unload_cpl, bs->unload_err); 1370 1371 free(bs); 1372 } 1373 1374 static void 1375 _spdk_bs_free(struct spdk_blob_store *bs) 1376 { 1377 spdk_bs_unregister_md_thread(bs); 1378 spdk_io_device_unregister(&bs->io_target, NULL); 1379 spdk_io_device_unregister(&bs->md_target, _spdk_bs_dev_destroy); 1380 } 1381 1382 void 1383 spdk_bs_opts_init(struct spdk_bs_opts *opts) 1384 { 1385 opts->cluster_sz = SPDK_BLOB_OPTS_CLUSTER_SZ; 1386 opts->num_md_pages = SPDK_BLOB_OPTS_NUM_MD_PAGES; 1387 opts->max_md_ops = SPDK_BLOB_OPTS_MAX_MD_OPS; 1388 opts->max_channel_ops = SPDK_BLOB_OPTS_MAX_CHANNEL_OPS; 1389 memset(&opts->bstype, 0, sizeof(opts->bstype)); 1390 } 1391 1392 static int 1393 _spdk_bs_opts_verify(struct spdk_bs_opts *opts) 1394 { 1395 if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 || 1396 opts->max_channel_ops == 0) { 1397 SPDK_ERRLOG("Blobstore options cannot be set to 0\n"); 1398 return -1; 1399 } 1400 1401 return 0; 1402 } 1403 1404 static struct spdk_blob_store * 1405 _spdk_bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts) 1406 { 1407 struct spdk_blob_store *bs; 1408 uint64_t dev_size; 1409 int rc; 1410 1411 dev_size = dev->blocklen * dev->blockcnt; 1412 if (dev_size < opts->cluster_sz) { 1413 /* Device size cannot be smaller than cluster size of blobstore */ 1414 SPDK_ERRLOG("Device size %" PRIu64 " is smaller than cluster size %d\n", dev_size, 1415 opts->cluster_sz); 1416 return NULL; 1417 } 1418 if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) { 1419 /* Cluster size cannot be smaller than page size */ 1420 SPDK_ERRLOG("Cluster size %d is smaller than page size %d\n", 1421 opts->cluster_sz, SPDK_BS_PAGE_SIZE); 1422 return NULL; 1423 } 1424 bs = calloc(1, sizeof(struct spdk_blob_store)); 1425 if (!bs) { 1426 return NULL; 1427 } 1428 1429 TAILQ_INIT(&bs->blobs); 1430 bs->dev = dev; 1431 1432 /* 1433 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an 1434 * even multiple of the cluster size. 1435 */ 1436 bs->cluster_sz = opts->cluster_sz; 1437 bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen); 1438 bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE; 1439 bs->num_free_clusters = bs->total_clusters; 1440 bs->used_clusters = spdk_bit_array_create(bs->total_clusters); 1441 if (bs->used_clusters == NULL) { 1442 free(bs); 1443 return NULL; 1444 } 1445 1446 bs->md_target.max_md_ops = opts->max_md_ops; 1447 bs->io_target.max_channel_ops = opts->max_channel_ops; 1448 bs->super_blob = SPDK_BLOBID_INVALID; 1449 memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype)); 1450 1451 /* The metadata is assumed to be at least 1 page */ 1452 bs->used_md_pages = spdk_bit_array_create(1); 1453 1454 spdk_io_device_register(&bs->md_target, _spdk_bs_md_channel_create, _spdk_bs_channel_destroy, 1455 sizeof(struct spdk_bs_channel)); 1456 rc = spdk_bs_register_md_thread(bs); 1457 if (rc == -1) { 1458 spdk_io_device_unregister(&bs->md_target, NULL); 1459 spdk_bit_array_free(&bs->used_md_pages); 1460 spdk_bit_array_free(&bs->used_clusters); 1461 free(bs); 1462 return NULL; 1463 } 1464 1465 spdk_io_device_register(&bs->io_target, _spdk_bs_io_channel_create, _spdk_bs_channel_destroy, 1466 sizeof(struct spdk_bs_channel)); 1467 1468 return bs; 1469 } 1470 1471 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */ 1472 1473 struct spdk_bs_load_ctx { 1474 struct spdk_blob_store *bs; 1475 struct spdk_bs_super_block *super; 1476 1477 struct spdk_bs_md_mask *mask; 1478 bool in_page_chain; 1479 uint32_t page_index; 1480 uint32_t cur_page; 1481 struct spdk_blob_md_page *page; 1482 }; 1483 1484 static void 1485 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask) 1486 { 1487 uint32_t i = 0; 1488 1489 while (true) { 1490 i = spdk_bit_array_find_first_set(array, i); 1491 if (i >= mask->length) { 1492 break; 1493 } 1494 mask->mask[i / 8] |= 1U << (i % 8); 1495 i++; 1496 } 1497 } 1498 1499 static void 1500 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs, 1501 struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 1502 { 1503 /* Update the values in the super block */ 1504 super->super_blob = bs->super_blob; 1505 memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype)); 1506 super->crc = _spdk_blob_md_page_calc_crc(super); 1507 spdk_bs_sequence_write(seq, super, _spdk_bs_page_to_lba(bs, 0), 1508 _spdk_bs_byte_to_lba(bs, sizeof(*super)), 1509 cb_fn, cb_arg); 1510 } 1511 1512 static void 1513 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 1514 { 1515 struct spdk_bs_load_ctx *ctx = arg; 1516 uint64_t mask_size, lba, lba_count; 1517 1518 /* Write out the used clusters mask */ 1519 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 1520 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 1521 if (!ctx->mask) { 1522 spdk_dma_free(ctx->super); 1523 free(ctx); 1524 spdk_bs_sequence_finish(seq, -ENOMEM); 1525 return; 1526 } 1527 1528 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS; 1529 ctx->mask->length = ctx->bs->total_clusters; 1530 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters)); 1531 1532 _spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask); 1533 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 1534 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 1535 spdk_bs_sequence_write(seq, ctx->mask, lba, lba_count, cb_fn, arg); 1536 } 1537 1538 static void 1539 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn) 1540 { 1541 struct spdk_bs_load_ctx *ctx = arg; 1542 uint64_t mask_size, lba, lba_count; 1543 1544 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 1545 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 1546 if (!ctx->mask) { 1547 spdk_dma_free(ctx->super); 1548 free(ctx); 1549 spdk_bs_sequence_finish(seq, -ENOMEM); 1550 return; 1551 } 1552 1553 ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES; 1554 ctx->mask->length = ctx->super->md_len; 1555 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages)); 1556 1557 _spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask); 1558 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 1559 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 1560 spdk_bs_sequence_write(seq, ctx->mask, lba, lba_count, cb_fn, arg); 1561 } 1562 1563 static void 1564 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1565 { 1566 struct spdk_bs_load_ctx *ctx = cb_arg; 1567 uint32_t i, j; 1568 int rc; 1569 1570 /* The type must be correct */ 1571 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS); 1572 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 1573 assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof( 1574 struct spdk_blob_md_page) * 8)); 1575 /* The length of the mask must be exactly equal to the total number of clusters */ 1576 assert(ctx->mask->length == ctx->bs->total_clusters); 1577 1578 rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); 1579 if (rc < 0) { 1580 spdk_dma_free(ctx->super); 1581 spdk_dma_free(ctx->mask); 1582 _spdk_bs_free(ctx->bs); 1583 free(ctx); 1584 spdk_bs_sequence_finish(seq, -ENOMEM); 1585 return; 1586 } 1587 1588 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 1589 for (i = 0; i < ctx->mask->length / 8; i++) { 1590 uint8_t segment = ctx->mask->mask[i]; 1591 for (j = 0; segment && (j < 8); j++) { 1592 if (segment & 1U) { 1593 spdk_bit_array_set(ctx->bs->used_clusters, (i * 8) + j); 1594 assert(ctx->bs->num_free_clusters > 0); 1595 ctx->bs->num_free_clusters--; 1596 } 1597 segment >>= 1U; 1598 } 1599 } 1600 1601 spdk_dma_free(ctx->super); 1602 spdk_dma_free(ctx->mask); 1603 free(ctx); 1604 1605 spdk_bs_sequence_finish(seq, bserrno); 1606 } 1607 1608 static void 1609 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1610 { 1611 struct spdk_bs_load_ctx *ctx = cb_arg; 1612 uint64_t lba, lba_count, mask_size; 1613 uint32_t i, j; 1614 int rc; 1615 1616 /* The type must be correct */ 1617 assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES); 1618 /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */ 1619 assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE * 1620 8)); 1621 /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */ 1622 assert(ctx->mask->length == ctx->super->md_len); 1623 1624 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length); 1625 if (rc < 0) { 1626 spdk_dma_free(ctx->super); 1627 spdk_dma_free(ctx->mask); 1628 _spdk_bs_free(ctx->bs); 1629 free(ctx); 1630 spdk_bs_sequence_finish(seq, -ENOMEM); 1631 return; 1632 } 1633 1634 for (i = 0; i < ctx->mask->length / 8; i++) { 1635 uint8_t segment = ctx->mask->mask[i]; 1636 for (j = 0; segment && (j < 8); j++) { 1637 if (segment & 1U) { 1638 spdk_bit_array_set(ctx->bs->used_md_pages, (i * 8) + j); 1639 } 1640 segment >>= 1U; 1641 } 1642 } 1643 spdk_dma_free(ctx->mask); 1644 1645 /* Read the used clusters mask */ 1646 mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE; 1647 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 1648 if (!ctx->mask) { 1649 spdk_dma_free(ctx->super); 1650 _spdk_bs_free(ctx->bs); 1651 free(ctx); 1652 spdk_bs_sequence_finish(seq, -ENOMEM); 1653 return; 1654 } 1655 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start); 1656 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len); 1657 spdk_bs_sequence_read(seq, ctx->mask, lba, lba_count, 1658 _spdk_bs_load_used_clusters_cpl, ctx); 1659 } 1660 1661 static void 1662 _spdk_bs_load_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1663 { 1664 struct spdk_bs_load_ctx *ctx = cb_arg; 1665 uint64_t lba, lba_count, mask_size; 1666 1667 /* Read the used pages mask */ 1668 mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE; 1669 ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL); 1670 if (!ctx->mask) { 1671 spdk_dma_free(ctx->super); 1672 _spdk_bs_free(ctx->bs); 1673 free(ctx); 1674 spdk_bs_sequence_finish(seq, -ENOMEM); 1675 return; 1676 } 1677 1678 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start); 1679 lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len); 1680 spdk_bs_sequence_read(seq, ctx->mask, lba, lba_count, 1681 _spdk_bs_load_used_pages_cpl, ctx); 1682 } 1683 1684 static int 1685 _spdk_bs_load_replay_md_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob_store *bs) 1686 { 1687 struct spdk_blob_md_descriptor *desc; 1688 size_t cur_desc = 0; 1689 1690 desc = (struct spdk_blob_md_descriptor *)page->descriptors; 1691 while (cur_desc < sizeof(page->descriptors)) { 1692 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) { 1693 if (desc->length == 0) { 1694 /* If padding and length are 0, this terminates the page */ 1695 break; 1696 } 1697 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) { 1698 struct spdk_blob_md_descriptor_extent *desc_extent; 1699 unsigned int i, j; 1700 unsigned int cluster_count = 0; 1701 1702 desc_extent = (struct spdk_blob_md_descriptor_extent *)desc; 1703 1704 for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) { 1705 for (j = 0; j < desc_extent->extents[i].length; j++) { 1706 spdk_bit_array_set(bs->used_clusters, desc_extent->extents[i].cluster_idx + j); 1707 if (bs->num_free_clusters == 0) { 1708 return -1; 1709 } 1710 bs->num_free_clusters--; 1711 cluster_count++; 1712 } 1713 } 1714 if (cluster_count == 0) { 1715 return -1; 1716 } 1717 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) { 1718 /* Skip this item */ 1719 } else { 1720 /* Error */ 1721 return -1; 1722 } 1723 /* Advance to the next descriptor */ 1724 cur_desc += sizeof(*desc) + desc->length; 1725 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) { 1726 break; 1727 } 1728 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc); 1729 } 1730 return 0; 1731 } 1732 1733 static bool _spdk_bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx) 1734 { 1735 uint32_t crc; 1736 1737 crc = _spdk_blob_md_page_calc_crc(ctx->page); 1738 if (crc != ctx->page->crc) { 1739 return false; 1740 } 1741 1742 if (_spdk_bs_page_to_blobid(ctx->cur_page) != ctx->page->id) { 1743 return false; 1744 } 1745 return true; 1746 } 1747 1748 static void 1749 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg); 1750 1751 static void 1752 _spdk_bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1753 { 1754 struct spdk_bs_load_ctx *ctx = cb_arg; 1755 1756 spdk_dma_free(ctx->mask); 1757 spdk_dma_free(ctx->super); 1758 spdk_bs_sequence_finish(seq, bserrno); 1759 free(ctx); 1760 } 1761 1762 static void 1763 _spdk_bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1764 { 1765 struct spdk_bs_load_ctx *ctx = cb_arg; 1766 1767 spdk_dma_free(ctx->mask); 1768 1769 _spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_load_write_used_clusters_cpl); 1770 } 1771 1772 static void 1773 _spdk_bs_load_write_used_md(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1774 { 1775 _spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_load_write_used_pages_cpl); 1776 } 1777 1778 static void 1779 _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1780 { 1781 struct spdk_bs_load_ctx *ctx = cb_arg; 1782 uint32_t page_num; 1783 1784 if (bserrno != 0) { 1785 spdk_dma_free(ctx->super); 1786 _spdk_bs_free(ctx->bs); 1787 free(ctx); 1788 spdk_bs_sequence_finish(seq, bserrno); 1789 return; 1790 } 1791 1792 page_num = ctx->cur_page; 1793 if (_spdk_bs_load_cur_md_page_valid(ctx) == true) { 1794 if (ctx->page->sequence_num == 0 || ctx->in_page_chain == true) { 1795 spdk_bit_array_set(ctx->bs->used_md_pages, page_num); 1796 if (_spdk_bs_load_replay_md_parse_page(ctx->page, ctx->bs)) { 1797 spdk_dma_free(ctx->super); 1798 _spdk_bs_free(ctx->bs); 1799 free(ctx); 1800 spdk_bs_sequence_finish(seq, -EILSEQ); 1801 return; 1802 } 1803 if (ctx->page->next != SPDK_INVALID_MD_PAGE) { 1804 ctx->in_page_chain = true; 1805 ctx->cur_page = ctx->page->next; 1806 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 1807 return; 1808 } 1809 } 1810 } 1811 1812 ctx->in_page_chain = false; 1813 1814 do { 1815 ctx->page_index++; 1816 } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true); 1817 1818 if (ctx->page_index < ctx->super->md_len) { 1819 ctx->cur_page = ctx->page_index; 1820 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 1821 } else { 1822 spdk_dma_free(ctx->page); 1823 _spdk_bs_load_write_used_md(seq, ctx, bserrno); 1824 } 1825 } 1826 1827 static void 1828 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg) 1829 { 1830 struct spdk_bs_load_ctx *ctx = cb_arg; 1831 uint64_t lba; 1832 1833 assert(ctx->cur_page < ctx->super->md_len); 1834 lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page); 1835 spdk_bs_sequence_read(seq, ctx->page, lba, 1836 _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE), 1837 _spdk_bs_load_replay_md_cpl, ctx); 1838 } 1839 1840 static void 1841 _spdk_bs_load_replay_md(spdk_bs_sequence_t *seq, void *cb_arg) 1842 { 1843 struct spdk_bs_load_ctx *ctx = cb_arg; 1844 1845 ctx->page_index = 0; 1846 ctx->cur_page = 0; 1847 ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE, 1848 SPDK_BS_PAGE_SIZE, 1849 NULL); 1850 if (!ctx->page) { 1851 spdk_dma_free(ctx->super); 1852 _spdk_bs_free(ctx->bs); 1853 free(ctx); 1854 spdk_bs_sequence_finish(seq, -ENOMEM); 1855 return; 1856 } 1857 _spdk_bs_load_replay_cur_md_page(seq, cb_arg); 1858 } 1859 1860 static void 1861 _spdk_bs_recover(spdk_bs_sequence_t *seq, void *cb_arg) 1862 { 1863 struct spdk_bs_load_ctx *ctx = cb_arg; 1864 int rc; 1865 1866 rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len); 1867 if (rc < 0) { 1868 spdk_dma_free(ctx->super); 1869 _spdk_bs_free(ctx->bs); 1870 free(ctx); 1871 spdk_bs_sequence_finish(seq, -ENOMEM); 1872 return; 1873 } 1874 1875 rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters); 1876 if (rc < 0) { 1877 spdk_dma_free(ctx->super); 1878 _spdk_bs_free(ctx->bs); 1879 free(ctx); 1880 spdk_bs_sequence_finish(seq, -ENOMEM); 1881 return; 1882 } 1883 1884 ctx->bs->num_free_clusters = ctx->bs->total_clusters; 1885 _spdk_bs_load_replay_md(seq, cb_arg); 1886 } 1887 1888 static void 1889 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 1890 { 1891 struct spdk_bs_load_ctx *ctx = cb_arg; 1892 uint32_t crc; 1893 static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH]; 1894 1895 if (ctx->super->version > SPDK_BS_VERSION || 1896 ctx->super->version < SPDK_BS_INITIAL_VERSION) { 1897 spdk_dma_free(ctx->super); 1898 _spdk_bs_free(ctx->bs); 1899 free(ctx); 1900 spdk_bs_sequence_finish(seq, -EILSEQ); 1901 return; 1902 } 1903 1904 if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 1905 sizeof(ctx->super->signature)) != 0) { 1906 spdk_dma_free(ctx->super); 1907 _spdk_bs_free(ctx->bs); 1908 free(ctx); 1909 spdk_bs_sequence_finish(seq, -EILSEQ); 1910 return; 1911 } 1912 1913 crc = _spdk_blob_md_page_calc_crc(ctx->super); 1914 if (crc != ctx->super->crc) { 1915 spdk_dma_free(ctx->super); 1916 _spdk_bs_free(ctx->bs); 1917 free(ctx); 1918 spdk_bs_sequence_finish(seq, -EILSEQ); 1919 return; 1920 } 1921 1922 if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 1923 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Bstype matched - loading blobstore\n"); 1924 } else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) { 1925 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n"); 1926 } else { 1927 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Unexpected bstype\n"); 1928 SPDK_TRACEDUMP(SPDK_TRACE_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 1929 SPDK_TRACEDUMP(SPDK_TRACE_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH); 1930 spdk_dma_free(ctx->super); 1931 _spdk_bs_free(ctx->bs); 1932 free(ctx); 1933 spdk_bs_sequence_finish(seq, -ENXIO); 1934 return; 1935 } 1936 1937 /* Parse the super block */ 1938 ctx->bs->cluster_sz = ctx->super->cluster_size; 1939 ctx->bs->total_clusters = ctx->bs->dev->blockcnt / (ctx->bs->cluster_sz / ctx->bs->dev->blocklen); 1940 ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE; 1941 ctx->bs->md_start = ctx->super->md_start; 1942 ctx->bs->md_len = ctx->super->md_len; 1943 ctx->bs->total_data_clusters = ctx->bs->total_clusters - divide_round_up( 1944 ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster); 1945 ctx->bs->super_blob = ctx->super->super_blob; 1946 memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype)); 1947 1948 if (ctx->super->clean == 1) { 1949 ctx->super->clean = 0; 1950 _spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_load_write_super_cpl, ctx); 1951 } else { 1952 _spdk_bs_recover(seq, ctx); 1953 } 1954 } 1955 1956 void 1957 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 1958 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 1959 { 1960 struct spdk_blob_store *bs; 1961 struct spdk_bs_cpl cpl; 1962 spdk_bs_sequence_t *seq; 1963 struct spdk_bs_load_ctx *ctx; 1964 struct spdk_bs_opts opts = {}; 1965 1966 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Loading blobstore from dev %p\n", dev); 1967 1968 if (o) { 1969 opts = *o; 1970 } else { 1971 spdk_bs_opts_init(&opts); 1972 } 1973 1974 if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) { 1975 cb_fn(cb_arg, NULL, -EINVAL); 1976 return; 1977 } 1978 1979 bs = _spdk_bs_alloc(dev, &opts); 1980 if (!bs) { 1981 cb_fn(cb_arg, NULL, -ENOMEM); 1982 return; 1983 } 1984 1985 ctx = calloc(1, sizeof(*ctx)); 1986 if (!ctx) { 1987 _spdk_bs_free(bs); 1988 cb_fn(cb_arg, NULL, -ENOMEM); 1989 return; 1990 } 1991 1992 ctx->bs = bs; 1993 1994 /* Allocate memory for the super block */ 1995 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 1996 if (!ctx->super) { 1997 free(ctx); 1998 _spdk_bs_free(bs); 1999 return; 2000 } 2001 2002 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 2003 cpl.u.bs_handle.cb_fn = cb_fn; 2004 cpl.u.bs_handle.cb_arg = cb_arg; 2005 cpl.u.bs_handle.bs = bs; 2006 2007 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2008 if (!seq) { 2009 spdk_dma_free(ctx->super); 2010 free(ctx); 2011 _spdk_bs_free(bs); 2012 cb_fn(cb_arg, NULL, -ENOMEM); 2013 return; 2014 } 2015 2016 /* Read the super block */ 2017 spdk_bs_sequence_read(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 2018 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 2019 _spdk_bs_load_super_cpl, ctx); 2020 } 2021 2022 /* END spdk_bs_load */ 2023 2024 /* START spdk_bs_init */ 2025 2026 struct spdk_bs_init_ctx { 2027 struct spdk_blob_store *bs; 2028 struct spdk_bs_super_block *super; 2029 }; 2030 2031 static void 2032 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2033 { 2034 struct spdk_bs_init_ctx *ctx = cb_arg; 2035 2036 spdk_dma_free(ctx->super); 2037 free(ctx); 2038 2039 spdk_bs_sequence_finish(seq, bserrno); 2040 } 2041 2042 static void 2043 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2044 { 2045 struct spdk_bs_init_ctx *ctx = cb_arg; 2046 2047 /* Write super block */ 2048 spdk_bs_sequence_write(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0), 2049 _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)), 2050 _spdk_bs_init_persist_super_cpl, ctx); 2051 } 2052 2053 void 2054 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o, 2055 spdk_bs_op_with_handle_complete cb_fn, void *cb_arg) 2056 { 2057 struct spdk_bs_init_ctx *ctx; 2058 struct spdk_blob_store *bs; 2059 struct spdk_bs_cpl cpl; 2060 spdk_bs_sequence_t *seq; 2061 uint64_t num_md_pages; 2062 uint64_t num_md_clusters; 2063 uint32_t i; 2064 struct spdk_bs_opts opts = {}; 2065 int rc; 2066 2067 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Initializing blobstore on dev %p\n", dev); 2068 2069 if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) { 2070 SPDK_ERRLOG("unsupported dev block length of %d\n", 2071 dev->blocklen); 2072 dev->destroy(dev); 2073 cb_fn(cb_arg, NULL, -EINVAL); 2074 return; 2075 } 2076 2077 if (o) { 2078 opts = *o; 2079 } else { 2080 spdk_bs_opts_init(&opts); 2081 } 2082 2083 if (_spdk_bs_opts_verify(&opts) != 0) { 2084 dev->destroy(dev); 2085 cb_fn(cb_arg, NULL, -EINVAL); 2086 return; 2087 } 2088 2089 bs = _spdk_bs_alloc(dev, &opts); 2090 if (!bs) { 2091 dev->destroy(dev); 2092 cb_fn(cb_arg, NULL, -ENOMEM); 2093 return; 2094 } 2095 2096 if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) { 2097 /* By default, allocate 1 page per cluster. 2098 * Technically, this over-allocates metadata 2099 * because more metadata will reduce the number 2100 * of usable clusters. This can be addressed with 2101 * more complex math in the future. 2102 */ 2103 bs->md_len = bs->total_clusters; 2104 } else { 2105 bs->md_len = opts.num_md_pages; 2106 } 2107 2108 rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len); 2109 if (rc < 0) { 2110 _spdk_bs_free(bs); 2111 cb_fn(cb_arg, NULL, -ENOMEM); 2112 return; 2113 } 2114 2115 ctx = calloc(1, sizeof(*ctx)); 2116 if (!ctx) { 2117 _spdk_bs_free(bs); 2118 cb_fn(cb_arg, NULL, -ENOMEM); 2119 return; 2120 } 2121 2122 ctx->bs = bs; 2123 2124 /* Allocate memory for the super block */ 2125 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 2126 if (!ctx->super) { 2127 free(ctx); 2128 _spdk_bs_free(bs); 2129 return; 2130 } 2131 memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG, 2132 sizeof(ctx->super->signature)); 2133 ctx->super->version = SPDK_BS_VERSION; 2134 ctx->super->length = sizeof(*ctx->super); 2135 ctx->super->super_blob = bs->super_blob; 2136 ctx->super->clean = 0; 2137 ctx->super->cluster_size = bs->cluster_sz; 2138 memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype)); 2139 2140 /* Calculate how many pages the metadata consumes at the front 2141 * of the disk. 2142 */ 2143 2144 /* The super block uses 1 page */ 2145 num_md_pages = 1; 2146 2147 /* The used_md_pages mask requires 1 bit per metadata page, rounded 2148 * up to the nearest page, plus a header. 2149 */ 2150 ctx->super->used_page_mask_start = num_md_pages; 2151 ctx->super->used_page_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) + 2152 divide_round_up(bs->md_len, 8), 2153 SPDK_BS_PAGE_SIZE); 2154 num_md_pages += ctx->super->used_page_mask_len; 2155 2156 /* The used_clusters mask requires 1 bit per cluster, rounded 2157 * up to the nearest page, plus a header. 2158 */ 2159 ctx->super->used_cluster_mask_start = num_md_pages; 2160 ctx->super->used_cluster_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) + 2161 divide_round_up(bs->total_clusters, 8), 2162 SPDK_BS_PAGE_SIZE); 2163 num_md_pages += ctx->super->used_cluster_mask_len; 2164 2165 /* The metadata region size was chosen above */ 2166 ctx->super->md_start = bs->md_start = num_md_pages; 2167 ctx->super->md_len = bs->md_len; 2168 num_md_pages += bs->md_len; 2169 2170 ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super); 2171 2172 num_md_clusters = divide_round_up(num_md_pages, bs->pages_per_cluster); 2173 if (num_md_clusters > bs->total_clusters) { 2174 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, " 2175 "please decrease number of pages reserved for metadata " 2176 "or increase cluster size.\n"); 2177 spdk_dma_free(ctx->super); 2178 free(ctx); 2179 _spdk_bs_free(bs); 2180 cb_fn(cb_arg, NULL, -ENOMEM); 2181 return; 2182 } 2183 /* Claim all of the clusters used by the metadata */ 2184 for (i = 0; i < num_md_clusters; i++) { 2185 _spdk_bs_claim_cluster(bs, i); 2186 } 2187 2188 bs->total_data_clusters = bs->num_free_clusters; 2189 2190 cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE; 2191 cpl.u.bs_handle.cb_fn = cb_fn; 2192 cpl.u.bs_handle.cb_arg = cb_arg; 2193 cpl.u.bs_handle.bs = bs; 2194 2195 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2196 if (!seq) { 2197 spdk_dma_free(ctx->super); 2198 free(ctx); 2199 _spdk_bs_free(bs); 2200 cb_fn(cb_arg, NULL, -ENOMEM); 2201 return; 2202 } 2203 2204 /* Zero the entire device */ 2205 spdk_bs_sequence_write_zeroes(seq, 0, bs->dev->blockcnt, _spdk_bs_init_trim_cpl, ctx); 2206 } 2207 2208 /* END spdk_bs_init */ 2209 2210 /* START spdk_bs_destroy */ 2211 2212 static void 2213 _spdk_bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2214 { 2215 struct spdk_bs_init_ctx *ctx = cb_arg; 2216 struct spdk_blob_store *bs = ctx->bs; 2217 2218 /* 2219 * We need to defer calling spdk_bs_call_cpl() until after 2220 * dev destruction, so tuck these away for later use. 2221 */ 2222 bs->unload_err = bserrno; 2223 memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 2224 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 2225 2226 spdk_bs_sequence_finish(seq, bserrno); 2227 2228 _spdk_bs_free(bs); 2229 spdk_dma_free(ctx->super); 2230 free(ctx); 2231 } 2232 2233 void 2234 spdk_bs_destroy(struct spdk_blob_store *bs, bool unmap_device, spdk_bs_op_complete cb_fn, 2235 void *cb_arg) 2236 { 2237 struct spdk_bs_cpl cpl; 2238 spdk_bs_sequence_t *seq; 2239 struct spdk_bs_init_ctx *ctx; 2240 2241 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Destroying blobstore\n"); 2242 2243 if (!TAILQ_EMPTY(&bs->blobs)) { 2244 SPDK_ERRLOG("Blobstore still has open blobs\n"); 2245 cb_fn(cb_arg, -EBUSY); 2246 return; 2247 } 2248 2249 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 2250 cpl.u.bs_basic.cb_fn = cb_fn; 2251 cpl.u.bs_basic.cb_arg = cb_arg; 2252 2253 ctx = calloc(1, sizeof(*ctx)); 2254 if (!ctx) { 2255 cb_fn(cb_arg, -ENOMEM); 2256 return; 2257 } 2258 2259 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 2260 if (!ctx->super) { 2261 free(ctx); 2262 cb_fn(cb_arg, -ENOMEM); 2263 return; 2264 } 2265 2266 ctx->bs = bs; 2267 2268 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2269 if (!seq) { 2270 spdk_dma_free(ctx->super); 2271 free(ctx); 2272 cb_fn(cb_arg, -ENOMEM); 2273 return; 2274 } 2275 2276 if (unmap_device) { 2277 /* TRIM the entire device */ 2278 spdk_bs_sequence_unmap(seq, 0, bs->dev->blockcnt, _spdk_bs_destroy_trim_cpl, ctx); 2279 } else { 2280 /* Write zeroes to the super block */ 2281 spdk_bs_sequence_write(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), _spdk_bs_byte_to_lba(bs, 2282 sizeof(*ctx->super)), _spdk_bs_destroy_trim_cpl, ctx); 2283 } 2284 } 2285 2286 /* END spdk_bs_destroy */ 2287 2288 /* START spdk_bs_unload */ 2289 2290 static void 2291 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2292 { 2293 struct spdk_bs_load_ctx *ctx = cb_arg; 2294 2295 spdk_dma_free(ctx->super); 2296 2297 /* 2298 * We need to defer calling spdk_bs_call_cpl() until after 2299 * dev destuction, so tuck these away for later use. 2300 */ 2301 ctx->bs->unload_err = bserrno; 2302 memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl)); 2303 seq->cpl.type = SPDK_BS_CPL_TYPE_NONE; 2304 2305 spdk_bs_sequence_finish(seq, bserrno); 2306 2307 _spdk_bs_free(ctx->bs); 2308 free(ctx); 2309 } 2310 2311 static void 2312 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2313 { 2314 struct spdk_bs_load_ctx *ctx = cb_arg; 2315 2316 spdk_dma_free(ctx->mask); 2317 ctx->super->clean = 1; 2318 2319 _spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx); 2320 } 2321 2322 static void 2323 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2324 { 2325 struct spdk_bs_load_ctx *ctx = cb_arg; 2326 2327 spdk_dma_free(ctx->mask); 2328 2329 _spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_unload_write_used_clusters_cpl); 2330 } 2331 2332 static void 2333 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2334 { 2335 _spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl); 2336 } 2337 2338 void 2339 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg) 2340 { 2341 struct spdk_bs_cpl cpl; 2342 spdk_bs_sequence_t *seq; 2343 struct spdk_bs_load_ctx *ctx; 2344 2345 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Syncing blobstore\n"); 2346 2347 if (!TAILQ_EMPTY(&bs->blobs)) { 2348 SPDK_ERRLOG("Blobstore still has open blobs\n"); 2349 cb_fn(cb_arg, -EBUSY); 2350 return; 2351 } 2352 2353 ctx = calloc(1, sizeof(*ctx)); 2354 if (!ctx) { 2355 cb_fn(cb_arg, -ENOMEM); 2356 return; 2357 } 2358 2359 ctx->bs = bs; 2360 2361 ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL); 2362 if (!ctx->super) { 2363 free(ctx); 2364 cb_fn(cb_arg, -ENOMEM); 2365 return; 2366 } 2367 2368 cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC; 2369 cpl.u.bs_basic.cb_fn = cb_fn; 2370 cpl.u.bs_basic.cb_arg = cb_arg; 2371 2372 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2373 if (!seq) { 2374 spdk_dma_free(ctx->super); 2375 free(ctx); 2376 cb_fn(cb_arg, -ENOMEM); 2377 return; 2378 } 2379 2380 /* Read super block */ 2381 spdk_bs_sequence_read(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), 2382 _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)), 2383 _spdk_bs_unload_read_super_cpl, ctx); 2384 } 2385 2386 /* END spdk_bs_unload */ 2387 2388 void 2389 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid, 2390 spdk_bs_op_complete cb_fn, void *cb_arg) 2391 { 2392 bs->super_blob = blobid; 2393 cb_fn(cb_arg, 0); 2394 } 2395 2396 void 2397 spdk_bs_get_super(struct spdk_blob_store *bs, 2398 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 2399 { 2400 if (bs->super_blob == SPDK_BLOBID_INVALID) { 2401 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT); 2402 } else { 2403 cb_fn(cb_arg, bs->super_blob, 0); 2404 } 2405 } 2406 2407 uint64_t 2408 spdk_bs_get_cluster_size(struct spdk_blob_store *bs) 2409 { 2410 return bs->cluster_sz; 2411 } 2412 2413 uint64_t 2414 spdk_bs_get_page_size(struct spdk_blob_store *bs) 2415 { 2416 return SPDK_BS_PAGE_SIZE; 2417 } 2418 2419 uint64_t 2420 spdk_bs_free_cluster_count(struct spdk_blob_store *bs) 2421 { 2422 return bs->num_free_clusters; 2423 } 2424 2425 uint64_t 2426 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs) 2427 { 2428 return bs->total_data_clusters; 2429 } 2430 2431 int spdk_bs_register_md_thread(struct spdk_blob_store *bs) 2432 { 2433 bs->md_target.md_channel = spdk_get_io_channel(&bs->md_target); 2434 if (!bs->md_target.md_channel) { 2435 SPDK_ERRLOG("Failed to get IO channel.\n"); 2436 return -1; 2437 } 2438 2439 return 0; 2440 } 2441 2442 int spdk_bs_unregister_md_thread(struct spdk_blob_store *bs) 2443 { 2444 spdk_put_io_channel(bs->md_target.md_channel); 2445 2446 return 0; 2447 } 2448 2449 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob) 2450 { 2451 assert(blob != NULL); 2452 2453 return blob->id; 2454 } 2455 2456 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob) 2457 { 2458 assert(blob != NULL); 2459 2460 return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters); 2461 } 2462 2463 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob) 2464 { 2465 assert(blob != NULL); 2466 2467 return blob->active.num_clusters; 2468 } 2469 2470 /* START spdk_bs_md_create_blob */ 2471 2472 static void 2473 _spdk_bs_md_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2474 { 2475 struct spdk_blob *blob = cb_arg; 2476 2477 _spdk_blob_free(blob); 2478 2479 spdk_bs_sequence_finish(seq, bserrno); 2480 } 2481 2482 void spdk_bs_md_create_blob(struct spdk_blob_store *bs, 2483 spdk_blob_op_with_id_complete cb_fn, void *cb_arg) 2484 { 2485 struct spdk_blob *blob; 2486 uint32_t page_idx; 2487 struct spdk_bs_cpl cpl; 2488 spdk_bs_sequence_t *seq; 2489 spdk_blob_id id; 2490 2491 page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0); 2492 if (page_idx >= spdk_bit_array_capacity(bs->used_md_pages)) { 2493 cb_fn(cb_arg, 0, -ENOMEM); 2494 return; 2495 } 2496 spdk_bit_array_set(bs->used_md_pages, page_idx); 2497 2498 id = _spdk_bs_page_to_blobid(page_idx); 2499 2500 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx); 2501 2502 blob = _spdk_blob_alloc(bs, id); 2503 if (!blob) { 2504 cb_fn(cb_arg, 0, -ENOMEM); 2505 return; 2506 } 2507 2508 cpl.type = SPDK_BS_CPL_TYPE_BLOBID; 2509 cpl.u.blobid.cb_fn = cb_fn; 2510 cpl.u.blobid.cb_arg = cb_arg; 2511 cpl.u.blobid.blobid = blob->id; 2512 2513 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2514 if (!seq) { 2515 _spdk_blob_free(blob); 2516 cb_fn(cb_arg, 0, -ENOMEM); 2517 return; 2518 } 2519 2520 _spdk_blob_persist(seq, blob, _spdk_bs_md_create_blob_cpl, blob); 2521 } 2522 2523 /* END spdk_bs_md_create_blob */ 2524 2525 /* START spdk_bs_md_resize_blob */ 2526 int 2527 spdk_bs_md_resize_blob(struct spdk_blob *blob, uint64_t sz) 2528 { 2529 int rc; 2530 2531 assert(blob != NULL); 2532 2533 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz); 2534 2535 if (sz == blob->active.num_clusters) { 2536 return 0; 2537 } 2538 2539 rc = _spdk_resize_blob(blob, sz); 2540 if (rc < 0) { 2541 return rc; 2542 } 2543 2544 return 0; 2545 } 2546 2547 /* END spdk_bs_md_resize_blob */ 2548 2549 2550 /* START spdk_bs_md_delete_blob */ 2551 2552 static void 2553 _spdk_bs_md_delete_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2554 { 2555 struct spdk_blob *blob = cb_arg; 2556 2557 _spdk_blob_free(blob); 2558 2559 spdk_bs_sequence_finish(seq, bserrno); 2560 } 2561 2562 static void 2563 _spdk_bs_md_delete_open_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2564 { 2565 struct spdk_blob *blob = cb_arg; 2566 2567 /* If the blob have crc error, we just return NULL. */ 2568 if (blob == NULL) { 2569 spdk_bs_sequence_finish(seq, bserrno); 2570 return; 2571 } 2572 blob->state = SPDK_BLOB_STATE_DIRTY; 2573 blob->active.num_pages = 0; 2574 _spdk_resize_blob(blob, 0); 2575 2576 _spdk_blob_persist(seq, blob, _spdk_bs_md_delete_blob_cpl, blob); 2577 } 2578 2579 void 2580 spdk_bs_md_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 2581 spdk_blob_op_complete cb_fn, void *cb_arg) 2582 { 2583 struct spdk_blob *blob; 2584 struct spdk_bs_cpl cpl; 2585 spdk_bs_sequence_t *seq; 2586 2587 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Deleting blob %lu\n", blobid); 2588 2589 blob = _spdk_blob_lookup(bs, blobid); 2590 if (blob) { 2591 assert(blob->open_ref > 0); 2592 cb_fn(cb_arg, -EINVAL); 2593 return; 2594 } 2595 2596 blob = _spdk_blob_alloc(bs, blobid); 2597 if (!blob) { 2598 cb_fn(cb_arg, -ENOMEM); 2599 return; 2600 } 2601 2602 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2603 cpl.u.blob_basic.cb_fn = cb_fn; 2604 cpl.u.blob_basic.cb_arg = cb_arg; 2605 2606 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2607 if (!seq) { 2608 _spdk_blob_free(blob); 2609 cb_fn(cb_arg, -ENOMEM); 2610 return; 2611 } 2612 2613 _spdk_blob_load(seq, blob, _spdk_bs_md_delete_open_cpl, blob); 2614 } 2615 2616 /* END spdk_bs_md_delete_blob */ 2617 2618 /* START spdk_bs_md_open_blob */ 2619 2620 static void 2621 _spdk_bs_md_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2622 { 2623 struct spdk_blob *blob = cb_arg; 2624 2625 /* If the blob have crc error, we just return NULL. */ 2626 if (blob == NULL) { 2627 seq->cpl.u.blob_handle.blob = NULL; 2628 spdk_bs_sequence_finish(seq, bserrno); 2629 return; 2630 } 2631 2632 blob->open_ref++; 2633 2634 TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link); 2635 2636 spdk_bs_sequence_finish(seq, bserrno); 2637 } 2638 2639 void spdk_bs_md_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid, 2640 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 2641 { 2642 struct spdk_blob *blob; 2643 struct spdk_bs_cpl cpl; 2644 spdk_bs_sequence_t *seq; 2645 uint32_t page_num; 2646 2647 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Opening blob %lu\n", blobid); 2648 2649 blob = _spdk_blob_lookup(bs, blobid); 2650 if (blob) { 2651 blob->open_ref++; 2652 cb_fn(cb_arg, blob, 0); 2653 return; 2654 } 2655 2656 page_num = _spdk_bs_blobid_to_page(blobid); 2657 if (spdk_bit_array_get(bs->used_md_pages, page_num) == false) { 2658 /* Invalid blobid */ 2659 cb_fn(cb_arg, NULL, -ENOENT); 2660 return; 2661 } 2662 2663 blob = _spdk_blob_alloc(bs, blobid); 2664 if (!blob) { 2665 cb_fn(cb_arg, NULL, -ENOMEM); 2666 return; 2667 } 2668 2669 cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE; 2670 cpl.u.blob_handle.cb_fn = cb_fn; 2671 cpl.u.blob_handle.cb_arg = cb_arg; 2672 cpl.u.blob_handle.blob = blob; 2673 2674 seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl); 2675 if (!seq) { 2676 _spdk_blob_free(blob); 2677 cb_fn(cb_arg, NULL, -ENOMEM); 2678 return; 2679 } 2680 2681 _spdk_blob_load(seq, blob, _spdk_bs_md_open_blob_cpl, blob); 2682 } 2683 2684 /* START spdk_bs_md_sync_blob */ 2685 static void 2686 _spdk_blob_sync_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2687 { 2688 spdk_bs_sequence_finish(seq, bserrno); 2689 } 2690 2691 void spdk_bs_md_sync_blob(struct spdk_blob *blob, 2692 spdk_blob_op_complete cb_fn, void *cb_arg) 2693 { 2694 struct spdk_bs_cpl cpl; 2695 spdk_bs_sequence_t *seq; 2696 2697 assert(blob != NULL); 2698 2699 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Syncing blob %lu\n", blob->id); 2700 2701 assert(blob->state != SPDK_BLOB_STATE_LOADING && 2702 blob->state != SPDK_BLOB_STATE_SYNCING); 2703 2704 if (blob->state == SPDK_BLOB_STATE_CLEAN) { 2705 cb_fn(cb_arg, 0); 2706 return; 2707 } 2708 2709 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2710 cpl.u.blob_basic.cb_fn = cb_fn; 2711 cpl.u.blob_basic.cb_arg = cb_arg; 2712 2713 seq = spdk_bs_sequence_start(blob->bs->md_target.md_channel, &cpl); 2714 if (!seq) { 2715 cb_fn(cb_arg, -ENOMEM); 2716 return; 2717 } 2718 2719 _spdk_blob_persist(seq, blob, _spdk_blob_sync_cpl, blob); 2720 } 2721 2722 /* END spdk_bs_md_sync_blob */ 2723 2724 /* START spdk_bs_md_close_blob */ 2725 2726 static void 2727 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno) 2728 { 2729 struct spdk_blob **blob = cb_arg; 2730 2731 if ((*blob)->open_ref == 0) { 2732 TAILQ_REMOVE(&(*blob)->bs->blobs, (*blob), link); 2733 _spdk_blob_free((*blob)); 2734 } 2735 2736 *blob = NULL; 2737 2738 spdk_bs_sequence_finish(seq, bserrno); 2739 } 2740 2741 void spdk_bs_md_close_blob(struct spdk_blob **b, 2742 spdk_blob_op_complete cb_fn, void *cb_arg) 2743 { 2744 struct spdk_bs_cpl cpl; 2745 struct spdk_blob *blob; 2746 spdk_bs_sequence_t *seq; 2747 2748 assert(b != NULL); 2749 blob = *b; 2750 assert(blob != NULL); 2751 2752 SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Closing blob %lu\n", blob->id); 2753 2754 assert(blob->state != SPDK_BLOB_STATE_LOADING && 2755 blob->state != SPDK_BLOB_STATE_SYNCING); 2756 2757 if (blob->open_ref == 0) { 2758 cb_fn(cb_arg, -EBADF); 2759 return; 2760 } 2761 2762 blob->open_ref--; 2763 2764 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC; 2765 cpl.u.blob_basic.cb_fn = cb_fn; 2766 cpl.u.blob_basic.cb_arg = cb_arg; 2767 2768 seq = spdk_bs_sequence_start(blob->bs->md_target.md_channel, &cpl); 2769 if (!seq) { 2770 cb_fn(cb_arg, -ENOMEM); 2771 return; 2772 } 2773 2774 if (blob->state == SPDK_BLOB_STATE_CLEAN) { 2775 _spdk_blob_close_cpl(seq, b, 0); 2776 return; 2777 } 2778 2779 /* Sync metadata */ 2780 _spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, b); 2781 } 2782 2783 /* END spdk_bs_md_close_blob */ 2784 2785 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs) 2786 { 2787 return spdk_get_io_channel(&bs->io_target); 2788 } 2789 2790 void spdk_bs_free_io_channel(struct spdk_io_channel *channel) 2791 { 2792 spdk_put_io_channel(channel); 2793 } 2794 2795 void spdk_bs_io_flush_channel(struct spdk_io_channel *channel, 2796 spdk_blob_op_complete cb_fn, void *cb_arg) 2797 { 2798 /* Flush is synchronous right now */ 2799 cb_fn(cb_arg, 0); 2800 } 2801 2802 void spdk_bs_io_write_blob(struct spdk_blob *blob, struct spdk_io_channel *channel, 2803 void *payload, uint64_t offset, uint64_t length, 2804 spdk_blob_op_complete cb_fn, void *cb_arg) 2805 { 2806 _spdk_blob_request_submit_rw(blob, channel, payload, offset, length, cb_fn, cb_arg, false); 2807 } 2808 2809 void spdk_bs_io_read_blob(struct spdk_blob *blob, struct spdk_io_channel *channel, 2810 void *payload, uint64_t offset, uint64_t length, 2811 spdk_blob_op_complete cb_fn, void *cb_arg) 2812 { 2813 _spdk_blob_request_submit_rw(blob, channel, payload, offset, length, cb_fn, cb_arg, true); 2814 } 2815 2816 void spdk_bs_io_writev_blob(struct spdk_blob *blob, struct spdk_io_channel *channel, 2817 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 2818 spdk_blob_op_complete cb_fn, void *cb_arg) 2819 { 2820 _spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false); 2821 } 2822 2823 void spdk_bs_io_readv_blob(struct spdk_blob *blob, struct spdk_io_channel *channel, 2824 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length, 2825 spdk_blob_op_complete cb_fn, void *cb_arg) 2826 { 2827 _spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true); 2828 } 2829 2830 struct spdk_bs_iter_ctx { 2831 int64_t page_num; 2832 struct spdk_blob_store *bs; 2833 2834 spdk_blob_op_with_handle_complete cb_fn; 2835 void *cb_arg; 2836 }; 2837 2838 static void 2839 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno) 2840 { 2841 struct spdk_bs_iter_ctx *ctx = cb_arg; 2842 struct spdk_blob_store *bs = ctx->bs; 2843 spdk_blob_id id; 2844 2845 if (bserrno == 0) { 2846 ctx->cb_fn(ctx->cb_arg, blob, bserrno); 2847 free(ctx); 2848 return; 2849 } 2850 2851 ctx->page_num++; 2852 ctx->page_num = spdk_bit_array_find_first_set(bs->used_md_pages, ctx->page_num); 2853 if (ctx->page_num >= spdk_bit_array_capacity(bs->used_md_pages)) { 2854 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT); 2855 free(ctx); 2856 return; 2857 } 2858 2859 id = _spdk_bs_page_to_blobid(ctx->page_num); 2860 2861 blob = _spdk_blob_lookup(bs, id); 2862 if (blob) { 2863 blob->open_ref++; 2864 ctx->cb_fn(ctx->cb_arg, blob, 0); 2865 free(ctx); 2866 return; 2867 } 2868 2869 spdk_bs_md_open_blob(bs, id, _spdk_bs_iter_cpl, ctx); 2870 } 2871 2872 void 2873 spdk_bs_md_iter_first(struct spdk_blob_store *bs, 2874 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 2875 { 2876 struct spdk_bs_iter_ctx *ctx; 2877 2878 ctx = calloc(1, sizeof(*ctx)); 2879 if (!ctx) { 2880 cb_fn(cb_arg, NULL, -ENOMEM); 2881 return; 2882 } 2883 2884 ctx->page_num = -1; 2885 ctx->bs = bs; 2886 ctx->cb_fn = cb_fn; 2887 ctx->cb_arg = cb_arg; 2888 2889 _spdk_bs_iter_cpl(ctx, NULL, -1); 2890 } 2891 2892 static void 2893 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno) 2894 { 2895 struct spdk_bs_iter_ctx *ctx = cb_arg; 2896 2897 _spdk_bs_iter_cpl(ctx, NULL, -1); 2898 } 2899 2900 void 2901 spdk_bs_md_iter_next(struct spdk_blob_store *bs, struct spdk_blob **b, 2902 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg) 2903 { 2904 struct spdk_bs_iter_ctx *ctx; 2905 struct spdk_blob *blob; 2906 2907 assert(b != NULL); 2908 blob = *b; 2909 assert(blob != NULL); 2910 2911 ctx = calloc(1, sizeof(*ctx)); 2912 if (!ctx) { 2913 cb_fn(cb_arg, NULL, -ENOMEM); 2914 return; 2915 } 2916 2917 ctx->page_num = _spdk_bs_blobid_to_page(blob->id); 2918 ctx->bs = bs; 2919 ctx->cb_fn = cb_fn; 2920 ctx->cb_arg = cb_arg; 2921 2922 /* Close the existing blob */ 2923 spdk_bs_md_close_blob(b, _spdk_bs_iter_close_cpl, ctx); 2924 } 2925 2926 int 2927 spdk_blob_md_set_xattr(struct spdk_blob *blob, const char *name, const void *value, 2928 uint16_t value_len) 2929 { 2930 struct spdk_xattr *xattr; 2931 2932 assert(blob != NULL); 2933 2934 assert(blob->state != SPDK_BLOB_STATE_LOADING && 2935 blob->state != SPDK_BLOB_STATE_SYNCING); 2936 2937 TAILQ_FOREACH(xattr, &blob->xattrs, link) { 2938 if (!strcmp(name, xattr->name)) { 2939 free(xattr->value); 2940 xattr->value_len = value_len; 2941 xattr->value = malloc(value_len); 2942 memcpy(xattr->value, value, value_len); 2943 2944 blob->state = SPDK_BLOB_STATE_DIRTY; 2945 2946 return 0; 2947 } 2948 } 2949 2950 xattr = calloc(1, sizeof(*xattr)); 2951 if (!xattr) { 2952 return -1; 2953 } 2954 xattr->name = strdup(name); 2955 xattr->value_len = value_len; 2956 xattr->value = malloc(value_len); 2957 memcpy(xattr->value, value, value_len); 2958 TAILQ_INSERT_TAIL(&blob->xattrs, xattr, link); 2959 2960 blob->state = SPDK_BLOB_STATE_DIRTY; 2961 2962 return 0; 2963 } 2964 2965 int 2966 spdk_blob_md_remove_xattr(struct spdk_blob *blob, const char *name) 2967 { 2968 struct spdk_xattr *xattr; 2969 2970 assert(blob != NULL); 2971 2972 assert(blob->state != SPDK_BLOB_STATE_LOADING && 2973 blob->state != SPDK_BLOB_STATE_SYNCING); 2974 2975 TAILQ_FOREACH(xattr, &blob->xattrs, link) { 2976 if (!strcmp(name, xattr->name)) { 2977 TAILQ_REMOVE(&blob->xattrs, xattr, link); 2978 free(xattr->value); 2979 free(xattr->name); 2980 free(xattr); 2981 2982 blob->state = SPDK_BLOB_STATE_DIRTY; 2983 2984 return 0; 2985 } 2986 } 2987 2988 return -ENOENT; 2989 } 2990 2991 int 2992 spdk_bs_md_get_xattr_value(struct spdk_blob *blob, const char *name, 2993 const void **value, size_t *value_len) 2994 { 2995 struct spdk_xattr *xattr; 2996 2997 TAILQ_FOREACH(xattr, &blob->xattrs, link) { 2998 if (!strcmp(name, xattr->name)) { 2999 *value = xattr->value; 3000 *value_len = xattr->value_len; 3001 return 0; 3002 } 3003 } 3004 3005 return -ENOENT; 3006 } 3007 3008 struct spdk_xattr_names { 3009 uint32_t count; 3010 const char *names[0]; 3011 }; 3012 3013 int 3014 spdk_bs_md_get_xattr_names(struct spdk_blob *blob, 3015 struct spdk_xattr_names **names) 3016 { 3017 struct spdk_xattr *xattr; 3018 int count = 0; 3019 3020 TAILQ_FOREACH(xattr, &blob->xattrs, link) { 3021 count++; 3022 } 3023 3024 *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *)); 3025 if (*names == NULL) { 3026 return -ENOMEM; 3027 } 3028 3029 TAILQ_FOREACH(xattr, &blob->xattrs, link) { 3030 (*names)->names[(*names)->count++] = xattr->name; 3031 } 3032 3033 return 0; 3034 } 3035 3036 uint32_t 3037 spdk_xattr_names_get_count(struct spdk_xattr_names *names) 3038 { 3039 assert(names != NULL); 3040 3041 return names->count; 3042 } 3043 3044 const char * 3045 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index) 3046 { 3047 if (index >= names->count) { 3048 return NULL; 3049 } 3050 3051 return names->names[index]; 3052 } 3053 3054 void 3055 spdk_xattr_names_free(struct spdk_xattr_names *names) 3056 { 3057 free(names); 3058 } 3059 3060 struct spdk_bs_type 3061 spdk_bs_get_bstype(struct spdk_blob_store *bs) 3062 { 3063 return bs->bstype; 3064 } 3065 3066 void 3067 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype) 3068 { 3069 memcpy(&bs->bstype, &bstype, sizeof(bstype)); 3070 } 3071 3072 SPDK_LOG_REGISTER_TRACE_FLAG("blob", SPDK_TRACE_BLOB); 3073