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 /* In Memory Data Structures 43 * 44 * The following data structures exist only in memory. 45 */ 46 47 #define SPDK_BLOB_OPTS_CLUSTER_SZ (1024 * 1024) 48 #define SPDK_BLOB_OPTS_NUM_MD_PAGES UINT32_MAX 49 #define SPDK_BLOB_OPTS_MAX_MD_OPS 32 50 51 struct spdk_xattr { 52 /* TODO: reorder for best packing */ 53 uint32_t index; 54 char *name; 55 void *value; 56 uint16_t value_len; 57 TAILQ_ENTRY(spdk_xattr) link; 58 }; 59 60 /* The mutable part of the blob data that is sync'd to 61 * disk. The data in here is both mutable and persistent. 62 */ 63 struct spdk_blob_mut_data { 64 /* Number of data clusters in the blob */ 65 uint64_t num_clusters; 66 67 /* Array LBAs that are the beginning of a cluster, in 68 * the order they appear in the blob. 69 */ 70 uint64_t *clusters; 71 72 /* The size of the clusters array. This is greater than or 73 * equal to 'num_clusters'. 74 */ 75 size_t cluster_array_size; 76 77 /* Number of metadata pages */ 78 uint32_t num_pages; 79 80 /* Array of page offsets into the metadata region, in 81 * the order of the metadata page sequence. 82 */ 83 uint32_t *pages; 84 }; 85 86 enum spdk_blob_state { 87 /* The blob in-memory version does not match the on-disk 88 * version. 89 */ 90 SPDK_BLOB_STATE_DIRTY, 91 92 /* The blob in memory version of the blob matches the on disk 93 * version. 94 */ 95 SPDK_BLOB_STATE_CLEAN, 96 97 /* The in-memory state being synchronized with the on-disk 98 * blob state. */ 99 SPDK_BLOB_STATE_LOADING, 100 101 /* The disk state is being synchronized with the current 102 * blob state. 103 */ 104 SPDK_BLOB_STATE_SYNCING, 105 }; 106 107 struct spdk_blob { 108 struct spdk_blob_store *bs; 109 110 uint32_t open_ref; 111 112 spdk_blob_id id; 113 114 enum spdk_blob_state state; 115 116 /* Two copies of the mutable data. One is a version 117 * that matches the last known data on disk (clean). 118 * The other (active) is the current data. Syncing 119 * a blob makes the clean match the active. 120 */ 121 struct spdk_blob_mut_data clean; 122 struct spdk_blob_mut_data active; 123 124 /* TODO: The xattrs are mutable, but we don't want to be 125 * copying them unecessarily. Figure this out. 126 */ 127 TAILQ_HEAD(, spdk_xattr) xattrs; 128 129 TAILQ_ENTRY(spdk_blob) link; 130 }; 131 132 struct spdk_blob_store { 133 uint64_t md_start; /* Offset from beginning of disk, in pages */ 134 uint32_t md_len; /* Count, in pages */ 135 struct spdk_io_channel *md_channel; 136 137 struct spdk_bs_dev *dev; 138 139 struct spdk_bit_array *used_md_pages; 140 struct spdk_bit_array *used_clusters; 141 142 uint32_t cluster_sz; 143 uint64_t total_clusters; 144 uint64_t num_free_clusters; 145 uint32_t pages_per_cluster; 146 147 uint32_t max_md_ops; 148 149 spdk_blob_id super_blob; 150 151 TAILQ_HEAD(, spdk_blob) blobs; 152 }; 153 154 struct spdk_bs_channel { 155 struct spdk_bs_request_set *req_mem; 156 TAILQ_HEAD(, spdk_bs_request_set) reqs; 157 158 struct spdk_blob_store *bs; 159 160 struct spdk_bs_dev *dev; 161 struct spdk_io_channel *dev_channel; 162 }; 163 164 /* On-Disk Data Structures 165 * 166 * The following data structures exist on disk. 167 */ 168 #define SPDK_BS_VERSION 1 169 170 #pragma pack(push, 1) 171 172 #define SPDK_MD_MASK_TYPE_USED_PAGES 0 173 #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1 174 175 struct spdk_bs_md_mask { 176 uint8_t type; 177 uint32_t length; /* In bits */ 178 uint8_t mask[0]; 179 }; 180 181 #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0 182 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT 1 183 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2 184 185 struct spdk_blob_md_descriptor_xattr { 186 uint8_t type; 187 uint32_t length; 188 189 uint16_t name_length; 190 uint16_t value_length; 191 192 char name[0]; 193 /* String name immediately followed by string value. */ 194 }; 195 196 struct spdk_blob_md_descriptor_extent { 197 uint8_t type; 198 uint32_t length; 199 200 struct { 201 uint32_t cluster_idx; 202 uint32_t length; /* In units of clusters */ 203 } extents[0]; 204 }; 205 206 struct spdk_blob_md_descriptor { 207 uint8_t type; 208 uint32_t length; 209 }; 210 211 #define SPDK_INVALID_MD_PAGE UINT32_MAX 212 213 struct spdk_blob_md_page { 214 spdk_blob_id id; 215 216 uint32_t sequence_num; 217 uint32_t reserved0; 218 219 /* Descriptors here */ 220 uint8_t descriptors[4072]; 221 222 uint32_t next; 223 uint32_t crc; 224 }; 225 SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_md_page) == 0x1000, "Invalid md page size"); 226 227 #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB" 228 229 struct spdk_bs_super_block { 230 uint8_t signature[8]; 231 uint32_t version; 232 uint32_t length; 233 uint32_t clean; /* If there was a clean shutdown, this is 1. */ 234 spdk_blob_id super_blob; 235 236 uint32_t cluster_size; /* In bytes */ 237 238 uint32_t used_page_mask_start; /* Offset from beginning of disk, in pages */ 239 uint32_t used_page_mask_len; /* Count, in pages */ 240 241 uint32_t used_cluster_mask_start; /* Offset from beginning of disk, in pages */ 242 uint32_t used_cluster_mask_len; /* Count, in pages */ 243 244 uint32_t md_start; /* Offset from beginning of disk, in pages */ 245 uint32_t md_len; /* Count, in pages */ 246 247 uint8_t reserved[4040]; 248 }; 249 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size"); 250 251 #pragma pack(pop) 252 253 /* Unit Conversions 254 * 255 * The blobstore works with several different units: 256 * - Byte: Self explanatory 257 * - LBA: The logical blocks on the backing storage device. 258 * - Page: The read/write units of blobs and metadata. This is 259 * an offset into a blob in units of 4KiB. 260 * - Cluster Index: The disk is broken into a sequential list of 261 * clusters. This is the offset from the beginning. 262 * 263 * NOTE: These conversions all act on simple magnitudes, not with any sort 264 * of knowledge about the blobs themselves. For instance, converting 265 * a page to an lba with the conversion function below simply converts 266 * a number of pages to an equivalent number of lbas, but that 267 * lba certainly isn't the right lba that corresponds to a page offset 268 * for a particular blob. 269 */ 270 static inline uint64_t 271 _spdk_bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length) 272 { 273 assert(length % bs->dev->blocklen == 0); 274 275 return length / bs->dev->blocklen; 276 } 277 278 static inline uint64_t 279 _spdk_bs_lba_to_byte(struct spdk_blob_store *bs, uint64_t lba) 280 { 281 return lba * bs->dev->blocklen; 282 } 283 284 static inline uint64_t 285 _spdk_bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page) 286 { 287 return page * sizeof(struct spdk_blob_md_page) / bs->dev->blocklen; 288 } 289 290 static inline uint32_t 291 _spdk_bs_lba_to_page(struct spdk_blob_store *bs, uint64_t lba) 292 { 293 uint64_t lbas_per_page; 294 295 lbas_per_page = sizeof(struct spdk_blob_md_page) / bs->dev->blocklen; 296 297 assert(lba % lbas_per_page == 0); 298 299 return lba / lbas_per_page; 300 } 301 302 static inline uint64_t 303 _spdk_bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster) 304 { 305 return cluster * bs->pages_per_cluster; 306 } 307 308 static inline uint32_t 309 _spdk_bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page) 310 { 311 assert(page % bs->pages_per_cluster == 0); 312 313 return page / bs->pages_per_cluster; 314 } 315 316 static inline uint64_t 317 _spdk_bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster) 318 { 319 return cluster * (bs->cluster_sz / bs->dev->blocklen); 320 } 321 322 static inline uint32_t 323 _spdk_bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba) 324 { 325 assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0); 326 327 return lba / (bs->cluster_sz / bs->dev->blocklen); 328 } 329 330 /* End basic conversions */ 331 332 static inline uint32_t 333 _spdk_bs_blobid_to_page(spdk_blob_id id) 334 { 335 return id & 0xFFFFFFFF; 336 } 337 338 /* Given a page offset into a blob, look up the LBA for the 339 * start of that page. 340 */ 341 static inline uint64_t 342 _spdk_bs_blob_page_to_lba(struct spdk_blob *blob, uint32_t page) 343 { 344 uint64_t lba; 345 uint32_t pages_per_cluster; 346 347 pages_per_cluster = blob->bs->pages_per_cluster; 348 349 assert(page < blob->active.num_clusters * pages_per_cluster); 350 351 lba = blob->active.clusters[page / pages_per_cluster]; 352 lba += _spdk_bs_page_to_lba(blob->bs, page % pages_per_cluster); 353 354 return lba; 355 } 356 357 /* Given a page offset into a blob, look up the number of pages until the 358 * next cluster boundary. 359 */ 360 static inline uint32_t 361 _spdk_bs_num_pages_to_cluster_boundary(struct spdk_blob *blob, uint32_t page) 362 { 363 uint32_t pages_per_cluster; 364 365 pages_per_cluster = blob->bs->pages_per_cluster; 366 367 return pages_per_cluster - (page % pages_per_cluster); 368 } 369 370 #endif 371