xref: /spdk/test/unit/lib/blob/blob.c/blob_ut.c (revision 23dbcec57d9eec63b3730054dba9e9a0a500a077)
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 
4514 	ut_blob_close_and_delete(bs, blob);
4515 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4516 
4517 	spdk_bs_free_io_channel(ch);
4518 	poll_threads();
4519 	g_blob = NULL;
4520 	g_blobid = 0;
4521 
4522 	spdk_bs_unload(bs, bs_op_complete, NULL);
4523 	poll_threads();
4524 	CU_ASSERT(g_bserrno == 0);
4525 	g_bs = NULL;
4526 }
4527 
4528 static void
4529 blob_thin_prov_rle(void)
4530 {
4531 	static const uint8_t zero[10 * 4096] = { 0 };
4532 	struct spdk_blob_store *bs = g_bs;
4533 	struct spdk_blob *blob;
4534 	struct spdk_io_channel *channel;
4535 	struct spdk_blob_opts opts;
4536 	spdk_blob_id blobid;
4537 	uint64_t free_clusters;
4538 	uint64_t page_size;
4539 	uint8_t payload_read[10 * 4096];
4540 	uint8_t payload_write[10 * 4096];
4541 	uint64_t write_bytes;
4542 	uint64_t read_bytes;
4543 	uint64_t io_unit;
4544 
4545 	free_clusters = spdk_bs_free_cluster_count(bs);
4546 	page_size = spdk_bs_get_page_size(bs);
4547 
4548 	ut_spdk_blob_opts_init(&opts);
4549 	opts.thin_provision = true;
4550 	opts.num_clusters = 5;
4551 
4552 	blob = ut_blob_create_and_open(bs, &opts);
4553 	blobid = spdk_blob_get_id(blob);
4554 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4555 
4556 	channel = spdk_bs_alloc_io_channel(bs);
4557 	CU_ASSERT(channel != NULL);
4558 
4559 	/* Target specifically second cluster in a blob as first allocation */
4560 	io_unit = bs_cluster_to_page(bs, 1) * bs_io_unit_per_page(bs);
4561 
4562 	/* Payload should be all zeros from unallocated clusters */
4563 	memset(payload_read, 0xFF, sizeof(payload_read));
4564 	spdk_blob_io_read(blob, channel, payload_read, io_unit, 10, blob_op_complete, NULL);
4565 	poll_threads();
4566 	CU_ASSERT(g_bserrno == 0);
4567 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4568 
4569 	write_bytes = g_dev_write_bytes;
4570 	read_bytes = g_dev_read_bytes;
4571 
4572 	/* Issue write to second cluster in a blob */
4573 	memset(payload_write, 0xE5, sizeof(payload_write));
4574 	spdk_blob_io_write(blob, channel, payload_write, io_unit, 10, blob_op_complete, NULL);
4575 	poll_threads();
4576 	CU_ASSERT(g_bserrno == 0);
4577 	CU_ASSERT(free_clusters - 1 == spdk_bs_free_cluster_count(bs));
4578 	/* For thin-provisioned blob we need to write 10 pages plus one page metadata and
4579 	 * read 0 bytes */
4580 	if (g_use_extent_table) {
4581 		/* Add one more page for EXTENT_PAGE write */
4582 		CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 12);
4583 	} else {
4584 		CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 11);
4585 	}
4586 	CU_ASSERT(g_dev_read_bytes - read_bytes == 0);
4587 
4588 	spdk_blob_io_read(blob, channel, payload_read, io_unit, 10, blob_op_complete, NULL);
4589 	poll_threads();
4590 	CU_ASSERT(g_bserrno == 0);
4591 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4592 
4593 	spdk_bs_free_io_channel(channel);
4594 	poll_threads();
4595 
4596 	spdk_blob_close(blob, blob_op_complete, NULL);
4597 	poll_threads();
4598 	CU_ASSERT(g_bserrno == 0);
4599 
4600 	ut_bs_reload(&bs, NULL);
4601 
4602 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4603 	poll_threads();
4604 	CU_ASSERT(g_bserrno == 0);
4605 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4606 	blob = g_blob;
4607 
4608 	channel = spdk_bs_alloc_io_channel(bs);
4609 	CU_ASSERT(channel != NULL);
4610 
4611 	/* Read second cluster after blob reload to confirm data written */
4612 	spdk_blob_io_read(blob, channel, payload_read, io_unit, 10, blob_op_complete, NULL);
4613 	poll_threads();
4614 	CU_ASSERT(g_bserrno == 0);
4615 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4616 
4617 	spdk_bs_free_io_channel(channel);
4618 	poll_threads();
4619 
4620 	ut_blob_close_and_delete(bs, blob);
4621 }
4622 
4623 static void
4624 blob_thin_prov_rw_iov(void)
4625 {
4626 	static const uint8_t zero[10 * 4096] = { 0 };
4627 	struct spdk_blob_store *bs = g_bs;
4628 	struct spdk_blob *blob;
4629 	struct spdk_io_channel *channel;
4630 	struct spdk_blob_opts opts;
4631 	uint64_t free_clusters;
4632 	uint8_t payload_read[10 * 4096];
4633 	uint8_t payload_write[10 * 4096];
4634 	struct iovec iov_read[3];
4635 	struct iovec iov_write[3];
4636 
4637 	free_clusters = spdk_bs_free_cluster_count(bs);
4638 
4639 	channel = spdk_bs_alloc_io_channel(bs);
4640 	CU_ASSERT(channel != NULL);
4641 
4642 	ut_spdk_blob_opts_init(&opts);
4643 	opts.thin_provision = true;
4644 
4645 	blob = ut_blob_create_and_open(bs, &opts);
4646 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4647 
4648 	CU_ASSERT(blob->active.num_clusters == 0);
4649 
4650 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
4651 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
4652 	poll_threads();
4653 	CU_ASSERT(g_bserrno == 0);
4654 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4655 	CU_ASSERT(blob->active.num_clusters == 5);
4656 
4657 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4658 	poll_threads();
4659 	CU_ASSERT(g_bserrno == 0);
4660 	/* Sync must not change anything */
4661 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4662 	CU_ASSERT(blob->active.num_clusters == 5);
4663 
4664 	/* Payload should be all zeros from unallocated clusters */
4665 	memset(payload_read, 0xAA, sizeof(payload_read));
4666 	iov_read[0].iov_base = payload_read;
4667 	iov_read[0].iov_len = 3 * 4096;
4668 	iov_read[1].iov_base = payload_read + 3 * 4096;
4669 	iov_read[1].iov_len = 4 * 4096;
4670 	iov_read[2].iov_base = payload_read + 7 * 4096;
4671 	iov_read[2].iov_len = 3 * 4096;
4672 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4673 	poll_threads();
4674 	CU_ASSERT(g_bserrno == 0);
4675 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4676 
4677 	memset(payload_write, 0xE5, sizeof(payload_write));
4678 	iov_write[0].iov_base = payload_write;
4679 	iov_write[0].iov_len = 1 * 4096;
4680 	iov_write[1].iov_base = payload_write + 1 * 4096;
4681 	iov_write[1].iov_len = 5 * 4096;
4682 	iov_write[2].iov_base = payload_write + 6 * 4096;
4683 	iov_write[2].iov_len = 4 * 4096;
4684 
4685 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
4686 	poll_threads();
4687 	CU_ASSERT(g_bserrno == 0);
4688 
4689 	memset(payload_read, 0xAA, sizeof(payload_read));
4690 	iov_read[0].iov_base = payload_read;
4691 	iov_read[0].iov_len = 3 * 4096;
4692 	iov_read[1].iov_base = payload_read + 3 * 4096;
4693 	iov_read[1].iov_len = 4 * 4096;
4694 	iov_read[2].iov_base = payload_read + 7 * 4096;
4695 	iov_read[2].iov_len = 3 * 4096;
4696 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4697 	poll_threads();
4698 	CU_ASSERT(g_bserrno == 0);
4699 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4700 
4701 	spdk_bs_free_io_channel(channel);
4702 	poll_threads();
4703 
4704 	ut_blob_close_and_delete(bs, blob);
4705 }
4706 
4707 struct iter_ctx {
4708 	int		current_iter;
4709 	spdk_blob_id	blobid[4];
4710 };
4711 
4712 static void
4713 test_iter(void *arg, struct spdk_blob *blob, int bserrno)
4714 {
4715 	struct iter_ctx *iter_ctx = arg;
4716 	spdk_blob_id blobid;
4717 
4718 	CU_ASSERT(bserrno == 0);
4719 	blobid = spdk_blob_get_id(blob);
4720 	CU_ASSERT(blobid == iter_ctx->blobid[iter_ctx->current_iter++]);
4721 }
4722 
4723 static void
4724 bs_load_iter_test(void)
4725 {
4726 	struct spdk_blob_store *bs;
4727 	struct spdk_bs_dev *dev;
4728 	struct iter_ctx iter_ctx = { 0 };
4729 	struct spdk_blob *blob;
4730 	int i, rc;
4731 	struct spdk_bs_opts opts;
4732 
4733 	dev = init_dev();
4734 	spdk_bs_opts_init(&opts, sizeof(opts));
4735 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4736 
4737 	/* Initialize a new blob store */
4738 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
4739 	poll_threads();
4740 	CU_ASSERT(g_bserrno == 0);
4741 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4742 	bs = g_bs;
4743 
4744 	for (i = 0; i < 4; i++) {
4745 		blob = ut_blob_create_and_open(bs, NULL);
4746 		iter_ctx.blobid[i] = spdk_blob_get_id(blob);
4747 
4748 		/* Just save the blobid as an xattr for testing purposes. */
4749 		rc = spdk_blob_set_xattr(blob, "blobid", &iter_ctx.blobid[i], sizeof(spdk_blob_id));
4750 		CU_ASSERT(rc == 0);
4751 
4752 		/* Resize the blob */
4753 		spdk_blob_resize(blob, i, blob_op_complete, NULL);
4754 		poll_threads();
4755 		CU_ASSERT(g_bserrno == 0);
4756 
4757 		spdk_blob_close(blob, blob_op_complete, NULL);
4758 		poll_threads();
4759 		CU_ASSERT(g_bserrno == 0);
4760 	}
4761 
4762 	g_bserrno = -1;
4763 	spdk_bs_unload(bs, bs_op_complete, NULL);
4764 	poll_threads();
4765 	CU_ASSERT(g_bserrno == 0);
4766 
4767 	dev = init_dev();
4768 	spdk_bs_opts_init(&opts, sizeof(opts));
4769 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4770 	opts.iter_cb_fn = test_iter;
4771 	opts.iter_cb_arg = &iter_ctx;
4772 
4773 	/* Test blob iteration during load after a clean shutdown. */
4774 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
4775 	poll_threads();
4776 	CU_ASSERT(g_bserrno == 0);
4777 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4778 	bs = g_bs;
4779 
4780 	/* Dirty shutdown */
4781 	bs_free(bs);
4782 
4783 	dev = init_dev();
4784 	spdk_bs_opts_init(&opts, sizeof(opts));
4785 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4786 	opts.iter_cb_fn = test_iter;
4787 	iter_ctx.current_iter = 0;
4788 	opts.iter_cb_arg = &iter_ctx;
4789 
4790 	/* Test blob iteration during load after a dirty shutdown. */
4791 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
4792 	poll_threads();
4793 	CU_ASSERT(g_bserrno == 0);
4794 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4795 	bs = g_bs;
4796 
4797 	spdk_bs_unload(bs, bs_op_complete, NULL);
4798 	poll_threads();
4799 	CU_ASSERT(g_bserrno == 0);
4800 	g_bs = NULL;
4801 }
4802 
4803 static void
4804 blob_snapshot_rw(void)
4805 {
4806 	static const uint8_t zero[10 * 4096] = { 0 };
4807 	struct spdk_blob_store *bs = g_bs;
4808 	struct spdk_blob *blob, *snapshot;
4809 	struct spdk_io_channel *channel;
4810 	struct spdk_blob_opts opts;
4811 	spdk_blob_id blobid, snapshotid;
4812 	uint64_t free_clusters;
4813 	uint64_t cluster_size;
4814 	uint64_t page_size;
4815 	uint8_t payload_read[10 * 4096];
4816 	uint8_t payload_write[10 * 4096];
4817 	uint64_t write_bytes_start;
4818 	uint64_t read_bytes_start;
4819 	uint64_t copy_bytes_start;
4820 	uint64_t write_bytes;
4821 	uint64_t read_bytes;
4822 	uint64_t copy_bytes;
4823 
4824 	free_clusters = spdk_bs_free_cluster_count(bs);
4825 	cluster_size = spdk_bs_get_cluster_size(bs);
4826 	page_size = spdk_bs_get_page_size(bs);
4827 
4828 	channel = spdk_bs_alloc_io_channel(bs);
4829 	CU_ASSERT(channel != NULL);
4830 
4831 	ut_spdk_blob_opts_init(&opts);
4832 	opts.thin_provision = true;
4833 	opts.num_clusters = 5;
4834 
4835 	blob = ut_blob_create_and_open(bs, &opts);
4836 	blobid = spdk_blob_get_id(blob);
4837 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4838 
4839 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4840 
4841 	memset(payload_read, 0xFF, sizeof(payload_read));
4842 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4843 	poll_threads();
4844 	CU_ASSERT(g_bserrno == 0);
4845 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4846 
4847 	memset(payload_write, 0xE5, sizeof(payload_write));
4848 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4849 	poll_threads();
4850 	CU_ASSERT(g_bserrno == 0);
4851 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4852 
4853 	/* Create snapshot from blob */
4854 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4855 	poll_threads();
4856 	CU_ASSERT(g_bserrno == 0);
4857 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4858 	snapshotid = g_blobid;
4859 
4860 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4861 	poll_threads();
4862 	CU_ASSERT(g_bserrno == 0);
4863 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4864 	snapshot = g_blob;
4865 	CU_ASSERT(snapshot->data_ro == true);
4866 	CU_ASSERT(snapshot->md_ro == true);
4867 
4868 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
4869 
4870 	write_bytes_start = g_dev_write_bytes;
4871 	read_bytes_start = g_dev_read_bytes;
4872 	copy_bytes_start = g_dev_copy_bytes;
4873 
4874 	memset(payload_write, 0xAA, sizeof(payload_write));
4875 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4876 	poll_threads();
4877 	CU_ASSERT(g_bserrno == 0);
4878 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4879 
4880 	/* For a clone we need to allocate and copy one cluster, update one page of metadata
4881 	 * and then write 10 pages of payload.
4882 	 */
4883 	write_bytes = g_dev_write_bytes - write_bytes_start;
4884 	read_bytes = g_dev_read_bytes - read_bytes_start;
4885 	copy_bytes = g_dev_copy_bytes - copy_bytes_start;
4886 	if (g_dev_copy_enabled) {
4887 		CU_ASSERT(copy_bytes == cluster_size);
4888 	} else {
4889 		CU_ASSERT(copy_bytes == 0);
4890 	}
4891 	if (g_use_extent_table) {
4892 		/* Add one more page for EXTENT_PAGE write */
4893 		CU_ASSERT(write_bytes + copy_bytes == page_size * 12 + cluster_size);
4894 	} else {
4895 		CU_ASSERT(write_bytes + copy_bytes == page_size * 11 + cluster_size);
4896 	}
4897 	CU_ASSERT(read_bytes + copy_bytes == cluster_size);
4898 
4899 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4900 	poll_threads();
4901 	CU_ASSERT(g_bserrno == 0);
4902 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4903 
4904 	/* Data on snapshot should not change after write to clone */
4905 	memset(payload_write, 0xE5, sizeof(payload_write));
4906 	spdk_blob_io_read(snapshot, channel, payload_read, 4, 10, blob_op_complete, NULL);
4907 	poll_threads();
4908 	CU_ASSERT(g_bserrno == 0);
4909 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4910 
4911 	ut_blob_close_and_delete(bs, blob);
4912 	ut_blob_close_and_delete(bs, snapshot);
4913 
4914 	spdk_bs_free_io_channel(channel);
4915 	poll_threads();
4916 	g_blob = NULL;
4917 	g_blobid = 0;
4918 }
4919 
4920 static void
4921 blob_snapshot_rw_iov(void)
4922 {
4923 	static const uint8_t zero[10 * 4096] = { 0 };
4924 	struct spdk_blob_store *bs = g_bs;
4925 	struct spdk_blob *blob, *snapshot;
4926 	struct spdk_io_channel *channel;
4927 	struct spdk_blob_opts opts;
4928 	spdk_blob_id blobid, snapshotid;
4929 	uint64_t free_clusters;
4930 	uint8_t payload_read[10 * 4096];
4931 	uint8_t payload_write[10 * 4096];
4932 	struct iovec iov_read[3];
4933 	struct iovec iov_write[3];
4934 
4935 	free_clusters = spdk_bs_free_cluster_count(bs);
4936 
4937 	channel = spdk_bs_alloc_io_channel(bs);
4938 	CU_ASSERT(channel != NULL);
4939 
4940 	ut_spdk_blob_opts_init(&opts);
4941 	opts.thin_provision = true;
4942 	opts.num_clusters = 5;
4943 
4944 	blob = ut_blob_create_and_open(bs, &opts);
4945 	blobid = spdk_blob_get_id(blob);
4946 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4947 
4948 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4949 
4950 	/* Create snapshot from blob */
4951 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4952 	poll_threads();
4953 	CU_ASSERT(g_bserrno == 0);
4954 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4955 	snapshotid = g_blobid;
4956 
4957 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4958 	poll_threads();
4959 	CU_ASSERT(g_bserrno == 0);
4960 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4961 	snapshot = g_blob;
4962 	CU_ASSERT(snapshot->data_ro == true);
4963 	CU_ASSERT(snapshot->md_ro == true);
4964 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
4965 
4966 	/* Payload should be all zeros from unallocated clusters */
4967 	memset(payload_read, 0xAA, sizeof(payload_read));
4968 	iov_read[0].iov_base = payload_read;
4969 	iov_read[0].iov_len = 3 * 4096;
4970 	iov_read[1].iov_base = payload_read + 3 * 4096;
4971 	iov_read[1].iov_len = 4 * 4096;
4972 	iov_read[2].iov_base = payload_read + 7 * 4096;
4973 	iov_read[2].iov_len = 3 * 4096;
4974 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4975 	poll_threads();
4976 	CU_ASSERT(g_bserrno == 0);
4977 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4978 
4979 	memset(payload_write, 0xE5, sizeof(payload_write));
4980 	iov_write[0].iov_base = payload_write;
4981 	iov_write[0].iov_len = 1 * 4096;
4982 	iov_write[1].iov_base = payload_write + 1 * 4096;
4983 	iov_write[1].iov_len = 5 * 4096;
4984 	iov_write[2].iov_base = payload_write + 6 * 4096;
4985 	iov_write[2].iov_len = 4 * 4096;
4986 
4987 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
4988 	poll_threads();
4989 	CU_ASSERT(g_bserrno == 0);
4990 
4991 	memset(payload_read, 0xAA, sizeof(payload_read));
4992 	iov_read[0].iov_base = payload_read;
4993 	iov_read[0].iov_len = 3 * 4096;
4994 	iov_read[1].iov_base = payload_read + 3 * 4096;
4995 	iov_read[1].iov_len = 4 * 4096;
4996 	iov_read[2].iov_base = payload_read + 7 * 4096;
4997 	iov_read[2].iov_len = 3 * 4096;
4998 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4999 	poll_threads();
5000 	CU_ASSERT(g_bserrno == 0);
5001 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
5002 
5003 	spdk_bs_free_io_channel(channel);
5004 	poll_threads();
5005 
5006 	ut_blob_close_and_delete(bs, blob);
5007 	ut_blob_close_and_delete(bs, snapshot);
5008 }
5009 
5010 /**
5011  * Inflate / decouple parent rw unit tests.
5012  *
5013  * --------------
5014  * original blob:         0         1         2         3         4
5015  *                   ,---------+---------+---------+---------+---------.
5016  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
5017  *                   +---------+---------+---------+---------+---------+
5018  *         snapshot2 |    -    |yyyyyyyyy|    -    |yyyyyyyyy|    -    |
5019  *                   +---------+---------+---------+---------+---------+
5020  *         blob      |    -    |zzzzzzzzz|    -    |    -    |    -    |
5021  *                   '---------+---------+---------+---------+---------'
5022  *                   .         .         .         .         .         .
5023  * --------          .         .         .         .         .         .
5024  * inflate:          .         .         .         .         .         .
5025  *                   ,---------+---------+---------+---------+---------.
5026  *         blob      |xxxxxxxxx|zzzzzzzzz|xxxxxxxxx|yyyyyyyyy|000000000|
5027  *                   '---------+---------+---------+---------+---------'
5028  *
5029  *         NOTE: needs to allocate 4 clusters, thin provisioning removed, dependency
5030  *               on snapshot2 and snapshot removed .         .         .
5031  *                   .         .         .         .         .         .
5032  * ----------------  .         .         .         .         .         .
5033  * decouple parent:  .         .         .         .         .         .
5034  *                   ,---------+---------+---------+---------+---------.
5035  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
5036  *                   +---------+---------+---------+---------+---------+
5037  *         blob      |    -    |zzzzzzzzz|    -    |yyyyyyyyy|    -    |
5038  *                   '---------+---------+---------+---------+---------'
5039  *
5040  *         NOTE: needs to allocate 1 cluster, 3 clusters unallocated, dependency
5041  *               on snapshot2 removed and on snapshot still exists. Snapshot2
5042  *               should remain a clone of snapshot.
5043  */
5044 static void
5045 _blob_inflate_rw(bool decouple_parent)
5046 {
5047 	struct spdk_blob_store *bs = g_bs;
5048 	struct spdk_blob *blob, *snapshot, *snapshot2;
5049 	struct spdk_io_channel *channel;
5050 	struct spdk_blob_opts opts;
5051 	spdk_blob_id blobid, snapshotid, snapshot2id;
5052 	uint64_t free_clusters;
5053 	uint64_t cluster_size;
5054 
5055 	uint64_t payload_size;
5056 	uint8_t *payload_read;
5057 	uint8_t *payload_write;
5058 	uint8_t *payload_clone;
5059 
5060 	uint64_t pages_per_cluster;
5061 	uint64_t pages_per_payload;
5062 
5063 	int i;
5064 	spdk_blob_id ids[2];
5065 	size_t count;
5066 
5067 	free_clusters = spdk_bs_free_cluster_count(bs);
5068 	cluster_size = spdk_bs_get_cluster_size(bs);
5069 	pages_per_cluster = cluster_size / spdk_bs_get_page_size(bs);
5070 	pages_per_payload = pages_per_cluster * 5;
5071 
5072 	payload_size = cluster_size * 5;
5073 
5074 	payload_read = malloc(payload_size);
5075 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
5076 
5077 	payload_write = malloc(payload_size);
5078 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
5079 
5080 	payload_clone = malloc(payload_size);
5081 	SPDK_CU_ASSERT_FATAL(payload_clone != NULL);
5082 
5083 	channel = spdk_bs_alloc_io_channel(bs);
5084 	SPDK_CU_ASSERT_FATAL(channel != NULL);
5085 
5086 	/* Create blob */
5087 	ut_spdk_blob_opts_init(&opts);
5088 	opts.thin_provision = true;
5089 	opts.num_clusters = 5;
5090 
5091 	blob = ut_blob_create_and_open(bs, &opts);
5092 	blobid = spdk_blob_get_id(blob);
5093 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
5094 
5095 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
5096 
5097 	/* 1) Initial read should return zeroed payload */
5098 	memset(payload_read, 0xFF, payload_size);
5099 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5100 			  blob_op_complete, NULL);
5101 	poll_threads();
5102 	CU_ASSERT(g_bserrno == 0);
5103 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
5104 
5105 	/* Fill whole blob with a pattern, except last cluster (to be sure it
5106 	 * isn't allocated) */
5107 	memset(payload_write, 0xE5, payload_size - cluster_size);
5108 	spdk_blob_io_write(blob, channel, payload_write, 0, pages_per_payload -
5109 			   pages_per_cluster, blob_op_complete, NULL);
5110 	poll_threads();
5111 	CU_ASSERT(g_bserrno == 0);
5112 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
5113 
5114 	/* 2) Create snapshot from blob (first level) */
5115 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5116 	poll_threads();
5117 	CU_ASSERT(g_bserrno == 0);
5118 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5119 	snapshotid = g_blobid;
5120 
5121 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
5122 	poll_threads();
5123 	CU_ASSERT(g_bserrno == 0);
5124 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5125 	snapshot = g_blob;
5126 	CU_ASSERT(snapshot->data_ro == true);
5127 	CU_ASSERT(snapshot->md_ro == true);
5128 
5129 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
5130 
5131 	/* Write every second cluster with a pattern.
5132 	 *
5133 	 * Last cluster shouldn't be written, to be sure that snapshot nor clone
5134 	 * doesn't allocate it.
5135 	 *
5136 	 * payload_clone stores expected result on "blob" read at the time and
5137 	 * is used only to check data consistency on clone before and after
5138 	 * inflation. Initially we fill it with a backing snapshots pattern
5139 	 * used before.
5140 	 */
5141 	memset(payload_clone, 0xE5, payload_size - cluster_size);
5142 	memset(payload_clone + payload_size - cluster_size, 0x00, cluster_size);
5143 	memset(payload_write, 0xAA, payload_size);
5144 	for (i = 1; i < 5; i += 2) {
5145 		spdk_blob_io_write(blob, channel, payload_write, i * pages_per_cluster,
5146 				   pages_per_cluster, blob_op_complete, NULL);
5147 		poll_threads();
5148 		CU_ASSERT(g_bserrno == 0);
5149 
5150 		/* Update expected result */
5151 		memcpy(payload_clone + (cluster_size * i), payload_write,
5152 		       cluster_size);
5153 	}
5154 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
5155 
5156 	/* Check data consistency on clone */
5157 	memset(payload_read, 0xFF, payload_size);
5158 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5159 			  blob_op_complete, NULL);
5160 	poll_threads();
5161 	CU_ASSERT(g_bserrno == 0);
5162 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
5163 
5164 	/* 3) Create second levels snapshot from blob */
5165 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5166 	poll_threads();
5167 	CU_ASSERT(g_bserrno == 0);
5168 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5169 	snapshot2id = g_blobid;
5170 
5171 	spdk_bs_open_blob(bs, snapshot2id, blob_op_with_handle_complete, NULL);
5172 	poll_threads();
5173 	CU_ASSERT(g_bserrno == 0);
5174 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5175 	snapshot2 = g_blob;
5176 	CU_ASSERT(snapshot2->data_ro == true);
5177 	CU_ASSERT(snapshot2->md_ro == true);
5178 
5179 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 5);
5180 
5181 	CU_ASSERT(snapshot2->parent_id == snapshotid);
5182 
5183 	/* Write one cluster on the top level blob. This cluster (1) covers
5184 	 * already allocated cluster in the snapshot2, so shouldn't be inflated
5185 	 * at all */
5186 	spdk_blob_io_write(blob, channel, payload_write, pages_per_cluster,
5187 			   pages_per_cluster, blob_op_complete, NULL);
5188 	poll_threads();
5189 	CU_ASSERT(g_bserrno == 0);
5190 
5191 	/* Update expected result */
5192 	memcpy(payload_clone + cluster_size, payload_write, cluster_size);
5193 
5194 	/* Check data consistency on clone */
5195 	memset(payload_read, 0xFF, payload_size);
5196 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5197 			  blob_op_complete, NULL);
5198 	poll_threads();
5199 	CU_ASSERT(g_bserrno == 0);
5200 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
5201 
5202 
5203 	/* Close all blobs */
5204 	spdk_blob_close(blob, blob_op_complete, NULL);
5205 	poll_threads();
5206 	CU_ASSERT(g_bserrno == 0);
5207 
5208 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
5209 	poll_threads();
5210 	CU_ASSERT(g_bserrno == 0);
5211 
5212 	spdk_blob_close(snapshot, blob_op_complete, NULL);
5213 	poll_threads();
5214 	CU_ASSERT(g_bserrno == 0);
5215 
5216 	/* Check snapshot-clone relations */
5217 	count = 2;
5218 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
5219 	CU_ASSERT(count == 1);
5220 	CU_ASSERT(ids[0] == snapshot2id);
5221 
5222 	count = 2;
5223 	CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
5224 	CU_ASSERT(count == 1);
5225 	CU_ASSERT(ids[0] == blobid);
5226 
5227 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshot2id);
5228 
5229 	free_clusters = spdk_bs_free_cluster_count(bs);
5230 	if (!decouple_parent) {
5231 		/* Do full blob inflation */
5232 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
5233 		poll_threads();
5234 		CU_ASSERT(g_bserrno == 0);
5235 
5236 		/* All clusters should be inflated (except one already allocated
5237 		 * in a top level blob) */
5238 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 4);
5239 
5240 		/* Check if relation tree updated correctly */
5241 		count = 2;
5242 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
5243 
5244 		/* snapshotid have one clone */
5245 		CU_ASSERT(count == 1);
5246 		CU_ASSERT(ids[0] == snapshot2id);
5247 
5248 		/* snapshot2id have no clones */
5249 		count = 2;
5250 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
5251 		CU_ASSERT(count == 0);
5252 
5253 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
5254 	} else {
5255 		/* Decouple parent of blob */
5256 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
5257 		poll_threads();
5258 		CU_ASSERT(g_bserrno == 0);
5259 
5260 		/* Only one cluster from a parent should be inflated (second one
5261 		 * is covered by a cluster written on a top level blob, and
5262 		 * already allocated) */
5263 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 1);
5264 
5265 		/* Check if relation tree updated correctly */
5266 		count = 2;
5267 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
5268 
5269 		/* snapshotid have two clones now */
5270 		CU_ASSERT(count == 2);
5271 		CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5272 		CU_ASSERT(ids[0] == snapshot2id || ids[1] == snapshot2id);
5273 
5274 		/* snapshot2id have no clones */
5275 		count = 2;
5276 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
5277 		CU_ASSERT(count == 0);
5278 
5279 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5280 	}
5281 
5282 	/* Try to delete snapshot2 (should pass) */
5283 	spdk_bs_delete_blob(bs, snapshot2id, blob_op_complete, NULL);
5284 	poll_threads();
5285 	CU_ASSERT(g_bserrno == 0);
5286 
5287 	/* Try to delete base snapshot */
5288 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5289 	poll_threads();
5290 	CU_ASSERT(g_bserrno == 0);
5291 
5292 	/* Reopen blob after snapshot deletion */
5293 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
5294 	poll_threads();
5295 	CU_ASSERT(g_bserrno == 0);
5296 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5297 	blob = g_blob;
5298 
5299 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
5300 
5301 	/* Check data consistency on inflated blob */
5302 	memset(payload_read, 0xFF, payload_size);
5303 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
5304 			  blob_op_complete, NULL);
5305 	poll_threads();
5306 	CU_ASSERT(g_bserrno == 0);
5307 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
5308 
5309 	spdk_bs_free_io_channel(channel);
5310 	poll_threads();
5311 
5312 	free(payload_read);
5313 	free(payload_write);
5314 	free(payload_clone);
5315 
5316 	ut_blob_close_and_delete(bs, blob);
5317 }
5318 
5319 static void
5320 blob_inflate_rw(void)
5321 {
5322 	_blob_inflate_rw(false);
5323 	_blob_inflate_rw(true);
5324 }
5325 
5326 /**
5327  * Snapshot-clones relation test
5328  *
5329  *         snapshot
5330  *            |
5331  *      +-----+-----+
5332  *      |           |
5333  *   blob(ro)   snapshot2
5334  *      |           |
5335  *   clone2      clone
5336  */
5337 static void
5338 blob_relations(void)
5339 {
5340 	struct spdk_blob_store *bs;
5341 	struct spdk_bs_dev *dev;
5342 	struct spdk_bs_opts bs_opts;
5343 	struct spdk_blob_opts opts;
5344 	struct spdk_blob *blob, *snapshot, *snapshot2, *clone, *clone2;
5345 	spdk_blob_id blobid, cloneid, snapshotid, cloneid2, snapshotid2;
5346 	int rc;
5347 	size_t count;
5348 	spdk_blob_id ids[10] = {};
5349 
5350 	dev = init_dev();
5351 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
5352 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
5353 
5354 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
5355 	poll_threads();
5356 	CU_ASSERT(g_bserrno == 0);
5357 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
5358 	bs = g_bs;
5359 
5360 	/* 1. Create blob with 10 clusters */
5361 
5362 	ut_spdk_blob_opts_init(&opts);
5363 	opts.num_clusters = 10;
5364 
5365 	blob = ut_blob_create_and_open(bs, &opts);
5366 	blobid = spdk_blob_get_id(blob);
5367 
5368 	CU_ASSERT(!spdk_blob_is_read_only(blob));
5369 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
5370 	CU_ASSERT(!spdk_blob_is_clone(blob));
5371 	CU_ASSERT(!spdk_blob_is_thin_provisioned(blob));
5372 
5373 	/* blob should not have underlying snapshot nor clones */
5374 	CU_ASSERT(blob->parent_id == SPDK_BLOBID_INVALID);
5375 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
5376 	count = SPDK_COUNTOF(ids);
5377 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5378 	CU_ASSERT(rc == 0);
5379 	CU_ASSERT(count == 0);
5380 
5381 
5382 	/* 2. Create snapshot */
5383 
5384 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5385 	poll_threads();
5386 	CU_ASSERT(g_bserrno == 0);
5387 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5388 	snapshotid = g_blobid;
5389 
5390 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
5391 	poll_threads();
5392 	CU_ASSERT(g_bserrno == 0);
5393 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5394 	snapshot = g_blob;
5395 
5396 	CU_ASSERT(spdk_blob_is_read_only(snapshot));
5397 	CU_ASSERT(spdk_blob_is_snapshot(snapshot));
5398 	CU_ASSERT(!spdk_blob_is_clone(snapshot));
5399 	CU_ASSERT(snapshot->parent_id == SPDK_BLOBID_INVALID);
5400 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
5401 
5402 	/* Check if original blob is converted to the clone of snapshot */
5403 	CU_ASSERT(!spdk_blob_is_read_only(blob));
5404 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
5405 	CU_ASSERT(spdk_blob_is_clone(blob));
5406 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
5407 	CU_ASSERT(blob->parent_id == snapshotid);
5408 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5409 
5410 	count = SPDK_COUNTOF(ids);
5411 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5412 	CU_ASSERT(rc == 0);
5413 	CU_ASSERT(count == 1);
5414 	CU_ASSERT(ids[0] == blobid);
5415 
5416 
5417 	/* 3. Create clone from snapshot */
5418 
5419 	spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
5420 	poll_threads();
5421 	CU_ASSERT(g_bserrno == 0);
5422 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5423 	cloneid = g_blobid;
5424 
5425 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
5426 	poll_threads();
5427 	CU_ASSERT(g_bserrno == 0);
5428 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5429 	clone = g_blob;
5430 
5431 	CU_ASSERT(!spdk_blob_is_read_only(clone));
5432 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
5433 	CU_ASSERT(spdk_blob_is_clone(clone));
5434 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
5435 	CU_ASSERT(clone->parent_id == snapshotid);
5436 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid);
5437 
5438 	count = SPDK_COUNTOF(ids);
5439 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5440 	CU_ASSERT(rc == 0);
5441 	CU_ASSERT(count == 0);
5442 
5443 	/* Check if clone is on the snapshot's list */
5444 	count = SPDK_COUNTOF(ids);
5445 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5446 	CU_ASSERT(rc == 0);
5447 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5448 	CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid);
5449 
5450 
5451 	/* 4. Create snapshot of the clone */
5452 
5453 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
5454 	poll_threads();
5455 	CU_ASSERT(g_bserrno == 0);
5456 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5457 	snapshotid2 = g_blobid;
5458 
5459 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
5460 	poll_threads();
5461 	CU_ASSERT(g_bserrno == 0);
5462 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5463 	snapshot2 = g_blob;
5464 
5465 	CU_ASSERT(spdk_blob_is_read_only(snapshot2));
5466 	CU_ASSERT(spdk_blob_is_snapshot(snapshot2));
5467 	CU_ASSERT(spdk_blob_is_clone(snapshot2));
5468 	CU_ASSERT(snapshot2->parent_id == snapshotid);
5469 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
5470 
5471 	/* Check if clone is converted to the clone of snapshot2 and snapshot2
5472 	 * is a child of snapshot */
5473 	CU_ASSERT(!spdk_blob_is_read_only(clone));
5474 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
5475 	CU_ASSERT(spdk_blob_is_clone(clone));
5476 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
5477 	CU_ASSERT(clone->parent_id == snapshotid2);
5478 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5479 
5480 	count = SPDK_COUNTOF(ids);
5481 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5482 	CU_ASSERT(rc == 0);
5483 	CU_ASSERT(count == 1);
5484 	CU_ASSERT(ids[0] == cloneid);
5485 
5486 
5487 	/* 5. Try to create clone from read only blob */
5488 
5489 	/* Mark blob as read only */
5490 	spdk_blob_set_read_only(blob);
5491 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
5492 	poll_threads();
5493 	CU_ASSERT(g_bserrno == 0);
5494 
5495 	/* Check if previously created blob is read only clone */
5496 	CU_ASSERT(spdk_blob_is_read_only(blob));
5497 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
5498 	CU_ASSERT(spdk_blob_is_clone(blob));
5499 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
5500 
5501 	/* Create clone from read only blob */
5502 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5503 	poll_threads();
5504 	CU_ASSERT(g_bserrno == 0);
5505 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5506 	cloneid2 = g_blobid;
5507 
5508 	spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL);
5509 	poll_threads();
5510 	CU_ASSERT(g_bserrno == 0);
5511 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5512 	clone2 = g_blob;
5513 
5514 	CU_ASSERT(!spdk_blob_is_read_only(clone2));
5515 	CU_ASSERT(!spdk_blob_is_snapshot(clone2));
5516 	CU_ASSERT(spdk_blob_is_clone(clone2));
5517 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone2));
5518 
5519 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5520 
5521 	count = SPDK_COUNTOF(ids);
5522 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5523 	CU_ASSERT(rc == 0);
5524 
5525 	CU_ASSERT(count == 1);
5526 	CU_ASSERT(ids[0] == cloneid2);
5527 
5528 	/* Close blobs */
5529 
5530 	spdk_blob_close(clone2, blob_op_complete, NULL);
5531 	poll_threads();
5532 	CU_ASSERT(g_bserrno == 0);
5533 
5534 	spdk_blob_close(blob, blob_op_complete, NULL);
5535 	poll_threads();
5536 	CU_ASSERT(g_bserrno == 0);
5537 
5538 	spdk_blob_close(clone, blob_op_complete, NULL);
5539 	poll_threads();
5540 	CU_ASSERT(g_bserrno == 0);
5541 
5542 	spdk_blob_close(snapshot, blob_op_complete, NULL);
5543 	poll_threads();
5544 	CU_ASSERT(g_bserrno == 0);
5545 
5546 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
5547 	poll_threads();
5548 	CU_ASSERT(g_bserrno == 0);
5549 
5550 	/* Try to delete snapshot with more than 1 clone */
5551 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5552 	poll_threads();
5553 	CU_ASSERT(g_bserrno != 0);
5554 
5555 	ut_bs_reload(&bs, &bs_opts);
5556 
5557 	/* NULL ids array should return number of clones in count */
5558 	count = SPDK_COUNTOF(ids);
5559 	rc = spdk_blob_get_clones(bs, snapshotid, NULL, &count);
5560 	CU_ASSERT(rc == -ENOMEM);
5561 	CU_ASSERT(count == 2);
5562 
5563 	/* incorrect array size */
5564 	count = 1;
5565 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5566 	CU_ASSERT(rc == -ENOMEM);
5567 	CU_ASSERT(count == 2);
5568 
5569 
5570 	/* Verify structure of loaded blob store */
5571 
5572 	/* snapshot */
5573 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
5574 
5575 	count = SPDK_COUNTOF(ids);
5576 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5577 	CU_ASSERT(rc == 0);
5578 	CU_ASSERT(count == 2);
5579 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5580 	CU_ASSERT(ids[0] == snapshotid2 || ids[1] == snapshotid2);
5581 
5582 	/* blob */
5583 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5584 	count = SPDK_COUNTOF(ids);
5585 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5586 	CU_ASSERT(rc == 0);
5587 	CU_ASSERT(count == 1);
5588 	CU_ASSERT(ids[0] == cloneid2);
5589 
5590 	/* clone */
5591 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5592 	count = SPDK_COUNTOF(ids);
5593 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5594 	CU_ASSERT(rc == 0);
5595 	CU_ASSERT(count == 0);
5596 
5597 	/* snapshot2 */
5598 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
5599 	count = SPDK_COUNTOF(ids);
5600 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5601 	CU_ASSERT(rc == 0);
5602 	CU_ASSERT(count == 1);
5603 	CU_ASSERT(ids[0] == cloneid);
5604 
5605 	/* clone2 */
5606 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5607 	count = SPDK_COUNTOF(ids);
5608 	rc = spdk_blob_get_clones(bs, cloneid2, ids, &count);
5609 	CU_ASSERT(rc == 0);
5610 	CU_ASSERT(count == 0);
5611 
5612 	/* Try to delete blob that user should not be able to remove */
5613 
5614 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5615 	poll_threads();
5616 	CU_ASSERT(g_bserrno != 0);
5617 
5618 	/* Remove all blobs */
5619 
5620 	spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL);
5621 	poll_threads();
5622 	CU_ASSERT(g_bserrno == 0);
5623 
5624 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
5625 	poll_threads();
5626 	CU_ASSERT(g_bserrno == 0);
5627 
5628 	spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL);
5629 	poll_threads();
5630 	CU_ASSERT(g_bserrno == 0);
5631 
5632 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
5633 	poll_threads();
5634 	CU_ASSERT(g_bserrno == 0);
5635 
5636 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5637 	poll_threads();
5638 	CU_ASSERT(g_bserrno == 0);
5639 
5640 	spdk_bs_unload(bs, bs_op_complete, NULL);
5641 	poll_threads();
5642 	CU_ASSERT(g_bserrno == 0);
5643 
5644 	g_bs = NULL;
5645 }
5646 
5647 /**
5648  * Snapshot-clones relation test 2
5649  *
5650  *         snapshot1
5651  *            |
5652  *         snapshot2
5653  *            |
5654  *      +-----+-----+
5655  *      |           |
5656  *   blob(ro)   snapshot3
5657  *      |           |
5658  *      |       snapshot4
5659  *      |        |     |
5660  *   clone2   clone  clone3
5661  */
5662 static void
5663 blob_relations2(void)
5664 {
5665 	struct spdk_blob_store *bs;
5666 	struct spdk_bs_dev *dev;
5667 	struct spdk_bs_opts bs_opts;
5668 	struct spdk_blob_opts opts;
5669 	struct spdk_blob *blob, *snapshot1, *snapshot2, *snapshot3, *snapshot4, *clone, *clone2;
5670 	spdk_blob_id blobid, snapshotid1, snapshotid2, snapshotid3, snapshotid4, cloneid, cloneid2,
5671 		     cloneid3;
5672 	int rc;
5673 	size_t count;
5674 	spdk_blob_id ids[10] = {};
5675 
5676 	dev = init_dev();
5677 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
5678 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
5679 
5680 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
5681 	poll_threads();
5682 	CU_ASSERT(g_bserrno == 0);
5683 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
5684 	bs = g_bs;
5685 
5686 	/* 1. Create blob with 10 clusters */
5687 
5688 	ut_spdk_blob_opts_init(&opts);
5689 	opts.num_clusters = 10;
5690 
5691 	blob = ut_blob_create_and_open(bs, &opts);
5692 	blobid = spdk_blob_get_id(blob);
5693 
5694 	/* 2. Create snapshot1 */
5695 
5696 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5697 	poll_threads();
5698 	CU_ASSERT(g_bserrno == 0);
5699 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5700 	snapshotid1 = g_blobid;
5701 
5702 	spdk_bs_open_blob(bs, snapshotid1, blob_op_with_handle_complete, NULL);
5703 	poll_threads();
5704 	CU_ASSERT(g_bserrno == 0);
5705 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5706 	snapshot1 = g_blob;
5707 
5708 	CU_ASSERT(snapshot1->parent_id == SPDK_BLOBID_INVALID);
5709 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid1) == SPDK_BLOBID_INVALID);
5710 
5711 	CU_ASSERT(blob->parent_id == snapshotid1);
5712 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid1);
5713 
5714 	/* Check if blob is the clone of snapshot1 */
5715 	CU_ASSERT(blob->parent_id == snapshotid1);
5716 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid1);
5717 
5718 	count = SPDK_COUNTOF(ids);
5719 	rc = spdk_blob_get_clones(bs, snapshotid1, ids, &count);
5720 	CU_ASSERT(rc == 0);
5721 	CU_ASSERT(count == 1);
5722 	CU_ASSERT(ids[0] == blobid);
5723 
5724 	/* 3. Create another snapshot */
5725 
5726 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5727 	poll_threads();
5728 	CU_ASSERT(g_bserrno == 0);
5729 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5730 	snapshotid2 = g_blobid;
5731 
5732 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
5733 	poll_threads();
5734 	CU_ASSERT(g_bserrno == 0);
5735 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5736 	snapshot2 = g_blob;
5737 
5738 	CU_ASSERT(spdk_blob_is_clone(snapshot2));
5739 	CU_ASSERT(snapshot2->parent_id == snapshotid1);
5740 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid1);
5741 
5742 	/* Check if snapshot2 is the clone of snapshot1 and blob
5743 	 * is a child of snapshot2 */
5744 	CU_ASSERT(blob->parent_id == snapshotid2);
5745 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid2);
5746 
5747 	count = SPDK_COUNTOF(ids);
5748 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5749 	CU_ASSERT(rc == 0);
5750 	CU_ASSERT(count == 1);
5751 	CU_ASSERT(ids[0] == blobid);
5752 
5753 	/* 4. Create clone from snapshot */
5754 
5755 	spdk_bs_create_clone(bs, snapshotid2, NULL, blob_op_with_id_complete, NULL);
5756 	poll_threads();
5757 	CU_ASSERT(g_bserrno == 0);
5758 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5759 	cloneid = g_blobid;
5760 
5761 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
5762 	poll_threads();
5763 	CU_ASSERT(g_bserrno == 0);
5764 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5765 	clone = g_blob;
5766 
5767 	CU_ASSERT(clone->parent_id == snapshotid2);
5768 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5769 
5770 	/* Check if clone is on the snapshot's list */
5771 	count = SPDK_COUNTOF(ids);
5772 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5773 	CU_ASSERT(rc == 0);
5774 	CU_ASSERT(count == 2);
5775 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5776 	CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid);
5777 
5778 	/* 5. Create snapshot of the clone */
5779 
5780 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
5781 	poll_threads();
5782 	CU_ASSERT(g_bserrno == 0);
5783 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5784 	snapshotid3 = g_blobid;
5785 
5786 	spdk_bs_open_blob(bs, snapshotid3, blob_op_with_handle_complete, NULL);
5787 	poll_threads();
5788 	CU_ASSERT(g_bserrno == 0);
5789 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5790 	snapshot3 = g_blob;
5791 
5792 	CU_ASSERT(snapshot3->parent_id == snapshotid2);
5793 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid3) == snapshotid2);
5794 
5795 	/* Check if clone is converted to the clone of snapshot3 and snapshot3
5796 	 * is a child of snapshot2 */
5797 	CU_ASSERT(clone->parent_id == snapshotid3);
5798 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid3);
5799 
5800 	count = SPDK_COUNTOF(ids);
5801 	rc = spdk_blob_get_clones(bs, snapshotid3, ids, &count);
5802 	CU_ASSERT(rc == 0);
5803 	CU_ASSERT(count == 1);
5804 	CU_ASSERT(ids[0] == cloneid);
5805 
5806 	/* 6. Create another snapshot of the clone */
5807 
5808 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
5809 	poll_threads();
5810 	CU_ASSERT(g_bserrno == 0);
5811 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5812 	snapshotid4 = g_blobid;
5813 
5814 	spdk_bs_open_blob(bs, snapshotid4, blob_op_with_handle_complete, NULL);
5815 	poll_threads();
5816 	CU_ASSERT(g_bserrno == 0);
5817 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5818 	snapshot4 = g_blob;
5819 
5820 	CU_ASSERT(snapshot4->parent_id == snapshotid3);
5821 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid4) == snapshotid3);
5822 
5823 	/* Check if clone is converted to the clone of snapshot4 and snapshot4
5824 	 * is a child of snapshot3 */
5825 	CU_ASSERT(clone->parent_id == snapshotid4);
5826 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid4);
5827 
5828 	count = SPDK_COUNTOF(ids);
5829 	rc = spdk_blob_get_clones(bs, snapshotid4, ids, &count);
5830 	CU_ASSERT(rc == 0);
5831 	CU_ASSERT(count == 1);
5832 	CU_ASSERT(ids[0] == cloneid);
5833 
5834 	/* 7. Remove snapshot 4 */
5835 
5836 	ut_blob_close_and_delete(bs, snapshot4);
5837 
5838 	/* Check if relations are back to state from before creating snapshot 4 */
5839 	CU_ASSERT(clone->parent_id == snapshotid3);
5840 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid3);
5841 
5842 	count = SPDK_COUNTOF(ids);
5843 	rc = spdk_blob_get_clones(bs, snapshotid3, ids, &count);
5844 	CU_ASSERT(rc == 0);
5845 	CU_ASSERT(count == 1);
5846 	CU_ASSERT(ids[0] == cloneid);
5847 
5848 	/* 8. Create second clone of snapshot 3 and try to remove snapshot 3 */
5849 
5850 	spdk_bs_create_clone(bs, snapshotid3, NULL, blob_op_with_id_complete, NULL);
5851 	poll_threads();
5852 	CU_ASSERT(g_bserrno == 0);
5853 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5854 	cloneid3 = g_blobid;
5855 
5856 	spdk_bs_delete_blob(bs, snapshotid3, blob_op_complete, NULL);
5857 	poll_threads();
5858 	CU_ASSERT(g_bserrno != 0);
5859 
5860 	/* 9. Open snapshot 3 again and try to remove it while clone 3 is closed */
5861 
5862 	spdk_bs_open_blob(bs, snapshotid3, blob_op_with_handle_complete, NULL);
5863 	poll_threads();
5864 	CU_ASSERT(g_bserrno == 0);
5865 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5866 	snapshot3 = g_blob;
5867 
5868 	spdk_bs_delete_blob(bs, snapshotid3, blob_op_complete, NULL);
5869 	poll_threads();
5870 	CU_ASSERT(g_bserrno != 0);
5871 
5872 	spdk_blob_close(snapshot3, blob_op_complete, NULL);
5873 	poll_threads();
5874 	CU_ASSERT(g_bserrno == 0);
5875 
5876 	spdk_bs_delete_blob(bs, cloneid3, blob_op_complete, NULL);
5877 	poll_threads();
5878 	CU_ASSERT(g_bserrno == 0);
5879 
5880 	/* 10. Remove snapshot 1 */
5881 
5882 	ut_blob_close_and_delete(bs, snapshot1);
5883 
5884 	/* Check if relations are back to state from before creating snapshot 4 (before step 6) */
5885 	CU_ASSERT(snapshot2->parent_id == SPDK_BLOBID_INVALID);
5886 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == SPDK_BLOBID_INVALID);
5887 
5888 	count = SPDK_COUNTOF(ids);
5889 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5890 	CU_ASSERT(rc == 0);
5891 	CU_ASSERT(count == 2);
5892 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5893 	CU_ASSERT(ids[0] == snapshotid3 || ids[1] == snapshotid3);
5894 
5895 	/* 11. Try to create clone from read only blob */
5896 
5897 	/* Mark blob as read only */
5898 	spdk_blob_set_read_only(blob);
5899 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
5900 	poll_threads();
5901 	CU_ASSERT(g_bserrno == 0);
5902 
5903 	/* Create clone from read only blob */
5904 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
5905 	poll_threads();
5906 	CU_ASSERT(g_bserrno == 0);
5907 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
5908 	cloneid2 = g_blobid;
5909 
5910 	spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL);
5911 	poll_threads();
5912 	CU_ASSERT(g_bserrno == 0);
5913 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
5914 	clone2 = g_blob;
5915 
5916 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5917 
5918 	count = SPDK_COUNTOF(ids);
5919 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5920 	CU_ASSERT(rc == 0);
5921 	CU_ASSERT(count == 1);
5922 	CU_ASSERT(ids[0] == cloneid2);
5923 
5924 	/* Close blobs */
5925 
5926 	spdk_blob_close(clone2, blob_op_complete, NULL);
5927 	poll_threads();
5928 	CU_ASSERT(g_bserrno == 0);
5929 
5930 	spdk_blob_close(blob, blob_op_complete, NULL);
5931 	poll_threads();
5932 	CU_ASSERT(g_bserrno == 0);
5933 
5934 	spdk_blob_close(clone, blob_op_complete, NULL);
5935 	poll_threads();
5936 	CU_ASSERT(g_bserrno == 0);
5937 
5938 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
5939 	poll_threads();
5940 	CU_ASSERT(g_bserrno == 0);
5941 
5942 	spdk_blob_close(snapshot3, blob_op_complete, NULL);
5943 	poll_threads();
5944 	CU_ASSERT(g_bserrno == 0);
5945 
5946 	ut_bs_reload(&bs, &bs_opts);
5947 
5948 	/* Verify structure of loaded blob store */
5949 
5950 	/* snapshot2 */
5951 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == SPDK_BLOBID_INVALID);
5952 
5953 	count = SPDK_COUNTOF(ids);
5954 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5955 	CU_ASSERT(rc == 0);
5956 	CU_ASSERT(count == 2);
5957 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5958 	CU_ASSERT(ids[0] == snapshotid3 || ids[1] == snapshotid3);
5959 
5960 	/* blob */
5961 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid2);
5962 	count = SPDK_COUNTOF(ids);
5963 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5964 	CU_ASSERT(rc == 0);
5965 	CU_ASSERT(count == 1);
5966 	CU_ASSERT(ids[0] == cloneid2);
5967 
5968 	/* clone */
5969 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid3);
5970 	count = SPDK_COUNTOF(ids);
5971 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5972 	CU_ASSERT(rc == 0);
5973 	CU_ASSERT(count == 0);
5974 
5975 	/* snapshot3 */
5976 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid3) == snapshotid2);
5977 	count = SPDK_COUNTOF(ids);
5978 	rc = spdk_blob_get_clones(bs, snapshotid3, ids, &count);
5979 	CU_ASSERT(rc == 0);
5980 	CU_ASSERT(count == 1);
5981 	CU_ASSERT(ids[0] == cloneid);
5982 
5983 	/* clone2 */
5984 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5985 	count = SPDK_COUNTOF(ids);
5986 	rc = spdk_blob_get_clones(bs, cloneid2, ids, &count);
5987 	CU_ASSERT(rc == 0);
5988 	CU_ASSERT(count == 0);
5989 
5990 	/* Try to delete all blobs in the worse possible order */
5991 
5992 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
5993 	poll_threads();
5994 	CU_ASSERT(g_bserrno != 0);
5995 
5996 	spdk_bs_delete_blob(bs, snapshotid3, blob_op_complete, NULL);
5997 	poll_threads();
5998 	CU_ASSERT(g_bserrno == 0);
5999 
6000 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6001 	poll_threads();
6002 	CU_ASSERT(g_bserrno != 0);
6003 
6004 	spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL);
6005 	poll_threads();
6006 	CU_ASSERT(g_bserrno == 0);
6007 
6008 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6009 	poll_threads();
6010 	CU_ASSERT(g_bserrno == 0);
6011 
6012 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
6013 	poll_threads();
6014 	CU_ASSERT(g_bserrno == 0);
6015 
6016 	spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL);
6017 	poll_threads();
6018 	CU_ASSERT(g_bserrno == 0);
6019 
6020 	spdk_bs_unload(bs, bs_op_complete, NULL);
6021 	poll_threads();
6022 	CU_ASSERT(g_bserrno == 0);
6023 
6024 	g_bs = NULL;
6025 }
6026 
6027 /**
6028  * Snapshot-clones relation test 3
6029  *
6030  *         snapshot0
6031  *            |
6032  *         snapshot1
6033  *            |
6034  *         snapshot2
6035  *            |
6036  *           blob
6037  */
6038 static void
6039 blob_relations3(void)
6040 {
6041 	struct spdk_blob_store *bs;
6042 	struct spdk_bs_dev *dev;
6043 	struct spdk_io_channel *channel;
6044 	struct spdk_bs_opts bs_opts;
6045 	struct spdk_blob_opts opts;
6046 	struct spdk_blob *blob;
6047 	spdk_blob_id blobid, snapshotid0, snapshotid1, snapshotid2;
6048 
6049 	dev = init_dev();
6050 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
6051 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
6052 
6053 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
6054 	poll_threads();
6055 	CU_ASSERT(g_bserrno == 0);
6056 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6057 	bs = g_bs;
6058 
6059 	channel = spdk_bs_alloc_io_channel(bs);
6060 	SPDK_CU_ASSERT_FATAL(channel != NULL);
6061 
6062 	/* 1. Create blob with 10 clusters */
6063 	ut_spdk_blob_opts_init(&opts);
6064 	opts.num_clusters = 10;
6065 
6066 	blob = ut_blob_create_and_open(bs, &opts);
6067 	blobid = spdk_blob_get_id(blob);
6068 
6069 	/* 2. Create snapshot0 */
6070 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6071 	poll_threads();
6072 	CU_ASSERT(g_bserrno == 0);
6073 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6074 	snapshotid0 = g_blobid;
6075 
6076 	/* 3. Create snapshot1 */
6077 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6078 	poll_threads();
6079 	CU_ASSERT(g_bserrno == 0);
6080 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6081 	snapshotid1 = g_blobid;
6082 
6083 	/* 4. Create snapshot2 */
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 	snapshotid2 = g_blobid;
6089 
6090 	/* 5. Decouple blob */
6091 	spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
6092 	poll_threads();
6093 	CU_ASSERT(g_bserrno == 0);
6094 
6095 	/* 6. Decouple snapshot2. Make sure updating md of snapshot2 is possible */
6096 	spdk_bs_blob_decouple_parent(bs, channel, snapshotid2, blob_op_complete, NULL);
6097 	poll_threads();
6098 	CU_ASSERT(g_bserrno == 0);
6099 
6100 	/* 7. Delete blob */
6101 	spdk_blob_close(blob, blob_op_complete, NULL);
6102 	poll_threads();
6103 	CU_ASSERT(g_bserrno == 0);
6104 
6105 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
6106 	poll_threads();
6107 	CU_ASSERT(g_bserrno == 0);
6108 
6109 	/* 8. Delete snapshot2.
6110 	 * If md of snapshot 2 was updated, it should be possible to delete it */
6111 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
6112 	poll_threads();
6113 	CU_ASSERT(g_bserrno == 0);
6114 
6115 	/* Remove remaining blobs and unload bs */
6116 	spdk_bs_delete_blob(bs, snapshotid1, blob_op_complete, NULL);
6117 	poll_threads();
6118 	CU_ASSERT(g_bserrno == 0);
6119 
6120 	spdk_bs_delete_blob(bs, snapshotid0, blob_op_complete, NULL);
6121 	poll_threads();
6122 	CU_ASSERT(g_bserrno == 0);
6123 
6124 	spdk_bs_free_io_channel(channel);
6125 	poll_threads();
6126 
6127 	spdk_bs_unload(bs, bs_op_complete, NULL);
6128 	poll_threads();
6129 	CU_ASSERT(g_bserrno == 0);
6130 
6131 	g_bs = NULL;
6132 }
6133 
6134 static void
6135 blobstore_clean_power_failure(void)
6136 {
6137 	struct spdk_blob_store *bs;
6138 	struct spdk_blob *blob;
6139 	struct spdk_power_failure_thresholds thresholds = {};
6140 	bool clean = false;
6141 	struct spdk_bs_super_block *super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
6142 	struct spdk_bs_super_block super_copy = {};
6143 
6144 	thresholds.general_threshold = 1;
6145 	while (!clean) {
6146 		/* Create bs and blob */
6147 		suite_blob_setup();
6148 		SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6149 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6150 		bs = g_bs;
6151 		blob = g_blob;
6152 
6153 		/* Super block should not change for rest of the UT,
6154 		 * save it and compare later. */
6155 		memcpy(&super_copy, super, sizeof(struct spdk_bs_super_block));
6156 		SPDK_CU_ASSERT_FATAL(super->clean == 0);
6157 		SPDK_CU_ASSERT_FATAL(bs->clean == 0);
6158 
6159 		/* Force bs/super block in a clean state.
6160 		 * Along with marking blob dirty, to cause blob persist. */
6161 		blob->state = SPDK_BLOB_STATE_DIRTY;
6162 		bs->clean = 1;
6163 		super->clean = 1;
6164 		super->crc = blob_md_page_calc_crc(super);
6165 
6166 		g_bserrno = -1;
6167 		dev_set_power_failure_thresholds(thresholds);
6168 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
6169 		poll_threads();
6170 		dev_reset_power_failure_event();
6171 
6172 		if (g_bserrno == 0) {
6173 			/* After successful md sync, both bs and super block
6174 			 * should be marked as not clean. */
6175 			SPDK_CU_ASSERT_FATAL(bs->clean == 0);
6176 			SPDK_CU_ASSERT_FATAL(super->clean == 0);
6177 			clean = true;
6178 		}
6179 
6180 		/* Depending on the point of failure, super block was either updated or not. */
6181 		super_copy.clean = super->clean;
6182 		super_copy.crc = blob_md_page_calc_crc(&super_copy);
6183 		/* Compare that the values in super block remained unchanged. */
6184 		SPDK_CU_ASSERT_FATAL(!memcmp(&super_copy, super, sizeof(struct spdk_bs_super_block)));
6185 
6186 		/* Delete blob and unload bs */
6187 		suite_blob_cleanup();
6188 
6189 		thresholds.general_threshold++;
6190 	}
6191 }
6192 
6193 static void
6194 blob_delete_snapshot_power_failure(void)
6195 {
6196 	struct spdk_bs_dev *dev;
6197 	struct spdk_blob_store *bs;
6198 	struct spdk_blob_opts opts;
6199 	struct spdk_blob *blob, *snapshot;
6200 	struct spdk_power_failure_thresholds thresholds = {};
6201 	spdk_blob_id blobid, snapshotid;
6202 	const void *value;
6203 	size_t value_len;
6204 	size_t count;
6205 	spdk_blob_id ids[3] = {};
6206 	int rc;
6207 	bool deleted = false;
6208 	int delete_snapshot_bserrno = -1;
6209 
6210 	thresholds.general_threshold = 1;
6211 	while (!deleted) {
6212 		dev = init_dev();
6213 
6214 		spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
6215 		poll_threads();
6216 		CU_ASSERT(g_bserrno == 0);
6217 		SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6218 		bs = g_bs;
6219 
6220 		/* Create blob */
6221 		ut_spdk_blob_opts_init(&opts);
6222 		opts.num_clusters = 10;
6223 
6224 		spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
6225 		poll_threads();
6226 		CU_ASSERT(g_bserrno == 0);
6227 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6228 		blobid = g_blobid;
6229 
6230 		/* Create snapshot */
6231 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6232 		poll_threads();
6233 		CU_ASSERT(g_bserrno == 0);
6234 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6235 		snapshotid = g_blobid;
6236 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6237 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6238 
6239 		dev_set_power_failure_thresholds(thresholds);
6240 
6241 		spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
6242 		poll_threads();
6243 		delete_snapshot_bserrno = g_bserrno;
6244 
6245 		/* Do not shut down cleanly. Assumption is that after snapshot deletion
6246 		 * reports success, changes to both blobs should already persisted. */
6247 		dev_reset_power_failure_event();
6248 		ut_bs_dirty_load(&bs, NULL);
6249 
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 		spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
6254 		poll_threads();
6255 		CU_ASSERT(g_bserrno == 0);
6256 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6257 		blob = g_blob;
6258 		SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(blob) == true);
6259 
6260 		spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
6261 		poll_threads();
6262 
6263 		if (g_bserrno == 0) {
6264 			SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6265 			snapshot = g_blob;
6266 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
6267 			count = SPDK_COUNTOF(ids);
6268 			rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
6269 			CU_ASSERT(rc == 0);
6270 			CU_ASSERT(count == 1);
6271 			CU_ASSERT(ids[0] == blobid);
6272 			rc = spdk_blob_get_xattr_value(snapshot, SNAPSHOT_PENDING_REMOVAL, &value, &value_len);
6273 			CU_ASSERT(rc != 0);
6274 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(snapshot) == false);
6275 
6276 			spdk_blob_close(snapshot, blob_op_complete, NULL);
6277 			poll_threads();
6278 			CU_ASSERT(g_bserrno == 0);
6279 		} else {
6280 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
6281 			/* Snapshot might have been left in unrecoverable state, so it does not open.
6282 			 * Yet delete might perform further changes to the clone after that.
6283 			 * This UT should test until snapshot is deleted and delete call succeeds. */
6284 			if (delete_snapshot_bserrno == 0) {
6285 				deleted = true;
6286 			}
6287 		}
6288 
6289 		spdk_blob_close(blob, blob_op_complete, NULL);
6290 		poll_threads();
6291 		CU_ASSERT(g_bserrno == 0);
6292 
6293 		spdk_bs_unload(bs, bs_op_complete, NULL);
6294 		poll_threads();
6295 		CU_ASSERT(g_bserrno == 0);
6296 
6297 		thresholds.general_threshold++;
6298 	}
6299 }
6300 
6301 static void
6302 blob_create_snapshot_power_failure(void)
6303 {
6304 	struct spdk_blob_store *bs = g_bs;
6305 	struct spdk_bs_dev *dev;
6306 	struct spdk_blob_opts opts;
6307 	struct spdk_blob *blob, *snapshot;
6308 	struct spdk_power_failure_thresholds thresholds = {};
6309 	spdk_blob_id blobid, snapshotid;
6310 	const void *value;
6311 	size_t value_len;
6312 	size_t count;
6313 	spdk_blob_id ids[3] = {};
6314 	int rc;
6315 	bool created = false;
6316 	int create_snapshot_bserrno = -1;
6317 
6318 	thresholds.general_threshold = 1;
6319 	while (!created) {
6320 		dev = init_dev();
6321 
6322 		spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
6323 		poll_threads();
6324 		CU_ASSERT(g_bserrno == 0);
6325 		SPDK_CU_ASSERT_FATAL(g_bs != NULL);
6326 		bs = g_bs;
6327 
6328 		/* Create blob */
6329 		ut_spdk_blob_opts_init(&opts);
6330 		opts.num_clusters = 10;
6331 
6332 		spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
6333 		poll_threads();
6334 		CU_ASSERT(g_bserrno == 0);
6335 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
6336 		blobid = g_blobid;
6337 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6338 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6339 
6340 		dev_set_power_failure_thresholds(thresholds);
6341 
6342 		/* Create snapshot */
6343 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
6344 		poll_threads();
6345 		create_snapshot_bserrno = g_bserrno;
6346 		snapshotid = g_blobid;
6347 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6348 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6349 
6350 		/* Do not shut down cleanly. Assumption is that after create snapshot
6351 		 * reports success, both blobs should be power-fail safe. */
6352 		dev_reset_power_failure_event();
6353 		ut_bs_dirty_load(&bs, NULL);
6354 
6355 		SPDK_CU_ASSERT_FATAL(spdk_bit_pool_is_allocated(bs->used_clusters, 1));
6356 		SPDK_CU_ASSERT_FATAL(!spdk_bit_pool_is_allocated(bs->used_clusters, 11));
6357 
6358 		spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
6359 		poll_threads();
6360 		CU_ASSERT(g_bserrno == 0);
6361 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6362 		blob = g_blob;
6363 
6364 		if (snapshotid != SPDK_BLOBID_INVALID) {
6365 			spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
6366 			poll_threads();
6367 		}
6368 
6369 		if ((snapshotid != SPDK_BLOBID_INVALID) && (g_bserrno == 0)) {
6370 			SPDK_CU_ASSERT_FATAL(g_blob != NULL);
6371 			snapshot = g_blob;
6372 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(blob) == true);
6373 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(snapshot) == false);
6374 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
6375 			count = SPDK_COUNTOF(ids);
6376 			rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
6377 			CU_ASSERT(rc == 0);
6378 			CU_ASSERT(count == 1);
6379 			CU_ASSERT(ids[0] == blobid);
6380 			rc = spdk_blob_get_xattr_value(snapshot, SNAPSHOT_IN_PROGRESS, &value, &value_len);
6381 			CU_ASSERT(rc != 0);
6382 
6383 			spdk_blob_close(snapshot, blob_op_complete, NULL);
6384 			poll_threads();
6385 			CU_ASSERT(g_bserrno == 0);
6386 			if (create_snapshot_bserrno == 0) {
6387 				created = true;
6388 			}
6389 		} else {
6390 			CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
6391 			SPDK_CU_ASSERT_FATAL(spdk_blob_is_thin_provisioned(blob) == false);
6392 		}
6393 
6394 		spdk_blob_close(blob, blob_op_complete, NULL);
6395 		poll_threads();
6396 		CU_ASSERT(g_bserrno == 0);
6397 
6398 		spdk_bs_unload(bs, bs_op_complete, NULL);
6399 		poll_threads();
6400 		CU_ASSERT(g_bserrno == 0);
6401 
6402 		thresholds.general_threshold++;
6403 	}
6404 }
6405 
6406 static void
6407 test_io_write(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6408 {
6409 	uint8_t payload_ff[64 * 512];
6410 	uint8_t payload_aa[64 * 512];
6411 	uint8_t payload_00[64 * 512];
6412 	uint8_t *cluster0, *cluster1;
6413 
6414 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6415 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6416 	memset(payload_00, 0x00, sizeof(payload_00));
6417 
6418 	/* Try to perform I/O with io unit = 512 */
6419 	spdk_blob_io_write(blob, channel, payload_ff, 0, 1, blob_op_complete, NULL);
6420 	poll_threads();
6421 	CU_ASSERT(g_bserrno == 0);
6422 
6423 	/* If thin provisioned is set cluster should be allocated now */
6424 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[0] != 0);
6425 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6426 
6427 	/* Each character 0-F symbolizes single io_unit containing 512 bytes block filled with that character.
6428 	* Each page is separated by |. Whole block [...] symbolizes one cluster (containing 4 pages). */
6429 	/* cluster0: [ F000 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6430 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6431 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 31 * 512) == 0);
6432 
6433 	/* Verify write with offset on first page */
6434 	spdk_blob_io_write(blob, channel, payload_ff, 2, 1, blob_op_complete, NULL);
6435 	poll_threads();
6436 	CU_ASSERT(g_bserrno == 0);
6437 
6438 	/* cluster0: [ F0F0 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6439 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6440 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6441 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6442 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6443 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_00, 28 * 512) == 0);
6444 
6445 	/* Verify write with offset on first page */
6446 	spdk_blob_io_write(blob, channel, payload_ff, 4, 4, blob_op_complete, NULL);
6447 	poll_threads();
6448 
6449 	/* cluster0: [ F0F0 FFFF | 0000 0000 | 0000 0000 | 0000 0000 ] */
6450 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6451 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6452 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6453 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6454 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 4 * 512) == 0);
6455 	CU_ASSERT(memcmp(cluster0 + 8 * 512, payload_00, 24 * 512) == 0);
6456 
6457 	/* Verify write with offset on second page */
6458 	spdk_blob_io_write(blob, channel, payload_ff, 8, 4, blob_op_complete, NULL);
6459 	poll_threads();
6460 
6461 	/* cluster0: [ F0F0 FFFF | FFFF 0000 | 0000 0000 | 0000 0000 ] */
6462 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6463 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6464 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6465 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6466 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 8 * 512) == 0);
6467 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6468 
6469 	/* Verify write across multiple pages */
6470 	spdk_blob_io_write(blob, channel, payload_aa, 4, 8, blob_op_complete, NULL);
6471 	poll_threads();
6472 
6473 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 0000 ] */
6474 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6475 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6476 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6477 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6478 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6479 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6480 
6481 	/* Verify write across multiple clusters */
6482 	spdk_blob_io_write(blob, channel, payload_ff, 28, 8, blob_op_complete, NULL);
6483 	poll_threads();
6484 
6485 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6486 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6487 
6488 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6489 	 * cluster1: [ FFFF 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6490 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6491 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6492 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6493 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6494 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6495 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6496 
6497 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6498 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 28 * 512) == 0);
6499 
6500 	/* Verify write to second cluster */
6501 	spdk_blob_io_write(blob, channel, payload_ff, 32 + 12, 2, blob_op_complete, NULL);
6502 	poll_threads();
6503 
6504 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6505 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6506 
6507 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6508 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ] */
6509 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6510 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6511 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6512 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6513 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6514 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6515 
6516 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6517 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 8 * 512) == 0);
6518 	CU_ASSERT(memcmp(cluster1 + 12 * 512, payload_ff, 2 * 512) == 0);
6519 	CU_ASSERT(memcmp(cluster1 + 14 * 512, payload_00, 18 * 512) == 0);
6520 }
6521 
6522 static void
6523 test_io_read(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6524 {
6525 	uint8_t payload_read[64 * 512];
6526 	uint8_t payload_ff[64 * 512];
6527 	uint8_t payload_aa[64 * 512];
6528 	uint8_t payload_00[64 * 512];
6529 
6530 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6531 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6532 	memset(payload_00, 0x00, sizeof(payload_00));
6533 
6534 	/* Read only first io unit */
6535 	/* cluster0: [ (F)0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6536 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6537 	 * payload_read: F000 0000 | 0000 0000 ... */
6538 	memset(payload_read, 0x00, sizeof(payload_read));
6539 	spdk_blob_io_read(blob, channel, payload_read, 0, 1, blob_op_complete, NULL);
6540 	poll_threads();
6541 	CU_ASSERT(g_bserrno == 0);
6542 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6543 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 31 * 512) == 0);
6544 
6545 	/* Read four io_units starting from offset = 2
6546 	 * cluster0: [ F0(F0 AA)AA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6547 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6548 	 * payload_read: F0AA 0000 | 0000 0000 ... */
6549 
6550 	memset(payload_read, 0x00, sizeof(payload_read));
6551 	spdk_blob_io_read(blob, channel, payload_read, 2, 4, blob_op_complete, NULL);
6552 	poll_threads();
6553 	CU_ASSERT(g_bserrno == 0);
6554 
6555 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6556 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
6557 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_aa, 512) == 0);
6558 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_aa, 512) == 0);
6559 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6560 
6561 	/* Read eight io_units across multiple pages
6562 	 * cluster0: [ F0F0 (AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6563 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6564 	 * payload_read: AAAA AAAA | 0000 0000 ... */
6565 	memset(payload_read, 0x00, sizeof(payload_read));
6566 	spdk_blob_io_read(blob, channel, payload_read, 4, 8, blob_op_complete, NULL);
6567 	poll_threads();
6568 	CU_ASSERT(g_bserrno == 0);
6569 
6570 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_aa, 8 * 512) == 0);
6571 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6572 
6573 	/* Read eight io_units across multiple clusters
6574 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 (FFFF ]
6575 	 * cluster1: [ FFFF) 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6576 	 * payload_read: FFFF FFFF | 0000 0000 ... */
6577 	memset(payload_read, 0x00, sizeof(payload_read));
6578 	spdk_blob_io_read(blob, channel, payload_read, 28, 8, blob_op_complete, NULL);
6579 	poll_threads();
6580 	CU_ASSERT(g_bserrno == 0);
6581 
6582 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 8 * 512) == 0);
6583 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6584 
6585 	/* Read four io_units from second cluster
6586 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6587 	 * cluster1: [ FFFF 0000 | 00(00 FF)00 | 0000 0000 | 0000 0000 ]
6588 	 * payload_read: 00FF 0000 | 0000 0000 ... */
6589 	memset(payload_read, 0x00, sizeof(payload_read));
6590 	spdk_blob_io_read(blob, channel, payload_read, 32 + 10, 4, blob_op_complete, NULL);
6591 	poll_threads();
6592 	CU_ASSERT(g_bserrno == 0);
6593 
6594 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_00, 2 * 512) == 0);
6595 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 2 * 512) == 0);
6596 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6597 
6598 	/* Read second cluster
6599 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6600 	 * cluster1: [ (FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ]
6601 	 * payload_read: FFFF 0000 | 0000 FF00 ... */
6602 	memset(payload_read, 0x00, sizeof(payload_read));
6603 	spdk_blob_io_read(blob, channel, payload_read, 32, 32, blob_op_complete, NULL);
6604 	poll_threads();
6605 	CU_ASSERT(g_bserrno == 0);
6606 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 4 * 512) == 0);
6607 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 8 * 512) == 0);
6608 	CU_ASSERT(memcmp(payload_read + 12 * 512, payload_ff, 2 * 512) == 0);
6609 	CU_ASSERT(memcmp(payload_read + 14 * 512, payload_00, 18 * 512) == 0);
6610 
6611 	/* Read whole two clusters
6612 	 * cluster0: [ (F0F0 AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6613 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ] */
6614 	memset(payload_read, 0x00, sizeof(payload_read));
6615 	spdk_blob_io_read(blob, channel, payload_read, 0, 64, blob_op_complete, NULL);
6616 	poll_threads();
6617 	CU_ASSERT(g_bserrno == 0);
6618 
6619 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6620 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
6621 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 512) == 0);
6622 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_00, 512) == 0);
6623 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_aa, 8 * 512) == 0);
6624 	CU_ASSERT(memcmp(payload_read + 28 * 512, payload_ff, 4 * 512) == 0);
6625 
6626 	CU_ASSERT(memcmp(payload_read + (32 + 0) * 512, payload_ff, 4 * 512) == 0);
6627 	CU_ASSERT(memcmp(payload_read + (32 + 4) * 512, payload_00, 8 * 512) == 0);
6628 	CU_ASSERT(memcmp(payload_read + (32 + 12) * 512, payload_ff, 2 * 512) == 0);
6629 	CU_ASSERT(memcmp(payload_read + (32 + 14) * 512, payload_00, 18 * 512) == 0);
6630 }
6631 
6632 
6633 static void
6634 test_io_unmap(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6635 {
6636 	uint8_t payload_ff[64 * 512];
6637 	uint8_t payload_aa[64 * 512];
6638 	uint8_t payload_00[64 * 512];
6639 	uint8_t *cluster0, *cluster1;
6640 
6641 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6642 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6643 	memset(payload_00, 0x00, sizeof(payload_00));
6644 
6645 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6646 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6647 
6648 	/* Unmap */
6649 	spdk_blob_io_unmap(blob, channel, 0, 64, blob_op_complete, NULL);
6650 	poll_threads();
6651 
6652 	CU_ASSERT(g_bserrno == 0);
6653 
6654 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_00, 32 * 512) == 0);
6655 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_00, 32 * 512) == 0);
6656 }
6657 
6658 static void
6659 test_io_zeroes(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel)
6660 {
6661 	uint8_t payload_ff[64 * 512];
6662 	uint8_t payload_aa[64 * 512];
6663 	uint8_t payload_00[64 * 512];
6664 	uint8_t *cluster0, *cluster1;
6665 
6666 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6667 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6668 	memset(payload_00, 0x00, sizeof(payload_00));
6669 
6670 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6671 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6672 
6673 	/* Write zeroes  */
6674 	spdk_blob_io_write_zeroes(blob, channel, 0, 64, blob_op_complete, NULL);
6675 	poll_threads();
6676 
6677 	CU_ASSERT(g_bserrno == 0);
6678 
6679 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_00, 32 * 512) == 0);
6680 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_00, 32 * 512) == 0);
6681 }
6682 
6683 static inline void
6684 test_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
6685 		    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
6686 		    spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
6687 {
6688 	if (io_opts) {
6689 		g_dev_writev_ext_called = false;
6690 		memset(&g_blob_ext_io_opts, 0, sizeof(g_blob_ext_io_opts));
6691 		spdk_blob_io_writev_ext(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL,
6692 					io_opts);
6693 	} else {
6694 		spdk_blob_io_writev(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL);
6695 	}
6696 	poll_threads();
6697 	CU_ASSERT(g_bserrno == 0);
6698 	if (io_opts) {
6699 		CU_ASSERT(g_dev_writev_ext_called);
6700 		CU_ASSERT(memcmp(io_opts, &g_blob_ext_io_opts, sizeof(g_blob_ext_io_opts)) == 0);
6701 	}
6702 }
6703 
6704 static void
6705 test_iov_write(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel,
6706 	       bool ext_api)
6707 {
6708 	uint8_t payload_ff[64 * 512];
6709 	uint8_t payload_aa[64 * 512];
6710 	uint8_t payload_00[64 * 512];
6711 	uint8_t *cluster0, *cluster1;
6712 	struct iovec iov[4];
6713 	struct spdk_blob_ext_io_opts ext_opts = {
6714 		.memory_domain = (struct spdk_memory_domain *)0xfeedbeef,
6715 		.memory_domain_ctx = (void *)0xf00df00d,
6716 		.size = sizeof(struct spdk_blob_ext_io_opts),
6717 		.user_ctx = (void *)123,
6718 	};
6719 
6720 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6721 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6722 	memset(payload_00, 0x00, sizeof(payload_00));
6723 
6724 	/* Try to perform I/O with io unit = 512 */
6725 	iov[0].iov_base = payload_ff;
6726 	iov[0].iov_len = 1 * 512;
6727 
6728 	test_blob_io_writev(blob, channel, iov, 1, 0, 1, blob_op_complete, NULL,
6729 			    ext_api ? &ext_opts : NULL);
6730 
6731 	/* If thin provisioned is set cluster should be allocated now */
6732 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[0] != 0);
6733 	cluster0 = &g_dev_buffer[blob->active.clusters[0] * dev->blocklen];
6734 
6735 	/* Each character 0-F symbolizes single io_unit containing 512 bytes block filled with that character.
6736 	* Each page is separated by |. Whole block [...] symbolizes one cluster (containing 4 pages). */
6737 	/* cluster0: [ F000 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6738 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6739 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 31 * 512) == 0);
6740 
6741 	/* Verify write with offset on first page */
6742 	iov[0].iov_base = payload_ff;
6743 	iov[0].iov_len = 1 * 512;
6744 
6745 	test_blob_io_writev(blob, channel, iov, 1, 2, 1, blob_op_complete, NULL,
6746 			    ext_api ? &ext_opts : NULL);
6747 
6748 	/* cluster0: [ F0F0 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6749 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6750 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6751 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6752 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6753 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_00, 28 * 512) == 0);
6754 
6755 	/* Verify write with offset on first page */
6756 	iov[0].iov_base = payload_ff;
6757 	iov[0].iov_len = 4 * 512;
6758 	spdk_blob_io_writev(blob, channel, iov, 1, 4, 4, blob_op_complete, NULL);
6759 	poll_threads();
6760 
6761 	/* cluster0: [ F0F0 FFFF | 0000 0000 | 0000 0000 | 0000 0000 ] */
6762 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6763 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6764 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6765 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6766 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_ff, 4 * 512) == 0);
6767 	CU_ASSERT(memcmp(cluster0 + 8 * 512, payload_00, 24 * 512) == 0);
6768 
6769 	/* Verify write with offset on second page */
6770 	iov[0].iov_base = payload_ff;
6771 	iov[0].iov_len = 4 * 512;
6772 	spdk_blob_io_writev(blob, channel, iov, 1, 8, 4, blob_op_complete, NULL);
6773 	poll_threads();
6774 
6775 	/* cluster0: [ F0F0 FFFF | FFFF 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, 8 * 512) == 0);
6781 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6782 
6783 	/* Verify write across multiple pages */
6784 	iov[0].iov_base = payload_aa;
6785 	iov[0].iov_len = 8 * 512;
6786 
6787 	test_blob_io_writev(blob, channel, iov, 1, 4, 8, blob_op_complete, NULL,
6788 			    ext_api ? &ext_opts : NULL);
6789 
6790 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 0000 ] */
6791 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6792 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6793 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6794 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6795 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6796 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 20 * 512) == 0);
6797 
6798 	/* Verify write across multiple clusters */
6799 
6800 	iov[0].iov_base = payload_ff;
6801 	iov[0].iov_len = 8 * 512;
6802 
6803 	test_blob_io_writev(blob, channel, iov, 1, 28, 8, blob_op_complete, NULL,
6804 			    ext_api ? &ext_opts : NULL);
6805 
6806 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6807 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6808 
6809 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6810 	 * cluster1: [ FFFF 0000 | 0000 0000 | 0000 0000 | 0000 0000 ] */
6811 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6812 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6813 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6814 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6815 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6816 	CU_ASSERT(memcmp(cluster0 + 12 * 512, payload_00, 16 * 512) == 0);
6817 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6818 
6819 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6820 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 28 * 512) == 0);
6821 
6822 	/* Verify write to second cluster */
6823 
6824 	iov[0].iov_base = payload_ff;
6825 	iov[0].iov_len = 2 * 512;
6826 
6827 	test_blob_io_writev(blob, channel, iov, 1, 32 + 12, 2, blob_op_complete, NULL,
6828 			    ext_api ? &ext_opts : NULL);
6829 
6830 	SPDK_CU_ASSERT_FATAL(blob->active.clusters[1] != 0);
6831 	cluster1 = &g_dev_buffer[blob->active.clusters[1] * dev->blocklen];
6832 
6833 	/* cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6834 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ] */
6835 	CU_ASSERT(memcmp(cluster0 + 0 * 512, payload_ff, 512) == 0);
6836 	CU_ASSERT(memcmp(cluster0 + 1 * 512, payload_00, 512) == 0);
6837 	CU_ASSERT(memcmp(cluster0 + 2 * 512, payload_ff, 512) == 0);
6838 	CU_ASSERT(memcmp(cluster0 + 3 * 512, payload_00, 512) == 0);
6839 	CU_ASSERT(memcmp(cluster0 + 4 * 512, payload_aa, 8 * 512) == 0);
6840 	CU_ASSERT(memcmp(cluster0 + 28 * 512, payload_ff, 4 * 512) == 0);
6841 
6842 	CU_ASSERT(memcmp(cluster1 + 0 * 512, payload_ff, 4 * 512) == 0);
6843 	CU_ASSERT(memcmp(cluster1 + 4 * 512, payload_00, 8 * 512) == 0);
6844 	CU_ASSERT(memcmp(cluster1 + 12 * 512, payload_ff, 2 * 512) == 0);
6845 	CU_ASSERT(memcmp(cluster1 + 14 * 512, payload_00, 18 * 512) == 0);
6846 }
6847 
6848 static inline void
6849 test_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
6850 		   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
6851 		   spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
6852 {
6853 	if (io_opts) {
6854 		g_dev_readv_ext_called = false;
6855 		memset(&g_blob_ext_io_opts, 0, sizeof(g_blob_ext_io_opts));
6856 		spdk_blob_io_readv_ext(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL, io_opts);
6857 	} else {
6858 		spdk_blob_io_readv(blob, channel, iov, iovcnt, offset, length, blob_op_complete, NULL);
6859 	}
6860 	poll_threads();
6861 	CU_ASSERT(g_bserrno == 0);
6862 	if (io_opts) {
6863 		CU_ASSERT(g_dev_readv_ext_called);
6864 		CU_ASSERT(memcmp(io_opts, &g_blob_ext_io_opts, sizeof(g_blob_ext_io_opts)) == 0);
6865 	}
6866 }
6867 
6868 static void
6869 test_iov_read(struct spdk_bs_dev *dev, struct spdk_blob *blob, struct spdk_io_channel *channel,
6870 	      bool ext_api)
6871 {
6872 	uint8_t payload_read[64 * 512];
6873 	uint8_t payload_ff[64 * 512];
6874 	uint8_t payload_aa[64 * 512];
6875 	uint8_t payload_00[64 * 512];
6876 	struct iovec iov[4];
6877 	struct spdk_blob_ext_io_opts ext_opts = {
6878 		.memory_domain = (struct spdk_memory_domain *)0xfeedbeef,
6879 		.memory_domain_ctx = (void *)0xf00df00d,
6880 		.size = sizeof(struct spdk_blob_ext_io_opts),
6881 		.user_ctx = (void *)123,
6882 	};
6883 
6884 	memset(payload_ff, 0xFF, sizeof(payload_ff));
6885 	memset(payload_aa, 0xAA, sizeof(payload_aa));
6886 	memset(payload_00, 0x00, sizeof(payload_00));
6887 
6888 	/* Read only first io unit */
6889 	/* cluster0: [ (F)0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6890 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6891 	 * payload_read: F000 0000 | 0000 0000 ... */
6892 	memset(payload_read, 0x00, sizeof(payload_read));
6893 	iov[0].iov_base = payload_read;
6894 	iov[0].iov_len = 1 * 512;
6895 
6896 	test_blob_io_readv(blob, channel, iov, 1, 0, 1, blob_op_complete, NULL, ext_api ? &ext_opts : NULL);
6897 
6898 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
6899 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 31 * 512) == 0);
6900 
6901 	/* Read four io_units starting from offset = 2
6902 	 * cluster0: [ F0(F0 AA)AA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6903 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6904 	 * payload_read: F0AA 0000 | 0000 0000 ... */
6905 
6906 	memset(payload_read, 0x00, sizeof(payload_read));
6907 	iov[0].iov_base = payload_read;
6908 	iov[0].iov_len = 4 * 512;
6909 
6910 	test_blob_io_readv(blob, channel, iov, 1, 2, 4, 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, 512) == 0);
6914 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_aa, 512) == 0);
6915 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_aa, 512) == 0);
6916 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6917 
6918 	/* Read eight io_units across multiple pages
6919 	 * cluster0: [ F0F0 (AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6920 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6921 	 * payload_read: AAAA AAAA | 0000 0000 ... */
6922 	memset(payload_read, 0x00, sizeof(payload_read));
6923 	iov[0].iov_base = payload_read;
6924 	iov[0].iov_len = 4 * 512;
6925 	iov[1].iov_base = payload_read + 4 * 512;
6926 	iov[1].iov_len = 4 * 512;
6927 
6928 	test_blob_io_readv(blob, channel, iov, 2, 4, 8, blob_op_complete, NULL, ext_api ? &ext_opts : NULL);
6929 
6930 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_aa, 8 * 512) == 0);
6931 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6932 
6933 	/* Read eight io_units across multiple clusters
6934 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 (FFFF ]
6935 	 * cluster1: [ FFFF) 0000 | 0000 FF00 | 0000 0000 | 0000 0000 ]
6936 	 * payload_read: FFFF FFFF | 0000 0000 ... */
6937 	memset(payload_read, 0x00, sizeof(payload_read));
6938 	iov[0].iov_base = payload_read;
6939 	iov[0].iov_len = 2 * 512;
6940 	iov[1].iov_base = payload_read + 2 * 512;
6941 	iov[1].iov_len = 2 * 512;
6942 	iov[2].iov_base = payload_read + 4 * 512;
6943 	iov[2].iov_len = 2 * 512;
6944 	iov[3].iov_base = payload_read + 6 * 512;
6945 	iov[3].iov_len = 2 * 512;
6946 
6947 	test_blob_io_readv(blob, channel, iov, 4, 28, 8, blob_op_complete, NULL,
6948 			   ext_api ? &ext_opts : NULL);
6949 
6950 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 8 * 512) == 0);
6951 	CU_ASSERT(memcmp(payload_read + 8 * 512, payload_00, 24 * 512) == 0);
6952 
6953 	/* Read four io_units from second cluster
6954 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6955 	 * cluster1: [ FFFF 0000 | 00(00 FF)00 | 0000 0000 | 0000 0000 ]
6956 	 * payload_read: 00FF 0000 | 0000 0000 ... */
6957 	memset(payload_read, 0x00, sizeof(payload_read));
6958 	iov[0].iov_base = payload_read;
6959 	iov[0].iov_len = 1 * 512;
6960 	iov[1].iov_base = payload_read + 1 * 512;
6961 	iov[1].iov_len = 3 * 512;
6962 
6963 	test_blob_io_readv(blob, channel, iov, 2, 32 + 10, 4, blob_op_complete, NULL,
6964 			   ext_api ? &ext_opts : NULL);
6965 
6966 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_00, 2 * 512) == 0);
6967 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 2 * 512) == 0);
6968 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 28 * 512) == 0);
6969 
6970 	/* Read second cluster
6971 	 * cluster0: [ F0F0 AAAA | AAAA 0000 | 0000 0000 | 0000 FFFF ]
6972 	 * cluster1: [ (FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ]
6973 	 * payload_read: FFFF 0000 | 0000 FF00 ... */
6974 	memset(payload_read, 0x00, sizeof(payload_read));
6975 	iov[0].iov_base = payload_read;
6976 	iov[0].iov_len = 1 * 512;
6977 	iov[1].iov_base = payload_read + 1 * 512;
6978 	iov[1].iov_len = 2 * 512;
6979 	iov[2].iov_base = payload_read + 3 * 512;
6980 	iov[2].iov_len = 4 * 512;
6981 	iov[3].iov_base = payload_read + 7 * 512;
6982 	iov[3].iov_len = 25 * 512;
6983 
6984 	test_blob_io_readv(blob, channel, iov, 4, 32, 32, blob_op_complete, NULL,
6985 			   ext_api ? &ext_opts : NULL);
6986 
6987 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 4 * 512) == 0);
6988 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_00, 8 * 512) == 0);
6989 	CU_ASSERT(memcmp(payload_read + 12 * 512, payload_ff, 2 * 512) == 0);
6990 	CU_ASSERT(memcmp(payload_read + 14 * 512, payload_00, 18 * 512) == 0);
6991 
6992 	/* Read whole two clusters
6993 	 * cluster0: [ (F0F0 AAAA | AAAA) 0000 | 0000 0000 | 0000 FFFF ]
6994 	 * cluster1: [ FFFF 0000 | 0000 FF00 | 0000 0000 | 0000 0000) ] */
6995 	memset(payload_read, 0x00, sizeof(payload_read));
6996 	iov[0].iov_base = payload_read;
6997 	iov[0].iov_len = 1 * 512;
6998 	iov[1].iov_base = payload_read + 1 * 512;
6999 	iov[1].iov_len = 8 * 512;
7000 	iov[2].iov_base = payload_read + 9 * 512;
7001 	iov[2].iov_len = 16 * 512;
7002 	iov[3].iov_base = payload_read + 25 * 512;
7003 	iov[3].iov_len = 39 * 512;
7004 
7005 	test_blob_io_readv(blob, channel, iov, 4, 0, 64, blob_op_complete, NULL,
7006 			   ext_api ? &ext_opts : NULL);
7007 
7008 	CU_ASSERT(memcmp(payload_read + 0 * 512, payload_ff, 512) == 0);
7009 	CU_ASSERT(memcmp(payload_read + 1 * 512, payload_00, 512) == 0);
7010 	CU_ASSERT(memcmp(payload_read + 2 * 512, payload_ff, 512) == 0);
7011 	CU_ASSERT(memcmp(payload_read + 3 * 512, payload_00, 512) == 0);
7012 	CU_ASSERT(memcmp(payload_read + 4 * 512, payload_aa, 8 * 512) == 0);
7013 	CU_ASSERT(memcmp(payload_read + 28 * 512, payload_ff, 4 * 512) == 0);
7014 
7015 	CU_ASSERT(memcmp(payload_read + (32 + 0) * 512, payload_ff, 4 * 512) == 0);
7016 	CU_ASSERT(memcmp(payload_read + (32 + 4) * 512, payload_00, 8 * 512) == 0);
7017 	CU_ASSERT(memcmp(payload_read + (32 + 12) * 512, payload_ff, 2 * 512) == 0);
7018 	CU_ASSERT(memcmp(payload_read + (32 + 14) * 512, payload_00, 18 * 512) == 0);
7019 }
7020 
7021 static void
7022 blob_io_unit(void)
7023 {
7024 	struct spdk_bs_opts bsopts;
7025 	struct spdk_blob_opts opts;
7026 	struct spdk_blob_store *bs;
7027 	struct spdk_bs_dev *dev;
7028 	struct spdk_blob *blob, *snapshot, *clone;
7029 	spdk_blob_id blobid;
7030 	struct spdk_io_channel *channel;
7031 
7032 	/* Create dev with 512 bytes io unit size */
7033 
7034 	spdk_bs_opts_init(&bsopts, sizeof(bsopts));
7035 	bsopts.cluster_sz = SPDK_BS_PAGE_SIZE * 4;	/* 8 * 4 = 32 io_unit */
7036 	snprintf(bsopts.bstype.bstype, sizeof(bsopts.bstype.bstype), "TESTTYPE");
7037 
7038 	/* Try to initialize a new blob store with unsupported io_unit */
7039 	dev = init_dev();
7040 	dev->blocklen = 512;
7041 	dev->blockcnt =  DEV_BUFFER_SIZE / dev->blocklen;
7042 
7043 	/* Initialize a new blob store */
7044 	spdk_bs_init(dev, &bsopts, bs_op_with_handle_complete, NULL);
7045 	poll_threads();
7046 	CU_ASSERT(g_bserrno == 0);
7047 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7048 	bs = g_bs;
7049 
7050 	CU_ASSERT(spdk_bs_get_io_unit_size(bs) == 512);
7051 	channel = spdk_bs_alloc_io_channel(bs);
7052 
7053 	/* Create thick provisioned blob */
7054 	ut_spdk_blob_opts_init(&opts);
7055 	opts.thin_provision = false;
7056 	opts.num_clusters = 32;
7057 
7058 	blob = ut_blob_create_and_open(bs, &opts);
7059 	blobid = spdk_blob_get_id(blob);
7060 
7061 	test_io_write(dev, blob, channel);
7062 	test_io_read(dev, blob, channel);
7063 	test_io_zeroes(dev, blob, channel);
7064 
7065 	test_iov_write(dev, blob, channel, false);
7066 	test_iov_read(dev, blob, channel, false);
7067 	test_io_zeroes(dev, blob, channel);
7068 
7069 	test_iov_write(dev, blob, channel, true);
7070 	test_iov_read(dev, blob, channel, true);
7071 
7072 	test_io_unmap(dev, blob, channel);
7073 
7074 	spdk_blob_close(blob, blob_op_complete, NULL);
7075 	poll_threads();
7076 	CU_ASSERT(g_bserrno == 0);
7077 	blob = NULL;
7078 	g_blob = NULL;
7079 
7080 	/* Create thin provisioned blob */
7081 
7082 	ut_spdk_blob_opts_init(&opts);
7083 	opts.thin_provision = true;
7084 	opts.num_clusters = 32;
7085 
7086 	blob = ut_blob_create_and_open(bs, &opts);
7087 	blobid = spdk_blob_get_id(blob);
7088 
7089 	test_io_write(dev, blob, channel);
7090 	test_io_read(dev, blob, channel);
7091 	test_io_zeroes(dev, blob, channel);
7092 
7093 	test_iov_write(dev, blob, channel, false);
7094 	test_iov_read(dev, blob, channel, false);
7095 	test_io_zeroes(dev, blob, channel);
7096 
7097 	test_iov_write(dev, blob, channel, true);
7098 	test_iov_read(dev, blob, channel, true);
7099 
7100 	/* Create snapshot */
7101 
7102 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7103 	poll_threads();
7104 	CU_ASSERT(g_bserrno == 0);
7105 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7106 	blobid = g_blobid;
7107 
7108 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7109 	poll_threads();
7110 	CU_ASSERT(g_bserrno == 0);
7111 	CU_ASSERT(g_blob != NULL);
7112 	snapshot = g_blob;
7113 
7114 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7115 	poll_threads();
7116 	CU_ASSERT(g_bserrno == 0);
7117 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7118 	blobid = g_blobid;
7119 
7120 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7121 	poll_threads();
7122 	CU_ASSERT(g_bserrno == 0);
7123 	CU_ASSERT(g_blob != NULL);
7124 	clone = g_blob;
7125 
7126 	test_io_read(dev, blob, channel);
7127 	test_io_read(dev, snapshot, channel);
7128 	test_io_read(dev, clone, channel);
7129 
7130 	test_iov_read(dev, blob, channel, false);
7131 	test_iov_read(dev, snapshot, channel, false);
7132 	test_iov_read(dev, clone, channel, false);
7133 
7134 	test_iov_read(dev, blob, channel, true);
7135 	test_iov_read(dev, snapshot, channel, true);
7136 	test_iov_read(dev, clone, channel, true);
7137 
7138 	/* Inflate clone */
7139 
7140 	spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
7141 	poll_threads();
7142 
7143 	CU_ASSERT(g_bserrno == 0);
7144 
7145 	test_io_read(dev, clone, channel);
7146 
7147 	test_io_unmap(dev, clone, channel);
7148 
7149 	test_iov_write(dev, clone, channel, false);
7150 	test_iov_read(dev, clone, channel, false);
7151 	test_io_unmap(dev, clone, channel);
7152 
7153 	test_iov_write(dev, clone, channel, true);
7154 	test_iov_read(dev, clone, channel, true);
7155 
7156 	spdk_blob_close(blob, blob_op_complete, NULL);
7157 	spdk_blob_close(snapshot, blob_op_complete, NULL);
7158 	spdk_blob_close(clone, blob_op_complete, NULL);
7159 	poll_threads();
7160 	CU_ASSERT(g_bserrno == 0);
7161 	blob = NULL;
7162 	g_blob = NULL;
7163 
7164 	spdk_bs_free_io_channel(channel);
7165 	poll_threads();
7166 
7167 	/* Unload the blob store */
7168 	spdk_bs_unload(bs, bs_op_complete, NULL);
7169 	poll_threads();
7170 	CU_ASSERT(g_bserrno == 0);
7171 	g_bs = NULL;
7172 	g_blob = NULL;
7173 	g_blobid = 0;
7174 }
7175 
7176 static void
7177 blob_io_unit_compatibility(void)
7178 {
7179 	struct spdk_bs_opts bsopts;
7180 	struct spdk_blob_store *bs;
7181 	struct spdk_bs_dev *dev;
7182 	struct spdk_bs_super_block *super;
7183 
7184 	/* Create dev with 512 bytes io unit size */
7185 
7186 	spdk_bs_opts_init(&bsopts, sizeof(bsopts));
7187 	bsopts.cluster_sz = SPDK_BS_PAGE_SIZE * 4;	/* 8 * 4 = 32 io_unit */
7188 	snprintf(bsopts.bstype.bstype, sizeof(bsopts.bstype.bstype), "TESTTYPE");
7189 
7190 	/* Try to initialize a new blob store with unsupported io_unit */
7191 	dev = init_dev();
7192 	dev->blocklen = 512;
7193 	dev->blockcnt =  DEV_BUFFER_SIZE / dev->blocklen;
7194 
7195 	/* Initialize a new blob store */
7196 	spdk_bs_init(dev, &bsopts, bs_op_with_handle_complete, NULL);
7197 	poll_threads();
7198 	CU_ASSERT(g_bserrno == 0);
7199 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7200 	bs = g_bs;
7201 
7202 	CU_ASSERT(spdk_bs_get_io_unit_size(bs) == 512);
7203 
7204 	/* Unload the blob store */
7205 	spdk_bs_unload(bs, bs_op_complete, NULL);
7206 	poll_threads();
7207 	CU_ASSERT(g_bserrno == 0);
7208 
7209 	/* Modify super block to behave like older version.
7210 	 * Check if loaded io unit size equals SPDK_BS_PAGE_SIZE */
7211 	super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
7212 	super->io_unit_size = 0;
7213 	super->crc = blob_md_page_calc_crc(super);
7214 
7215 	dev = init_dev();
7216 	dev->blocklen = 512;
7217 	dev->blockcnt =  DEV_BUFFER_SIZE / dev->blocklen;
7218 
7219 	spdk_bs_load(dev, &bsopts, bs_op_with_handle_complete, NULL);
7220 	poll_threads();
7221 	CU_ASSERT(g_bserrno == 0);
7222 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7223 	bs = g_bs;
7224 
7225 	CU_ASSERT(spdk_bs_get_io_unit_size(bs) == SPDK_BS_PAGE_SIZE);
7226 
7227 	/* Unload the blob store */
7228 	spdk_bs_unload(bs, bs_op_complete, NULL);
7229 	poll_threads();
7230 	CU_ASSERT(g_bserrno == 0);
7231 
7232 	g_bs = NULL;
7233 	g_blob = NULL;
7234 	g_blobid = 0;
7235 }
7236 
7237 static void
7238 first_sync_complete(void *cb_arg, int bserrno)
7239 {
7240 	struct spdk_blob *blob = cb_arg;
7241 	int rc;
7242 
7243 	CU_ASSERT(bserrno == 0);
7244 	rc = spdk_blob_set_xattr(blob, "sync", "second", strlen("second") + 1);
7245 	CU_ASSERT(rc == 0);
7246 	CU_ASSERT(g_bserrno == -1);
7247 
7248 	/* Keep g_bserrno at -1, only the
7249 	 * second sync completion should set it at 0. */
7250 }
7251 
7252 static void
7253 second_sync_complete(void *cb_arg, int bserrno)
7254 {
7255 	struct spdk_blob *blob = cb_arg;
7256 	const void *value;
7257 	size_t value_len;
7258 	int rc;
7259 
7260 	CU_ASSERT(bserrno == 0);
7261 
7262 	/* Verify that the first sync completion had a chance to execute */
7263 	rc = spdk_blob_get_xattr_value(blob, "sync", &value, &value_len);
7264 	CU_ASSERT(rc == 0);
7265 	SPDK_CU_ASSERT_FATAL(value != NULL);
7266 	CU_ASSERT(value_len == strlen("second") + 1);
7267 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, "second", value_len);
7268 
7269 	CU_ASSERT(g_bserrno == -1);
7270 	g_bserrno = bserrno;
7271 }
7272 
7273 static void
7274 blob_simultaneous_operations(void)
7275 {
7276 	struct spdk_blob_store *bs = g_bs;
7277 	struct spdk_blob_opts opts;
7278 	struct spdk_blob *blob, *snapshot;
7279 	spdk_blob_id blobid, snapshotid;
7280 	struct spdk_io_channel *channel;
7281 	int rc;
7282 
7283 	channel = spdk_bs_alloc_io_channel(bs);
7284 	SPDK_CU_ASSERT_FATAL(channel != NULL);
7285 
7286 	ut_spdk_blob_opts_init(&opts);
7287 	opts.num_clusters = 10;
7288 
7289 	blob = ut_blob_create_and_open(bs, &opts);
7290 	blobid = spdk_blob_get_id(blob);
7291 
7292 	/* Create snapshot and try to remove blob in the same time:
7293 	 * - snapshot should be created successfully
7294 	 * - delete operation should fail w -EBUSY */
7295 	CU_ASSERT(blob->locked_operation_in_progress == false);
7296 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7297 	CU_ASSERT(blob->locked_operation_in_progress == true);
7298 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
7299 	CU_ASSERT(blob->locked_operation_in_progress == true);
7300 	/* Deletion failure */
7301 	CU_ASSERT(g_bserrno == -EBUSY);
7302 	poll_threads();
7303 	CU_ASSERT(blob->locked_operation_in_progress == false);
7304 	/* Snapshot creation success */
7305 	CU_ASSERT(g_bserrno == 0);
7306 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7307 
7308 	snapshotid = g_blobid;
7309 
7310 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
7311 	poll_threads();
7312 	CU_ASSERT(g_bserrno == 0);
7313 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7314 	snapshot = g_blob;
7315 
7316 	/* Inflate blob and try to remove blob in the same time:
7317 	 * - blob should be inflated successfully
7318 	 * - delete operation should fail w -EBUSY */
7319 	CU_ASSERT(blob->locked_operation_in_progress == false);
7320 	spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
7321 	CU_ASSERT(blob->locked_operation_in_progress == true);
7322 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
7323 	CU_ASSERT(blob->locked_operation_in_progress == true);
7324 	/* Deletion failure */
7325 	CU_ASSERT(g_bserrno == -EBUSY);
7326 	poll_threads();
7327 	CU_ASSERT(blob->locked_operation_in_progress == false);
7328 	/* Inflation success */
7329 	CU_ASSERT(g_bserrno == 0);
7330 
7331 	/* Clone snapshot and try to remove snapshot in the same time:
7332 	 * - snapshot should be cloned successfully
7333 	 * - delete operation should fail w -EBUSY */
7334 	CU_ASSERT(blob->locked_operation_in_progress == false);
7335 	spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
7336 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
7337 	/* Deletion failure */
7338 	CU_ASSERT(g_bserrno == -EBUSY);
7339 	poll_threads();
7340 	CU_ASSERT(blob->locked_operation_in_progress == false);
7341 	/* Clone created */
7342 	CU_ASSERT(g_bserrno == 0);
7343 
7344 	/* Resize blob and try to remove blob in the same time:
7345 	 * - blob should be resized successfully
7346 	 * - delete operation should fail w -EBUSY */
7347 	CU_ASSERT(blob->locked_operation_in_progress == false);
7348 	spdk_blob_resize(blob, 50, blob_op_complete, NULL);
7349 	CU_ASSERT(blob->locked_operation_in_progress == true);
7350 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
7351 	CU_ASSERT(blob->locked_operation_in_progress == true);
7352 	/* Deletion failure */
7353 	CU_ASSERT(g_bserrno == -EBUSY);
7354 	poll_threads();
7355 	CU_ASSERT(blob->locked_operation_in_progress == false);
7356 	/* Blob resized successfully */
7357 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
7358 	poll_threads();
7359 	CU_ASSERT(g_bserrno == 0);
7360 
7361 	/* Issue two consecutive blob syncs, neither should fail.
7362 	 * Force sync to actually occur by marking blob dirty each time.
7363 	 * Execution of sync should not be enough to complete the operation,
7364 	 * since disk I/O is required to complete it. */
7365 	g_bserrno = -1;
7366 
7367 	rc = spdk_blob_set_xattr(blob, "sync", "first", strlen("first") + 1);
7368 	CU_ASSERT(rc == 0);
7369 	spdk_blob_sync_md(blob, first_sync_complete, blob);
7370 	CU_ASSERT(g_bserrno == -1);
7371 
7372 	spdk_blob_sync_md(blob, second_sync_complete, blob);
7373 	CU_ASSERT(g_bserrno == -1);
7374 
7375 	poll_threads();
7376 	CU_ASSERT(g_bserrno == 0);
7377 
7378 	spdk_bs_free_io_channel(channel);
7379 	poll_threads();
7380 
7381 	ut_blob_close_and_delete(bs, snapshot);
7382 	ut_blob_close_and_delete(bs, blob);
7383 }
7384 
7385 static void
7386 blob_persist_test(void)
7387 {
7388 	struct spdk_blob_store *bs = g_bs;
7389 	struct spdk_blob_opts opts;
7390 	struct spdk_blob *blob;
7391 	spdk_blob_id blobid;
7392 	struct spdk_io_channel *channel;
7393 	char *xattr;
7394 	size_t xattr_length;
7395 	int rc;
7396 	uint32_t page_count_clear, page_count_xattr;
7397 	uint64_t poller_iterations;
7398 	bool run_poller;
7399 
7400 	channel = spdk_bs_alloc_io_channel(bs);
7401 	SPDK_CU_ASSERT_FATAL(channel != NULL);
7402 
7403 	ut_spdk_blob_opts_init(&opts);
7404 	opts.num_clusters = 10;
7405 
7406 	blob = ut_blob_create_and_open(bs, &opts);
7407 	blobid = spdk_blob_get_id(blob);
7408 
7409 	/* Save the amount of md pages used after creation of a blob.
7410 	 * This should be consistent after removing xattr. */
7411 	page_count_clear = spdk_bit_array_count_set(bs->used_md_pages);
7412 	SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_clear);
7413 	SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_clear);
7414 
7415 	/* Add xattr with maximum length of descriptor to exceed single metadata page. */
7416 	xattr_length = SPDK_BS_MAX_DESC_SIZE - sizeof(struct spdk_blob_md_descriptor_xattr) -
7417 		       strlen("large_xattr");
7418 	xattr = calloc(xattr_length, sizeof(char));
7419 	SPDK_CU_ASSERT_FATAL(xattr != NULL);
7420 
7421 	rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
7422 	SPDK_CU_ASSERT_FATAL(rc == 0);
7423 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
7424 	poll_threads();
7425 	SPDK_CU_ASSERT_FATAL(g_bserrno == 0);
7426 
7427 	/* Save the amount of md pages used after adding the large xattr */
7428 	page_count_xattr = spdk_bit_array_count_set(bs->used_md_pages);
7429 	SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_xattr);
7430 	SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_xattr);
7431 
7432 	/* Add xattr to a blob and sync it. While sync is occurring, remove the xattr and sync again.
7433 	 * Interrupt the first sync after increasing number of poller iterations, until it succeeds.
7434 	 * Expectation is that after second sync completes no xattr is saved in metadata. */
7435 	poller_iterations = 1;
7436 	run_poller = true;
7437 	while (run_poller) {
7438 		rc = spdk_blob_set_xattr(blob, "large_xattr", xattr, xattr_length);
7439 		SPDK_CU_ASSERT_FATAL(rc == 0);
7440 		g_bserrno = -1;
7441 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
7442 		poll_thread_times(0, poller_iterations);
7443 		if (g_bserrno == 0) {
7444 			/* Poller iteration count was high enough for first sync to complete.
7445 			 * Verify that blob takes up enough of md_pages to store the xattr. */
7446 			SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_xattr);
7447 			SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_xattr);
7448 			SPDK_CU_ASSERT_FATAL(spdk_bit_array_count_set(bs->used_md_pages) == page_count_xattr);
7449 			run_poller = false;
7450 		}
7451 		rc = spdk_blob_remove_xattr(blob, "large_xattr");
7452 		SPDK_CU_ASSERT_FATAL(rc == 0);
7453 		spdk_blob_sync_md(blob, blob_op_complete, NULL);
7454 		poll_threads();
7455 		SPDK_CU_ASSERT_FATAL(g_bserrno == 0);
7456 		SPDK_CU_ASSERT_FATAL(blob->active.num_pages + blob->active.num_extent_pages == page_count_clear);
7457 		SPDK_CU_ASSERT_FATAL(blob->clean.num_pages + blob->clean.num_extent_pages == page_count_clear);
7458 		SPDK_CU_ASSERT_FATAL(spdk_bit_array_count_set(bs->used_md_pages) == page_count_clear);
7459 
7460 		/* Reload bs and re-open blob to verify that xattr was not persisted. */
7461 		spdk_blob_close(blob, blob_op_complete, NULL);
7462 		poll_threads();
7463 		CU_ASSERT(g_bserrno == 0);
7464 
7465 		ut_bs_reload(&bs, NULL);
7466 
7467 		spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7468 		poll_threads();
7469 		CU_ASSERT(g_bserrno == 0);
7470 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7471 		blob = g_blob;
7472 
7473 		rc = spdk_blob_get_xattr_value(blob, "large_xattr", (const void **)&xattr, &xattr_length);
7474 		SPDK_CU_ASSERT_FATAL(rc == -ENOENT);
7475 
7476 		poller_iterations++;
7477 		/* Stop at high iteration count to prevent infinite loop.
7478 		 * This value should be enough for first md sync to complete in any case. */
7479 		SPDK_CU_ASSERT_FATAL(poller_iterations < 50);
7480 	}
7481 
7482 	free(xattr);
7483 
7484 	ut_blob_close_and_delete(bs, blob);
7485 
7486 	spdk_bs_free_io_channel(channel);
7487 	poll_threads();
7488 }
7489 
7490 static void
7491 blob_decouple_snapshot(void)
7492 {
7493 	struct spdk_blob_store *bs = g_bs;
7494 	struct spdk_blob_opts opts;
7495 	struct spdk_blob *blob, *snapshot1, *snapshot2;
7496 	struct spdk_io_channel *channel;
7497 	spdk_blob_id blobid, snapshotid;
7498 	uint64_t cluster;
7499 
7500 	for (int delete_snapshot_first = 0; delete_snapshot_first <= 1; delete_snapshot_first++) {
7501 		channel = spdk_bs_alloc_io_channel(bs);
7502 		SPDK_CU_ASSERT_FATAL(channel != NULL);
7503 
7504 		ut_spdk_blob_opts_init(&opts);
7505 		opts.num_clusters = 10;
7506 		opts.thin_provision = false;
7507 
7508 		blob = ut_blob_create_and_open(bs, &opts);
7509 		blobid = spdk_blob_get_id(blob);
7510 
7511 		/* Create first snapshot */
7512 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 0);
7513 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7514 		poll_threads();
7515 		CU_ASSERT(g_bserrno == 0);
7516 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7517 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 1);
7518 		snapshotid = g_blobid;
7519 
7520 		spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
7521 		poll_threads();
7522 		CU_ASSERT(g_bserrno == 0);
7523 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7524 		snapshot1 = g_blob;
7525 
7526 		/* Create the second one */
7527 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 1);
7528 		spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
7529 		poll_threads();
7530 		CU_ASSERT(g_bserrno == 0);
7531 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7532 		CU_ASSERT_EQUAL(_get_snapshots_count(bs), 2);
7533 		snapshotid = g_blobid;
7534 
7535 		spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
7536 		poll_threads();
7537 		CU_ASSERT(g_bserrno == 0);
7538 		SPDK_CU_ASSERT_FATAL(g_blob != NULL);
7539 		snapshot2 = g_blob;
7540 		CU_ASSERT_EQUAL(spdk_blob_get_parent_snapshot(bs, snapshot2->id), snapshot1->id);
7541 
7542 		/* Now decouple the second snapshot forcing it to copy the written clusters */
7543 		spdk_bs_blob_decouple_parent(bs, channel, snapshot2->id, blob_op_complete, NULL);
7544 		poll_threads();
7545 		CU_ASSERT(g_bserrno == 0);
7546 
7547 		/* Verify that the snapshot has been decoupled and that the clusters have been copied */
7548 		CU_ASSERT_EQUAL(spdk_blob_get_parent_snapshot(bs, snapshot2->id), SPDK_BLOBID_INVALID);
7549 		for (cluster = 0; cluster < snapshot2->active.num_clusters; ++cluster) {
7550 			CU_ASSERT_NOT_EQUAL(snapshot2->active.clusters[cluster], 0);
7551 			CU_ASSERT_NOT_EQUAL(snapshot2->active.clusters[cluster],
7552 					    snapshot1->active.clusters[cluster]);
7553 		}
7554 
7555 		spdk_bs_free_io_channel(channel);
7556 
7557 		if (delete_snapshot_first) {
7558 			ut_blob_close_and_delete(bs, snapshot2);
7559 			ut_blob_close_and_delete(bs, snapshot1);
7560 			ut_blob_close_and_delete(bs, blob);
7561 		} else {
7562 			ut_blob_close_and_delete(bs, blob);
7563 			ut_blob_close_and_delete(bs, snapshot2);
7564 			ut_blob_close_and_delete(bs, snapshot1);
7565 		}
7566 		poll_threads();
7567 	}
7568 }
7569 
7570 static void
7571 blob_seek_io_unit(void)
7572 {
7573 	struct spdk_blob_store *bs = g_bs;
7574 	struct spdk_blob *blob;
7575 	struct spdk_io_channel *channel;
7576 	struct spdk_blob_opts opts;
7577 	uint64_t free_clusters;
7578 	uint8_t payload[10 * 4096];
7579 	uint64_t offset;
7580 	uint64_t io_unit, io_units_per_cluster;
7581 
7582 	free_clusters = spdk_bs_free_cluster_count(bs);
7583 
7584 	channel = spdk_bs_alloc_io_channel(bs);
7585 	CU_ASSERT(channel != NULL);
7586 
7587 	/* Set blob as thin provisioned */
7588 	ut_spdk_blob_opts_init(&opts);
7589 	opts.thin_provision = true;
7590 
7591 	/* Create a blob */
7592 	blob = ut_blob_create_and_open(bs, &opts);
7593 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
7594 
7595 	io_units_per_cluster = bs_io_units_per_cluster(blob);
7596 
7597 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
7598 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
7599 	poll_threads();
7600 	CU_ASSERT(g_bserrno == 0);
7601 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
7602 	CU_ASSERT(blob->active.num_clusters == 5);
7603 
7604 	/* Write at the beginning of first cluster */
7605 	offset = 0;
7606 	spdk_blob_io_write(blob, channel, payload, offset, 1, blob_op_complete, NULL);
7607 	poll_threads();
7608 	CU_ASSERT(g_bserrno == 0);
7609 
7610 	io_unit = spdk_blob_get_next_allocated_io_unit(blob, 0);
7611 	CU_ASSERT(io_unit == offset);
7612 
7613 	io_unit = spdk_blob_get_next_unallocated_io_unit(blob, 0);
7614 	CU_ASSERT(io_unit == io_units_per_cluster);
7615 
7616 	/* Write in the middle of third cluster */
7617 	offset = 2 * io_units_per_cluster + io_units_per_cluster / 2;
7618 	spdk_blob_io_write(blob, channel, payload, offset, 1, blob_op_complete, NULL);
7619 	poll_threads();
7620 	CU_ASSERT(g_bserrno == 0);
7621 
7622 	io_unit = spdk_blob_get_next_allocated_io_unit(blob, io_units_per_cluster);
7623 	CU_ASSERT(io_unit == 2 * io_units_per_cluster);
7624 
7625 	io_unit = spdk_blob_get_next_unallocated_io_unit(blob, 2 * io_units_per_cluster);
7626 	CU_ASSERT(io_unit == 3 * io_units_per_cluster);
7627 
7628 	/* Write at the end of last cluster */
7629 	offset = 5 * io_units_per_cluster - 1;
7630 	spdk_blob_io_write(blob, channel, payload, offset, 1, blob_op_complete, NULL);
7631 	poll_threads();
7632 	CU_ASSERT(g_bserrno == 0);
7633 
7634 	io_unit = spdk_blob_get_next_allocated_io_unit(blob, 3 * io_units_per_cluster);
7635 	CU_ASSERT(io_unit == 4 * io_units_per_cluster);
7636 
7637 	io_unit = spdk_blob_get_next_unallocated_io_unit(blob, 4 * io_units_per_cluster);
7638 	CU_ASSERT(io_unit == UINT64_MAX);
7639 
7640 	spdk_bs_free_io_channel(channel);
7641 	poll_threads();
7642 
7643 	ut_blob_close_and_delete(bs, blob);
7644 }
7645 
7646 static void
7647 blob_esnap_create(void)
7648 {
7649 	struct spdk_blob_store	*bs = g_bs;
7650 	struct spdk_bs_opts	bs_opts;
7651 	struct ut_esnap_opts	esnap_opts;
7652 	struct spdk_blob_opts	opts;
7653 	struct spdk_blob_open_opts open_opts;
7654 	struct spdk_blob	*blob;
7655 	uint32_t		cluster_sz, block_sz;
7656 	const uint32_t		esnap_num_clusters = 4;
7657 	uint64_t		esnap_num_blocks;
7658 	uint32_t		sz;
7659 	spdk_blob_id		blobid;
7660 	uint32_t		bs_ctx_count, blob_ctx_count;
7661 
7662 	cluster_sz = spdk_bs_get_cluster_size(bs);
7663 	block_sz = spdk_bs_get_io_unit_size(bs);
7664 	esnap_num_blocks = cluster_sz * esnap_num_clusters / block_sz;
7665 
7666 	/* Create a normal blob and verify it is not an esnap clone. */
7667 	ut_spdk_blob_opts_init(&opts);
7668 	blob = ut_blob_create_and_open(bs, &opts);
7669 	CU_ASSERT(!spdk_blob_is_esnap_clone(blob));
7670 	ut_blob_close_and_delete(bs, blob);
7671 
7672 	/* Create an esnap clone blob then verify it is an esnap clone and has the right size */
7673 	ut_spdk_blob_opts_init(&opts);
7674 	ut_esnap_opts_init(block_sz, esnap_num_blocks, __func__, NULL, &esnap_opts);
7675 	opts.esnap_id = &esnap_opts;
7676 	opts.esnap_id_len = sizeof(esnap_opts);
7677 	opts.num_clusters = esnap_num_clusters;
7678 	blob = ut_blob_create_and_open(bs, &opts);
7679 	SPDK_CU_ASSERT_FATAL(blob != NULL);
7680 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7681 	SPDK_CU_ASSERT_FATAL(blob_is_esnap_clone(blob));
7682 	SPDK_CU_ASSERT_FATAL(!spdk_blob_is_clone(blob));
7683 	sz = spdk_blob_get_num_clusters(blob);
7684 	CU_ASSERT(sz == esnap_num_clusters);
7685 	ut_blob_close_and_delete(bs, blob);
7686 
7687 	/* Create an esnap clone without the size and verify it can be grown */
7688 	ut_spdk_blob_opts_init(&opts);
7689 	ut_esnap_opts_init(block_sz, esnap_num_blocks, __func__, NULL, &esnap_opts);
7690 	opts.esnap_id = &esnap_opts;
7691 	opts.esnap_id_len = sizeof(esnap_opts);
7692 	blob = ut_blob_create_and_open(bs, &opts);
7693 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7694 	sz = spdk_blob_get_num_clusters(blob);
7695 	CU_ASSERT(sz == 0);
7696 	spdk_blob_resize(blob, 1, blob_op_complete, NULL);
7697 	poll_threads();
7698 	CU_ASSERT(g_bserrno == 0);
7699 	sz = spdk_blob_get_num_clusters(blob);
7700 	CU_ASSERT(sz == 1);
7701 	spdk_blob_resize(blob, esnap_num_clusters, blob_op_complete, NULL);
7702 	poll_threads();
7703 	CU_ASSERT(g_bserrno == 0);
7704 	sz = spdk_blob_get_num_clusters(blob);
7705 	CU_ASSERT(sz == esnap_num_clusters);
7706 	spdk_blob_resize(blob, esnap_num_clusters + 1, blob_op_complete, NULL);
7707 	poll_threads();
7708 	CU_ASSERT(g_bserrno == 0);
7709 	sz = spdk_blob_get_num_clusters(blob);
7710 	CU_ASSERT(sz == esnap_num_clusters + 1);
7711 
7712 	/* Reload the blobstore and be sure that the blob can be opened. */
7713 	blobid = spdk_blob_get_id(blob);
7714 	spdk_blob_close(blob, blob_op_complete, NULL);
7715 	poll_threads();
7716 	CU_ASSERT(g_bserrno == 0);
7717 	g_blob = NULL;
7718 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7719 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
7720 	ut_bs_reload(&bs, &bs_opts);
7721 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7722 	poll_threads();
7723 	CU_ASSERT(g_bserrno == 0);
7724 	CU_ASSERT(g_blob != NULL);
7725 	blob = g_blob;
7726 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7727 	sz = spdk_blob_get_num_clusters(blob);
7728 	CU_ASSERT(sz == esnap_num_clusters + 1);
7729 
7730 	/* Reload the blobstore without esnap_bs_dev_create: should fail to open blob. */
7731 	spdk_blob_close(blob, blob_op_complete, NULL);
7732 	poll_threads();
7733 	CU_ASSERT(g_bserrno == 0);
7734 	g_blob = NULL;
7735 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7736 	ut_bs_reload(&bs, &bs_opts);
7737 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7738 	poll_threads();
7739 	CU_ASSERT(g_bserrno != 0);
7740 	CU_ASSERT(g_blob == NULL);
7741 
7742 	/* Reload the blobstore with ctx set and verify it is passed to the esnap create callback */
7743 	bs_ctx_count = 0;
7744 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7745 	bs_opts.esnap_bs_dev_create = ut_esnap_create_with_count;
7746 	bs_opts.esnap_ctx = &bs_ctx_count;
7747 	ut_bs_reload(&bs, &bs_opts);
7748 	/* Loading the blobstore triggers the esnap to be loaded */
7749 	CU_ASSERT(bs_ctx_count == 1);
7750 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
7751 	poll_threads();
7752 	CU_ASSERT(g_bserrno == 0);
7753 	CU_ASSERT(g_blob != NULL);
7754 	/* Opening the blob also triggers the esnap to be loaded */
7755 	CU_ASSERT(bs_ctx_count == 2);
7756 	blob = g_blob;
7757 	SPDK_CU_ASSERT_FATAL(spdk_blob_is_esnap_clone(blob));
7758 	sz = spdk_blob_get_num_clusters(blob);
7759 	CU_ASSERT(sz == esnap_num_clusters + 1);
7760 	spdk_blob_close(blob, blob_op_complete, NULL);
7761 	poll_threads();
7762 	CU_ASSERT(g_bserrno == 0);
7763 	g_blob = NULL;
7764 	/* If open_opts.esnap_ctx is set it is passed to the esnap create callback */
7765 	blob_ctx_count = 0;
7766 	spdk_blob_open_opts_init(&open_opts, sizeof(open_opts));
7767 	open_opts.esnap_ctx = &blob_ctx_count;
7768 	spdk_bs_open_blob_ext(bs, blobid, &open_opts, blob_op_with_handle_complete, NULL);
7769 	poll_threads();
7770 	blob = g_blob;
7771 	CU_ASSERT(bs_ctx_count == 3);
7772 	CU_ASSERT(blob_ctx_count == 1);
7773 	spdk_blob_close(blob, blob_op_complete, NULL);
7774 	poll_threads();
7775 	CU_ASSERT(g_bserrno == 0);
7776 	g_blob = NULL;
7777 }
7778 
7779 static void
7780 blob_esnap_clone_reload(void)
7781 {
7782 	struct spdk_blob_store	*bs = g_bs;
7783 	struct spdk_bs_opts	bs_opts;
7784 	struct ut_esnap_opts	esnap_opts;
7785 	struct spdk_blob_opts	opts;
7786 	struct spdk_blob	*eclone1, *snap1, *clone1;
7787 	uint32_t		cluster_sz = spdk_bs_get_cluster_size(bs);
7788 	uint32_t		block_sz = spdk_bs_get_io_unit_size(bs);
7789 	const uint32_t		esnap_num_clusters = 4;
7790 	uint64_t		esnap_num_blocks = cluster_sz * esnap_num_clusters / block_sz;
7791 	spdk_blob_id		eclone1_id, snap1_id, clone1_id;
7792 	struct spdk_io_channel	*bs_ch;
7793 	char			buf[block_sz];
7794 	int			bserr1, bserr2, bserr3, bserr4;
7795 	struct spdk_bs_dev	*dev;
7796 
7797 	/* Create and open an esnap clone blob */
7798 	ut_spdk_blob_opts_init(&opts);
7799 	ut_esnap_opts_init(block_sz, esnap_num_blocks, __func__, NULL, &esnap_opts);
7800 	opts.esnap_id = &esnap_opts;
7801 	opts.esnap_id_len = sizeof(esnap_opts);
7802 	opts.num_clusters = esnap_num_clusters;
7803 	eclone1 = ut_blob_create_and_open(bs, &opts);
7804 	CU_ASSERT(eclone1 != NULL);
7805 	CU_ASSERT(spdk_blob_is_esnap_clone(eclone1));
7806 	eclone1_id = eclone1->id;
7807 
7808 	/* Create and open a snapshot of eclone1 */
7809 	spdk_bs_create_snapshot(bs, eclone1_id, NULL, blob_op_with_id_complete, NULL);
7810 	poll_threads();
7811 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7812 	CU_ASSERT(g_bserrno == 0);
7813 	snap1_id = g_blobid;
7814 	spdk_bs_open_blob(bs, snap1_id, blob_op_with_handle_complete, NULL);
7815 	poll_threads();
7816 	CU_ASSERT(g_bserrno == 0);
7817 	CU_ASSERT(g_blob != NULL);
7818 	snap1 = g_blob;
7819 
7820 	/* Create and open regular clone of snap1 */
7821 	spdk_bs_create_clone(bs, snap1_id, NULL, blob_op_with_id_complete, NULL);
7822 	poll_threads();
7823 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
7824 	SPDK_CU_ASSERT_FATAL(g_bserrno == 0);
7825 	clone1_id = g_blobid;
7826 	spdk_bs_open_blob(bs, clone1_id, blob_op_with_handle_complete, NULL);
7827 	poll_threads();
7828 	CU_ASSERT(g_bserrno == 0);
7829 	CU_ASSERT(g_blob != NULL);
7830 	clone1 = g_blob;
7831 
7832 	/* Close the blobs in preparation for reloading the blobstore */
7833 	spdk_blob_close(clone1, blob_op_complete, NULL);
7834 	poll_threads();
7835 	CU_ASSERT(g_bserrno == 0);
7836 	spdk_blob_close(snap1, blob_op_complete, NULL);
7837 	poll_threads();
7838 	CU_ASSERT(g_bserrno == 0);
7839 	spdk_blob_close(eclone1, blob_op_complete, NULL);
7840 	poll_threads();
7841 	CU_ASSERT(g_bserrno == 0);
7842 	g_blob = NULL;
7843 
7844 	/* Reload the blobstore */
7845 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7846 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
7847 	ut_bs_reload(&bs, &bs_opts);
7848 
7849 	/* Be sure each of the blobs can be opened */
7850 	spdk_bs_open_blob(bs, eclone1_id, blob_op_with_handle_complete, NULL);
7851 	poll_threads();
7852 	CU_ASSERT(g_bserrno == 0);
7853 	CU_ASSERT(g_blob != NULL);
7854 	eclone1 = g_blob;
7855 	spdk_bs_open_blob(bs, snap1_id, blob_op_with_handle_complete, NULL);
7856 	poll_threads();
7857 	CU_ASSERT(g_bserrno == 0);
7858 	CU_ASSERT(g_blob != NULL);
7859 	snap1 = g_blob;
7860 	spdk_bs_open_blob(bs, clone1_id, blob_op_with_handle_complete, NULL);
7861 	poll_threads();
7862 	CU_ASSERT(g_bserrno == 0);
7863 	CU_ASSERT(g_blob != NULL);
7864 	clone1 = g_blob;
7865 
7866 	/* Perform some reads on each of them to cause channels to be allocated */
7867 	bs_ch = spdk_bs_alloc_io_channel(bs);
7868 	CU_ASSERT(bs_ch != NULL);
7869 	spdk_blob_io_read(eclone1, bs_ch, buf, 0, 1, bs_op_complete, NULL);
7870 	poll_threads();
7871 	CU_ASSERT(g_bserrno == 0);
7872 	spdk_blob_io_read(snap1, bs_ch, buf, 0, 1, bs_op_complete, NULL);
7873 	poll_threads();
7874 	CU_ASSERT(g_bserrno == 0);
7875 	spdk_blob_io_read(clone1, bs_ch, buf, 0, 1, bs_op_complete, NULL);
7876 	poll_threads();
7877 	CU_ASSERT(g_bserrno == 0);
7878 
7879 	/*
7880 	 * Unload the blobstore in a way similar to how lvstore unloads it.  This should exercise
7881 	 * the deferred unload path in spdk_bs_unload().
7882 	 */
7883 	bserr1 = 0xbad;
7884 	bserr2 = 0xbad;
7885 	bserr3 = 0xbad;
7886 	bserr4 = 0xbad;
7887 	spdk_blob_close(eclone1, blob_op_complete, &bserr1);
7888 	spdk_blob_close(snap1, blob_op_complete, &bserr2);
7889 	spdk_blob_close(clone1, blob_op_complete, &bserr3);
7890 	spdk_bs_unload(bs, blob_op_complete, &bserr4);
7891 	spdk_bs_free_io_channel(bs_ch);
7892 	poll_threads();
7893 	CU_ASSERT(bserr1 == 0);
7894 	CU_ASSERT(bserr2 == 0);
7895 	CU_ASSERT(bserr3 == 0);
7896 	CU_ASSERT(bserr4 == 0);
7897 	g_blob = NULL;
7898 
7899 	/* Reload the blobstore */
7900 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
7901 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
7902 	dev = init_dev();
7903 	spdk_bs_load(dev, &bs_opts, bs_op_with_handle_complete, NULL);
7904 	poll_threads();
7905 	CU_ASSERT(g_bserrno == 0);
7906 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7907 }
7908 
7909 static bool
7910 blob_esnap_verify_contents(struct spdk_blob *blob, struct spdk_io_channel *ch,
7911 			   uint64_t offset, uint64_t size, uint32_t readsize, const char *how)
7912 {
7913 	const uint32_t	bs_blksz = blob->bs->io_unit_size;
7914 	const uint32_t	esnap_blksz = blob->back_bs_dev ? blob->back_bs_dev->blocklen : bs_blksz;
7915 	const uint32_t	start_blk = offset / bs_blksz;
7916 	const uint32_t	num_blocks = spdk_max(size, readsize) / bs_blksz;
7917 	const uint32_t	blocks_per_read = spdk_min(size, readsize) / bs_blksz;
7918 	uint32_t	blob_block;
7919 	struct iovec	iov;
7920 	uint8_t		buf[spdk_min(size, readsize)];
7921 	bool		block_ok;
7922 
7923 	SPDK_CU_ASSERT_FATAL(offset % bs_blksz == 0);
7924 	SPDK_CU_ASSERT_FATAL(size % bs_blksz == 0);
7925 	SPDK_CU_ASSERT_FATAL(readsize % bs_blksz == 0);
7926 
7927 	memset(buf, 0, readsize);
7928 	iov.iov_base = buf;
7929 	iov.iov_len = readsize;
7930 	for (blob_block = start_blk; blob_block < num_blocks; blob_block += blocks_per_read) {
7931 		if (strcmp(how, "read") == 0) {
7932 			spdk_blob_io_read(blob, ch, buf, blob_block, blocks_per_read,
7933 					  bs_op_complete, NULL);
7934 		} else if (strcmp(how, "readv") == 0) {
7935 			spdk_blob_io_readv(blob, ch, &iov, 1, blob_block, blocks_per_read,
7936 					   bs_op_complete, NULL);
7937 		} else if (strcmp(how, "readv_ext") == 0) {
7938 			/*
7939 			 * This is currently pointless. NULL ext_opts leads to dev->readv(), not
7940 			 * dev->readv_ext().
7941 			 */
7942 			spdk_blob_io_readv_ext(blob, ch, &iov, 1, blob_block, blocks_per_read,
7943 					       bs_op_complete, NULL, NULL);
7944 		} else {
7945 			abort();
7946 		}
7947 		poll_threads();
7948 		CU_ASSERT(g_bserrno == 0);
7949 		if (g_bserrno != 0) {
7950 			return false;
7951 		}
7952 		block_ok = ut_esnap_content_is_correct(buf, blocks_per_read * bs_blksz, blob->id,
7953 						       blob_block * bs_blksz, esnap_blksz);
7954 		CU_ASSERT(block_ok);
7955 		if (!block_ok) {
7956 			return false;
7957 		}
7958 	}
7959 
7960 	return true;
7961 }
7962 
7963 static void
7964 blob_esnap_io_size(uint32_t bs_blksz, uint32_t esnap_blksz)
7965 {
7966 	struct spdk_bs_dev	*dev;
7967 	struct spdk_blob_store	*bs;
7968 	struct spdk_bs_opts	bsopts;
7969 	struct spdk_blob_opts	opts;
7970 	struct ut_esnap_opts	esnap_opts;
7971 	struct spdk_blob	*blob;
7972 	const uint32_t		cluster_sz = 16 * 1024;
7973 	const uint64_t		esnap_num_clusters = 4;
7974 	const uint32_t		esnap_sz = cluster_sz * esnap_num_clusters;
7975 	const uint64_t		esnap_num_blocks = esnap_sz / esnap_blksz;
7976 	const uint64_t		blob_num_blocks = esnap_sz / bs_blksz;
7977 	uint32_t		block;
7978 	struct spdk_io_channel	*bs_ch;
7979 
7980 	spdk_bs_opts_init(&bsopts, sizeof(bsopts));
7981 	bsopts.cluster_sz = cluster_sz;
7982 	bsopts.esnap_bs_dev_create = ut_esnap_create;
7983 
7984 	/* Create device with desired block size */
7985 	dev = init_dev();
7986 	dev->blocklen = bs_blksz;
7987 	dev->blockcnt = DEV_BUFFER_SIZE / dev->blocklen;
7988 
7989 	/* Initialize a new blob store */
7990 	spdk_bs_init(dev, &bsopts, bs_op_with_handle_complete, NULL);
7991 	poll_threads();
7992 	CU_ASSERT(g_bserrno == 0);
7993 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
7994 	SPDK_CU_ASSERT_FATAL(g_bs->io_unit_size == bs_blksz);
7995 	bs = g_bs;
7996 
7997 	bs_ch = spdk_bs_alloc_io_channel(bs);
7998 	SPDK_CU_ASSERT_FATAL(bs_ch != NULL);
7999 
8000 	/* Create and open the esnap clone  */
8001 	ut_spdk_blob_opts_init(&opts);
8002 	ut_esnap_opts_init(esnap_blksz, esnap_num_blocks, __func__, NULL, &esnap_opts);
8003 	opts.esnap_id = &esnap_opts;
8004 	opts.esnap_id_len = sizeof(esnap_opts);
8005 	opts.num_clusters = esnap_num_clusters;
8006 	blob = ut_blob_create_and_open(bs, &opts);
8007 	SPDK_CU_ASSERT_FATAL(blob != NULL);
8008 
8009 	/* Verify that large reads return the content of the esnap device */
8010 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, esnap_sz, "read"));
8011 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, esnap_sz, "readv"));
8012 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, esnap_sz, "readv_ext"));
8013 	/* Verify that small reads return the content of the esnap device */
8014 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, bs_blksz, "read"));
8015 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, bs_blksz, "readv"));
8016 	CU_ASSERT(blob_esnap_verify_contents(blob, bs_ch, 0, esnap_sz, bs_blksz, "readv_ext"));
8017 
8018 	/* Write one blob block at a time; verify that the surrounding blocks are OK */
8019 	for (block = 0; block < blob_num_blocks; block++) {
8020 		char		buf[bs_blksz];
8021 		union ut_word	word;
8022 
8023 		word.f.blob_id = 0xfedcba90;
8024 		word.f.lba = block;
8025 		ut_memset8(buf, word.num, bs_blksz);
8026 
8027 		spdk_blob_io_write(blob, bs_ch, buf, block, 1, bs_op_complete, NULL);
8028 		poll_threads();
8029 		CU_ASSERT(g_bserrno == 0);
8030 		if (g_bserrno != 0) {
8031 			break;
8032 		}
8033 
8034 		/* Read and verify the block before the current block */
8035 		if (block != 0) {
8036 			spdk_blob_io_read(blob, bs_ch, buf, block - 1, 1, bs_op_complete, NULL);
8037 			poll_threads();
8038 			CU_ASSERT(g_bserrno == 0);
8039 			if (g_bserrno != 0) {
8040 				break;
8041 			}
8042 			CU_ASSERT(ut_esnap_content_is_correct(buf, bs_blksz, word.f.blob_id,
8043 							      (block - 1) * bs_blksz, bs_blksz));
8044 		}
8045 
8046 		/* Read and verify the current block */
8047 		spdk_blob_io_read(blob, bs_ch, buf, block, 1, bs_op_complete, NULL);
8048 		poll_threads();
8049 		CU_ASSERT(g_bserrno == 0);
8050 		if (g_bserrno != 0) {
8051 			break;
8052 		}
8053 		CU_ASSERT(ut_esnap_content_is_correct(buf, bs_blksz, word.f.blob_id,
8054 						      block * bs_blksz, bs_blksz));
8055 
8056 		/* Check the block that follows */
8057 		if (block + 1 < blob_num_blocks) {
8058 			g_bserrno = 0xbad;
8059 			spdk_blob_io_read(blob, bs_ch, buf, block + 1, 1, bs_op_complete, NULL);
8060 			poll_threads();
8061 			CU_ASSERT(g_bserrno == 0);
8062 			if (g_bserrno != 0) {
8063 				break;
8064 			}
8065 			CU_ASSERT(ut_esnap_content_is_correct(buf, bs_blksz, blob->id,
8066 							      (block + 1) * bs_blksz,
8067 							      esnap_blksz));
8068 		}
8069 	}
8070 
8071 	/* Clean up */
8072 	spdk_bs_free_io_channel(bs_ch);
8073 	g_bserrno = 0xbad;
8074 	spdk_blob_close(blob, blob_op_complete, NULL);
8075 	poll_threads();
8076 	CU_ASSERT(g_bserrno == 0);
8077 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
8078 	poll_threads();
8079 	CU_ASSERT(g_bserrno == 0);
8080 	g_bs = NULL;
8081 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8082 }
8083 
8084 static void
8085 blob_esnap_io_4096_4096(void)
8086 {
8087 	blob_esnap_io_size(4096, 4096);
8088 }
8089 
8090 static void
8091 blob_esnap_io_512_512(void)
8092 {
8093 	blob_esnap_io_size(512, 512);
8094 }
8095 
8096 static void
8097 blob_esnap_io_4096_512(void)
8098 {
8099 	blob_esnap_io_size(4096, 512);
8100 }
8101 
8102 static void
8103 blob_esnap_io_512_4096(void)
8104 {
8105 	struct spdk_bs_dev	*dev;
8106 	struct spdk_blob_store	*bs;
8107 	struct spdk_bs_opts	bs_opts;
8108 	struct spdk_blob_opts	blob_opts;
8109 	struct ut_esnap_opts	esnap_opts;
8110 	uint64_t		cluster_sz = 16 * 1024;
8111 	uint32_t		bs_blksz = 512;
8112 	uint32_t		esnap_blksz = 4096;
8113 	uint64_t		esnap_num_blocks = 64;
8114 	spdk_blob_id		blobid;
8115 
8116 	/* Create device with desired block size */
8117 	dev = init_dev();
8118 	dev->blocklen = bs_blksz;
8119 	dev->blockcnt = DEV_BUFFER_SIZE / dev->blocklen;
8120 
8121 	/* Initialize a new blob store */
8122 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
8123 	bs_opts.cluster_sz = cluster_sz;
8124 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
8125 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
8126 	poll_threads();
8127 	CU_ASSERT(g_bserrno == 0);
8128 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8129 	SPDK_CU_ASSERT_FATAL(g_bs->io_unit_size == bs_blksz);
8130 	bs = g_bs;
8131 
8132 	/* Try to create and open the esnap clone. Create should succeed, open should fail. */
8133 	ut_spdk_blob_opts_init(&blob_opts);
8134 	ut_esnap_opts_init(esnap_blksz, esnap_num_blocks, __func__, NULL, &esnap_opts);
8135 	blob_opts.esnap_id = &esnap_opts;
8136 	blob_opts.esnap_id_len = sizeof(esnap_opts);
8137 	blob_opts.num_clusters = esnap_num_blocks * esnap_blksz / bs_blksz;
8138 	spdk_bs_create_blob_ext(bs, &blob_opts, blob_op_with_id_complete, NULL);
8139 	poll_threads();
8140 	CU_ASSERT(g_bserrno == 0);
8141 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8142 	blobid = g_blobid;
8143 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8144 	poll_threads();
8145 	CU_ASSERT(g_bserrno == -EINVAL);
8146 	CU_ASSERT(g_blob == NULL);
8147 
8148 	/* Clean up */
8149 	spdk_bs_unload(bs, bs_op_complete, NULL);
8150 	poll_threads();
8151 	CU_ASSERT(g_bserrno == 0);
8152 	g_bs = NULL;
8153 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8154 }
8155 
8156 static void
8157 blob_esnap_thread_add_remove(void)
8158 {
8159 	struct spdk_blob_store	*bs = g_bs;
8160 	struct spdk_blob_opts	opts;
8161 	struct ut_esnap_opts	ut_esnap_opts;
8162 	struct spdk_blob	*blob;
8163 	struct ut_esnap_dev	*ut_dev;
8164 	spdk_blob_id		blobid;
8165 	uint64_t		start_thread = g_ut_thread_id;
8166 	bool			destroyed = false;
8167 	struct spdk_io_channel	*ch0, *ch1;
8168 	struct ut_esnap_channel	*ut_ch0, *ut_ch1;
8169 	const uint32_t		blocklen = bs->io_unit_size;
8170 	char			buf[blocklen * 4];
8171 
8172 	SPDK_CU_ASSERT_FATAL(g_ut_num_threads > 1);
8173 	set_thread(0);
8174 
8175 	/* Create the esnap clone */
8176 	ut_esnap_opts_init(blocklen, 2048, "add_remove_1", &destroyed, &ut_esnap_opts);
8177 	ut_spdk_blob_opts_init(&opts);
8178 	opts.esnap_id = &ut_esnap_opts;
8179 	opts.esnap_id_len = sizeof(ut_esnap_opts);
8180 	opts.num_clusters = 10;
8181 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
8182 	poll_threads();
8183 	CU_ASSERT(g_bserrno == 0);
8184 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8185 	blobid = g_blobid;
8186 
8187 	/* Open the blob. No channels should be allocated yet. */
8188 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8189 	poll_threads();
8190 	CU_ASSERT(g_bserrno == 0);
8191 	CU_ASSERT(g_blob != NULL);
8192 	blob = g_blob;
8193 	ut_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8194 	CU_ASSERT(ut_dev != NULL);
8195 	CU_ASSERT(ut_dev->num_channels == 0);
8196 
8197 	/* Create a channel on thread 0. It is lazily created on the first read. */
8198 	ch0 = spdk_bs_alloc_io_channel(bs);
8199 	CU_ASSERT(ch0 != NULL);
8200 	ut_ch0 = ut_esnap_get_io_channel(ch0, blobid);
8201 	CU_ASSERT(ut_ch0 == NULL);
8202 	CU_ASSERT(ut_dev->num_channels == 0);
8203 	spdk_blob_io_read(blob, ch0, buf, 0, 1, bs_op_complete, NULL);
8204 	poll_threads();
8205 	CU_ASSERT(g_bserrno == 0);
8206 	CU_ASSERT(ut_dev->num_channels == 1);
8207 	ut_ch0 = ut_esnap_get_io_channel(ch0, blobid);
8208 	CU_ASSERT(ut_ch0 != NULL);
8209 	CU_ASSERT(ut_ch0->blocks_read == 1);
8210 
8211 	/* Create a channel on thread 1 and verify its lazy creation too. */
8212 	set_thread(1);
8213 	ch1 = spdk_bs_alloc_io_channel(bs);
8214 	CU_ASSERT(ch1 != NULL);
8215 	ut_ch1 = ut_esnap_get_io_channel(ch1, blobid);
8216 	CU_ASSERT(ut_ch1 == NULL);
8217 	CU_ASSERT(ut_dev->num_channels == 1);
8218 	spdk_blob_io_read(blob, ch1, buf, 0, 4, bs_op_complete, NULL);
8219 	poll_threads();
8220 	CU_ASSERT(g_bserrno == 0);
8221 	CU_ASSERT(ut_dev->num_channels == 2);
8222 	ut_ch1 = ut_esnap_get_io_channel(ch1, blobid);
8223 	CU_ASSERT(ut_ch1 != NULL);
8224 	CU_ASSERT(ut_ch1->blocks_read == 4);
8225 
8226 	/* Close the channel on thread 0 and verify the bs_dev channel is also gone. */
8227 	set_thread(0);
8228 	spdk_bs_free_io_channel(ch0);
8229 	poll_threads();
8230 	CU_ASSERT(ut_dev->num_channels == 1);
8231 
8232 	/* Close the blob. There is no outstanding IO so it should close right away. */
8233 	g_bserrno = 0xbad;
8234 	spdk_blob_close(blob, blob_op_complete, NULL);
8235 	poll_threads();
8236 	CU_ASSERT(g_bserrno == 0);
8237 	CU_ASSERT(destroyed);
8238 
8239 	/* The esnap channel for the blob should be gone now too. */
8240 	ut_ch1 = ut_esnap_get_io_channel(ch1, blobid);
8241 	CU_ASSERT(ut_ch1 == NULL);
8242 
8243 	/* Clean up */
8244 	set_thread(1);
8245 	spdk_bs_free_io_channel(ch1);
8246 	set_thread(start_thread);
8247 }
8248 
8249 static void
8250 freeze_done(void *cb_arg, int bserrno)
8251 {
8252 	uint32_t *freeze_cnt = cb_arg;
8253 
8254 	CU_ASSERT(bserrno == 0);
8255 	(*freeze_cnt)++;
8256 }
8257 
8258 static void
8259 unfreeze_done(void *cb_arg, int bserrno)
8260 {
8261 	uint32_t *unfreeze_cnt = cb_arg;
8262 
8263 	CU_ASSERT(bserrno == 0);
8264 	(*unfreeze_cnt)++;
8265 }
8266 
8267 static void
8268 blob_nested_freezes(void)
8269 {
8270 	struct spdk_blob_store *bs = g_bs;
8271 	struct spdk_blob *blob;
8272 	struct spdk_io_channel *channel[2];
8273 	struct spdk_blob_opts opts;
8274 	uint32_t freeze_cnt, unfreeze_cnt;
8275 	int i;
8276 
8277 	for (i = 0; i < 2; i++) {
8278 		set_thread(i);
8279 		channel[i] = spdk_bs_alloc_io_channel(bs);
8280 		SPDK_CU_ASSERT_FATAL(channel[i] != NULL);
8281 	}
8282 
8283 	set_thread(0);
8284 
8285 	ut_spdk_blob_opts_init(&opts);
8286 	blob = ut_blob_create_and_open(bs, &opts);
8287 
8288 	/* First just test a single freeze/unfreeze. */
8289 	freeze_cnt = 0;
8290 	unfreeze_cnt = 0;
8291 	CU_ASSERT(blob->frozen_refcnt == 0);
8292 	blob_freeze_io(blob, freeze_done, &freeze_cnt);
8293 	CU_ASSERT(blob->frozen_refcnt == 1);
8294 	CU_ASSERT(freeze_cnt == 0);
8295 	poll_threads();
8296 	CU_ASSERT(freeze_cnt == 1);
8297 	blob_unfreeze_io(blob, unfreeze_done, &unfreeze_cnt);
8298 	CU_ASSERT(blob->frozen_refcnt == 0);
8299 	CU_ASSERT(unfreeze_cnt == 0);
8300 	poll_threads();
8301 	CU_ASSERT(unfreeze_cnt == 1);
8302 
8303 	/* Now nest multiple freeze/unfreeze operations.  We should
8304 	 * expect a callback for each operation, but only after
8305 	 * the threads have been polled to ensure a for_each_channel()
8306 	 * was executed.
8307 	 */
8308 	freeze_cnt = 0;
8309 	unfreeze_cnt = 0;
8310 	CU_ASSERT(blob->frozen_refcnt == 0);
8311 	blob_freeze_io(blob, freeze_done, &freeze_cnt);
8312 	CU_ASSERT(blob->frozen_refcnt == 1);
8313 	CU_ASSERT(freeze_cnt == 0);
8314 	blob_freeze_io(blob, freeze_done, &freeze_cnt);
8315 	CU_ASSERT(blob->frozen_refcnt == 2);
8316 	CU_ASSERT(freeze_cnt == 0);
8317 	poll_threads();
8318 	CU_ASSERT(freeze_cnt == 2);
8319 	blob_unfreeze_io(blob, unfreeze_done, &unfreeze_cnt);
8320 	CU_ASSERT(blob->frozen_refcnt == 1);
8321 	CU_ASSERT(unfreeze_cnt == 0);
8322 	blob_unfreeze_io(blob, unfreeze_done, &unfreeze_cnt);
8323 	CU_ASSERT(blob->frozen_refcnt == 0);
8324 	CU_ASSERT(unfreeze_cnt == 0);
8325 	poll_threads();
8326 	CU_ASSERT(unfreeze_cnt == 2);
8327 
8328 	for (i = 0; i < 2; i++) {
8329 		set_thread(i);
8330 		spdk_bs_free_io_channel(channel[i]);
8331 	}
8332 	set_thread(0);
8333 	ut_blob_close_and_delete(bs, blob);
8334 
8335 	poll_threads();
8336 	g_blob = NULL;
8337 	g_blobid = 0;
8338 }
8339 
8340 static void
8341 blob_ext_md_pages(void)
8342 {
8343 	struct spdk_blob_store *bs;
8344 	struct spdk_bs_dev *dev;
8345 	struct spdk_blob *blob;
8346 	struct spdk_blob_opts opts;
8347 	struct spdk_bs_opts bs_opts;
8348 	uint64_t free_clusters;
8349 
8350 	dev = init_dev();
8351 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
8352 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
8353 	/* Issue #2932 was a bug in how we use bs_allocate_cluster() during resize.
8354 	 * It requires num_md_pages that is much smaller than the number of clusters.
8355 	 * Make sure we can create a blob that uses all of the free clusters.
8356 	 */
8357 	bs_opts.cluster_sz = 65536;
8358 	bs_opts.num_md_pages = 16;
8359 
8360 	/* Initialize a new blob store */
8361 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
8362 	poll_threads();
8363 	CU_ASSERT(g_bserrno == 0);
8364 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8365 	bs = g_bs;
8366 
8367 	free_clusters = spdk_bs_free_cluster_count(bs);
8368 
8369 	ut_spdk_blob_opts_init(&opts);
8370 	opts.num_clusters = free_clusters;
8371 
8372 	blob = ut_blob_create_and_open(bs, &opts);
8373 	spdk_blob_close(blob, blob_op_complete, NULL);
8374 	CU_ASSERT(g_bserrno == 0);
8375 
8376 	spdk_bs_unload(bs, bs_op_complete, NULL);
8377 	poll_threads();
8378 	CU_ASSERT(g_bserrno == 0);
8379 	g_bs = NULL;
8380 }
8381 
8382 static void
8383 blob_esnap_clone_snapshot(void)
8384 {
8385 	/*
8386 	 * When a snapshot is created, the blob that is being snapped becomes
8387 	 * the leaf node (a clone of the snapshot) and the newly created
8388 	 * snapshot sits between the snapped blob and the external snapshot.
8389 	 *
8390 	 * Before creating snap1
8391 	 *
8392 	 *   ,--------.     ,----------.
8393 	 *   |  blob  |     |  vbdev   |
8394 	 *   | blob1  |<----| nvme1n42 |
8395 	 *   |  (rw)  |     |   (ro)   |
8396 	 *   `--------'     `----------'
8397 	 *       Figure 1
8398 	 *
8399 	 * After creating snap1
8400 	 *
8401 	 *   ,--------.     ,--------.     ,----------.
8402 	 *   |  blob  |     |  blob  |     |  vbdev   |
8403 	 *   | blob1  |<----| snap1  |<----| nvme1n42 |
8404 	 *   |  (rw)  |     |  (ro)  |     |   (ro)   |
8405 	 *   `--------'     `--------'     `----------'
8406 	 *       Figure 2
8407 	 *
8408 	 * Starting from Figure 2, if snap1 is removed, the chain reverts to
8409 	 * what it looks like in Figure 1.
8410 	 *
8411 	 * Starting from Figure 2, if blob1 is removed, the chain becomes:
8412 	 *
8413 	 *   ,--------.     ,----------.
8414 	 *   |  blob  |     |  vbdev   |
8415 	 *   | snap1  |<----| nvme1n42 |
8416 	 *   |  (ro)  |     |   (ro)   |
8417 	 *   `--------'     `----------'
8418 	 *       Figure 3
8419 	 *
8420 	 * In each case, the blob pointed to by the nvme vbdev is considered
8421 	 * the "esnap clone".  The esnap clone must have:
8422 	 *
8423 	 *   - XATTR_INTERNAL for BLOB_EXTERNAL_SNAPSHOT_ID (e.g. name or UUID)
8424 	 *   - blob->invalid_flags must contain SPDK_BLOB_EXTERNAL_SNAPSHOT
8425 	 *   - blob->parent_id must be SPDK_BLOBID_EXTERNAL_SNAPSHOT.
8426 	 *
8427 	 * No other blob that descends from the esnap clone may have any of
8428 	 * those set.
8429 	 */
8430 	struct spdk_blob_store	*bs = g_bs;
8431 	const uint32_t		blocklen = bs->io_unit_size;
8432 	struct spdk_blob_opts	opts;
8433 	struct ut_esnap_opts	esnap_opts;
8434 	struct spdk_blob	*blob, *snap_blob;
8435 	spdk_blob_id		blobid, snap_blobid;
8436 	bool			destroyed = false;
8437 
8438 	/* Create the esnap clone */
8439 	ut_esnap_opts_init(blocklen, 2048, __func__, &destroyed, &esnap_opts);
8440 	ut_spdk_blob_opts_init(&opts);
8441 	opts.esnap_id = &esnap_opts;
8442 	opts.esnap_id_len = sizeof(esnap_opts);
8443 	opts.num_clusters = 10;
8444 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
8445 	poll_threads();
8446 	CU_ASSERT(g_bserrno == 0);
8447 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8448 	blobid = g_blobid;
8449 
8450 	/* Open the blob. */
8451 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8452 	poll_threads();
8453 	CU_ASSERT(g_bserrno == 0);
8454 	CU_ASSERT(g_blob != NULL);
8455 	blob = g_blob;
8456 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8457 
8458 	/*
8459 	 * Create a snapshot of the blob. The snapshot becomes the esnap clone.
8460 	 */
8461 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
8462 	poll_threads();
8463 	CU_ASSERT(g_bserrno == 0);
8464 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8465 	snap_blobid = g_blobid;
8466 
8467 	spdk_bs_open_blob(bs, snap_blobid, blob_op_with_handle_complete, NULL);
8468 	poll_threads();
8469 	CU_ASSERT(g_bserrno == 0);
8470 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8471 	snap_blob = g_blob;
8472 
8473 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8474 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8475 
8476 	/*
8477 	 * Delete the snapshot.  The original blob becomes the esnap clone.
8478 	 */
8479 	ut_blob_close_and_delete(bs, snap_blob);
8480 	snap_blob = NULL;
8481 	snap_blobid = SPDK_BLOBID_INVALID;
8482 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8483 
8484 	/*
8485 	 * Create the snapshot again, then delete the original blob.  The
8486 	 * snapshot should survive as the esnap clone.
8487 	 */
8488 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
8489 	poll_threads();
8490 	CU_ASSERT(g_bserrno == 0);
8491 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8492 	snap_blobid = g_blobid;
8493 
8494 	spdk_bs_open_blob(bs, snap_blobid, blob_op_with_handle_complete, NULL);
8495 	poll_threads();
8496 	CU_ASSERT(g_bserrno == 0);
8497 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8498 	snap_blob = g_blob;
8499 
8500 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8501 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8502 
8503 	ut_blob_close_and_delete(bs, blob);
8504 	blob = NULL;
8505 	blobid = SPDK_BLOBID_INVALID;
8506 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8507 
8508 	/*
8509 	 * Clone the snapshot.  The snapshot continues to be the esnap clone.
8510 	 */
8511 	spdk_bs_create_clone(bs, snap_blobid, NULL, blob_op_with_id_complete, NULL);
8512 	poll_threads();
8513 	CU_ASSERT(g_bserrno == 0);
8514 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8515 	blobid = g_blobid;
8516 
8517 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8518 	poll_threads();
8519 	CU_ASSERT(g_bserrno == 0);
8520 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8521 	blob = g_blob;
8522 
8523 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8524 	UT_ASSERT_IS_ESNAP_CLONE(snap_blob, &esnap_opts, sizeof(esnap_opts));
8525 
8526 	/*
8527 	 * Delete the snapshot. The clone becomes the esnap clone.
8528 	 */
8529 	ut_blob_close_and_delete(bs, snap_blob);
8530 	snap_blob = NULL;
8531 	snap_blobid = SPDK_BLOBID_INVALID;
8532 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8533 
8534 	/*
8535 	 * Clean up
8536 	 */
8537 	ut_blob_close_and_delete(bs, blob);
8538 }
8539 
8540 static uint64_t
8541 _blob_esnap_clone_hydrate(bool inflate)
8542 {
8543 	struct spdk_blob_store	*bs = g_bs;
8544 	struct spdk_blob_opts	opts;
8545 	struct ut_esnap_opts	esnap_opts;
8546 	struct spdk_blob	*blob;
8547 	spdk_blob_id		blobid;
8548 	struct spdk_io_channel *channel;
8549 	bool			destroyed = false;
8550 	const uint32_t		blocklen = spdk_bs_get_io_unit_size(bs);
8551 	const uint32_t		cluster_sz = spdk_bs_get_cluster_size(bs);
8552 	const uint64_t		esnap_num_clusters = 4;
8553 	const uint32_t		esnap_sz = cluster_sz * esnap_num_clusters;
8554 	const uint64_t		esnap_num_blocks = esnap_sz / blocklen;
8555 	uint64_t		num_failures = CU_get_number_of_failures();
8556 
8557 	channel = spdk_bs_alloc_io_channel(bs);
8558 	SPDK_CU_ASSERT_FATAL(channel != NULL);
8559 
8560 	/* Create the esnap clone */
8561 	ut_spdk_blob_opts_init(&opts);
8562 	ut_esnap_opts_init(blocklen, esnap_num_blocks, __func__, &destroyed, &esnap_opts);
8563 	opts.esnap_id = &esnap_opts;
8564 	opts.esnap_id_len = sizeof(esnap_opts);
8565 	opts.num_clusters = esnap_num_clusters;
8566 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
8567 	poll_threads();
8568 	CU_ASSERT(g_bserrno == 0);
8569 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8570 	blobid = g_blobid;
8571 
8572 	/* Open the esnap clone */
8573 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8574 	poll_threads();
8575 	CU_ASSERT(g_bserrno == 0);
8576 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
8577 	blob = g_blob;
8578 	UT_ASSERT_IS_ESNAP_CLONE(blob, &esnap_opts, sizeof(esnap_opts));
8579 
8580 	/*
8581 	 * Inflate or decouple  the blob then verify that it is no longer an esnap clone and has
8582 	 * right content
8583 	 */
8584 	if (inflate) {
8585 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
8586 	} else {
8587 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
8588 	}
8589 	poll_threads();
8590 	CU_ASSERT(g_bserrno == 0);
8591 	UT_ASSERT_IS_NOT_ESNAP_CLONE(blob);
8592 	CU_ASSERT(blob_esnap_verify_contents(blob, channel, 0, esnap_sz, esnap_sz, "read"));
8593 	ut_blob_close_and_delete(bs, blob);
8594 
8595 	/*
8596 	 * Clean up
8597 	 */
8598 	spdk_bs_free_io_channel(channel);
8599 	poll_threads();
8600 
8601 	/* Return number of new failures */
8602 	return CU_get_number_of_failures() - num_failures;
8603 }
8604 
8605 static void
8606 blob_esnap_clone_inflate(void)
8607 {
8608 	_blob_esnap_clone_hydrate(true);
8609 }
8610 
8611 static void
8612 blob_esnap_clone_decouple(void)
8613 {
8614 	_blob_esnap_clone_hydrate(false);
8615 }
8616 
8617 static void
8618 blob_esnap_hotplug(void)
8619 {
8620 	struct spdk_blob_store	*bs = g_bs;
8621 	struct ut_esnap_opts	esnap1_opts, esnap2_opts;
8622 	struct spdk_blob_opts	opts;
8623 	struct spdk_blob	*blob;
8624 	struct spdk_bs_dev	*bs_dev;
8625 	struct ut_esnap_dev	*esnap_dev;
8626 	uint32_t		cluster_sz = spdk_bs_get_cluster_size(bs);
8627 	uint32_t		block_sz = spdk_bs_get_io_unit_size(bs);
8628 	const uint32_t		esnap_num_clusters = 4;
8629 	uint64_t		esnap_num_blocks = cluster_sz * esnap_num_clusters / block_sz;
8630 	bool			destroyed1 = false, destroyed2 = false;
8631 	uint64_t		start_thread = g_ut_thread_id;
8632 	struct spdk_io_channel	*ch0, *ch1;
8633 	char			buf[block_sz];
8634 
8635 	/* Create and open an esnap clone blob */
8636 	ut_spdk_blob_opts_init(&opts);
8637 	ut_esnap_opts_init(block_sz, esnap_num_blocks, "esnap1", &destroyed1, &esnap1_opts);
8638 	opts.esnap_id = &esnap1_opts;
8639 	opts.esnap_id_len = sizeof(esnap1_opts);
8640 	opts.num_clusters = esnap_num_clusters;
8641 	blob = ut_blob_create_and_open(bs, &opts);
8642 	CU_ASSERT(blob != NULL);
8643 	CU_ASSERT(spdk_blob_is_esnap_clone(blob));
8644 	SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL);
8645 	esnap_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8646 	CU_ASSERT(strcmp(esnap_dev->ut_opts.name, "esnap1") == 0);
8647 
8648 	/* Replace the external snapshot */
8649 	ut_esnap_opts_init(block_sz, esnap_num_blocks, "esnap2", &destroyed2, &esnap2_opts);
8650 	bs_dev = ut_esnap_dev_alloc(&esnap2_opts);
8651 	CU_ASSERT(!destroyed1);
8652 	CU_ASSERT(!destroyed2);
8653 	g_bserrno = 0xbad;
8654 	spdk_blob_set_esnap_bs_dev(blob, bs_dev, bs_op_complete, NULL);
8655 	poll_threads();
8656 	CU_ASSERT(g_bserrno == 0);
8657 	CU_ASSERT(destroyed1);
8658 	CU_ASSERT(!destroyed2);
8659 	SPDK_CU_ASSERT_FATAL(bs_dev == blob->back_bs_dev);
8660 	SPDK_CU_ASSERT_FATAL(bs_dev == spdk_blob_get_esnap_bs_dev(blob));
8661 	esnap_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8662 	CU_ASSERT(strcmp(esnap_dev->ut_opts.name, "esnap2") == 0);
8663 
8664 	/* Create a couple channels */
8665 	set_thread(0);
8666 	ch0 = spdk_bs_alloc_io_channel(bs);
8667 	CU_ASSERT(ch0 != NULL);
8668 	spdk_blob_io_read(blob, ch0, buf, 0, 1, bs_op_complete, NULL);
8669 	set_thread(1);
8670 	ch1 = spdk_bs_alloc_io_channel(bs);
8671 	CU_ASSERT(ch1 != NULL);
8672 	spdk_blob_io_read(blob, ch1, buf, 0, 1, bs_op_complete, NULL);
8673 	set_thread(start_thread);
8674 	poll_threads();
8675 	CU_ASSERT(esnap_dev->num_channels == 2);
8676 
8677 	/* Replace the external snapshot */
8678 	ut_esnap_opts_init(block_sz, esnap_num_blocks, "esnap1a", &destroyed1, &esnap1_opts);
8679 	bs_dev = ut_esnap_dev_alloc(&esnap1_opts);
8680 	destroyed1 = destroyed2 = false;
8681 	g_bserrno = 0xbad;
8682 	spdk_blob_set_esnap_bs_dev(blob, bs_dev, bs_op_complete, NULL);
8683 	poll_threads();
8684 	CU_ASSERT(g_bserrno == 0);
8685 	CU_ASSERT(!destroyed1);
8686 	CU_ASSERT(destroyed2);
8687 	SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL);
8688 	esnap_dev = (struct ut_esnap_dev *)blob->back_bs_dev;
8689 	CU_ASSERT(strcmp(esnap_dev->ut_opts.name, "esnap1a") == 0);
8690 
8691 	/* Clean up */
8692 	set_thread(0);
8693 	spdk_bs_free_io_channel(ch0);
8694 	set_thread(1);
8695 	spdk_bs_free_io_channel(ch1);
8696 	set_thread(start_thread);
8697 	g_bserrno = 0xbad;
8698 	spdk_blob_close(blob, bs_op_complete, NULL);
8699 	poll_threads();
8700 	CU_ASSERT(g_bserrno == 0);
8701 }
8702 
8703 static bool g_blob_is_degraded;
8704 static int g_blob_is_degraded_called;
8705 
8706 static bool
8707 _blob_is_degraded(struct spdk_bs_dev *dev)
8708 {
8709 	g_blob_is_degraded_called++;
8710 	return g_blob_is_degraded;
8711 }
8712 
8713 static void
8714 blob_is_degraded(void)
8715 {
8716 	struct spdk_bs_dev bs_is_degraded_null = { 0 };
8717 	struct spdk_bs_dev bs_is_degraded = { .is_degraded = _blob_is_degraded };
8718 
8719 	/* No back_bs_dev, no bs->dev->is_degraded */
8720 	g_blob_is_degraded_called = 0;
8721 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8722 	CU_ASSERT(g_blob_is_degraded_called == 0);
8723 
8724 	/* No back_bs_dev, blobstore device degraded */
8725 	g_bs->dev->is_degraded = _blob_is_degraded;
8726 	g_blob_is_degraded_called = 0;
8727 	g_blob_is_degraded = true;
8728 	CU_ASSERT(spdk_blob_is_degraded(g_blob));
8729 	CU_ASSERT(g_blob_is_degraded_called == 1);
8730 
8731 	/* No back_bs_dev, blobstore device not degraded */
8732 	g_bs->dev->is_degraded = _blob_is_degraded;
8733 	g_blob_is_degraded_called = 0;
8734 	g_blob_is_degraded = false;
8735 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8736 	CU_ASSERT(g_blob_is_degraded_called == 1);
8737 
8738 	/* back_bs_dev does not define is_degraded, no bs->dev->is_degraded */
8739 	g_bs->dev->is_degraded = NULL;
8740 	g_blob->back_bs_dev = &bs_is_degraded_null;
8741 	g_blob_is_degraded_called = 0;
8742 	g_blob_is_degraded = false;
8743 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8744 	CU_ASSERT(g_blob_is_degraded_called == 0);
8745 
8746 	/* back_bs_dev is not degraded, no bs->dev->is_degraded */
8747 	g_bs->dev->is_degraded = NULL;
8748 	g_blob->back_bs_dev = &bs_is_degraded;
8749 	g_blob_is_degraded_called = 0;
8750 	g_blob_is_degraded = false;
8751 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8752 	CU_ASSERT(g_blob_is_degraded_called == 1);
8753 
8754 	/* back_bs_dev is degraded, no bs->dev->is_degraded */
8755 	g_bs->dev->is_degraded = NULL;
8756 	g_blob->back_bs_dev = &bs_is_degraded;
8757 	g_blob_is_degraded_called = 0;
8758 	g_blob_is_degraded = true;
8759 	CU_ASSERT(spdk_blob_is_degraded(g_blob));
8760 	CU_ASSERT(g_blob_is_degraded_called == 1);
8761 
8762 	/* back_bs_dev is not degraded, blobstore device is not degraded */
8763 	g_bs->dev->is_degraded = _blob_is_degraded;
8764 	g_blob->back_bs_dev = &bs_is_degraded;
8765 	g_blob_is_degraded_called = 0;
8766 	g_blob_is_degraded = false;
8767 	CU_ASSERT(!spdk_blob_is_degraded(g_blob));
8768 	CU_ASSERT(g_blob_is_degraded_called == 2);
8769 
8770 	g_blob->back_bs_dev = NULL;
8771 }
8772 
8773 static void
8774 suite_bs_setup(void)
8775 {
8776 	struct spdk_bs_dev *dev;
8777 
8778 	dev = init_dev();
8779 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8780 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
8781 	poll_threads();
8782 	CU_ASSERT(g_bserrno == 0);
8783 	CU_ASSERT(g_bs != NULL);
8784 }
8785 
8786 static void
8787 suite_esnap_bs_setup(void)
8788 {
8789 	struct spdk_bs_dev	*dev;
8790 	struct spdk_bs_opts	bs_opts;
8791 
8792 	dev = init_dev();
8793 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8794 	spdk_bs_opts_init(&bs_opts, sizeof(bs_opts));
8795 	bs_opts.cluster_sz = 16 * 1024;
8796 	bs_opts.esnap_bs_dev_create = ut_esnap_create;
8797 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
8798 	poll_threads();
8799 	CU_ASSERT(g_bserrno == 0);
8800 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
8801 }
8802 
8803 static void
8804 suite_bs_cleanup(void)
8805 {
8806 	if (g_bs != NULL) {
8807 		spdk_bs_unload(g_bs, bs_op_complete, NULL);
8808 		poll_threads();
8809 		CU_ASSERT(g_bserrno == 0);
8810 		g_bs = NULL;
8811 	}
8812 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
8813 }
8814 
8815 static struct spdk_blob *
8816 ut_blob_create_and_open(struct spdk_blob_store *bs, struct spdk_blob_opts *blob_opts)
8817 {
8818 	struct spdk_blob *blob;
8819 	struct spdk_blob_opts create_blob_opts;
8820 	spdk_blob_id blobid;
8821 
8822 	if (blob_opts == NULL) {
8823 		ut_spdk_blob_opts_init(&create_blob_opts);
8824 		blob_opts = &create_blob_opts;
8825 	}
8826 
8827 	spdk_bs_create_blob_ext(bs, blob_opts, blob_op_with_id_complete, NULL);
8828 	poll_threads();
8829 	CU_ASSERT(g_bserrno == 0);
8830 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
8831 	blobid = g_blobid;
8832 	g_blobid = -1;
8833 
8834 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
8835 	poll_threads();
8836 	CU_ASSERT(g_bserrno == 0);
8837 	CU_ASSERT(g_blob != NULL);
8838 	blob = g_blob;
8839 
8840 	g_blob = NULL;
8841 	g_bserrno = -1;
8842 
8843 	return blob;
8844 }
8845 
8846 static void
8847 ut_blob_close_and_delete(struct spdk_blob_store *bs, struct spdk_blob *blob)
8848 {
8849 	spdk_blob_id blobid = spdk_blob_get_id(blob);
8850 
8851 	spdk_blob_close(blob, blob_op_complete, NULL);
8852 	poll_threads();
8853 	CU_ASSERT(g_bserrno == 0);
8854 	g_blob = NULL;
8855 
8856 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
8857 	poll_threads();
8858 	CU_ASSERT(g_bserrno == 0);
8859 	g_bserrno = -1;
8860 }
8861 
8862 static void
8863 suite_blob_setup(void)
8864 {
8865 	suite_bs_setup();
8866 	CU_ASSERT(g_bs != NULL);
8867 
8868 	g_blob = ut_blob_create_and_open(g_bs, NULL);
8869 	CU_ASSERT(g_blob != NULL);
8870 }
8871 
8872 static void
8873 suite_blob_cleanup(void)
8874 {
8875 	ut_blob_close_and_delete(g_bs, g_blob);
8876 	CU_ASSERT(g_blob == NULL);
8877 
8878 	suite_bs_cleanup();
8879 	CU_ASSERT(g_bs == NULL);
8880 }
8881 
8882 static int
8883 ut_setup_config_nocopy_noextent(void)
8884 {
8885 	g_dev_copy_enabled = false;
8886 	g_use_extent_table = false;
8887 
8888 	return 0;
8889 }
8890 
8891 static int
8892 ut_setup_config_nocopy_extent(void)
8893 {
8894 	g_dev_copy_enabled = false;
8895 	g_use_extent_table = true;
8896 
8897 	return 0;
8898 }
8899 
8900 static int
8901 ut_setup_config_copy_noextent(void)
8902 {
8903 	g_dev_copy_enabled = true;
8904 	g_use_extent_table = false;
8905 
8906 	return 0;
8907 }
8908 
8909 static int
8910 ut_setup_config_copy_extent(void)
8911 {
8912 	g_dev_copy_enabled = true;
8913 	g_use_extent_table = true;
8914 
8915 	return 0;
8916 }
8917 
8918 struct ut_config {
8919 	const char *suffix;
8920 	CU_InitializeFunc setup_cb;
8921 };
8922 
8923 int
8924 main(int argc, char **argv)
8925 {
8926 	CU_pSuite		suite, suite_bs, suite_blob, suite_esnap_bs;
8927 	unsigned int		i, num_failures;
8928 	char			suite_name[4096];
8929 	struct ut_config	*config;
8930 	struct ut_config	configs[] = {
8931 		{"nocopy_noextent", ut_setup_config_nocopy_noextent},
8932 		{"nocopy_extent", ut_setup_config_nocopy_extent},
8933 		{"copy_noextent", ut_setup_config_copy_noextent},
8934 		{"copy_extent", ut_setup_config_copy_extent},
8935 	};
8936 
8937 	CU_initialize_registry();
8938 
8939 	for (i = 0; i < SPDK_COUNTOF(configs); ++i) {
8940 		config = &configs[i];
8941 
8942 		snprintf(suite_name, sizeof(suite_name), "blob_%s", config->suffix);
8943 		suite = CU_add_suite(suite_name, config->setup_cb, NULL);
8944 
8945 		snprintf(suite_name, sizeof(suite_name), "blob_bs_%s", config->suffix);
8946 		suite_bs = CU_add_suite_with_setup_and_teardown(suite_name, config->setup_cb, NULL,
8947 				suite_bs_setup, suite_bs_cleanup);
8948 
8949 		snprintf(suite_name, sizeof(suite_name), "blob_blob_%s", config->suffix);
8950 		suite_blob = CU_add_suite_with_setup_and_teardown(suite_name, config->setup_cb, NULL,
8951 				suite_blob_setup, suite_blob_cleanup);
8952 
8953 		snprintf(suite_name, sizeof(suite_name), "blob_esnap_bs_%s", config->suffix);
8954 		suite_esnap_bs = CU_add_suite_with_setup_and_teardown(suite_name, config->setup_cb, NULL,
8955 				 suite_esnap_bs_setup,
8956 				 suite_bs_cleanup);
8957 
8958 		CU_ADD_TEST(suite, blob_init);
8959 		CU_ADD_TEST(suite_bs, blob_open);
8960 		CU_ADD_TEST(suite_bs, blob_create);
8961 		CU_ADD_TEST(suite_bs, blob_create_loop);
8962 		CU_ADD_TEST(suite_bs, blob_create_fail);
8963 		CU_ADD_TEST(suite_bs, blob_create_internal);
8964 		CU_ADD_TEST(suite_bs, blob_create_zero_extent);
8965 		CU_ADD_TEST(suite, blob_thin_provision);
8966 		CU_ADD_TEST(suite_bs, blob_snapshot);
8967 		CU_ADD_TEST(suite_bs, blob_clone);
8968 		CU_ADD_TEST(suite_bs, blob_inflate);
8969 		CU_ADD_TEST(suite_bs, blob_delete);
8970 		CU_ADD_TEST(suite_bs, blob_resize_test);
8971 		CU_ADD_TEST(suite, blob_read_only);
8972 		CU_ADD_TEST(suite_bs, channel_ops);
8973 		CU_ADD_TEST(suite_bs, blob_super);
8974 		CU_ADD_TEST(suite_blob, blob_write);
8975 		CU_ADD_TEST(suite_blob, blob_read);
8976 		CU_ADD_TEST(suite_blob, blob_rw_verify);
8977 		CU_ADD_TEST(suite_bs, blob_rw_verify_iov);
8978 		CU_ADD_TEST(suite_blob, blob_rw_verify_iov_nomem);
8979 		CU_ADD_TEST(suite_blob, blob_rw_iov_read_only);
8980 		CU_ADD_TEST(suite_bs, blob_unmap);
8981 		CU_ADD_TEST(suite_bs, blob_iter);
8982 		CU_ADD_TEST(suite_blob, blob_xattr);
8983 		CU_ADD_TEST(suite_bs, blob_parse_md);
8984 		CU_ADD_TEST(suite, bs_load);
8985 		CU_ADD_TEST(suite_bs, bs_load_pending_removal);
8986 		CU_ADD_TEST(suite, bs_load_custom_cluster_size);
8987 		CU_ADD_TEST(suite, bs_load_after_failed_grow);
8988 		CU_ADD_TEST(suite_bs, bs_unload);
8989 		CU_ADD_TEST(suite, bs_cluster_sz);
8990 		CU_ADD_TEST(suite_bs, bs_usable_clusters);
8991 		CU_ADD_TEST(suite, bs_resize_md);
8992 		CU_ADD_TEST(suite, bs_destroy);
8993 		CU_ADD_TEST(suite, bs_type);
8994 		CU_ADD_TEST(suite, bs_super_block);
8995 		CU_ADD_TEST(suite, bs_test_recover_cluster_count);
8996 		CU_ADD_TEST(suite, bs_grow_live);
8997 		CU_ADD_TEST(suite, bs_grow_live_no_space);
8998 		CU_ADD_TEST(suite, bs_test_grow);
8999 		CU_ADD_TEST(suite, blob_serialize_test);
9000 		CU_ADD_TEST(suite_bs, blob_crc);
9001 		CU_ADD_TEST(suite, super_block_crc);
9002 		CU_ADD_TEST(suite_blob, blob_dirty_shutdown);
9003 		CU_ADD_TEST(suite_bs, blob_flags);
9004 		CU_ADD_TEST(suite_bs, bs_version);
9005 		CU_ADD_TEST(suite_bs, blob_set_xattrs_test);
9006 		CU_ADD_TEST(suite_bs, blob_thin_prov_alloc);
9007 		CU_ADD_TEST(suite_bs, blob_insert_cluster_msg_test);
9008 		CU_ADD_TEST(suite_bs, blob_thin_prov_rw);
9009 		CU_ADD_TEST(suite, blob_thin_prov_write_count_io);
9010 		CU_ADD_TEST(suite_bs, blob_thin_prov_rle);
9011 		CU_ADD_TEST(suite_bs, blob_thin_prov_rw_iov);
9012 		CU_ADD_TEST(suite, bs_load_iter_test);
9013 		CU_ADD_TEST(suite_bs, blob_snapshot_rw);
9014 		CU_ADD_TEST(suite_bs, blob_snapshot_rw_iov);
9015 		CU_ADD_TEST(suite, blob_relations);
9016 		CU_ADD_TEST(suite, blob_relations2);
9017 		CU_ADD_TEST(suite, blob_relations3);
9018 		CU_ADD_TEST(suite, blobstore_clean_power_failure);
9019 		CU_ADD_TEST(suite, blob_delete_snapshot_power_failure);
9020 		CU_ADD_TEST(suite, blob_create_snapshot_power_failure);
9021 		CU_ADD_TEST(suite_bs, blob_inflate_rw);
9022 		CU_ADD_TEST(suite_bs, blob_snapshot_freeze_io);
9023 		CU_ADD_TEST(suite_bs, blob_operation_split_rw);
9024 		CU_ADD_TEST(suite_bs, blob_operation_split_rw_iov);
9025 		CU_ADD_TEST(suite, blob_io_unit);
9026 		CU_ADD_TEST(suite, blob_io_unit_compatibility);
9027 		CU_ADD_TEST(suite_bs, blob_simultaneous_operations);
9028 		CU_ADD_TEST(suite_bs, blob_persist_test);
9029 		CU_ADD_TEST(suite_bs, blob_decouple_snapshot);
9030 		CU_ADD_TEST(suite_bs, blob_seek_io_unit);
9031 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_create);
9032 		CU_ADD_TEST(suite_bs, blob_nested_freezes);
9033 		CU_ADD_TEST(suite, blob_ext_md_pages);
9034 		CU_ADD_TEST(suite, blob_esnap_io_4096_4096);
9035 		CU_ADD_TEST(suite, blob_esnap_io_512_512);
9036 		CU_ADD_TEST(suite, blob_esnap_io_4096_512);
9037 		CU_ADD_TEST(suite, blob_esnap_io_512_4096);
9038 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_thread_add_remove);
9039 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_snapshot);
9040 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_inflate);
9041 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_decouple);
9042 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_clone_reload);
9043 		CU_ADD_TEST(suite_esnap_bs, blob_esnap_hotplug);
9044 		CU_ADD_TEST(suite_blob, blob_is_degraded);
9045 	}
9046 
9047 	allocate_threads(2);
9048 	set_thread(0);
9049 
9050 	g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
9051 
9052 	num_failures = spdk_ut_run_tests(argc, argv, NULL);
9053 
9054 	free(g_dev_buffer);
9055 
9056 	free_threads();
9057 
9058 	return num_failures;
9059 }
9060