xref: /spdk/lib/blob/blobstore.h (revision 712a3f69d32632bf6c862f00200f7f437d3f7529)
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 	/* Number of data clusters retrived from extent table,
170 	 * that many have to be read from extent pages. */
171 	uint64_t	remaining_clusters_in_et;
172 };
173 
174 struct spdk_blob_store {
175 	uint64_t			md_start; /* Offset from beginning of disk, in pages */
176 	uint32_t			md_len; /* Count, in pages */
177 
178 	struct spdk_io_channel		*md_channel;
179 	uint32_t			max_channel_ops;
180 
181 	struct spdk_thread		*md_thread;
182 
183 	struct spdk_bs_dev		*dev;
184 
185 	struct spdk_bit_array		*used_md_pages;
186 	struct spdk_bit_array		*used_clusters;
187 	struct spdk_bit_array		*used_blobids;
188 
189 	pthread_mutex_t			used_clusters_mutex;
190 
191 	uint32_t			cluster_sz;
192 	uint64_t			total_clusters;
193 	uint64_t			total_data_clusters;
194 	uint64_t			num_free_clusters;
195 	uint64_t			pages_per_cluster;
196 	uint32_t			io_unit_size;
197 
198 	spdk_blob_id			super_blob;
199 	struct spdk_bs_type		bstype;
200 
201 	struct spdk_bs_cpl		unload_cpl;
202 	int				unload_err;
203 
204 	TAILQ_HEAD(, spdk_blob)		blobs;
205 	TAILQ_HEAD(, spdk_blob_list)	snapshots;
206 
207 	bool                            clean;
208 };
209 
210 struct spdk_bs_channel {
211 	struct spdk_bs_request_set	*req_mem;
212 	TAILQ_HEAD(, spdk_bs_request_set) reqs;
213 
214 	struct spdk_blob_store		*bs;
215 
216 	struct spdk_bs_dev		*dev;
217 	struct spdk_io_channel		*dev_channel;
218 
219 	TAILQ_HEAD(, spdk_bs_request_set) need_cluster_alloc;
220 	TAILQ_HEAD(, spdk_bs_request_set) queued_io;
221 };
222 
223 /** operation type */
224 enum spdk_blob_op_type {
225 	SPDK_BLOB_WRITE,
226 	SPDK_BLOB_READ,
227 	SPDK_BLOB_UNMAP,
228 	SPDK_BLOB_WRITE_ZEROES,
229 	SPDK_BLOB_WRITEV,
230 	SPDK_BLOB_READV,
231 };
232 
233 /* back bs_dev */
234 
235 #define BLOB_SNAPSHOT "SNAP"
236 #define SNAPSHOT_IN_PROGRESS "SNAPTMP"
237 #define SNAPSHOT_PENDING_REMOVAL "SNAPRM"
238 
239 struct spdk_blob_bs_dev {
240 	struct spdk_bs_dev bs_dev;
241 	struct spdk_blob *blob;
242 };
243 
244 /* On-Disk Data Structures
245  *
246  * The following data structures exist on disk.
247  */
248 #define SPDK_BS_INITIAL_VERSION 1
249 #define SPDK_BS_VERSION 3 /* current version */
250 
251 #pragma pack(push, 1)
252 
253 #define SPDK_MD_MASK_TYPE_USED_PAGES 0
254 #define SPDK_MD_MASK_TYPE_USED_CLUSTERS 1
255 #define SPDK_MD_MASK_TYPE_USED_BLOBIDS 2
256 
257 struct spdk_bs_md_mask {
258 	uint8_t		type;
259 	uint32_t	length; /* In bits */
260 	uint8_t		mask[0];
261 };
262 
263 #define SPDK_MD_DESCRIPTOR_TYPE_PADDING 0
264 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR 2
265 #define SPDK_MD_DESCRIPTOR_TYPE_FLAGS 3
266 #define SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL 4
267 
268 /* Following descriptors define cluster layout in a blob.
269  * EXTENT_RLE cannot be present in blobs metadata,
270  * at the same time as EXTENT_TABLE and EXTENT_PAGE descriptors. */
271 
272 /* EXTENT_RLE descriptor holds an array of LBA that points to
273  * beginning of allocated clusters. The array is run-length encoded,
274  * with 0's being unallocated clusters. It is part of serialized
275  * metadata chain for a blob. */
276 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE 1
277 /* EXTENT_TABLE descriptor holds array of md page offsets that
278  * point to pages with EXTENT_PAGE descriptor. The 0's in the array
279  * are run-length encoded, non-zero values are unallocated pages.
280  * It is part of serialized metadata chain for a blob. */
281 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE 5
282 /* EXTENT_PAGE descriptor holds an array of LBAs that point to
283  * beginning of allocated clusters. The array is run-length encoded,
284  * with 0's being unallocated clusters. It is NOT part of
285  * serialized metadata chain for a blob. */
286 #define SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE 6
287 
288 struct spdk_blob_md_descriptor_xattr {
289 	uint8_t		type;
290 	uint32_t	length;
291 
292 	uint16_t	name_length;
293 	uint16_t	value_length;
294 
295 	char		name[0];
296 	/* String name immediately followed by string value. */
297 };
298 
299 struct spdk_blob_md_descriptor_extent_rle {
300 	uint8_t		type;
301 	uint32_t	length;
302 
303 	struct {
304 		uint32_t        cluster_idx;
305 		uint32_t        length; /* In units of clusters */
306 	} extents[0];
307 };
308 
309 struct spdk_blob_md_descriptor_extent_table {
310 	uint8_t		type;
311 	uint32_t	length;
312 
313 	/* Number of data clusters in the blob */
314 	uint64_t	num_clusters;
315 
316 	struct {
317 		uint32_t	page_idx;
318 		uint32_t	num_pages; /* In units of pages */
319 	} extent_page[0];
320 };
321 
322 struct spdk_blob_md_descriptor_extent_page {
323 	uint8_t		type;
324 	uint32_t	length;
325 
326 	/* First cluster index in this extent page */
327 	uint32_t	start_cluster_idx;
328 
329 	uint32_t        cluster_idx[0];
330 };
331 
332 #define SPDK_BLOB_THIN_PROV (1ULL << 0)
333 #define SPDK_BLOB_INTERNAL_XATTR (1ULL << 1)
334 #define SPDK_BLOB_EXTENT_TABLE (1ULL << 2)
335 #define SPDK_BLOB_INVALID_FLAGS_MASK	(SPDK_BLOB_THIN_PROV | SPDK_BLOB_INTERNAL_XATTR | SPDK_BLOB_EXTENT_TABLE)
336 
337 #define SPDK_BLOB_READ_ONLY (1ULL << 0)
338 #define SPDK_BLOB_DATA_RO_FLAGS_MASK	SPDK_BLOB_READ_ONLY
339 
340 #define SPDK_BLOB_CLEAR_METHOD_SHIFT 0
341 #define SPDK_BLOB_CLEAR_METHOD (3ULL << SPDK_BLOB_CLEAR_METHOD_SHIFT)
342 #define SPDK_BLOB_MD_RO_FLAGS_MASK	SPDK_BLOB_CLEAR_METHOD
343 
344 struct spdk_blob_md_descriptor_flags {
345 	uint8_t		type;
346 	uint32_t	length;
347 
348 	/*
349 	 * If a flag in invalid_flags is set that the application is not aware of,
350 	 *  it will not allow the blob to be opened.
351 	 */
352 	uint64_t	invalid_flags;
353 
354 	/*
355 	 * If a flag in data_ro_flags is set that the application is not aware of,
356 	 *  allow the blob to be opened in data_read_only and md_read_only mode.
357 	 */
358 	uint64_t	data_ro_flags;
359 
360 	/*
361 	 * If a flag in md_ro_flags is set the the application is not aware of,
362 	 *  allow the blob to be opened in md_read_only mode.
363 	 */
364 	uint64_t	md_ro_flags;
365 };
366 
367 struct spdk_blob_md_descriptor {
368 	uint8_t		type;
369 	uint32_t	length;
370 };
371 
372 #define SPDK_INVALID_MD_PAGE UINT32_MAX
373 
374 struct spdk_blob_md_page {
375 	spdk_blob_id     id;
376 
377 	uint32_t        sequence_num;
378 	uint32_t	reserved0;
379 
380 	/* Descriptors here */
381 	uint8_t		descriptors[4072];
382 
383 	uint32_t	next;
384 	uint32_t	crc;
385 };
386 #define SPDK_BS_PAGE_SIZE 0x1000
387 SPDK_STATIC_ASSERT(SPDK_BS_PAGE_SIZE == sizeof(struct spdk_blob_md_page), "Invalid md page size");
388 
389 #define SPDK_BS_MAX_DESC_SIZE sizeof(((struct spdk_blob_md_page*)0)->descriptors)
390 
391 /* Maximum number of extents a single Extent Page can fit.
392  * For an SPDK_BS_PAGE_SIZE of 4K SPDK_EXTENTS_PER_EP would be 512. */
393 #define SPDK_EXTENTS_PER_EP_MAX ((SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_extent_page)) / sizeof(uint32_t))
394 #define SPDK_EXTENTS_PER_EP (spdk_align64pow2(SPDK_EXTENTS_PER_EP_MAX + 1) >> 1u)
395 
396 #define SPDK_BS_SUPER_BLOCK_SIG "SPDKBLOB"
397 
398 struct spdk_bs_super_block {
399 	uint8_t		signature[8];
400 	uint32_t        version;
401 	uint32_t        length;
402 	uint32_t	clean; /* If there was a clean shutdown, this is 1. */
403 	spdk_blob_id	super_blob;
404 
405 	uint32_t	cluster_size; /* In bytes */
406 
407 	uint32_t	used_page_mask_start; /* Offset from beginning of disk, in pages */
408 	uint32_t	used_page_mask_len; /* Count, in pages */
409 
410 	uint32_t	used_cluster_mask_start; /* Offset from beginning of disk, in pages */
411 	uint32_t	used_cluster_mask_len; /* Count, in pages */
412 
413 	uint32_t	md_start; /* Offset from beginning of disk, in pages */
414 	uint32_t	md_len; /* Count, in pages */
415 
416 	struct spdk_bs_type	bstype; /* blobstore type */
417 
418 	uint32_t	used_blobid_mask_start; /* Offset from beginning of disk, in pages */
419 	uint32_t	used_blobid_mask_len; /* Count, in pages */
420 
421 	uint64_t        size; /* size of blobstore in bytes */
422 	uint32_t        io_unit_size; /* Size of io unit in bytes */
423 
424 	uint8_t         reserved[4000];
425 	uint32_t	crc;
426 };
427 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block) == 0x1000, "Invalid super block size");
428 
429 #pragma pack(pop)
430 
431 struct spdk_bs_dev *spdk_bs_create_zeroes_dev(void);
432 struct spdk_bs_dev *spdk_bs_create_blob_bs_dev(struct spdk_blob *blob);
433 
434 /* Unit Conversions
435  *
436  * The blobstore works with several different units:
437  * - Byte: Self explanatory
438  * - LBA: The logical blocks on the backing storage device.
439  * - Page: The read/write units of blobs and metadata. This is
440  *         an offset into a blob in units of 4KiB.
441  * - Cluster Index: The disk is broken into a sequential list of
442  *		    clusters. This is the offset from the beginning.
443  *
444  * NOTE: These conversions all act on simple magnitudes, not with any sort
445  *        of knowledge about the blobs themselves. For instance, converting
446  *        a page to an lba with the conversion function below simply converts
447  *        a number of pages to an equivalent number of lbas, but that
448  *        lba certainly isn't the right lba that corresponds to a page offset
449  *        for a particular blob.
450  */
451 static inline uint64_t
452 _spdk_bs_byte_to_lba(struct spdk_blob_store *bs, uint64_t length)
453 {
454 	assert(length % bs->dev->blocklen == 0);
455 
456 	return length / bs->dev->blocklen;
457 }
458 
459 static inline uint64_t
460 _spdk_bs_dev_byte_to_lba(struct spdk_bs_dev *bs_dev, uint64_t length)
461 {
462 	assert(length % bs_dev->blocklen == 0);
463 
464 	return length / bs_dev->blocklen;
465 }
466 
467 static inline uint64_t
468 _spdk_bs_page_to_lba(struct spdk_blob_store *bs, uint64_t page)
469 {
470 	return page * SPDK_BS_PAGE_SIZE / bs->dev->blocklen;
471 }
472 
473 static inline uint64_t
474 _spdk_bs_md_page_to_lba(struct spdk_blob_store *bs, uint32_t page)
475 {
476 	assert(page < bs->md_len);
477 	return _spdk_bs_page_to_lba(bs, page + bs->md_start);
478 }
479 
480 static inline uint64_t
481 _spdk_bs_dev_page_to_lba(struct spdk_bs_dev *bs_dev, uint64_t page)
482 {
483 	return page * SPDK_BS_PAGE_SIZE / bs_dev->blocklen;
484 }
485 
486 static inline uint64_t
487 _spdk_bs_io_unit_per_page(struct spdk_blob_store *bs)
488 {
489 	return SPDK_BS_PAGE_SIZE / bs->io_unit_size;
490 }
491 
492 static inline uint64_t
493 _spdk_bs_io_unit_to_page(struct spdk_blob_store *bs, uint64_t io_unit)
494 {
495 	return io_unit / _spdk_bs_io_unit_per_page(bs);
496 }
497 
498 static inline uint64_t
499 _spdk_bs_cluster_to_page(struct spdk_blob_store *bs, uint32_t cluster)
500 {
501 	return (uint64_t)cluster * bs->pages_per_cluster;
502 }
503 
504 static inline uint32_t
505 _spdk_bs_page_to_cluster(struct spdk_blob_store *bs, uint64_t page)
506 {
507 	assert(page % bs->pages_per_cluster == 0);
508 
509 	return page / bs->pages_per_cluster;
510 }
511 
512 static inline uint64_t
513 _spdk_bs_cluster_to_lba(struct spdk_blob_store *bs, uint32_t cluster)
514 {
515 	return (uint64_t)cluster * (bs->cluster_sz / bs->dev->blocklen);
516 }
517 
518 static inline uint32_t
519 _spdk_bs_lba_to_cluster(struct spdk_blob_store *bs, uint64_t lba)
520 {
521 	assert(lba % (bs->cluster_sz / bs->dev->blocklen) == 0);
522 
523 	return lba / (bs->cluster_sz / bs->dev->blocklen);
524 }
525 
526 static inline uint64_t
527 _spdk_bs_io_unit_to_back_dev_lba(struct spdk_blob *blob, uint64_t io_unit)
528 {
529 	return io_unit * (blob->bs->io_unit_size / blob->back_bs_dev->blocklen);
530 }
531 
532 static inline uint64_t
533 _spdk_bs_back_dev_lba_to_io_unit(struct spdk_blob *blob, uint64_t lba)
534 {
535 	return lba * (blob->back_bs_dev->blocklen / blob->bs->io_unit_size);
536 }
537 
538 static inline uint64_t
539 _spdk_bs_cluster_to_extent_table_id(uint64_t cluster_num)
540 {
541 	return cluster_num / SPDK_EXTENTS_PER_EP;
542 }
543 
544 static inline uint32_t *
545 _spdk_bs_cluster_to_extent_page(struct spdk_blob *blob, uint64_t cluster_num)
546 {
547 	uint64_t extent_table_id = _spdk_bs_cluster_to_extent_table_id(cluster_num);
548 
549 	assert(blob->use_extent_table);
550 	assert(extent_table_id < blob->active.extent_pages_array_size);
551 
552 	return &blob->active.extent_pages[extent_table_id];
553 }
554 
555 /* End basic conversions */
556 
557 static inline uint64_t
558 _spdk_bs_blobid_to_page(spdk_blob_id id)
559 {
560 	return id & 0xFFFFFFFF;
561 }
562 
563 /* The blob id is a 64 bit number. The lower 32 bits are the page_idx. The upper
564  * 32 bits are not currently used. Stick a 1 there just to catch bugs where the
565  * code assumes blob id == page_idx.
566  */
567 static inline spdk_blob_id
568 _spdk_bs_page_to_blobid(uint64_t page_idx)
569 {
570 	if (page_idx > UINT32_MAX) {
571 		return SPDK_BLOBID_INVALID;
572 	}
573 	return SPDK_BLOB_BLOBID_HIGH_BIT | page_idx;
574 }
575 
576 /* Given an io unit offset into a blob, look up the LBA for the
577  * start of that io unit.
578  */
579 static inline uint64_t
580 _spdk_bs_blob_io_unit_to_lba(struct spdk_blob *blob, uint64_t io_unit)
581 {
582 	uint64_t	lba;
583 	uint64_t	pages_per_cluster;
584 	uint64_t	io_units_per_cluster;
585 	uint64_t	io_units_per_page;
586 	uint64_t	page;
587 
588 	page = _spdk_bs_io_unit_to_page(blob->bs, io_unit);
589 
590 	pages_per_cluster = blob->bs->pages_per_cluster;
591 	io_units_per_page = _spdk_bs_io_unit_per_page(blob->bs);
592 	io_units_per_cluster = io_units_per_page * pages_per_cluster;
593 
594 	assert(page < blob->active.num_clusters * pages_per_cluster);
595 
596 	lba = blob->active.clusters[page / pages_per_cluster];
597 	lba += io_unit % io_units_per_cluster;
598 	return lba;
599 }
600 
601 /* Given an io_unit offset into a blob, look up the number of io_units until the
602  * next cluster boundary.
603  */
604 static inline uint32_t
605 _spdk_bs_num_io_units_to_cluster_boundary(struct spdk_blob *blob, uint64_t io_unit)
606 {
607 	uint64_t	io_units_per_cluster;
608 
609 	io_units_per_cluster = _spdk_bs_io_unit_per_page(blob->bs) * blob->bs->pages_per_cluster;
610 
611 	return io_units_per_cluster - (io_unit % io_units_per_cluster);
612 }
613 
614 /* Given a page offset into a blob, look up the number of pages until the
615  * next cluster boundary.
616  */
617 static inline uint32_t
618 _spdk_bs_num_pages_to_cluster_boundary(struct spdk_blob *blob, uint64_t page)
619 {
620 	uint64_t	pages_per_cluster;
621 
622 	pages_per_cluster = blob->bs->pages_per_cluster;
623 
624 	return pages_per_cluster - (page % pages_per_cluster);
625 }
626 
627 /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */
628 static inline uint32_t
629 _spdk_bs_io_unit_to_cluster_start(struct spdk_blob *blob, uint64_t io_unit)
630 {
631 	uint64_t	pages_per_cluster;
632 	uint64_t	page;
633 
634 	pages_per_cluster = blob->bs->pages_per_cluster;
635 	page = _spdk_bs_io_unit_to_page(blob->bs, io_unit);
636 
637 	return page - (page % pages_per_cluster);
638 }
639 
640 /* Given an io_unit offset into a blob, look up the number of pages into blob to beginning of current cluster */
641 static inline uint32_t
642 _spdk_bs_io_unit_to_cluster_number(struct spdk_blob *blob, uint64_t io_unit)
643 {
644 	return (io_unit / _spdk_bs_io_unit_per_page(blob->bs)) / blob->bs->pages_per_cluster;
645 }
646 
647 /* Given an io unit offset into a blob, look up if it is from allocated cluster. */
648 static inline bool
649 _spdk_bs_io_unit_is_allocated(struct spdk_blob *blob, uint64_t io_unit)
650 {
651 	uint64_t	lba;
652 	uint64_t	page;
653 	uint64_t	pages_per_cluster;
654 
655 	pages_per_cluster = blob->bs->pages_per_cluster;
656 	page = _spdk_bs_io_unit_to_page(blob->bs, io_unit);
657 
658 	assert(page < blob->active.num_clusters * pages_per_cluster);
659 
660 	lba = blob->active.clusters[page / pages_per_cluster];
661 
662 	if (lba == 0) {
663 		assert(spdk_blob_is_thin_provisioned(blob));
664 		return false;
665 	} else {
666 		return true;
667 	}
668 }
669 
670 #endif
671