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