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