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