xref: /spdk/test/unit/lib/blob/blob.c/blob_ut.c (revision 41fd1f6be3dce03b34930989efe3d2ba54fd0b7d)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (C) 2017 Intel Corporation.
3  *   All rights reserved.
4  *   Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 
9 #include "spdk_internal/cunit.h"
10 #include "spdk/blob.h"
11 #include "spdk/string.h"
12 
13 #include "common/lib/ut_multithread.c"
14 #include "../bs_dev_common.c"
15 #include "thread/thread.c"
16 #include "blob/blobstore.c"
17 #include "blob/request.c"
18 #include "blob/zeroes.c"
19 #include "blob/blob_bs_dev.c"
20 #include "esnap_dev.c"
21 
22 struct spdk_blob_store *g_bs;
23 spdk_blob_id g_blobid;
24 struct spdk_blob *g_blob, *g_blob2;
25 int g_bserrno, g_bserrno2;
26 struct spdk_xattr_names *g_names;
27 int g_done;
28 char *g_xattr_names[] = {"first", "second", "third"};
29 char *g_xattr_values[] = {"one", "two", "three"};
30 uint64_t g_ctx = 1729;
31 bool g_use_extent_table = false;
32 
33 struct spdk_bs_super_block_ver1 {
34 	uint8_t		signature[8];
35 	uint32_t        version;
36 	uint32_t        length;
37 	uint32_t	clean; /* If there was a clean shutdown, this is 1. */
38 	spdk_blob_id	super_blob;
39 
40 	uint32_t	cluster_size; /* In bytes */
41 
42 	uint32_t	used_page_mask_start; /* Offset from beginning of disk, in pages */
43 	uint32_t	used_page_mask_len; /* Count, in pages */
44 
45 	uint32_t	used_cluster_mask_start; /* Offset from beginning of disk, in pages */
46 	uint32_t	used_cluster_mask_len; /* Count, in pages */
47 
48 	uint32_t	md_start; /* Offset from beginning of disk, in pages */
49 	uint32_t	md_len; /* Count, in pages */
50 
51 	uint8_t		reserved[4036];
52 	uint32_t	crc;
53 } __attribute__((packed));
54 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block_ver1) == 0x1000, "Invalid super block size");
55 
56 static struct spdk_blob *ut_blob_create_and_open(struct spdk_blob_store *bs,
57 		struct spdk_blob_opts *blob_opts);
58 static void ut_blob_close_and_delete(struct spdk_blob_store *bs, struct spdk_blob *blob);
59 static void suite_blob_setup(void);
60 static void suite_blob_cleanup(void);
61 
62 DEFINE_STUB(spdk_memory_domain_memzero, int, (struct spdk_memory_domain *src_domain,
63 		void *src_domain_ctx, struct iovec *iov, uint32_t iovcnt, void (*cpl_cb)(void *, int),
64 		void *cpl_cb_arg), 0);
65 
66 static bool
67 is_esnap_clone(struct spdk_blob *_blob, const void *id, size_t id_len)
68 {
69 	const void *val = NULL;
70 	size_t len = 0;
71 	bool c0, c1, c2, c3;
72 
73 	CU_ASSERT(blob_get_xattr_value(_blob, BLOB_EXTERNAL_SNAPSHOT_ID, &val, &len,
74 				       true) == 0);
75 	CU_ASSERT((c0 = (len == id_len)));
76 	CU_ASSERT((c1 = (val != NULL && memcmp(val, id, len) == 0)));
77 	CU_ASSERT((c2 = !!(_blob->invalid_flags & SPDK_BLOB_EXTERNAL_SNAPSHOT)));
78 	CU_ASSERT((c3 = (_blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT)));
79 
80 	return c0 && c1 && c2 && c3;
81 }
82 
83 static bool
84 is_not_esnap_clone(struct spdk_blob *_blob)
85 {
86 	const void *val = NULL;
87 	size_t len = 0;
88 	bool c1, c2, c3, c4;
89 
90 	CU_ASSERT((c1 = (blob_get_xattr_value(_blob, BLOB_EXTERNAL_SNAPSHOT_ID, &val, &len,
91 					      true) == -ENOENT)));
92 	CU_ASSERT((c2 = (val == NULL)));
93 	CU_ASSERT((c3 = ((_blob->invalid_flags & SPDK_BLOB_EXTERNAL_SNAPSHOT) == 0)));
94 	CU_ASSERT((c4 = (_blob->parent_id != SPDK_BLOBID_EXTERNAL_SNAPSHOT)));
95 
96 	return c1 && c2 && c3 && c4;
97 }
98 
99 #define UT_ASSERT_IS_ESNAP_CLONE(_blob, _id, _len) CU_ASSERT(is_esnap_clone(_blob, _id, _len))
100 #define UT_ASSERT_IS_NOT_ESNAP_CLONE(_blob) CU_ASSERT(is_not_esnap_clone(_blob))
101 
102 static void
103 _get_xattr_value(void *arg, const char *name,
104 		 const void **value, size_t *value_len)
105 {
106 	uint64_t i;
107 
108 	SPDK_CU_ASSERT_FATAL(value_len != NULL);
109 	SPDK_CU_ASSERT_FATAL(value != NULL);
110 	CU_ASSERT(arg == &g_ctx);
111 
112 	for (i = 0; i < sizeof(g_xattr_names); i++) {
113 		if (!strcmp(name, g_xattr_names[i])) {
114 			*value_len = strlen(g_xattr_values[i]);
115 			*value = g_xattr_values[i];
116 			break;
117 		}
118 	}
119 }
120 
121 static void
122 _get_xattr_value_null(void *arg, const char *name,
123 		      const void **value, size_t *value_len)
124 {
125 	SPDK_CU_ASSERT_FATAL(value_len != NULL);
126 	SPDK_CU_ASSERT_FATAL(value != NULL);
127 	CU_ASSERT(arg == NULL);
128 
129 	*value_len = 0;
130 	*value = NULL;
131 }
132 
133 static int
134 _get_snapshots_count(struct spdk_blob_store *bs)
135 {
136 	struct spdk_blob_list *snapshot = NULL;
137 	int count = 0;
138 
139 	TAILQ_FOREACH(snapshot, &bs->snapshots, link) {
140 		count += 1;
141 	}
142 
143 	return count;
144 }
145 
146 static void
147 ut_spdk_blob_opts_init(struct spdk_blob_opts *opts)
148 {
149 	spdk_blob_opts_init(opts, sizeof(*opts));
150 	opts->use_extent_table = g_use_extent_table;
151 }
152 
153 static void
154 bs_op_complete(void *cb_arg, int bserrno)
155 {
156 	g_bserrno = bserrno;
157 }
158 
159 static void
160 bs_op_with_handle_complete(void *cb_arg, struct spdk_blob_store *bs,
161 			   int bserrno)
162 {
163 	g_bs = bs;
164 	g_bserrno = bserrno;
165 }
166 
167 static void
168 blob_op_complete(void *cb_arg, int bserrno)
169 {
170 	if (cb_arg != NULL) {
171 		int *errp = cb_arg;
172 
173 		*errp = bserrno;
174 	}
175 	g_bserrno = bserrno;
176 }
177 
178 static void
179 blob_op_with_id_complete(void *cb_arg, spdk_blob_id blobid, int bserrno)
180 {
181 	g_blobid = blobid;
182 	g_bserrno = bserrno;
183 }
184 
185 static void
186 blob_op_with_handle_complete(void *cb_arg, struct spdk_blob *blb, int bserrno)
187 {
188 	g_blob = blb;
189 	g_bserrno = bserrno;
190 }
191 
192 static void
193 blob_op_with_handle_complete2(void *cb_arg, struct spdk_blob *blob, int bserrno)
194 {
195 	if (g_blob == NULL) {
196 		g_blob = blob;
197 		g_bserrno = bserrno;
198 	} else {
199 		g_blob2 = blob;
200 		g_bserrno2 = bserrno;
201 	}
202 }
203 
204 static void
205 ut_bs_reload(struct spdk_blob_store **bs, struct spdk_bs_opts *opts)
206 {
207 	struct spdk_bs_dev *dev;
208 
209 	/* Unload the blob store */
210 	spdk_bs_unload(*bs, bs_op_complete, NULL);
211 	poll_threads();
212 	CU_ASSERT(g_bserrno == 0);
213 
214 	dev = init_dev();
215 	/* Load an existing blob store */
216 	spdk_bs_load(dev, opts, bs_op_with_handle_complete, NULL);
217 	poll_threads();
218 	CU_ASSERT(g_bserrno == 0);
219 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
220 	*bs = g_bs;
221 
222 	g_bserrno = -1;
223 }
224 
225 static void
226 ut_bs_dirty_load(struct spdk_blob_store **bs, struct spdk_bs_opts *opts)
227 {
228 	struct spdk_bs_dev *dev;
229 
230 	/* Dirty shutdown */
231 	bs_free(*bs);
232 
233 	dev = init_dev();
234 	/* Load an existing blob store */
235 	spdk_bs_load(dev, opts, bs_op_with_handle_complete, NULL);
236 	poll_threads();
237 	CU_ASSERT(g_bserrno == 0);
238 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
239 	*bs = g_bs;
240 
241 	g_bserrno = -1;
242 }
243 
244 static void
245 blob_init(void)
246 {
247 	struct spdk_blob_store *bs;
248 	struct spdk_bs_dev *dev;
249 
250 	dev = init_dev();
251 
252 	/* should fail for an unsupported blocklen */
253 	dev->blocklen = 500;
254 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
255 	poll_threads();
256 	CU_ASSERT(g_bserrno == -EINVAL);
257 
258 	dev = init_dev();
259 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
260 	poll_threads();
261 	CU_ASSERT(g_bserrno == 0);
262 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
263 	bs = g_bs;
264 
265 	spdk_bs_unload(bs, bs_op_complete, NULL);
266 	poll_threads();
267 	CU_ASSERT(g_bserrno == 0);
268 	g_bs = NULL;
269 }
270 
271 static void
272 blob_super(void)
273 {
274 	struct spdk_blob_store *bs = g_bs;
275 	spdk_blob_id blobid;
276 	struct spdk_blob_opts blob_opts;
277 
278 	/* Get the super blob without having set one */
279 	spdk_bs_get_super(bs, blob_op_with_id_complete, NULL);
280 	poll_threads();
281 	CU_ASSERT(g_bserrno == -ENOENT);
282 	CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID);
283 
284 	/* Create a blob */
285 	ut_spdk_blob_opts_init(&blob_opts);
286 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
287 	poll_threads();
288 	CU_ASSERT(g_bserrno == 0);
289 	CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
290 	blobid = g_blobid;
291 
292 	/* Set the blob as the super blob */
293 	spdk_bs_set_super(bs, blobid, blob_op_complete, NULL);
294 	poll_threads();
295 	CU_ASSERT(g_bserrno == 0);
296 
297 	/* Get the super blob */
298 	spdk_bs_get_super(bs, blob_op_with_id_complete, NULL);
299 	poll_threads();
300 	CU_ASSERT(g_bserrno == 0);
301 	CU_ASSERT(blobid == g_blobid);
302 }
303 
304 static void
305 blob_open(void)
306 {
307 	struct spdk_blob_store *bs = g_bs;
308 	struct spdk_blob *blob;
309 	struct spdk_blob_opts blob_opts;
310 	spdk_blob_id blobid, blobid2;
311 
312 	ut_spdk_blob_opts_init(&blob_opts);
313 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
314 	poll_threads();
315 	CU_ASSERT(g_bserrno == 0);
316 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
317 	blobid = g_blobid;
318 
319 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
320 	poll_threads();
321 	CU_ASSERT(g_bserrno == 0);
322 	CU_ASSERT(g_blob != NULL);
323 	blob = g_blob;
324 
325 	blobid2 = spdk_blob_get_id(blob);
326 	CU_ASSERT(blobid == blobid2);
327 
328 	/* Try to open file again.  It should return success. */
329 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
330 	poll_threads();
331 	CU_ASSERT(g_bserrno == 0);
332 	CU_ASSERT(blob == g_blob);
333 
334 	spdk_blob_close(blob, blob_op_complete, NULL);
335 	poll_threads();
336 	CU_ASSERT(g_bserrno == 0);
337 
338 	/*
339 	 * Close the file a second time, releasing the second reference.  This
340 	 *  should succeed.
341 	 */
342 	blob = g_blob;
343 	spdk_blob_close(blob, blob_op_complete, NULL);
344 	poll_threads();
345 	CU_ASSERT(g_bserrno == 0);
346 
347 	/*
348 	 * Try to open file again.  It should succeed.  This tests the case
349 	 *  where the file is opened, closed, then re-opened again.
350 	 */
351 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
352 	poll_threads();
353 	CU_ASSERT(g_bserrno == 0);
354 	CU_ASSERT(g_blob != NULL);
355 	blob = g_blob;
356 	spdk_blob_close(blob, blob_op_complete, NULL);
357 	poll_threads();
358 	CU_ASSERT(g_bserrno == 0);
359 
360 	/* Try to open file twice in succession.  This should return the same
361 	 * blob object.
362 	 */
363 	g_blob = NULL;
364 	g_blob2 = NULL;
365 	g_bserrno = -1;
366 	g_bserrno2 = -1;
367 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete2, NULL);
368 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete2, NULL);
369 	poll_threads();
370 	CU_ASSERT(g_bserrno == 0);
371 	CU_ASSERT(g_bserrno2 == 0);
372 	CU_ASSERT(g_blob != NULL);
373 	CU_ASSERT(g_blob2 != NULL);
374 	CU_ASSERT(g_blob == g_blob2);
375 
376 	g_bserrno = -1;
377 	spdk_blob_close(g_blob, blob_op_complete, NULL);
378 	poll_threads();
379 	CU_ASSERT(g_bserrno == 0);
380 
381 	ut_blob_close_and_delete(bs, g_blob);
382 }
383 
384 static void
385 blob_create(void)
386 {
387 	struct spdk_blob_store *bs = g_bs;
388 	struct spdk_blob *blob;
389 	struct spdk_blob_opts opts;
390 	spdk_blob_id blobid;
391 
392 	/* Create blob with 10 clusters */
393 
394 	ut_spdk_blob_opts_init(&opts);
395 	opts.num_clusters = 10;
396 
397 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
398 	poll_threads();
399 	CU_ASSERT(g_bserrno == 0);
400 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
401 	blobid = g_blobid;
402 
403 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
404 	poll_threads();
405 	CU_ASSERT(g_bserrno == 0);
406 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
407 	blob = g_blob;
408 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
409 
410 	spdk_blob_close(blob, blob_op_complete, NULL);
411 	poll_threads();
412 	CU_ASSERT(g_bserrno == 0);
413 
414 	/* Create blob with 0 clusters */
415 
416 	ut_spdk_blob_opts_init(&opts);
417 	opts.num_clusters = 0;
418 
419 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
420 	poll_threads();
421 	CU_ASSERT(g_bserrno == 0);
422 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
423 	blobid = g_blobid;
424 
425 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
426 	poll_threads();
427 	CU_ASSERT(g_bserrno == 0);
428 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
429 	blob = g_blob;
430 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
431 
432 	spdk_blob_close(blob, blob_op_complete, NULL);
433 	poll_threads();
434 	CU_ASSERT(g_bserrno == 0);
435 
436 	/* Create blob with default options (opts == NULL) */
437 
438 	spdk_bs_create_blob_ext(bs, NULL, blob_op_with_id_complete, NULL);
439 	poll_threads();
440 	CU_ASSERT(g_bserrno == 0);
441 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
442 	blobid = g_blobid;
443 
444 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
445 	poll_threads();
446 	CU_ASSERT(g_bserrno == 0);
447 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
448 	blob = g_blob;
449 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
450 
451 	spdk_blob_close(blob, blob_op_complete, NULL);
452 	poll_threads();
453 	CU_ASSERT(g_bserrno == 0);
454 
455 	/* Try to create blob with size larger than blobstore */
456 
457 	ut_spdk_blob_opts_init(&opts);
458 	opts.num_clusters = bs->total_clusters + 1;
459 
460 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
461 	poll_threads();
462 	CU_ASSERT(g_bserrno == -ENOSPC);
463 }
464 
465 static void
466 blob_create_zero_extent(void)
467 {
468 	struct spdk_blob_store *bs = g_bs;
469 	struct spdk_blob *blob;
470 	spdk_blob_id blobid;
471 
472 	/* Create blob with default options (opts == NULL) */
473 	spdk_bs_create_blob_ext(bs, NULL, blob_op_with_id_complete, NULL);
474 	poll_threads();
475 	CU_ASSERT(g_bserrno == 0);
476 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
477 	blobid = g_blobid;
478 
479 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
480 	poll_threads();
481 	CU_ASSERT(g_bserrno == 0);
482 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
483 	blob = g_blob;
484 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
485 	CU_ASSERT(blob->extent_table_found == true);
486 	CU_ASSERT(blob->active.extent_pages_array_size == 0);
487 	CU_ASSERT(blob->active.extent_pages == NULL);
488 
489 	spdk_blob_close(blob, blob_op_complete, NULL);
490 	poll_threads();
491 	CU_ASSERT(g_bserrno == 0);
492 
493 	/* Create blob with NULL internal options  */
494 	bs_create_blob(bs, NULL, NULL, blob_op_with_id_complete, NULL);
495 	poll_threads();
496 	CU_ASSERT(g_bserrno == 0);
497 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
498 	blobid = g_blobid;
499 
500 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
501 	poll_threads();
502 	CU_ASSERT(g_bserrno == 0);
503 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
504 	blob = g_blob;
505 	CU_ASSERT(TAILQ_FIRST(&blob->xattrs_internal) == NULL);
506 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
507 	CU_ASSERT(blob->extent_table_found == true);
508 	CU_ASSERT(blob->active.extent_pages_array_size == 0);
509 	CU_ASSERT(blob->active.extent_pages == NULL);
510 
511 	spdk_blob_close(blob, blob_op_complete, NULL);
512 	poll_threads();
513 	CU_ASSERT(g_bserrno == 0);
514 }
515 
516 /*
517  * Create and delete one blob in a loop over and over again.  This helps ensure
518  * that the internal bit masks tracking used clusters and md_pages are being
519  * tracked correctly.
520  */
521 static void
522 blob_create_loop(void)
523 {
524 	struct spdk_blob_store *bs = g_bs;
525 	struct spdk_blob_opts opts;
526 	uint32_t i, loop_count;
527 
528 	loop_count = 4 * spdk_max(spdk_bit_array_capacity(bs->used_md_pages),
529 				  spdk_bit_pool_capacity(bs->used_clusters));
530 
531 	for (i = 0; i < loop_count; i++) {
532 		ut_spdk_blob_opts_init(&opts);
533 		opts.num_clusters = 1;
534 		g_bserrno = -1;
535 		g_blobid = SPDK_BLOBID_INVALID;
536 		spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
537 		poll_threads();
538 		CU_ASSERT(g_bserrno == 0);
539 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
540 		spdk_bs_delete_blob(bs, g_blobid, blob_op_complete, NULL);
541 		poll_threads();
542 		CU_ASSERT(g_bserrno == 0);
543 	}
544 }
545 
546 static void
547 blob_create_fail(void)
548 {
549 	struct spdk_blob_store *bs = g_bs;
550 	struct spdk_blob_opts opts;
551 	spdk_blob_id blobid;
552 	uint32_t used_blobids_count = spdk_bit_array_count_set(bs->used_blobids);
553 	uint32_t used_md_pages_count = spdk_bit_array_count_set(bs->used_md_pages);
554 
555 	/* NULL callback */
556 	ut_spdk_blob_opts_init(&opts);
557 	opts.xattrs.names = g_xattr_names;
558 	opts.xattrs.get_value = NULL;
559 	opts.xattrs.count = 1;
560 	opts.xattrs.ctx = &g_ctx;
561 
562 	blobid = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
563 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
564 	poll_threads();
565 	CU_ASSERT(g_bserrno == -EINVAL);
566 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
567 	CU_ASSERT(spdk_bit_array_count_set(bs->used_blobids) == used_blobids_count);
568 	CU_ASSERT(spdk_bit_array_count_set(bs->used_md_pages) == used_md_pages_count);
569 
570 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
571 	poll_threads();
572 	CU_ASSERT(g_bserrno == -ENOENT);
573 	SPDK_CU_ASSERT_FATAL(g_blob == NULL);
574 
575 	ut_bs_reload(&bs, NULL);
576 	CU_ASSERT(spdk_bit_array_count_set(bs->used_blobids) == used_blobids_count);
577 	CU_ASSERT(spdk_bit_array_count_set(bs->used_md_pages) == used_md_pages_count);
578 
579 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
580 	poll_threads();
581 	CU_ASSERT(g_blob == NULL);
582 	CU_ASSERT(g_bserrno == -ENOENT);
583 }
584 
585 static void
586 blob_create_internal(void)
587 {
588 	struct spdk_blob_store *bs = g_bs;
589 	struct spdk_blob *blob;
590 	struct spdk_blob_opts opts;
591 	struct spdk_blob_xattr_opts internal_xattrs;
592 	const void *value;
593 	size_t value_len;
594 	spdk_blob_id blobid;
595 	int rc;
596 
597 	/* Create blob with custom xattrs */
598 
599 	ut_spdk_blob_opts_init(&opts);
600 	blob_xattrs_init(&internal_xattrs);
601 	internal_xattrs.count = 3;
602 	internal_xattrs.names = g_xattr_names;
603 	internal_xattrs.get_value = _get_xattr_value;
604 	internal_xattrs.ctx = &g_ctx;
605 
606 	bs_create_blob(bs, &opts, &internal_xattrs, blob_op_with_id_complete, NULL);
607 	poll_threads();
608 	CU_ASSERT(g_bserrno == 0);
609 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
610 	blobid = g_blobid;
611 
612 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
613 	poll_threads();
614 	CU_ASSERT(g_bserrno == 0);
615 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
616 	blob = g_blob;
617 
618 	rc = blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len, true);
619 	CU_ASSERT(rc == 0);
620 	SPDK_CU_ASSERT_FATAL(value != NULL);
621 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
622 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
623 
624 	rc = blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len, true);
625 	CU_ASSERT(rc == 0);
626 	SPDK_CU_ASSERT_FATAL(value != NULL);
627 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
628 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
629 
630 	rc = blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len, true);
631 	CU_ASSERT(rc == 0);
632 	SPDK_CU_ASSERT_FATAL(value != NULL);
633 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
634 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
635 
636 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len);
637 	CU_ASSERT(rc != 0);
638 
639 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len);
640 	CU_ASSERT(rc != 0);
641 
642 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len);
643 	CU_ASSERT(rc != 0);
644 
645 	spdk_blob_close(blob, blob_op_complete, NULL);
646 	poll_threads();
647 	CU_ASSERT(g_bserrno == 0);
648 
649 	/* Create blob with NULL internal options  */
650 
651 	bs_create_blob(bs, NULL, NULL, blob_op_with_id_complete, NULL);
652 	poll_threads();
653 	CU_ASSERT(g_bserrno == 0);
654 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
655 	blobid = g_blobid;
656 
657 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
658 	poll_threads();
659 	CU_ASSERT(g_bserrno == 0);
660 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
661 	CU_ASSERT(TAILQ_FIRST(&g_blob->xattrs_internal) == NULL);
662 	CU_ASSERT(spdk_blob_get_num_clusters(g_blob) == 0);
663 
664 	blob = g_blob;
665 
666 	spdk_blob_close(blob, blob_op_complete, NULL);
667 	poll_threads();
668 	CU_ASSERT(g_bserrno == 0);
669 }
670 
671 static void
672 blob_thin_provision(void)
673 {
674 	struct spdk_blob_store *bs;
675 	struct spdk_bs_dev *dev;
676 	struct spdk_blob *blob;
677 	struct spdk_blob_opts opts;
678 	struct spdk_bs_opts bs_opts;
679 	spdk_blob_id blobid;
680 
681 	dev = init_dev();
682 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
683 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
684 
685 	/* Initialize a new blob store */
686 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
687 	poll_threads();
688 	CU_ASSERT(g_bserrno == 0);
689 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
690 
691 	bs = g_bs;
692 
693 	/* Create blob with thin provisioning enabled */
694 
695 	ut_spdk_blob_opts_init(&opts);
696 	opts.thin_provision = true;
697 	opts.num_clusters = 10;
698 
699 	blob = ut_blob_create_and_open(bs, &opts);
700 	blobid = spdk_blob_get_id(blob);
701 	CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
702 	/* In thin provisioning with num_clusters is set, if not using the
703 	 * extent table, there is no allocation. If extent table is used,
704 	 * there is related allocation happened. */
705 	if (blob->extent_table_found == true) {
706 		CU_ASSERT(blob->active.extent_pages_array_size > 0);
707 		CU_ASSERT(blob->active.extent_pages != NULL);
708 	} else {
709 		CU_ASSERT(blob->active.extent_pages_array_size == 0);
710 		CU_ASSERT(blob->active.extent_pages == NULL);
711 	}
712 
713 	spdk_blob_close(blob, blob_op_complete, NULL);
714 	CU_ASSERT(g_bserrno == 0);
715 
716 	/* Do not shut down cleanly.  This makes sure that when we load again
717 	 *  and try to recover a valid used_cluster map, that blobstore will
718 	 *  ignore clusters with index 0 since these are unallocated clusters.
719 	 */
720 	ut_bs_dirty_load(&bs, &bs_opts);
721 
722 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
723 	poll_threads();
724 	CU_ASSERT(g_bserrno == 0);
725 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
726 	blob = g_blob;
727 	CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
728 
729 	ut_blob_close_and_delete(bs, blob);
730 
731 	spdk_bs_unload(bs, bs_op_complete, NULL);
732 	poll_threads();
733 	CU_ASSERT(g_bserrno == 0);
734 	g_bs = NULL;
735 }
736 
737 static void
738 blob_snapshot(void)
739 {
740 	struct spdk_blob_store *bs = g_bs;
741 	struct spdk_blob *blob;
742 	struct spdk_blob *snapshot, *snapshot2;
743 	struct spdk_blob_bs_dev *blob_bs_dev;
744 	struct spdk_blob_opts opts;
745 	struct spdk_blob_xattr_opts xattrs;
746 	spdk_blob_id blobid;
747 	spdk_blob_id snapshotid;
748 	spdk_blob_id snapshotid2;
749 	const void *value;
750 	size_t value_len;
751 	int rc;
752 	spdk_blob_id ids[2];
753 	size_t count;
754 
755 	/* Create blob with 10 clusters */
756 	ut_spdk_blob_opts_init(&opts);
757 	opts.num_clusters = 10;
758 
759 	blob = ut_blob_create_and_open(bs, &opts);
760 	blobid = spdk_blob_get_id(blob);
761 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
762 
763 	/* Create snapshot from blob */
764 	CU_ASSERT_EQUAL(_get_snapshots_count(bs), 0);
765 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
766 	poll_threads();
767 	CU_ASSERT(g_bserrno == 0);
768 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
769 	CU_ASSERT_EQUAL(_get_snapshots_count(bs), 1);
770 	snapshotid = g_blobid;
771 
772 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
773 	poll_threads();
774 	CU_ASSERT(g_bserrno == 0);
775 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
776 	snapshot = g_blob;
777 	CU_ASSERT(snapshot->data_ro == true);
778 	CU_ASSERT(snapshot->md_ro == true);
779 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10);
780 
781 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
782 	CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
783 	CU_ASSERT(spdk_mem_all_zero(blob->active.clusters,
784 				    blob->active.num_clusters * sizeof(blob->active.clusters[0])));
785 
786 	/* Try to create snapshot from clone with xattrs */
787 	xattrs.names = g_xattr_names;
788 	xattrs.get_value = _get_xattr_value;
789 	xattrs.count = 3;
790 	xattrs.ctx = &g_ctx;
791 	spdk_bs_create_snapshot(bs, blobid, &xattrs, blob_op_with_id_complete, NULL);
792 	poll_threads();
793 	CU_ASSERT(g_bserrno == 0);
794 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
795 	CU_ASSERT_EQUAL(_get_snapshots_count(bs), 2);
796 	snapshotid2 = g_blobid;
797 
798 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
799 	CU_ASSERT(g_bserrno == 0);
800 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
801 	snapshot2 = g_blob;
802 	CU_ASSERT(snapshot2->data_ro == true);
803 	CU_ASSERT(snapshot2->md_ro == true);
804 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 10);
805 
806 	/* Confirm that blob is backed by snapshot2 and snapshot2 is backed by snapshot */
807 	CU_ASSERT(snapshot->back_bs_dev == NULL);
808 	SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL);
809 	SPDK_CU_ASSERT_FATAL(snapshot2->back_bs_dev != NULL);
810 
811 	blob_bs_dev = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
812 	CU_ASSERT(blob_bs_dev->blob == snapshot2);
813 
814 	blob_bs_dev = (struct spdk_blob_bs_dev *)snapshot2->back_bs_dev;
815 	CU_ASSERT(blob_bs_dev->blob == snapshot);
816 
817 	rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[0], &value, &value_len);
818 	CU_ASSERT(rc == 0);
819 	SPDK_CU_ASSERT_FATAL(value != NULL);
820 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
821 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
822 
823 	rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[1], &value, &value_len);
824 	CU_ASSERT(rc == 0);
825 	SPDK_CU_ASSERT_FATAL(value != NULL);
826 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
827 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
828 
829 	rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[2], &value, &value_len);
830 	CU_ASSERT(rc == 0);
831 	SPDK_CU_ASSERT_FATAL(value != NULL);
832 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
833 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
834 
835 	/* Confirm that blob is clone of snapshot2, and snapshot2 is clone of snapshot */
836 	count = 2;
837 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid2, ids, &count) == 0);
838 	CU_ASSERT(count == 1);
839 	CU_ASSERT(ids[0] == blobid);
840 
841 	count = 2;
842 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
843 	CU_ASSERT(count == 1);
844 	CU_ASSERT(ids[0] == snapshotid2);
845 
846 	/* Try to create snapshot from snapshot */
847 	spdk_bs_create_snapshot(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
848 	poll_threads();
849 	CU_ASSERT(g_bserrno == -EINVAL);
850 	CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID);
851 	CU_ASSERT_EQUAL(_get_snapshots_count(bs), 2);
852 
853 	/* Delete blob and confirm that it is no longer on snapshot2 clone list */
854 	ut_blob_close_and_delete(bs, blob);
855 	count = 2;
856 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid2, ids, &count) == 0);
857 	CU_ASSERT(count == 0);
858 
859 	/* Delete snapshot2 and confirm that it is no longer on snapshot clone list */
860 	ut_blob_close_and_delete(bs, snapshot2);
861 	CU_ASSERT_EQUAL(_get_snapshots_count(bs), 1);
862 	count = 2;
863 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid2, ids, &count) == 0);
864 	CU_ASSERT(count == 0);
865 
866 	ut_blob_close_and_delete(bs, snapshot);
867 	CU_ASSERT_EQUAL(_get_snapshots_count(bs), 0);
868 }
869 
870 static void
871 blob_snapshot_freeze_io(void)
872 {
873 	struct spdk_io_channel *channel;
874 	struct spdk_bs_channel *bs_channel;
875 	struct spdk_blob_store *bs = g_bs;
876 	struct spdk_blob *blob;
877 	struct spdk_blob_opts opts;
878 	spdk_blob_id blobid;
879 	uint32_t num_of_pages = 10;
880 	uint8_t payload_read[num_of_pages * SPDK_BS_PAGE_SIZE];
881 	uint8_t payload_write[num_of_pages * SPDK_BS_PAGE_SIZE];
882 	uint8_t payload_zero[num_of_pages * SPDK_BS_PAGE_SIZE];
883 
884 	memset(payload_write, 0xE5, sizeof(payload_write));
885 	memset(payload_read, 0x00, sizeof(payload_read));
886 	memset(payload_zero, 0x00, sizeof(payload_zero));
887 
888 	/* Test freeze I/O during snapshot */
889 	channel = spdk_bs_alloc_io_channel(bs);
890 	bs_channel = spdk_io_channel_get_ctx(channel);
891 
892 	/* Create blob with 10 clusters */
893 	ut_spdk_blob_opts_init(&opts);
894 	opts.num_clusters = 10;
895 	opts.thin_provision = false;
896 
897 	blob = ut_blob_create_and_open(bs, &opts);
898 	blobid = spdk_blob_get_id(blob);
899 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
900 
901 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
902 
903 	/* This is implementation specific.
904 	 * Flag 'frozen_io' is set in _spdk_bs_snapshot_freeze_cpl callback.
905 	 * Four async I/O operations happen before that. */
906 	poll_thread_times(0, 5);
907 
908 	CU_ASSERT(TAILQ_EMPTY(&bs_channel->queued_io));
909 
910 	/* Blob I/O should be frozen here */
911 	CU_ASSERT(blob->frozen_refcnt == 1);
912 
913 	/* Write to the blob */
914 	spdk_blob_io_write(blob, channel, payload_write, 0, num_of_pages, blob_op_complete, NULL);
915 
916 	/* Verify that I/O is queued */
917 	CU_ASSERT(!TAILQ_EMPTY(&bs_channel->queued_io));
918 	/* Verify that payload is not written to disk, at this point the blobs already switched */
919 	CU_ASSERT(blob->active.clusters[0] == 0);
920 
921 	/* Finish all operations including spdk_bs_create_snapshot */
922 	poll_threads();
923 
924 	/* Verify snapshot */
925 	CU_ASSERT(g_bserrno == 0);
926 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
927 
928 	/* Verify that blob has unset frozen_io */
929 	CU_ASSERT(blob->frozen_refcnt == 0);
930 
931 	/* Verify that postponed I/O completed successfully by comparing payload */
932 	spdk_blob_io_read(blob, channel, payload_read, 0, num_of_pages, blob_op_complete, NULL);
933 	poll_threads();
934 	CU_ASSERT(g_bserrno == 0);
935 	CU_ASSERT(memcmp(payload_write, payload_read, num_of_pages * SPDK_BS_PAGE_SIZE) == 0);
936 
937 	spdk_bs_free_io_channel(channel);
938 	poll_threads();
939 
940 	ut_blob_close_and_delete(bs, blob);
941 }
942 
943 static void
944 blob_clone(void)
945 {
946 	struct spdk_blob_store *bs = g_bs;
947 	struct spdk_blob_opts opts;
948 	struct spdk_blob *blob, *snapshot, *clone;
949 	spdk_blob_id blobid, cloneid, snapshotid;
950 	struct spdk_blob_xattr_opts xattrs;
951 	const void *value;
952 	size_t value_len;
953 	int rc;
954 
955 	/* Create blob with 10 clusters */
956 
957 	ut_spdk_blob_opts_init(&opts);
958 	opts.num_clusters = 10;
959 
960 	blob = ut_blob_create_and_open(bs, &opts);
961 	blobid = spdk_blob_get_id(blob);
962 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
963 
964 	/* Create snapshot */
965 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
966 	poll_threads();
967 	CU_ASSERT(g_bserrno == 0);
968 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
969 	snapshotid = g_blobid;
970 
971 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
972 	poll_threads();
973 	CU_ASSERT(g_bserrno == 0);
974 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
975 	snapshot = g_blob;
976 	CU_ASSERT(snapshot->data_ro == true);
977 	CU_ASSERT(snapshot->md_ro == true);
978 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10);
979 
980 	spdk_blob_close(snapshot, blob_op_complete, NULL);
981 	poll_threads();
982 	CU_ASSERT(g_bserrno == 0);
983 
984 	/* Create clone from snapshot with xattrs */
985 	xattrs.names = g_xattr_names;
986 	xattrs.get_value = _get_xattr_value;
987 	xattrs.count = 3;
988 	xattrs.ctx = &g_ctx;
989 
990 	spdk_bs_create_clone(bs, snapshotid, &xattrs, blob_op_with_id_complete, NULL);
991 	poll_threads();
992 	CU_ASSERT(g_bserrno == 0);
993 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
994 	cloneid = g_blobid;
995 
996 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
997 	poll_threads();
998 	CU_ASSERT(g_bserrno == 0);
999 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1000 	clone = g_blob;
1001 	CU_ASSERT(clone->data_ro == false);
1002 	CU_ASSERT(clone->md_ro == false);
1003 	CU_ASSERT(spdk_blob_get_num_clusters(clone) == 10);
1004 
1005 	rc = spdk_blob_get_xattr_value(clone, g_xattr_names[0], &value, &value_len);
1006 	CU_ASSERT(rc == 0);
1007 	SPDK_CU_ASSERT_FATAL(value != NULL);
1008 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
1009 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
1010 
1011 	rc = spdk_blob_get_xattr_value(clone, g_xattr_names[1], &value, &value_len);
1012 	CU_ASSERT(rc == 0);
1013 	SPDK_CU_ASSERT_FATAL(value != NULL);
1014 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
1015 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
1016 
1017 	rc = spdk_blob_get_xattr_value(clone, g_xattr_names[2], &value, &value_len);
1018 	CU_ASSERT(rc == 0);
1019 	SPDK_CU_ASSERT_FATAL(value != NULL);
1020 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
1021 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
1022 
1023 
1024 	spdk_blob_close(clone, blob_op_complete, NULL);
1025 	poll_threads();
1026 	CU_ASSERT(g_bserrno == 0);
1027 
1028 	/* Try to create clone from not read only blob */
1029 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
1030 	poll_threads();
1031 	CU_ASSERT(g_bserrno == -EINVAL);
1032 	CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID);
1033 
1034 	/* Mark blob as read only */
1035 	spdk_blob_set_read_only(blob);
1036 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
1037 	poll_threads();
1038 	CU_ASSERT(g_bserrno == 0);
1039 
1040 	/* Create clone from read only blob */
1041 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
1042 	poll_threads();
1043 	CU_ASSERT(g_bserrno == 0);
1044 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1045 	cloneid = g_blobid;
1046 
1047 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
1048 	poll_threads();
1049 	CU_ASSERT(g_bserrno == 0);
1050 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1051 	clone = g_blob;
1052 	CU_ASSERT(clone->data_ro == false);
1053 	CU_ASSERT(clone->md_ro == false);
1054 	CU_ASSERT(spdk_blob_get_num_clusters(clone) == 10);
1055 
1056 	ut_blob_close_and_delete(bs, clone);
1057 	ut_blob_close_and_delete(bs, blob);
1058 }
1059 
1060 static void
1061 _blob_inflate(bool decouple_parent)
1062 {
1063 	struct spdk_blob_store *bs = g_bs;
1064 	struct spdk_blob_opts opts;
1065 	struct spdk_blob *blob, *snapshot;
1066 	spdk_blob_id blobid, snapshotid;
1067 	struct spdk_io_channel *channel;
1068 	uint64_t free_clusters;
1069 
1070 	channel = spdk_bs_alloc_io_channel(bs);
1071 	SPDK_CU_ASSERT_FATAL(channel != NULL);
1072 
1073 	/* Create blob with 10 clusters */
1074 
1075 	ut_spdk_blob_opts_init(&opts);
1076 	opts.num_clusters = 10;
1077 	opts.thin_provision = true;
1078 
1079 	blob = ut_blob_create_and_open(bs, &opts);
1080 	blobid = spdk_blob_get_id(blob);
1081 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
1082 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == true);
1083 
1084 	/* 1) Blob with no parent */
1085 	if (decouple_parent) {
1086 		/* Decouple parent of blob with no parent (should fail) */
1087 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
1088 		poll_threads();
1089 		CU_ASSERT(g_bserrno != 0);
1090 	} else {
1091 		/* Inflate of thin blob with no parent should made it thick */
1092 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
1093 		poll_threads();
1094 		CU_ASSERT(g_bserrno == 0);
1095 		CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == false);
1096 	}
1097 
1098 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
1099 	poll_threads();
1100 	CU_ASSERT(g_bserrno == 0);
1101 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1102 	snapshotid = g_blobid;
1103 
1104 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == true);
1105 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
1106 
1107 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
1108 	poll_threads();
1109 	CU_ASSERT(g_bserrno == 0);
1110 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1111 	snapshot = g_blob;
1112 	CU_ASSERT(snapshot->data_ro == true);
1113 	CU_ASSERT(snapshot->md_ro == true);
1114 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10);
1115 
1116 	spdk_blob_close(snapshot, blob_op_complete, NULL);
1117 	poll_threads();
1118 	CU_ASSERT(g_bserrno == 0);
1119 
1120 	free_clusters = spdk_bs_free_cluster_count(bs);
1121 
1122 	/* 2) Blob with parent */
1123 	if (!decouple_parent) {
1124 		/* Do full blob inflation */
1125 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
1126 		poll_threads();
1127 		CU_ASSERT(g_bserrno == 0);
1128 		/* all 10 clusters should be allocated */
1129 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 10);
1130 	} else {
1131 		/* Decouple parent of blob */
1132 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
1133 		poll_threads();
1134 		CU_ASSERT(g_bserrno == 0);
1135 		/* when only parent is removed, none of the clusters should be allocated */
1136 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters);
1137 	}
1138 
1139 	/* Now, it should be possible to delete snapshot */
1140 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
1141 	poll_threads();
1142 	CU_ASSERT(g_bserrno == 0);
1143 
1144 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
1145 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == decouple_parent);
1146 
1147 	spdk_bs_free_io_channel(channel);
1148 	poll_threads();
1149 
1150 	ut_blob_close_and_delete(bs, blob);
1151 }
1152 
1153 static void
1154 blob_inflate(void)
1155 {
1156 	_blob_inflate(false);
1157 	_blob_inflate(true);
1158 }
1159 
1160 static void
1161 blob_delete(void)
1162 {
1163 	struct spdk_blob_store *bs = g_bs;
1164 	struct spdk_blob_opts blob_opts;
1165 	spdk_blob_id blobid;
1166 
1167 	/* Create a blob and then delete it. */
1168 	ut_spdk_blob_opts_init(&blob_opts);
1169 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
1170 	poll_threads();
1171 	CU_ASSERT(g_bserrno == 0);
1172 	CU_ASSERT(g_blobid > 0);
1173 	blobid = g_blobid;
1174 
1175 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
1176 	poll_threads();
1177 	CU_ASSERT(g_bserrno == 0);
1178 
1179 	/* Try to open the blob */
1180 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1181 	poll_threads();
1182 	CU_ASSERT(g_bserrno == -ENOENT);
1183 }
1184 
1185 static void
1186 blob_resize_test(void)
1187 {
1188 	struct spdk_blob_store *bs = g_bs;
1189 	struct spdk_blob *blob;
1190 	uint64_t free_clusters;
1191 
1192 	free_clusters = spdk_bs_free_cluster_count(bs);
1193 
1194 	blob = ut_blob_create_and_open(bs, NULL);
1195 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
1196 
1197 	/* Confirm that resize fails if blob is marked read-only. */
1198 	blob->md_ro = true;
1199 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1200 	poll_threads();
1201 	CU_ASSERT(g_bserrno == -EPERM);
1202 	blob->md_ro = false;
1203 
1204 	/* The blob started at 0 clusters. Resize it to be 5. */
1205 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1206 	poll_threads();
1207 	CU_ASSERT(g_bserrno == 0);
1208 	CU_ASSERT((free_clusters - 5) == spdk_bs_free_cluster_count(bs));
1209 
1210 	/* Shrink the blob to 3 clusters. This will not actually release
1211 	 * the old clusters until the blob is synced.
1212 	 */
1213 	spdk_blob_resize(blob, 3, blob_op_complete, NULL);
1214 	poll_threads();
1215 	CU_ASSERT(g_bserrno == 0);
1216 	/* Verify there are still 5 clusters in use */
1217 	CU_ASSERT((free_clusters - 5) == spdk_bs_free_cluster_count(bs));
1218 
1219 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
1220 	poll_threads();
1221 	CU_ASSERT(g_bserrno == 0);
1222 	/* Now there are only 3 clusters in use */
1223 	CU_ASSERT((free_clusters - 3) == spdk_bs_free_cluster_count(bs));
1224 
1225 	/* Resize the blob to be 10 clusters. Growth takes effect immediately. */
1226 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
1227 	poll_threads();
1228 	CU_ASSERT(g_bserrno == 0);
1229 	CU_ASSERT((free_clusters - 10) == spdk_bs_free_cluster_count(bs));
1230 
1231 	/* Try to resize the blob to size larger than blobstore. */
1232 	spdk_blob_resize(blob, bs->total_clusters + 1, blob_op_complete, NULL);
1233 	poll_threads();
1234 	CU_ASSERT(g_bserrno == -ENOSPC);
1235 
1236 	ut_blob_close_and_delete(bs, blob);
1237 }
1238 
1239 static void
1240 blob_read_only(void)
1241 {
1242 	struct spdk_blob_store *bs;
1243 	struct spdk_bs_dev *dev;
1244 	struct spdk_blob *blob;
1245 	struct spdk_bs_opts opts;
1246 	spdk_blob_id blobid;
1247 	int rc;
1248 
1249 	dev = init_dev();
1250 	spdk_bs_opts_init(&opts, sizeof(opts));
1251 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
1252 
1253 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
1254 	poll_threads();
1255 	CU_ASSERT(g_bserrno == 0);
1256 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1257 	bs = g_bs;
1258 
1259 	blob = ut_blob_create_and_open(bs, NULL);
1260 	blobid = spdk_blob_get_id(blob);
1261 
1262 	rc = spdk_blob_set_read_only(blob);
1263 	CU_ASSERT(rc == 0);
1264 
1265 	CU_ASSERT(blob->data_ro == false);
1266 	CU_ASSERT(blob->md_ro == false);
1267 
1268 	spdk_blob_sync_md(blob, bs_op_complete, NULL);
1269 	poll_threads();
1270 
1271 	CU_ASSERT(blob->data_ro == true);
1272 	CU_ASSERT(blob->md_ro == true);
1273 	CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY);
1274 
1275 	spdk_blob_close(blob, blob_op_complete, NULL);
1276 	poll_threads();
1277 	CU_ASSERT(g_bserrno == 0);
1278 
1279 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1280 	poll_threads();
1281 	CU_ASSERT(g_bserrno == 0);
1282 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1283 	blob = g_blob;
1284 
1285 	CU_ASSERT(blob->data_ro == true);
1286 	CU_ASSERT(blob->md_ro == true);
1287 	CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY);
1288 
1289 	spdk_blob_close(blob, blob_op_complete, NULL);
1290 	poll_threads();
1291 	CU_ASSERT(g_bserrno == 0);
1292 
1293 	ut_bs_reload(&bs, &opts);
1294 
1295 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1296 	poll_threads();
1297 	CU_ASSERT(g_bserrno == 0);
1298 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1299 	blob = g_blob;
1300 
1301 	CU_ASSERT(blob->data_ro == true);
1302 	CU_ASSERT(blob->md_ro == true);
1303 	CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY);
1304 
1305 	ut_blob_close_and_delete(bs, blob);
1306 
1307 	spdk_bs_unload(bs, bs_op_complete, NULL);
1308 	poll_threads();
1309 	CU_ASSERT(g_bserrno == 0);
1310 }
1311 
1312 static void
1313 channel_ops(void)
1314 {
1315 	struct spdk_blob_store *bs = g_bs;
1316 	struct spdk_io_channel *channel;
1317 
1318 	channel = spdk_bs_alloc_io_channel(bs);
1319 	CU_ASSERT(channel != NULL);
1320 
1321 	spdk_bs_free_io_channel(channel);
1322 	poll_threads();
1323 }
1324 
1325 static void
1326 blob_write(void)
1327 {
1328 	struct spdk_blob_store *bs = g_bs;
1329 	struct spdk_blob *blob = g_blob;
1330 	struct spdk_io_channel *channel;
1331 	uint64_t pages_per_cluster;
1332 	uint8_t payload[10 * 4096];
1333 
1334 	pages_per_cluster = spdk_bs_get_cluster_size(bs) / spdk_bs_get_page_size(bs);
1335 
1336 	channel = spdk_bs_alloc_io_channel(bs);
1337 	CU_ASSERT(channel != NULL);
1338 
1339 	/* Write to a blob with 0 size */
1340 	spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1341 	poll_threads();
1342 	CU_ASSERT(g_bserrno == -EINVAL);
1343 
1344 	/* Resize the blob */
1345 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1346 	poll_threads();
1347 	CU_ASSERT(g_bserrno == 0);
1348 
1349 	/* Confirm that write fails if blob is marked read-only. */
1350 	blob->data_ro = true;
1351 	spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1352 	poll_threads();
1353 	CU_ASSERT(g_bserrno == -EPERM);
1354 	blob->data_ro = false;
1355 
1356 	/* Write to the blob */
1357 	spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1358 	poll_threads();
1359 	CU_ASSERT(g_bserrno == 0);
1360 
1361 	/* Write starting beyond the end */
1362 	spdk_blob_io_write(blob, channel, payload, 5 * pages_per_cluster, 1, blob_op_complete,
1363 			   NULL);
1364 	poll_threads();
1365 	CU_ASSERT(g_bserrno == -EINVAL);
1366 
1367 	/* Write starting at a valid location but going off the end */
1368 	spdk_blob_io_write(blob, channel, payload, 4 * pages_per_cluster, pages_per_cluster + 1,
1369 			   blob_op_complete, NULL);
1370 	poll_threads();
1371 	CU_ASSERT(g_bserrno == -EINVAL);
1372 
1373 	spdk_bs_free_io_channel(channel);
1374 	poll_threads();
1375 }
1376 
1377 static void
1378 blob_read(void)
1379 {
1380 	struct spdk_blob_store *bs = g_bs;
1381 	struct spdk_blob *blob = g_blob;
1382 	struct spdk_io_channel *channel;
1383 	uint64_t pages_per_cluster;
1384 	uint8_t payload[10 * 4096];
1385 
1386 	pages_per_cluster = spdk_bs_get_cluster_size(bs) / spdk_bs_get_page_size(bs);
1387 
1388 	channel = spdk_bs_alloc_io_channel(bs);
1389 	CU_ASSERT(channel != NULL);
1390 
1391 	/* Read from a blob with 0 size */
1392 	spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1393 	poll_threads();
1394 	CU_ASSERT(g_bserrno == -EINVAL);
1395 
1396 	/* Resize the blob */
1397 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1398 	poll_threads();
1399 	CU_ASSERT(g_bserrno == 0);
1400 
1401 	/* Confirm that read passes if blob is marked read-only. */
1402 	blob->data_ro = true;
1403 	spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1404 	poll_threads();
1405 	CU_ASSERT(g_bserrno == 0);
1406 	blob->data_ro = false;
1407 
1408 	/* Read from the blob */
1409 	spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1410 	poll_threads();
1411 	CU_ASSERT(g_bserrno == 0);
1412 
1413 	/* Read starting beyond the end */
1414 	spdk_blob_io_read(blob, channel, payload, 5 * pages_per_cluster, 1, blob_op_complete,
1415 			  NULL);
1416 	poll_threads();
1417 	CU_ASSERT(g_bserrno == -EINVAL);
1418 
1419 	/* Read starting at a valid location but going off the end */
1420 	spdk_blob_io_read(blob, channel, payload, 4 * pages_per_cluster, pages_per_cluster + 1,
1421 			  blob_op_complete, NULL);
1422 	poll_threads();
1423 	CU_ASSERT(g_bserrno == -EINVAL);
1424 
1425 	spdk_bs_free_io_channel(channel);
1426 	poll_threads();
1427 }
1428 
1429 static void
1430 blob_rw_verify(void)
1431 {
1432 	struct spdk_blob_store *bs = g_bs;
1433 	struct spdk_blob *blob = g_blob;
1434 	struct spdk_io_channel *channel;
1435 	uint8_t payload_read[10 * 4096];
1436 	uint8_t payload_write[10 * 4096];
1437 
1438 	channel = spdk_bs_alloc_io_channel(bs);
1439 	CU_ASSERT(channel != NULL);
1440 
1441 	spdk_blob_resize(blob, 32, blob_op_complete, NULL);
1442 	poll_threads();
1443 	CU_ASSERT(g_bserrno == 0);
1444 
1445 	memset(payload_write, 0xE5, sizeof(payload_write));
1446 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
1447 	poll_threads();
1448 	CU_ASSERT(g_bserrno == 0);
1449 
1450 	memset(payload_read, 0x00, sizeof(payload_read));
1451 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
1452 	poll_threads();
1453 	CU_ASSERT(g_bserrno == 0);
1454 	CU_ASSERT(memcmp(payload_write, payload_read, 4 * 4096) == 0);
1455 
1456 	spdk_bs_free_io_channel(channel);
1457 	poll_threads();
1458 }
1459 
1460 static void
1461 blob_rw_verify_iov(void)
1462 {
1463 	struct spdk_blob_store *bs = g_bs;
1464 	struct spdk_blob *blob;
1465 	struct spdk_io_channel *channel;
1466 	uint8_t payload_read[10 * 4096];
1467 	uint8_t payload_write[10 * 4096];
1468 	struct iovec iov_read[3];
1469 	struct iovec iov_write[3];
1470 	void *buf;
1471 
1472 	channel = spdk_bs_alloc_io_channel(bs);
1473 	CU_ASSERT(channel != NULL);
1474 
1475 	blob = ut_blob_create_and_open(bs, NULL);
1476 
1477 	spdk_blob_resize(blob, 2, blob_op_complete, NULL);
1478 	poll_threads();
1479 	CU_ASSERT(g_bserrno == 0);
1480 
1481 	/*
1482 	 * Manually adjust the offset of the blob's second cluster.  This allows
1483 	 *  us to make sure that the readv/write code correctly accounts for I/O
1484 	 *  that cross cluster boundaries.  Start by asserting that the allocated
1485 	 *  clusters are where we expect before modifying the second cluster.
1486 	 */
1487 	CU_ASSERT(blob->active.clusters[0] == 1 * 256);
1488 	CU_ASSERT(blob->active.clusters[1] == 2 * 256);
1489 	blob->active.clusters[1] = 3 * 256;
1490 
1491 	memset(payload_write, 0xE5, sizeof(payload_write));
1492 	iov_write[0].iov_base = payload_write;
1493 	iov_write[0].iov_len = 1 * 4096;
1494 	iov_write[1].iov_base = payload_write + 1 * 4096;
1495 	iov_write[1].iov_len = 5 * 4096;
1496 	iov_write[2].iov_base = payload_write + 6 * 4096;
1497 	iov_write[2].iov_len = 4 * 4096;
1498 	/*
1499 	 * Choose a page offset just before the cluster boundary.  The first 6 pages of payload
1500 	 *  will get written to the first cluster, the last 4 to the second cluster.
1501 	 */
1502 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
1503 	poll_threads();
1504 	CU_ASSERT(g_bserrno == 0);
1505 
1506 	memset(payload_read, 0xAA, sizeof(payload_read));
1507 	iov_read[0].iov_base = payload_read;
1508 	iov_read[0].iov_len = 3 * 4096;
1509 	iov_read[1].iov_base = payload_read + 3 * 4096;
1510 	iov_read[1].iov_len = 4 * 4096;
1511 	iov_read[2].iov_base = payload_read + 7 * 4096;
1512 	iov_read[2].iov_len = 3 * 4096;
1513 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
1514 	poll_threads();
1515 	CU_ASSERT(g_bserrno == 0);
1516 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
1517 
1518 	buf = calloc(1, 256 * 4096);
1519 	SPDK_CU_ASSERT_FATAL(buf != NULL);
1520 	/* Check that cluster 2 on "disk" was not modified. */
1521 	CU_ASSERT(memcmp(buf, &g_dev_buffer[512 * 4096], 256 * 4096) == 0);
1522 	free(buf);
1523 
1524 	spdk_blob_close(blob, blob_op_complete, NULL);
1525 	poll_threads();
1526 	CU_ASSERT(g_bserrno == 0);
1527 
1528 	spdk_bs_free_io_channel(channel);
1529 	poll_threads();
1530 }
1531 
1532 static uint32_t
1533 bs_channel_get_req_count(struct spdk_io_channel *_channel)
1534 {
1535 	struct spdk_bs_channel *channel = spdk_io_channel_get_ctx(_channel);
1536 	struct spdk_bs_request_set *set;
1537 	uint32_t count = 0;
1538 
1539 	TAILQ_FOREACH(set, &channel->reqs, link) {
1540 		count++;
1541 	}
1542 
1543 	return count;
1544 }
1545 
1546 static void
1547 blob_rw_verify_iov_nomem(void)
1548 {
1549 	struct spdk_blob_store *bs = g_bs;
1550 	struct spdk_blob *blob = g_blob;
1551 	struct spdk_io_channel *channel;
1552 	uint8_t payload_write[10 * 4096];
1553 	struct iovec iov_write[3];
1554 	uint32_t req_count;
1555 
1556 	channel = spdk_bs_alloc_io_channel(bs);
1557 	CU_ASSERT(channel != NULL);
1558 
1559 	spdk_blob_resize(blob, 2, blob_op_complete, NULL);
1560 	poll_threads();
1561 	CU_ASSERT(g_bserrno == 0);
1562 
1563 	/*
1564 	 * Choose a page offset just before the cluster boundary.  The first 6 pages of payload
1565 	 *  will get written to the first cluster, the last 4 to the second cluster.
1566 	 */
1567 	iov_write[0].iov_base = payload_write;
1568 	iov_write[0].iov_len = 1 * 4096;
1569 	iov_write[1].iov_base = payload_write + 1 * 4096;
1570 	iov_write[1].iov_len = 5 * 4096;
1571 	iov_write[2].iov_base = payload_write + 6 * 4096;
1572 	iov_write[2].iov_len = 4 * 4096;
1573 	MOCK_SET(calloc, NULL);
1574 	req_count = bs_channel_get_req_count(channel);
1575 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
1576 	poll_threads();
1577 	CU_ASSERT(g_bserrno = -ENOMEM);
1578 	CU_ASSERT(req_count == bs_channel_get_req_count(channel));
1579 	MOCK_CLEAR(calloc);
1580 
1581 	spdk_bs_free_io_channel(channel);
1582 	poll_threads();
1583 }
1584 
1585 static void
1586 blob_rw_iov_read_only(void)
1587 {
1588 	struct spdk_blob_store *bs = g_bs;
1589 	struct spdk_blob *blob = g_blob;
1590 	struct spdk_io_channel *channel;
1591 	uint8_t payload_read[4096];
1592 	uint8_t payload_write[4096];
1593 	struct iovec iov_read;
1594 	struct iovec iov_write;
1595 
1596 	channel = spdk_bs_alloc_io_channel(bs);
1597 	CU_ASSERT(channel != NULL);
1598 
1599 	spdk_blob_resize(blob, 2, blob_op_complete, NULL);
1600 	poll_threads();
1601 	CU_ASSERT(g_bserrno == 0);
1602 
1603 	/* Verify that writev failed if read_only flag is set. */
1604 	blob->data_ro = true;
1605 	iov_write.iov_base = payload_write;
1606 	iov_write.iov_len = sizeof(payload_write);
1607 	spdk_blob_io_writev(blob, channel, &iov_write, 1, 0, 1, blob_op_complete, NULL);
1608 	poll_threads();
1609 	CU_ASSERT(g_bserrno == -EPERM);
1610 
1611 	/* Verify that reads pass if data_ro flag is set. */
1612 	iov_read.iov_base = payload_read;
1613 	iov_read.iov_len = sizeof(payload_read);
1614 	spdk_blob_io_readv(blob, channel, &iov_read, 1, 0, 1, blob_op_complete, NULL);
1615 	poll_threads();
1616 	CU_ASSERT(g_bserrno == 0);
1617 
1618 	spdk_bs_free_io_channel(channel);
1619 	poll_threads();
1620 }
1621 
1622 static void
1623 _blob_io_read_no_split(struct spdk_blob *blob, struct spdk_io_channel *channel,
1624 		       uint8_t *payload, uint64_t offset, uint64_t length,
1625 		       spdk_blob_op_complete cb_fn, void *cb_arg)
1626 {
1627 	uint64_t i;
1628 	uint8_t *buf;
1629 	uint64_t page_size = spdk_bs_get_page_size(blob->bs);
1630 
1631 	/* To be sure that operation is NOT split, read one page at the time */
1632 	buf = payload;
1633 	for (i = 0; i < length; i++) {
1634 		spdk_blob_io_read(blob, channel, buf, i + offset, 1, blob_op_complete, NULL);
1635 		poll_threads();
1636 		if (g_bserrno != 0) {
1637 			/* Pass the error code up */
1638 			break;
1639 		}
1640 		buf += page_size;
1641 	}
1642 
1643 	cb_fn(cb_arg, g_bserrno);
1644 }
1645 
1646 static void
1647 _blob_io_write_no_split(struct spdk_blob *blob, struct spdk_io_channel *channel,
1648 			uint8_t *payload, uint64_t offset, uint64_t length,
1649 			spdk_blob_op_complete cb_fn, void *cb_arg)
1650 {
1651 	uint64_t i;
1652 	uint8_t *buf;
1653 	uint64_t page_size = spdk_bs_get_page_size(blob->bs);
1654 
1655 	/* To be sure that operation is NOT split, write one page at the time */
1656 	buf = payload;
1657 	for (i = 0; i < length; i++) {
1658 		spdk_blob_io_write(blob, channel, buf, i + offset, 1, blob_op_complete, NULL);
1659 		poll_threads();
1660 		if (g_bserrno != 0) {
1661 			/* Pass the error code up */
1662 			break;
1663 		}
1664 		buf += page_size;
1665 	}
1666 
1667 	cb_fn(cb_arg, g_bserrno);
1668 }
1669 
1670 static void
1671 blob_operation_split_rw(void)
1672 {
1673 	struct spdk_blob_store *bs = g_bs;
1674 	struct spdk_blob *blob;
1675 	struct spdk_io_channel *channel;
1676 	struct spdk_blob_opts opts;
1677 	uint64_t cluster_size;
1678 
1679 	uint64_t payload_size;
1680 	uint8_t *payload_read;
1681 	uint8_t *payload_write;
1682 	uint8_t *payload_pattern;
1683 
1684 	uint64_t page_size;
1685 	uint64_t pages_per_cluster;
1686 	uint64_t pages_per_payload;
1687 
1688 	uint64_t i;
1689 
1690 	cluster_size = spdk_bs_get_cluster_size(bs);
1691 	page_size = spdk_bs_get_page_size(bs);
1692 	pages_per_cluster = cluster_size / page_size;
1693 	pages_per_payload = pages_per_cluster * 5;
1694 	payload_size = cluster_size * 5;
1695 
1696 	payload_read = malloc(payload_size);
1697 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
1698 
1699 	payload_write = malloc(payload_size);
1700 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
1701 
1702 	payload_pattern = malloc(payload_size);
1703 	SPDK_CU_ASSERT_FATAL(payload_pattern != NULL);
1704 
1705 	/* Prepare random pattern to write */
1706 	memset(payload_pattern, 0xFF, payload_size);
1707 	for (i = 0; i < pages_per_payload; i++) {
1708 		*((uint64_t *)(payload_pattern + page_size * i)) = (i + 1);
1709 	}
1710 
1711 	channel = spdk_bs_alloc_io_channel(bs);
1712 	SPDK_CU_ASSERT_FATAL(channel != NULL);
1713 
1714 	/* Create blob */
1715 	ut_spdk_blob_opts_init(&opts);
1716 	opts.thin_provision = false;
1717 	opts.num_clusters = 5;
1718 
1719 	blob = ut_blob_create_and_open(bs, &opts);
1720 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
1721 
1722 	/* Initial read should return zeroed payload */
1723 	memset(payload_read, 0xFF, payload_size);
1724 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1725 	poll_threads();
1726 	CU_ASSERT(g_bserrno == 0);
1727 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
1728 
1729 	/* Fill whole blob except last page */
1730 	spdk_blob_io_write(blob, channel, payload_pattern, 0, pages_per_payload - 1,
1731 			   blob_op_complete, NULL);
1732 	poll_threads();
1733 	CU_ASSERT(g_bserrno == 0);
1734 
1735 	/* Write last page with a pattern */
1736 	spdk_blob_io_write(blob, channel, payload_pattern, pages_per_payload - 1, 1,
1737 			   blob_op_complete, NULL);
1738 	poll_threads();
1739 	CU_ASSERT(g_bserrno == 0);
1740 
1741 	/* Read whole blob and check consistency */
1742 	memset(payload_read, 0xFF, payload_size);
1743 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1744 	poll_threads();
1745 	CU_ASSERT(g_bserrno == 0);
1746 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size - page_size) == 0);
1747 	CU_ASSERT(memcmp(payload_pattern, payload_read + payload_size - page_size, page_size) == 0);
1748 
1749 	/* Fill whole blob except first page */
1750 	spdk_blob_io_write(blob, channel, payload_pattern, 1, pages_per_payload - 1,
1751 			   blob_op_complete, NULL);
1752 	poll_threads();
1753 	CU_ASSERT(g_bserrno == 0);
1754 
1755 	/* Write first page with a pattern */
1756 	spdk_blob_io_write(blob, channel, payload_pattern, 0, 1,
1757 			   blob_op_complete, NULL);
1758 	poll_threads();
1759 	CU_ASSERT(g_bserrno == 0);
1760 
1761 	/* Read whole blob and check consistency */
1762 	memset(payload_read, 0xFF, payload_size);
1763 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1764 	poll_threads();
1765 	CU_ASSERT(g_bserrno == 0);
1766 	CU_ASSERT(memcmp(payload_pattern, payload_read + page_size, payload_size - page_size) == 0);
1767 	CU_ASSERT(memcmp(payload_pattern, payload_read, page_size) == 0);
1768 
1769 
1770 	/* Fill whole blob with a pattern (5 clusters) */
1771 
1772 	/* 1. Read test. */
1773 	_blob_io_write_no_split(blob, channel, payload_pattern, 0, pages_per_payload,
1774 				blob_op_complete, NULL);
1775 	poll_threads();
1776 	CU_ASSERT(g_bserrno == 0);
1777 
1778 	memset(payload_read, 0xFF, payload_size);
1779 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1780 	poll_threads();
1781 	poll_threads();
1782 	CU_ASSERT(g_bserrno == 0);
1783 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1784 
1785 	/* 2. Write test. */
1786 	spdk_blob_io_write(blob, channel, payload_pattern, 0, pages_per_payload,
1787 			   blob_op_complete, NULL);
1788 	poll_threads();
1789 	CU_ASSERT(g_bserrno == 0);
1790 
1791 	memset(payload_read, 0xFF, payload_size);
1792 	_blob_io_read_no_split(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1793 	poll_threads();
1794 	CU_ASSERT(g_bserrno == 0);
1795 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1796 
1797 	spdk_bs_free_io_channel(channel);
1798 	poll_threads();
1799 
1800 	g_blob = NULL;
1801 	g_blobid = 0;
1802 
1803 	free(payload_read);
1804 	free(payload_write);
1805 	free(payload_pattern);
1806 
1807 	ut_blob_close_and_delete(bs, blob);
1808 }
1809 
1810 static void
1811 blob_operation_split_rw_iov(void)
1812 {
1813 	struct spdk_blob_store *bs = g_bs;
1814 	struct spdk_blob *blob;
1815 	struct spdk_io_channel *channel;
1816 	struct spdk_blob_opts opts;
1817 	uint64_t cluster_size;
1818 
1819 	uint64_t payload_size;
1820 	uint8_t *payload_read;
1821 	uint8_t *payload_write;
1822 	uint8_t *payload_pattern;
1823 
1824 	uint64_t page_size;
1825 	uint64_t pages_per_cluster;
1826 	uint64_t pages_per_payload;
1827 
1828 	struct iovec iov_read[2];
1829 	struct iovec iov_write[2];
1830 
1831 	uint64_t i, j;
1832 
1833 	cluster_size = spdk_bs_get_cluster_size(bs);
1834 	page_size = spdk_bs_get_page_size(bs);
1835 	pages_per_cluster = cluster_size / page_size;
1836 	pages_per_payload = pages_per_cluster * 5;
1837 	payload_size = cluster_size * 5;
1838 
1839 	payload_read = malloc(payload_size);
1840 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
1841 
1842 	payload_write = malloc(payload_size);
1843 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
1844 
1845 	payload_pattern = malloc(payload_size);
1846 	SPDK_CU_ASSERT_FATAL(payload_pattern != NULL);
1847 
1848 	/* Prepare random pattern to write */
1849 	for (i = 0; i < pages_per_payload; i++) {
1850 		for (j = 0; j < page_size / sizeof(uint64_t); j++) {
1851 			uint64_t *tmp;
1852 
1853 			tmp = (uint64_t *)payload_pattern;
1854 			tmp += ((page_size * i) / sizeof(uint64_t)) + j;
1855 			*tmp = i + 1;
1856 		}
1857 	}
1858 
1859 	channel = spdk_bs_alloc_io_channel(bs);
1860 	SPDK_CU_ASSERT_FATAL(channel != NULL);
1861 
1862 	/* Create blob */
1863 	ut_spdk_blob_opts_init(&opts);
1864 	opts.thin_provision = false;
1865 	opts.num_clusters = 5;
1866 
1867 	blob = ut_blob_create_and_open(bs, &opts);
1868 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
1869 
1870 	/* Initial read should return zeroes payload */
1871 	memset(payload_read, 0xFF, payload_size);
1872 	iov_read[0].iov_base = payload_read;
1873 	iov_read[0].iov_len = cluster_size * 3;
1874 	iov_read[1].iov_base = payload_read + cluster_size * 3;
1875 	iov_read[1].iov_len = cluster_size * 2;
1876 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1877 	poll_threads();
1878 	CU_ASSERT(g_bserrno == 0);
1879 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
1880 
1881 	/* First of iovs fills whole blob except last page and second of iovs writes last page
1882 	 *  with a pattern. */
1883 	iov_write[0].iov_base = payload_pattern;
1884 	iov_write[0].iov_len = payload_size - page_size;
1885 	iov_write[1].iov_base = payload_pattern;
1886 	iov_write[1].iov_len = page_size;
1887 	spdk_blob_io_writev(blob, channel, iov_write, 2, 0, pages_per_payload, blob_op_complete, NULL);
1888 	poll_threads();
1889 	CU_ASSERT(g_bserrno == 0);
1890 
1891 	/* Read whole blob and check consistency */
1892 	memset(payload_read, 0xFF, payload_size);
1893 	iov_read[0].iov_base = payload_read;
1894 	iov_read[0].iov_len = cluster_size * 2;
1895 	iov_read[1].iov_base = payload_read + cluster_size * 2;
1896 	iov_read[1].iov_len = cluster_size * 3;
1897 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1898 	poll_threads();
1899 	CU_ASSERT(g_bserrno == 0);
1900 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size - page_size) == 0);
1901 	CU_ASSERT(memcmp(payload_pattern, payload_read + payload_size - page_size, page_size) == 0);
1902 
1903 	/* First of iovs fills only first page and second of iovs writes whole blob except
1904 	 *  first page with a pattern. */
1905 	iov_write[0].iov_base = payload_pattern;
1906 	iov_write[0].iov_len = page_size;
1907 	iov_write[1].iov_base = payload_pattern;
1908 	iov_write[1].iov_len = payload_size - page_size;
1909 	spdk_blob_io_writev(blob, channel, iov_write, 2, 0, pages_per_payload, blob_op_complete, NULL);
1910 	poll_threads();
1911 	CU_ASSERT(g_bserrno == 0);
1912 
1913 	/* Read whole blob and check consistency */
1914 	memset(payload_read, 0xFF, payload_size);
1915 	iov_read[0].iov_base = payload_read;
1916 	iov_read[0].iov_len = cluster_size * 4;
1917 	iov_read[1].iov_base = payload_read + cluster_size * 4;
1918 	iov_read[1].iov_len = cluster_size;
1919 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1920 	poll_threads();
1921 	CU_ASSERT(g_bserrno == 0);
1922 	CU_ASSERT(memcmp(payload_pattern, payload_read + page_size, payload_size - page_size) == 0);
1923 	CU_ASSERT(memcmp(payload_pattern, payload_read, page_size) == 0);
1924 
1925 
1926 	/* Fill whole blob with a pattern (5 clusters) */
1927 
1928 	/* 1. Read test. */
1929 	_blob_io_write_no_split(blob, channel, payload_pattern, 0, pages_per_payload,
1930 				blob_op_complete, NULL);
1931 	poll_threads();
1932 	CU_ASSERT(g_bserrno == 0);
1933 
1934 	memset(payload_read, 0xFF, payload_size);
1935 	iov_read[0].iov_base = payload_read;
1936 	iov_read[0].iov_len = cluster_size;
1937 	iov_read[1].iov_base = payload_read + cluster_size;
1938 	iov_read[1].iov_len = cluster_size * 4;
1939 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1940 	poll_threads();
1941 	CU_ASSERT(g_bserrno == 0);
1942 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1943 
1944 	/* 2. Write test. */
1945 	iov_write[0].iov_base = payload_read;
1946 	iov_write[0].iov_len = cluster_size * 2;
1947 	iov_write[1].iov_base = payload_read + cluster_size * 2;
1948 	iov_write[1].iov_len = cluster_size * 3;
1949 	spdk_blob_io_writev(blob, channel, iov_write, 2, 0, pages_per_payload, blob_op_complete, NULL);
1950 	poll_threads();
1951 	CU_ASSERT(g_bserrno == 0);
1952 
1953 	memset(payload_read, 0xFF, payload_size);
1954 	_blob_io_read_no_split(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1955 	poll_threads();
1956 	CU_ASSERT(g_bserrno == 0);
1957 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1958 
1959 	spdk_bs_free_io_channel(channel);
1960 	poll_threads();
1961 
1962 	g_blob = NULL;
1963 	g_blobid = 0;
1964 
1965 	free(payload_read);
1966 	free(payload_write);
1967 	free(payload_pattern);
1968 
1969 	ut_blob_close_and_delete(bs, blob);
1970 }
1971 
1972 static void
1973 blob_unmap(void)
1974 {
1975 	struct spdk_blob_store *bs = g_bs;
1976 	struct spdk_blob *blob;
1977 	struct spdk_io_channel *channel;
1978 	struct spdk_blob_opts opts;
1979 	uint8_t payload[4096];
1980 	int i;
1981 
1982 	channel = spdk_bs_alloc_io_channel(bs);
1983 	CU_ASSERT(channel != NULL);
1984 
1985 	ut_spdk_blob_opts_init(&opts);
1986 	opts.num_clusters = 10;
1987 
1988 	blob = ut_blob_create_and_open(bs, &opts);
1989 
1990 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
1991 	poll_threads();
1992 	CU_ASSERT(g_bserrno == 0);
1993 
1994 	memset(payload, 0, sizeof(payload));
1995 	payload[0] = 0xFF;
1996 
1997 	/*
1998 	 * Set first byte of every cluster to 0xFF.
1999 	 * First cluster on device is reserved so let's start from cluster number 1
2000 	 */
2001 	for (i = 1; i < 11; i++) {
2002 		g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] = 0xFF;
2003 	}
2004 
2005 	/* Confirm writes */
2006 	for (i = 0; i < 10; i++) {
2007 		payload[0] = 0;
2008 		spdk_blob_io_read(blob, channel, &payload, i * SPDK_BLOB_OPTS_CLUSTER_SZ / 4096, 1,
2009 				  blob_op_complete, NULL);
2010 		poll_threads();
2011 		CU_ASSERT(g_bserrno == 0);
2012 		CU_ASSERT(payload[0] == 0xFF);
2013 	}
2014 
2015 	/* Mark some clusters as unallocated */
2016 	blob->active.clusters[1] = 0;
2017 	blob->active.clusters[2] = 0;
2018 	blob->active.clusters[3] = 0;
2019 	blob->active.clusters[6] = 0;
2020 	blob->active.clusters[8] = 0;
2021 
2022 	/* Unmap clusters by resizing to 0 */
2023 	spdk_blob_resize(blob, 0, blob_op_complete, NULL);
2024 	poll_threads();
2025 	CU_ASSERT(g_bserrno == 0);
2026 
2027 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2028 	poll_threads();
2029 	CU_ASSERT(g_bserrno == 0);
2030 
2031 	/* Confirm that only 'allocated' clusters were unmapped */
2032 	for (i = 1; i < 11; i++) {
2033 		switch (i) {
2034 		case 2:
2035 		case 3:
2036 		case 4:
2037 		case 7:
2038 		case 9:
2039 			CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0xFF);
2040 			break;
2041 		default:
2042 			CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0);
2043 			break;
2044 		}
2045 	}
2046 
2047 	spdk_bs_free_io_channel(channel);
2048 	poll_threads();
2049 
2050 	ut_blob_close_and_delete(bs, blob);
2051 }
2052 
2053 static void
2054 blob_iter(void)
2055 {
2056 	struct spdk_blob_store *bs = g_bs;
2057 	struct spdk_blob *blob;
2058 	spdk_blob_id blobid;
2059 	struct spdk_blob_opts blob_opts;
2060 
2061 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
2062 	poll_threads();
2063 	CU_ASSERT(g_blob == NULL);
2064 	CU_ASSERT(g_bserrno == -ENOENT);
2065 
2066 	ut_spdk_blob_opts_init(&blob_opts);
2067 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
2068 	poll_threads();
2069 	CU_ASSERT(g_bserrno == 0);
2070 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2071 	blobid = g_blobid;
2072 
2073 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
2074 	poll_threads();
2075 	CU_ASSERT(g_blob != NULL);
2076 	CU_ASSERT(g_bserrno == 0);
2077 	blob = g_blob;
2078 	CU_ASSERT(spdk_blob_get_id(blob) == blobid);
2079 
2080 	spdk_bs_iter_next(bs, blob, blob_op_with_handle_complete, NULL);
2081 	poll_threads();
2082 	CU_ASSERT(g_blob == NULL);
2083 	CU_ASSERT(g_bserrno == -ENOENT);
2084 }
2085 
2086 static void
2087 blob_xattr(void)
2088 {
2089 	struct spdk_blob_store *bs = g_bs;
2090 	struct spdk_blob *blob = g_blob;
2091 	spdk_blob_id blobid = spdk_blob_get_id(blob);
2092 	uint64_t length;
2093 	int rc;
2094 	const char *name1, *name2;
2095 	const void *value;
2096 	size_t value_len;
2097 	struct spdk_xattr_names *names;
2098 
2099 	/* Test that set_xattr fails if md_ro flag is set. */
2100 	blob->md_ro = true;
2101 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2102 	CU_ASSERT(rc == -EPERM);
2103 
2104 	blob->md_ro = false;
2105 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2106 	CU_ASSERT(rc == 0);
2107 
2108 	length = 2345;
2109 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2110 	CU_ASSERT(rc == 0);
2111 
2112 	/* Overwrite "length" xattr. */
2113 	length = 3456;
2114 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2115 	CU_ASSERT(rc == 0);
2116 
2117 	/* get_xattr should still work even if md_ro flag is set. */
2118 	value = NULL;
2119 	blob->md_ro = true;
2120 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2121 	CU_ASSERT(rc == 0);
2122 	SPDK_CU_ASSERT_FATAL(value != NULL);
2123 	CU_ASSERT(*(uint64_t *)value == length);
2124 	CU_ASSERT(value_len == 8);
2125 	blob->md_ro = false;
2126 
2127 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
2128 	CU_ASSERT(rc == -ENOENT);
2129 
2130 	names = NULL;
2131 	rc = spdk_blob_get_xattr_names(blob, &names);
2132 	CU_ASSERT(rc == 0);
2133 	SPDK_CU_ASSERT_FATAL(names != NULL);
2134 	CU_ASSERT(spdk_xattr_names_get_count(names) == 2);
2135 	name1 = spdk_xattr_names_get_name(names, 0);
2136 	SPDK_CU_ASSERT_FATAL(name1 != NULL);
2137 	CU_ASSERT(!strcmp(name1, "name") || !strcmp(name1, "length"));
2138 	name2 = spdk_xattr_names_get_name(names, 1);
2139 	SPDK_CU_ASSERT_FATAL(name2 != NULL);
2140 	CU_ASSERT(!strcmp(name2, "name") || !strcmp(name2, "length"));
2141 	CU_ASSERT(strcmp(name1, name2));
2142 	spdk_xattr_names_free(names);
2143 
2144 	/* Confirm that remove_xattr fails if md_ro is set to true. */
2145 	blob->md_ro = true;
2146 	rc = spdk_blob_remove_xattr(blob, "name");
2147 	CU_ASSERT(rc == -EPERM);
2148 
2149 	blob->md_ro = false;
2150 	rc = spdk_blob_remove_xattr(blob, "name");
2151 	CU_ASSERT(rc == 0);
2152 
2153 	rc = spdk_blob_remove_xattr(blob, "foobar");
2154 	CU_ASSERT(rc == -ENOENT);
2155 
2156 	/* Set internal xattr */
2157 	length = 7898;
2158 	rc = blob_set_xattr(blob, "internal", &length, sizeof(length), true);
2159 	CU_ASSERT(rc == 0);
2160 	rc = blob_get_xattr_value(blob, "internal", &value, &value_len, true);
2161 	CU_ASSERT(rc == 0);
2162 	CU_ASSERT(*(uint64_t *)value == length);
2163 	/* try to get public xattr with same name */
2164 	rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len);
2165 	CU_ASSERT(rc != 0);
2166 	rc = blob_get_xattr_value(blob, "internal", &value, &value_len, false);
2167 	CU_ASSERT(rc != 0);
2168 	/* Check if SPDK_BLOB_INTERNAL_XATTR is set */
2169 	CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) ==
2170 		  SPDK_BLOB_INTERNAL_XATTR);
2171 
2172 	spdk_blob_close(blob, blob_op_complete, NULL);
2173 	poll_threads();
2174 
2175 	/* Check if xattrs are persisted */
2176 	ut_bs_reload(&bs, NULL);
2177 
2178 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2179 	poll_threads();
2180 	CU_ASSERT(g_bserrno == 0);
2181 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2182 	blob = g_blob;
2183 
2184 	rc = blob_get_xattr_value(blob, "internal", &value, &value_len, true);
2185 	CU_ASSERT(rc == 0);
2186 	CU_ASSERT(*(uint64_t *)value == length);
2187 
2188 	/* try to get internal xattr trough public call */
2189 	rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len);
2190 	CU_ASSERT(rc != 0);
2191 
2192 	rc = blob_remove_xattr(blob, "internal", true);
2193 	CU_ASSERT(rc == 0);
2194 
2195 	CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) == 0);
2196 }
2197 
2198 static void
2199 blob_parse_md(void)
2200 {
2201 	struct spdk_blob_store *bs = g_bs;
2202 	struct spdk_blob *blob;
2203 	int rc;
2204 	uint32_t used_pages;
2205 	size_t xattr_length;
2206 	char *xattr;
2207 
2208 	used_pages = spdk_bit_array_count_set(bs->used_md_pages);
2209 	blob = ut_blob_create_and_open(bs, NULL);
2210 
2211 	/* Create large extent to force more than 1 page of metadata. */
2212 	xattr_length = SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_xattr) -
2213 		       strlen("large_xattr");
2214 	xattr = calloc(xattr_length, sizeof(char));
2215 	SPDK_CU_ASSERT_FATAL(xattr != NULL);
2216 	rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
2217 	free(xattr);
2218 	SPDK_CU_ASSERT_FATAL(rc == 0);
2219 
2220 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2221 	poll_threads();
2222 
2223 	/* Delete the blob and verify that number of pages returned to before its creation. */
2224 	SPDK_CU_ASSERT_FATAL(used_pages != spdk_bit_array_count_set(bs->used_md_pages));
2225 	ut_blob_close_and_delete(bs, blob);
2226 	SPDK_CU_ASSERT_FATAL(used_pages == spdk_bit_array_count_set(bs->used_md_pages));
2227 }
2228 
2229 static void
2230 bs_load(void)
2231 {
2232 	struct spdk_blob_store *bs;
2233 	struct spdk_bs_dev *dev;
2234 	spdk_blob_id blobid;
2235 	struct spdk_blob *blob;
2236 	struct spdk_bs_super_block *super_block;
2237 	uint64_t length;
2238 	int rc;
2239 	const void *value;
2240 	size_t value_len;
2241 	struct spdk_bs_opts opts;
2242 	struct spdk_blob_opts blob_opts;
2243 
2244 	dev = init_dev();
2245 	spdk_bs_opts_init(&opts, sizeof(opts));
2246 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2247 
2248 	/* Initialize a new blob store */
2249 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2250 	poll_threads();
2251 	CU_ASSERT(g_bserrno == 0);
2252 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2253 	bs = g_bs;
2254 
2255 	/* Try to open a blobid that does not exist */
2256 	spdk_bs_open_blob(bs, 0, blob_op_with_handle_complete, NULL);
2257 	poll_threads();
2258 	CU_ASSERT(g_bserrno == -ENOENT);
2259 	CU_ASSERT(g_blob == NULL);
2260 
2261 	/* Create a blob */
2262 	blob = ut_blob_create_and_open(bs, NULL);
2263 	blobid = spdk_blob_get_id(blob);
2264 
2265 	/* Try again to open valid blob but without the upper bit set */
2266 	spdk_bs_open_blob(bs, blobid & 0xFFFFFFFF, blob_op_with_handle_complete, NULL);
2267 	poll_threads();
2268 	CU_ASSERT(g_bserrno == -ENOENT);
2269 	CU_ASSERT(g_blob == NULL);
2270 
2271 	/* Set some xattrs */
2272 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2273 	CU_ASSERT(rc == 0);
2274 
2275 	length = 2345;
2276 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2277 	CU_ASSERT(rc == 0);
2278 
2279 	/* Resize the blob */
2280 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
2281 	poll_threads();
2282 	CU_ASSERT(g_bserrno == 0);
2283 
2284 	spdk_blob_close(blob, blob_op_complete, NULL);
2285 	poll_threads();
2286 	CU_ASSERT(g_bserrno == 0);
2287 	blob = NULL;
2288 	g_blob = NULL;
2289 	g_blobid = SPDK_BLOBID_INVALID;
2290 
2291 	/* Unload the blob store */
2292 	spdk_bs_unload(bs, bs_op_complete, NULL);
2293 	poll_threads();
2294 	CU_ASSERT(g_bserrno == 0);
2295 	g_bs = NULL;
2296 	g_blob = NULL;
2297 	g_blobid = 0;
2298 
2299 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2300 	CU_ASSERT(super_block->clean == 1);
2301 
2302 	/* Load should fail for device with an unsupported blocklen */
2303 	dev = init_dev();
2304 	dev->blocklen = SPDK_BS_PAGE_SIZE * 2;
2305 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
2306 	poll_threads();
2307 	CU_ASSERT(g_bserrno == -EINVAL);
2308 
2309 	/* Load should when max_md_ops is set to zero */
2310 	dev = init_dev();
2311 	spdk_bs_opts_init(&opts, sizeof(opts));
2312 	opts.max_md_ops = 0;
2313 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2314 	poll_threads();
2315 	CU_ASSERT(g_bserrno == -EINVAL);
2316 
2317 	/* Load should when max_channel_ops is set to zero */
2318 	dev = init_dev();
2319 	spdk_bs_opts_init(&opts, sizeof(opts));
2320 	opts.max_channel_ops = 0;
2321 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2322 	poll_threads();
2323 	CU_ASSERT(g_bserrno == -EINVAL);
2324 
2325 	/* Load an existing blob store */
2326 	dev = init_dev();
2327 	spdk_bs_opts_init(&opts, sizeof(opts));
2328 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2329 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2330 	poll_threads();
2331 	CU_ASSERT(g_bserrno == 0);
2332 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2333 	bs = g_bs;
2334 
2335 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2336 	CU_ASSERT(super_block->clean == 1);
2337 	CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen);
2338 
2339 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2340 	poll_threads();
2341 	CU_ASSERT(g_bserrno == 0);
2342 	CU_ASSERT(g_blob != NULL);
2343 	blob = g_blob;
2344 
2345 	/* Verify that blobstore is marked dirty after first metadata sync */
2346 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2347 	CU_ASSERT(super_block->clean == 1);
2348 
2349 	/* Get the xattrs */
2350 	value = NULL;
2351 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2352 	CU_ASSERT(rc == 0);
2353 	SPDK_CU_ASSERT_FATAL(value != NULL);
2354 	CU_ASSERT(*(uint64_t *)value == length);
2355 	CU_ASSERT(value_len == 8);
2356 
2357 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
2358 	CU_ASSERT(rc == -ENOENT);
2359 
2360 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
2361 
2362 	spdk_blob_close(blob, blob_op_complete, NULL);
2363 	poll_threads();
2364 	CU_ASSERT(g_bserrno == 0);
2365 	blob = NULL;
2366 	g_blob = NULL;
2367 
2368 	spdk_bs_unload(bs, bs_op_complete, NULL);
2369 	poll_threads();
2370 	CU_ASSERT(g_bserrno == 0);
2371 	g_bs = NULL;
2372 
2373 	/* Load should fail: bdev size < saved size */
2374 	dev = init_dev();
2375 	dev->blockcnt /= 2;
2376 
2377 	spdk_bs_opts_init(&opts, sizeof(opts));
2378 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2379 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2380 	poll_threads();
2381 
2382 	CU_ASSERT(g_bserrno == -EILSEQ);
2383 
2384 	/* Load should succeed: bdev size > saved size */
2385 	dev = init_dev();
2386 	dev->blockcnt *= 4;
2387 
2388 	spdk_bs_opts_init(&opts, sizeof(opts));
2389 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2390 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2391 	poll_threads();
2392 	CU_ASSERT(g_bserrno == 0);
2393 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2394 	bs = g_bs;
2395 
2396 	CU_ASSERT(g_bserrno == 0);
2397 	spdk_bs_unload(bs, bs_op_complete, NULL);
2398 	poll_threads();
2399 
2400 
2401 	/* Test compatibility mode */
2402 
2403 	dev = init_dev();
2404 	super_block->size = 0;
2405 	super_block->crc = blob_md_page_calc_crc(super_block);
2406 
2407 	spdk_bs_opts_init(&opts, sizeof(opts));
2408 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2409 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2410 	poll_threads();
2411 	CU_ASSERT(g_bserrno == 0);
2412 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2413 	bs = g_bs;
2414 
2415 	/* Create a blob */
2416 	ut_spdk_blob_opts_init(&blob_opts);
2417 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
2418 	poll_threads();
2419 	CU_ASSERT(g_bserrno == 0);
2420 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2421 
2422 	/* Blobstore should update number of blocks in super_block */
2423 	CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen);
2424 	CU_ASSERT(super_block->clean == 0);
2425 
2426 	spdk_bs_unload(bs, bs_op_complete, NULL);
2427 	poll_threads();
2428 	CU_ASSERT(g_bserrno == 0);
2429 	CU_ASSERT(super_block->clean == 1);
2430 	g_bs = NULL;
2431 
2432 }
2433 
2434 static void
2435 bs_load_pending_removal(void)
2436 {
2437 	struct spdk_blob_store *bs = g_bs;
2438 	struct spdk_blob_opts opts;
2439 	struct spdk_blob *blob, *snapshot;
2440 	spdk_blob_id blobid, snapshotid;
2441 	const void *value;
2442 	size_t value_len;
2443 	int rc;
2444 
2445 	/* Create blob */
2446 	ut_spdk_blob_opts_init(&opts);
2447 	opts.num_clusters = 10;
2448 
2449 	blob = ut_blob_create_and_open(bs, &opts);
2450 	blobid = spdk_blob_get_id(blob);
2451 
2452 	/* Create snapshot */
2453 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
2454 	poll_threads();
2455 	CU_ASSERT(g_bserrno == 0);
2456 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2457 	snapshotid = g_blobid;
2458 
2459 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
2460 	poll_threads();
2461 	CU_ASSERT(g_bserrno == 0);
2462 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2463 	snapshot = g_blob;
2464 
2465 	/* Set SNAPSHOT_PENDING_REMOVAL xattr */
2466 	snapshot->md_ro = false;
2467 	rc = blob_set_xattr(snapshot, SNAPSHOT_PENDING_REMOVAL, &blobid, sizeof(spdk_blob_id), true);
2468 	CU_ASSERT(rc == 0);
2469 	snapshot->md_ro = true;
2470 
2471 	spdk_blob_close(snapshot, blob_op_complete, NULL);
2472 	poll_threads();
2473 	CU_ASSERT(g_bserrno == 0);
2474 
2475 	spdk_blob_close(blob, blob_op_complete, NULL);
2476 	poll_threads();
2477 	CU_ASSERT(g_bserrno == 0);
2478 
2479 	/* Reload blobstore */
2480 	ut_bs_reload(&bs, NULL);
2481 
2482 	/* Snapshot should not be removed as blob is still pointing to it */
2483 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
2484 	poll_threads();
2485 	CU_ASSERT(g_bserrno == 0);
2486 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2487 	snapshot = g_blob;
2488 
2489 	/* SNAPSHOT_PENDING_REMOVAL xattr should be removed during load */
2490 	rc = spdk_blob_get_xattr_value(snapshot, SNAPSHOT_PENDING_REMOVAL, &value, &value_len);
2491 	CU_ASSERT(rc != 0);
2492 
2493 	/* Set SNAPSHOT_PENDING_REMOVAL xattr again */
2494 	snapshot->md_ro = false;
2495 	rc = blob_set_xattr(snapshot, SNAPSHOT_PENDING_REMOVAL, &blobid, sizeof(spdk_blob_id), true);
2496 	CU_ASSERT(rc == 0);
2497 	snapshot->md_ro = true;
2498 
2499 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2500 	poll_threads();
2501 	CU_ASSERT(g_bserrno == 0);
2502 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2503 	blob = g_blob;
2504 
2505 	/* Remove parent_id from blob by removing BLOB_SNAPSHOT xattr */
2506 	blob_remove_xattr(blob, BLOB_SNAPSHOT, true);
2507 
2508 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2509 	poll_threads();
2510 	CU_ASSERT(g_bserrno == 0);
2511 
2512 	spdk_blob_close(snapshot, blob_op_complete, NULL);
2513 	poll_threads();
2514 	CU_ASSERT(g_bserrno == 0);
2515 
2516 	spdk_blob_close(blob, blob_op_complete, NULL);
2517 	poll_threads();
2518 	CU_ASSERT(g_bserrno == 0);
2519 
2520 	/* Reload blobstore */
2521 	ut_bs_reload(&bs, NULL);
2522 
2523 	/* Snapshot should be removed as blob is not pointing to it anymore */
2524 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
2525 	poll_threads();
2526 	CU_ASSERT(g_bserrno != 0);
2527 }
2528 
2529 static void
2530 bs_load_custom_cluster_size(void)
2531 {
2532 	struct spdk_blob_store *bs;
2533 	struct spdk_bs_dev *dev;
2534 	struct spdk_bs_super_block *super_block;
2535 	struct spdk_bs_opts opts;
2536 	uint32_t custom_cluster_size = 4194304; /* 4MiB */
2537 	uint32_t cluster_sz;
2538 	uint64_t total_clusters;
2539 
2540 	dev = init_dev();
2541 	spdk_bs_opts_init(&opts, sizeof(opts));
2542 	opts.cluster_sz = custom_cluster_size;
2543 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2544 
2545 	/* Initialize a new blob store */
2546 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2547 	poll_threads();
2548 	CU_ASSERT(g_bserrno == 0);
2549 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2550 	bs = g_bs;
2551 	cluster_sz = bs->cluster_sz;
2552 	total_clusters = bs->total_clusters;
2553 
2554 	/* Unload the blob store */
2555 	spdk_bs_unload(bs, bs_op_complete, NULL);
2556 	poll_threads();
2557 	CU_ASSERT(g_bserrno == 0);
2558 	g_bs = NULL;
2559 	g_blob = NULL;
2560 	g_blobid = 0;
2561 
2562 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2563 	CU_ASSERT(super_block->clean == 1);
2564 
2565 	/* Load an existing blob store */
2566 	dev = init_dev();
2567 	spdk_bs_opts_init(&opts, sizeof(opts));
2568 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2569 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2570 	poll_threads();
2571 	CU_ASSERT(g_bserrno == 0);
2572 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2573 	bs = g_bs;
2574 	/* Compare cluster size and number to one after initialization */
2575 	CU_ASSERT(cluster_sz == bs->cluster_sz);
2576 	CU_ASSERT(total_clusters == bs->total_clusters);
2577 
2578 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2579 	CU_ASSERT(super_block->clean == 1);
2580 	CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen);
2581 
2582 	spdk_bs_unload(bs, bs_op_complete, NULL);
2583 	poll_threads();
2584 	CU_ASSERT(g_bserrno == 0);
2585 	CU_ASSERT(super_block->clean == 1);
2586 	g_bs = NULL;
2587 }
2588 
2589 static void
2590 bs_load_after_failed_grow(void)
2591 {
2592 	struct spdk_blob_store *bs;
2593 	struct spdk_bs_dev *dev;
2594 	struct spdk_bs_super_block *super_block;
2595 	struct spdk_bs_opts opts;
2596 	struct spdk_bs_md_mask *mask;
2597 	struct spdk_blob_opts blob_opts;
2598 	struct spdk_blob *blob, *snapshot;
2599 	spdk_blob_id blobid, snapshotid;
2600 	uint64_t total_data_clusters;
2601 
2602 	dev = init_dev();
2603 	spdk_bs_opts_init(&opts, sizeof(opts));
2604 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2605 	/*
2606 	 * The bdev_size is 64M, cluster_sz is 1M, so there are 64 clusters. The
2607 	 * blobstore will create 64 md pages by default. We set num_md_pages to 128,
2608 	 * thus the blobstore could grow to the double size.
2609 	 */
2610 	opts.num_md_pages = 128;
2611 
2612 	/* Initialize a new blob store */
2613 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2614 	poll_threads();
2615 	CU_ASSERT(g_bserrno == 0);
2616 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2617 	bs = g_bs;
2618 
2619 	/* Create blob */
2620 	ut_spdk_blob_opts_init(&blob_opts);
2621 	blob_opts.num_clusters = 10;
2622 
2623 	blob = ut_blob_create_and_open(bs, &blob_opts);
2624 	blobid = spdk_blob_get_id(blob);
2625 
2626 	/* Create snapshot */
2627 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
2628 	poll_threads();
2629 	CU_ASSERT(g_bserrno == 0);
2630 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2631 	snapshotid = g_blobid;
2632 
2633 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
2634 	poll_threads();
2635 	CU_ASSERT(g_bserrno == 0);
2636 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2637 	snapshot = g_blob;
2638 
2639 	spdk_blob_close(snapshot, blob_op_complete, NULL);
2640 	poll_threads();
2641 	CU_ASSERT(g_bserrno == 0);
2642 
2643 	spdk_blob_close(blob, blob_op_complete, NULL);
2644 	poll_threads();
2645 	CU_ASSERT(g_bserrno == 0);
2646 
2647 	total_data_clusters = bs->total_data_clusters;
2648 	CU_ASSERT(bs->num_free_clusters + 10 == total_data_clusters);
2649 
2650 	/* Unload the blob store */
2651 	spdk_bs_unload(bs, bs_op_complete, NULL);
2652 	poll_threads();
2653 	CU_ASSERT(g_bserrno == 0);
2654 	g_bs = NULL;
2655 	g_blob = NULL;
2656 	g_blobid = 0;
2657 
2658 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2659 	CU_ASSERT(super_block->clean == 1);
2660 
2661 	mask = (struct spdk_bs_md_mask *)(g_dev_buffer + super_block->used_cluster_mask_start * 4096);
2662 	CU_ASSERT(mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
2663 	CU_ASSERT(mask->length == super_block->size / super_block->cluster_size);
2664 
2665 	/*
2666 	 * We change the mask->length to emulate this scenario: A spdk_bs_grow failed after it changed
2667 	 * the used_cluster bitmap length, but it didn't change the super block yet.
2668 	 */
2669 	mask->length *= 2;
2670 
2671 	/* Load an existing blob store */
2672 	dev = init_dev();
2673 	dev->blockcnt *= 2;
2674 	spdk_bs_opts_init(&opts, sizeof(opts));
2675 	opts.clear_method = BS_CLEAR_WITH_NONE;
2676 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2677 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2678 	poll_threads();
2679 	CU_ASSERT(g_bserrno == 0);
2680 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2681 	bs = g_bs;
2682 
2683 	/* Check the capacity is the same as before */
2684 	CU_ASSERT(bs->total_data_clusters == total_data_clusters);
2685 	CU_ASSERT(bs->num_free_clusters + 10 == total_data_clusters);
2686 
2687 	/* Check the blob and the snapshot are still available */
2688 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2689 	poll_threads();
2690 	CU_ASSERT(g_bserrno == 0);
2691 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2692 	blob = g_blob;
2693 
2694 	spdk_blob_close(blob, blob_op_complete, NULL);
2695 	poll_threads();
2696 	CU_ASSERT(g_bserrno == 0);
2697 
2698 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
2699 	poll_threads();
2700 	CU_ASSERT(g_bserrno == 0);
2701 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2702 	snapshot = g_blob;
2703 
2704 	spdk_blob_close(snapshot, blob_op_complete, NULL);
2705 	poll_threads();
2706 	CU_ASSERT(g_bserrno == 0);
2707 
2708 	spdk_bs_unload(bs, bs_op_complete, NULL);
2709 	poll_threads();
2710 	CU_ASSERT(g_bserrno == 0);
2711 	CU_ASSERT(super_block->clean == 1);
2712 	g_bs = NULL;
2713 }
2714 
2715 static void
2716 bs_type(void)
2717 {
2718 	struct spdk_blob_store *bs;
2719 	struct spdk_bs_dev *dev;
2720 	struct spdk_bs_opts opts;
2721 
2722 	dev = init_dev();
2723 	spdk_bs_opts_init(&opts, sizeof(opts));
2724 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2725 
2726 	/* Initialize a new blob store */
2727 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2728 	poll_threads();
2729 	CU_ASSERT(g_bserrno == 0);
2730 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2731 	bs = g_bs;
2732 
2733 	/* Unload the blob store */
2734 	spdk_bs_unload(bs, bs_op_complete, NULL);
2735 	poll_threads();
2736 	CU_ASSERT(g_bserrno == 0);
2737 	g_bs = NULL;
2738 	g_blob = NULL;
2739 	g_blobid = 0;
2740 
2741 	/* Load non existing blobstore type */
2742 	dev = init_dev();
2743 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING");
2744 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2745 	poll_threads();
2746 	CU_ASSERT(g_bserrno != 0);
2747 
2748 	/* Load with empty blobstore type */
2749 	dev = init_dev();
2750 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2751 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2752 	poll_threads();
2753 	CU_ASSERT(g_bserrno == 0);
2754 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2755 	bs = g_bs;
2756 
2757 	spdk_bs_unload(bs, bs_op_complete, NULL);
2758 	poll_threads();
2759 	CU_ASSERT(g_bserrno == 0);
2760 	g_bs = NULL;
2761 
2762 	/* Initialize a new blob store with empty bstype */
2763 	dev = init_dev();
2764 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2765 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2766 	poll_threads();
2767 	CU_ASSERT(g_bserrno == 0);
2768 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2769 	bs = g_bs;
2770 
2771 	spdk_bs_unload(bs, bs_op_complete, NULL);
2772 	poll_threads();
2773 	CU_ASSERT(g_bserrno == 0);
2774 	g_bs = NULL;
2775 
2776 	/* Load non existing blobstore type */
2777 	dev = init_dev();
2778 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING");
2779 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2780 	poll_threads();
2781 	CU_ASSERT(g_bserrno != 0);
2782 
2783 	/* Load with empty blobstore type */
2784 	dev = init_dev();
2785 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2786 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2787 	poll_threads();
2788 	CU_ASSERT(g_bserrno == 0);
2789 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2790 	bs = g_bs;
2791 
2792 	spdk_bs_unload(bs, bs_op_complete, NULL);
2793 	poll_threads();
2794 	CU_ASSERT(g_bserrno == 0);
2795 	g_bs = NULL;
2796 }
2797 
2798 static void
2799 bs_super_block(void)
2800 {
2801 	struct spdk_blob_store *bs;
2802 	struct spdk_bs_dev *dev;
2803 	struct spdk_bs_super_block *super_block;
2804 	struct spdk_bs_opts opts;
2805 	struct spdk_bs_super_block_ver1 super_block_v1;
2806 
2807 	dev = init_dev();
2808 	spdk_bs_opts_init(&opts, sizeof(opts));
2809 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2810 
2811 	/* Initialize a new blob store */
2812 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2813 	poll_threads();
2814 	CU_ASSERT(g_bserrno == 0);
2815 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2816 	bs = g_bs;
2817 
2818 	/* Unload the blob store */
2819 	spdk_bs_unload(bs, bs_op_complete, NULL);
2820 	poll_threads();
2821 	CU_ASSERT(g_bserrno == 0);
2822 	g_bs = NULL;
2823 	g_blob = NULL;
2824 	g_blobid = 0;
2825 
2826 	/* Load an existing blob store with version newer than supported */
2827 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2828 	super_block->version++;
2829 
2830 	dev = init_dev();
2831 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2832 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2833 	poll_threads();
2834 	CU_ASSERT(g_bserrno != 0);
2835 
2836 	/* Create a new blob store with super block version 1 */
2837 	dev = init_dev();
2838 	super_block_v1.version = 1;
2839 	memcpy(super_block_v1.signature, "SPDKBLOB", sizeof(super_block_v1.signature));
2840 	super_block_v1.length = 0x1000;
2841 	super_block_v1.clean = 1;
2842 	super_block_v1.super_blob = 0xFFFFFFFFFFFFFFFF;
2843 	super_block_v1.cluster_size = 0x100000;
2844 	super_block_v1.used_page_mask_start = 0x01;
2845 	super_block_v1.used_page_mask_len = 0x01;
2846 	super_block_v1.used_cluster_mask_start = 0x02;
2847 	super_block_v1.used_cluster_mask_len = 0x01;
2848 	super_block_v1.md_start = 0x03;
2849 	super_block_v1.md_len = 0x40;
2850 	memset(super_block_v1.reserved, 0, 4036);
2851 	super_block_v1.crc = blob_md_page_calc_crc(&super_block_v1);
2852 	memcpy(g_dev_buffer, &super_block_v1, sizeof(struct spdk_bs_super_block_ver1));
2853 
2854 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2855 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2856 	poll_threads();
2857 	CU_ASSERT(g_bserrno == 0);
2858 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2859 	bs = g_bs;
2860 
2861 	spdk_bs_unload(bs, bs_op_complete, NULL);
2862 	poll_threads();
2863 	CU_ASSERT(g_bserrno == 0);
2864 	g_bs = NULL;
2865 }
2866 
2867 static void
2868 bs_test_recover_cluster_count(void)
2869 {
2870 	struct spdk_blob_store *bs;
2871 	struct spdk_bs_dev *dev;
2872 	struct spdk_bs_super_block super_block;
2873 	struct spdk_bs_opts opts;
2874 
2875 	dev = init_dev();
2876 	spdk_bs_opts_init(&opts, sizeof(opts));
2877 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2878 
2879 	super_block.version = 3;
2880 	memcpy(super_block.signature, "SPDKBLOB", sizeof(super_block.signature));
2881 	super_block.length = 0x1000;
2882 	super_block.clean = 0;
2883 	super_block.super_blob = 0xFFFFFFFFFFFFFFFF;
2884 	super_block.cluster_size = 4096;
2885 	super_block.used_page_mask_start = 0x01;
2886 	super_block.used_page_mask_len = 0x01;
2887 	super_block.used_cluster_mask_start = 0x02;
2888 	super_block.used_cluster_mask_len = 0x01;
2889 	super_block.used_blobid_mask_start = 0x03;
2890 	super_block.used_blobid_mask_len = 0x01;
2891 	super_block.md_start = 0x04;
2892 	super_block.md_len = 0x40;
2893 	memset(super_block.bstype.bstype, 0, sizeof(super_block.bstype.bstype));
2894 	super_block.size = dev->blockcnt * dev->blocklen;
2895 	super_block.io_unit_size = 0x1000;
2896 	memset(super_block.reserved, 0, 4000);
2897 	super_block.crc = blob_md_page_calc_crc(&super_block);
2898 	memcpy(g_dev_buffer, &super_block, sizeof(struct spdk_bs_super_block));
2899 
2900 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2901 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2902 	poll_threads();
2903 	CU_ASSERT(g_bserrno == 0);
2904 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2905 	bs = g_bs;
2906 	CU_ASSERT(bs->num_free_clusters == bs->total_clusters - (super_block.md_start +
2907 			super_block.md_len));
2908 
2909 	spdk_bs_unload(bs, bs_op_complete, NULL);
2910 	poll_threads();
2911 	CU_ASSERT(g_bserrno == 0);
2912 	g_bs = NULL;
2913 }
2914 
2915 static void
2916 bs_grow_live_size(uint64_t new_blockcnt)
2917 {
2918 	struct spdk_blob_store *bs;
2919 	struct spdk_bs_dev *dev;
2920 	struct spdk_bs_super_block super_block;
2921 	struct spdk_bs_opts opts;
2922 	struct spdk_bs_md_mask mask;
2923 	uint64_t bdev_size;
2924 	uint64_t total_data_clusters;
2925 
2926 	/*
2927 	 * Further down the test the dev size will be larger than the g_dev_buffer size,
2928 	 * so we set clear_method to NONE, or the blobstore will try to clear the dev and
2929 	 * will write beyond the end of g_dev_buffer.
2930 	 */
2931 	dev = init_dev();
2932 	spdk_bs_opts_init(&opts, sizeof(opts));
2933 	opts.clear_method = BS_CLEAR_WITH_NONE;
2934 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2935 	poll_threads();
2936 	CU_ASSERT(g_bserrno == 0);
2937 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2938 	bs = g_bs;
2939 	CU_ASSERT(spdk_bs_total_data_cluster_count(bs) == 63);
2940 
2941 	/*
2942 	 * Set the dev size according to the new_blockcnt,
2943 	 * then the blobstore will adjust the metadata according to the new size.
2944 	 */
2945 	dev->blockcnt = new_blockcnt;
2946 	bdev_size = dev->blockcnt * dev->blocklen;
2947 	spdk_bs_grow_live(bs, bs_op_complete, NULL);
2948 	poll_threads();
2949 	CU_ASSERT(g_bserrno == 0);
2950 	total_data_clusters = spdk_bs_total_data_cluster_count(bs);
2951 	/* One cluster of 1MiB size is used for metadata */
2952 	CU_ASSERT(total_data_clusters == (bdev_size / (1 * 1024 * 1024)) - 1);
2953 
2954 	/* Make sure the super block is updated. */
2955 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
2956 	CU_ASSERT(super_block.size == bdev_size);
2957 	CU_ASSERT(super_block.clean == 0);
2958 	/* The used_cluster mask is not written out until first spdk_bs_unload. */
2959 	memcpy(&mask, g_dev_buffer + super_block.used_cluster_mask_start * 4096,
2960 	       sizeof(struct spdk_bs_md_mask));
2961 	CU_ASSERT(mask.type == 0);
2962 	CU_ASSERT(mask.length == 0);
2963 
2964 	spdk_bs_unload(bs, bs_op_complete, NULL);
2965 	poll_threads();
2966 	CU_ASSERT(g_bserrno == 0);
2967 	g_bs = NULL;
2968 
2969 	/* Make sure all metadata is correct, super block and used_cluster mask. */
2970 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
2971 	CU_ASSERT(super_block.size == bdev_size);
2972 	CU_ASSERT(super_block.clean == 1);
2973 	memcpy(&mask, g_dev_buffer + super_block.used_cluster_mask_start * 4096,
2974 	       sizeof(struct spdk_bs_md_mask));
2975 	CU_ASSERT(mask.type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
2976 	CU_ASSERT(mask.length == bdev_size / (1 * 1024 * 1024));
2977 
2978 	/* Load blobstore and check the cluster counts again. */
2979 	dev = init_dev();
2980 	dev->blockcnt = new_blockcnt;
2981 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
2982 	poll_threads();
2983 	CU_ASSERT(g_bserrno == 0);
2984 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2985 	CU_ASSERT(super_block.clean == 1);
2986 	bs = g_bs;
2987 	CU_ASSERT(total_data_clusters == spdk_bs_total_data_cluster_count(bs));
2988 
2989 	/* Perform grow without change in size, expected pass. */
2990 	spdk_bs_grow_live(bs, bs_op_complete, NULL);
2991 	poll_threads();
2992 	CU_ASSERT(g_bserrno == 0);
2993 	CU_ASSERT(total_data_clusters == spdk_bs_total_data_cluster_count(bs));
2994 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
2995 	CU_ASSERT(super_block.size == bdev_size);
2996 	CU_ASSERT(super_block.clean == 1);
2997 
2998 	spdk_bs_unload(bs, bs_op_complete, NULL);
2999 	poll_threads();
3000 	CU_ASSERT(g_bserrno == 0);
3001 	g_bs = NULL;
3002 }
3003 
3004 static void
3005 bs_grow_live(void)
3006 {
3007 	/* No change expected */
3008 	bs_grow_live_size(DEV_BUFFER_BLOCKCNT);
3009 
3010 	/* Size slightly increased, but not enough to increase cluster count */
3011 	bs_grow_live_size(DEV_BUFFER_BLOCKCNT + 1);
3012 
3013 	/* Size doubled, increasing the cluster count */
3014 	bs_grow_live_size(DEV_BUFFER_BLOCKCNT * 2);
3015 }
3016 
3017 static void
3018 bs_grow_live_no_space(void)
3019 {
3020 	struct spdk_blob_store *bs;
3021 	struct spdk_bs_dev *dev;
3022 	struct spdk_bs_super_block super_block;
3023 	struct spdk_bs_opts opts;
3024 	struct spdk_bs_md_mask mask;
3025 	uint64_t bdev_size_init;
3026 	uint64_t total_data_clusters, max_clusters;
3027 
3028 	/*
3029 	 * Further down the test the dev size will be larger than the g_dev_buffer size,
3030 	 * so we set clear_method to NONE, or the blobstore will try to clear the dev and
3031 	 * will write beyond the end of g_dev_buffer.
3032 	 */
3033 	dev = init_dev();
3034 	bdev_size_init = dev->blockcnt * dev->blocklen;
3035 	spdk_bs_opts_init(&opts, sizeof(opts));
3036 	opts.clear_method = BS_CLEAR_WITH_NONE;
3037 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3038 	poll_threads();
3039 	CU_ASSERT(g_bserrno == 0);
3040 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3041 	bs = g_bs;
3042 	total_data_clusters = spdk_bs_total_data_cluster_count(bs);
3043 	CU_ASSERT(total_data_clusters == 63);
3044 
3045 	/*
3046 	 * The default dev size is 64M, here we set the dev size to 32M,
3047 	 * expecting EILSEQ due to super_block validation and no change in blobstore.
3048 	 */
3049 	dev->blockcnt = (32L * 1024L * 1024L) / dev->blocklen;
3050 	spdk_bs_grow_live(bs, bs_op_complete, NULL);
3051 	poll_threads();
3052 	/* This error code comes from bs_super_validate() */
3053 	CU_ASSERT(g_bserrno == -EILSEQ);
3054 	CU_ASSERT(total_data_clusters == spdk_bs_total_data_cluster_count(bs));
3055 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
3056 	CU_ASSERT(super_block.size == bdev_size_init);
3057 
3058 	/*
3059 	 * Blobstore in this test has only space for single md_page for used_clusters,
3060 	 * which fits 1 bit per cluster minus the md header.
3061 	 *
3062 	 * Dev size is increased to exceed the reserved space for the used_cluster_mask
3063 	 * in the metadata, expecting ENOSPC and no change in blobstore.
3064 	 */
3065 	max_clusters = (spdk_bs_get_page_size(bs) - sizeof(struct spdk_bs_md_mask)) * 8;
3066 	max_clusters += 1;
3067 	dev->blockcnt = (max_clusters * spdk_bs_get_cluster_size(bs)) / dev->blocklen;
3068 	spdk_bs_grow_live(bs, bs_op_complete, NULL);
3069 	poll_threads();
3070 	CU_ASSERT(g_bserrno == -ENOSPC);
3071 	CU_ASSERT(total_data_clusters == spdk_bs_total_data_cluster_count(bs));
3072 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
3073 	CU_ASSERT(super_block.size == bdev_size_init);
3074 
3075 	/*
3076 	 * No change should have occurred for the duration of the test,
3077 	 * unload blobstore and check metadata.
3078 	 */
3079 	spdk_bs_unload(bs, bs_op_complete, NULL);
3080 	poll_threads();
3081 	CU_ASSERT(g_bserrno == 0);
3082 	g_bs = NULL;
3083 
3084 	/* Make sure all metadata is correct, super block and used_cluster mask. */
3085 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
3086 	CU_ASSERT(super_block.size == bdev_size_init);
3087 	CU_ASSERT(super_block.clean == 1);
3088 	memcpy(&mask, g_dev_buffer + super_block.used_cluster_mask_start * 4096,
3089 	       sizeof(struct spdk_bs_md_mask));
3090 	CU_ASSERT(mask.type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
3091 	CU_ASSERT(mask.length == bdev_size_init / (1 * 1024 * 1024));
3092 
3093 	/* Load blobstore and check the cluster counts again. */
3094 	dev = init_dev();
3095 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3096 	poll_threads();
3097 	CU_ASSERT(g_bserrno == 0);
3098 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3099 	bs = g_bs;
3100 	CU_ASSERT(total_data_clusters == spdk_bs_total_data_cluster_count(bs));
3101 
3102 	spdk_bs_unload(bs, bs_op_complete, NULL);
3103 	poll_threads();
3104 	CU_ASSERT(g_bserrno == 0);
3105 	g_bs = NULL;
3106 }
3107 
3108 static void
3109 bs_test_grow(void)
3110 {
3111 	struct spdk_blob_store *bs;
3112 	struct spdk_bs_dev *dev;
3113 	struct spdk_bs_super_block super_block;
3114 	struct spdk_bs_opts opts;
3115 	struct spdk_bs_md_mask mask;
3116 	uint64_t bdev_size;
3117 
3118 	dev = init_dev();
3119 	bdev_size = dev->blockcnt * dev->blocklen;
3120 	spdk_bs_opts_init(&opts, sizeof(opts));
3121 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3122 	poll_threads();
3123 	CU_ASSERT(g_bserrno == 0);
3124 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3125 	bs = g_bs;
3126 
3127 	spdk_bs_unload(bs, bs_op_complete, NULL);
3128 	poll_threads();
3129 	CU_ASSERT(g_bserrno == 0);
3130 	g_bs = NULL;
3131 
3132 	/*
3133 	 * To make sure all the metadata are updated to the disk,
3134 	 * we check the g_dev_buffer after spdk_bs_unload.
3135 	 */
3136 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
3137 	CU_ASSERT(super_block.size == bdev_size);
3138 
3139 	/*
3140 	 * Make sure the used_cluster mask is correct.
3141 	 */
3142 	memcpy(&mask, g_dev_buffer + super_block.used_cluster_mask_start * 4096,
3143 	       sizeof(struct spdk_bs_md_mask));
3144 	CU_ASSERT(mask.type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
3145 	CU_ASSERT(mask.length == bdev_size / (1 * 1024 * 1024));
3146 
3147 	/*
3148 	 * The default dev size is 64M, here we set the dev size to 128M,
3149 	 * then the blobstore will adjust the metadata according to the new size.
3150 	 * The dev size is larger than the g_dev_buffer size, so we set clear_method
3151 	 * to NONE, or the blobstore will try to clear the dev and will write beyond
3152 	 * the end of g_dev_buffer.
3153 	 */
3154 	dev = init_dev();
3155 	dev->blockcnt = (128L * 1024L * 1024L) / dev->blocklen;
3156 	bdev_size = dev->blockcnt * dev->blocklen;
3157 	spdk_bs_opts_init(&opts, sizeof(opts));
3158 	opts.clear_method = BS_CLEAR_WITH_NONE;
3159 	spdk_bs_grow(dev, &opts, bs_op_with_handle_complete, NULL);
3160 	poll_threads();
3161 	CU_ASSERT(g_bserrno == 0);
3162 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3163 	bs = g_bs;
3164 
3165 	/*
3166 	 * After spdk_bs_grow, all metadata are updated to the disk.
3167 	 * So we can check g_dev_buffer now.
3168 	 */
3169 	memcpy(&super_block, g_dev_buffer, sizeof(struct spdk_bs_super_block));
3170 	CU_ASSERT(super_block.size == bdev_size);
3171 
3172 	/*
3173 	 * Make sure the used_cluster mask has been updated according to the bdev size
3174 	 */
3175 	memcpy(&mask, g_dev_buffer + super_block.used_cluster_mask_start * 4096,
3176 	       sizeof(struct spdk_bs_md_mask));
3177 	CU_ASSERT(mask.type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
3178 	CU_ASSERT(mask.length == bdev_size / (1 * 1024 * 1024));
3179 
3180 	spdk_bs_unload(bs, bs_op_complete, NULL);
3181 	poll_threads();
3182 	CU_ASSERT(g_bserrno == 0);
3183 	g_bs = NULL;
3184 }
3185 
3186 /*
3187  * Create a blobstore and then unload it.
3188  */
3189 static void
3190 bs_unload(void)
3191 {
3192 	struct spdk_blob_store *bs = g_bs;
3193 	struct spdk_blob *blob;
3194 
3195 	/* Create a blob and open it. */
3196 	blob = ut_blob_create_and_open(bs, NULL);
3197 
3198 	/* Try to unload blobstore, should fail with open blob */
3199 	g_bserrno = -1;
3200 	spdk_bs_unload(bs, bs_op_complete, NULL);
3201 	poll_threads();
3202 	CU_ASSERT(g_bserrno == -EBUSY);
3203 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3204 
3205 	/* Close the blob, then successfully unload blobstore */
3206 	g_bserrno = -1;
3207 	spdk_blob_close(blob, blob_op_complete, NULL);
3208 	poll_threads();
3209 	CU_ASSERT(g_bserrno == 0);
3210 }
3211 
3212 /*
3213  * Create a blobstore with a cluster size different than the default, and ensure it is
3214  *  persisted.
3215  */
3216 static void
3217 bs_cluster_sz(void)
3218 {
3219 	struct spdk_blob_store *bs;
3220 	struct spdk_bs_dev *dev;
3221 	struct spdk_bs_opts opts;
3222 	uint32_t cluster_sz;
3223 
3224 	/* Set cluster size to zero */
3225 	dev = init_dev();
3226 	spdk_bs_opts_init(&opts, sizeof(opts));
3227 	opts.cluster_sz = 0;
3228 
3229 	/* Initialize a new blob store */
3230 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3231 	poll_threads();
3232 	CU_ASSERT(g_bserrno == -EINVAL);
3233 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
3234 
3235 	/*
3236 	 * Set cluster size to blobstore page size,
3237 	 * to work it is required to be at least twice the blobstore page size.
3238 	 */
3239 	dev = init_dev();
3240 	spdk_bs_opts_init(&opts, sizeof(opts));
3241 	opts.cluster_sz = SPDK_BS_PAGE_SIZE;
3242 
3243 	/* Initialize a new blob store */
3244 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3245 	poll_threads();
3246 	CU_ASSERT(g_bserrno == -ENOMEM);
3247 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
3248 
3249 	/*
3250 	 * Set cluster size to lower than page size,
3251 	 * to work it is required to be at least twice the blobstore page size.
3252 	 */
3253 	dev = init_dev();
3254 	spdk_bs_opts_init(&opts, sizeof(opts));
3255 	opts.cluster_sz = SPDK_BS_PAGE_SIZE - 1;
3256 
3257 	/* Initialize a new blob store */
3258 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3259 	poll_threads();
3260 	CU_ASSERT(g_bserrno == -EINVAL);
3261 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
3262 
3263 	/* Set cluster size to twice the default */
3264 	dev = init_dev();
3265 	spdk_bs_opts_init(&opts, sizeof(opts));
3266 	opts.cluster_sz *= 2;
3267 	cluster_sz = opts.cluster_sz;
3268 
3269 	/* Initialize a new blob store */
3270 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3271 	poll_threads();
3272 	CU_ASSERT(g_bserrno == 0);
3273 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3274 	bs = g_bs;
3275 
3276 	CU_ASSERT(spdk_bs_get_cluster_size(bs) == cluster_sz);
3277 
3278 	ut_bs_reload(&bs, &opts);
3279 
3280 	CU_ASSERT(spdk_bs_get_cluster_size(bs) == cluster_sz);
3281 
3282 	spdk_bs_unload(bs, bs_op_complete, NULL);
3283 	poll_threads();
3284 	CU_ASSERT(g_bserrno == 0);
3285 	g_bs = NULL;
3286 }
3287 
3288 /*
3289  * Create a blobstore, reload it and ensure total usable cluster count
3290  *  stays the same.
3291  */
3292 static void
3293 bs_usable_clusters(void)
3294 {
3295 	struct spdk_blob_store *bs = g_bs;
3296 	struct spdk_blob *blob;
3297 	uint32_t clusters;
3298 	int i;
3299 
3300 
3301 	clusters = spdk_bs_total_data_cluster_count(bs);
3302 
3303 	ut_bs_reload(&bs, NULL);
3304 
3305 	CU_ASSERT(spdk_bs_total_data_cluster_count(bs) == clusters);
3306 
3307 	/* Create and resize blobs to make sure that useable cluster count won't change */
3308 	for (i = 0; i < 4; i++) {
3309 		g_bserrno = -1;
3310 		g_blobid = SPDK_BLOBID_INVALID;
3311 		blob = ut_blob_create_and_open(bs, NULL);
3312 
3313 		spdk_blob_resize(blob, 10, blob_op_complete, NULL);
3314 		poll_threads();
3315 		CU_ASSERT(g_bserrno == 0);
3316 
3317 		g_bserrno = -1;
3318 		spdk_blob_close(blob, blob_op_complete, NULL);
3319 		poll_threads();
3320 		CU_ASSERT(g_bserrno == 0);
3321 
3322 		CU_ASSERT(spdk_bs_total_data_cluster_count(bs) == clusters);
3323 	}
3324 
3325 	/* Reload the blob store to make sure that nothing changed */
3326 	ut_bs_reload(&bs, NULL);
3327 
3328 	CU_ASSERT(spdk_bs_total_data_cluster_count(bs) == clusters);
3329 }
3330 
3331 /*
3332  * Test resizing of the metadata blob.  This requires creating enough blobs
3333  *  so that one cluster is not enough to fit the metadata for those blobs.
3334  *  To induce this condition to happen more quickly, we reduce the cluster
3335  *  size to 16KB, which means only 4 4KB blob metadata pages can fit.
3336  */
3337 static void
3338 bs_resize_md(void)
3339 {
3340 	struct spdk_blob_store *bs;
3341 	const int CLUSTER_PAGE_COUNT = 4;
3342 	const int NUM_BLOBS = CLUSTER_PAGE_COUNT * 4;
3343 	struct spdk_bs_dev *dev;
3344 	struct spdk_bs_opts opts;
3345 	struct spdk_blob *blob;
3346 	struct spdk_blob_opts blob_opts;
3347 	uint32_t cluster_sz;
3348 	spdk_blob_id blobids[NUM_BLOBS];
3349 	int i;
3350 
3351 
3352 	dev = init_dev();
3353 	spdk_bs_opts_init(&opts, sizeof(opts));
3354 	opts.cluster_sz = CLUSTER_PAGE_COUNT * 4096;
3355 	cluster_sz = opts.cluster_sz;
3356 
3357 	/* Initialize a new blob store */
3358 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3359 	poll_threads();
3360 	CU_ASSERT(g_bserrno == 0);
3361 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3362 	bs = g_bs;
3363 
3364 	CU_ASSERT(spdk_bs_get_cluster_size(bs) == cluster_sz);
3365 
3366 	ut_spdk_blob_opts_init(&blob_opts);
3367 
3368 	for (i = 0; i < NUM_BLOBS; i++) {
3369 		g_bserrno = -1;
3370 		g_blobid = SPDK_BLOBID_INVALID;
3371 		spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
3372 		poll_threads();
3373 		CU_ASSERT(g_bserrno == 0);
3374 		CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
3375 		blobids[i] = g_blobid;
3376 	}
3377 
3378 	ut_bs_reload(&bs, &opts);
3379 
3380 	CU_ASSERT(spdk_bs_get_cluster_size(bs) == cluster_sz);
3381 
3382 	for (i = 0; i < NUM_BLOBS; i++) {
3383 		g_bserrno = -1;
3384 		g_blob = NULL;
3385 		spdk_bs_open_blob(bs, blobids[i], blob_op_with_handle_complete, NULL);
3386 		poll_threads();
3387 		CU_ASSERT(g_bserrno == 0);
3388 		CU_ASSERT(g_blob !=  NULL);
3389 		blob = g_blob;
3390 		g_bserrno = -1;
3391 		spdk_blob_close(blob, blob_op_complete, NULL);
3392 		poll_threads();
3393 		CU_ASSERT(g_bserrno == 0);
3394 	}
3395 
3396 	spdk_bs_unload(bs, bs_op_complete, NULL);
3397 	poll_threads();
3398 	CU_ASSERT(g_bserrno == 0);
3399 	g_bs = NULL;
3400 }
3401 
3402 static void
3403 bs_destroy(void)
3404 {
3405 	struct spdk_blob_store *bs;
3406 	struct spdk_bs_dev *dev;
3407 
3408 	/* Initialize a new blob store */
3409 	dev = init_dev();
3410 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3411 	poll_threads();
3412 	CU_ASSERT(g_bserrno == 0);
3413 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3414 	bs = g_bs;
3415 
3416 	/* Destroy the blob store */
3417 	g_bserrno = -1;
3418 	spdk_bs_destroy(bs, bs_op_complete, NULL);
3419 	poll_threads();
3420 	CU_ASSERT(g_bserrno == 0);
3421 
3422 	/* Loading an non-existent blob store should fail. */
3423 	g_bs = NULL;
3424 	dev = init_dev();
3425 
3426 	g_bserrno = 0;
3427 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3428 	poll_threads();
3429 	CU_ASSERT(g_bserrno != 0);
3430 }
3431 
3432 /* Try to hit all of the corner cases associated with serializing
3433  * a blob to disk
3434  */
3435 static void
3436 blob_serialize_test(void)
3437 {
3438 	struct spdk_bs_dev *dev;
3439 	struct spdk_bs_opts opts;
3440 	struct spdk_blob_store *bs;
3441 	spdk_blob_id blobid[2];
3442 	struct spdk_blob *blob[2];
3443 	uint64_t i;
3444 	char *value;
3445 	int rc;
3446 
3447 	dev = init_dev();
3448 
3449 	/* Initialize a new blobstore with very small clusters */
3450 	spdk_bs_opts_init(&opts, sizeof(opts));
3451 	opts.cluster_sz = dev->blocklen * 8;
3452 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3453 	poll_threads();
3454 	CU_ASSERT(g_bserrno == 0);
3455 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3456 	bs = g_bs;
3457 
3458 	/* Create and open two blobs */
3459 	for (i = 0; i < 2; i++) {
3460 		blob[i] = ut_blob_create_and_open(bs, NULL);
3461 		blobid[i] = spdk_blob_get_id(blob[i]);
3462 
3463 		/* Set a fairly large xattr on both blobs to eat up
3464 		 * metadata space
3465 		 */
3466 		value = calloc(dev->blocklen - 64, sizeof(char));
3467 		SPDK_CU_ASSERT_FATAL(value != NULL);
3468 		memset(value, i, dev->blocklen / 2);
3469 		rc = spdk_blob_set_xattr(blob[i], "name", value, dev->blocklen - 64);
3470 		CU_ASSERT(rc == 0);
3471 		free(value);
3472 	}
3473 
3474 	/* Resize the blobs, alternating 1 cluster at a time.
3475 	 * This thwarts run length encoding and will cause spill
3476 	 * over of the extents.
3477 	 */
3478 	for (i = 0; i < 6; i++) {
3479 		spdk_blob_resize(blob[i % 2], (i / 2) + 1, blob_op_complete, NULL);
3480 		poll_threads();
3481 		CU_ASSERT(g_bserrno == 0);
3482 	}
3483 
3484 	for (i = 0; i < 2; i++) {
3485 		spdk_blob_sync_md(blob[i], blob_op_complete, NULL);
3486 		poll_threads();
3487 		CU_ASSERT(g_bserrno == 0);
3488 	}
3489 
3490 	/* Close the blobs */
3491 	for (i = 0; i < 2; i++) {
3492 		spdk_blob_close(blob[i], blob_op_complete, NULL);
3493 		poll_threads();
3494 		CU_ASSERT(g_bserrno == 0);
3495 	}
3496 
3497 	ut_bs_reload(&bs, &opts);
3498 
3499 	for (i = 0; i < 2; i++) {
3500 		blob[i] = NULL;
3501 
3502 		spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL);
3503 		poll_threads();
3504 		CU_ASSERT(g_bserrno == 0);
3505 		CU_ASSERT(g_blob != NULL);
3506 		blob[i] = g_blob;
3507 
3508 		CU_ASSERT(spdk_blob_get_num_clusters(blob[i]) == 3);
3509 
3510 		spdk_blob_close(blob[i], blob_op_complete, NULL);
3511 		poll_threads();
3512 		CU_ASSERT(g_bserrno == 0);
3513 	}
3514 
3515 	spdk_bs_unload(bs, bs_op_complete, NULL);
3516 	poll_threads();
3517 	CU_ASSERT(g_bserrno == 0);
3518 	g_bs = NULL;
3519 }
3520 
3521 static void
3522 blob_crc(void)
3523 {
3524 	struct spdk_blob_store *bs = g_bs;
3525 	struct spdk_blob *blob;
3526 	spdk_blob_id blobid;
3527 	uint32_t page_num;
3528 	int index;
3529 	struct spdk_blob_md_page *page;
3530 
3531 	blob = ut_blob_create_and_open(bs, NULL);
3532 	blobid = spdk_blob_get_id(blob);
3533 
3534 	spdk_blob_close(blob, blob_op_complete, NULL);
3535 	poll_threads();
3536 	CU_ASSERT(g_bserrno == 0);
3537 
3538 	page_num = bs_blobid_to_page(blobid);
3539 	index = DEV_BUFFER_BLOCKLEN * (bs->md_start + page_num);
3540 	page = (struct spdk_blob_md_page *)&g_dev_buffer[index];
3541 	page->crc = 0;
3542 
3543 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3544 	poll_threads();
3545 	CU_ASSERT(g_bserrno == -EINVAL);
3546 	CU_ASSERT(g_blob == NULL);
3547 	g_bserrno = 0;
3548 
3549 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3550 	poll_threads();
3551 	CU_ASSERT(g_bserrno == -EINVAL);
3552 }
3553 
3554 static void
3555 super_block_crc(void)
3556 {
3557 	struct spdk_blob_store *bs;
3558 	struct spdk_bs_dev *dev;
3559 	struct spdk_bs_super_block *super_block;
3560 
3561 	dev = init_dev();
3562 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3563 	poll_threads();
3564 	CU_ASSERT(g_bserrno == 0);
3565 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3566 	bs = g_bs;
3567 
3568 	spdk_bs_unload(bs, bs_op_complete, NULL);
3569 	poll_threads();
3570 	CU_ASSERT(g_bserrno == 0);
3571 	g_bs = NULL;
3572 
3573 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
3574 	super_block->crc = 0;
3575 	dev = init_dev();
3576 
3577 	/* Load an existing blob store */
3578 	g_bserrno = 0;
3579 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3580 	poll_threads();
3581 	CU_ASSERT(g_bserrno == -EILSEQ);
3582 }
3583 
3584 /* For blob dirty shutdown test case we do the following sub-test cases:
3585  * 1 Initialize new blob store and create 1 super blob with some xattrs, then we
3586  *   dirty shutdown and reload the blob store and verify the xattrs.
3587  * 2 Resize the blob from 10 clusters to 20 clusters and then dirty shutdown,
3588  *   reload the blob store and verify the clusters number.
3589  * 3 Create the second blob and then dirty shutdown, reload the blob store
3590  *   and verify the second blob.
3591  * 4 Delete the second blob and then dirty shutdown, reload the blob store
3592  *   and verify the second blob is invalid.
3593  * 5 Create the second blob again and also create the third blob, modify the
3594  *   md of second blob which makes the md invalid, and then dirty shutdown,
3595  *   reload the blob store verify the second blob, it should invalid and also
3596  *   verify the third blob, it should correct.
3597  */
3598 static void
3599 blob_dirty_shutdown(void)
3600 {
3601 	int rc;
3602 	int index;
3603 	struct spdk_blob_store *bs = g_bs;
3604 	spdk_blob_id blobid1, blobid2, blobid3;
3605 	struct spdk_blob *blob = g_blob;
3606 	uint64_t length;
3607 	uint64_t free_clusters;
3608 	const void *value;
3609 	size_t value_len;
3610 	uint32_t page_num;
3611 	struct spdk_blob_md_page *page;
3612 	struct spdk_blob_opts blob_opts;
3613 
3614 	/* Create first blob */
3615 	blobid1 = spdk_blob_get_id(blob);
3616 
3617 	/* Set some xattrs */
3618 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
3619 	CU_ASSERT(rc == 0);
3620 
3621 	length = 2345;
3622 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3623 	CU_ASSERT(rc == 0);
3624 
3625 	/* Put xattr that fits exactly single page.
3626 	 * This results in adding additional pages to MD.
3627 	 * First is flags and smaller xattr, second the large xattr,
3628 	 * third are just the extents.
3629 	 */
3630 	size_t xattr_length = 4072 - sizeof(struct spdk_blob_md_descriptor_xattr) -
3631 			      strlen("large_xattr");
3632 	char *xattr = calloc(xattr_length, sizeof(char));
3633 	SPDK_CU_ASSERT_FATAL(xattr != NULL);
3634 	rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
3635 	free(xattr);
3636 	SPDK_CU_ASSERT_FATAL(rc == 0);
3637 
3638 	/* Resize the blob */
3639 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
3640 	poll_threads();
3641 	CU_ASSERT(g_bserrno == 0);
3642 
3643 	/* Set the blob as the super blob */
3644 	spdk_bs_set_super(bs, blobid1, blob_op_complete, NULL);
3645 	poll_threads();
3646 	CU_ASSERT(g_bserrno == 0);
3647 
3648 	free_clusters = spdk_bs_free_cluster_count(bs);
3649 
3650 	spdk_blob_close(blob, blob_op_complete, NULL);
3651 	poll_threads();
3652 	CU_ASSERT(g_bserrno == 0);
3653 	blob = NULL;
3654 	g_blob = NULL;
3655 	g_blobid = SPDK_BLOBID_INVALID;
3656 
3657 	ut_bs_dirty_load(&bs, NULL);
3658 
3659 	/* Get the super blob */
3660 	spdk_bs_get_super(bs, blob_op_with_id_complete, NULL);
3661 	poll_threads();
3662 	CU_ASSERT(g_bserrno == 0);
3663 	CU_ASSERT(blobid1 == g_blobid);
3664 
3665 	spdk_bs_open_blob(bs, blobid1, blob_op_with_handle_complete, NULL);
3666 	poll_threads();
3667 	CU_ASSERT(g_bserrno == 0);
3668 	CU_ASSERT(g_blob != NULL);
3669 	blob = g_blob;
3670 
3671 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3672 
3673 	/* Get the xattrs */
3674 	value = NULL;
3675 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
3676 	CU_ASSERT(rc == 0);
3677 	SPDK_CU_ASSERT_FATAL(value != NULL);
3678 	CU_ASSERT(*(uint64_t *)value == length);
3679 	CU_ASSERT(value_len == 8);
3680 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
3681 
3682 	/* Resize the blob */
3683 	spdk_blob_resize(blob, 20, blob_op_complete, NULL);
3684 	poll_threads();
3685 	CU_ASSERT(g_bserrno == 0);
3686 
3687 	free_clusters = spdk_bs_free_cluster_count(bs);
3688 
3689 	spdk_blob_close(blob, blob_op_complete, NULL);
3690 	poll_threads();
3691 	CU_ASSERT(g_bserrno == 0);
3692 	blob = NULL;
3693 	g_blob = NULL;
3694 	g_blobid = SPDK_BLOBID_INVALID;
3695 
3696 	ut_bs_dirty_load(&bs, NULL);
3697 
3698 	spdk_bs_open_blob(bs, blobid1, blob_op_with_handle_complete, NULL);
3699 	poll_threads();
3700 	CU_ASSERT(g_bserrno == 0);
3701 	CU_ASSERT(g_blob != NULL);
3702 	blob = g_blob;
3703 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 20);
3704 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3705 
3706 	spdk_blob_close(blob, blob_op_complete, NULL);
3707 	poll_threads();
3708 	CU_ASSERT(g_bserrno == 0);
3709 	blob = NULL;
3710 	g_blob = NULL;
3711 	g_blobid = SPDK_BLOBID_INVALID;
3712 
3713 	/* Create second blob */
3714 	blob = ut_blob_create_and_open(bs, NULL);
3715 	blobid2 = spdk_blob_get_id(blob);
3716 
3717 	/* Set some xattrs */
3718 	rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1);
3719 	CU_ASSERT(rc == 0);
3720 
3721 	length = 5432;
3722 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3723 	CU_ASSERT(rc == 0);
3724 
3725 	/* Resize the blob */
3726 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
3727 	poll_threads();
3728 	CU_ASSERT(g_bserrno == 0);
3729 
3730 	free_clusters = spdk_bs_free_cluster_count(bs);
3731 
3732 	spdk_blob_close(blob, blob_op_complete, NULL);
3733 	poll_threads();
3734 	CU_ASSERT(g_bserrno == 0);
3735 	blob = NULL;
3736 	g_blob = NULL;
3737 	g_blobid = SPDK_BLOBID_INVALID;
3738 
3739 	ut_bs_dirty_load(&bs, NULL);
3740 
3741 	spdk_bs_open_blob(bs, blobid2, blob_op_with_handle_complete, NULL);
3742 	poll_threads();
3743 	CU_ASSERT(g_bserrno == 0);
3744 	CU_ASSERT(g_blob != NULL);
3745 	blob = g_blob;
3746 
3747 	/* Get the xattrs */
3748 	value = NULL;
3749 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
3750 	CU_ASSERT(rc == 0);
3751 	SPDK_CU_ASSERT_FATAL(value != NULL);
3752 	CU_ASSERT(*(uint64_t *)value == length);
3753 	CU_ASSERT(value_len == 8);
3754 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
3755 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3756 
3757 	ut_blob_close_and_delete(bs, blob);
3758 
3759 	free_clusters = spdk_bs_free_cluster_count(bs);
3760 
3761 	ut_bs_dirty_load(&bs, NULL);
3762 
3763 	spdk_bs_open_blob(bs, blobid2, blob_op_with_handle_complete, NULL);
3764 	poll_threads();
3765 	CU_ASSERT(g_bserrno != 0);
3766 	CU_ASSERT(g_blob == NULL);
3767 
3768 	spdk_bs_open_blob(bs, blobid1, blob_op_with_handle_complete, NULL);
3769 	poll_threads();
3770 	CU_ASSERT(g_bserrno == 0);
3771 	CU_ASSERT(g_blob != NULL);
3772 	blob = g_blob;
3773 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3774 	spdk_blob_close(blob, blob_op_complete, NULL);
3775 	poll_threads();
3776 	CU_ASSERT(g_bserrno == 0);
3777 
3778 	ut_bs_reload(&bs, NULL);
3779 
3780 	/* Create second blob */
3781 	ut_spdk_blob_opts_init(&blob_opts);
3782 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
3783 	poll_threads();
3784 	CU_ASSERT(g_bserrno == 0);
3785 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3786 	blobid2 = g_blobid;
3787 
3788 	/* Create third blob */
3789 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
3790 	poll_threads();
3791 	CU_ASSERT(g_bserrno == 0);
3792 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3793 	blobid3 = g_blobid;
3794 
3795 	spdk_bs_open_blob(bs, blobid2, blob_op_with_handle_complete, NULL);
3796 	poll_threads();
3797 	CU_ASSERT(g_bserrno == 0);
3798 	CU_ASSERT(g_blob != NULL);
3799 	blob = g_blob;
3800 
3801 	/* Set some xattrs for second blob */
3802 	rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1);
3803 	CU_ASSERT(rc == 0);
3804 
3805 	length = 5432;
3806 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3807 	CU_ASSERT(rc == 0);
3808 
3809 	spdk_blob_close(blob, blob_op_complete, NULL);
3810 	poll_threads();
3811 	CU_ASSERT(g_bserrno == 0);
3812 	blob = NULL;
3813 	g_blob = NULL;
3814 	g_blobid = SPDK_BLOBID_INVALID;
3815 
3816 	spdk_bs_open_blob(bs, blobid3, blob_op_with_handle_complete, NULL);
3817 	poll_threads();
3818 	CU_ASSERT(g_bserrno == 0);
3819 	CU_ASSERT(g_blob != NULL);
3820 	blob = g_blob;
3821 
3822 	/* Set some xattrs for third blob */
3823 	rc = spdk_blob_set_xattr(blob, "name", "log2.txt", strlen("log2.txt") + 1);
3824 	CU_ASSERT(rc == 0);
3825 
3826 	length = 5432;
3827 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3828 	CU_ASSERT(rc == 0);
3829 
3830 	spdk_blob_close(blob, blob_op_complete, NULL);
3831 	poll_threads();
3832 	CU_ASSERT(g_bserrno == 0);
3833 	blob = NULL;
3834 	g_blob = NULL;
3835 	g_blobid = SPDK_BLOBID_INVALID;
3836 
3837 	/* Mark second blob as invalid */
3838 	page_num = bs_blobid_to_page(blobid2);
3839 
3840 	index = DEV_BUFFER_BLOCKLEN * (bs->md_start + page_num);
3841 	page = (struct spdk_blob_md_page *)&g_dev_buffer[index];
3842 	page->sequence_num = 1;
3843 	page->crc = blob_md_page_calc_crc(page);
3844 
3845 	free_clusters = spdk_bs_free_cluster_count(bs);
3846 
3847 	ut_bs_dirty_load(&bs, NULL);
3848 
3849 	spdk_bs_open_blob(bs, blobid2, blob_op_with_handle_complete, NULL);
3850 	poll_threads();
3851 	CU_ASSERT(g_bserrno != 0);
3852 	CU_ASSERT(g_blob == NULL);
3853 
3854 	spdk_bs_open_blob(bs, blobid3, blob_op_with_handle_complete, NULL);
3855 	poll_threads();
3856 	CU_ASSERT(g_bserrno == 0);
3857 	CU_ASSERT(g_blob != NULL);
3858 	blob = g_blob;
3859 
3860 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3861 }
3862 
3863 static void
3864 blob_flags(void)
3865 {
3866 	struct spdk_blob_store *bs = g_bs;
3867 	spdk_blob_id blobid_invalid, blobid_data_ro, blobid_md_ro;
3868 	struct spdk_blob *blob_invalid, *blob_data_ro, *blob_md_ro;
3869 	struct spdk_blob_opts blob_opts;
3870 	int rc;
3871 
3872 	/* Create three blobs - one each for testing invalid, data_ro and md_ro flags. */
3873 	blob_invalid = ut_blob_create_and_open(bs, NULL);
3874 	blobid_invalid = spdk_blob_get_id(blob_invalid);
3875 
3876 	blob_data_ro = ut_blob_create_and_open(bs, NULL);
3877 	blobid_data_ro = spdk_blob_get_id(blob_data_ro);
3878 
3879 	ut_spdk_blob_opts_init(&blob_opts);
3880 	blob_opts.clear_method = BLOB_CLEAR_WITH_WRITE_ZEROES;
3881 	blob_md_ro = ut_blob_create_and_open(bs, &blob_opts);
3882 	blobid_md_ro = spdk_blob_get_id(blob_md_ro);
3883 	CU_ASSERT((blob_md_ro->md_ro_flags & SPDK_BLOB_MD_RO_FLAGS_MASK) == BLOB_CLEAR_WITH_WRITE_ZEROES);
3884 
3885 	/* Change the size of blob_data_ro to check if flags are serialized
3886 	 * when blob has non zero number of extents */
3887 	spdk_blob_resize(blob_data_ro, 10, blob_op_complete, NULL);
3888 	poll_threads();
3889 	CU_ASSERT(g_bserrno == 0);
3890 
3891 	/* Set the xattr to check if flags are serialized
3892 	 * when blob has non zero number of xattrs */
3893 	rc = spdk_blob_set_xattr(blob_md_ro, "name", "log.txt", strlen("log.txt") + 1);
3894 	CU_ASSERT(rc == 0);
3895 
3896 	blob_invalid->invalid_flags = (1ULL << 63);
3897 	blob_invalid->state = SPDK_BLOB_STATE_DIRTY;
3898 	blob_data_ro->data_ro_flags = (1ULL << 62);
3899 	blob_data_ro->state = SPDK_BLOB_STATE_DIRTY;
3900 	blob_md_ro->md_ro_flags = (1ULL << 61);
3901 	blob_md_ro->state = SPDK_BLOB_STATE_DIRTY;
3902 
3903 	g_bserrno = -1;
3904 	spdk_blob_sync_md(blob_invalid, blob_op_complete, NULL);
3905 	poll_threads();
3906 	CU_ASSERT(g_bserrno == 0);
3907 	g_bserrno = -1;
3908 	spdk_blob_sync_md(blob_data_ro, blob_op_complete, NULL);
3909 	poll_threads();
3910 	CU_ASSERT(g_bserrno == 0);
3911 	g_bserrno = -1;
3912 	spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL);
3913 	poll_threads();
3914 	CU_ASSERT(g_bserrno == 0);
3915 
3916 	g_bserrno = -1;
3917 	spdk_blob_close(blob_invalid, blob_op_complete, NULL);
3918 	poll_threads();
3919 	CU_ASSERT(g_bserrno == 0);
3920 	blob_invalid = NULL;
3921 	g_bserrno = -1;
3922 	spdk_blob_close(blob_data_ro, blob_op_complete, NULL);
3923 	poll_threads();
3924 	CU_ASSERT(g_bserrno == 0);
3925 	blob_data_ro = NULL;
3926 	g_bserrno = -1;
3927 	spdk_blob_close(blob_md_ro, blob_op_complete, NULL);
3928 	poll_threads();
3929 	CU_ASSERT(g_bserrno == 0);
3930 	blob_md_ro = NULL;
3931 
3932 	g_blob = NULL;
3933 	g_blobid = SPDK_BLOBID_INVALID;
3934 
3935 	ut_bs_reload(&bs, NULL);
3936 
3937 	g_blob = NULL;
3938 	g_bserrno = 0;
3939 	spdk_bs_open_blob(bs, blobid_invalid, blob_op_with_handle_complete, NULL);
3940 	poll_threads();
3941 	CU_ASSERT(g_bserrno != 0);
3942 	CU_ASSERT(g_blob == NULL);
3943 
3944 	g_blob = NULL;
3945 	g_bserrno = -1;
3946 	spdk_bs_open_blob(bs, blobid_data_ro, blob_op_with_handle_complete, NULL);
3947 	poll_threads();
3948 	CU_ASSERT(g_bserrno == 0);
3949 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3950 	blob_data_ro = g_blob;
3951 	/* If an unknown data_ro flag was found, the blob should be marked both data and md read-only. */
3952 	CU_ASSERT(blob_data_ro->data_ro == true);
3953 	CU_ASSERT(blob_data_ro->md_ro == true);
3954 	CU_ASSERT(spdk_blob_get_num_clusters(blob_data_ro) == 10);
3955 
3956 	g_blob = NULL;
3957 	g_bserrno = -1;
3958 	spdk_bs_open_blob(bs, blobid_md_ro, blob_op_with_handle_complete, NULL);
3959 	poll_threads();
3960 	CU_ASSERT(g_bserrno == 0);
3961 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3962 	blob_md_ro = g_blob;
3963 	CU_ASSERT(blob_md_ro->data_ro == false);
3964 	CU_ASSERT(blob_md_ro->md_ro == true);
3965 
3966 	g_bserrno = -1;
3967 	spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL);
3968 	poll_threads();
3969 	CU_ASSERT(g_bserrno == 0);
3970 
3971 	ut_blob_close_and_delete(bs, blob_data_ro);
3972 	ut_blob_close_and_delete(bs, blob_md_ro);
3973 }
3974 
3975 static void
3976 bs_version(void)
3977 {
3978 	struct spdk_bs_super_block *super;
3979 	struct spdk_blob_store *bs = g_bs;
3980 	struct spdk_bs_dev *dev;
3981 	struct spdk_blob *blob;
3982 	struct spdk_blob_opts blob_opts;
3983 	spdk_blob_id blobid;
3984 
3985 	/* Unload the blob store */
3986 	spdk_bs_unload(bs, bs_op_complete, NULL);
3987 	poll_threads();
3988 	CU_ASSERT(g_bserrno == 0);
3989 	g_bs = NULL;
3990 
3991 	/*
3992 	 * Change the bs version on disk.  This will allow us to
3993 	 *  test that the version does not get modified automatically
3994 	 *  when loading and unloading the blobstore.
3995 	 */
3996 	super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
3997 	CU_ASSERT(super->version == SPDK_BS_VERSION);
3998 	CU_ASSERT(super->clean == 1);
3999 	super->version = 2;
4000 	/*
4001 	 * Version 2 metadata does not have a used blobid mask, so clear
4002 	 *  those fields in the super block and zero the corresponding
4003 	 *  region on "disk".  We will use this to ensure blob IDs are
4004 	 *  correctly reconstructed.
4005 	 */
4006 	memset(&g_dev_buffer[super->used_blobid_mask_start * SPDK_BS_PAGE_SIZE], 0,
4007 	       super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE);
4008 	super->used_blobid_mask_start = 0;
4009 	super->used_blobid_mask_len = 0;
4010 	super->crc = blob_md_page_calc_crc(super);
4011 
4012 	/* Load an existing blob store */
4013 	dev = init_dev();
4014 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
4015 	poll_threads();
4016 	CU_ASSERT(g_bserrno == 0);
4017 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4018 	CU_ASSERT(super->clean == 1);
4019 	bs = g_bs;
4020 
4021 	/*
4022 	 * Create a blob - just to make sure that when we unload it
4023 	 *  results in writing the super block (since metadata pages
4024 	 *  were allocated.
4025 	 */
4026 	ut_spdk_blob_opts_init(&blob_opts);
4027 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
4028 	poll_threads();
4029 	CU_ASSERT(g_bserrno == 0);
4030 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4031 	blobid = g_blobid;
4032 
4033 	/* Unload the blob store */
4034 	spdk_bs_unload(bs, bs_op_complete, NULL);
4035 	poll_threads();
4036 	CU_ASSERT(g_bserrno == 0);
4037 	g_bs = NULL;
4038 	CU_ASSERT(super->version == 2);
4039 	CU_ASSERT(super->used_blobid_mask_start == 0);
4040 	CU_ASSERT(super->used_blobid_mask_len == 0);
4041 
4042 	dev = init_dev();
4043 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
4044 	poll_threads();
4045 	CU_ASSERT(g_bserrno == 0);
4046 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4047 	bs = g_bs;
4048 
4049 	g_blob = NULL;
4050 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4051 	poll_threads();
4052 	CU_ASSERT(g_bserrno == 0);
4053 	CU_ASSERT(g_blob != NULL);
4054 	blob = g_blob;
4055 
4056 	ut_blob_close_and_delete(bs, blob);
4057 
4058 	CU_ASSERT(super->version == 2);
4059 	CU_ASSERT(super->used_blobid_mask_start == 0);
4060 	CU_ASSERT(super->used_blobid_mask_len == 0);
4061 }
4062 
4063 static void
4064 blob_set_xattrs_test(void)
4065 {
4066 	struct spdk_blob_store *bs = g_bs;
4067 	struct spdk_blob *blob;
4068 	struct spdk_blob_opts opts;
4069 	const void *value;
4070 	size_t value_len;
4071 	char *xattr;
4072 	size_t xattr_length;
4073 	int rc;
4074 
4075 	/* Create blob with extra attributes */
4076 	ut_spdk_blob_opts_init(&opts);
4077 
4078 	opts.xattrs.names = g_xattr_names;
4079 	opts.xattrs.get_value = _get_xattr_value;
4080 	opts.xattrs.count = 3;
4081 	opts.xattrs.ctx = &g_ctx;
4082 
4083 	blob = ut_blob_create_and_open(bs, &opts);
4084 
4085 	/* Get the xattrs */
4086 	value = NULL;
4087 
4088 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len);
4089 	CU_ASSERT(rc == 0);
4090 	SPDK_CU_ASSERT_FATAL(value != NULL);
4091 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
4092 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
4093 
4094 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len);
4095 	CU_ASSERT(rc == 0);
4096 	SPDK_CU_ASSERT_FATAL(value != NULL);
4097 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
4098 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
4099 
4100 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len);
4101 	CU_ASSERT(rc == 0);
4102 	SPDK_CU_ASSERT_FATAL(value != NULL);
4103 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
4104 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
4105 
4106 	/* Try to get non existing attribute */
4107 
4108 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
4109 	CU_ASSERT(rc == -ENOENT);
4110 
4111 	/* Try xattr exceeding maximum length of descriptor in single page */
4112 	xattr_length = SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_xattr) -
4113 		       strlen("large_xattr") + 1;
4114 	xattr = calloc(xattr_length, sizeof(char));
4115 	SPDK_CU_ASSERT_FATAL(xattr != NULL);
4116 	rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
4117 	free(xattr);
4118 	SPDK_CU_ASSERT_FATAL(rc == -ENOMEM);
4119 
4120 	spdk_blob_close(blob, blob_op_complete, NULL);
4121 	poll_threads();
4122 	CU_ASSERT(g_bserrno == 0);
4123 	blob = NULL;
4124 	g_blob = NULL;
4125 	g_blobid = SPDK_BLOBID_INVALID;
4126 
4127 	/* NULL callback */
4128 	ut_spdk_blob_opts_init(&opts);
4129 	opts.xattrs.names = g_xattr_names;
4130 	opts.xattrs.get_value = NULL;
4131 	opts.xattrs.count = 1;
4132 	opts.xattrs.ctx = &g_ctx;
4133 
4134 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4135 	poll_threads();
4136 	CU_ASSERT(g_bserrno == -EINVAL);
4137 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4138 
4139 	/* NULL values */
4140 	ut_spdk_blob_opts_init(&opts);
4141 	opts.xattrs.names = g_xattr_names;
4142 	opts.xattrs.get_value = _get_xattr_value_null;
4143 	opts.xattrs.count = 1;
4144 	opts.xattrs.ctx = NULL;
4145 
4146 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4147 	poll_threads();
4148 	CU_ASSERT(g_bserrno == -EINVAL);
4149 }
4150 
4151 static void
4152 blob_thin_prov_alloc(void)
4153 {
4154 	struct spdk_blob_store *bs = g_bs;
4155 	struct spdk_blob *blob;
4156 	struct spdk_blob_opts opts;
4157 	spdk_blob_id blobid;
4158 	uint64_t free_clusters;
4159 
4160 	free_clusters = spdk_bs_free_cluster_count(bs);
4161 
4162 	/* Set blob as thin provisioned */
4163 	ut_spdk_blob_opts_init(&opts);
4164 	opts.thin_provision = true;
4165 
4166 	blob = ut_blob_create_and_open(bs, &opts);
4167 	blobid = spdk_blob_get_id(blob);
4168 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4169 
4170 	CU_ASSERT(blob->active.num_clusters == 0);
4171 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
4172 
4173 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
4174 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
4175 	poll_threads();
4176 	CU_ASSERT(g_bserrno == 0);
4177 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4178 	CU_ASSERT(blob->active.num_clusters == 5);
4179 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4180 
4181 	/* Grow it to 1TB - still unallocated */
4182 	spdk_blob_resize(blob, 262144, blob_op_complete, NULL);
4183 	poll_threads();
4184 	CU_ASSERT(g_bserrno == 0);
4185 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4186 	CU_ASSERT(blob->active.num_clusters == 262144);
4187 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 262144);
4188 
4189 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4190 	poll_threads();
4191 	CU_ASSERT(g_bserrno == 0);
4192 	/* Sync must not change anything */
4193 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4194 	CU_ASSERT(blob->active.num_clusters == 262144);
4195 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 262144);
4196 	/* Since clusters are not allocated,
4197 	 * number of metadata pages is expected to be minimal.
4198 	 */
4199 	CU_ASSERT(blob->active.num_pages == 1);
4200 
4201 	/* Shrink the blob to 3 clusters - still unallocated */
4202 	spdk_blob_resize(blob, 3, blob_op_complete, NULL);
4203 	poll_threads();
4204 	CU_ASSERT(g_bserrno == 0);
4205 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4206 	CU_ASSERT(blob->active.num_clusters == 3);
4207 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3);
4208 
4209 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4210 	poll_threads();
4211 	CU_ASSERT(g_bserrno == 0);
4212 	/* Sync must not change anything */
4213 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4214 	CU_ASSERT(blob->active.num_clusters == 3);
4215 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3);
4216 
4217 	spdk_blob_close(blob, blob_op_complete, NULL);
4218 	poll_threads();
4219 	CU_ASSERT(g_bserrno == 0);
4220 
4221 	ut_bs_reload(&bs, NULL);
4222 
4223 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4224 	poll_threads();
4225 	CU_ASSERT(g_bserrno == 0);
4226 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4227 	blob = g_blob;
4228 
4229 	/* Check that clusters allocation and size is still the same */
4230 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4231 	CU_ASSERT(blob->active.num_clusters == 3);
4232 
4233 	ut_blob_close_and_delete(bs, blob);
4234 }
4235 
4236 static void
4237 blob_insert_cluster_msg_test(void)
4238 {
4239 	struct spdk_blob_store *bs = g_bs;
4240 	struct spdk_blob *blob;
4241 	struct spdk_blob_opts opts;
4242 	struct spdk_blob_md_page page = {};
4243 	spdk_blob_id blobid;
4244 	uint64_t free_clusters;
4245 	uint64_t new_cluster = 0;
4246 	uint32_t cluster_num = 3;
4247 	uint32_t extent_page = 0;
4248 
4249 	free_clusters = spdk_bs_free_cluster_count(bs);
4250 
4251 	/* Set blob as thin provisioned */
4252 	ut_spdk_blob_opts_init(&opts);
4253 	opts.thin_provision = true;
4254 	opts.num_clusters = 4;
4255 
4256 	blob = ut_blob_create_and_open(bs, &opts);
4257 	blobid = spdk_blob_get_id(blob);
4258 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4259 
4260 	CU_ASSERT(blob->active.num_clusters == 4);
4261 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 4);
4262 	CU_ASSERT(blob->active.clusters[cluster_num] == 0);
4263 
4264 	/* Specify cluster_num to allocate and new_cluster will be returned to insert on md_thread.
4265 	 * This is to simulate behaviour when cluster is allocated after blob creation.
4266 	 * Such as _spdk_bs_allocate_and_copy_cluster(). */
4267 	spdk_spin_lock(&bs->used_lock);
4268 	bs_allocate_cluster(blob, cluster_num, &new_cluster, &extent_page, false);
4269 	CU_ASSERT(blob->active.clusters[cluster_num] == 0);
4270 	spdk_spin_unlock(&bs->used_lock);
4271 
4272 	blob_insert_cluster_on_md_thread(blob, cluster_num, new_cluster, extent_page, &page,
4273 					 blob_op_complete, NULL);
4274 	poll_threads();
4275 
4276 	CU_ASSERT(blob->active.clusters[cluster_num] != 0);
4277 
4278 	spdk_blob_close(blob, blob_op_complete, NULL);
4279 	poll_threads();
4280 	CU_ASSERT(g_bserrno == 0);
4281 
4282 	ut_bs_reload(&bs, NULL);
4283 
4284 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4285 	poll_threads();
4286 	CU_ASSERT(g_bserrno == 0);
4287 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4288 	blob = g_blob;
4289 
4290 	CU_ASSERT(blob->active.clusters[cluster_num] != 0);
4291 
4292 	ut_blob_close_and_delete(bs, blob);
4293 }
4294 
4295 static void
4296 blob_thin_prov_rw(void)
4297 {
4298 	static const uint8_t zero[10 * 4096] = { 0 };
4299 	struct spdk_blob_store *bs = g_bs;
4300 	struct spdk_blob *blob, *blob_id0;
4301 	struct spdk_io_channel *channel, *channel_thread1;
4302 	struct spdk_blob_opts opts;
4303 	uint64_t free_clusters;
4304 	uint64_t page_size;
4305 	uint8_t payload_read[10 * 4096];
4306 	uint8_t payload_write[10 * 4096];
4307 	uint64_t write_bytes;
4308 	uint64_t read_bytes;
4309 
4310 	free_clusters = spdk_bs_free_cluster_count(bs);
4311 	page_size = spdk_bs_get_page_size(bs);
4312 
4313 	channel = spdk_bs_alloc_io_channel(bs);
4314 	CU_ASSERT(channel != NULL);
4315 
4316 	ut_spdk_blob_opts_init(&opts);
4317 	opts.thin_provision = true;
4318 
4319 	/* Create and delete blob at md page 0, so that next md page allocation
4320 	 * for extent will use that. */
4321 	blob_id0 = ut_blob_create_and_open(bs, &opts);
4322 	blob = ut_blob_create_and_open(bs, &opts);
4323 	ut_blob_close_and_delete(bs, blob_id0);
4324 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4325 
4326 	CU_ASSERT(blob->active.num_clusters == 0);
4327 
4328 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
4329 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
4330 	poll_threads();
4331 	CU_ASSERT(g_bserrno == 0);
4332 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4333 	CU_ASSERT(blob->active.num_clusters == 5);
4334 
4335 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4336 	poll_threads();
4337 	CU_ASSERT(g_bserrno == 0);
4338 	/* Sync must not change anything */
4339 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4340 	CU_ASSERT(blob->active.num_clusters == 5);
4341 
4342 	/* Payload should be all zeros from unallocated clusters */
4343 	memset(payload_read, 0xFF, sizeof(payload_read));
4344 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4345 	poll_threads();
4346 	CU_ASSERT(g_bserrno == 0);
4347 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4348 
4349 	write_bytes = g_dev_write_bytes;
4350 	read_bytes = g_dev_read_bytes;
4351 
4352 	/* Perform write on thread 1. That will allocate cluster on thread 0 via send_msg */
4353 	set_thread(1);
4354 	channel_thread1 = spdk_bs_alloc_io_channel(bs);
4355 	CU_ASSERT(channel_thread1 != NULL);
4356 	memset(payload_write, 0xE5, sizeof(payload_write));
4357 	spdk_blob_io_write(blob, channel_thread1, payload_write, 4, 10, blob_op_complete, NULL);
4358 	CU_ASSERT(free_clusters - 1 == spdk_bs_free_cluster_count(bs));
4359 	/* Perform write on thread 0. That will try to allocate cluster,
4360 	 * but fail due to another thread issuing the cluster allocation first. */
4361 	set_thread(0);
4362 	memset(payload_write, 0xE5, sizeof(payload_write));
4363 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4364 	CU_ASSERT(free_clusters - 2 == spdk_bs_free_cluster_count(bs));
4365 	poll_threads();
4366 	CU_ASSERT(g_bserrno == 0);
4367 	CU_ASSERT(free_clusters - 1 == spdk_bs_free_cluster_count(bs));
4368 	/* For thin-provisioned blob we need to write 20 pages plus one page metadata and
4369 	 * read 0 bytes */
4370 	if (g_use_extent_table) {
4371 		/* Add one more page for EXTENT_PAGE write */
4372 		CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 22);
4373 	} else {
4374 		CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 21);
4375 	}
4376 	CU_ASSERT(g_dev_read_bytes - read_bytes == 0);
4377 
4378 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4379 	poll_threads();
4380 	CU_ASSERT(g_bserrno == 0);
4381 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4382 
4383 	ut_blob_close_and_delete(bs, blob);
4384 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4385 
4386 	set_thread(1);
4387 	spdk_bs_free_io_channel(channel_thread1);
4388 	set_thread(0);
4389 	spdk_bs_free_io_channel(channel);
4390 	poll_threads();
4391 	g_blob = NULL;
4392 	g_blobid = 0;
4393 }
4394 
4395 static void
4396 blob_thin_prov_write_count_io(void)
4397 {
4398 	struct spdk_blob_store *bs;
4399 	struct spdk_blob *blob;
4400 	struct spdk_io_channel *ch;
4401 	struct spdk_bs_dev *dev;
4402 	struct spdk_bs_opts bs_opts;
4403 	struct spdk_blob_opts opts;
4404 	uint64_t free_clusters;
4405 	uint64_t page_size;
4406 	uint8_t payload_write[4096];
4407 	uint64_t write_bytes;
4408 	uint64_t read_bytes;
4409 	const uint32_t CLUSTER_SZ = 16384;
4410 	uint32_t pages_per_cluster;
4411 	uint32_t pages_per_extent_page;
4412 	uint32_t i;
4413 
4414 	/* Use a very small cluster size for this test.  This ensures we need multiple
4415 	 * extent pages to hold all of the clusters even for relatively small blobs like
4416 	 * we are restricted to for the unit tests (i.e. we don't want to allocate multi-GB
4417 	 * buffers).
4418 	 */
4419 	dev = init_dev();
4420 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
4421 	bs_opts.cluster_sz = CLUSTER_SZ;
4422 
4423 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
4424 	poll_threads();
4425 	CU_ASSERT(g_bserrno == 0);
4426 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4427 	bs = g_bs;
4428 
4429 	free_clusters = spdk_bs_free_cluster_count(bs);
4430 	page_size = spdk_bs_get_page_size(bs);
4431 	pages_per_cluster = CLUSTER_SZ / page_size;
4432 	pages_per_extent_page = SPDK_EXTENTS_PER_EP * pages_per_cluster;
4433 
4434 	ch = spdk_bs_alloc_io_channel(bs);
4435 	SPDK_CU_ASSERT_FATAL(ch != NULL);
4436 
4437 	ut_spdk_blob_opts_init(&opts);
4438 	opts.thin_provision = true;
4439 
4440 	blob = ut_blob_create_and_open(bs, &opts);
4441 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4442 
4443 	/* Resize the blob so that it will require 8 extent pages to hold all of
4444 	 * the clusters.
4445 	 */
4446 	g_bserrno = -1;
4447 	spdk_blob_resize(blob, SPDK_EXTENTS_PER_EP * 8, blob_op_complete, NULL);
4448 	poll_threads();
4449 	CU_ASSERT(g_bserrno == 0);
4450 
4451 	g_bserrno = -1;
4452 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4453 	poll_threads();
4454 	CU_ASSERT(g_bserrno == 0);
4455 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4456 	CU_ASSERT(blob->active.num_clusters == SPDK_EXTENTS_PER_EP * 8);
4457 
4458 	memset(payload_write, 0, sizeof(payload_write));
4459 	for (i = 0; i < 8; i++) {
4460 		write_bytes = g_dev_write_bytes;
4461 		read_bytes = g_dev_read_bytes;
4462 
4463 		g_bserrno = -1;
4464 		spdk_blob_io_write(blob, ch, payload_write, pages_per_extent_page * i, 1, blob_op_complete, NULL);
4465 		poll_threads();
4466 		CU_ASSERT(g_bserrno == 0);
4467 		CU_ASSERT(free_clusters - (2 * i + 1) == spdk_bs_free_cluster_count(bs));
4468 
4469 		CU_ASSERT(g_dev_read_bytes == read_bytes);
4470 		if (!g_use_extent_table) {
4471 			/* For legacy metadata, we should have written two pages - one for the
4472 			 * write I/O itself, another for the blob's primary metadata.
4473 			 */
4474 			CU_ASSERT((g_dev_write_bytes - write_bytes) / page_size == 2);
4475 		} else {
4476 			/* For extent table metadata, we should have written three pages - one
4477 			 * for the write I/O, one for the extent page, one for the blob's primary
4478 			 * metadata.
4479 			 */
4480 			CU_ASSERT((g_dev_write_bytes - write_bytes) / page_size == 3);
4481 		}
4482 
4483 		/* The write should have synced the metadata already.  Do another sync here
4484 		 * just to confirm.
4485 		 */
4486 		write_bytes = g_dev_write_bytes;
4487 		read_bytes = g_dev_read_bytes;
4488 
4489 		g_bserrno = -1;
4490 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
4491 		poll_threads();
4492 		CU_ASSERT(g_bserrno == 0);
4493 		CU_ASSERT(free_clusters - (2 * i + 1) == spdk_bs_free_cluster_count(bs));
4494 
4495 		CU_ASSERT(g_dev_read_bytes == read_bytes);
4496 		CU_ASSERT(g_dev_write_bytes == write_bytes);
4497 
4498 		/* Now write to another unallocated cluster that is part of the same extent page. */
4499 		g_bserrno = -1;
4500 		spdk_blob_io_write(blob, ch, payload_write, pages_per_extent_page * i + pages_per_cluster,
4501 				   1, blob_op_complete, NULL);
4502 		poll_threads();
4503 		CU_ASSERT(g_bserrno == 0);
4504 		CU_ASSERT(free_clusters - (2 * i + 2) == spdk_bs_free_cluster_count(bs));
4505 
4506 		CU_ASSERT(g_dev_read_bytes == read_bytes);
4507 		/*
4508 		 * For legacy metadata, we should have written the I/O and the primary metadata page.
4509 		 * For extent table metadata, we should have written the I/O and the extent metadata page.
4510 		 */
4511 		CU_ASSERT((g_dev_write_bytes - write_bytes) / page_size == 2);
4512 
4513 		/* Send unmap aligned to the whole cluster - should free it up */
4514 		g_bserrno = -1;
4515 		spdk_blob_io_unmap(blob, ch, pages_per_extent_page * i, pages_per_cluster, blob_op_complete, NULL);
4516 		poll_threads();
4517 		CU_ASSERT(g_bserrno == 0);
4518 		CU_ASSERT(free_clusters - (2 * i + 1) == spdk_bs_free_cluster_count(bs));
4519 
4520 		/* Write back to the freed cluster */
4521 		g_bserrno = -1;
4522 		spdk_blob_io_write(blob, ch, payload_write, pages_per_extent_page * i, 1, blob_op_complete, NULL);
4523 		poll_threads();
4524 		CU_ASSERT(g_bserrno == 0);
4525 		CU_ASSERT(free_clusters - (2 * i + 2) == spdk_bs_free_cluster_count(bs));
4526 	}
4527 
4528 	ut_blob_close_and_delete(bs, blob);
4529 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4530 
4531 	spdk_bs_free_io_channel(ch);
4532 	poll_threads();
4533 	g_blob = NULL;
4534 	g_blobid = 0;
4535 
4536 	spdk_bs_unload(bs, bs_op_complete, NULL);
4537 	poll_threads();
4538 	CU_ASSERT(g_bserrno == 0);
4539 	g_bs = NULL;
4540 }
4541 
4542 static void
4543 blob_thin_prov_rle(void)
4544 {
4545 	static const uint8_t zero[10 * 4096] = { 0 };
4546 	struct spdk_blob_store *bs = g_bs;
4547 	struct spdk_blob *blob;
4548 	struct spdk_io_channel *channel;
4549 	struct spdk_blob_opts opts;
4550 	spdk_blob_id blobid;
4551 	uint64_t free_clusters;
4552 	uint64_t page_size;
4553 	uint8_t payload_read[10 * 4096];
4554 	uint8_t payload_write[10 * 4096];
4555 	uint64_t write_bytes;
4556 	uint64_t read_bytes;
4557 	uint64_t io_unit;
4558 
4559 	free_clusters = spdk_bs_free_cluster_count(bs);
4560 	page_size = spdk_bs_get_page_size(bs);
4561 
4562 	ut_spdk_blob_opts_init(&opts);
4563 	opts.thin_provision = true;
4564 	opts.num_clusters = 5;
4565 
4566 	blob = ut_blob_create_and_open(bs, &opts);
4567 	blobid = spdk_blob_get_id(blob);
4568 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4569 
4570 	channel = spdk_bs_alloc_io_channel(bs);
4571 	CU_ASSERT(channel != NULL);
4572 
4573 	/* Target specifically second cluster in a blob as first allocation */
4574 	io_unit = bs_cluster_to_page(bs, 1) * bs_io_unit_per_page(bs);
4575 
4576 	/* Payload should be all zeros from unallocated clusters */
4577 	memset(payload_read, 0xFF, sizeof(payload_read));
4578 	spdk_blob_io_read(blob, channel, payload_read, io_unit, 10, blob_op_complete, NULL);
4579 	poll_threads();
4580 	CU_ASSERT(g_bserrno == 0);
4581 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4582 
4583 	write_bytes = g_dev_write_bytes;
4584 	read_bytes = g_dev_read_bytes;
4585 
4586 	/* Issue write to second cluster in a blob */
4587 	memset(payload_write, 0xE5, sizeof(payload_write));
4588 	spdk_blob_io_write(blob, channel, payload_write, io_unit, 10, blob_op_complete, NULL);
4589 	poll_threads();
4590 	CU_ASSERT(g_bserrno == 0);
4591 	CU_ASSERT(free_clusters - 1 == spdk_bs_free_cluster_count(bs));
4592 	/* For thin-provisioned blob we need to write 10 pages plus one page metadata and
4593 	 * read 0 bytes */
4594 	if (g_use_extent_table) {
4595 		/* Add one more page for EXTENT_PAGE write */
4596 		CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 12);
4597 	} else {
4598 		CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 11);
4599 	}
4600 	CU_ASSERT(g_dev_read_bytes - read_bytes == 0);
4601 
4602 	spdk_blob_io_read(blob, channel, payload_read, io_unit, 10, blob_op_complete, NULL);
4603 	poll_threads();
4604 	CU_ASSERT(g_bserrno == 0);
4605 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4606 
4607 	spdk_bs_free_io_channel(channel);
4608 	poll_threads();
4609 
4610 	spdk_blob_close(blob, blob_op_complete, NULL);
4611 	poll_threads();
4612 	CU_ASSERT(g_bserrno == 0);
4613 
4614 	ut_bs_reload(&bs, NULL);
4615 
4616 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4617 	poll_threads();
4618 	CU_ASSERT(g_bserrno == 0);
4619 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4620 	blob = g_blob;
4621 
4622 	channel = spdk_bs_alloc_io_channel(bs);
4623 	CU_ASSERT(channel != NULL);
4624 
4625 	/* Read second cluster after blob reload to confirm data written */
4626 	spdk_blob_io_read(blob, channel, payload_read, io_unit, 10, blob_op_complete, NULL);
4627 	poll_threads();
4628 	CU_ASSERT(g_bserrno == 0);
4629 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4630 
4631 	spdk_bs_free_io_channel(channel);
4632 	poll_threads();
4633 
4634 	ut_blob_close_and_delete(bs, blob);
4635 }
4636 
4637 static void
4638 blob_thin_prov_rw_iov(void)
4639 {
4640 	static const uint8_t zero[10 * 4096] = { 0 };
4641 	struct spdk_blob_store *bs = g_bs;
4642 	struct spdk_blob *blob;
4643 	struct spdk_io_channel *channel;
4644 	struct spdk_blob_opts opts;
4645 	uint64_t free_clusters;
4646 	uint8_t payload_read[10 * 4096];
4647 	uint8_t payload_write[10 * 4096];
4648 	struct iovec iov_read[3];
4649 	struct iovec iov_write[3];
4650 
4651 	free_clusters = spdk_bs_free_cluster_count(bs);
4652 
4653 	channel = spdk_bs_alloc_io_channel(bs);
4654 	CU_ASSERT(channel != NULL);
4655 
4656 	ut_spdk_blob_opts_init(&opts);
4657 	opts.thin_provision = true;
4658 
4659 	blob = ut_blob_create_and_open(bs, &opts);
4660 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4661 
4662 	CU_ASSERT(blob->active.num_clusters == 0);
4663 
4664 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
4665 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
4666 	poll_threads();
4667 	CU_ASSERT(g_bserrno == 0);
4668 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4669 	CU_ASSERT(blob->active.num_clusters == 5);
4670 
4671 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4672 	poll_threads();
4673 	CU_ASSERT(g_bserrno == 0);
4674 	/* Sync must not change anything */
4675 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4676 	CU_ASSERT(blob->active.num_clusters == 5);
4677 
4678 	/* Payload should be all zeros from unallocated clusters */
4679 	memset(payload_read, 0xAA, sizeof(payload_read));
4680 	iov_read[0].iov_base = payload_read;
4681 	iov_read[0].iov_len = 3 * 4096;
4682 	iov_read[1].iov_base = payload_read + 3 * 4096;
4683 	iov_read[1].iov_len = 4 * 4096;
4684 	iov_read[2].iov_base = payload_read + 7 * 4096;
4685 	iov_read[2].iov_len = 3 * 4096;
4686 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4687 	poll_threads();
4688 	CU_ASSERT(g_bserrno == 0);
4689 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4690 
4691 	memset(payload_write, 0xE5, sizeof(payload_write));
4692 	iov_write[0].iov_base = payload_write;
4693 	iov_write[0].iov_len = 1 * 4096;
4694 	iov_write[1].iov_base = payload_write + 1 * 4096;
4695 	iov_write[1].iov_len = 5 * 4096;
4696 	iov_write[2].iov_base = payload_write + 6 * 4096;
4697 	iov_write[2].iov_len = 4 * 4096;
4698 
4699 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
4700 	poll_threads();
4701 	CU_ASSERT(g_bserrno == 0);
4702 
4703 	memset(payload_read, 0xAA, sizeof(payload_read));
4704 	iov_read[0].iov_base = payload_read;
4705 	iov_read[0].iov_len = 3 * 4096;
4706 	iov_read[1].iov_base = payload_read + 3 * 4096;
4707 	iov_read[1].iov_len = 4 * 4096;
4708 	iov_read[2].iov_base = payload_read + 7 * 4096;
4709 	iov_read[2].iov_len = 3 * 4096;
4710 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4711 	poll_threads();
4712 	CU_ASSERT(g_bserrno == 0);
4713 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4714 
4715 	spdk_bs_free_io_channel(channel);
4716 	poll_threads();
4717 
4718 	ut_blob_close_and_delete(bs, blob);
4719 }
4720 
4721 struct iter_ctx {
4722 	int		current_iter;
4723 	spdk_blob_id	blobid[4];
4724 };
4725 
4726 static void
4727 test_iter(void *arg, struct spdk_blob *blob, int bserrno)
4728 {
4729 	struct iter_ctx *iter_ctx = arg;
4730 	spdk_blob_id blobid;
4731 
4732 	CU_ASSERT(bserrno == 0);
4733 	blobid = spdk_blob_get_id(blob);
4734 	CU_ASSERT(blobid == iter_ctx->blobid[iter_ctx->current_iter++]);
4735 }
4736 
4737 static void
4738 bs_load_iter_test(void)
4739 {
4740 	struct spdk_blob_store *bs;
4741 	struct spdk_bs_dev *dev;
4742 	struct iter_ctx iter_ctx = { 0 };
4743 	struct spdk_blob *blob;
4744 	int i, rc;
4745 	struct spdk_bs_opts opts;
4746 
4747 	dev = init_dev();
4748 	spdk_bs_opts_init(&opts, sizeof(opts));
4749 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4750 
4751 	/* Initialize a new blob store */
4752 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
4753 	poll_threads();
4754 	CU_ASSERT(g_bserrno == 0);
4755 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4756 	bs = g_bs;
4757 
4758 	for (i = 0; i < 4; i++) {
4759 		blob = ut_blob_create_and_open(bs, NULL);
4760 		iter_ctx.blobid[i] = spdk_blob_get_id(blob);
4761 
4762 		/* Just save the blobid as an xattr for testing purposes. */
4763 		rc = spdk_blob_set_xattr(blob, "blobid", &iter_ctx.blobid[i], sizeof(spdk_blob_id));
4764 		CU_ASSERT(rc == 0);
4765 
4766 		/* Resize the blob */
4767 		spdk_blob_resize(blob, i, blob_op_complete, NULL);
4768 		poll_threads();
4769 		CU_ASSERT(g_bserrno == 0);
4770 
4771 		spdk_blob_close(blob, blob_op_complete, NULL);
4772 		poll_threads();
4773 		CU_ASSERT(g_bserrno == 0);
4774 	}
4775 
4776 	g_bserrno = -1;
4777 	spdk_bs_unload(bs, bs_op_complete, NULL);
4778 	poll_threads();
4779 	CU_ASSERT(g_bserrno == 0);
4780 
4781 	dev = init_dev();
4782 	spdk_bs_opts_init(&opts, sizeof(opts));
4783 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4784 	opts.iter_cb_fn = test_iter;
4785 	opts.iter_cb_arg = &iter_ctx;
4786 
4787 	/* Test blob iteration during load after a clean shutdown. */
4788 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
4789 	poll_threads();
4790 	CU_ASSERT(g_bserrno == 0);
4791 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4792 	bs = g_bs;
4793 
4794 	/* Dirty shutdown */
4795 	bs_free(bs);
4796 
4797 	dev = init_dev();
4798 	spdk_bs_opts_init(&opts, sizeof(opts));
4799 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4800 	opts.iter_cb_fn = test_iter;
4801 	iter_ctx.current_iter = 0;
4802 	opts.iter_cb_arg = &iter_ctx;
4803 
4804 	/* Test blob iteration during load after a dirty shutdown. */
4805 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
4806 	poll_threads();
4807 	CU_ASSERT(g_bserrno == 0);
4808 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4809 	bs = g_bs;
4810 
4811 	spdk_bs_unload(bs, bs_op_complete, NULL);
4812 	poll_threads();
4813 	CU_ASSERT(g_bserrno == 0);
4814 	g_bs = NULL;
4815 }
4816 
4817 static void
4818 blob_snapshot_rw(void)
4819 {
4820 	static const uint8_t zero[10 * 4096] = { 0 };
4821 	struct spdk_blob_store *bs = g_bs;
4822 	struct spdk_blob *blob, *snapshot;
4823 	struct spdk_io_channel *channel;
4824 	struct spdk_blob_opts opts;
4825 	spdk_blob_id blobid, snapshotid;
4826 	uint64_t free_clusters;
4827 	uint64_t cluster_size;
4828 	uint64_t page_size;
4829 	uint8_t payload_read[10 * 4096];
4830 	uint8_t payload_write[10 * 4096];
4831 	uint64_t write_bytes_start;
4832 	uint64_t read_bytes_start;
4833 	uint64_t copy_bytes_start;
4834 	uint64_t write_bytes;
4835 	uint64_t read_bytes;
4836 	uint64_t copy_bytes;
4837 
4838 	free_clusters = spdk_bs_free_cluster_count(bs);
4839 	cluster_size = spdk_bs_get_cluster_size(bs);
4840 	page_size = spdk_bs_get_page_size(bs);
4841 
4842 	channel = spdk_bs_alloc_io_channel(bs);
4843 	CU_ASSERT(channel != NULL);
4844 
4845 	ut_spdk_blob_opts_init(&opts);
4846 	opts.thin_provision = true;
4847 	opts.num_clusters = 5;
4848 
4849 	blob = ut_blob_create_and_open(bs, &opts);
4850 	blobid = spdk_blob_get_id(blob);
4851 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4852 
4853 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4854 
4855 	memset(payload_read, 0xFF, sizeof(payload_read));
4856 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4857 	poll_threads();
4858 	CU_ASSERT(g_bserrno == 0);
4859 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4860 
4861 	memset(payload_write, 0xE5, sizeof(payload_write));
4862 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4863 	poll_threads();
4864 	CU_ASSERT(g_bserrno == 0);
4865 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4866 
4867 	/* Create snapshot from blob */
4868 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4869 	poll_threads();
4870 	CU_ASSERT(g_bserrno == 0);
4871 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4872 	snapshotid = g_blobid;
4873 
4874 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4875 	poll_threads();
4876 	CU_ASSERT(g_bserrno == 0);
4877 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4878 	snapshot = g_blob;
4879 	CU_ASSERT(snapshot->data_ro == true);
4880 	CU_ASSERT(snapshot->md_ro == true);
4881 
4882 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
4883 
4884 	write_bytes_start = g_dev_write_bytes;
4885 	read_bytes_start = g_dev_read_bytes;
4886 	copy_bytes_start = g_dev_copy_bytes;
4887 
4888 	memset(payload_write, 0xAA, sizeof(payload_write));
4889 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4890 	poll_threads();
4891 	CU_ASSERT(g_bserrno == 0);
4892 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4893 
4894 	/* For a clone we need to allocate and copy one cluster, update one page of metadata
4895 	 * and then write 10 pages of payload.
4896 	 */
4897 	write_bytes = g_dev_write_bytes - write_bytes_start;
4898 	read_bytes = g_dev_read_bytes - read_bytes_start;
4899 	copy_bytes = g_dev_copy_bytes - copy_bytes_start;
4900 	if (g_dev_copy_enabled) {
4901 		CU_ASSERT(copy_bytes == cluster_size);
4902 	} else {
4903 		CU_ASSERT(copy_bytes == 0);
4904 	}
4905 	if (g_use_extent_table) {
4906 		/* Add one more page for EXTENT_PAGE write */
4907 		CU_ASSERT(write_bytes + copy_bytes == page_size * 12 + cluster_size);
4908 	} else {
4909 		CU_ASSERT(write_bytes + copy_bytes == page_size * 11 + cluster_size);
4910 	}
4911 	CU_ASSERT(read_bytes + copy_bytes == cluster_size);
4912 
4913 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4914 	poll_threads();
4915 	CU_ASSERT(g_bserrno == 0);
4916 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4917 
4918 	/* Data on snapshot should not change after write to clone */
4919 	memset(payload_write, 0xE5, sizeof(payload_write));
4920 	spdk_blob_io_read(snapshot, channel, payload_read, 4, 10, blob_op_complete, NULL);
4921 	poll_threads();
4922 	CU_ASSERT(g_bserrno == 0);
4923 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4924 
4925 	ut_blob_close_and_delete(bs, blob);
4926 	ut_blob_close_and_delete(bs, snapshot);
4927 
4928 	spdk_bs_free_io_channel(channel);
4929 	poll_threads();
4930 	g_blob = NULL;
4931 	g_blobid = 0;
4932 }
4933 
4934 static void
4935 blob_snapshot_rw_iov(void)
4936 {
4937 	static const uint8_t zero[10 * 4096] = { 0 };
4938 	struct spdk_blob_store *bs = g_bs;
4939 	struct spdk_blob *blob, *snapshot;
4940 	struct spdk_io_channel *channel;
4941 	struct spdk_blob_opts opts;
4942 	spdk_blob_id blobid, snapshotid;
4943 	uint64_t free_clusters;
4944 	uint8_t payload_read[10 * 4096];
4945 	uint8_t payload_write[10 * 4096];
4946 	struct iovec iov_read[3];
4947 	struct iovec iov_write[3];
4948 
4949 	free_clusters = spdk_bs_free_cluster_count(bs);
4950 
4951 	channel = spdk_bs_alloc_io_channel(bs);
4952 	CU_ASSERT(channel != NULL);
4953 
4954 	ut_spdk_blob_opts_init(&opts);
4955 	opts.thin_provision = true;
4956 	opts.num_clusters = 5;
4957 
4958 	blob = ut_blob_create_and_open(bs, &opts);
4959 	blobid = spdk_blob_get_id(blob);
4960 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4961 
4962 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4963 
4964 	/* Create snapshot from blob */
4965 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4966 	poll_threads();
4967 	CU_ASSERT(g_bserrno == 0);
4968 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4969 	snapshotid = g_blobid;
4970 
4971 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4972 	poll_threads();
4973 	CU_ASSERT(g_bserrno == 0);
4974 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4975 	snapshot = g_blob;
4976 	CU_ASSERT(snapshot->data_ro == true);
4977 	CU_ASSERT(snapshot->md_ro == true);
4978 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
4979 
4980 	/* Payload should be all zeros from unallocated clusters */
4981 	memset(payload_read, 0xAA, sizeof(payload_read));
4982 	iov_read[0].iov_base = payload_read;
4983 	iov_read[0].iov_len = 3 * 4096;
4984 	iov_read[1].iov_base = payload_read + 3 * 4096;
4985 	iov_read[1].iov_len = 4 * 4096;
4986 	iov_read[2].iov_base = payload_read + 7 * 4096;
4987 	iov_read[2].iov_len = 3 * 4096;
4988 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4989 	poll_threads();
4990 	CU_ASSERT(g_bserrno == 0);
4991 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4992 
4993 	memset(payload_write, 0xE5, sizeof(payload_write));
4994 	iov_write[0].iov_base = payload_write;
4995 	iov_write[0].iov_len = 1 * 4096;
4996 	iov_write[1].iov_base = payload_write + 1 * 4096;
4997 	iov_write[1].iov_len = 5 * 4096;
4998 	iov_write[2].iov_base = payload_write + 6 * 4096;
4999 	iov_write[2].iov_len = 4 * 4096;
5000 
5001 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
5002 	poll_threads();
5003 	CU_ASSERT(g_bserrno == 0);
5004 
5005 	memset(payload_read, 0xAA, sizeof(payload_read));
5006 	iov_read[0].iov_base = payload_read;
5007 	iov_read[0].iov_len = 3 * 4096;
5008 	iov_read[1].iov_base = payload_read + 3 * 4096;
5009 	iov_read[1].iov_len = 4 * 4096;
5010 	iov_read[2].iov_base = payload_read + 7 * 4096;
5011 	iov_read[2].iov_len = 3 * 4096;
5012 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
5013 	poll_threads();
5014 	CU_ASSERT(g_bserrno == 0);
5015 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
5016 
5017 	spdk_bs_free_io_channel(channel);
5018 	poll_threads();
5019 
5020 	ut_blob_close_and_delete(bs, blob);
5021 	ut_blob_close_and_delete(bs, snapshot);
5022 }
5023 
5024 /**
5025  * Inflate / decouple parent rw unit tests.
5026  *
5027  * --------------
5028  * original blob:         0         1         2         3         4
5029  *                   ,---------+---------+---------+---------+---------.
5030  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
5031  *                   +---------+---------+---------+---------+---------+
5032  *         snapshot2 |    -    |yyyyyyyyy|    -    |yyyyyyyyy|    -    |
5033  *                   +---------+---------+---------+---------+---------+
5034  *         blob      |    -    |zzzzzzzzz|    -    |    -    |    -    |
5035  *                   '---------+---------+---------+---------+---------'
5036  *                   .         .         .         .         .         .
5037  * --------          .         .         .         .         .         .
5038  * inflate:          .         .         .         .         .         .
5039  *                   ,---------+---------+---------+---------+---------.
5040  *         blob      |xxxxxxxxx|zzzzzzzzz|xxxxxxxxx|yyyyyyyyy|000000000|
5041  *                   '---------+---------+---------+---------+---------'
5042  *
5043  *         NOTE: needs to allocate 4 clusters, thin provisioning removed, dependency
5044  *               on snapshot2 and snapshot removed .         .         .
5045  *                   .         .         .         .         .         .
5046  * ----------------  .         .         .         .         .         .
5047  * decouple parent:  .         .         .         .         .         .
5048  *                   ,---------+---------+---------+---------+---------.
5049  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
5050  *                   +---------+---------+---------+---------+---------+
5051  *         blob      |    -    |zzzzzzzzz|    -    |yyyyyyyyy|    -    |
5052  *                   '---------+---------+---------+---------+---------'
5053  *
5054  *         NOTE: needs to allocate 1 cluster, 3 clusters unallocated, dependency
5055  *               on snapshot2 removed and on snapshot still exists. Snapshot2
5056  *               should remain a clone of snapshot.
5057  */
5058 static void
5059 _blob_inflate_rw(bool decouple_parent)
5060 {
5061 	struct spdk_blob_store *bs = g_bs;
5062 	struct spdk_blob *blob, *snapshot, *snapshot2;
5063 	struct spdk_io_channel *channel;
5064 	struct spdk_blob_opts opts;
5065 	spdk_blob_id blobid, snapshotid, snapshot2id;
5066 	uint64_t free_clusters;
5067 	uint64_t cluster_size;
5068 
5069 	uint64_t payload_size;
5070 	uint8_t *payload_read;
5071 	uint8_t *payload_write;
5072 	uint8_t *payload_clone;
5073 
5074 	uint64_t pages_per_cluster;
5075 	uint64_t pages_per_payload;
5076 
5077 	int i;
5078 	spdk_blob_id ids[2];
5079 	size_t count;
5080 
5081 	free_clusters = spdk_bs_free_cluster_count(bs);
5082 	cluster_size = spdk_bs_get_cluster_size(bs);
5083 	pages_per_cluster = cluster_size / spdk_bs_get_page_size(bs);
5084 	pages_per_payload = pages_per_cluster * 5;
5085 
5086 	payload_size = cluster_size * 5;
5087 
5088 	payload_read = malloc(payload_size);
5089 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
5090 
5091 	payload_write = malloc(payload_size);
5092 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
5093 
5094 	payload_clone = malloc(payload_size);
5095 	SPDK_CU_ASSERT_FATAL(payload_clone != NULL);
5096 
5097 	channel = spdk_bs_alloc_io_channel(bs);
5098 	SPDK_CU_ASSERT_FATAL(channel != NULL);
5099 
5100 	/* Create blob */
5101 	ut_spdk_blob_opts_init(&opts);
5102 	opts.thin_provision = true;
5103 	opts.num_clusters = 5;
5104 
5105 	blob = ut_blob_create_and_open(bs, &opts);
5106 	blobid = spdk_blob_get_id(blob);
5107 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
5108 
5109 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
5110 
5111 	/* 1) Initial read should return zeroed payload */
5112 	memset(payload_read, 0xFF, payload_size);
5113 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5114 			  blob_op_complete, NULL);
5115 	poll_threads();
5116 	CU_ASSERT(g_bserrno == 0);
5117 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
5118 
5119 	/* Fill whole blob with a pattern, except last cluster (to be sure it
5120 	 * isn't allocated) */
5121 	memset(payload_write, 0xE5, payload_size - cluster_size);
5122 	spdk_blob_io_write(blob, channel, payload_write, 0, pages_per_payload -
5123 			   pages_per_cluster, blob_op_complete, NULL);
5124 	poll_threads();
5125 	CU_ASSERT(g_bserrno == 0);
5126 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
5127 
5128 	/* 2) Create snapshot from blob (first level) */
5129 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5130 	poll_threads();
5131 	CU_ASSERT(g_bserrno == 0);
5132 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5133 	snapshotid = g_blobid;
5134 
5135 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
5136 	poll_threads();
5137 	CU_ASSERT(g_bserrno == 0);
5138 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5139 	snapshot = g_blob;
5140 	CU_ASSERT(snapshot->data_ro == true);
5141 	CU_ASSERT(snapshot->md_ro == true);
5142 
5143 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
5144 
5145 	/* Write every second cluster with a pattern.
5146 	 *
5147 	 * Last cluster shouldn't be written, to be sure that snapshot nor clone
5148 	 * doesn't allocate it.
5149 	 *
5150 	 * payload_clone stores expected result on "blob" read at the time and
5151 	 * is used only to check data consistency on clone before and after
5152 	 * inflation. Initially we fill it with a backing snapshots pattern
5153 	 * used before.
5154 	 */
5155 	memset(payload_clone, 0xE5, payload_size - cluster_size);
5156 	memset(payload_clone + payload_size - cluster_size, 0x00, cluster_size);
5157 	memset(payload_write, 0xAA, payload_size);
5158 	for (i = 1; i < 5; i += 2) {
5159 		spdk_blob_io_write(blob, channel, payload_write, i * pages_per_cluster,
5160 				   pages_per_cluster, blob_op_complete, NULL);
5161 		poll_threads();
5162 		CU_ASSERT(g_bserrno == 0);
5163 
5164 		/* Update expected result */
5165 		memcpy(payload_clone + (cluster_size * i), payload_write,
5166 		       cluster_size);
5167 	}
5168 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
5169 
5170 	/* Check data consistency on clone */
5171 	memset(payload_read, 0xFF, payload_size);
5172 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5173 			  blob_op_complete, NULL);
5174 	poll_threads();
5175 	CU_ASSERT(g_bserrno == 0);
5176 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
5177 
5178 	/* 3) Create second levels snapshot from blob */
5179 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5180 	poll_threads();
5181 	CU_ASSERT(g_bserrno == 0);
5182 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5183 	snapshot2id = g_blobid;
5184 
5185 	spdk_bs_open_blob(bs, snapshot2id, blob_op_with_handle_complete, NULL);
5186 	poll_threads();
5187 	CU_ASSERT(g_bserrno == 0);
5188 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5189 	snapshot2 = g_blob;
5190 	CU_ASSERT(snapshot2->data_ro == true);
5191 	CU_ASSERT(snapshot2->md_ro == true);
5192 
5193 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 5);
5194 
5195 	CU_ASSERT(snapshot2->parent_id == snapshotid);
5196 
5197 	/* Write one cluster on the top level blob. This cluster (1) covers
5198 	 * already allocated cluster in the snapshot2, so shouldn't be inflated
5199 	 * at all */
5200 	spdk_blob_io_write(blob, channel, payload_write, pages_per_cluster,
5201 			   pages_per_cluster, blob_op_complete, NULL);
5202 	poll_threads();
5203 	CU_ASSERT(g_bserrno == 0);
5204 
5205 	/* Update expected result */
5206 	memcpy(payload_clone + cluster_size, payload_write, cluster_size);
5207 
5208 	/* Check data consistency on clone */
5209 	memset(payload_read, 0xFF, payload_size);
5210 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5211 			  blob_op_complete, NULL);
5212 	poll_threads();
5213 	CU_ASSERT(g_bserrno == 0);
5214 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
5215 
5216 
5217 	/* Close all blobs */
5218 	spdk_blob_close(blob, blob_op_complete, NULL);
5219 	poll_threads();
5220 	CU_ASSERT(g_bserrno == 0);
5221 
5222 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
5223 	poll_threads();
5224 	CU_ASSERT(g_bserrno == 0);
5225 
5226 	spdk_blob_close(snapshot, blob_op_complete, NULL);
5227 	poll_threads();
5228 	CU_ASSERT(g_bserrno == 0);
5229 
5230 	/* Check snapshot-clone relations */
5231 	count = 2;
5232 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
5233 	CU_ASSERT(count == 1);
5234 	CU_ASSERT(ids[0] == snapshot2id);
5235 
5236 	count = 2;
5237 	CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
5238 	CU_ASSERT(count == 1);
5239 	CU_ASSERT(ids[0] == blobid);
5240 
5241 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshot2id);
5242 
5243 	free_clusters = spdk_bs_free_cluster_count(bs);
5244 	if (!decouple_parent) {
5245 		/* Do full blob inflation */
5246 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
5247 		poll_threads();
5248 		CU_ASSERT(g_bserrno == 0);
5249 
5250 		/* All clusters should be inflated (except one already allocated
5251 		 * in a top level blob) */
5252 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 4);
5253 
5254 		/* Check if relation tree updated correctly */
5255 		count = 2;
5256 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
5257 
5258 		/* snapshotid have one clone */
5259 		CU_ASSERT(count == 1);
5260 		CU_ASSERT(ids[0] == snapshot2id);
5261 
5262 		/* snapshot2id have no clones */
5263 		count = 2;
5264 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
5265 		CU_ASSERT(count == 0);
5266 
5267 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
5268 	} else {
5269 		/* Decouple parent of blob */
5270 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
5271 		poll_threads();
5272 		CU_ASSERT(g_bserrno == 0);
5273 
5274 		/* Only one cluster from a parent should be inflated (second one
5275 		 * is covered by a cluster written on a top level blob, and
5276 		 * already allocated) */
5277 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 1);
5278 
5279 		/* Check if relation tree updated correctly */
5280 		count = 2;
5281 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
5282 
5283 		/* snapshotid have two clones now */
5284 		CU_ASSERT(count == 2);
5285 		CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5286 		CU_ASSERT(ids[0] == snapshot2id || ids[1] == snapshot2id);
5287 
5288 		/* snapshot2id have no clones */
5289 		count = 2;
5290 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
5291 		CU_ASSERT(count == 0);
5292 
5293 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5294 	}
5295 
5296 	/* Try to delete snapshot2 (should pass) */
5297 	spdk_bs_delete_blob(bs, snapshot2id, blob_op_complete, NULL);
5298 	poll_threads();
5299 	CU_ASSERT(g_bserrno == 0);
5300 
5301 	/* Try to delete base snapshot */
5302 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5303 	poll_threads();
5304 	CU_ASSERT(g_bserrno == 0);
5305 
5306 	/* Reopen blob after snapshot deletion */
5307 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
5308 	poll_threads();
5309 	CU_ASSERT(g_bserrno == 0);
5310 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5311 	blob = g_blob;
5312 
5313 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
5314 
5315 	/* Check data consistency on inflated blob */
5316 	memset(payload_read, 0xFF, payload_size);
5317 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5318 			  blob_op_complete, NULL);
5319 	poll_threads();
5320 	CU_ASSERT(g_bserrno == 0);
5321 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
5322 
5323 	spdk_bs_free_io_channel(channel);
5324 	poll_threads();
5325 
5326 	free(payload_read);
5327 	free(payload_write);
5328 	free(payload_clone);
5329 
5330 	ut_blob_close_and_delete(bs, blob);
5331 }
5332 
5333 static void
5334 blob_inflate_rw(void)
5335 {
5336 	_blob_inflate_rw(false);
5337 	_blob_inflate_rw(true);
5338 }
5339 
5340 /**
5341  * Snapshot-clones relation test
5342  *
5343  *         snapshot
5344  *            |
5345  *      +-----+-----+
5346  *      |           |
5347  *   blob(ro)   snapshot2
5348  *      |           |
5349  *   clone2      clone
5350  */
5351 static void
5352 blob_relations(void)
5353 {
5354 	struct spdk_blob_store *bs;
5355 	struct spdk_bs_dev *dev;
5356 	struct spdk_bs_opts bs_opts;
5357 	struct spdk_blob_opts opts;
5358 	struct spdk_blob *blob, *snapshot, *snapshot2, *clone, *clone2;
5359 	spdk_blob_id blobid, cloneid, snapshotid, cloneid2, snapshotid2;
5360 	int rc;
5361 	size_t count;
5362 	spdk_blob_id ids[10] = {};
5363 
5364 	dev = init_dev();
5365 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
5366 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
5367 
5368 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
5369 	poll_threads();
5370 	CU_ASSERT(g_bserrno == 0);
5371 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
5372 	bs = g_bs;
5373 
5374 	/* 1. Create blob with 10 clusters */
5375 
5376 	ut_spdk_blob_opts_init(&opts);
5377 	opts.num_clusters = 10;
5378 
5379 	blob = ut_blob_create_and_open(bs, &opts);
5380 	blobid = spdk_blob_get_id(blob);
5381 
5382 	CU_ASSERT(!spdk_blob_is_read_only(blob));
5383 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
5384 	CU_ASSERT(!spdk_blob_is_clone(blob));
5385 	CU_ASSERT(!spdk_blob_is_thin_provisioned(blob));
5386 
5387 	/* blob should not have underlying snapshot nor clones */
5388 	CU_ASSERT(blob->parent_id == SPDK_BLOBID_INVALID);
5389 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
5390 	count = SPDK_COUNTOF(ids);
5391 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5392 	CU_ASSERT(rc == 0);
5393 	CU_ASSERT(count == 0);
5394 
5395 
5396 	/* 2. Create snapshot */
5397 
5398 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5399 	poll_threads();
5400 	CU_ASSERT(g_bserrno == 0);
5401 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5402 	snapshotid = g_blobid;
5403 
5404 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
5405 	poll_threads();
5406 	CU_ASSERT(g_bserrno == 0);
5407 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5408 	snapshot = g_blob;
5409 
5410 	CU_ASSERT(spdk_blob_is_read_only(snapshot));
5411 	CU_ASSERT(spdk_blob_is_snapshot(snapshot));
5412 	CU_ASSERT(!spdk_blob_is_clone(snapshot));
5413 	CU_ASSERT(snapshot->parent_id == SPDK_BLOBID_INVALID);
5414 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
5415 
5416 	/* Check if original blob is converted to the clone of snapshot */
5417 	CU_ASSERT(!spdk_blob_is_read_only(blob));
5418 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
5419 	CU_ASSERT(spdk_blob_is_clone(blob));
5420 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
5421 	CU_ASSERT(blob->parent_id == snapshotid);
5422 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5423 
5424 	count = SPDK_COUNTOF(ids);
5425 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5426 	CU_ASSERT(rc == 0);
5427 	CU_ASSERT(count == 1);
5428 	CU_ASSERT(ids[0] == blobid);
5429 
5430 
5431 	/* 3. Create clone from snapshot */
5432 
5433 	spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
5434 	poll_threads();
5435 	CU_ASSERT(g_bserrno == 0);
5436 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5437 	cloneid = g_blobid;
5438 
5439 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
5440 	poll_threads();
5441 	CU_ASSERT(g_bserrno == 0);
5442 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5443 	clone = g_blob;
5444 
5445 	CU_ASSERT(!spdk_blob_is_read_only(clone));
5446 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
5447 	CU_ASSERT(spdk_blob_is_clone(clone));
5448 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
5449 	CU_ASSERT(clone->parent_id == snapshotid);
5450 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid);
5451 
5452 	count = SPDK_COUNTOF(ids);
5453 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5454 	CU_ASSERT(rc == 0);
5455 	CU_ASSERT(count == 0);
5456 
5457 	/* Check if clone is on the snapshot's list */
5458 	count = SPDK_COUNTOF(ids);
5459 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5460 	CU_ASSERT(rc == 0);
5461 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5462 	CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid);
5463 
5464 
5465 	/* 4. Create snapshot of the clone */
5466 
5467 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
5468 	poll_threads();
5469 	CU_ASSERT(g_bserrno == 0);
5470 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5471 	snapshotid2 = g_blobid;
5472 
5473 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
5474 	poll_threads();
5475 	CU_ASSERT(g_bserrno == 0);
5476 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5477 	snapshot2 = g_blob;
5478 
5479 	CU_ASSERT(spdk_blob_is_read_only(snapshot2));
5480 	CU_ASSERT(spdk_blob_is_snapshot(snapshot2));
5481 	CU_ASSERT(spdk_blob_is_clone(snapshot2));
5482 	CU_ASSERT(snapshot2->parent_id == snapshotid);
5483 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
5484 
5485 	/* Check if clone is converted to the clone of snapshot2 and snapshot2
5486 	 * is a child of snapshot */
5487 	CU_ASSERT(!spdk_blob_is_read_only(clone));
5488 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
5489 	CU_ASSERT(spdk_blob_is_clone(clone));
5490 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
5491 	CU_ASSERT(clone->parent_id == snapshotid2);
5492 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5493 
5494 	count = SPDK_COUNTOF(ids);
5495 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5496 	CU_ASSERT(rc == 0);
5497 	CU_ASSERT(count == 1);
5498 	CU_ASSERT(ids[0] == cloneid);
5499 
5500 
5501 	/* 5. Try to create clone from read only blob */
5502 
5503 	/* Mark blob as read only */
5504 	spdk_blob_set_read_only(blob);
5505 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
5506 	poll_threads();
5507 	CU_ASSERT(g_bserrno == 0);
5508 
5509 	/* Check if previously created blob is read only clone */
5510 	CU_ASSERT(spdk_blob_is_read_only(blob));
5511 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
5512 	CU_ASSERT(spdk_blob_is_clone(blob));
5513 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
5514 
5515 	/* Create clone from read only blob */
5516 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5517 	poll_threads();
5518 	CU_ASSERT(g_bserrno == 0);
5519 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5520 	cloneid2 = g_blobid;
5521 
5522 	spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL);
5523 	poll_threads();
5524 	CU_ASSERT(g_bserrno == 0);
5525 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5526 	clone2 = g_blob;
5527 
5528 	CU_ASSERT(!spdk_blob_is_read_only(clone2));
5529 	CU_ASSERT(!spdk_blob_is_snapshot(clone2));
5530 	CU_ASSERT(spdk_blob_is_clone(clone2));
5531 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone2));
5532 
5533 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5534 
5535 	count = SPDK_COUNTOF(ids);
5536 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5537 	CU_ASSERT(rc == 0);
5538 
5539 	CU_ASSERT(count == 1);
5540 	CU_ASSERT(ids[0] == cloneid2);
5541 
5542 	/* Close blobs */
5543 
5544 	spdk_blob_close(clone2, blob_op_complete, NULL);
5545 	poll_threads();
5546 	CU_ASSERT(g_bserrno == 0);
5547 
5548 	spdk_blob_close(blob, blob_op_complete, NULL);
5549 	poll_threads();
5550 	CU_ASSERT(g_bserrno == 0);
5551 
5552 	spdk_blob_close(clone, blob_op_complete, NULL);
5553 	poll_threads();
5554 	CU_ASSERT(g_bserrno == 0);
5555 
5556 	spdk_blob_close(snapshot, blob_op_complete, NULL);
5557 	poll_threads();
5558 	CU_ASSERT(g_bserrno == 0);
5559 
5560 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
5561 	poll_threads();
5562 	CU_ASSERT(g_bserrno == 0);
5563 
5564 	/* Try to delete snapshot with more than 1 clone */
5565 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5566 	poll_threads();
5567 	CU_ASSERT(g_bserrno != 0);
5568 
5569 	ut_bs_reload(&bs, &bs_opts);
5570 
5571 	/* NULL ids array should return number of clones in count */
5572 	count = SPDK_COUNTOF(ids);
5573 	rc = spdk_blob_get_clones(bs, snapshotid, NULL, &count);
5574 	CU_ASSERT(rc == -ENOMEM);
5575 	CU_ASSERT(count == 2);
5576 
5577 	/* incorrect array size */
5578 	count = 1;
5579 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5580 	CU_ASSERT(rc == -ENOMEM);
5581 	CU_ASSERT(count == 2);
5582 
5583 
5584 	/* Verify structure of loaded blob store */
5585 
5586 	/* snapshot */
5587 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
5588 
5589 	count = SPDK_COUNTOF(ids);
5590 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5591 	CU_ASSERT(rc == 0);
5592 	CU_ASSERT(count == 2);
5593 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5594 	CU_ASSERT(ids[0] == snapshotid2 || ids[1] == snapshotid2);
5595 
5596 	/* blob */
5597 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5598 	count = SPDK_COUNTOF(ids);
5599 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5600 	CU_ASSERT(rc == 0);
5601 	CU_ASSERT(count == 1);
5602 	CU_ASSERT(ids[0] == cloneid2);
5603 
5604 	/* clone */
5605 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5606 	count = SPDK_COUNTOF(ids);
5607 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5608 	CU_ASSERT(rc == 0);
5609 	CU_ASSERT(count == 0);
5610 
5611 	/* snapshot2 */
5612 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
5613 	count = SPDK_COUNTOF(ids);
5614 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5615 	CU_ASSERT(rc == 0);
5616 	CU_ASSERT(count == 1);
5617 	CU_ASSERT(ids[0] == cloneid);
5618 
5619 	/* clone2 */
5620 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5621 	count = SPDK_COUNTOF(ids);
5622 	rc = spdk_blob_get_clones(bs, cloneid2, ids, &count);
5623 	CU_ASSERT(rc == 0);
5624 	CU_ASSERT(count == 0);
5625 
5626 	/* Try to delete blob that user should not be able to remove */
5627 
5628 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5629 	poll_threads();
5630 	CU_ASSERT(g_bserrno != 0);
5631 
5632 	/* Remove all blobs */
5633 
5634 	spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL);
5635 	poll_threads();
5636 	CU_ASSERT(g_bserrno == 0);
5637 
5638 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
5639 	poll_threads();
5640 	CU_ASSERT(g_bserrno == 0);
5641 
5642 	spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL);
5643 	poll_threads();
5644 	CU_ASSERT(g_bserrno == 0);
5645 
5646 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
5647 	poll_threads();
5648 	CU_ASSERT(g_bserrno == 0);
5649 
5650 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5651 	poll_threads();
5652 	CU_ASSERT(g_bserrno == 0);
5653 
5654 	spdk_bs_unload(bs, bs_op_complete, NULL);
5655 	poll_threads();
5656 	CU_ASSERT(g_bserrno == 0);
5657 
5658 	g_bs = NULL;
5659 }
5660 
5661 /**
5662  * Snapshot-clones relation test 2
5663  *
5664  *         snapshot1
5665  *            |
5666  *         snapshot2
5667  *            |
5668  *      +-----+-----+
5669  *      |           |
5670  *   blob(ro)   snapshot3
5671  *      |           |
5672  *      |       snapshot4
5673  *      |        |     |
5674  *   clone2   clone  clone3
5675  */
5676 static void
5677 blob_relations2(void)
5678 {
5679 	struct spdk_blob_store *bs;
5680 	struct spdk_bs_dev *dev;
5681 	struct spdk_bs_opts bs_opts;
5682 	struct spdk_blob_opts opts;
5683 	struct spdk_blob *blob, *snapshot1, *snapshot2, *snapshot3, *snapshot4, *clone, *clone2;
5684 	spdk_blob_id blobid, snapshotid1, snapshotid2, snapshotid3, snapshotid4, cloneid, cloneid2,
5685 		     cloneid3;
5686 	int rc;
5687 	size_t count;
5688 	spdk_blob_id ids[10] = {};
5689 
5690 	dev = init_dev();
5691 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
5692 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
5693 
5694 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
5695 	poll_threads();
5696 	CU_ASSERT(g_bserrno == 0);
5697 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
5698 	bs = g_bs;
5699 
5700 	/* 1. Create blob with 10 clusters */
5701 
5702 	ut_spdk_blob_opts_init(&opts);
5703 	opts.num_clusters = 10;
5704 
5705 	blob = ut_blob_create_and_open(bs, &opts);
5706 	blobid = spdk_blob_get_id(blob);
5707 
5708 	/* 2. Create snapshot1 */
5709 
5710 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5711 	poll_threads();
5712 	CU_ASSERT(g_bserrno == 0);
5713 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5714 	snapshotid1 = g_blobid;
5715 
5716 	spdk_bs_open_blob(bs, snapshotid1, blob_op_with_handle_complete, NULL);
5717 	poll_threads();
5718 	CU_ASSERT(g_bserrno == 0);
5719 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5720 	snapshot1 = g_blob;
5721 
5722 	CU_ASSERT(snapshot1->parent_id == SPDK_BLOBID_INVALID);
5723 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid1) == SPDK_BLOBID_INVALID);
5724 
5725 	CU_ASSERT(blob->parent_id == snapshotid1);
5726 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid1);
5727 
5728 	/* Check if blob is the clone of snapshot1 */
5729 	CU_ASSERT(blob->parent_id == snapshotid1);
5730 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid1);
5731 
5732 	count = SPDK_COUNTOF(ids);
5733 	rc = spdk_blob_get_clones(bs, snapshotid1, ids, &count);
5734 	CU_ASSERT(rc == 0);
5735 	CU_ASSERT(count == 1);
5736 	CU_ASSERT(ids[0] == blobid);
5737 
5738 	/* 3. Create another snapshot */
5739 
5740 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5741 	poll_threads();
5742 	CU_ASSERT(g_bserrno == 0);
5743 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5744 	snapshotid2 = g_blobid;
5745 
5746 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
5747 	poll_threads();
5748 	CU_ASSERT(g_bserrno == 0);
5749 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5750 	snapshot2 = g_blob;
5751 
5752 	CU_ASSERT(spdk_blob_is_clone(snapshot2));
5753 	CU_ASSERT(snapshot2->parent_id == snapshotid1);
5754 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid1);
5755 
5756 	/* Check if snapshot2 is the clone of snapshot1 and blob
5757 	 * is a child of snapshot2 */
5758 	CU_ASSERT(blob->parent_id == snapshotid2);
5759 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid2);
5760 
5761 	count = SPDK_COUNTOF(ids);
5762 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5763 	CU_ASSERT(rc == 0);
5764 	CU_ASSERT(count == 1);
5765 	CU_ASSERT(ids[0] == blobid);
5766 
5767 	/* 4. Create clone from snapshot */
5768 
5769 	spdk_bs_create_clone(bs, snapshotid2, NULL, blob_op_with_id_complete, NULL);
5770 	poll_threads();
5771 	CU_ASSERT(g_bserrno == 0);
5772 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5773 	cloneid = g_blobid;
5774 
5775 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
5776 	poll_threads();
5777 	CU_ASSERT(g_bserrno == 0);
5778 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5779 	clone = g_blob;
5780 
5781 	CU_ASSERT(clone->parent_id == snapshotid2);
5782 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5783 
5784 	/* Check if clone is on the snapshot's list */
5785 	count = SPDK_COUNTOF(ids);
5786 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5787 	CU_ASSERT(rc == 0);
5788 	CU_ASSERT(count == 2);
5789 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5790 	CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid);
5791 
5792 	/* 5. Create snapshot of the clone */
5793 
5794 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
5795 	poll_threads();
5796 	CU_ASSERT(g_bserrno == 0);
5797 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5798 	snapshotid3 = g_blobid;
5799 
5800 	spdk_bs_open_blob(bs, snapshotid3, blob_op_with_handle_complete, NULL);
5801 	poll_threads();
5802 	CU_ASSERT(g_bserrno == 0);
5803 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5804 	snapshot3 = g_blob;
5805 
5806 	CU_ASSERT(snapshot3->parent_id == snapshotid2);
5807 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid3) == snapshotid2);
5808 
5809 	/* Check if clone is converted to the clone of snapshot3 and snapshot3
5810 	 * is a child of snapshot2 */
5811 	CU_ASSERT(clone->parent_id == snapshotid3);
5812 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid3);
5813 
5814 	count = SPDK_COUNTOF(ids);
5815 	rc = spdk_blob_get_clones(bs, snapshotid3, ids, &count);
5816 	CU_ASSERT(rc == 0);
5817 	CU_ASSERT(count == 1);
5818 	CU_ASSERT(ids[0] == cloneid);
5819 
5820 	/* 6. Create another snapshot of the clone */
5821 
5822 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
5823 	poll_threads();
5824 	CU_ASSERT(g_bserrno == 0);
5825 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5826 	snapshotid4 = g_blobid;
5827 
5828 	spdk_bs_open_blob(bs, snapshotid4, blob_op_with_handle_complete, NULL);
5829 	poll_threads();
5830 	CU_ASSERT(g_bserrno == 0);
5831 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5832 	snapshot4 = g_blob;
5833 
5834 	CU_ASSERT(snapshot4->parent_id == snapshotid3);
5835 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid4) == snapshotid3);
5836 
5837 	/* Check if clone is converted to the clone of snapshot4 and snapshot4
5838 	 * is a child of snapshot3 */
5839 	CU_ASSERT(clone->parent_id == snapshotid4);
5840 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid4);
5841 
5842 	count = SPDK_COUNTOF(ids);
5843 	rc = spdk_blob_get_clones(bs, snapshotid4, ids, &count);
5844 	CU_ASSERT(rc == 0);
5845 	CU_ASSERT(count == 1);
5846 	CU_ASSERT(ids[0] == cloneid);
5847 
5848 	/* 7. Remove snapshot 4 */
5849 
5850 	ut_blob_close_and_delete(bs, snapshot4);
5851 
5852 	/* Check if relations are back to state from before creating snapshot 4 */
5853 	CU_ASSERT(clone->parent_id == snapshotid3);
5854 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid3);
5855 
5856 	count = SPDK_COUNTOF(ids);
5857 	rc = spdk_blob_get_clones(bs, snapshotid3, ids, &count);
5858 	CU_ASSERT(rc == 0);
5859 	CU_ASSERT(count == 1);
5860 	CU_ASSERT(ids[0] == cloneid);
5861 
5862 	/* 8. Create second clone of snapshot 3 and try to remove snapshot 3 */
5863 
5864 	spdk_bs_create_clone(bs, snapshotid3, NULL, blob_op_with_id_complete, NULL);
5865 	poll_threads();
5866 	CU_ASSERT(g_bserrno == 0);
5867 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5868 	cloneid3 = g_blobid;
5869 
5870 	spdk_bs_delete_blob(bs, snapshotid3, blob_op_complete, NULL);
5871 	poll_threads();
5872 	CU_ASSERT(g_bserrno != 0);
5873 
5874 	/* 9. Open snapshot 3 again and try to remove it while clone 3 is closed */
5875 
5876 	spdk_bs_open_blob(bs, snapshotid3, blob_op_with_handle_complete, NULL);
5877 	poll_threads();
5878 	CU_ASSERT(g_bserrno == 0);
5879 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5880 	snapshot3 = g_blob;
5881 
5882 	spdk_bs_delete_blob(bs, snapshotid3, blob_op_complete, NULL);
5883 	poll_threads();
5884 	CU_ASSERT(g_bserrno != 0);
5885 
5886 	spdk_blob_close(snapshot3, blob_op_complete, NULL);
5887 	poll_threads();
5888 	CU_ASSERT(g_bserrno == 0);
5889 
5890 	spdk_bs_delete_blob(bs, cloneid3, blob_op_complete, NULL);
5891 	poll_threads();
5892 	CU_ASSERT(g_bserrno == 0);
5893 
5894 	/* 10. Remove snapshot 1 */
5895 
5896 	ut_blob_close_and_delete(bs, snapshot1);
5897 
5898 	/* Check if relations are back to state from before creating snapshot 4 (before step 6) */
5899 	CU_ASSERT(snapshot2->parent_id == SPDK_BLOBID_INVALID);
5900 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == SPDK_BLOBID_INVALID);
5901 
5902 	count = SPDK_COUNTOF(ids);
5903 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5904 	CU_ASSERT(rc == 0);
5905 	CU_ASSERT(count == 2);
5906 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5907 	CU_ASSERT(ids[0] == snapshotid3 || ids[1] == snapshotid3);
5908 
5909 	/* 11. Try to create clone from read only blob */
5910 
5911 	/* Mark blob as read only */
5912 	spdk_blob_set_read_only(blob);
5913 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
5914 	poll_threads();
5915 	CU_ASSERT(g_bserrno == 0);
5916 
5917 	/* Create clone from read only blob */
5918 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5919 	poll_threads();
5920 	CU_ASSERT(g_bserrno == 0);
5921 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5922 	cloneid2 = g_blobid;
5923 
5924 	spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL);
5925 	poll_threads();
5926 	CU_ASSERT(g_bserrno == 0);
5927 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5928 	clone2 = g_blob;
5929 
5930 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5931 
5932 	count = SPDK_COUNTOF(ids);
5933 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5934 	CU_ASSERT(rc == 0);
5935 	CU_ASSERT(count == 1);
5936 	CU_ASSERT(ids[0] == cloneid2);
5937 
5938 	/* Close blobs */
5939 
5940 	spdk_blob_close(clone2, blob_op_complete, NULL);
5941 	poll_threads();
5942 	CU_ASSERT(g_bserrno == 0);
5943 
5944 	spdk_blob_close(blob, blob_op_complete, NULL);
5945 	poll_threads();
5946 	CU_ASSERT(g_bserrno == 0);
5947 
5948 	spdk_blob_close(clone, blob_op_complete, NULL);
5949 	poll_threads();
5950 	CU_ASSERT(g_bserrno == 0);
5951 
5952 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
5953 	poll_threads();
5954 	CU_ASSERT(g_bserrno == 0);
5955 
5956 	spdk_blob_close(snapshot3, blob_op_complete, NULL);
5957 	poll_threads();
5958 	CU_ASSERT(g_bserrno == 0);
5959 
5960 	ut_bs_reload(&bs, &bs_opts);
5961 
5962 	/* Verify structure of loaded blob store */
5963 
5964 	/* snapshot2 */
5965 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == SPDK_BLOBID_INVALID);
5966 
5967 	count = SPDK_COUNTOF(ids);
5968 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5969 	CU_ASSERT(rc == 0);
5970 	CU_ASSERT(count == 2);
5971 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5972 	CU_ASSERT(ids[0] == snapshotid3 || ids[1] == snapshotid3);
5973 
5974 	/* blob */
5975 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid2);
5976 	count = SPDK_COUNTOF(ids);
5977 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5978 	CU_ASSERT(rc == 0);
5979 	CU_ASSERT(count == 1);
5980 	CU_ASSERT(ids[0] == cloneid2);
5981 
5982 	/* clone */
5983 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid3);
5984 	count = SPDK_COUNTOF(ids);
5985 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5986 	CU_ASSERT(rc == 0);
5987 	CU_ASSERT(count == 0);
5988 
5989 	/* snapshot3 */
5990 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid3) == snapshotid2);
5991 	count = SPDK_COUNTOF(ids);
5992 	rc = spdk_blob_get_clones(bs, snapshotid3, ids, &count);
5993 	CU_ASSERT(rc == 0);
5994 	CU_ASSERT(count == 1);
5995 	CU_ASSERT(ids[0] == cloneid);
5996 
5997 	/* clone2 */
5998 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5999 	count = SPDK_COUNTOF(ids);
6000 	rc = spdk_blob_get_clones(bs, cloneid2, ids, &count);
6001 	CU_ASSERT(rc == 0);
6002 	CU_ASSERT(count == 0);
6003 
6004 	/* Try to delete all blobs in the worse possible order */
6005 
6006 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6007 	poll_threads();
6008 	CU_ASSERT(g_bserrno != 0);
6009 
6010 	spdk_bs_delete_blob(bs, snapshotid3, blob_op_complete, NULL);
6011 	poll_threads();
6012 	CU_ASSERT(g_bserrno == 0);
6013 
6014 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6015 	poll_threads();
6016 	CU_ASSERT(g_bserrno != 0);
6017 
6018 	spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL);
6019 	poll_threads();
6020 	CU_ASSERT(g_bserrno == 0);
6021 
6022 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6023 	poll_threads();
6024 	CU_ASSERT(g_bserrno == 0);
6025 
6026 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
6027 	poll_threads();
6028 	CU_ASSERT(g_bserrno == 0);
6029 
6030 	spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL);
6031 	poll_threads();
6032 	CU_ASSERT(g_bserrno == 0);
6033 
6034 	spdk_bs_unload(bs, bs_op_complete, NULL);
6035 	poll_threads();
6036 	CU_ASSERT(g_bserrno == 0);
6037 
6038 	g_bs = NULL;
6039 }
6040 
6041 /**
6042  * Snapshot-clones relation test 3
6043  *
6044  *         snapshot0
6045  *            |
6046  *         snapshot1
6047  *            |
6048  *         snapshot2
6049  *            |
6050  *           blob
6051  */
6052 static void
6053 blob_relations3(void)
6054 {
6055 	struct spdk_blob_store *bs;
6056 	struct spdk_bs_dev *dev;
6057 	struct spdk_io_channel *channel;
6058 	struct spdk_bs_opts bs_opts;
6059 	struct spdk_blob_opts opts;
6060 	struct spdk_blob *blob;
6061 	spdk_blob_id blobid, snapshotid0, snapshotid1, snapshotid2;
6062 
6063 	dev = init_dev();
6064 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
6065 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
6066 
6067 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
6068 	poll_threads();
6069 	CU_ASSERT(g_bserrno == 0);
6070 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6071 	bs = g_bs;
6072 
6073 	channel = spdk_bs_alloc_io_channel(bs);
6074 	SPDK_CU_ASSERT_FATAL(channel != NULL);
6075 
6076 	/* 1. Create blob with 10 clusters */
6077 	ut_spdk_blob_opts_init(&opts);
6078 	opts.num_clusters = 10;
6079 
6080 	blob = ut_blob_create_and_open(bs, &opts);
6081 	blobid = spdk_blob_get_id(blob);
6082 
6083 	/* 2. Create snapshot0 */
6084 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6085 	poll_threads();
6086 	CU_ASSERT(g_bserrno == 0);
6087 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6088 	snapshotid0 = g_blobid;
6089 
6090 	/* 3. Create snapshot1 */
6091 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6092 	poll_threads();
6093 	CU_ASSERT(g_bserrno == 0);
6094 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6095 	snapshotid1 = g_blobid;
6096 
6097 	/* 4. Create snapshot2 */
6098 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6099 	poll_threads();
6100 	CU_ASSERT(g_bserrno == 0);
6101 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6102 	snapshotid2 = g_blobid;
6103 
6104 	/* 5. Decouple blob */
6105 	spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
6106 	poll_threads();
6107 	CU_ASSERT(g_bserrno == 0);
6108 
6109 	/* 6. Decouple snapshot2. Make sure updating md of snapshot2 is possible */
6110 	spdk_bs_blob_decouple_parent(bs, channel, snapshotid2, blob_op_complete, NULL);
6111 	poll_threads();
6112 	CU_ASSERT(g_bserrno == 0);
6113 
6114 	/* 7. Delete blob */
6115 	spdk_blob_close(blob, blob_op_complete, NULL);
6116 	poll_threads();
6117 	CU_ASSERT(g_bserrno == 0);
6118 
6119 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
6120 	poll_threads();
6121 	CU_ASSERT(g_bserrno == 0);
6122 
6123 	/* 8. Delete snapshot2.
6124 	 * If md of snapshot 2 was updated, it should be possible to delete it */
6125 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6126 	poll_threads();
6127 	CU_ASSERT(g_bserrno == 0);
6128 
6129 	/* Remove remaining blobs and unload bs */
6130 	spdk_bs_delete_blob(bs, snapshotid1, blob_op_complete, NULL);
6131 	poll_threads();
6132 	CU_ASSERT(g_bserrno == 0);
6133 
6134 	spdk_bs_delete_blob(bs, snapshotid0, blob_op_complete, NULL);
6135 	poll_threads();
6136 	CU_ASSERT(g_bserrno == 0);
6137 
6138 	spdk_bs_free_io_channel(channel);
6139 	poll_threads();
6140 
6141 	spdk_bs_unload(bs, bs_op_complete, NULL);
6142 	poll_threads();
6143 	CU_ASSERT(g_bserrno == 0);
6144 
6145 	g_bs = NULL;
6146 }
6147 
6148 static void
6149 blobstore_clean_power_failure(void)
6150 {
6151 	struct spdk_blob_store *bs;
6152 	struct spdk_blob *blob;
6153 	struct spdk_power_failure_thresholds thresholds = {};
6154 	bool clean = false;
6155 	struct spdk_bs_super_block *super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
6156 	struct spdk_bs_super_block super_copy = {};
6157 
6158 	thresholds.general_threshold = 1;
6159 	while (!clean) {
6160 		/* Create bs and blob */
6161 		suite_blob_setup();
6162 		SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6163 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6164 		bs = g_bs;
6165 		blob = g_blob;
6166 
6167 		/* Super block should not change for rest of the UT,
6168 		 * save it and compare later. */
6169 		memcpy(&super_copy, super, sizeof(struct spdk_bs_super_block));
6170 		SPDK_CU_ASSERT_FATAL(super->clean == 0);
6171 		SPDK_CU_ASSERT_FATAL(bs->clean == 0);
6172 
6173 		/* Force bs/super block in a clean state.
6174 		 * Along with marking blob dirty, to cause blob persist. */
6175 		blob->state = SPDK_BLOB_STATE_DIRTY;
6176 		bs->clean = 1;
6177 		super->clean = 1;
6178 		super->crc = blob_md_page_calc_crc(super);
6179 
6180 		g_bserrno = -1;
6181 		dev_set_power_failure_thresholds(thresholds);
6182 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
6183 		poll_threads();
6184 		dev_reset_power_failure_event();
6185 
6186 		if (g_bserrno == 0) {
6187 			/* After successful md sync, both bs and super block
6188 			 * should be marked as not clean. */
6189 			SPDK_CU_ASSERT_FATAL(bs->clean == 0);
6190 			SPDK_CU_ASSERT_FATAL(super->clean == 0);
6191 			clean = true;
6192 		}
6193 
6194 		/* Depending on the point of failure, super block was either updated or not. */
6195 		super_copy.clean = super->clean;
6196 		super_copy.crc = blob_md_page_calc_crc(&super_copy);
6197 		/* Compare that the values in super block remained unchanged. */
6198 		SPDK_CU_ASSERT_FATAL(!memcmp(&super_copy, super, sizeof(struct spdk_bs_super_block)));
6199 
6200 		/* Delete blob and unload bs */
6201 		suite_blob_cleanup();
6202 
6203 		thresholds.general_threshold++;
6204 	}
6205 }
6206 
6207 static void
6208 blob_delete_snapshot_power_failure(void)
6209 {
6210 	struct spdk_bs_dev *dev;
6211 	struct spdk_blob_store *bs;
6212 	struct spdk_blob_opts opts;
6213 	struct spdk_blob *blob, *snapshot;
6214 	struct spdk_power_failure_thresholds thresholds = {};
6215 	spdk_blob_id blobid, snapshotid;
6216 	const void *value;
6217 	size_t value_len;
6218 	size_t count;
6219 	spdk_blob_id ids[3] = {};
6220 	int rc;
6221 	bool deleted = false;
6222 	int delete_snapshot_bserrno = -1;
6223 
6224 	thresholds.general_threshold = 1;
6225 	while (!deleted) {
6226 		dev = init_dev();
6227 
6228 		spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
6229 		poll_threads();
6230 		CU_ASSERT(g_bserrno == 0);
6231 		SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6232 		bs = g_bs;
6233 
6234 		/* Create blob */
6235 		ut_spdk_blob_opts_init(&opts);
6236 		opts.num_clusters = 10;
6237 
6238 		spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
6239 		poll_threads();
6240 		CU_ASSERT(g_bserrno == 0);
6241 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6242 		blobid = g_blobid;
6243 
6244 		/* Create snapshot */
6245 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6246 		poll_threads();
6247 		CU_ASSERT(g_bserrno == 0);
6248 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6249 		snapshotid = g_blobid;
6250 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6251 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6252 
6253 		dev_set_power_failure_thresholds(thresholds);
6254 
6255 		spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
6256 		poll_threads();
6257 		delete_snapshot_bserrno = g_bserrno;
6258 
6259 		/* Do not shut down cleanly. Assumption is that after snapshot deletion
6260 		 * reports success, changes to both blobs should already persisted. */
6261 		dev_reset_power_failure_event();
6262 		ut_bs_dirty_load(&bs, NULL);
6263 
6264 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6265 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6266 
6267 		spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
6268 		poll_threads();
6269 		CU_ASSERT(g_bserrno == 0);
6270 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6271 		blob = g_blob;
6272 		SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(blob) == true);
6273 
6274 		spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
6275 		poll_threads();
6276 
6277 		if (g_bserrno == 0) {
6278 			SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6279 			snapshot = g_blob;
6280 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
6281 			count = SPDK_COUNTOF(ids);
6282 			rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
6283 			CU_ASSERT(rc == 0);
6284 			CU_ASSERT(count == 1);
6285 			CU_ASSERT(ids[0] == blobid);
6286 			rc = spdk_blob_get_xattr_value(snapshot, SNAPSHOT_PENDING_REMOVAL, &value, &value_len);
6287 			CU_ASSERT(rc != 0);
6288 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(snapshot) == false);
6289 
6290 			spdk_blob_close(snapshot, blob_op_complete, NULL);
6291 			poll_threads();
6292 			CU_ASSERT(g_bserrno == 0);
6293 		} else {
6294 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
6295 			/* Snapshot might have been left in unrecoverable state, so it does not open.
6296 			 * Yet delete might perform further changes to the clone after that.
6297 			 * This UT should test until snapshot is deleted and delete call succeeds. */
6298 			if (delete_snapshot_bserrno == 0) {
6299 				deleted = true;
6300 			}
6301 		}
6302 
6303 		spdk_blob_close(blob, blob_op_complete, NULL);
6304 		poll_threads();
6305 		CU_ASSERT(g_bserrno == 0);
6306 
6307 		spdk_bs_unload(bs, bs_op_complete, NULL);
6308 		poll_threads();
6309 		CU_ASSERT(g_bserrno == 0);
6310 
6311 		thresholds.general_threshold++;
6312 	}
6313 }
6314 
6315 static void
6316 blob_create_snapshot_power_failure(void)
6317 {
6318 	struct spdk_blob_store *bs = g_bs;
6319 	struct spdk_bs_dev *dev;
6320 	struct spdk_blob_opts opts;
6321 	struct spdk_blob *blob, *snapshot;
6322 	struct spdk_power_failure_thresholds thresholds = {};
6323 	spdk_blob_id blobid, snapshotid;
6324 	const void *value;
6325 	size_t value_len;
6326 	size_t count;
6327 	spdk_blob_id ids[3] = {};
6328 	int rc;
6329 	bool created = false;
6330 	int create_snapshot_bserrno = -1;
6331 
6332 	thresholds.general_threshold = 1;
6333 	while (!created) {
6334 		dev = init_dev();
6335 
6336 		spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
6337 		poll_threads();
6338 		CU_ASSERT(g_bserrno == 0);
6339 		SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6340 		bs = g_bs;
6341 
6342 		/* Create blob */
6343 		ut_spdk_blob_opts_init(&opts);
6344 		opts.num_clusters = 10;
6345 
6346 		spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
6347 		poll_threads();
6348 		CU_ASSERT(g_bserrno == 0);
6349 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6350 		blobid = g_blobid;
6351 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6352 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6353 
6354 		dev_set_power_failure_thresholds(thresholds);
6355 
6356 		/* Create snapshot */
6357 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6358 		poll_threads();
6359 		create_snapshot_bserrno = g_bserrno;
6360 		snapshotid = g_blobid;
6361 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6362 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6363 
6364 		/* Do not shut down cleanly. Assumption is that after create snapshot
6365 		 * reports success, both blobs should be power-fail safe. */
6366 		dev_reset_power_failure_event();
6367 		ut_bs_dirty_load(&bs, NULL);
6368 
6369 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6370 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6371 
6372 		spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
6373 		poll_threads();
6374 		CU_ASSERT(g_bserrno == 0);
6375 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6376 		blob = g_blob;
6377 
6378 		if (snapshotid != SPDK_BLOBID_INVALID) {
6379 			spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
6380 			poll_threads();
6381 		}
6382 
6383 		if ((snapshotid != SPDK_BLOBID_INVALID) && (g_bserrno == 0)) {
6384 			SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6385 			snapshot = g_blob;
6386 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(blob) == true);
6387 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(snapshot) == false);
6388 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
6389 			count = SPDK_COUNTOF(ids);
6390 			rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
6391 			CU_ASSERT(rc == 0);
6392 			CU_ASSERT(count == 1);
6393 			CU_ASSERT(ids[0] == blobid);
6394 			rc = spdk_blob_get_xattr_value(snapshot, SNAPSHOT_IN_PROGRESS, &value, &value_len);
6395 			CU_ASSERT(rc != 0);
6396 
6397 			spdk_blob_close(snapshot, blob_op_complete, NULL);
6398 			poll_threads();
6399 			CU_ASSERT(g_bserrno == 0);
6400 			if (create_snapshot_bserrno == 0) {
6401 				created = true;
6402 			}
6403 		} else {
6404 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
6405 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(blob) == false);
6406 		}
6407 
6408 		spdk_blob_close(blob, blob_op_complete, NULL);
6409 		poll_threads();
6410 		CU_ASSERT(g_bserrno == 0);
6411 
6412 		spdk_bs_unload(bs, bs_op_complete, NULL);
6413 		poll_threads();
6414 		CU_ASSERT(g_bserrno == 0);
6415 
6416 		thresholds.general_threshold++;
6417 	}
6418 }
6419 
6420 static void
6421 test_io_write(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6422 {
6423 	uint8_t payload_ff[64 * 512];
6424 	uint8_t payload_aa[64 * 512];
6425 	uint8_t payload_00[64 * 512];
6426 	uint8_t *cluster0, *cluster1;
6427 
6428 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6429 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6430 	memset(payload_00, 0x00, sizeof(payload_00));
6431 
6432 	/* Try to perform I/O with io unit = 512 */
6433 	spdk_blob_io_write(blob, channel, payload_ff, 0, 1, blob_op_complete, NULL);
6434 	poll_threads();
6435 	CU_ASSERT(g_bserrno == 0);
6436 
6437 	/* If thin provisioned is set cluster should be allocated now */
6438 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[0] != 0);
6439 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6440 
6441 	/* Each character 0-F symbolizes single io_unit containing 512 bytes block filled with that character.
6442 	* Each page is separated by |. Whole block [...] symbolizes one cluster (containing 4 pages). */
6443 	/* cluster0: [ F000 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6444 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6445 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 31 * 512) == 0);
6446 
6447 	/* Verify write with offset on first page */
6448 	spdk_blob_io_write(blob, channel, payload_ff, 2, 1, blob_op_complete, NULL);
6449 	poll_threads();
6450 	CU_ASSERT(g_bserrno == 0);
6451 
6452 	/* cluster0: [ F0F0 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6453 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6454 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6455 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6456 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6457 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_00, 28 * 512) == 0);
6458 
6459 	/* Verify write with offset on first page */
6460 	spdk_blob_io_write(blob, channel, payload_ff, 4, 4, blob_op_complete, NULL);
6461 	poll_threads();
6462 
6463 	/* cluster0: [ F0F0 FFFF | 0000 0000 | 0000 0000 | 0000 0000 ] */
6464 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6465 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6466 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6467 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6468 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 4 * 512) == 0);
6469 	CU_ASSERT(memcmp(cluster0 + 8 * 512, payload_00, 24 * 512) == 0);
6470 
6471 	/* Verify write with offset on second page */
6472 	spdk_blob_io_write(blob, channel, payload_ff, 8, 4, blob_op_complete, NULL);
6473 	poll_threads();
6474 
6475 	/* cluster0: [ F0F0 FFFF | FFFF 0000 | 0000 0000 | 0000 0000 ] */
6476 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6477 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6478 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6479 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6480 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 8 * 512) == 0);
6481 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6482 
6483 	/* Verify write across multiple pages */
6484 	spdk_blob_io_write(blob, channel, payload_aa, 4, 8, blob_op_complete, NULL);
6485 	poll_threads();
6486 
6487 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 0000 ] */
6488 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6489 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6490 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6491 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6492 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6493 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6494 
6495 	/* Verify write across multiple clusters */
6496 	spdk_blob_io_write(blob, channel, payload_ff, 28, 8, blob_op_complete, NULL);
6497 	poll_threads();
6498 
6499 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6500 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6501 
6502 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6503 	 * cluster1: [ FFFF 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6504 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6505 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6506 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6507 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6508 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6509 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6510 
6511 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6512 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 28 * 512) == 0);
6513 
6514 	/* Verify write to second cluster */
6515 	spdk_blob_io_write(blob, channel, payload_ff, 32 + 12, 2, blob_op_complete, NULL);
6516 	poll_threads();
6517 
6518 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6519 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6520 
6521 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6522 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ] */
6523 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6524 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6525 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6526 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6527 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6528 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6529 
6530 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6531 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 8 * 512) == 0);
6532 	CU_ASSERT(memcmp(cluster1 + 12 * 512, payload_ff, 2 * 512) == 0);
6533 	CU_ASSERT(memcmp(cluster1 + 14 * 512, payload_00, 18 * 512) == 0);
6534 }
6535 
6536 static void
6537 test_io_read(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6538 {
6539 	uint8_t payload_read[64 * 512];
6540 	uint8_t payload_ff[64 * 512];
6541 	uint8_t payload_aa[64 * 512];
6542 	uint8_t payload_00[64 * 512];
6543 
6544 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6545 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6546 	memset(payload_00, 0x00, sizeof(payload_00));
6547 
6548 	/* Read only first io unit */
6549 	/* cluster0: [ (F)0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6550 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6551 	 * payload_read: F000 0000 | 0000 0000 ... */
6552 	memset(payload_read, 0x00, sizeof(payload_read));
6553 	spdk_blob_io_read(blob, channel, payload_read, 0, 1, blob_op_complete, NULL);
6554 	poll_threads();
6555 	CU_ASSERT(g_bserrno == 0);
6556 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6557 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 31 * 512) == 0);
6558 
6559 	/* Read four io_units starting from offset = 2
6560 	 * cluster0: [ F0(F0 AA)AA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6561 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6562 	 * payload_read: F0AA 0000 | 0000 0000 ... */
6563 
6564 	memset(payload_read, 0x00, sizeof(payload_read));
6565 	spdk_blob_io_read(blob, channel, payload_read, 2, 4, blob_op_complete, NULL);
6566 	poll_threads();
6567 	CU_ASSERT(g_bserrno == 0);
6568 
6569 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6570 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
6571 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_aa, 512) == 0);
6572 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_aa, 512) == 0);
6573 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6574 
6575 	/* Read eight io_units across multiple pages
6576 	 * cluster0: [ F0F0 (AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6577 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6578 	 * payload_read: AAAA AAAA | 0000 0000 ... */
6579 	memset(payload_read, 0x00, sizeof(payload_read));
6580 	spdk_blob_io_read(blob, channel, payload_read, 4, 8, blob_op_complete, NULL);
6581 	poll_threads();
6582 	CU_ASSERT(g_bserrno == 0);
6583 
6584 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_aa, 8 * 512) == 0);
6585 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6586 
6587 	/* Read eight io_units across multiple clusters
6588 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 (FFFF ]
6589 	 * cluster1: [ FFFF) 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6590 	 * payload_read: FFFF FFFF | 0000 0000 ... */
6591 	memset(payload_read, 0x00, sizeof(payload_read));
6592 	spdk_blob_io_read(blob, channel, payload_read, 28, 8, blob_op_complete, NULL);
6593 	poll_threads();
6594 	CU_ASSERT(g_bserrno == 0);
6595 
6596 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 8 * 512) == 0);
6597 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6598 
6599 	/* Read four io_units from second cluster
6600 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6601 	 * cluster1: [ FFFF 0000 | 00(00 FF)00 | 0000 0000 | 0000 0000 ]
6602 	 * payload_read: 00FF 0000 | 0000 0000 ... */
6603 	memset(payload_read, 0x00, sizeof(payload_read));
6604 	spdk_blob_io_read(blob, channel, payload_read, 32 + 10, 4, blob_op_complete, NULL);
6605 	poll_threads();
6606 	CU_ASSERT(g_bserrno == 0);
6607 
6608 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_00, 2 * 512) == 0);
6609 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 2 * 512) == 0);
6610 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6611 
6612 	/* Read second cluster
6613 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6614 	 * cluster1: [ (FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ]
6615 	 * payload_read: FFFF 0000 | 0000 FF00 ... */
6616 	memset(payload_read, 0x00, sizeof(payload_read));
6617 	spdk_blob_io_read(blob, channel, payload_read, 32, 32, blob_op_complete, NULL);
6618 	poll_threads();
6619 	CU_ASSERT(g_bserrno == 0);
6620 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 4 * 512) == 0);
6621 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 8 * 512) == 0);
6622 	CU_ASSERT(memcmp(payload_read + 12 * 512, payload_ff, 2 * 512) == 0);
6623 	CU_ASSERT(memcmp(payload_read + 14 * 512, payload_00, 18 * 512) == 0);
6624 
6625 	/* Read whole two clusters
6626 	 * cluster0: [ (F0F0 AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6627 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ] */
6628 	memset(payload_read, 0x00, sizeof(payload_read));
6629 	spdk_blob_io_read(blob, channel, payload_read, 0, 64, blob_op_complete, NULL);
6630 	poll_threads();
6631 	CU_ASSERT(g_bserrno == 0);
6632 
6633 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6634 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
6635 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 512) == 0);
6636 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_00, 512) == 0);
6637 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_aa, 8 * 512) == 0);
6638 	CU_ASSERT(memcmp(payload_read + 28 * 512, payload_ff, 4 * 512) == 0);
6639 
6640 	CU_ASSERT(memcmp(payload_read + (32 + 0) * 512, payload_ff, 4 * 512) == 0);
6641 	CU_ASSERT(memcmp(payload_read + (32 + 4) * 512, payload_00, 8 * 512) == 0);
6642 	CU_ASSERT(memcmp(payload_read + (32 + 12) * 512, payload_ff, 2 * 512) == 0);
6643 	CU_ASSERT(memcmp(payload_read + (32 + 14) * 512, payload_00, 18 * 512) == 0);
6644 }
6645 
6646 
6647 static void
6648 test_io_unmap(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6649 {
6650 	uint8_t payload_ff[64 * 512];
6651 	uint8_t payload_aa[64 * 512];
6652 	uint8_t payload_00[64 * 512];
6653 	uint8_t *cluster0, *cluster1;
6654 
6655 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6656 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6657 	memset(payload_00, 0x00, sizeof(payload_00));
6658 
6659 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6660 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6661 
6662 	/* Unmap */
6663 	spdk_blob_io_unmap(blob, channel, 0, 64, blob_op_complete, NULL);
6664 	poll_threads();
6665 
6666 	CU_ASSERT(g_bserrno == 0);
6667 
6668 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_00, 32 * 512) == 0);
6669 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_00, 32 * 512) == 0);
6670 }
6671 
6672 static void
6673 test_io_zeroes(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6674 {
6675 	uint8_t payload_ff[64 * 512];
6676 	uint8_t payload_aa[64 * 512];
6677 	uint8_t payload_00[64 * 512];
6678 	uint8_t *cluster0, *cluster1;
6679 
6680 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6681 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6682 	memset(payload_00, 0x00, sizeof(payload_00));
6683 
6684 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6685 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6686 
6687 	/* Write zeroes  */
6688 	spdk_blob_io_write_zeroes(blob, channel, 0, 64, blob_op_complete, NULL);
6689 	poll_threads();
6690 
6691 	CU_ASSERT(g_bserrno == 0);
6692 
6693 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_00, 32 * 512) == 0);
6694 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_00, 32 * 512) == 0);
6695 }
6696 
6697 static inline void
6698 test_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
6699 		    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
6700 		    spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
6701 {
6702 	if (io_opts) {
6703 		g_dev_writev_ext_called = false;
6704 		memset(&g_blob_ext_io_opts, 0, sizeof(g_blob_ext_io_opts));
6705 		spdk_blob_io_writev_ext(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL,
6706 					io_opts);
6707 	} else {
6708 		spdk_blob_io_writev(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL);
6709 	}
6710 	poll_threads();
6711 	CU_ASSERT(g_bserrno == 0);
6712 	if (io_opts) {
6713 		CU_ASSERT(g_dev_writev_ext_called);
6714 		CU_ASSERT(memcmp(io_opts, &g_blob_ext_io_opts, sizeof(g_blob_ext_io_opts)) == 0);
6715 	}
6716 }
6717 
6718 static void
6719 test_iov_write(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel,
6720 	       bool ext_api)
6721 {
6722 	uint8_t payload_ff[64 * 512];
6723 	uint8_t payload_aa[64 * 512];
6724 	uint8_t payload_00[64 * 512];
6725 	uint8_t *cluster0, *cluster1;
6726 	struct iovec iov[4];
6727 	struct spdk_blob_ext_io_opts ext_opts = {
6728 		.memory_domain = (struct spdk_memory_domain *)0xfeedbeef,
6729 		.memory_domain_ctx = (void *)0xf00df00d,
6730 		.size = sizeof(struct spdk_blob_ext_io_opts),
6731 		.user_ctx = (void *)123,
6732 	};
6733 
6734 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6735 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6736 	memset(payload_00, 0x00, sizeof(payload_00));
6737 
6738 	/* Try to perform I/O with io unit = 512 */
6739 	iov[0].iov_base = payload_ff;
6740 	iov[0].iov_len = 1 * 512;
6741 
6742 	test_blob_io_writev(blob, channel, iov, 1, 0, 1, blob_op_complete, NULL,
6743 			    ext_api ? &ext_opts : NULL);
6744 
6745 	/* If thin provisioned is set cluster should be allocated now */
6746 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[0] != 0);
6747 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6748 
6749 	/* Each character 0-F symbolizes single io_unit containing 512 bytes block filled with that character.
6750 	* Each page is separated by |. Whole block [...] symbolizes one cluster (containing 4 pages). */
6751 	/* cluster0: [ F000 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6752 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6753 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 31 * 512) == 0);
6754 
6755 	/* Verify write with offset on first page */
6756 	iov[0].iov_base = payload_ff;
6757 	iov[0].iov_len = 1 * 512;
6758 
6759 	test_blob_io_writev(blob, channel, iov, 1, 2, 1, blob_op_complete, NULL,
6760 			    ext_api ? &ext_opts : NULL);
6761 
6762 	/* cluster0: [ F0F0 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6763 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6764 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6765 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6766 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6767 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_00, 28 * 512) == 0);
6768 
6769 	/* Verify write with offset on first page */
6770 	iov[0].iov_base = payload_ff;
6771 	iov[0].iov_len = 4 * 512;
6772 	spdk_blob_io_writev(blob, channel, iov, 1, 4, 4, blob_op_complete, NULL);
6773 	poll_threads();
6774 
6775 	/* cluster0: [ F0F0 FFFF | 0000 0000 | 0000 0000 | 0000 0000 ] */
6776 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6777 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6778 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6779 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6780 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 4 * 512) == 0);
6781 	CU_ASSERT(memcmp(cluster0 + 8 * 512, payload_00, 24 * 512) == 0);
6782 
6783 	/* Verify write with offset on second page */
6784 	iov[0].iov_base = payload_ff;
6785 	iov[0].iov_len = 4 * 512;
6786 	spdk_blob_io_writev(blob, channel, iov, 1, 8, 4, blob_op_complete, NULL);
6787 	poll_threads();
6788 
6789 	/* cluster0: [ F0F0 FFFF | FFFF 0000 | 0000 0000 | 0000 0000 ] */
6790 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6791 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6792 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6793 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6794 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 8 * 512) == 0);
6795 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6796 
6797 	/* Verify write across multiple pages */
6798 	iov[0].iov_base = payload_aa;
6799 	iov[0].iov_len = 8 * 512;
6800 
6801 	test_blob_io_writev(blob, channel, iov, 1, 4, 8, blob_op_complete, NULL,
6802 			    ext_api ? &ext_opts : NULL);
6803 
6804 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 0000 ] */
6805 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6806 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6807 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6808 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6809 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6810 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6811 
6812 	/* Verify write across multiple clusters */
6813 
6814 	iov[0].iov_base = payload_ff;
6815 	iov[0].iov_len = 8 * 512;
6816 
6817 	test_blob_io_writev(blob, channel, iov, 1, 28, 8, blob_op_complete, NULL,
6818 			    ext_api ? &ext_opts : NULL);
6819 
6820 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6821 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6822 
6823 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6824 	 * cluster1: [ FFFF 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6825 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6826 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6827 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6828 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6829 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6830 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 16 * 512) == 0);
6831 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6832 
6833 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6834 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 28 * 512) == 0);
6835 
6836 	/* Verify write to second cluster */
6837 
6838 	iov[0].iov_base = payload_ff;
6839 	iov[0].iov_len = 2 * 512;
6840 
6841 	test_blob_io_writev(blob, channel, iov, 1, 32 + 12, 2, blob_op_complete, NULL,
6842 			    ext_api ? &ext_opts : NULL);
6843 
6844 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6845 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6846 
6847 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6848 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ] */
6849 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6850 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6851 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6852 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6853 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6854 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6855 
6856 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6857 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 8 * 512) == 0);
6858 	CU_ASSERT(memcmp(cluster1 + 12 * 512, payload_ff, 2 * 512) == 0);
6859 	CU_ASSERT(memcmp(cluster1 + 14 * 512, payload_00, 18 * 512) == 0);
6860 }
6861 
6862 static inline void
6863 test_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
6864 		   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
6865 		   spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
6866 {
6867 	if (io_opts) {
6868 		g_dev_readv_ext_called = false;
6869 		memset(&g_blob_ext_io_opts, 0, sizeof(g_blob_ext_io_opts));
6870 		spdk_blob_io_readv_ext(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL, io_opts);
6871 	} else {
6872 		spdk_blob_io_readv(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL);
6873 	}
6874 	poll_threads();
6875 	CU_ASSERT(g_bserrno == 0);
6876 	if (io_opts) {
6877 		CU_ASSERT(g_dev_readv_ext_called);
6878 		CU_ASSERT(memcmp(io_opts, &g_blob_ext_io_opts, sizeof(g_blob_ext_io_opts)) == 0);
6879 	}
6880 }
6881 
6882 static void
6883 test_iov_read(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel,
6884 	      bool ext_api)
6885 {
6886 	uint8_t payload_read[64 * 512];
6887 	uint8_t payload_ff[64 * 512];
6888 	uint8_t payload_aa[64 * 512];
6889 	uint8_t payload_00[64 * 512];
6890 	struct iovec iov[4];
6891 	struct spdk_blob_ext_io_opts ext_opts = {
6892 		.memory_domain = (struct spdk_memory_domain *)0xfeedbeef,
6893 		.memory_domain_ctx = (void *)0xf00df00d,
6894 		.size = sizeof(struct spdk_blob_ext_io_opts),
6895 		.user_ctx = (void *)123,
6896 	};
6897 
6898 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6899 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6900 	memset(payload_00, 0x00, sizeof(payload_00));
6901 
6902 	/* Read only first io unit */
6903 	/* cluster0: [ (F)0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6904 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6905 	 * payload_read: F000 0000 | 0000 0000 ... */
6906 	memset(payload_read, 0x00, sizeof(payload_read));
6907 	iov[0].iov_base = payload_read;
6908 	iov[0].iov_len = 1 * 512;
6909 
6910 	test_blob_io_readv(blob, channel, iov, 1, 0, 1, blob_op_complete, NULL, ext_api ? &ext_opts : NULL);
6911 
6912 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6913 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 31 * 512) == 0);
6914 
6915 	/* Read four io_units starting from offset = 2
6916 	 * cluster0: [ F0(F0 AA)AA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6917 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6918 	 * payload_read: F0AA 0000 | 0000 0000 ... */
6919 
6920 	memset(payload_read, 0x00, sizeof(payload_read));
6921 	iov[0].iov_base = payload_read;
6922 	iov[0].iov_len = 4 * 512;
6923 
6924 	test_blob_io_readv(blob, channel, iov, 1, 2, 4, blob_op_complete, NULL, ext_api ? &ext_opts : NULL);
6925 
6926 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6927 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
6928 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_aa, 512) == 0);
6929 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_aa, 512) == 0);
6930 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6931 
6932 	/* Read eight io_units across multiple pages
6933 	 * cluster0: [ F0F0 (AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6934 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6935 	 * payload_read: AAAA AAAA | 0000 0000 ... */
6936 	memset(payload_read, 0x00, sizeof(payload_read));
6937 	iov[0].iov_base = payload_read;
6938 	iov[0].iov_len = 4 * 512;
6939 	iov[1].iov_base = payload_read + 4 * 512;
6940 	iov[1].iov_len = 4 * 512;
6941 
6942 	test_blob_io_readv(blob, channel, iov, 2, 4, 8, blob_op_complete, NULL, ext_api ? &ext_opts : NULL);
6943 
6944 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_aa, 8 * 512) == 0);
6945 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6946 
6947 	/* Read eight io_units across multiple clusters
6948 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 (FFFF ]
6949 	 * cluster1: [ FFFF) 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6950 	 * payload_read: FFFF FFFF | 0000 0000 ... */
6951 	memset(payload_read, 0x00, sizeof(payload_read));
6952 	iov[0].iov_base = payload_read;
6953 	iov[0].iov_len = 2 * 512;
6954 	iov[1].iov_base = payload_read + 2 * 512;
6955 	iov[1].iov_len = 2 * 512;
6956 	iov[2].iov_base = payload_read + 4 * 512;
6957 	iov[2].iov_len = 2 * 512;
6958 	iov[3].iov_base = payload_read + 6 * 512;
6959 	iov[3].iov_len = 2 * 512;
6960 
6961 	test_blob_io_readv(blob, channel, iov, 4, 28, 8, blob_op_complete, NULL,
6962 			   ext_api ? &ext_opts : NULL);
6963 
6964 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 8 * 512) == 0);
6965 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6966 
6967 	/* Read four io_units from second cluster
6968 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6969 	 * cluster1: [ FFFF 0000 | 00(00 FF)00 | 0000 0000 | 0000 0000 ]
6970 	 * payload_read: 00FF 0000 | 0000 0000 ... */
6971 	memset(payload_read, 0x00, sizeof(payload_read));
6972 	iov[0].iov_base = payload_read;
6973 	iov[0].iov_len = 1 * 512;
6974 	iov[1].iov_base = payload_read + 1 * 512;
6975 	iov[1].iov_len = 3 * 512;
6976 
6977 	test_blob_io_readv(blob, channel, iov, 2, 32 + 10, 4, blob_op_complete, NULL,
6978 			   ext_api ? &ext_opts : NULL);
6979 
6980 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_00, 2 * 512) == 0);
6981 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 2 * 512) == 0);
6982 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6983 
6984 	/* Read second cluster
6985 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6986 	 * cluster1: [ (FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ]
6987 	 * payload_read: FFFF 0000 | 0000 FF00 ... */
6988 	memset(payload_read, 0x00, sizeof(payload_read));
6989 	iov[0].iov_base = payload_read;
6990 	iov[0].iov_len = 1 * 512;
6991 	iov[1].iov_base = payload_read + 1 * 512;
6992 	iov[1].iov_len = 2 * 512;
6993 	iov[2].iov_base = payload_read + 3 * 512;
6994 	iov[2].iov_len = 4 * 512;
6995 	iov[3].iov_base = payload_read + 7 * 512;
6996 	iov[3].iov_len = 25 * 512;
6997 
6998 	test_blob_io_readv(blob, channel, iov, 4, 32, 32, blob_op_complete, NULL,
6999 			   ext_api ? &ext_opts : NULL);
7000 
7001 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 4 * 512) == 0);
7002 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 8 * 512) == 0);
7003 	CU_ASSERT(memcmp(payload_read + 12 * 512, payload_ff, 2 * 512) == 0);
7004 	CU_ASSERT(memcmp(payload_read + 14 * 512, payload_00, 18 * 512) == 0);
7005 
7006 	/* Read whole two clusters
7007 	 * cluster0: [ (F0F0 AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
7008 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ] */
7009 	memset(payload_read, 0x00, sizeof(payload_read));
7010 	iov[0].iov_base = payload_read;
7011 	iov[0].iov_len = 1 * 512;
7012 	iov[1].iov_base = payload_read + 1 * 512;
7013 	iov[1].iov_len = 8 * 512;
7014 	iov[2].iov_base = payload_read + 9 * 512;
7015 	iov[2].iov_len = 16 * 512;
7016 	iov[3].iov_base = payload_read + 25 * 512;
7017 	iov[3].iov_len = 39 * 512;
7018 
7019 	test_blob_io_readv(blob, channel, iov, 4, 0, 64, blob_op_complete, NULL,
7020 			   ext_api ? &ext_opts : NULL);
7021 
7022 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
7023 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
7024 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 512) == 0);
7025 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_00, 512) == 0);
7026 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_aa, 8 * 512) == 0);
7027 	CU_ASSERT(memcmp(payload_read + 28 * 512, payload_ff, 4 * 512) == 0);
7028 
7029 	CU_ASSERT(memcmp(payload_read + (32 + 0) * 512, payload_ff, 4 * 512) == 0);
7030 	CU_ASSERT(memcmp(payload_read + (32 + 4) * 512, payload_00, 8 * 512) == 0);
7031 	CU_ASSERT(memcmp(payload_read + (32 + 12) * 512, payload_ff, 2 * 512) == 0);
7032 	CU_ASSERT(memcmp(payload_read + (32 + 14) * 512, payload_00, 18 * 512) == 0);
7033 }
7034 
7035 static void
7036 blob_io_unit(void)
7037 {
7038 	struct spdk_bs_opts bsopts;
7039 	struct spdk_blob_opts opts;
7040 	struct spdk_blob_store *bs;
7041 	struct spdk_bs_dev *dev;
7042 	struct spdk_blob *blob, *snapshot, *clone;
7043 	spdk_blob_id blobid;
7044 	struct spdk_io_channel *channel;
7045 
7046 	/* Create dev with 512 bytes io unit size */
7047 
7048 	spdk_bs_opts_init(&bsopts, sizeof(bsopts));
7049 	bsopts.cluster_sz = SPDK_BS_PAGE_SIZE * 4;	/* 8 * 4 = 32 io_unit */
7050 	snprintf(bsopts.bstype.bstype, sizeof(bsopts.bstype.bstype), "TESTTYPE");
7051 
7052 	/* Try to initialize a new blob store with unsupported io_unit */
7053 	dev = init_dev();
7054 	dev->blocklen = 512;
7055 	dev->blockcnt =  DEV_BUFFER_SIZE / dev->blocklen;
7056 
7057 	/* Initialize a new blob store */
7058 	spdk_bs_init(dev, &bsopts, bs_op_with_handle_complete, NULL);
7059 	poll_threads();
7060 	CU_ASSERT(g_bserrno == 0);
7061 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7062 	bs = g_bs;
7063 
7064 	CU_ASSERT(spdk_bs_get_io_unit_size(bs) == 512);
7065 	channel = spdk_bs_alloc_io_channel(bs);
7066 
7067 	/* Create thick provisioned blob */
7068 	ut_spdk_blob_opts_init(&opts);
7069 	opts.thin_provision = false;
7070 	opts.num_clusters = 32;
7071 
7072 	blob = ut_blob_create_and_open(bs, &opts);
7073 	blobid = spdk_blob_get_id(blob);
7074 
7075 	test_io_write(dev, blob, channel);
7076 	test_io_read(dev, blob, channel);
7077 	test_io_zeroes(dev, blob, channel);
7078 
7079 	test_iov_write(dev, blob, channel, false);
7080 	test_iov_read(dev, blob, channel, false);
7081 	test_io_zeroes(dev, blob, channel);
7082 
7083 	test_iov_write(dev, blob, channel, true);
7084 	test_iov_read(dev, blob, channel, true);
7085 
7086 	test_io_unmap(dev, blob, channel);
7087 
7088 	spdk_blob_close(blob, blob_op_complete, NULL);
7089 	poll_threads();
7090 	CU_ASSERT(g_bserrno == 0);
7091 	blob = NULL;
7092 	g_blob = NULL;
7093 
7094 	/* Create thin provisioned blob */
7095 
7096 	ut_spdk_blob_opts_init(&opts);
7097 	opts.thin_provision = true;
7098 	opts.num_clusters = 32;
7099 
7100 	blob = ut_blob_create_and_open(bs, &opts);
7101 	blobid = spdk_blob_get_id(blob);
7102 
7103 	test_io_write(dev, blob, channel);
7104 	test_io_read(dev, blob, channel);
7105 	test_io_zeroes(dev, blob, channel);
7106 
7107 	test_iov_write(dev, blob, channel, false);
7108 	test_iov_read(dev, blob, channel, false);
7109 	test_io_zeroes(dev, blob, channel);
7110 
7111 	test_iov_write(dev, blob, channel, true);
7112 	test_iov_read(dev, blob, channel, true);
7113 
7114 	/* Create snapshot */
7115 
7116 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7117 	poll_threads();
7118 	CU_ASSERT(g_bserrno == 0);
7119 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7120 	blobid = g_blobid;
7121 
7122 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7123 	poll_threads();
7124 	CU_ASSERT(g_bserrno == 0);
7125 	CU_ASSERT(g_blob != NULL);
7126 	snapshot = g_blob;
7127 
7128 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7129 	poll_threads();
7130 	CU_ASSERT(g_bserrno == 0);
7131 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7132 	blobid = g_blobid;
7133 
7134 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7135 	poll_threads();
7136 	CU_ASSERT(g_bserrno == 0);
7137 	CU_ASSERT(g_blob != NULL);
7138 	clone = g_blob;
7139 
7140 	test_io_read(dev, blob, channel);
7141 	test_io_read(dev, snapshot, channel);
7142 	test_io_read(dev, clone, channel);
7143 
7144 	test_iov_read(dev, blob, channel, false);
7145 	test_iov_read(dev, snapshot, channel, false);
7146 	test_iov_read(dev, clone, channel, false);
7147 
7148 	test_iov_read(dev, blob, channel, true);
7149 	test_iov_read(dev, snapshot, channel, true);
7150 	test_iov_read(dev, clone, channel, true);
7151 
7152 	/* Inflate clone */
7153 
7154 	spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
7155 	poll_threads();
7156 
7157 	CU_ASSERT(g_bserrno == 0);
7158 
7159 	test_io_read(dev, clone, channel);
7160 
7161 	test_io_unmap(dev, clone, channel);
7162 
7163 	test_iov_write(dev, clone, channel, false);
7164 	test_iov_read(dev, clone, channel, false);
7165 	test_io_unmap(dev, clone, channel);
7166 
7167 	test_iov_write(dev, clone, channel, true);
7168 	test_iov_read(dev, clone, channel, true);
7169 
7170 	spdk_blob_close(blob, blob_op_complete, NULL);
7171 	spdk_blob_close(snapshot, blob_op_complete, NULL);
7172 	spdk_blob_close(clone, blob_op_complete, NULL);
7173 	poll_threads();
7174 	CU_ASSERT(g_bserrno == 0);
7175 	blob = NULL;
7176 	g_blob = NULL;
7177 
7178 	spdk_bs_free_io_channel(channel);
7179 	poll_threads();
7180 
7181 	/* Unload the blob store */
7182 	spdk_bs_unload(bs, bs_op_complete, NULL);
7183 	poll_threads();
7184 	CU_ASSERT(g_bserrno == 0);
7185 	g_bs = NULL;
7186 	g_blob = NULL;
7187 	g_blobid = 0;
7188 }
7189 
7190 static void
7191 blob_io_unit_compatibility(void)
7192 {
7193 	struct spdk_bs_opts bsopts;
7194 	struct spdk_blob_store *bs;
7195 	struct spdk_bs_dev *dev;
7196 	struct spdk_bs_super_block *super;
7197 
7198 	/* Create dev with 512 bytes io unit size */
7199 
7200 	spdk_bs_opts_init(&bsopts, sizeof(bsopts));
7201 	bsopts.cluster_sz = SPDK_BS_PAGE_SIZE * 4;	/* 8 * 4 = 32 io_unit */
7202 	snprintf(bsopts.bstype.bstype, sizeof(bsopts.bstype.bstype), "TESTTYPE");
7203 
7204 	/* Try to initialize a new blob store with unsupported io_unit */
7205 	dev = init_dev();
7206 	dev->blocklen = 512;
7207 	dev->blockcnt =  DEV_BUFFER_SIZE / dev->blocklen;
7208 
7209 	/* Initialize a new blob store */
7210 	spdk_bs_init(dev, &bsopts, bs_op_with_handle_complete, NULL);
7211 	poll_threads();
7212 	CU_ASSERT(g_bserrno == 0);
7213 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7214 	bs = g_bs;
7215 
7216 	CU_ASSERT(spdk_bs_get_io_unit_size(bs) == 512);
7217 
7218 	/* Unload the blob store */
7219 	spdk_bs_unload(bs, bs_op_complete, NULL);
7220 	poll_threads();
7221 	CU_ASSERT(g_bserrno == 0);
7222 
7223 	/* Modify super block to behave like older version.
7224 	 * Check if loaded io unit size equals SPDK_BS_PAGE_SIZE */
7225 	super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
7226 	super->io_unit_size = 0;
7227 	super->crc = blob_md_page_calc_crc(super);
7228 
7229 	dev = init_dev();
7230 	dev->blocklen = 512;
7231 	dev->blockcnt =  DEV_BUFFER_SIZE / dev->blocklen;
7232 
7233 	spdk_bs_load(dev, &bsopts, bs_op_with_handle_complete, NULL);
7234 	poll_threads();
7235 	CU_ASSERT(g_bserrno == 0);
7236 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7237 	bs = g_bs;
7238 
7239 	CU_ASSERT(spdk_bs_get_io_unit_size(bs) == SPDK_BS_PAGE_SIZE);
7240 
7241 	/* Unload the blob store */
7242 	spdk_bs_unload(bs, bs_op_complete, NULL);
7243 	poll_threads();
7244 	CU_ASSERT(g_bserrno == 0);
7245 
7246 	g_bs = NULL;
7247 	g_blob = NULL;
7248 	g_blobid = 0;
7249 }
7250 
7251 static void
7252 first_sync_complete(void *cb_arg, int bserrno)
7253 {
7254 	struct spdk_blob *blob = cb_arg;
7255 	int rc;
7256 
7257 	CU_ASSERT(bserrno == 0);
7258 	rc = spdk_blob_set_xattr(blob, "sync", "second", strlen("second") + 1);
7259 	CU_ASSERT(rc == 0);
7260 	CU_ASSERT(g_bserrno == -1);
7261 
7262 	/* Keep g_bserrno at -1, only the
7263 	 * second sync completion should set it at 0. */
7264 }
7265 
7266 static void
7267 second_sync_complete(void *cb_arg, int bserrno)
7268 {
7269 	struct spdk_blob *blob = cb_arg;
7270 	const void *value;
7271 	size_t value_len;
7272 	int rc;
7273 
7274 	CU_ASSERT(bserrno == 0);
7275 
7276 	/* Verify that the first sync completion had a chance to execute */
7277 	rc = spdk_blob_get_xattr_value(blob, "sync", &value, &value_len);
7278 	CU_ASSERT(rc == 0);
7279 	SPDK_CU_ASSERT_FATAL(value != NULL);
7280 	CU_ASSERT(value_len == strlen("second") + 1);
7281 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, "second", value_len);
7282 
7283 	CU_ASSERT(g_bserrno == -1);
7284 	g_bserrno = bserrno;
7285 }
7286 
7287 static void
7288 blob_simultaneous_operations(void)
7289 {
7290 	struct spdk_blob_store *bs = g_bs;
7291 	struct spdk_blob_opts opts;
7292 	struct spdk_blob *blob, *snapshot;
7293 	spdk_blob_id blobid, snapshotid;
7294 	struct spdk_io_channel *channel;
7295 	int rc;
7296 
7297 	channel = spdk_bs_alloc_io_channel(bs);
7298 	SPDK_CU_ASSERT_FATAL(channel != NULL);
7299 
7300 	ut_spdk_blob_opts_init(&opts);
7301 	opts.num_clusters = 10;
7302 
7303 	blob = ut_blob_create_and_open(bs, &opts);
7304 	blobid = spdk_blob_get_id(blob);
7305 
7306 	/* Create snapshot and try to remove blob in the same time:
7307 	 * - snapshot should be created successfully
7308 	 * - delete operation should fail w -EBUSY */
7309 	CU_ASSERT(blob->locked_operation_in_progress == false);
7310 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7311 	CU_ASSERT(blob->locked_operation_in_progress == true);
7312 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
7313 	CU_ASSERT(blob->locked_operation_in_progress == true);
7314 	/* Deletion failure */
7315 	CU_ASSERT(g_bserrno == -EBUSY);
7316 	poll_threads();
7317 	CU_ASSERT(blob->locked_operation_in_progress == false);
7318 	/* Snapshot creation success */
7319 	CU_ASSERT(g_bserrno == 0);
7320 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7321 
7322 	snapshotid = g_blobid;
7323 
7324 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
7325 	poll_threads();
7326 	CU_ASSERT(g_bserrno == 0);
7327 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7328 	snapshot = g_blob;
7329 
7330 	/* Inflate blob and try to remove blob in the same time:
7331 	 * - blob should be inflated successfully
7332 	 * - delete operation should fail w -EBUSY */
7333 	CU_ASSERT(blob->locked_operation_in_progress == false);
7334 	spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
7335 	CU_ASSERT(blob->locked_operation_in_progress == true);
7336 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
7337 	CU_ASSERT(blob->locked_operation_in_progress == true);
7338 	/* Deletion failure */
7339 	CU_ASSERT(g_bserrno == -EBUSY);
7340 	poll_threads();
7341 	CU_ASSERT(blob->locked_operation_in_progress == false);
7342 	/* Inflation success */
7343 	CU_ASSERT(g_bserrno == 0);
7344 
7345 	/* Clone snapshot and try to remove snapshot in the same time:
7346 	 * - snapshot should be cloned successfully
7347 	 * - delete operation should fail w -EBUSY */
7348 	CU_ASSERT(blob->locked_operation_in_progress == false);
7349 	spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
7350 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
7351 	/* Deletion failure */
7352 	CU_ASSERT(g_bserrno == -EBUSY);
7353 	poll_threads();
7354 	CU_ASSERT(blob->locked_operation_in_progress == false);
7355 	/* Clone created */
7356 	CU_ASSERT(g_bserrno == 0);
7357 
7358 	/* Resize blob and try to remove blob in the same time:
7359 	 * - blob should be resized successfully
7360 	 * - delete operation should fail w -EBUSY */
7361 	CU_ASSERT(blob->locked_operation_in_progress == false);
7362 	spdk_blob_resize(blob, 50, blob_op_complete, NULL);
7363 	CU_ASSERT(blob->locked_operation_in_progress == true);
7364 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
7365 	CU_ASSERT(blob->locked_operation_in_progress == true);
7366 	/* Deletion failure */
7367 	CU_ASSERT(g_bserrno == -EBUSY);
7368 	poll_threads();
7369 	CU_ASSERT(blob->locked_operation_in_progress == false);
7370 	/* Blob resized successfully */
7371 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
7372 	poll_threads();
7373 	CU_ASSERT(g_bserrno == 0);
7374 
7375 	/* Issue two consecutive blob syncs, neither should fail.
7376 	 * Force sync to actually occur by marking blob dirty each time.
7377 	 * Execution of sync should not be enough to complete the operation,
7378 	 * since disk I/O is required to complete it. */
7379 	g_bserrno = -1;
7380 
7381 	rc = spdk_blob_set_xattr(blob, "sync", "first", strlen("first") + 1);
7382 	CU_ASSERT(rc == 0);
7383 	spdk_blob_sync_md(blob, first_sync_complete, blob);
7384 	CU_ASSERT(g_bserrno == -1);
7385 
7386 	spdk_blob_sync_md(blob, second_sync_complete, blob);
7387 	CU_ASSERT(g_bserrno == -1);
7388 
7389 	poll_threads();
7390 	CU_ASSERT(g_bserrno == 0);
7391 
7392 	spdk_bs_free_io_channel(channel);
7393 	poll_threads();
7394 
7395 	ut_blob_close_and_delete(bs, snapshot);
7396 	ut_blob_close_and_delete(bs, blob);
7397 }
7398 
7399 static void
7400 blob_persist_test(void)
7401 {
7402 	struct spdk_blob_store *bs = g_bs;
7403 	struct spdk_blob_opts opts;
7404 	struct spdk_blob *blob;
7405 	spdk_blob_id blobid;
7406 	struct spdk_io_channel *channel;
7407 	char *xattr;
7408 	size_t xattr_length;
7409 	int rc;
7410 	uint32_t page_count_clear, page_count_xattr;
7411 	uint64_t poller_iterations;
7412 	bool run_poller;
7413 
7414 	channel = spdk_bs_alloc_io_channel(bs);
7415 	SPDK_CU_ASSERT_FATAL(channel != NULL);
7416 
7417 	ut_spdk_blob_opts_init(&opts);
7418 	opts.num_clusters = 10;
7419 
7420 	blob = ut_blob_create_and_open(bs, &opts);
7421 	blobid = spdk_blob_get_id(blob);
7422 
7423 	/* Save the amount of md pages used after creation of a blob.
7424 	 * This should be consistent after removing xattr. */
7425 	page_count_clear = spdk_bit_array_count_set(bs->used_md_pages);
7426 	SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_clear);
7427 	SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_clear);
7428 
7429 	/* Add xattr with maximum length of descriptor to exceed single metadata page. */
7430 	xattr_length = SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_xattr) -
7431 		       strlen("large_xattr");
7432 	xattr = calloc(xattr_length, sizeof(char));
7433 	SPDK_CU_ASSERT_FATAL(xattr != NULL);
7434 
7435 	rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
7436 	SPDK_CU_ASSERT_FATAL(rc == 0);
7437 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
7438 	poll_threads();
7439 	SPDK_CU_ASSERT_FATAL(g_bserrno == 0);
7440 
7441 	/* Save the amount of md pages used after adding the large xattr */
7442 	page_count_xattr = spdk_bit_array_count_set(bs->used_md_pages);
7443 	SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_xattr);
7444 	SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_xattr);
7445 
7446 	/* Add xattr to a blob and sync it. While sync is occurring, remove the xattr and sync again.
7447 	 * Interrupt the first sync after increasing number of poller iterations, until it succeeds.
7448 	 * Expectation is that after second sync completes no xattr is saved in metadata. */
7449 	poller_iterations = 1;
7450 	run_poller = true;
7451 	while (run_poller) {
7452 		rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
7453 		SPDK_CU_ASSERT_FATAL(rc == 0);
7454 		g_bserrno = -1;
7455 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
7456 		poll_thread_times(0, poller_iterations);
7457 		if (g_bserrno == 0) {
7458 			/* Poller iteration count was high enough for first sync to complete.
7459 			 * Verify that blob takes up enough of md_pages to store the xattr. */
7460 			SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_xattr);
7461 			SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_xattr);
7462 			SPDK_CU_ASSERT_FATAL(spdk_bit_array_count_set(bs->used_md_pages) == page_count_xattr);
7463 			run_poller = false;
7464 		}
7465 		rc = spdk_blob_remove_xattr(blob, "large_xattr");
7466 		SPDK_CU_ASSERT_FATAL(rc == 0);
7467 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
7468 		poll_threads();
7469 		SPDK_CU_ASSERT_FATAL(g_bserrno == 0);
7470 		SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_clear);
7471 		SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_clear);
7472 		SPDK_CU_ASSERT_FATAL(spdk_bit_array_count_set(bs->used_md_pages) == page_count_clear);
7473 
7474 		/* Reload bs and re-open blob to verify that xattr was not persisted. */
7475 		spdk_blob_close(blob, blob_op_complete, NULL);
7476 		poll_threads();
7477 		CU_ASSERT(g_bserrno == 0);
7478 
7479 		ut_bs_reload(&bs, NULL);
7480 
7481 		spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7482 		poll_threads();
7483 		CU_ASSERT(g_bserrno == 0);
7484 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7485 		blob = g_blob;
7486 
7487 		rc = spdk_blob_get_xattr_value(blob, "large_xattr", (const void **)&xattr, &xattr_length);
7488 		SPDK_CU_ASSERT_FATAL(rc == -ENOENT);
7489 
7490 		poller_iterations++;
7491 		/* Stop at high iteration count to prevent infinite loop.
7492 		 * This value should be enough for first md sync to complete in any case. */
7493 		SPDK_CU_ASSERT_FATAL(poller_iterations < 50);
7494 	}
7495 
7496 	free(xattr);
7497 
7498 	ut_blob_close_and_delete(bs, blob);
7499 
7500 	spdk_bs_free_io_channel(channel);
7501 	poll_threads();
7502 }
7503 
7504 static void
7505 blob_decouple_snapshot(void)
7506 {
7507 	struct spdk_blob_store *bs = g_bs;
7508 	struct spdk_blob_opts opts;
7509 	struct spdk_blob *blob, *snapshot1, *snapshot2;
7510 	struct spdk_io_channel *channel;
7511 	spdk_blob_id blobid, snapshotid;
7512 	uint64_t cluster;
7513 
7514 	for (int delete_snapshot_first = 0; delete_snapshot_first <= 1; delete_snapshot_first++) {
7515 		channel = spdk_bs_alloc_io_channel(bs);
7516 		SPDK_CU_ASSERT_FATAL(channel != NULL);
7517 
7518 		ut_spdk_blob_opts_init(&opts);
7519 		opts.num_clusters = 10;
7520 		opts.thin_provision = false;
7521 
7522 		blob = ut_blob_create_and_open(bs, &opts);
7523 		blobid = spdk_blob_get_id(blob);
7524 
7525 		/* Create first snapshot */
7526 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 0);
7527 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7528 		poll_threads();
7529 		CU_ASSERT(g_bserrno == 0);
7530 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7531 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 1);
7532 		snapshotid = g_blobid;
7533 
7534 		spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
7535 		poll_threads();
7536 		CU_ASSERT(g_bserrno == 0);
7537 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7538 		snapshot1 = g_blob;
7539 
7540 		/* Create the second one */
7541 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 1);
7542 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7543 		poll_threads();
7544 		CU_ASSERT(g_bserrno == 0);
7545 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7546 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 2);
7547 		snapshotid = g_blobid;
7548 
7549 		spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
7550 		poll_threads();
7551 		CU_ASSERT(g_bserrno == 0);
7552 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7553 		snapshot2 = g_blob;
7554 		CU_ASSERT_EQUAL(spdk_blob_get_parent_snapshot(bs, snapshot2->id), snapshot1->id);
7555 
7556 		/* Now decouple the second snapshot forcing it to copy the written clusters */
7557 		spdk_bs_blob_decouple_parent(bs, channel, snapshot2->id, blob_op_complete, NULL);
7558 		poll_threads();
7559 		CU_ASSERT(g_bserrno == 0);
7560 
7561 		/* Verify that the snapshot has been decoupled and that the clusters have been copied */
7562 		CU_ASSERT_EQUAL(spdk_blob_get_parent_snapshot(bs, snapshot2->id), SPDK_BLOBID_INVALID);
7563 		for (cluster = 0; cluster < snapshot2->active.num_clusters; ++cluster) {
7564 			CU_ASSERT_NOT_EQUAL(snapshot2->active.clusters[cluster], 0);
7565 			CU_ASSERT_NOT_EQUAL(snapshot2->active.clusters[cluster],
7566 					    snapshot1->active.clusters[cluster]);
7567 		}
7568 
7569 		spdk_bs_free_io_channel(channel);
7570 
7571 		if (delete_snapshot_first) {
7572 			ut_blob_close_and_delete(bs, snapshot2);
7573 			ut_blob_close_and_delete(bs, snapshot1);
7574 			ut_blob_close_and_delete(bs, blob);
7575 		} else {
7576 			ut_blob_close_and_delete(bs, blob);
7577 			ut_blob_close_and_delete(bs, snapshot2);
7578 			ut_blob_close_and_delete(bs, snapshot1);
7579 		}
7580 		poll_threads();
7581 	}
7582 }
7583 
7584 static void
7585 blob_seek_io_unit(void)
7586 {
7587 	struct spdk_blob_store *bs = g_bs;
7588 	struct spdk_blob *blob;
7589 	struct spdk_io_channel *channel;
7590 	struct spdk_blob_opts opts;
7591 	uint64_t free_clusters;
7592 	uint8_t payload[10 * 4096];
7593 	uint64_t offset;
7594 	uint64_t io_unit, io_units_per_cluster;
7595 
7596 	free_clusters = spdk_bs_free_cluster_count(bs);
7597 
7598 	channel = spdk_bs_alloc_io_channel(bs);
7599 	CU_ASSERT(channel != NULL);
7600 
7601 	/* Set blob as thin provisioned */
7602 	ut_spdk_blob_opts_init(&opts);
7603 	opts.thin_provision = true;
7604 
7605 	/* Create a blob */
7606 	blob = ut_blob_create_and_open(bs, &opts);
7607 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
7608 
7609 	io_units_per_cluster = bs_io_units_per_cluster(blob);
7610 
7611 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
7612 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
7613 	poll_threads();
7614 	CU_ASSERT(g_bserrno == 0);
7615 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
7616 	CU_ASSERT(blob->active.num_clusters == 5);
7617 
7618 	/* Write at the beginning of first cluster */
7619 	offset = 0;
7620 	spdk_blob_io_write(blob, channel, payload, offset, 1, blob_op_complete, NULL);
7621 	poll_threads();
7622 	CU_ASSERT(g_bserrno == 0);
7623 
7624 	io_unit = spdk_blob_get_next_allocated_io_unit(blob, 0);
7625 	CU_ASSERT(io_unit == offset);
7626 
7627 	io_unit = spdk_blob_get_next_unallocated_io_unit(blob, 0);
7628 	CU_ASSERT(io_unit == io_units_per_cluster);
7629 
7630 	/* Write in the middle of third cluster */
7631 	offset = 2 * io_units_per_cluster + io_units_per_cluster / 2;
7632 	spdk_blob_io_write(blob, channel, payload, offset, 1, blob_op_complete, NULL);
7633 	poll_threads();
7634 	CU_ASSERT(g_bserrno == 0);
7635 
7636 	io_unit = spdk_blob_get_next_allocated_io_unit(blob, io_units_per_cluster);
7637 	CU_ASSERT(io_unit == 2 * io_units_per_cluster);
7638 
7639 	io_unit = spdk_blob_get_next_unallocated_io_unit(blob, 2 * io_units_per_cluster);
7640 	CU_ASSERT(io_unit == 3 * io_units_per_cluster);
7641 
7642 	/* Write at the end of last cluster */
7643 	offset = 5 * io_units_per_cluster - 1;
7644 	spdk_blob_io_write(blob, channel, payload, offset, 1, blob_op_complete, NULL);
7645 	poll_threads();
7646 	CU_ASSERT(g_bserrno == 0);
7647 
7648 	io_unit = spdk_blob_get_next_allocated_io_unit(blob, 3 * io_units_per_cluster);
7649 	CU_ASSERT(io_unit == 4 * io_units_per_cluster);
7650 
7651 	io_unit = spdk_blob_get_next_unallocated_io_unit(blob, 4 * io_units_per_cluster);
7652 	CU_ASSERT(io_unit == UINT64_MAX);
7653 
7654 	spdk_bs_free_io_channel(channel);
7655 	poll_threads();
7656 
7657 	ut_blob_close_and_delete(bs, blob);
7658 }
7659 
7660 static void
7661 blob_esnap_create(void)
7662 {
7663 	struct spdk_blob_store	*bs = g_bs;
7664 	struct spdk_bs_opts	bs_opts;
7665 	struct ut_esnap_opts	esnap_opts;
7666 	struct spdk_blob_opts	opts;
7667 	struct spdk_blob_open_opts open_opts;
7668 	struct spdk_blob	*blob;
7669 	uint32_t		cluster_sz, block_sz;
7670 	const uint32_t		esnap_num_clusters = 4;
7671 	uint64_t		esnap_num_blocks;
7672 	uint32_t		sz;
7673 	spdk_blob_id		blobid;
7674 	uint32_t		bs_ctx_count, blob_ctx_count;
7675 
7676 	cluster_sz = spdk_bs_get_cluster_size(bs);
7677 	block_sz = spdk_bs_get_io_unit_size(bs);
7678 	esnap_num_blocks = cluster_sz * esnap_num_clusters / block_sz;
7679 
7680 	/* Create a normal blob and verify it is not an esnap clone. */
7681 	ut_spdk_blob_opts_init(&opts);
7682 	blob = ut_blob_create_and_open(bs, &opts);
7683 	CU_ASSERT(!spdk_blob_is_esnap_clone(blob));
7684 	ut_blob_close_and_delete(bs, blob);
7685 
7686 	/* Create an esnap clone blob then verify it is an esnap clone and has the right size */
7687 	ut_spdk_blob_opts_init(&opts);
7688 	ut_esnap_opts_init(block_sz, esnap_num_blocks, __func__, NULL, &esnap_opts);
7689 	opts.esnap_id = &esnap_opts;
7690 	opts.esnap_id_len = sizeof(esnap_opts);
7691 	opts.num_clusters = esnap_num_clusters;
7692 	blob = ut_blob_create_and_open(bs, &opts);
7693 	SPDK_CU_ASSERT_FATAL(blob != NULL);
7694 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7695 	SPDK_CU_ASSERT_FATAL(blob_is_esnap_clone(blob));
7696 	SPDK_CU_ASSERT_FATAL(!spdk_blob_is_clone(blob));
7697 	sz = spdk_blob_get_num_clusters(blob);
7698 	CU_ASSERT(sz == esnap_num_clusters);
7699 	ut_blob_close_and_delete(bs, blob);
7700 
7701 	/* Create an esnap clone without the size and verify it can be grown */
7702 	ut_spdk_blob_opts_init(&opts);
7703 	ut_esnap_opts_init(block_sz, esnap_num_blocks, __func__, NULL, &esnap_opts);
7704 	opts.esnap_id = &esnap_opts;
7705 	opts.esnap_id_len = sizeof(esnap_opts);
7706 	blob = ut_blob_create_and_open(bs, &opts);
7707 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7708 	sz = spdk_blob_get_num_clusters(blob);
7709 	CU_ASSERT(sz == 0);
7710 	spdk_blob_resize(blob, 1, blob_op_complete, NULL);
7711 	poll_threads();
7712 	CU_ASSERT(g_bserrno == 0);
7713 	sz = spdk_blob_get_num_clusters(blob);
7714 	CU_ASSERT(sz == 1);
7715 	spdk_blob_resize(blob, esnap_num_clusters, blob_op_complete, NULL);
7716 	poll_threads();
7717 	CU_ASSERT(g_bserrno == 0);
7718 	sz = spdk_blob_get_num_clusters(blob);
7719 	CU_ASSERT(sz == esnap_num_clusters);
7720 	spdk_blob_resize(blob, esnap_num_clusters + 1, blob_op_complete, NULL);
7721 	poll_threads();
7722 	CU_ASSERT(g_bserrno == 0);
7723 	sz = spdk_blob_get_num_clusters(blob);
7724 	CU_ASSERT(sz == esnap_num_clusters + 1);
7725 
7726 	/* Reload the blobstore and be sure that the blob can be opened. */
7727 	blobid = spdk_blob_get_id(blob);
7728 	spdk_blob_close(blob, blob_op_complete, NULL);
7729 	poll_threads();
7730 	CU_ASSERT(g_bserrno == 0);
7731 	g_blob = NULL;
7732 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7733 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
7734 	ut_bs_reload(&bs, &bs_opts);
7735 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7736 	poll_threads();
7737 	CU_ASSERT(g_bserrno == 0);
7738 	CU_ASSERT(g_blob != NULL);
7739 	blob = g_blob;
7740 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7741 	sz = spdk_blob_get_num_clusters(blob);
7742 	CU_ASSERT(sz == esnap_num_clusters + 1);
7743 
7744 	/* Reload the blobstore without esnap_bs_dev_create: should fail to open blob. */
7745 	spdk_blob_close(blob, blob_op_complete, NULL);
7746 	poll_threads();
7747 	CU_ASSERT(g_bserrno == 0);
7748 	g_blob = NULL;
7749 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7750 	ut_bs_reload(&bs, &bs_opts);
7751 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7752 	poll_threads();
7753 	CU_ASSERT(g_bserrno != 0);
7754 	CU_ASSERT(g_blob == NULL);
7755 
7756 	/* Reload the blobstore with ctx set and verify it is passed to the esnap create callback */
7757 	bs_ctx_count = 0;
7758 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7759 	bs_opts.esnap_bs_dev_create = ut_esnap_create_with_count;
7760 	bs_opts.esnap_ctx = &bs_ctx_count;
7761 	ut_bs_reload(&bs, &bs_opts);
7762 	/* Loading the blobstore triggers the esnap to be loaded */
7763 	CU_ASSERT(bs_ctx_count == 1);
7764 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7765 	poll_threads();
7766 	CU_ASSERT(g_bserrno == 0);
7767 	CU_ASSERT(g_blob != NULL);
7768 	/* Opening the blob also triggers the esnap to be loaded */
7769 	CU_ASSERT(bs_ctx_count == 2);
7770 	blob = g_blob;
7771 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7772 	sz = spdk_blob_get_num_clusters(blob);
7773 	CU_ASSERT(sz == esnap_num_clusters + 1);
7774 	spdk_blob_close(blob, blob_op_complete, NULL);
7775 	poll_threads();
7776 	CU_ASSERT(g_bserrno == 0);
7777 	g_blob = NULL;
7778 	/* If open_opts.esnap_ctx is set it is passed to the esnap create callback */
7779 	blob_ctx_count = 0;
7780 	spdk_blob_open_opts_init(&open_opts, sizeof(open_opts));
7781 	open_opts.esnap_ctx = &blob_ctx_count;
7782 	spdk_bs_open_blob_ext(bs, blobid, &open_opts, blob_op_with_handle_complete, NULL);
7783 	poll_threads();
7784 	blob = g_blob;
7785 	CU_ASSERT(bs_ctx_count == 3);
7786 	CU_ASSERT(blob_ctx_count == 1);
7787 	spdk_blob_close(blob, blob_op_complete, NULL);
7788 	poll_threads();
7789 	CU_ASSERT(g_bserrno == 0);
7790 	g_blob = NULL;
7791 }
7792 
7793 static void
7794 blob_esnap_clone_reload(void)
7795 {
7796 	struct spdk_blob_store	*bs = g_bs;
7797 	struct spdk_bs_opts	bs_opts;
7798 	struct ut_esnap_opts	esnap_opts;
7799 	struct spdk_blob_opts	opts;
7800 	struct spdk_blob	*eclone1, *snap1, *clone1;
7801 	uint32_t		cluster_sz = spdk_bs_get_cluster_size(bs);
7802 	uint32_t		block_sz = spdk_bs_get_io_unit_size(bs);
7803 	const uint32_t		esnap_num_clusters = 4;
7804 	uint64_t		esnap_num_blocks = cluster_sz * esnap_num_clusters / block_sz;
7805 	spdk_blob_id		eclone1_id, snap1_id, clone1_id;
7806 	struct spdk_io_channel	*bs_ch;
7807 	char			buf[block_sz];
7808 	int			bserr1, bserr2, bserr3, bserr4;
7809 	struct spdk_bs_dev	*dev;
7810 
7811 	/* Create and open an esnap clone blob */
7812 	ut_spdk_blob_opts_init(&opts);
7813 	ut_esnap_opts_init(block_sz, esnap_num_blocks, __func__, NULL, &esnap_opts);
7814 	opts.esnap_id = &esnap_opts;
7815 	opts.esnap_id_len = sizeof(esnap_opts);
7816 	opts.num_clusters = esnap_num_clusters;
7817 	eclone1 = ut_blob_create_and_open(bs, &opts);
7818 	CU_ASSERT(eclone1 != NULL);
7819 	CU_ASSERT(spdk_blob_is_esnap_clone(eclone1));
7820 	eclone1_id = eclone1->id;
7821 
7822 	/* Create and open a snapshot of eclone1 */
7823 	spdk_bs_create_snapshot(bs, eclone1_id, NULL, blob_op_with_id_complete, NULL);
7824 	poll_threads();
7825 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7826 	CU_ASSERT(g_bserrno == 0);
7827 	snap1_id = g_blobid;
7828 	spdk_bs_open_blob(bs, snap1_id, blob_op_with_handle_complete, NULL);
7829 	poll_threads();
7830 	CU_ASSERT(g_bserrno == 0);
7831 	CU_ASSERT(g_blob != NULL);
7832 	snap1 = g_blob;
7833 
7834 	/* Create and open regular clone of snap1 */
7835 	spdk_bs_create_clone(bs, snap1_id, NULL, blob_op_with_id_complete, NULL);
7836 	poll_threads();
7837 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7838 	SPDK_CU_ASSERT_FATAL(g_bserrno == 0);
7839 	clone1_id = g_blobid;
7840 	spdk_bs_open_blob(bs, clone1_id, blob_op_with_handle_complete, NULL);
7841 	poll_threads();
7842 	CU_ASSERT(g_bserrno == 0);
7843 	CU_ASSERT(g_blob != NULL);
7844 	clone1 = g_blob;
7845 
7846 	/* Close the blobs in preparation for reloading the blobstore */
7847 	spdk_blob_close(clone1, blob_op_complete, NULL);
7848 	poll_threads();
7849 	CU_ASSERT(g_bserrno == 0);
7850 	spdk_blob_close(snap1, blob_op_complete, NULL);
7851 	poll_threads();
7852 	CU_ASSERT(g_bserrno == 0);
7853 	spdk_blob_close(eclone1, blob_op_complete, NULL);
7854 	poll_threads();
7855 	CU_ASSERT(g_bserrno == 0);
7856 	g_blob = NULL;
7857 
7858 	/* Reload the blobstore */
7859 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7860 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
7861 	ut_bs_reload(&bs, &bs_opts);
7862 
7863 	/* Be sure each of the blobs can be opened */
7864 	spdk_bs_open_blob(bs, eclone1_id, blob_op_with_handle_complete, NULL);
7865 	poll_threads();
7866 	CU_ASSERT(g_bserrno == 0);
7867 	CU_ASSERT(g_blob != NULL);
7868 	eclone1 = g_blob;
7869 	spdk_bs_open_blob(bs, snap1_id, blob_op_with_handle_complete, NULL);
7870 	poll_threads();
7871 	CU_ASSERT(g_bserrno == 0);
7872 	CU_ASSERT(g_blob != NULL);
7873 	snap1 = g_blob;
7874 	spdk_bs_open_blob(bs, clone1_id, blob_op_with_handle_complete, NULL);
7875 	poll_threads();
7876 	CU_ASSERT(g_bserrno == 0);
7877 	CU_ASSERT(g_blob != NULL);
7878 	clone1 = g_blob;
7879 
7880 	/* Perform some reads on each of them to cause channels to be allocated */
7881 	bs_ch = spdk_bs_alloc_io_channel(bs);
7882 	CU_ASSERT(bs_ch != NULL);
7883 	spdk_blob_io_read(eclone1, bs_ch, buf, 0, 1, bs_op_complete, NULL);
7884 	poll_threads();
7885 	CU_ASSERT(g_bserrno == 0);
7886 	spdk_blob_io_read(snap1, bs_ch, buf, 0, 1, bs_op_complete, NULL);
7887 	poll_threads();
7888 	CU_ASSERT(g_bserrno == 0);
7889 	spdk_blob_io_read(clone1, bs_ch, buf, 0, 1, bs_op_complete, NULL);
7890 	poll_threads();
7891 	CU_ASSERT(g_bserrno == 0);
7892 
7893 	/*
7894 	 * Unload the blobstore in a way similar to how lvstore unloads it.  This should exercise
7895 	 * the deferred unload path in spdk_bs_unload().
7896 	 */
7897 	bserr1 = 0xbad;
7898 	bserr2 = 0xbad;
7899 	bserr3 = 0xbad;
7900 	bserr4 = 0xbad;
7901 	spdk_blob_close(eclone1, blob_op_complete, &bserr1);
7902 	spdk_blob_close(snap1, blob_op_complete, &bserr2);
7903 	spdk_blob_close(clone1, blob_op_complete, &bserr3);
7904 	spdk_bs_unload(bs, blob_op_complete, &bserr4);
7905 	spdk_bs_free_io_channel(bs_ch);
7906 	poll_threads();
7907 	CU_ASSERT(bserr1 == 0);
7908 	CU_ASSERT(bserr2 == 0);
7909 	CU_ASSERT(bserr3 == 0);
7910 	CU_ASSERT(bserr4 == 0);
7911 	g_blob = NULL;
7912 
7913 	/* Reload the blobstore */
7914 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7915 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
7916 	dev = init_dev();
7917 	spdk_bs_load(dev, &bs_opts, bs_op_with_handle_complete, NULL);
7918 	poll_threads();
7919 	CU_ASSERT(g_bserrno == 0);
7920 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7921 }
7922 
7923 static bool
7924 blob_esnap_verify_contents(struct spdk_blob *blob, struct spdk_io_channel *ch,
7925 			   uint64_t offset, uint64_t size, uint32_t readsize, const char *how)
7926 {
7927 	const uint32_t	bs_blksz = blob->bs->io_unit_size;
7928 	const uint32_t	esnap_blksz = blob->back_bs_dev ? blob->back_bs_dev->blocklen : bs_blksz;
7929 	const uint32_t	start_blk = offset / bs_blksz;
7930 	const uint32_t	num_blocks = spdk_max(size, readsize) / bs_blksz;
7931 	const uint32_t	blocks_per_read = spdk_min(size, readsize) / bs_blksz;
7932 	uint32_t	blob_block;
7933 	struct iovec	iov;
7934 	uint8_t		buf[spdk_min(size, readsize)];
7935 	bool		block_ok;
7936 
7937 	SPDK_CU_ASSERT_FATAL(offset % bs_blksz == 0);
7938 	SPDK_CU_ASSERT_FATAL(size % bs_blksz == 0);
7939 	SPDK_CU_ASSERT_FATAL(readsize % bs_blksz == 0);
7940 
7941 	memset(buf, 0, readsize);
7942 	iov.iov_base = buf;
7943 	iov.iov_len = readsize;
7944 	for (blob_block = start_blk; blob_block < num_blocks; blob_block += blocks_per_read) {
7945 		if (strcmp(how, "read") == 0) {
7946 			spdk_blob_io_read(blob, ch, buf, blob_block, blocks_per_read,
7947 					  bs_op_complete, NULL);
7948 		} else if (strcmp(how, "readv") == 0) {
7949 			spdk_blob_io_readv(blob, ch, &iov, 1, blob_block, blocks_per_read,
7950 					   bs_op_complete, NULL);
7951 		} else if (strcmp(how, "readv_ext") == 0) {
7952 			/*
7953 			 * This is currently pointless. NULL ext_opts leads to dev->readv(), not
7954 			 * dev->readv_ext().
7955 			 */
7956 			spdk_blob_io_readv_ext(blob, ch, &iov, 1, blob_block, blocks_per_read,
7957 					       bs_op_complete, NULL, NULL);
7958 		} else {
7959 			abort();
7960 		}
7961 		poll_threads();
7962 		CU_ASSERT(g_bserrno == 0);
7963 		if (g_bserrno != 0) {
7964 			return false;
7965 		}
7966 		block_ok = ut_esnap_content_is_correct(buf, blocks_per_read * bs_blksz, blob->id,
7967 						       blob_block * bs_blksz, esnap_blksz);
7968 		CU_ASSERT(block_ok);
7969 		if (!block_ok) {
7970 			return false;
7971 		}
7972 	}
7973 
7974 	return true;
7975 }
7976 
7977 static void
7978 blob_esnap_io_size(uint32_t bs_blksz, uint32_t esnap_blksz)
7979 {
7980 	struct spdk_bs_dev	*dev;
7981 	struct spdk_blob_store	*bs;
7982 	struct spdk_bs_opts	bsopts;
7983 	struct spdk_blob_opts	opts;
7984 	struct ut_esnap_opts	esnap_opts;
7985 	struct spdk_blob	*blob;
7986 	const uint32_t		cluster_sz = 16 * 1024;
7987 	const uint64_t		esnap_num_clusters = 4;
7988 	const uint32_t		esnap_sz = cluster_sz * esnap_num_clusters;
7989 	const uint64_t		esnap_num_blocks = esnap_sz / esnap_blksz;
7990 	const uint64_t		blob_num_blocks = esnap_sz / bs_blksz;
7991 	uint32_t		block;
7992 	struct spdk_io_channel	*bs_ch;
7993 
7994 	spdk_bs_opts_init(&bsopts, sizeof(bsopts));
7995 	bsopts.cluster_sz = cluster_sz;
7996 	bsopts.esnap_bs_dev_create = ut_esnap_create;
7997 
7998 	/* Create device with desired block size */
7999 	dev = init_dev();
8000 	dev->blocklen = bs_blksz;
8001 	dev->blockcnt = DEV_BUFFER_SIZE / dev->blocklen;
8002 
8003 	/* Initialize a new blob store */
8004 	spdk_bs_init(dev, &bsopts, bs_op_with_handle_complete, NULL);
8005 	poll_threads();
8006 	CU_ASSERT(g_bserrno == 0);
8007 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8008 	SPDK_CU_ASSERT_FATAL(g_bs->io_unit_size == bs_blksz);
8009 	bs = g_bs;
8010 
8011 	bs_ch = spdk_bs_alloc_io_channel(bs);
8012 	SPDK_CU_ASSERT_FATAL(bs_ch != NULL);
8013 
8014 	/* Create and open the esnap clone  */
8015 	ut_spdk_blob_opts_init(&opts);
8016 	ut_esnap_opts_init(esnap_blksz, esnap_num_blocks, __func__, NULL, &esnap_opts);
8017 	opts.esnap_id = &esnap_opts;
8018 	opts.esnap_id_len = sizeof(esnap_opts);
8019 	opts.num_clusters = esnap_num_clusters;
8020 	blob = ut_blob_create_and_open(bs, &opts);
8021 	SPDK_CU_ASSERT_FATAL(blob != NULL);
8022 
8023 	/* Verify that large reads return the content of the esnap device */
8024 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, esnap_sz, "read"));
8025 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, esnap_sz, "readv"));
8026 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, esnap_sz, "readv_ext"));
8027 	/* Verify that small reads return the content of the esnap device */
8028 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, bs_blksz, "read"));
8029 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, bs_blksz, "readv"));
8030 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, bs_blksz, "readv_ext"));
8031 
8032 	/* Write one blob block at a time; verify that the surrounding blocks are OK */
8033 	for (block = 0; block < blob_num_blocks; block++) {
8034 		char		buf[bs_blksz];
8035 		union ut_word	word;
8036 
8037 		word.f.blob_id = 0xfedcba90;
8038 		word.f.lba = block;
8039 		ut_memset8(buf, word.num, bs_blksz);
8040 
8041 		spdk_blob_io_write(blob, bs_ch, buf, block, 1, bs_op_complete, NULL);
8042 		poll_threads();
8043 		CU_ASSERT(g_bserrno == 0);
8044 		if (g_bserrno != 0) {
8045 			break;
8046 		}
8047 
8048 		/* Read and verify the block before the current block */
8049 		if (block != 0) {
8050 			spdk_blob_io_read(blob, bs_ch, buf, block - 1, 1, bs_op_complete, NULL);
8051 			poll_threads();
8052 			CU_ASSERT(g_bserrno == 0);
8053 			if (g_bserrno != 0) {
8054 				break;
8055 			}
8056 			CU_ASSERT(ut_esnap_content_is_correct(buf, bs_blksz, word.f.blob_id,
8057 							      (block - 1) * bs_blksz, bs_blksz));
8058 		}
8059 
8060 		/* Read and verify the current block */
8061 		spdk_blob_io_read(blob, bs_ch, buf, block, 1, bs_op_complete, NULL);
8062 		poll_threads();
8063 		CU_ASSERT(g_bserrno == 0);
8064 		if (g_bserrno != 0) {
8065 			break;
8066 		}
8067 		CU_ASSERT(ut_esnap_content_is_correct(buf, bs_blksz, word.f.blob_id,
8068 						      block * bs_blksz, bs_blksz));
8069 
8070 		/* Check the block that follows */
8071 		if (block + 1 < blob_num_blocks) {
8072 			g_bserrno = 0xbad;
8073 			spdk_blob_io_read(blob, bs_ch, buf, block + 1, 1, bs_op_complete, NULL);
8074 			poll_threads();
8075 			CU_ASSERT(g_bserrno == 0);
8076 			if (g_bserrno != 0) {
8077 				break;
8078 			}
8079 			CU_ASSERT(ut_esnap_content_is_correct(buf, bs_blksz, blob->id,
8080 							      (block + 1) * bs_blksz,
8081 							      esnap_blksz));
8082 		}
8083 	}
8084 
8085 	/* Clean up */
8086 	spdk_bs_free_io_channel(bs_ch);
8087 	g_bserrno = 0xbad;
8088 	spdk_blob_close(blob, blob_op_complete, NULL);
8089 	poll_threads();
8090 	CU_ASSERT(g_bserrno == 0);
8091 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
8092 	poll_threads();
8093 	CU_ASSERT(g_bserrno == 0);
8094 	g_bs = NULL;
8095 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8096 }
8097 
8098 static void
8099 blob_esnap_io_4096_4096(void)
8100 {
8101 	blob_esnap_io_size(4096, 4096);
8102 }
8103 
8104 static void
8105 blob_esnap_io_512_512(void)
8106 {
8107 	blob_esnap_io_size(512, 512);
8108 }
8109 
8110 static void
8111 blob_esnap_io_4096_512(void)
8112 {
8113 	blob_esnap_io_size(4096, 512);
8114 }
8115 
8116 static void
8117 blob_esnap_io_512_4096(void)
8118 {
8119 	struct spdk_bs_dev	*dev;
8120 	struct spdk_blob_store	*bs;
8121 	struct spdk_bs_opts	bs_opts;
8122 	struct spdk_blob_opts	blob_opts;
8123 	struct ut_esnap_opts	esnap_opts;
8124 	uint64_t		cluster_sz = 16 * 1024;
8125 	uint32_t		bs_blksz = 512;
8126 	uint32_t		esnap_blksz = 4096;
8127 	uint64_t		esnap_num_blocks = 64;
8128 	spdk_blob_id		blobid;
8129 
8130 	/* Create device with desired block size */
8131 	dev = init_dev();
8132 	dev->blocklen = bs_blksz;
8133 	dev->blockcnt = DEV_BUFFER_SIZE / dev->blocklen;
8134 
8135 	/* Initialize a new blob store */
8136 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
8137 	bs_opts.cluster_sz = cluster_sz;
8138 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
8139 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
8140 	poll_threads();
8141 	CU_ASSERT(g_bserrno == 0);
8142 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8143 	SPDK_CU_ASSERT_FATAL(g_bs->io_unit_size == bs_blksz);
8144 	bs = g_bs;
8145 
8146 	/* Try to create and open the esnap clone. Create should succeed, open should fail. */
8147 	ut_spdk_blob_opts_init(&blob_opts);
8148 	ut_esnap_opts_init(esnap_blksz, esnap_num_blocks, __func__, NULL, &esnap_opts);
8149 	blob_opts.esnap_id = &esnap_opts;
8150 	blob_opts.esnap_id_len = sizeof(esnap_opts);
8151 	blob_opts.num_clusters = esnap_num_blocks * esnap_blksz / bs_blksz;
8152 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
8153 	poll_threads();
8154 	CU_ASSERT(g_bserrno == 0);
8155 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8156 	blobid = g_blobid;
8157 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8158 	poll_threads();
8159 	CU_ASSERT(g_bserrno == -EINVAL);
8160 	CU_ASSERT(g_blob == NULL);
8161 
8162 	/* Clean up */
8163 	spdk_bs_unload(bs, bs_op_complete, NULL);
8164 	poll_threads();
8165 	CU_ASSERT(g_bserrno == 0);
8166 	g_bs = NULL;
8167 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8168 }
8169 
8170 static void
8171 blob_esnap_thread_add_remove(void)
8172 {
8173 	struct spdk_blob_store	*bs = g_bs;
8174 	struct spdk_blob_opts	opts;
8175 	struct ut_esnap_opts	ut_esnap_opts;
8176 	struct spdk_blob	*blob;
8177 	struct ut_esnap_dev	*ut_dev;
8178 	spdk_blob_id		blobid;
8179 	uint64_t		start_thread = g_ut_thread_id;
8180 	bool			destroyed = false;
8181 	struct spdk_io_channel	*ch0, *ch1;
8182 	struct ut_esnap_channel	*ut_ch0, *ut_ch1;
8183 	const uint32_t		blocklen = bs->io_unit_size;
8184 	char			buf[blocklen * 4];
8185 
8186 	SPDK_CU_ASSERT_FATAL(g_ut_num_threads > 1);
8187 	set_thread(0);
8188 
8189 	/* Create the esnap clone */
8190 	ut_esnap_opts_init(blocklen, 2048, "add_remove_1", &destroyed, &ut_esnap_opts);
8191 	ut_spdk_blob_opts_init(&opts);
8192 	opts.esnap_id = &ut_esnap_opts;
8193 	opts.esnap_id_len = sizeof(ut_esnap_opts);
8194 	opts.num_clusters = 10;
8195 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
8196 	poll_threads();
8197 	CU_ASSERT(g_bserrno == 0);
8198 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8199 	blobid = g_blobid;
8200 
8201 	/* Open the blob. No channels should be allocated yet. */
8202 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8203 	poll_threads();
8204 	CU_ASSERT(g_bserrno == 0);
8205 	CU_ASSERT(g_blob != NULL);
8206 	blob = g_blob;
8207 	ut_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8208 	CU_ASSERT(ut_dev != NULL);
8209 	CU_ASSERT(ut_dev->num_channels == 0);
8210 
8211 	/* Create a channel on thread 0. It is lazily created on the first read. */
8212 	ch0 = spdk_bs_alloc_io_channel(bs);
8213 	CU_ASSERT(ch0 != NULL);
8214 	ut_ch0 = ut_esnap_get_io_channel(ch0, blobid);
8215 	CU_ASSERT(ut_ch0 == NULL);
8216 	CU_ASSERT(ut_dev->num_channels == 0);
8217 	spdk_blob_io_read(blob, ch0, buf, 0, 1, bs_op_complete, NULL);
8218 	poll_threads();
8219 	CU_ASSERT(g_bserrno == 0);
8220 	CU_ASSERT(ut_dev->num_channels == 1);
8221 	ut_ch0 = ut_esnap_get_io_channel(ch0, blobid);
8222 	CU_ASSERT(ut_ch0 != NULL);
8223 	CU_ASSERT(ut_ch0->blocks_read == 1);
8224 
8225 	/* Create a channel on thread 1 and verify its lazy creation too. */
8226 	set_thread(1);
8227 	ch1 = spdk_bs_alloc_io_channel(bs);
8228 	CU_ASSERT(ch1 != NULL);
8229 	ut_ch1 = ut_esnap_get_io_channel(ch1, blobid);
8230 	CU_ASSERT(ut_ch1 == NULL);
8231 	CU_ASSERT(ut_dev->num_channels == 1);
8232 	spdk_blob_io_read(blob, ch1, buf, 0, 4, bs_op_complete, NULL);
8233 	poll_threads();
8234 	CU_ASSERT(g_bserrno == 0);
8235 	CU_ASSERT(ut_dev->num_channels == 2);
8236 	ut_ch1 = ut_esnap_get_io_channel(ch1, blobid);
8237 	CU_ASSERT(ut_ch1 != NULL);
8238 	CU_ASSERT(ut_ch1->blocks_read == 4);
8239 
8240 	/* Close the channel on thread 0 and verify the bs_dev channel is also gone. */
8241 	set_thread(0);
8242 	spdk_bs_free_io_channel(ch0);
8243 	poll_threads();
8244 	CU_ASSERT(ut_dev->num_channels == 1);
8245 
8246 	/* Close the blob. There is no outstanding IO so it should close right away. */
8247 	g_bserrno = 0xbad;
8248 	spdk_blob_close(blob, blob_op_complete, NULL);
8249 	poll_threads();
8250 	CU_ASSERT(g_bserrno == 0);
8251 	CU_ASSERT(destroyed);
8252 
8253 	/* The esnap channel for the blob should be gone now too. */
8254 	ut_ch1 = ut_esnap_get_io_channel(ch1, blobid);
8255 	CU_ASSERT(ut_ch1 == NULL);
8256 
8257 	/* Clean up */
8258 	set_thread(1);
8259 	spdk_bs_free_io_channel(ch1);
8260 	set_thread(start_thread);
8261 }
8262 
8263 static void
8264 freeze_done(void *cb_arg, int bserrno)
8265 {
8266 	uint32_t *freeze_cnt = cb_arg;
8267 
8268 	CU_ASSERT(bserrno == 0);
8269 	(*freeze_cnt)++;
8270 }
8271 
8272 static void
8273 unfreeze_done(void *cb_arg, int bserrno)
8274 {
8275 	uint32_t *unfreeze_cnt = cb_arg;
8276 
8277 	CU_ASSERT(bserrno == 0);
8278 	(*unfreeze_cnt)++;
8279 }
8280 
8281 static void
8282 blob_nested_freezes(void)
8283 {
8284 	struct spdk_blob_store *bs = g_bs;
8285 	struct spdk_blob *blob;
8286 	struct spdk_io_channel *channel[2];
8287 	struct spdk_blob_opts opts;
8288 	uint32_t freeze_cnt, unfreeze_cnt;
8289 	int i;
8290 
8291 	for (i = 0; i < 2; i++) {
8292 		set_thread(i);
8293 		channel[i] = spdk_bs_alloc_io_channel(bs);
8294 		SPDK_CU_ASSERT_FATAL(channel[i] != NULL);
8295 	}
8296 
8297 	set_thread(0);
8298 
8299 	ut_spdk_blob_opts_init(&opts);
8300 	blob = ut_blob_create_and_open(bs, &opts);
8301 
8302 	/* First just test a single freeze/unfreeze. */
8303 	freeze_cnt = 0;
8304 	unfreeze_cnt = 0;
8305 	CU_ASSERT(blob->frozen_refcnt == 0);
8306 	blob_freeze_io(blob, freeze_done, &freeze_cnt);
8307 	CU_ASSERT(blob->frozen_refcnt == 1);
8308 	CU_ASSERT(freeze_cnt == 0);
8309 	poll_threads();
8310 	CU_ASSERT(freeze_cnt == 1);
8311 	blob_unfreeze_io(blob, unfreeze_done, &unfreeze_cnt);
8312 	CU_ASSERT(blob->frozen_refcnt == 0);
8313 	CU_ASSERT(unfreeze_cnt == 0);
8314 	poll_threads();
8315 	CU_ASSERT(unfreeze_cnt == 1);
8316 
8317 	/* Now nest multiple freeze/unfreeze operations.  We should
8318 	 * expect a callback for each operation, but only after
8319 	 * the threads have been polled to ensure a for_each_channel()
8320 	 * was executed.
8321 	 */
8322 	freeze_cnt = 0;
8323 	unfreeze_cnt = 0;
8324 	CU_ASSERT(blob->frozen_refcnt == 0);
8325 	blob_freeze_io(blob, freeze_done, &freeze_cnt);
8326 	CU_ASSERT(blob->frozen_refcnt == 1);
8327 	CU_ASSERT(freeze_cnt == 0);
8328 	blob_freeze_io(blob, freeze_done, &freeze_cnt);
8329 	CU_ASSERT(blob->frozen_refcnt == 2);
8330 	CU_ASSERT(freeze_cnt == 0);
8331 	poll_threads();
8332 	CU_ASSERT(freeze_cnt == 2);
8333 	blob_unfreeze_io(blob, unfreeze_done, &unfreeze_cnt);
8334 	CU_ASSERT(blob->frozen_refcnt == 1);
8335 	CU_ASSERT(unfreeze_cnt == 0);
8336 	blob_unfreeze_io(blob, unfreeze_done, &unfreeze_cnt);
8337 	CU_ASSERT(blob->frozen_refcnt == 0);
8338 	CU_ASSERT(unfreeze_cnt == 0);
8339 	poll_threads();
8340 	CU_ASSERT(unfreeze_cnt == 2);
8341 
8342 	for (i = 0; i < 2; i++) {
8343 		set_thread(i);
8344 		spdk_bs_free_io_channel(channel[i]);
8345 	}
8346 	set_thread(0);
8347 	ut_blob_close_and_delete(bs, blob);
8348 
8349 	poll_threads();
8350 	g_blob = NULL;
8351 	g_blobid = 0;
8352 }
8353 
8354 static void
8355 blob_ext_md_pages(void)
8356 {
8357 	struct spdk_blob_store *bs;
8358 	struct spdk_bs_dev *dev;
8359 	struct spdk_blob *blob;
8360 	struct spdk_blob_opts opts;
8361 	struct spdk_bs_opts bs_opts;
8362 	uint64_t free_clusters;
8363 
8364 	dev = init_dev();
8365 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
8366 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
8367 	/* Issue #2932 was a bug in how we use bs_allocate_cluster() during resize.
8368 	 * It requires num_md_pages that is much smaller than the number of clusters.
8369 	 * Make sure we can create a blob that uses all of the free clusters.
8370 	 */
8371 	bs_opts.cluster_sz = 65536;
8372 	bs_opts.num_md_pages = 16;
8373 
8374 	/* Initialize a new blob store */
8375 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
8376 	poll_threads();
8377 	CU_ASSERT(g_bserrno == 0);
8378 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8379 	bs = g_bs;
8380 
8381 	free_clusters = spdk_bs_free_cluster_count(bs);
8382 
8383 	ut_spdk_blob_opts_init(&opts);
8384 	opts.num_clusters = free_clusters;
8385 
8386 	blob = ut_blob_create_and_open(bs, &opts);
8387 	spdk_blob_close(blob, blob_op_complete, NULL);
8388 	CU_ASSERT(g_bserrno == 0);
8389 
8390 	spdk_bs_unload(bs, bs_op_complete, NULL);
8391 	poll_threads();
8392 	CU_ASSERT(g_bserrno == 0);
8393 	g_bs = NULL;
8394 }
8395 
8396 static void
8397 blob_esnap_clone_snapshot(void)
8398 {
8399 	/*
8400 	 * When a snapshot is created, the blob that is being snapped becomes
8401 	 * the leaf node (a clone of the snapshot) and the newly created
8402 	 * snapshot sits between the snapped blob and the external snapshot.
8403 	 *
8404 	 * Before creating snap1
8405 	 *
8406 	 *   ,--------.     ,----------.
8407 	 *   |  blob  |     |  vbdev   |
8408 	 *   | blob1  |<----| nvme1n42 |
8409 	 *   |  (rw)  |     |   (ro)   |
8410 	 *   `--------'     `----------'
8411 	 *       Figure 1
8412 	 *
8413 	 * After creating snap1
8414 	 *
8415 	 *   ,--------.     ,--------.     ,----------.
8416 	 *   |  blob  |     |  blob  |     |  vbdev   |
8417 	 *   | blob1  |<----| snap1  |<----| nvme1n42 |
8418 	 *   |  (rw)  |     |  (ro)  |     |   (ro)   |
8419 	 *   `--------'     `--------'     `----------'
8420 	 *       Figure 2
8421 	 *
8422 	 * Starting from Figure 2, if snap1 is removed, the chain reverts to
8423 	 * what it looks like in Figure 1.
8424 	 *
8425 	 * Starting from Figure 2, if blob1 is removed, the chain becomes:
8426 	 *
8427 	 *   ,--------.     ,----------.
8428 	 *   |  blob  |     |  vbdev   |
8429 	 *   | snap1  |<----| nvme1n42 |
8430 	 *   |  (ro)  |     |   (ro)   |
8431 	 *   `--------'     `----------'
8432 	 *       Figure 3
8433 	 *
8434 	 * In each case, the blob pointed to by the nvme vbdev is considered
8435 	 * the "esnap clone".  The esnap clone must have:
8436 	 *
8437 	 *   - XATTR_INTERNAL for BLOB_EXTERNAL_SNAPSHOT_ID (e.g. name or UUID)
8438 	 *   - blob->invalid_flags must contain SPDK_BLOB_EXTERNAL_SNAPSHOT
8439 	 *   - blob->parent_id must be SPDK_BLOBID_EXTERNAL_SNAPSHOT.
8440 	 *
8441 	 * No other blob that descends from the esnap clone may have any of
8442 	 * those set.
8443 	 */
8444 	struct spdk_blob_store	*bs = g_bs;
8445 	const uint32_t		blocklen = bs->io_unit_size;
8446 	struct spdk_blob_opts	opts;
8447 	struct ut_esnap_opts	esnap_opts;
8448 	struct spdk_blob	*blob, *snap_blob;
8449 	spdk_blob_id		blobid, snap_blobid;
8450 	bool			destroyed = false;
8451 
8452 	/* Create the esnap clone */
8453 	ut_esnap_opts_init(blocklen, 2048, __func__, &destroyed, &esnap_opts);
8454 	ut_spdk_blob_opts_init(&opts);
8455 	opts.esnap_id = &esnap_opts;
8456 	opts.esnap_id_len = sizeof(esnap_opts);
8457 	opts.num_clusters = 10;
8458 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
8459 	poll_threads();
8460 	CU_ASSERT(g_bserrno == 0);
8461 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8462 	blobid = g_blobid;
8463 
8464 	/* Open the blob. */
8465 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8466 	poll_threads();
8467 	CU_ASSERT(g_bserrno == 0);
8468 	CU_ASSERT(g_blob != NULL);
8469 	blob = g_blob;
8470 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8471 
8472 	/*
8473 	 * Create a snapshot of the blob. The snapshot becomes the esnap clone.
8474 	 */
8475 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
8476 	poll_threads();
8477 	CU_ASSERT(g_bserrno == 0);
8478 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8479 	snap_blobid = g_blobid;
8480 
8481 	spdk_bs_open_blob(bs, snap_blobid, blob_op_with_handle_complete, NULL);
8482 	poll_threads();
8483 	CU_ASSERT(g_bserrno == 0);
8484 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8485 	snap_blob = g_blob;
8486 
8487 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8488 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8489 
8490 	/*
8491 	 * Delete the snapshot.  The original blob becomes the esnap clone.
8492 	 */
8493 	ut_blob_close_and_delete(bs, snap_blob);
8494 	snap_blob = NULL;
8495 	snap_blobid = SPDK_BLOBID_INVALID;
8496 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8497 
8498 	/*
8499 	 * Create the snapshot again, then delete the original blob.  The
8500 	 * snapshot should survive as the esnap clone.
8501 	 */
8502 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
8503 	poll_threads();
8504 	CU_ASSERT(g_bserrno == 0);
8505 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8506 	snap_blobid = g_blobid;
8507 
8508 	spdk_bs_open_blob(bs, snap_blobid, blob_op_with_handle_complete, NULL);
8509 	poll_threads();
8510 	CU_ASSERT(g_bserrno == 0);
8511 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8512 	snap_blob = g_blob;
8513 
8514 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8515 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8516 
8517 	ut_blob_close_and_delete(bs, blob);
8518 	blob = NULL;
8519 	blobid = SPDK_BLOBID_INVALID;
8520 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8521 
8522 	/*
8523 	 * Clone the snapshot.  The snapshot continues to be the esnap clone.
8524 	 */
8525 	spdk_bs_create_clone(bs, snap_blobid, NULL, blob_op_with_id_complete, NULL);
8526 	poll_threads();
8527 	CU_ASSERT(g_bserrno == 0);
8528 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8529 	blobid = g_blobid;
8530 
8531 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8532 	poll_threads();
8533 	CU_ASSERT(g_bserrno == 0);
8534 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8535 	blob = g_blob;
8536 
8537 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8538 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8539 
8540 	/*
8541 	 * Delete the snapshot. The clone becomes the esnap clone.
8542 	 */
8543 	ut_blob_close_and_delete(bs, snap_blob);
8544 	snap_blob = NULL;
8545 	snap_blobid = SPDK_BLOBID_INVALID;
8546 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8547 
8548 	/*
8549 	 * Clean up
8550 	 */
8551 	ut_blob_close_and_delete(bs, blob);
8552 }
8553 
8554 static uint64_t
8555 _blob_esnap_clone_hydrate(bool inflate)
8556 {
8557 	struct spdk_blob_store	*bs = g_bs;
8558 	struct spdk_blob_opts	opts;
8559 	struct ut_esnap_opts	esnap_opts;
8560 	struct spdk_blob	*blob;
8561 	spdk_blob_id		blobid;
8562 	struct spdk_io_channel *channel;
8563 	bool			destroyed = false;
8564 	const uint32_t		blocklen = spdk_bs_get_io_unit_size(bs);
8565 	const uint32_t		cluster_sz = spdk_bs_get_cluster_size(bs);
8566 	const uint64_t		esnap_num_clusters = 4;
8567 	const uint32_t		esnap_sz = cluster_sz * esnap_num_clusters;
8568 	const uint64_t		esnap_num_blocks = esnap_sz / blocklen;
8569 	uint64_t		num_failures = CU_get_number_of_failures();
8570 
8571 	channel = spdk_bs_alloc_io_channel(bs);
8572 	SPDK_CU_ASSERT_FATAL(channel != NULL);
8573 
8574 	/* Create the esnap clone */
8575 	ut_spdk_blob_opts_init(&opts);
8576 	ut_esnap_opts_init(blocklen, esnap_num_blocks, __func__, &destroyed, &esnap_opts);
8577 	opts.esnap_id = &esnap_opts;
8578 	opts.esnap_id_len = sizeof(esnap_opts);
8579 	opts.num_clusters = esnap_num_clusters;
8580 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
8581 	poll_threads();
8582 	CU_ASSERT(g_bserrno == 0);
8583 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8584 	blobid = g_blobid;
8585 
8586 	/* Open the esnap clone */
8587 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8588 	poll_threads();
8589 	CU_ASSERT(g_bserrno == 0);
8590 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8591 	blob = g_blob;
8592 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8593 
8594 	/*
8595 	 * Inflate or decouple  the blob then verify that it is no longer an esnap clone and has
8596 	 * right content
8597 	 */
8598 	if (inflate) {
8599 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
8600 	} else {
8601 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
8602 	}
8603 	poll_threads();
8604 	CU_ASSERT(g_bserrno == 0);
8605 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8606 	CU_ASSERT(blob_esnap_verify_contents(blob, channel, 0, esnap_sz, esnap_sz, "read"));
8607 	ut_blob_close_and_delete(bs, blob);
8608 
8609 	/*
8610 	 * Clean up
8611 	 */
8612 	spdk_bs_free_io_channel(channel);
8613 	poll_threads();
8614 
8615 	/* Return number of new failures */
8616 	return CU_get_number_of_failures() - num_failures;
8617 }
8618 
8619 static void
8620 blob_esnap_clone_inflate(void)
8621 {
8622 	_blob_esnap_clone_hydrate(true);
8623 }
8624 
8625 static void
8626 blob_esnap_clone_decouple(void)
8627 {
8628 	_blob_esnap_clone_hydrate(false);
8629 }
8630 
8631 static void
8632 blob_esnap_hotplug(void)
8633 {
8634 	struct spdk_blob_store	*bs = g_bs;
8635 	struct ut_esnap_opts	esnap1_opts, esnap2_opts;
8636 	struct spdk_blob_opts	opts;
8637 	struct spdk_blob	*blob;
8638 	struct spdk_bs_dev	*bs_dev;
8639 	struct ut_esnap_dev	*esnap_dev;
8640 	uint32_t		cluster_sz = spdk_bs_get_cluster_size(bs);
8641 	uint32_t		block_sz = spdk_bs_get_io_unit_size(bs);
8642 	const uint32_t		esnap_num_clusters = 4;
8643 	uint64_t		esnap_num_blocks = cluster_sz * esnap_num_clusters / block_sz;
8644 	bool			destroyed1 = false, destroyed2 = false;
8645 	uint64_t		start_thread = g_ut_thread_id;
8646 	struct spdk_io_channel	*ch0, *ch1;
8647 	char			buf[block_sz];
8648 
8649 	/* Create and open an esnap clone blob */
8650 	ut_spdk_blob_opts_init(&opts);
8651 	ut_esnap_opts_init(block_sz, esnap_num_blocks, "esnap1", &destroyed1, &esnap1_opts);
8652 	opts.esnap_id = &esnap1_opts;
8653 	opts.esnap_id_len = sizeof(esnap1_opts);
8654 	opts.num_clusters = esnap_num_clusters;
8655 	blob = ut_blob_create_and_open(bs, &opts);
8656 	CU_ASSERT(blob != NULL);
8657 	CU_ASSERT(spdk_blob_is_esnap_clone(blob));
8658 	SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL);
8659 	esnap_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8660 	CU_ASSERT(strcmp(esnap_dev->ut_opts.name, "esnap1") == 0);
8661 
8662 	/* Replace the external snapshot */
8663 	ut_esnap_opts_init(block_sz, esnap_num_blocks, "esnap2", &destroyed2, &esnap2_opts);
8664 	bs_dev = ut_esnap_dev_alloc(&esnap2_opts);
8665 	CU_ASSERT(!destroyed1);
8666 	CU_ASSERT(!destroyed2);
8667 	g_bserrno = 0xbad;
8668 	spdk_blob_set_esnap_bs_dev(blob, bs_dev, bs_op_complete, NULL);
8669 	poll_threads();
8670 	CU_ASSERT(g_bserrno == 0);
8671 	CU_ASSERT(destroyed1);
8672 	CU_ASSERT(!destroyed2);
8673 	SPDK_CU_ASSERT_FATAL(bs_dev == blob->back_bs_dev);
8674 	SPDK_CU_ASSERT_FATAL(bs_dev == spdk_blob_get_esnap_bs_dev(blob));
8675 	esnap_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8676 	CU_ASSERT(strcmp(esnap_dev->ut_opts.name, "esnap2") == 0);
8677 
8678 	/* Create a couple channels */
8679 	set_thread(0);
8680 	ch0 = spdk_bs_alloc_io_channel(bs);
8681 	CU_ASSERT(ch0 != NULL);
8682 	spdk_blob_io_read(blob, ch0, buf, 0, 1, bs_op_complete, NULL);
8683 	set_thread(1);
8684 	ch1 = spdk_bs_alloc_io_channel(bs);
8685 	CU_ASSERT(ch1 != NULL);
8686 	spdk_blob_io_read(blob, ch1, buf, 0, 1, bs_op_complete, NULL);
8687 	set_thread(start_thread);
8688 	poll_threads();
8689 	CU_ASSERT(esnap_dev->num_channels == 2);
8690 
8691 	/* Replace the external snapshot */
8692 	ut_esnap_opts_init(block_sz, esnap_num_blocks, "esnap1a", &destroyed1, &esnap1_opts);
8693 	bs_dev = ut_esnap_dev_alloc(&esnap1_opts);
8694 	destroyed1 = destroyed2 = false;
8695 	g_bserrno = 0xbad;
8696 	spdk_blob_set_esnap_bs_dev(blob, bs_dev, bs_op_complete, NULL);
8697 	poll_threads();
8698 	CU_ASSERT(g_bserrno == 0);
8699 	CU_ASSERT(!destroyed1);
8700 	CU_ASSERT(destroyed2);
8701 	SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL);
8702 	esnap_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8703 	CU_ASSERT(strcmp(esnap_dev->ut_opts.name, "esnap1a") == 0);
8704 
8705 	/* Clean up */
8706 	set_thread(0);
8707 	spdk_bs_free_io_channel(ch0);
8708 	set_thread(1);
8709 	spdk_bs_free_io_channel(ch1);
8710 	set_thread(start_thread);
8711 	g_bserrno = 0xbad;
8712 	spdk_blob_close(blob, bs_op_complete, NULL);
8713 	poll_threads();
8714 	CU_ASSERT(g_bserrno == 0);
8715 }
8716 
8717 static bool g_blob_is_degraded;
8718 static int g_blob_is_degraded_called;
8719 
8720 static bool
8721 _blob_is_degraded(struct spdk_bs_dev *dev)
8722 {
8723 	g_blob_is_degraded_called++;
8724 	return g_blob_is_degraded;
8725 }
8726 
8727 static void
8728 blob_is_degraded(void)
8729 {
8730 	struct spdk_bs_dev bs_is_degraded_null = { 0 };
8731 	struct spdk_bs_dev bs_is_degraded = { .is_degraded = _blob_is_degraded };
8732 
8733 	/* No back_bs_dev, no bs->dev->is_degraded */
8734 	g_blob_is_degraded_called = 0;
8735 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8736 	CU_ASSERT(g_blob_is_degraded_called == 0);
8737 
8738 	/* No back_bs_dev, blobstore device degraded */
8739 	g_bs->dev->is_degraded = _blob_is_degraded;
8740 	g_blob_is_degraded_called = 0;
8741 	g_blob_is_degraded = true;
8742 	CU_ASSERT(spdk_blob_is_degraded(g_blob));
8743 	CU_ASSERT(g_blob_is_degraded_called == 1);
8744 
8745 	/* No back_bs_dev, blobstore device not degraded */
8746 	g_bs->dev->is_degraded = _blob_is_degraded;
8747 	g_blob_is_degraded_called = 0;
8748 	g_blob_is_degraded = false;
8749 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8750 	CU_ASSERT(g_blob_is_degraded_called == 1);
8751 
8752 	/* back_bs_dev does not define is_degraded, no bs->dev->is_degraded */
8753 	g_bs->dev->is_degraded = NULL;
8754 	g_blob->back_bs_dev = &bs_is_degraded_null;
8755 	g_blob_is_degraded_called = 0;
8756 	g_blob_is_degraded = false;
8757 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8758 	CU_ASSERT(g_blob_is_degraded_called == 0);
8759 
8760 	/* back_bs_dev is not degraded, no bs->dev->is_degraded */
8761 	g_bs->dev->is_degraded = NULL;
8762 	g_blob->back_bs_dev = &bs_is_degraded;
8763 	g_blob_is_degraded_called = 0;
8764 	g_blob_is_degraded = false;
8765 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8766 	CU_ASSERT(g_blob_is_degraded_called == 1);
8767 
8768 	/* back_bs_dev is degraded, no bs->dev->is_degraded */
8769 	g_bs->dev->is_degraded = NULL;
8770 	g_blob->back_bs_dev = &bs_is_degraded;
8771 	g_blob_is_degraded_called = 0;
8772 	g_blob_is_degraded = true;
8773 	CU_ASSERT(spdk_blob_is_degraded(g_blob));
8774 	CU_ASSERT(g_blob_is_degraded_called == 1);
8775 
8776 	/* back_bs_dev is not degraded, blobstore device is not degraded */
8777 	g_bs->dev->is_degraded = _blob_is_degraded;
8778 	g_blob->back_bs_dev = &bs_is_degraded;
8779 	g_blob_is_degraded_called = 0;
8780 	g_blob_is_degraded = false;
8781 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8782 	CU_ASSERT(g_blob_is_degraded_called == 2);
8783 
8784 	g_blob->back_bs_dev = NULL;
8785 }
8786 
8787 static void
8788 suite_bs_setup(void)
8789 {
8790 	struct spdk_bs_dev *dev;
8791 
8792 	dev = init_dev();
8793 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8794 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
8795 	poll_threads();
8796 	CU_ASSERT(g_bserrno == 0);
8797 	CU_ASSERT(g_bs != NULL);
8798 }
8799 
8800 static void
8801 suite_esnap_bs_setup(void)
8802 {
8803 	struct spdk_bs_dev	*dev;
8804 	struct spdk_bs_opts	bs_opts;
8805 
8806 	dev = init_dev();
8807 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8808 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
8809 	bs_opts.cluster_sz = 16 * 1024;
8810 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
8811 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
8812 	poll_threads();
8813 	CU_ASSERT(g_bserrno == 0);
8814 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8815 }
8816 
8817 static void
8818 suite_bs_cleanup(void)
8819 {
8820 	if (g_bs != NULL) {
8821 		spdk_bs_unload(g_bs, bs_op_complete, NULL);
8822 		poll_threads();
8823 		CU_ASSERT(g_bserrno == 0);
8824 		g_bs = NULL;
8825 	}
8826 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8827 }
8828 
8829 static struct spdk_blob *
8830 ut_blob_create_and_open(struct spdk_blob_store *bs, struct spdk_blob_opts *blob_opts)
8831 {
8832 	struct spdk_blob *blob;
8833 	struct spdk_blob_opts create_blob_opts;
8834 	spdk_blob_id blobid;
8835 
8836 	if (blob_opts == NULL) {
8837 		ut_spdk_blob_opts_init(&create_blob_opts);
8838 		blob_opts = &create_blob_opts;
8839 	}
8840 
8841 	spdk_bs_create_blob_ext(bs, blob_opts, blob_op_with_id_complete, NULL);
8842 	poll_threads();
8843 	CU_ASSERT(g_bserrno == 0);
8844 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8845 	blobid = g_blobid;
8846 	g_blobid = -1;
8847 
8848 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8849 	poll_threads();
8850 	CU_ASSERT(g_bserrno == 0);
8851 	CU_ASSERT(g_blob != NULL);
8852 	blob = g_blob;
8853 
8854 	g_blob = NULL;
8855 	g_bserrno = -1;
8856 
8857 	return blob;
8858 }
8859 
8860 static void
8861 ut_blob_close_and_delete(struct spdk_blob_store *bs, struct spdk_blob *blob)
8862 {
8863 	spdk_blob_id blobid = spdk_blob_get_id(blob);
8864 
8865 	spdk_blob_close(blob, blob_op_complete, NULL);
8866 	poll_threads();
8867 	CU_ASSERT(g_bserrno == 0);
8868 	g_blob = NULL;
8869 
8870 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
8871 	poll_threads();
8872 	CU_ASSERT(g_bserrno == 0);
8873 	g_bserrno = -1;
8874 }
8875 
8876 static void
8877 suite_blob_setup(void)
8878 {
8879 	suite_bs_setup();
8880 	CU_ASSERT(g_bs != NULL);
8881 
8882 	g_blob = ut_blob_create_and_open(g_bs, NULL);
8883 	CU_ASSERT(g_blob != NULL);
8884 }
8885 
8886 static void
8887 suite_blob_cleanup(void)
8888 {
8889 	ut_blob_close_and_delete(g_bs, g_blob);
8890 	CU_ASSERT(g_blob == NULL);
8891 
8892 	suite_bs_cleanup();
8893 	CU_ASSERT(g_bs == NULL);
8894 }
8895 
8896 static int
8897 ut_setup_config_nocopy_noextent(void)
8898 {
8899 	g_dev_copy_enabled = false;
8900 	g_use_extent_table = false;
8901 
8902 	return 0;
8903 }
8904 
8905 static int
8906 ut_setup_config_nocopy_extent(void)
8907 {
8908 	g_dev_copy_enabled = false;
8909 	g_use_extent_table = true;
8910 
8911 	return 0;
8912 }
8913 
8914 static int
8915 ut_setup_config_copy_noextent(void)
8916 {
8917 	g_dev_copy_enabled = true;
8918 	g_use_extent_table = false;
8919 
8920 	return 0;
8921 }
8922 
8923 static int
8924 ut_setup_config_copy_extent(void)
8925 {
8926 	g_dev_copy_enabled = true;
8927 	g_use_extent_table = true;
8928 
8929 	return 0;
8930 }
8931 
8932 struct ut_config {
8933 	const char *suffix;
8934 	CU_InitializeFunc setup_cb;
8935 };
8936 
8937 int
8938 main(int argc, char **argv)
8939 {
8940 	CU_pSuite		suite, suite_bs, suite_blob, suite_esnap_bs;
8941 	unsigned int		i, num_failures;
8942 	char			suite_name[4096];
8943 	struct ut_config	*config;
8944 	struct ut_config	configs[] = {
8945 		{"nocopy_noextent", ut_setup_config_nocopy_noextent},
8946 		{"nocopy_extent", ut_setup_config_nocopy_extent},
8947 		{"copy_noextent", ut_setup_config_copy_noextent},
8948 		{"copy_extent", ut_setup_config_copy_extent},
8949 	};
8950 
8951 	CU_initialize_registry();
8952 
8953 	for (i = 0; i < SPDK_COUNTOF(configs); ++i) {
8954 		config = &configs[i];
8955 
8956 		snprintf(suite_name, sizeof(suite_name), "blob_%s", config->suffix);
8957 		suite = CU_add_suite(suite_name, config->setup_cb, NULL);
8958 
8959 		snprintf(suite_name, sizeof(suite_name), "blob_bs_%s", config->suffix);
8960 		suite_bs = CU_add_suite_with_setup_and_teardown(suite_name, config->setup_cb, NULL,
8961 				suite_bs_setup, suite_bs_cleanup);
8962 
8963 		snprintf(suite_name, sizeof(suite_name), "blob_blob_%s", config->suffix);
8964 		suite_blob = CU_add_suite_with_setup_and_teardown(suite_name, config->setup_cb, NULL,
8965 				suite_blob_setup, suite_blob_cleanup);
8966 
8967 		snprintf(suite_name, sizeof(suite_name), "blob_esnap_bs_%s", config->suffix);
8968 		suite_esnap_bs = CU_add_suite_with_setup_and_teardown(suite_name, config->setup_cb, NULL,
8969 				 suite_esnap_bs_setup,
8970 				 suite_bs_cleanup);
8971 
8972 		CU_ADD_TEST(suite, blob_init);
8973 		CU_ADD_TEST(suite_bs, blob_open);
8974 		CU_ADD_TEST(suite_bs, blob_create);
8975 		CU_ADD_TEST(suite_bs, blob_create_loop);
8976 		CU_ADD_TEST(suite_bs, blob_create_fail);
8977 		CU_ADD_TEST(suite_bs, blob_create_internal);
8978 		CU_ADD_TEST(suite_bs, blob_create_zero_extent);
8979 		CU_ADD_TEST(suite, blob_thin_provision);
8980 		CU_ADD_TEST(suite_bs, blob_snapshot);
8981 		CU_ADD_TEST(suite_bs, blob_clone);
8982 		CU_ADD_TEST(suite_bs, blob_inflate);
8983 		CU_ADD_TEST(suite_bs, blob_delete);
8984 		CU_ADD_TEST(suite_bs, blob_resize_test);
8985 		CU_ADD_TEST(suite, blob_read_only);
8986 		CU_ADD_TEST(suite_bs, channel_ops);
8987 		CU_ADD_TEST(suite_bs, blob_super);
8988 		CU_ADD_TEST(suite_blob, blob_write);
8989 		CU_ADD_TEST(suite_blob, blob_read);
8990 		CU_ADD_TEST(suite_blob, blob_rw_verify);
8991 		CU_ADD_TEST(suite_bs, blob_rw_verify_iov);
8992 		CU_ADD_TEST(suite_blob, blob_rw_verify_iov_nomem);
8993 		CU_ADD_TEST(suite_blob, blob_rw_iov_read_only);
8994 		CU_ADD_TEST(suite_bs, blob_unmap);
8995 		CU_ADD_TEST(suite_bs, blob_iter);
8996 		CU_ADD_TEST(suite_blob, blob_xattr);
8997 		CU_ADD_TEST(suite_bs, blob_parse_md);
8998 		CU_ADD_TEST(suite, bs_load);
8999 		CU_ADD_TEST(suite_bs, bs_load_pending_removal);
9000 		CU_ADD_TEST(suite, bs_load_custom_cluster_size);
9001 		CU_ADD_TEST(suite, bs_load_after_failed_grow);
9002 		CU_ADD_TEST(suite_bs, bs_unload);
9003 		CU_ADD_TEST(suite, bs_cluster_sz);
9004 		CU_ADD_TEST(suite_bs, bs_usable_clusters);
9005 		CU_ADD_TEST(suite, bs_resize_md);
9006 		CU_ADD_TEST(suite, bs_destroy);
9007 		CU_ADD_TEST(suite, bs_type);
9008 		CU_ADD_TEST(suite, bs_super_block);
9009 		CU_ADD_TEST(suite, bs_test_recover_cluster_count);
9010 		CU_ADD_TEST(suite, bs_grow_live);
9011 		CU_ADD_TEST(suite, bs_grow_live_no_space);
9012 		CU_ADD_TEST(suite, bs_test_grow);
9013 		CU_ADD_TEST(suite, blob_serialize_test);
9014 		CU_ADD_TEST(suite_bs, blob_crc);
9015 		CU_ADD_TEST(suite, super_block_crc);
9016 		CU_ADD_TEST(suite_blob, blob_dirty_shutdown);
9017 		CU_ADD_TEST(suite_bs, blob_flags);
9018 		CU_ADD_TEST(suite_bs, bs_version);
9019 		CU_ADD_TEST(suite_bs, blob_set_xattrs_test);
9020 		CU_ADD_TEST(suite_bs, blob_thin_prov_alloc);
9021 		CU_ADD_TEST(suite_bs, blob_insert_cluster_msg_test);
9022 		CU_ADD_TEST(suite_bs, blob_thin_prov_rw);
9023 		CU_ADD_TEST(suite, blob_thin_prov_write_count_io);
9024 		CU_ADD_TEST(suite_bs, blob_thin_prov_rle);
9025 		CU_ADD_TEST(suite_bs, blob_thin_prov_rw_iov);
9026 		CU_ADD_TEST(suite, bs_load_iter_test);
9027 		CU_ADD_TEST(suite_bs, blob_snapshot_rw);
9028 		CU_ADD_TEST(suite_bs, blob_snapshot_rw_iov);
9029 		CU_ADD_TEST(suite, blob_relations);
9030 		CU_ADD_TEST(suite, blob_relations2);
9031 		CU_ADD_TEST(suite, blob_relations3);
9032 		CU_ADD_TEST(suite, blobstore_clean_power_failure);
9033 		CU_ADD_TEST(suite, blob_delete_snapshot_power_failure);
9034 		CU_ADD_TEST(suite, blob_create_snapshot_power_failure);
9035 		CU_ADD_TEST(suite_bs, blob_inflate_rw);
9036 		CU_ADD_TEST(suite_bs, blob_snapshot_freeze_io);
9037 		CU_ADD_TEST(suite_bs, blob_operation_split_rw);
9038 		CU_ADD_TEST(suite_bs, blob_operation_split_rw_iov);
9039 		CU_ADD_TEST(suite, blob_io_unit);
9040 		CU_ADD_TEST(suite, blob_io_unit_compatibility);
9041 		CU_ADD_TEST(suite_bs, blob_simultaneous_operations);
9042 		CU_ADD_TEST(suite_bs, blob_persist_test);
9043 		CU_ADD_TEST(suite_bs, blob_decouple_snapshot);
9044 		CU_ADD_TEST(suite_bs, blob_seek_io_unit);
9045 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_create);
9046 		CU_ADD_TEST(suite_bs, blob_nested_freezes);
9047 		CU_ADD_TEST(suite, blob_ext_md_pages);
9048 		CU_ADD_TEST(suite, blob_esnap_io_4096_4096);
9049 		CU_ADD_TEST(suite, blob_esnap_io_512_512);
9050 		CU_ADD_TEST(suite, blob_esnap_io_4096_512);
9051 		CU_ADD_TEST(suite, blob_esnap_io_512_4096);
9052 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_thread_add_remove);
9053 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_snapshot);
9054 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_inflate);
9055 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_decouple);
9056 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_reload);
9057 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_hotplug);
9058 		CU_ADD_TEST(suite_blob, blob_is_degraded);
9059 	}
9060 
9061 	allocate_threads(2);
9062 	set_thread(0);
9063 
9064 	g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
9065 
9066 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
9067 
9068 	free(g_dev_buffer);
9069 
9070 	free_threads();
9071 
9072 	return num_failures;
9073 }
9074