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