xref: /spdk/lib/blob/blobstore.c (revision ee8af4e9f3baa48b1edda539fe679a4bc08af69a)
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 int spdk_bs_register_md_thread(struct spdk_blob_store *bs);
51 static int spdk_bs_unregister_md_thread(struct spdk_blob_store *bs);
52 static void _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
53 void _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
54 		uint64_t cluster, spdk_blob_op_complete cb_fn, void *cb_arg);
55 
56 static int _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
57 				uint16_t value_len, bool internal);
58 static int _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
59 				      const void **value, size_t *value_len, bool internal);
60 static int _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal);
61 
62 static void
63 _spdk_blob_verify_md_op(struct spdk_blob *blob)
64 {
65 	assert(blob != NULL);
66 	assert(spdk_get_thread() == blob->bs->md_thread);
67 	assert(blob->state != SPDK_BLOB_STATE_LOADING);
68 }
69 
70 static inline size_t
71 divide_round_up(size_t num, size_t divisor)
72 {
73 	return (num + divisor - 1) / divisor;
74 }
75 
76 static void
77 _spdk_bs_claim_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
78 {
79 	assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters));
80 	assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == false);
81 	assert(bs->num_free_clusters > 0);
82 
83 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming cluster %u\n", cluster_num);
84 
85 	spdk_bit_array_set(bs->used_clusters, cluster_num);
86 	bs->num_free_clusters--;
87 }
88 
89 static int
90 _spdk_blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster)
91 {
92 	uint64_t *cluster_lba = &blob->active.clusters[cluster_num];
93 
94 	_spdk_blob_verify_md_op(blob);
95 
96 	if (*cluster_lba != 0) {
97 		return -EEXIST;
98 	}
99 
100 	*cluster_lba = _spdk_bs_cluster_to_lba(blob->bs, cluster);
101 	return 0;
102 }
103 
104 static int
105 _spdk_bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num,
106 			  uint64_t *lowest_free_cluster, bool update_map)
107 {
108 	pthread_mutex_lock(&blob->bs->used_clusters_mutex);
109 	*lowest_free_cluster = spdk_bit_array_find_first_clear(blob->bs->used_clusters,
110 			       *lowest_free_cluster);
111 	if (*lowest_free_cluster >= blob->bs->total_clusters) {
112 		/* No more free clusters. Cannot satisfy the request */
113 		pthread_mutex_unlock(&blob->bs->used_clusters_mutex);
114 		return -ENOSPC;
115 	}
116 
117 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming cluster %lu for blob %lu\n", *lowest_free_cluster, blob->id);
118 	_spdk_bs_claim_cluster(blob->bs, *lowest_free_cluster);
119 	pthread_mutex_unlock(&blob->bs->used_clusters_mutex);
120 
121 	if (update_map) {
122 		_spdk_blob_insert_cluster(blob, cluster_num, *lowest_free_cluster);
123 	}
124 
125 	return 0;
126 }
127 
128 static void
129 _spdk_bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
130 {
131 	assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters));
132 	assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == true);
133 	assert(bs->num_free_clusters < bs->total_clusters);
134 
135 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Releasing cluster %u\n", cluster_num);
136 
137 	pthread_mutex_lock(&bs->used_clusters_mutex);
138 	spdk_bit_array_clear(bs->used_clusters, cluster_num);
139 	bs->num_free_clusters++;
140 	pthread_mutex_unlock(&bs->used_clusters_mutex);
141 }
142 
143 void
144 spdk_blob_opts_init(struct spdk_blob_opts *opts)
145 {
146 	opts->num_clusters = 0;
147 	opts->thin_provision = false;
148 	opts->xattrs.count = 0;
149 	opts->xattrs.names = NULL;
150 	opts->xattrs.ctx = NULL;
151 	opts->xattrs.get_value = NULL;
152 }
153 
154 static struct spdk_blob *
155 _spdk_blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
156 {
157 	struct spdk_blob *blob;
158 
159 	blob = calloc(1, sizeof(*blob));
160 	if (!blob) {
161 		return NULL;
162 	}
163 
164 	blob->id = id;
165 	blob->bs = bs;
166 
167 	blob->state = SPDK_BLOB_STATE_DIRTY;
168 	blob->active.num_pages = 1;
169 	blob->active.pages = calloc(1, sizeof(*blob->active.pages));
170 	if (!blob->active.pages) {
171 		free(blob);
172 		return NULL;
173 	}
174 
175 	blob->active.pages[0] = _spdk_bs_blobid_to_page(id);
176 
177 	TAILQ_INIT(&blob->xattrs);
178 	TAILQ_INIT(&blob->xattrs_internal);
179 
180 	return blob;
181 }
182 
183 static void
184 _spdk_xattrs_free(struct spdk_xattr_tailq *xattrs)
185 {
186 	struct spdk_xattr 	*xattr, *xattr_tmp;
187 
188 	TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) {
189 		TAILQ_REMOVE(xattrs, xattr, link);
190 		free(xattr->name);
191 		free(xattr->value);
192 		free(xattr);
193 	}
194 }
195 
196 static void
197 _spdk_blob_free(struct spdk_blob *blob)
198 {
199 	assert(blob != NULL);
200 
201 	free(blob->active.clusters);
202 	free(blob->clean.clusters);
203 	free(blob->active.pages);
204 	free(blob->clean.pages);
205 
206 	_spdk_xattrs_free(&blob->xattrs);
207 	_spdk_xattrs_free(&blob->xattrs_internal);
208 
209 	free(blob);
210 }
211 
212 static int
213 _spdk_blob_mark_clean(struct spdk_blob *blob)
214 {
215 	uint64_t *clusters = NULL;
216 	uint32_t *pages = NULL;
217 
218 	assert(blob != NULL);
219 
220 	if (blob->active.num_clusters) {
221 		assert(blob->active.clusters);
222 		clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
223 		if (!clusters) {
224 			return -1;
225 		}
226 		memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*clusters));
227 	}
228 
229 	if (blob->active.num_pages) {
230 		assert(blob->active.pages);
231 		pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
232 		if (!pages) {
233 			free(clusters);
234 			return -1;
235 		}
236 		memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*pages));
237 	}
238 
239 	free(blob->clean.clusters);
240 	free(blob->clean.pages);
241 
242 	blob->clean.num_clusters = blob->active.num_clusters;
243 	blob->clean.clusters = blob->active.clusters;
244 	blob->clean.num_pages = blob->active.num_pages;
245 	blob->clean.pages = blob->active.pages;
246 
247 	blob->active.clusters = clusters;
248 	blob->active.pages = pages;
249 
250 	/* If the metadata was dirtied again while the metadata was being written to disk,
251 	 *  we do not want to revert the DIRTY state back to CLEAN here.
252 	 */
253 	if (blob->state == SPDK_BLOB_STATE_LOADING) {
254 		blob->state = SPDK_BLOB_STATE_CLEAN;
255 	}
256 
257 	return 0;
258 }
259 
260 static int
261 _spdk_blob_deserialize_xattr(struct spdk_blob *blob,
262 			     struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal)
263 {
264 	struct spdk_xattr                       *xattr;
265 
266 	if (desc_xattr->length != sizeof(desc_xattr->name_length) +
267 	    sizeof(desc_xattr->value_length) +
268 	    desc_xattr->name_length + desc_xattr->value_length) {
269 		return -EINVAL;
270 	}
271 
272 	xattr = calloc(1, sizeof(*xattr));
273 	if (xattr == NULL) {
274 		return -ENOMEM;
275 	}
276 
277 	xattr->name = malloc(desc_xattr->name_length + 1);
278 	if (xattr->name == NULL) {
279 		free(xattr);
280 		return -ENOMEM;
281 	}
282 	strncpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
283 	xattr->name[desc_xattr->name_length] = '\0';
284 
285 	xattr->value = malloc(desc_xattr->value_length);
286 	if (xattr->value == NULL) {
287 		free(xattr->name);
288 		free(xattr);
289 		return -ENOMEM;
290 	}
291 	xattr->value_len = desc_xattr->value_length;
292 	memcpy(xattr->value,
293 	       (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
294 	       desc_xattr->value_length);
295 
296 	TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link);
297 
298 	return 0;
299 }
300 
301 
302 static int
303 _spdk_blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
304 {
305 	struct spdk_blob_md_descriptor *desc;
306 	size_t	cur_desc = 0;
307 	void *tmp;
308 
309 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
310 	while (cur_desc < sizeof(page->descriptors)) {
311 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
312 			if (desc->length == 0) {
313 				/* If padding and length are 0, this terminates the page */
314 				break;
315 			}
316 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
317 			struct spdk_blob_md_descriptor_flags	*desc_flags;
318 
319 			desc_flags = (struct spdk_blob_md_descriptor_flags *)desc;
320 
321 			if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) {
322 				return -EINVAL;
323 			}
324 
325 			if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) !=
326 			    SPDK_BLOB_INVALID_FLAGS_MASK) {
327 				return -EINVAL;
328 			}
329 
330 			if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) !=
331 			    SPDK_BLOB_DATA_RO_FLAGS_MASK) {
332 				blob->data_ro = true;
333 				blob->md_ro = true;
334 			}
335 
336 			if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) !=
337 			    SPDK_BLOB_MD_RO_FLAGS_MASK) {
338 				blob->md_ro = true;
339 			}
340 
341 			if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
342 				blob->data_ro = true;
343 				blob->md_ro = true;
344 			}
345 
346 			blob->invalid_flags = desc_flags->invalid_flags;
347 			blob->data_ro_flags = desc_flags->data_ro_flags;
348 			blob->md_ro_flags = desc_flags->md_ro_flags;
349 
350 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) {
351 			struct spdk_blob_md_descriptor_extent	*desc_extent;
352 			unsigned int				i, j;
353 			unsigned int				cluster_count = blob->active.num_clusters;
354 
355 			desc_extent = (struct spdk_blob_md_descriptor_extent *)desc;
356 
357 			if (desc_extent->length == 0 ||
358 			    (desc_extent->length % sizeof(desc_extent->extents[0]) != 0)) {
359 				return -EINVAL;
360 			}
361 
362 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) {
363 				for (j = 0; j < desc_extent->extents[i].length; j++) {
364 					if (!spdk_bit_array_get(blob->bs->used_clusters,
365 								desc_extent->extents[i].cluster_idx + j)) {
366 						return -EINVAL;
367 					}
368 					cluster_count++;
369 				}
370 			}
371 
372 			if (cluster_count == 0) {
373 				return -EINVAL;
374 			}
375 			tmp = realloc(blob->active.clusters, cluster_count * sizeof(uint64_t));
376 			if (tmp == NULL) {
377 				return -ENOMEM;
378 			}
379 			blob->active.clusters = tmp;
380 			blob->active.cluster_array_size = cluster_count;
381 
382 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) {
383 				for (j = 0; j < desc_extent->extents[i].length; j++) {
384 					if (desc_extent->extents[i].cluster_idx != 0) {
385 						blob->active.clusters[blob->active.num_clusters++] = _spdk_bs_cluster_to_lba(blob->bs,
386 								desc_extent->extents[i].cluster_idx + j);
387 					} else if (spdk_blob_is_thin_provisioned(blob)) {
388 						blob->active.clusters[blob->active.num_clusters++] = 0;
389 					} else {
390 						return -EINVAL;
391 					}
392 				}
393 			}
394 
395 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
396 			int rc;
397 
398 			rc = _spdk_blob_deserialize_xattr(blob,
399 							  (struct spdk_blob_md_descriptor_xattr *) desc, false);
400 			if (rc != 0) {
401 				return rc;
402 			}
403 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
404 			int rc;
405 
406 			rc = _spdk_blob_deserialize_xattr(blob,
407 							  (struct spdk_blob_md_descriptor_xattr *) desc, true);
408 			if (rc != 0) {
409 				return rc;
410 			}
411 		} else {
412 			/* Unrecognized descriptor type.  Do not fail - just continue to the
413 			 *  next descriptor.  If this descriptor is associated with some feature
414 			 *  defined in a newer version of blobstore, that version of blobstore
415 			 *  should create and set an associated feature flag to specify if this
416 			 *  blob can be loaded or not.
417 			 */
418 		}
419 
420 		/* Advance to the next descriptor */
421 		cur_desc += sizeof(*desc) + desc->length;
422 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
423 			break;
424 		}
425 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
426 	}
427 
428 	return 0;
429 }
430 
431 static int
432 _spdk_blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
433 		 struct spdk_blob *blob)
434 {
435 	const struct spdk_blob_md_page *page;
436 	uint32_t i;
437 	int rc;
438 
439 	assert(page_count > 0);
440 	assert(pages[0].sequence_num == 0);
441 	assert(blob != NULL);
442 	assert(blob->state == SPDK_BLOB_STATE_LOADING);
443 	assert(blob->active.clusters == NULL);
444 
445 	/* The blobid provided doesn't match what's in the MD, this can
446 	 * happen for example if a bogus blobid is passed in through open.
447 	 */
448 	if (blob->id != pages[0].id) {
449 		SPDK_ERRLOG("Blobid (%lu) doesn't match what's in metadata (%lu)\n",
450 			    blob->id, pages[0].id);
451 		return -ENOENT;
452 	}
453 
454 	for (i = 0; i < page_count; i++) {
455 		page = &pages[i];
456 
457 		assert(page->id == blob->id);
458 		assert(page->sequence_num == i);
459 
460 		rc = _spdk_blob_parse_page(page, blob);
461 		if (rc != 0) {
462 			return rc;
463 		}
464 	}
465 
466 	return 0;
467 }
468 
469 static int
470 _spdk_blob_serialize_add_page(const struct spdk_blob *blob,
471 			      struct spdk_blob_md_page **pages,
472 			      uint32_t *page_count,
473 			      struct spdk_blob_md_page **last_page)
474 {
475 	struct spdk_blob_md_page *page;
476 
477 	assert(pages != NULL);
478 	assert(page_count != NULL);
479 
480 	if (*page_count == 0) {
481 		assert(*pages == NULL);
482 		*page_count = 1;
483 		*pages = spdk_dma_malloc(SPDK_BS_PAGE_SIZE,
484 					 SPDK_BS_PAGE_SIZE,
485 					 NULL);
486 	} else {
487 		assert(*pages != NULL);
488 		(*page_count)++;
489 		*pages = spdk_dma_realloc(*pages,
490 					  SPDK_BS_PAGE_SIZE * (*page_count),
491 					  SPDK_BS_PAGE_SIZE,
492 					  NULL);
493 	}
494 
495 	if (*pages == NULL) {
496 		*page_count = 0;
497 		*last_page = NULL;
498 		return -ENOMEM;
499 	}
500 
501 	page = &(*pages)[*page_count - 1];
502 	memset(page, 0, sizeof(*page));
503 	page->id = blob->id;
504 	page->sequence_num = *page_count - 1;
505 	page->next = SPDK_INVALID_MD_PAGE;
506 	*last_page = page;
507 
508 	return 0;
509 }
510 
511 /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
512  * Update required_sz on both success and failure.
513  *
514  */
515 static int
516 _spdk_blob_serialize_xattr(const struct spdk_xattr *xattr,
517 			   uint8_t *buf, size_t buf_sz,
518 			   size_t *required_sz, bool internal)
519 {
520 	struct spdk_blob_md_descriptor_xattr	*desc;
521 
522 	*required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
523 		       strlen(xattr->name) +
524 		       xattr->value_len;
525 
526 	if (buf_sz < *required_sz) {
527 		return -1;
528 	}
529 
530 	desc = (struct spdk_blob_md_descriptor_xattr *)buf;
531 
532 	desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR;
533 	desc->length = sizeof(desc->name_length) +
534 		       sizeof(desc->value_length) +
535 		       strlen(xattr->name) +
536 		       xattr->value_len;
537 	desc->name_length = strlen(xattr->name);
538 	desc->value_length = xattr->value_len;
539 
540 	memcpy(desc->name, xattr->name, desc->name_length);
541 	memcpy((void *)((uintptr_t)desc->name + desc->name_length),
542 	       xattr->value,
543 	       desc->value_length);
544 
545 	return 0;
546 }
547 
548 static void
549 _spdk_blob_serialize_extent(const struct spdk_blob *blob,
550 			    uint64_t start_cluster, uint64_t *next_cluster,
551 			    uint8_t *buf, size_t buf_sz)
552 {
553 	struct spdk_blob_md_descriptor_extent *desc;
554 	size_t cur_sz;
555 	uint64_t i, extent_idx;
556 	uint32_t lba, lba_per_cluster, lba_count;
557 
558 	/* The buffer must have room for at least one extent */
559 	cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->extents[0]);
560 	if (buf_sz < cur_sz) {
561 		*next_cluster = start_cluster;
562 		return;
563 	}
564 
565 	desc = (struct spdk_blob_md_descriptor_extent *)buf;
566 	desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT;
567 
568 	lba_per_cluster = _spdk_bs_cluster_to_lba(blob->bs, 1);
569 
570 	lba = blob->active.clusters[start_cluster];
571 	lba_count = lba_per_cluster;
572 	extent_idx = 0;
573 	for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
574 		if ((lba + lba_count) == blob->active.clusters[i]) {
575 			lba_count += lba_per_cluster;
576 			continue;
577 		}
578 		desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
579 		desc->extents[extent_idx].length = lba_count / lba_per_cluster;
580 		extent_idx++;
581 
582 		cur_sz += sizeof(desc->extents[extent_idx]);
583 
584 		if (buf_sz < cur_sz) {
585 			/* If we ran out of buffer space, return */
586 			desc->length = sizeof(desc->extents[0]) * extent_idx;
587 			*next_cluster = i;
588 			return;
589 		}
590 
591 		lba = blob->active.clusters[i];
592 		lba_count = lba_per_cluster;
593 	}
594 
595 	desc->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
596 	desc->extents[extent_idx].length = lba_count / lba_per_cluster;
597 	extent_idx++;
598 
599 	desc->length = sizeof(desc->extents[0]) * extent_idx;
600 	*next_cluster = blob->active.num_clusters;
601 
602 	return;
603 }
604 
605 static void
606 _spdk_blob_serialize_flags(const struct spdk_blob *blob,
607 			   uint8_t *buf, size_t *buf_sz)
608 {
609 	struct spdk_blob_md_descriptor_flags *desc;
610 
611 	/*
612 	 * Flags get serialized first, so we should always have room for the flags
613 	 *  descriptor.
614 	 */
615 	assert(*buf_sz >= sizeof(*desc));
616 
617 	desc = (struct spdk_blob_md_descriptor_flags *)buf;
618 	desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS;
619 	desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor);
620 	desc->invalid_flags = blob->invalid_flags;
621 	desc->data_ro_flags = blob->data_ro_flags;
622 	desc->md_ro_flags = blob->md_ro_flags;
623 
624 	*buf_sz -= sizeof(*desc);
625 }
626 
627 static int
628 _spdk_blob_serialize_xattrs(const struct spdk_blob *blob,
629 			    const struct spdk_xattr_tailq *xattrs, bool internal,
630 			    struct spdk_blob_md_page **pages,
631 			    struct spdk_blob_md_page *cur_page,
632 			    uint32_t *page_count, uint8_t **buf,
633 			    size_t *remaining_sz)
634 {
635 	const struct spdk_xattr	*xattr;
636 	int	rc;
637 
638 	TAILQ_FOREACH(xattr, xattrs, link) {
639 		size_t required_sz = 0;
640 
641 		rc = _spdk_blob_serialize_xattr(xattr,
642 						*buf, *remaining_sz,
643 						&required_sz, internal);
644 		if (rc < 0) {
645 			/* Need to add a new page to the chain */
646 			rc = _spdk_blob_serialize_add_page(blob, pages, page_count,
647 							   &cur_page);
648 			if (rc < 0) {
649 				spdk_dma_free(*pages);
650 				*pages = NULL;
651 				*page_count = 0;
652 				return rc;
653 			}
654 
655 			*buf = (uint8_t *)cur_page->descriptors;
656 			*remaining_sz = sizeof(cur_page->descriptors);
657 
658 			/* Try again */
659 			required_sz = 0;
660 			rc = _spdk_blob_serialize_xattr(xattr,
661 							*buf, *remaining_sz,
662 							&required_sz, internal);
663 
664 			if (rc < 0) {
665 				spdk_dma_free(*pages);
666 				*pages = NULL;
667 				*page_count = 0;
668 				return -1;
669 			}
670 		}
671 
672 		*remaining_sz -= required_sz;
673 		*buf += required_sz;
674 	}
675 
676 	return 0;
677 }
678 
679 static int
680 _spdk_blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
681 		     uint32_t *page_count)
682 {
683 	struct spdk_blob_md_page		*cur_page;
684 	int 					rc;
685 	uint8_t					*buf;
686 	size_t					remaining_sz;
687 	uint64_t				last_cluster;
688 
689 	assert(pages != NULL);
690 	assert(page_count != NULL);
691 	assert(blob != NULL);
692 	assert(blob->state == SPDK_BLOB_STATE_DIRTY);
693 
694 	*pages = NULL;
695 	*page_count = 0;
696 
697 	/* A blob always has at least 1 page, even if it has no descriptors */
698 	rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page);
699 	if (rc < 0) {
700 		return rc;
701 	}
702 
703 	buf = (uint8_t *)cur_page->descriptors;
704 	remaining_sz = sizeof(cur_page->descriptors);
705 
706 	/* Serialize flags */
707 	_spdk_blob_serialize_flags(blob, buf, &remaining_sz);
708 	buf += sizeof(struct spdk_blob_md_descriptor_flags);
709 
710 	/* Serialize xattrs */
711 	rc = _spdk_blob_serialize_xattrs(blob, &blob->xattrs, false,
712 					 pages, cur_page, page_count, &buf, &remaining_sz);
713 	if (rc < 0) {
714 		return rc;
715 	}
716 
717 	/* Serialize internal xattrs */
718 	rc = _spdk_blob_serialize_xattrs(blob, &blob->xattrs_internal, true,
719 					 pages, cur_page, page_count, &buf, &remaining_sz);
720 	if (rc < 0) {
721 		return rc;
722 	}
723 
724 	/* Serialize extents */
725 	last_cluster = 0;
726 	while (last_cluster < blob->active.num_clusters) {
727 		_spdk_blob_serialize_extent(blob, last_cluster, &last_cluster,
728 					    buf, remaining_sz);
729 
730 		if (last_cluster == blob->active.num_clusters) {
731 			break;
732 		}
733 
734 		rc = _spdk_blob_serialize_add_page(blob, pages, page_count,
735 						   &cur_page);
736 		if (rc < 0) {
737 			return rc;
738 		}
739 
740 		buf = (uint8_t *)cur_page->descriptors;
741 		remaining_sz = sizeof(cur_page->descriptors);
742 	}
743 
744 	return 0;
745 }
746 
747 struct spdk_blob_load_ctx {
748 	struct spdk_blob 		*blob;
749 
750 	struct spdk_blob_md_page	*pages;
751 	uint32_t			num_pages;
752 
753 	spdk_bs_sequence_cpl		cb_fn;
754 	void				*cb_arg;
755 };
756 
757 static uint32_t
758 _spdk_blob_md_page_calc_crc(void *page)
759 {
760 	uint32_t		crc;
761 
762 	crc = BLOB_CRC32C_INITIAL;
763 	crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
764 	crc ^= BLOB_CRC32C_INITIAL;
765 
766 	return crc;
767 
768 }
769 
770 static void
771 _spdk_blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
772 {
773 	struct spdk_blob_load_ctx 	*ctx = cb_arg;
774 	struct spdk_blob 		*blob = ctx->blob;
775 	struct spdk_blob_md_page	*page;
776 	int				rc;
777 	uint32_t			crc;
778 
779 	page = &ctx->pages[ctx->num_pages - 1];
780 	crc = _spdk_blob_md_page_calc_crc(page);
781 	if (crc != page->crc) {
782 		SPDK_ERRLOG("Metadata page %d crc mismatch\n", ctx->num_pages);
783 		_spdk_blob_free(blob);
784 		ctx->cb_fn(seq, NULL, -EINVAL);
785 		spdk_dma_free(ctx->pages);
786 		free(ctx);
787 		return;
788 	}
789 
790 	if (page->next != SPDK_INVALID_MD_PAGE) {
791 		uint32_t next_page = page->next;
792 		uint64_t next_lba = _spdk_bs_page_to_lba(blob->bs, blob->bs->md_start + next_page);
793 
794 
795 		assert(next_lba < (blob->bs->md_start + blob->bs->md_len));
796 
797 		/* Read the next page */
798 		ctx->num_pages++;
799 		ctx->pages = spdk_dma_realloc(ctx->pages, (sizeof(*page) * ctx->num_pages),
800 					      sizeof(*page), NULL);
801 		if (ctx->pages == NULL) {
802 			ctx->cb_fn(seq, ctx->cb_arg, -ENOMEM);
803 			free(ctx);
804 			return;
805 		}
806 
807 		spdk_bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1],
808 					  next_lba,
809 					  _spdk_bs_byte_to_lba(blob->bs, sizeof(*page)),
810 					  _spdk_blob_load_cpl, ctx);
811 		return;
812 	}
813 
814 	/* Parse the pages */
815 	rc = _spdk_blob_parse(ctx->pages, ctx->num_pages, blob);
816 	if (rc) {
817 		_spdk_blob_free(blob);
818 		ctx->cb_fn(seq, NULL, rc);
819 		spdk_dma_free(ctx->pages);
820 		free(ctx);
821 		return;
822 	}
823 
824 	if (spdk_blob_is_thin_provisioned(blob) == true) {
825 		blob->back_bs_dev = spdk_bs_create_zeroes_dev();
826 	}
827 
828 	_spdk_blob_mark_clean(blob);
829 
830 	ctx->cb_fn(seq, ctx->cb_arg, rc);
831 
832 	/* Free the memory */
833 	spdk_dma_free(ctx->pages);
834 	free(ctx);
835 }
836 
837 /* Load a blob from disk given a blobid */
838 static void
839 _spdk_blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
840 		spdk_bs_sequence_cpl cb_fn, void *cb_arg)
841 {
842 	struct spdk_blob_load_ctx *ctx;
843 	struct spdk_blob_store *bs;
844 	uint32_t page_num;
845 	uint64_t lba;
846 
847 	_spdk_blob_verify_md_op(blob);
848 
849 	bs = blob->bs;
850 
851 	ctx = calloc(1, sizeof(*ctx));
852 	if (!ctx) {
853 		cb_fn(seq, cb_arg, -ENOMEM);
854 		return;
855 	}
856 
857 	ctx->blob = blob;
858 	ctx->pages = spdk_dma_realloc(ctx->pages, SPDK_BS_PAGE_SIZE,
859 				      SPDK_BS_PAGE_SIZE, NULL);
860 	if (!ctx->pages) {
861 		free(ctx);
862 		cb_fn(seq, cb_arg, -ENOMEM);
863 		return;
864 	}
865 	ctx->num_pages = 1;
866 	ctx->cb_fn = cb_fn;
867 	ctx->cb_arg = cb_arg;
868 
869 	page_num = _spdk_bs_blobid_to_page(blob->id);
870 	lba = _spdk_bs_page_to_lba(blob->bs, bs->md_start + page_num);
871 
872 	blob->state = SPDK_BLOB_STATE_LOADING;
873 
874 	spdk_bs_sequence_read_dev(seq, &ctx->pages[0], lba,
875 				  _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
876 				  _spdk_blob_load_cpl, ctx);
877 }
878 
879 struct spdk_blob_persist_ctx {
880 	struct spdk_blob 		*blob;
881 
882 	struct spdk_blob_md_page	*pages;
883 
884 	uint64_t			idx;
885 
886 	spdk_bs_sequence_t		*seq;
887 	spdk_bs_sequence_cpl		cb_fn;
888 	void				*cb_arg;
889 };
890 
891 static void
892 _spdk_blob_persist_complete(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
893 {
894 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
895 	struct spdk_blob 		*blob = ctx->blob;
896 
897 	if (bserrno == 0) {
898 		_spdk_blob_mark_clean(blob);
899 	}
900 
901 	/* Call user callback */
902 	ctx->cb_fn(seq, ctx->cb_arg, bserrno);
903 
904 	/* Free the memory */
905 	spdk_dma_free(ctx->pages);
906 	free(ctx);
907 }
908 
909 static void
910 _spdk_blob_persist_unmap_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
911 {
912 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
913 	struct spdk_blob 		*blob = ctx->blob;
914 	struct spdk_blob_store		*bs = blob->bs;
915 	void				*tmp;
916 	size_t				i;
917 
918 	/* Release all clusters that were truncated */
919 	for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
920 		uint32_t cluster_num = _spdk_bs_lba_to_cluster(bs, blob->active.clusters[i]);
921 
922 		/* Nothing to release if it was not allocated */
923 		if (blob->active.clusters[i] != 0) {
924 			_spdk_bs_release_cluster(bs, cluster_num);
925 		}
926 	}
927 
928 	if (blob->active.num_clusters == 0) {
929 		free(blob->active.clusters);
930 		blob->active.clusters = NULL;
931 		blob->active.cluster_array_size = 0;
932 	} else {
933 		tmp = realloc(blob->active.clusters, sizeof(uint64_t) * blob->active.num_clusters);
934 		assert(tmp != NULL);
935 		blob->active.clusters = tmp;
936 		blob->active.cluster_array_size = blob->active.num_clusters;
937 	}
938 
939 	_spdk_blob_persist_complete(seq, ctx, bserrno);
940 }
941 
942 static void
943 _spdk_blob_persist_unmap_clusters(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
944 {
945 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
946 	struct spdk_blob 		*blob = ctx->blob;
947 	struct spdk_blob_store		*bs = blob->bs;
948 	spdk_bs_batch_t			*batch;
949 	size_t				i;
950 	uint64_t			lba;
951 	uint32_t			lba_count;
952 
953 	/* Clusters don't move around in blobs. The list shrinks or grows
954 	 * at the end, but no changes ever occur in the middle of the list.
955 	 */
956 
957 	batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_unmap_clusters_cpl, ctx);
958 
959 	/* Unmap all clusters that were truncated */
960 	lba = 0;
961 	lba_count = 0;
962 	for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
963 		uint64_t next_lba = blob->active.clusters[i];
964 		uint32_t next_lba_count = _spdk_bs_cluster_to_lba(bs, 1);
965 
966 		if (next_lba > 0 && (lba + lba_count) == next_lba) {
967 			/* This cluster is contiguous with the previous one. */
968 			lba_count += next_lba_count;
969 			continue;
970 		}
971 
972 		/* This cluster is not contiguous with the previous one. */
973 
974 		/* If a run of LBAs previously existing, send them
975 		 * as an unmap.
976 		 */
977 		if (lba_count > 0) {
978 			spdk_bs_batch_unmap_dev(batch, lba, lba_count);
979 		}
980 
981 		/* Start building the next batch */
982 		lba = next_lba;
983 		if (next_lba > 0) {
984 			lba_count = next_lba_count;
985 		} else {
986 			lba_count = 0;
987 		}
988 	}
989 
990 	/* If we ended with a contiguous set of LBAs, send the unmap now */
991 	if (lba_count > 0) {
992 		spdk_bs_batch_unmap_dev(batch, lba, lba_count);
993 	}
994 
995 	spdk_bs_batch_close(batch);
996 }
997 
998 static void
999 _spdk_blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1000 {
1001 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1002 	struct spdk_blob 		*blob = ctx->blob;
1003 	struct spdk_blob_store		*bs = blob->bs;
1004 	size_t				i;
1005 
1006 	/* This loop starts at 1 because the first page is special and handled
1007 	 * below. The pages (except the first) are never written in place,
1008 	 * so any pages in the clean list must be zeroed.
1009 	 */
1010 	for (i = 1; i < blob->clean.num_pages; i++) {
1011 		spdk_bit_array_clear(bs->used_md_pages, blob->clean.pages[i]);
1012 	}
1013 
1014 	if (blob->active.num_pages == 0) {
1015 		uint32_t page_num;
1016 
1017 		page_num = _spdk_bs_blobid_to_page(blob->id);
1018 		spdk_bit_array_clear(bs->used_md_pages, page_num);
1019 	}
1020 
1021 	/* Move on to unmapping clusters */
1022 	_spdk_blob_persist_unmap_clusters(seq, ctx, 0);
1023 }
1024 
1025 static void
1026 _spdk_blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1027 {
1028 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
1029 	struct spdk_blob 		*blob = ctx->blob;
1030 	struct spdk_blob_store		*bs = blob->bs;
1031 	uint64_t			lba;
1032 	uint32_t			lba_count;
1033 	spdk_bs_batch_t			*batch;
1034 	size_t				i;
1035 
1036 	batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_zero_pages_cpl, ctx);
1037 
1038 	lba_count = _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
1039 
1040 	/* This loop starts at 1 because the first page is special and handled
1041 	 * below. The pages (except the first) are never written in place,
1042 	 * so any pages in the clean list must be zeroed.
1043 	 */
1044 	for (i = 1; i < blob->clean.num_pages; i++) {
1045 		lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->clean.pages[i]);
1046 
1047 		spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count);
1048 	}
1049 
1050 	/* The first page will only be zeroed if this is a delete. */
1051 	if (blob->active.num_pages == 0) {
1052 		uint32_t page_num;
1053 
1054 		/* The first page in the metadata goes where the blobid indicates */
1055 		page_num = _spdk_bs_blobid_to_page(blob->id);
1056 		lba = _spdk_bs_page_to_lba(bs, bs->md_start + page_num);
1057 
1058 		spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count);
1059 	}
1060 
1061 	spdk_bs_batch_close(batch);
1062 }
1063 
1064 static void
1065 _spdk_blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1066 {
1067 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1068 	struct spdk_blob		*blob = ctx->blob;
1069 	struct spdk_blob_store		*bs = blob->bs;
1070 	uint64_t			lba;
1071 	uint32_t			lba_count;
1072 	struct spdk_blob_md_page	*page;
1073 
1074 	if (blob->active.num_pages == 0) {
1075 		/* Move on to the next step */
1076 		_spdk_blob_persist_zero_pages(seq, ctx, 0);
1077 		return;
1078 	}
1079 
1080 	lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page));
1081 
1082 	page = &ctx->pages[0];
1083 	/* The first page in the metadata goes where the blobid indicates */
1084 	lba = _spdk_bs_page_to_lba(bs, bs->md_start + _spdk_bs_blobid_to_page(blob->id));
1085 
1086 	spdk_bs_sequence_write_dev(seq, page, lba, lba_count,
1087 				   _spdk_blob_persist_zero_pages, ctx);
1088 }
1089 
1090 static void
1091 _spdk_blob_persist_write_page_chain(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1092 {
1093 	struct spdk_blob_persist_ctx 	*ctx = cb_arg;
1094 	struct spdk_blob 		*blob = ctx->blob;
1095 	struct spdk_blob_store		*bs = blob->bs;
1096 	uint64_t 			lba;
1097 	uint32_t			lba_count;
1098 	struct spdk_blob_md_page	*page;
1099 	spdk_bs_batch_t			*batch;
1100 	size_t				i;
1101 
1102 	/* Clusters don't move around in blobs. The list shrinks or grows
1103 	 * at the end, but no changes ever occur in the middle of the list.
1104 	 */
1105 
1106 	lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page));
1107 
1108 	batch = spdk_bs_sequence_to_batch(seq, _spdk_blob_persist_write_page_root, ctx);
1109 
1110 	/* This starts at 1. The root page is not written until
1111 	 * all of the others are finished
1112 	 */
1113 	for (i = 1; i < blob->active.num_pages; i++) {
1114 		page = &ctx->pages[i];
1115 		assert(page->sequence_num == i);
1116 
1117 		lba = _spdk_bs_page_to_lba(bs, bs->md_start + blob->active.pages[i]);
1118 
1119 		spdk_bs_batch_write_dev(batch, page, lba, lba_count);
1120 	}
1121 
1122 	spdk_bs_batch_close(batch);
1123 }
1124 
1125 static int
1126 _spdk_resize_blob(struct spdk_blob *blob, uint64_t sz)
1127 {
1128 	uint64_t	i;
1129 	uint64_t	*tmp;
1130 	uint64_t	lfc; /* lowest free cluster */
1131 	uint64_t	num_clusters;
1132 	struct spdk_blob_store *bs;
1133 
1134 	bs = blob->bs;
1135 
1136 	_spdk_blob_verify_md_op(blob);
1137 
1138 	if (blob->active.num_clusters == sz) {
1139 		return 0;
1140 	}
1141 
1142 	if (blob->active.num_clusters < blob->active.cluster_array_size) {
1143 		/* If this blob was resized to be larger, then smaller, then
1144 		 * larger without syncing, then the cluster array already
1145 		 * contains spare assigned clusters we can use.
1146 		 */
1147 		num_clusters = spdk_min(blob->active.cluster_array_size,
1148 					sz);
1149 	} else {
1150 		num_clusters = blob->active.num_clusters;
1151 	}
1152 
1153 	/* Do two passes - one to verify that we can obtain enough clusters
1154 	 * and another to actually claim them.
1155 	 */
1156 
1157 	if (spdk_blob_is_thin_provisioned(blob) == false) {
1158 		lfc = 0;
1159 		for (i = num_clusters; i < sz; i++) {
1160 			lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc);
1161 			if (lfc >= bs->total_clusters) {
1162 				/* No more free clusters. Cannot satisfy the request */
1163 				return -ENOSPC;
1164 			}
1165 			lfc++;
1166 		}
1167 	}
1168 
1169 	if (sz > num_clusters) {
1170 		/* Expand the cluster array if necessary.
1171 		 * We only shrink the array when persisting.
1172 		 */
1173 		tmp = realloc(blob->active.clusters, sizeof(uint64_t) * sz);
1174 		if (sz > 0 && tmp == NULL) {
1175 			return -ENOMEM;
1176 		}
1177 		memset(tmp + blob->active.cluster_array_size, 0,
1178 		       sizeof(uint64_t) * (sz - blob->active.cluster_array_size));
1179 		blob->active.clusters = tmp;
1180 		blob->active.cluster_array_size = sz;
1181 	}
1182 
1183 	blob->state = SPDK_BLOB_STATE_DIRTY;
1184 
1185 	if (spdk_blob_is_thin_provisioned(blob) == false) {
1186 		lfc = 0;
1187 		for (i = num_clusters; i < sz; i++) {
1188 			_spdk_bs_allocate_cluster(blob, i, &lfc, true);
1189 			lfc++;
1190 		}
1191 	}
1192 
1193 	blob->active.num_clusters = sz;
1194 
1195 	return 0;
1196 }
1197 
1198 static void
1199 _spdk_blob_persist_start(struct spdk_blob_persist_ctx *ctx)
1200 {
1201 	spdk_bs_sequence_t *seq = ctx->seq;
1202 	struct spdk_blob *blob = ctx->blob;
1203 	struct spdk_blob_store *bs = blob->bs;
1204 	uint64_t i;
1205 	uint32_t page_num;
1206 	int rc;
1207 
1208 	if (blob->active.num_pages == 0) {
1209 		/* This is the signal that the blob should be deleted.
1210 		 * Immediately jump to the clean up routine. */
1211 		assert(blob->clean.num_pages > 0);
1212 		ctx->idx = blob->clean.num_pages - 1;
1213 		blob->state = SPDK_BLOB_STATE_CLEAN;
1214 		_spdk_blob_persist_zero_pages(seq, ctx, 0);
1215 		return;
1216 
1217 	}
1218 
1219 	/* Generate the new metadata */
1220 	rc = _spdk_blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
1221 	if (rc < 0) {
1222 		_spdk_blob_persist_complete(seq, ctx, rc);
1223 		return;
1224 	}
1225 
1226 	assert(blob->active.num_pages >= 1);
1227 
1228 	/* Resize the cache of page indices */
1229 	blob->active.pages = realloc(blob->active.pages,
1230 				     blob->active.num_pages * sizeof(*blob->active.pages));
1231 	if (!blob->active.pages) {
1232 		_spdk_blob_persist_complete(seq, ctx, -ENOMEM);
1233 		return;
1234 	}
1235 
1236 	/* Assign this metadata to pages. This requires two passes -
1237 	 * one to verify that there are enough pages and a second
1238 	 * to actually claim them. */
1239 	page_num = 0;
1240 	/* Note that this loop starts at one. The first page location is fixed by the blobid. */
1241 	for (i = 1; i < blob->active.num_pages; i++) {
1242 		page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
1243 		if (page_num >= spdk_bit_array_capacity(bs->used_md_pages)) {
1244 			_spdk_blob_persist_complete(seq, ctx, -ENOMEM);
1245 			return;
1246 		}
1247 		page_num++;
1248 	}
1249 
1250 	page_num = 0;
1251 	blob->active.pages[0] = _spdk_bs_blobid_to_page(blob->id);
1252 	for (i = 1; i < blob->active.num_pages; i++) {
1253 		page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
1254 		ctx->pages[i - 1].next = page_num;
1255 		/* Now that previous metadata page is complete, calculate the crc for it. */
1256 		ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]);
1257 		blob->active.pages[i] = page_num;
1258 		spdk_bit_array_set(bs->used_md_pages, page_num);
1259 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming page %u for blob %lu\n", page_num, blob->id);
1260 		page_num++;
1261 	}
1262 	ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]);
1263 	/* Start writing the metadata from last page to first */
1264 	ctx->idx = blob->active.num_pages - 1;
1265 	blob->state = SPDK_BLOB_STATE_CLEAN;
1266 	_spdk_blob_persist_write_page_chain(seq, ctx, 0);
1267 }
1268 
1269 /* Write a blob to disk */
1270 static void
1271 _spdk_blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
1272 		   spdk_bs_sequence_cpl cb_fn, void *cb_arg)
1273 {
1274 	struct spdk_blob_persist_ctx *ctx;
1275 
1276 	_spdk_blob_verify_md_op(blob);
1277 
1278 	if (blob->state == SPDK_BLOB_STATE_CLEAN) {
1279 		cb_fn(seq, cb_arg, 0);
1280 		return;
1281 	}
1282 
1283 	ctx = calloc(1, sizeof(*ctx));
1284 	if (!ctx) {
1285 		cb_fn(seq, cb_arg, -ENOMEM);
1286 		return;
1287 	}
1288 	ctx->blob = blob;
1289 	ctx->seq = seq;
1290 	ctx->cb_fn = cb_fn;
1291 	ctx->cb_arg = cb_arg;
1292 
1293 	_spdk_blob_persist_start(ctx);
1294 }
1295 
1296 struct spdk_blob_copy_cluster_ctx {
1297 	struct spdk_blob *blob;
1298 	uint8_t *buf;
1299 	uint64_t page;
1300 	uint64_t new_cluster;
1301 	spdk_bs_sequence_t *seq;
1302 };
1303 
1304 static void
1305 _spdk_blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno)
1306 {
1307 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
1308 	struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq;
1309 	TAILQ_HEAD(, spdk_bs_request_set) requests;
1310 	spdk_bs_user_op_t *op;
1311 
1312 	TAILQ_INIT(&requests);
1313 	TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link);
1314 
1315 	while (!TAILQ_EMPTY(&requests)) {
1316 		op = TAILQ_FIRST(&requests);
1317 		TAILQ_REMOVE(&requests, op, link);
1318 		if (bserrno == 0) {
1319 			spdk_bs_user_op_execute(op);
1320 		} else {
1321 			spdk_bs_user_op_abort(op);
1322 		}
1323 	}
1324 
1325 	spdk_dma_free(ctx->buf);
1326 	free(ctx);
1327 }
1328 
1329 static void
1330 _spdk_blob_insert_cluster_cpl(void *cb_arg, int bserrno)
1331 {
1332 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
1333 
1334 	if (bserrno) {
1335 		uint32_t cluster_number;
1336 
1337 		if (bserrno == -EEXIST) {
1338 			/* The metadata insert failed because another thread
1339 			 * allocated the cluster first. Free our cluster
1340 			 * but continue without error. */
1341 			bserrno = 0;
1342 		}
1343 
1344 		cluster_number = _spdk_bs_page_to_cluster(ctx->blob->bs, ctx->page);
1345 		_spdk_bs_release_cluster(ctx->blob->bs, cluster_number);
1346 	}
1347 
1348 	spdk_bs_sequence_finish(ctx->seq, bserrno);
1349 }
1350 
1351 static void
1352 _spdk_blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1353 {
1354 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
1355 	uint32_t cluster_number;
1356 
1357 	if (bserrno) {
1358 		/* The write failed, so jump to the final completion handler */
1359 		spdk_bs_sequence_finish(seq, bserrno);
1360 		return;
1361 	}
1362 
1363 	cluster_number = _spdk_bs_page_to_cluster(ctx->blob->bs, ctx->page);
1364 
1365 	_spdk_blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
1366 					       _spdk_blob_insert_cluster_cpl, ctx);
1367 }
1368 
1369 static void
1370 _spdk_blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1371 {
1372 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
1373 
1374 	if (bserrno != 0) {
1375 		/* The read failed, so jump to the final completion handler */
1376 		spdk_bs_sequence_finish(seq, bserrno);
1377 		return;
1378 	}
1379 
1380 	/* Write whole cluster */
1381 	spdk_bs_sequence_write_dev(seq, ctx->buf,
1382 				   _spdk_bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
1383 				   _spdk_bs_cluster_to_lba(ctx->blob->bs, 1),
1384 				   _spdk_blob_write_copy_cpl, ctx);
1385 }
1386 
1387 static void
1388 _spdk_bs_allocate_and_copy_cluster(struct spdk_blob *blob,
1389 				   struct spdk_io_channel *_ch,
1390 				   uint64_t offset, spdk_bs_user_op_t *op)
1391 {
1392 	struct spdk_bs_cpl cpl;
1393 	struct spdk_bs_channel *ch;
1394 	struct spdk_blob_copy_cluster_ctx *ctx;
1395 	uint32_t cluster_start_page;
1396 	uint32_t cluster_number;
1397 	int rc;
1398 
1399 	ch = spdk_io_channel_get_ctx(_ch);
1400 
1401 	if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) {
1402 		/* There are already operations pending. Queue this user op
1403 		 * and return because it will be re-executed when the outstanding
1404 		 * cluster allocation completes. */
1405 		TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
1406 		return;
1407 	}
1408 
1409 	/* Round the page offset down to the first page in the cluster */
1410 	cluster_start_page = _spdk_bs_page_to_cluster_start(blob, offset);
1411 
1412 	/* Calculate which index in the metadata cluster array the corresponding
1413 	 * cluster is supposed to be at. */
1414 	cluster_number = _spdk_bs_page_to_cluster(blob->bs, cluster_start_page);
1415 
1416 	ctx = calloc(1, sizeof(*ctx));
1417 	if (!ctx) {
1418 		spdk_bs_user_op_abort(op);
1419 		return;
1420 	}
1421 
1422 	assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0);
1423 
1424 	ctx->blob = blob;
1425 	ctx->page = cluster_start_page;
1426 
1427 	ctx->buf = spdk_dma_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen, NULL);
1428 	if (!ctx->buf) {
1429 		SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n",
1430 			    blob->bs->cluster_sz);
1431 		free(ctx);
1432 		spdk_bs_user_op_abort(op);
1433 		return;
1434 	}
1435 
1436 	rc = _spdk_bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, false);
1437 	if (rc != 0) {
1438 		spdk_dma_free(ctx->buf);
1439 		free(ctx);
1440 		spdk_bs_user_op_abort(op);
1441 		return;
1442 	}
1443 
1444 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
1445 	cpl.u.blob_basic.cb_fn = _spdk_blob_allocate_and_copy_cluster_cpl;
1446 	cpl.u.blob_basic.cb_arg = ctx;
1447 
1448 	ctx->seq = spdk_bs_sequence_start(_ch, &cpl);
1449 	if (!ctx->seq) {
1450 		_spdk_bs_release_cluster(blob->bs, ctx->new_cluster);
1451 		spdk_dma_free(ctx->buf);
1452 		free(ctx);
1453 		spdk_bs_user_op_abort(op);
1454 		return;
1455 	}
1456 
1457 	/* Queue the user op to block other incoming operations */
1458 	TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
1459 
1460 	/* Read cluster from backing device */
1461 	spdk_bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf,
1462 				     _spdk_bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
1463 				     _spdk_bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz),
1464 				     _spdk_blob_write_copy, ctx);
1465 }
1466 
1467 static void
1468 _spdk_blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t page, uint64_t length,
1469 				       uint64_t *lba,	uint32_t *lba_count)
1470 {
1471 	*lba_count = _spdk_bs_page_to_lba(blob->bs, length);
1472 
1473 	if (!_spdk_bs_page_is_allocated(blob, page)) {
1474 		assert(blob->back_bs_dev != NULL);
1475 		*lba = _spdk_bs_dev_page_to_lba(blob->back_bs_dev, page);
1476 		*lba_count = _spdk_bs_blob_lba_to_back_dev_lba(blob, *lba_count);
1477 	} else {
1478 		*lba = _spdk_bs_blob_page_to_lba(blob, page);
1479 	}
1480 }
1481 
1482 static void
1483 _spdk_blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob,
1484 				   void *payload, uint64_t offset, uint64_t length,
1485 				   spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
1486 {
1487 	spdk_bs_batch_t		*batch;
1488 	struct spdk_bs_cpl	cpl;
1489 	uint64_t		op_length;
1490 	uint8_t			*buf;
1491 
1492 	assert(blob != NULL);
1493 
1494 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
1495 	cpl.u.blob_basic.cb_fn = cb_fn;
1496 	cpl.u.blob_basic.cb_arg = cb_arg;
1497 
1498 	batch = spdk_bs_batch_open(ch, &cpl);
1499 	if (!batch) {
1500 		cb_fn(cb_arg, -ENOMEM);
1501 		return;
1502 	}
1503 
1504 	buf = payload;
1505 	while (length > 0) {
1506 		op_length = spdk_min(length, _spdk_bs_num_pages_to_cluster_boundary(blob, offset));
1507 
1508 		switch (op_type) {
1509 		case SPDK_BLOB_READ:
1510 			spdk_bs_batch_read_blob(batch, blob, buf, offset, op_length);
1511 			break;
1512 		case SPDK_BLOB_WRITE:
1513 			spdk_bs_batch_write_blob(batch, blob, buf, offset, op_length);
1514 			break;
1515 		case SPDK_BLOB_UNMAP:
1516 			spdk_bs_batch_unmap_blob(batch, blob, offset, op_length);
1517 			break;
1518 		case SPDK_BLOB_WRITE_ZEROES:
1519 			spdk_bs_batch_write_zeroes_blob(batch, blob, offset, op_length);
1520 			break;
1521 		case SPDK_BLOB_READV:
1522 		case SPDK_BLOB_WRITEV:
1523 			SPDK_ERRLOG("readv/write not valid for %s\n", __func__);
1524 			break;
1525 		}
1526 
1527 		length -= op_length;
1528 		offset += op_length;
1529 		if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) {
1530 			buf += op_length * SPDK_BS_PAGE_SIZE;
1531 		}
1532 	}
1533 
1534 	spdk_bs_batch_close(batch);
1535 }
1536 
1537 static void
1538 _spdk_blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob,
1539 				    void *payload, uint64_t offset, uint64_t length,
1540 				    spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
1541 {
1542 	struct spdk_bs_cpl cpl;
1543 	uint64_t lba;
1544 	uint32_t lba_count;
1545 
1546 	assert(blob != NULL);
1547 
1548 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
1549 	cpl.u.blob_basic.cb_fn = cb_fn;
1550 	cpl.u.blob_basic.cb_arg = cb_arg;
1551 
1552 	_spdk_blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
1553 
1554 	switch (op_type) {
1555 	case SPDK_BLOB_READ: {
1556 		spdk_bs_batch_t *batch;
1557 
1558 		batch = spdk_bs_batch_open(_ch, &cpl);
1559 		if (!batch) {
1560 			cb_fn(cb_arg, -ENOMEM);
1561 			return;
1562 		}
1563 
1564 		if (_spdk_bs_page_is_allocated(blob, offset)) {
1565 			/* Read from the blob */
1566 			spdk_bs_batch_read_dev(batch, payload, lba, lba_count);
1567 		} else {
1568 			/* Read from the backing block device */
1569 			spdk_bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count);
1570 		}
1571 
1572 		spdk_bs_batch_close(batch);
1573 		break;
1574 	}
1575 	case SPDK_BLOB_WRITE:
1576 	case SPDK_BLOB_WRITE_ZEROES: {
1577 		if (_spdk_bs_page_is_allocated(blob, offset)) {
1578 			/* Write to the blob */
1579 			spdk_bs_batch_t *batch;
1580 
1581 			batch = spdk_bs_batch_open(_ch, &cpl);
1582 			if (!batch) {
1583 				cb_fn(cb_arg, -ENOMEM);
1584 				return;
1585 			}
1586 
1587 			if (op_type == SPDK_BLOB_WRITE) {
1588 				spdk_bs_batch_write_dev(batch, payload, lba, lba_count);
1589 			} else {
1590 				spdk_bs_batch_write_zeroes_dev(batch, lba, lba_count);
1591 			}
1592 
1593 			spdk_bs_batch_close(batch);
1594 		} else {
1595 			/* Queue this operation and allocate the cluster */
1596 			spdk_bs_user_op_t *op;
1597 
1598 			op = spdk_bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
1599 			if (!op) {
1600 				cb_fn(cb_arg, -ENOMEM);
1601 				return;
1602 			}
1603 
1604 			_spdk_bs_allocate_and_copy_cluster(blob, _ch, offset, op);
1605 		}
1606 		break;
1607 	}
1608 	case SPDK_BLOB_UNMAP: {
1609 		spdk_bs_batch_t *batch;
1610 
1611 		batch = spdk_bs_batch_open(_ch, &cpl);
1612 		if (!batch) {
1613 			cb_fn(cb_arg, -ENOMEM);
1614 			return;
1615 		}
1616 
1617 		if (_spdk_bs_page_is_allocated(blob, offset)) {
1618 			spdk_bs_batch_unmap_dev(batch, lba, lba_count);
1619 		}
1620 
1621 		spdk_bs_batch_close(batch);
1622 		break;
1623 	}
1624 	case SPDK_BLOB_READV:
1625 	case SPDK_BLOB_WRITEV:
1626 		SPDK_ERRLOG("readv/write not valid\n");
1627 		cb_fn(cb_arg, -EINVAL);
1628 		break;
1629 	}
1630 }
1631 
1632 static void
1633 _spdk_blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel,
1634 			     void *payload, uint64_t offset, uint64_t length,
1635 			     spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
1636 {
1637 	assert(blob != NULL);
1638 
1639 	if (blob->data_ro && op_type != SPDK_BLOB_READ) {
1640 		cb_fn(cb_arg, -EPERM);
1641 		return;
1642 	}
1643 
1644 	if (offset + length > blob->active.num_clusters * blob->bs->pages_per_cluster) {
1645 		cb_fn(cb_arg, -EINVAL);
1646 		return;
1647 	}
1648 
1649 	if (length <= _spdk_bs_num_pages_to_cluster_boundary(blob, offset)) {
1650 		_spdk_blob_request_submit_op_single(_channel, blob, payload, offset, length,
1651 						    cb_fn, cb_arg, op_type);
1652 	} else {
1653 		_spdk_blob_request_submit_op_split(_channel, blob, payload, offset, length,
1654 						   cb_fn, cb_arg, op_type);
1655 	}
1656 }
1657 
1658 struct rw_iov_ctx {
1659 	struct spdk_blob *blob;
1660 	struct spdk_io_channel *channel;
1661 	spdk_blob_op_complete cb_fn;
1662 	void *cb_arg;
1663 	bool read;
1664 	int iovcnt;
1665 	struct iovec *orig_iov;
1666 	uint64_t page_offset;
1667 	uint64_t pages_remaining;
1668 	uint64_t pages_done;
1669 	struct iovec iov[0];
1670 };
1671 
1672 static void
1673 _spdk_rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1674 {
1675 	assert(cb_arg == NULL);
1676 	spdk_bs_sequence_finish(seq, bserrno);
1677 }
1678 
1679 static void
1680 _spdk_rw_iov_split_next(void *cb_arg, int bserrno)
1681 {
1682 	struct rw_iov_ctx *ctx = cb_arg;
1683 	struct spdk_blob *blob = ctx->blob;
1684 	struct iovec *iov, *orig_iov;
1685 	int iovcnt;
1686 	size_t orig_iovoff;
1687 	uint64_t page_count, pages_to_boundary, page_offset;
1688 	uint64_t byte_count;
1689 
1690 	if (bserrno != 0 || ctx->pages_remaining == 0) {
1691 		ctx->cb_fn(ctx->cb_arg, bserrno);
1692 		free(ctx);
1693 		return;
1694 	}
1695 
1696 	page_offset = ctx->page_offset;
1697 	pages_to_boundary = _spdk_bs_num_pages_to_cluster_boundary(blob, page_offset);
1698 	page_count = spdk_min(ctx->pages_remaining, pages_to_boundary);
1699 
1700 	/*
1701 	 * Get index and offset into the original iov array for our current position in the I/O sequence.
1702 	 *  byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
1703 	 *  point to the current position in the I/O sequence.
1704 	 */
1705 	byte_count = ctx->pages_done * sizeof(struct spdk_blob_md_page);
1706 	orig_iov = &ctx->orig_iov[0];
1707 	orig_iovoff = 0;
1708 	while (byte_count > 0) {
1709 		if (byte_count >= orig_iov->iov_len) {
1710 			byte_count -= orig_iov->iov_len;
1711 			orig_iov++;
1712 		} else {
1713 			orig_iovoff = byte_count;
1714 			byte_count = 0;
1715 		}
1716 	}
1717 
1718 	/*
1719 	 * Build an iov array for the next I/O in the sequence.  byte_count will keep track of how many
1720 	 *  bytes of this next I/O remain to be accounted for in the new iov array.
1721 	 */
1722 	byte_count = page_count * sizeof(struct spdk_blob_md_page);
1723 	iov = &ctx->iov[0];
1724 	iovcnt = 0;
1725 	while (byte_count > 0) {
1726 		iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
1727 		iov->iov_base = orig_iov->iov_base + orig_iovoff;
1728 		byte_count -= iov->iov_len;
1729 		orig_iovoff = 0;
1730 		orig_iov++;
1731 		iov++;
1732 		iovcnt++;
1733 	}
1734 
1735 	ctx->page_offset += page_count;
1736 	ctx->pages_done += page_count;
1737 	ctx->pages_remaining -= page_count;
1738 	iov = &ctx->iov[0];
1739 
1740 	if (ctx->read) {
1741 		spdk_blob_io_readv(ctx->blob, ctx->channel, iov, iovcnt, page_offset,
1742 				   page_count, _spdk_rw_iov_split_next, ctx);
1743 	} else {
1744 		spdk_blob_io_writev(ctx->blob, ctx->channel, iov, iovcnt, page_offset,
1745 				    page_count, _spdk_rw_iov_split_next, ctx);
1746 	}
1747 }
1748 
1749 static void
1750 _spdk_blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
1751 				 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
1752 				 spdk_blob_op_complete cb_fn, void *cb_arg, bool read)
1753 {
1754 	struct spdk_bs_cpl	cpl;
1755 
1756 	assert(blob != NULL);
1757 
1758 	if (!read && blob->data_ro) {
1759 		cb_fn(cb_arg, -EPERM);
1760 		return;
1761 	}
1762 
1763 	if (length == 0) {
1764 		cb_fn(cb_arg, 0);
1765 		return;
1766 	}
1767 
1768 	if (offset + length > blob->active.num_clusters * blob->bs->pages_per_cluster) {
1769 		cb_fn(cb_arg, -EINVAL);
1770 		return;
1771 	}
1772 
1773 	/*
1774 	 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
1775 	 *  to split a request that spans a cluster boundary.  For I/O that do not span a cluster boundary,
1776 	 *  there will be no noticeable difference compared to using a batch.  For I/O that do span a cluster
1777 	 *  boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
1778 	 *  to allocate a separate iov array and split the I/O such that none of the resulting
1779 	 *  smaller I/O cross a cluster boundary.  These smaller I/O will be issued in sequence (not in parallel)
1780 	 *  but since this case happens very infrequently, any performance impact will be negligible.
1781 	 *
1782 	 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
1783 	 *  for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
1784 	 *  in a batch.  That would also require creating an intermediate spdk_bs_cpl that would get called
1785 	 *  when the batch was completed, to allow for freeing the memory for the iov arrays.
1786 	 */
1787 	if (spdk_likely(length <= _spdk_bs_num_pages_to_cluster_boundary(blob, offset))) {
1788 		uint32_t lba_count;
1789 		uint64_t lba;
1790 
1791 		_spdk_blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
1792 
1793 		cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
1794 		cpl.u.blob_basic.cb_fn = cb_fn;
1795 		cpl.u.blob_basic.cb_arg = cb_arg;
1796 
1797 		if (read) {
1798 			spdk_bs_sequence_t *seq;
1799 
1800 			seq = spdk_bs_sequence_start(_channel, &cpl);
1801 			if (!seq) {
1802 				cb_fn(cb_arg, -ENOMEM);
1803 				return;
1804 			}
1805 
1806 			if (_spdk_bs_page_is_allocated(blob, offset)) {
1807 				spdk_bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL);
1808 			} else {
1809 				spdk_bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count,
1810 							      _spdk_rw_iov_done, NULL);
1811 			}
1812 		} else {
1813 			if (_spdk_bs_page_is_allocated(blob, offset)) {
1814 				spdk_bs_sequence_t *seq;
1815 
1816 				seq = spdk_bs_sequence_start(_channel, &cpl);
1817 				if (!seq) {
1818 					cb_fn(cb_arg, -ENOMEM);
1819 					return;
1820 				}
1821 
1822 				spdk_bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL);
1823 			} else {
1824 				/* Queue this operation and allocate the cluster */
1825 				spdk_bs_user_op_t *op;
1826 
1827 				op = spdk_bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset, length);
1828 				if (!op) {
1829 					cb_fn(cb_arg, -ENOMEM);
1830 					return;
1831 				}
1832 
1833 				_spdk_bs_allocate_and_copy_cluster(blob, _channel, offset, op);
1834 			}
1835 		}
1836 	} else {
1837 		struct rw_iov_ctx *ctx;
1838 
1839 		ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
1840 		if (ctx == NULL) {
1841 			cb_fn(cb_arg, -ENOMEM);
1842 			return;
1843 		}
1844 
1845 		ctx->blob = blob;
1846 		ctx->channel = _channel;
1847 		ctx->cb_fn = cb_fn;
1848 		ctx->cb_arg = cb_arg;
1849 		ctx->read = read;
1850 		ctx->orig_iov = iov;
1851 		ctx->iovcnt = iovcnt;
1852 		ctx->page_offset = offset;
1853 		ctx->pages_remaining = length;
1854 		ctx->pages_done = 0;
1855 
1856 		_spdk_rw_iov_split_next(ctx, 0);
1857 	}
1858 }
1859 
1860 static struct spdk_blob *
1861 _spdk_blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
1862 {
1863 	struct spdk_blob *blob;
1864 
1865 	TAILQ_FOREACH(blob, &bs->blobs, link) {
1866 		if (blob->id == blobid) {
1867 			return blob;
1868 		}
1869 	}
1870 
1871 	return NULL;
1872 }
1873 
1874 static int
1875 _spdk_bs_channel_create(void *io_device, void *ctx_buf)
1876 {
1877 	struct spdk_blob_store		*bs = io_device;
1878 	struct spdk_bs_channel		*channel = ctx_buf;
1879 	struct spdk_bs_dev		*dev;
1880 	uint32_t			max_ops = bs->max_channel_ops;
1881 	uint32_t			i;
1882 
1883 	dev = bs->dev;
1884 
1885 	channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
1886 	if (!channel->req_mem) {
1887 		return -1;
1888 	}
1889 
1890 	TAILQ_INIT(&channel->reqs);
1891 
1892 	for (i = 0; i < max_ops; i++) {
1893 		TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
1894 	}
1895 
1896 	channel->bs = bs;
1897 	channel->dev = dev;
1898 	channel->dev_channel = dev->create_channel(dev);
1899 
1900 	if (!channel->dev_channel) {
1901 		SPDK_ERRLOG("Failed to create device channel.\n");
1902 		free(channel->req_mem);
1903 		return -1;
1904 	}
1905 
1906 	TAILQ_INIT(&channel->need_cluster_alloc);
1907 
1908 	return 0;
1909 }
1910 
1911 static void
1912 _spdk_bs_channel_destroy(void *io_device, void *ctx_buf)
1913 {
1914 	struct spdk_bs_channel *channel = ctx_buf;
1915 	spdk_bs_user_op_t *op;
1916 
1917 	while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) {
1918 		op = TAILQ_FIRST(&channel->need_cluster_alloc);
1919 		TAILQ_REMOVE(&channel->need_cluster_alloc, op, link);
1920 		spdk_bs_user_op_abort(op);
1921 	}
1922 
1923 	free(channel->req_mem);
1924 	channel->dev->destroy_channel(channel->dev, channel->dev_channel);
1925 }
1926 
1927 static void
1928 _spdk_bs_dev_destroy(void *io_device)
1929 {
1930 	struct spdk_blob_store *bs = io_device;
1931 	struct spdk_blob	*blob, *blob_tmp;
1932 
1933 	bs->dev->destroy(bs->dev);
1934 
1935 	TAILQ_FOREACH_SAFE(blob, &bs->blobs, link, blob_tmp) {
1936 		TAILQ_REMOVE(&bs->blobs, blob, link);
1937 		_spdk_blob_free(blob);
1938 	}
1939 
1940 	pthread_mutex_destroy(&bs->used_clusters_mutex);
1941 
1942 	spdk_bit_array_free(&bs->used_blobids);
1943 	spdk_bit_array_free(&bs->used_md_pages);
1944 	spdk_bit_array_free(&bs->used_clusters);
1945 	/*
1946 	 * If this function is called for any reason except a successful unload,
1947 	 * the unload_cpl type will be NONE and this will be a nop.
1948 	 */
1949 	spdk_bs_call_cpl(&bs->unload_cpl, bs->unload_err);
1950 
1951 	free(bs);
1952 }
1953 
1954 static void
1955 _spdk_bs_free(struct spdk_blob_store *bs)
1956 {
1957 	spdk_bs_unregister_md_thread(bs);
1958 	spdk_io_device_unregister(bs, _spdk_bs_dev_destroy);
1959 }
1960 
1961 void
1962 spdk_bs_opts_init(struct spdk_bs_opts *opts)
1963 {
1964 	opts->cluster_sz = SPDK_BLOB_OPTS_CLUSTER_SZ;
1965 	opts->num_md_pages = SPDK_BLOB_OPTS_NUM_MD_PAGES;
1966 	opts->max_md_ops = SPDK_BLOB_OPTS_MAX_MD_OPS;
1967 	opts->max_channel_ops = SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS;
1968 	memset(&opts->bstype, 0, sizeof(opts->bstype));
1969 }
1970 
1971 static int
1972 _spdk_bs_opts_verify(struct spdk_bs_opts *opts)
1973 {
1974 	if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
1975 	    opts->max_channel_ops == 0) {
1976 		SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
1977 		return -1;
1978 	}
1979 
1980 	return 0;
1981 }
1982 
1983 static struct spdk_blob_store *
1984 _spdk_bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts)
1985 {
1986 	struct spdk_blob_store	*bs;
1987 	uint64_t dev_size;
1988 	int rc;
1989 
1990 	dev_size = dev->blocklen * dev->blockcnt;
1991 	if (dev_size < opts->cluster_sz) {
1992 		/* Device size cannot be smaller than cluster size of blobstore */
1993 		SPDK_ERRLOG("Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n",
1994 			    dev_size, opts->cluster_sz);
1995 		return NULL;
1996 	}
1997 	if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
1998 		/* Cluster size cannot be smaller than page size */
1999 		SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n",
2000 			    opts->cluster_sz, SPDK_BS_PAGE_SIZE);
2001 		return NULL;
2002 	}
2003 	bs = calloc(1, sizeof(struct spdk_blob_store));
2004 	if (!bs) {
2005 		return NULL;
2006 	}
2007 
2008 	TAILQ_INIT(&bs->blobs);
2009 	bs->dev = dev;
2010 	bs->md_thread = spdk_get_thread();
2011 	assert(bs->md_thread != NULL);
2012 
2013 	/*
2014 	 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an
2015 	 *  even multiple of the cluster size.
2016 	 */
2017 	bs->cluster_sz = opts->cluster_sz;
2018 	bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
2019 	bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
2020 	bs->num_free_clusters = bs->total_clusters;
2021 	bs->used_clusters = spdk_bit_array_create(bs->total_clusters);
2022 	if (bs->used_clusters == NULL) {
2023 		free(bs);
2024 		return NULL;
2025 	}
2026 
2027 	bs->max_channel_ops = opts->max_channel_ops;
2028 	bs->super_blob = SPDK_BLOBID_INVALID;
2029 	memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
2030 
2031 	/* The metadata is assumed to be at least 1 page */
2032 	bs->used_md_pages = spdk_bit_array_create(1);
2033 	bs->used_blobids = spdk_bit_array_create(0);
2034 
2035 	pthread_mutex_init(&bs->used_clusters_mutex, NULL);
2036 
2037 	spdk_io_device_register(bs, _spdk_bs_channel_create, _spdk_bs_channel_destroy,
2038 				sizeof(struct spdk_bs_channel));
2039 	rc = spdk_bs_register_md_thread(bs);
2040 	if (rc == -1) {
2041 		spdk_io_device_unregister(bs, NULL);
2042 		pthread_mutex_destroy(&bs->used_clusters_mutex);
2043 		spdk_bit_array_free(&bs->used_blobids);
2044 		spdk_bit_array_free(&bs->used_md_pages);
2045 		spdk_bit_array_free(&bs->used_clusters);
2046 		free(bs);
2047 		return NULL;
2048 	}
2049 
2050 	return bs;
2051 }
2052 
2053 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */
2054 
2055 struct spdk_bs_load_ctx {
2056 	struct spdk_blob_store		*bs;
2057 	struct spdk_bs_super_block	*super;
2058 
2059 	struct spdk_bs_md_mask		*mask;
2060 	bool				in_page_chain;
2061 	uint32_t			page_index;
2062 	uint32_t			cur_page;
2063 	struct spdk_blob_md_page	*page;
2064 	bool				is_load;
2065 };
2066 
2067 static void
2068 _spdk_bs_load_ctx_fail(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno)
2069 {
2070 	assert(bserrno != 0);
2071 
2072 	spdk_dma_free(ctx->super);
2073 	spdk_bs_sequence_finish(seq, bserrno);
2074 	/*
2075 	 * Only free the blobstore when a load fails.  If an unload fails (for some reason)
2076 	 *  we want to keep the blobstore in case the caller wants to try again.
2077 	 */
2078 	if (ctx->is_load) {
2079 		_spdk_bs_free(ctx->bs);
2080 	}
2081 	free(ctx);
2082 }
2083 
2084 static void
2085 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask)
2086 {
2087 	uint32_t i = 0;
2088 
2089 	while (true) {
2090 		i = spdk_bit_array_find_first_set(array, i);
2091 		if (i >= mask->length) {
2092 			break;
2093 		}
2094 		mask->mask[i / 8] |= 1U << (i % 8);
2095 		i++;
2096 	}
2097 }
2098 
2099 static void
2100 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
2101 		     struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
2102 {
2103 	/* Update the values in the super block */
2104 	super->super_blob = bs->super_blob;
2105 	memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
2106 	super->crc = _spdk_blob_md_page_calc_crc(super);
2107 	spdk_bs_sequence_write_dev(seq, super, _spdk_bs_page_to_lba(bs, 0),
2108 				   _spdk_bs_byte_to_lba(bs, sizeof(*super)),
2109 				   cb_fn, cb_arg);
2110 }
2111 
2112 static void
2113 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
2114 {
2115 	struct spdk_bs_load_ctx	*ctx = arg;
2116 	uint64_t	mask_size, lba, lba_count;
2117 
2118 	/* Write out the used clusters mask */
2119 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
2120 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
2121 	if (!ctx->mask) {
2122 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2123 		return;
2124 	}
2125 
2126 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
2127 	ctx->mask->length = ctx->bs->total_clusters;
2128 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters));
2129 
2130 	_spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask);
2131 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
2132 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
2133 	spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
2134 }
2135 
2136 static void
2137 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
2138 {
2139 	struct spdk_bs_load_ctx	*ctx = arg;
2140 	uint64_t	mask_size, lba, lba_count;
2141 
2142 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
2143 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
2144 	if (!ctx->mask) {
2145 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2146 		return;
2147 	}
2148 
2149 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
2150 	ctx->mask->length = ctx->super->md_len;
2151 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
2152 
2153 	_spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask);
2154 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
2155 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
2156 	spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
2157 }
2158 
2159 static void
2160 _spdk_bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
2161 {
2162 	struct spdk_bs_load_ctx	*ctx = arg;
2163 	uint64_t	mask_size, lba, lba_count;
2164 
2165 	if (ctx->super->used_blobid_mask_len == 0) {
2166 		/*
2167 		 * This is a pre-v3 on-disk format where the blobid mask does not get
2168 		 *  written to disk.
2169 		 */
2170 		cb_fn(seq, arg, 0);
2171 		return;
2172 	}
2173 
2174 	mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
2175 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
2176 	if (!ctx->mask) {
2177 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2178 		return;
2179 	}
2180 
2181 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS;
2182 	ctx->mask->length = ctx->super->md_len;
2183 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids));
2184 
2185 	_spdk_bs_set_mask(ctx->bs->used_blobids, ctx->mask);
2186 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
2187 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
2188 	spdk_bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
2189 }
2190 
2191 static void
2192 _spdk_bs_load_complete(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno)
2193 {
2194 	spdk_dma_free(ctx->super);
2195 	spdk_dma_free(ctx->mask);
2196 	free(ctx);
2197 	spdk_bs_sequence_finish(seq, bserrno);
2198 }
2199 
2200 static void
2201 _spdk_bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2202 {
2203 	struct spdk_bs_load_ctx *ctx = cb_arg;
2204 	uint32_t i, j;
2205 	int rc;
2206 
2207 	/* The type must be correct */
2208 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS);
2209 
2210 	/* The length of the mask (in bits) must not be greater than
2211 	 * the length of the buffer (converted to bits) */
2212 	assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8));
2213 
2214 	/* The length of the mask must be exactly equal to the size
2215 	 * (in pages) of the metadata region */
2216 	assert(ctx->mask->length == ctx->super->md_len);
2217 
2218 	rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length);
2219 	if (rc < 0) {
2220 		spdk_dma_free(ctx->mask);
2221 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2222 		return;
2223 	}
2224 
2225 	for (i = 0; i < ctx->mask->length / 8; i++) {
2226 		uint8_t segment = ctx->mask->mask[i];
2227 		for (j = 0; segment; j++) {
2228 			if (segment & 1U) {
2229 				spdk_bit_array_set(ctx->bs->used_blobids, (i * 8) + j);
2230 			}
2231 			segment >>= 1U;
2232 		}
2233 	}
2234 
2235 	_spdk_bs_load_complete(seq, ctx, bserrno);
2236 }
2237 
2238 static void
2239 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2240 {
2241 	struct spdk_bs_load_ctx *ctx = cb_arg;
2242 	uint64_t		lba, lba_count, mask_size;
2243 	uint32_t		i, j;
2244 	int			rc;
2245 
2246 	/* The type must be correct */
2247 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
2248 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
2249 	assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
2250 					     struct spdk_blob_md_page) * 8));
2251 	/* The length of the mask must be exactly equal to the total number of clusters */
2252 	assert(ctx->mask->length == ctx->bs->total_clusters);
2253 
2254 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
2255 	if (rc < 0) {
2256 		spdk_dma_free(ctx->mask);
2257 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2258 		return;
2259 	}
2260 
2261 	ctx->bs->num_free_clusters = ctx->bs->total_clusters;
2262 	for (i = 0; i < ctx->mask->length / 8; i++) {
2263 		uint8_t segment = ctx->mask->mask[i];
2264 		for (j = 0; segment && (j < 8); j++) {
2265 			if (segment & 1U) {
2266 				spdk_bit_array_set(ctx->bs->used_clusters, (i * 8) + j);
2267 				assert(ctx->bs->num_free_clusters > 0);
2268 				ctx->bs->num_free_clusters--;
2269 			}
2270 			segment >>= 1U;
2271 		}
2272 	}
2273 
2274 	spdk_dma_free(ctx->mask);
2275 
2276 	/* Read the used blobids mask */
2277 	mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
2278 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
2279 	if (!ctx->mask) {
2280 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2281 		return;
2282 	}
2283 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
2284 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
2285 	spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
2286 				  _spdk_bs_load_used_blobids_cpl, ctx);
2287 }
2288 
2289 static void
2290 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2291 {
2292 	struct spdk_bs_load_ctx *ctx = cb_arg;
2293 	uint64_t		lba, lba_count, mask_size;
2294 	uint32_t		i, j;
2295 	int			rc;
2296 
2297 	/* The type must be correct */
2298 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
2299 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
2300 	assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
2301 				     8));
2302 	/* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
2303 	assert(ctx->mask->length == ctx->super->md_len);
2304 
2305 	rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
2306 	if (rc < 0) {
2307 		spdk_dma_free(ctx->mask);
2308 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2309 		return;
2310 	}
2311 
2312 	for (i = 0; i < ctx->mask->length / 8; i++) {
2313 		uint8_t segment = ctx->mask->mask[i];
2314 		for (j = 0; segment && (j < 8); j++) {
2315 			if (segment & 1U) {
2316 				spdk_bit_array_set(ctx->bs->used_md_pages, (i * 8) + j);
2317 			}
2318 			segment >>= 1U;
2319 		}
2320 	}
2321 	spdk_dma_free(ctx->mask);
2322 
2323 	/* Read the used clusters mask */
2324 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
2325 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
2326 	if (!ctx->mask) {
2327 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2328 		return;
2329 	}
2330 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
2331 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
2332 	spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
2333 				  _spdk_bs_load_used_clusters_cpl, ctx);
2334 }
2335 
2336 static void
2337 _spdk_bs_load_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2338 {
2339 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2340 	uint64_t lba, lba_count, mask_size;
2341 
2342 	/* Read the used pages mask */
2343 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
2344 	ctx->mask = spdk_dma_zmalloc(mask_size, 0x1000, NULL);
2345 	if (!ctx->mask) {
2346 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2347 		return;
2348 	}
2349 
2350 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
2351 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
2352 	spdk_bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
2353 				  _spdk_bs_load_used_pages_cpl, ctx);
2354 }
2355 
2356 static int
2357 _spdk_bs_load_replay_md_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob_store *bs)
2358 {
2359 	struct spdk_blob_md_descriptor *desc;
2360 	size_t	cur_desc = 0;
2361 
2362 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
2363 	while (cur_desc < sizeof(page->descriptors)) {
2364 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
2365 			if (desc->length == 0) {
2366 				/* If padding and length are 0, this terminates the page */
2367 				break;
2368 			}
2369 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT) {
2370 			struct spdk_blob_md_descriptor_extent	*desc_extent;
2371 			unsigned int				i, j;
2372 			unsigned int				cluster_count = 0;
2373 
2374 			desc_extent = (struct spdk_blob_md_descriptor_extent *)desc;
2375 
2376 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->extents[0]); i++) {
2377 				for (j = 0; j < desc_extent->extents[i].length; j++) {
2378 					spdk_bit_array_set(bs->used_clusters, desc_extent->extents[i].cluster_idx + j);
2379 					if (bs->num_free_clusters == 0) {
2380 						return -1;
2381 					}
2382 					bs->num_free_clusters--;
2383 					cluster_count++;
2384 				}
2385 			}
2386 			if (cluster_count == 0) {
2387 				return -1;
2388 			}
2389 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
2390 			/* Skip this item */
2391 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
2392 			/* Skip this item */
2393 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
2394 			/* Skip this item */
2395 		} else {
2396 			/* Error */
2397 			return -1;
2398 		}
2399 		/* Advance to the next descriptor */
2400 		cur_desc += sizeof(*desc) + desc->length;
2401 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
2402 			break;
2403 		}
2404 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
2405 	}
2406 	return 0;
2407 }
2408 
2409 static bool _spdk_bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
2410 {
2411 	uint32_t crc;
2412 
2413 	crc = _spdk_blob_md_page_calc_crc(ctx->page);
2414 	if (crc != ctx->page->crc) {
2415 		return false;
2416 	}
2417 
2418 	if (_spdk_bs_page_to_blobid(ctx->cur_page) != ctx->page->id) {
2419 		return false;
2420 	}
2421 	return true;
2422 }
2423 
2424 static void
2425 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
2426 
2427 static void
2428 _spdk_bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2429 {
2430 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2431 
2432 	_spdk_bs_load_complete(seq, ctx, bserrno);
2433 }
2434 
2435 static void
2436 _spdk_bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2437 {
2438 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2439 
2440 	spdk_dma_free(ctx->mask);
2441 	ctx->mask = NULL;
2442 
2443 	_spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_load_write_used_clusters_cpl);
2444 }
2445 
2446 static void
2447 _spdk_bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2448 {
2449 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2450 
2451 	spdk_dma_free(ctx->mask);
2452 	ctx->mask = NULL;
2453 
2454 	_spdk_bs_write_used_blobids(seq, cb_arg, _spdk_bs_load_write_used_blobids_cpl);
2455 }
2456 
2457 static void
2458 _spdk_bs_load_write_used_md(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2459 {
2460 	_spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_load_write_used_pages_cpl);
2461 }
2462 
2463 static void
2464 _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2465 {
2466 	struct spdk_bs_load_ctx *ctx = cb_arg;
2467 	uint64_t num_md_clusters;
2468 	uint64_t i;
2469 	uint32_t page_num;
2470 
2471 	if (bserrno != 0) {
2472 		_spdk_bs_load_ctx_fail(seq, ctx, bserrno);
2473 		return;
2474 	}
2475 
2476 	page_num = ctx->cur_page;
2477 	if (_spdk_bs_load_cur_md_page_valid(ctx) == true) {
2478 		if (ctx->page->sequence_num == 0 || ctx->in_page_chain == true) {
2479 			spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
2480 			if (ctx->page->sequence_num == 0) {
2481 				spdk_bit_array_set(ctx->bs->used_blobids, page_num);
2482 			}
2483 			if (_spdk_bs_load_replay_md_parse_page(ctx->page, ctx->bs)) {
2484 				_spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ);
2485 				return;
2486 			}
2487 			if (ctx->page->next != SPDK_INVALID_MD_PAGE) {
2488 				ctx->in_page_chain = true;
2489 				ctx->cur_page = ctx->page->next;
2490 				_spdk_bs_load_replay_cur_md_page(seq, cb_arg);
2491 				return;
2492 			}
2493 		}
2494 	}
2495 
2496 	ctx->in_page_chain = false;
2497 
2498 	do {
2499 		ctx->page_index++;
2500 	} while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
2501 
2502 	if (ctx->page_index < ctx->super->md_len) {
2503 		ctx->cur_page = ctx->page_index;
2504 		_spdk_bs_load_replay_cur_md_page(seq, cb_arg);
2505 	} else {
2506 		/* Claim all of the clusters used by the metadata */
2507 		num_md_clusters = divide_round_up(ctx->super->md_len, ctx->bs->pages_per_cluster);
2508 		for (i = 0; i < num_md_clusters; i++) {
2509 			_spdk_bs_claim_cluster(ctx->bs, i);
2510 		}
2511 		spdk_dma_free(ctx->page);
2512 		_spdk_bs_load_write_used_md(seq, ctx, bserrno);
2513 	}
2514 }
2515 
2516 static void
2517 _spdk_bs_load_replay_cur_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
2518 {
2519 	struct spdk_bs_load_ctx *ctx = cb_arg;
2520 	uint64_t lba;
2521 
2522 	assert(ctx->cur_page < ctx->super->md_len);
2523 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
2524 	spdk_bs_sequence_read_dev(seq, ctx->page, lba,
2525 				  _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
2526 				  _spdk_bs_load_replay_md_cpl, ctx);
2527 }
2528 
2529 static void
2530 _spdk_bs_load_replay_md(spdk_bs_sequence_t *seq, void *cb_arg)
2531 {
2532 	struct spdk_bs_load_ctx *ctx = cb_arg;
2533 
2534 	ctx->page_index = 0;
2535 	ctx->cur_page = 0;
2536 	ctx->page = spdk_dma_zmalloc(SPDK_BS_PAGE_SIZE,
2537 				     SPDK_BS_PAGE_SIZE,
2538 				     NULL);
2539 	if (!ctx->page) {
2540 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2541 		return;
2542 	}
2543 	_spdk_bs_load_replay_cur_md_page(seq, cb_arg);
2544 }
2545 
2546 static void
2547 _spdk_bs_recover(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2548 {
2549 	struct spdk_bs_load_ctx *ctx = cb_arg;
2550 	int 		rc;
2551 
2552 	if (bserrno != 0) {
2553 		_spdk_bs_load_ctx_fail(seq, ctx, -EIO);
2554 		return;
2555 	}
2556 
2557 	rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
2558 	if (rc < 0) {
2559 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2560 		return;
2561 	}
2562 
2563 	rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len);
2564 	if (rc < 0) {
2565 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2566 		return;
2567 	}
2568 
2569 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
2570 	if (rc < 0) {
2571 		_spdk_bs_load_ctx_fail(seq, ctx, -ENOMEM);
2572 		return;
2573 	}
2574 
2575 	ctx->bs->num_free_clusters = ctx->bs->total_clusters;
2576 	_spdk_bs_load_replay_md(seq, cb_arg);
2577 }
2578 
2579 static void
2580 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2581 {
2582 	struct spdk_bs_load_ctx *ctx = cb_arg;
2583 	uint32_t	crc;
2584 	static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
2585 
2586 	if (ctx->super->version > SPDK_BS_VERSION ||
2587 	    ctx->super->version < SPDK_BS_INITIAL_VERSION) {
2588 		_spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ);
2589 		return;
2590 	}
2591 
2592 	if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
2593 		   sizeof(ctx->super->signature)) != 0) {
2594 		_spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ);
2595 		return;
2596 	}
2597 
2598 	crc = _spdk_blob_md_page_calc_crc(ctx->super);
2599 	if (crc != ctx->super->crc) {
2600 		_spdk_bs_load_ctx_fail(seq, ctx, -EILSEQ);
2601 		return;
2602 	}
2603 
2604 	if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
2605 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype matched - loading blobstore\n");
2606 	} else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
2607 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n");
2608 	} else {
2609 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Unexpected bstype\n");
2610 		SPDK_TRACEDUMP(SPDK_LOG_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
2611 		SPDK_TRACEDUMP(SPDK_LOG_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
2612 		_spdk_bs_load_ctx_fail(seq, ctx, -ENXIO);
2613 		return;
2614 	}
2615 
2616 	/* Parse the super block */
2617 	ctx->bs->cluster_sz = ctx->super->cluster_size;
2618 	ctx->bs->total_clusters = ctx->bs->dev->blockcnt / (ctx->bs->cluster_sz / ctx->bs->dev->blocklen);
2619 	ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
2620 	ctx->bs->md_start = ctx->super->md_start;
2621 	ctx->bs->md_len = ctx->super->md_len;
2622 	ctx->bs->total_data_clusters = ctx->bs->total_clusters - divide_round_up(
2623 					       ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
2624 	ctx->bs->super_blob = ctx->super->super_blob;
2625 	memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
2626 
2627 	if (ctx->super->clean == 0) {
2628 		_spdk_bs_recover(seq, ctx, 0);
2629 	} else if (ctx->super->used_blobid_mask_len == 0) {
2630 		/*
2631 		 * Metadata is clean, but this is an old metadata format without
2632 		 *  a blobid mask.  Clear the clean bit and then build the masks
2633 		 *  using _spdk_bs_recover.
2634 		 */
2635 		ctx->super->clean = 0;
2636 		_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_recover, ctx);
2637 	} else {
2638 		ctx->super->clean = 0;
2639 		_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_load_write_super_cpl, ctx);
2640 	}
2641 }
2642 
2643 void
2644 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
2645 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
2646 {
2647 	struct spdk_blob_store	*bs;
2648 	struct spdk_bs_cpl	cpl;
2649 	spdk_bs_sequence_t	*seq;
2650 	struct spdk_bs_load_ctx *ctx;
2651 	struct spdk_bs_opts	opts = {};
2652 
2653 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Loading blobstore from dev %p\n", dev);
2654 
2655 	if (o) {
2656 		opts = *o;
2657 	} else {
2658 		spdk_bs_opts_init(&opts);
2659 	}
2660 
2661 	if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
2662 		cb_fn(cb_arg, NULL, -EINVAL);
2663 		return;
2664 	}
2665 
2666 	bs = _spdk_bs_alloc(dev, &opts);
2667 	if (!bs) {
2668 		cb_fn(cb_arg, NULL, -ENOMEM);
2669 		return;
2670 	}
2671 
2672 	ctx = calloc(1, sizeof(*ctx));
2673 	if (!ctx) {
2674 		_spdk_bs_free(bs);
2675 		cb_fn(cb_arg, NULL, -ENOMEM);
2676 		return;
2677 	}
2678 
2679 	ctx->bs = bs;
2680 	ctx->is_load = true;
2681 
2682 	/* Allocate memory for the super block */
2683 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
2684 	if (!ctx->super) {
2685 		free(ctx);
2686 		_spdk_bs_free(bs);
2687 		return;
2688 	}
2689 
2690 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
2691 	cpl.u.bs_handle.cb_fn = cb_fn;
2692 	cpl.u.bs_handle.cb_arg = cb_arg;
2693 	cpl.u.bs_handle.bs = bs;
2694 
2695 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
2696 	if (!seq) {
2697 		spdk_dma_free(ctx->super);
2698 		free(ctx);
2699 		_spdk_bs_free(bs);
2700 		cb_fn(cb_arg, NULL, -ENOMEM);
2701 		return;
2702 	}
2703 
2704 	/* Read the super block */
2705 	spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
2706 				  _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
2707 				  _spdk_bs_load_super_cpl, ctx);
2708 }
2709 
2710 /* END spdk_bs_load */
2711 
2712 /* START spdk_bs_init */
2713 
2714 struct spdk_bs_init_ctx {
2715 	struct spdk_blob_store		*bs;
2716 	struct spdk_bs_super_block	*super;
2717 };
2718 
2719 static void
2720 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2721 {
2722 	struct spdk_bs_init_ctx *ctx = cb_arg;
2723 
2724 	spdk_dma_free(ctx->super);
2725 	free(ctx);
2726 
2727 	spdk_bs_sequence_finish(seq, bserrno);
2728 }
2729 
2730 static void
2731 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2732 {
2733 	struct spdk_bs_init_ctx *ctx = cb_arg;
2734 
2735 	/* Write super block */
2736 	spdk_bs_sequence_write_dev(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0),
2737 				   _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
2738 				   _spdk_bs_init_persist_super_cpl, ctx);
2739 }
2740 
2741 void
2742 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
2743 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
2744 {
2745 	struct spdk_bs_init_ctx *ctx;
2746 	struct spdk_blob_store	*bs;
2747 	struct spdk_bs_cpl	cpl;
2748 	spdk_bs_sequence_t	*seq;
2749 	spdk_bs_batch_t		*batch;
2750 	uint64_t		num_md_lba;
2751 	uint64_t		num_md_pages;
2752 	uint64_t		num_md_clusters;
2753 	uint32_t		i;
2754 	struct spdk_bs_opts	opts = {};
2755 	int			rc;
2756 
2757 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Initializing blobstore on dev %p\n", dev);
2758 
2759 	if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
2760 		SPDK_ERRLOG("unsupported dev block length of %d\n",
2761 			    dev->blocklen);
2762 		dev->destroy(dev);
2763 		cb_fn(cb_arg, NULL, -EINVAL);
2764 		return;
2765 	}
2766 
2767 	if (o) {
2768 		opts = *o;
2769 	} else {
2770 		spdk_bs_opts_init(&opts);
2771 	}
2772 
2773 	if (_spdk_bs_opts_verify(&opts) != 0) {
2774 		dev->destroy(dev);
2775 		cb_fn(cb_arg, NULL, -EINVAL);
2776 		return;
2777 	}
2778 
2779 	bs = _spdk_bs_alloc(dev, &opts);
2780 	if (!bs) {
2781 		dev->destroy(dev);
2782 		cb_fn(cb_arg, NULL, -ENOMEM);
2783 		return;
2784 	}
2785 
2786 	if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
2787 		/* By default, allocate 1 page per cluster.
2788 		 * Technically, this over-allocates metadata
2789 		 * because more metadata will reduce the number
2790 		 * of usable clusters. This can be addressed with
2791 		 * more complex math in the future.
2792 		 */
2793 		bs->md_len = bs->total_clusters;
2794 	} else {
2795 		bs->md_len = opts.num_md_pages;
2796 	}
2797 
2798 	rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
2799 	if (rc < 0) {
2800 		_spdk_bs_free(bs);
2801 		cb_fn(cb_arg, NULL, -ENOMEM);
2802 		return;
2803 	}
2804 
2805 	rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len);
2806 	if (rc < 0) {
2807 		_spdk_bs_free(bs);
2808 		cb_fn(cb_arg, NULL, -ENOMEM);
2809 		return;
2810 	}
2811 
2812 	ctx = calloc(1, sizeof(*ctx));
2813 	if (!ctx) {
2814 		_spdk_bs_free(bs);
2815 		cb_fn(cb_arg, NULL, -ENOMEM);
2816 		return;
2817 	}
2818 
2819 	ctx->bs = bs;
2820 
2821 	/* Allocate memory for the super block */
2822 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
2823 	if (!ctx->super) {
2824 		free(ctx);
2825 		_spdk_bs_free(bs);
2826 		return;
2827 	}
2828 	memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
2829 	       sizeof(ctx->super->signature));
2830 	ctx->super->version = SPDK_BS_VERSION;
2831 	ctx->super->length = sizeof(*ctx->super);
2832 	ctx->super->super_blob = bs->super_blob;
2833 	ctx->super->clean = 0;
2834 	ctx->super->cluster_size = bs->cluster_sz;
2835 	memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
2836 
2837 	/* Calculate how many pages the metadata consumes at the front
2838 	 * of the disk.
2839 	 */
2840 
2841 	/* The super block uses 1 page */
2842 	num_md_pages = 1;
2843 
2844 	/* The used_md_pages mask requires 1 bit per metadata page, rounded
2845 	 * up to the nearest page, plus a header.
2846 	 */
2847 	ctx->super->used_page_mask_start = num_md_pages;
2848 	ctx->super->used_page_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
2849 					 divide_round_up(bs->md_len, 8),
2850 					 SPDK_BS_PAGE_SIZE);
2851 	num_md_pages += ctx->super->used_page_mask_len;
2852 
2853 	/* The used_clusters mask requires 1 bit per cluster, rounded
2854 	 * up to the nearest page, plus a header.
2855 	 */
2856 	ctx->super->used_cluster_mask_start = num_md_pages;
2857 	ctx->super->used_cluster_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
2858 					    divide_round_up(bs->total_clusters, 8),
2859 					    SPDK_BS_PAGE_SIZE);
2860 	num_md_pages += ctx->super->used_cluster_mask_len;
2861 
2862 	/* The used_blobids mask requires 1 bit per metadata page, rounded
2863 	 * up to the nearest page, plus a header.
2864 	 */
2865 	ctx->super->used_blobid_mask_start = num_md_pages;
2866 	ctx->super->used_blobid_mask_len = divide_round_up(sizeof(struct spdk_bs_md_mask) +
2867 					   divide_round_up(bs->md_len, 8),
2868 					   SPDK_BS_PAGE_SIZE);
2869 	num_md_pages += ctx->super->used_blobid_mask_len;
2870 
2871 	/* The metadata region size was chosen above */
2872 	ctx->super->md_start = bs->md_start = num_md_pages;
2873 	ctx->super->md_len = bs->md_len;
2874 	num_md_pages += bs->md_len;
2875 
2876 	num_md_lba = _spdk_bs_page_to_lba(bs, num_md_pages);
2877 
2878 	ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super);
2879 
2880 	num_md_clusters = divide_round_up(num_md_pages, bs->pages_per_cluster);
2881 	if (num_md_clusters > bs->total_clusters) {
2882 		SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
2883 			    "please decrease number of pages reserved for metadata "
2884 			    "or increase cluster size.\n");
2885 		spdk_dma_free(ctx->super);
2886 		free(ctx);
2887 		_spdk_bs_free(bs);
2888 		cb_fn(cb_arg, NULL, -ENOMEM);
2889 		return;
2890 	}
2891 	/* Claim all of the clusters used by the metadata */
2892 	for (i = 0; i < num_md_clusters; i++) {
2893 		_spdk_bs_claim_cluster(bs, i);
2894 	}
2895 
2896 	bs->total_data_clusters = bs->num_free_clusters;
2897 
2898 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
2899 	cpl.u.bs_handle.cb_fn = cb_fn;
2900 	cpl.u.bs_handle.cb_arg = cb_arg;
2901 	cpl.u.bs_handle.bs = bs;
2902 
2903 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
2904 	if (!seq) {
2905 		spdk_dma_free(ctx->super);
2906 		free(ctx);
2907 		_spdk_bs_free(bs);
2908 		cb_fn(cb_arg, NULL, -ENOMEM);
2909 		return;
2910 	}
2911 
2912 	batch = spdk_bs_sequence_to_batch(seq, _spdk_bs_init_trim_cpl, ctx);
2913 
2914 	/* Clear metadata space */
2915 	spdk_bs_batch_write_zeroes_dev(batch, 0, num_md_lba);
2916 	/* Trim data clusters */
2917 	spdk_bs_batch_unmap_dev(batch, num_md_lba, ctx->bs->dev->blockcnt - num_md_lba);
2918 
2919 	spdk_bs_batch_close(batch);
2920 }
2921 
2922 /* END spdk_bs_init */
2923 
2924 /* START spdk_bs_destroy */
2925 
2926 static void
2927 _spdk_bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2928 {
2929 	struct spdk_bs_init_ctx *ctx = cb_arg;
2930 	struct spdk_blob_store *bs = ctx->bs;
2931 
2932 	/*
2933 	 * We need to defer calling spdk_bs_call_cpl() until after
2934 	 * dev destruction, so tuck these away for later use.
2935 	 */
2936 	bs->unload_err = bserrno;
2937 	memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
2938 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
2939 
2940 	spdk_bs_sequence_finish(seq, bserrno);
2941 
2942 	_spdk_bs_free(bs);
2943 	free(ctx);
2944 }
2945 
2946 void
2947 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn,
2948 		void *cb_arg)
2949 {
2950 	struct spdk_bs_cpl	cpl;
2951 	spdk_bs_sequence_t	*seq;
2952 	struct spdk_bs_init_ctx *ctx;
2953 
2954 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Destroying blobstore\n");
2955 
2956 	if (!TAILQ_EMPTY(&bs->blobs)) {
2957 		SPDK_ERRLOG("Blobstore still has open blobs\n");
2958 		cb_fn(cb_arg, -EBUSY);
2959 		return;
2960 	}
2961 
2962 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
2963 	cpl.u.bs_basic.cb_fn = cb_fn;
2964 	cpl.u.bs_basic.cb_arg = cb_arg;
2965 
2966 	ctx = calloc(1, sizeof(*ctx));
2967 	if (!ctx) {
2968 		cb_fn(cb_arg, -ENOMEM);
2969 		return;
2970 	}
2971 
2972 	ctx->bs = bs;
2973 
2974 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
2975 	if (!seq) {
2976 		free(ctx);
2977 		cb_fn(cb_arg, -ENOMEM);
2978 		return;
2979 	}
2980 
2981 	/* Write zeroes to the super block */
2982 	spdk_bs_sequence_write_zeroes_dev(seq,
2983 					  _spdk_bs_page_to_lba(bs, 0),
2984 					  _spdk_bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)),
2985 					  _spdk_bs_destroy_trim_cpl, ctx);
2986 }
2987 
2988 /* END spdk_bs_destroy */
2989 
2990 /* START spdk_bs_unload */
2991 
2992 static void
2993 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2994 {
2995 	struct spdk_bs_load_ctx	*ctx = cb_arg;
2996 
2997 	spdk_dma_free(ctx->super);
2998 
2999 	/*
3000 	 * We need to defer calling spdk_bs_call_cpl() until after
3001 	 * dev destuction, so tuck these away for later use.
3002 	 */
3003 	ctx->bs->unload_err = bserrno;
3004 	memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
3005 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
3006 
3007 	spdk_bs_sequence_finish(seq, bserrno);
3008 
3009 	_spdk_bs_free(ctx->bs);
3010 	free(ctx);
3011 }
3012 
3013 static void
3014 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3015 {
3016 	struct spdk_bs_load_ctx	*ctx = cb_arg;
3017 
3018 	spdk_dma_free(ctx->mask);
3019 	ctx->super->clean = 1;
3020 
3021 	_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx);
3022 }
3023 
3024 static void
3025 _spdk_bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3026 {
3027 	struct spdk_bs_load_ctx	*ctx = cb_arg;
3028 
3029 	spdk_dma_free(ctx->mask);
3030 	ctx->mask = NULL;
3031 
3032 	_spdk_bs_write_used_clusters(seq, cb_arg, _spdk_bs_unload_write_used_clusters_cpl);
3033 }
3034 
3035 static void
3036 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3037 {
3038 	struct spdk_bs_load_ctx	*ctx = cb_arg;
3039 
3040 	spdk_dma_free(ctx->mask);
3041 	ctx->mask = NULL;
3042 
3043 	_spdk_bs_write_used_blobids(seq, cb_arg, _spdk_bs_unload_write_used_blobids_cpl);
3044 }
3045 
3046 static void
3047 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3048 {
3049 	_spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl);
3050 }
3051 
3052 void
3053 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
3054 {
3055 	struct spdk_bs_cpl	cpl;
3056 	spdk_bs_sequence_t	*seq;
3057 	struct spdk_bs_load_ctx *ctx;
3058 
3059 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blobstore\n");
3060 
3061 	if (!TAILQ_EMPTY(&bs->blobs)) {
3062 		SPDK_ERRLOG("Blobstore still has open blobs\n");
3063 		cb_fn(cb_arg, -EBUSY);
3064 		return;
3065 	}
3066 
3067 	ctx = calloc(1, sizeof(*ctx));
3068 	if (!ctx) {
3069 		cb_fn(cb_arg, -ENOMEM);
3070 		return;
3071 	}
3072 
3073 	ctx->bs = bs;
3074 	ctx->is_load = false;
3075 
3076 	ctx->super = spdk_dma_zmalloc(sizeof(*ctx->super), 0x1000, NULL);
3077 	if (!ctx->super) {
3078 		free(ctx);
3079 		cb_fn(cb_arg, -ENOMEM);
3080 		return;
3081 	}
3082 
3083 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
3084 	cpl.u.bs_basic.cb_fn = cb_fn;
3085 	cpl.u.bs_basic.cb_arg = cb_arg;
3086 
3087 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
3088 	if (!seq) {
3089 		spdk_dma_free(ctx->super);
3090 		free(ctx);
3091 		cb_fn(cb_arg, -ENOMEM);
3092 		return;
3093 	}
3094 
3095 	/* Read super block */
3096 	spdk_bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
3097 				  _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
3098 				  _spdk_bs_unload_read_super_cpl, ctx);
3099 }
3100 
3101 /* END spdk_bs_unload */
3102 
3103 void
3104 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
3105 		  spdk_bs_op_complete cb_fn, void *cb_arg)
3106 {
3107 	bs->super_blob = blobid;
3108 	cb_fn(cb_arg, 0);
3109 }
3110 
3111 void
3112 spdk_bs_get_super(struct spdk_blob_store *bs,
3113 		  spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
3114 {
3115 	if (bs->super_blob == SPDK_BLOBID_INVALID) {
3116 		cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
3117 	} else {
3118 		cb_fn(cb_arg, bs->super_blob, 0);
3119 	}
3120 }
3121 
3122 uint64_t
3123 spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
3124 {
3125 	return bs->cluster_sz;
3126 }
3127 
3128 uint64_t
3129 spdk_bs_get_page_size(struct spdk_blob_store *bs)
3130 {
3131 	return SPDK_BS_PAGE_SIZE;
3132 }
3133 
3134 uint64_t
3135 spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
3136 {
3137 	return bs->num_free_clusters;
3138 }
3139 
3140 uint64_t
3141 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs)
3142 {
3143 	return bs->total_data_clusters;
3144 }
3145 
3146 static int
3147 spdk_bs_register_md_thread(struct spdk_blob_store *bs)
3148 {
3149 	bs->md_channel = spdk_get_io_channel(bs);
3150 	if (!bs->md_channel) {
3151 		SPDK_ERRLOG("Failed to get IO channel.\n");
3152 		return -1;
3153 	}
3154 
3155 	return 0;
3156 }
3157 
3158 static int
3159 spdk_bs_unregister_md_thread(struct spdk_blob_store *bs)
3160 {
3161 	spdk_put_io_channel(bs->md_channel);
3162 
3163 	return 0;
3164 }
3165 
3166 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob)
3167 {
3168 	assert(blob != NULL);
3169 
3170 	return blob->id;
3171 }
3172 
3173 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob)
3174 {
3175 	assert(blob != NULL);
3176 
3177 	return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters);
3178 }
3179 
3180 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob)
3181 {
3182 	assert(blob != NULL);
3183 
3184 	return blob->active.num_clusters;
3185 }
3186 
3187 /* START spdk_bs_create_blob */
3188 
3189 static void
3190 _spdk_bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3191 {
3192 	struct spdk_blob *blob = cb_arg;
3193 
3194 	_spdk_blob_free(blob);
3195 
3196 	spdk_bs_sequence_finish(seq, bserrno);
3197 }
3198 
3199 static int
3200 _spdk_blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs,
3201 		      bool internal)
3202 {
3203 	uint64_t i;
3204 	size_t value_len = 0;
3205 	int rc;
3206 	const void *value = NULL;
3207 	if (xattrs->count > 0 && xattrs->get_value == NULL) {
3208 		return -EINVAL;
3209 	}
3210 	for (i = 0; i < xattrs->count; i++) {
3211 		xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len);
3212 		if (value == NULL || value_len == 0) {
3213 			return -EINVAL;
3214 		}
3215 		rc = _spdk_blob_set_xattr(blob, xattrs->names[i], value, value_len, internal);
3216 		if (rc < 0) {
3217 			return rc;
3218 		}
3219 	}
3220 	return 0;
3221 }
3222 
3223 static void
3224 _spdk_blob_set_thin_provision(struct spdk_blob *blob)
3225 {
3226 	_spdk_blob_verify_md_op(blob);
3227 	blob->invalid_flags |= SPDK_BLOB_THIN_PROV;
3228 	blob->state = SPDK_BLOB_STATE_DIRTY;
3229 }
3230 
3231 void spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts,
3232 			     spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
3233 {
3234 	struct spdk_blob	*blob;
3235 	uint32_t		page_idx;
3236 	struct spdk_bs_cpl 	cpl;
3237 	struct spdk_blob_opts	opts_default;
3238 	spdk_bs_sequence_t	*seq;
3239 	spdk_blob_id		id;
3240 	int rc;
3241 
3242 	assert(spdk_get_thread() == bs->md_thread);
3243 
3244 	page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
3245 	if (page_idx >= spdk_bit_array_capacity(bs->used_md_pages)) {
3246 		cb_fn(cb_arg, 0, -ENOMEM);
3247 		return;
3248 	}
3249 	spdk_bit_array_set(bs->used_blobids, page_idx);
3250 	spdk_bit_array_set(bs->used_md_pages, page_idx);
3251 
3252 	id = _spdk_bs_page_to_blobid(page_idx);
3253 
3254 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx);
3255 
3256 	blob = _spdk_blob_alloc(bs, id);
3257 	if (!blob) {
3258 		cb_fn(cb_arg, 0, -ENOMEM);
3259 		return;
3260 	}
3261 
3262 	if (!opts) {
3263 		spdk_blob_opts_init(&opts_default);
3264 		opts = &opts_default;
3265 	}
3266 
3267 	rc = _spdk_blob_set_xattrs(blob, &opts->xattrs, false);
3268 	if (rc < 0) {
3269 		_spdk_blob_free(blob);
3270 		cb_fn(cb_arg, 0, rc);
3271 		return;
3272 	}
3273 	if (opts->thin_provision) {
3274 		_spdk_blob_set_thin_provision(blob);
3275 	}
3276 
3277 	rc = spdk_blob_resize(blob, opts->num_clusters);
3278 	if (rc < 0) {
3279 		_spdk_blob_free(blob);
3280 		cb_fn(cb_arg, 0, rc);
3281 		return;
3282 	}
3283 	cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
3284 	cpl.u.blobid.cb_fn = cb_fn;
3285 	cpl.u.blobid.cb_arg = cb_arg;
3286 	cpl.u.blobid.blobid = blob->id;
3287 
3288 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
3289 	if (!seq) {
3290 		_spdk_blob_free(blob);
3291 		cb_fn(cb_arg, 0, -ENOMEM);
3292 		return;
3293 	}
3294 
3295 	_spdk_blob_persist(seq, blob, _spdk_bs_create_blob_cpl, blob);
3296 }
3297 
3298 void spdk_bs_create_blob(struct spdk_blob_store *bs,
3299 			 spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
3300 {
3301 	spdk_bs_create_blob_ext(bs, NULL, cb_fn, cb_arg);
3302 }
3303 
3304 /* END spdk_bs_create_blob */
3305 
3306 /* START spdk_blob_resize */
3307 int
3308 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz)
3309 {
3310 	int			rc;
3311 
3312 	_spdk_blob_verify_md_op(blob);
3313 
3314 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz);
3315 
3316 	if (blob->md_ro) {
3317 		return -EPERM;
3318 	}
3319 
3320 	if (sz == blob->active.num_clusters) {
3321 		return 0;
3322 	}
3323 
3324 	rc = _spdk_resize_blob(blob, sz);
3325 	if (rc < 0) {
3326 		return rc;
3327 	}
3328 
3329 	return 0;
3330 }
3331 
3332 /* END spdk_blob_resize */
3333 
3334 
3335 /* START spdk_bs_delete_blob */
3336 
3337 static void
3338 _spdk_bs_delete_close_cpl(void *cb_arg, int bserrno)
3339 {
3340 	spdk_bs_sequence_t *seq = cb_arg;
3341 
3342 	spdk_bs_sequence_finish(seq, bserrno);
3343 }
3344 
3345 static void
3346 _spdk_bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3347 {
3348 	struct spdk_blob *blob = cb_arg;
3349 
3350 	if (bserrno != 0) {
3351 		/*
3352 		 * We already removed this blob from the blobstore tailq, so
3353 		 *  we need to free it here since this is the last reference
3354 		 *  to it.
3355 		 */
3356 		_spdk_blob_free(blob);
3357 		_spdk_bs_delete_close_cpl(seq, bserrno);
3358 		return;
3359 	}
3360 
3361 	/*
3362 	 * This will immediately decrement the ref_count and call
3363 	 *  the completion routine since the metadata state is clean.
3364 	 *  By calling spdk_blob_close, we reduce the number of call
3365 	 *  points into code that touches the blob->open_ref count
3366 	 *  and the blobstore's blob list.
3367 	 */
3368 	spdk_blob_close(blob, _spdk_bs_delete_close_cpl, seq);
3369 }
3370 
3371 static void
3372 _spdk_bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
3373 {
3374 	spdk_bs_sequence_t *seq = cb_arg;
3375 	uint32_t page_num;
3376 
3377 	if (bserrno != 0) {
3378 		spdk_bs_sequence_finish(seq, bserrno);
3379 		return;
3380 	}
3381 
3382 	_spdk_blob_verify_md_op(blob);
3383 
3384 	if (blob->open_ref > 1) {
3385 		/*
3386 		 * Someone has this blob open (besides this delete context).
3387 		 *  Decrement the ref count directly and return -EBUSY.
3388 		 */
3389 		blob->open_ref--;
3390 		spdk_bs_sequence_finish(seq, -EBUSY);
3391 		return;
3392 	}
3393 
3394 	/*
3395 	 * Remove the blob from the blob_store list now, to ensure it does not
3396 	 *  get returned after this point by _spdk_blob_lookup().
3397 	 */
3398 	TAILQ_REMOVE(&blob->bs->blobs, blob, link);
3399 	page_num = _spdk_bs_blobid_to_page(blob->id);
3400 	spdk_bit_array_clear(blob->bs->used_blobids, page_num);
3401 	blob->state = SPDK_BLOB_STATE_DIRTY;
3402 	blob->active.num_pages = 0;
3403 	_spdk_resize_blob(blob, 0);
3404 
3405 	_spdk_blob_persist(seq, blob, _spdk_bs_delete_persist_cpl, blob);
3406 }
3407 
3408 void
3409 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
3410 		    spdk_blob_op_complete cb_fn, void *cb_arg)
3411 {
3412 	struct spdk_bs_cpl	cpl;
3413 	spdk_bs_sequence_t 	*seq;
3414 
3415 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Deleting blob %lu\n", blobid);
3416 
3417 	assert(spdk_get_thread() == bs->md_thread);
3418 
3419 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3420 	cpl.u.blob_basic.cb_fn = cb_fn;
3421 	cpl.u.blob_basic.cb_arg = cb_arg;
3422 
3423 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
3424 	if (!seq) {
3425 		cb_fn(cb_arg, -ENOMEM);
3426 		return;
3427 	}
3428 
3429 	spdk_bs_open_blob(bs, blobid, _spdk_bs_delete_open_cpl, seq);
3430 }
3431 
3432 /* END spdk_bs_delete_blob */
3433 
3434 /* START spdk_bs_open_blob */
3435 
3436 static void
3437 _spdk_bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3438 {
3439 	struct spdk_blob *blob = cb_arg;
3440 
3441 	/* If the blob have crc error, we just return NULL. */
3442 	if (blob == NULL) {
3443 		seq->cpl.u.blob_handle.blob = NULL;
3444 		spdk_bs_sequence_finish(seq, bserrno);
3445 		return;
3446 	}
3447 
3448 	blob->open_ref++;
3449 
3450 	TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link);
3451 
3452 	spdk_bs_sequence_finish(seq, bserrno);
3453 }
3454 
3455 void spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
3456 		       spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
3457 {
3458 	struct spdk_blob		*blob;
3459 	struct spdk_bs_cpl		cpl;
3460 	spdk_bs_sequence_t		*seq;
3461 	uint32_t			page_num;
3462 
3463 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Opening blob %lu\n", blobid);
3464 	assert(spdk_get_thread() == bs->md_thread);
3465 
3466 	page_num = _spdk_bs_blobid_to_page(blobid);
3467 	if (spdk_bit_array_get(bs->used_blobids, page_num) == false) {
3468 		/* Invalid blobid */
3469 		cb_fn(cb_arg, NULL, -ENOENT);
3470 		return;
3471 	}
3472 
3473 	blob = _spdk_blob_lookup(bs, blobid);
3474 	if (blob) {
3475 		blob->open_ref++;
3476 		cb_fn(cb_arg, blob, 0);
3477 		return;
3478 	}
3479 
3480 	blob = _spdk_blob_alloc(bs, blobid);
3481 	if (!blob) {
3482 		cb_fn(cb_arg, NULL, -ENOMEM);
3483 		return;
3484 	}
3485 
3486 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
3487 	cpl.u.blob_handle.cb_fn = cb_fn;
3488 	cpl.u.blob_handle.cb_arg = cb_arg;
3489 	cpl.u.blob_handle.blob = blob;
3490 
3491 	seq = spdk_bs_sequence_start(bs->md_channel, &cpl);
3492 	if (!seq) {
3493 		_spdk_blob_free(blob);
3494 		cb_fn(cb_arg, NULL, -ENOMEM);
3495 		return;
3496 	}
3497 
3498 	_spdk_blob_load(seq, blob, _spdk_bs_open_blob_cpl, blob);
3499 }
3500 /* END spdk_bs_open_blob */
3501 
3502 /* START spdk_blob_set_read_only */
3503 int spdk_blob_set_read_only(struct spdk_blob *blob)
3504 {
3505 	_spdk_blob_verify_md_op(blob);
3506 
3507 	blob->data_ro_flags |= SPDK_BLOB_READ_ONLY;
3508 
3509 	blob->state = SPDK_BLOB_STATE_DIRTY;
3510 	return 0;
3511 }
3512 /* END spdk_blob_set_read_only */
3513 
3514 /* START spdk_blob_sync_md */
3515 
3516 static void
3517 _spdk_blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3518 {
3519 	struct spdk_blob *blob = cb_arg;
3520 
3521 	if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
3522 		blob->data_ro = true;
3523 		blob->md_ro = true;
3524 	}
3525 
3526 	spdk_bs_sequence_finish(seq, bserrno);
3527 }
3528 
3529 static void
3530 _spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
3531 {
3532 	struct spdk_bs_cpl	cpl;
3533 	spdk_bs_sequence_t	*seq;
3534 
3535 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3536 	cpl.u.blob_basic.cb_fn = cb_fn;
3537 	cpl.u.blob_basic.cb_arg = cb_arg;
3538 
3539 	seq = spdk_bs_sequence_start(blob->bs->md_channel, &cpl);
3540 	if (!seq) {
3541 		cb_fn(cb_arg, -ENOMEM);
3542 		return;
3543 	}
3544 
3545 	_spdk_blob_persist(seq, blob, _spdk_blob_sync_md_cpl, blob);
3546 }
3547 
3548 void
3549 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
3550 {
3551 	_spdk_blob_verify_md_op(blob);
3552 
3553 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blob %lu\n", blob->id);
3554 
3555 	if (blob->md_ro) {
3556 		assert(blob->state == SPDK_BLOB_STATE_CLEAN);
3557 		cb_fn(cb_arg, 0);
3558 		return;
3559 	}
3560 
3561 	_spdk_blob_sync_md(blob, cb_fn, cb_arg);
3562 }
3563 
3564 /* END spdk_blob_sync_md */
3565 
3566 struct spdk_blob_insert_cluster_ctx {
3567 	struct spdk_thread	*thread;
3568 	struct spdk_blob	*blob;
3569 	uint32_t		cluster_num;	/* cluster index in blob */
3570 	uint32_t		cluster;	/* cluster on disk */
3571 	int			rc;
3572 	spdk_blob_op_complete	cb_fn;
3573 	void			*cb_arg;
3574 };
3575 
3576 static void
3577 _spdk_blob_insert_cluster_msg_cpl(void *arg)
3578 {
3579 	struct spdk_blob_insert_cluster_ctx *ctx = arg;
3580 
3581 	ctx->cb_fn(ctx->cb_arg, ctx->rc);
3582 	free(ctx);
3583 }
3584 
3585 static void
3586 _spdk_blob_insert_cluster_msg_cb(void *arg, int bserrno)
3587 {
3588 	struct spdk_blob_insert_cluster_ctx *ctx = arg;
3589 
3590 	ctx->rc = bserrno;
3591 	spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx);
3592 }
3593 
3594 static void
3595 _spdk_blob_insert_cluster_msg(void *arg)
3596 {
3597 	struct spdk_blob_insert_cluster_ctx *ctx = arg;
3598 
3599 	ctx->rc = _spdk_blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster);
3600 	if (ctx->rc != 0) {
3601 		spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx);
3602 		return;
3603 	}
3604 
3605 	ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
3606 	_spdk_blob_sync_md(ctx->blob, _spdk_blob_insert_cluster_msg_cb, ctx);
3607 }
3608 
3609 void
3610 _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
3611 				       uint64_t cluster, spdk_blob_op_complete cb_fn, void *cb_arg)
3612 {
3613 	struct spdk_blob_insert_cluster_ctx *ctx;
3614 
3615 	ctx = calloc(1, sizeof(*ctx));
3616 	if (ctx == NULL) {
3617 		cb_fn(cb_arg, -ENOMEM);
3618 		return;
3619 	}
3620 
3621 	ctx->thread = spdk_get_thread();
3622 	ctx->blob = blob;
3623 	ctx->cluster_num = cluster_num;
3624 	ctx->cluster = cluster;
3625 	ctx->cb_fn = cb_fn;
3626 	ctx->cb_arg = cb_arg;
3627 
3628 	spdk_thread_send_msg(blob->bs->md_thread, _spdk_blob_insert_cluster_msg, ctx);
3629 }
3630 
3631 /* START spdk_blob_close */
3632 
3633 static void
3634 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3635 {
3636 	struct spdk_blob *blob = cb_arg;
3637 
3638 	if (bserrno == 0) {
3639 		blob->open_ref--;
3640 		if (blob->open_ref == 0) {
3641 			/*
3642 			 * Blobs with active.num_pages == 0 are deleted blobs.
3643 			 *  these blobs are removed from the blob_store list
3644 			 *  when the deletion process starts - so don't try to
3645 			 *  remove them again.
3646 			 */
3647 			if (blob->active.num_pages > 0) {
3648 				TAILQ_REMOVE(&blob->bs->blobs, blob, link);
3649 			}
3650 			_spdk_blob_free(blob);
3651 		}
3652 	}
3653 
3654 	spdk_bs_sequence_finish(seq, bserrno);
3655 }
3656 
3657 void spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
3658 {
3659 	struct spdk_bs_cpl	cpl;
3660 	spdk_bs_sequence_t	*seq;
3661 
3662 	_spdk_blob_verify_md_op(blob);
3663 
3664 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Closing blob %lu\n", blob->id);
3665 
3666 	if (blob->open_ref == 0) {
3667 		cb_fn(cb_arg, -EBADF);
3668 		return;
3669 	}
3670 
3671 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
3672 	cpl.u.blob_basic.cb_fn = cb_fn;
3673 	cpl.u.blob_basic.cb_arg = cb_arg;
3674 
3675 	seq = spdk_bs_sequence_start(blob->bs->md_channel, &cpl);
3676 	if (!seq) {
3677 		cb_fn(cb_arg, -ENOMEM);
3678 		return;
3679 	}
3680 
3681 	/* Sync metadata */
3682 	_spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, blob);
3683 }
3684 
3685 /* END spdk_blob_close */
3686 
3687 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
3688 {
3689 	return spdk_get_io_channel(bs);
3690 }
3691 
3692 void spdk_bs_free_io_channel(struct spdk_io_channel *channel)
3693 {
3694 	spdk_put_io_channel(channel);
3695 }
3696 
3697 void spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
3698 			uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
3699 {
3700 	_spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
3701 				     SPDK_BLOB_UNMAP);
3702 }
3703 
3704 void spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
3705 			       uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
3706 {
3707 	_spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
3708 				     SPDK_BLOB_WRITE_ZEROES);
3709 }
3710 
3711 void spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
3712 			void *payload, uint64_t offset, uint64_t length,
3713 			spdk_blob_op_complete cb_fn, void *cb_arg)
3714 {
3715 	_spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
3716 				     SPDK_BLOB_WRITE);
3717 }
3718 
3719 void spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
3720 		       void *payload, uint64_t offset, uint64_t length,
3721 		       spdk_blob_op_complete cb_fn, void *cb_arg)
3722 {
3723 	_spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
3724 				     SPDK_BLOB_READ);
3725 }
3726 
3727 void spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
3728 			 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
3729 			 spdk_blob_op_complete cb_fn, void *cb_arg)
3730 {
3731 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false);
3732 }
3733 
3734 void spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
3735 			struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
3736 			spdk_blob_op_complete cb_fn, void *cb_arg)
3737 {
3738 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true);
3739 }
3740 
3741 void spdk_bs_io_unmap_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
3742 			   uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
3743 {
3744 	spdk_blob_io_unmap(blob, channel, offset, length, cb_fn, cb_arg);
3745 }
3746 
3747 void spdk_bs_io_write_zeroes_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
3748 				  uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
3749 {
3750 	spdk_blob_io_write_zeroes(blob, channel, offset, length, cb_fn, cb_arg);
3751 }
3752 
3753 void spdk_bs_io_write_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
3754 			   void *payload, uint64_t offset, uint64_t length,
3755 			   spdk_blob_op_complete cb_fn, void *cb_arg)
3756 {
3757 	spdk_blob_io_write(blob, channel, payload, offset, length, cb_fn, cb_arg);
3758 }
3759 
3760 void spdk_bs_io_read_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
3761 			  void *payload, uint64_t offset, uint64_t length,
3762 			  spdk_blob_op_complete cb_fn, void *cb_arg)
3763 {
3764 	spdk_blob_io_read(blob, channel, payload, offset, length, cb_fn, cb_arg);
3765 }
3766 
3767 void spdk_bs_io_writev_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
3768 			    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
3769 			    spdk_blob_op_complete cb_fn, void *cb_arg)
3770 {
3771 	spdk_blob_io_writev(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg);
3772 }
3773 
3774 void spdk_bs_io_readv_blob(struct spdk_blob *blob, struct spdk_io_channel *channel,
3775 			   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
3776 			   spdk_blob_op_complete cb_fn, void *cb_arg)
3777 {
3778 	spdk_blob_io_readv(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg);
3779 }
3780 
3781 struct spdk_bs_iter_ctx {
3782 	int64_t page_num;
3783 	struct spdk_blob_store *bs;
3784 
3785 	spdk_blob_op_with_handle_complete cb_fn;
3786 	void *cb_arg;
3787 };
3788 
3789 static void
3790 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
3791 {
3792 	struct spdk_bs_iter_ctx *ctx = cb_arg;
3793 	struct spdk_blob_store *bs = ctx->bs;
3794 	spdk_blob_id id;
3795 
3796 	if (bserrno == 0) {
3797 		ctx->cb_fn(ctx->cb_arg, _blob, bserrno);
3798 		free(ctx);
3799 		return;
3800 	}
3801 
3802 	ctx->page_num++;
3803 	ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num);
3804 	if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) {
3805 		ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
3806 		free(ctx);
3807 		return;
3808 	}
3809 
3810 	id = _spdk_bs_page_to_blobid(ctx->page_num);
3811 
3812 	spdk_bs_open_blob(bs, id, _spdk_bs_iter_cpl, ctx);
3813 }
3814 
3815 void
3816 spdk_bs_iter_first(struct spdk_blob_store *bs,
3817 		   spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
3818 {
3819 	struct spdk_bs_iter_ctx *ctx;
3820 
3821 	ctx = calloc(1, sizeof(*ctx));
3822 	if (!ctx) {
3823 		cb_fn(cb_arg, NULL, -ENOMEM);
3824 		return;
3825 	}
3826 
3827 	ctx->page_num = -1;
3828 	ctx->bs = bs;
3829 	ctx->cb_fn = cb_fn;
3830 	ctx->cb_arg = cb_arg;
3831 
3832 	_spdk_bs_iter_cpl(ctx, NULL, -1);
3833 }
3834 
3835 static void
3836 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno)
3837 {
3838 	struct spdk_bs_iter_ctx *ctx = cb_arg;
3839 
3840 	_spdk_bs_iter_cpl(ctx, NULL, -1);
3841 }
3842 
3843 void
3844 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob,
3845 		  spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
3846 {
3847 	struct spdk_bs_iter_ctx *ctx;
3848 
3849 	assert(blob != NULL);
3850 
3851 	ctx = calloc(1, sizeof(*ctx));
3852 	if (!ctx) {
3853 		cb_fn(cb_arg, NULL, -ENOMEM);
3854 		return;
3855 	}
3856 
3857 	ctx->page_num = _spdk_bs_blobid_to_page(blob->id);
3858 	ctx->bs = bs;
3859 	ctx->cb_fn = cb_fn;
3860 	ctx->cb_arg = cb_arg;
3861 
3862 	/* Close the existing blob */
3863 	spdk_blob_close(blob, _spdk_bs_iter_close_cpl, ctx);
3864 }
3865 
3866 static int
3867 _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
3868 		     uint16_t value_len, bool internal)
3869 {
3870 	struct spdk_xattr_tailq *xattrs;
3871 	struct spdk_xattr 	*xattr;
3872 
3873 	_spdk_blob_verify_md_op(blob);
3874 
3875 	if (blob->md_ro) {
3876 		return -EPERM;
3877 	}
3878 
3879 	if (internal) {
3880 		xattrs = &blob->xattrs_internal;
3881 		blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR;
3882 	} else {
3883 		xattrs = &blob->xattrs;
3884 	}
3885 
3886 	TAILQ_FOREACH(xattr, xattrs, link) {
3887 		if (!strcmp(name, xattr->name)) {
3888 			free(xattr->value);
3889 			xattr->value_len = value_len;
3890 			xattr->value = malloc(value_len);
3891 			memcpy(xattr->value, value, value_len);
3892 
3893 			blob->state = SPDK_BLOB_STATE_DIRTY;
3894 
3895 			return 0;
3896 		}
3897 	}
3898 
3899 	xattr = calloc(1, sizeof(*xattr));
3900 	if (!xattr) {
3901 		return -1;
3902 	}
3903 	xattr->name = strdup(name);
3904 	xattr->value_len = value_len;
3905 	xattr->value = malloc(value_len);
3906 	memcpy(xattr->value, value, value_len);
3907 	TAILQ_INSERT_TAIL(xattrs, xattr, link);
3908 
3909 	blob->state = SPDK_BLOB_STATE_DIRTY;
3910 
3911 	return 0;
3912 }
3913 
3914 int
3915 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
3916 		    uint16_t value_len)
3917 {
3918 	return _spdk_blob_set_xattr(blob, name, value, value_len, false);
3919 }
3920 
3921 static int
3922 _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal)
3923 {
3924 	struct spdk_xattr_tailq *xattrs;
3925 	struct spdk_xattr	*xattr;
3926 
3927 	_spdk_blob_verify_md_op(blob);
3928 
3929 	if (blob->md_ro) {
3930 		return -EPERM;
3931 	}
3932 	xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
3933 
3934 	TAILQ_FOREACH(xattr, xattrs, link) {
3935 		if (!strcmp(name, xattr->name)) {
3936 			TAILQ_REMOVE(xattrs, xattr, link);
3937 			free(xattr->value);
3938 			free(xattr->name);
3939 			free(xattr);
3940 
3941 			if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) {
3942 				blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR;
3943 			}
3944 			blob->state = SPDK_BLOB_STATE_DIRTY;
3945 
3946 			return 0;
3947 		}
3948 	}
3949 
3950 	return -ENOENT;
3951 }
3952 
3953 int
3954 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name)
3955 {
3956 	return _spdk_blob_remove_xattr(blob, name, false);
3957 }
3958 
3959 static int
3960 _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
3961 			   const void **value, size_t *value_len, bool internal)
3962 {
3963 	struct spdk_xattr	*xattr;
3964 	struct spdk_xattr_tailq *xattrs;
3965 
3966 	_spdk_blob_verify_md_op(blob);
3967 
3968 	xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
3969 
3970 	TAILQ_FOREACH(xattr, xattrs, link) {
3971 		if (!strcmp(name, xattr->name)) {
3972 			*value = xattr->value;
3973 			*value_len = xattr->value_len;
3974 			return 0;
3975 		}
3976 	}
3977 	return -ENOENT;
3978 }
3979 
3980 int
3981 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
3982 			  const void **value, size_t *value_len)
3983 {
3984 	return _spdk_blob_get_xattr_value(blob, name, value, value_len, false);
3985 }
3986 
3987 struct spdk_xattr_names {
3988 	uint32_t	count;
3989 	const char	*names[0];
3990 };
3991 
3992 static int
3993 _spdk_blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names)
3994 {
3995 	struct spdk_xattr	*xattr;
3996 	int			count = 0;
3997 
3998 	TAILQ_FOREACH(xattr, xattrs, link) {
3999 		count++;
4000 	}
4001 
4002 	*names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
4003 	if (*names == NULL) {
4004 		return -ENOMEM;
4005 	}
4006 
4007 	TAILQ_FOREACH(xattr, xattrs, link) {
4008 		(*names)->names[(*names)->count++] = xattr->name;
4009 	}
4010 
4011 	return 0;
4012 }
4013 
4014 int
4015 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names)
4016 {
4017 	_spdk_blob_verify_md_op(blob);
4018 
4019 	return _spdk_blob_get_xattr_names(&blob->xattrs, names);
4020 }
4021 
4022 uint32_t
4023 spdk_xattr_names_get_count(struct spdk_xattr_names *names)
4024 {
4025 	assert(names != NULL);
4026 
4027 	return names->count;
4028 }
4029 
4030 const char *
4031 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
4032 {
4033 	if (index >= names->count) {
4034 		return NULL;
4035 	}
4036 
4037 	return names->names[index];
4038 }
4039 
4040 void
4041 spdk_xattr_names_free(struct spdk_xattr_names *names)
4042 {
4043 	free(names);
4044 }
4045 
4046 struct spdk_bs_type
4047 spdk_bs_get_bstype(struct spdk_blob_store *bs)
4048 {
4049 	return bs->bstype;
4050 }
4051 
4052 void
4053 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
4054 {
4055 	memcpy(&bs->bstype, &bstype, sizeof(bstype));
4056 }
4057 
4058 SPDK_LOG_REGISTER_COMPONENT("blob", SPDK_LOG_BLOB)
4059