xref: /spdk/lib/blob/blobstore.h (revision 2f5c602574a98ede645991abe279a96e19c50196)
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 extent pages */
81 	uint64_t	num_extent_pages;
82 
83 	/* Array of page offsets into the metadata region,
84 	 * containing extents. Can contain entries for not yet
85 	 * allocated pages. */
86 	uint32_t	*extent_pages;
87 
88 	/* The size of the extent page array. This is greater than or
89 	 * equal to 'num_extent_pages'. */
90 	size_t		extent_pages_array_size;
91 
92 	/* Number of metadata pages */
93 	uint32_t	num_pages;
94 
95 	/* Array of page offsets into the metadata region, in
96 	 * the order of the metadata page sequence.
97 	 */
98 	uint32_t	*pages;
99 };
100 
101 enum spdk_blob_state {
102 	/* The blob in-memory version does not match the on-disk
103 	 * version.
104 	 */
105 	SPDK_BLOB_STATE_DIRTY,
106 
107 	/* The blob in memory version of the blob matches the on disk
108 	 * version.
109 	 */
110 	SPDK_BLOB_STATE_CLEAN,
111 
112 	/* The in-memory state being synchronized with the on-disk
113 	 * blob state. */
114 	SPDK_BLOB_STATE_LOADING,
115 };
116 
117 TAILQ_HEAD(spdk_xattr_tailq, spdk_xattr);
118 
119 struct spdk_blob_list {
120 	spdk_blob_id id;
121 	size_t clone_count;
122 	TAILQ_HEAD(, spdk_blob_list) clones;
123 	TAILQ_ENTRY(spdk_blob_list) link;
124 };
125 
126 struct spdk_blob {
127 	struct spdk_blob_store *bs;
128 
129 	uint32_t	open_ref;
130 
131 	spdk_blob_id	id;
132 	spdk_blob_id	parent_id;
133 
134 	enum spdk_blob_state		state;
135 
136 	/* Two copies of the mutable data. One is a version
137 	 * that matches the last known data on disk (clean).
138 	 * The other (active) is the current data. Syncing
139 	 * a blob makes the clean match the active.
140 	 */
141 	struct spdk_blob_mut_data	clean;
142 	struct spdk_blob_mut_data	active;
143 
144 	bool		invalid;
145 	bool		data_ro;
146 	bool		md_ro;
147 
148 	uint64_t	invalid_flags;
149 	uint64_t	data_ro_flags;
150 	uint64_t	md_ro_flags;
151 
152 	struct spdk_bs_dev *back_bs_dev;
153 
154 	/* TODO: The xattrs are mutable, but we don't want to be
155 	 * copying them unnecessarily. Figure this out.
156 	 */
157 	struct spdk_xattr_tailq xattrs;
158 	struct spdk_xattr_tailq xattrs_internal;
159 
160 	TAILQ_ENTRY(spdk_blob) link;
161 
162 	uint32_t frozen_refcnt;
163 	bool locked_operation_in_progress;
164 	enum blob_clear_method clear_method;
165 	bool extent_rle_found;
166 	bool extent_table_found;
167 	bool use_extent_table;
168 
169 	/* A list of pending metadata pending_persists */
170 	TAILQ_HEAD(, spdk_blob_persist_ctx) pending_persists;
171 	TAILQ_HEAD(, spdk_blob_persist_ctx) persists_to_complete;
172 
173 	/* Number of data clusters retrived from extent table,
174 	 * that many have to be read from extent pages. */
175 	uint64_t	remaining_clusters_in_et;
176 };
177 
178 struct spdk_blob_store {
179 	uint64_t			md_start; /* Offset from beginning of disk, in pages */
180 	uint32_t			md_len; /* Count, in pages */
181 
182 	struct spdk_io_channel		*md_channel;
183 	uint32_t			max_channel_ops;
184 
185 	struct spdk_thread		*md_thread;
186 
187 	struct spdk_bs_dev		*dev;
188 
189 	struct spdk_bit_array		*used_md_pages;
190 	struct spdk_bit_pool		*used_clusters;
191 	struct spdk_bit_array		*used_blobids;
192 	struct spdk_bit_array		*open_blobids;
193 
194 	pthread_mutex_t			used_clusters_mutex;
195 
196 	uint32_t			cluster_sz;
197 	uint64_t			total_clusters;
198 	uint64_t			total_data_clusters;
199 	uint64_t			num_free_clusters;
200 	uint64_t			pages_per_cluster;
201 	uint8_t				pages_per_cluster_shift;
202 	uint32_t			io_unit_size;
203 
204 	spdk_blob_id			super_blob;
205 	struct spdk_bs_type		bstype;
206 
207 	struct spdk_bs_cpl		unload_cpl;
208 	int				unload_err;
209 
210 	TAILQ_HEAD(, spdk_blob)		blobs;
211 	TAILQ_HEAD(, spdk_blob_list)	snapshots;
212 
213 	bool                            clean;
214 };
215 
216 struct spdk_bs_channel {
217 	struct spdk_bs_request_set	*req_mem;
218 	TAILQ_HEAD(, spdk_bs_request_set) reqs;
219 
220 	struct spdk_blob_store		*bs;
221 
222 	struct spdk_bs_dev		*dev;
223 	struct spdk_io_channel		*dev_channel;
224 
225 	TAILQ_HEAD(, spdk_bs_request_set) need_cluster_alloc;
226 	TAILQ_HEAD(, spdk_bs_request_set) queued_io;
227 };
228 
229 /** operation type */
230 enum spdk_blob_op_type {
231 	SPDK_BLOB_WRITE,
232 	SPDK_BLOB_READ,
233 	SPDK_BLOB_UNMAP,
234 	SPDK_BLOB_WRITE_ZEROES,
235 	SPDK_BLOB_WRITEV,
236 	SPDK_BLOB_READV,
237 };
238 
239 /* back bs_dev */
240 
241 #define BLOB_SNAPSHOT "SNAP"
242 #define SNAPSHOT_IN_PROGRESS "SNAPTMP"
243 #define SNAPSHOT_PENDING_REMOVAL "SNAPRM"
244 
245 struct spdk_blob_bs_dev {
246 	struct spdk_bs_dev bs_dev;
247 	struct spdk_blob *blob;
248 };
249 
250 /* On-Disk Data Structures
251  *
252  * The following data structures exist on disk.
253  */
254 #define SPDK_BS_INITIAL_VERSION 1
255 #define SPDK_BS_VERSION 3 /* current version */
256 
257 #pragma pack(push, 1)
258 
259 #define SPDK_MD_MASK_TYPE_USED_PAGES 0
260 #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1
261 #define SPDK_MD_MASK_TYPE_USED_BLOBIDS 2
262 
263 struct spdk_bs_md_mask {
264 	uint8_t		type;
265 	uint32_t	length; /* In bits */
266 	uint8_t		mask[0];
267 };
268 
269 #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0
270 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2
271 #define SPDK_MD_DESCRIPTOR_TYPE_FLAGS 3
272 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL 4
273 
274 /* Following descriptors define cluster layout in a blob.
275  * EXTENT_RLE cannot be present in blobs metadata,
276  * at the same time as EXTENT_TABLE and EXTENT_PAGE descriptors. */
277 
278 /* EXTENT_RLE descriptor holds an array of LBA that points to
279  * beginning of allocated clusters. The array is run-length encoded,
280  * with 0's being unallocated clusters. It is part of serialized
281  * metadata chain for a blob. */
282 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE 1
283 /* EXTENT_TABLE descriptor holds array of md page offsets that
284  * point to pages with EXTENT_PAGE descriptor. The 0's in the array
285  * are run-length encoded, non-zero values are unallocated pages.
286  * It is part of serialized metadata chain for a blob. */
287 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE 5
288 /* EXTENT_PAGE descriptor holds an array of LBAs that point to
289  * beginning of allocated clusters. The array is run-length encoded,
290  * with 0's being unallocated clusters. It is NOT part of
291  * serialized metadata chain for a blob. */
292 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE 6
293 
294 struct spdk_blob_md_descriptor_xattr {
295 	uint8_t		type;
296 	uint32_t	length;
297 
298 	uint16_t	name_length;
299 	uint16_t	value_length;
300 
301 	char		name[0];
302 	/* String name immediately followed by string value. */
303 };
304 
305 struct spdk_blob_md_descriptor_extent_rle {
306 	uint8_t		type;
307 	uint32_t	length;
308 
309 	struct {
310 		uint32_t        cluster_idx;
311 		uint32_t        length; /* In units of clusters */
312 	} extents[0];
313 };
314 
315 struct spdk_blob_md_descriptor_extent_table {
316 	uint8_t		type;
317 	uint32_t	length;
318 
319 	/* Number of data clusters in the blob */
320 	uint64_t	num_clusters;
321 
322 	struct {
323 		uint32_t	page_idx;
324 		uint32_t	num_pages; /* In units of pages */
325 	} extent_page[0];
326 };
327 
328 struct spdk_blob_md_descriptor_extent_page {
329 	uint8_t		type;
330 	uint32_t	length;
331 
332 	/* First cluster index in this extent page */
333 	uint32_t	start_cluster_idx;
334 
335 	uint32_t        cluster_idx[0];
336 };
337 
338 #define SPDK_BLOB_THIN_PROV (1ULL << 0)
339 #define SPDK_BLOB_INTERNAL_XATTR (1ULL << 1)
340 #define SPDK_BLOB_EXTENT_TABLE (1ULL << 2)
341 #define SPDK_BLOB_INVALID_FLAGS_MASK	(SPDK_BLOB_THIN_PROV | SPDK_BLOB_INTERNAL_XATTR | SPDK_BLOB_EXTENT_TABLE)
342 
343 #define SPDK_BLOB_READ_ONLY (1ULL << 0)
344 #define SPDK_BLOB_DATA_RO_FLAGS_MASK	SPDK_BLOB_READ_ONLY
345 
346 #define SPDK_BLOB_CLEAR_METHOD_SHIFT 0
347 #define SPDK_BLOB_CLEAR_METHOD (3ULL << SPDK_BLOB_CLEAR_METHOD_SHIFT)
348 #define SPDK_BLOB_MD_RO_FLAGS_MASK	SPDK_BLOB_CLEAR_METHOD
349 
350 struct spdk_blob_md_descriptor_flags {
351 	uint8_t		type;
352 	uint32_t	length;
353 
354 	/*
355 	 * If a flag in invalid_flags is set that the application is not aware of,
356 	 *  it will not allow the blob to be opened.
357 	 */
358 	uint64_t	invalid_flags;
359 
360 	/*
361 	 * If a flag in data_ro_flags is set that the application is not aware of,
362 	 *  allow the blob to be opened in data_read_only and md_read_only mode.
363 	 */
364 	uint64_t	data_ro_flags;
365 
366 	/*
367 	 * If a flag in md_ro_flags is set the the application is not aware of,
368 	 *  allow the blob to be opened in md_read_only mode.
369 	 */
370 	uint64_t	md_ro_flags;
371 };
372 
373 struct spdk_blob_md_descriptor {
374 	uint8_t		type;
375 	uint32_t	length;
376 };
377 
378 #define SPDK_INVALID_MD_PAGE UINT32_MAX
379 
380 struct spdk_blob_md_page {
381 	spdk_blob_id     id;
382 
383 	uint32_t        sequence_num;
384 	uint32_t	reserved0;
385 
386 	/* Descriptors here */
387 	uint8_t		descriptors[4072];
388 
389 	uint32_t	next;
390 	uint32_t	crc;
391 };
392 #define SPDK_BS_PAGE_SIZE 0x1000
393 SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size");
394 
395 #define SPDK_BS_MAX_DESC_SIZE SPDK_SIZEOF_MEMBER(struct spdk_blob_md_page, descriptors)
396 
397 /* Maximum number of extents a single Extent Page can fit.
398  * For an SPDK_BS_PAGE_SIZE of 4K SPDK_EXTENTS_PER_EP would be 512. */
399 #define SPDK_EXTENTS_PER_EP_MAX ((SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_extent_page)) / sizeof(uint32_t))
400 #define SPDK_EXTENTS_PER_EP (spdk_align64pow2(SPDK_EXTENTS_PER_EP_MAX + 1) >> 1u)
401 
402 #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB"
403 
404 struct spdk_bs_super_block {
405 	uint8_t		signature[8];
406 	uint32_t        version;
407 	uint32_t        length;
408 	uint32_t	clean; /* If there was a clean shutdown, this is 1. */
409 	spdk_blob_id	super_blob;
410 
411 	uint32_t	cluster_size; /* In bytes */
412 
413 	uint32_t	used_page_mask_start; /* Offset from beginning of disk, in pages */
414 	uint32_t	used_page_mask_len; /* Count, in pages */
415 
416 	uint32_t	used_cluster_mask_start; /* Offset from beginning of disk, in pages */
417 	uint32_t	used_cluster_mask_len; /* Count, in pages */
418 
419 	uint32_t	md_start; /* Offset from beginning of disk, in pages */
420 	uint32_t	md_len; /* Count, in pages */
421 
422 	struct spdk_bs_type	bstype; /* blobstore type */
423 
424 	uint32_t	used_blobid_mask_start; /* Offset from beginning of disk, in pages */
425 	uint32_t	used_blobid_mask_len; /* Count, in pages */
426 
427 	uint64_t        size; /* size of blobstore in bytes */
428 	uint32_t        io_unit_size; /* Size of io unit in bytes */
429 
430 	uint8_t         reserved[4000];
431 	uint32_t	crc;
432 };
433 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size");
434 
435 #pragma pack(pop)
436 
437 struct spdk_bs_dev *bs_create_zeroes_dev(void);
438 struct spdk_bs_dev *bs_create_blob_bs_dev(struct spdk_blob *blob);
439 
440 /* Unit Conversions
441  *
442  * The blobstore works with several different units:
443  * - Byte: Self explanatory
444  * - LBA: The logical blocks on the backing storage device.
445  * - Page: The read/write units of blobs and metadata. This is
446  *         an offset into a blob in units of 4KiB.
447  * - Cluster Index: The disk is broken into a sequential list of
448  *		    clusters. This is the offset from the beginning.
449  *
450  * NOTE: These conversions all act on simple magnitudes, not with any sort
451  *        of knowledge about the blobs themselves. For instance, converting
452  *        a page to an lba with the conversion function below simply converts
453  *        a number of pages to an equivalent number of lbas, but that
454  *        lba certainly isn't the right lba that corresponds to a page offset
455  *        for a particular blob.
456  */
457 static inline uint64_t
458 bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length)
459 {
460 	assert(length % bs->dev->blocklen == 0);
461 
462 	return length / bs->dev->blocklen;
463 }
464 
465 static inline uint64_t
466 bs_dev_byte_to_lba(struct spdk_bs_dev *bs_dev, uint64_t length)
467 {
468 	assert(length % bs_dev->blocklen == 0);
469 
470 	return length / bs_dev->blocklen;
471 }
472 
473 static inline uint64_t
474 bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page)
475 {
476 	return page * SPDK_BS_PAGE_SIZE / bs->dev->blocklen;
477 }
478 
479 static inline uint64_t
480 bs_md_page_to_lba(struct spdk_blob_store *bs, uint32_t page)
481 {
482 	assert(page < bs->md_len);
483 	return bs_page_to_lba(bs, page + bs->md_start);
484 }
485 
486 static inline uint64_t
487 bs_dev_page_to_lba(struct spdk_bs_dev *bs_dev, uint64_t page)
488 {
489 	return page * SPDK_BS_PAGE_SIZE / bs_dev->blocklen;
490 }
491 
492 static inline uint64_t
493 bs_io_unit_per_page(struct spdk_blob_store *bs)
494 {
495 	return SPDK_BS_PAGE_SIZE / bs->io_unit_size;
496 }
497 
498 static inline uint64_t
499 bs_io_unit_to_page(struct spdk_blob_store *bs, uint64_t io_unit)
500 {
501 	return io_unit / bs_io_unit_per_page(bs);
502 }
503 
504 static inline uint64_t
505 bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster)
506 {
507 	return (uint64_t)cluster * bs->pages_per_cluster;
508 }
509 
510 static inline uint32_t
511 bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page)
512 {
513 	assert(page % bs->pages_per_cluster == 0);
514 
515 	return page / bs->pages_per_cluster;
516 }
517 
518 static inline uint64_t
519 bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster)
520 {
521 	return (uint64_t)cluster * (bs->cluster_sz / bs->dev->blocklen);
522 }
523 
524 static inline uint32_t
525 bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba)
526 {
527 	assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0);
528 
529 	return lba / (bs->cluster_sz / bs->dev->blocklen);
530 }
531 
532 static inline uint64_t
533 bs_io_unit_to_back_dev_lba(struct spdk_blob *blob, uint64_t io_unit)
534 {
535 	return io_unit * (blob->bs->io_unit_size / blob->back_bs_dev->blocklen);
536 }
537 
538 static inline uint64_t
539 bs_back_dev_lba_to_io_unit(struct spdk_blob *blob, uint64_t lba)
540 {
541 	return lba * (blob->back_bs_dev->blocklen / blob->bs->io_unit_size);
542 }
543 
544 static inline uint64_t
545 bs_cluster_to_extent_table_id(uint64_t cluster_num)
546 {
547 	return cluster_num / SPDK_EXTENTS_PER_EP;
548 }
549 
550 static inline uint32_t *
551 bs_cluster_to_extent_page(struct spdk_blob *blob, uint64_t cluster_num)
552 {
553 	uint64_t extent_table_id = bs_cluster_to_extent_table_id(cluster_num);
554 
555 	assert(blob->use_extent_table);
556 	assert(extent_table_id < blob->active.extent_pages_array_size);
557 
558 	return &blob->active.extent_pages[extent_table_id];
559 }
560 
561 /* End basic conversions */
562 
563 static inline uint64_t
564 bs_blobid_to_page(spdk_blob_id id)
565 {
566 	return id & 0xFFFFFFFF;
567 }
568 
569 /* The blob id is a 64 bit number. The lower 32 bits are the page_idx. The upper
570  * 32 bits are not currently used. Stick a 1 there just to catch bugs where the
571  * code assumes blob id == page_idx.
572  */
573 static inline spdk_blob_id
574 bs_page_to_blobid(uint64_t page_idx)
575 {
576 	if (page_idx > UINT32_MAX) {
577 		return SPDK_BLOBID_INVALID;
578 	}
579 	return SPDK_BLOB_BLOBID_HIGH_BIT | page_idx;
580 }
581 
582 /* Given an io unit offset into a blob, look up the LBA for the
583  * start of that io unit.
584  */
585 static inline uint64_t
586 bs_blob_io_unit_to_lba(struct spdk_blob *blob, uint64_t io_unit)
587 {
588 	uint64_t	lba;
589 	uint64_t	pages_per_cluster;
590 	uint8_t		shift;
591 	uint64_t	io_units_per_cluster;
592 	uint64_t	io_units_per_page;
593 	uint64_t	page;
594 
595 	page = bs_io_unit_to_page(blob->bs, io_unit);
596 
597 	pages_per_cluster = blob->bs->pages_per_cluster;
598 	shift = blob->bs->pages_per_cluster_shift;
599 	io_units_per_page = bs_io_unit_per_page(blob->bs);
600 
601 	assert(page < blob->active.num_clusters * pages_per_cluster);
602 
603 	if (shift != 0) {
604 		io_units_per_cluster = io_units_per_page << shift;
605 		lba = blob->active.clusters[page >> shift];
606 	} else {
607 		io_units_per_cluster = io_units_per_page * pages_per_cluster;
608 		lba = blob->active.clusters[page / pages_per_cluster];
609 	}
610 	lba += io_unit % io_units_per_cluster;
611 	return lba;
612 }
613 
614 /* Given an io_unit offset into a blob, look up the number of io_units until the
615  * next cluster boundary.
616  */
617 static inline uint32_t
618 bs_num_io_units_to_cluster_boundary(struct spdk_blob *blob, uint64_t io_unit)
619 {
620 	uint64_t	io_units_per_cluster;
621 	uint8_t         shift = blob->bs->pages_per_cluster_shift;
622 
623 	if (shift != 0) {
624 		io_units_per_cluster = bs_io_unit_per_page(blob->bs) << shift;
625 	} else {
626 		io_units_per_cluster = bs_io_unit_per_page(blob->bs) * blob->bs->pages_per_cluster;
627 	}
628 
629 	return io_units_per_cluster - (io_unit % io_units_per_cluster);
630 }
631 
632 /* Given a page offset into a blob, look up the number of pages until the
633  * next cluster boundary.
634  */
635 static inline uint32_t
636 bs_num_pages_to_cluster_boundary(struct spdk_blob *blob, uint64_t page)
637 {
638 	uint64_t	pages_per_cluster;
639 
640 	pages_per_cluster = blob->bs->pages_per_cluster;
641 
642 	return pages_per_cluster - (page % pages_per_cluster);
643 }
644 
645 /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */
646 static inline uint32_t
647 bs_io_unit_to_cluster_start(struct spdk_blob *blob, uint64_t io_unit)
648 {
649 	uint64_t	pages_per_cluster;
650 	uint64_t	page;
651 
652 	pages_per_cluster = blob->bs->pages_per_cluster;
653 	page = bs_io_unit_to_page(blob->bs, io_unit);
654 
655 	return page - (page % pages_per_cluster);
656 }
657 
658 /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */
659 static inline uint32_t
660 bs_io_unit_to_cluster_number(struct spdk_blob *blob, uint64_t io_unit)
661 {
662 	uint64_t	pages_per_cluster = blob->bs->pages_per_cluster;
663 	uint8_t		shift = blob->bs->pages_per_cluster_shift;
664 	uint32_t	page_offset;
665 
666 	page_offset = io_unit / bs_io_unit_per_page(blob->bs);
667 	if (shift != 0) {
668 		return page_offset >> shift;
669 	} else {
670 		return page_offset / pages_per_cluster;
671 	}
672 }
673 
674 /* Given an io unit offset into a blob, look up if it is from allocated cluster. */
675 static inline bool
676 bs_io_unit_is_allocated(struct spdk_blob *blob, uint64_t io_unit)
677 {
678 	uint64_t	lba;
679 	uint64_t	page;
680 	uint64_t	pages_per_cluster;
681 	uint8_t		shift;
682 
683 	shift = blob->bs->pages_per_cluster_shift;
684 	pages_per_cluster = blob->bs->pages_per_cluster;
685 	page = bs_io_unit_to_page(blob->bs, io_unit);
686 
687 	assert(page < blob->active.num_clusters * pages_per_cluster);
688 
689 	if (shift != 0) {
690 		lba = blob->active.clusters[page >> shift];
691 	} else {
692 		lba = blob->active.clusters[page / pages_per_cluster];
693 	}
694 
695 	if (lba == 0) {
696 		assert(spdk_blob_is_thin_provisioned(blob));
697 		return false;
698 	} else {
699 		return true;
700 	}
701 }
702 
703 #endif
704