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