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 #ifndef SPDK_BLOBSTORE_H 35 #define SPDK_BLOBSTORE_H 36 37 #include "spdk/assert.h" 38 #include "spdk/blob.h" 39 #include "spdk/queue.h" 40 #include "spdk/util.h" 41 #include "spdk/tree.h" 42 43 #include "request.h" 44 45 /* In Memory Data Structures 46 * 47 * The following data structures exist only in memory. 48 */ 49 50 #define SPDK_BLOB_OPTS_CLUSTER_SZ (1024 * 1024) 51 #define SPDK_BLOB_OPTS_NUM_MD_PAGES UINT32_MAX 52 #define SPDK_BLOB_OPTS_MAX_MD_OPS 32 53 #define SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS 512 54 #define SPDK_BLOB_BLOBID_HIGH_BIT (1ULL << 32) 55 56 struct spdk_xattr { 57 uint32_t index; 58 uint16_t value_len; 59 char *name; 60 void *value; 61 TAILQ_ENTRY(spdk_xattr) link; 62 }; 63 64 /* The mutable part of the blob data that is sync'd to 65 * disk. The data in here is both mutable and persistent. 66 */ 67 struct spdk_blob_mut_data { 68 /* Number of data clusters in the blob */ 69 uint64_t num_clusters; 70 71 /* Array LBAs that are the beginning of a cluster, in 72 * the order they appear in the blob. 73 */ 74 uint64_t *clusters; 75 76 /* The size of the clusters array. This is greater than or 77 * equal to 'num_clusters'. 78 */ 79 size_t cluster_array_size; 80 81 /* Number of extent pages */ 82 uint64_t num_extent_pages; 83 84 /* Array of page offsets into the metadata region, 85 * containing extents. Can contain entries for not yet 86 * allocated pages. */ 87 uint32_t *extent_pages; 88 89 /* The size of the extent page array. This is greater than or 90 * equal to 'num_extent_pages'. */ 91 size_t extent_pages_array_size; 92 93 /* Number of metadata pages */ 94 uint32_t num_pages; 95 96 /* Array of page offsets into the metadata region, in 97 * the order of the metadata page sequence. 98 */ 99 uint32_t *pages; 100 }; 101 102 enum spdk_blob_state { 103 /* The blob in-memory version does not match the on-disk 104 * version. 105 */ 106 SPDK_BLOB_STATE_DIRTY, 107 108 /* The blob in memory version of the blob matches the on disk 109 * version. 110 */ 111 SPDK_BLOB_STATE_CLEAN, 112 113 /* The in-memory state being synchronized with the on-disk 114 * blob state. */ 115 SPDK_BLOB_STATE_LOADING, 116 }; 117 118 TAILQ_HEAD(spdk_xattr_tailq, spdk_xattr); 119 120 struct spdk_blob_list { 121 spdk_blob_id id; 122 size_t clone_count; 123 TAILQ_HEAD(, spdk_blob_list) clones; 124 TAILQ_ENTRY(spdk_blob_list) link; 125 }; 126 127 struct spdk_blob { 128 struct spdk_blob_store *bs; 129 130 uint32_t open_ref; 131 132 spdk_blob_id id; 133 spdk_blob_id parent_id; 134 135 enum spdk_blob_state state; 136 137 /* Two copies of the mutable data. One is a version 138 * that matches the last known data on disk (clean). 139 * The other (active) is the current data. Syncing 140 * a blob makes the clean match the active. 141 */ 142 struct spdk_blob_mut_data clean; 143 struct spdk_blob_mut_data active; 144 145 bool invalid; 146 bool data_ro; 147 bool md_ro; 148 149 uint64_t invalid_flags; 150 uint64_t data_ro_flags; 151 uint64_t md_ro_flags; 152 153 struct spdk_bs_dev *back_bs_dev; 154 155 /* TODO: The xattrs are mutable, but we don't want to be 156 * copying them unnecessarily. Figure this out. 157 */ 158 struct spdk_xattr_tailq xattrs; 159 struct spdk_xattr_tailq xattrs_internal; 160 161 RB_ENTRY(spdk_blob) link; 162 163 uint32_t frozen_refcnt; 164 bool locked_operation_in_progress; 165 enum blob_clear_method clear_method; 166 bool extent_rle_found; 167 bool extent_table_found; 168 bool use_extent_table; 169 170 /* A list of pending metadata pending_persists */ 171 TAILQ_HEAD(, spdk_blob_persist_ctx) pending_persists; 172 TAILQ_HEAD(, spdk_blob_persist_ctx) persists_to_complete; 173 174 /* Number of data clusters retrieved from extent table, 175 * that many have to be read from extent pages. */ 176 uint64_t remaining_clusters_in_et; 177 }; 178 179 struct spdk_blob_store { 180 uint64_t md_start; /* Offset from beginning of disk, in pages */ 181 uint32_t md_len; /* Count, in pages */ 182 183 struct spdk_io_channel *md_channel; 184 uint32_t max_channel_ops; 185 186 struct spdk_thread *md_thread; 187 188 struct spdk_bs_dev *dev; 189 190 struct spdk_bit_array *used_md_pages; 191 struct spdk_bit_pool *used_clusters; 192 struct spdk_bit_array *used_blobids; 193 struct spdk_bit_array *open_blobids; 194 195 pthread_mutex_t used_clusters_mutex; 196 197 uint32_t cluster_sz; 198 uint64_t total_clusters; 199 uint64_t total_data_clusters; 200 uint64_t num_free_clusters; 201 uint64_t pages_per_cluster; 202 uint8_t pages_per_cluster_shift; 203 uint32_t io_unit_size; 204 205 spdk_blob_id super_blob; 206 struct spdk_bs_type bstype; 207 208 struct spdk_bs_cpl unload_cpl; 209 int unload_err; 210 211 RB_HEAD(spdk_blob_tree, spdk_blob) open_blobs; 212 TAILQ_HEAD(, spdk_blob_list) snapshots; 213 214 bool clean; 215 }; 216 217 struct spdk_bs_channel { 218 struct spdk_bs_request_set *req_mem; 219 TAILQ_HEAD(, spdk_bs_request_set) reqs; 220 221 struct spdk_blob_store *bs; 222 223 struct spdk_bs_dev *dev; 224 struct spdk_io_channel *dev_channel; 225 226 TAILQ_HEAD(, spdk_bs_request_set) need_cluster_alloc; 227 TAILQ_HEAD(, spdk_bs_request_set) queued_io; 228 }; 229 230 /** operation type */ 231 enum spdk_blob_op_type { 232 SPDK_BLOB_WRITE, 233 SPDK_BLOB_READ, 234 SPDK_BLOB_UNMAP, 235 SPDK_BLOB_WRITE_ZEROES, 236 SPDK_BLOB_WRITEV, 237 SPDK_BLOB_READV, 238 }; 239 240 /* back bs_dev */ 241 242 #define BLOB_SNAPSHOT "SNAP" 243 #define SNAPSHOT_IN_PROGRESS "SNAPTMP" 244 #define SNAPSHOT_PENDING_REMOVAL "SNAPRM" 245 246 struct spdk_blob_bs_dev { 247 struct spdk_bs_dev bs_dev; 248 struct spdk_blob *blob; 249 }; 250 251 /* On-Disk Data Structures 252 * 253 * The following data structures exist on disk. 254 */ 255 #define SPDK_BS_INITIAL_VERSION 1 256 #define SPDK_BS_VERSION 3 /* current version */ 257 258 #pragma pack(push, 1) 259 260 #define SPDK_MD_MASK_TYPE_USED_PAGES 0 261 #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1 262 #define SPDK_MD_MASK_TYPE_USED_BLOBIDS 2 263 264 struct spdk_bs_md_mask { 265 uint8_t type; 266 uint32_t length; /* In bits */ 267 uint8_t mask[0]; 268 }; 269 270 #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0 271 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2 272 #define SPDK_MD_DESCRIPTOR_TYPE_FLAGS 3 273 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL 4 274 275 /* Following descriptors define cluster layout in a blob. 276 * EXTENT_RLE cannot be present in blobs metadata, 277 * at the same time as EXTENT_TABLE and EXTENT_PAGE descriptors. */ 278 279 /* EXTENT_RLE descriptor holds an array of LBA that points to 280 * beginning of allocated clusters. The array is run-length encoded, 281 * with 0's being unallocated clusters. It is part of serialized 282 * metadata chain for a blob. */ 283 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE 1 284 /* EXTENT_TABLE descriptor holds array of md page offsets that 285 * point to pages with EXTENT_PAGE descriptor. The 0's in the array 286 * are run-length encoded, non-zero values are unallocated pages. 287 * It is part of serialized metadata chain for a blob. */ 288 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE 5 289 /* EXTENT_PAGE descriptor holds an array of LBAs that point to 290 * beginning of allocated clusters. The array is run-length encoded, 291 * with 0's being unallocated clusters. It is NOT part of 292 * serialized metadata chain for a blob. */ 293 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE 6 294 295 struct spdk_blob_md_descriptor_xattr { 296 uint8_t type; 297 uint32_t length; 298 299 uint16_t name_length; 300 uint16_t value_length; 301 302 char name[0]; 303 /* String name immediately followed by string value. */ 304 }; 305 306 struct spdk_blob_md_descriptor_extent_rle { 307 uint8_t type; 308 uint32_t length; 309 310 struct { 311 uint32_t cluster_idx; 312 uint32_t length; /* In units of clusters */ 313 } extents[0]; 314 }; 315 316 struct spdk_blob_md_descriptor_extent_table { 317 uint8_t type; 318 uint32_t length; 319 320 /* Number of data clusters in the blob */ 321 uint64_t num_clusters; 322 323 struct { 324 uint32_t page_idx; 325 uint32_t num_pages; /* In units of pages */ 326 } extent_page[0]; 327 }; 328 329 struct spdk_blob_md_descriptor_extent_page { 330 uint8_t type; 331 uint32_t length; 332 333 /* First cluster index in this extent page */ 334 uint32_t start_cluster_idx; 335 336 uint32_t cluster_idx[0]; 337 }; 338 339 #define SPDK_BLOB_THIN_PROV (1ULL << 0) 340 #define SPDK_BLOB_INTERNAL_XATTR (1ULL << 1) 341 #define SPDK_BLOB_EXTENT_TABLE (1ULL << 2) 342 #define SPDK_BLOB_INVALID_FLAGS_MASK (SPDK_BLOB_THIN_PROV | SPDK_BLOB_INTERNAL_XATTR | SPDK_BLOB_EXTENT_TABLE) 343 344 #define SPDK_BLOB_READ_ONLY (1ULL << 0) 345 #define SPDK_BLOB_DATA_RO_FLAGS_MASK SPDK_BLOB_READ_ONLY 346 347 #define SPDK_BLOB_CLEAR_METHOD_SHIFT 0 348 #define SPDK_BLOB_CLEAR_METHOD (3ULL << SPDK_BLOB_CLEAR_METHOD_SHIFT) 349 #define SPDK_BLOB_MD_RO_FLAGS_MASK SPDK_BLOB_CLEAR_METHOD 350 351 struct spdk_blob_md_descriptor_flags { 352 uint8_t type; 353 uint32_t length; 354 355 /* 356 * If a flag in invalid_flags is set that the application is not aware of, 357 * it will not allow the blob to be opened. 358 */ 359 uint64_t invalid_flags; 360 361 /* 362 * If a flag in data_ro_flags is set that the application is not aware of, 363 * allow the blob to be opened in data_read_only and md_read_only mode. 364 */ 365 uint64_t data_ro_flags; 366 367 /* 368 * If a flag in md_ro_flags is set the the application is not aware of, 369 * allow the blob to be opened in md_read_only mode. 370 */ 371 uint64_t md_ro_flags; 372 }; 373 374 struct spdk_blob_md_descriptor { 375 uint8_t type; 376 uint32_t length; 377 }; 378 379 #define SPDK_INVALID_MD_PAGE UINT32_MAX 380 381 struct spdk_blob_md_page { 382 spdk_blob_id id; 383 384 uint32_t sequence_num; 385 uint32_t reserved0; 386 387 /* Descriptors here */ 388 uint8_t descriptors[4072]; 389 390 uint32_t next; 391 uint32_t crc; 392 }; 393 #define SPDK_BS_PAGE_SIZE 0x1000 394 SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size"); 395 396 #define SPDK_BS_MAX_DESC_SIZE SPDK_SIZEOF_MEMBER(struct spdk_blob_md_page, descriptors) 397 398 /* Maximum number of extents a single Extent Page can fit. 399 * For an SPDK_BS_PAGE_SIZE of 4K SPDK_EXTENTS_PER_EP would be 512. */ 400 #define SPDK_EXTENTS_PER_EP_MAX ((SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_extent_page)) / sizeof(uint32_t)) 401 #define SPDK_EXTENTS_PER_EP (spdk_align64pow2(SPDK_EXTENTS_PER_EP_MAX + 1) >> 1u) 402 403 #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB" 404 405 struct spdk_bs_super_block { 406 uint8_t signature[8]; 407 uint32_t version; 408 uint32_t length; 409 uint32_t clean; /* If there was a clean shutdown, this is 1. */ 410 spdk_blob_id super_blob; 411 412 uint32_t cluster_size; /* In bytes */ 413 414 uint32_t used_page_mask_start; /* Offset from beginning of disk, in pages */ 415 uint32_t used_page_mask_len; /* Count, in pages */ 416 417 uint32_t used_cluster_mask_start; /* Offset from beginning of disk, in pages */ 418 uint32_t used_cluster_mask_len; /* Count, in pages */ 419 420 uint32_t md_start; /* Offset from beginning of disk, in pages */ 421 uint32_t md_len; /* Count, in pages */ 422 423 struct spdk_bs_type bstype; /* blobstore type */ 424 425 uint32_t used_blobid_mask_start; /* Offset from beginning of disk, in pages */ 426 uint32_t used_blobid_mask_len; /* Count, in pages */ 427 428 uint64_t size; /* size of blobstore in bytes */ 429 uint32_t io_unit_size; /* Size of io unit in bytes */ 430 431 uint8_t reserved[4000]; 432 uint32_t crc; 433 }; 434 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size"); 435 436 #pragma pack(pop) 437 438 struct spdk_bs_dev *bs_create_zeroes_dev(void); 439 struct spdk_bs_dev *bs_create_blob_bs_dev(struct spdk_blob *blob); 440 441 /* Unit Conversions 442 * 443 * The blobstore works with several different units: 444 * - Byte: Self explanatory 445 * - LBA: The logical blocks on the backing storage device. 446 * - Page: The read/write units of blobs and metadata. This is 447 * an offset into a blob in units of 4KiB. 448 * - Cluster Index: The disk is broken into a sequential list of 449 * clusters. This is the offset from the beginning. 450 * 451 * NOTE: These conversions all act on simple magnitudes, not with any sort 452 * of knowledge about the blobs themselves. For instance, converting 453 * a page to an lba with the conversion function below simply converts 454 * a number of pages to an equivalent number of lbas, but that 455 * lba certainly isn't the right lba that corresponds to a page offset 456 * for a particular blob. 457 */ 458 static inline uint64_t 459 bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length) 460 { 461 assert(length % bs->dev->blocklen == 0); 462 463 return length / bs->dev->blocklen; 464 } 465 466 static inline uint64_t 467 bs_dev_byte_to_lba(struct spdk_bs_dev *bs_dev, uint64_t length) 468 { 469 assert(length % bs_dev->blocklen == 0); 470 471 return length / bs_dev->blocklen; 472 } 473 474 static inline uint64_t 475 bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page) 476 { 477 return page * SPDK_BS_PAGE_SIZE / bs->dev->blocklen; 478 } 479 480 static inline uint64_t 481 bs_md_page_to_lba(struct spdk_blob_store *bs, uint32_t page) 482 { 483 assert(page < bs->md_len); 484 return bs_page_to_lba(bs, page + bs->md_start); 485 } 486 487 static inline uint64_t 488 bs_dev_page_to_lba(struct spdk_bs_dev *bs_dev, uint64_t page) 489 { 490 return page * SPDK_BS_PAGE_SIZE / bs_dev->blocklen; 491 } 492 493 static inline uint64_t 494 bs_io_unit_per_page(struct spdk_blob_store *bs) 495 { 496 return SPDK_BS_PAGE_SIZE / bs->io_unit_size; 497 } 498 499 static inline uint64_t 500 bs_io_unit_to_page(struct spdk_blob_store *bs, uint64_t io_unit) 501 { 502 return io_unit / bs_io_unit_per_page(bs); 503 } 504 505 static inline uint64_t 506 bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster) 507 { 508 return (uint64_t)cluster * bs->pages_per_cluster; 509 } 510 511 static inline uint32_t 512 bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page) 513 { 514 assert(page % bs->pages_per_cluster == 0); 515 516 return page / bs->pages_per_cluster; 517 } 518 519 static inline uint64_t 520 bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster) 521 { 522 return (uint64_t)cluster * (bs->cluster_sz / bs->dev->blocklen); 523 } 524 525 static inline uint32_t 526 bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba) 527 { 528 assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0); 529 530 return lba / (bs->cluster_sz / bs->dev->blocklen); 531 } 532 533 static inline uint64_t 534 bs_io_unit_to_back_dev_lba(struct spdk_blob *blob, uint64_t io_unit) 535 { 536 return io_unit * (blob->bs->io_unit_size / blob->back_bs_dev->blocklen); 537 } 538 539 static inline uint64_t 540 bs_back_dev_lba_to_io_unit(struct spdk_blob *blob, uint64_t lba) 541 { 542 return lba * (blob->back_bs_dev->blocklen / blob->bs->io_unit_size); 543 } 544 545 static inline uint64_t 546 bs_cluster_to_extent_table_id(uint64_t cluster_num) 547 { 548 return cluster_num / SPDK_EXTENTS_PER_EP; 549 } 550 551 static inline uint32_t * 552 bs_cluster_to_extent_page(struct spdk_blob *blob, uint64_t cluster_num) 553 { 554 uint64_t extent_table_id = bs_cluster_to_extent_table_id(cluster_num); 555 556 assert(blob->use_extent_table); 557 assert(extent_table_id < blob->active.extent_pages_array_size); 558 559 return &blob->active.extent_pages[extent_table_id]; 560 } 561 562 /* End basic conversions */ 563 564 static inline uint64_t 565 bs_blobid_to_page(spdk_blob_id id) 566 { 567 return id & 0xFFFFFFFF; 568 } 569 570 /* The blob id is a 64 bit number. The lower 32 bits are the page_idx. The upper 571 * 32 bits are not currently used. Stick a 1 there just to catch bugs where the 572 * code assumes blob id == page_idx. 573 */ 574 static inline spdk_blob_id 575 bs_page_to_blobid(uint64_t page_idx) 576 { 577 if (page_idx > UINT32_MAX) { 578 return SPDK_BLOBID_INVALID; 579 } 580 return SPDK_BLOB_BLOBID_HIGH_BIT | page_idx; 581 } 582 583 /* Given an io unit offset into a blob, look up the LBA for the 584 * start of that io unit. 585 */ 586 static inline uint64_t 587 bs_blob_io_unit_to_lba(struct spdk_blob *blob, uint64_t io_unit) 588 { 589 uint64_t lba; 590 uint64_t pages_per_cluster; 591 uint8_t shift; 592 uint64_t io_units_per_cluster; 593 uint64_t io_units_per_page; 594 uint64_t page; 595 596 page = bs_io_unit_to_page(blob->bs, io_unit); 597 598 pages_per_cluster = blob->bs->pages_per_cluster; 599 shift = blob->bs->pages_per_cluster_shift; 600 io_units_per_page = bs_io_unit_per_page(blob->bs); 601 602 assert(page < blob->active.num_clusters * pages_per_cluster); 603 604 if (shift != 0) { 605 io_units_per_cluster = io_units_per_page << shift; 606 lba = blob->active.clusters[page >> shift]; 607 } else { 608 io_units_per_cluster = io_units_per_page * pages_per_cluster; 609 lba = blob->active.clusters[page / pages_per_cluster]; 610 } 611 lba += io_unit % io_units_per_cluster; 612 return lba; 613 } 614 615 /* Given an io_unit offset into a blob, look up the number of io_units until the 616 * next cluster boundary. 617 */ 618 static inline uint32_t 619 bs_num_io_units_to_cluster_boundary(struct spdk_blob *blob, uint64_t io_unit) 620 { 621 uint64_t io_units_per_cluster; 622 uint8_t shift = blob->bs->pages_per_cluster_shift; 623 624 if (shift != 0) { 625 io_units_per_cluster = bs_io_unit_per_page(blob->bs) << shift; 626 } else { 627 io_units_per_cluster = bs_io_unit_per_page(blob->bs) * blob->bs->pages_per_cluster; 628 } 629 630 return io_units_per_cluster - (io_unit % io_units_per_cluster); 631 } 632 633 /* Given a page offset into a blob, look up the number of pages until the 634 * next cluster boundary. 635 */ 636 static inline uint32_t 637 bs_num_pages_to_cluster_boundary(struct spdk_blob *blob, uint64_t page) 638 { 639 uint64_t pages_per_cluster; 640 641 pages_per_cluster = blob->bs->pages_per_cluster; 642 643 return pages_per_cluster - (page % pages_per_cluster); 644 } 645 646 /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */ 647 static inline uint32_t 648 bs_io_unit_to_cluster_start(struct spdk_blob *blob, uint64_t io_unit) 649 { 650 uint64_t pages_per_cluster; 651 uint64_t page; 652 653 pages_per_cluster = blob->bs->pages_per_cluster; 654 page = bs_io_unit_to_page(blob->bs, io_unit); 655 656 return page - (page % pages_per_cluster); 657 } 658 659 /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */ 660 static inline uint32_t 661 bs_io_unit_to_cluster_number(struct spdk_blob *blob, uint64_t io_unit) 662 { 663 uint64_t pages_per_cluster = blob->bs->pages_per_cluster; 664 uint8_t shift = blob->bs->pages_per_cluster_shift; 665 uint32_t page_offset; 666 667 page_offset = io_unit / bs_io_unit_per_page(blob->bs); 668 if (shift != 0) { 669 return page_offset >> shift; 670 } else { 671 return page_offset / pages_per_cluster; 672 } 673 } 674 675 /* Given an io unit offset into a blob, look up if it is from allocated cluster. */ 676 static inline bool 677 bs_io_unit_is_allocated(struct spdk_blob *blob, uint64_t io_unit) 678 { 679 uint64_t lba; 680 uint64_t page; 681 uint64_t pages_per_cluster; 682 uint8_t shift; 683 684 shift = blob->bs->pages_per_cluster_shift; 685 pages_per_cluster = blob->bs->pages_per_cluster; 686 page = bs_io_unit_to_page(blob->bs, io_unit); 687 688 assert(page < blob->active.num_clusters * pages_per_cluster); 689 690 if (shift != 0) { 691 lba = blob->active.clusters[page >> shift]; 692 } else { 693 lba = blob->active.clusters[page / pages_per_cluster]; 694 } 695 696 if (lba == 0) { 697 assert(spdk_blob_is_thin_provisioned(blob)); 698 return false; 699 } else { 700 return true; 701 } 702 } 703 704 #endif 705