xref: /spdk/lib/blob/blobstore.c (revision 2baeea7dd43483689a430ab2f03091373a626a7b)
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_zero_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 zeroed.
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_zero_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_zero_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 zeroed.
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_write_zeroes(batch, lba, lba_count);
812 	}
813 
814 	/* The first page will only be zeroed 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_write_zeroes(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_zero_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_zero_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_zero_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 	if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
1412 		/* Cluster size cannot be smaller than page size */
1413 		SPDK_ERRLOG("Cluster size %d is smaller than page size %d\n",
1414 			    opts->cluster_sz, SPDK_BS_PAGE_SIZE);
1415 		return NULL;
1416 	}
1417 	bs = calloc(1, sizeof(struct spdk_blob_store));
1418 	if (!bs) {
1419 		return NULL;
1420 	}
1421 
1422 	TAILQ_INIT(&bs->blobs);
1423 	bs->dev = dev;
1424 
1425 	/*
1426 	 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an
1427 	 *  even multiple of the cluster size.
1428 	 */
1429 	bs->cluster_sz = opts->cluster_sz;
1430 	bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
1431 	bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
1432 	bs->num_free_clusters = bs->total_clusters;
1433 	bs->used_clusters = spdk_bit_array_create(bs->total_clusters);
1434 	if (bs->used_clusters == NULL) {
1435 		free(bs);
1436 		return NULL;
1437 	}
1438 
1439 	bs->md_target.max_md_ops = opts->max_md_ops;
1440 	bs->io_target.max_channel_ops = opts->max_channel_ops;
1441 	bs->super_blob = SPDK_BLOBID_INVALID;
1442 	memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
1443 
1444 	/* The metadata is assumed to be at least 1 page */
1445 	bs->used_md_pages = spdk_bit_array_create(1);
1446 
1447 	spdk_io_device_register(&bs->md_target, _spdk_bs_md_channel_create, _spdk_bs_channel_destroy,
1448 				sizeof(struct spdk_bs_channel));
1449 	spdk_bs_register_md_thread(bs);
1450 
1451 	spdk_io_device_register(&bs->io_target, _spdk_bs_io_channel_create, _spdk_bs_channel_destroy,
1452 				sizeof(struct spdk_bs_channel));
1453 
1454 	return bs;
1455 }
1456 
1457 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */
1458 
1459 struct spdk_bs_load_ctx {
1460 	struct spdk_blob_store		*bs;
1461 	struct spdk_bs_super_block	*super;
1462 
1463 	struct spdk_bs_md_mask		*mask;
1464 	bool				in_page_chain;
1465 	uint32_t			page_index;
1466 	uint32_t			cur_page;
1467 	struct spdk_blob_md_page	*page;
1468 };
1469 
1470 static void
1471 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask)
1472 {
1473 	uint32_t i = 0;
1474 
1475 	while (true) {
1476 		i = spdk_bit_array_find_first_set(array, i);
1477 		if (i >= mask->length) {
1478 			break;
1479 		}
1480 		mask->mask[i / 8] |= 1U << (i % 8);
1481 		i++;
1482 	}
1483 }
1484 
1485 static void
1486 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
1487 		     struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
1488 {
1489 	/* Update the values in the super block */
1490 	super->super_blob = bs->super_blob;
1491 	memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
1492 	super->crc = _spdk_blob_md_page_calc_crc(super);
1493 	spdk_bs_sequence_write(seq, super, _spdk_bs_page_to_lba(bs, 0),
1494 			       _spdk_bs_byte_to_lba(bs, sizeof(*super)),
1495 			       cb_fn, cb_arg);
1496 }
1497 
1498 static void
1499 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
1500 {
1501 	struct spdk_bs_load_ctx	*ctx = arg;
1502 	uint64_t	mask_size, lba, lba_count;
1503 
1504 	/* Write out the used clusters mask */
1505 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
1506 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1507 	if (!ctx->mask) {
1508 		spdk_dma_free(ctx->super);
1509 		free(ctx);
1510 		spdk_bs_sequence_finish(seq, -ENOMEM);
1511 		return;
1512 	}
1513 
1514 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
1515 	ctx->mask->length = ctx->bs->total_clusters;
1516 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters));
1517 
1518 	_spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask);
1519 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
1520 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
1521 	spdk_bs_sequence_write(seq, ctx->mask, lba, lba_count, cb_fn, arg);
1522 }
1523 
1524 static void
1525 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
1526 {
1527 	struct spdk_bs_load_ctx	*ctx = arg;
1528 	uint64_t	mask_size, lba, lba_count;
1529 
1530 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
1531 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1532 	if (!ctx->mask) {
1533 		spdk_dma_free(ctx->super);
1534 		free(ctx);
1535 		spdk_bs_sequence_finish(seq, -ENOMEM);
1536 		return;
1537 	}
1538 
1539 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
1540 	ctx->mask->length = ctx->super->md_len;
1541 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
1542 
1543 	_spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask);
1544 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
1545 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
1546 	spdk_bs_sequence_write(seq, ctx->mask, lba, lba_count, cb_fn, arg);
1547 }
1548 
1549 static void
1550 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1551 {
1552 	struct spdk_bs_load_ctx *ctx = cb_arg;
1553 	uint32_t		i, j;
1554 	int			rc;
1555 
1556 	/* The type must be correct */
1557 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
1558 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
1559 	assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
1560 					     struct spdk_blob_md_page) * 8));
1561 	/* The length of the mask must be exactly equal to the total number of clusters */
1562 	assert(ctx->mask->length == ctx->bs->total_clusters);
1563 
1564 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
1565 	if (rc < 0) {
1566 		spdk_dma_free(ctx->super);
1567 		spdk_dma_free(ctx->mask);
1568 		_spdk_bs_free(ctx->bs);
1569 		free(ctx);
1570 		spdk_bs_sequence_finish(seq, -ENOMEM);
1571 		return;
1572 	}
1573 
1574 	ctx->bs->num_free_clusters = ctx->bs->total_clusters;
1575 	for (i = 0; i < ctx->mask->length / 8; i++) {
1576 		uint8_t segment = ctx->mask->mask[i];
1577 		for (j = 0; segment && (j < 8); j++) {
1578 			if (segment & 1U) {
1579 				spdk_bit_array_set(ctx->bs->used_clusters, (i * 8) + j);
1580 				assert(ctx->bs->num_free_clusters > 0);
1581 				ctx->bs->num_free_clusters--;
1582 			}
1583 			segment >>= 1U;
1584 		}
1585 	}
1586 
1587 	spdk_dma_free(ctx->super);
1588 	spdk_dma_free(ctx->mask);
1589 	free(ctx);
1590 
1591 	spdk_bs_sequence_finish(seq, bserrno);
1592 }
1593 
1594 static void
1595 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1596 {
1597 	struct spdk_bs_load_ctx *ctx = cb_arg;
1598 	uint64_t		lba, lba_count, mask_size;
1599 	uint32_t		i, j;
1600 	int			rc;
1601 
1602 	/* The type must be correct */
1603 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
1604 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
1605 	assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
1606 				     8));
1607 	/* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
1608 	assert(ctx->mask->length == ctx->super->md_len);
1609 
1610 	rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
1611 	if (rc < 0) {
1612 		spdk_dma_free(ctx->super);
1613 		spdk_dma_free(ctx->mask);
1614 		_spdk_bs_free(ctx->bs);
1615 		free(ctx);
1616 		spdk_bs_sequence_finish(seq, -ENOMEM);
1617 		return;
1618 	}
1619 
1620 	for (i = 0; i < ctx->mask->length / 8; i++) {
1621 		uint8_t segment = ctx->mask->mask[i];
1622 		for (j = 0; segment && (j < 8); j++) {
1623 			if (segment & 1U) {
1624 				spdk_bit_array_set(ctx->bs->used_md_pages, (i * 8) + j);
1625 			}
1626 			segment >>= 1U;
1627 		}
1628 	}
1629 	spdk_dma_free(ctx->mask);
1630 
1631 	/* Read the used clusters mask */
1632 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
1633 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1634 	if (!ctx->mask) {
1635 		spdk_dma_free(ctx->super);
1636 		_spdk_bs_free(ctx->bs);
1637 		free(ctx);
1638 		spdk_bs_sequence_finish(seq, -ENOMEM);
1639 		return;
1640 	}
1641 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
1642 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
1643 	spdk_bs_sequence_read(seq, ctx->mask, lba, lba_count,
1644 			      _spdk_bs_load_used_clusters_cpl, ctx);
1645 }
1646 
1647 static void
1648 _spdk_bs_load_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1649 {
1650 	struct spdk_bs_load_ctx	*ctx = cb_arg;
1651 	uint64_t lba, lba_count, mask_size;
1652 
1653 	/* Read the used pages mask */
1654 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
1655 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
1656 	if (!ctx->mask) {
1657 		spdk_dma_free(ctx->super);
1658 		_spdk_bs_free(ctx->bs);
1659 		free(ctx);
1660 		spdk_bs_sequence_finish(seq, -ENOMEM);
1661 		return;
1662 	}
1663 
1664 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
1665 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
1666 	spdk_bs_sequence_read(seq, ctx->mask, lba, lba_count,
1667 			      _spdk_bs_load_used_pages_cpl, ctx);
1668 }
1669 
1670 static int
1671 _spdk_bs_load_replay_md_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob_store *bs)
1672 {
1673 	struct spdk_blob_md_descriptor *desc;
1674 	size_t	cur_desc = 0;
1675 
1676 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
1677 	while (cur_desc < sizeof(page->descriptors)) {
1678 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
1679 			if (desc->length == 0) {
1680 				/* If padding and length are 0, this terminates the page */
1681 				break;
1682 			}
1683 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) {
1684 			struct spdk_blob_md_descriptor_extent	*desc_extent;
1685 			unsigned int				i, j;
1686 			unsigned int				cluster_count = 0;
1687 
1688 			desc_extent = (struct spdk_blob_md_descriptor_extent *)desc;
1689 
1690 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) {
1691 				for (j = 0; j < desc_extent->extents[i].length; j++) {
1692 					spdk_bit_array_set(bs->used_clusters, desc_extent->extents[i].cluster_idx + j);
1693 					if (bs->num_free_clusters == 0) {
1694 						return -1;
1695 					}
1696 					bs->num_free_clusters--;
1697 					cluster_count++;
1698 				}
1699 			}
1700 			if (cluster_count == 0) {
1701 				return -1;
1702 			}
1703 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
1704 			/* Skip this item */
1705 		} else {
1706 			/* Error */
1707 			return -1;
1708 		}
1709 		/* Advance to the next descriptor */
1710 		cur_desc += sizeof(*desc) + desc->length;
1711 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
1712 			break;
1713 		}
1714 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
1715 	}
1716 	return 0;
1717 }
1718 
1719 static bool _spdk_bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
1720 {
1721 	uint32_t crc;
1722 
1723 	crc = _spdk_blob_md_page_calc_crc(ctx->page);
1724 	if (crc != ctx->page->crc) {
1725 		return false;
1726 	}
1727 
1728 	if (_spdk_bs_page_to_blobid(ctx->cur_page) != ctx->page->id) {
1729 		return false;
1730 	}
1731 	return true;
1732 }
1733 
1734 static void
1735 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
1736 
1737 static void
1738 _spdk_bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1739 {
1740 	struct spdk_bs_load_ctx	*ctx = cb_arg;
1741 
1742 	spdk_dma_free(ctx->mask);
1743 	spdk_dma_free(ctx->super);
1744 	spdk_bs_sequence_finish(seq, bserrno);
1745 	free(ctx);
1746 }
1747 
1748 static void
1749 _spdk_bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1750 {
1751 	struct spdk_bs_load_ctx	*ctx = cb_arg;
1752 
1753 	spdk_dma_free(ctx->mask);
1754 
1755 	_spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_load_write_used_clusters_cpl);
1756 }
1757 
1758 static void
1759 _spdk_bs_load_write_used_md(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1760 {
1761 	_spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_load_write_used_pages_cpl);
1762 }
1763 
1764 static void
1765 _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1766 {
1767 	struct spdk_bs_load_ctx *ctx = cb_arg;
1768 	uint32_t page_num;
1769 
1770 	if (bserrno != 0) {
1771 		spdk_dma_free(ctx->super);
1772 		_spdk_bs_free(ctx->bs);
1773 		free(ctx);
1774 		spdk_bs_sequence_finish(seq, bserrno);
1775 		return;
1776 	}
1777 
1778 	page_num = ctx->cur_page;
1779 	if (_spdk_bs_load_cur_md_page_valid(ctx) == true) {
1780 		if (ctx->page->sequence_num == 0 || ctx->in_page_chain == true) {
1781 			spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
1782 			if (_spdk_bs_load_replay_md_parse_page(ctx->page, ctx->bs)) {
1783 				spdk_dma_free(ctx->super);
1784 				_spdk_bs_free(ctx->bs);
1785 				free(ctx);
1786 				spdk_bs_sequence_finish(seq, -EILSEQ);
1787 				return;
1788 			}
1789 			if (ctx->page->next != SPDK_INVALID_MD_PAGE) {
1790 				ctx->in_page_chain = true;
1791 				ctx->cur_page = ctx->page->next;
1792 				_spdk_bs_load_replay_cur_md_page(seq, cb_arg);
1793 				return;
1794 			}
1795 		}
1796 	}
1797 
1798 	ctx->in_page_chain = false;
1799 
1800 	do {
1801 		ctx->page_index++;
1802 	} while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
1803 
1804 	if (ctx->page_index < ctx->super->md_len) {
1805 		ctx->cur_page = ctx->page_index;
1806 		_spdk_bs_load_replay_cur_md_page(seq, cb_arg);
1807 	} else {
1808 		spdk_dma_free(ctx->page);
1809 		_spdk_bs_load_write_used_md(seq, ctx, bserrno);
1810 	}
1811 }
1812 
1813 static void
1814 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
1815 {
1816 	struct spdk_bs_load_ctx *ctx = cb_arg;
1817 	uint64_t lba;
1818 
1819 	assert(ctx->cur_page < ctx->super->md_len);
1820 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
1821 	spdk_bs_sequence_read(seq, ctx->page, lba,
1822 			      _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
1823 			      _spdk_bs_load_replay_md_cpl, ctx);
1824 }
1825 
1826 static void
1827 _spdk_bs_load_replay_md(spdk_bs_sequence_t *seq, void *cb_arg)
1828 {
1829 	struct spdk_bs_load_ctx *ctx = cb_arg;
1830 
1831 	ctx->page_index = 0;
1832 	ctx->cur_page = 0;
1833 	ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE,
1834 				     SPDK_BS_PAGE_SIZE,
1835 				     NULL);
1836 	if (!ctx->page) {
1837 		spdk_dma_free(ctx->super);
1838 		_spdk_bs_free(ctx->bs);
1839 		free(ctx);
1840 		spdk_bs_sequence_finish(seq, -ENOMEM);
1841 		return;
1842 	}
1843 	_spdk_bs_load_replay_cur_md_page(seq, cb_arg);
1844 }
1845 
1846 static void
1847 _spdk_bs_recover(spdk_bs_sequence_t *seq, void *cb_arg)
1848 {
1849 	struct spdk_bs_load_ctx *ctx = cb_arg;
1850 	int 		rc;
1851 
1852 	rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
1853 	if (rc < 0) {
1854 		spdk_dma_free(ctx->super);
1855 		_spdk_bs_free(ctx->bs);
1856 		free(ctx);
1857 		spdk_bs_sequence_finish(seq, -ENOMEM);
1858 		return;
1859 	}
1860 
1861 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
1862 	if (rc < 0) {
1863 		spdk_dma_free(ctx->super);
1864 		_spdk_bs_free(ctx->bs);
1865 		free(ctx);
1866 		spdk_bs_sequence_finish(seq, -ENOMEM);
1867 		return;
1868 	}
1869 
1870 	ctx->bs->num_free_clusters = ctx->bs->total_clusters;
1871 	_spdk_bs_load_replay_md(seq, cb_arg);
1872 }
1873 
1874 static void
1875 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1876 {
1877 	struct spdk_bs_load_ctx *ctx = cb_arg;
1878 	uint32_t	crc;
1879 	static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
1880 
1881 	if (ctx->super->version > SPDK_BS_VERSION ||
1882 	    ctx->super->version < SPDK_BS_INITIAL_VERSION) {
1883 		spdk_dma_free(ctx->super);
1884 		_spdk_bs_free(ctx->bs);
1885 		free(ctx);
1886 		spdk_bs_sequence_finish(seq, -EILSEQ);
1887 		return;
1888 	}
1889 
1890 	if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
1891 		   sizeof(ctx->super->signature)) != 0) {
1892 		spdk_dma_free(ctx->super);
1893 		_spdk_bs_free(ctx->bs);
1894 		free(ctx);
1895 		spdk_bs_sequence_finish(seq, -EILSEQ);
1896 		return;
1897 	}
1898 
1899 	crc = _spdk_blob_md_page_calc_crc(ctx->super);
1900 	if (crc != ctx->super->crc) {
1901 		spdk_dma_free(ctx->super);
1902 		_spdk_bs_free(ctx->bs);
1903 		free(ctx);
1904 		spdk_bs_sequence_finish(seq, -EILSEQ);
1905 		return;
1906 	}
1907 
1908 	if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1909 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Bstype matched - loading blobstore\n");
1910 	} else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
1911 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n");
1912 	} else {
1913 		SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Unexpected bstype\n");
1914 		SPDK_TRACEDUMP(SPDK_TRACE_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1915 		SPDK_TRACEDUMP(SPDK_TRACE_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
1916 		spdk_dma_free(ctx->super);
1917 		_spdk_bs_free(ctx->bs);
1918 		free(ctx);
1919 		spdk_bs_sequence_finish(seq, -ENXIO);
1920 		return;
1921 	}
1922 
1923 	/* Parse the super block */
1924 	ctx->bs->cluster_sz = ctx->super->cluster_size;
1925 	ctx->bs->total_clusters = ctx->bs->dev->blockcnt / (ctx->bs->cluster_sz / ctx->bs->dev->blocklen);
1926 	ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
1927 	ctx->bs->md_start = ctx->super->md_start;
1928 	ctx->bs->md_len = ctx->super->md_len;
1929 	ctx->bs->super_blob = ctx->super->super_blob;
1930 	memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
1931 
1932 	if (ctx->super->clean == 1) {
1933 		ctx->super->clean = 0;
1934 		_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_load_write_super_cpl, ctx);
1935 	} else {
1936 		_spdk_bs_recover(seq, ctx);
1937 	}
1938 }
1939 
1940 void
1941 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
1942 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
1943 {
1944 	struct spdk_blob_store	*bs;
1945 	struct spdk_bs_cpl	cpl;
1946 	spdk_bs_sequence_t	*seq;
1947 	struct spdk_bs_load_ctx *ctx;
1948 	struct spdk_bs_opts	opts = {};
1949 
1950 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Loading blobstore from dev %p\n", dev);
1951 
1952 	if (o) {
1953 		opts = *o;
1954 	} else {
1955 		spdk_bs_opts_init(&opts);
1956 	}
1957 
1958 	if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
1959 		cb_fn(cb_arg, NULL, -EINVAL);
1960 		return;
1961 	}
1962 
1963 	bs = _spdk_bs_alloc(dev, &opts);
1964 	if (!bs) {
1965 		cb_fn(cb_arg, NULL, -ENOMEM);
1966 		return;
1967 	}
1968 
1969 	ctx = calloc(1, sizeof(*ctx));
1970 	if (!ctx) {
1971 		_spdk_bs_free(bs);
1972 		cb_fn(cb_arg, NULL, -ENOMEM);
1973 		return;
1974 	}
1975 
1976 	ctx->bs = bs;
1977 
1978 	/* Allocate memory for the super block */
1979 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
1980 	if (!ctx->super) {
1981 		free(ctx);
1982 		_spdk_bs_free(bs);
1983 		return;
1984 	}
1985 
1986 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
1987 	cpl.u.bs_handle.cb_fn = cb_fn;
1988 	cpl.u.bs_handle.cb_arg = cb_arg;
1989 	cpl.u.bs_handle.bs = bs;
1990 
1991 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
1992 	if (!seq) {
1993 		spdk_dma_free(ctx->super);
1994 		free(ctx);
1995 		_spdk_bs_free(bs);
1996 		cb_fn(cb_arg, NULL, -ENOMEM);
1997 		return;
1998 	}
1999 
2000 	/* Read the super block */
2001 	spdk_bs_sequence_read(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
2002 			      _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
2003 			      _spdk_bs_load_super_cpl, ctx);
2004 }
2005 
2006 /* END spdk_bs_load */
2007 
2008 /* START spdk_bs_init */
2009 
2010 struct spdk_bs_init_ctx {
2011 	struct spdk_blob_store		*bs;
2012 	struct spdk_bs_super_block	*super;
2013 };
2014 
2015 static void
2016 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2017 {
2018 	struct spdk_bs_init_ctx *ctx = cb_arg;
2019 
2020 	spdk_dma_free(ctx->super);
2021 	free(ctx);
2022 
2023 	spdk_bs_sequence_finish(seq, bserrno);
2024 }
2025 
2026 static void
2027 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2028 {
2029 	struct spdk_bs_init_ctx *ctx = cb_arg;
2030 
2031 	/* Write super block */
2032 	spdk_bs_sequence_write(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0),
2033 			       _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
2034 			       _spdk_bs_init_persist_super_cpl, ctx);
2035 }
2036 
2037 void
2038 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
2039 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
2040 {
2041 	struct spdk_bs_init_ctx *ctx;
2042 	struct spdk_blob_store	*bs;
2043 	struct spdk_bs_cpl	cpl;
2044 	spdk_bs_sequence_t	*seq;
2045 	uint64_t		num_md_pages;
2046 	uint64_t		num_md_clusters;
2047 	uint32_t		i;
2048 	struct spdk_bs_opts	opts = {};
2049 	int			rc;
2050 
2051 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Initializing blobstore on dev %p\n", dev);
2052 
2053 	if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
2054 		SPDK_ERRLOG("unsupported dev block length of %d\n",
2055 			    dev->blocklen);
2056 		dev->destroy(dev);
2057 		cb_fn(cb_arg, NULL, -EINVAL);
2058 		return;
2059 	}
2060 
2061 	if (o) {
2062 		opts = *o;
2063 	} else {
2064 		spdk_bs_opts_init(&opts);
2065 	}
2066 
2067 	if (_spdk_bs_opts_verify(&opts) != 0) {
2068 		dev->destroy(dev);
2069 		cb_fn(cb_arg, NULL, -EINVAL);
2070 		return;
2071 	}
2072 
2073 	bs = _spdk_bs_alloc(dev, &opts);
2074 	if (!bs) {
2075 		dev->destroy(dev);
2076 		cb_fn(cb_arg, NULL, -ENOMEM);
2077 		return;
2078 	}
2079 
2080 	if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
2081 		/* By default, allocate 1 page per cluster.
2082 		 * Technically, this over-allocates metadata
2083 		 * because more metadata will reduce the number
2084 		 * of usable clusters. This can be addressed with
2085 		 * more complex math in the future.
2086 		 */
2087 		bs->md_len = bs->total_clusters;
2088 	} else {
2089 		bs->md_len = opts.num_md_pages;
2090 	}
2091 
2092 	rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
2093 	if (rc < 0) {
2094 		_spdk_bs_free(bs);
2095 		cb_fn(cb_arg, NULL, -ENOMEM);
2096 		return;
2097 	}
2098 
2099 	ctx = calloc(1, sizeof(*ctx));
2100 	if (!ctx) {
2101 		_spdk_bs_free(bs);
2102 		cb_fn(cb_arg, NULL, -ENOMEM);
2103 		return;
2104 	}
2105 
2106 	ctx->bs = bs;
2107 
2108 	/* Allocate memory for the super block */
2109 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
2110 	if (!ctx->super) {
2111 		free(ctx);
2112 		_spdk_bs_free(bs);
2113 		return;
2114 	}
2115 	memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
2116 	       sizeof(ctx->super->signature));
2117 	ctx->super->version = SPDK_BS_VERSION;
2118 	ctx->super->length = sizeof(*ctx->super);
2119 	ctx->super->super_blob = bs->super_blob;
2120 	ctx->super->clean = 0;
2121 	ctx->super->cluster_size = bs->cluster_sz;
2122 	memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
2123 
2124 	/* Calculate how many pages the metadata consumes at the front
2125 	 * of the disk.
2126 	 */
2127 
2128 	/* The super block uses 1 page */
2129 	num_md_pages = 1;
2130 
2131 	/* The used_md_pages mask requires 1 bit per metadata page, rounded
2132 	 * up to the nearest page, plus a header.
2133 	 */
2134 	ctx->super->used_page_mask_start = num_md_pages;
2135 	ctx->super->used_page_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
2136 					 divide_round_up(bs->md_len, 8),
2137 					 SPDK_BS_PAGE_SIZE);
2138 	num_md_pages += ctx->super->used_page_mask_len;
2139 
2140 	/* The used_clusters mask requires 1 bit per cluster, rounded
2141 	 * up to the nearest page, plus a header.
2142 	 */
2143 	ctx->super->used_cluster_mask_start = num_md_pages;
2144 	ctx->super->used_cluster_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
2145 					    divide_round_up(bs->total_clusters, 8),
2146 					    SPDK_BS_PAGE_SIZE);
2147 	num_md_pages += ctx->super->used_cluster_mask_len;
2148 
2149 	/* The metadata region size was chosen above */
2150 	ctx->super->md_start = bs->md_start = num_md_pages;
2151 	ctx->super->md_len = bs->md_len;
2152 	num_md_pages += bs->md_len;
2153 
2154 	ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super);
2155 
2156 	num_md_clusters = divide_round_up(num_md_pages, bs->pages_per_cluster);
2157 	if (num_md_clusters > bs->total_clusters) {
2158 		SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
2159 			    "please decrease number of pages reserved for metadata "
2160 			    "or increase cluster size.\n");
2161 		spdk_dma_free(ctx->super);
2162 		free(ctx);
2163 		_spdk_bs_free(bs);
2164 		cb_fn(cb_arg, NULL, -ENOMEM);
2165 		return;
2166 	}
2167 	/* Claim all of the clusters used by the metadata */
2168 	for (i = 0; i < num_md_clusters; i++) {
2169 		_spdk_bs_claim_cluster(bs, i);
2170 	}
2171 
2172 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
2173 	cpl.u.bs_handle.cb_fn = cb_fn;
2174 	cpl.u.bs_handle.cb_arg = cb_arg;
2175 	cpl.u.bs_handle.bs = bs;
2176 
2177 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2178 	if (!seq) {
2179 		spdk_dma_free(ctx->super);
2180 		free(ctx);
2181 		_spdk_bs_free(bs);
2182 		cb_fn(cb_arg, NULL, -ENOMEM);
2183 		return;
2184 	}
2185 
2186 	/* Zero the entire device */
2187 	spdk_bs_sequence_write_zeroes(seq, 0, bs->dev->blockcnt, _spdk_bs_init_trim_cpl, ctx);
2188 }
2189 
2190 /* END spdk_bs_init */
2191 
2192 /* START spdk_bs_destroy */
2193 
2194 static void
2195 _spdk_bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2196 {
2197 	struct spdk_bs_init_ctx *ctx = cb_arg;
2198 	struct spdk_blob_store *bs = ctx->bs;
2199 
2200 	/*
2201 	 * We need to defer calling spdk_bs_call_cpl() until after
2202 	 * dev destruction, so tuck these away for later use.
2203 	 */
2204 	bs->unload_err = bserrno;
2205 	memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
2206 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
2207 
2208 	spdk_bs_sequence_finish(seq, bserrno);
2209 
2210 	_spdk_bs_free(bs);
2211 	spdk_dma_free(ctx->super);
2212 	free(ctx);
2213 }
2214 
2215 void
2216 spdk_bs_destroy(struct spdk_blob_store *bs, bool unmap_device, spdk_bs_op_complete cb_fn,
2217 		void *cb_arg)
2218 {
2219 	struct spdk_bs_cpl	cpl;
2220 	spdk_bs_sequence_t	*seq;
2221 	struct spdk_bs_init_ctx *ctx;
2222 
2223 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Destroying blobstore\n");
2224 
2225 	if (!TAILQ_EMPTY(&bs->blobs)) {
2226 		SPDK_ERRLOG("Blobstore still has open blobs\n");
2227 		cb_fn(cb_arg, -EBUSY);
2228 		return;
2229 	}
2230 
2231 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
2232 	cpl.u.bs_basic.cb_fn = cb_fn;
2233 	cpl.u.bs_basic.cb_arg = cb_arg;
2234 
2235 	ctx = calloc(1, sizeof(*ctx));
2236 	if (!ctx) {
2237 		cb_fn(cb_arg, -ENOMEM);
2238 		return;
2239 	}
2240 
2241 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
2242 	if (!ctx->super) {
2243 		free(ctx);
2244 		cb_fn(cb_arg, -ENOMEM);
2245 		return;
2246 	}
2247 
2248 	ctx->bs = bs;
2249 
2250 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2251 	if (!seq) {
2252 		spdk_dma_free(ctx->super);
2253 		free(ctx);
2254 		cb_fn(cb_arg, -ENOMEM);
2255 		return;
2256 	}
2257 
2258 	if (unmap_device) {
2259 		/* TRIM the entire device */
2260 		spdk_bs_sequence_unmap(seq, 0,  bs->dev->blockcnt,  _spdk_bs_destroy_trim_cpl, ctx);
2261 	} else {
2262 		/* Write zeroes to the super block */
2263 		spdk_bs_sequence_write(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0), _spdk_bs_byte_to_lba(bs,
2264 				       sizeof(*ctx->super)), _spdk_bs_destroy_trim_cpl, ctx);
2265 	}
2266 }
2267 
2268 /* END spdk_bs_destroy */
2269 
2270 /* START spdk_bs_unload */
2271 
2272 static void
2273 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2274 {
2275 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2276 
2277 	spdk_dma_free(ctx->super);
2278 
2279 	/*
2280 	 * We need to defer calling spdk_bs_call_cpl() until after
2281 	 * dev destuction, so tuck these away for later use.
2282 	 */
2283 	ctx->bs->unload_err = bserrno;
2284 	memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
2285 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
2286 
2287 	spdk_bs_sequence_finish(seq, bserrno);
2288 
2289 	_spdk_bs_free(ctx->bs);
2290 	free(ctx);
2291 }
2292 
2293 static void
2294 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2295 {
2296 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2297 
2298 	spdk_dma_free(ctx->mask);
2299 	ctx->super->clean = 1;
2300 
2301 	_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx);
2302 }
2303 
2304 static void
2305 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2306 {
2307 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2308 
2309 	spdk_dma_free(ctx->mask);
2310 
2311 	_spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_unload_write_used_clusters_cpl);
2312 }
2313 
2314 static void
2315 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2316 {
2317 	_spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl);
2318 }
2319 
2320 void
2321 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
2322 {
2323 	struct spdk_bs_cpl	cpl;
2324 	spdk_bs_sequence_t	*seq;
2325 	struct spdk_bs_load_ctx *ctx;
2326 
2327 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Syncing blobstore\n");
2328 
2329 	if (!TAILQ_EMPTY(&bs->blobs)) {
2330 		SPDK_ERRLOG("Blobstore still has open blobs\n");
2331 		cb_fn(cb_arg, -EBUSY);
2332 		return;
2333 	}
2334 
2335 	ctx = calloc(1, sizeof(*ctx));
2336 	if (!ctx) {
2337 		cb_fn(cb_arg, -ENOMEM);
2338 		return;
2339 	}
2340 
2341 	ctx->bs = bs;
2342 
2343 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
2344 	if (!ctx->super) {
2345 		free(ctx);
2346 		cb_fn(cb_arg, -ENOMEM);
2347 		return;
2348 	}
2349 
2350 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
2351 	cpl.u.bs_basic.cb_fn = cb_fn;
2352 	cpl.u.bs_basic.cb_arg = cb_arg;
2353 
2354 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2355 	if (!seq) {
2356 		spdk_dma_free(ctx->super);
2357 		free(ctx);
2358 		cb_fn(cb_arg, -ENOMEM);
2359 		return;
2360 	}
2361 
2362 	/* Read super block */
2363 	spdk_bs_sequence_read(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
2364 			      _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
2365 			      _spdk_bs_unload_read_super_cpl, ctx);
2366 }
2367 
2368 /* END spdk_bs_unload */
2369 
2370 void
2371 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
2372 		  spdk_bs_op_complete cb_fn, void *cb_arg)
2373 {
2374 	bs->super_blob = blobid;
2375 	cb_fn(cb_arg, 0);
2376 }
2377 
2378 void
2379 spdk_bs_get_super(struct spdk_blob_store *bs,
2380 		  spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
2381 {
2382 	if (bs->super_blob == SPDK_BLOBID_INVALID) {
2383 		cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
2384 	} else {
2385 		cb_fn(cb_arg, bs->super_blob, 0);
2386 	}
2387 }
2388 
2389 uint64_t
2390 spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
2391 {
2392 	return bs->cluster_sz;
2393 }
2394 
2395 uint64_t
2396 spdk_bs_get_page_size(struct spdk_blob_store *bs)
2397 {
2398 	return SPDK_BS_PAGE_SIZE;
2399 }
2400 
2401 uint64_t
2402 spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
2403 {
2404 	return bs->num_free_clusters;
2405 }
2406 
2407 int spdk_bs_register_md_thread(struct spdk_blob_store *bs)
2408 {
2409 	bs->md_target.md_channel = spdk_get_io_channel(&bs->md_target);
2410 
2411 	return 0;
2412 }
2413 
2414 int spdk_bs_unregister_md_thread(struct spdk_blob_store *bs)
2415 {
2416 	spdk_put_io_channel(bs->md_target.md_channel);
2417 
2418 	return 0;
2419 }
2420 
2421 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob)
2422 {
2423 	assert(blob != NULL);
2424 
2425 	return blob->id;
2426 }
2427 
2428 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob)
2429 {
2430 	assert(blob != NULL);
2431 
2432 	return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters);
2433 }
2434 
2435 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob)
2436 {
2437 	assert(blob != NULL);
2438 
2439 	return blob->active.num_clusters;
2440 }
2441 
2442 /* START spdk_bs_md_create_blob */
2443 
2444 static void
2445 _spdk_bs_md_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2446 {
2447 	struct spdk_blob *blob = cb_arg;
2448 
2449 	_spdk_blob_free(blob);
2450 
2451 	spdk_bs_sequence_finish(seq, bserrno);
2452 }
2453 
2454 void spdk_bs_md_create_blob(struct spdk_blob_store *bs,
2455 			    spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
2456 {
2457 	struct spdk_blob	*blob;
2458 	uint32_t		page_idx;
2459 	struct spdk_bs_cpl 	cpl;
2460 	spdk_bs_sequence_t	*seq;
2461 	spdk_blob_id		id;
2462 
2463 	page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
2464 	if (page_idx >= spdk_bit_array_capacity(bs->used_md_pages)) {
2465 		cb_fn(cb_arg, 0, -ENOMEM);
2466 		return;
2467 	}
2468 	spdk_bit_array_set(bs->used_md_pages, page_idx);
2469 
2470 	id = _spdk_bs_page_to_blobid(page_idx);
2471 
2472 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx);
2473 
2474 	blob = _spdk_blob_alloc(bs, id);
2475 	if (!blob) {
2476 		cb_fn(cb_arg, 0, -ENOMEM);
2477 		return;
2478 	}
2479 
2480 	cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
2481 	cpl.u.blobid.cb_fn = cb_fn;
2482 	cpl.u.blobid.cb_arg = cb_arg;
2483 	cpl.u.blobid.blobid = blob->id;
2484 
2485 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2486 	if (!seq) {
2487 		_spdk_blob_free(blob);
2488 		cb_fn(cb_arg, 0, -ENOMEM);
2489 		return;
2490 	}
2491 
2492 	_spdk_blob_persist(seq, blob, _spdk_bs_md_create_blob_cpl, blob);
2493 }
2494 
2495 /* END spdk_bs_md_create_blob */
2496 
2497 /* START spdk_bs_md_resize_blob */
2498 int
2499 spdk_bs_md_resize_blob(struct spdk_blob *blob, uint64_t sz)
2500 {
2501 	int			rc;
2502 
2503 	assert(blob != NULL);
2504 
2505 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz);
2506 
2507 	if (sz == blob->active.num_clusters) {
2508 		return 0;
2509 	}
2510 
2511 	rc = _spdk_resize_blob(blob, sz);
2512 	if (rc < 0) {
2513 		return rc;
2514 	}
2515 
2516 	return 0;
2517 }
2518 
2519 /* END spdk_bs_md_resize_blob */
2520 
2521 
2522 /* START spdk_bs_md_delete_blob */
2523 
2524 static void
2525 _spdk_bs_md_delete_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2526 {
2527 	struct spdk_blob *blob = cb_arg;
2528 
2529 	_spdk_blob_free(blob);
2530 
2531 	spdk_bs_sequence_finish(seq, bserrno);
2532 }
2533 
2534 static void
2535 _spdk_bs_md_delete_open_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2536 {
2537 	struct spdk_blob *blob = cb_arg;
2538 
2539 	/* If the blob have crc error, we just return NULL. */
2540 	if (blob == NULL) {
2541 		spdk_bs_sequence_finish(seq, bserrno);
2542 		return;
2543 	}
2544 	blob->state = SPDK_BLOB_STATE_DIRTY;
2545 	blob->active.num_pages = 0;
2546 	_spdk_resize_blob(blob, 0);
2547 
2548 	_spdk_blob_persist(seq, blob, _spdk_bs_md_delete_blob_cpl, blob);
2549 }
2550 
2551 void
2552 spdk_bs_md_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
2553 		       spdk_blob_op_complete cb_fn, void *cb_arg)
2554 {
2555 	struct spdk_blob	*blob;
2556 	struct spdk_bs_cpl	cpl;
2557 	spdk_bs_sequence_t 	*seq;
2558 
2559 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Deleting blob %lu\n", blobid);
2560 
2561 	blob = _spdk_blob_lookup(bs, blobid);
2562 	if (blob) {
2563 		assert(blob->open_ref > 0);
2564 		cb_fn(cb_arg, -EINVAL);
2565 		return;
2566 	}
2567 
2568 	blob = _spdk_blob_alloc(bs, blobid);
2569 	if (!blob) {
2570 		cb_fn(cb_arg, -ENOMEM);
2571 		return;
2572 	}
2573 
2574 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2575 	cpl.u.blob_basic.cb_fn = cb_fn;
2576 	cpl.u.blob_basic.cb_arg = cb_arg;
2577 
2578 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2579 	if (!seq) {
2580 		_spdk_blob_free(blob);
2581 		cb_fn(cb_arg, -ENOMEM);
2582 		return;
2583 	}
2584 
2585 	_spdk_blob_load(seq, blob, _spdk_bs_md_delete_open_cpl, blob);
2586 }
2587 
2588 /* END spdk_bs_md_delete_blob */
2589 
2590 /* START spdk_bs_md_open_blob */
2591 
2592 static void
2593 _spdk_bs_md_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2594 {
2595 	struct spdk_blob *blob = cb_arg;
2596 
2597 	/* If the blob have crc error, we just return NULL. */
2598 	if (blob == NULL) {
2599 		seq->cpl.u.blob_handle.blob = NULL;
2600 		spdk_bs_sequence_finish(seq, bserrno);
2601 		return;
2602 	}
2603 
2604 	blob->open_ref++;
2605 
2606 	TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link);
2607 
2608 	spdk_bs_sequence_finish(seq, bserrno);
2609 }
2610 
2611 void spdk_bs_md_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
2612 			  spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
2613 {
2614 	struct spdk_blob		*blob;
2615 	struct spdk_bs_cpl		cpl;
2616 	spdk_bs_sequence_t		*seq;
2617 	uint32_t			page_num;
2618 
2619 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Opening blob %lu\n", blobid);
2620 
2621 	blob = _spdk_blob_lookup(bs, blobid);
2622 	if (blob) {
2623 		blob->open_ref++;
2624 		cb_fn(cb_arg, blob, 0);
2625 		return;
2626 	}
2627 
2628 	page_num = _spdk_bs_blobid_to_page(blobid);
2629 	if (spdk_bit_array_get(bs->used_md_pages, page_num) == false) {
2630 		/* Invalid blobid */
2631 		cb_fn(cb_arg, NULL, -ENOENT);
2632 		return;
2633 	}
2634 
2635 	blob = _spdk_blob_alloc(bs, blobid);
2636 	if (!blob) {
2637 		cb_fn(cb_arg, NULL, -ENOMEM);
2638 		return;
2639 	}
2640 
2641 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
2642 	cpl.u.blob_handle.cb_fn = cb_fn;
2643 	cpl.u.blob_handle.cb_arg = cb_arg;
2644 	cpl.u.blob_handle.blob = blob;
2645 
2646 	seq = spdk_bs_sequence_start(bs->md_target.md_channel, &cpl);
2647 	if (!seq) {
2648 		_spdk_blob_free(blob);
2649 		cb_fn(cb_arg, NULL, -ENOMEM);
2650 		return;
2651 	}
2652 
2653 	_spdk_blob_load(seq, blob, _spdk_bs_md_open_blob_cpl, blob);
2654 }
2655 
2656 /* START spdk_bs_md_sync_blob */
2657 static void
2658 _spdk_blob_sync_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2659 {
2660 	spdk_bs_sequence_finish(seq, bserrno);
2661 }
2662 
2663 void spdk_bs_md_sync_blob(struct spdk_blob *blob,
2664 			  spdk_blob_op_complete cb_fn, void *cb_arg)
2665 {
2666 	struct spdk_bs_cpl	cpl;
2667 	spdk_bs_sequence_t	*seq;
2668 
2669 	assert(blob != NULL);
2670 
2671 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Syncing blob %lu\n", blob->id);
2672 
2673 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2674 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2675 
2676 	if (blob->state == SPDK_BLOB_STATE_CLEAN) {
2677 		cb_fn(cb_arg, 0);
2678 		return;
2679 	}
2680 
2681 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2682 	cpl.u.blob_basic.cb_fn = cb_fn;
2683 	cpl.u.blob_basic.cb_arg = cb_arg;
2684 
2685 	seq = spdk_bs_sequence_start(blob->bs->md_target.md_channel, &cpl);
2686 	if (!seq) {
2687 		cb_fn(cb_arg, -ENOMEM);
2688 		return;
2689 	}
2690 
2691 	_spdk_blob_persist(seq, blob, _spdk_blob_sync_cpl, blob);
2692 }
2693 
2694 /* END spdk_bs_md_sync_blob */
2695 
2696 /* START spdk_bs_md_close_blob */
2697 
2698 static void
2699 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2700 {
2701 	struct spdk_blob **blob = cb_arg;
2702 
2703 	if ((*blob)->open_ref == 0) {
2704 		TAILQ_REMOVE(&(*blob)->bs->blobs, (*blob), link);
2705 		_spdk_blob_free((*blob));
2706 	}
2707 
2708 	*blob = NULL;
2709 
2710 	spdk_bs_sequence_finish(seq, bserrno);
2711 }
2712 
2713 void spdk_bs_md_close_blob(struct spdk_blob **b,
2714 			   spdk_blob_op_complete cb_fn, void *cb_arg)
2715 {
2716 	struct spdk_bs_cpl	cpl;
2717 	struct spdk_blob	*blob;
2718 	spdk_bs_sequence_t	*seq;
2719 
2720 	assert(b != NULL);
2721 	blob = *b;
2722 	assert(blob != NULL);
2723 
2724 	SPDK_DEBUGLOG(SPDK_TRACE_BLOB, "Closing blob %lu\n", blob->id);
2725 
2726 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2727 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2728 
2729 	if (blob->open_ref == 0) {
2730 		cb_fn(cb_arg, -EBADF);
2731 		return;
2732 	}
2733 
2734 	blob->open_ref--;
2735 
2736 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2737 	cpl.u.blob_basic.cb_fn = cb_fn;
2738 	cpl.u.blob_basic.cb_arg = cb_arg;
2739 
2740 	seq = spdk_bs_sequence_start(blob->bs->md_target.md_channel, &cpl);
2741 	if (!seq) {
2742 		cb_fn(cb_arg, -ENOMEM);
2743 		return;
2744 	}
2745 
2746 	if (blob->state == SPDK_BLOB_STATE_CLEAN) {
2747 		_spdk_blob_close_cpl(seq, b, 0);
2748 		return;
2749 	}
2750 
2751 	/* Sync metadata */
2752 	_spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, b);
2753 }
2754 
2755 /* END spdk_bs_md_close_blob */
2756 
2757 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
2758 {
2759 	return spdk_get_io_channel(&bs->io_target);
2760 }
2761 
2762 void spdk_bs_free_io_channel(struct spdk_io_channel *channel)
2763 {
2764 	spdk_put_io_channel(channel);
2765 }
2766 
2767 void spdk_bs_io_flush_channel(struct spdk_io_channel *channel,
2768 			      spdk_blob_op_complete cb_fn, void *cb_arg)
2769 {
2770 	/* Flush is synchronous right now */
2771 	cb_fn(cb_arg, 0);
2772 }
2773 
2774 void spdk_bs_io_write_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2775 			   void *payload, uint64_t offset, uint64_t length,
2776 			   spdk_blob_op_complete cb_fn, void *cb_arg)
2777 {
2778 	_spdk_blob_request_submit_rw(blob, channel, payload, offset, length, cb_fn, cb_arg, false);
2779 }
2780 
2781 void spdk_bs_io_read_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2782 			  void *payload, uint64_t offset, uint64_t length,
2783 			  spdk_blob_op_complete cb_fn, void *cb_arg)
2784 {
2785 	_spdk_blob_request_submit_rw(blob, channel, payload, offset, length, cb_fn, cb_arg, true);
2786 }
2787 
2788 void spdk_bs_io_writev_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2789 			    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
2790 			    spdk_blob_op_complete cb_fn, void *cb_arg)
2791 {
2792 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false);
2793 }
2794 
2795 void spdk_bs_io_readv_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
2796 			   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
2797 			   spdk_blob_op_complete cb_fn, void *cb_arg)
2798 {
2799 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true);
2800 }
2801 
2802 struct spdk_bs_iter_ctx {
2803 	int64_t page_num;
2804 	struct spdk_blob_store *bs;
2805 
2806 	spdk_blob_op_with_handle_complete cb_fn;
2807 	void *cb_arg;
2808 };
2809 
2810 static void
2811 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
2812 {
2813 	struct spdk_bs_iter_ctx *ctx = cb_arg;
2814 	struct spdk_blob_store *bs = ctx->bs;
2815 	spdk_blob_id id;
2816 
2817 	if (bserrno == 0) {
2818 		ctx->cb_fn(ctx->cb_arg, blob, bserrno);
2819 		free(ctx);
2820 		return;
2821 	}
2822 
2823 	ctx->page_num++;
2824 	ctx->page_num = spdk_bit_array_find_first_set(bs->used_md_pages, ctx->page_num);
2825 	if (ctx->page_num >= spdk_bit_array_capacity(bs->used_md_pages)) {
2826 		ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
2827 		free(ctx);
2828 		return;
2829 	}
2830 
2831 	id = _spdk_bs_page_to_blobid(ctx->page_num);
2832 
2833 	blob = _spdk_blob_lookup(bs, id);
2834 	if (blob) {
2835 		blob->open_ref++;
2836 		ctx->cb_fn(ctx->cb_arg, blob, 0);
2837 		free(ctx);
2838 		return;
2839 	}
2840 
2841 	spdk_bs_md_open_blob(bs, id, _spdk_bs_iter_cpl, ctx);
2842 }
2843 
2844 void
2845 spdk_bs_md_iter_first(struct spdk_blob_store *bs,
2846 		      spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
2847 {
2848 	struct spdk_bs_iter_ctx *ctx;
2849 
2850 	ctx = calloc(1, sizeof(*ctx));
2851 	if (!ctx) {
2852 		cb_fn(cb_arg, NULL, -ENOMEM);
2853 		return;
2854 	}
2855 
2856 	ctx->page_num = -1;
2857 	ctx->bs = bs;
2858 	ctx->cb_fn = cb_fn;
2859 	ctx->cb_arg = cb_arg;
2860 
2861 	_spdk_bs_iter_cpl(ctx, NULL, -1);
2862 }
2863 
2864 static void
2865 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno)
2866 {
2867 	struct spdk_bs_iter_ctx *ctx = cb_arg;
2868 
2869 	_spdk_bs_iter_cpl(ctx, NULL, -1);
2870 }
2871 
2872 void
2873 spdk_bs_md_iter_next(struct spdk_blob_store *bs, struct spdk_blob **b,
2874 		     spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
2875 {
2876 	struct spdk_bs_iter_ctx *ctx;
2877 	struct spdk_blob	*blob;
2878 
2879 	assert(b != NULL);
2880 	blob = *b;
2881 	assert(blob != NULL);
2882 
2883 	ctx = calloc(1, sizeof(*ctx));
2884 	if (!ctx) {
2885 		cb_fn(cb_arg, NULL, -ENOMEM);
2886 		return;
2887 	}
2888 
2889 	ctx->page_num = _spdk_bs_blobid_to_page(blob->id);
2890 	ctx->bs = bs;
2891 	ctx->cb_fn = cb_fn;
2892 	ctx->cb_arg = cb_arg;
2893 
2894 	/* Close the existing blob */
2895 	spdk_bs_md_close_blob(b, _spdk_bs_iter_close_cpl, ctx);
2896 }
2897 
2898 int
2899 spdk_blob_md_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
2900 		       uint16_t value_len)
2901 {
2902 	struct spdk_xattr 	*xattr;
2903 
2904 	assert(blob != NULL);
2905 
2906 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2907 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2908 
2909 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2910 		if (!strcmp(name, xattr->name)) {
2911 			free(xattr->value);
2912 			xattr->value_len = value_len;
2913 			xattr->value = malloc(value_len);
2914 			memcpy(xattr->value, value, value_len);
2915 
2916 			blob->state = SPDK_BLOB_STATE_DIRTY;
2917 
2918 			return 0;
2919 		}
2920 	}
2921 
2922 	xattr = calloc(1, sizeof(*xattr));
2923 	if (!xattr) {
2924 		return -1;
2925 	}
2926 	xattr->name = strdup(name);
2927 	xattr->value_len = value_len;
2928 	xattr->value = malloc(value_len);
2929 	memcpy(xattr->value, value, value_len);
2930 	TAILQ_INSERT_TAIL(&blob->xattrs, xattr, link);
2931 
2932 	blob->state = SPDK_BLOB_STATE_DIRTY;
2933 
2934 	return 0;
2935 }
2936 
2937 int
2938 spdk_blob_md_remove_xattr(struct spdk_blob *blob, const char *name)
2939 {
2940 	struct spdk_xattr	*xattr;
2941 
2942 	assert(blob != NULL);
2943 
2944 	assert(blob->state != SPDK_BLOB_STATE_LOADING &&
2945 	       blob->state != SPDK_BLOB_STATE_SYNCING);
2946 
2947 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2948 		if (!strcmp(name, xattr->name)) {
2949 			TAILQ_REMOVE(&blob->xattrs, xattr, link);
2950 			free(xattr->value);
2951 			free(xattr->name);
2952 			free(xattr);
2953 
2954 			blob->state = SPDK_BLOB_STATE_DIRTY;
2955 
2956 			return 0;
2957 		}
2958 	}
2959 
2960 	return -ENOENT;
2961 }
2962 
2963 int
2964 spdk_bs_md_get_xattr_value(struct spdk_blob *blob, const char *name,
2965 			   const void **value, size_t *value_len)
2966 {
2967 	struct spdk_xattr	*xattr;
2968 
2969 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2970 		if (!strcmp(name, xattr->name)) {
2971 			*value = xattr->value;
2972 			*value_len = xattr->value_len;
2973 			return 0;
2974 		}
2975 	}
2976 
2977 	return -ENOENT;
2978 }
2979 
2980 struct spdk_xattr_names {
2981 	uint32_t	count;
2982 	const char	*names[0];
2983 };
2984 
2985 int
2986 spdk_bs_md_get_xattr_names(struct spdk_blob *blob,
2987 			   struct spdk_xattr_names **names)
2988 {
2989 	struct spdk_xattr	*xattr;
2990 	int			count = 0;
2991 
2992 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
2993 		count++;
2994 	}
2995 
2996 	*names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
2997 	if (*names == NULL) {
2998 		return -ENOMEM;
2999 	}
3000 
3001 	TAILQ_FOREACH(xattr, &blob->xattrs, link) {
3002 		(*names)->names[(*names)->count++] = xattr->name;
3003 	}
3004 
3005 	return 0;
3006 }
3007 
3008 uint32_t
3009 spdk_xattr_names_get_count(struct spdk_xattr_names *names)
3010 {
3011 	assert(names != NULL);
3012 
3013 	return names->count;
3014 }
3015 
3016 const char *
3017 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
3018 {
3019 	if (index >= names->count) {
3020 		return NULL;
3021 	}
3022 
3023 	return names->names[index];
3024 }
3025 
3026 void
3027 spdk_xattr_names_free(struct spdk_xattr_names *names)
3028 {
3029 	free(names);
3030 }
3031 
3032 struct spdk_bs_type
3033 spdk_bs_get_bstype(struct spdk_blob_store *bs)
3034 {
3035 	return bs->bstype;
3036 }
3037 
3038 void
3039 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
3040 {
3041 	memcpy(&bs->bstype, &bstype, sizeof(bstype));
3042 }
3043 
3044 SPDK_LOG_REGISTER_TRACE_FLAG("blob", SPDK_TRACE_BLOB);
3045