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 42 #include "request.h" 43 44 /* In Memory Data Structures 45 * 46 * The following data structures exist only in memory. 47 */ 48 49 #define SPDK_BLOB_OPTS_CLUSTER_SZ (1024 * 1024) 50 #define SPDK_BLOB_OPTS_NUM_MD_PAGES UINT32_MAX 51 #define SPDK_BLOB_OPTS_MAX_MD_OPS 32 52 #define SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS 512 53 #define SPDK_BLOB_BLOBID_HIGH_BIT (1ULL << 32) 54 55 struct spdk_xattr { 56 /* TODO: reorder for best packing */ 57 uint32_t index; 58 char *name; 59 void *value; 60 uint16_t value_len; 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 metadata pages */ 82 uint32_t num_pages; 83 84 /* Array of page offsets into the metadata region, in 85 * the order of the metadata page sequence. 86 */ 87 uint32_t *pages; 88 }; 89 90 enum spdk_blob_state { 91 /* The blob in-memory version does not match the on-disk 92 * version. 93 */ 94 SPDK_BLOB_STATE_DIRTY, 95 96 /* The blob in memory version of the blob matches the on disk 97 * version. 98 */ 99 SPDK_BLOB_STATE_CLEAN, 100 101 /* The in-memory state being synchronized with the on-disk 102 * blob state. */ 103 SPDK_BLOB_STATE_LOADING, 104 }; 105 106 TAILQ_HEAD(spdk_xattr_tailq, spdk_xattr); 107 108 struct spdk_blob_list { 109 spdk_blob_id id; 110 size_t clone_count; 111 TAILQ_HEAD(, spdk_blob_list) clones; 112 TAILQ_ENTRY(spdk_blob_list) link; 113 }; 114 115 struct spdk_blob { 116 struct spdk_blob_store *bs; 117 118 uint32_t open_ref; 119 120 spdk_blob_id id; 121 spdk_blob_id parent_id; 122 123 enum spdk_blob_state state; 124 125 /* Two copies of the mutable data. One is a version 126 * that matches the last known data on disk (clean). 127 * The other (active) is the current data. Syncing 128 * a blob makes the clean match the active. 129 */ 130 struct spdk_blob_mut_data clean; 131 struct spdk_blob_mut_data active; 132 133 bool invalid; 134 bool data_ro; 135 bool md_ro; 136 137 uint64_t invalid_flags; 138 uint64_t data_ro_flags; 139 uint64_t md_ro_flags; 140 141 struct spdk_bs_dev *back_bs_dev; 142 143 /* TODO: The xattrs are mutable, but we don't want to be 144 * copying them unecessarily. Figure this out. 145 */ 146 struct spdk_xattr_tailq xattrs; 147 struct spdk_xattr_tailq xattrs_internal; 148 149 TAILQ_ENTRY(spdk_blob) link; 150 }; 151 152 struct spdk_blob_store { 153 uint64_t md_start; /* Offset from beginning of disk, in pages */ 154 uint32_t md_len; /* Count, in pages */ 155 156 struct spdk_io_channel *md_channel; 157 uint32_t max_channel_ops; 158 159 struct spdk_thread *md_thread; 160 161 struct spdk_bs_dev *dev; 162 163 struct spdk_bit_array *used_md_pages; 164 struct spdk_bit_array *used_clusters; 165 struct spdk_bit_array *used_blobids; 166 167 pthread_mutex_t used_clusters_mutex; 168 169 uint32_t cluster_sz; 170 uint64_t total_clusters; 171 uint64_t total_data_clusters; 172 uint64_t num_free_clusters; 173 uint32_t pages_per_cluster; 174 175 spdk_blob_id super_blob; 176 struct spdk_bs_type bstype; 177 178 struct spdk_bs_cpl unload_cpl; 179 int unload_err; 180 181 TAILQ_HEAD(, spdk_blob) blobs; 182 TAILQ_HEAD(, spdk_blob_list) snapshots; 183 184 bool clean; 185 }; 186 187 struct spdk_bs_channel { 188 struct spdk_bs_request_set *req_mem; 189 TAILQ_HEAD(, spdk_bs_request_set) reqs; 190 191 struct spdk_blob_store *bs; 192 193 struct spdk_bs_dev *dev; 194 struct spdk_io_channel *dev_channel; 195 196 TAILQ_HEAD(, spdk_bs_request_set) need_cluster_alloc; 197 }; 198 199 /** operation type */ 200 enum spdk_blob_op_type { 201 SPDK_BLOB_WRITE, 202 SPDK_BLOB_READ, 203 SPDK_BLOB_UNMAP, 204 SPDK_BLOB_WRITE_ZEROES, 205 SPDK_BLOB_WRITEV, 206 SPDK_BLOB_READV, 207 }; 208 209 /* back bs_dev */ 210 211 #define BLOB_SNAPSHOT "SNAP" 212 #define SNAPSHOT_IN_PROGRESS "SNAPTMP" 213 214 struct spdk_blob_bs_dev { 215 struct spdk_bs_dev bs_dev; 216 struct spdk_blob *blob; 217 }; 218 219 /* On-Disk Data Structures 220 * 221 * The following data structures exist on disk. 222 */ 223 #define SPDK_BS_INITIAL_VERSION 1 224 #define SPDK_BS_VERSION 3 /* current version */ 225 226 #pragma pack(push, 1) 227 228 #define SPDK_MD_MASK_TYPE_USED_PAGES 0 229 #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1 230 #define SPDK_MD_MASK_TYPE_USED_BLOBIDS 2 231 232 struct spdk_bs_md_mask { 233 uint8_t type; 234 uint32_t length; /* In bits */ 235 uint8_t mask[0]; 236 }; 237 238 #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0 239 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT 1 240 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2 241 #define SPDK_MD_DESCRIPTOR_TYPE_FLAGS 3 242 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL 4 243 244 struct spdk_blob_md_descriptor_xattr { 245 uint8_t type; 246 uint32_t length; 247 248 uint16_t name_length; 249 uint16_t value_length; 250 251 char name[0]; 252 /* String name immediately followed by string value. */ 253 }; 254 255 struct spdk_blob_md_descriptor_extent { 256 uint8_t type; 257 uint32_t length; 258 259 struct { 260 uint32_t cluster_idx; 261 uint32_t length; /* In units of clusters */ 262 } extents[0]; 263 }; 264 265 #define SPDK_BLOB_THIN_PROV (1ULL << 0) 266 #define SPDK_BLOB_INTERNAL_XATTR (1ULL << 1) 267 #define SPDK_BLOB_INVALID_FLAGS_MASK (SPDK_BLOB_THIN_PROV | SPDK_BLOB_INTERNAL_XATTR) 268 269 #define SPDK_BLOB_READ_ONLY (1ULL << 0) 270 #define SPDK_BLOB_DATA_RO_FLAGS_MASK SPDK_BLOB_READ_ONLY 271 #define SPDK_BLOB_MD_RO_FLAGS_MASK 0 272 273 struct spdk_blob_md_descriptor_flags { 274 uint8_t type; 275 uint32_t length; 276 277 /* 278 * If a flag in invalid_flags is set that the application is not aware of, 279 * it will not allow the blob to be opened. 280 */ 281 uint64_t invalid_flags; 282 283 /* 284 * If a flag in data_ro_flags is set that the application is not aware of, 285 * allow the blob to be opened in data_read_only and md_read_only mode. 286 */ 287 uint64_t data_ro_flags; 288 289 /* 290 * If a flag in md_ro_flags is set the the application is not aware of, 291 * allow the blob to be opened in md_read_only mode. 292 */ 293 uint64_t md_ro_flags; 294 }; 295 296 struct spdk_blob_md_descriptor { 297 uint8_t type; 298 uint32_t length; 299 }; 300 301 #define SPDK_INVALID_MD_PAGE UINT32_MAX 302 303 struct spdk_blob_md_page { 304 spdk_blob_id id; 305 306 uint32_t sequence_num; 307 uint32_t reserved0; 308 309 /* Descriptors here */ 310 uint8_t descriptors[4072]; 311 312 uint32_t next; 313 uint32_t crc; 314 }; 315 #define SPDK_BS_PAGE_SIZE 0x1000 316 SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size"); 317 318 #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB" 319 320 struct spdk_bs_super_block { 321 uint8_t signature[8]; 322 uint32_t version; 323 uint32_t length; 324 uint32_t clean; /* If there was a clean shutdown, this is 1. */ 325 spdk_blob_id super_blob; 326 327 uint32_t cluster_size; /* In bytes */ 328 329 uint32_t used_page_mask_start; /* Offset from beginning of disk, in pages */ 330 uint32_t used_page_mask_len; /* Count, in pages */ 331 332 uint32_t used_cluster_mask_start; /* Offset from beginning of disk, in pages */ 333 uint32_t used_cluster_mask_len; /* Count, in pages */ 334 335 uint32_t md_start; /* Offset from beginning of disk, in pages */ 336 uint32_t md_len; /* Count, in pages */ 337 338 struct spdk_bs_type bstype; /* blobstore type */ 339 340 uint32_t used_blobid_mask_start; /* Offset from beginning of disk, in pages */ 341 uint32_t used_blobid_mask_len; /* Count, in pages */ 342 343 uint8_t reserved[4012]; 344 uint32_t crc; 345 }; 346 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size"); 347 348 #pragma pack(pop) 349 350 struct spdk_bs_dev *spdk_bs_create_zeroes_dev(void); 351 struct spdk_bs_dev *spdk_bs_create_blob_bs_dev(struct spdk_blob *blob); 352 353 /* Unit Conversions 354 * 355 * The blobstore works with several different units: 356 * - Byte: Self explanatory 357 * - LBA: The logical blocks on the backing storage device. 358 * - Page: The read/write units of blobs and metadata. This is 359 * an offset into a blob in units of 4KiB. 360 * - Cluster Index: The disk is broken into a sequential list of 361 * clusters. This is the offset from the beginning. 362 * 363 * NOTE: These conversions all act on simple magnitudes, not with any sort 364 * of knowledge about the blobs themselves. For instance, converting 365 * a page to an lba with the conversion function below simply converts 366 * a number of pages to an equivalent number of lbas, but that 367 * lba certainly isn't the right lba that corresponds to a page offset 368 * for a particular blob. 369 */ 370 static inline uint64_t 371 _spdk_bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length) 372 { 373 assert(length % bs->dev->blocklen == 0); 374 375 return length / bs->dev->blocklen; 376 } 377 378 static inline uint64_t 379 _spdk_bs_dev_byte_to_lba(struct spdk_bs_dev *bs_dev, uint64_t length) 380 { 381 assert(length % bs_dev->blocklen == 0); 382 383 return length / bs_dev->blocklen; 384 } 385 386 static inline uint64_t 387 _spdk_bs_lba_to_byte(struct spdk_blob_store *bs, uint64_t lba) 388 { 389 return lba * bs->dev->blocklen; 390 } 391 392 static inline uint64_t 393 _spdk_bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page) 394 { 395 return page * SPDK_BS_PAGE_SIZE / bs->dev->blocklen; 396 } 397 398 static inline uint64_t 399 _spdk_bs_dev_page_to_lba(struct spdk_bs_dev *bs_dev, uint64_t page) 400 { 401 return page * SPDK_BS_PAGE_SIZE / bs_dev->blocklen; 402 } 403 404 static inline uint32_t 405 _spdk_bs_lba_to_page(struct spdk_blob_store *bs, uint64_t lba) 406 { 407 uint64_t lbas_per_page; 408 409 lbas_per_page = SPDK_BS_PAGE_SIZE / bs->dev->blocklen; 410 411 assert(lba % lbas_per_page == 0); 412 413 return lba / lbas_per_page; 414 } 415 416 static inline uint64_t 417 _spdk_bs_dev_lba_to_page(struct spdk_bs_dev *bs_dev, uint64_t lba) 418 { 419 uint64_t lbas_per_page; 420 421 lbas_per_page = SPDK_BS_PAGE_SIZE / bs_dev->blocklen; 422 423 assert(lba % lbas_per_page == 0); 424 425 return lba / lbas_per_page; 426 } 427 428 static inline uint64_t 429 _spdk_bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster) 430 { 431 return cluster * bs->pages_per_cluster; 432 } 433 434 static inline uint32_t 435 _spdk_bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page) 436 { 437 assert(page % bs->pages_per_cluster == 0); 438 439 return page / bs->pages_per_cluster; 440 } 441 442 static inline uint64_t 443 _spdk_bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster) 444 { 445 return cluster * (bs->cluster_sz / bs->dev->blocklen); 446 } 447 448 static inline uint32_t 449 _spdk_bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba) 450 { 451 assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0); 452 453 return lba / (bs->cluster_sz / bs->dev->blocklen); 454 } 455 456 static inline uint64_t 457 _spdk_bs_blob_lba_to_back_dev_lba(struct spdk_blob *blob, uint64_t lba) 458 { 459 return lba * blob->bs->dev->blocklen / blob->back_bs_dev->blocklen; 460 } 461 462 static inline uint64_t 463 _spdk_bs_blob_lba_from_back_dev_lba(struct spdk_blob *blob, uint64_t lba) 464 { 465 return lba * blob->back_bs_dev->blocklen / blob->bs->dev->blocklen; 466 } 467 468 /* End basic conversions */ 469 470 static inline uint32_t 471 _spdk_bs_blobid_to_page(spdk_blob_id id) 472 { 473 return id & 0xFFFFFFFF; 474 } 475 476 /* The blob id is a 64 bit number. The lower 32 bits are the page_idx. The upper 477 * 32 bits are not currently used. Stick a 1 there just to catch bugs where the 478 * code assumes blob id == page_idx. 479 */ 480 static inline spdk_blob_id 481 _spdk_bs_page_to_blobid(uint32_t page_idx) 482 { 483 return SPDK_BLOB_BLOBID_HIGH_BIT | page_idx; 484 } 485 486 /* Given a page offset into a blob, look up the LBA for the 487 * start of that page. 488 */ 489 static inline uint64_t 490 _spdk_bs_blob_page_to_lba(struct spdk_blob *blob, uint32_t page) 491 { 492 uint64_t lba; 493 uint32_t pages_per_cluster; 494 495 pages_per_cluster = blob->bs->pages_per_cluster; 496 497 assert(page < blob->active.num_clusters * pages_per_cluster); 498 499 lba = blob->active.clusters[page / pages_per_cluster]; 500 lba += _spdk_bs_page_to_lba(blob->bs, page % pages_per_cluster); 501 502 return lba; 503 } 504 505 /* Given a page offset into a blob, look up the number of pages until the 506 * next cluster boundary. 507 */ 508 static inline uint32_t 509 _spdk_bs_num_pages_to_cluster_boundary(struct spdk_blob *blob, uint32_t page) 510 { 511 uint32_t pages_per_cluster; 512 513 pages_per_cluster = blob->bs->pages_per_cluster; 514 515 return pages_per_cluster - (page % pages_per_cluster); 516 } 517 518 /* Given a page offset into a blob, look up the number of pages into blob to beginning of current cluster */ 519 static inline uint32_t 520 _spdk_bs_page_to_cluster_start(struct spdk_blob *blob, uint32_t page) 521 { 522 uint32_t pages_per_cluster; 523 524 pages_per_cluster = blob->bs->pages_per_cluster; 525 526 return page - (page % pages_per_cluster); 527 } 528 529 /* Given a page offset into a blob, look up if it is from allocated cluster. */ 530 static inline bool 531 _spdk_bs_page_is_allocated(struct spdk_blob *blob, uint32_t page) 532 { 533 uint64_t lba; 534 uint32_t pages_per_cluster; 535 536 pages_per_cluster = blob->bs->pages_per_cluster; 537 538 assert(page < blob->active.num_clusters * pages_per_cluster); 539 540 lba = blob->active.clusters[page / pages_per_cluster]; 541 542 if (lba == 0) { 543 assert(spdk_blob_is_thin_provisioned(blob)); 544 return false; 545 } else { 546 return true; 547 } 548 } 549 550 #endif 551