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