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_MAX_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 /* The disk state is being synchronized with the current 106 * blob state. 107 */ 108 SPDK_BLOB_STATE_SYNCING, 109 }; 110 111 struct spdk_blob { 112 struct spdk_blob_store *bs; 113 114 uint32_t open_ref; 115 116 spdk_blob_id id; 117 118 enum spdk_blob_state state; 119 120 /* Two copies of the mutable data. One is a version 121 * that matches the last known data on disk (clean). 122 * The other (active) is the current data. Syncing 123 * a blob makes the clean match the active. 124 */ 125 struct spdk_blob_mut_data clean; 126 struct spdk_blob_mut_data active; 127 128 bool invalid; 129 bool data_ro; 130 bool md_ro; 131 132 uint64_t invalid_flags; 133 uint64_t data_ro_flags; 134 uint64_t md_ro_flags; 135 136 /* TODO: The xattrs are mutable, but we don't want to be 137 * copying them unecessarily. Figure this out. 138 */ 139 TAILQ_HEAD(, spdk_xattr) xattrs; 140 141 TAILQ_ENTRY(spdk_blob) link; 142 }; 143 144 struct spdk_blob_store { 145 uint64_t md_start; /* Offset from beginning of disk, in pages */ 146 uint32_t md_len; /* Count, in pages */ 147 148 struct { 149 uint32_t max_md_ops; 150 struct spdk_io_channel *md_channel; 151 } md_target; 152 153 struct { 154 uint32_t max_channel_ops; 155 } io_target; 156 157 158 struct spdk_bs_dev *dev; 159 160 struct spdk_bit_array *used_md_pages; 161 struct spdk_bit_array *used_clusters; 162 163 uint32_t cluster_sz; 164 uint64_t total_clusters; 165 uint64_t total_data_clusters; 166 uint64_t num_free_clusters; 167 uint32_t pages_per_cluster; 168 169 spdk_blob_id super_blob; 170 struct spdk_bs_type bstype; 171 172 struct spdk_bs_cpl unload_cpl; 173 int unload_err; 174 175 TAILQ_HEAD(, spdk_blob) blobs; 176 }; 177 178 struct spdk_bs_channel { 179 struct spdk_bs_request_set *req_mem; 180 TAILQ_HEAD(, spdk_bs_request_set) reqs; 181 182 struct spdk_blob_store *bs; 183 184 struct spdk_bs_dev *dev; 185 struct spdk_io_channel *dev_channel; 186 }; 187 188 /** operation type */ 189 enum spdk_blob_op_type { 190 SPDK_BLOB_WRITE, 191 SPDK_BLOB_READ, 192 SPDK_BLOB_UNMAP, 193 SPDK_BLOB_WRITE_ZEROES, 194 }; 195 196 /* On-Disk Data Structures 197 * 198 * The following data structures exist on disk. 199 */ 200 #define SPDK_BS_INITIAL_VERSION 1 201 #define SPDK_BS_VERSION 3 /* current version */ 202 203 #pragma pack(push, 1) 204 205 #define SPDK_MD_MASK_TYPE_USED_PAGES 0 206 #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1 207 208 struct spdk_bs_md_mask { 209 uint8_t type; 210 uint32_t length; /* In bits */ 211 uint8_t mask[0]; 212 }; 213 214 #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0 215 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT 1 216 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2 217 #define SPDK_MD_DESCRIPTOR_TYPE_FLAGS 3 218 219 struct spdk_blob_md_descriptor_xattr { 220 uint8_t type; 221 uint32_t length; 222 223 uint16_t name_length; 224 uint16_t value_length; 225 226 char name[0]; 227 /* String name immediately followed by string value. */ 228 }; 229 230 struct spdk_blob_md_descriptor_extent { 231 uint8_t type; 232 uint32_t length; 233 234 struct { 235 uint32_t cluster_idx; 236 uint32_t length; /* In units of clusters */ 237 } extents[0]; 238 }; 239 240 /* 241 * As new flags are defined, these values will be updated to reflect the 242 * mask of all flag values understood by this application. 243 */ 244 #define SPDK_BLOB_INVALID_FLAGS_MASK 0 245 #define SPDK_BLOB_DATA_RO_FLAGS_MASK 0 246 #define SPDK_BLOB_MD_RO_FLAGS_MASK 0 247 248 struct spdk_blob_md_descriptor_flags { 249 uint8_t type; 250 uint32_t length; 251 252 /* 253 * If a flag in invalid_flags is set that the application is not aware of, 254 * it will not allow the blob to be opened. 255 */ 256 uint64_t invalid_flags; 257 258 /* 259 * If a flag in data_ro_flags is set that the application is not aware of, 260 * allow the blob to be opened in data_read_only and md_read_only mode. 261 */ 262 uint64_t data_ro_flags; 263 264 /* 265 * If a flag in md_ro_flags is set the the application is not aware of, 266 * allow the blob to be opened in md_read_only mode. 267 */ 268 uint64_t md_ro_flags; 269 }; 270 271 struct spdk_blob_md_descriptor { 272 uint8_t type; 273 uint32_t length; 274 }; 275 276 #define SPDK_INVALID_MD_PAGE UINT32_MAX 277 278 struct spdk_blob_md_page { 279 spdk_blob_id id; 280 281 uint32_t sequence_num; 282 uint32_t reserved0; 283 284 /* Descriptors here */ 285 uint8_t descriptors[4072]; 286 287 uint32_t next; 288 uint32_t crc; 289 }; 290 #define SPDK_BS_PAGE_SIZE 0x1000 291 SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size"); 292 293 #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB" 294 295 struct spdk_bs_super_block { 296 uint8_t signature[8]; 297 uint32_t version; 298 uint32_t length; 299 uint32_t clean; /* If there was a clean shutdown, this is 1. */ 300 spdk_blob_id super_blob; 301 302 uint32_t cluster_size; /* In bytes */ 303 304 uint32_t used_page_mask_start; /* Offset from beginning of disk, in pages */ 305 uint32_t used_page_mask_len; /* Count, in pages */ 306 307 uint32_t used_cluster_mask_start; /* Offset from beginning of disk, in pages */ 308 uint32_t used_cluster_mask_len; /* Count, in pages */ 309 310 uint32_t md_start; /* Offset from beginning of disk, in pages */ 311 uint32_t md_len; /* Count, in pages */ 312 313 struct spdk_bs_type bstype; /* blobstore type */ 314 315 uint8_t reserved[4020]; 316 uint32_t crc; 317 }; 318 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size"); 319 320 #pragma pack(pop) 321 322 /* Unit Conversions 323 * 324 * The blobstore works with several different units: 325 * - Byte: Self explanatory 326 * - LBA: The logical blocks on the backing storage device. 327 * - Page: The read/write units of blobs and metadata. This is 328 * an offset into a blob in units of 4KiB. 329 * - Cluster Index: The disk is broken into a sequential list of 330 * clusters. This is the offset from the beginning. 331 * 332 * NOTE: These conversions all act on simple magnitudes, not with any sort 333 * of knowledge about the blobs themselves. For instance, converting 334 * a page to an lba with the conversion function below simply converts 335 * a number of pages to an equivalent number of lbas, but that 336 * lba certainly isn't the right lba that corresponds to a page offset 337 * for a particular blob. 338 */ 339 static inline uint64_t 340 _spdk_bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length) 341 { 342 assert(length % bs->dev->blocklen == 0); 343 344 return length / bs->dev->blocklen; 345 } 346 347 static inline uint64_t 348 _spdk_bs_lba_to_byte(struct spdk_blob_store *bs, uint64_t lba) 349 { 350 return lba * bs->dev->blocklen; 351 } 352 353 static inline uint64_t 354 _spdk_bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page) 355 { 356 return page * SPDK_BS_PAGE_SIZE / bs->dev->blocklen; 357 } 358 359 static inline uint32_t 360 _spdk_bs_lba_to_page(struct spdk_blob_store *bs, uint64_t lba) 361 { 362 uint64_t lbas_per_page; 363 364 lbas_per_page = SPDK_BS_PAGE_SIZE / bs->dev->blocklen; 365 366 assert(lba % lbas_per_page == 0); 367 368 return lba / lbas_per_page; 369 } 370 371 static inline uint64_t 372 _spdk_bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster) 373 { 374 return cluster * bs->pages_per_cluster; 375 } 376 377 static inline uint32_t 378 _spdk_bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page) 379 { 380 assert(page % bs->pages_per_cluster == 0); 381 382 return page / bs->pages_per_cluster; 383 } 384 385 static inline uint64_t 386 _spdk_bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster) 387 { 388 return cluster * (bs->cluster_sz / bs->dev->blocklen); 389 } 390 391 static inline uint32_t 392 _spdk_bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba) 393 { 394 assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0); 395 396 return lba / (bs->cluster_sz / bs->dev->blocklen); 397 } 398 399 /* End basic conversions */ 400 401 static inline uint32_t 402 _spdk_bs_blobid_to_page(spdk_blob_id id) 403 { 404 return id & 0xFFFFFFFF; 405 } 406 407 /* The blob id is a 64 bit number. The lower 32 bits are the page_idx. The upper 408 * 32 bits are not currently used. Stick a 1 there just to catch bugs where the 409 * code assumes blob id == page_idx. 410 */ 411 static inline spdk_blob_id 412 _spdk_bs_page_to_blobid(uint32_t page_idx) 413 { 414 return SPDK_BLOB_BLOBID_HIGH_BIT | page_idx; 415 } 416 417 /* Given a page offset into a blob, look up the LBA for the 418 * start of that page. 419 */ 420 static inline uint64_t 421 _spdk_bs_blob_page_to_lba(struct spdk_blob *blob, uint32_t page) 422 { 423 uint64_t lba; 424 uint32_t pages_per_cluster; 425 426 pages_per_cluster = blob->bs->pages_per_cluster; 427 428 assert(page < blob->active.num_clusters * pages_per_cluster); 429 430 lba = blob->active.clusters[page / pages_per_cluster]; 431 lba += _spdk_bs_page_to_lba(blob->bs, page % pages_per_cluster); 432 433 return lba; 434 } 435 436 /* Given a page offset into a blob, look up the number of pages until the 437 * next cluster boundary. 438 */ 439 static inline uint32_t 440 _spdk_bs_num_pages_to_cluster_boundary(struct spdk_blob *blob, uint32_t page) 441 { 442 uint32_t pages_per_cluster; 443 444 pages_per_cluster = blob->bs->pages_per_cluster; 445 446 return pages_per_cluster - (page % pages_per_cluster); 447 } 448 449 #endif 450