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