xref: /spdk/lib/blob/blobstore.c (revision cfb65ba61180ae80899756239e08f4656866beb3)
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/thread.h"
41 #include "spdk/bit_array.h"
42 #include "spdk/likely.h"
43 #include "spdk/util.h"
44 #include "spdk/string.h"
45 
46 #include "spdk_internal/assert.h"
47 #include "spdk_internal/log.h"
48 
49 #include "blobstore.h"
50 
51 #define BLOB_CRC32C_INITIAL    0xffffffffUL
52 
53 static int spdk_bs_register_md_thread(struct spdk_blob_store *bs);
54 static int spdk_bs_unregister_md_thread(struct spdk_blob_store *bs);
55 static void _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
56 static void _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
57 		uint64_t cluster, uint32_t extent, spdk_blob_op_complete cb_fn, void *cb_arg);
58 
59 static int _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
60 				uint16_t value_len, bool internal);
61 static int _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
62 				      const void **value, size_t *value_len, bool internal);
63 static int _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal);
64 
65 static void _spdk_blob_insert_extent(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
66 				     spdk_blob_op_complete cb_fn, void *cb_arg);
67 
68 static void
69 _spdk_blob_verify_md_op(struct spdk_blob *blob)
70 {
71 	assert(blob != NULL);
72 	assert(spdk_get_thread() == blob->bs->md_thread);
73 	assert(blob->state != SPDK_BLOB_STATE_LOADING);
74 }
75 
76 static struct spdk_blob_list *
77 _spdk_bs_get_snapshot_entry(struct spdk_blob_store *bs, spdk_blob_id blobid)
78 {
79 	struct spdk_blob_list *snapshot_entry = NULL;
80 
81 	TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
82 		if (snapshot_entry->id == blobid) {
83 			break;
84 		}
85 	}
86 
87 	return snapshot_entry;
88 }
89 
90 static void
91 _spdk_bs_claim_md_page(struct spdk_blob_store *bs, uint32_t page)
92 {
93 	assert(page < spdk_bit_array_capacity(bs->used_md_pages));
94 	assert(spdk_bit_array_get(bs->used_md_pages, page) == false);
95 
96 	spdk_bit_array_set(bs->used_md_pages, page);
97 }
98 
99 static void
100 _spdk_bs_release_md_page(struct spdk_blob_store *bs, uint32_t page)
101 {
102 	assert(page < spdk_bit_array_capacity(bs->used_md_pages));
103 	assert(spdk_bit_array_get(bs->used_md_pages, page) == true);
104 
105 	spdk_bit_array_clear(bs->used_md_pages, page);
106 }
107 
108 static void
109 _spdk_bs_claim_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
110 {
111 	assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters));
112 	assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == false);
113 	assert(bs->num_free_clusters > 0);
114 
115 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming cluster %u\n", cluster_num);
116 
117 	spdk_bit_array_set(bs->used_clusters, cluster_num);
118 	bs->num_free_clusters--;
119 }
120 
121 static int
122 _spdk_blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster)
123 {
124 	uint64_t *cluster_lba = &blob->active.clusters[cluster_num];
125 
126 	_spdk_blob_verify_md_op(blob);
127 
128 	if (*cluster_lba != 0) {
129 		return -EEXIST;
130 	}
131 
132 	*cluster_lba = _spdk_bs_cluster_to_lba(blob->bs, cluster);
133 	return 0;
134 }
135 
136 static int
137 _spdk_bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num,
138 			  uint64_t *lowest_free_cluster, uint32_t *lowest_free_md_page, bool update_map)
139 {
140 	uint32_t *extent_page = 0;
141 
142 	pthread_mutex_lock(&blob->bs->used_clusters_mutex);
143 	*lowest_free_cluster = spdk_bit_array_find_first_clear(blob->bs->used_clusters,
144 			       *lowest_free_cluster);
145 	if (*lowest_free_cluster == UINT32_MAX) {
146 		/* No more free clusters. Cannot satisfy the request */
147 		pthread_mutex_unlock(&blob->bs->used_clusters_mutex);
148 		return -ENOSPC;
149 	}
150 
151 	if (blob->use_extent_table) {
152 		extent_page = _spdk_bs_cluster_to_extent_page(blob, cluster_num);
153 		if (*extent_page == 0) {
154 			/* No extent_page is allocated for the cluster */
155 			*lowest_free_md_page = spdk_bit_array_find_first_clear(blob->bs->used_md_pages,
156 					       *lowest_free_md_page);
157 			if (*lowest_free_md_page == UINT32_MAX) {
158 				/* No more free md pages. Cannot satisfy the request */
159 				pthread_mutex_unlock(&blob->bs->used_clusters_mutex);
160 				return -ENOSPC;
161 			}
162 			_spdk_bs_claim_md_page(blob->bs, *lowest_free_md_page);
163 		}
164 	}
165 
166 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming cluster %lu for blob %lu\n", *lowest_free_cluster, blob->id);
167 	_spdk_bs_claim_cluster(blob->bs, *lowest_free_cluster);
168 
169 	pthread_mutex_unlock(&blob->bs->used_clusters_mutex);
170 
171 	if (update_map) {
172 		_spdk_blob_insert_cluster(blob, cluster_num, *lowest_free_cluster);
173 		if (blob->use_extent_table && *extent_page == 0) {
174 			*extent_page = *lowest_free_md_page;
175 		}
176 	}
177 
178 	return 0;
179 }
180 
181 static void
182 _spdk_bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
183 {
184 	assert(cluster_num < spdk_bit_array_capacity(bs->used_clusters));
185 	assert(spdk_bit_array_get(bs->used_clusters, cluster_num) == true);
186 	assert(bs->num_free_clusters < bs->total_clusters);
187 
188 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Releasing cluster %u\n", cluster_num);
189 
190 	pthread_mutex_lock(&bs->used_clusters_mutex);
191 	spdk_bit_array_clear(bs->used_clusters, cluster_num);
192 	bs->num_free_clusters++;
193 	pthread_mutex_unlock(&bs->used_clusters_mutex);
194 }
195 
196 static void
197 _spdk_blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs)
198 {
199 	xattrs->count = 0;
200 	xattrs->names = NULL;
201 	xattrs->ctx = NULL;
202 	xattrs->get_value = NULL;
203 }
204 
205 void
206 spdk_blob_opts_init(struct spdk_blob_opts *opts)
207 {
208 	opts->num_clusters = 0;
209 	opts->thin_provision = false;
210 	opts->clear_method = BLOB_CLEAR_WITH_DEFAULT;
211 	_spdk_blob_xattrs_init(&opts->xattrs);
212 	opts->use_extent_table = true;
213 }
214 
215 void
216 spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts)
217 {
218 	opts->clear_method = BLOB_CLEAR_WITH_DEFAULT;
219 }
220 
221 static struct spdk_blob *
222 _spdk_blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
223 {
224 	struct spdk_blob *blob;
225 
226 	blob = calloc(1, sizeof(*blob));
227 	if (!blob) {
228 		return NULL;
229 	}
230 
231 	blob->id = id;
232 	blob->bs = bs;
233 
234 	blob->parent_id = SPDK_BLOBID_INVALID;
235 
236 	blob->state = SPDK_BLOB_STATE_DIRTY;
237 	blob->extent_rle_found = false;
238 	blob->extent_table_found = false;
239 	blob->active.num_pages = 1;
240 	blob->active.pages = calloc(1, sizeof(*blob->active.pages));
241 	if (!blob->active.pages) {
242 		free(blob);
243 		return NULL;
244 	}
245 
246 	blob->active.pages[0] = _spdk_bs_blobid_to_page(id);
247 
248 	TAILQ_INIT(&blob->xattrs);
249 	TAILQ_INIT(&blob->xattrs_internal);
250 	TAILQ_INIT(&blob->pending_persists);
251 
252 	return blob;
253 }
254 
255 static void
256 _spdk_xattrs_free(struct spdk_xattr_tailq *xattrs)
257 {
258 	struct spdk_xattr	*xattr, *xattr_tmp;
259 
260 	TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) {
261 		TAILQ_REMOVE(xattrs, xattr, link);
262 		free(xattr->name);
263 		free(xattr->value);
264 		free(xattr);
265 	}
266 }
267 
268 static void
269 _spdk_blob_free(struct spdk_blob *blob)
270 {
271 	assert(blob != NULL);
272 	assert(TAILQ_EMPTY(&blob->pending_persists));
273 
274 	free(blob->active.extent_pages);
275 	free(blob->clean.extent_pages);
276 	free(blob->active.clusters);
277 	free(blob->clean.clusters);
278 	free(blob->active.pages);
279 	free(blob->clean.pages);
280 
281 	_spdk_xattrs_free(&blob->xattrs);
282 	_spdk_xattrs_free(&blob->xattrs_internal);
283 
284 	if (blob->back_bs_dev) {
285 		blob->back_bs_dev->destroy(blob->back_bs_dev);
286 	}
287 
288 	free(blob);
289 }
290 
291 struct freeze_io_ctx {
292 	struct spdk_bs_cpl cpl;
293 	struct spdk_blob *blob;
294 };
295 
296 static void
297 _spdk_blob_io_sync(struct spdk_io_channel_iter *i)
298 {
299 	spdk_for_each_channel_continue(i, 0);
300 }
301 
302 static void
303 _spdk_blob_execute_queued_io(struct spdk_io_channel_iter *i)
304 {
305 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
306 	struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch);
307 	struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
308 	struct spdk_bs_request_set	*set;
309 	struct spdk_bs_user_op_args	*args;
310 	spdk_bs_user_op_t *op, *tmp;
311 
312 	TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) {
313 		set = (struct spdk_bs_request_set *)op;
314 		args = &set->u.user_op;
315 
316 		if (args->blob == ctx->blob) {
317 			TAILQ_REMOVE(&ch->queued_io, op, link);
318 			bs_user_op_execute(op);
319 		}
320 	}
321 
322 	spdk_for_each_channel_continue(i, 0);
323 }
324 
325 static void
326 _spdk_blob_io_cpl(struct spdk_io_channel_iter *i, int status)
327 {
328 	struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
329 
330 	ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0);
331 
332 	free(ctx);
333 }
334 
335 static void
336 _spdk_blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
337 {
338 	struct freeze_io_ctx *ctx;
339 
340 	ctx = calloc(1, sizeof(*ctx));
341 	if (!ctx) {
342 		cb_fn(cb_arg, -ENOMEM);
343 		return;
344 	}
345 
346 	ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
347 	ctx->cpl.u.blob_basic.cb_fn = cb_fn;
348 	ctx->cpl.u.blob_basic.cb_arg = cb_arg;
349 	ctx->blob = blob;
350 
351 	/* Freeze I/O on blob */
352 	blob->frozen_refcnt++;
353 
354 	if (blob->frozen_refcnt == 1) {
355 		spdk_for_each_channel(blob->bs, _spdk_blob_io_sync, ctx, _spdk_blob_io_cpl);
356 	} else {
357 		cb_fn(cb_arg, 0);
358 		free(ctx);
359 	}
360 }
361 
362 static void
363 _spdk_blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
364 {
365 	struct freeze_io_ctx *ctx;
366 
367 	ctx = calloc(1, sizeof(*ctx));
368 	if (!ctx) {
369 		cb_fn(cb_arg, -ENOMEM);
370 		return;
371 	}
372 
373 	ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
374 	ctx->cpl.u.blob_basic.cb_fn = cb_fn;
375 	ctx->cpl.u.blob_basic.cb_arg = cb_arg;
376 	ctx->blob = blob;
377 
378 	assert(blob->frozen_refcnt > 0);
379 
380 	blob->frozen_refcnt--;
381 
382 	if (blob->frozen_refcnt == 0) {
383 		spdk_for_each_channel(blob->bs, _spdk_blob_execute_queued_io, ctx, _spdk_blob_io_cpl);
384 	} else {
385 		cb_fn(cb_arg, 0);
386 		free(ctx);
387 	}
388 }
389 
390 static int
391 _spdk_blob_mark_clean(struct spdk_blob *blob)
392 {
393 	uint32_t *extent_pages = NULL;
394 	uint64_t *clusters = NULL;
395 	uint32_t *pages = NULL;
396 
397 	assert(blob != NULL);
398 
399 	if (blob->active.num_extent_pages) {
400 		assert(blob->active.extent_pages);
401 		extent_pages = calloc(blob->active.num_extent_pages, sizeof(*blob->active.extent_pages));
402 		if (!extent_pages) {
403 			return -ENOMEM;
404 		}
405 		memcpy(extent_pages, blob->active.extent_pages,
406 		       blob->active.num_extent_pages * sizeof(*extent_pages));
407 	}
408 
409 	if (blob->active.num_clusters) {
410 		assert(blob->active.clusters);
411 		clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
412 		if (!clusters) {
413 			free(extent_pages);
414 			return -ENOMEM;
415 		}
416 		memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
417 	}
418 
419 	if (blob->active.num_pages) {
420 		assert(blob->active.pages);
421 		pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
422 		if (!pages) {
423 			free(extent_pages);
424 			free(clusters);
425 			return -ENOMEM;
426 		}
427 		memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
428 	}
429 
430 	free(blob->clean.extent_pages);
431 	free(blob->clean.clusters);
432 	free(blob->clean.pages);
433 
434 	blob->clean.num_extent_pages = blob->active.num_extent_pages;
435 	blob->clean.extent_pages = blob->active.extent_pages;
436 	blob->clean.num_clusters = blob->active.num_clusters;
437 	blob->clean.clusters = blob->active.clusters;
438 	blob->clean.num_pages = blob->active.num_pages;
439 	blob->clean.pages = blob->active.pages;
440 
441 	blob->active.extent_pages = extent_pages;
442 	blob->active.clusters = clusters;
443 	blob->active.pages = pages;
444 
445 	/* If the metadata was dirtied again while the metadata was being written to disk,
446 	 *  we do not want to revert the DIRTY state back to CLEAN here.
447 	 */
448 	if (blob->state == SPDK_BLOB_STATE_LOADING) {
449 		blob->state = SPDK_BLOB_STATE_CLEAN;
450 	}
451 
452 	return 0;
453 }
454 
455 static int
456 _spdk_blob_deserialize_xattr(struct spdk_blob *blob,
457 			     struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal)
458 {
459 	struct spdk_xattr                       *xattr;
460 
461 	if (desc_xattr->length != sizeof(desc_xattr->name_length) +
462 	    sizeof(desc_xattr->value_length) +
463 	    desc_xattr->name_length + desc_xattr->value_length) {
464 		return -EINVAL;
465 	}
466 
467 	xattr = calloc(1, sizeof(*xattr));
468 	if (xattr == NULL) {
469 		return -ENOMEM;
470 	}
471 
472 	xattr->name = malloc(desc_xattr->name_length + 1);
473 	if (xattr->name == NULL) {
474 		free(xattr);
475 		return -ENOMEM;
476 	}
477 	memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
478 	xattr->name[desc_xattr->name_length] = '\0';
479 
480 	xattr->value = malloc(desc_xattr->value_length);
481 	if (xattr->value == NULL) {
482 		free(xattr->name);
483 		free(xattr);
484 		return -ENOMEM;
485 	}
486 	xattr->value_len = desc_xattr->value_length;
487 	memcpy(xattr->value,
488 	       (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
489 	       desc_xattr->value_length);
490 
491 	TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link);
492 
493 	return 0;
494 }
495 
496 
497 static int
498 _spdk_blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
499 {
500 	struct spdk_blob_md_descriptor *desc;
501 	size_t	cur_desc = 0;
502 	void *tmp;
503 
504 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
505 	while (cur_desc < sizeof(page->descriptors)) {
506 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
507 			if (desc->length == 0) {
508 				/* If padding and length are 0, this terminates the page */
509 				break;
510 			}
511 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
512 			struct spdk_blob_md_descriptor_flags	*desc_flags;
513 
514 			desc_flags = (struct spdk_blob_md_descriptor_flags *)desc;
515 
516 			if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) {
517 				return -EINVAL;
518 			}
519 
520 			if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) !=
521 			    SPDK_BLOB_INVALID_FLAGS_MASK) {
522 				return -EINVAL;
523 			}
524 
525 			if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) !=
526 			    SPDK_BLOB_DATA_RO_FLAGS_MASK) {
527 				blob->data_ro = true;
528 				blob->md_ro = true;
529 			}
530 
531 			if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) !=
532 			    SPDK_BLOB_MD_RO_FLAGS_MASK) {
533 				blob->md_ro = true;
534 			}
535 
536 			if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
537 				blob->data_ro = true;
538 				blob->md_ro = true;
539 			}
540 
541 			blob->invalid_flags = desc_flags->invalid_flags;
542 			blob->data_ro_flags = desc_flags->data_ro_flags;
543 			blob->md_ro_flags = desc_flags->md_ro_flags;
544 
545 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
546 			struct spdk_blob_md_descriptor_extent_rle	*desc_extent_rle;
547 			unsigned int				i, j;
548 			unsigned int				cluster_count = blob->active.num_clusters;
549 
550 			if (blob->extent_table_found) {
551 				/* Extent Table already present in the md,
552 				 * both descriptors should never be at the same time. */
553 				return -EINVAL;
554 			}
555 			blob->extent_rle_found = true;
556 
557 			desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
558 
559 			if (desc_extent_rle->length == 0 ||
560 			    (desc_extent_rle->length % sizeof(desc_extent_rle->extents[0]) != 0)) {
561 				return -EINVAL;
562 			}
563 
564 			for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
565 				for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
566 					if (desc_extent_rle->extents[i].cluster_idx != 0) {
567 						if (!spdk_bit_array_get(blob->bs->used_clusters,
568 									desc_extent_rle->extents[i].cluster_idx + j)) {
569 							return -EINVAL;
570 						}
571 					}
572 					cluster_count++;
573 				}
574 			}
575 
576 			if (cluster_count == 0) {
577 				return -EINVAL;
578 			}
579 			tmp = realloc(blob->active.clusters, cluster_count * sizeof(*blob->active.clusters));
580 			if (tmp == NULL) {
581 				return -ENOMEM;
582 			}
583 			blob->active.clusters = tmp;
584 			blob->active.cluster_array_size = cluster_count;
585 
586 			for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
587 				for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
588 					if (desc_extent_rle->extents[i].cluster_idx != 0) {
589 						blob->active.clusters[blob->active.num_clusters++] = _spdk_bs_cluster_to_lba(blob->bs,
590 								desc_extent_rle->extents[i].cluster_idx + j);
591 					} else if (spdk_blob_is_thin_provisioned(blob)) {
592 						blob->active.clusters[blob->active.num_clusters++] = 0;
593 					} else {
594 						return -EINVAL;
595 					}
596 				}
597 			}
598 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
599 			struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
600 			uint32_t num_extent_pages = blob->active.num_extent_pages;
601 			uint32_t i, j;
602 			size_t extent_pages_length;
603 
604 			desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
605 			extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
606 
607 			if (blob->extent_rle_found) {
608 				/* This means that Extent RLE is present in MD,
609 				 * both should never be at the same time. */
610 				return -EINVAL;
611 			} else if (blob->extent_table_found &&
612 				   desc_extent_table->num_clusters != blob->remaining_clusters_in_et) {
613 				/* Number of clusters in this ET does not match number
614 				 * from previously read EXTENT_TABLE. */
615 				return -EINVAL;
616 			}
617 
618 			blob->extent_table_found = true;
619 
620 			if (desc_extent_table->length == 0 ||
621 			    (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
622 				return -EINVAL;
623 			}
624 
625 			for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
626 				num_extent_pages += desc_extent_table->extent_page[i].num_pages;
627 			}
628 
629 			tmp = realloc(blob->active.extent_pages, num_extent_pages * sizeof(uint32_t));
630 			if (tmp == NULL) {
631 				return -ENOMEM;
632 			}
633 			blob->active.extent_pages = tmp;
634 			blob->active.extent_pages_array_size = num_extent_pages;
635 
636 			blob->remaining_clusters_in_et = desc_extent_table->num_clusters;
637 
638 			/* Extent table entries contain md page numbers for extent pages.
639 			 * Zeroes represent unallocated extent pages, those are run-length-encoded.
640 			 */
641 			for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
642 				if (desc_extent_table->extent_page[i].page_idx != 0) {
643 					assert(desc_extent_table->extent_page[i].num_pages == 1);
644 					blob->active.extent_pages[blob->active.num_extent_pages++] =
645 						desc_extent_table->extent_page[i].page_idx;
646 				} else if (spdk_blob_is_thin_provisioned(blob)) {
647 					for (j = 0; j < desc_extent_table->extent_page[i].num_pages; j++) {
648 						blob->active.extent_pages[blob->active.num_extent_pages++] = 0;
649 					}
650 				} else {
651 					return -EINVAL;
652 				}
653 			}
654 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
655 			struct spdk_blob_md_descriptor_extent_page	*desc_extent;
656 			unsigned int					i;
657 			unsigned int					cluster_count = 0;
658 			size_t						cluster_idx_length;
659 
660 			if (blob->extent_rle_found) {
661 				/* This means that Extent RLE is present in MD,
662 				 * both should never be at the same time. */
663 				return -EINVAL;
664 			}
665 
666 			desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
667 			cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
668 
669 			if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
670 			    (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
671 				return -EINVAL;
672 			}
673 
674 			for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
675 				if (desc_extent->cluster_idx[i] != 0) {
676 					if (!spdk_bit_array_get(blob->bs->used_clusters, desc_extent->cluster_idx[i])) {
677 						return -EINVAL;
678 					}
679 				}
680 				cluster_count++;
681 			}
682 
683 			if (cluster_count == 0) {
684 				return -EINVAL;
685 			}
686 
687 			/* When reading extent pages sequentially starting cluster idx should match
688 			 * current size of a blob.
689 			 * If changed to batch reading, this check shall be removed. */
690 			if (desc_extent->start_cluster_idx != blob->active.num_clusters) {
691 				return -EINVAL;
692 			}
693 
694 			tmp = realloc(blob->active.clusters,
695 				      (cluster_count + blob->active.num_clusters) * sizeof(*blob->active.clusters));
696 			if (tmp == NULL) {
697 				return -ENOMEM;
698 			}
699 			blob->active.clusters = tmp;
700 			blob->active.cluster_array_size = (cluster_count + blob->active.num_clusters);
701 
702 			for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
703 				if (desc_extent->cluster_idx[i] != 0) {
704 					blob->active.clusters[blob->active.num_clusters++] = _spdk_bs_cluster_to_lba(blob->bs,
705 							desc_extent->cluster_idx[i]);
706 				} else if (spdk_blob_is_thin_provisioned(blob)) {
707 					blob->active.clusters[blob->active.num_clusters++] = 0;
708 				} else {
709 					return -EINVAL;
710 				}
711 			}
712 			assert(desc_extent->start_cluster_idx + cluster_count == blob->active.num_clusters);
713 			assert(blob->remaining_clusters_in_et >= cluster_count);
714 			blob->remaining_clusters_in_et -= cluster_count;
715 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
716 			int rc;
717 
718 			rc = _spdk_blob_deserialize_xattr(blob,
719 							  (struct spdk_blob_md_descriptor_xattr *) desc, false);
720 			if (rc != 0) {
721 				return rc;
722 			}
723 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
724 			int rc;
725 
726 			rc = _spdk_blob_deserialize_xattr(blob,
727 							  (struct spdk_blob_md_descriptor_xattr *) desc, true);
728 			if (rc != 0) {
729 				return rc;
730 			}
731 		} else {
732 			/* Unrecognized descriptor type.  Do not fail - just continue to the
733 			 *  next descriptor.  If this descriptor is associated with some feature
734 			 *  defined in a newer version of blobstore, that version of blobstore
735 			 *  should create and set an associated feature flag to specify if this
736 			 *  blob can be loaded or not.
737 			 */
738 		}
739 
740 		/* Advance to the next descriptor */
741 		cur_desc += sizeof(*desc) + desc->length;
742 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
743 			break;
744 		}
745 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
746 	}
747 
748 	return 0;
749 }
750 
751 static bool _spdk_bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page);
752 
753 static int
754 _spdk_blob_parse_extent_page(struct spdk_blob_md_page *extent_page, struct spdk_blob *blob)
755 {
756 	assert(blob != NULL);
757 	assert(blob->state == SPDK_BLOB_STATE_LOADING);
758 
759 	if (_spdk_bs_load_cur_extent_page_valid(extent_page) == false) {
760 		return -ENOENT;
761 	}
762 
763 	return _spdk_blob_parse_page(extent_page, blob);
764 }
765 
766 static int
767 _spdk_blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
768 		 struct spdk_blob *blob)
769 {
770 	const struct spdk_blob_md_page *page;
771 	uint32_t i;
772 	int rc;
773 
774 	assert(page_count > 0);
775 	assert(pages[0].sequence_num == 0);
776 	assert(blob != NULL);
777 	assert(blob->state == SPDK_BLOB_STATE_LOADING);
778 	assert(blob->active.clusters == NULL);
779 
780 	/* The blobid provided doesn't match what's in the MD, this can
781 	 * happen for example if a bogus blobid is passed in through open.
782 	 */
783 	if (blob->id != pages[0].id) {
784 		SPDK_ERRLOG("Blobid (%lu) doesn't match what's in metadata (%lu)\n",
785 			    blob->id, pages[0].id);
786 		return -ENOENT;
787 	}
788 
789 	for (i = 0; i < page_count; i++) {
790 		page = &pages[i];
791 
792 		assert(page->id == blob->id);
793 		assert(page->sequence_num == i);
794 
795 		rc = _spdk_blob_parse_page(page, blob);
796 		if (rc != 0) {
797 			return rc;
798 		}
799 	}
800 
801 	return 0;
802 }
803 
804 static int
805 _spdk_blob_serialize_add_page(const struct spdk_blob *blob,
806 			      struct spdk_blob_md_page **pages,
807 			      uint32_t *page_count,
808 			      struct spdk_blob_md_page **last_page)
809 {
810 	struct spdk_blob_md_page *page;
811 
812 	assert(pages != NULL);
813 	assert(page_count != NULL);
814 
815 	if (*page_count == 0) {
816 		assert(*pages == NULL);
817 		*page_count = 1;
818 		*pages = spdk_malloc(SPDK_BS_PAGE_SIZE, SPDK_BS_PAGE_SIZE,
819 				     NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
820 	} else {
821 		assert(*pages != NULL);
822 		(*page_count)++;
823 		*pages = spdk_realloc(*pages,
824 				      SPDK_BS_PAGE_SIZE * (*page_count),
825 				      SPDK_BS_PAGE_SIZE);
826 	}
827 
828 	if (*pages == NULL) {
829 		*page_count = 0;
830 		*last_page = NULL;
831 		return -ENOMEM;
832 	}
833 
834 	page = &(*pages)[*page_count - 1];
835 	memset(page, 0, sizeof(*page));
836 	page->id = blob->id;
837 	page->sequence_num = *page_count - 1;
838 	page->next = SPDK_INVALID_MD_PAGE;
839 	*last_page = page;
840 
841 	return 0;
842 }
843 
844 /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
845  * Update required_sz on both success and failure.
846  *
847  */
848 static int
849 _spdk_blob_serialize_xattr(const struct spdk_xattr *xattr,
850 			   uint8_t *buf, size_t buf_sz,
851 			   size_t *required_sz, bool internal)
852 {
853 	struct spdk_blob_md_descriptor_xattr	*desc;
854 
855 	*required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
856 		       strlen(xattr->name) +
857 		       xattr->value_len;
858 
859 	if (buf_sz < *required_sz) {
860 		return -1;
861 	}
862 
863 	desc = (struct spdk_blob_md_descriptor_xattr *)buf;
864 
865 	desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR;
866 	desc->length = sizeof(desc->name_length) +
867 		       sizeof(desc->value_length) +
868 		       strlen(xattr->name) +
869 		       xattr->value_len;
870 	desc->name_length = strlen(xattr->name);
871 	desc->value_length = xattr->value_len;
872 
873 	memcpy(desc->name, xattr->name, desc->name_length);
874 	memcpy((void *)((uintptr_t)desc->name + desc->name_length),
875 	       xattr->value,
876 	       desc->value_length);
877 
878 	return 0;
879 }
880 
881 static void
882 _spdk_blob_serialize_extent_table_entry(const struct spdk_blob *blob,
883 					uint64_t start_ep, uint64_t *next_ep,
884 					uint8_t **buf, size_t *remaining_sz)
885 {
886 	struct spdk_blob_md_descriptor_extent_table *desc;
887 	size_t cur_sz;
888 	uint64_t i, et_idx;
889 	uint32_t extent_page, ep_len;
890 
891 	/* The buffer must have room for at least num_clusters entry */
892 	cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->num_clusters);
893 	if (*remaining_sz < cur_sz) {
894 		*next_ep = start_ep;
895 		return;
896 	}
897 
898 	desc = (struct spdk_blob_md_descriptor_extent_table *)*buf;
899 	desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE;
900 
901 	desc->num_clusters = blob->active.num_clusters;
902 
903 	ep_len = 1;
904 	et_idx = 0;
905 	for (i = start_ep; i < blob->active.num_extent_pages; i++) {
906 		if (*remaining_sz < cur_sz  + sizeof(desc->extent_page[0])) {
907 			/* If we ran out of buffer space, return */
908 			break;
909 		}
910 
911 		extent_page = blob->active.extent_pages[i];
912 		/* Verify that next extent_page is unallocated */
913 		if (extent_page == 0 &&
914 		    (i + 1 < blob->active.num_extent_pages && blob->active.extent_pages[i + 1] == 0)) {
915 			ep_len++;
916 			continue;
917 		}
918 		desc->extent_page[et_idx].page_idx = extent_page;
919 		desc->extent_page[et_idx].num_pages = ep_len;
920 		et_idx++;
921 
922 		ep_len = 1;
923 		cur_sz += sizeof(desc->extent_page[et_idx]);
924 	}
925 	*next_ep = i;
926 
927 	desc->length = sizeof(desc->num_clusters) + sizeof(desc->extent_page[0]) * et_idx;
928 	*remaining_sz -= sizeof(struct spdk_blob_md_descriptor) + desc->length;
929 	*buf += sizeof(struct spdk_blob_md_descriptor) + desc->length;
930 }
931 
932 static int
933 _spdk_blob_serialize_extent_table(const struct spdk_blob *blob,
934 				  struct spdk_blob_md_page **pages,
935 				  struct spdk_blob_md_page *cur_page,
936 				  uint32_t *page_count, uint8_t **buf,
937 				  size_t *remaining_sz)
938 {
939 	uint64_t				last_extent_page;
940 	int					rc;
941 
942 	last_extent_page = 0;
943 	/* At least single extent table entry has to be always persisted.
944 	 * Such case occurs with num_extent_pages == 0. */
945 	while (last_extent_page <= blob->active.num_extent_pages) {
946 		_spdk_blob_serialize_extent_table_entry(blob, last_extent_page, &last_extent_page, buf,
947 							remaining_sz);
948 
949 		if (last_extent_page == blob->active.num_extent_pages) {
950 			break;
951 		}
952 
953 		rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page);
954 		if (rc < 0) {
955 			return rc;
956 		}
957 
958 		*buf = (uint8_t *)cur_page->descriptors;
959 		*remaining_sz = sizeof(cur_page->descriptors);
960 	}
961 
962 	return 0;
963 }
964 
965 static void
966 _spdk_blob_serialize_extent_rle(const struct spdk_blob *blob,
967 				uint64_t start_cluster, uint64_t *next_cluster,
968 				uint8_t **buf, size_t *buf_sz)
969 {
970 	struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
971 	size_t cur_sz;
972 	uint64_t i, extent_idx;
973 	uint64_t lba, lba_per_cluster, lba_count;
974 
975 	/* The buffer must have room for at least one extent */
976 	cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc_extent_rle->extents[0]);
977 	if (*buf_sz < cur_sz) {
978 		*next_cluster = start_cluster;
979 		return;
980 	}
981 
982 	desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)*buf;
983 	desc_extent_rle->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE;
984 
985 	lba_per_cluster = _spdk_bs_cluster_to_lba(blob->bs, 1);
986 
987 	lba = blob->active.clusters[start_cluster];
988 	lba_count = lba_per_cluster;
989 	extent_idx = 0;
990 	for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
991 		if ((lba + lba_count) == blob->active.clusters[i] && lba != 0) {
992 			/* Run-length encode sequential non-zero LBA */
993 			lba_count += lba_per_cluster;
994 			continue;
995 		} else if (lba == 0 && blob->active.clusters[i] == 0) {
996 			/* Run-length encode unallocated clusters */
997 			lba_count += lba_per_cluster;
998 			continue;
999 		}
1000 		desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
1001 		desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
1002 		extent_idx++;
1003 
1004 		cur_sz += sizeof(desc_extent_rle->extents[extent_idx]);
1005 
1006 		if (*buf_sz < cur_sz) {
1007 			/* If we ran out of buffer space, return */
1008 			*next_cluster = i;
1009 			break;
1010 		}
1011 
1012 		lba = blob->active.clusters[i];
1013 		lba_count = lba_per_cluster;
1014 	}
1015 
1016 	if (*buf_sz >= cur_sz) {
1017 		desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
1018 		desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
1019 		extent_idx++;
1020 
1021 		*next_cluster = blob->active.num_clusters;
1022 	}
1023 
1024 	desc_extent_rle->length = sizeof(desc_extent_rle->extents[0]) * extent_idx;
1025 	*buf_sz -= sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
1026 	*buf += sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
1027 }
1028 
1029 static int
1030 _spdk_blob_serialize_extents_rle(const struct spdk_blob *blob,
1031 				 struct spdk_blob_md_page **pages,
1032 				 struct spdk_blob_md_page *cur_page,
1033 				 uint32_t *page_count, uint8_t **buf,
1034 				 size_t *remaining_sz)
1035 {
1036 	uint64_t				last_cluster;
1037 	int					rc;
1038 
1039 	last_cluster = 0;
1040 	while (last_cluster < blob->active.num_clusters) {
1041 		_spdk_blob_serialize_extent_rle(blob, last_cluster, &last_cluster, buf, remaining_sz);
1042 
1043 		if (last_cluster == blob->active.num_clusters) {
1044 			break;
1045 		}
1046 
1047 		rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page);
1048 		if (rc < 0) {
1049 			return rc;
1050 		}
1051 
1052 		*buf = (uint8_t *)cur_page->descriptors;
1053 		*remaining_sz = sizeof(cur_page->descriptors);
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 static void
1060 _spdk_blob_serialize_extent_page(const struct spdk_blob *blob,
1061 				 uint64_t cluster, struct spdk_blob_md_page *page)
1062 {
1063 	struct spdk_blob_md_descriptor_extent_page *desc_extent;
1064 	uint64_t i, extent_idx;
1065 	uint64_t lba, lba_per_cluster;
1066 	uint64_t start_cluster_idx = (cluster / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
1067 
1068 	desc_extent = (struct spdk_blob_md_descriptor_extent_page *) page->descriptors;
1069 	desc_extent->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE;
1070 
1071 	lba_per_cluster = _spdk_bs_cluster_to_lba(blob->bs, 1);
1072 
1073 	desc_extent->start_cluster_idx = start_cluster_idx;
1074 	extent_idx = 0;
1075 	for (i = start_cluster_idx; i < blob->active.num_clusters; i++) {
1076 		lba = blob->active.clusters[i];
1077 		desc_extent->cluster_idx[extent_idx++] = lba / lba_per_cluster;
1078 		if (extent_idx >= SPDK_EXTENTS_PER_EP) {
1079 			break;
1080 		}
1081 	}
1082 	desc_extent->length = sizeof(desc_extent->start_cluster_idx) +
1083 			      sizeof(desc_extent->cluster_idx[0]) * extent_idx;
1084 }
1085 
1086 static void
1087 _spdk_blob_serialize_flags(const struct spdk_blob *blob,
1088 			   uint8_t *buf, size_t *buf_sz)
1089 {
1090 	struct spdk_blob_md_descriptor_flags *desc;
1091 
1092 	/*
1093 	 * Flags get serialized first, so we should always have room for the flags
1094 	 *  descriptor.
1095 	 */
1096 	assert(*buf_sz >= sizeof(*desc));
1097 
1098 	desc = (struct spdk_blob_md_descriptor_flags *)buf;
1099 	desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS;
1100 	desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor);
1101 	desc->invalid_flags = blob->invalid_flags;
1102 	desc->data_ro_flags = blob->data_ro_flags;
1103 	desc->md_ro_flags = blob->md_ro_flags;
1104 
1105 	*buf_sz -= sizeof(*desc);
1106 }
1107 
1108 static int
1109 _spdk_blob_serialize_xattrs(const struct spdk_blob *blob,
1110 			    const struct spdk_xattr_tailq *xattrs, bool internal,
1111 			    struct spdk_blob_md_page **pages,
1112 			    struct spdk_blob_md_page *cur_page,
1113 			    uint32_t *page_count, uint8_t **buf,
1114 			    size_t *remaining_sz)
1115 {
1116 	const struct spdk_xattr	*xattr;
1117 	int	rc;
1118 
1119 	TAILQ_FOREACH(xattr, xattrs, link) {
1120 		size_t required_sz = 0;
1121 
1122 		rc = _spdk_blob_serialize_xattr(xattr,
1123 						*buf, *remaining_sz,
1124 						&required_sz, internal);
1125 		if (rc < 0) {
1126 			/* Need to add a new page to the chain */
1127 			rc = _spdk_blob_serialize_add_page(blob, pages, page_count,
1128 							   &cur_page);
1129 			if (rc < 0) {
1130 				spdk_free(*pages);
1131 				*pages = NULL;
1132 				*page_count = 0;
1133 				return rc;
1134 			}
1135 
1136 			*buf = (uint8_t *)cur_page->descriptors;
1137 			*remaining_sz = sizeof(cur_page->descriptors);
1138 
1139 			/* Try again */
1140 			required_sz = 0;
1141 			rc = _spdk_blob_serialize_xattr(xattr,
1142 							*buf, *remaining_sz,
1143 							&required_sz, internal);
1144 
1145 			if (rc < 0) {
1146 				spdk_free(*pages);
1147 				*pages = NULL;
1148 				*page_count = 0;
1149 				return rc;
1150 			}
1151 		}
1152 
1153 		*remaining_sz -= required_sz;
1154 		*buf += required_sz;
1155 	}
1156 
1157 	return 0;
1158 }
1159 
1160 static int
1161 _spdk_blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
1162 		     uint32_t *page_count)
1163 {
1164 	struct spdk_blob_md_page		*cur_page;
1165 	int					rc;
1166 	uint8_t					*buf;
1167 	size_t					remaining_sz;
1168 
1169 	assert(pages != NULL);
1170 	assert(page_count != NULL);
1171 	assert(blob != NULL);
1172 	assert(blob->state == SPDK_BLOB_STATE_DIRTY);
1173 
1174 	*pages = NULL;
1175 	*page_count = 0;
1176 
1177 	/* A blob always has at least 1 page, even if it has no descriptors */
1178 	rc = _spdk_blob_serialize_add_page(blob, pages, page_count, &cur_page);
1179 	if (rc < 0) {
1180 		return rc;
1181 	}
1182 
1183 	buf = (uint8_t *)cur_page->descriptors;
1184 	remaining_sz = sizeof(cur_page->descriptors);
1185 
1186 	/* Serialize flags */
1187 	_spdk_blob_serialize_flags(blob, buf, &remaining_sz);
1188 	buf += sizeof(struct spdk_blob_md_descriptor_flags);
1189 
1190 	/* Serialize xattrs */
1191 	rc = _spdk_blob_serialize_xattrs(blob, &blob->xattrs, false,
1192 					 pages, cur_page, page_count, &buf, &remaining_sz);
1193 	if (rc < 0) {
1194 		return rc;
1195 	}
1196 
1197 	/* Serialize internal xattrs */
1198 	rc = _spdk_blob_serialize_xattrs(blob, &blob->xattrs_internal, true,
1199 					 pages, cur_page, page_count, &buf, &remaining_sz);
1200 	if (rc < 0) {
1201 		return rc;
1202 	}
1203 
1204 	if (blob->use_extent_table) {
1205 		/* Serialize extent table */
1206 		rc = _spdk_blob_serialize_extent_table(blob, pages, cur_page, page_count, &buf, &remaining_sz);
1207 	} else {
1208 		/* Serialize extents */
1209 		rc = _spdk_blob_serialize_extents_rle(blob, pages, cur_page, page_count, &buf, &remaining_sz);
1210 	}
1211 
1212 	return rc;
1213 }
1214 
1215 struct spdk_blob_load_ctx {
1216 	struct spdk_blob		*blob;
1217 
1218 	struct spdk_blob_md_page	*pages;
1219 	uint32_t			num_pages;
1220 	uint32_t			next_extent_page;
1221 	spdk_bs_sequence_t	        *seq;
1222 
1223 	spdk_bs_sequence_cpl		cb_fn;
1224 	void				*cb_arg;
1225 };
1226 
1227 static uint32_t
1228 _spdk_blob_md_page_calc_crc(void *page)
1229 {
1230 	uint32_t		crc;
1231 
1232 	crc = BLOB_CRC32C_INITIAL;
1233 	crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
1234 	crc ^= BLOB_CRC32C_INITIAL;
1235 
1236 	return crc;
1237 
1238 }
1239 
1240 static void
1241 _spdk_blob_load_final(void *cb_arg, int bserrno)
1242 {
1243 	struct spdk_blob_load_ctx	*ctx = cb_arg;
1244 	struct spdk_blob		*blob = ctx->blob;
1245 
1246 	if (bserrno == 0) {
1247 		_spdk_blob_mark_clean(blob);
1248 	}
1249 
1250 	ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno);
1251 
1252 	/* Free the memory */
1253 	spdk_free(ctx->pages);
1254 	free(ctx);
1255 }
1256 
1257 static void
1258 _spdk_blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
1259 {
1260 	struct spdk_blob_load_ctx	*ctx = cb_arg;
1261 	struct spdk_blob		*blob = ctx->blob;
1262 
1263 	if (bserrno == 0) {
1264 		blob->back_bs_dev = bs_create_blob_bs_dev(snapshot);
1265 		if (blob->back_bs_dev == NULL) {
1266 			bserrno = -ENOMEM;
1267 		}
1268 	}
1269 	if (bserrno != 0) {
1270 		SPDK_ERRLOG("Snapshot fail\n");
1271 	}
1272 
1273 	_spdk_blob_load_final(ctx, bserrno);
1274 }
1275 
1276 static void _spdk_blob_update_clear_method(struct spdk_blob *blob);
1277 
1278 static void
1279 _spdk_blob_load_backing_dev(void *cb_arg)
1280 {
1281 	struct spdk_blob_load_ctx	*ctx = cb_arg;
1282 	struct spdk_blob		*blob = ctx->blob;
1283 	const void			*value;
1284 	size_t				len;
1285 	int				rc;
1286 
1287 	if (spdk_blob_is_thin_provisioned(blob)) {
1288 		rc = _spdk_blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true);
1289 		if (rc == 0) {
1290 			if (len != sizeof(spdk_blob_id)) {
1291 				_spdk_blob_load_final(ctx, -EINVAL);
1292 				return;
1293 			}
1294 			/* open snapshot blob and continue in the callback function */
1295 			blob->parent_id = *(spdk_blob_id *)value;
1296 			spdk_bs_open_blob(blob->bs, blob->parent_id,
1297 					  _spdk_blob_load_snapshot_cpl, ctx);
1298 			return;
1299 		} else {
1300 			/* add zeroes_dev for thin provisioned blob */
1301 			blob->back_bs_dev = bs_create_zeroes_dev();
1302 		}
1303 	} else {
1304 		/* standard blob */
1305 		blob->back_bs_dev = NULL;
1306 	}
1307 	_spdk_blob_load_final(ctx, 0);
1308 }
1309 
1310 static void
1311 _spdk_blob_load_cpl_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1312 {
1313 	struct spdk_blob_load_ctx	*ctx = cb_arg;
1314 	struct spdk_blob		*blob = ctx->blob;
1315 	struct spdk_blob_md_page	*page;
1316 	uint64_t			i;
1317 	uint32_t			crc;
1318 	uint64_t			lba;
1319 	void				*tmp;
1320 	uint64_t			sz;
1321 
1322 	if (bserrno) {
1323 		SPDK_ERRLOG("Extent page read failed: %d\n", bserrno);
1324 		_spdk_blob_load_final(ctx, bserrno);
1325 		return;
1326 	}
1327 
1328 	if (ctx->pages == NULL) {
1329 		/* First iteration of this function, allocate buffer for single EXTENT_PAGE */
1330 		ctx->pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE, SPDK_BS_PAGE_SIZE, NULL, SPDK_ENV_SOCKET_ID_ANY,
1331 					  SPDK_MALLOC_DMA);
1332 		if (!ctx->pages) {
1333 			_spdk_blob_load_final(ctx, -ENOMEM);
1334 			return;
1335 		}
1336 		ctx->num_pages = 1;
1337 		ctx->next_extent_page = 0;
1338 	} else {
1339 		page = &ctx->pages[0];
1340 		crc = _spdk_blob_md_page_calc_crc(page);
1341 		if (crc != page->crc) {
1342 			_spdk_blob_load_final(ctx, -EINVAL);
1343 			return;
1344 		}
1345 
1346 		if (page->next != SPDK_INVALID_MD_PAGE) {
1347 			_spdk_blob_load_final(ctx, -EINVAL);
1348 			return;
1349 		}
1350 
1351 		bserrno = _spdk_blob_parse_extent_page(page, blob);
1352 		if (bserrno) {
1353 			_spdk_blob_load_final(ctx, bserrno);
1354 			return;
1355 		}
1356 	}
1357 
1358 	for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) {
1359 		if (blob->active.extent_pages[i] != 0) {
1360 			/* Extent page was allocated, read and parse it. */
1361 			lba = _spdk_bs_md_page_to_lba(blob->bs, blob->active.extent_pages[i]);
1362 			ctx->next_extent_page = i + 1;
1363 
1364 			bs_sequence_read_dev(seq, &ctx->pages[0], lba,
1365 					     _spdk_bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
1366 					     _spdk_blob_load_cpl_extents_cpl, ctx);
1367 			return;
1368 		} else {
1369 			/* Thin provisioned blobs can point to unallocated extent pages.
1370 			 * In this case blob size should be increased by up to the amount left in remaining_clusters_in_et. */
1371 
1372 			sz = spdk_min(blob->remaining_clusters_in_et, SPDK_EXTENTS_PER_EP);
1373 			blob->active.num_clusters += sz;
1374 			blob->remaining_clusters_in_et -= sz;
1375 
1376 			assert(spdk_blob_is_thin_provisioned(blob));
1377 			assert(i + 1 < blob->active.num_extent_pages || blob->remaining_clusters_in_et == 0);
1378 
1379 			tmp = realloc(blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
1380 			if (tmp == NULL) {
1381 				_spdk_blob_load_final(ctx, -ENOMEM);
1382 				return;
1383 			}
1384 			memset(tmp + blob->active.cluster_array_size, 0,
1385 			       sizeof(*blob->active.clusters) * (blob->active.num_clusters - blob->active.cluster_array_size));
1386 			blob->active.clusters = tmp;
1387 			blob->active.cluster_array_size = blob->active.num_clusters;
1388 		}
1389 	}
1390 
1391 	_spdk_blob_load_backing_dev(ctx);
1392 }
1393 
1394 static void
1395 _spdk_blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1396 {
1397 	struct spdk_blob_load_ctx	*ctx = cb_arg;
1398 	struct spdk_blob		*blob = ctx->blob;
1399 	struct spdk_blob_md_page	*page;
1400 	int				rc;
1401 	uint32_t			crc;
1402 
1403 	if (bserrno) {
1404 		SPDK_ERRLOG("Metadata page read failed: %d\n", bserrno);
1405 		_spdk_blob_load_final(ctx, bserrno);
1406 		return;
1407 	}
1408 
1409 	page = &ctx->pages[ctx->num_pages - 1];
1410 	crc = _spdk_blob_md_page_calc_crc(page);
1411 	if (crc != page->crc) {
1412 		SPDK_ERRLOG("Metadata page %d crc mismatch\n", ctx->num_pages);
1413 		_spdk_blob_load_final(ctx, -EINVAL);
1414 		return;
1415 	}
1416 
1417 	if (page->next != SPDK_INVALID_MD_PAGE) {
1418 		uint32_t next_page = page->next;
1419 		uint64_t next_lba = _spdk_bs_md_page_to_lba(blob->bs, next_page);
1420 
1421 		/* Read the next page */
1422 		ctx->num_pages++;
1423 		ctx->pages = spdk_realloc(ctx->pages, (sizeof(*page) * ctx->num_pages),
1424 					  sizeof(*page));
1425 		if (ctx->pages == NULL) {
1426 			_spdk_blob_load_final(ctx, -ENOMEM);
1427 			return;
1428 		}
1429 
1430 		bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1],
1431 				     next_lba,
1432 				     _spdk_bs_byte_to_lba(blob->bs, sizeof(*page)),
1433 				     _spdk_blob_load_cpl, ctx);
1434 		return;
1435 	}
1436 
1437 	/* Parse the pages */
1438 	rc = _spdk_blob_parse(ctx->pages, ctx->num_pages, blob);
1439 	if (rc) {
1440 		_spdk_blob_load_final(ctx, rc);
1441 		return;
1442 	}
1443 
1444 	if (blob->extent_table_found == true) {
1445 		/* If EXTENT_TABLE was found, that means support for it should be enabled. */
1446 		assert(blob->extent_rle_found == false);
1447 		blob->use_extent_table = true;
1448 	} else {
1449 		/* If EXTENT_RLE or no extent_* descriptor was found disable support
1450 		 * for extent table. No extent_* descriptors means that blob has length of 0
1451 		 * and no extent_rle descriptors were persisted for it.
1452 		 * EXTENT_TABLE if used, is always present in metadata regardless of length. */
1453 		blob->use_extent_table = false;
1454 	}
1455 
1456 	/* Check the clear_method stored in metadata vs what may have been passed
1457 	 * via spdk_bs_open_blob_ext() and update accordingly.
1458 	 */
1459 	_spdk_blob_update_clear_method(blob);
1460 
1461 	spdk_free(ctx->pages);
1462 	ctx->pages = NULL;
1463 
1464 	if (blob->extent_table_found) {
1465 		_spdk_blob_load_cpl_extents_cpl(seq, ctx, 0);
1466 	} else {
1467 		_spdk_blob_load_backing_dev(ctx);
1468 	}
1469 }
1470 
1471 /* Load a blob from disk given a blobid */
1472 static void
1473 _spdk_blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
1474 		spdk_bs_sequence_cpl cb_fn, void *cb_arg)
1475 {
1476 	struct spdk_blob_load_ctx *ctx;
1477 	struct spdk_blob_store *bs;
1478 	uint32_t page_num;
1479 	uint64_t lba;
1480 
1481 	_spdk_blob_verify_md_op(blob);
1482 
1483 	bs = blob->bs;
1484 
1485 	ctx = calloc(1, sizeof(*ctx));
1486 	if (!ctx) {
1487 		cb_fn(seq, cb_arg, -ENOMEM);
1488 		return;
1489 	}
1490 
1491 	ctx->blob = blob;
1492 	ctx->pages = spdk_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, SPDK_BS_PAGE_SIZE);
1493 	if (!ctx->pages) {
1494 		free(ctx);
1495 		cb_fn(seq, cb_arg, -ENOMEM);
1496 		return;
1497 	}
1498 	ctx->num_pages = 1;
1499 	ctx->cb_fn = cb_fn;
1500 	ctx->cb_arg = cb_arg;
1501 	ctx->seq = seq;
1502 
1503 	page_num = _spdk_bs_blobid_to_page(blob->id);
1504 	lba = _spdk_bs_md_page_to_lba(blob->bs, page_num);
1505 
1506 	blob->state = SPDK_BLOB_STATE_LOADING;
1507 
1508 	bs_sequence_read_dev(seq, &ctx->pages[0], lba,
1509 			     _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
1510 			     _spdk_blob_load_cpl, ctx);
1511 }
1512 
1513 struct spdk_blob_persist_ctx {
1514 	struct spdk_blob		*blob;
1515 
1516 	struct spdk_bs_super_block	*super;
1517 
1518 	struct spdk_blob_md_page	*pages;
1519 	uint32_t			next_extent_page;
1520 	struct spdk_blob_md_page	*extent_page;
1521 
1522 	spdk_bs_sequence_t		*seq;
1523 	spdk_bs_sequence_cpl		cb_fn;
1524 	void				*cb_arg;
1525 	TAILQ_ENTRY(spdk_blob_persist_ctx) link;
1526 };
1527 
1528 static void
1529 spdk_bs_batch_clear_dev(struct spdk_blob_persist_ctx *ctx, spdk_bs_batch_t *batch, uint64_t lba,
1530 			uint32_t lba_count)
1531 {
1532 	switch (ctx->blob->clear_method) {
1533 	case BLOB_CLEAR_WITH_DEFAULT:
1534 	case BLOB_CLEAR_WITH_UNMAP:
1535 		bs_batch_unmap_dev(batch, lba, lba_count);
1536 		break;
1537 	case BLOB_CLEAR_WITH_WRITE_ZEROES:
1538 		bs_batch_write_zeroes_dev(batch, lba, lba_count);
1539 		break;
1540 	case BLOB_CLEAR_WITH_NONE:
1541 	default:
1542 		break;
1543 	}
1544 }
1545 
1546 static void _spdk_blob_persist_check_dirty(struct spdk_blob_persist_ctx *ctx);
1547 
1548 static void
1549 _spdk_blob_persist_complete(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1550 {
1551 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1552 	struct spdk_blob_persist_ctx	*next_persist;
1553 	struct spdk_blob		*blob = ctx->blob;
1554 
1555 	if (bserrno == 0) {
1556 		_spdk_blob_mark_clean(blob);
1557 	}
1558 
1559 	assert(ctx == TAILQ_FIRST(&blob->pending_persists));
1560 	TAILQ_REMOVE(&blob->pending_persists, ctx, link);
1561 
1562 	next_persist = TAILQ_FIRST(&blob->pending_persists);
1563 
1564 	/* Call user callback */
1565 	ctx->cb_fn(seq, ctx->cb_arg, bserrno);
1566 
1567 	/* Free the memory */
1568 	spdk_free(ctx->pages);
1569 	free(ctx);
1570 
1571 	if (next_persist != NULL) {
1572 		_spdk_blob_persist_check_dirty(next_persist);
1573 	}
1574 }
1575 
1576 static void
1577 _spdk_blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1578 {
1579 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1580 	struct spdk_blob		*blob = ctx->blob;
1581 	struct spdk_blob_store		*bs = blob->bs;
1582 	size_t				i;
1583 
1584 	if (bserrno != 0) {
1585 		_spdk_blob_persist_complete(seq, ctx, bserrno);
1586 		return;
1587 	}
1588 
1589 	/* Release all clusters that were truncated */
1590 	for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
1591 		uint32_t cluster_num = _spdk_bs_lba_to_cluster(bs, blob->active.clusters[i]);
1592 
1593 		/* Nothing to release if it was not allocated */
1594 		if (blob->active.clusters[i] != 0) {
1595 			_spdk_bs_release_cluster(bs, cluster_num);
1596 		}
1597 	}
1598 
1599 	if (blob->active.num_clusters == 0) {
1600 		free(blob->active.clusters);
1601 		blob->active.clusters = NULL;
1602 		blob->active.cluster_array_size = 0;
1603 	} else if (blob->active.num_clusters != blob->active.cluster_array_size) {
1604 #ifndef __clang_analyzer__
1605 		void *tmp;
1606 
1607 		/* scan-build really can't figure reallocs, workaround it */
1608 		tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * blob->active.num_clusters);
1609 		assert(tmp != NULL);
1610 		blob->active.clusters = tmp;
1611 
1612 		tmp = realloc(blob->active.extent_pages, sizeof(uint32_t) * blob->active.num_extent_pages);
1613 		assert(tmp != NULL);
1614 		blob->active.extent_pages = tmp;
1615 #endif
1616 		blob->active.extent_pages_array_size = blob->active.num_extent_pages;
1617 		blob->active.cluster_array_size = blob->active.num_clusters;
1618 	}
1619 
1620 	/* TODO: Add path to persist clear extent pages. */
1621 	_spdk_blob_persist_complete(seq, ctx, bserrno);
1622 }
1623 
1624 static void
1625 _spdk_blob_persist_clear_clusters(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1626 {
1627 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1628 	struct spdk_blob		*blob = ctx->blob;
1629 	struct spdk_blob_store		*bs = blob->bs;
1630 	spdk_bs_batch_t			*batch;
1631 	size_t				i;
1632 	uint64_t			lba;
1633 	uint32_t			lba_count;
1634 
1635 	if (bserrno != 0) {
1636 		_spdk_blob_persist_complete(seq, ctx, bserrno);
1637 		return;
1638 	}
1639 
1640 	/* Clusters don't move around in blobs. The list shrinks or grows
1641 	 * at the end, but no changes ever occur in the middle of the list.
1642 	 */
1643 
1644 	batch = bs_sequence_to_batch(seq, _spdk_blob_persist_clear_clusters_cpl, ctx);
1645 
1646 	/* Clear all clusters that were truncated */
1647 	lba = 0;
1648 	lba_count = 0;
1649 	for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
1650 		uint64_t next_lba = blob->active.clusters[i];
1651 		uint32_t next_lba_count = _spdk_bs_cluster_to_lba(bs, 1);
1652 
1653 		if (next_lba > 0 && (lba + lba_count) == next_lba) {
1654 			/* This cluster is contiguous with the previous one. */
1655 			lba_count += next_lba_count;
1656 			continue;
1657 		}
1658 
1659 		/* This cluster is not contiguous with the previous one. */
1660 
1661 		/* If a run of LBAs previously existing, clear them now */
1662 		if (lba_count > 0) {
1663 			spdk_bs_batch_clear_dev(ctx, batch, lba, lba_count);
1664 		}
1665 
1666 		/* Start building the next batch */
1667 		lba = next_lba;
1668 		if (next_lba > 0) {
1669 			lba_count = next_lba_count;
1670 		} else {
1671 			lba_count = 0;
1672 		}
1673 	}
1674 
1675 	/* If we ended with a contiguous set of LBAs, clear them now */
1676 	if (lba_count > 0) {
1677 		spdk_bs_batch_clear_dev(ctx, batch, lba, lba_count);
1678 	}
1679 
1680 	bs_batch_close(batch);
1681 }
1682 
1683 static void
1684 _spdk_blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1685 {
1686 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1687 	struct spdk_blob		*blob = ctx->blob;
1688 	struct spdk_blob_store		*bs = blob->bs;
1689 	size_t				i;
1690 
1691 	if (bserrno != 0) {
1692 		_spdk_blob_persist_complete(seq, ctx, bserrno);
1693 		return;
1694 	}
1695 
1696 	/* This loop starts at 1 because the first page is special and handled
1697 	 * below. The pages (except the first) are never written in place,
1698 	 * so any pages in the clean list must be zeroed.
1699 	 */
1700 	for (i = 1; i < blob->clean.num_pages; i++) {
1701 		_spdk_bs_release_md_page(bs, blob->clean.pages[i]);
1702 	}
1703 
1704 	if (blob->active.num_pages == 0) {
1705 		uint32_t page_num;
1706 
1707 		page_num = _spdk_bs_blobid_to_page(blob->id);
1708 		_spdk_bs_release_md_page(bs, page_num);
1709 	}
1710 
1711 	/* Move on to clearing clusters */
1712 	_spdk_blob_persist_clear_clusters(seq, ctx, 0);
1713 }
1714 
1715 static void
1716 _spdk_blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1717 {
1718 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1719 	struct spdk_blob		*blob = ctx->blob;
1720 	struct spdk_blob_store		*bs = blob->bs;
1721 	uint64_t			lba;
1722 	uint32_t			lba_count;
1723 	spdk_bs_batch_t			*batch;
1724 	size_t				i;
1725 
1726 	if (bserrno != 0) {
1727 		_spdk_blob_persist_complete(seq, ctx, bserrno);
1728 		return;
1729 	}
1730 
1731 	batch = bs_sequence_to_batch(seq, _spdk_blob_persist_zero_pages_cpl, ctx);
1732 
1733 	lba_count = _spdk_bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
1734 
1735 	/* This loop starts at 1 because the first page is special and handled
1736 	 * below. The pages (except the first) are never written in place,
1737 	 * so any pages in the clean list must be zeroed.
1738 	 */
1739 	for (i = 1; i < blob->clean.num_pages; i++) {
1740 		lba = _spdk_bs_md_page_to_lba(bs, blob->clean.pages[i]);
1741 
1742 		bs_batch_write_zeroes_dev(batch, lba, lba_count);
1743 	}
1744 
1745 	/* The first page will only be zeroed if this is a delete. */
1746 	if (blob->active.num_pages == 0) {
1747 		uint32_t page_num;
1748 
1749 		/* The first page in the metadata goes where the blobid indicates */
1750 		page_num = _spdk_bs_blobid_to_page(blob->id);
1751 		lba = _spdk_bs_md_page_to_lba(bs, page_num);
1752 
1753 		bs_batch_write_zeroes_dev(batch, lba, lba_count);
1754 	}
1755 
1756 	bs_batch_close(batch);
1757 }
1758 
1759 static void
1760 _spdk_blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1761 {
1762 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1763 	struct spdk_blob		*blob = ctx->blob;
1764 	struct spdk_blob_store		*bs = blob->bs;
1765 	uint64_t			lba;
1766 	uint32_t			lba_count;
1767 	struct spdk_blob_md_page	*page;
1768 
1769 	if (bserrno != 0) {
1770 		_spdk_blob_persist_complete(seq, ctx, bserrno);
1771 		return;
1772 	}
1773 
1774 	if (blob->active.num_pages == 0) {
1775 		/* Move on to the next step */
1776 		_spdk_blob_persist_zero_pages(seq, ctx, 0);
1777 		return;
1778 	}
1779 
1780 	lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page));
1781 
1782 	page = &ctx->pages[0];
1783 	/* The first page in the metadata goes where the blobid indicates */
1784 	lba = _spdk_bs_md_page_to_lba(bs, _spdk_bs_blobid_to_page(blob->id));
1785 
1786 	bs_sequence_write_dev(seq, page, lba, lba_count,
1787 			      _spdk_blob_persist_zero_pages, ctx);
1788 }
1789 
1790 static void
1791 _spdk_blob_persist_write_page_chain(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1792 {
1793 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
1794 	struct spdk_blob		*blob = ctx->blob;
1795 	struct spdk_blob_store		*bs = blob->bs;
1796 	uint64_t			lba;
1797 	uint32_t			lba_count;
1798 	struct spdk_blob_md_page	*page;
1799 	spdk_bs_batch_t			*batch;
1800 	size_t				i;
1801 
1802 	if (bserrno != 0) {
1803 		_spdk_blob_persist_complete(seq, ctx, bserrno);
1804 		return;
1805 	}
1806 
1807 	/* Clusters don't move around in blobs. The list shrinks or grows
1808 	 * at the end, but no changes ever occur in the middle of the list.
1809 	 */
1810 
1811 	lba_count = _spdk_bs_byte_to_lba(bs, sizeof(*page));
1812 
1813 	batch = bs_sequence_to_batch(seq, _spdk_blob_persist_write_page_root, ctx);
1814 
1815 	/* This starts at 1. The root page is not written until
1816 	 * all of the others are finished
1817 	 */
1818 	for (i = 1; i < blob->active.num_pages; i++) {
1819 		page = &ctx->pages[i];
1820 		assert(page->sequence_num == i);
1821 
1822 		lba = _spdk_bs_md_page_to_lba(bs, blob->active.pages[i]);
1823 
1824 		bs_batch_write_dev(batch, page, lba, lba_count);
1825 	}
1826 
1827 	bs_batch_close(batch);
1828 }
1829 
1830 static int
1831 _spdk_blob_resize(struct spdk_blob *blob, uint64_t sz)
1832 {
1833 	uint64_t	i;
1834 	uint64_t	*tmp;
1835 	uint64_t	lfc; /* lowest free cluster */
1836 	uint32_t	lfmd; /*  lowest free md page */
1837 	uint64_t	num_clusters;
1838 	uint32_t	*ep_tmp;
1839 	uint64_t	new_num_ep = 0, current_num_ep = 0;
1840 	struct spdk_blob_store *bs;
1841 
1842 	bs = blob->bs;
1843 
1844 	_spdk_blob_verify_md_op(blob);
1845 
1846 	if (blob->active.num_clusters == sz) {
1847 		return 0;
1848 	}
1849 
1850 	if (blob->active.num_clusters < blob->active.cluster_array_size) {
1851 		/* If this blob was resized to be larger, then smaller, then
1852 		 * larger without syncing, then the cluster array already
1853 		 * contains spare assigned clusters we can use.
1854 		 */
1855 		num_clusters = spdk_min(blob->active.cluster_array_size,
1856 					sz);
1857 	} else {
1858 		num_clusters = blob->active.num_clusters;
1859 	}
1860 
1861 	if (blob->use_extent_table) {
1862 		/* Round up since every cluster beyond current Extent Table size,
1863 		 * requires new extent page. */
1864 		new_num_ep = spdk_divide_round_up(sz, SPDK_EXTENTS_PER_EP);
1865 		current_num_ep = spdk_divide_round_up(num_clusters, SPDK_EXTENTS_PER_EP);
1866 	}
1867 
1868 	/* Do two passes - one to verify that we can obtain enough clusters
1869 	 * and md pages, another to actually claim them.
1870 	 */
1871 
1872 	if (spdk_blob_is_thin_provisioned(blob) == false) {
1873 		lfc = 0;
1874 		for (i = num_clusters; i < sz; i++) {
1875 			lfc = spdk_bit_array_find_first_clear(bs->used_clusters, lfc);
1876 			if (lfc == UINT32_MAX) {
1877 				/* No more free clusters. Cannot satisfy the request */
1878 				return -ENOSPC;
1879 			}
1880 			lfc++;
1881 		}
1882 		lfmd = 0;
1883 		for (i = current_num_ep; i < new_num_ep ; i++) {
1884 			lfmd = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, lfmd);
1885 			if (lfmd == UINT32_MAX) {
1886 				/* No more free md pages. Cannot satisfy the request */
1887 				return -ENOSPC;
1888 			}
1889 		}
1890 	}
1891 
1892 	if (sz > num_clusters) {
1893 		/* Expand the cluster array if necessary.
1894 		 * We only shrink the array when persisting.
1895 		 */
1896 		tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * sz);
1897 		if (sz > 0 && tmp == NULL) {
1898 			return -ENOMEM;
1899 		}
1900 		memset(tmp + blob->active.cluster_array_size, 0,
1901 		       sizeof(*blob->active.clusters) * (sz - blob->active.cluster_array_size));
1902 		blob->active.clusters = tmp;
1903 		blob->active.cluster_array_size = sz;
1904 
1905 		/* Expand the extents table, only if enough clusters were added */
1906 		if (new_num_ep > current_num_ep && blob->use_extent_table) {
1907 			ep_tmp = realloc(blob->active.extent_pages, sizeof(*blob->active.extent_pages) * new_num_ep);
1908 			if (new_num_ep > 0 && ep_tmp == NULL) {
1909 				return -ENOMEM;
1910 			}
1911 			memset(ep_tmp + blob->active.extent_pages_array_size, 0,
1912 			       sizeof(*blob->active.extent_pages) * (new_num_ep - blob->active.extent_pages_array_size));
1913 			blob->active.extent_pages = ep_tmp;
1914 			blob->active.extent_pages_array_size = new_num_ep;
1915 		}
1916 	}
1917 
1918 	blob->state = SPDK_BLOB_STATE_DIRTY;
1919 
1920 	if (spdk_blob_is_thin_provisioned(blob) == false) {
1921 		lfc = 0;
1922 		lfmd = 0;
1923 		for (i = num_clusters; i < sz; i++) {
1924 			_spdk_bs_allocate_cluster(blob, i, &lfc, &lfmd, true);
1925 			lfc++;
1926 			lfmd++;
1927 		}
1928 	}
1929 
1930 	blob->active.num_clusters = sz;
1931 	blob->active.num_extent_pages = new_num_ep;
1932 
1933 	return 0;
1934 }
1935 
1936 static void
1937 _spdk_blob_persist_generate_new_md(struct spdk_blob_persist_ctx *ctx)
1938 {
1939 	spdk_bs_sequence_t *seq = ctx->seq;
1940 	struct spdk_blob *blob = ctx->blob;
1941 	struct spdk_blob_store *bs = blob->bs;
1942 	uint64_t i;
1943 	uint32_t page_num;
1944 	void *tmp;
1945 	int rc;
1946 
1947 	/* Generate the new metadata */
1948 	rc = _spdk_blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
1949 	if (rc < 0) {
1950 		_spdk_blob_persist_complete(seq, ctx, rc);
1951 		return;
1952 	}
1953 
1954 	assert(blob->active.num_pages >= 1);
1955 
1956 	/* Resize the cache of page indices */
1957 	tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
1958 	if (!tmp) {
1959 		_spdk_blob_persist_complete(seq, ctx, -ENOMEM);
1960 		return;
1961 	}
1962 	blob->active.pages = tmp;
1963 
1964 	/* Assign this metadata to pages. This requires two passes -
1965 	 * one to verify that there are enough pages and a second
1966 	 * to actually claim them. */
1967 	page_num = 0;
1968 	/* Note that this loop starts at one. The first page location is fixed by the blobid. */
1969 	for (i = 1; i < blob->active.num_pages; i++) {
1970 		page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
1971 		if (page_num == UINT32_MAX) {
1972 			_spdk_blob_persist_complete(seq, ctx, -ENOMEM);
1973 			return;
1974 		}
1975 		page_num++;
1976 	}
1977 
1978 	page_num = 0;
1979 	blob->active.pages[0] = _spdk_bs_blobid_to_page(blob->id);
1980 	for (i = 1; i < blob->active.num_pages; i++) {
1981 		page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
1982 		ctx->pages[i - 1].next = page_num;
1983 		/* Now that previous metadata page is complete, calculate the crc for it. */
1984 		ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]);
1985 		blob->active.pages[i] = page_num;
1986 		_spdk_bs_claim_md_page(bs, page_num);
1987 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Claiming page %u for blob %lu\n", page_num, blob->id);
1988 		page_num++;
1989 	}
1990 	ctx->pages[i - 1].crc = _spdk_blob_md_page_calc_crc(&ctx->pages[i - 1]);
1991 	/* Start writing the metadata from last page to first */
1992 	blob->state = SPDK_BLOB_STATE_CLEAN;
1993 	_spdk_blob_persist_write_page_chain(seq, ctx, 0);
1994 }
1995 
1996 static void
1997 _spdk_blob_persist_write_extent_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
1998 {
1999 	struct spdk_blob_persist_ctx	*ctx = cb_arg;
2000 	struct spdk_blob		*blob = ctx->blob;
2001 	size_t				i;
2002 	uint32_t			extent_page_id;
2003 	uint32_t                        page_count = 0;
2004 	int				rc;
2005 
2006 	if (ctx->extent_page != NULL) {
2007 		spdk_free(ctx->extent_page);
2008 		ctx->extent_page = NULL;
2009 	}
2010 
2011 	if (bserrno != 0) {
2012 		_spdk_blob_persist_complete(seq, ctx, bserrno);
2013 		return;
2014 	}
2015 
2016 	/* Only write out changed extent pages */
2017 	for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) {
2018 		extent_page_id = blob->active.extent_pages[i];
2019 		if (extent_page_id == 0) {
2020 			/* No Extent Page to persist */
2021 			assert(spdk_blob_is_thin_provisioned(blob));
2022 			continue;
2023 		}
2024 		/* Writing out new extent page for the first time. Either active extent pages is larger
2025 		 * than clean extent pages or there was no extent page assigned due to thin provisioning. */
2026 		if (i >= blob->clean.extent_pages_array_size || blob->clean.extent_pages[i] == 0) {
2027 			blob->state = SPDK_BLOB_STATE_DIRTY;
2028 			assert(spdk_bit_array_get(blob->bs->used_md_pages, extent_page_id));
2029 			ctx->next_extent_page = i + 1;
2030 			rc = _spdk_blob_serialize_add_page(ctx->blob, &ctx->extent_page, &page_count, &ctx->extent_page);
2031 			if (rc < 0) {
2032 				_spdk_blob_persist_complete(seq, ctx, rc);
2033 				return;
2034 			}
2035 
2036 			_spdk_blob_serialize_extent_page(blob, i * SPDK_EXTENTS_PER_EP, ctx->extent_page);
2037 
2038 			ctx->extent_page->crc = _spdk_blob_md_page_calc_crc(ctx->extent_page);
2039 
2040 			bs_sequence_write_dev(seq, ctx->extent_page, _spdk_bs_md_page_to_lba(blob->bs, extent_page_id),
2041 					      _spdk_bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
2042 					      _spdk_blob_persist_write_extent_pages, ctx);
2043 			return;
2044 		}
2045 		assert(blob->clean.extent_pages[i] != 0);
2046 	}
2047 
2048 	_spdk_blob_persist_generate_new_md(ctx);
2049 }
2050 
2051 static void
2052 _spdk_blob_persist_start(struct spdk_blob_persist_ctx *ctx)
2053 {
2054 	spdk_bs_sequence_t *seq = ctx->seq;
2055 	struct spdk_blob *blob = ctx->blob;
2056 
2057 	if (blob->active.num_pages == 0) {
2058 		/* This is the signal that the blob should be deleted.
2059 		 * Immediately jump to the clean up routine. */
2060 		assert(blob->clean.num_pages > 0);
2061 		blob->state = SPDK_BLOB_STATE_CLEAN;
2062 		_spdk_blob_persist_zero_pages(seq, ctx, 0);
2063 		return;
2064 
2065 	}
2066 
2067 	_spdk_blob_persist_write_extent_pages(seq, ctx, 0);
2068 }
2069 
2070 static void
2071 _spdk_blob_persist_dirty_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2072 {
2073 	struct spdk_blob_persist_ctx *ctx = cb_arg;
2074 
2075 	spdk_free(ctx->super);
2076 
2077 	if (bserrno != 0) {
2078 		_spdk_blob_persist_complete(seq, ctx, bserrno);
2079 		return;
2080 	}
2081 
2082 	ctx->blob->bs->clean = 0;
2083 
2084 	_spdk_blob_persist_start(ctx);
2085 }
2086 
2087 static void
2088 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
2089 		     struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg);
2090 
2091 
2092 static void
2093 _spdk_blob_persist_dirty(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2094 {
2095 	struct spdk_blob_persist_ctx *ctx = cb_arg;
2096 
2097 	if (bserrno != 0) {
2098 		spdk_free(ctx->super);
2099 		_spdk_blob_persist_complete(seq, ctx, bserrno);
2100 		return;
2101 	}
2102 
2103 	ctx->super->clean = 0;
2104 	if (ctx->super->size == 0) {
2105 		ctx->super->size = ctx->blob->bs->dev->blockcnt * ctx->blob->bs->dev->blocklen;
2106 	}
2107 
2108 	_spdk_bs_write_super(seq, ctx->blob->bs, ctx->super, _spdk_blob_persist_dirty_cpl, ctx);
2109 }
2110 
2111 static void
2112 _spdk_blob_persist_check_dirty(struct spdk_blob_persist_ctx *ctx)
2113 {
2114 	if (ctx->blob->bs->clean) {
2115 		ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
2116 					  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
2117 		if (!ctx->super) {
2118 			_spdk_blob_persist_complete(ctx->seq, ctx, -ENOMEM);
2119 			return;
2120 		}
2121 
2122 		bs_sequence_read_dev(ctx->seq, ctx->super, _spdk_bs_page_to_lba(ctx->blob->bs, 0),
2123 				     _spdk_bs_byte_to_lba(ctx->blob->bs, sizeof(*ctx->super)),
2124 				     _spdk_blob_persist_dirty, ctx);
2125 	} else {
2126 		_spdk_blob_persist_start(ctx);
2127 	}
2128 }
2129 
2130 /* Write a blob to disk */
2131 static void
2132 _spdk_blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
2133 		   spdk_bs_sequence_cpl cb_fn, void *cb_arg)
2134 {
2135 	struct spdk_blob_persist_ctx *ctx;
2136 
2137 	_spdk_blob_verify_md_op(blob);
2138 
2139 	if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->pending_persists)) {
2140 		cb_fn(seq, cb_arg, 0);
2141 		return;
2142 	}
2143 
2144 	ctx = calloc(1, sizeof(*ctx));
2145 	if (!ctx) {
2146 		cb_fn(seq, cb_arg, -ENOMEM);
2147 		return;
2148 	}
2149 	ctx->blob = blob;
2150 	ctx->seq = seq;
2151 	ctx->cb_fn = cb_fn;
2152 	ctx->cb_arg = cb_arg;
2153 	ctx->next_extent_page = 0;
2154 
2155 	/* Multiple blob persists can affect one another, via blob->state or
2156 	 * blob mutable data changes. To prevent it, queue up the persists. */
2157 	if (!TAILQ_EMPTY(&blob->pending_persists)) {
2158 		TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link);
2159 		return;
2160 	}
2161 	TAILQ_INSERT_HEAD(&blob->pending_persists, ctx, link);
2162 
2163 	_spdk_blob_persist_check_dirty(ctx);
2164 }
2165 
2166 struct spdk_blob_copy_cluster_ctx {
2167 	struct spdk_blob *blob;
2168 	uint8_t *buf;
2169 	uint64_t page;
2170 	uint64_t new_cluster;
2171 	uint32_t new_extent_page;
2172 	spdk_bs_sequence_t *seq;
2173 };
2174 
2175 static void
2176 _spdk_blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno)
2177 {
2178 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2179 	struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq;
2180 	TAILQ_HEAD(, spdk_bs_request_set) requests;
2181 	spdk_bs_user_op_t *op;
2182 
2183 	TAILQ_INIT(&requests);
2184 	TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link);
2185 
2186 	while (!TAILQ_EMPTY(&requests)) {
2187 		op = TAILQ_FIRST(&requests);
2188 		TAILQ_REMOVE(&requests, op, link);
2189 		if (bserrno == 0) {
2190 			bs_user_op_execute(op);
2191 		} else {
2192 			bs_user_op_abort(op);
2193 		}
2194 	}
2195 
2196 	spdk_free(ctx->buf);
2197 	free(ctx);
2198 }
2199 
2200 static void
2201 _spdk_blob_insert_cluster_cpl(void *cb_arg, int bserrno)
2202 {
2203 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2204 
2205 	if (bserrno) {
2206 		if (bserrno == -EEXIST) {
2207 			/* The metadata insert failed because another thread
2208 			 * allocated the cluster first. Free our cluster
2209 			 * but continue without error. */
2210 			bserrno = 0;
2211 		}
2212 		_spdk_bs_release_cluster(ctx->blob->bs, ctx->new_cluster);
2213 		if (ctx->new_extent_page != 0) {
2214 			_spdk_bs_release_md_page(ctx->blob->bs, ctx->new_extent_page);
2215 		}
2216 	}
2217 
2218 	bs_sequence_finish(ctx->seq, bserrno);
2219 }
2220 
2221 static void
2222 _spdk_blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2223 {
2224 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2225 	uint32_t cluster_number;
2226 
2227 	if (bserrno) {
2228 		/* The write failed, so jump to the final completion handler */
2229 		bs_sequence_finish(seq, bserrno);
2230 		return;
2231 	}
2232 
2233 	cluster_number = _spdk_bs_page_to_cluster(ctx->blob->bs, ctx->page);
2234 
2235 	_spdk_blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
2236 					       ctx->new_extent_page, _spdk_blob_insert_cluster_cpl, ctx);
2237 }
2238 
2239 static void
2240 _spdk_blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2241 {
2242 	struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
2243 
2244 	if (bserrno != 0) {
2245 		/* The read failed, so jump to the final completion handler */
2246 		bs_sequence_finish(seq, bserrno);
2247 		return;
2248 	}
2249 
2250 	/* Write whole cluster */
2251 	bs_sequence_write_dev(seq, ctx->buf,
2252 			      _spdk_bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
2253 			      _spdk_bs_cluster_to_lba(ctx->blob->bs, 1),
2254 			      _spdk_blob_write_copy_cpl, ctx);
2255 }
2256 
2257 static void
2258 _spdk_bs_allocate_and_copy_cluster(struct spdk_blob *blob,
2259 				   struct spdk_io_channel *_ch,
2260 				   uint64_t io_unit, spdk_bs_user_op_t *op)
2261 {
2262 	struct spdk_bs_cpl cpl;
2263 	struct spdk_bs_channel *ch;
2264 	struct spdk_blob_copy_cluster_ctx *ctx;
2265 	uint32_t cluster_start_page;
2266 	uint32_t cluster_number;
2267 	int rc;
2268 
2269 	ch = spdk_io_channel_get_ctx(_ch);
2270 
2271 	if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) {
2272 		/* There are already operations pending. Queue this user op
2273 		 * and return because it will be re-executed when the outstanding
2274 		 * cluster allocation completes. */
2275 		TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
2276 		return;
2277 	}
2278 
2279 	/* Round the io_unit offset down to the first page in the cluster */
2280 	cluster_start_page = _spdk_bs_io_unit_to_cluster_start(blob, io_unit);
2281 
2282 	/* Calculate which index in the metadata cluster array the corresponding
2283 	 * cluster is supposed to be at. */
2284 	cluster_number = _spdk_bs_io_unit_to_cluster_number(blob, io_unit);
2285 
2286 	ctx = calloc(1, sizeof(*ctx));
2287 	if (!ctx) {
2288 		bs_user_op_abort(op);
2289 		return;
2290 	}
2291 
2292 	assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0);
2293 
2294 	ctx->blob = blob;
2295 	ctx->page = cluster_start_page;
2296 
2297 	if (blob->parent_id != SPDK_BLOBID_INVALID) {
2298 		ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen,
2299 				       NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
2300 		if (!ctx->buf) {
2301 			SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n",
2302 				    blob->bs->cluster_sz);
2303 			free(ctx);
2304 			bs_user_op_abort(op);
2305 			return;
2306 		}
2307 	}
2308 
2309 	rc = _spdk_bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page,
2310 				       false);
2311 	if (rc != 0) {
2312 		spdk_free(ctx->buf);
2313 		free(ctx);
2314 		bs_user_op_abort(op);
2315 		return;
2316 	}
2317 
2318 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2319 	cpl.u.blob_basic.cb_fn = _spdk_blob_allocate_and_copy_cluster_cpl;
2320 	cpl.u.blob_basic.cb_arg = ctx;
2321 
2322 	ctx->seq = bs_sequence_start(_ch, &cpl);
2323 	if (!ctx->seq) {
2324 		_spdk_bs_release_cluster(blob->bs, ctx->new_cluster);
2325 		spdk_free(ctx->buf);
2326 		free(ctx);
2327 		bs_user_op_abort(op);
2328 		return;
2329 	}
2330 
2331 	/* Queue the user op to block other incoming operations */
2332 	TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
2333 
2334 	if (blob->parent_id != SPDK_BLOBID_INVALID) {
2335 		/* Read cluster from backing device */
2336 		bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf,
2337 					_spdk_bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
2338 					_spdk_bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz),
2339 					_spdk_blob_write_copy, ctx);
2340 	} else {
2341 		_spdk_blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
2342 						       ctx->new_extent_page, _spdk_blob_insert_cluster_cpl, ctx);
2343 	}
2344 }
2345 
2346 static inline void
2347 _spdk_blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length,
2348 				       uint64_t *lba,	uint32_t *lba_count)
2349 {
2350 	*lba_count = length;
2351 
2352 	if (!_spdk_bs_io_unit_is_allocated(blob, io_unit)) {
2353 		assert(blob->back_bs_dev != NULL);
2354 		*lba = _spdk_bs_io_unit_to_back_dev_lba(blob, io_unit);
2355 		*lba_count = _spdk_bs_io_unit_to_back_dev_lba(blob, *lba_count);
2356 	} else {
2357 		*lba = _spdk_bs_blob_io_unit_to_lba(blob, io_unit);
2358 	}
2359 }
2360 
2361 struct op_split_ctx {
2362 	struct spdk_blob *blob;
2363 	struct spdk_io_channel *channel;
2364 	uint64_t io_unit_offset;
2365 	uint64_t io_units_remaining;
2366 	void *curr_payload;
2367 	enum spdk_blob_op_type op_type;
2368 	spdk_bs_sequence_t *seq;
2369 };
2370 
2371 static void
2372 _spdk_blob_request_submit_op_split_next(void *cb_arg, int bserrno)
2373 {
2374 	struct op_split_ctx	*ctx = cb_arg;
2375 	struct spdk_blob	*blob = ctx->blob;
2376 	struct spdk_io_channel	*ch = ctx->channel;
2377 	enum spdk_blob_op_type	op_type = ctx->op_type;
2378 	uint8_t			*buf = ctx->curr_payload;
2379 	uint64_t		offset = ctx->io_unit_offset;
2380 	uint64_t		length = ctx->io_units_remaining;
2381 	uint64_t		op_length;
2382 
2383 	if (bserrno != 0 || ctx->io_units_remaining == 0) {
2384 		bs_sequence_finish(ctx->seq, bserrno);
2385 		free(ctx);
2386 		return;
2387 	}
2388 
2389 	op_length = spdk_min(length, _spdk_bs_num_io_units_to_cluster_boundary(blob,
2390 			     offset));
2391 
2392 	/* Update length and payload for next operation */
2393 	ctx->io_units_remaining -= op_length;
2394 	ctx->io_unit_offset += op_length;
2395 	if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) {
2396 		ctx->curr_payload += op_length * blob->bs->io_unit_size;
2397 	}
2398 
2399 	switch (op_type) {
2400 	case SPDK_BLOB_READ:
2401 		spdk_blob_io_read(blob, ch, buf, offset, op_length,
2402 				  _spdk_blob_request_submit_op_split_next, ctx);
2403 		break;
2404 	case SPDK_BLOB_WRITE:
2405 		spdk_blob_io_write(blob, ch, buf, offset, op_length,
2406 				   _spdk_blob_request_submit_op_split_next, ctx);
2407 		break;
2408 	case SPDK_BLOB_UNMAP:
2409 		spdk_blob_io_unmap(blob, ch, offset, op_length,
2410 				   _spdk_blob_request_submit_op_split_next, ctx);
2411 		break;
2412 	case SPDK_BLOB_WRITE_ZEROES:
2413 		spdk_blob_io_write_zeroes(blob, ch, offset, op_length,
2414 					  _spdk_blob_request_submit_op_split_next, ctx);
2415 		break;
2416 	case SPDK_BLOB_READV:
2417 	case SPDK_BLOB_WRITEV:
2418 		SPDK_ERRLOG("readv/write not valid\n");
2419 		bs_sequence_finish(ctx->seq, -EINVAL);
2420 		free(ctx);
2421 		break;
2422 	}
2423 }
2424 
2425 static void
2426 _spdk_blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob,
2427 				   void *payload, uint64_t offset, uint64_t length,
2428 				   spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
2429 {
2430 	struct op_split_ctx *ctx;
2431 	spdk_bs_sequence_t *seq;
2432 	struct spdk_bs_cpl cpl;
2433 
2434 	assert(blob != NULL);
2435 
2436 	ctx = calloc(1, sizeof(struct op_split_ctx));
2437 	if (ctx == NULL) {
2438 		cb_fn(cb_arg, -ENOMEM);
2439 		return;
2440 	}
2441 
2442 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2443 	cpl.u.blob_basic.cb_fn = cb_fn;
2444 	cpl.u.blob_basic.cb_arg = cb_arg;
2445 
2446 	seq = bs_sequence_start(ch, &cpl);
2447 	if (!seq) {
2448 		free(ctx);
2449 		cb_fn(cb_arg, -ENOMEM);
2450 		return;
2451 	}
2452 
2453 	ctx->blob = blob;
2454 	ctx->channel = ch;
2455 	ctx->curr_payload = payload;
2456 	ctx->io_unit_offset = offset;
2457 	ctx->io_units_remaining = length;
2458 	ctx->op_type = op_type;
2459 	ctx->seq = seq;
2460 
2461 	_spdk_blob_request_submit_op_split_next(ctx, 0);
2462 }
2463 
2464 static void
2465 _spdk_blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob,
2466 				    void *payload, uint64_t offset, uint64_t length,
2467 				    spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
2468 {
2469 	struct spdk_bs_cpl cpl;
2470 	uint64_t lba;
2471 	uint32_t lba_count;
2472 
2473 	assert(blob != NULL);
2474 
2475 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2476 	cpl.u.blob_basic.cb_fn = cb_fn;
2477 	cpl.u.blob_basic.cb_arg = cb_arg;
2478 
2479 	_spdk_blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
2480 
2481 	if (blob->frozen_refcnt) {
2482 		/* This blob I/O is frozen */
2483 		spdk_bs_user_op_t *op;
2484 		struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
2485 
2486 		op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
2487 		if (!op) {
2488 			cb_fn(cb_arg, -ENOMEM);
2489 			return;
2490 		}
2491 
2492 		TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
2493 
2494 		return;
2495 	}
2496 
2497 	switch (op_type) {
2498 	case SPDK_BLOB_READ: {
2499 		spdk_bs_batch_t *batch;
2500 
2501 		batch = bs_batch_open(_ch, &cpl);
2502 		if (!batch) {
2503 			cb_fn(cb_arg, -ENOMEM);
2504 			return;
2505 		}
2506 
2507 		if (_spdk_bs_io_unit_is_allocated(blob, offset)) {
2508 			/* Read from the blob */
2509 			bs_batch_read_dev(batch, payload, lba, lba_count);
2510 		} else {
2511 			/* Read from the backing block device */
2512 			spdk_bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count);
2513 		}
2514 
2515 		bs_batch_close(batch);
2516 		break;
2517 	}
2518 	case SPDK_BLOB_WRITE:
2519 	case SPDK_BLOB_WRITE_ZEROES: {
2520 		if (_spdk_bs_io_unit_is_allocated(blob, offset)) {
2521 			/* Write to the blob */
2522 			spdk_bs_batch_t *batch;
2523 
2524 			if (lba_count == 0) {
2525 				cb_fn(cb_arg, 0);
2526 				return;
2527 			}
2528 
2529 			batch = bs_batch_open(_ch, &cpl);
2530 			if (!batch) {
2531 				cb_fn(cb_arg, -ENOMEM);
2532 				return;
2533 			}
2534 
2535 			if (op_type == SPDK_BLOB_WRITE) {
2536 				bs_batch_write_dev(batch, payload, lba, lba_count);
2537 			} else {
2538 				bs_batch_write_zeroes_dev(batch, lba, lba_count);
2539 			}
2540 
2541 			bs_batch_close(batch);
2542 		} else {
2543 			/* Queue this operation and allocate the cluster */
2544 			spdk_bs_user_op_t *op;
2545 
2546 			op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
2547 			if (!op) {
2548 				cb_fn(cb_arg, -ENOMEM);
2549 				return;
2550 			}
2551 
2552 			_spdk_bs_allocate_and_copy_cluster(blob, _ch, offset, op);
2553 		}
2554 		break;
2555 	}
2556 	case SPDK_BLOB_UNMAP: {
2557 		spdk_bs_batch_t *batch;
2558 
2559 		batch = bs_batch_open(_ch, &cpl);
2560 		if (!batch) {
2561 			cb_fn(cb_arg, -ENOMEM);
2562 			return;
2563 		}
2564 
2565 		if (_spdk_bs_io_unit_is_allocated(blob, offset)) {
2566 			bs_batch_unmap_dev(batch, lba, lba_count);
2567 		}
2568 
2569 		bs_batch_close(batch);
2570 		break;
2571 	}
2572 	case SPDK_BLOB_READV:
2573 	case SPDK_BLOB_WRITEV:
2574 		SPDK_ERRLOG("readv/write not valid\n");
2575 		cb_fn(cb_arg, -EINVAL);
2576 		break;
2577 	}
2578 }
2579 
2580 static void
2581 _spdk_blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel,
2582 			     void *payload, uint64_t offset, uint64_t length,
2583 			     spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
2584 {
2585 	assert(blob != NULL);
2586 
2587 	if (blob->data_ro && op_type != SPDK_BLOB_READ) {
2588 		cb_fn(cb_arg, -EPERM);
2589 		return;
2590 	}
2591 
2592 	if (offset + length > _spdk_bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
2593 		cb_fn(cb_arg, -EINVAL);
2594 		return;
2595 	}
2596 	if (length <= _spdk_bs_num_io_units_to_cluster_boundary(blob, offset)) {
2597 		_spdk_blob_request_submit_op_single(_channel, blob, payload, offset, length,
2598 						    cb_fn, cb_arg, op_type);
2599 	} else {
2600 		_spdk_blob_request_submit_op_split(_channel, blob, payload, offset, length,
2601 						   cb_fn, cb_arg, op_type);
2602 	}
2603 }
2604 
2605 struct rw_iov_ctx {
2606 	struct spdk_blob *blob;
2607 	struct spdk_io_channel *channel;
2608 	spdk_blob_op_complete cb_fn;
2609 	void *cb_arg;
2610 	bool read;
2611 	int iovcnt;
2612 	struct iovec *orig_iov;
2613 	uint64_t io_unit_offset;
2614 	uint64_t io_units_remaining;
2615 	uint64_t io_units_done;
2616 	struct iovec iov[0];
2617 };
2618 
2619 static void
2620 _spdk_rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
2621 {
2622 	assert(cb_arg == NULL);
2623 	bs_sequence_finish(seq, bserrno);
2624 }
2625 
2626 static void
2627 _spdk_rw_iov_split_next(void *cb_arg, int bserrno)
2628 {
2629 	struct rw_iov_ctx *ctx = cb_arg;
2630 	struct spdk_blob *blob = ctx->blob;
2631 	struct iovec *iov, *orig_iov;
2632 	int iovcnt;
2633 	size_t orig_iovoff;
2634 	uint64_t io_units_count, io_units_to_boundary, io_unit_offset;
2635 	uint64_t byte_count;
2636 
2637 	if (bserrno != 0 || ctx->io_units_remaining == 0) {
2638 		ctx->cb_fn(ctx->cb_arg, bserrno);
2639 		free(ctx);
2640 		return;
2641 	}
2642 
2643 	io_unit_offset = ctx->io_unit_offset;
2644 	io_units_to_boundary = _spdk_bs_num_io_units_to_cluster_boundary(blob, io_unit_offset);
2645 	io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary);
2646 	/*
2647 	 * Get index and offset into the original iov array for our current position in the I/O sequence.
2648 	 *  byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
2649 	 *  point to the current position in the I/O sequence.
2650 	 */
2651 	byte_count = ctx->io_units_done * blob->bs->io_unit_size;
2652 	orig_iov = &ctx->orig_iov[0];
2653 	orig_iovoff = 0;
2654 	while (byte_count > 0) {
2655 		if (byte_count >= orig_iov->iov_len) {
2656 			byte_count -= orig_iov->iov_len;
2657 			orig_iov++;
2658 		} else {
2659 			orig_iovoff = byte_count;
2660 			byte_count = 0;
2661 		}
2662 	}
2663 
2664 	/*
2665 	 * Build an iov array for the next I/O in the sequence.  byte_count will keep track of how many
2666 	 *  bytes of this next I/O remain to be accounted for in the new iov array.
2667 	 */
2668 	byte_count = io_units_count * blob->bs->io_unit_size;
2669 	iov = &ctx->iov[0];
2670 	iovcnt = 0;
2671 	while (byte_count > 0) {
2672 		assert(iovcnt < ctx->iovcnt);
2673 		iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
2674 		iov->iov_base = orig_iov->iov_base + orig_iovoff;
2675 		byte_count -= iov->iov_len;
2676 		orig_iovoff = 0;
2677 		orig_iov++;
2678 		iov++;
2679 		iovcnt++;
2680 	}
2681 
2682 	ctx->io_unit_offset += io_units_count;
2683 	ctx->io_units_remaining -= io_units_count;
2684 	ctx->io_units_done += io_units_count;
2685 	iov = &ctx->iov[0];
2686 
2687 	if (ctx->read) {
2688 		spdk_blob_io_readv(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
2689 				   io_units_count, _spdk_rw_iov_split_next, ctx);
2690 	} else {
2691 		spdk_blob_io_writev(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
2692 				    io_units_count, _spdk_rw_iov_split_next, ctx);
2693 	}
2694 }
2695 
2696 static void
2697 _spdk_blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
2698 				 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
2699 				 spdk_blob_op_complete cb_fn, void *cb_arg, bool read)
2700 {
2701 	struct spdk_bs_cpl	cpl;
2702 
2703 	assert(blob != NULL);
2704 
2705 	if (!read && blob->data_ro) {
2706 		cb_fn(cb_arg, -EPERM);
2707 		return;
2708 	}
2709 
2710 	if (length == 0) {
2711 		cb_fn(cb_arg, 0);
2712 		return;
2713 	}
2714 
2715 	if (offset + length > _spdk_bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
2716 		cb_fn(cb_arg, -EINVAL);
2717 		return;
2718 	}
2719 
2720 	/*
2721 	 * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
2722 	 *  to split a request that spans a cluster boundary.  For I/O that do not span a cluster boundary,
2723 	 *  there will be no noticeable difference compared to using a batch.  For I/O that do span a cluster
2724 	 *  boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
2725 	 *  to allocate a separate iov array and split the I/O such that none of the resulting
2726 	 *  smaller I/O cross a cluster boundary.  These smaller I/O will be issued in sequence (not in parallel)
2727 	 *  but since this case happens very infrequently, any performance impact will be negligible.
2728 	 *
2729 	 * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
2730 	 *  for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
2731 	 *  in a batch.  That would also require creating an intermediate spdk_bs_cpl that would get called
2732 	 *  when the batch was completed, to allow for freeing the memory for the iov arrays.
2733 	 */
2734 	if (spdk_likely(length <= _spdk_bs_num_io_units_to_cluster_boundary(blob, offset))) {
2735 		uint32_t lba_count;
2736 		uint64_t lba;
2737 
2738 		cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
2739 		cpl.u.blob_basic.cb_fn = cb_fn;
2740 		cpl.u.blob_basic.cb_arg = cb_arg;
2741 
2742 		if (blob->frozen_refcnt) {
2743 			/* This blob I/O is frozen */
2744 			enum spdk_blob_op_type op_type;
2745 			spdk_bs_user_op_t *op;
2746 			struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel);
2747 
2748 			op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV;
2749 			op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length);
2750 			if (!op) {
2751 				cb_fn(cb_arg, -ENOMEM);
2752 				return;
2753 			}
2754 
2755 			TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
2756 
2757 			return;
2758 		}
2759 
2760 		_spdk_blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
2761 
2762 		if (read) {
2763 			spdk_bs_sequence_t *seq;
2764 
2765 			seq = bs_sequence_start(_channel, &cpl);
2766 			if (!seq) {
2767 				cb_fn(cb_arg, -ENOMEM);
2768 				return;
2769 			}
2770 
2771 			if (_spdk_bs_io_unit_is_allocated(blob, offset)) {
2772 				bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL);
2773 			} else {
2774 				bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count,
2775 							 _spdk_rw_iov_done, NULL);
2776 			}
2777 		} else {
2778 			if (_spdk_bs_io_unit_is_allocated(blob, offset)) {
2779 				spdk_bs_sequence_t *seq;
2780 
2781 				seq = bs_sequence_start(_channel, &cpl);
2782 				if (!seq) {
2783 					cb_fn(cb_arg, -ENOMEM);
2784 					return;
2785 				}
2786 
2787 				bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, _spdk_rw_iov_done, NULL);
2788 			} else {
2789 				/* Queue this operation and allocate the cluster */
2790 				spdk_bs_user_op_t *op;
2791 
2792 				op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset,
2793 						      length);
2794 				if (!op) {
2795 					cb_fn(cb_arg, -ENOMEM);
2796 					return;
2797 				}
2798 
2799 				_spdk_bs_allocate_and_copy_cluster(blob, _channel, offset, op);
2800 			}
2801 		}
2802 	} else {
2803 		struct rw_iov_ctx *ctx;
2804 
2805 		ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
2806 		if (ctx == NULL) {
2807 			cb_fn(cb_arg, -ENOMEM);
2808 			return;
2809 		}
2810 
2811 		ctx->blob = blob;
2812 		ctx->channel = _channel;
2813 		ctx->cb_fn = cb_fn;
2814 		ctx->cb_arg = cb_arg;
2815 		ctx->read = read;
2816 		ctx->orig_iov = iov;
2817 		ctx->iovcnt = iovcnt;
2818 		ctx->io_unit_offset = offset;
2819 		ctx->io_units_remaining = length;
2820 		ctx->io_units_done = 0;
2821 
2822 		_spdk_rw_iov_split_next(ctx, 0);
2823 	}
2824 }
2825 
2826 static struct spdk_blob *
2827 _spdk_blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
2828 {
2829 	struct spdk_blob *blob;
2830 
2831 	TAILQ_FOREACH(blob, &bs->blobs, link) {
2832 		if (blob->id == blobid) {
2833 			return blob;
2834 		}
2835 	}
2836 
2837 	return NULL;
2838 }
2839 
2840 static void
2841 _spdk_blob_get_snapshot_and_clone_entries(struct spdk_blob *blob,
2842 		struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry)
2843 {
2844 	assert(blob != NULL);
2845 	*snapshot_entry = NULL;
2846 	*clone_entry = NULL;
2847 
2848 	if (blob->parent_id == SPDK_BLOBID_INVALID) {
2849 		return;
2850 	}
2851 
2852 	TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) {
2853 		if ((*snapshot_entry)->id == blob->parent_id) {
2854 			break;
2855 		}
2856 	}
2857 
2858 	if (*snapshot_entry != NULL) {
2859 		TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) {
2860 			if ((*clone_entry)->id == blob->id) {
2861 				break;
2862 			}
2863 		}
2864 
2865 		assert(clone_entry != NULL);
2866 	}
2867 }
2868 
2869 static int
2870 _spdk_bs_channel_create(void *io_device, void *ctx_buf)
2871 {
2872 	struct spdk_blob_store		*bs = io_device;
2873 	struct spdk_bs_channel		*channel = ctx_buf;
2874 	struct spdk_bs_dev		*dev;
2875 	uint32_t			max_ops = bs->max_channel_ops;
2876 	uint32_t			i;
2877 
2878 	dev = bs->dev;
2879 
2880 	channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
2881 	if (!channel->req_mem) {
2882 		return -1;
2883 	}
2884 
2885 	TAILQ_INIT(&channel->reqs);
2886 
2887 	for (i = 0; i < max_ops; i++) {
2888 		TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
2889 	}
2890 
2891 	channel->bs = bs;
2892 	channel->dev = dev;
2893 	channel->dev_channel = dev->create_channel(dev);
2894 
2895 	if (!channel->dev_channel) {
2896 		SPDK_ERRLOG("Failed to create device channel.\n");
2897 		free(channel->req_mem);
2898 		return -1;
2899 	}
2900 
2901 	TAILQ_INIT(&channel->need_cluster_alloc);
2902 	TAILQ_INIT(&channel->queued_io);
2903 
2904 	return 0;
2905 }
2906 
2907 static void
2908 _spdk_bs_channel_destroy(void *io_device, void *ctx_buf)
2909 {
2910 	struct spdk_bs_channel *channel = ctx_buf;
2911 	spdk_bs_user_op_t *op;
2912 
2913 	while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) {
2914 		op = TAILQ_FIRST(&channel->need_cluster_alloc);
2915 		TAILQ_REMOVE(&channel->need_cluster_alloc, op, link);
2916 		bs_user_op_abort(op);
2917 	}
2918 
2919 	while (!TAILQ_EMPTY(&channel->queued_io)) {
2920 		op = TAILQ_FIRST(&channel->queued_io);
2921 		TAILQ_REMOVE(&channel->queued_io, op, link);
2922 		bs_user_op_abort(op);
2923 	}
2924 
2925 	free(channel->req_mem);
2926 	channel->dev->destroy_channel(channel->dev, channel->dev_channel);
2927 }
2928 
2929 static void
2930 _spdk_bs_dev_destroy(void *io_device)
2931 {
2932 	struct spdk_blob_store *bs = io_device;
2933 	struct spdk_blob	*blob, *blob_tmp;
2934 
2935 	bs->dev->destroy(bs->dev);
2936 
2937 	TAILQ_FOREACH_SAFE(blob, &bs->blobs, link, blob_tmp) {
2938 		TAILQ_REMOVE(&bs->blobs, blob, link);
2939 		_spdk_blob_free(blob);
2940 	}
2941 
2942 	pthread_mutex_destroy(&bs->used_clusters_mutex);
2943 
2944 	spdk_bit_array_free(&bs->used_blobids);
2945 	spdk_bit_array_free(&bs->used_md_pages);
2946 	spdk_bit_array_free(&bs->used_clusters);
2947 	/*
2948 	 * If this function is called for any reason except a successful unload,
2949 	 * the unload_cpl type will be NONE and this will be a nop.
2950 	 */
2951 	bs_call_cpl(&bs->unload_cpl, bs->unload_err);
2952 
2953 	free(bs);
2954 }
2955 
2956 static int
2957 _spdk_bs_blob_list_add(struct spdk_blob *blob)
2958 {
2959 	spdk_blob_id snapshot_id;
2960 	struct spdk_blob_list *snapshot_entry = NULL;
2961 	struct spdk_blob_list *clone_entry = NULL;
2962 
2963 	assert(blob != NULL);
2964 
2965 	snapshot_id = blob->parent_id;
2966 	if (snapshot_id == SPDK_BLOBID_INVALID) {
2967 		return 0;
2968 	}
2969 
2970 	snapshot_entry = _spdk_bs_get_snapshot_entry(blob->bs, snapshot_id);
2971 	if (snapshot_entry == NULL) {
2972 		/* Snapshot not found */
2973 		snapshot_entry = calloc(1, sizeof(struct spdk_blob_list));
2974 		if (snapshot_entry == NULL) {
2975 			return -ENOMEM;
2976 		}
2977 		snapshot_entry->id = snapshot_id;
2978 		TAILQ_INIT(&snapshot_entry->clones);
2979 		TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link);
2980 	} else {
2981 		TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
2982 			if (clone_entry->id == blob->id) {
2983 				break;
2984 			}
2985 		}
2986 	}
2987 
2988 	if (clone_entry == NULL) {
2989 		/* Clone not found */
2990 		clone_entry = calloc(1, sizeof(struct spdk_blob_list));
2991 		if (clone_entry == NULL) {
2992 			return -ENOMEM;
2993 		}
2994 		clone_entry->id = blob->id;
2995 		TAILQ_INIT(&clone_entry->clones);
2996 		TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link);
2997 		snapshot_entry->clone_count++;
2998 	}
2999 
3000 	return 0;
3001 }
3002 
3003 static void
3004 _spdk_bs_blob_list_remove(struct spdk_blob *blob)
3005 {
3006 	struct spdk_blob_list *snapshot_entry = NULL;
3007 	struct spdk_blob_list *clone_entry = NULL;
3008 
3009 	_spdk_blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry);
3010 
3011 	if (snapshot_entry == NULL) {
3012 		return;
3013 	}
3014 
3015 	blob->parent_id = SPDK_BLOBID_INVALID;
3016 	TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
3017 	free(clone_entry);
3018 
3019 	snapshot_entry->clone_count--;
3020 }
3021 
3022 static int
3023 _spdk_bs_blob_list_free(struct spdk_blob_store *bs)
3024 {
3025 	struct spdk_blob_list *snapshot_entry;
3026 	struct spdk_blob_list *snapshot_entry_tmp;
3027 	struct spdk_blob_list *clone_entry;
3028 	struct spdk_blob_list *clone_entry_tmp;
3029 
3030 	TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) {
3031 		TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) {
3032 			TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
3033 			free(clone_entry);
3034 		}
3035 		TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link);
3036 		free(snapshot_entry);
3037 	}
3038 
3039 	return 0;
3040 }
3041 
3042 static void
3043 _spdk_bs_free(struct spdk_blob_store *bs)
3044 {
3045 	_spdk_bs_blob_list_free(bs);
3046 
3047 	spdk_bs_unregister_md_thread(bs);
3048 	spdk_io_device_unregister(bs, _spdk_bs_dev_destroy);
3049 }
3050 
3051 void
3052 spdk_bs_opts_init(struct spdk_bs_opts *opts)
3053 {
3054 	opts->cluster_sz = SPDK_BLOB_OPTS_CLUSTER_SZ;
3055 	opts->num_md_pages = SPDK_BLOB_OPTS_NUM_MD_PAGES;
3056 	opts->max_md_ops = SPDK_BLOB_OPTS_MAX_MD_OPS;
3057 	opts->max_channel_ops = SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS;
3058 	opts->clear_method = BS_CLEAR_WITH_UNMAP;
3059 	memset(&opts->bstype, 0, sizeof(opts->bstype));
3060 	opts->iter_cb_fn = NULL;
3061 	opts->iter_cb_arg = NULL;
3062 }
3063 
3064 static int
3065 _spdk_bs_opts_verify(struct spdk_bs_opts *opts)
3066 {
3067 	if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
3068 	    opts->max_channel_ops == 0) {
3069 		SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
3070 		return -1;
3071 	}
3072 
3073 	return 0;
3074 }
3075 
3076 static int
3077 _spdk_bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs)
3078 {
3079 	struct spdk_blob_store	*bs;
3080 	uint64_t dev_size;
3081 	int rc;
3082 
3083 	dev_size = dev->blocklen * dev->blockcnt;
3084 	if (dev_size < opts->cluster_sz) {
3085 		/* Device size cannot be smaller than cluster size of blobstore */
3086 		SPDK_INFOLOG(SPDK_LOG_BLOB, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n",
3087 			     dev_size, opts->cluster_sz);
3088 		return -ENOSPC;
3089 	}
3090 	if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
3091 		/* Cluster size cannot be smaller than page size */
3092 		SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n",
3093 			    opts->cluster_sz, SPDK_BS_PAGE_SIZE);
3094 		return -EINVAL;
3095 	}
3096 	bs = calloc(1, sizeof(struct spdk_blob_store));
3097 	if (!bs) {
3098 		return -ENOMEM;
3099 	}
3100 
3101 	TAILQ_INIT(&bs->blobs);
3102 	TAILQ_INIT(&bs->snapshots);
3103 	bs->dev = dev;
3104 	bs->md_thread = spdk_get_thread();
3105 	assert(bs->md_thread != NULL);
3106 
3107 	/*
3108 	 * Do not use _spdk_bs_lba_to_cluster() here since blockcnt may not be an
3109 	 *  even multiple of the cluster size.
3110 	 */
3111 	bs->cluster_sz = opts->cluster_sz;
3112 	bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
3113 	bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
3114 	if (spdk_u32_is_pow2(bs->pages_per_cluster)) {
3115 		bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster);
3116 	}
3117 	bs->num_free_clusters = bs->total_clusters;
3118 	bs->used_clusters = spdk_bit_array_create(bs->total_clusters);
3119 	bs->io_unit_size = dev->blocklen;
3120 	if (bs->used_clusters == NULL) {
3121 		free(bs);
3122 		return -ENOMEM;
3123 	}
3124 
3125 	bs->max_channel_ops = opts->max_channel_ops;
3126 	bs->super_blob = SPDK_BLOBID_INVALID;
3127 	memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
3128 
3129 	/* The metadata is assumed to be at least 1 page */
3130 	bs->used_md_pages = spdk_bit_array_create(1);
3131 	bs->used_blobids = spdk_bit_array_create(0);
3132 
3133 	pthread_mutex_init(&bs->used_clusters_mutex, NULL);
3134 
3135 	spdk_io_device_register(bs, _spdk_bs_channel_create, _spdk_bs_channel_destroy,
3136 				sizeof(struct spdk_bs_channel), "blobstore");
3137 	rc = spdk_bs_register_md_thread(bs);
3138 	if (rc == -1) {
3139 		spdk_io_device_unregister(bs, NULL);
3140 		pthread_mutex_destroy(&bs->used_clusters_mutex);
3141 		spdk_bit_array_free(&bs->used_blobids);
3142 		spdk_bit_array_free(&bs->used_md_pages);
3143 		spdk_bit_array_free(&bs->used_clusters);
3144 		free(bs);
3145 		/* FIXME: this is a lie but don't know how to get a proper error code here */
3146 		return -ENOMEM;
3147 	}
3148 
3149 	*_bs = bs;
3150 	return 0;
3151 }
3152 
3153 /* START spdk_bs_load, spdk_bs_load_ctx will used for both load and unload. */
3154 
3155 struct spdk_bs_load_ctx {
3156 	struct spdk_blob_store		*bs;
3157 	struct spdk_bs_super_block	*super;
3158 
3159 	struct spdk_bs_md_mask		*mask;
3160 	bool				in_page_chain;
3161 	uint32_t			page_index;
3162 	uint32_t			cur_page;
3163 	struct spdk_blob_md_page	*page;
3164 
3165 	uint64_t			num_extent_pages;
3166 	uint32_t			*extent_page_num;
3167 	struct spdk_blob_md_page	*extent_pages;
3168 
3169 	spdk_bs_sequence_t			*seq;
3170 	spdk_blob_op_with_handle_complete	iter_cb_fn;
3171 	void					*iter_cb_arg;
3172 	struct spdk_blob			*blob;
3173 	spdk_blob_id				blobid;
3174 };
3175 
3176 static void
3177 _spdk_bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno)
3178 {
3179 	assert(bserrno != 0);
3180 
3181 	spdk_free(ctx->super);
3182 	bs_sequence_finish(ctx->seq, bserrno);
3183 	_spdk_bs_free(ctx->bs);
3184 	free(ctx);
3185 }
3186 
3187 static void
3188 _spdk_bs_set_mask(struct spdk_bit_array *array, struct spdk_bs_md_mask *mask)
3189 {
3190 	uint32_t i = 0;
3191 
3192 	while (true) {
3193 		i = spdk_bit_array_find_first_set(array, i);
3194 		if (i >= mask->length) {
3195 			break;
3196 		}
3197 		mask->mask[i / 8] |= 1U << (i % 8);
3198 		i++;
3199 	}
3200 }
3201 
3202 static int
3203 _spdk_bs_load_mask(struct spdk_bit_array **array_ptr, struct spdk_bs_md_mask *mask)
3204 {
3205 	struct spdk_bit_array *array;
3206 	uint32_t i;
3207 
3208 	if (spdk_bit_array_resize(array_ptr, mask->length) < 0) {
3209 		return -ENOMEM;
3210 	}
3211 
3212 	array = *array_ptr;
3213 	for (i = 0; i < mask->length; i++) {
3214 		if (mask->mask[i / 8] & (1U << (i % 8))) {
3215 			spdk_bit_array_set(array, i);
3216 		}
3217 	}
3218 
3219 	return 0;
3220 }
3221 
3222 static void
3223 _spdk_bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
3224 		     struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
3225 {
3226 	/* Update the values in the super block */
3227 	super->super_blob = bs->super_blob;
3228 	memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
3229 	super->crc = _spdk_blob_md_page_calc_crc(super);
3230 	bs_sequence_write_dev(seq, super, _spdk_bs_page_to_lba(bs, 0),
3231 			      _spdk_bs_byte_to_lba(bs, sizeof(*super)),
3232 			      cb_fn, cb_arg);
3233 }
3234 
3235 static void
3236 _spdk_bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
3237 {
3238 	struct spdk_bs_load_ctx	*ctx = arg;
3239 	uint64_t	mask_size, lba, lba_count;
3240 
3241 	/* Write out the used clusters mask */
3242 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
3243 	ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
3244 				 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
3245 	if (!ctx->mask) {
3246 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3247 		return;
3248 	}
3249 
3250 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
3251 	ctx->mask->length = ctx->bs->total_clusters;
3252 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_clusters));
3253 
3254 	_spdk_bs_set_mask(ctx->bs->used_clusters, ctx->mask);
3255 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
3256 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
3257 	bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
3258 }
3259 
3260 static void
3261 _spdk_bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
3262 {
3263 	struct spdk_bs_load_ctx	*ctx = arg;
3264 	uint64_t	mask_size, lba, lba_count;
3265 
3266 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
3267 	ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
3268 				 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
3269 	if (!ctx->mask) {
3270 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3271 		return;
3272 	}
3273 
3274 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
3275 	ctx->mask->length = ctx->super->md_len;
3276 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
3277 
3278 	_spdk_bs_set_mask(ctx->bs->used_md_pages, ctx->mask);
3279 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
3280 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
3281 	bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
3282 }
3283 
3284 static void
3285 _spdk_bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
3286 {
3287 	struct spdk_bs_load_ctx	*ctx = arg;
3288 	uint64_t	mask_size, lba, lba_count;
3289 
3290 	if (ctx->super->used_blobid_mask_len == 0) {
3291 		/*
3292 		 * This is a pre-v3 on-disk format where the blobid mask does not get
3293 		 *  written to disk.
3294 		 */
3295 		cb_fn(seq, arg, 0);
3296 		return;
3297 	}
3298 
3299 	mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
3300 	ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
3301 				 SPDK_MALLOC_DMA);
3302 	if (!ctx->mask) {
3303 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3304 		return;
3305 	}
3306 
3307 	ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS;
3308 	ctx->mask->length = ctx->super->md_len;
3309 	assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids));
3310 
3311 	_spdk_bs_set_mask(ctx->bs->used_blobids, ctx->mask);
3312 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
3313 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
3314 	bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
3315 }
3316 
3317 static void
3318 _spdk_blob_set_thin_provision(struct spdk_blob *blob)
3319 {
3320 	_spdk_blob_verify_md_op(blob);
3321 	blob->invalid_flags |= SPDK_BLOB_THIN_PROV;
3322 	blob->state = SPDK_BLOB_STATE_DIRTY;
3323 }
3324 
3325 static void
3326 _spdk_blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method)
3327 {
3328 	_spdk_blob_verify_md_op(blob);
3329 	blob->clear_method = clear_method;
3330 	blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT);
3331 	blob->state = SPDK_BLOB_STATE_DIRTY;
3332 }
3333 
3334 static void _spdk_bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno);
3335 
3336 static void
3337 _spdk_bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno)
3338 {
3339 	struct spdk_bs_load_ctx *ctx = cb_arg;
3340 	spdk_blob_id id;
3341 	int64_t page_num;
3342 
3343 	/* Iterate to next blob (we can't use spdk_bs_iter_next function as our
3344 	 * last blob has been removed */
3345 	page_num = _spdk_bs_blobid_to_page(ctx->blobid);
3346 	page_num++;
3347 	page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num);
3348 	if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) {
3349 		_spdk_bs_load_iter(ctx, NULL, -ENOENT);
3350 		return;
3351 	}
3352 
3353 	id = _spdk_bs_page_to_blobid(page_num);
3354 
3355 	spdk_bs_open_blob(ctx->bs, id, _spdk_bs_load_iter, ctx);
3356 }
3357 
3358 static void
3359 _spdk_bs_delete_corrupted_close_cb(void *cb_arg, int bserrno)
3360 {
3361 	struct spdk_bs_load_ctx *ctx = cb_arg;
3362 
3363 	if (bserrno != 0) {
3364 		SPDK_ERRLOG("Failed to close corrupted blob\n");
3365 		spdk_bs_iter_next(ctx->bs, ctx->blob, _spdk_bs_load_iter, ctx);
3366 		return;
3367 	}
3368 
3369 	spdk_bs_delete_blob(ctx->bs, ctx->blobid, _spdk_bs_delete_corrupted_blob_cpl, ctx);
3370 }
3371 
3372 static void
3373 _spdk_bs_delete_corrupted_blob(void *cb_arg, int bserrno)
3374 {
3375 	struct spdk_bs_load_ctx *ctx = cb_arg;
3376 	uint64_t i;
3377 
3378 	if (bserrno != 0) {
3379 		SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
3380 		spdk_bs_iter_next(ctx->bs, ctx->blob, _spdk_bs_load_iter, ctx);
3381 		return;
3382 	}
3383 
3384 	/* Snapshot and clone have the same copy of cluster map and extent pages
3385 	 * at this point. Let's clear both for snpashot now,
3386 	 * so that it won't be cleared for clone later when we remove snapshot.
3387 	 * Also set thin provision to pass data corruption check */
3388 	for (i = 0; i < ctx->blob->active.num_clusters; i++) {
3389 		ctx->blob->active.clusters[i] = 0;
3390 	}
3391 	for (i = 0; i < ctx->blob->active.num_extent_pages; i++) {
3392 		ctx->blob->active.extent_pages[i] = 0;
3393 	}
3394 
3395 	ctx->blob->md_ro = false;
3396 
3397 	_spdk_blob_set_thin_provision(ctx->blob);
3398 
3399 	ctx->blobid = ctx->blob->id;
3400 
3401 	spdk_blob_close(ctx->blob, _spdk_bs_delete_corrupted_close_cb, ctx);
3402 }
3403 
3404 static void
3405 _spdk_bs_update_corrupted_blob(void *cb_arg, int bserrno)
3406 {
3407 	struct spdk_bs_load_ctx *ctx = cb_arg;
3408 
3409 	if (bserrno != 0) {
3410 		SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
3411 		spdk_bs_iter_next(ctx->bs, ctx->blob, _spdk_bs_load_iter, ctx);
3412 		return;
3413 	}
3414 
3415 	ctx->blob->md_ro = false;
3416 	_spdk_blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true);
3417 	_spdk_blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true);
3418 	spdk_blob_set_read_only(ctx->blob);
3419 
3420 	if (ctx->iter_cb_fn) {
3421 		ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0);
3422 	}
3423 	_spdk_bs_blob_list_add(ctx->blob);
3424 
3425 	spdk_bs_iter_next(ctx->bs, ctx->blob, _spdk_bs_load_iter, ctx);
3426 }
3427 
3428 static void
3429 _spdk_bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno)
3430 {
3431 	struct spdk_bs_load_ctx *ctx = cb_arg;
3432 
3433 	if (bserrno != 0) {
3434 		SPDK_ERRLOG("Failed to open clone of a corrupted blob\n");
3435 		spdk_bs_iter_next(ctx->bs, ctx->blob, _spdk_bs_load_iter, ctx);
3436 		return;
3437 	}
3438 
3439 	if (blob->parent_id == ctx->blob->id) {
3440 		/* Power failure occured before updating clone (snapshot delete case)
3441 		 * or after updating clone (creating snapshot case) - keep snapshot */
3442 		spdk_blob_close(blob, _spdk_bs_update_corrupted_blob, ctx);
3443 	} else {
3444 		/* Power failure occured after updating clone (snapshot delete case)
3445 		 * or before updating clone (creating snapshot case) - remove snapshot */
3446 		spdk_blob_close(blob, _spdk_bs_delete_corrupted_blob, ctx);
3447 	}
3448 }
3449 
3450 static void
3451 _spdk_bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno)
3452 {
3453 	struct spdk_bs_load_ctx *ctx = arg;
3454 	const void *value;
3455 	size_t len;
3456 	int rc = 0;
3457 
3458 	if (bserrno == 0) {
3459 		/* Examine blob if it is corrupted after power failure. Fix
3460 		 * the ones that can be fixed and remove any other corrupted
3461 		 * ones. If it is not corrupted just process it */
3462 		rc = _spdk_blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true);
3463 		if (rc != 0) {
3464 			rc = _spdk_blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true);
3465 			if (rc != 0) {
3466 				/* Not corrupted - process it and continue with iterating through blobs */
3467 				if (ctx->iter_cb_fn) {
3468 					ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0);
3469 				}
3470 				_spdk_bs_blob_list_add(blob);
3471 				spdk_bs_iter_next(ctx->bs, blob, _spdk_bs_load_iter, ctx);
3472 				return;
3473 			}
3474 
3475 		}
3476 
3477 		assert(len == sizeof(spdk_blob_id));
3478 
3479 		ctx->blob = blob;
3480 
3481 		/* Open clone to check if we are able to fix this blob or should we remove it */
3482 		spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, _spdk_bs_examine_clone, ctx);
3483 		return;
3484 	} else if (bserrno == -ENOENT) {
3485 		bserrno = 0;
3486 	} else {
3487 		/*
3488 		 * This case needs to be looked at further.  Same problem
3489 		 *  exists with applications that rely on explicit blob
3490 		 *  iteration.  We should just skip the blob that failed
3491 		 *  to load and continue on to the next one.
3492 		 */
3493 		SPDK_ERRLOG("Error in iterating blobs\n");
3494 	}
3495 
3496 	ctx->iter_cb_fn = NULL;
3497 
3498 	spdk_free(ctx->super);
3499 	spdk_free(ctx->mask);
3500 	bs_sequence_finish(ctx->seq, bserrno);
3501 	free(ctx);
3502 }
3503 
3504 static void
3505 _spdk_bs_load_complete(struct spdk_bs_load_ctx *ctx)
3506 {
3507 	spdk_bs_iter_first(ctx->bs, _spdk_bs_load_iter, ctx);
3508 }
3509 
3510 static void
3511 _spdk_bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3512 {
3513 	struct spdk_bs_load_ctx *ctx = cb_arg;
3514 	int rc;
3515 
3516 	/* The type must be correct */
3517 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS);
3518 
3519 	/* The length of the mask (in bits) must not be greater than
3520 	 * the length of the buffer (converted to bits) */
3521 	assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8));
3522 
3523 	/* The length of the mask must be exactly equal to the size
3524 	 * (in pages) of the metadata region */
3525 	assert(ctx->mask->length == ctx->super->md_len);
3526 
3527 	rc = _spdk_bs_load_mask(&ctx->bs->used_blobids, ctx->mask);
3528 	if (rc < 0) {
3529 		spdk_free(ctx->mask);
3530 		_spdk_bs_load_ctx_fail(ctx, rc);
3531 		return;
3532 	}
3533 
3534 	_spdk_bs_load_complete(ctx);
3535 }
3536 
3537 static void
3538 _spdk_bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3539 {
3540 	struct spdk_bs_load_ctx *ctx = cb_arg;
3541 	uint64_t		lba, lba_count, mask_size;
3542 	int			rc;
3543 
3544 	if (bserrno != 0) {
3545 		_spdk_bs_load_ctx_fail(ctx, bserrno);
3546 		return;
3547 	}
3548 
3549 	/* The type must be correct */
3550 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
3551 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
3552 	assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
3553 					     struct spdk_blob_md_page) * 8));
3554 	/* The length of the mask must be exactly equal to the total number of clusters */
3555 	assert(ctx->mask->length == ctx->bs->total_clusters);
3556 
3557 	rc = _spdk_bs_load_mask(&ctx->bs->used_clusters, ctx->mask);
3558 	if (rc < 0) {
3559 		spdk_free(ctx->mask);
3560 		_spdk_bs_load_ctx_fail(ctx, rc);
3561 		return;
3562 	}
3563 
3564 	ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->bs->used_clusters);
3565 	assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
3566 
3567 	spdk_free(ctx->mask);
3568 
3569 	/* Read the used blobids mask */
3570 	mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
3571 	ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
3572 				 SPDK_MALLOC_DMA);
3573 	if (!ctx->mask) {
3574 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3575 		return;
3576 	}
3577 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
3578 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
3579 	bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
3580 			     _spdk_bs_load_used_blobids_cpl, ctx);
3581 }
3582 
3583 static void
3584 _spdk_bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3585 {
3586 	struct spdk_bs_load_ctx *ctx = cb_arg;
3587 	uint64_t		lba, lba_count, mask_size;
3588 	int			rc;
3589 
3590 	if (bserrno != 0) {
3591 		_spdk_bs_load_ctx_fail(ctx, bserrno);
3592 		return;
3593 	}
3594 
3595 	/* The type must be correct */
3596 	assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
3597 	/* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
3598 	assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
3599 				     8));
3600 	/* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
3601 	assert(ctx->mask->length == ctx->super->md_len);
3602 
3603 	rc = _spdk_bs_load_mask(&ctx->bs->used_md_pages, ctx->mask);
3604 	if (rc < 0) {
3605 		spdk_free(ctx->mask);
3606 		_spdk_bs_load_ctx_fail(ctx, rc);
3607 		return;
3608 	}
3609 
3610 	spdk_free(ctx->mask);
3611 
3612 	/* Read the used clusters mask */
3613 	mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
3614 	ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
3615 				 SPDK_MALLOC_DMA);
3616 	if (!ctx->mask) {
3617 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3618 		return;
3619 	}
3620 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
3621 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
3622 	bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
3623 			     _spdk_bs_load_used_clusters_cpl, ctx);
3624 }
3625 
3626 static void
3627 _spdk_bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx)
3628 {
3629 	uint64_t lba, lba_count, mask_size;
3630 
3631 	/* Read the used pages mask */
3632 	mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
3633 	ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
3634 				 SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
3635 	if (!ctx->mask) {
3636 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3637 		return;
3638 	}
3639 
3640 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
3641 	lba_count = _spdk_bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
3642 	bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
3643 			     _spdk_bs_load_used_pages_cpl, ctx);
3644 }
3645 
3646 static int
3647 _spdk_bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page)
3648 {
3649 	struct spdk_blob_store *bs = ctx->bs;
3650 	struct spdk_blob_md_descriptor *desc;
3651 	size_t	cur_desc = 0;
3652 
3653 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
3654 	while (cur_desc < sizeof(page->descriptors)) {
3655 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
3656 			if (desc->length == 0) {
3657 				/* If padding and length are 0, this terminates the page */
3658 				break;
3659 			}
3660 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
3661 			struct spdk_blob_md_descriptor_extent_rle	*desc_extent_rle;
3662 			unsigned int				i, j;
3663 			unsigned int				cluster_count = 0;
3664 			uint32_t				cluster_idx;
3665 
3666 			desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
3667 
3668 			for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
3669 				for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
3670 					cluster_idx = desc_extent_rle->extents[i].cluster_idx;
3671 					/*
3672 					 * cluster_idx = 0 means an unallocated cluster - don't mark that
3673 					 * in the used cluster map.
3674 					 */
3675 					if (cluster_idx != 0) {
3676 						spdk_bit_array_set(bs->used_clusters, cluster_idx + j);
3677 						if (bs->num_free_clusters == 0) {
3678 							return -ENOSPC;
3679 						}
3680 						bs->num_free_clusters--;
3681 					}
3682 					cluster_count++;
3683 				}
3684 			}
3685 			if (cluster_count == 0) {
3686 				return -EINVAL;
3687 			}
3688 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
3689 			struct spdk_blob_md_descriptor_extent_page	*desc_extent;
3690 			uint32_t					i;
3691 			uint32_t					cluster_count = 0;
3692 			uint32_t					cluster_idx;
3693 			size_t						cluster_idx_length;
3694 
3695 			desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
3696 			cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
3697 
3698 			if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
3699 			    (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
3700 				return -EINVAL;
3701 			}
3702 
3703 			for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
3704 				cluster_idx = desc_extent->cluster_idx[i];
3705 				/*
3706 				 * cluster_idx = 0 means an unallocated cluster - don't mark that
3707 				 * in the used cluster map.
3708 				 */
3709 				if (cluster_idx != 0) {
3710 					if (cluster_idx < desc_extent->start_cluster_idx &&
3711 					    cluster_idx >= desc_extent->start_cluster_idx + cluster_count) {
3712 						return -EINVAL;
3713 					}
3714 					spdk_bit_array_set(bs->used_clusters, cluster_idx);
3715 					if (bs->num_free_clusters == 0) {
3716 						return -ENOSPC;
3717 					}
3718 					bs->num_free_clusters--;
3719 				}
3720 				cluster_count++;
3721 			}
3722 
3723 			if (cluster_count == 0) {
3724 				return -EINVAL;
3725 			}
3726 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
3727 			/* Skip this item */
3728 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
3729 			/* Skip this item */
3730 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
3731 			/* Skip this item */
3732 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
3733 			struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
3734 			uint32_t num_extent_pages = ctx->num_extent_pages;
3735 			uint32_t i;
3736 			size_t extent_pages_length;
3737 			void *tmp;
3738 
3739 			desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
3740 			extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
3741 
3742 			if (desc_extent_table->length == 0 ||
3743 			    (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
3744 				return -EINVAL;
3745 			}
3746 
3747 			for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
3748 				if (desc_extent_table->extent_page[i].page_idx != 0) {
3749 					if (desc_extent_table->extent_page[i].num_pages != 1) {
3750 						return -EINVAL;
3751 					}
3752 					num_extent_pages += 1;
3753 				}
3754 			}
3755 
3756 			if (num_extent_pages > 0) {
3757 				tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t));
3758 				if (tmp == NULL) {
3759 					return -ENOMEM;
3760 				}
3761 				ctx->extent_page_num = tmp;
3762 
3763 				/* Extent table entries contain md page numbers for extent pages.
3764 				 * Zeroes represent unallocated extent pages, those are run-length-encoded.
3765 				 */
3766 				for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
3767 					if (desc_extent_table->extent_page[i].page_idx != 0) {
3768 						ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx;
3769 						ctx->num_extent_pages += 1;
3770 					}
3771 				}
3772 			}
3773 		} else {
3774 			/* Error */
3775 			return -EINVAL;
3776 		}
3777 		/* Advance to the next descriptor */
3778 		cur_desc += sizeof(*desc) + desc->length;
3779 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
3780 			break;
3781 		}
3782 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
3783 	}
3784 	return 0;
3785 }
3786 
3787 static bool _spdk_bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page)
3788 {
3789 	uint32_t crc;
3790 	struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors;
3791 	size_t desc_len;
3792 
3793 	crc = _spdk_blob_md_page_calc_crc(page);
3794 	if (crc != page->crc) {
3795 		return false;
3796 	}
3797 
3798 	/* Extent page should always be of sequence num 0. */
3799 	if (page->sequence_num != 0) {
3800 		return false;
3801 	}
3802 
3803 	/* Descriptor type must be EXTENT_PAGE. */
3804 	if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
3805 		return false;
3806 	}
3807 
3808 	/* Descriptor length cannot exceed the page. */
3809 	desc_len = sizeof(*desc) + desc->length;
3810 	if (desc_len > sizeof(page->descriptors)) {
3811 		return false;
3812 	}
3813 
3814 	/* It has to be the only descriptor in the page. */
3815 	if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) {
3816 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len);
3817 		if (desc->length != 0) {
3818 			return false;
3819 		}
3820 	}
3821 
3822 	return true;
3823 }
3824 
3825 static bool _spdk_bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
3826 {
3827 	uint32_t crc;
3828 	struct spdk_blob_md_page *page = ctx->page;
3829 
3830 	crc = _spdk_blob_md_page_calc_crc(page);
3831 	if (crc != page->crc) {
3832 		return false;
3833 	}
3834 
3835 	/* First page of a sequence should match the blobid. */
3836 	if (page->sequence_num == 0 &&
3837 	    _spdk_bs_page_to_blobid(ctx->cur_page) != page->id) {
3838 		return false;
3839 	}
3840 	assert(_spdk_bs_load_cur_extent_page_valid(page) == false);
3841 
3842 	return true;
3843 }
3844 
3845 static void
3846 _spdk_bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx);
3847 
3848 static void
3849 _spdk_bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3850 {
3851 	struct spdk_bs_load_ctx	*ctx = cb_arg;
3852 
3853 	if (bserrno != 0) {
3854 		_spdk_bs_load_ctx_fail(ctx, bserrno);
3855 		return;
3856 	}
3857 
3858 	_spdk_bs_load_complete(ctx);
3859 }
3860 
3861 static void
3862 _spdk_bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3863 {
3864 	struct spdk_bs_load_ctx	*ctx = cb_arg;
3865 
3866 	spdk_free(ctx->mask);
3867 	ctx->mask = NULL;
3868 
3869 	if (bserrno != 0) {
3870 		_spdk_bs_load_ctx_fail(ctx, bserrno);
3871 		return;
3872 	}
3873 
3874 	_spdk_bs_write_used_clusters(seq, ctx, _spdk_bs_load_write_used_clusters_cpl);
3875 }
3876 
3877 static void
3878 _spdk_bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3879 {
3880 	struct spdk_bs_load_ctx	*ctx = cb_arg;
3881 
3882 	spdk_free(ctx->mask);
3883 	ctx->mask = NULL;
3884 
3885 	if (bserrno != 0) {
3886 		_spdk_bs_load_ctx_fail(ctx, bserrno);
3887 		return;
3888 	}
3889 
3890 	_spdk_bs_write_used_blobids(seq, ctx, _spdk_bs_load_write_used_blobids_cpl);
3891 }
3892 
3893 static void
3894 _spdk_bs_load_write_used_md(struct spdk_bs_load_ctx *ctx)
3895 {
3896 	_spdk_bs_write_used_md(ctx->seq, ctx, _spdk_bs_load_write_used_pages_cpl);
3897 }
3898 
3899 static void
3900 _spdk_bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx)
3901 {
3902 	uint64_t num_md_clusters;
3903 	uint64_t i;
3904 
3905 	ctx->in_page_chain = false;
3906 
3907 	do {
3908 		ctx->page_index++;
3909 	} while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
3910 
3911 	if (ctx->page_index < ctx->super->md_len) {
3912 		ctx->cur_page = ctx->page_index;
3913 		_spdk_bs_load_replay_cur_md_page(ctx);
3914 	} else {
3915 		/* Claim all of the clusters used by the metadata */
3916 		num_md_clusters = spdk_divide_round_up(ctx->super->md_len, ctx->bs->pages_per_cluster);
3917 		for (i = 0; i < num_md_clusters; i++) {
3918 			_spdk_bs_claim_cluster(ctx->bs, i);
3919 		}
3920 		spdk_free(ctx->page);
3921 		_spdk_bs_load_write_used_md(ctx);
3922 	}
3923 }
3924 
3925 static void
3926 _spdk_bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3927 {
3928 	struct spdk_bs_load_ctx *ctx = cb_arg;
3929 	uint32_t page_num;
3930 	uint64_t i;
3931 
3932 	if (bserrno != 0) {
3933 		spdk_free(ctx->extent_pages);
3934 		_spdk_bs_load_ctx_fail(ctx, bserrno);
3935 		return;
3936 	}
3937 
3938 	for (i = 0; i < ctx->num_extent_pages; i++) {
3939 		/* Extent pages are only read when present within in chain md.
3940 		 * Integrity of md is not right if that page was not a valid extent page. */
3941 		if (_spdk_bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) {
3942 			spdk_free(ctx->extent_pages);
3943 			_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
3944 			return;
3945 		}
3946 
3947 		page_num = ctx->extent_page_num[i];
3948 		spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
3949 		if (_spdk_bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) {
3950 			spdk_free(ctx->extent_pages);
3951 			_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
3952 			return;
3953 		}
3954 	}
3955 
3956 	spdk_free(ctx->extent_pages);
3957 	free(ctx->extent_page_num);
3958 	ctx->extent_page_num = NULL;
3959 	ctx->num_extent_pages = 0;
3960 
3961 	_spdk_bs_load_replay_md_chain_cpl(ctx);
3962 }
3963 
3964 static void
3965 _spdk_bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx)
3966 {
3967 	spdk_bs_batch_t *batch;
3968 	uint32_t page;
3969 	uint64_t lba;
3970 	uint64_t i;
3971 
3972 	ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, SPDK_BS_PAGE_SIZE,
3973 					 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
3974 	if (!ctx->extent_pages) {
3975 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
3976 		return;
3977 	}
3978 
3979 	batch = bs_sequence_to_batch(ctx->seq, _spdk_bs_load_replay_extent_page_cpl, ctx);
3980 
3981 	for (i = 0; i < ctx->num_extent_pages; i++) {
3982 		page = ctx->extent_page_num[i];
3983 		assert(page < ctx->super->md_len);
3984 		lba = _spdk_bs_md_page_to_lba(ctx->bs, page);
3985 		bs_batch_read_dev(batch, &ctx->extent_pages[i], lba,
3986 				  _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE));
3987 	}
3988 
3989 	bs_batch_close(batch);
3990 }
3991 
3992 static void
3993 _spdk_bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
3994 {
3995 	struct spdk_bs_load_ctx *ctx = cb_arg;
3996 	uint32_t page_num;
3997 	struct spdk_blob_md_page *page;
3998 
3999 	if (bserrno != 0) {
4000 		_spdk_bs_load_ctx_fail(ctx, bserrno);
4001 		return;
4002 	}
4003 
4004 	page_num = ctx->cur_page;
4005 	page = ctx->page;
4006 	if (_spdk_bs_load_cur_md_page_valid(ctx) == true) {
4007 		if (page->sequence_num == 0 || ctx->in_page_chain == true) {
4008 			_spdk_bs_claim_md_page(ctx->bs, page_num);
4009 			if (page->sequence_num == 0) {
4010 				spdk_bit_array_set(ctx->bs->used_blobids, page_num);
4011 			}
4012 			if (_spdk_bs_load_replay_md_parse_page(ctx, page)) {
4013 				_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
4014 				return;
4015 			}
4016 			if (page->next != SPDK_INVALID_MD_PAGE) {
4017 				ctx->in_page_chain = true;
4018 				ctx->cur_page = page->next;
4019 				_spdk_bs_load_replay_cur_md_page(ctx);
4020 				return;
4021 			}
4022 			if (ctx->num_extent_pages != 0) {
4023 				_spdk_bs_load_replay_extent_pages(ctx);
4024 				return;
4025 			}
4026 		}
4027 	}
4028 	_spdk_bs_load_replay_md_chain_cpl(ctx);
4029 }
4030 
4031 static void
4032 _spdk_bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx)
4033 {
4034 	uint64_t lba;
4035 
4036 	assert(ctx->cur_page < ctx->super->md_len);
4037 	lba = _spdk_bs_md_page_to_lba(ctx->bs, ctx->cur_page);
4038 	bs_sequence_read_dev(ctx->seq, ctx->page, lba,
4039 			     _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
4040 			     _spdk_bs_load_replay_md_cpl, ctx);
4041 }
4042 
4043 static void
4044 _spdk_bs_load_replay_md(struct spdk_bs_load_ctx *ctx)
4045 {
4046 	ctx->page_index = 0;
4047 	ctx->cur_page = 0;
4048 	ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, SPDK_BS_PAGE_SIZE,
4049 				 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4050 	if (!ctx->page) {
4051 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
4052 		return;
4053 	}
4054 	_spdk_bs_load_replay_cur_md_page(ctx);
4055 }
4056 
4057 static void
4058 _spdk_bs_recover(struct spdk_bs_load_ctx *ctx)
4059 {
4060 	int		rc;
4061 
4062 	rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
4063 	if (rc < 0) {
4064 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
4065 		return;
4066 	}
4067 
4068 	rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len);
4069 	if (rc < 0) {
4070 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
4071 		return;
4072 	}
4073 
4074 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
4075 	if (rc < 0) {
4076 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
4077 		return;
4078 	}
4079 
4080 	ctx->bs->num_free_clusters = ctx->bs->total_clusters;
4081 	_spdk_bs_load_replay_md(ctx);
4082 }
4083 
4084 static void
4085 _spdk_bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4086 {
4087 	struct spdk_bs_load_ctx *ctx = cb_arg;
4088 	uint32_t	crc;
4089 	int		rc;
4090 	static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
4091 
4092 	if (ctx->super->version > SPDK_BS_VERSION ||
4093 	    ctx->super->version < SPDK_BS_INITIAL_VERSION) {
4094 		_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
4095 		return;
4096 	}
4097 
4098 	if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
4099 		   sizeof(ctx->super->signature)) != 0) {
4100 		_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
4101 		return;
4102 	}
4103 
4104 	crc = _spdk_blob_md_page_calc_crc(ctx->super);
4105 	if (crc != ctx->super->crc) {
4106 		_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
4107 		return;
4108 	}
4109 
4110 	if (memcmp(&ctx->bs->bstype, &ctx->super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
4111 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype matched - loading blobstore\n");
4112 	} else if (memcmp(&ctx->bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
4113 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Bstype wildcard used - loading blobstore regardless bstype\n");
4114 	} else {
4115 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Unexpected bstype\n");
4116 		SPDK_LOGDUMP(SPDK_LOG_BLOB, "Expected:", ctx->bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
4117 		SPDK_LOGDUMP(SPDK_LOG_BLOB, "Found:", ctx->super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
4118 		_spdk_bs_load_ctx_fail(ctx, -ENXIO);
4119 		return;
4120 	}
4121 
4122 	if (ctx->super->size > ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen) {
4123 		SPDK_NOTICELOG("Size mismatch, dev size: %lu, blobstore size: %lu\n",
4124 			       ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen, ctx->super->size);
4125 		_spdk_bs_load_ctx_fail(ctx, -EILSEQ);
4126 		return;
4127 	}
4128 
4129 	if (ctx->super->size == 0) {
4130 		ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
4131 	}
4132 
4133 	if (ctx->super->io_unit_size == 0) {
4134 		ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
4135 	}
4136 
4137 	/* Parse the super block */
4138 	ctx->bs->clean = 1;
4139 	ctx->bs->cluster_sz = ctx->super->cluster_size;
4140 	ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
4141 	ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
4142 	if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
4143 		ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
4144 	}
4145 	ctx->bs->io_unit_size = ctx->super->io_unit_size;
4146 	rc = spdk_bit_array_resize(&ctx->bs->used_clusters, ctx->bs->total_clusters);
4147 	if (rc < 0) {
4148 		_spdk_bs_load_ctx_fail(ctx, -ENOMEM);
4149 		return;
4150 	}
4151 	ctx->bs->md_start = ctx->super->md_start;
4152 	ctx->bs->md_len = ctx->super->md_len;
4153 	ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
4154 					       ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
4155 	ctx->bs->super_blob = ctx->super->super_blob;
4156 	memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
4157 
4158 	if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) {
4159 		_spdk_bs_recover(ctx);
4160 	} else {
4161 		_spdk_bs_load_read_used_pages(ctx);
4162 	}
4163 }
4164 
4165 void
4166 spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
4167 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
4168 {
4169 	struct spdk_blob_store	*bs;
4170 	struct spdk_bs_cpl	cpl;
4171 	struct spdk_bs_load_ctx *ctx;
4172 	struct spdk_bs_opts	opts = {};
4173 	int err;
4174 
4175 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Loading blobstore from dev %p\n", dev);
4176 
4177 	if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
4178 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "unsupported dev block length of %d\n", dev->blocklen);
4179 		dev->destroy(dev);
4180 		cb_fn(cb_arg, NULL, -EINVAL);
4181 		return;
4182 	}
4183 
4184 	if (o) {
4185 		opts = *o;
4186 	} else {
4187 		spdk_bs_opts_init(&opts);
4188 	}
4189 
4190 	if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
4191 		dev->destroy(dev);
4192 		cb_fn(cb_arg, NULL, -EINVAL);
4193 		return;
4194 	}
4195 
4196 	err = _spdk_bs_alloc(dev, &opts, &bs);
4197 	if (err) {
4198 		dev->destroy(dev);
4199 		cb_fn(cb_arg, NULL, err);
4200 		return;
4201 	}
4202 
4203 	ctx = calloc(1, sizeof(*ctx));
4204 	if (!ctx) {
4205 		_spdk_bs_free(bs);
4206 		cb_fn(cb_arg, NULL, -ENOMEM);
4207 		return;
4208 	}
4209 
4210 	ctx->bs = bs;
4211 	ctx->iter_cb_fn = opts.iter_cb_fn;
4212 	ctx->iter_cb_arg = opts.iter_cb_arg;
4213 
4214 	/* Allocate memory for the super block */
4215 	ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
4216 				  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4217 	if (!ctx->super) {
4218 		free(ctx);
4219 		_spdk_bs_free(bs);
4220 		cb_fn(cb_arg, NULL, -ENOMEM);
4221 		return;
4222 	}
4223 
4224 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
4225 	cpl.u.bs_handle.cb_fn = cb_fn;
4226 	cpl.u.bs_handle.cb_arg = cb_arg;
4227 	cpl.u.bs_handle.bs = bs;
4228 
4229 	ctx->seq = bs_sequence_start(bs->md_channel, &cpl);
4230 	if (!ctx->seq) {
4231 		spdk_free(ctx->super);
4232 		free(ctx);
4233 		_spdk_bs_free(bs);
4234 		cb_fn(cb_arg, NULL, -ENOMEM);
4235 		return;
4236 	}
4237 
4238 	/* Read the super block */
4239 	bs_sequence_read_dev(ctx->seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
4240 			     _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
4241 			     _spdk_bs_load_super_cpl, ctx);
4242 }
4243 
4244 /* END spdk_bs_load */
4245 
4246 /* START spdk_bs_dump */
4247 
4248 struct spdk_bs_dump_ctx {
4249 	struct spdk_blob_store		*bs;
4250 	struct spdk_bs_super_block	*super;
4251 	uint32_t			cur_page;
4252 	struct spdk_blob_md_page	*page;
4253 	spdk_bs_sequence_t		*seq;
4254 	FILE				*fp;
4255 	spdk_bs_dump_print_xattr	print_xattr_fn;
4256 	char				xattr_name[4096];
4257 };
4258 
4259 static void
4260 _spdk_bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_dump_ctx *ctx, int bserrno)
4261 {
4262 	spdk_free(ctx->super);
4263 
4264 	/*
4265 	 * We need to defer calling bs_call_cpl() until after
4266 	 * dev destruction, so tuck these away for later use.
4267 	 */
4268 	ctx->bs->unload_err = bserrno;
4269 	memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
4270 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
4271 
4272 	bs_sequence_finish(seq, 0);
4273 	_spdk_bs_free(ctx->bs);
4274 	free(ctx);
4275 }
4276 
4277 static void _spdk_bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
4278 
4279 static void
4280 _spdk_bs_dump_print_md_page(struct spdk_bs_dump_ctx *ctx)
4281 {
4282 	uint32_t page_idx = ctx->cur_page;
4283 	struct spdk_blob_md_page *page = ctx->page;
4284 	struct spdk_blob_md_descriptor *desc;
4285 	size_t cur_desc = 0;
4286 	uint32_t crc;
4287 
4288 	fprintf(ctx->fp, "=========\n");
4289 	fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx);
4290 	fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id);
4291 
4292 	crc = _spdk_blob_md_page_calc_crc(page);
4293 	fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch");
4294 
4295 	desc = (struct spdk_blob_md_descriptor *)page->descriptors;
4296 	while (cur_desc < sizeof(page->descriptors)) {
4297 		if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
4298 			if (desc->length == 0) {
4299 				/* If padding and length are 0, this terminates the page */
4300 				break;
4301 			}
4302 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
4303 			struct spdk_blob_md_descriptor_extent_rle	*desc_extent_rle;
4304 			unsigned int				i;
4305 
4306 			desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
4307 
4308 			for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
4309 				if (desc_extent_rle->extents[i].cluster_idx != 0) {
4310 					fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
4311 						desc_extent_rle->extents[i].cluster_idx);
4312 				} else {
4313 					fprintf(ctx->fp, "Unallocated Extent - ");
4314 				}
4315 				fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length);
4316 				fprintf(ctx->fp, "\n");
4317 			}
4318 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
4319 			struct spdk_blob_md_descriptor_extent_page	*desc_extent;
4320 			unsigned int					i;
4321 
4322 			desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
4323 
4324 			for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) {
4325 				if (desc_extent->cluster_idx[i] != 0) {
4326 					fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
4327 						desc_extent->cluster_idx[i]);
4328 				} else {
4329 					fprintf(ctx->fp, "Unallocated Extent");
4330 				}
4331 				fprintf(ctx->fp, "\n");
4332 			}
4333 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
4334 			struct spdk_blob_md_descriptor_xattr *desc_xattr;
4335 			uint32_t i;
4336 
4337 			desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc;
4338 
4339 			if (desc_xattr->length !=
4340 			    sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) +
4341 			    desc_xattr->name_length + desc_xattr->value_length) {
4342 			}
4343 
4344 			memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length);
4345 			ctx->xattr_name[desc_xattr->name_length] = '\0';
4346 			fprintf(ctx->fp, "XATTR: name = \"%s\"\n", ctx->xattr_name);
4347 			fprintf(ctx->fp, "       value = \"");
4348 			ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name,
4349 					    (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
4350 					    desc_xattr->value_length);
4351 			fprintf(ctx->fp, "\"\n");
4352 			for (i = 0; i < desc_xattr->value_length; i++) {
4353 				if (i % 16 == 0) {
4354 					fprintf(ctx->fp, "               ");
4355 				}
4356 				fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i));
4357 				if ((i + 1) % 16 == 0) {
4358 					fprintf(ctx->fp, "\n");
4359 				}
4360 			}
4361 			if (i % 16 != 0) {
4362 				fprintf(ctx->fp, "\n");
4363 			}
4364 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
4365 			/* TODO */
4366 		} else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
4367 			/* TODO */
4368 		} else {
4369 			/* Error */
4370 		}
4371 		/* Advance to the next descriptor */
4372 		cur_desc += sizeof(*desc) + desc->length;
4373 		if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
4374 			break;
4375 		}
4376 		desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
4377 	}
4378 }
4379 
4380 static void
4381 _spdk_bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4382 {
4383 	struct spdk_bs_dump_ctx *ctx = cb_arg;
4384 
4385 	if (bserrno != 0) {
4386 		_spdk_bs_dump_finish(seq, ctx, bserrno);
4387 		return;
4388 	}
4389 
4390 	if (ctx->page->id != 0) {
4391 		_spdk_bs_dump_print_md_page(ctx);
4392 	}
4393 
4394 	ctx->cur_page++;
4395 
4396 	if (ctx->cur_page < ctx->super->md_len) {
4397 		_spdk_bs_dump_read_md_page(seq, ctx);
4398 	} else {
4399 		spdk_free(ctx->page);
4400 		_spdk_bs_dump_finish(seq, ctx, 0);
4401 	}
4402 }
4403 
4404 static void
4405 _spdk_bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
4406 {
4407 	struct spdk_bs_dump_ctx *ctx = cb_arg;
4408 	uint64_t lba;
4409 
4410 	assert(ctx->cur_page < ctx->super->md_len);
4411 	lba = _spdk_bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
4412 	bs_sequence_read_dev(seq, ctx->page, lba,
4413 			     _spdk_bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
4414 			     _spdk_bs_dump_read_md_page_cpl, ctx);
4415 }
4416 
4417 static void
4418 _spdk_bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4419 {
4420 	struct spdk_bs_dump_ctx *ctx = cb_arg;
4421 
4422 	fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature);
4423 	if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
4424 		   sizeof(ctx->super->signature)) != 0) {
4425 		fprintf(ctx->fp, "(Mismatch)\n");
4426 		_spdk_bs_dump_finish(seq, ctx, bserrno);
4427 		return;
4428 	} else {
4429 		fprintf(ctx->fp, "(OK)\n");
4430 	}
4431 	fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version);
4432 	fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc,
4433 		(ctx->super->crc == _spdk_blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch");
4434 	fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype);
4435 	fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size);
4436 	fprintf(ctx->fp, "Super Blob ID: ");
4437 	if (ctx->super->super_blob == SPDK_BLOBID_INVALID) {
4438 		fprintf(ctx->fp, "(None)\n");
4439 	} else {
4440 		fprintf(ctx->fp, "%" PRIu64 "\n", ctx->super->super_blob);
4441 	}
4442 	fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean);
4443 	fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start);
4444 	fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len);
4445 	fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start);
4446 	fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len);
4447 	fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start);
4448 	fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len);
4449 	fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start);
4450 	fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len);
4451 
4452 	ctx->cur_page = 0;
4453 	ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, SPDK_BS_PAGE_SIZE,
4454 				 NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4455 	if (!ctx->page) {
4456 		_spdk_bs_dump_finish(seq, ctx, -ENOMEM);
4457 		return;
4458 	}
4459 	_spdk_bs_dump_read_md_page(seq, ctx);
4460 }
4461 
4462 void
4463 spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn,
4464 	     spdk_bs_op_complete cb_fn, void *cb_arg)
4465 {
4466 	struct spdk_blob_store	*bs;
4467 	struct spdk_bs_cpl	cpl;
4468 	spdk_bs_sequence_t	*seq;
4469 	struct spdk_bs_dump_ctx *ctx;
4470 	struct spdk_bs_opts	opts = {};
4471 	int err;
4472 
4473 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Dumping blobstore from dev %p\n", dev);
4474 
4475 	spdk_bs_opts_init(&opts);
4476 
4477 	err = _spdk_bs_alloc(dev, &opts, &bs);
4478 	if (err) {
4479 		dev->destroy(dev);
4480 		cb_fn(cb_arg, err);
4481 		return;
4482 	}
4483 
4484 	ctx = calloc(1, sizeof(*ctx));
4485 	if (!ctx) {
4486 		_spdk_bs_free(bs);
4487 		cb_fn(cb_arg, -ENOMEM);
4488 		return;
4489 	}
4490 
4491 	ctx->bs = bs;
4492 	ctx->fp = fp;
4493 	ctx->print_xattr_fn = print_xattr_fn;
4494 
4495 	/* Allocate memory for the super block */
4496 	ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
4497 				  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4498 	if (!ctx->super) {
4499 		free(ctx);
4500 		_spdk_bs_free(bs);
4501 		cb_fn(cb_arg, -ENOMEM);
4502 		return;
4503 	}
4504 
4505 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
4506 	cpl.u.bs_basic.cb_fn = cb_fn;
4507 	cpl.u.bs_basic.cb_arg = cb_arg;
4508 
4509 	seq = bs_sequence_start(bs->md_channel, &cpl);
4510 	if (!seq) {
4511 		spdk_free(ctx->super);
4512 		free(ctx);
4513 		_spdk_bs_free(bs);
4514 		cb_fn(cb_arg, -ENOMEM);
4515 		return;
4516 	}
4517 
4518 	/* Read the super block */
4519 	bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
4520 			     _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
4521 			     _spdk_bs_dump_super_cpl, ctx);
4522 }
4523 
4524 /* END spdk_bs_dump */
4525 
4526 /* START spdk_bs_init */
4527 
4528 struct spdk_bs_init_ctx {
4529 	struct spdk_blob_store		*bs;
4530 	struct spdk_bs_super_block	*super;
4531 };
4532 
4533 static void
4534 _spdk_bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4535 {
4536 	struct spdk_bs_init_ctx *ctx = cb_arg;
4537 
4538 	spdk_free(ctx->super);
4539 	free(ctx);
4540 
4541 	bs_sequence_finish(seq, bserrno);
4542 }
4543 
4544 static void
4545 _spdk_bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4546 {
4547 	struct spdk_bs_init_ctx *ctx = cb_arg;
4548 
4549 	/* Write super block */
4550 	bs_sequence_write_dev(seq, ctx->super, _spdk_bs_page_to_lba(ctx->bs, 0),
4551 			      _spdk_bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
4552 			      _spdk_bs_init_persist_super_cpl, ctx);
4553 }
4554 
4555 void
4556 spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
4557 	     spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
4558 {
4559 	struct spdk_bs_init_ctx *ctx;
4560 	struct spdk_blob_store	*bs;
4561 	struct spdk_bs_cpl	cpl;
4562 	spdk_bs_sequence_t	*seq;
4563 	spdk_bs_batch_t		*batch;
4564 	uint64_t		num_md_lba;
4565 	uint64_t		num_md_pages;
4566 	uint64_t		num_md_clusters;
4567 	uint32_t		i;
4568 	struct spdk_bs_opts	opts = {};
4569 	int			rc;
4570 
4571 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Initializing blobstore on dev %p\n", dev);
4572 
4573 	if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
4574 		SPDK_ERRLOG("unsupported dev block length of %d\n",
4575 			    dev->blocklen);
4576 		dev->destroy(dev);
4577 		cb_fn(cb_arg, NULL, -EINVAL);
4578 		return;
4579 	}
4580 
4581 	if (o) {
4582 		opts = *o;
4583 	} else {
4584 		spdk_bs_opts_init(&opts);
4585 	}
4586 
4587 	if (_spdk_bs_opts_verify(&opts) != 0) {
4588 		dev->destroy(dev);
4589 		cb_fn(cb_arg, NULL, -EINVAL);
4590 		return;
4591 	}
4592 
4593 	rc = _spdk_bs_alloc(dev, &opts, &bs);
4594 	if (rc) {
4595 		dev->destroy(dev);
4596 		cb_fn(cb_arg, NULL, rc);
4597 		return;
4598 	}
4599 
4600 	if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
4601 		/* By default, allocate 1 page per cluster.
4602 		 * Technically, this over-allocates metadata
4603 		 * because more metadata will reduce the number
4604 		 * of usable clusters. This can be addressed with
4605 		 * more complex math in the future.
4606 		 */
4607 		bs->md_len = bs->total_clusters;
4608 	} else {
4609 		bs->md_len = opts.num_md_pages;
4610 	}
4611 	rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
4612 	if (rc < 0) {
4613 		_spdk_bs_free(bs);
4614 		cb_fn(cb_arg, NULL, -ENOMEM);
4615 		return;
4616 	}
4617 
4618 	rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len);
4619 	if (rc < 0) {
4620 		_spdk_bs_free(bs);
4621 		cb_fn(cb_arg, NULL, -ENOMEM);
4622 		return;
4623 	}
4624 
4625 	ctx = calloc(1, sizeof(*ctx));
4626 	if (!ctx) {
4627 		_spdk_bs_free(bs);
4628 		cb_fn(cb_arg, NULL, -ENOMEM);
4629 		return;
4630 	}
4631 
4632 	ctx->bs = bs;
4633 
4634 	/* Allocate memory for the super block */
4635 	ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
4636 				  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4637 	if (!ctx->super) {
4638 		free(ctx);
4639 		_spdk_bs_free(bs);
4640 		cb_fn(cb_arg, NULL, -ENOMEM);
4641 		return;
4642 	}
4643 	memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
4644 	       sizeof(ctx->super->signature));
4645 	ctx->super->version = SPDK_BS_VERSION;
4646 	ctx->super->length = sizeof(*ctx->super);
4647 	ctx->super->super_blob = bs->super_blob;
4648 	ctx->super->clean = 0;
4649 	ctx->super->cluster_size = bs->cluster_sz;
4650 	ctx->super->io_unit_size = bs->io_unit_size;
4651 	memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
4652 
4653 	/* Calculate how many pages the metadata consumes at the front
4654 	 * of the disk.
4655 	 */
4656 
4657 	/* The super block uses 1 page */
4658 	num_md_pages = 1;
4659 
4660 	/* The used_md_pages mask requires 1 bit per metadata page, rounded
4661 	 * up to the nearest page, plus a header.
4662 	 */
4663 	ctx->super->used_page_mask_start = num_md_pages;
4664 	ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
4665 					 spdk_divide_round_up(bs->md_len, 8),
4666 					 SPDK_BS_PAGE_SIZE);
4667 	num_md_pages += ctx->super->used_page_mask_len;
4668 
4669 	/* The used_clusters mask requires 1 bit per cluster, rounded
4670 	 * up to the nearest page, plus a header.
4671 	 */
4672 	ctx->super->used_cluster_mask_start = num_md_pages;
4673 	ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
4674 					    spdk_divide_round_up(bs->total_clusters, 8),
4675 					    SPDK_BS_PAGE_SIZE);
4676 	num_md_pages += ctx->super->used_cluster_mask_len;
4677 
4678 	/* The used_blobids mask requires 1 bit per metadata page, rounded
4679 	 * up to the nearest page, plus a header.
4680 	 */
4681 	ctx->super->used_blobid_mask_start = num_md_pages;
4682 	ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
4683 					   spdk_divide_round_up(bs->md_len, 8),
4684 					   SPDK_BS_PAGE_SIZE);
4685 	num_md_pages += ctx->super->used_blobid_mask_len;
4686 
4687 	/* The metadata region size was chosen above */
4688 	ctx->super->md_start = bs->md_start = num_md_pages;
4689 	ctx->super->md_len = bs->md_len;
4690 	num_md_pages += bs->md_len;
4691 
4692 	num_md_lba = _spdk_bs_page_to_lba(bs, num_md_pages);
4693 
4694 	ctx->super->size = dev->blockcnt * dev->blocklen;
4695 
4696 	ctx->super->crc = _spdk_blob_md_page_calc_crc(ctx->super);
4697 
4698 	num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster);
4699 	if (num_md_clusters > bs->total_clusters) {
4700 		SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
4701 			    "please decrease number of pages reserved for metadata "
4702 			    "or increase cluster size.\n");
4703 		spdk_free(ctx->super);
4704 		free(ctx);
4705 		_spdk_bs_free(bs);
4706 		cb_fn(cb_arg, NULL, -ENOMEM);
4707 		return;
4708 	}
4709 	/* Claim all of the clusters used by the metadata */
4710 	for (i = 0; i < num_md_clusters; i++) {
4711 		_spdk_bs_claim_cluster(bs, i);
4712 	}
4713 
4714 	bs->total_data_clusters = bs->num_free_clusters;
4715 
4716 	cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
4717 	cpl.u.bs_handle.cb_fn = cb_fn;
4718 	cpl.u.bs_handle.cb_arg = cb_arg;
4719 	cpl.u.bs_handle.bs = bs;
4720 
4721 	seq = bs_sequence_start(bs->md_channel, &cpl);
4722 	if (!seq) {
4723 		spdk_free(ctx->super);
4724 		free(ctx);
4725 		_spdk_bs_free(bs);
4726 		cb_fn(cb_arg, NULL, -ENOMEM);
4727 		return;
4728 	}
4729 
4730 	batch = bs_sequence_to_batch(seq, _spdk_bs_init_trim_cpl, ctx);
4731 
4732 	/* Clear metadata space */
4733 	bs_batch_write_zeroes_dev(batch, 0, num_md_lba);
4734 
4735 	switch (opts.clear_method) {
4736 	case BS_CLEAR_WITH_UNMAP:
4737 		/* Trim data clusters */
4738 		bs_batch_unmap_dev(batch, num_md_lba, ctx->bs->dev->blockcnt - num_md_lba);
4739 		break;
4740 	case BS_CLEAR_WITH_WRITE_ZEROES:
4741 		/* Write_zeroes to data clusters */
4742 		bs_batch_write_zeroes_dev(batch, num_md_lba, ctx->bs->dev->blockcnt - num_md_lba);
4743 		break;
4744 	case BS_CLEAR_WITH_NONE:
4745 	default:
4746 		break;
4747 	}
4748 
4749 	bs_batch_close(batch);
4750 }
4751 
4752 /* END spdk_bs_init */
4753 
4754 /* START spdk_bs_destroy */
4755 
4756 static void
4757 _spdk_bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4758 {
4759 	struct spdk_bs_init_ctx *ctx = cb_arg;
4760 	struct spdk_blob_store *bs = ctx->bs;
4761 
4762 	/*
4763 	 * We need to defer calling bs_call_cpl() until after
4764 	 * dev destruction, so tuck these away for later use.
4765 	 */
4766 	bs->unload_err = bserrno;
4767 	memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
4768 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
4769 
4770 	bs_sequence_finish(seq, bserrno);
4771 
4772 	_spdk_bs_free(bs);
4773 	free(ctx);
4774 }
4775 
4776 void
4777 spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn,
4778 		void *cb_arg)
4779 {
4780 	struct spdk_bs_cpl	cpl;
4781 	spdk_bs_sequence_t	*seq;
4782 	struct spdk_bs_init_ctx *ctx;
4783 
4784 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Destroying blobstore\n");
4785 
4786 	if (!TAILQ_EMPTY(&bs->blobs)) {
4787 		SPDK_ERRLOG("Blobstore still has open blobs\n");
4788 		cb_fn(cb_arg, -EBUSY);
4789 		return;
4790 	}
4791 
4792 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
4793 	cpl.u.bs_basic.cb_fn = cb_fn;
4794 	cpl.u.bs_basic.cb_arg = cb_arg;
4795 
4796 	ctx = calloc(1, sizeof(*ctx));
4797 	if (!ctx) {
4798 		cb_fn(cb_arg, -ENOMEM);
4799 		return;
4800 	}
4801 
4802 	ctx->bs = bs;
4803 
4804 	seq = bs_sequence_start(bs->md_channel, &cpl);
4805 	if (!seq) {
4806 		free(ctx);
4807 		cb_fn(cb_arg, -ENOMEM);
4808 		return;
4809 	}
4810 
4811 	/* Write zeroes to the super block */
4812 	bs_sequence_write_zeroes_dev(seq,
4813 				     _spdk_bs_page_to_lba(bs, 0),
4814 				     _spdk_bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)),
4815 				     _spdk_bs_destroy_trim_cpl, ctx);
4816 }
4817 
4818 /* END spdk_bs_destroy */
4819 
4820 /* START spdk_bs_unload */
4821 
4822 static void
4823 _spdk_bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno)
4824 {
4825 	spdk_bs_sequence_t *seq = ctx->seq;
4826 
4827 	spdk_free(ctx->super);
4828 
4829 	/*
4830 	 * We need to defer calling bs_call_cpl() until after
4831 	 * dev destruction, so tuck these away for later use.
4832 	 */
4833 	ctx->bs->unload_err = bserrno;
4834 	memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
4835 	seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
4836 
4837 	bs_sequence_finish(seq, bserrno);
4838 
4839 	_spdk_bs_free(ctx->bs);
4840 	free(ctx);
4841 }
4842 
4843 static void
4844 _spdk_bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4845 {
4846 	struct spdk_bs_load_ctx	*ctx = cb_arg;
4847 
4848 	_spdk_bs_unload_finish(ctx, bserrno);
4849 }
4850 
4851 static void
4852 _spdk_bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4853 {
4854 	struct spdk_bs_load_ctx	*ctx = cb_arg;
4855 
4856 	spdk_free(ctx->mask);
4857 
4858 	if (bserrno != 0) {
4859 		_spdk_bs_unload_finish(ctx, bserrno);
4860 		return;
4861 	}
4862 
4863 	ctx->super->clean = 1;
4864 
4865 	_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_unload_write_super_cpl, ctx);
4866 }
4867 
4868 static void
4869 _spdk_bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4870 {
4871 	struct spdk_bs_load_ctx	*ctx = cb_arg;
4872 
4873 	spdk_free(ctx->mask);
4874 	ctx->mask = NULL;
4875 
4876 	if (bserrno != 0) {
4877 		_spdk_bs_unload_finish(ctx, bserrno);
4878 		return;
4879 	}
4880 
4881 	_spdk_bs_write_used_clusters(seq, ctx, _spdk_bs_unload_write_used_clusters_cpl);
4882 }
4883 
4884 static void
4885 _spdk_bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4886 {
4887 	struct spdk_bs_load_ctx	*ctx = cb_arg;
4888 
4889 	spdk_free(ctx->mask);
4890 	ctx->mask = NULL;
4891 
4892 	if (bserrno != 0) {
4893 		_spdk_bs_unload_finish(ctx, bserrno);
4894 		return;
4895 	}
4896 
4897 	_spdk_bs_write_used_blobids(seq, ctx, _spdk_bs_unload_write_used_blobids_cpl);
4898 }
4899 
4900 static void
4901 _spdk_bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4902 {
4903 	struct spdk_bs_load_ctx	*ctx = cb_arg;
4904 
4905 	if (bserrno != 0) {
4906 		_spdk_bs_unload_finish(ctx, bserrno);
4907 		return;
4908 	}
4909 
4910 	_spdk_bs_write_used_md(seq, cb_arg, _spdk_bs_unload_write_used_pages_cpl);
4911 }
4912 
4913 void
4914 spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
4915 {
4916 	struct spdk_bs_cpl	cpl;
4917 	struct spdk_bs_load_ctx *ctx;
4918 
4919 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blobstore\n");
4920 
4921 	if (!TAILQ_EMPTY(&bs->blobs)) {
4922 		SPDK_ERRLOG("Blobstore still has open blobs\n");
4923 		cb_fn(cb_arg, -EBUSY);
4924 		return;
4925 	}
4926 
4927 	ctx = calloc(1, sizeof(*ctx));
4928 	if (!ctx) {
4929 		cb_fn(cb_arg, -ENOMEM);
4930 		return;
4931 	}
4932 
4933 	ctx->bs = bs;
4934 
4935 	ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
4936 				  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
4937 	if (!ctx->super) {
4938 		free(ctx);
4939 		cb_fn(cb_arg, -ENOMEM);
4940 		return;
4941 	}
4942 
4943 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
4944 	cpl.u.bs_basic.cb_fn = cb_fn;
4945 	cpl.u.bs_basic.cb_arg = cb_arg;
4946 
4947 	ctx->seq = bs_sequence_start(bs->md_channel, &cpl);
4948 	if (!ctx->seq) {
4949 		spdk_free(ctx->super);
4950 		free(ctx);
4951 		cb_fn(cb_arg, -ENOMEM);
4952 		return;
4953 	}
4954 
4955 	/* Read super block */
4956 	bs_sequence_read_dev(ctx->seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
4957 			     _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
4958 			     _spdk_bs_unload_read_super_cpl, ctx);
4959 }
4960 
4961 /* END spdk_bs_unload */
4962 
4963 /* START spdk_bs_set_super */
4964 
4965 struct spdk_bs_set_super_ctx {
4966 	struct spdk_blob_store		*bs;
4967 	struct spdk_bs_super_block	*super;
4968 };
4969 
4970 static void
4971 _spdk_bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4972 {
4973 	struct spdk_bs_set_super_ctx	*ctx = cb_arg;
4974 
4975 	if (bserrno != 0) {
4976 		SPDK_ERRLOG("Unable to write to super block of blobstore\n");
4977 	}
4978 
4979 	spdk_free(ctx->super);
4980 
4981 	bs_sequence_finish(seq, bserrno);
4982 
4983 	free(ctx);
4984 }
4985 
4986 static void
4987 _spdk_bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
4988 {
4989 	struct spdk_bs_set_super_ctx	*ctx = cb_arg;
4990 
4991 	if (bserrno != 0) {
4992 		SPDK_ERRLOG("Unable to read super block of blobstore\n");
4993 		spdk_free(ctx->super);
4994 		bs_sequence_finish(seq, bserrno);
4995 		free(ctx);
4996 		return;
4997 	}
4998 
4999 	_spdk_bs_write_super(seq, ctx->bs, ctx->super, _spdk_bs_set_super_write_cpl, ctx);
5000 }
5001 
5002 void
5003 spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
5004 		  spdk_bs_op_complete cb_fn, void *cb_arg)
5005 {
5006 	struct spdk_bs_cpl		cpl;
5007 	spdk_bs_sequence_t		*seq;
5008 	struct spdk_bs_set_super_ctx	*ctx;
5009 
5010 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Setting super blob id on blobstore\n");
5011 
5012 	ctx = calloc(1, sizeof(*ctx));
5013 	if (!ctx) {
5014 		cb_fn(cb_arg, -ENOMEM);
5015 		return;
5016 	}
5017 
5018 	ctx->bs = bs;
5019 
5020 	ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
5021 				  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
5022 	if (!ctx->super) {
5023 		free(ctx);
5024 		cb_fn(cb_arg, -ENOMEM);
5025 		return;
5026 	}
5027 
5028 	cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
5029 	cpl.u.bs_basic.cb_fn = cb_fn;
5030 	cpl.u.bs_basic.cb_arg = cb_arg;
5031 
5032 	seq = bs_sequence_start(bs->md_channel, &cpl);
5033 	if (!seq) {
5034 		spdk_free(ctx->super);
5035 		free(ctx);
5036 		cb_fn(cb_arg, -ENOMEM);
5037 		return;
5038 	}
5039 
5040 	bs->super_blob = blobid;
5041 
5042 	/* Read super block */
5043 	bs_sequence_read_dev(seq, ctx->super, _spdk_bs_page_to_lba(bs, 0),
5044 			     _spdk_bs_byte_to_lba(bs, sizeof(*ctx->super)),
5045 			     _spdk_bs_set_super_read_cpl, ctx);
5046 }
5047 
5048 /* END spdk_bs_set_super */
5049 
5050 void
5051 spdk_bs_get_super(struct spdk_blob_store *bs,
5052 		  spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
5053 {
5054 	if (bs->super_blob == SPDK_BLOBID_INVALID) {
5055 		cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
5056 	} else {
5057 		cb_fn(cb_arg, bs->super_blob, 0);
5058 	}
5059 }
5060 
5061 uint64_t
5062 spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
5063 {
5064 	return bs->cluster_sz;
5065 }
5066 
5067 uint64_t
5068 spdk_bs_get_page_size(struct spdk_blob_store *bs)
5069 {
5070 	return SPDK_BS_PAGE_SIZE;
5071 }
5072 
5073 uint64_t
5074 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs)
5075 {
5076 	return bs->io_unit_size;
5077 }
5078 
5079 uint64_t
5080 spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
5081 {
5082 	return bs->num_free_clusters;
5083 }
5084 
5085 uint64_t
5086 spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs)
5087 {
5088 	return bs->total_data_clusters;
5089 }
5090 
5091 static int
5092 spdk_bs_register_md_thread(struct spdk_blob_store *bs)
5093 {
5094 	bs->md_channel = spdk_get_io_channel(bs);
5095 	if (!bs->md_channel) {
5096 		SPDK_ERRLOG("Failed to get IO channel.\n");
5097 		return -1;
5098 	}
5099 
5100 	return 0;
5101 }
5102 
5103 static int
5104 spdk_bs_unregister_md_thread(struct spdk_blob_store *bs)
5105 {
5106 	spdk_put_io_channel(bs->md_channel);
5107 
5108 	return 0;
5109 }
5110 
5111 spdk_blob_id spdk_blob_get_id(struct spdk_blob *blob)
5112 {
5113 	assert(blob != NULL);
5114 
5115 	return blob->id;
5116 }
5117 
5118 uint64_t spdk_blob_get_num_pages(struct spdk_blob *blob)
5119 {
5120 	assert(blob != NULL);
5121 
5122 	return _spdk_bs_cluster_to_page(blob->bs, blob->active.num_clusters);
5123 }
5124 
5125 uint64_t spdk_blob_get_num_io_units(struct spdk_blob *blob)
5126 {
5127 	assert(blob != NULL);
5128 
5129 	return spdk_blob_get_num_pages(blob) * _spdk_bs_io_unit_per_page(blob->bs);
5130 }
5131 
5132 uint64_t spdk_blob_get_num_clusters(struct spdk_blob *blob)
5133 {
5134 	assert(blob != NULL);
5135 
5136 	return blob->active.num_clusters;
5137 }
5138 
5139 /* START spdk_bs_create_blob */
5140 
5141 static void
5142 _spdk_bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
5143 {
5144 	struct spdk_blob *blob = cb_arg;
5145 
5146 	_spdk_blob_free(blob);
5147 
5148 	bs_sequence_finish(seq, bserrno);
5149 }
5150 
5151 static int
5152 _spdk_blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs,
5153 		      bool internal)
5154 {
5155 	uint64_t i;
5156 	size_t value_len = 0;
5157 	int rc;
5158 	const void *value = NULL;
5159 	if (xattrs->count > 0 && xattrs->get_value == NULL) {
5160 		return -EINVAL;
5161 	}
5162 	for (i = 0; i < xattrs->count; i++) {
5163 		xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len);
5164 		if (value == NULL || value_len == 0) {
5165 			return -EINVAL;
5166 		}
5167 		rc = _spdk_blob_set_xattr(blob, xattrs->names[i], value, value_len, internal);
5168 		if (rc < 0) {
5169 			return rc;
5170 		}
5171 	}
5172 	return 0;
5173 }
5174 
5175 static void
5176 _spdk_bs_create_blob(struct spdk_blob_store *bs,
5177 		     const struct spdk_blob_opts *opts,
5178 		     const struct spdk_blob_xattr_opts *internal_xattrs,
5179 		     spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
5180 {
5181 	struct spdk_blob	*blob;
5182 	uint32_t		page_idx;
5183 	struct spdk_bs_cpl	cpl;
5184 	struct spdk_blob_opts	opts_default;
5185 	struct spdk_blob_xattr_opts internal_xattrs_default;
5186 	spdk_bs_sequence_t	*seq;
5187 	spdk_blob_id		id;
5188 	int rc;
5189 
5190 	assert(spdk_get_thread() == bs->md_thread);
5191 
5192 	page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
5193 	if (page_idx == UINT32_MAX) {
5194 		cb_fn(cb_arg, 0, -ENOMEM);
5195 		return;
5196 	}
5197 	spdk_bit_array_set(bs->used_blobids, page_idx);
5198 	_spdk_bs_claim_md_page(bs, page_idx);
5199 
5200 	id = _spdk_bs_page_to_blobid(page_idx);
5201 
5202 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Creating blob with id %lu at page %u\n", id, page_idx);
5203 
5204 	blob = _spdk_blob_alloc(bs, id);
5205 	if (!blob) {
5206 		cb_fn(cb_arg, 0, -ENOMEM);
5207 		return;
5208 	}
5209 
5210 	if (!opts) {
5211 		spdk_blob_opts_init(&opts_default);
5212 		opts = &opts_default;
5213 	}
5214 
5215 	blob->use_extent_table = opts->use_extent_table;
5216 	if (blob->use_extent_table) {
5217 		blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE;
5218 	}
5219 
5220 	if (!internal_xattrs) {
5221 		_spdk_blob_xattrs_init(&internal_xattrs_default);
5222 		internal_xattrs = &internal_xattrs_default;
5223 	}
5224 
5225 	rc = _spdk_blob_set_xattrs(blob, &opts->xattrs, false);
5226 	if (rc < 0) {
5227 		_spdk_blob_free(blob);
5228 		cb_fn(cb_arg, 0, rc);
5229 		return;
5230 	}
5231 
5232 	rc = _spdk_blob_set_xattrs(blob, internal_xattrs, true);
5233 	if (rc < 0) {
5234 		_spdk_blob_free(blob);
5235 		cb_fn(cb_arg, 0, rc);
5236 		return;
5237 	}
5238 
5239 	if (opts->thin_provision) {
5240 		_spdk_blob_set_thin_provision(blob);
5241 	}
5242 
5243 	_spdk_blob_set_clear_method(blob, opts->clear_method);
5244 
5245 	rc = _spdk_blob_resize(blob, opts->num_clusters);
5246 	if (rc < 0) {
5247 		_spdk_blob_free(blob);
5248 		cb_fn(cb_arg, 0, rc);
5249 		return;
5250 	}
5251 	cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
5252 	cpl.u.blobid.cb_fn = cb_fn;
5253 	cpl.u.blobid.cb_arg = cb_arg;
5254 	cpl.u.blobid.blobid = blob->id;
5255 
5256 	seq = bs_sequence_start(bs->md_channel, &cpl);
5257 	if (!seq) {
5258 		_spdk_blob_free(blob);
5259 		cb_fn(cb_arg, 0, -ENOMEM);
5260 		return;
5261 	}
5262 
5263 	_spdk_blob_persist(seq, blob, _spdk_bs_create_blob_cpl, blob);
5264 }
5265 
5266 void spdk_bs_create_blob(struct spdk_blob_store *bs,
5267 			 spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
5268 {
5269 	_spdk_bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg);
5270 }
5271 
5272 void spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts,
5273 			     spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
5274 {
5275 	_spdk_bs_create_blob(bs, opts, NULL, cb_fn, cb_arg);
5276 }
5277 
5278 /* END spdk_bs_create_blob */
5279 
5280 /* START blob_cleanup */
5281 
5282 struct spdk_clone_snapshot_ctx {
5283 	struct spdk_bs_cpl      cpl;
5284 	int bserrno;
5285 	bool frozen;
5286 
5287 	struct spdk_io_channel *channel;
5288 
5289 	/* Current cluster for inflate operation */
5290 	uint64_t cluster;
5291 
5292 	/* For inflation force allocation of all unallocated clusters and remove
5293 	 * thin-provisioning. Otherwise only decouple parent and keep clone thin. */
5294 	bool allocate_all;
5295 
5296 	struct {
5297 		spdk_blob_id id;
5298 		struct spdk_blob *blob;
5299 	} original;
5300 	struct {
5301 		spdk_blob_id id;
5302 		struct spdk_blob *blob;
5303 	} new;
5304 
5305 	/* xattrs specified for snapshot/clones only. They have no impact on
5306 	 * the original blobs xattrs. */
5307 	const struct spdk_blob_xattr_opts *xattrs;
5308 };
5309 
5310 static void
5311 _spdk_bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno)
5312 {
5313 	struct spdk_clone_snapshot_ctx *ctx = cb_arg;
5314 	struct spdk_bs_cpl *cpl = &ctx->cpl;
5315 
5316 	if (bserrno != 0) {
5317 		if (ctx->bserrno != 0) {
5318 			SPDK_ERRLOG("Cleanup error %d\n", bserrno);
5319 		} else {
5320 			ctx->bserrno = bserrno;
5321 		}
5322 	}
5323 
5324 	switch (cpl->type) {
5325 	case SPDK_BS_CPL_TYPE_BLOBID:
5326 		cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno);
5327 		break;
5328 	case SPDK_BS_CPL_TYPE_BLOB_BASIC:
5329 		cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
5330 		break;
5331 	default:
5332 		SPDK_UNREACHABLE();
5333 		break;
5334 	}
5335 
5336 	free(ctx);
5337 }
5338 
5339 static void
5340 _spdk_bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
5341 {
5342 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5343 	struct spdk_blob *origblob = ctx->original.blob;
5344 
5345 	if (bserrno != 0) {
5346 		if (ctx->bserrno != 0) {
5347 			SPDK_ERRLOG("Unfreeze error %d\n", bserrno);
5348 		} else {
5349 			ctx->bserrno = bserrno;
5350 		}
5351 	}
5352 
5353 	ctx->original.id = origblob->id;
5354 	origblob->locked_operation_in_progress = false;
5355 
5356 	spdk_blob_close(origblob, _spdk_bs_clone_snapshot_cleanup_finish, ctx);
5357 }
5358 
5359 static void
5360 _spdk_bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno)
5361 {
5362 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5363 	struct spdk_blob *origblob = ctx->original.blob;
5364 
5365 	if (bserrno != 0) {
5366 		if (ctx->bserrno != 0) {
5367 			SPDK_ERRLOG("Cleanup error %d\n", bserrno);
5368 		} else {
5369 			ctx->bserrno = bserrno;
5370 		}
5371 	}
5372 
5373 	if (ctx->frozen) {
5374 		/* Unfreeze any outstanding I/O */
5375 		_spdk_blob_unfreeze_io(origblob, _spdk_bs_snapshot_unfreeze_cpl, ctx);
5376 	} else {
5377 		_spdk_bs_snapshot_unfreeze_cpl(ctx, 0);
5378 	}
5379 
5380 }
5381 
5382 static void
5383 _spdk_bs_clone_snapshot_newblob_cleanup(void *cb_arg, int bserrno)
5384 {
5385 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5386 	struct spdk_blob *newblob = ctx->new.blob;
5387 
5388 	if (bserrno != 0) {
5389 		if (ctx->bserrno != 0) {
5390 			SPDK_ERRLOG("Cleanup error %d\n", bserrno);
5391 		} else {
5392 			ctx->bserrno = bserrno;
5393 		}
5394 	}
5395 
5396 	ctx->new.id = newblob->id;
5397 	spdk_blob_close(newblob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx);
5398 }
5399 
5400 /* END blob_cleanup */
5401 
5402 /* START spdk_bs_create_snapshot */
5403 
5404 static void
5405 _spdk_bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2)
5406 {
5407 	uint64_t *cluster_temp;
5408 	uint32_t *extent_page_temp;
5409 
5410 	cluster_temp = blob1->active.clusters;
5411 	blob1->active.clusters = blob2->active.clusters;
5412 	blob2->active.clusters = cluster_temp;
5413 
5414 	extent_page_temp = blob1->active.extent_pages;
5415 	blob1->active.extent_pages = blob2->active.extent_pages;
5416 	blob2->active.extent_pages = extent_page_temp;
5417 }
5418 
5419 static void
5420 _spdk_bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno)
5421 {
5422 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5423 	struct spdk_blob *origblob = ctx->original.blob;
5424 	struct spdk_blob *newblob = ctx->new.blob;
5425 
5426 	if (bserrno != 0) {
5427 		_spdk_bs_snapshot_swap_cluster_maps(newblob, origblob);
5428 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5429 		return;
5430 	}
5431 
5432 	/* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */
5433 	bserrno = _spdk_blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true);
5434 	if (bserrno != 0) {
5435 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5436 		return;
5437 	}
5438 
5439 	_spdk_bs_blob_list_add(ctx->original.blob);
5440 
5441 	spdk_blob_set_read_only(newblob);
5442 
5443 	/* sync snapshot metadata */
5444 	spdk_blob_sync_md(newblob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx);
5445 }
5446 
5447 static void
5448 _spdk_bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno)
5449 {
5450 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5451 	struct spdk_blob *origblob = ctx->original.blob;
5452 	struct spdk_blob *newblob = ctx->new.blob;
5453 
5454 	if (bserrno != 0) {
5455 		/* return cluster map back to original */
5456 		_spdk_bs_snapshot_swap_cluster_maps(newblob, origblob);
5457 
5458 		/* Newblob md sync failed. Valid clusters are only present in origblob.
5459 		 * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occured.
5460 		 * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */
5461 		_spdk_blob_set_thin_provision(newblob);
5462 		assert(spdk_mem_all_zero(newblob->active.clusters,
5463 					 newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
5464 		assert(spdk_mem_all_zero(newblob->active.extent_pages,
5465 					 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
5466 
5467 		_spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
5468 		return;
5469 	}
5470 
5471 	/* Set internal xattr for snapshot id */
5472 	bserrno = _spdk_blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true);
5473 	if (bserrno != 0) {
5474 		/* return cluster map back to original */
5475 		_spdk_bs_snapshot_swap_cluster_maps(newblob, origblob);
5476 		_spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
5477 		return;
5478 	}
5479 
5480 	_spdk_bs_blob_list_remove(origblob);
5481 	origblob->parent_id = newblob->id;
5482 
5483 	/* Create new back_bs_dev for snapshot */
5484 	origblob->back_bs_dev = bs_create_blob_bs_dev(newblob);
5485 	if (origblob->back_bs_dev == NULL) {
5486 		/* return cluster map back to original */
5487 		_spdk_bs_snapshot_swap_cluster_maps(newblob, origblob);
5488 		_spdk_bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL);
5489 		return;
5490 	}
5491 
5492 	/* set clone blob as thin provisioned */
5493 	_spdk_blob_set_thin_provision(origblob);
5494 
5495 	_spdk_bs_blob_list_add(newblob);
5496 
5497 	/* sync clone metadata */
5498 	spdk_blob_sync_md(origblob, _spdk_bs_snapshot_origblob_sync_cpl, ctx);
5499 }
5500 
5501 static void
5502 _spdk_bs_snapshot_freeze_cpl(void *cb_arg, int rc)
5503 {
5504 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5505 	struct spdk_blob *origblob = ctx->original.blob;
5506 	struct spdk_blob *newblob = ctx->new.blob;
5507 	int bserrno;
5508 
5509 	if (rc != 0) {
5510 		_spdk_bs_clone_snapshot_newblob_cleanup(ctx, rc);
5511 		return;
5512 	}
5513 
5514 	ctx->frozen = true;
5515 
5516 	/* set new back_bs_dev for snapshot */
5517 	newblob->back_bs_dev = origblob->back_bs_dev;
5518 	/* Set invalid flags from origblob */
5519 	newblob->invalid_flags = origblob->invalid_flags;
5520 
5521 	/* inherit parent from original blob if set */
5522 	newblob->parent_id = origblob->parent_id;
5523 	if (origblob->parent_id != SPDK_BLOBID_INVALID) {
5524 		/* Set internal xattr for snapshot id */
5525 		bserrno = _spdk_blob_set_xattr(newblob, BLOB_SNAPSHOT,
5526 					       &origblob->parent_id, sizeof(spdk_blob_id), true);
5527 		if (bserrno != 0) {
5528 			_spdk_bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
5529 			return;
5530 		}
5531 	}
5532 
5533 	/* swap cluster maps */
5534 	_spdk_bs_snapshot_swap_cluster_maps(newblob, origblob);
5535 
5536 	/* Set the clear method on the new blob to match the original. */
5537 	_spdk_blob_set_clear_method(newblob, origblob->clear_method);
5538 
5539 	/* sync snapshot metadata */
5540 	spdk_blob_sync_md(newblob, _spdk_bs_snapshot_newblob_sync_cpl, ctx);
5541 }
5542 
5543 static void
5544 _spdk_bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
5545 {
5546 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5547 	struct spdk_blob *origblob = ctx->original.blob;
5548 	struct spdk_blob *newblob = _blob;
5549 
5550 	if (bserrno != 0) {
5551 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5552 		return;
5553 	}
5554 
5555 	ctx->new.blob = newblob;
5556 	assert(spdk_blob_is_thin_provisioned(newblob));
5557 	assert(spdk_mem_all_zero(newblob->active.clusters,
5558 				 newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
5559 	assert(spdk_mem_all_zero(newblob->active.extent_pages,
5560 				 newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
5561 
5562 	_spdk_blob_freeze_io(origblob, _spdk_bs_snapshot_freeze_cpl, ctx);
5563 }
5564 
5565 static void
5566 _spdk_bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
5567 {
5568 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5569 	struct spdk_blob *origblob = ctx->original.blob;
5570 
5571 	if (bserrno != 0) {
5572 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5573 		return;
5574 	}
5575 
5576 	ctx->new.id = blobid;
5577 	ctx->cpl.u.blobid.blobid = blobid;
5578 
5579 	spdk_bs_open_blob(origblob->bs, ctx->new.id, _spdk_bs_snapshot_newblob_open_cpl, ctx);
5580 }
5581 
5582 
5583 static void
5584 _spdk_bs_xattr_snapshot(void *arg, const char *name,
5585 			const void **value, size_t *value_len)
5586 {
5587 	assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0);
5588 
5589 	struct spdk_blob *blob = (struct spdk_blob *)arg;
5590 	*value = &blob->id;
5591 	*value_len = sizeof(blob->id);
5592 }
5593 
5594 static void
5595 _spdk_bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
5596 {
5597 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5598 	struct spdk_blob_opts opts;
5599 	struct spdk_blob_xattr_opts internal_xattrs;
5600 	char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS };
5601 
5602 	if (bserrno != 0) {
5603 		_spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno);
5604 		return;
5605 	}
5606 
5607 	ctx->original.blob = _blob;
5608 
5609 	if (_blob->data_ro || _blob->md_ro) {
5610 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot create snapshot from read only blob with id %lu\n",
5611 			      _blob->id);
5612 		ctx->bserrno = -EINVAL;
5613 		spdk_blob_close(_blob, _spdk_bs_clone_snapshot_cleanup_finish, ctx);
5614 		return;
5615 	}
5616 
5617 	if (_blob->locked_operation_in_progress) {
5618 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot create snapshot - another operation in progress\n");
5619 		ctx->bserrno = -EBUSY;
5620 		spdk_blob_close(_blob, _spdk_bs_clone_snapshot_cleanup_finish, ctx);
5621 		return;
5622 	}
5623 
5624 	_blob->locked_operation_in_progress = true;
5625 
5626 	spdk_blob_opts_init(&opts);
5627 	_spdk_blob_xattrs_init(&internal_xattrs);
5628 
5629 	/* Change the size of new blob to the same as in original blob,
5630 	 * but do not allocate clusters */
5631 	opts.thin_provision = true;
5632 	opts.num_clusters = spdk_blob_get_num_clusters(_blob);
5633 	opts.use_extent_table = _blob->use_extent_table;
5634 
5635 	/* If there are any xattrs specified for snapshot, set them now */
5636 	if (ctx->xattrs) {
5637 		memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
5638 	}
5639 	/* Set internal xattr SNAPSHOT_IN_PROGRESS */
5640 	internal_xattrs.count = 1;
5641 	internal_xattrs.ctx = _blob;
5642 	internal_xattrs.names = xattrs_names;
5643 	internal_xattrs.get_value = _spdk_bs_xattr_snapshot;
5644 
5645 	_spdk_bs_create_blob(_blob->bs, &opts, &internal_xattrs,
5646 			     _spdk_bs_snapshot_newblob_create_cpl, ctx);
5647 }
5648 
5649 void spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid,
5650 			     const struct spdk_blob_xattr_opts *snapshot_xattrs,
5651 			     spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
5652 {
5653 	struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
5654 
5655 	if (!ctx) {
5656 		cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
5657 		return;
5658 	}
5659 	ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
5660 	ctx->cpl.u.blobid.cb_fn = cb_fn;
5661 	ctx->cpl.u.blobid.cb_arg = cb_arg;
5662 	ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
5663 	ctx->bserrno = 0;
5664 	ctx->frozen = false;
5665 	ctx->original.id = blobid;
5666 	ctx->xattrs = snapshot_xattrs;
5667 
5668 	spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_snapshot_origblob_open_cpl, ctx);
5669 }
5670 /* END spdk_bs_create_snapshot */
5671 
5672 /* START spdk_bs_create_clone */
5673 
5674 static void
5675 _spdk_bs_xattr_clone(void *arg, const char *name,
5676 		     const void **value, size_t *value_len)
5677 {
5678 	assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0);
5679 
5680 	struct spdk_blob *blob = (struct spdk_blob *)arg;
5681 	*value = &blob->id;
5682 	*value_len = sizeof(blob->id);
5683 }
5684 
5685 static void
5686 _spdk_bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
5687 {
5688 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5689 	struct spdk_blob *clone = _blob;
5690 
5691 	ctx->new.blob = clone;
5692 	_spdk_bs_blob_list_add(clone);
5693 
5694 	spdk_blob_close(clone, _spdk_bs_clone_snapshot_origblob_cleanup, ctx);
5695 }
5696 
5697 static void
5698 _spdk_bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
5699 {
5700 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5701 
5702 	ctx->cpl.u.blobid.blobid = blobid;
5703 	spdk_bs_open_blob(ctx->original.blob->bs, blobid, _spdk_bs_clone_newblob_open_cpl, ctx);
5704 }
5705 
5706 static void
5707 _spdk_bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
5708 {
5709 	struct spdk_clone_snapshot_ctx	*ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5710 	struct spdk_blob_opts		opts;
5711 	struct spdk_blob_xattr_opts internal_xattrs;
5712 	char *xattr_names[] = { BLOB_SNAPSHOT };
5713 
5714 	if (bserrno != 0) {
5715 		_spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno);
5716 		return;
5717 	}
5718 
5719 	ctx->original.blob = _blob;
5720 
5721 	if (!_blob->data_ro || !_blob->md_ro) {
5722 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Clone not from read-only blob\n");
5723 		ctx->bserrno = -EINVAL;
5724 		spdk_blob_close(_blob, _spdk_bs_clone_snapshot_cleanup_finish, ctx);
5725 		return;
5726 	}
5727 
5728 	if (_blob->locked_operation_in_progress) {
5729 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot create clone - another operation in progress\n");
5730 		ctx->bserrno = -EBUSY;
5731 		spdk_blob_close(_blob, _spdk_bs_clone_snapshot_cleanup_finish, ctx);
5732 		return;
5733 	}
5734 
5735 	_blob->locked_operation_in_progress = true;
5736 
5737 	spdk_blob_opts_init(&opts);
5738 	_spdk_blob_xattrs_init(&internal_xattrs);
5739 
5740 	opts.thin_provision = true;
5741 	opts.num_clusters = spdk_blob_get_num_clusters(_blob);
5742 	opts.use_extent_table = _blob->use_extent_table;
5743 	if (ctx->xattrs) {
5744 		memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
5745 	}
5746 
5747 	/* Set internal xattr BLOB_SNAPSHOT */
5748 	internal_xattrs.count = 1;
5749 	internal_xattrs.ctx = _blob;
5750 	internal_xattrs.names = xattr_names;
5751 	internal_xattrs.get_value = _spdk_bs_xattr_clone;
5752 
5753 	_spdk_bs_create_blob(_blob->bs, &opts, &internal_xattrs,
5754 			     _spdk_bs_clone_newblob_create_cpl, ctx);
5755 }
5756 
5757 void spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid,
5758 			  const struct spdk_blob_xattr_opts *clone_xattrs,
5759 			  spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
5760 {
5761 	struct spdk_clone_snapshot_ctx	*ctx = calloc(1, sizeof(*ctx));
5762 
5763 	if (!ctx) {
5764 		cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
5765 		return;
5766 	}
5767 
5768 	ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
5769 	ctx->cpl.u.blobid.cb_fn = cb_fn;
5770 	ctx->cpl.u.blobid.cb_arg = cb_arg;
5771 	ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
5772 	ctx->bserrno = 0;
5773 	ctx->xattrs = clone_xattrs;
5774 	ctx->original.id = blobid;
5775 
5776 	spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_clone_origblob_open_cpl, ctx);
5777 }
5778 
5779 /* END spdk_bs_create_clone */
5780 
5781 /* START spdk_bs_inflate_blob */
5782 
5783 static void
5784 _spdk_bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno)
5785 {
5786 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5787 	struct spdk_blob *_blob = ctx->original.blob;
5788 
5789 	if (bserrno != 0) {
5790 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5791 		return;
5792 	}
5793 
5794 	assert(_parent != NULL);
5795 
5796 	_spdk_bs_blob_list_remove(_blob);
5797 	_blob->parent_id = _parent->id;
5798 	_spdk_blob_set_xattr(_blob, BLOB_SNAPSHOT, &_blob->parent_id,
5799 			     sizeof(spdk_blob_id), true);
5800 
5801 	_blob->back_bs_dev->destroy(_blob->back_bs_dev);
5802 	_blob->back_bs_dev = bs_create_blob_bs_dev(_parent);
5803 	_spdk_bs_blob_list_add(_blob);
5804 
5805 	spdk_blob_sync_md(_blob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx);
5806 }
5807 
5808 static void
5809 _spdk_bs_inflate_blob_done(void *cb_arg, int bserrno)
5810 {
5811 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5812 	struct spdk_blob *_blob = ctx->original.blob;
5813 	struct spdk_blob *_parent;
5814 
5815 	if (bserrno != 0) {
5816 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5817 		return;
5818 	}
5819 
5820 	if (ctx->allocate_all) {
5821 		/* remove thin provisioning */
5822 		_spdk_bs_blob_list_remove(_blob);
5823 		_spdk_blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
5824 		_blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV;
5825 		_blob->back_bs_dev->destroy(_blob->back_bs_dev);
5826 		_blob->back_bs_dev = NULL;
5827 		_blob->parent_id = SPDK_BLOBID_INVALID;
5828 	} else {
5829 		_parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob;
5830 		if (_parent->parent_id != SPDK_BLOBID_INVALID) {
5831 			/* We must change the parent of the inflated blob */
5832 			spdk_bs_open_blob(_blob->bs, _parent->parent_id,
5833 					  _spdk_bs_inflate_blob_set_parent_cpl, ctx);
5834 			return;
5835 		}
5836 
5837 		_spdk_bs_blob_list_remove(_blob);
5838 		_spdk_blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
5839 		_blob->parent_id = SPDK_BLOBID_INVALID;
5840 		_blob->back_bs_dev->destroy(_blob->back_bs_dev);
5841 		_blob->back_bs_dev = bs_create_zeroes_dev();
5842 	}
5843 
5844 	_blob->state = SPDK_BLOB_STATE_DIRTY;
5845 	spdk_blob_sync_md(_blob, _spdk_bs_clone_snapshot_origblob_cleanup, ctx);
5846 }
5847 
5848 /* Check if cluster needs allocation */
5849 static inline bool
5850 _spdk_bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all)
5851 {
5852 	struct spdk_blob_bs_dev *b;
5853 
5854 	assert(blob != NULL);
5855 
5856 	if (blob->active.clusters[cluster] != 0) {
5857 		/* Cluster is already allocated */
5858 		return false;
5859 	}
5860 
5861 	if (blob->parent_id == SPDK_BLOBID_INVALID) {
5862 		/* Blob have no parent blob */
5863 		return allocate_all;
5864 	}
5865 
5866 	b = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
5867 	return (allocate_all || b->blob->active.clusters[cluster] != 0);
5868 }
5869 
5870 static void
5871 _spdk_bs_inflate_blob_touch_next(void *cb_arg, int bserrno)
5872 {
5873 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5874 	struct spdk_blob *_blob = ctx->original.blob;
5875 	uint64_t offset;
5876 
5877 	if (bserrno != 0) {
5878 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
5879 		return;
5880 	}
5881 
5882 	for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) {
5883 		if (_spdk_bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) {
5884 			break;
5885 		}
5886 	}
5887 
5888 	if (ctx->cluster < _blob->active.num_clusters) {
5889 		offset = _spdk_bs_cluster_to_lba(_blob->bs, ctx->cluster);
5890 
5891 		/* We may safely increment a cluster before write */
5892 		ctx->cluster++;
5893 
5894 		/* Use zero length write to touch a cluster */
5895 		spdk_blob_io_write(_blob, ctx->channel, NULL, offset, 0,
5896 				   _spdk_bs_inflate_blob_touch_next, ctx);
5897 	} else {
5898 		_spdk_bs_inflate_blob_done(cb_arg, bserrno);
5899 	}
5900 }
5901 
5902 static void
5903 _spdk_bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
5904 {
5905 	struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
5906 	uint64_t lfc; /* lowest free cluster */
5907 	uint64_t i;
5908 
5909 	if (bserrno != 0) {
5910 		_spdk_bs_clone_snapshot_cleanup_finish(ctx, bserrno);
5911 		return;
5912 	}
5913 
5914 	ctx->original.blob = _blob;
5915 
5916 	if (_blob->locked_operation_in_progress) {
5917 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot inflate blob - another operation in progress\n");
5918 		ctx->bserrno = -EBUSY;
5919 		spdk_blob_close(_blob, _spdk_bs_clone_snapshot_cleanup_finish, ctx);
5920 		return;
5921 	}
5922 
5923 	_blob->locked_operation_in_progress = true;
5924 
5925 	if (!ctx->allocate_all && _blob->parent_id == SPDK_BLOBID_INVALID) {
5926 		/* This blob have no parent, so we cannot decouple it. */
5927 		SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n");
5928 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL);
5929 		return;
5930 	}
5931 
5932 	if (spdk_blob_is_thin_provisioned(_blob) == false) {
5933 		/* This is not thin provisioned blob. No need to inflate. */
5934 		_spdk_bs_clone_snapshot_origblob_cleanup(ctx, 0);
5935 		return;
5936 	}
5937 
5938 	/* Do two passes - one to verify that we can obtain enough clusters
5939 	 * and another to actually claim them.
5940 	 */
5941 	lfc = 0;
5942 	for (i = 0; i < _blob->active.num_clusters; i++) {
5943 		if (_spdk_bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) {
5944 			lfc = spdk_bit_array_find_first_clear(_blob->bs->used_clusters, lfc);
5945 			if (lfc == UINT32_MAX) {
5946 				/* No more free clusters. Cannot satisfy the request */
5947 				_spdk_bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC);
5948 				return;
5949 			}
5950 			lfc++;
5951 		}
5952 	}
5953 
5954 	ctx->cluster = 0;
5955 	_spdk_bs_inflate_blob_touch_next(ctx, 0);
5956 }
5957 
5958 static void
5959 _spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
5960 		      spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg)
5961 {
5962 	struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
5963 
5964 	if (!ctx) {
5965 		cb_fn(cb_arg, -ENOMEM);
5966 		return;
5967 	}
5968 	ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
5969 	ctx->cpl.u.bs_basic.cb_fn = cb_fn;
5970 	ctx->cpl.u.bs_basic.cb_arg = cb_arg;
5971 	ctx->bserrno = 0;
5972 	ctx->original.id = blobid;
5973 	ctx->channel = channel;
5974 	ctx->allocate_all = allocate_all;
5975 
5976 	spdk_bs_open_blob(bs, ctx->original.id, _spdk_bs_inflate_blob_open_cpl, ctx);
5977 }
5978 
5979 void
5980 spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
5981 		     spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
5982 {
5983 	_spdk_bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg);
5984 }
5985 
5986 void
5987 spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
5988 			     spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
5989 {
5990 	_spdk_bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg);
5991 }
5992 /* END spdk_bs_inflate_blob */
5993 
5994 /* START spdk_blob_resize */
5995 struct spdk_bs_resize_ctx {
5996 	spdk_blob_op_complete cb_fn;
5997 	void *cb_arg;
5998 	struct spdk_blob *blob;
5999 	uint64_t sz;
6000 	int rc;
6001 };
6002 
6003 static void
6004 _spdk_bs_resize_unfreeze_cpl(void *cb_arg, int rc)
6005 {
6006 	struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
6007 
6008 	if (rc != 0) {
6009 		SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc);
6010 	}
6011 
6012 	if (ctx->rc != 0) {
6013 		SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc);
6014 		rc = ctx->rc;
6015 	}
6016 
6017 	ctx->blob->locked_operation_in_progress = false;
6018 
6019 	ctx->cb_fn(ctx->cb_arg, rc);
6020 	free(ctx);
6021 }
6022 
6023 static void
6024 _spdk_bs_resize_freeze_cpl(void *cb_arg, int rc)
6025 {
6026 	struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
6027 
6028 	if (rc != 0) {
6029 		ctx->blob->locked_operation_in_progress = false;
6030 		ctx->cb_fn(ctx->cb_arg, rc);
6031 		free(ctx);
6032 		return;
6033 	}
6034 
6035 	ctx->rc = _spdk_blob_resize(ctx->blob, ctx->sz);
6036 
6037 	_spdk_blob_unfreeze_io(ctx->blob, _spdk_bs_resize_unfreeze_cpl, ctx);
6038 }
6039 
6040 void
6041 spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg)
6042 {
6043 	struct spdk_bs_resize_ctx *ctx;
6044 
6045 	_spdk_blob_verify_md_op(blob);
6046 
6047 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Resizing blob %lu to %lu clusters\n", blob->id, sz);
6048 
6049 	if (blob->md_ro) {
6050 		cb_fn(cb_arg, -EPERM);
6051 		return;
6052 	}
6053 
6054 	if (sz == blob->active.num_clusters) {
6055 		cb_fn(cb_arg, 0);
6056 		return;
6057 	}
6058 
6059 	if (blob->locked_operation_in_progress) {
6060 		cb_fn(cb_arg, -EBUSY);
6061 		return;
6062 	}
6063 
6064 	ctx = calloc(1, sizeof(*ctx));
6065 	if (!ctx) {
6066 		cb_fn(cb_arg, -ENOMEM);
6067 		return;
6068 	}
6069 
6070 	blob->locked_operation_in_progress = true;
6071 	ctx->cb_fn = cb_fn;
6072 	ctx->cb_arg = cb_arg;
6073 	ctx->blob = blob;
6074 	ctx->sz = sz;
6075 	_spdk_blob_freeze_io(blob, _spdk_bs_resize_freeze_cpl, ctx);
6076 }
6077 
6078 /* END spdk_blob_resize */
6079 
6080 
6081 /* START spdk_bs_delete_blob */
6082 
6083 static void
6084 _spdk_bs_delete_close_cpl(void *cb_arg, int bserrno)
6085 {
6086 	spdk_bs_sequence_t *seq = cb_arg;
6087 
6088 	bs_sequence_finish(seq, bserrno);
6089 }
6090 
6091 static void
6092 _spdk_bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6093 {
6094 	struct spdk_blob *blob = cb_arg;
6095 
6096 	if (bserrno != 0) {
6097 		/*
6098 		 * We already removed this blob from the blobstore tailq, so
6099 		 *  we need to free it here since this is the last reference
6100 		 *  to it.
6101 		 */
6102 		_spdk_blob_free(blob);
6103 		_spdk_bs_delete_close_cpl(seq, bserrno);
6104 		return;
6105 	}
6106 
6107 	/*
6108 	 * This will immediately decrement the ref_count and call
6109 	 *  the completion routine since the metadata state is clean.
6110 	 *  By calling spdk_blob_close, we reduce the number of call
6111 	 *  points into code that touches the blob->open_ref count
6112 	 *  and the blobstore's blob list.
6113 	 */
6114 	spdk_blob_close(blob, _spdk_bs_delete_close_cpl, seq);
6115 }
6116 
6117 struct delete_snapshot_ctx {
6118 	struct spdk_blob_list *parent_snapshot_entry;
6119 	struct spdk_blob *snapshot;
6120 	bool snapshot_md_ro;
6121 	struct spdk_blob *clone;
6122 	bool clone_md_ro;
6123 	spdk_blob_op_with_handle_complete cb_fn;
6124 	void *cb_arg;
6125 	int bserrno;
6126 };
6127 
6128 static void
6129 _spdk_delete_blob_cleanup_finish(void *cb_arg, int bserrno)
6130 {
6131 	struct delete_snapshot_ctx *ctx = cb_arg;
6132 
6133 	if (bserrno != 0) {
6134 		SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno);
6135 	}
6136 
6137 	assert(ctx != NULL);
6138 
6139 	if (bserrno != 0 && ctx->bserrno == 0) {
6140 		ctx->bserrno = bserrno;
6141 	}
6142 
6143 	ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno);
6144 	free(ctx);
6145 }
6146 
6147 static void
6148 _spdk_delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno)
6149 {
6150 	struct delete_snapshot_ctx *ctx = cb_arg;
6151 
6152 	if (bserrno != 0) {
6153 		ctx->bserrno = bserrno;
6154 		SPDK_ERRLOG("Clone cleanup error %d\n", bserrno);
6155 	}
6156 
6157 	if (ctx->bserrno != 0) {
6158 		assert(_spdk_blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL);
6159 		TAILQ_INSERT_HEAD(&ctx->snapshot->bs->blobs, ctx->snapshot, link);
6160 	}
6161 
6162 	ctx->snapshot->locked_operation_in_progress = false;
6163 	ctx->snapshot->md_ro = ctx->snapshot_md_ro;
6164 
6165 	spdk_blob_close(ctx->snapshot, _spdk_delete_blob_cleanup_finish, ctx);
6166 }
6167 
6168 static void
6169 _spdk_delete_snapshot_cleanup_clone(void *cb_arg, int bserrno)
6170 {
6171 	struct delete_snapshot_ctx *ctx = cb_arg;
6172 
6173 	ctx->clone->locked_operation_in_progress = false;
6174 	ctx->clone->md_ro = ctx->clone_md_ro;
6175 
6176 	spdk_blob_close(ctx->clone, _spdk_delete_snapshot_cleanup_snapshot, ctx);
6177 }
6178 
6179 static void
6180 _spdk_delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
6181 {
6182 	struct delete_snapshot_ctx *ctx = cb_arg;
6183 
6184 	if (bserrno) {
6185 		ctx->bserrno = bserrno;
6186 		_spdk_delete_snapshot_cleanup_clone(ctx, 0);
6187 		return;
6188 	}
6189 
6190 	ctx->clone->locked_operation_in_progress = false;
6191 	spdk_blob_close(ctx->clone, _spdk_delete_blob_cleanup_finish, ctx);
6192 }
6193 
6194 static void
6195 _spdk_delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno)
6196 {
6197 	struct delete_snapshot_ctx *ctx = cb_arg;
6198 	struct spdk_blob_list *parent_snapshot_entry = NULL;
6199 	struct spdk_blob_list *snapshot_entry = NULL;
6200 	struct spdk_blob_list *clone_entry = NULL;
6201 	struct spdk_blob_list *snapshot_clone_entry = NULL;
6202 
6203 	if (bserrno) {
6204 		SPDK_ERRLOG("Failed to sync MD on blob\n");
6205 		ctx->bserrno = bserrno;
6206 		_spdk_delete_snapshot_cleanup_clone(ctx, 0);
6207 		return;
6208 	}
6209 
6210 	/* Get snapshot entry for the snapshot we want to remove */
6211 	snapshot_entry = _spdk_bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id);
6212 
6213 	assert(snapshot_entry != NULL);
6214 
6215 	/* Remove clone entry in this snapshot (at this point there can be only one clone) */
6216 	clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
6217 	assert(clone_entry != NULL);
6218 	TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
6219 	snapshot_entry->clone_count--;
6220 	assert(TAILQ_EMPTY(&snapshot_entry->clones));
6221 
6222 	if (ctx->snapshot->parent_id != SPDK_BLOBID_INVALID) {
6223 		/* This snapshot is at the same time a clone of another snapshot - we need to
6224 		 * update parent snapshot (remove current clone, add new one inherited from
6225 		 * the snapshot that is being removed) */
6226 
6227 		/* Get snapshot entry for parent snapshot and clone entry within that snapshot for
6228 		 * snapshot that we are removing */
6229 		_spdk_blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry,
6230 				&snapshot_clone_entry);
6231 
6232 		/* Switch clone entry in parent snapshot */
6233 		TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link);
6234 		TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link);
6235 		free(snapshot_clone_entry);
6236 	} else {
6237 		/* No parent snapshot - just remove clone entry */
6238 		free(clone_entry);
6239 	}
6240 
6241 	/* Restore md_ro flags */
6242 	ctx->clone->md_ro = ctx->clone_md_ro;
6243 	ctx->snapshot->md_ro = ctx->snapshot_md_ro;
6244 
6245 	_spdk_blob_unfreeze_io(ctx->clone, _spdk_delete_snapshot_unfreeze_cpl, ctx);
6246 }
6247 
6248 static void
6249 _spdk_delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno)
6250 {
6251 	struct delete_snapshot_ctx *ctx = cb_arg;
6252 	uint64_t i;
6253 
6254 	ctx->snapshot->md_ro = false;
6255 
6256 	if (bserrno) {
6257 		SPDK_ERRLOG("Failed to sync MD on clone\n");
6258 		ctx->bserrno = bserrno;
6259 
6260 		/* Restore snapshot to previous state */
6261 		bserrno = _spdk_blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
6262 		if (bserrno != 0) {
6263 			_spdk_delete_snapshot_cleanup_clone(ctx, bserrno);
6264 			return;
6265 		}
6266 
6267 		spdk_blob_sync_md(ctx->snapshot, _spdk_delete_snapshot_cleanup_clone, ctx);
6268 		return;
6269 	}
6270 
6271 	/* Clear cluster map entries for snapshot */
6272 	for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
6273 		if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) {
6274 			ctx->snapshot->active.clusters[i] = 0;
6275 		}
6276 	}
6277 	for (i = 0; i < ctx->snapshot->active.num_extent_pages &&
6278 	     i < ctx->clone->active.num_extent_pages; i++) {
6279 		if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) {
6280 			ctx->snapshot->active.extent_pages[i] = 0;
6281 		}
6282 	}
6283 
6284 	_spdk_blob_set_thin_provision(ctx->snapshot);
6285 	ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY;
6286 
6287 	if (ctx->parent_snapshot_entry != NULL) {
6288 		ctx->snapshot->back_bs_dev = NULL;
6289 	}
6290 
6291 	spdk_blob_sync_md(ctx->snapshot, _spdk_delete_snapshot_sync_snapshot_cpl, ctx);
6292 }
6293 
6294 static void
6295 _spdk_delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno)
6296 {
6297 	struct delete_snapshot_ctx *ctx = cb_arg;
6298 	uint64_t i;
6299 
6300 	/* Temporarily override md_ro flag for clone for MD modification */
6301 	ctx->clone_md_ro = ctx->clone->md_ro;
6302 	ctx->clone->md_ro = false;
6303 
6304 	if (bserrno) {
6305 		SPDK_ERRLOG("Failed to sync MD with xattr on blob\n");
6306 		ctx->bserrno = bserrno;
6307 		_spdk_delete_snapshot_cleanup_clone(ctx, 0);
6308 		return;
6309 	}
6310 
6311 	/* Copy snapshot map to clone map (only unallocated clusters in clone) */
6312 	for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
6313 		if (ctx->clone->active.clusters[i] == 0) {
6314 			ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i];
6315 		}
6316 	}
6317 	for (i = 0; i < ctx->snapshot->active.num_extent_pages &&
6318 	     i < ctx->clone->active.num_extent_pages; i++) {
6319 		if (ctx->clone->active.extent_pages[i] == 0) {
6320 			ctx->clone->active.extent_pages[i] = ctx->snapshot->active.extent_pages[i];
6321 		}
6322 	}
6323 
6324 	/* Delete old backing bs_dev from clone (related to snapshot that will be removed) */
6325 	ctx->clone->back_bs_dev->destroy(ctx->clone->back_bs_dev);
6326 
6327 	/* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */
6328 	if (ctx->parent_snapshot_entry != NULL) {
6329 		/* ...to parent snapshot */
6330 		ctx->clone->parent_id = ctx->parent_snapshot_entry->id;
6331 		ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
6332 		_spdk_blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id,
6333 				     sizeof(spdk_blob_id),
6334 				     true);
6335 	} else {
6336 		/* ...to blobid invalid and zeroes dev */
6337 		ctx->clone->parent_id = SPDK_BLOBID_INVALID;
6338 		ctx->clone->back_bs_dev = bs_create_zeroes_dev();
6339 		_spdk_blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true);
6340 	}
6341 
6342 	spdk_blob_sync_md(ctx->clone, _spdk_delete_snapshot_sync_clone_cpl, ctx);
6343 }
6344 
6345 static void
6346 _spdk_delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno)
6347 {
6348 	struct delete_snapshot_ctx *ctx = cb_arg;
6349 
6350 	if (bserrno) {
6351 		SPDK_ERRLOG("Failed to freeze I/O on clone\n");
6352 		ctx->bserrno = bserrno;
6353 		_spdk_delete_snapshot_cleanup_clone(ctx, 0);
6354 		return;
6355 	}
6356 
6357 	/* Temporarily override md_ro flag for snapshot for MD modification */
6358 	ctx->snapshot_md_ro = ctx->snapshot->md_ro;
6359 	ctx->snapshot->md_ro = false;
6360 
6361 	/* Mark blob as pending for removal for power failure safety, use clone id for recovery */
6362 	ctx->bserrno = _spdk_blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id,
6363 					    sizeof(spdk_blob_id), true);
6364 	if (ctx->bserrno != 0) {
6365 		_spdk_delete_snapshot_cleanup_clone(ctx, 0);
6366 		return;
6367 	}
6368 
6369 	spdk_blob_sync_md(ctx->snapshot, _spdk_delete_snapshot_sync_snapshot_xattr_cpl, ctx);
6370 }
6371 
6372 static void
6373 _spdk_delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno)
6374 {
6375 	struct delete_snapshot_ctx *ctx = cb_arg;
6376 
6377 	if (bserrno) {
6378 		SPDK_ERRLOG("Failed to open clone\n");
6379 		ctx->bserrno = bserrno;
6380 		_spdk_delete_snapshot_cleanup_snapshot(ctx, 0);
6381 		return;
6382 	}
6383 
6384 	ctx->clone = clone;
6385 
6386 	if (clone->locked_operation_in_progress) {
6387 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot remove blob - another operation in progress on its clone\n");
6388 		ctx->bserrno = -EBUSY;
6389 		spdk_blob_close(ctx->clone, _spdk_delete_snapshot_cleanup_snapshot, ctx);
6390 		return;
6391 	}
6392 
6393 	clone->locked_operation_in_progress = true;
6394 
6395 	_spdk_blob_freeze_io(clone, _spdk_delete_snapshot_freeze_io_cb, ctx);
6396 }
6397 
6398 static void
6399 _spdk_update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx)
6400 {
6401 	struct spdk_blob_list *snapshot_entry = NULL;
6402 	struct spdk_blob_list *clone_entry = NULL;
6403 	struct spdk_blob_list *snapshot_clone_entry = NULL;
6404 
6405 	/* Get snapshot entry for the snapshot we want to remove */
6406 	snapshot_entry = _spdk_bs_get_snapshot_entry(snapshot->bs, snapshot->id);
6407 
6408 	assert(snapshot_entry != NULL);
6409 
6410 	/* Get clone of the snapshot (at this point there can be only one clone) */
6411 	clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
6412 	assert(snapshot_entry->clone_count == 1);
6413 	assert(clone_entry != NULL);
6414 
6415 	/* Get snapshot entry for parent snapshot and clone entry within that snapshot for
6416 	 * snapshot that we are removing */
6417 	_spdk_blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry,
6418 			&snapshot_clone_entry);
6419 
6420 	spdk_bs_open_blob(snapshot->bs, clone_entry->id, _spdk_delete_snapshot_open_clone_cb, ctx);
6421 }
6422 
6423 static void
6424 _spdk_bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno)
6425 {
6426 	spdk_bs_sequence_t *seq = cb_arg;
6427 	struct spdk_blob_list *snapshot_entry = NULL;
6428 	uint32_t page_num;
6429 
6430 	if (bserrno) {
6431 		SPDK_ERRLOG("Failed to remove blob\n");
6432 		bs_sequence_finish(seq, bserrno);
6433 		return;
6434 	}
6435 
6436 	/* Remove snapshot from the list */
6437 	snapshot_entry = _spdk_bs_get_snapshot_entry(blob->bs, blob->id);
6438 	if (snapshot_entry != NULL) {
6439 		TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link);
6440 		free(snapshot_entry);
6441 	}
6442 
6443 	page_num = _spdk_bs_blobid_to_page(blob->id);
6444 	spdk_bit_array_clear(blob->bs->used_blobids, page_num);
6445 	blob->state = SPDK_BLOB_STATE_DIRTY;
6446 	blob->active.num_pages = 0;
6447 	_spdk_blob_resize(blob, 0);
6448 
6449 	_spdk_blob_persist(seq, blob, _spdk_bs_delete_persist_cpl, blob);
6450 }
6451 
6452 static int
6453 _spdk_bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone)
6454 {
6455 	struct spdk_blob_list *snapshot_entry = NULL;
6456 	struct spdk_blob_list *clone_entry = NULL;
6457 	struct spdk_blob *clone = NULL;
6458 	bool has_one_clone = false;
6459 
6460 	/* Check if this is a snapshot with clones */
6461 	snapshot_entry = _spdk_bs_get_snapshot_entry(blob->bs, blob->id);
6462 	if (snapshot_entry != NULL) {
6463 		if (snapshot_entry->clone_count > 1) {
6464 			SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n");
6465 			return -EBUSY;
6466 		} else if (snapshot_entry->clone_count == 1) {
6467 			has_one_clone = true;
6468 		}
6469 	}
6470 
6471 	/* Check if someone has this blob open (besides this delete context):
6472 	 * - open_ref = 1 - only this context opened blob, so it is ok to remove it
6473 	 * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot
6474 	 *	and that is ok, because we will update it accordingly */
6475 	if (blob->open_ref <= 2 && has_one_clone) {
6476 		clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
6477 		assert(clone_entry != NULL);
6478 		clone = _spdk_blob_lookup(blob->bs, clone_entry->id);
6479 
6480 		if (blob->open_ref == 2 && clone == NULL) {
6481 			/* Clone is closed and someone else opened this blob */
6482 			SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
6483 			return -EBUSY;
6484 		}
6485 
6486 		*update_clone = true;
6487 		return 0;
6488 	}
6489 
6490 	if (blob->open_ref > 1) {
6491 		SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
6492 		return -EBUSY;
6493 	}
6494 
6495 	assert(has_one_clone == false);
6496 	*update_clone = false;
6497 	return 0;
6498 }
6499 
6500 static void
6501 _spdk_bs_delete_enomem_close_cpl(void *cb_arg, int bserrno)
6502 {
6503 	spdk_bs_sequence_t *seq = cb_arg;
6504 
6505 	bs_sequence_finish(seq, -ENOMEM);
6506 }
6507 
6508 static void
6509 _spdk_bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
6510 {
6511 	spdk_bs_sequence_t *seq = cb_arg;
6512 	struct delete_snapshot_ctx *ctx;
6513 	bool update_clone = false;
6514 
6515 	if (bserrno != 0) {
6516 		bs_sequence_finish(seq, bserrno);
6517 		return;
6518 	}
6519 
6520 	_spdk_blob_verify_md_op(blob);
6521 
6522 	ctx = calloc(1, sizeof(*ctx));
6523 	if (ctx == NULL) {
6524 		spdk_blob_close(blob, _spdk_bs_delete_enomem_close_cpl, seq);
6525 		return;
6526 	}
6527 
6528 	ctx->snapshot = blob;
6529 	ctx->cb_fn = _spdk_bs_delete_blob_finish;
6530 	ctx->cb_arg = seq;
6531 
6532 	/* Check if blob can be removed and if it is a snapshot with clone on top of it */
6533 	ctx->bserrno = _spdk_bs_is_blob_deletable(blob, &update_clone);
6534 	if (ctx->bserrno) {
6535 		spdk_blob_close(blob, _spdk_delete_blob_cleanup_finish, ctx);
6536 		return;
6537 	}
6538 
6539 	if (blob->locked_operation_in_progress) {
6540 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Cannot remove blob - another operation in progress\n");
6541 		ctx->bserrno = -EBUSY;
6542 		spdk_blob_close(blob, _spdk_delete_blob_cleanup_finish, ctx);
6543 		return;
6544 	}
6545 
6546 	blob->locked_operation_in_progress = true;
6547 
6548 	/*
6549 	 * Remove the blob from the blob_store list now, to ensure it does not
6550 	 *  get returned after this point by _spdk_blob_lookup().
6551 	 */
6552 	TAILQ_REMOVE(&blob->bs->blobs, blob, link);
6553 
6554 	if (update_clone) {
6555 		/* This blob is a snapshot with active clone - update clone first */
6556 		_spdk_update_clone_on_snapshot_deletion(blob, ctx);
6557 	} else {
6558 		/* This blob does not have any clones - just remove it */
6559 		_spdk_bs_blob_list_remove(blob);
6560 		_spdk_bs_delete_blob_finish(seq, blob, 0);
6561 		free(ctx);
6562 	}
6563 }
6564 
6565 void
6566 spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
6567 		    spdk_blob_op_complete cb_fn, void *cb_arg)
6568 {
6569 	struct spdk_bs_cpl	cpl;
6570 	spdk_bs_sequence_t	*seq;
6571 
6572 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Deleting blob %lu\n", blobid);
6573 
6574 	assert(spdk_get_thread() == bs->md_thread);
6575 
6576 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
6577 	cpl.u.blob_basic.cb_fn = cb_fn;
6578 	cpl.u.blob_basic.cb_arg = cb_arg;
6579 
6580 	seq = bs_sequence_start(bs->md_channel, &cpl);
6581 	if (!seq) {
6582 		cb_fn(cb_arg, -ENOMEM);
6583 		return;
6584 	}
6585 
6586 	spdk_bs_open_blob(bs, blobid, _spdk_bs_delete_open_cpl, seq);
6587 }
6588 
6589 /* END spdk_bs_delete_blob */
6590 
6591 /* START spdk_bs_open_blob */
6592 
6593 static void
6594 _spdk_bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6595 {
6596 	struct spdk_blob *blob = cb_arg;
6597 
6598 	if (bserrno != 0) {
6599 		_spdk_blob_free(blob);
6600 		seq->cpl.u.blob_handle.blob = NULL;
6601 		bs_sequence_finish(seq, bserrno);
6602 		return;
6603 	}
6604 
6605 	blob->open_ref++;
6606 
6607 	TAILQ_INSERT_HEAD(&blob->bs->blobs, blob, link);
6608 
6609 	bs_sequence_finish(seq, bserrno);
6610 }
6611 
6612 static void _spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
6613 			       struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
6614 {
6615 	struct spdk_blob		*blob;
6616 	struct spdk_bs_cpl		cpl;
6617 	struct spdk_blob_open_opts	opts_default;
6618 	spdk_bs_sequence_t		*seq;
6619 	uint32_t			page_num;
6620 
6621 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Opening blob %lu\n", blobid);
6622 	assert(spdk_get_thread() == bs->md_thread);
6623 
6624 	page_num = _spdk_bs_blobid_to_page(blobid);
6625 	if (spdk_bit_array_get(bs->used_blobids, page_num) == false) {
6626 		/* Invalid blobid */
6627 		cb_fn(cb_arg, NULL, -ENOENT);
6628 		return;
6629 	}
6630 
6631 	blob = _spdk_blob_lookup(bs, blobid);
6632 	if (blob) {
6633 		blob->open_ref++;
6634 		cb_fn(cb_arg, blob, 0);
6635 		return;
6636 	}
6637 
6638 	blob = _spdk_blob_alloc(bs, blobid);
6639 	if (!blob) {
6640 		cb_fn(cb_arg, NULL, -ENOMEM);
6641 		return;
6642 	}
6643 
6644 	if (!opts) {
6645 		spdk_blob_open_opts_init(&opts_default);
6646 		opts = &opts_default;
6647 	}
6648 
6649 	blob->clear_method = opts->clear_method;
6650 
6651 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
6652 	cpl.u.blob_handle.cb_fn = cb_fn;
6653 	cpl.u.blob_handle.cb_arg = cb_arg;
6654 	cpl.u.blob_handle.blob = blob;
6655 
6656 	seq = bs_sequence_start(bs->md_channel, &cpl);
6657 	if (!seq) {
6658 		_spdk_blob_free(blob);
6659 		cb_fn(cb_arg, NULL, -ENOMEM);
6660 		return;
6661 	}
6662 
6663 	_spdk_blob_load(seq, blob, _spdk_bs_open_blob_cpl, blob);
6664 }
6665 
6666 void spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
6667 		       spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
6668 {
6669 	_spdk_bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg);
6670 }
6671 
6672 void spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid,
6673 			   struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
6674 {
6675 	_spdk_bs_open_blob(bs, blobid, opts, cb_fn, cb_arg);
6676 }
6677 
6678 /* END spdk_bs_open_blob */
6679 
6680 /* START spdk_blob_set_read_only */
6681 int spdk_blob_set_read_only(struct spdk_blob *blob)
6682 {
6683 	_spdk_blob_verify_md_op(blob);
6684 
6685 	blob->data_ro_flags |= SPDK_BLOB_READ_ONLY;
6686 
6687 	blob->state = SPDK_BLOB_STATE_DIRTY;
6688 	return 0;
6689 }
6690 /* END spdk_blob_set_read_only */
6691 
6692 /* START spdk_blob_sync_md */
6693 
6694 static void
6695 _spdk_blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6696 {
6697 	struct spdk_blob *blob = cb_arg;
6698 
6699 	if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
6700 		blob->data_ro = true;
6701 		blob->md_ro = true;
6702 	}
6703 
6704 	bs_sequence_finish(seq, bserrno);
6705 }
6706 
6707 static void
6708 _spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
6709 {
6710 	struct spdk_bs_cpl	cpl;
6711 	spdk_bs_sequence_t	*seq;
6712 
6713 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
6714 	cpl.u.blob_basic.cb_fn = cb_fn;
6715 	cpl.u.blob_basic.cb_arg = cb_arg;
6716 
6717 	seq = bs_sequence_start(blob->bs->md_channel, &cpl);
6718 	if (!seq) {
6719 		cb_fn(cb_arg, -ENOMEM);
6720 		return;
6721 	}
6722 
6723 	_spdk_blob_persist(seq, blob, _spdk_blob_sync_md_cpl, blob);
6724 }
6725 
6726 void
6727 spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
6728 {
6729 	_spdk_blob_verify_md_op(blob);
6730 
6731 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Syncing blob %lu\n", blob->id);
6732 
6733 	if (blob->md_ro) {
6734 		assert(blob->state == SPDK_BLOB_STATE_CLEAN);
6735 		cb_fn(cb_arg, 0);
6736 		return;
6737 	}
6738 
6739 	_spdk_blob_sync_md(blob, cb_fn, cb_arg);
6740 }
6741 
6742 /* END spdk_blob_sync_md */
6743 
6744 struct spdk_blob_insert_cluster_ctx {
6745 	struct spdk_thread	*thread;
6746 	struct spdk_blob	*blob;
6747 	uint32_t		cluster_num;	/* cluster index in blob */
6748 	uint32_t		cluster;	/* cluster on disk */
6749 	uint32_t		extent_page;	/* extent page on disk */
6750 	int			rc;
6751 	spdk_blob_op_complete	cb_fn;
6752 	void			*cb_arg;
6753 };
6754 
6755 static void
6756 _spdk_blob_insert_cluster_msg_cpl(void *arg)
6757 {
6758 	struct spdk_blob_insert_cluster_ctx *ctx = arg;
6759 
6760 	ctx->cb_fn(ctx->cb_arg, ctx->rc);
6761 	free(ctx);
6762 }
6763 
6764 static void
6765 _spdk_blob_insert_cluster_msg_cb(void *arg, int bserrno)
6766 {
6767 	struct spdk_blob_insert_cluster_ctx *ctx = arg;
6768 
6769 	ctx->rc = bserrno;
6770 	spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx);
6771 }
6772 
6773 static void
6774 _spdk_blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6775 {
6776 	struct spdk_blob_md_page        *page = cb_arg;
6777 
6778 	bs_sequence_finish(seq, bserrno);
6779 	spdk_free(page);
6780 }
6781 
6782 static void
6783 _spdk_blob_insert_extent(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
6784 			 spdk_blob_op_complete cb_fn, void *cb_arg)
6785 {
6786 	spdk_bs_sequence_t		*seq;
6787 	struct spdk_bs_cpl		cpl;
6788 	struct spdk_blob_md_page	*page = NULL;
6789 	uint32_t			page_count = 0;
6790 	int				rc;
6791 
6792 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
6793 	cpl.u.blob_basic.cb_fn = cb_fn;
6794 	cpl.u.blob_basic.cb_arg = cb_arg;
6795 
6796 	seq = bs_sequence_start(blob->bs->md_channel, &cpl);
6797 	if (!seq) {
6798 		cb_fn(cb_arg, -ENOMEM);
6799 		return;
6800 	}
6801 	rc = _spdk_blob_serialize_add_page(blob, &page, &page_count, &page);
6802 	if (rc < 0) {
6803 		bs_sequence_finish(seq, rc);
6804 		return;
6805 	}
6806 
6807 	_spdk_blob_serialize_extent_page(blob, cluster_num, page);
6808 
6809 	page->crc = _spdk_blob_md_page_calc_crc(page);
6810 
6811 	assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true);
6812 
6813 	bs_sequence_write_dev(seq, page, _spdk_bs_md_page_to_lba(blob->bs, extent),
6814 			      _spdk_bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
6815 			      _spdk_blob_persist_extent_page_cpl, page);
6816 }
6817 
6818 static void
6819 _spdk_blob_insert_cluster_msg(void *arg)
6820 {
6821 	struct spdk_blob_insert_cluster_ctx *ctx = arg;
6822 	uint32_t *extent_page;
6823 
6824 	ctx->rc = _spdk_blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster);
6825 	if (ctx->rc != 0) {
6826 		spdk_thread_send_msg(ctx->thread, _spdk_blob_insert_cluster_msg_cpl, ctx);
6827 		return;
6828 	}
6829 
6830 	if (ctx->blob->use_extent_table == false) {
6831 		/* Extent table is not used, proceed with sync of md that will only use extents_rle. */
6832 		ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
6833 		_spdk_blob_sync_md(ctx->blob, _spdk_blob_insert_cluster_msg_cb, ctx);
6834 		return;
6835 	}
6836 
6837 	extent_page = _spdk_bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
6838 	if (*extent_page == 0) {
6839 		/* Extent page requires allocation.
6840 		 * It was already claimed in the used_md_pages map and placed in ctx.
6841 		 * Blob persist will take care of writing out new extent page on disk. */
6842 		assert(ctx->extent_page != 0);
6843 		assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
6844 		*extent_page = ctx->extent_page;
6845 		ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
6846 		_spdk_blob_sync_md(ctx->blob, _spdk_blob_insert_cluster_msg_cb, ctx);
6847 	} else {
6848 		/* It is possible for original thread to allocate extent page for
6849 		 * different cluster in the same extent page. In such case proceed with
6850 		 * updating the existing extent page, but release the additional one. */
6851 		if (ctx->extent_page != 0) {
6852 			assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
6853 			_spdk_bs_release_md_page(ctx->blob->bs, ctx->extent_page);
6854 		}
6855 		/* Extent page already allocated.
6856 		 * Every cluster allocation, requires just an update of single extent page. */
6857 		_spdk_blob_insert_extent(ctx->blob, *extent_page, ctx->cluster_num,
6858 					 _spdk_blob_insert_cluster_msg_cb, ctx);
6859 	}
6860 }
6861 
6862 static void
6863 _spdk_blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
6864 				       uint64_t cluster, uint32_t extent_page, spdk_blob_op_complete cb_fn, void *cb_arg)
6865 {
6866 	struct spdk_blob_insert_cluster_ctx *ctx;
6867 
6868 	ctx = calloc(1, sizeof(*ctx));
6869 	if (ctx == NULL) {
6870 		cb_fn(cb_arg, -ENOMEM);
6871 		return;
6872 	}
6873 
6874 	ctx->thread = spdk_get_thread();
6875 	ctx->blob = blob;
6876 	ctx->cluster_num = cluster_num;
6877 	ctx->cluster = cluster;
6878 	ctx->extent_page = extent_page;
6879 	ctx->cb_fn = cb_fn;
6880 	ctx->cb_arg = cb_arg;
6881 
6882 	spdk_thread_send_msg(blob->bs->md_thread, _spdk_blob_insert_cluster_msg, ctx);
6883 }
6884 
6885 /* START spdk_blob_close */
6886 
6887 static void
6888 _spdk_blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
6889 {
6890 	struct spdk_blob *blob = cb_arg;
6891 
6892 	if (bserrno == 0) {
6893 		blob->open_ref--;
6894 		if (blob->open_ref == 0) {
6895 			/*
6896 			 * Blobs with active.num_pages == 0 are deleted blobs.
6897 			 *  these blobs are removed from the blob_store list
6898 			 *  when the deletion process starts - so don't try to
6899 			 *  remove them again.
6900 			 */
6901 			if (blob->active.num_pages > 0) {
6902 				TAILQ_REMOVE(&blob->bs->blobs, blob, link);
6903 			}
6904 			_spdk_blob_free(blob);
6905 		}
6906 	}
6907 
6908 	bs_sequence_finish(seq, bserrno);
6909 }
6910 
6911 void spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
6912 {
6913 	struct spdk_bs_cpl	cpl;
6914 	spdk_bs_sequence_t	*seq;
6915 
6916 	_spdk_blob_verify_md_op(blob);
6917 
6918 	SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Closing blob %lu\n", blob->id);
6919 
6920 	if (blob->open_ref == 0) {
6921 		cb_fn(cb_arg, -EBADF);
6922 		return;
6923 	}
6924 
6925 	cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
6926 	cpl.u.blob_basic.cb_fn = cb_fn;
6927 	cpl.u.blob_basic.cb_arg = cb_arg;
6928 
6929 	seq = bs_sequence_start(blob->bs->md_channel, &cpl);
6930 	if (!seq) {
6931 		cb_fn(cb_arg, -ENOMEM);
6932 		return;
6933 	}
6934 
6935 	/* Sync metadata */
6936 	_spdk_blob_persist(seq, blob, _spdk_blob_close_cpl, blob);
6937 }
6938 
6939 /* END spdk_blob_close */
6940 
6941 struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
6942 {
6943 	return spdk_get_io_channel(bs);
6944 }
6945 
6946 void spdk_bs_free_io_channel(struct spdk_io_channel *channel)
6947 {
6948 	spdk_put_io_channel(channel);
6949 }
6950 
6951 void spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
6952 			uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
6953 {
6954 	_spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
6955 				     SPDK_BLOB_UNMAP);
6956 }
6957 
6958 void spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
6959 			       uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
6960 {
6961 	_spdk_blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
6962 				     SPDK_BLOB_WRITE_ZEROES);
6963 }
6964 
6965 void spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
6966 			void *payload, uint64_t offset, uint64_t length,
6967 			spdk_blob_op_complete cb_fn, void *cb_arg)
6968 {
6969 	_spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
6970 				     SPDK_BLOB_WRITE);
6971 }
6972 
6973 void spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
6974 		       void *payload, uint64_t offset, uint64_t length,
6975 		       spdk_blob_op_complete cb_fn, void *cb_arg)
6976 {
6977 	_spdk_blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
6978 				     SPDK_BLOB_READ);
6979 }
6980 
6981 void spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
6982 			 struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
6983 			 spdk_blob_op_complete cb_fn, void *cb_arg)
6984 {
6985 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false);
6986 }
6987 
6988 void spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
6989 			struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
6990 			spdk_blob_op_complete cb_fn, void *cb_arg)
6991 {
6992 	_spdk_blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true);
6993 }
6994 
6995 struct spdk_bs_iter_ctx {
6996 	int64_t page_num;
6997 	struct spdk_blob_store *bs;
6998 
6999 	spdk_blob_op_with_handle_complete cb_fn;
7000 	void *cb_arg;
7001 };
7002 
7003 static void
7004 _spdk_bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
7005 {
7006 	struct spdk_bs_iter_ctx *ctx = cb_arg;
7007 	struct spdk_blob_store *bs = ctx->bs;
7008 	spdk_blob_id id;
7009 
7010 	if (bserrno == 0) {
7011 		ctx->cb_fn(ctx->cb_arg, _blob, bserrno);
7012 		free(ctx);
7013 		return;
7014 	}
7015 
7016 	ctx->page_num++;
7017 	ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num);
7018 	if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) {
7019 		ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
7020 		free(ctx);
7021 		return;
7022 	}
7023 
7024 	id = _spdk_bs_page_to_blobid(ctx->page_num);
7025 
7026 	spdk_bs_open_blob(bs, id, _spdk_bs_iter_cpl, ctx);
7027 }
7028 
7029 void
7030 spdk_bs_iter_first(struct spdk_blob_store *bs,
7031 		   spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
7032 {
7033 	struct spdk_bs_iter_ctx *ctx;
7034 
7035 	ctx = calloc(1, sizeof(*ctx));
7036 	if (!ctx) {
7037 		cb_fn(cb_arg, NULL, -ENOMEM);
7038 		return;
7039 	}
7040 
7041 	ctx->page_num = -1;
7042 	ctx->bs = bs;
7043 	ctx->cb_fn = cb_fn;
7044 	ctx->cb_arg = cb_arg;
7045 
7046 	_spdk_bs_iter_cpl(ctx, NULL, -1);
7047 }
7048 
7049 static void
7050 _spdk_bs_iter_close_cpl(void *cb_arg, int bserrno)
7051 {
7052 	struct spdk_bs_iter_ctx *ctx = cb_arg;
7053 
7054 	_spdk_bs_iter_cpl(ctx, NULL, -1);
7055 }
7056 
7057 void
7058 spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob,
7059 		  spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
7060 {
7061 	struct spdk_bs_iter_ctx *ctx;
7062 
7063 	assert(blob != NULL);
7064 
7065 	ctx = calloc(1, sizeof(*ctx));
7066 	if (!ctx) {
7067 		cb_fn(cb_arg, NULL, -ENOMEM);
7068 		return;
7069 	}
7070 
7071 	ctx->page_num = _spdk_bs_blobid_to_page(blob->id);
7072 	ctx->bs = bs;
7073 	ctx->cb_fn = cb_fn;
7074 	ctx->cb_arg = cb_arg;
7075 
7076 	/* Close the existing blob */
7077 	spdk_blob_close(blob, _spdk_bs_iter_close_cpl, ctx);
7078 }
7079 
7080 static int
7081 _spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
7082 		     uint16_t value_len, bool internal)
7083 {
7084 	struct spdk_xattr_tailq *xattrs;
7085 	struct spdk_xattr	*xattr;
7086 	size_t			desc_size;
7087 
7088 	_spdk_blob_verify_md_op(blob);
7089 
7090 	if (blob->md_ro) {
7091 		return -EPERM;
7092 	}
7093 
7094 	desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len;
7095 	if (desc_size > SPDK_BS_MAX_DESC_SIZE) {
7096 		SPDK_DEBUGLOG(SPDK_LOG_BLOB, "Xattr '%s' of size %ld does not fix into single page %ld\n", name,
7097 			      desc_size, SPDK_BS_MAX_DESC_SIZE);
7098 		return -ENOMEM;
7099 	}
7100 
7101 	if (internal) {
7102 		xattrs = &blob->xattrs_internal;
7103 		blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR;
7104 	} else {
7105 		xattrs = &blob->xattrs;
7106 	}
7107 
7108 	TAILQ_FOREACH(xattr, xattrs, link) {
7109 		if (!strcmp(name, xattr->name)) {
7110 			free(xattr->value);
7111 			xattr->value_len = value_len;
7112 			xattr->value = malloc(value_len);
7113 			memcpy(xattr->value, value, value_len);
7114 
7115 			blob->state = SPDK_BLOB_STATE_DIRTY;
7116 
7117 			return 0;
7118 		}
7119 	}
7120 
7121 	xattr = calloc(1, sizeof(*xattr));
7122 	if (!xattr) {
7123 		return -ENOMEM;
7124 	}
7125 	xattr->name = strdup(name);
7126 	xattr->value_len = value_len;
7127 	xattr->value = malloc(value_len);
7128 	memcpy(xattr->value, value, value_len);
7129 	TAILQ_INSERT_TAIL(xattrs, xattr, link);
7130 
7131 	blob->state = SPDK_BLOB_STATE_DIRTY;
7132 
7133 	return 0;
7134 }
7135 
7136 int
7137 spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
7138 		    uint16_t value_len)
7139 {
7140 	return _spdk_blob_set_xattr(blob, name, value, value_len, false);
7141 }
7142 
7143 static int
7144 _spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal)
7145 {
7146 	struct spdk_xattr_tailq *xattrs;
7147 	struct spdk_xattr	*xattr;
7148 
7149 	_spdk_blob_verify_md_op(blob);
7150 
7151 	if (blob->md_ro) {
7152 		return -EPERM;
7153 	}
7154 	xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
7155 
7156 	TAILQ_FOREACH(xattr, xattrs, link) {
7157 		if (!strcmp(name, xattr->name)) {
7158 			TAILQ_REMOVE(xattrs, xattr, link);
7159 			free(xattr->value);
7160 			free(xattr->name);
7161 			free(xattr);
7162 
7163 			if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) {
7164 				blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR;
7165 			}
7166 			blob->state = SPDK_BLOB_STATE_DIRTY;
7167 
7168 			return 0;
7169 		}
7170 	}
7171 
7172 	return -ENOENT;
7173 }
7174 
7175 int
7176 spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name)
7177 {
7178 	return _spdk_blob_remove_xattr(blob, name, false);
7179 }
7180 
7181 static int
7182 _spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
7183 			   const void **value, size_t *value_len, bool internal)
7184 {
7185 	struct spdk_xattr	*xattr;
7186 	struct spdk_xattr_tailq *xattrs;
7187 
7188 	xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
7189 
7190 	TAILQ_FOREACH(xattr, xattrs, link) {
7191 		if (!strcmp(name, xattr->name)) {
7192 			*value = xattr->value;
7193 			*value_len = xattr->value_len;
7194 			return 0;
7195 		}
7196 	}
7197 	return -ENOENT;
7198 }
7199 
7200 int
7201 spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
7202 			  const void **value, size_t *value_len)
7203 {
7204 	_spdk_blob_verify_md_op(blob);
7205 
7206 	return _spdk_blob_get_xattr_value(blob, name, value, value_len, false);
7207 }
7208 
7209 struct spdk_xattr_names {
7210 	uint32_t	count;
7211 	const char	*names[0];
7212 };
7213 
7214 static int
7215 _spdk_blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names)
7216 {
7217 	struct spdk_xattr	*xattr;
7218 	int			count = 0;
7219 
7220 	TAILQ_FOREACH(xattr, xattrs, link) {
7221 		count++;
7222 	}
7223 
7224 	*names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
7225 	if (*names == NULL) {
7226 		return -ENOMEM;
7227 	}
7228 
7229 	TAILQ_FOREACH(xattr, xattrs, link) {
7230 		(*names)->names[(*names)->count++] = xattr->name;
7231 	}
7232 
7233 	return 0;
7234 }
7235 
7236 int
7237 spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names)
7238 {
7239 	_spdk_blob_verify_md_op(blob);
7240 
7241 	return _spdk_blob_get_xattr_names(&blob->xattrs, names);
7242 }
7243 
7244 uint32_t
7245 spdk_xattr_names_get_count(struct spdk_xattr_names *names)
7246 {
7247 	assert(names != NULL);
7248 
7249 	return names->count;
7250 }
7251 
7252 const char *
7253 spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
7254 {
7255 	if (index >= names->count) {
7256 		return NULL;
7257 	}
7258 
7259 	return names->names[index];
7260 }
7261 
7262 void
7263 spdk_xattr_names_free(struct spdk_xattr_names *names)
7264 {
7265 	free(names);
7266 }
7267 
7268 struct spdk_bs_type
7269 spdk_bs_get_bstype(struct spdk_blob_store *bs)
7270 {
7271 	return bs->bstype;
7272 }
7273 
7274 void
7275 spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
7276 {
7277 	memcpy(&bs->bstype, &bstype, sizeof(bstype));
7278 }
7279 
7280 bool
7281 spdk_blob_is_read_only(struct spdk_blob *blob)
7282 {
7283 	assert(blob != NULL);
7284 	return (blob->data_ro || blob->md_ro);
7285 }
7286 
7287 bool
7288 spdk_blob_is_snapshot(struct spdk_blob *blob)
7289 {
7290 	struct spdk_blob_list *snapshot_entry;
7291 
7292 	assert(blob != NULL);
7293 
7294 	snapshot_entry = _spdk_bs_get_snapshot_entry(blob->bs, blob->id);
7295 	if (snapshot_entry == NULL) {
7296 		return false;
7297 	}
7298 
7299 	return true;
7300 }
7301 
7302 bool
7303 spdk_blob_is_clone(struct spdk_blob *blob)
7304 {
7305 	assert(blob != NULL);
7306 
7307 	if (blob->parent_id != SPDK_BLOBID_INVALID) {
7308 		assert(spdk_blob_is_thin_provisioned(blob));
7309 		return true;
7310 	}
7311 
7312 	return false;
7313 }
7314 
7315 bool
7316 spdk_blob_is_thin_provisioned(struct spdk_blob *blob)
7317 {
7318 	assert(blob != NULL);
7319 	return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
7320 }
7321 
7322 static void
7323 _spdk_blob_update_clear_method(struct spdk_blob *blob)
7324 {
7325 	enum blob_clear_method stored_cm;
7326 
7327 	assert(blob != NULL);
7328 
7329 	/* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored
7330 	 * in metadata previously.  If something other than the default was
7331 	 * specified, ignore stored value and used what was passed in.
7332 	 */
7333 	stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT);
7334 
7335 	if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) {
7336 		blob->clear_method = stored_cm;
7337 	} else if (blob->clear_method != stored_cm) {
7338 		SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n",
7339 			     blob->clear_method, stored_cm);
7340 	}
7341 }
7342 
7343 spdk_blob_id
7344 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id)
7345 {
7346 	struct spdk_blob_list *snapshot_entry = NULL;
7347 	struct spdk_blob_list *clone_entry = NULL;
7348 
7349 	TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
7350 		TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
7351 			if (clone_entry->id == blob_id) {
7352 				return snapshot_entry->id;
7353 			}
7354 		}
7355 	}
7356 
7357 	return SPDK_BLOBID_INVALID;
7358 }
7359 
7360 int
7361 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids,
7362 		     size_t *count)
7363 {
7364 	struct spdk_blob_list *snapshot_entry, *clone_entry;
7365 	size_t n;
7366 
7367 	snapshot_entry = _spdk_bs_get_snapshot_entry(bs, blobid);
7368 	if (snapshot_entry == NULL) {
7369 		*count = 0;
7370 		return 0;
7371 	}
7372 
7373 	if (ids == NULL || *count < snapshot_entry->clone_count) {
7374 		*count = snapshot_entry->clone_count;
7375 		return -ENOMEM;
7376 	}
7377 	*count = snapshot_entry->clone_count;
7378 
7379 	n = 0;
7380 	TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
7381 		ids[n++] = clone_entry->id;
7382 	}
7383 
7384 	return 0;
7385 }
7386 
7387 SPDK_LOG_REGISTER_COMPONENT("blob", SPDK_LOG_BLOB)
7388