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