xref: /spdk/lib/blob/blobstore.c (revision eb8b1e20a9c8a6bc79f32fde8693d2791a74c34d)
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 #include "spdk/stdinc.h"
35 
36 #include "spdk/blob.h"
37 #include "spdk/crc32.h"
38 #include "spdk/env.h"
39 #include "spdk/queue.h"
40 #include "spdk/io_channel.h"
41 #include "spdk/bit_array.h"
42 #include "spdk/likely.h"
43 
44 #include "spdk_internal/log.h"
45 
46 #include "blobstore.h"
47 
48 #define BLOB_CRC32C_INITIAL    0xffffffffUL
49 
50 static inline size_t
51 divide_round_up(size_t num, size_t divisor)
52 {
53 	return (num + divisor - 1) / divisor;
54 }
55 
56 static void
57 _spdk_bs_claim_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
58 {
59 	assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters));
60 	assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == false);
61 	assert(bs->num_free_clusters > 0);
62 
63 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Claiming cluster %u\n", cluster_num);
64 
65 	spdk_bit_array_set(bs->used_clusters, cluster_num);
66 	bs->num_free_clusters--;
67 }
68 
69 static void
70 _spdk_bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
71 {
72 	assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters));
73 	assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == true);
74 	assert(bs->num_free_clusters < bs->total_clusters);
75 
76 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Releasing cluster %u\n", cluster_num);
77 
78 	spdk_bit_array_clear(bs->used_clusters, cluster_num);
79 	bs->num_free_clusters++;
80 }
81 
82 static struct spdk_blob *
83 _spdk_blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
84 {
85 	struct spdk_blob *blob;
86 
87 	blob = calloc(1, sizeof(*blob));
88 	if (!blob) {
89 		return NULL;
90 	}
91 
92 	blob->id = id;
93 	blob->bs = bs;
94 
95 	blob->state = SPDK_BLOB_STATE_DIRTY;
96 	blob->active.num_pages = 1;
97 	blob->active.pages = calloc(1, sizeof(*blob->active.pages));
98 	if (!blob->active.pages) {
99 		free(blob);
100 		return NULL;
101 	}
102 
103 	blob->active.pages[0] = _spdk_bs_blobid_to_page(id);
104 
105 	TAILQ_INIT(&blob->xattrs);
106 
107 	return blob;
108 }
109 
110 static void
111 _spdk_blob_free(struct spdk_blob *blob)
112 {
113 	struct spdk_xattr 	*xattr, *xattr_tmp;
114 
115 	assert(blob != NULL);
116 
117 	free(blob->active.clusters);
118 	free(blob->clean.clusters);
119 	free(blob->active.pages);
120 	free(blob->clean.pages);
121 
122 	TAILQ_FOREACH_SAFE(xattr, &blob->xattrs, link, xattr_tmp) {
123 		TAILQ_REMOVE(&blob->xattrs, xattr, link);
124 		free(xattr->name);
125 		free(xattr->value);
126 		free(xattr);
127 	}
128 
129 	free(blob);
130 }
131 
132 static int
133 _spdk_blob_mark_clean(struct spdk_blob *blob)
134 {
135 	uint64_t *clusters = NULL;
136 	uint32_t *pages = NULL;
137 
138 	assert(blob != NULL);
139 	assert(blob->state == SPDK_BLOB_STATE_LOADING ||
140 	       blob->state == SPDK_BLOB_STATE_SYNCING);
141 
142 	if (blob->active.num_clusters) {
143 		assert(blob->active.clusters);
144 		clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
145 		if (!clusters) {
146 			return -1;
147 		}
148 		memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*clusters));
149 	}
150 
151 	if (blob->active.num_pages) {
152 		assert(blob->active.pages);
153 		pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
154 		if (!pages) {
155 			free(clusters);
156 			return -1;
157 		}
158 		memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*pages));
159 	}
160 
161 	free(blob->clean.clusters);
162 	free(blob->clean.pages);
163 
164 	blob->clean.num_clusters = blob->active.num_clusters;
165 	blob->clean.clusters = blob->active.clusters;
166 	blob->clean.num_pages = blob->active.num_pages;
167 	blob->clean.pages = blob->active.pages;
168 
169 	blob->active.clusters = clusters;
170 	blob->active.pages = pages;
171 
172 	blob->state = SPDK_BLOB_STATE_CLEAN;
173 
174 	return 0;
175 }
176 
177 static void
178 _spdk_blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
179 {
180 	struct spdk_blob_md_descriptor *desc;
181 	size_t	cur_desc = 0;
182 	void *tmp;
183 
184 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
185 	while (cur_desc < sizeof(page->descriptors)) {
186 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
187 			if (desc->length == 0) {
188 				/* If padding and length are 0, this terminates the page */
189 				break;
190 			}
191 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) {
192 			struct spdk_blob_md_descriptor_extent	*desc_extent;
193 			unsigned int				i, j;
194 			unsigned int				cluster_count = blob->active.num_clusters;
195 
196 			desc_extent = (struct spdk_blob_md_descriptor_extent *)desc;
197 
198 			assert(desc_extent->length > 0);
199 			assert(desc_extent->length % sizeof(desc_extent->extents[0]) == 0);
200 
201 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) {
202 				for (j = 0; j < desc_extent->extents[i].length; j++) {
203 					assert(spdk_bit_array_get(blob->bs->used_clusters, desc_extent->extents[i].cluster_idx + j));
204 					cluster_count++;
205 				}
206 			}
207 
208 			assert(cluster_count > 0);
209 			tmp = realloc(blob->active.clusters, cluster_count * sizeof(uint64_t));
210 			assert(tmp != NULL);
211 			blob->active.clusters = tmp;
212 			blob->active.cluster_array_size = cluster_count;
213 
214 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) {
215 				for (j = 0; j < desc_extent->extents[i].length; j++) {
216 					blob->active.clusters[blob->active.num_clusters++] = _spdk_bs_cluster_to_lba(blob->bs,
217 							desc_extent->extents[i].cluster_idx + j);
218 				}
219 			}
220 
221 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
222 			struct spdk_blob_md_descriptor_xattr	*desc_xattr;
223 			struct spdk_xattr 			*xattr;
224 
225 			desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc;
226 
227 			assert(desc_xattr->length == sizeof(desc_xattr->name_length) +
228 			       sizeof(desc_xattr->value_length) +
229 			       desc_xattr->name_length + desc_xattr->value_length);
230 
231 			xattr = calloc(1, sizeof(*xattr));
232 			assert(xattr != NULL);
233 
234 			xattr->name = malloc(desc_xattr->name_length + 1);
235 			assert(xattr->name);
236 			strncpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
237 			xattr->name[desc_xattr->name_length] = '\0';
238 
239 			xattr->value = malloc(desc_xattr->value_length);
240 			assert(xattr->value != NULL);
241 			xattr->value_len = desc_xattr->value_length;
242 			memcpy(xattr->value,
243 			       (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
244 			       desc_xattr->value_length);
245 
246 			TAILQ_INSERT_TAIL(&blob->xattrs, xattr, link);
247 		} else {
248 			/* Error */
249 			break;
250 		}
251 
252 		/* Advance to the next descriptor */
253 		cur_desc += sizeof(*desc) + desc->length;
254 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
255 			break;
256 		}
257 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
258 	}
259 }
260 
261 static int
262 _spdk_blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
263 		 struct spdk_blob *blob)
264 {
265 	const struct spdk_blob_md_page *page;
266 	uint32_t i;
267 
268 	assert(page_count > 0);
269 	assert(pages[0].sequence_num == 0);
270 	assert(blob != NULL);
271 	assert(blob->state == SPDK_BLOB_STATE_LOADING);
272 	assert(blob->active.clusters == NULL);
273 	assert(blob->state == SPDK_BLOB_STATE_LOADING);
274 
275 	/* The blobid provided doesn't match what's in the MD, this can
276 	 * happen for example if a bogus blobid is passed in through open.
277 	 */
278 	if (blob->id != pages[0].id) {
279 		SPDK_ERRLOG("Blobid (%lu) doesn't match what's in metadata (%lu)\n",
280 			    blob->id, pages[0].id);
281 		return -ENOENT;
282 	}
283 
284 	for (i = 0; i < page_count; i++) {
285 		page = &pages[i];
286 
287 		assert(page->id == blob->id);
288 		assert(page->sequence_num == i);
289 
290 		_spdk_blob_parse_page(page, blob);
291 	}
292 
293 	return 0;
294 }
295 
296 static int
297 _spdk_blob_serialize_add_page(const struct spdk_blob *blob,
298 			      struct spdk_blob_md_page **pages,
299 			      uint32_t *page_count,
300 			      struct spdk_blob_md_page **last_page)
301 {
302 	struct spdk_blob_md_page *page;
303 
304 	assert(pages != NULL);
305 	assert(page_count != NULL);
306 
307 	if (*page_count == 0) {
308 		assert(*pages == NULL);
309 		*page_count = 1;
310 		*pages = spdk_dma_malloc(SPDK_BS_PAGE_SIZE,
311 					 SPDK_BS_PAGE_SIZE,
312 					 NULL);
313 	} else {
314 		assert(*pages != NULL);
315 		(*page_count)++;
316 		*pages = spdk_dma_realloc(*pages,
317 					  SPDK_BS_PAGE_SIZE * (*page_count),
318 					  SPDK_BS_PAGE_SIZE,
319 					  NULL);
320 	}
321 
322 	if (*pages == NULL) {
323 		*page_count = 0;
324 		*last_page = NULL;
325 		return -ENOMEM;
326 	}
327 
328 	page = &(*pages)[*page_count - 1];
329 	memset(page, 0, sizeof(*page));
330 	page->id = blob->id;
331 	page->sequence_num = *page_count - 1;
332 	page->next = SPDK_INVALID_MD_PAGE;
333 	*last_page = page;
334 
335 	return 0;
336 }
337 
338 /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
339  * Update required_sz on both success and failure.
340  *
341  */
342 static int
343 _spdk_blob_serialize_xattr(const struct spdk_xattr *xattr,
344 			   uint8_t *buf, size_t buf_sz,
345 			   size_t *required_sz)
346 {
347 	struct spdk_blob_md_descriptor_xattr	*desc;
348 
349 	*required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
350 		       strlen(xattr->name) +
351 		       xattr->value_len;
352 
353 	if (buf_sz < *required_sz) {
354 		return -1;
355 	}
356 
357 	desc = (struct spdk_blob_md_descriptor_xattr *)buf;
358 
359 	desc->type = SPDK_MD_DESCRIPTOR_TYPE_XATTR;
360 	desc->length = sizeof(desc->name_length) +
361 		       sizeof(desc->value_length) +
362 		       strlen(xattr->name) +
363 		       xattr->value_len;
364 	desc->name_length = strlen(xattr->name);
365 	desc->value_length = xattr->value_len;
366 
367 	memcpy(desc->name, xattr->name, desc->name_length);
368 	memcpy((void *)((uintptr_t)desc->name + desc->name_length),
369 	       xattr->value,
370 	       desc->value_length);
371 
372 	return 0;
373 }
374 
375 static void
376 _spdk_blob_serialize_extent(const struct spdk_blob *blob,
377 			    uint64_t start_cluster, uint64_t *next_cluster,
378 			    uint8_t *buf, size_t buf_sz)
379 {
380 	struct spdk_blob_md_descriptor_extent *desc;
381 	size_t cur_sz;
382 	uint64_t i, extent_idx;
383 	uint32_t lba, lba_per_cluster, lba_count;
384 
385 	/* The buffer must have room for at least one extent */
386 	cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->extents[0]);
387 	if (buf_sz < cur_sz) {
388 		*next_cluster = start_cluster;
389 		return;
390 	}
391 
392 	desc = (struct spdk_blob_md_descriptor_extent *)buf;
393 	desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT;
394 
395 	lba_per_cluster = _spdk_bs_cluster_to_lba(blob->bs, 1);
396 
397 	lba = blob->active.clusters[start_cluster];
398 	lba_count = lba_per_cluster;
399 	extent_idx = 0;
400 	for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
401 		if ((lba + lba_count) == blob->active.clusters[i]) {
402 			lba_count += lba_per_cluster;
403 			continue;
404 		}
405 		desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
406 		desc->extents[extent_idx].length = lba_count / lba_per_cluster;
407 		extent_idx++;
408 
409 		cur_sz += sizeof(desc->extents[extent_idx]);
410 
411 		if (buf_sz < cur_sz) {
412 			/* If we ran out of buffer space, return */
413 			desc->length = sizeof(desc->extents[0]) * extent_idx;
414 			*next_cluster = i;
415 			return;
416 		}
417 
418 		lba = blob->active.clusters[i];
419 		lba_count = lba_per_cluster;
420 	}
421 
422 	desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
423 	desc->extents[extent_idx].length = lba_count / lba_per_cluster;
424 	extent_idx++;
425 
426 	desc->length = sizeof(desc->extents[0]) * extent_idx;
427 	*next_cluster = blob->active.num_clusters;
428 
429 	return;
430 }
431 
432 static int
433 _spdk_blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
434 		     uint32_t *page_count)
435 {
436 	struct spdk_blob_md_page		*cur_page;
437 	const struct spdk_xattr			*xattr;
438 	int 					rc;
439 	uint8_t					*buf;
440 	size_t					remaining_sz;
441 	uint64_t				last_cluster;
442 
443 	assert(pages != NULL);
444 	assert(page_count != NULL);
445 	assert(blob != NULL);
446 	assert(blob->state == SPDK_BLOB_STATE_SYNCING);
447 
448 	*pages = NULL;
449 	*page_count = 0;
450 
451 	/* A blob always has at least 1 page, even if it has no descriptors */
452 	rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page);
453 	if (rc < 0) {
454 		return rc;
455 	}
456 
457 	buf = (uint8_t *)cur_page->descriptors;
458 	remaining_sz = sizeof(cur_page->descriptors);
459 
460 	/* Serialize xattrs */
461 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
462 		size_t required_sz = 0;
463 		rc = _spdk_blob_serialize_xattr(xattr,
464 						buf, remaining_sz,
465 						&required_sz);
466 		if (rc < 0) {
467 			/* Need to add a new page to the chain */
468 			rc = _spdk_blob_serialize_add_page(blob, pages, page_count,
469 							   &cur_page);
470 			if (rc < 0) {
471 				spdk_dma_free(*pages);
472 				*pages = NULL;
473 				*page_count = 0;
474 				return rc;
475 			}
476 
477 			buf = (uint8_t *)cur_page->descriptors;
478 			remaining_sz = sizeof(cur_page->descriptors);
479 
480 			/* Try again */
481 			required_sz = 0;
482 			rc = _spdk_blob_serialize_xattr(xattr,
483 							buf, remaining_sz,
484 							&required_sz);
485 
486 			if (rc < 0) {
487 				spdk_dma_free(*pages);
488 				*pages = NULL;
489 				*page_count = 0;
490 				return -1;
491 			}
492 		}
493 
494 		remaining_sz -= required_sz;
495 		buf += required_sz;
496 	}
497 
498 	/* Serialize extents */
499 	last_cluster = 0;
500 	while (last_cluster < blob->active.num_clusters) {
501 		_spdk_blob_serialize_extent(blob, last_cluster, &last_cluster,
502 					    buf, remaining_sz);
503 
504 		if (last_cluster == blob->active.num_clusters) {
505 			break;
506 		}
507 
508 		rc = _spdk_blob_serialize_add_page(blob, pages, page_count,
509 						   &cur_page);
510 		if (rc < 0) {
511 			return rc;
512 		}
513 
514 		buf = (uint8_t *)cur_page->descriptors;
515 		remaining_sz = sizeof(cur_page->descriptors);
516 	}
517 
518 	return 0;
519 }
520 
521 struct spdk_blob_load_ctx {
522 	struct spdk_blob 		*blob;
523 
524 	struct spdk_blob_md_page	*pages;
525 	uint32_t			num_pages;
526 
527 	spdk_bs_sequence_cpl		cb_fn;
528 	void				*cb_arg;
529 };
530 
531 static uint32_t
532 _spdk_blob_md_page_calc_crc(void *page)
533 {
534 	uint32_t		crc;
535 
536 	crc = BLOB_CRC32C_INITIAL;
537 	crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
538 	crc ^= BLOB_CRC32C_INITIAL;
539 
540 	return crc;
541 
542 }
543 
544 static void
545 _spdk_blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
546 {
547 	struct spdk_blob_load_ctx 	*ctx = cb_arg;
548 	struct spdk_blob 		*blob = ctx->blob;
549 	struct spdk_blob_md_page	*page;
550 	int				rc;
551 	uint32_t			crc;
552 
553 	page = &ctx->pages[ctx->num_pages - 1];
554 	crc = _spdk_blob_md_page_calc_crc(page);
555 	if (crc != page->crc) {
556 		SPDK_ERRLOG("Metadata page %d crc mismatch\n", ctx->num_pages);
557 		_spdk_blob_free(blob);
558 		ctx->cb_fn(seq, NULL, -EINVAL);
559 		spdk_dma_free(ctx->pages);
560 		free(ctx);
561 		return;
562 	}
563 
564 	if (page->next != SPDK_INVALID_MD_PAGE) {
565 		uint32_t next_page = page->next;
566 		uint64_t next_lba = _spdk_bs_page_to_lba(blob->bs, blob->bs->md_start + next_page);
567 
568 
569 		assert(next_lba < (blob->bs->md_start + blob->bs->md_len));
570 
571 		/* Read the next page */
572 		ctx->num_pages++;
573 		ctx->pages = spdk_dma_realloc(ctx->pages, (sizeof(*page) * ctx->num_pages),
574 					      sizeof(*page), NULL);
575 		if (ctx->pages == NULL) {
576 			ctx->cb_fn(seq, ctx->cb_arg, -ENOMEM);
577 			free(ctx);
578 			return;
579 		}
580 
581 		spdk_bs_sequence_read(seq, &ctx->pages[ctx->num_pages - 1],
582 				      next_lba,
583 				      _spdk_bs_byte_to_lba(blob->bs, sizeof(*page)),
584 				      _spdk_blob_load_cpl, ctx);
585 		return;
586 	}
587 
588 	/* Parse the pages */
589 	rc = _spdk_blob_parse(ctx->pages, ctx->num_pages, blob);
590 	if (rc) {
591 		_spdk_blob_free(blob);
592 		ctx->cb_fn(seq, NULL, rc);
593 		spdk_dma_free(ctx->pages);
594 		free(ctx);
595 		return;
596 	}
597 
598 	_spdk_blob_mark_clean(blob);
599 
600 	ctx->cb_fn(seq, ctx->cb_arg, rc);
601 
602 	/* Free the memory */
603 	spdk_dma_free(ctx->pages);
604 	free(ctx);
605 }
606 
607 /* Load a blob from disk given a blobid */
608 static void
609 _spdk_blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
610 		spdk_bs_sequence_cpl cb_fn, void *cb_arg)
611 {
612 	struct spdk_blob_load_ctx *ctx;
613 	struct spdk_blob_store *bs;
614 	uint32_t page_num;
615 	uint64_t lba;
616 
617 	assert(blob != NULL);
618 	assert(blob->state == SPDK_BLOB_STATE_CLEAN ||
619 	       blob->state == SPDK_BLOB_STATE_DIRTY);
620 
621 	bs = blob->bs;
622 
623 	ctx = calloc(1, sizeof(*ctx));
624 	if (!ctx) {
625 		cb_fn(seq, cb_arg, -ENOMEM);
626 		return;
627 	}
628 
629 	ctx->blob = blob;
630 	ctx->pages = spdk_dma_realloc(ctx->pages, SPDK_BS_PAGE_SIZE,
631 				      SPDK_BS_PAGE_SIZE, NULL);
632 	if (!ctx->pages) {
633 		free(ctx);
634 		cb_fn(seq, cb_arg, -ENOMEM);
635 		return;
636 	}
637 	ctx->num_pages = 1;
638 	ctx->cb_fn = cb_fn;
639 	ctx->cb_arg = cb_arg;
640 
641 	page_num = _spdk_bs_blobid_to_page(blob->id);
642 	lba = _spdk_bs_page_to_lba(blob->bs, bs->md_start + page_num);
643 
644 	blob->state = SPDK_BLOB_STATE_LOADING;
645 
646 	spdk_bs_sequence_read(seq, &ctx->pages[0], lba,
647 			      _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
648 			      _spdk_blob_load_cpl, ctx);
649 }
650 
651 struct spdk_blob_persist_ctx {
652 	struct spdk_blob 		*blob;
653 
654 	struct spdk_blob_md_page	*pages;
655 
656 	uint64_t			idx;
657 
658 	spdk_bs_sequence_cpl		cb_fn;
659 	void				*cb_arg;
660 };
661 
662 static void
663 _spdk_blob_persist_complete(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
664 {
665 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
666 	struct spdk_blob 		*blob = ctx->blob;
667 
668 	if (bserrno == 0) {
669 		_spdk_blob_mark_clean(blob);
670 	}
671 
672 	/* Call user callback */
673 	ctx->cb_fn(seq, ctx->cb_arg, bserrno);
674 
675 	/* Free the memory */
676 	spdk_dma_free(ctx->pages);
677 	free(ctx);
678 }
679 
680 static void
681 _spdk_blob_persist_unmap_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
682 {
683 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
684 	struct spdk_blob 		*blob = ctx->blob;
685 	struct spdk_blob_store		*bs = blob->bs;
686 	void				*tmp;
687 	size_t				i;
688 
689 	/* Release all clusters that were truncated */
690 	for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
691 		uint32_t cluster_num = _spdk_bs_lba_to_cluster(bs, blob->active.clusters[i]);
692 
693 		_spdk_bs_release_cluster(bs, cluster_num);
694 	}
695 
696 	if (blob->active.num_clusters == 0) {
697 		free(blob->active.clusters);
698 		blob->active.clusters = NULL;
699 		blob->active.cluster_array_size = 0;
700 	} else {
701 		tmp = realloc(blob->active.clusters, sizeof(uint64_t) * blob->active.num_clusters);
702 		assert(tmp != NULL);
703 		blob->active.clusters = tmp;
704 		blob->active.cluster_array_size = blob->active.num_clusters;
705 	}
706 
707 	_spdk_blob_persist_complete(seq, ctx, bserrno);
708 }
709 
710 static void
711 _spdk_blob_persist_unmap_clusters(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
712 {
713 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
714 	struct spdk_blob 		*blob = ctx->blob;
715 	struct spdk_blob_store		*bs = blob->bs;
716 	spdk_bs_batch_t			*batch;
717 	size_t				i;
718 	uint64_t			lba;
719 	uint32_t			lba_count;
720 
721 	/* Clusters don't move around in blobs. The list shrinks or grows
722 	 * at the end, but no changes ever occur in the middle of the list.
723 	 */
724 
725 	batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_unmap_clusters_cpl, ctx);
726 
727 	/* Unmap all clusters that were truncated */
728 	lba = 0;
729 	lba_count = 0;
730 	for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
731 		uint64_t next_lba = blob->active.clusters[i];
732 		uint32_t next_lba_count = _spdk_bs_cluster_to_lba(bs, 1);
733 
734 		if ((lba + lba_count) == next_lba) {
735 			/* This cluster is contiguous with the previous one. */
736 			lba_count += next_lba_count;
737 			continue;
738 		}
739 
740 		/* This cluster is not contiguous with the previous one. */
741 
742 		/* If a run of LBAs previously existing, send them
743 		 * as an unmap.
744 		 */
745 		if (lba_count > 0) {
746 			spdk_bs_batch_unmap(batch, lba, lba_count);
747 		}
748 
749 		/* Start building the next batch */
750 		lba = next_lba;
751 		lba_count = next_lba_count;
752 	}
753 
754 	/* If we ended with a contiguous set of LBAs, send the unmap now */
755 	if (lba_count > 0) {
756 		spdk_bs_batch_unmap(batch, lba, lba_count);
757 	}
758 
759 	spdk_bs_batch_close(batch);
760 }
761 
762 static void
763 _spdk_blob_persist_unmap_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
764 {
765 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
766 	struct spdk_blob 		*blob = ctx->blob;
767 	struct spdk_blob_store		*bs = blob->bs;
768 	size_t				i;
769 
770 	/* This loop starts at 1 because the first page is special and handled
771 	 * below. The pages (except the first) are never written in place,
772 	 * so any pages in the clean list must be unmapped.
773 	 */
774 	for (i = 1; i < blob->clean.num_pages; i++) {
775 		spdk_bit_array_clear(bs->used_md_pages, blob->clean.pages[i]);
776 	}
777 
778 	if (blob->active.num_pages == 0) {
779 		uint32_t page_num;
780 
781 		page_num = _spdk_bs_blobid_to_page(blob->id);
782 		spdk_bit_array_clear(bs->used_md_pages, page_num);
783 	}
784 
785 	/* Move on to unmapping clusters */
786 	_spdk_blob_persist_unmap_clusters(seq, ctx, 0);
787 }
788 
789 static void
790 _spdk_blob_persist_unmap_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
791 {
792 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
793 	struct spdk_blob 		*blob = ctx->blob;
794 	struct spdk_blob_store		*bs = blob->bs;
795 	uint64_t			lba;
796 	uint32_t			lba_count;
797 	spdk_bs_batch_t			*batch;
798 	size_t				i;
799 
800 	batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_unmap_pages_cpl, ctx);
801 
802 	lba_count = _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
803 
804 	/* This loop starts at 1 because the first page is special and handled
805 	 * below. The pages (except the first) are never written in place,
806 	 * so any pages in the clean list must be unmapped.
807 	 */
808 	for (i = 1; i < blob->clean.num_pages; i++) {
809 		lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->clean.pages[i]);
810 
811 		spdk_bs_batch_unmap(batch, lba, lba_count);
812 	}
813 
814 	/* The first page will only be unmapped if this is a delete. */
815 	if (blob->active.num_pages == 0) {
816 		uint32_t page_num;
817 
818 		/* The first page in the metadata goes where the blobid indicates */
819 		page_num = _spdk_bs_blobid_to_page(blob->id);
820 		lba = _spdk_bs_page_to_lba(bs, bs->md_start + page_num);
821 
822 		spdk_bs_batch_unmap(batch, lba, lba_count);
823 	}
824 
825 	spdk_bs_batch_close(batch);
826 }
827 
828 static void
829 _spdk_blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
830 {
831 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
832 	struct spdk_blob		*blob = ctx->blob;
833 	struct spdk_blob_store		*bs = blob->bs;
834 	uint64_t			lba;
835 	uint32_t			lba_count;
836 	struct spdk_blob_md_page	*page;
837 
838 	if (blob->active.num_pages == 0) {
839 		/* Move on to the next step */
840 		_spdk_blob_persist_unmap_pages(seq, ctx, 0);
841 		return;
842 	}
843 
844 	lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page));
845 
846 	page = &ctx->pages[0];
847 	/* The first page in the metadata goes where the blobid indicates */
848 	lba = _spdk_bs_page_to_lba(bs, bs->md_start + _spdk_bs_blobid_to_page(blob->id));
849 
850 	spdk_bs_sequence_write(seq, page, lba, lba_count,
851 			       _spdk_blob_persist_unmap_pages, ctx);
852 }
853 
854 static void
855 _spdk_blob_persist_write_page_chain(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
856 {
857 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
858 	struct spdk_blob 		*blob = ctx->blob;
859 	struct spdk_blob_store		*bs = blob->bs;
860 	uint64_t 			lba;
861 	uint32_t			lba_count;
862 	struct spdk_blob_md_page	*page;
863 	spdk_bs_batch_t			*batch;
864 	size_t				i;
865 
866 	/* Clusters don't move around in blobs. The list shrinks or grows
867 	 * at the end, but no changes ever occur in the middle of the list.
868 	 */
869 
870 	lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page));
871 
872 	batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_write_page_root, ctx);
873 
874 	/* This starts at 1. The root page is not written until
875 	 * all of the others are finished
876 	 */
877 	for (i = 1; i < blob->active.num_pages; i++) {
878 		page = &ctx->pages[i];
879 		assert(page->sequence_num == i);
880 
881 		lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->active.pages[i]);
882 
883 		spdk_bs_batch_write(batch, page, lba, lba_count);
884 	}
885 
886 	spdk_bs_batch_close(batch);
887 }
888 
889 static int
890 _spdk_resize_blob(struct spdk_blob *blob, uint64_t sz)
891 {
892 	uint64_t	i;
893 	uint64_t	*tmp;
894 	uint64_t	lfc; /* lowest free cluster */
895 	struct spdk_blob_store *bs;
896 
897 	bs = blob->bs;
898 
899 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
900 	       blob->state != SPDK_BLOB_STATE_SYNCING);
901 
902 	if (blob->active.num_clusters == sz) {
903 		return 0;
904 	}
905 
906 	if (blob->active.num_clusters < blob->active.cluster_array_size) {
907 		/* If this blob was resized to be larger, then smaller, then
908 		 * larger without syncing, then the cluster array already
909 		 * contains spare assigned clusters we can use.
910 		 */
911 		blob->active.num_clusters = spdk_min(blob->active.cluster_array_size,
912 						     sz);
913 	}
914 
915 	blob->state = SPDK_BLOB_STATE_DIRTY;
916 
917 	/* Do two passes - one to verify that we can obtain enough clusters
918 	 * and another to actually claim them.
919 	 */
920 
921 	lfc = 0;
922 	for (i = blob->active.num_clusters; i < sz; i++) {
923 		lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc);
924 		if (lfc >= bs->total_clusters) {
925 			/* No more free clusters. Cannot satisfy the request */
926 			assert(false);
927 			return -1;
928 		}
929 		lfc++;
930 	}
931 
932 	if (sz > blob->active.num_clusters) {
933 		/* Expand the cluster array if necessary.
934 		 * We only shrink the array when persisting.
935 		 */
936 		tmp = realloc(blob->active.clusters, sizeof(uint64_t) * sz);
937 		if (sz > 0 && tmp == NULL) {
938 			assert(false);
939 			return -1;
940 		}
941 		blob->active.clusters = tmp;
942 		blob->active.cluster_array_size = sz;
943 	}
944 
945 	lfc = 0;
946 	for (i = blob->active.num_clusters; i < sz; i++) {
947 		lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc);
948 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Claiming cluster %lu for blob %lu\n", lfc, blob->id);
949 		_spdk_bs_claim_cluster(bs, lfc);
950 		blob->active.clusters[i] = _spdk_bs_cluster_to_lba(bs, lfc);
951 		lfc++;
952 	}
953 
954 	blob->active.num_clusters = sz;
955 
956 	return 0;
957 }
958 
959 /* Write a blob to disk */
960 static void
961 _spdk_blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
962 		   spdk_bs_sequence_cpl cb_fn, void *cb_arg)
963 {
964 	struct spdk_blob_persist_ctx *ctx;
965 	int rc;
966 	uint64_t i;
967 	uint32_t page_num;
968 	struct spdk_blob_store *bs;
969 
970 	assert(blob != NULL);
971 	assert(blob->state == SPDK_BLOB_STATE_CLEAN ||
972 	       blob->state == SPDK_BLOB_STATE_DIRTY);
973 
974 	if (blob->state == SPDK_BLOB_STATE_CLEAN) {
975 		cb_fn(seq, cb_arg, 0);
976 		return;
977 	}
978 
979 	bs = blob->bs;
980 
981 	ctx = calloc(1, sizeof(*ctx));
982 	if (!ctx) {
983 		cb_fn(seq, cb_arg, -ENOMEM);
984 		return;
985 	}
986 	ctx->blob = blob;
987 	ctx->cb_fn = cb_fn;
988 	ctx->cb_arg = cb_arg;
989 
990 	blob->state = SPDK_BLOB_STATE_SYNCING;
991 
992 	if (blob->active.num_pages == 0) {
993 		/* This is the signal that the blob should be deleted.
994 		 * Immediately jump to the clean up routine. */
995 		assert(blob->clean.num_pages > 0);
996 		ctx->idx = blob->clean.num_pages - 1;
997 		_spdk_blob_persist_unmap_pages(seq, ctx, 0);
998 		return;
999 
1000 	}
1001 
1002 	/* Generate the new metadata */
1003 	rc = _spdk_blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
1004 	if (rc < 0) {
1005 		free(ctx);
1006 		cb_fn(seq, cb_arg, rc);
1007 		return;
1008 	}
1009 
1010 	assert(blob->active.num_pages >= 1);
1011 
1012 	/* Resize the cache of page indices */
1013 	blob->active.pages = realloc(blob->active.pages,
1014 				     blob->active.num_pages * sizeof(*blob->active.pages));
1015 	if (!blob->active.pages) {
1016 		free(ctx);
1017 		cb_fn(seq, cb_arg, -ENOMEM);
1018 		return;
1019 	}
1020 
1021 	/* Assign this metadata to pages. This requires two passes -
1022 	 * one to verify that there are enough pages and a second
1023 	 * to actually claim them. */
1024 	page_num = 0;
1025 	/* Note that this loop starts at one. The first page location is fixed by the blobid. */
1026 	for (i = 1; i < blob->active.num_pages; i++) {
1027 		page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
1028 		if (page_num >= spdk_bit_array_capacity(bs->used_md_pages)) {
1029 			spdk_dma_free(ctx->pages);
1030 			free(ctx);
1031 			blob->state = SPDK_BLOB_STATE_DIRTY;
1032 			cb_fn(seq, cb_arg, -ENOMEM);
1033 			return;
1034 		}
1035 		page_num++;
1036 	}
1037 
1038 	page_num = 0;
1039 	blob->active.pages[0] = _spdk_bs_blobid_to_page(blob->id);
1040 	for (i = 1; i < blob->active.num_pages; i++) {
1041 		page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
1042 		ctx->pages[i - 1].next = page_num;
1043 		/* Now that previous metadata page is complete, calculate the crc for it. */
1044 		ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]);
1045 		blob->active.pages[i] = page_num;
1046 		spdk_bit_array_set(bs->used_md_pages, page_num);
1047 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Claiming page %u for blob %lu\n", page_num, blob->id);
1048 		page_num++;
1049 	}
1050 	ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]);
1051 	/* Start writing the metadata from last page to first */
1052 	ctx->idx = blob->active.num_pages - 1;
1053 	_spdk_blob_persist_write_page_chain(seq, ctx, 0);
1054 }
1055 
1056 static void
1057 _spdk_blob_request_submit_rw(struct spdk_blob *blob, struct spdk_io_channel *_channel,
1058 			     void *payload, uint64_t offset, uint64_t length,
1059 			     spdk_blob_op_complete cb_fn, void *cb_arg, bool read)
1060 {
1061 	spdk_bs_batch_t			*batch;
1062 	struct spdk_bs_cpl		cpl;
1063 	uint64_t			lba;
1064 	uint32_t			lba_count;
1065 	uint8_t				*buf;
1066 	uint64_t			page;
1067 
1068 	assert(blob != NULL);
1069 
1070 	if (offset + length > blob->active.num_clusters * blob->bs->pages_per_cluster) {
1071 		cb_fn(cb_arg, -EINVAL);
1072 		return;
1073 	}
1074 
1075 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
1076 	cpl.u.blob_basic.cb_fn = cb_fn;
1077 	cpl.u.blob_basic.cb_arg = cb_arg;
1078 
1079 	batch = spdk_bs_batch_open(_channel, &cpl);
1080 	if (!batch) {
1081 		cb_fn(cb_arg, -ENOMEM);
1082 		return;
1083 	}
1084 
1085 	length = _spdk_bs_page_to_lba(blob->bs, length);
1086 	page = offset;
1087 	buf = payload;
1088 	while (length > 0) {
1089 		lba = _spdk_bs_blob_page_to_lba(blob, page);
1090 		lba_count = spdk_min(length,
1091 				     _spdk_bs_page_to_lba(blob->bs,
1092 						     _spdk_bs_num_pages_to_cluster_boundary(blob, page)));
1093 
1094 		if (read) {
1095 			spdk_bs_batch_read(batch, buf, lba, lba_count);
1096 		} else {
1097 			spdk_bs_batch_write(batch, buf, lba, lba_count);
1098 		}
1099 
1100 		length -= lba_count;
1101 		buf += _spdk_bs_lba_to_byte(blob->bs, lba_count);
1102 		page += _spdk_bs_lba_to_page(blob->bs, lba_count);
1103 	}
1104 
1105 	spdk_bs_batch_close(batch);
1106 }
1107 
1108 struct rw_iov_ctx {
1109 	struct spdk_blob *blob;
1110 	bool read;
1111 	int iovcnt;
1112 	struct iovec *orig_iov;
1113 	uint64_t page_offset;
1114 	uint64_t pages_remaining;
1115 	uint64_t pages_done;
1116 	struct iovec iov[0];
1117 };
1118 
1119 static void
1120 _spdk_rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1121 {
1122 	assert(cb_arg == NULL);
1123 	spdk_bs_sequence_finish(seq, bserrno);
1124 }
1125 
1126 static void
1127 _spdk_rw_iov_split_next(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1128 {
1129 	struct rw_iov_ctx *ctx = cb_arg;
1130 	struct iovec *iov, *orig_iov;
1131 	int iovcnt;
1132 	size_t orig_iovoff;
1133 	uint64_t lba;
1134 	uint64_t page_count, pages_to_boundary;
1135 	uint32_t lba_count;
1136 	uint64_t byte_count;
1137 
1138 	if (bserrno != 0 || ctx->pages_remaining == 0) {
1139 		free(ctx);
1140 		spdk_bs_sequence_finish(seq, bserrno);
1141 		return;
1142 	}
1143 
1144 	pages_to_boundary = _spdk_bs_num_pages_to_cluster_boundary(ctx->blob, ctx->page_offset);
1145 	page_count = spdk_min(ctx->pages_remaining, pages_to_boundary);
1146 	lba = _spdk_bs_blob_page_to_lba(ctx->blob, ctx->page_offset);
1147 	lba_count = _spdk_bs_page_to_lba(ctx->blob->bs, page_count);
1148 
1149 	/*
1150 	 * Get index and offset into the original iov array for our current position in the I/O sequence.
1151 	 *  byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
1152 	 *  point to the current position in the I/O sequence.
1153 	 */
1154 	byte_count = ctx->pages_done * sizeof(struct spdk_blob_md_page);
1155 	orig_iov = &ctx->orig_iov[0];
1156 	orig_iovoff = 0;
1157 	while (byte_count > 0) {
1158 		if (byte_count >= orig_iov->iov_len) {
1159 			byte_count -= orig_iov->iov_len;
1160 			orig_iov++;
1161 		} else {
1162 			orig_iovoff = byte_count;
1163 			byte_count = 0;
1164 		}
1165 	}
1166 
1167 	/*
1168 	 * Build an iov array for the next I/O in the sequence.  byte_count will keep track of how many
1169 	 *  bytes of this next I/O remain to be accounted for in the new iov array.
1170 	 */
1171 	byte_count = page_count * sizeof(struct spdk_blob_md_page);
1172 	iov = &ctx->iov[0];
1173 	iovcnt = 0;
1174 	while (byte_count > 0) {
1175 		iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
1176 		iov->iov_base = orig_iov->iov_base + orig_iovoff;
1177 		byte_count -= iov->iov_len;
1178 		orig_iovoff = 0;
1179 		orig_iov++;
1180 		iov++;
1181 		iovcnt++;
1182 	}
1183 
1184 	ctx->page_offset += page_count;
1185 	ctx->pages_done += page_count;
1186 	ctx->pages_remaining -= page_count;
1187 	iov = &ctx->iov[0];
1188 
1189 	if (ctx->read) {
1190 		spdk_bs_sequence_readv(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_split_next, ctx);
1191 	} else {
1192 		spdk_bs_sequence_writev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_split_next, ctx);
1193 	}
1194 }
1195 
1196 static void
1197 _spdk_blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
1198 				 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
1199 				 spdk_blob_op_complete cb_fn, void *cb_arg, bool read)
1200 {
1201 	spdk_bs_sequence_t		*seq;
1202 	struct spdk_bs_cpl		cpl;
1203 
1204 	assert(blob != NULL);
1205 
1206 	if (length == 0) {
1207 		cb_fn(cb_arg, 0);
1208 		return;
1209 	}
1210 
1211 	if (offset + length > blob->active.num_clusters * blob->bs->pages_per_cluster) {
1212 		cb_fn(cb_arg, -EINVAL);
1213 		return;
1214 	}
1215 
1216 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
1217 	cpl.u.blob_basic.cb_fn = cb_fn;
1218 	cpl.u.blob_basic.cb_arg = cb_arg;
1219 
1220 	/*
1221 	 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
1222 	 *  to split a request that spans a cluster boundary.  For I/O that do not span a cluster boundary,
1223 	 *  there will be no noticeable difference compared to using a batch.  For I/O that do span a cluster
1224 	 *  boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
1225 	 *  to allocate a separate iov array and split the I/O such that none of the resulting
1226 	 *  smaller I/O cross a cluster boundary.  These smaller I/O will be issued in sequence (not in parallel)
1227 	 *  but since this case happens very infrequently, any performance impact will be negligible.
1228 	 *
1229 	 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
1230 	 *  for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
1231 	 *  in a batch.  That would also require creating an intermediate spdk_bs_cpl that would get called
1232 	 *  when the batch was completed, to allow for freeing the memory for the iov arrays.
1233 	 */
1234 	seq = spdk_bs_sequence_start(_channel, &cpl);
1235 	if (!seq) {
1236 		cb_fn(cb_arg, -ENOMEM);
1237 		return;
1238 	}
1239 
1240 	if (spdk_likely(length <= _spdk_bs_num_pages_to_cluster_boundary(blob, offset))) {
1241 		uint64_t lba = _spdk_bs_blob_page_to_lba(blob, offset);
1242 		uint32_t lba_count = _spdk_bs_page_to_lba(blob->bs, length);
1243 
1244 		if (read) {
1245 			spdk_bs_sequence_readv(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL);
1246 		} else {
1247 			spdk_bs_sequence_writev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL);
1248 		}
1249 	} else {
1250 		struct rw_iov_ctx *ctx;
1251 
1252 		ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
1253 		if (ctx == NULL) {
1254 			spdk_bs_sequence_finish(seq, -ENOMEM);
1255 			return;
1256 		}
1257 
1258 		ctx->blob = blob;
1259 		ctx->read = read;
1260 		ctx->orig_iov = iov;
1261 		ctx->iovcnt = iovcnt;
1262 		ctx->page_offset = offset;
1263 		ctx->pages_remaining = length;
1264 		ctx->pages_done = 0;
1265 
1266 		_spdk_rw_iov_split_next(seq, ctx, 0);
1267 	}
1268 }
1269 
1270 static struct spdk_blob *
1271 _spdk_blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
1272 {
1273 	struct spdk_blob *blob;
1274 
1275 	TAILQ_FOREACH(blob, &bs->blobs, link) {
1276 		if (blob->id == blobid) {
1277 			return blob;
1278 		}
1279 	}
1280 
1281 	return NULL;
1282 }
1283 
1284 static int
1285 _spdk_bs_channel_create(struct spdk_blob_store *bs, struct spdk_bs_channel *channel,
1286 			uint32_t max_ops)
1287 {
1288 	struct spdk_bs_dev		*dev;
1289 	uint32_t			i;
1290 
1291 	dev = bs->dev;
1292 
1293 	channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
1294 	if (!channel->req_mem) {
1295 		return -1;
1296 	}
1297 
1298 	TAILQ_INIT(&channel->reqs);
1299 
1300 	for (i = 0; i < max_ops; i++) {
1301 		TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
1302 	}
1303 
1304 	channel->bs = bs;
1305 	channel->dev = dev;
1306 	channel->dev_channel = dev->create_channel(dev);
1307 
1308 	return 0;
1309 }
1310 
1311 static int
1312 _spdk_bs_md_channel_create(void *io_device, void *ctx_buf)
1313 {
1314 	struct spdk_blob_store		*bs;
1315 	struct spdk_bs_channel		*channel = ctx_buf;
1316 
1317 	bs = SPDK_CONTAINEROF(io_device, struct spdk_blob_store, md_target);
1318 
1319 	return _spdk_bs_channel_create(bs, channel, bs->md_target.max_md_ops);
1320 }
1321 
1322 static int
1323 _spdk_bs_io_channel_create(void *io_device, void *ctx_buf)
1324 {
1325 	struct spdk_blob_store		*bs;
1326 	struct spdk_bs_channel		*channel = ctx_buf;
1327 
1328 	bs = SPDK_CONTAINEROF(io_device, struct spdk_blob_store, io_target);
1329 
1330 	return _spdk_bs_channel_create(bs, channel, bs->io_target.max_channel_ops);
1331 }
1332 
1333 
1334 static void
1335 _spdk_bs_channel_destroy(void *io_device, void *ctx_buf)
1336 {
1337 	struct spdk_bs_channel *channel = ctx_buf;
1338 
1339 	free(channel->req_mem);
1340 	channel->dev->destroy_channel(channel->dev, channel->dev_channel);
1341 }
1342 
1343 static void
1344 _spdk_bs_dev_destroy(void *io_device)
1345 {
1346 	struct spdk_blob_store *bs;
1347 	struct spdk_blob	*blob, *blob_tmp;
1348 
1349 	bs = SPDK_CONTAINEROF(io_device, struct spdk_blob_store, md_target);
1350 	bs->dev->destroy(bs->dev);
1351 
1352 	TAILQ_FOREACH_SAFE(blob, &bs->blobs, link, blob_tmp) {
1353 		TAILQ_REMOVE(&bs->blobs, blob, link);
1354 		_spdk_blob_free(blob);
1355 	}
1356 
1357 	spdk_bit_array_free(&bs->used_md_pages);
1358 	spdk_bit_array_free(&bs->used_clusters);
1359 	/*
1360 	 * If this function is called for any reason except a successful unload,
1361 	 * the unload_cpl type will be NONE and this will be a nop.
1362 	 */
1363 	spdk_bs_call_cpl(&bs->unload_cpl, bs->unload_err);
1364 
1365 	free(bs);
1366 }
1367 
1368 static void
1369 _spdk_bs_free(struct spdk_blob_store *bs)
1370 {
1371 	spdk_bs_unregister_md_thread(bs);
1372 	spdk_io_device_unregister(&bs->io_target, NULL);
1373 	spdk_io_device_unregister(&bs->md_target, _spdk_bs_dev_destroy);
1374 }
1375 
1376 void
1377 spdk_bs_opts_init(struct spdk_bs_opts *opts)
1378 {
1379 	opts->cluster_sz = SPDK_BLOB_OPTS_CLUSTER_SZ;
1380 	opts->num_md_pages = SPDK_BLOB_OPTS_NUM_MD_PAGES;
1381 	opts->max_md_ops = SPDK_BLOB_OPTS_MAX_MD_OPS;
1382 	opts->max_channel_ops = SPDK_BLOB_OPTS_MAX_CHANNEL_OPS;
1383 	memset(&opts->bstype, 0, sizeof(opts->bstype));
1384 }
1385 
1386 static int
1387 _spdk_bs_opts_verify(struct spdk_bs_opts *opts)
1388 {
1389 	if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
1390 	    opts->max_channel_ops == 0) {
1391 		SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
1392 		return -1;
1393 	}
1394 
1395 	return 0;
1396 }
1397 
1398 static struct spdk_blob_store *
1399 _spdk_bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts)
1400 {
1401 	struct spdk_blob_store	*bs;
1402 	uint64_t dev_size;
1403 
1404 	dev_size = dev->blocklen * dev->blockcnt;
1405 	if (dev_size < opts->cluster_sz) {
1406 		/* Device size cannot be smaller than cluster size of blobstore */
1407 		SPDK_ERRLOG("Device size %" PRIu64 " is smaller than cluster size %d\n", dev_size,
1408 			    opts->cluster_sz);
1409 		return NULL;
1410 	}
1411 	bs = calloc(1, sizeof(struct spdk_blob_store));
1412 	if (!bs) {
1413 		return NULL;
1414 	}
1415 
1416 	TAILQ_INIT(&bs->blobs);
1417 	bs->dev = dev;
1418 
1419 	/*
1420 	 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an
1421 	 *  even multiple of the cluster size.
1422 	 */
1423 	bs->cluster_sz = opts->cluster_sz;
1424 	bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
1425 	bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
1426 	bs->num_free_clusters = bs->total_clusters;
1427 	bs->used_clusters = spdk_bit_array_create(bs->total_clusters);
1428 	if (bs->used_clusters == NULL) {
1429 		free(bs);
1430 		return NULL;
1431 	}
1432 
1433 	bs->md_target.max_md_ops = opts->max_md_ops;
1434 	bs->io_target.max_channel_ops = opts->max_channel_ops;
1435 	bs->super_blob = SPDK_BLOBID_INVALID;
1436 	memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
1437 
1438 	/* The metadata is assumed to be at least 1 page */
1439 	bs->used_md_pages = spdk_bit_array_create(1);
1440 
1441 	spdk_io_device_register(&bs->md_target, _spdk_bs_md_channel_create, _spdk_bs_channel_destroy,
1442 				sizeof(struct spdk_bs_channel));
1443 	spdk_bs_register_md_thread(bs);
1444 
1445 	spdk_io_device_register(&bs->io_target, _spdk_bs_io_channel_create, _spdk_bs_channel_destroy,
1446 				sizeof(struct spdk_bs_channel));
1447 
1448 	return bs;
1449 }
1450 
1451 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */
1452 
1453 struct spdk_bs_load_ctx {
1454 	struct spdk_blob_store		*bs;
1455 	struct spdk_bs_super_block	*super;
1456 
1457 	struct spdk_bs_md_mask		*mask;
1458 };
1459 
1460 static void
1461 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask)
1462 {
1463 	uint32_t i = 0;
1464 
1465 	while (true) {
1466 		i = spdk_bit_array_find_first_set(array, i);
1467 		if (i >= mask->length) {
1468 			break;
1469 		}
1470 		mask->mask[i / 8] |= 1U << (i % 8);
1471 		i++;
1472 	}
1473 }
1474 
1475 static void
1476 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
1477 		     struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
1478 {
1479 	/* Update the values in the super block */
1480 	super->super_blob = bs->super_blob;
1481 	memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
1482 	super->crc = _spdk_blob_md_page_calc_crc(super);
1483 	spdk_bs_sequence_write(seq, super, _spdk_bs_page_to_lba(bs, 0),
1484 			       _spdk_bs_byte_to_lba(bs, sizeof(*super)),
1485 			       cb_fn, cb_arg);
1486 }
1487 
1488 static void
1489 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
1490 {
1491 	struct spdk_bs_load_ctx	*ctx = arg;
1492 	uint64_t	mask_size, lba, lba_count;
1493 
1494 	/* Write out the used clusters mask */
1495 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
1496 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1497 	if (!ctx->mask) {
1498 		spdk_dma_free(ctx->super);
1499 		free(ctx);
1500 		spdk_bs_sequence_finish(seq, -ENOMEM);
1501 		return;
1502 	}
1503 
1504 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
1505 	ctx->mask->length = ctx->bs->total_clusters;
1506 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters));
1507 
1508 	_spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask);
1509 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
1510 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
1511 	spdk_bs_sequence_write(seq, ctx->mask, lba, lba_count, cb_fn, arg);
1512 }
1513 
1514 static void
1515 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
1516 {
1517 	struct spdk_bs_load_ctx	*ctx = arg;
1518 	uint64_t	mask_size, lba, lba_count;
1519 
1520 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
1521 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1522 	if (!ctx->mask) {
1523 		spdk_dma_free(ctx->super);
1524 		free(ctx);
1525 		spdk_bs_sequence_finish(seq, -ENOMEM);
1526 		return;
1527 	}
1528 
1529 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
1530 	ctx->mask->length = ctx->super->md_len;
1531 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
1532 
1533 	_spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask);
1534 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
1535 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
1536 	spdk_bs_sequence_write(seq, ctx->mask, lba, lba_count, cb_fn, arg);
1537 }
1538 
1539 static void
1540 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1541 {
1542 	struct spdk_bs_load_ctx *ctx = cb_arg;
1543 	uint32_t		i, j;
1544 	int			rc;
1545 
1546 	/* The type must be correct */
1547 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
1548 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
1549 	assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
1550 					     struct spdk_blob_md_page) * 8));
1551 	/* The length of the mask must be exactly equal to the total number of clusters */
1552 	assert(ctx->mask->length == ctx->bs->total_clusters);
1553 
1554 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
1555 	if (rc < 0) {
1556 		spdk_dma_free(ctx->super);
1557 		spdk_dma_free(ctx->mask);
1558 		_spdk_bs_free(ctx->bs);
1559 		free(ctx);
1560 		spdk_bs_sequence_finish(seq, -ENOMEM);
1561 		return;
1562 	}
1563 
1564 	ctx->bs->num_free_clusters = ctx->bs->total_clusters;
1565 	for (i = 0; i < ctx->mask->length / 8; i++) {
1566 		uint8_t segment = ctx->mask->mask[i];
1567 		for (j = 0; segment && (j < 8); j++) {
1568 			if (segment & 1U) {
1569 				spdk_bit_array_set(ctx->bs->used_clusters, (i * 8) + j);
1570 				assert(ctx->bs->num_free_clusters > 0);
1571 				ctx->bs->num_free_clusters--;
1572 			}
1573 			segment >>= 1U;
1574 		}
1575 	}
1576 
1577 	spdk_dma_free(ctx->super);
1578 	spdk_dma_free(ctx->mask);
1579 	free(ctx);
1580 
1581 	spdk_bs_sequence_finish(seq, bserrno);
1582 }
1583 
1584 static void
1585 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1586 {
1587 	struct spdk_bs_load_ctx *ctx = cb_arg;
1588 	uint64_t		lba, lba_count, mask_size;
1589 	uint32_t		i, j;
1590 	int			rc;
1591 
1592 	/* The type must be correct */
1593 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
1594 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
1595 	assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
1596 				     8));
1597 	/* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
1598 	assert(ctx->mask->length == ctx->super->md_len);
1599 
1600 	rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
1601 	if (rc < 0) {
1602 		spdk_dma_free(ctx->super);
1603 		spdk_dma_free(ctx->mask);
1604 		_spdk_bs_free(ctx->bs);
1605 		free(ctx);
1606 		spdk_bs_sequence_finish(seq, -ENOMEM);
1607 		return;
1608 	}
1609 
1610 	for (i = 0; i < ctx->mask->length / 8; i++) {
1611 		uint8_t segment = ctx->mask->mask[i];
1612 		for (j = 0; segment && (j < 8); j++) {
1613 			if (segment & 1U) {
1614 				spdk_bit_array_set(ctx->bs->used_md_pages, (i * 8) + j);
1615 			}
1616 			segment >>= 1U;
1617 		}
1618 	}
1619 	spdk_dma_free(ctx->mask);
1620 
1621 	/* Read the used clusters mask */
1622 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
1623 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1624 	if (!ctx->mask) {
1625 		spdk_dma_free(ctx->super);
1626 		_spdk_bs_free(ctx->bs);
1627 		free(ctx);
1628 		spdk_bs_sequence_finish(seq, -ENOMEM);
1629 		return;
1630 	}
1631 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
1632 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
1633 	spdk_bs_sequence_read(seq, ctx->mask, lba, lba_count,
1634 			      _spdk_bs_load_used_clusters_cpl, ctx);
1635 }
1636 
1637 static void
1638 _spdk_bs_load_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1639 {
1640 	struct spdk_bs_load_ctx	*ctx = cb_arg;
1641 	uint64_t lba, lba_count, mask_size;
1642 
1643 	/* Parse the super block */
1644 	ctx->bs->cluster_sz = ctx->super->cluster_size;
1645 	ctx->bs->total_clusters = ctx->bs->dev->blockcnt / (ctx->bs->cluster_sz / ctx->bs->dev->blocklen);
1646 	ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
1647 	ctx->bs->md_start = ctx->super->md_start;
1648 	ctx->bs->md_len = ctx->super->md_len;
1649 	ctx->bs->super_blob = ctx->super->super_blob;
1650 	memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
1651 
1652 	/* Read the used pages mask */
1653 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
1654 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1655 	if (!ctx->mask) {
1656 		spdk_dma_free(ctx->super);
1657 		_spdk_bs_free(ctx->bs);
1658 		free(ctx);
1659 		spdk_bs_sequence_finish(seq, -ENOMEM);
1660 		return;
1661 	}
1662 
1663 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
1664 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
1665 	spdk_bs_sequence_read(seq, ctx->mask, lba, lba_count,
1666 			      _spdk_bs_load_used_pages_cpl, ctx);
1667 }
1668 
1669 static void
1670 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1671 {
1672 	struct spdk_bs_load_ctx *ctx = cb_arg;
1673 	uint32_t	crc;
1674 	static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
1675 
1676 	if (ctx->super->version > SPDK_BS_VERSION ||
1677 	    ctx->super->version < SPDK_BS_INITIAL_VERSION) {
1678 		spdk_dma_free(ctx->super);
1679 		_spdk_bs_free(ctx->bs);
1680 		free(ctx);
1681 		spdk_bs_sequence_finish(seq, -EILSEQ);
1682 		return;
1683 	}
1684 
1685 	if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
1686 		   sizeof(ctx->super->signature)) != 0) {
1687 		spdk_dma_free(ctx->super);
1688 		_spdk_bs_free(ctx->bs);
1689 		free(ctx);
1690 		spdk_bs_sequence_finish(seq, -EILSEQ);
1691 		return;
1692 	}
1693 
1694 	crc = _spdk_blob_md_page_calc_crc(ctx->super);
1695 	if (crc != ctx->super->crc) {
1696 		spdk_dma_free(ctx->super);
1697 		_spdk_bs_free(ctx->bs);
1698 		free(ctx);
1699 		spdk_bs_sequence_finish(seq, -EILSEQ);
1700 		return;
1701 	}
1702 
1703 	if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1704 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Bstype matched - loading blobstore\n");
1705 	} else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1706 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n");
1707 	} else {
1708 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Unexpected bstype\n");
1709 		SPDK_TRACEDUMP(SPDK_TRACE_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1710 		SPDK_TRACEDUMP(SPDK_TRACE_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1711 		spdk_dma_free(ctx->super);
1712 		_spdk_bs_free(ctx->bs);
1713 		free(ctx);
1714 		spdk_bs_sequence_finish(seq, -ENXIO);
1715 		return;
1716 	}
1717 
1718 	if (ctx->super->clean != 1) {
1719 		/* TODO: ONLY CLEAN SHUTDOWN IS CURRENTLY SUPPORTED.
1720 		 * All of the necessary data to recover is available
1721 		 * on disk - the code just has not been written yet.
1722 		 */
1723 		assert(false);
1724 		spdk_dma_free(ctx->super);
1725 		_spdk_bs_free(ctx->bs);
1726 		free(ctx);
1727 		spdk_bs_sequence_finish(seq, -EILSEQ);
1728 		return;
1729 	}
1730 
1731 	ctx->super->clean = 0;
1732 	_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_load_write_super_cpl, ctx);
1733 }
1734 
1735 void
1736 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
1737 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
1738 {
1739 	struct spdk_blob_store	*bs;
1740 	struct spdk_bs_cpl	cpl;
1741 	spdk_bs_sequence_t	*seq;
1742 	struct spdk_bs_load_ctx *ctx;
1743 	struct spdk_bs_opts	opts = {};
1744 
1745 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Loading blobstore from dev %p\n", dev);
1746 
1747 	if (o) {
1748 		opts = *o;
1749 	} else {
1750 		spdk_bs_opts_init(&opts);
1751 	}
1752 
1753 	if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
1754 		cb_fn(cb_arg, NULL, -EINVAL);
1755 		return;
1756 	}
1757 
1758 	bs = _spdk_bs_alloc(dev, &opts);
1759 	if (!bs) {
1760 		cb_fn(cb_arg, NULL, -ENOMEM);
1761 		return;
1762 	}
1763 
1764 	ctx = calloc(1, sizeof(*ctx));
1765 	if (!ctx) {
1766 		_spdk_bs_free(bs);
1767 		cb_fn(cb_arg, NULL, -ENOMEM);
1768 		return;
1769 	}
1770 
1771 	ctx->bs = bs;
1772 
1773 	/* Allocate memory for the super block */
1774 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
1775 	if (!ctx->super) {
1776 		free(ctx);
1777 		_spdk_bs_free(bs);
1778 		return;
1779 	}
1780 
1781 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
1782 	cpl.u.bs_handle.cb_fn = cb_fn;
1783 	cpl.u.bs_handle.cb_arg = cb_arg;
1784 	cpl.u.bs_handle.bs = bs;
1785 
1786 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
1787 	if (!seq) {
1788 		spdk_dma_free(ctx->super);
1789 		free(ctx);
1790 		_spdk_bs_free(bs);
1791 		cb_fn(cb_arg, NULL, -ENOMEM);
1792 		return;
1793 	}
1794 
1795 	/* Read the super block */
1796 	spdk_bs_sequence_read(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
1797 			      _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
1798 			      _spdk_bs_load_super_cpl, ctx);
1799 }
1800 
1801 /* END spdk_bs_load */
1802 
1803 /* START spdk_bs_init */
1804 
1805 struct spdk_bs_init_ctx {
1806 	struct spdk_blob_store		*bs;
1807 	struct spdk_bs_super_block	*super;
1808 };
1809 
1810 static void
1811 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1812 {
1813 	struct spdk_bs_init_ctx *ctx = cb_arg;
1814 
1815 	spdk_dma_free(ctx->super);
1816 	free(ctx);
1817 
1818 	spdk_bs_sequence_finish(seq, bserrno);
1819 }
1820 
1821 static void
1822 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1823 {
1824 	struct spdk_bs_init_ctx *ctx = cb_arg;
1825 
1826 	/* Write super block */
1827 	spdk_bs_sequence_write(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0),
1828 			       _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
1829 			       _spdk_bs_init_persist_super_cpl, ctx);
1830 }
1831 
1832 void
1833 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
1834 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
1835 {
1836 	struct spdk_bs_init_ctx *ctx;
1837 	struct spdk_blob_store	*bs;
1838 	struct spdk_bs_cpl	cpl;
1839 	spdk_bs_sequence_t	*seq;
1840 	uint64_t		num_md_pages;
1841 	uint64_t		num_md_clusters;
1842 	uint32_t		i;
1843 	struct spdk_bs_opts	opts = {};
1844 	int			rc;
1845 
1846 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Initializing blobstore on dev %p\n", dev);
1847 
1848 	if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
1849 		SPDK_ERRLOG("unsupported dev block length of %d\n",
1850 			    dev->blocklen);
1851 		dev->destroy(dev);
1852 		cb_fn(cb_arg, NULL, -EINVAL);
1853 		return;
1854 	}
1855 
1856 	if (o) {
1857 		opts = *o;
1858 	} else {
1859 		spdk_bs_opts_init(&opts);
1860 	}
1861 
1862 	if (_spdk_bs_opts_verify(&opts) != 0) {
1863 		dev->destroy(dev);
1864 		cb_fn(cb_arg, NULL, -EINVAL);
1865 		return;
1866 	}
1867 
1868 	bs = _spdk_bs_alloc(dev, &opts);
1869 	if (!bs) {
1870 		dev->destroy(dev);
1871 		cb_fn(cb_arg, NULL, -ENOMEM);
1872 		return;
1873 	}
1874 
1875 	if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
1876 		/* By default, allocate 1 page per cluster.
1877 		 * Technically, this over-allocates metadata
1878 		 * because more metadata will reduce the number
1879 		 * of usable clusters. This can be addressed with
1880 		 * more complex math in the future.
1881 		 */
1882 		bs->md_len = bs->total_clusters;
1883 	} else {
1884 		bs->md_len = opts.num_md_pages;
1885 	}
1886 
1887 	rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
1888 	if (rc < 0) {
1889 		_spdk_bs_free(bs);
1890 		cb_fn(cb_arg, NULL, -ENOMEM);
1891 		return;
1892 	}
1893 
1894 	ctx = calloc(1, sizeof(*ctx));
1895 	if (!ctx) {
1896 		_spdk_bs_free(bs);
1897 		cb_fn(cb_arg, NULL, -ENOMEM);
1898 		return;
1899 	}
1900 
1901 	ctx->bs = bs;
1902 
1903 	/* Allocate memory for the super block */
1904 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
1905 	if (!ctx->super) {
1906 		free(ctx);
1907 		_spdk_bs_free(bs);
1908 		return;
1909 	}
1910 	memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
1911 	       sizeof(ctx->super->signature));
1912 	ctx->super->version = SPDK_BS_VERSION;
1913 	ctx->super->length = sizeof(*ctx->super);
1914 	ctx->super->super_blob = bs->super_blob;
1915 	ctx->super->clean = 0;
1916 	ctx->super->cluster_size = bs->cluster_sz;
1917 	memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
1918 
1919 	/* Calculate how many pages the metadata consumes at the front
1920 	 * of the disk.
1921 	 */
1922 
1923 	/* The super block uses 1 page */
1924 	num_md_pages = 1;
1925 
1926 	/* The used_md_pages mask requires 1 bit per metadata page, rounded
1927 	 * up to the nearest page, plus a header.
1928 	 */
1929 	ctx->super->used_page_mask_start = num_md_pages;
1930 	ctx->super->used_page_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
1931 					 divide_round_up(bs->md_len, 8),
1932 					 SPDK_BS_PAGE_SIZE);
1933 	num_md_pages += ctx->super->used_page_mask_len;
1934 
1935 	/* The used_clusters mask requires 1 bit per cluster, rounded
1936 	 * up to the nearest page, plus a header.
1937 	 */
1938 	ctx->super->used_cluster_mask_start = num_md_pages;
1939 	ctx->super->used_cluster_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
1940 					    divide_round_up(bs->total_clusters, 8),
1941 					    SPDK_BS_PAGE_SIZE);
1942 	num_md_pages += ctx->super->used_cluster_mask_len;
1943 
1944 	/* The metadata region size was chosen above */
1945 	ctx->super->md_start = bs->md_start = num_md_pages;
1946 	ctx->super->md_len = bs->md_len;
1947 	num_md_pages += bs->md_len;
1948 
1949 	ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super);
1950 
1951 	num_md_clusters = divide_round_up(num_md_pages, bs->pages_per_cluster);
1952 	if (num_md_clusters > bs->total_clusters) {
1953 		SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
1954 			    "please decrease number of pages reserved for metadata "
1955 			    "or increase cluster size.\n");
1956 		spdk_dma_free(ctx->super);
1957 		free(ctx);
1958 		_spdk_bs_free(bs);
1959 		cb_fn(cb_arg, NULL, -ENOMEM);
1960 		return;
1961 	}
1962 	/* Claim all of the clusters used by the metadata */
1963 	for (i = 0; i < num_md_clusters; i++) {
1964 		_spdk_bs_claim_cluster(bs, i);
1965 	}
1966 
1967 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
1968 	cpl.u.bs_handle.cb_fn = cb_fn;
1969 	cpl.u.bs_handle.cb_arg = cb_arg;
1970 	cpl.u.bs_handle.bs = bs;
1971 
1972 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
1973 	if (!seq) {
1974 		spdk_dma_free(ctx->super);
1975 		free(ctx);
1976 		_spdk_bs_free(bs);
1977 		cb_fn(cb_arg, NULL, -ENOMEM);
1978 		return;
1979 	}
1980 
1981 	/* TRIM the entire device */
1982 	spdk_bs_sequence_unmap(seq, 0, bs->dev->blockcnt, _spdk_bs_init_trim_cpl, ctx);
1983 }
1984 
1985 /* END spdk_bs_init */
1986 
1987 /* START spdk_bs_unload */
1988 
1989 static void
1990 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1991 {
1992 	struct spdk_bs_load_ctx	*ctx = cb_arg;
1993 
1994 	spdk_dma_free(ctx->super);
1995 
1996 	/*
1997 	 * We need to defer calling spdk_bs_call_cpl() until after
1998 	 * dev destuction, so tuck these away for later use.
1999 	 */
2000 	ctx->bs->unload_err = bserrno;
2001 	memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
2002 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
2003 
2004 	spdk_bs_sequence_finish(seq, bserrno);
2005 
2006 	_spdk_bs_free(ctx->bs);
2007 	free(ctx);
2008 }
2009 
2010 static void
2011 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2012 {
2013 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2014 
2015 	spdk_dma_free(ctx->mask);
2016 	ctx->super->clean = 1;
2017 
2018 	_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx);
2019 }
2020 
2021 static void
2022 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2023 {
2024 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2025 
2026 	spdk_dma_free(ctx->mask);
2027 
2028 	_spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_unload_write_used_clusters_cpl);
2029 }
2030 
2031 static void
2032 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2033 {
2034 	_spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl);
2035 }
2036 
2037 void
2038 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
2039 {
2040 	struct spdk_bs_cpl	cpl;
2041 	spdk_bs_sequence_t	*seq;
2042 	struct spdk_bs_load_ctx *ctx;
2043 
2044 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Syncing blobstore\n");
2045 
2046 	ctx = calloc(1, sizeof(*ctx));
2047 	if (!ctx) {
2048 		cb_fn(cb_arg, -ENOMEM);
2049 		return;
2050 	}
2051 
2052 	ctx->bs = bs;
2053 
2054 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
2055 	if (!ctx->super) {
2056 		free(ctx);
2057 		cb_fn(cb_arg, -ENOMEM);
2058 		return;
2059 	}
2060 
2061 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
2062 	cpl.u.bs_basic.cb_fn = cb_fn;
2063 	cpl.u.bs_basic.cb_arg = cb_arg;
2064 
2065 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2066 	if (!seq) {
2067 		spdk_dma_free(ctx->super);
2068 		free(ctx);
2069 		cb_fn(cb_arg, -ENOMEM);
2070 		return;
2071 	}
2072 
2073 	assert(TAILQ_EMPTY(&bs->blobs));
2074 
2075 	/* Read super block */
2076 	spdk_bs_sequence_read(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
2077 			      _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
2078 			      _spdk_bs_unload_read_super_cpl, ctx);
2079 }
2080 
2081 /* END spdk_bs_unload */
2082 
2083 void
2084 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
2085 		  spdk_bs_op_complete cb_fn, void *cb_arg)
2086 {
2087 	bs->super_blob = blobid;
2088 	cb_fn(cb_arg, 0);
2089 }
2090 
2091 void
2092 spdk_bs_get_super(struct spdk_blob_store *bs,
2093 		  spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
2094 {
2095 	if (bs->super_blob == SPDK_BLOBID_INVALID) {
2096 		cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
2097 	} else {
2098 		cb_fn(cb_arg, bs->super_blob, 0);
2099 	}
2100 }
2101 
2102 uint64_t
2103 spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
2104 {
2105 	return bs->cluster_sz;
2106 }
2107 
2108 uint64_t
2109 spdk_bs_get_page_size(struct spdk_blob_store *bs)
2110 {
2111 	return SPDK_BS_PAGE_SIZE;
2112 }
2113 
2114 uint64_t
2115 spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
2116 {
2117 	return bs->num_free_clusters;
2118 }
2119 
2120 int spdk_bs_register_md_thread(struct spdk_blob_store *bs)
2121 {
2122 	bs->md_target.md_channel = spdk_get_io_channel(&bs->md_target);
2123 
2124 	return 0;
2125 }
2126 
2127 int spdk_bs_unregister_md_thread(struct spdk_blob_store *bs)
2128 {
2129 	spdk_put_io_channel(bs->md_target.md_channel);
2130 
2131 	return 0;
2132 }
2133 
2134 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob)
2135 {
2136 	assert(blob != NULL);
2137 
2138 	return blob->id;
2139 }
2140 
2141 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob)
2142 {
2143 	assert(blob != NULL);
2144 
2145 	return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters);
2146 }
2147 
2148 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob)
2149 {
2150 	assert(blob != NULL);
2151 
2152 	return blob->active.num_clusters;
2153 }
2154 
2155 /* START spdk_bs_md_create_blob */
2156 
2157 static void
2158 _spdk_bs_md_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2159 {
2160 	struct spdk_blob *blob = cb_arg;
2161 
2162 	_spdk_blob_free(blob);
2163 
2164 	spdk_bs_sequence_finish(seq, bserrno);
2165 }
2166 
2167 void spdk_bs_md_create_blob(struct spdk_blob_store *bs,
2168 			    spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
2169 {
2170 	struct spdk_blob	*blob;
2171 	uint32_t		page_idx;
2172 	struct spdk_bs_cpl 	cpl;
2173 	spdk_bs_sequence_t	*seq;
2174 	spdk_blob_id		id;
2175 
2176 	page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
2177 	if (page_idx >= spdk_bit_array_capacity(bs->used_md_pages)) {
2178 		cb_fn(cb_arg, 0, -ENOMEM);
2179 		return;
2180 	}
2181 	spdk_bit_array_set(bs->used_md_pages, page_idx);
2182 
2183 	id = _spdk_bs_page_to_blobid(page_idx);
2184 
2185 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx);
2186 
2187 	blob = _spdk_blob_alloc(bs, id);
2188 	if (!blob) {
2189 		cb_fn(cb_arg, 0, -ENOMEM);
2190 		return;
2191 	}
2192 
2193 	cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
2194 	cpl.u.blobid.cb_fn = cb_fn;
2195 	cpl.u.blobid.cb_arg = cb_arg;
2196 	cpl.u.blobid.blobid = blob->id;
2197 
2198 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2199 	if (!seq) {
2200 		_spdk_blob_free(blob);
2201 		cb_fn(cb_arg, 0, -ENOMEM);
2202 		return;
2203 	}
2204 
2205 	_spdk_blob_persist(seq, blob, _spdk_bs_md_create_blob_cpl, blob);
2206 }
2207 
2208 /* END spdk_bs_md_create_blob */
2209 
2210 /* START spdk_bs_md_resize_blob */
2211 int
2212 spdk_bs_md_resize_blob(struct spdk_blob *blob, uint64_t sz)
2213 {
2214 	int			rc;
2215 
2216 	assert(blob != NULL);
2217 
2218 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz);
2219 
2220 	if (sz == blob->active.num_clusters) {
2221 		return 0;
2222 	}
2223 
2224 	rc = _spdk_resize_blob(blob, sz);
2225 	if (rc < 0) {
2226 		return rc;
2227 	}
2228 
2229 	return 0;
2230 }
2231 
2232 /* END spdk_bs_md_resize_blob */
2233 
2234 
2235 /* START spdk_bs_md_delete_blob */
2236 
2237 static void
2238 _spdk_bs_md_delete_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2239 {
2240 	struct spdk_blob *blob = cb_arg;
2241 
2242 	_spdk_blob_free(blob);
2243 
2244 	spdk_bs_sequence_finish(seq, bserrno);
2245 }
2246 
2247 static void
2248 _spdk_bs_md_delete_open_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2249 {
2250 	struct spdk_blob *blob = cb_arg;
2251 
2252 	/* If the blob have crc error, we just return NULL. */
2253 	if (blob == NULL) {
2254 		spdk_bs_sequence_finish(seq, bserrno);
2255 		return;
2256 	}
2257 	blob->state = SPDK_BLOB_STATE_DIRTY;
2258 	blob->active.num_pages = 0;
2259 	_spdk_resize_blob(blob, 0);
2260 
2261 	_spdk_blob_persist(seq, blob, _spdk_bs_md_delete_blob_cpl, blob);
2262 }
2263 
2264 void
2265 spdk_bs_md_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
2266 		       spdk_blob_op_complete cb_fn, void *cb_arg)
2267 {
2268 	struct spdk_blob	*blob;
2269 	struct spdk_bs_cpl	cpl;
2270 	spdk_bs_sequence_t 	*seq;
2271 
2272 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Deleting blob %lu\n", blobid);
2273 
2274 	blob = _spdk_blob_lookup(bs, blobid);
2275 	if (blob) {
2276 		assert(blob->open_ref > 0);
2277 		cb_fn(cb_arg, -EINVAL);
2278 		return;
2279 	}
2280 
2281 	blob = _spdk_blob_alloc(bs, blobid);
2282 	if (!blob) {
2283 		cb_fn(cb_arg, -ENOMEM);
2284 		return;
2285 	}
2286 
2287 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2288 	cpl.u.blob_basic.cb_fn = cb_fn;
2289 	cpl.u.blob_basic.cb_arg = cb_arg;
2290 
2291 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2292 	if (!seq) {
2293 		_spdk_blob_free(blob);
2294 		cb_fn(cb_arg, -ENOMEM);
2295 		return;
2296 	}
2297 
2298 	_spdk_blob_load(seq, blob, _spdk_bs_md_delete_open_cpl, blob);
2299 }
2300 
2301 /* END spdk_bs_md_delete_blob */
2302 
2303 /* START spdk_bs_md_open_blob */
2304 
2305 static void
2306 _spdk_bs_md_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2307 {
2308 	struct spdk_blob *blob = cb_arg;
2309 
2310 	/* If the blob have crc error, we just return NULL. */
2311 	if (blob == NULL) {
2312 		seq->cpl.u.blob_handle.blob = NULL;
2313 		spdk_bs_sequence_finish(seq, bserrno);
2314 		return;
2315 	}
2316 
2317 	blob->open_ref++;
2318 
2319 	TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link);
2320 
2321 	spdk_bs_sequence_finish(seq, bserrno);
2322 }
2323 
2324 void spdk_bs_md_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
2325 			  spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
2326 {
2327 	struct spdk_blob		*blob;
2328 	struct spdk_bs_cpl		cpl;
2329 	spdk_bs_sequence_t		*seq;
2330 	uint32_t			page_num;
2331 
2332 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Opening blob %lu\n", blobid);
2333 
2334 	blob = _spdk_blob_lookup(bs, blobid);
2335 	if (blob) {
2336 		blob->open_ref++;
2337 		cb_fn(cb_arg, blob, 0);
2338 		return;
2339 	}
2340 
2341 	page_num = _spdk_bs_blobid_to_page(blobid);
2342 	if (spdk_bit_array_get(bs->used_md_pages, page_num) == false) {
2343 		/* Invalid blobid */
2344 		cb_fn(cb_arg, NULL, -ENOENT);
2345 		return;
2346 	}
2347 
2348 	blob = _spdk_blob_alloc(bs, blobid);
2349 	if (!blob) {
2350 		cb_fn(cb_arg, NULL, -ENOMEM);
2351 		return;
2352 	}
2353 
2354 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
2355 	cpl.u.blob_handle.cb_fn = cb_fn;
2356 	cpl.u.blob_handle.cb_arg = cb_arg;
2357 	cpl.u.blob_handle.blob = blob;
2358 
2359 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2360 	if (!seq) {
2361 		_spdk_blob_free(blob);
2362 		cb_fn(cb_arg, NULL, -ENOMEM);
2363 		return;
2364 	}
2365 
2366 	_spdk_blob_load(seq, blob, _spdk_bs_md_open_blob_cpl, blob);
2367 }
2368 
2369 /* START spdk_bs_md_sync_blob */
2370 static void
2371 _spdk_blob_sync_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2372 {
2373 	spdk_bs_sequence_finish(seq, bserrno);
2374 }
2375 
2376 void spdk_bs_md_sync_blob(struct spdk_blob *blob,
2377 			  spdk_blob_op_complete cb_fn, void *cb_arg)
2378 {
2379 	struct spdk_bs_cpl	cpl;
2380 	spdk_bs_sequence_t	*seq;
2381 
2382 	assert(blob != NULL);
2383 
2384 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Syncing blob %lu\n", blob->id);
2385 
2386 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2387 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2388 
2389 	if (blob->state == SPDK_BLOB_STATE_CLEAN) {
2390 		cb_fn(cb_arg, 0);
2391 		return;
2392 	}
2393 
2394 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2395 	cpl.u.blob_basic.cb_fn = cb_fn;
2396 	cpl.u.blob_basic.cb_arg = cb_arg;
2397 
2398 	seq = spdk_bs_sequence_start(blob->bs->md_target.md_channel, &cpl);
2399 	if (!seq) {
2400 		cb_fn(cb_arg, -ENOMEM);
2401 		return;
2402 	}
2403 
2404 	_spdk_blob_persist(seq, blob, _spdk_blob_sync_cpl, blob);
2405 }
2406 
2407 /* END spdk_bs_md_sync_blob */
2408 
2409 /* START spdk_bs_md_close_blob */
2410 
2411 static void
2412 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2413 {
2414 	struct spdk_blob **blob = cb_arg;
2415 
2416 	if ((*blob)->open_ref == 0) {
2417 		TAILQ_REMOVE(&(*blob)->bs->blobs, (*blob), link);
2418 		_spdk_blob_free((*blob));
2419 	}
2420 
2421 	*blob = NULL;
2422 
2423 	spdk_bs_sequence_finish(seq, bserrno);
2424 }
2425 
2426 void spdk_bs_md_close_blob(struct spdk_blob **b,
2427 			   spdk_blob_op_complete cb_fn, void *cb_arg)
2428 {
2429 	struct spdk_bs_cpl	cpl;
2430 	struct spdk_blob	*blob;
2431 	spdk_bs_sequence_t	*seq;
2432 
2433 	assert(b != NULL);
2434 	blob = *b;
2435 	assert(blob != NULL);
2436 
2437 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Closing blob %lu\n", blob->id);
2438 
2439 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2440 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2441 
2442 	if (blob->open_ref == 0) {
2443 		cb_fn(cb_arg, -EBADF);
2444 		return;
2445 	}
2446 
2447 	blob->open_ref--;
2448 
2449 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2450 	cpl.u.blob_basic.cb_fn = cb_fn;
2451 	cpl.u.blob_basic.cb_arg = cb_arg;
2452 
2453 	seq = spdk_bs_sequence_start(blob->bs->md_target.md_channel, &cpl);
2454 	if (!seq) {
2455 		cb_fn(cb_arg, -ENOMEM);
2456 		return;
2457 	}
2458 
2459 	if (blob->state == SPDK_BLOB_STATE_CLEAN) {
2460 		_spdk_blob_close_cpl(seq, b, 0);
2461 		return;
2462 	}
2463 
2464 	/* Sync metadata */
2465 	_spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, b);
2466 }
2467 
2468 /* END spdk_bs_md_close_blob */
2469 
2470 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
2471 {
2472 	return spdk_get_io_channel(&bs->io_target);
2473 }
2474 
2475 void spdk_bs_free_io_channel(struct spdk_io_channel *channel)
2476 {
2477 	spdk_put_io_channel(channel);
2478 }
2479 
2480 void spdk_bs_io_flush_channel(struct spdk_io_channel *channel,
2481 			      spdk_blob_op_complete cb_fn, void *cb_arg)
2482 {
2483 	/* Flush is synchronous right now */
2484 	cb_fn(cb_arg, 0);
2485 }
2486 
2487 void spdk_bs_io_write_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2488 			   void *payload, uint64_t offset, uint64_t length,
2489 			   spdk_blob_op_complete cb_fn, void *cb_arg)
2490 {
2491 	_spdk_blob_request_submit_rw(blob, channel, payload, offset, length, cb_fn, cb_arg, false);
2492 }
2493 
2494 void spdk_bs_io_read_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2495 			  void *payload, uint64_t offset, uint64_t length,
2496 			  spdk_blob_op_complete cb_fn, void *cb_arg)
2497 {
2498 	_spdk_blob_request_submit_rw(blob, channel, payload, offset, length, cb_fn, cb_arg, true);
2499 }
2500 
2501 void spdk_bs_io_writev_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2502 			    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
2503 			    spdk_blob_op_complete cb_fn, void *cb_arg)
2504 {
2505 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false);
2506 }
2507 
2508 void spdk_bs_io_readv_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2509 			   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
2510 			   spdk_blob_op_complete cb_fn, void *cb_arg)
2511 {
2512 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true);
2513 }
2514 
2515 struct spdk_bs_iter_ctx {
2516 	int64_t page_num;
2517 	struct spdk_blob_store *bs;
2518 
2519 	spdk_blob_op_with_handle_complete cb_fn;
2520 	void *cb_arg;
2521 };
2522 
2523 static void
2524 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
2525 {
2526 	struct spdk_bs_iter_ctx *ctx = cb_arg;
2527 	struct spdk_blob_store *bs = ctx->bs;
2528 	spdk_blob_id id;
2529 
2530 	if (bserrno == 0) {
2531 		ctx->cb_fn(ctx->cb_arg, blob, bserrno);
2532 		free(ctx);
2533 		return;
2534 	}
2535 
2536 	ctx->page_num++;
2537 	ctx->page_num = spdk_bit_array_find_first_set(bs->used_md_pages, ctx->page_num);
2538 	if (ctx->page_num >= spdk_bit_array_capacity(bs->used_md_pages)) {
2539 		ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
2540 		free(ctx);
2541 		return;
2542 	}
2543 
2544 	id = _spdk_bs_page_to_blobid(ctx->page_num);
2545 
2546 	blob = _spdk_blob_lookup(bs, id);
2547 	if (blob) {
2548 		blob->open_ref++;
2549 		ctx->cb_fn(ctx->cb_arg, blob, 0);
2550 		free(ctx);
2551 		return;
2552 	}
2553 
2554 	spdk_bs_md_open_blob(bs, id, _spdk_bs_iter_cpl, ctx);
2555 }
2556 
2557 void
2558 spdk_bs_md_iter_first(struct spdk_blob_store *bs,
2559 		      spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
2560 {
2561 	struct spdk_bs_iter_ctx *ctx;
2562 
2563 	ctx = calloc(1, sizeof(*ctx));
2564 	if (!ctx) {
2565 		cb_fn(cb_arg, NULL, -ENOMEM);
2566 		return;
2567 	}
2568 
2569 	ctx->page_num = -1;
2570 	ctx->bs = bs;
2571 	ctx->cb_fn = cb_fn;
2572 	ctx->cb_arg = cb_arg;
2573 
2574 	_spdk_bs_iter_cpl(ctx, NULL, -1);
2575 }
2576 
2577 static void
2578 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno)
2579 {
2580 	struct spdk_bs_iter_ctx *ctx = cb_arg;
2581 
2582 	_spdk_bs_iter_cpl(ctx, NULL, -1);
2583 }
2584 
2585 void
2586 spdk_bs_md_iter_next(struct spdk_blob_store *bs, struct spdk_blob **b,
2587 		     spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
2588 {
2589 	struct spdk_bs_iter_ctx *ctx;
2590 	struct spdk_blob	*blob;
2591 
2592 	assert(b != NULL);
2593 	blob = *b;
2594 	assert(blob != NULL);
2595 
2596 	ctx = calloc(1, sizeof(*ctx));
2597 	if (!ctx) {
2598 		cb_fn(cb_arg, NULL, -ENOMEM);
2599 		return;
2600 	}
2601 
2602 	ctx->page_num = _spdk_bs_blobid_to_page(blob->id);
2603 	ctx->bs = bs;
2604 	ctx->cb_fn = cb_fn;
2605 	ctx->cb_arg = cb_arg;
2606 
2607 	/* Close the existing blob */
2608 	spdk_bs_md_close_blob(b, _spdk_bs_iter_close_cpl, ctx);
2609 }
2610 
2611 int
2612 spdk_blob_md_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
2613 		       uint16_t value_len)
2614 {
2615 	struct spdk_xattr 	*xattr;
2616 
2617 	assert(blob != NULL);
2618 
2619 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2620 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2621 
2622 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2623 		if (!strcmp(name, xattr->name)) {
2624 			free(xattr->value);
2625 			xattr->value_len = value_len;
2626 			xattr->value = malloc(value_len);
2627 			memcpy(xattr->value, value, value_len);
2628 
2629 			blob->state = SPDK_BLOB_STATE_DIRTY;
2630 
2631 			return 0;
2632 		}
2633 	}
2634 
2635 	xattr = calloc(1, sizeof(*xattr));
2636 	if (!xattr) {
2637 		return -1;
2638 	}
2639 	xattr->name = strdup(name);
2640 	xattr->value_len = value_len;
2641 	xattr->value = malloc(value_len);
2642 	memcpy(xattr->value, value, value_len);
2643 	TAILQ_INSERT_TAIL(&blob->xattrs, xattr, link);
2644 
2645 	blob->state = SPDK_BLOB_STATE_DIRTY;
2646 
2647 	return 0;
2648 }
2649 
2650 int
2651 spdk_blob_md_remove_xattr(struct spdk_blob *blob, const char *name)
2652 {
2653 	struct spdk_xattr	*xattr;
2654 
2655 	assert(blob != NULL);
2656 
2657 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2658 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2659 
2660 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2661 		if (!strcmp(name, xattr->name)) {
2662 			TAILQ_REMOVE(&blob->xattrs, xattr, link);
2663 			free(xattr->value);
2664 			free(xattr->name);
2665 			free(xattr);
2666 
2667 			blob->state = SPDK_BLOB_STATE_DIRTY;
2668 
2669 			return 0;
2670 		}
2671 	}
2672 
2673 	return -ENOENT;
2674 }
2675 
2676 int
2677 spdk_bs_md_get_xattr_value(struct spdk_blob *blob, const char *name,
2678 			   const void **value, size_t *value_len)
2679 {
2680 	struct spdk_xattr	*xattr;
2681 
2682 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2683 		if (!strcmp(name, xattr->name)) {
2684 			*value = xattr->value;
2685 			*value_len = xattr->value_len;
2686 			return 0;
2687 		}
2688 	}
2689 
2690 	return -ENOENT;
2691 }
2692 
2693 struct spdk_xattr_names {
2694 	uint32_t	count;
2695 	const char	*names[0];
2696 };
2697 
2698 int
2699 spdk_bs_md_get_xattr_names(struct spdk_blob *blob,
2700 			   struct spdk_xattr_names **names)
2701 {
2702 	struct spdk_xattr	*xattr;
2703 	int			count = 0;
2704 
2705 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2706 		count++;
2707 	}
2708 
2709 	*names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
2710 	if (*names == NULL) {
2711 		return -ENOMEM;
2712 	}
2713 
2714 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2715 		(*names)->names[(*names)->count++] = xattr->name;
2716 	}
2717 
2718 	return 0;
2719 }
2720 
2721 uint32_t
2722 spdk_xattr_names_get_count(struct spdk_xattr_names *names)
2723 {
2724 	assert(names != NULL);
2725 
2726 	return names->count;
2727 }
2728 
2729 const char *
2730 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
2731 {
2732 	if (index >= names->count) {
2733 		return NULL;
2734 	}
2735 
2736 	return names->names[index];
2737 }
2738 
2739 void
2740 spdk_xattr_names_free(struct spdk_xattr_names *names)
2741 {
2742 	free(names);
2743 }
2744 
2745 struct spdk_bs_type
2746 spdk_bs_get_bstype(struct spdk_blob_store *bs)
2747 {
2748 	return bs->bstype;
2749 }
2750 
2751 void
2752 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
2753 {
2754 	memcpy(&bs->bstype, &bstype, sizeof(bstype));
2755 }
2756 
2757 SPDK_LOG_REGISTER_TRACE_FLAG("blob", SPDK_TRACE_BLOB);
2758