xref: /spdk/test/unit/lib/blob/blob.c/blob_ut.c (revision 18261f8457e6dc48cf056fca31b7269ce7ead441)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk_cunit.h"
37 #include "spdk/blob.h"
38 #include "spdk/string.h"
39 
40 #include "common/lib/test_env.c"
41 #include "../bs_dev_common.c"
42 #include "blob/blobstore.c"
43 #include "blob/request.c"
44 #include "blob/zeroes.c"
45 #include "blob/blob_bs_dev.c"
46 
47 struct spdk_blob_store *g_bs;
48 spdk_blob_id g_blobid;
49 struct spdk_blob *g_blob;
50 int g_bserrno;
51 struct spdk_xattr_names *g_names;
52 int g_done;
53 char *g_xattr_names[] = {"first", "second", "third"};
54 char *g_xattr_values[] = {"one", "two", "three"};
55 uint64_t g_ctx = 1729;
56 
57 struct spdk_bs_super_block_ver1 {
58 	uint8_t		signature[8];
59 	uint32_t        version;
60 	uint32_t        length;
61 	uint32_t	clean; /* If there was a clean shutdown, this is 1. */
62 	spdk_blob_id	super_blob;
63 
64 	uint32_t	cluster_size; /* In bytes */
65 
66 	uint32_t	used_page_mask_start; /* Offset from beginning of disk, in pages */
67 	uint32_t	used_page_mask_len; /* Count, in pages */
68 
69 	uint32_t	used_cluster_mask_start; /* Offset from beginning of disk, in pages */
70 	uint32_t	used_cluster_mask_len; /* Count, in pages */
71 
72 	uint32_t	md_start; /* Offset from beginning of disk, in pages */
73 	uint32_t	md_len; /* Count, in pages */
74 
75 	uint8_t		reserved[4036];
76 	uint32_t	crc;
77 } __attribute__((packed));
78 SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_super_block_ver1) == 0x1000, "Invalid super block size");
79 
80 
81 static void
82 _get_xattr_value(void *arg, const char *name,
83 		 const void **value, size_t *value_len)
84 {
85 	uint64_t i;
86 
87 	SPDK_CU_ASSERT_FATAL(value_len != NULL);
88 	SPDK_CU_ASSERT_FATAL(value != NULL);
89 	CU_ASSERT(arg == &g_ctx)
90 
91 	for (i = 0; i < sizeof(g_xattr_names); i++) {
92 		if (!strcmp(name, g_xattr_names[i])) {
93 			*value_len = strlen(g_xattr_values[i]);
94 			*value = g_xattr_values[i];
95 			break;
96 		}
97 	}
98 }
99 
100 static void
101 _get_xattr_value_null(void *arg, const char *name,
102 		      const void **value, size_t *value_len)
103 {
104 	SPDK_CU_ASSERT_FATAL(value_len != NULL);
105 	SPDK_CU_ASSERT_FATAL(value != NULL);
106 	CU_ASSERT(arg == NULL)
107 
108 	*value_len = 0;
109 	*value = NULL;
110 }
111 
112 
113 
114 static void
115 bs_op_complete(void *cb_arg, int bserrno)
116 {
117 	g_bserrno = bserrno;
118 }
119 
120 static void
121 bs_op_with_handle_complete(void *cb_arg, struct spdk_blob_store *bs,
122 			   int bserrno)
123 {
124 	g_bs = bs;
125 	g_bserrno = bserrno;
126 }
127 
128 static void
129 blob_op_complete(void *cb_arg, int bserrno)
130 {
131 	g_bserrno = bserrno;
132 }
133 
134 static void
135 blob_op_with_id_complete(void *cb_arg, spdk_blob_id blobid, int bserrno)
136 {
137 	g_blobid = blobid;
138 	g_bserrno = bserrno;
139 }
140 
141 static void
142 blob_op_with_handle_complete(void *cb_arg, struct spdk_blob *blb, int bserrno)
143 {
144 	g_blob = blb;
145 	g_bserrno = bserrno;
146 }
147 
148 static void
149 blob_init(void)
150 {
151 	struct spdk_bs_dev *dev;
152 
153 	dev = init_dev();
154 
155 	/* should fail for an unsupported blocklen */
156 	dev->blocklen = 500;
157 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
158 	CU_ASSERT(g_bserrno == -EINVAL);
159 
160 	dev = init_dev();
161 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
162 	CU_ASSERT(g_bserrno == 0);
163 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
164 
165 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
166 	CU_ASSERT(g_bserrno == 0);
167 	g_bs = NULL;
168 }
169 
170 static void
171 blob_super(void)
172 {
173 	struct spdk_blob_store *bs;
174 	struct spdk_bs_dev *dev;
175 	spdk_blob_id blobid;
176 
177 	dev = init_dev();
178 
179 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
180 	CU_ASSERT(g_bserrno == 0);
181 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
182 	bs = g_bs;
183 
184 	/* Get the super blob without having set one */
185 	spdk_bs_get_super(bs, blob_op_with_id_complete, NULL);
186 	CU_ASSERT(g_bserrno == -ENOENT);
187 	CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID);
188 
189 	/* Create a blob */
190 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
191 	CU_ASSERT(g_bserrno == 0);
192 	CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
193 	blobid = g_blobid;
194 
195 	/* Set the blob as the super blob */
196 	spdk_bs_set_super(bs, blobid, blob_op_complete, NULL);
197 	CU_ASSERT(g_bserrno == 0);
198 
199 	/* Get the super blob */
200 	spdk_bs_get_super(bs, blob_op_with_id_complete, NULL);
201 	CU_ASSERT(g_bserrno == 0);
202 	CU_ASSERT(blobid == g_blobid);
203 
204 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
205 	CU_ASSERT(g_bserrno == 0);
206 	g_bs = NULL;
207 }
208 
209 static void
210 blob_open(void)
211 {
212 	struct spdk_blob_store *bs;
213 	struct spdk_bs_dev *dev;
214 	struct spdk_blob *blob;
215 	spdk_blob_id blobid, blobid2;
216 
217 	dev = init_dev();
218 
219 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
220 	CU_ASSERT(g_bserrno == 0);
221 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
222 	bs = g_bs;
223 
224 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
225 	CU_ASSERT(g_bserrno == 0);
226 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
227 	blobid = g_blobid;
228 
229 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
230 	CU_ASSERT(g_bserrno == 0);
231 	CU_ASSERT(g_blob != NULL);
232 	blob = g_blob;
233 
234 	blobid2 = spdk_blob_get_id(blob);
235 	CU_ASSERT(blobid == blobid2);
236 
237 	/* Try to open file again.  It should return success. */
238 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
239 	CU_ASSERT(g_bserrno == 0);
240 	CU_ASSERT(blob == g_blob);
241 
242 	spdk_blob_close(blob, blob_op_complete, NULL);
243 	CU_ASSERT(g_bserrno == 0);
244 
245 	/*
246 	 * Close the file a second time, releasing the second reference.  This
247 	 *  should succeed.
248 	 */
249 	blob = g_blob;
250 	spdk_blob_close(blob, blob_op_complete, NULL);
251 	CU_ASSERT(g_bserrno == 0);
252 
253 	/*
254 	 * Try to open file again.  It should succeed.  This tests the case
255 	 *  where the file is opened, closed, then re-opened again.
256 	 */
257 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
258 	CU_ASSERT(g_bserrno == 0);
259 	CU_ASSERT(g_blob != NULL);
260 	blob = g_blob;
261 
262 	spdk_blob_close(blob, blob_op_complete, NULL);
263 	CU_ASSERT(g_bserrno == 0);
264 
265 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
266 	CU_ASSERT(g_bserrno == 0);
267 	g_bs = NULL;
268 }
269 
270 static void
271 blob_create(void)
272 {
273 	struct spdk_blob_store *bs;
274 	struct spdk_bs_dev *dev;
275 	struct spdk_blob *blob;
276 	struct spdk_blob_opts opts;
277 	spdk_blob_id blobid;
278 
279 	dev = init_dev();
280 
281 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
282 	CU_ASSERT(g_bserrno == 0);
283 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
284 	bs = g_bs;
285 
286 	/* Create blob with 10 clusters */
287 
288 	spdk_blob_opts_init(&opts);
289 	opts.num_clusters = 10;
290 
291 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
292 	CU_ASSERT(g_bserrno == 0);
293 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
294 	blobid = g_blobid;
295 
296 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
297 	CU_ASSERT(g_bserrno == 0);
298 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
299 	blob = g_blob;
300 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
301 
302 	spdk_blob_close(blob, blob_op_complete, NULL);
303 	CU_ASSERT(g_bserrno == 0);
304 
305 	/* Create blob with 0 clusters */
306 
307 	spdk_blob_opts_init(&opts);
308 	opts.num_clusters = 0;
309 
310 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
311 	CU_ASSERT(g_bserrno == 0);
312 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
313 	blobid = g_blobid;
314 
315 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
316 	CU_ASSERT(g_bserrno == 0);
317 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
318 	blob = g_blob;
319 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0)
320 
321 	spdk_blob_close(blob, blob_op_complete, NULL);
322 	CU_ASSERT(g_bserrno == 0);
323 
324 	/* Create blob with default options (opts == NULL) */
325 
326 	spdk_bs_create_blob_ext(bs, NULL, blob_op_with_id_complete, NULL);
327 	CU_ASSERT(g_bserrno == 0);
328 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
329 	blobid = g_blobid;
330 
331 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
332 	CU_ASSERT(g_bserrno == 0);
333 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
334 	blob = g_blob;
335 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0)
336 
337 	spdk_blob_close(blob, blob_op_complete, NULL);
338 	CU_ASSERT(g_bserrno == 0);
339 
340 	/* Try to create blob with size larger than blobstore */
341 
342 	spdk_blob_opts_init(&opts);
343 	opts.num_clusters = bs->total_clusters + 1;
344 
345 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
346 	CU_ASSERT(g_bserrno == -ENOSPC);
347 
348 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
349 	CU_ASSERT(g_bserrno == 0);
350 	g_bs = NULL;
351 
352 }
353 
354 static void
355 blob_create_internal(void)
356 {
357 	struct spdk_blob_store *bs;
358 	struct spdk_bs_dev *dev;
359 	struct spdk_blob *blob;
360 	struct spdk_blob_opts opts;
361 	struct spdk_blob_xattr_opts internal_xattrs;
362 	const void *value;
363 	size_t value_len;
364 	spdk_blob_id blobid;
365 	int rc;
366 
367 	dev = init_dev();
368 
369 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
370 	CU_ASSERT(g_bserrno == 0);
371 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
372 	bs = g_bs;
373 
374 	/* Create blob with custom xattrs */
375 
376 	spdk_blob_opts_init(&opts);
377 	_spdk_blob_xattrs_init(&internal_xattrs);
378 	internal_xattrs.count = 3;
379 	internal_xattrs.names = g_xattr_names;
380 	internal_xattrs.get_value = _get_xattr_value;
381 	internal_xattrs.ctx = &g_ctx;
382 
383 	_spdk_bs_create_blob(bs, &opts, &internal_xattrs, blob_op_with_id_complete, NULL);
384 	CU_ASSERT(g_bserrno == 0);
385 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
386 	blobid = g_blobid;
387 
388 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
389 	CU_ASSERT(g_bserrno == 0);
390 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
391 	blob = g_blob;
392 
393 	rc = _spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len, true);
394 	CU_ASSERT(rc == 0);
395 	SPDK_CU_ASSERT_FATAL(value != NULL);
396 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
397 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
398 
399 	rc = _spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len, true);
400 	CU_ASSERT(rc == 0);
401 	SPDK_CU_ASSERT_FATAL(value != NULL);
402 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
403 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
404 
405 	rc = _spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len, true);
406 	CU_ASSERT(rc == 0);
407 	SPDK_CU_ASSERT_FATAL(value != NULL);
408 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
409 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
410 
411 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len);
412 	CU_ASSERT(rc != 0);
413 
414 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len);
415 	CU_ASSERT(rc != 0);
416 
417 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len);
418 	CU_ASSERT(rc != 0);
419 
420 	spdk_blob_close(blob, blob_op_complete, NULL);
421 	CU_ASSERT(g_bserrno == 0);
422 
423 	/* Create blob with NULL internal options  */
424 
425 	_spdk_bs_create_blob(bs, NULL, NULL, blob_op_with_id_complete, NULL);
426 	CU_ASSERT(g_bserrno == 0);
427 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
428 	blobid = g_blobid;
429 
430 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
431 	CU_ASSERT(g_bserrno == 0);
432 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
433 	CU_ASSERT(TAILQ_FIRST(&g_blob->xattrs_internal) == NULL);
434 
435 	blob = g_blob;
436 
437 	spdk_blob_close(blob, blob_op_complete, NULL);
438 	CU_ASSERT(g_bserrno == 0);
439 
440 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
441 	CU_ASSERT(g_bserrno == 0);
442 	g_bs = NULL;
443 
444 }
445 
446 static void
447 blob_thin_provision(void)
448 {
449 	struct spdk_blob_store *bs;
450 	struct spdk_bs_dev *dev;
451 	struct spdk_blob *blob;
452 	struct spdk_blob_opts opts;
453 	struct spdk_bs_opts bs_opts;
454 	spdk_blob_id blobid;
455 
456 	dev = init_dev();
457 	spdk_bs_opts_init(&bs_opts);
458 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
459 
460 	/* Initialize a new blob store */
461 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
462 	CU_ASSERT(g_bserrno == 0);
463 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
464 
465 	bs = g_bs;
466 
467 	/* Create blob with thin provisioning enabled */
468 
469 	spdk_blob_opts_init(&opts);
470 	opts.thin_provision = true;
471 	opts.num_clusters = 10;
472 
473 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
474 	CU_ASSERT(g_bserrno == 0);
475 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
476 	blobid = g_blobid;
477 
478 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
479 	CU_ASSERT(g_bserrno == 0);
480 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
481 	blob = g_blob;
482 	CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
483 
484 	spdk_blob_close(blob, blob_op_complete, NULL);
485 	CU_ASSERT(g_bserrno == 0);
486 
487 	/* Do not shut down cleanly.  This makes sure that when we load again
488 	 *  and try to recover a valid used_cluster map, that blobstore will
489 	 *  ignore clusters with index 0 since these are unallocated clusters.
490 	 */
491 
492 	/* Load an existing blob store and check if invalid_flags is set */
493 	dev = init_dev();
494 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
495 	spdk_bs_load(dev, &bs_opts, bs_op_with_handle_complete, NULL);
496 	CU_ASSERT(g_bserrno == 0);
497 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
498 
499 	bs = g_bs;
500 
501 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
502 	CU_ASSERT(g_bserrno == 0);
503 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
504 	blob = g_blob;
505 	CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
506 
507 	spdk_blob_close(blob, blob_op_complete, NULL);
508 	CU_ASSERT(g_bserrno == 0);
509 
510 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
511 	CU_ASSERT(g_bserrno == 0);
512 	g_bs = NULL;
513 }
514 
515 static void
516 blob_snapshot(void)
517 {
518 	struct spdk_blob_store *bs;
519 	struct spdk_bs_dev *dev;
520 	struct spdk_blob *blob;
521 	struct spdk_blob *snapshot, *snapshot2;
522 	struct spdk_blob_bs_dev *blob_bs_dev;
523 	struct spdk_blob_opts opts;
524 	struct spdk_blob_xattr_opts xattrs;
525 	spdk_blob_id blobid;
526 	spdk_blob_id snapshotid;
527 	const void *value;
528 	size_t value_len;
529 	int rc;
530 
531 	dev = init_dev();
532 
533 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
534 	CU_ASSERT(g_bserrno == 0);
535 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
536 	bs = g_bs;
537 
538 	/* Create blob with 10 clusters */
539 	spdk_blob_opts_init(&opts);
540 	opts.num_clusters = 10;
541 
542 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
543 	CU_ASSERT(g_bserrno == 0);
544 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
545 	blobid = g_blobid;
546 
547 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
548 	CU_ASSERT(g_bserrno == 0);
549 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
550 	blob = g_blob;
551 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
552 
553 	/* Create snapshot from blob */
554 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
555 	CU_ASSERT(g_bserrno == 0);
556 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
557 	snapshotid = g_blobid;
558 
559 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
560 	CU_ASSERT(g_bserrno == 0);
561 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
562 	snapshot = g_blob;
563 	CU_ASSERT(snapshot->data_ro == true)
564 	CU_ASSERT(snapshot->md_ro == true)
565 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10)
566 
567 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
568 	CU_ASSERT(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
569 	CU_ASSERT(spdk_mem_all_zero(blob->active.clusters,
570 				    blob->active.num_clusters * sizeof(blob->active.clusters[0])));
571 
572 	/* Try to create snapshot from clone with xattrs */
573 	xattrs.names = g_xattr_names;
574 	xattrs.get_value = _get_xattr_value;
575 	xattrs.count = 3;
576 	xattrs.ctx = &g_ctx;
577 	spdk_bs_create_snapshot(bs, blobid, &xattrs, blob_op_with_id_complete, NULL);
578 	CU_ASSERT(g_bserrno == 0);
579 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
580 	blobid = g_blobid;
581 
582 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
583 	CU_ASSERT(g_bserrno == 0);
584 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
585 	snapshot2 = g_blob;
586 	CU_ASSERT(snapshot2->data_ro == true)
587 	CU_ASSERT(snapshot2->md_ro == true)
588 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 10)
589 
590 	/* Confirm that blob is backed by snapshot2 and snapshot2 is backed by snapshot */
591 	CU_ASSERT(snapshot->back_bs_dev == NULL);
592 	SPDK_CU_ASSERT_FATAL(blob->back_bs_dev != NULL);
593 	SPDK_CU_ASSERT_FATAL(snapshot2->back_bs_dev != NULL);
594 
595 	blob_bs_dev = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
596 	CU_ASSERT(blob_bs_dev->blob == snapshot2);
597 
598 	blob_bs_dev = (struct spdk_blob_bs_dev *)snapshot2->back_bs_dev;
599 	CU_ASSERT(blob_bs_dev->blob == snapshot);
600 
601 	rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[0], &value, &value_len);
602 	CU_ASSERT(rc == 0);
603 	SPDK_CU_ASSERT_FATAL(value != NULL);
604 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
605 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
606 
607 	rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[1], &value, &value_len);
608 	CU_ASSERT(rc == 0);
609 	SPDK_CU_ASSERT_FATAL(value != NULL);
610 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
611 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
612 
613 	rc = spdk_blob_get_xattr_value(snapshot2, g_xattr_names[2], &value, &value_len);
614 	CU_ASSERT(rc == 0);
615 	SPDK_CU_ASSERT_FATAL(value != NULL);
616 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
617 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
618 
619 	/* Try to create snapshot from snapshot */
620 	spdk_bs_create_snapshot(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
621 	CU_ASSERT(g_bserrno == -EINVAL);
622 	CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID);
623 
624 	spdk_blob_close(blob, blob_op_complete, NULL);
625 	CU_ASSERT(g_bserrno == 0);
626 
627 	spdk_blob_close(snapshot, blob_op_complete, NULL);
628 	CU_ASSERT(g_bserrno == 0);
629 
630 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
631 	CU_ASSERT(g_bserrno == 0);
632 
633 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
634 	CU_ASSERT(g_bserrno == 0);
635 	g_bs = NULL;
636 }
637 
638 static void
639 blob_snapshot_freeze_io(void)
640 {
641 	struct spdk_io_channel *channel;
642 	struct spdk_bs_channel *bs_channel;
643 	struct spdk_blob_store *bs;
644 	struct spdk_bs_dev *dev;
645 	struct spdk_blob *blob;
646 	struct spdk_blob_opts opts;
647 	spdk_blob_id blobid;
648 	uint32_t num_of_pages = 10;
649 	uint8_t payload_read[num_of_pages * SPDK_BS_PAGE_SIZE];
650 	uint8_t payload_write[num_of_pages * SPDK_BS_PAGE_SIZE];
651 	uint8_t payload_zero[num_of_pages * SPDK_BS_PAGE_SIZE];
652 
653 	memset(payload_write, 0xE5, sizeof(payload_write));
654 	memset(payload_read, 0x00, sizeof(payload_read));
655 	memset(payload_zero, 0x00, sizeof(payload_zero));
656 
657 	dev = init_dev();
658 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
659 
660 	/* Test freeze I/O during snapshot */
661 
662 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
663 	CU_ASSERT(g_bserrno == 0);
664 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
665 	bs = g_bs;
666 
667 	channel = spdk_bs_alloc_io_channel(bs);
668 	bs_channel = spdk_io_channel_get_ctx(channel);
669 
670 	/* Create blob with 10 clusters */
671 	spdk_blob_opts_init(&opts);
672 	opts.num_clusters = 10;
673 	opts.thin_provision = false;
674 
675 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
676 	CU_ASSERT(g_bserrno == 0);
677 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
678 	blobid = g_blobid;
679 
680 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
681 	CU_ASSERT(g_bserrno == 0);
682 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
683 	blob = g_blob;
684 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
685 
686 	/* Enable explicitly calling callbacks. On each read/write to back device
687 	 * execution will stop and wait until _bs_flush_scheduler is called */
688 	g_scheduler_delay = true;
689 
690 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
691 
692 	/* This is implementation specific.
693 	 * Flag 'frozen_io' is set in _spdk_bs_snapshot_freeze_cpl callback.
694 	 * Four async I/O operations happen before that. */
695 
696 	_bs_flush_scheduler(4);
697 
698 	CU_ASSERT(TAILQ_EMPTY(&bs_channel->queued_io));
699 
700 	/* Blob I/O should be frozen here */
701 	CU_ASSERT(blob->frozen_refcnt == 1);
702 
703 	/* Write to the blob */
704 	spdk_blob_io_write(blob, channel, payload_write, 0, num_of_pages, blob_op_complete, NULL);
705 
706 	/* Verify that I/O is queued */
707 	CU_ASSERT(!TAILQ_EMPTY(&bs_channel->queued_io));
708 	/* Verify that payload is not written to disk */
709 	CU_ASSERT(memcmp(payload_zero, &g_dev_buffer[blob->active.clusters[0]*SPDK_BS_PAGE_SIZE],
710 			 SPDK_BS_PAGE_SIZE) == 0);
711 
712 	/* Disable scheduler delay.
713 	 * Finish all operations including spdk_bs_create_snapshot */
714 	g_scheduler_delay = false;
715 	_bs_flush_scheduler(1);
716 
717 	/* Verify snapshot */
718 	CU_ASSERT(g_bserrno == 0);
719 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
720 
721 	/* Verify that blob has unset frozen_io */
722 	CU_ASSERT(blob->frozen_refcnt == 0);
723 
724 	/* Verify that postponed I/O completed successfully by comparing payload */
725 	spdk_blob_io_read(blob, channel, payload_read, 0, num_of_pages, blob_op_complete, NULL);
726 	CU_ASSERT(g_bserrno == 0);
727 	CU_ASSERT(memcmp(payload_write, payload_read, num_of_pages * SPDK_BS_PAGE_SIZE) == 0);
728 
729 	spdk_blob_close(blob, blob_op_complete, NULL);
730 	CU_ASSERT(g_bserrno == 0);
731 
732 	spdk_bs_free_io_channel(channel);
733 
734 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
735 	CU_ASSERT(g_bserrno == 0);
736 	g_bs = NULL;
737 }
738 
739 static void
740 blob_clone(void)
741 {
742 	struct spdk_blob_store *bs;
743 	struct spdk_bs_dev *dev;
744 	struct spdk_blob_opts opts;
745 	struct spdk_blob *blob, *snapshot, *clone;
746 	spdk_blob_id blobid, cloneid, snapshotid;
747 	struct spdk_blob_xattr_opts xattrs;
748 	const void *value;
749 	size_t value_len;
750 	int rc;
751 
752 	dev = init_dev();
753 
754 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
755 	CU_ASSERT(g_bserrno == 0);
756 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
757 	bs = g_bs;
758 
759 	/* Create blob with 10 clusters */
760 
761 	spdk_blob_opts_init(&opts);
762 	opts.num_clusters = 10;
763 
764 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
765 	CU_ASSERT(g_bserrno == 0);
766 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
767 	blobid = g_blobid;
768 
769 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
770 	CU_ASSERT(g_bserrno == 0);
771 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
772 	blob = g_blob;
773 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
774 
775 	/* Create snapshot */
776 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
777 	CU_ASSERT(g_bserrno == 0);
778 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
779 	snapshotid = g_blobid;
780 
781 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
782 	CU_ASSERT(g_bserrno == 0);
783 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
784 	snapshot = g_blob;
785 	CU_ASSERT(snapshot->data_ro == true)
786 	CU_ASSERT(snapshot->md_ro == true)
787 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10);
788 
789 	spdk_blob_close(snapshot, blob_op_complete, NULL);
790 	CU_ASSERT(g_bserrno == 0);
791 
792 	/* Create clone from snapshot with xattrs */
793 	xattrs.names = g_xattr_names;
794 	xattrs.get_value = _get_xattr_value;
795 	xattrs.count = 3;
796 	xattrs.ctx = &g_ctx;
797 
798 	spdk_bs_create_clone(bs, snapshotid, &xattrs, blob_op_with_id_complete, NULL);
799 	CU_ASSERT(g_bserrno == 0);
800 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
801 	cloneid = g_blobid;
802 
803 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
804 	CU_ASSERT(g_bserrno == 0);
805 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
806 	clone = g_blob;
807 	CU_ASSERT(clone->data_ro == false)
808 	CU_ASSERT(clone->md_ro == false)
809 	CU_ASSERT(spdk_blob_get_num_clusters(clone) == 10);
810 
811 	rc = spdk_blob_get_xattr_value(clone, g_xattr_names[0], &value, &value_len);
812 	CU_ASSERT(rc == 0);
813 	SPDK_CU_ASSERT_FATAL(value != NULL);
814 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
815 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
816 
817 	rc = spdk_blob_get_xattr_value(clone, g_xattr_names[1], &value, &value_len);
818 	CU_ASSERT(rc == 0);
819 	SPDK_CU_ASSERT_FATAL(value != NULL);
820 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
821 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
822 
823 	rc = spdk_blob_get_xattr_value(clone, g_xattr_names[2], &value, &value_len);
824 	CU_ASSERT(rc == 0);
825 	SPDK_CU_ASSERT_FATAL(value != NULL);
826 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
827 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
828 
829 
830 	spdk_blob_close(clone, blob_op_complete, NULL);
831 	CU_ASSERT(g_bserrno == 0);
832 
833 	/* Try to create clone from not read only blob */
834 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
835 	CU_ASSERT(g_bserrno == -EINVAL);
836 	CU_ASSERT(g_blobid == SPDK_BLOBID_INVALID);
837 
838 	/* Mark blob as read only */
839 	spdk_blob_set_read_only(blob);
840 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
841 	CU_ASSERT(g_bserrno == 0);
842 
843 	/* Create clone from read only blob */
844 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
845 	CU_ASSERT(g_bserrno == 0);
846 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
847 	cloneid = g_blobid;
848 
849 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
850 	CU_ASSERT(g_bserrno == 0);
851 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
852 	clone = g_blob;
853 	CU_ASSERT(clone->data_ro == false)
854 	CU_ASSERT(clone->md_ro == false)
855 	CU_ASSERT(spdk_blob_get_num_clusters(clone) == 10);
856 
857 	spdk_blob_close(clone, blob_op_complete, NULL);
858 	CU_ASSERT(g_bserrno == 0);
859 
860 	spdk_blob_close(blob, blob_op_complete, NULL);
861 	CU_ASSERT(g_bserrno == 0);
862 
863 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
864 	CU_ASSERT(g_bserrno == 0);
865 	g_bs = NULL;
866 
867 }
868 
869 static void
870 _blob_inflate(bool decouple_parent)
871 {
872 	struct spdk_blob_store *bs;
873 	struct spdk_bs_dev *dev;
874 	struct spdk_blob_opts opts;
875 	struct spdk_blob *blob, *snapshot;
876 	spdk_blob_id blobid, snapshotid;
877 	struct spdk_io_channel *channel;
878 	uint64_t free_clusters;
879 
880 	dev = init_dev();
881 
882 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
883 	CU_ASSERT(g_bserrno == 0);
884 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
885 	bs = g_bs;
886 
887 	channel = spdk_bs_alloc_io_channel(bs);
888 	SPDK_CU_ASSERT_FATAL(channel != NULL);
889 
890 	/* Create blob with 10 clusters */
891 
892 	spdk_blob_opts_init(&opts);
893 	opts.num_clusters = 10;
894 	opts.thin_provision = true;
895 
896 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
897 	CU_ASSERT(g_bserrno == 0);
898 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
899 	blobid = g_blobid;
900 
901 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
902 	CU_ASSERT(g_bserrno == 0);
903 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
904 	blob = g_blob;
905 
906 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
907 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == true);
908 
909 	/* 1) Blob with no parent */
910 	if (decouple_parent) {
911 		/* Decouple parent of blob with no parent (should fail) */
912 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
913 		CU_ASSERT(g_bserrno != 0);
914 	} else {
915 		/* Inflate of thin blob with no parent should made it thick */
916 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
917 		CU_ASSERT(g_bserrno == 0);
918 		CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == false);
919 	}
920 
921 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
922 	CU_ASSERT(g_bserrno == 0);
923 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
924 	snapshotid = g_blobid;
925 
926 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == true);
927 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
928 
929 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
930 	CU_ASSERT(g_bserrno == 0);
931 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
932 	snapshot = g_blob;
933 	CU_ASSERT(snapshot->data_ro == true)
934 	CU_ASSERT(snapshot->md_ro == true)
935 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 10);
936 
937 	spdk_blob_close(snapshot, blob_op_complete, NULL);
938 	CU_ASSERT(g_bserrno == 0);
939 
940 	free_clusters = spdk_bs_free_cluster_count(bs);
941 
942 	/* 2) Blob with parent */
943 	if (!decouple_parent) {
944 		/* Do full blob inflation */
945 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
946 		CU_ASSERT(g_bserrno == 0);
947 		/* all 10 clusters should be allocated */
948 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 10);
949 	} else {
950 		/* Decouple parent of blob */
951 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
952 		CU_ASSERT(g_bserrno == 0);
953 		/* when only parent is removed, none of the clusters should be allocated */
954 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters);
955 	}
956 
957 	/* Now, it should be possible to delete snapshot */
958 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
959 	CU_ASSERT(g_bserrno == 0);
960 
961 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10)
962 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob) == decouple_parent);
963 
964 	spdk_blob_close(blob, blob_op_complete, NULL);
965 	CU_ASSERT(g_bserrno == 0);
966 
967 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
968 	CU_ASSERT(g_bserrno == 0);
969 	g_bs = NULL;
970 
971 	spdk_bs_free_io_channel(channel);
972 }
973 
974 static void
975 blob_inflate(void)
976 {
977 	_blob_inflate(false);
978 	_blob_inflate(true);
979 }
980 
981 static void
982 blob_delete(void)
983 {
984 	struct spdk_blob_store *bs;
985 	struct spdk_bs_dev *dev;
986 	spdk_blob_id blobid;
987 
988 	dev = init_dev();
989 
990 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
991 	CU_ASSERT(g_bserrno == 0);
992 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
993 	bs = g_bs;
994 
995 	/* Create a blob and then delete it. */
996 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
997 	CU_ASSERT(g_bserrno == 0);
998 	CU_ASSERT(g_blobid > 0);
999 	blobid = g_blobid;
1000 
1001 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
1002 	CU_ASSERT(g_bserrno == 0);
1003 
1004 	/* Try to open the blob */
1005 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1006 	CU_ASSERT(g_bserrno == -ENOENT);
1007 
1008 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1009 	CU_ASSERT(g_bserrno == 0);
1010 	g_bs = NULL;
1011 }
1012 
1013 static void
1014 blob_resize(void)
1015 {
1016 	struct spdk_blob_store *bs;
1017 	struct spdk_bs_dev *dev;
1018 	struct spdk_blob *blob;
1019 	spdk_blob_id blobid;
1020 	uint64_t free_clusters;
1021 
1022 	dev = init_dev();
1023 
1024 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1025 	CU_ASSERT(g_bserrno == 0);
1026 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1027 	bs = g_bs;
1028 	free_clusters = spdk_bs_free_cluster_count(bs);
1029 
1030 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1031 	CU_ASSERT(g_bserrno == 0);
1032 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1033 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
1034 	blobid = g_blobid;
1035 
1036 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1037 	CU_ASSERT(g_bserrno == 0);
1038 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1039 	blob = g_blob;
1040 
1041 	/* Confirm that resize fails if blob is marked read-only. */
1042 	blob->md_ro = true;
1043 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1044 	CU_ASSERT(g_bserrno == -EPERM);
1045 	blob->md_ro = false;
1046 
1047 	/* The blob started at 0 clusters. Resize it to be 5. */
1048 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1049 	CU_ASSERT(g_bserrno == 0);
1050 	CU_ASSERT((free_clusters - 5) == spdk_bs_free_cluster_count(bs));
1051 
1052 	/* Shrink the blob to 3 clusters. This will not actually release
1053 	 * the old clusters until the blob is synced.
1054 	 */
1055 	spdk_blob_resize(blob, 3, blob_op_complete, NULL);
1056 	CU_ASSERT(g_bserrno == 0);
1057 	/* Verify there are still 5 clusters in use */
1058 	CU_ASSERT((free_clusters - 5) == spdk_bs_free_cluster_count(bs));
1059 
1060 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
1061 	CU_ASSERT(g_bserrno == 0);
1062 	/* Now there are only 3 clusters in use */
1063 	CU_ASSERT((free_clusters - 3) == spdk_bs_free_cluster_count(bs));
1064 
1065 	/* Resize the blob to be 10 clusters. Growth takes effect immediately. */
1066 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
1067 	CU_ASSERT(g_bserrno == 0);
1068 	CU_ASSERT((free_clusters - 10) == spdk_bs_free_cluster_count(bs));
1069 
1070 	/* Try to resize the blob to size larger than blobstore. */
1071 	spdk_blob_resize(blob, bs->total_clusters + 1, blob_op_complete, NULL);
1072 	CU_ASSERT(g_bserrno == -ENOSPC);
1073 
1074 	spdk_blob_close(blob, blob_op_complete, NULL);
1075 	CU_ASSERT(g_bserrno == 0);
1076 
1077 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
1078 	CU_ASSERT(g_bserrno == 0);
1079 
1080 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1081 	CU_ASSERT(g_bserrno == 0);
1082 	g_bs = NULL;
1083 }
1084 
1085 static void
1086 blob_read_only(void)
1087 {
1088 	struct spdk_blob_store *bs;
1089 	struct spdk_bs_dev *dev;
1090 	struct spdk_blob *blob;
1091 	struct spdk_bs_opts opts;
1092 	spdk_blob_id blobid;
1093 	int rc;
1094 
1095 	dev = init_dev();
1096 	spdk_bs_opts_init(&opts);
1097 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
1098 
1099 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
1100 	CU_ASSERT(g_bserrno == 0);
1101 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1102 	bs = g_bs;
1103 
1104 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1105 	CU_ASSERT(g_bserrno == 0);
1106 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1107 	blobid = g_blobid;
1108 
1109 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1110 	CU_ASSERT(g_bserrno == 0);
1111 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1112 	blob = g_blob;
1113 
1114 	rc = spdk_blob_set_read_only(blob);
1115 	CU_ASSERT(rc == 0);
1116 
1117 	CU_ASSERT(blob->data_ro == false);
1118 	CU_ASSERT(blob->md_ro == false);
1119 
1120 	spdk_blob_sync_md(blob, bs_op_complete, NULL);
1121 
1122 	CU_ASSERT(blob->data_ro == true);
1123 	CU_ASSERT(blob->md_ro == true);
1124 	CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY);
1125 
1126 	spdk_blob_close(blob, blob_op_complete, NULL);
1127 	CU_ASSERT(g_bserrno == 0);
1128 
1129 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1130 	CU_ASSERT(g_bserrno == 0);
1131 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1132 	blob = g_blob;
1133 
1134 	CU_ASSERT(blob->data_ro == true);
1135 	CU_ASSERT(blob->md_ro == true);
1136 	CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY);
1137 
1138 	spdk_blob_close(blob, blob_op_complete, NULL);
1139 	CU_ASSERT(g_bserrno == 0);
1140 
1141 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1142 	CU_ASSERT(g_bserrno == 0);
1143 	g_bs = NULL;
1144 	g_blob = NULL;
1145 	g_blobid = 0;
1146 
1147 	/* Load an existing blob store */
1148 	dev = init_dev();
1149 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
1150 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
1151 	CU_ASSERT(g_bserrno == 0);
1152 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1153 
1154 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
1155 	CU_ASSERT(g_bserrno == 0);
1156 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1157 	blob = g_blob;
1158 
1159 	CU_ASSERT(blob->data_ro == true);
1160 	CU_ASSERT(blob->md_ro == true);
1161 	CU_ASSERT(blob->data_ro_flags & SPDK_BLOB_READ_ONLY);
1162 
1163 	spdk_blob_close(blob, blob_op_complete, NULL);
1164 	CU_ASSERT(g_bserrno == 0);
1165 
1166 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1167 	CU_ASSERT(g_bserrno == 0);
1168 
1169 }
1170 
1171 static void
1172 channel_ops(void)
1173 {
1174 	struct spdk_blob_store *bs;
1175 	struct spdk_bs_dev *dev;
1176 	struct spdk_io_channel *channel;
1177 
1178 	dev = init_dev();
1179 
1180 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1181 	CU_ASSERT(g_bserrno == 0);
1182 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1183 	bs = g_bs;
1184 
1185 	channel = spdk_bs_alloc_io_channel(bs);
1186 	CU_ASSERT(channel != NULL);
1187 
1188 	spdk_bs_free_io_channel(channel);
1189 
1190 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1191 	CU_ASSERT(g_bserrno == 0);
1192 	g_bs = NULL;
1193 }
1194 
1195 static void
1196 blob_write(void)
1197 {
1198 	struct spdk_blob_store *bs;
1199 	struct spdk_bs_dev *dev;
1200 	struct spdk_blob *blob;
1201 	struct spdk_io_channel *channel;
1202 	spdk_blob_id blobid;
1203 	uint64_t pages_per_cluster;
1204 	uint8_t payload[10 * 4096];
1205 
1206 	dev = init_dev();
1207 
1208 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1209 	CU_ASSERT(g_bserrno == 0);
1210 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1211 	bs = g_bs;
1212 
1213 	pages_per_cluster = spdk_bs_get_cluster_size(bs) / spdk_bs_get_page_size(bs);
1214 
1215 	channel = spdk_bs_alloc_io_channel(bs);
1216 	CU_ASSERT(channel != NULL);
1217 
1218 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1219 	CU_ASSERT(g_bserrno == 0);
1220 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1221 	blobid = g_blobid;
1222 
1223 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1224 	CU_ASSERT(g_bserrno == 0);
1225 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1226 	blob = g_blob;
1227 
1228 	/* Write to a blob with 0 size */
1229 	spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1230 	CU_ASSERT(g_bserrno == -EINVAL);
1231 
1232 	/* Resize the blob */
1233 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1234 	CU_ASSERT(g_bserrno == 0);
1235 
1236 	/* Confirm that write fails if blob is marked read-only. */
1237 	blob->data_ro = true;
1238 	spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1239 	CU_ASSERT(g_bserrno == -EPERM);
1240 	blob->data_ro = false;
1241 
1242 	/* Write to the blob */
1243 	spdk_blob_io_write(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1244 	CU_ASSERT(g_bserrno == 0);
1245 
1246 	/* Write starting beyond the end */
1247 	spdk_blob_io_write(blob, channel, payload, 5 * pages_per_cluster, 1, blob_op_complete,
1248 			   NULL);
1249 	CU_ASSERT(g_bserrno == -EINVAL);
1250 
1251 	/* Write starting at a valid location but going off the end */
1252 	spdk_blob_io_write(blob, channel, payload, 4 * pages_per_cluster, pages_per_cluster + 1,
1253 			   blob_op_complete, NULL);
1254 	CU_ASSERT(g_bserrno == -EINVAL);
1255 
1256 	spdk_blob_close(blob, blob_op_complete, NULL);
1257 	CU_ASSERT(g_bserrno == 0);
1258 
1259 	spdk_bs_free_io_channel(channel);
1260 
1261 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1262 	CU_ASSERT(g_bserrno == 0);
1263 	g_bs = NULL;
1264 }
1265 
1266 static void
1267 blob_read(void)
1268 {
1269 	struct spdk_blob_store *bs;
1270 	struct spdk_bs_dev *dev;
1271 	struct spdk_blob *blob;
1272 	struct spdk_io_channel *channel;
1273 	spdk_blob_id blobid;
1274 	uint64_t pages_per_cluster;
1275 	uint8_t payload[10 * 4096];
1276 
1277 	dev = init_dev();
1278 
1279 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1280 	CU_ASSERT(g_bserrno == 0);
1281 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1282 	bs = g_bs;
1283 
1284 	pages_per_cluster = spdk_bs_get_cluster_size(bs) / spdk_bs_get_page_size(bs);
1285 
1286 	channel = spdk_bs_alloc_io_channel(bs);
1287 	CU_ASSERT(channel != NULL);
1288 
1289 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1290 	CU_ASSERT(g_bserrno == 0);
1291 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1292 	blobid = g_blobid;
1293 
1294 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1295 	CU_ASSERT(g_bserrno == 0);
1296 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1297 	blob = g_blob;
1298 
1299 	/* Read from a blob with 0 size */
1300 	spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1301 	CU_ASSERT(g_bserrno == -EINVAL);
1302 
1303 	/* Resize the blob */
1304 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
1305 	CU_ASSERT(g_bserrno == 0);
1306 
1307 	/* Confirm that read passes if blob is marked read-only. */
1308 	blob->data_ro = true;
1309 	spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1310 	CU_ASSERT(g_bserrno == 0);
1311 	blob->data_ro = false;
1312 
1313 	/* Read from the blob */
1314 	spdk_blob_io_read(blob, channel, payload, 0, 1, blob_op_complete, NULL);
1315 	CU_ASSERT(g_bserrno == 0);
1316 
1317 	/* Read starting beyond the end */
1318 	spdk_blob_io_read(blob, channel, payload, 5 * pages_per_cluster, 1, blob_op_complete,
1319 			  NULL);
1320 	CU_ASSERT(g_bserrno == -EINVAL);
1321 
1322 	/* Read starting at a valid location but going off the end */
1323 	spdk_blob_io_read(blob, channel, payload, 4 * pages_per_cluster, pages_per_cluster + 1,
1324 			  blob_op_complete, NULL);
1325 	CU_ASSERT(g_bserrno == -EINVAL);
1326 
1327 	spdk_blob_close(blob, blob_op_complete, NULL);
1328 	CU_ASSERT(g_bserrno == 0);
1329 
1330 	spdk_bs_free_io_channel(channel);
1331 
1332 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1333 	CU_ASSERT(g_bserrno == 0);
1334 	g_bs = NULL;
1335 }
1336 
1337 static void
1338 blob_rw_verify(void)
1339 {
1340 	struct spdk_blob_store *bs;
1341 	struct spdk_bs_dev *dev;
1342 	struct spdk_blob *blob;
1343 	struct spdk_io_channel *channel;
1344 	spdk_blob_id blobid;
1345 	uint8_t payload_read[10 * 4096];
1346 	uint8_t payload_write[10 * 4096];
1347 
1348 	dev = init_dev();
1349 
1350 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1351 	CU_ASSERT(g_bserrno == 0);
1352 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1353 	bs = g_bs;
1354 
1355 	channel = spdk_bs_alloc_io_channel(bs);
1356 	CU_ASSERT(channel != NULL);
1357 
1358 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1359 	CU_ASSERT(g_bserrno == 0);
1360 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1361 	blobid = g_blobid;
1362 
1363 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1364 	CU_ASSERT(g_bserrno == 0);
1365 	CU_ASSERT(g_blob != NULL);
1366 	blob = g_blob;
1367 
1368 	spdk_blob_resize(blob, 32, blob_op_complete, NULL);
1369 	CU_ASSERT(g_bserrno == 0);
1370 
1371 	memset(payload_write, 0xE5, sizeof(payload_write));
1372 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
1373 	CU_ASSERT(g_bserrno == 0);
1374 
1375 	memset(payload_read, 0x00, sizeof(payload_read));
1376 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
1377 	CU_ASSERT(g_bserrno == 0);
1378 	CU_ASSERT(memcmp(payload_write, payload_read, 4 * 4096) == 0);
1379 
1380 	spdk_blob_close(blob, blob_op_complete, NULL);
1381 	CU_ASSERT(g_bserrno == 0);
1382 
1383 	spdk_bs_free_io_channel(channel);
1384 
1385 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1386 	CU_ASSERT(g_bserrno == 0);
1387 	g_bs = NULL;
1388 }
1389 
1390 static void
1391 blob_rw_verify_iov(void)
1392 {
1393 	struct spdk_blob_store *bs;
1394 	struct spdk_bs_dev *dev;
1395 	struct spdk_blob *blob;
1396 	struct spdk_io_channel *channel;
1397 	spdk_blob_id blobid;
1398 	uint8_t payload_read[10 * 4096];
1399 	uint8_t payload_write[10 * 4096];
1400 	struct iovec iov_read[3];
1401 	struct iovec iov_write[3];
1402 	void *buf;
1403 
1404 	dev = init_dev();
1405 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
1406 
1407 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1408 	CU_ASSERT(g_bserrno == 0);
1409 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1410 	bs = g_bs;
1411 
1412 	channel = spdk_bs_alloc_io_channel(bs);
1413 	CU_ASSERT(channel != NULL);
1414 
1415 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1416 	CU_ASSERT(g_bserrno == 0);
1417 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1418 	blobid = g_blobid;
1419 
1420 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1421 	CU_ASSERT(g_bserrno == 0);
1422 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1423 	blob = g_blob;
1424 
1425 	spdk_blob_resize(blob, 2, blob_op_complete, NULL);
1426 	CU_ASSERT(g_bserrno == 0);
1427 
1428 	/*
1429 	 * Manually adjust the offset of the blob's second cluster.  This allows
1430 	 *  us to make sure that the readv/write code correctly accounts for I/O
1431 	 *  that cross cluster boundaries.  Start by asserting that the allocated
1432 	 *  clusters are where we expect before modifying the second cluster.
1433 	 */
1434 	CU_ASSERT(blob->active.clusters[0] == 1 * 256);
1435 	CU_ASSERT(blob->active.clusters[1] == 2 * 256);
1436 	blob->active.clusters[1] = 3 * 256;
1437 
1438 	memset(payload_write, 0xE5, sizeof(payload_write));
1439 	iov_write[0].iov_base = payload_write;
1440 	iov_write[0].iov_len = 1 * 4096;
1441 	iov_write[1].iov_base = payload_write + 1 * 4096;
1442 	iov_write[1].iov_len = 5 * 4096;
1443 	iov_write[2].iov_base = payload_write + 6 * 4096;
1444 	iov_write[2].iov_len = 4 * 4096;
1445 	/*
1446 	 * Choose a page offset just before the cluster boundary.  The first 6 pages of payload
1447 	 *  will get written to the first cluster, the last 4 to the second cluster.
1448 	 */
1449 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
1450 	CU_ASSERT(g_bserrno == 0);
1451 
1452 	memset(payload_read, 0xAA, sizeof(payload_read));
1453 	iov_read[0].iov_base = payload_read;
1454 	iov_read[0].iov_len = 3 * 4096;
1455 	iov_read[1].iov_base = payload_read + 3 * 4096;
1456 	iov_read[1].iov_len = 4 * 4096;
1457 	iov_read[2].iov_base = payload_read + 7 * 4096;
1458 	iov_read[2].iov_len = 3 * 4096;
1459 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
1460 	CU_ASSERT(g_bserrno == 0);
1461 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
1462 
1463 	buf = calloc(1, 256 * 4096);
1464 	SPDK_CU_ASSERT_FATAL(buf != NULL);
1465 	/* Check that cluster 2 on "disk" was not modified. */
1466 	CU_ASSERT(memcmp(buf, &g_dev_buffer[512 * 4096], 256 * 4096) == 0);
1467 	free(buf);
1468 
1469 	spdk_blob_close(blob, blob_op_complete, NULL);
1470 	CU_ASSERT(g_bserrno == 0);
1471 
1472 	spdk_bs_free_io_channel(channel);
1473 
1474 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1475 	CU_ASSERT(g_bserrno == 0);
1476 	g_bs = NULL;
1477 }
1478 
1479 static uint32_t
1480 bs_channel_get_req_count(struct spdk_io_channel *_channel)
1481 {
1482 	struct spdk_bs_channel *channel = spdk_io_channel_get_ctx(_channel);
1483 	struct spdk_bs_request_set *set;
1484 	uint32_t count = 0;
1485 
1486 	TAILQ_FOREACH(set, &channel->reqs, link) {
1487 		count++;
1488 	}
1489 
1490 	return count;
1491 }
1492 
1493 static void
1494 blob_rw_verify_iov_nomem(void)
1495 {
1496 	struct spdk_blob_store *bs;
1497 	struct spdk_bs_dev *dev;
1498 	struct spdk_blob *blob;
1499 	struct spdk_io_channel *channel;
1500 	spdk_blob_id blobid;
1501 	uint8_t payload_write[10 * 4096];
1502 	struct iovec iov_write[3];
1503 	uint32_t req_count;
1504 
1505 	dev = init_dev();
1506 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
1507 
1508 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1509 	CU_ASSERT(g_bserrno == 0);
1510 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1511 	bs = g_bs;
1512 
1513 	channel = spdk_bs_alloc_io_channel(bs);
1514 	CU_ASSERT(channel != NULL);
1515 
1516 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1517 	CU_ASSERT(g_bserrno == 0);
1518 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1519 	blobid = g_blobid;
1520 
1521 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1522 	CU_ASSERT(g_bserrno == 0);
1523 	CU_ASSERT(g_blob != NULL);
1524 	blob = g_blob;
1525 
1526 	spdk_blob_resize(blob, 2, blob_op_complete, NULL);
1527 	CU_ASSERT(g_bserrno == 0);
1528 
1529 	/*
1530 	 * Choose a page offset just before the cluster boundary.  The first 6 pages of payload
1531 	 *  will get written to the first cluster, the last 4 to the second cluster.
1532 	 */
1533 	iov_write[0].iov_base = payload_write;
1534 	iov_write[0].iov_len = 1 * 4096;
1535 	iov_write[1].iov_base = payload_write + 1 * 4096;
1536 	iov_write[1].iov_len = 5 * 4096;
1537 	iov_write[2].iov_base = payload_write + 6 * 4096;
1538 	iov_write[2].iov_len = 4 * 4096;
1539 	MOCK_SET(calloc, NULL);
1540 	req_count = bs_channel_get_req_count(channel);
1541 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
1542 	CU_ASSERT(g_bserrno = -ENOMEM);
1543 	CU_ASSERT(req_count == bs_channel_get_req_count(channel));
1544 	MOCK_CLEAR(calloc);
1545 
1546 	spdk_blob_close(blob, blob_op_complete, NULL);
1547 	CU_ASSERT(g_bserrno == 0);
1548 
1549 	spdk_bs_free_io_channel(channel);
1550 
1551 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1552 	CU_ASSERT(g_bserrno == 0);
1553 	g_bs = NULL;
1554 }
1555 
1556 static void
1557 blob_rw_iov_read_only(void)
1558 {
1559 	struct spdk_blob_store *bs;
1560 	struct spdk_bs_dev *dev;
1561 	struct spdk_blob *blob;
1562 	struct spdk_io_channel *channel;
1563 	spdk_blob_id blobid;
1564 	uint8_t payload_read[4096];
1565 	uint8_t payload_write[4096];
1566 	struct iovec iov_read;
1567 	struct iovec iov_write;
1568 
1569 	dev = init_dev();
1570 	memset(g_dev_buffer, 0, DEV_BUFFER_SIZE);
1571 
1572 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1573 	CU_ASSERT(g_bserrno == 0);
1574 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1575 	bs = g_bs;
1576 
1577 	channel = spdk_bs_alloc_io_channel(bs);
1578 	CU_ASSERT(channel != NULL);
1579 
1580 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1581 	CU_ASSERT(g_bserrno == 0);
1582 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1583 	blobid = g_blobid;
1584 
1585 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1586 	CU_ASSERT(g_bserrno == 0);
1587 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1588 	blob = g_blob;
1589 
1590 	spdk_blob_resize(blob, 2, blob_op_complete, NULL);
1591 	CU_ASSERT(g_bserrno == 0);
1592 
1593 	/* Verify that writev failed if read_only flag is set. */
1594 	blob->data_ro = true;
1595 	iov_write.iov_base = payload_write;
1596 	iov_write.iov_len = sizeof(payload_write);
1597 	spdk_blob_io_writev(blob, channel, &iov_write, 1, 0, 1, blob_op_complete, NULL);
1598 	CU_ASSERT(g_bserrno == -EPERM);
1599 
1600 	/* Verify that reads pass if data_ro flag is set. */
1601 	iov_read.iov_base = payload_read;
1602 	iov_read.iov_len = sizeof(payload_read);
1603 	spdk_blob_io_readv(blob, channel, &iov_read, 1, 0, 1, blob_op_complete, NULL);
1604 	CU_ASSERT(g_bserrno == 0);
1605 
1606 	spdk_blob_close(blob, blob_op_complete, NULL);
1607 	CU_ASSERT(g_bserrno == 0);
1608 
1609 	spdk_bs_free_io_channel(channel);
1610 
1611 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1612 	CU_ASSERT(g_bserrno == 0);
1613 	g_bs = NULL;
1614 }
1615 
1616 static void
1617 _blob_io_read_no_split(struct spdk_blob *blob, struct spdk_io_channel *channel,
1618 		       uint8_t *payload, uint64_t offset, uint64_t length,
1619 		       spdk_blob_op_complete cb_fn, void *cb_arg)
1620 {
1621 	uint64_t i;
1622 	uint8_t *buf;
1623 	uint64_t page_size = spdk_bs_get_page_size(blob->bs);
1624 
1625 	/* To be sure that operation is NOT splitted, read one page at the time */
1626 	buf = payload;
1627 	for (i = 0; i < length; i++) {
1628 		spdk_blob_io_read(blob, channel, buf, i + offset, 1, blob_op_complete, NULL);
1629 		if (g_bserrno != 0) {
1630 			/* Pass the error code up */
1631 			break;
1632 		}
1633 		buf += page_size;
1634 	}
1635 
1636 	cb_fn(cb_arg, g_bserrno);
1637 }
1638 
1639 static void
1640 _blob_io_write_no_split(struct spdk_blob *blob, struct spdk_io_channel *channel,
1641 			uint8_t *payload, uint64_t offset, uint64_t length,
1642 			spdk_blob_op_complete cb_fn, void *cb_arg)
1643 {
1644 	uint64_t i;
1645 	uint8_t *buf;
1646 	uint64_t page_size = spdk_bs_get_page_size(blob->bs);
1647 
1648 	/* To be sure that operation is NOT splitted, write one page at the time */
1649 	buf = payload;
1650 	for (i = 0; i < length; i++) {
1651 		spdk_blob_io_write(blob, channel, buf, i + offset, 1, blob_op_complete, NULL);
1652 		if (g_bserrno != 0) {
1653 			/* Pass the error code up */
1654 			break;
1655 		}
1656 		buf += page_size;
1657 	}
1658 
1659 	cb_fn(cb_arg, g_bserrno);
1660 }
1661 
1662 static void
1663 blob_operation_split_rw(void)
1664 {
1665 	struct spdk_blob_store *bs;
1666 	struct spdk_bs_dev *dev;
1667 	struct spdk_blob *blob;
1668 	struct spdk_io_channel *channel;
1669 	struct spdk_blob_opts opts;
1670 	spdk_blob_id blobid;
1671 	uint64_t cluster_size;
1672 
1673 	uint64_t payload_size;
1674 	uint8_t *payload_read;
1675 	uint8_t *payload_write;
1676 	uint8_t *payload_pattern;
1677 
1678 	uint64_t page_size;
1679 	uint64_t pages_per_cluster;
1680 	uint64_t pages_per_payload;
1681 
1682 	uint64_t i;
1683 
1684 	dev = init_dev();
1685 
1686 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1687 	CU_ASSERT(g_bserrno == 0);
1688 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1689 	bs = g_bs;
1690 
1691 	cluster_size = spdk_bs_get_cluster_size(bs);
1692 	page_size = spdk_bs_get_page_size(bs);
1693 	pages_per_cluster = cluster_size / page_size;
1694 	pages_per_payload = pages_per_cluster * 5;
1695 	payload_size = cluster_size * 5;
1696 
1697 	payload_read = malloc(payload_size);
1698 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
1699 
1700 	payload_write = malloc(payload_size);
1701 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
1702 
1703 	payload_pattern = malloc(payload_size);
1704 	SPDK_CU_ASSERT_FATAL(payload_pattern != NULL);
1705 
1706 	/* Prepare random pattern to write */
1707 	memset(payload_pattern, 0xFF, payload_size);
1708 	for (i = 0; i < pages_per_payload; i++) {
1709 		*((uint64_t *)(payload_pattern + page_size * i)) = (i + 1);
1710 	}
1711 
1712 	channel = spdk_bs_alloc_io_channel(bs);
1713 	SPDK_CU_ASSERT_FATAL(channel != NULL);
1714 
1715 	/* Create blob */
1716 	spdk_blob_opts_init(&opts);
1717 	opts.thin_provision = false;
1718 	opts.num_clusters = 5;
1719 
1720 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
1721 	CU_ASSERT(g_bserrno == 0);
1722 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1723 	blobid = g_blobid;
1724 
1725 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1726 	CU_ASSERT(g_bserrno == 0);
1727 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1728 	blob = g_blob;
1729 
1730 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
1731 
1732 	/* Initial read should return zeroed payload */
1733 	memset(payload_read, 0xFF, payload_size);
1734 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1735 	CU_ASSERT(g_bserrno == 0);
1736 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
1737 
1738 	/* Fill whole blob except last page */
1739 	spdk_blob_io_write(blob, channel, payload_pattern, 0, pages_per_payload - 1,
1740 			   blob_op_complete, NULL);
1741 	CU_ASSERT(g_bserrno == 0);
1742 
1743 	/* Write last page with a pattern */
1744 	spdk_blob_io_write(blob, channel, payload_pattern, pages_per_payload - 1, 1,
1745 			   blob_op_complete, NULL);
1746 	CU_ASSERT(g_bserrno == 0);
1747 
1748 	/* Read whole blob and check consistency */
1749 	memset(payload_read, 0xFF, payload_size);
1750 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1751 	CU_ASSERT(g_bserrno == 0);
1752 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size - page_size) == 0);
1753 	CU_ASSERT(memcmp(payload_pattern, payload_read + payload_size - page_size, page_size) == 0);
1754 
1755 	/* Fill whole blob except first page */
1756 	spdk_blob_io_write(blob, channel, payload_pattern, 1, pages_per_payload - 1,
1757 			   blob_op_complete, NULL);
1758 	CU_ASSERT(g_bserrno == 0);
1759 
1760 	/* Write first page with a pattern */
1761 	spdk_blob_io_write(blob, channel, payload_pattern, 0, 1,
1762 			   blob_op_complete, NULL);
1763 	CU_ASSERT(g_bserrno == 0);
1764 
1765 	/* Read whole blob and check consistency */
1766 	memset(payload_read, 0xFF, payload_size);
1767 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1768 	CU_ASSERT(g_bserrno == 0);
1769 	CU_ASSERT(memcmp(payload_pattern, payload_read + page_size, payload_size - page_size) == 0);
1770 	CU_ASSERT(memcmp(payload_pattern, payload_read, page_size) == 0);
1771 
1772 
1773 	/* Fill whole blob with a pattern (5 clusters) */
1774 
1775 	/* 1. Read test. */
1776 	_blob_io_write_no_split(blob, channel, payload_pattern, 0, pages_per_payload,
1777 				blob_op_complete, NULL);
1778 	CU_ASSERT(g_bserrno == 0);
1779 
1780 	memset(payload_read, 0xFF, payload_size);
1781 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
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 	CU_ASSERT(g_bserrno == 0);
1789 
1790 	memset(payload_read, 0xFF, payload_size);
1791 	_blob_io_read_no_split(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1792 	CU_ASSERT(g_bserrno == 0);
1793 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1794 
1795 	spdk_blob_close(blob, blob_op_complete, NULL);
1796 	CU_ASSERT(g_bserrno == 0);
1797 
1798 	spdk_bs_free_io_channel(channel);
1799 
1800 	/* Unload the blob store */
1801 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1802 	CU_ASSERT(g_bserrno == 0);
1803 	g_bs = NULL;
1804 	g_blob = NULL;
1805 	g_blobid = 0;
1806 
1807 	free(payload_read);
1808 	free(payload_write);
1809 	free(payload_pattern);
1810 }
1811 
1812 static void
1813 blob_operation_split_rw_iov(void)
1814 {
1815 	struct spdk_blob_store *bs;
1816 	struct spdk_bs_dev *dev;
1817 	struct spdk_blob *blob;
1818 	struct spdk_io_channel *channel;
1819 	struct spdk_blob_opts opts;
1820 	spdk_blob_id blobid;
1821 	uint64_t cluster_size;
1822 
1823 	uint64_t payload_size;
1824 	uint8_t *payload_read;
1825 	uint8_t *payload_write;
1826 	uint8_t *payload_pattern;
1827 
1828 	uint64_t page_size;
1829 	uint64_t pages_per_cluster;
1830 	uint64_t pages_per_payload;
1831 
1832 	struct iovec iov_read[2];
1833 	struct iovec iov_write[2];
1834 
1835 	uint64_t i;
1836 
1837 	dev = init_dev();
1838 
1839 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1840 	CU_ASSERT(g_bserrno == 0);
1841 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1842 	bs = g_bs;
1843 
1844 	cluster_size = spdk_bs_get_cluster_size(bs);
1845 	page_size = spdk_bs_get_page_size(bs);
1846 	pages_per_cluster = cluster_size / page_size;
1847 	pages_per_payload = pages_per_cluster * 5;
1848 	payload_size = cluster_size * 5;
1849 
1850 	payload_read = malloc(payload_size);
1851 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
1852 
1853 	payload_write = malloc(payload_size);
1854 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
1855 
1856 	payload_pattern = malloc(payload_size);
1857 	SPDK_CU_ASSERT_FATAL(payload_pattern != NULL);
1858 
1859 	/* Prepare random pattern to write */
1860 	for (i = 0; i < pages_per_payload; i++) {
1861 		*((uint64_t *)(payload_pattern + page_size * i)) = (i + 1);
1862 	}
1863 
1864 	channel = spdk_bs_alloc_io_channel(bs);
1865 	SPDK_CU_ASSERT_FATAL(channel != NULL);
1866 
1867 	/* Create blob */
1868 	spdk_blob_opts_init(&opts);
1869 	opts.thin_provision = false;
1870 	opts.num_clusters = 5;
1871 
1872 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
1873 	CU_ASSERT(g_bserrno == 0);
1874 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1875 	blobid = g_blobid;
1876 
1877 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1878 	CU_ASSERT(g_bserrno == 0);
1879 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1880 	blob = g_blob;
1881 
1882 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
1883 
1884 	/* Initial read should return zeroes payload */
1885 	memset(payload_read, 0xFF, payload_size);
1886 	iov_read[0].iov_base = payload_read;
1887 	iov_read[0].iov_len = cluster_size * 3;
1888 	iov_read[1].iov_base = payload_read + cluster_size * 3;
1889 	iov_read[1].iov_len = cluster_size * 2;
1890 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1891 	CU_ASSERT(g_bserrno == 0);
1892 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
1893 
1894 	/* First of iovs fills whole blob except last page and second of iovs writes last page
1895 	 *  with a pattern. */
1896 	iov_write[0].iov_base = payload_pattern;
1897 	iov_write[0].iov_len = payload_size - page_size;
1898 	iov_write[1].iov_base = payload_pattern;
1899 	iov_write[1].iov_len = page_size;
1900 	spdk_blob_io_writev(blob, channel, iov_write, 2, 0, pages_per_payload, blob_op_complete, NULL);
1901 	CU_ASSERT(g_bserrno == 0);
1902 
1903 	/* Read whole blob and check consistency */
1904 	memset(payload_read, 0xFF, payload_size);
1905 	iov_read[0].iov_base = payload_read;
1906 	iov_read[0].iov_len = cluster_size * 2;
1907 	iov_read[1].iov_base = payload_read + cluster_size * 2;
1908 	iov_read[1].iov_len = cluster_size * 3;
1909 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1910 	CU_ASSERT(g_bserrno == 0);
1911 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size - page_size) == 0);
1912 	CU_ASSERT(memcmp(payload_pattern, payload_read + payload_size - page_size, page_size) == 0);
1913 
1914 	/* First of iovs fills only first page and second of iovs writes whole blob except
1915 	 *  first page with a pattern. */
1916 	iov_write[0].iov_base = payload_pattern;
1917 	iov_write[0].iov_len = page_size;
1918 	iov_write[1].iov_base = payload_pattern;
1919 	iov_write[1].iov_len = payload_size - page_size;
1920 	spdk_blob_io_writev(blob, channel, iov_write, 2, 0, pages_per_payload, blob_op_complete, NULL);
1921 	CU_ASSERT(g_bserrno == 0);
1922 
1923 	/* Read whole blob and check consistency */
1924 	memset(payload_read, 0xFF, payload_size);
1925 	iov_read[0].iov_base = payload_read;
1926 	iov_read[0].iov_len = cluster_size * 4;
1927 	iov_read[1].iov_base = payload_read + cluster_size * 4;
1928 	iov_read[1].iov_len = cluster_size;
1929 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1930 	CU_ASSERT(g_bserrno == 0);
1931 	CU_ASSERT(memcmp(payload_pattern, payload_read + page_size, payload_size - page_size) == 0);
1932 	CU_ASSERT(memcmp(payload_pattern, payload_read, page_size) == 0);
1933 
1934 
1935 	/* Fill whole blob with a pattern (5 clusters) */
1936 
1937 	/* 1. Read test. */
1938 	_blob_io_write_no_split(blob, channel, payload_pattern, 0, pages_per_payload,
1939 				blob_op_complete, NULL);
1940 	CU_ASSERT(g_bserrno == 0);
1941 
1942 	memset(payload_read, 0xFF, payload_size);
1943 	iov_read[0].iov_base = payload_read;
1944 	iov_read[0].iov_len = cluster_size;
1945 	iov_read[1].iov_base = payload_read + cluster_size;
1946 	iov_read[1].iov_len = cluster_size * 4;
1947 	spdk_blob_io_readv(blob, channel, iov_read, 2, 0, pages_per_payload, blob_op_complete, NULL);
1948 	CU_ASSERT(g_bserrno == 0);
1949 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1950 
1951 	/* 2. Write test. */
1952 	iov_write[0].iov_base = payload_read;
1953 	iov_write[0].iov_len = cluster_size * 2;
1954 	iov_write[1].iov_base = payload_read + cluster_size * 2;
1955 	iov_write[1].iov_len = cluster_size * 3;
1956 	spdk_blob_io_writev(blob, channel, iov_write, 2, 0, pages_per_payload, blob_op_complete, NULL);
1957 	CU_ASSERT(g_bserrno == 0);
1958 
1959 	memset(payload_read, 0xFF, payload_size);
1960 	_blob_io_read_no_split(blob, channel, payload_read, 0, pages_per_payload, blob_op_complete, NULL);
1961 	CU_ASSERT(g_bserrno == 0);
1962 	CU_ASSERT(memcmp(payload_pattern, payload_read, payload_size) == 0);
1963 
1964 	spdk_blob_close(blob, blob_op_complete, NULL);
1965 	CU_ASSERT(g_bserrno == 0);
1966 
1967 	spdk_bs_free_io_channel(channel);
1968 
1969 	/* Unload the blob store */
1970 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1971 	CU_ASSERT(g_bserrno == 0);
1972 	g_bs = NULL;
1973 	g_blob = NULL;
1974 	g_blobid = 0;
1975 
1976 	free(payload_read);
1977 	free(payload_write);
1978 	free(payload_pattern);
1979 }
1980 
1981 static void
1982 blob_unmap(void)
1983 {
1984 	struct spdk_blob_store *bs;
1985 	struct spdk_bs_dev *dev;
1986 	struct spdk_blob *blob;
1987 	struct spdk_io_channel *channel;
1988 	spdk_blob_id blobid;
1989 	struct spdk_blob_opts opts;
1990 	uint8_t payload[4096];
1991 	int i;
1992 
1993 	dev = init_dev();
1994 
1995 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1996 	CU_ASSERT(g_bserrno == 0);
1997 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1998 	bs = g_bs;
1999 
2000 	channel = spdk_bs_alloc_io_channel(bs);
2001 	CU_ASSERT(channel != NULL);
2002 
2003 	spdk_blob_opts_init(&opts);
2004 	opts.num_clusters = 10;
2005 
2006 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
2007 	CU_ASSERT(g_bserrno == 0);
2008 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2009 	blobid = g_blobid;
2010 
2011 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2012 	CU_ASSERT(g_bserrno == 0);
2013 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2014 	blob = g_blob;
2015 
2016 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
2017 	CU_ASSERT(g_bserrno == 0);
2018 
2019 	memset(payload, 0, sizeof(payload));
2020 	payload[0] = 0xFF;
2021 
2022 	/*
2023 	 * Set first byte of every cluster to 0xFF.
2024 	 * First cluster on device is reserved so let's start from cluster number 1
2025 	 */
2026 	for (i = 1; i < 11; i++) {
2027 		g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] = 0xFF;
2028 	}
2029 
2030 	/* Confirm writes */
2031 	for (i = 0; i < 10; i++) {
2032 		payload[0] = 0;
2033 		spdk_blob_io_read(blob, channel, &payload, i * SPDK_BLOB_OPTS_CLUSTER_SZ / 4096, 1,
2034 				  blob_op_complete, NULL);
2035 		CU_ASSERT(g_bserrno == 0);
2036 		CU_ASSERT(payload[0] == 0xFF);
2037 	}
2038 
2039 	/* Mark some clusters as unallocated */
2040 	blob->active.clusters[1] = 0;
2041 	blob->active.clusters[2] = 0;
2042 	blob->active.clusters[3] = 0;
2043 	blob->active.clusters[6] = 0;
2044 	blob->active.clusters[8] = 0;
2045 
2046 	/* Unmap clusters by resizing to 0 */
2047 	spdk_blob_resize(blob, 0, blob_op_complete, NULL);
2048 	CU_ASSERT(g_bserrno == 0);
2049 
2050 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2051 	CU_ASSERT(g_bserrno == 0);
2052 
2053 	/* Confirm that only 'allocated' clusters were unmapped */
2054 	for (i = 1; i < 11; i++) {
2055 		switch (i) {
2056 		case 2:
2057 		case 3:
2058 		case 4:
2059 		case 7:
2060 		case 9:
2061 			CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0xFF);
2062 			break;
2063 		default:
2064 			CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0);
2065 			break;
2066 		}
2067 	}
2068 
2069 	spdk_blob_close(blob, blob_op_complete, NULL);
2070 	CU_ASSERT(g_bserrno == 0);
2071 
2072 	spdk_bs_free_io_channel(channel);
2073 
2074 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2075 	CU_ASSERT(g_bserrno == 0);
2076 	g_bs = NULL;
2077 }
2078 
2079 
2080 static void
2081 blob_iter(void)
2082 {
2083 	struct spdk_blob_store *bs;
2084 	struct spdk_bs_dev *dev;
2085 	struct spdk_blob *blob;
2086 	spdk_blob_id blobid;
2087 
2088 	dev = init_dev();
2089 
2090 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2091 	CU_ASSERT(g_bserrno == 0);
2092 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2093 	bs = g_bs;
2094 
2095 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
2096 	CU_ASSERT(g_blob == NULL);
2097 	CU_ASSERT(g_bserrno == -ENOENT);
2098 
2099 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2100 	CU_ASSERT(g_bserrno == 0);
2101 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2102 	blobid = g_blobid;
2103 
2104 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
2105 	CU_ASSERT(g_blob != NULL);
2106 	CU_ASSERT(g_bserrno == 0);
2107 	blob = g_blob;
2108 	CU_ASSERT(spdk_blob_get_id(blob) == blobid);
2109 
2110 	spdk_bs_iter_next(bs, blob, blob_op_with_handle_complete, NULL);
2111 	CU_ASSERT(g_blob == NULL);
2112 	CU_ASSERT(g_bserrno == -ENOENT);
2113 
2114 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2115 	CU_ASSERT(g_bserrno == 0);
2116 	g_bs = NULL;
2117 }
2118 
2119 static void
2120 blob_xattr(void)
2121 {
2122 	struct spdk_blob_store *bs;
2123 	struct spdk_bs_dev *dev;
2124 	struct spdk_blob *blob;
2125 	spdk_blob_id blobid;
2126 	uint64_t length;
2127 	int rc;
2128 	const char *name1, *name2;
2129 	const void *value;
2130 	size_t value_len;
2131 	struct spdk_xattr_names *names;
2132 
2133 	dev = init_dev();
2134 
2135 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2136 	CU_ASSERT(g_bserrno == 0);
2137 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2138 	bs = g_bs;
2139 
2140 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2141 	CU_ASSERT(g_bserrno == 0);
2142 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2143 	blobid = g_blobid;
2144 
2145 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2146 	CU_ASSERT(g_bserrno == 0);
2147 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2148 	blob = g_blob;
2149 
2150 	/* Test that set_xattr fails if md_ro flag is set. */
2151 	blob->md_ro = true;
2152 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2153 	CU_ASSERT(rc == -EPERM);
2154 
2155 	blob->md_ro = false;
2156 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2157 	CU_ASSERT(rc == 0);
2158 
2159 	length = 2345;
2160 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2161 	CU_ASSERT(rc == 0);
2162 
2163 	/* Overwrite "length" xattr. */
2164 	length = 3456;
2165 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2166 	CU_ASSERT(rc == 0);
2167 
2168 	/* get_xattr should still work even if md_ro flag is set. */
2169 	value = NULL;
2170 	blob->md_ro = true;
2171 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2172 	CU_ASSERT(rc == 0);
2173 	SPDK_CU_ASSERT_FATAL(value != NULL);
2174 	CU_ASSERT(*(uint64_t *)value == length);
2175 	CU_ASSERT(value_len == 8);
2176 	blob->md_ro = false;
2177 
2178 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
2179 	CU_ASSERT(rc == -ENOENT);
2180 
2181 	names = NULL;
2182 	rc = spdk_blob_get_xattr_names(blob, &names);
2183 	CU_ASSERT(rc == 0);
2184 	SPDK_CU_ASSERT_FATAL(names != NULL);
2185 	CU_ASSERT(spdk_xattr_names_get_count(names) == 2);
2186 	name1 = spdk_xattr_names_get_name(names, 0);
2187 	SPDK_CU_ASSERT_FATAL(name1 != NULL);
2188 	CU_ASSERT(!strcmp(name1, "name") || !strcmp(name1, "length"));
2189 	name2 = spdk_xattr_names_get_name(names, 1);
2190 	SPDK_CU_ASSERT_FATAL(name2 != NULL);
2191 	CU_ASSERT(!strcmp(name2, "name") || !strcmp(name2, "length"));
2192 	CU_ASSERT(strcmp(name1, name2));
2193 	spdk_xattr_names_free(names);
2194 
2195 	/* Confirm that remove_xattr fails if md_ro is set to true. */
2196 	blob->md_ro = true;
2197 	rc = spdk_blob_remove_xattr(blob, "name");
2198 	CU_ASSERT(rc == -EPERM);
2199 
2200 	blob->md_ro = false;
2201 	rc = spdk_blob_remove_xattr(blob, "name");
2202 	CU_ASSERT(rc == 0);
2203 
2204 	rc = spdk_blob_remove_xattr(blob, "foobar");
2205 	CU_ASSERT(rc == -ENOENT);
2206 
2207 	/* Set internal xattr */
2208 	length = 7898;
2209 	rc = _spdk_blob_set_xattr(blob, "internal", &length, sizeof(length), true);
2210 	CU_ASSERT(rc == 0);
2211 	rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, true);
2212 	CU_ASSERT(rc == 0);
2213 	CU_ASSERT(*(uint64_t *)value == length);
2214 	/* try to get public xattr with same name */
2215 	rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len);
2216 	CU_ASSERT(rc != 0);
2217 	rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, false);
2218 	CU_ASSERT(rc != 0);
2219 	/* Check if SPDK_BLOB_INTERNAL_XATTR is set */
2220 	CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) ==
2221 		  SPDK_BLOB_INTERNAL_XATTR)
2222 
2223 	spdk_blob_close(blob, blob_op_complete, NULL);
2224 
2225 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2226 
2227 	/* Check if xattrs are persisted */
2228 	dev = init_dev();
2229 
2230 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
2231 	CU_ASSERT(g_bserrno == 0);
2232 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2233 
2234 	bs = g_bs;
2235 
2236 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2237 	CU_ASSERT(g_bserrno == 0);
2238 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2239 	blob = g_blob;
2240 
2241 	rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, true);
2242 	CU_ASSERT(rc == 0);
2243 	CU_ASSERT(*(uint64_t *)value == length);
2244 
2245 	/* try to get internal xattr trough public call */
2246 	rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len);
2247 	CU_ASSERT(rc != 0);
2248 
2249 	rc = _spdk_blob_remove_xattr(blob, "internal", true);
2250 	CU_ASSERT(rc == 0);
2251 
2252 	CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) == 0);
2253 
2254 	CU_ASSERT(g_bserrno == 0);
2255 	g_bs = NULL;
2256 }
2257 
2258 static void
2259 bs_load(void)
2260 {
2261 	struct spdk_bs_dev *dev;
2262 	spdk_blob_id blobid;
2263 	struct spdk_blob *blob;
2264 	struct spdk_bs_super_block *super_block;
2265 	uint64_t length;
2266 	int rc;
2267 	const void *value;
2268 	size_t value_len;
2269 	struct spdk_bs_opts opts;
2270 
2271 	dev = init_dev();
2272 	spdk_bs_opts_init(&opts);
2273 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2274 
2275 	/* Initialize a new blob store */
2276 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2277 	CU_ASSERT(g_bserrno == 0);
2278 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2279 
2280 	/* Try to open a blobid that does not exist */
2281 	spdk_bs_open_blob(g_bs, 0, blob_op_with_handle_complete, NULL);
2282 	CU_ASSERT(g_bserrno == -ENOENT);
2283 	CU_ASSERT(g_blob == NULL);
2284 
2285 	/* Create a blob */
2286 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2287 	CU_ASSERT(g_bserrno == 0);
2288 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2289 	blobid = g_blobid;
2290 
2291 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
2292 	CU_ASSERT(g_bserrno == 0);
2293 	CU_ASSERT(g_blob != NULL);
2294 	blob = g_blob;
2295 
2296 	/* Try again to open valid blob but without the upper bit set */
2297 	spdk_bs_open_blob(g_bs, blobid & 0xFFFFFFFF, blob_op_with_handle_complete, NULL);
2298 	CU_ASSERT(g_bserrno == -ENOENT);
2299 	CU_ASSERT(g_blob == NULL);
2300 
2301 	/* Set some xattrs */
2302 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2303 	CU_ASSERT(rc == 0);
2304 
2305 	length = 2345;
2306 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2307 	CU_ASSERT(rc == 0);
2308 
2309 	/* Resize the blob */
2310 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
2311 	CU_ASSERT(g_bserrno == 0);
2312 
2313 	spdk_blob_close(blob, blob_op_complete, NULL);
2314 	CU_ASSERT(g_bserrno == 0);
2315 	blob = NULL;
2316 	g_blob = NULL;
2317 	g_blobid = SPDK_BLOBID_INVALID;
2318 
2319 	/* Unload the blob store */
2320 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2321 	CU_ASSERT(g_bserrno == 0);
2322 	g_bs = NULL;
2323 	g_blob = NULL;
2324 	g_blobid = 0;
2325 
2326 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2327 	CU_ASSERT(super_block->clean == 1);
2328 
2329 	/* Load should fail for device with an unsupported blocklen */
2330 	dev = init_dev();
2331 	dev->blocklen = SPDK_BS_PAGE_SIZE * 2;
2332 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
2333 	CU_ASSERT(g_bserrno == -EINVAL);
2334 
2335 	/* Load should when max_md_ops is set to zero */
2336 	dev = init_dev();
2337 	spdk_bs_opts_init(&opts);
2338 	opts.max_md_ops = 0;
2339 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2340 	CU_ASSERT(g_bserrno == -EINVAL);
2341 
2342 	/* Load should when max_channel_ops is set to zero */
2343 	dev = init_dev();
2344 	spdk_bs_opts_init(&opts);
2345 	opts.max_channel_ops = 0;
2346 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2347 	CU_ASSERT(g_bserrno == -EINVAL);
2348 
2349 	/* Load an existing blob store */
2350 	dev = init_dev();
2351 	spdk_bs_opts_init(&opts);
2352 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2353 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2354 	CU_ASSERT(g_bserrno == 0);
2355 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2356 
2357 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2358 	CU_ASSERT(super_block->clean == 1);
2359 	CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen);
2360 
2361 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
2362 	CU_ASSERT(g_bserrno == 0);
2363 	CU_ASSERT(g_blob != NULL);
2364 	blob = g_blob;
2365 
2366 	/* Verify that blobstore is marked dirty after first metadata sync */
2367 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2368 	CU_ASSERT(super_block->clean == 1);
2369 
2370 	/* Get the xattrs */
2371 	value = NULL;
2372 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2373 	CU_ASSERT(rc == 0);
2374 	SPDK_CU_ASSERT_FATAL(value != NULL);
2375 	CU_ASSERT(*(uint64_t *)value == length);
2376 	CU_ASSERT(value_len == 8);
2377 
2378 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
2379 	CU_ASSERT(rc == -ENOENT);
2380 
2381 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
2382 
2383 	spdk_blob_close(blob, blob_op_complete, NULL);
2384 	CU_ASSERT(g_bserrno == 0);
2385 	blob = NULL;
2386 	g_blob = NULL;
2387 
2388 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2389 	CU_ASSERT(g_bserrno == 0);
2390 	g_bs = NULL;
2391 
2392 	/* Load should fail: bdev size < saved size */
2393 	dev = init_dev();
2394 	dev->blockcnt /= 2;
2395 
2396 	spdk_bs_opts_init(&opts);
2397 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2398 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2399 
2400 	CU_ASSERT(g_bserrno == -EILSEQ);
2401 
2402 	/* Load should succeed: bdev size > saved size */
2403 	dev = init_dev();
2404 	dev->blockcnt *= 4;
2405 
2406 	spdk_bs_opts_init(&opts);
2407 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2408 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2409 
2410 	CU_ASSERT(g_bserrno == 0);
2411 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2412 
2413 
2414 	/* Test compatibility mode */
2415 
2416 	dev = init_dev();
2417 	super_block->size = 0;
2418 	super_block->crc = _spdk_blob_md_page_calc_crc(super_block);
2419 
2420 	spdk_bs_opts_init(&opts);
2421 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2422 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2423 	CU_ASSERT(g_bserrno == 0);
2424 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2425 
2426 	/* Create a blob */
2427 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2428 	CU_ASSERT(g_bserrno == 0);
2429 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2430 
2431 	/* Blobstore should update number of blocks in super_block */
2432 	CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen);
2433 	CU_ASSERT(super_block->clean == 0);
2434 
2435 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2436 	CU_ASSERT(g_bserrno == 0);
2437 	CU_ASSERT(super_block->clean == 1);
2438 	g_bs = NULL;
2439 
2440 }
2441 
2442 static void
2443 bs_load_custom_cluster_size(void)
2444 {
2445 	struct spdk_bs_dev *dev;
2446 	struct spdk_bs_super_block *super_block;
2447 	struct spdk_bs_opts opts;
2448 	uint32_t custom_cluster_size = 4194304; /* 4MiB */
2449 	uint32_t cluster_sz;
2450 	uint64_t total_clusters;
2451 
2452 	dev = init_dev();
2453 	spdk_bs_opts_init(&opts);
2454 	opts.cluster_sz = custom_cluster_size;
2455 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2456 
2457 	/* Initialize a new blob store */
2458 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2459 	CU_ASSERT(g_bserrno == 0);
2460 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2461 	cluster_sz = g_bs->cluster_sz;
2462 	total_clusters = g_bs->total_clusters;
2463 
2464 	/* Unload the blob store */
2465 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2466 	CU_ASSERT(g_bserrno == 0);
2467 	g_bs = NULL;
2468 	g_blob = NULL;
2469 	g_blobid = 0;
2470 
2471 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2472 	CU_ASSERT(super_block->clean == 1);
2473 
2474 	/* Load an existing blob store */
2475 	dev = init_dev();
2476 	spdk_bs_opts_init(&opts);
2477 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2478 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2479 	CU_ASSERT(g_bserrno == 0);
2480 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2481 	/* Compare cluster size and number to one after initialization */
2482 	CU_ASSERT(cluster_sz == g_bs->cluster_sz);
2483 	CU_ASSERT(total_clusters == g_bs->total_clusters);
2484 
2485 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2486 	CU_ASSERT(super_block->clean == 1);
2487 	CU_ASSERT(super_block->size == dev->blockcnt * dev->blocklen);
2488 
2489 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2490 	CU_ASSERT(g_bserrno == 0);
2491 	CU_ASSERT(super_block->clean == 1);
2492 	g_bs = NULL;
2493 }
2494 
2495 static void
2496 bs_type(void)
2497 {
2498 	struct spdk_bs_dev *dev;
2499 	struct spdk_bs_opts opts;
2500 
2501 	dev = init_dev();
2502 	spdk_bs_opts_init(&opts);
2503 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2504 
2505 	/* Initialize a new blob store */
2506 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2507 	CU_ASSERT(g_bserrno == 0);
2508 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2509 
2510 	/* Unload the blob store */
2511 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2512 	CU_ASSERT(g_bserrno == 0);
2513 	g_bs = NULL;
2514 	g_blob = NULL;
2515 	g_blobid = 0;
2516 
2517 	/* Load non existing blobstore type */
2518 	dev = init_dev();
2519 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING");
2520 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2521 	CU_ASSERT(g_bserrno != 0);
2522 
2523 	/* Load with empty blobstore type */
2524 	dev = init_dev();
2525 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2526 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2527 	CU_ASSERT(g_bserrno == 0);
2528 
2529 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2530 	CU_ASSERT(g_bserrno == 0);
2531 	g_bs = NULL;
2532 
2533 	/* Initialize a new blob store with empty bstype */
2534 	dev = init_dev();
2535 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2536 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2537 	CU_ASSERT(g_bserrno == 0);
2538 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2539 
2540 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2541 	CU_ASSERT(g_bserrno == 0);
2542 	g_bs = NULL;
2543 
2544 	/* Load non existing blobstore type */
2545 	dev = init_dev();
2546 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING");
2547 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2548 	CU_ASSERT(g_bserrno != 0);
2549 
2550 	/* Load with empty blobstore type */
2551 	dev = init_dev();
2552 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2553 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2554 	CU_ASSERT(g_bserrno == 0);
2555 
2556 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2557 	CU_ASSERT(g_bserrno == 0);
2558 	g_bs = NULL;
2559 }
2560 
2561 static void
2562 bs_super_block(void)
2563 {
2564 	struct spdk_bs_dev *dev;
2565 	struct spdk_bs_super_block *super_block;
2566 	struct spdk_bs_opts opts;
2567 	struct spdk_bs_super_block_ver1 super_block_v1;
2568 
2569 	dev = init_dev();
2570 	spdk_bs_opts_init(&opts);
2571 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2572 
2573 	/* Initialize a new blob store */
2574 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2575 	CU_ASSERT(g_bserrno == 0);
2576 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2577 
2578 	/* Unload the blob store */
2579 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2580 	CU_ASSERT(g_bserrno == 0);
2581 	g_bs = NULL;
2582 	g_blob = NULL;
2583 	g_blobid = 0;
2584 
2585 	/* Load an existing blob store with version newer than supported */
2586 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2587 	super_block->version++;
2588 
2589 	dev = init_dev();
2590 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2591 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2592 	CU_ASSERT(g_bserrno != 0);
2593 
2594 	/* Create a new blob store with super block version 1 */
2595 	dev = init_dev();
2596 	super_block_v1.version = 1;
2597 	memcpy(super_block_v1.signature, "SPDKBLOB", sizeof(super_block_v1.signature));
2598 	super_block_v1.length = 0x1000;
2599 	super_block_v1.clean = 1;
2600 	super_block_v1.super_blob = 0xFFFFFFFFFFFFFFFF;
2601 	super_block_v1.cluster_size = 0x100000;
2602 	super_block_v1.used_page_mask_start = 0x01;
2603 	super_block_v1.used_page_mask_len = 0x01;
2604 	super_block_v1.used_cluster_mask_start = 0x02;
2605 	super_block_v1.used_cluster_mask_len = 0x01;
2606 	super_block_v1.md_start = 0x03;
2607 	super_block_v1.md_len = 0x40;
2608 	memset(super_block_v1.reserved, 0, 4036);
2609 	super_block_v1.crc = _spdk_blob_md_page_calc_crc(&super_block_v1);
2610 	memcpy(g_dev_buffer, &super_block_v1, sizeof(struct spdk_bs_super_block_ver1));
2611 
2612 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2613 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2614 	CU_ASSERT(g_bserrno == 0);
2615 
2616 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2617 	CU_ASSERT(g_bserrno == 0);
2618 	g_bs = NULL;
2619 }
2620 
2621 /*
2622  * Create a blobstore and then unload it.
2623  */
2624 static void
2625 bs_unload(void)
2626 {
2627 	struct spdk_bs_dev *dev;
2628 	struct spdk_blob_store *bs;
2629 	spdk_blob_id blobid;
2630 	struct spdk_blob *blob;
2631 
2632 	dev = init_dev();
2633 
2634 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2635 	CU_ASSERT(g_bserrno == 0);
2636 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2637 	bs = g_bs;
2638 
2639 	/* Create a blob and open it. */
2640 	g_bserrno = -1;
2641 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2642 	CU_ASSERT(g_bserrno == 0);
2643 	CU_ASSERT(g_blobid > 0);
2644 	blobid = g_blobid;
2645 
2646 	g_bserrno = -1;
2647 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2648 	CU_ASSERT(g_bserrno == 0);
2649 	CU_ASSERT(g_blob != NULL);
2650 	blob = g_blob;
2651 
2652 	/* Try to unload blobstore, should fail with open blob */
2653 	g_bserrno = -1;
2654 	spdk_bs_unload(bs, bs_op_complete, NULL);
2655 	CU_ASSERT(g_bserrno == -EBUSY);
2656 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2657 
2658 	/* Close the blob, then successfully unload blobstore */
2659 	g_bserrno = -1;
2660 	spdk_blob_close(blob, blob_op_complete, NULL);
2661 	CU_ASSERT(g_bserrno == 0);
2662 
2663 	g_bserrno = -1;
2664 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2665 	CU_ASSERT(g_bserrno == 0);
2666 	g_bs = NULL;
2667 }
2668 
2669 /*
2670  * Create a blobstore with a cluster size different than the default, and ensure it is
2671  *  persisted.
2672  */
2673 static void
2674 bs_cluster_sz(void)
2675 {
2676 	struct spdk_bs_dev *dev;
2677 	struct spdk_bs_opts opts;
2678 	uint32_t cluster_sz;
2679 
2680 	/* Set cluster size to zero */
2681 	dev = init_dev();
2682 	spdk_bs_opts_init(&opts);
2683 	opts.cluster_sz = 0;
2684 
2685 	/* Initialize a new blob store */
2686 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2687 	CU_ASSERT(g_bserrno == -EINVAL);
2688 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
2689 
2690 	/*
2691 	 * Set cluster size to blobstore page size,
2692 	 * to work it is required to be at least twice the blobstore page size.
2693 	 */
2694 	dev = init_dev();
2695 	spdk_bs_opts_init(&opts);
2696 	opts.cluster_sz = SPDK_BS_PAGE_SIZE;
2697 
2698 	/* Initialize a new blob store */
2699 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2700 	CU_ASSERT(g_bserrno == -ENOMEM);
2701 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
2702 
2703 	/*
2704 	 * Set cluster size to lower than page size,
2705 	 * to work it is required to be at least twice the blobstore page size.
2706 	 */
2707 	dev = init_dev();
2708 	spdk_bs_opts_init(&opts);
2709 	opts.cluster_sz = SPDK_BS_PAGE_SIZE - 1;
2710 
2711 	/* Initialize a new blob store */
2712 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2713 	CU_ASSERT(g_bserrno == -EINVAL);
2714 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
2715 
2716 	/* Set cluster size to twice the default */
2717 	dev = init_dev();
2718 	spdk_bs_opts_init(&opts);
2719 	opts.cluster_sz *= 2;
2720 	cluster_sz = opts.cluster_sz;
2721 
2722 	/* Initialize a new blob store */
2723 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2724 	CU_ASSERT(g_bserrno == 0);
2725 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2726 
2727 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2728 
2729 	/* Unload the blob store */
2730 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2731 	CU_ASSERT(g_bserrno == 0);
2732 	g_bs = NULL;
2733 	g_blob = NULL;
2734 	g_blobid = 0;
2735 
2736 	dev = init_dev();
2737 	/* Load an existing blob store */
2738 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2739 	CU_ASSERT(g_bserrno == 0);
2740 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2741 
2742 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2743 
2744 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2745 	CU_ASSERT(g_bserrno == 0);
2746 	g_bs = NULL;
2747 }
2748 
2749 /*
2750  * Create a blobstore, reload it and ensure total usable cluster count
2751  *  stays the same.
2752  */
2753 static void
2754 bs_usable_clusters(void)
2755 {
2756 	struct spdk_bs_dev *dev;
2757 	struct spdk_bs_opts opts;
2758 	uint32_t clusters;
2759 	int i;
2760 
2761 	/* Init blobstore */
2762 	dev = init_dev();
2763 	spdk_bs_opts_init(&opts);
2764 
2765 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2766 	CU_ASSERT(g_bserrno == 0);
2767 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2768 
2769 	clusters = spdk_bs_total_data_cluster_count(g_bs);
2770 
2771 	/* Unload the blob store */
2772 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2773 	CU_ASSERT(g_bserrno == 0);
2774 	g_bs = NULL;
2775 
2776 	dev = init_dev();
2777 	/* Load an existing blob store */
2778 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2779 	CU_ASSERT(g_bserrno == 0);
2780 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2781 
2782 	CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters);
2783 
2784 	/* Create and resize blobs to make sure that useable cluster count won't change */
2785 	for (i = 0; i < 4; i++) {
2786 		g_bserrno = -1;
2787 		g_blobid = SPDK_BLOBID_INVALID;
2788 		spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2789 		CU_ASSERT(g_bserrno == 0);
2790 		CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
2791 
2792 		g_bserrno = -1;
2793 		g_blob = NULL;
2794 		spdk_bs_open_blob(g_bs, g_blobid, blob_op_with_handle_complete, NULL);
2795 		CU_ASSERT(g_bserrno == 0);
2796 		CU_ASSERT(g_blob !=  NULL);
2797 
2798 		spdk_blob_resize(g_blob, 10, blob_op_complete, NULL);
2799 		CU_ASSERT(g_bserrno == 0);
2800 
2801 		g_bserrno = -1;
2802 		spdk_blob_close(g_blob, blob_op_complete, NULL);
2803 		CU_ASSERT(g_bserrno == 0);
2804 
2805 		CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters);
2806 	}
2807 
2808 	/* Reload the blob store to make sure that nothing changed */
2809 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2810 	CU_ASSERT(g_bserrno == 0);
2811 	g_bs = NULL;
2812 
2813 	dev = init_dev();
2814 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2815 	CU_ASSERT(g_bserrno == 0);
2816 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2817 
2818 	CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters);
2819 
2820 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2821 	CU_ASSERT(g_bserrno == 0);
2822 	g_bs = NULL;
2823 }
2824 
2825 /*
2826  * Test resizing of the metadata blob.  This requires creating enough blobs
2827  *  so that one cluster is not enough to fit the metadata for those blobs.
2828  *  To induce this condition to happen more quickly, we reduce the cluster
2829  *  size to 16KB, which means only 4 4KB blob metadata pages can fit.
2830  */
2831 static void
2832 bs_resize_md(void)
2833 {
2834 	const int CLUSTER_PAGE_COUNT = 4;
2835 	const int NUM_BLOBS = CLUSTER_PAGE_COUNT * 4;
2836 	struct spdk_bs_dev *dev;
2837 	struct spdk_bs_opts opts;
2838 	uint32_t cluster_sz;
2839 	spdk_blob_id blobids[NUM_BLOBS];
2840 	int i;
2841 
2842 
2843 	dev = init_dev();
2844 	spdk_bs_opts_init(&opts);
2845 	opts.cluster_sz = CLUSTER_PAGE_COUNT * 4096;
2846 	cluster_sz = opts.cluster_sz;
2847 
2848 	/* Initialize a new blob store */
2849 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2850 	CU_ASSERT(g_bserrno == 0);
2851 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2852 
2853 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2854 
2855 	for (i = 0; i < NUM_BLOBS; i++) {
2856 		g_bserrno = -1;
2857 		g_blobid = SPDK_BLOBID_INVALID;
2858 		spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2859 		CU_ASSERT(g_bserrno == 0);
2860 		CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
2861 		blobids[i] = g_blobid;
2862 	}
2863 
2864 	/* Unload the blob store */
2865 	g_bserrno = -1;
2866 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2867 	CU_ASSERT(g_bserrno == 0);
2868 
2869 	/* Load an existing blob store */
2870 	g_bserrno = -1;
2871 	g_bs = NULL;
2872 	dev = init_dev();
2873 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2874 	CU_ASSERT(g_bserrno == 0);
2875 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2876 
2877 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2878 
2879 	for (i = 0; i < NUM_BLOBS; i++) {
2880 		g_bserrno = -1;
2881 		g_blob = NULL;
2882 		spdk_bs_open_blob(g_bs, blobids[i], blob_op_with_handle_complete, NULL);
2883 		CU_ASSERT(g_bserrno == 0);
2884 		CU_ASSERT(g_blob !=  NULL);
2885 		g_bserrno = -1;
2886 		spdk_blob_close(g_blob, blob_op_complete, NULL);
2887 		CU_ASSERT(g_bserrno == 0);
2888 	}
2889 
2890 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2891 	CU_ASSERT(g_bserrno == 0);
2892 	g_bs = NULL;
2893 }
2894 
2895 static void
2896 bs_destroy(void)
2897 {
2898 	struct spdk_bs_dev *dev;
2899 	struct spdk_bs_opts opts;
2900 
2901 	/* Initialize a new blob store */
2902 	dev = init_dev();
2903 	spdk_bs_opts_init(&opts);
2904 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2905 	CU_ASSERT(g_bserrno == 0);
2906 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2907 
2908 	/* Destroy the blob store */
2909 	g_bserrno = -1;
2910 	spdk_bs_destroy(g_bs, bs_op_complete, NULL);
2911 	CU_ASSERT(g_bserrno == 0);
2912 
2913 	/* Loading an non-existent blob store should fail. */
2914 	g_bs = NULL;
2915 	dev = init_dev();
2916 
2917 	g_bserrno = 0;
2918 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2919 	CU_ASSERT(g_bserrno != 0);
2920 }
2921 
2922 /* Try to hit all of the corner cases associated with serializing
2923  * a blob to disk
2924  */
2925 static void
2926 blob_serialize(void)
2927 {
2928 	struct spdk_bs_dev *dev;
2929 	struct spdk_bs_opts opts;
2930 	struct spdk_blob_store *bs;
2931 	spdk_blob_id blobid[2];
2932 	struct spdk_blob *blob[2];
2933 	uint64_t i;
2934 	char *value;
2935 	int rc;
2936 
2937 	dev = init_dev();
2938 
2939 	/* Initialize a new blobstore with very small clusters */
2940 	spdk_bs_opts_init(&opts);
2941 	opts.cluster_sz = dev->blocklen * 8;
2942 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2943 	CU_ASSERT(g_bserrno == 0);
2944 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2945 	bs = g_bs;
2946 
2947 	/* Create and open two blobs */
2948 	for (i = 0; i < 2; i++) {
2949 		spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2950 		CU_ASSERT(g_bserrno == 0);
2951 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2952 		blobid[i] = g_blobid;
2953 
2954 		/* Open a blob */
2955 		spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL);
2956 		CU_ASSERT(g_bserrno == 0);
2957 		CU_ASSERT(g_blob != NULL);
2958 		blob[i] = g_blob;
2959 
2960 		/* Set a fairly large xattr on both blobs to eat up
2961 		 * metadata space
2962 		 */
2963 		value = calloc(dev->blocklen - 64, sizeof(char));
2964 		SPDK_CU_ASSERT_FATAL(value != NULL);
2965 		memset(value, i, dev->blocklen / 2);
2966 		rc = spdk_blob_set_xattr(blob[i], "name", value, dev->blocklen - 64);
2967 		CU_ASSERT(rc == 0);
2968 		free(value);
2969 	}
2970 
2971 	/* Resize the blobs, alternating 1 cluster at a time.
2972 	 * This thwarts run length encoding and will cause spill
2973 	 * over of the extents.
2974 	 */
2975 	for (i = 0; i < 6; i++) {
2976 		spdk_blob_resize(blob[i % 2], (i / 2) + 1, blob_op_complete, NULL);
2977 		CU_ASSERT(g_bserrno == 0);
2978 	}
2979 
2980 	for (i = 0; i < 2; i++) {
2981 		spdk_blob_sync_md(blob[i], blob_op_complete, NULL);
2982 		CU_ASSERT(g_bserrno == 0);
2983 	}
2984 
2985 	/* Close the blobs */
2986 	for (i = 0; i < 2; i++) {
2987 		spdk_blob_close(blob[i], blob_op_complete, NULL);
2988 		CU_ASSERT(g_bserrno == 0);
2989 	}
2990 
2991 	/* Unload the blobstore */
2992 	spdk_bs_unload(bs, bs_op_complete, NULL);
2993 	CU_ASSERT(g_bserrno == 0);
2994 	g_bs = NULL;
2995 	g_blob = NULL;
2996 	g_blobid = 0;
2997 	bs = NULL;
2998 
2999 	dev = init_dev();
3000 	/* Load an existing blob store */
3001 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3002 	CU_ASSERT(g_bserrno == 0);
3003 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3004 	bs = g_bs;
3005 
3006 	for (i = 0; i < 2; i++) {
3007 		blob[i] = NULL;
3008 
3009 		spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL);
3010 		CU_ASSERT(g_bserrno == 0);
3011 		CU_ASSERT(g_blob != NULL);
3012 		blob[i] = g_blob;
3013 
3014 		CU_ASSERT(spdk_blob_get_num_clusters(blob[i]) == 3);
3015 
3016 		spdk_blob_close(blob[i], blob_op_complete, NULL);
3017 		CU_ASSERT(g_bserrno == 0);
3018 	}
3019 
3020 	spdk_bs_unload(bs, bs_op_complete, NULL);
3021 	CU_ASSERT(g_bserrno == 0);
3022 	g_bs = NULL;
3023 }
3024 
3025 static void
3026 blob_crc(void)
3027 {
3028 	struct spdk_blob_store *bs;
3029 	struct spdk_bs_dev *dev;
3030 	struct spdk_blob *blob;
3031 	spdk_blob_id blobid;
3032 	uint32_t page_num;
3033 	int index;
3034 	struct spdk_blob_md_page *page;
3035 
3036 	dev = init_dev();
3037 
3038 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3039 	CU_ASSERT(g_bserrno == 0);
3040 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3041 	bs = g_bs;
3042 
3043 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
3044 	CU_ASSERT(g_bserrno == 0);
3045 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3046 	blobid = g_blobid;
3047 
3048 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3049 	CU_ASSERT(g_bserrno == 0);
3050 	CU_ASSERT(g_blob != NULL);
3051 	blob = g_blob;
3052 
3053 	spdk_blob_close(blob, blob_op_complete, NULL);
3054 	CU_ASSERT(g_bserrno == 0);
3055 
3056 	page_num = _spdk_bs_blobid_to_page(blobid);
3057 	index = DEV_BUFFER_BLOCKLEN * (bs->md_start + page_num);
3058 	page = (struct spdk_blob_md_page *)&g_dev_buffer[index];
3059 	page->crc = 0;
3060 
3061 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3062 	CU_ASSERT(g_bserrno == -EINVAL);
3063 	CU_ASSERT(g_blob == NULL);
3064 	g_bserrno = 0;
3065 
3066 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3067 	CU_ASSERT(g_bserrno == -EINVAL);
3068 
3069 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3070 	CU_ASSERT(g_bserrno == 0);
3071 	g_bs = NULL;
3072 }
3073 
3074 static void
3075 super_block_crc(void)
3076 {
3077 	struct spdk_bs_dev *dev;
3078 	struct spdk_bs_super_block *super_block;
3079 	struct spdk_bs_opts opts;
3080 
3081 	dev = init_dev();
3082 	spdk_bs_opts_init(&opts);
3083 
3084 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3085 	CU_ASSERT(g_bserrno == 0);
3086 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3087 
3088 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3089 	CU_ASSERT(g_bserrno == 0);
3090 	g_bs = NULL;
3091 
3092 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
3093 	super_block->crc = 0;
3094 	dev = init_dev();
3095 
3096 	/* Load an existing blob store */
3097 	g_bserrno = 0;
3098 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3099 	CU_ASSERT(g_bserrno == -EILSEQ);
3100 }
3101 
3102 /* For blob dirty shutdown test case we do the following sub-test cases:
3103  * 1 Initialize new blob store and create 1 super blob with some xattrs, then we
3104  *   dirty shutdown and reload the blob store and verify the xattrs.
3105  * 2 Resize the blob from 10 clusters to 20 clusters and then dirty shutdown,
3106  *   reload the blob store and verify the clusters number.
3107  * 3 Create the second blob and then dirty shutdown, reload the blob store
3108  *   and verify the second blob.
3109  * 4 Delete the second blob and then dirty shutdown, reload the blob store
3110  *   and verify the second blob is invalid.
3111  * 5 Create the second blob again and also create the third blob, modify the
3112  *   md of second blob which makes the md invalid, and then dirty shutdown,
3113  *   reload the blob store verify the second blob, it should invalid and also
3114  *   verify the third blob, it should correct.
3115  */
3116 static void
3117 blob_dirty_shutdown(void)
3118 {
3119 	int rc;
3120 	int index;
3121 	struct spdk_bs_dev *dev;
3122 	spdk_blob_id blobid1, blobid2, blobid3;
3123 	struct spdk_blob *blob;
3124 	uint64_t length;
3125 	uint64_t free_clusters;
3126 	const void *value;
3127 	size_t value_len;
3128 	uint32_t page_num;
3129 	struct spdk_blob_md_page *page;
3130 	struct spdk_bs_opts opts;
3131 
3132 	dev = init_dev();
3133 	spdk_bs_opts_init(&opts);
3134 	/* Initialize a new blob store */
3135 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3136 	CU_ASSERT(g_bserrno == 0);
3137 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3138 
3139 	/* Create first blob */
3140 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3141 	CU_ASSERT(g_bserrno == 0);
3142 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3143 	blobid1 = g_blobid;
3144 
3145 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
3146 	CU_ASSERT(g_bserrno == 0);
3147 	CU_ASSERT(g_blob != NULL);
3148 	blob = g_blob;
3149 
3150 	/* Set some xattrs */
3151 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
3152 	CU_ASSERT(rc == 0);
3153 
3154 	length = 2345;
3155 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3156 	CU_ASSERT(rc == 0);
3157 
3158 	/* Resize the blob */
3159 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
3160 	CU_ASSERT(g_bserrno == 0);
3161 
3162 	/* Set the blob as the super blob */
3163 	spdk_bs_set_super(g_bs, blobid1, blob_op_complete, NULL);
3164 	CU_ASSERT(g_bserrno == 0);
3165 
3166 	free_clusters = spdk_bs_free_cluster_count(g_bs);
3167 
3168 	spdk_blob_close(blob, blob_op_complete, NULL);
3169 	blob = NULL;
3170 	g_blob = NULL;
3171 	g_blobid = SPDK_BLOBID_INVALID;
3172 
3173 	/* Dirty shutdown */
3174 	_spdk_bs_free(g_bs);
3175 
3176 	/* reload blobstore */
3177 	dev = init_dev();
3178 	spdk_bs_opts_init(&opts);
3179 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3180 	CU_ASSERT(g_bserrno == 0);
3181 
3182 	/* Get the super blob */
3183 	spdk_bs_get_super(g_bs, blob_op_with_id_complete, NULL);
3184 	CU_ASSERT(g_bserrno == 0);
3185 	CU_ASSERT(blobid1 == g_blobid);
3186 
3187 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
3188 	CU_ASSERT(g_bserrno == 0);
3189 	CU_ASSERT(g_blob != NULL);
3190 	blob = g_blob;
3191 
3192 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
3193 
3194 	/* Get the xattrs */
3195 	value = NULL;
3196 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
3197 	CU_ASSERT(rc == 0);
3198 	SPDK_CU_ASSERT_FATAL(value != NULL);
3199 	CU_ASSERT(*(uint64_t *)value == length);
3200 	CU_ASSERT(value_len == 8);
3201 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
3202 
3203 	/* Resize the blob */
3204 	spdk_blob_resize(blob, 20, blob_op_complete, NULL);
3205 	CU_ASSERT(g_bserrno == 0);
3206 
3207 	free_clusters = spdk_bs_free_cluster_count(g_bs);
3208 
3209 	spdk_blob_close(blob, blob_op_complete, NULL);
3210 	CU_ASSERT(g_bserrno == 0);
3211 	blob = NULL;
3212 	g_blob = NULL;
3213 	g_blobid = SPDK_BLOBID_INVALID;
3214 
3215 	/* Dirty shutdown */
3216 	_spdk_bs_free(g_bs);
3217 
3218 	/* reload the blobstore */
3219 	dev = init_dev();
3220 	spdk_bs_opts_init(&opts);
3221 	/* Load an existing blob store */
3222 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3223 	CU_ASSERT(g_bserrno == 0);
3224 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3225 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
3226 	CU_ASSERT(g_bserrno == 0);
3227 	CU_ASSERT(g_blob != NULL);
3228 	blob = g_blob;
3229 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 20);
3230 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
3231 
3232 	spdk_blob_close(blob, blob_op_complete, NULL);
3233 	CU_ASSERT(g_bserrno == 0);
3234 	blob = NULL;
3235 	g_blob = NULL;
3236 	g_blobid = SPDK_BLOBID_INVALID;
3237 
3238 	/* Create second blob */
3239 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3240 	CU_ASSERT(g_bserrno == 0);
3241 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3242 	blobid2 = g_blobid;
3243 
3244 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
3245 	CU_ASSERT(g_bserrno == 0);
3246 	CU_ASSERT(g_blob != NULL);
3247 	blob = g_blob;
3248 
3249 	/* Set some xattrs */
3250 	rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1);
3251 	CU_ASSERT(rc == 0);
3252 
3253 	length = 5432;
3254 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3255 	CU_ASSERT(rc == 0);
3256 
3257 	/* Resize the blob */
3258 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
3259 	CU_ASSERT(g_bserrno == 0);
3260 
3261 	free_clusters = spdk_bs_free_cluster_count(g_bs);
3262 
3263 	spdk_blob_close(blob, blob_op_complete, NULL);
3264 	blob = NULL;
3265 	g_blob = NULL;
3266 	g_blobid = SPDK_BLOBID_INVALID;
3267 
3268 	/* Dirty shutdown */
3269 	_spdk_bs_free(g_bs);
3270 
3271 	/* reload the blobstore */
3272 	dev = init_dev();
3273 	spdk_bs_opts_init(&opts);
3274 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3275 	CU_ASSERT(g_bserrno == 0);
3276 
3277 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
3278 	CU_ASSERT(g_bserrno == 0);
3279 	CU_ASSERT(g_blob != NULL);
3280 	blob = g_blob;
3281 
3282 	/* Get the xattrs */
3283 	value = NULL;
3284 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
3285 	CU_ASSERT(rc == 0);
3286 	SPDK_CU_ASSERT_FATAL(value != NULL);
3287 	CU_ASSERT(*(uint64_t *)value == length);
3288 	CU_ASSERT(value_len == 8);
3289 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
3290 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
3291 
3292 	spdk_blob_close(blob, blob_op_complete, NULL);
3293 	CU_ASSERT(g_bserrno == 0);
3294 	spdk_bs_delete_blob(g_bs, blobid2, blob_op_complete, NULL);
3295 	CU_ASSERT(g_bserrno == 0);
3296 
3297 	free_clusters = spdk_bs_free_cluster_count(g_bs);
3298 
3299 	/* Dirty shutdown */
3300 	_spdk_bs_free(g_bs);
3301 	/* reload the blobstore */
3302 	dev = init_dev();
3303 	spdk_bs_opts_init(&opts);
3304 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3305 	CU_ASSERT(g_bserrno == 0);
3306 
3307 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
3308 	CU_ASSERT(g_bserrno != 0);
3309 	CU_ASSERT(g_blob == NULL);
3310 
3311 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
3312 	CU_ASSERT(g_bserrno == 0);
3313 	CU_ASSERT(g_blob != NULL);
3314 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
3315 	spdk_blob_close(g_blob, blob_op_complete, NULL);
3316 	CU_ASSERT(g_bserrno == 0);
3317 
3318 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3319 	CU_ASSERT(g_bserrno == 0);
3320 	g_bs = NULL;
3321 
3322 	/* reload the blobstore */
3323 	dev = init_dev();
3324 	spdk_bs_opts_init(&opts);
3325 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3326 	CU_ASSERT(g_bserrno == 0);
3327 
3328 	/* Create second blob */
3329 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3330 	CU_ASSERT(g_bserrno == 0);
3331 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3332 	blobid2 = g_blobid;
3333 
3334 	/* Create third blob */
3335 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3336 	CU_ASSERT(g_bserrno == 0);
3337 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3338 	blobid3 = g_blobid;
3339 
3340 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
3341 	CU_ASSERT(g_bserrno == 0);
3342 	CU_ASSERT(g_blob != NULL);
3343 	blob = g_blob;
3344 
3345 	/* Set some xattrs for second blob */
3346 	rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1);
3347 	CU_ASSERT(rc == 0);
3348 
3349 	length = 5432;
3350 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3351 	CU_ASSERT(rc == 0);
3352 
3353 	spdk_blob_close(blob, blob_op_complete, NULL);
3354 	blob = NULL;
3355 	g_blob = NULL;
3356 	g_blobid = SPDK_BLOBID_INVALID;
3357 
3358 	spdk_bs_open_blob(g_bs, blobid3, blob_op_with_handle_complete, NULL);
3359 	CU_ASSERT(g_bserrno == 0);
3360 	CU_ASSERT(g_blob != NULL);
3361 	blob = g_blob;
3362 
3363 	/* Set some xattrs for third blob */
3364 	rc = spdk_blob_set_xattr(blob, "name", "log2.txt", strlen("log2.txt") + 1);
3365 	CU_ASSERT(rc == 0);
3366 
3367 	length = 5432;
3368 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
3369 	CU_ASSERT(rc == 0);
3370 
3371 	spdk_blob_close(blob, blob_op_complete, NULL);
3372 	blob = NULL;
3373 	g_blob = NULL;
3374 	g_blobid = SPDK_BLOBID_INVALID;
3375 
3376 	/* Mark second blob as invalid */
3377 	page_num = _spdk_bs_blobid_to_page(blobid2);
3378 
3379 	index = DEV_BUFFER_BLOCKLEN * (g_bs->md_start + page_num);
3380 	page = (struct spdk_blob_md_page *)&g_dev_buffer[index];
3381 	page->sequence_num = 1;
3382 	page->crc = _spdk_blob_md_page_calc_crc(page);
3383 
3384 	free_clusters = spdk_bs_free_cluster_count(g_bs);
3385 
3386 	/* Dirty shutdown */
3387 	_spdk_bs_free(g_bs);
3388 	/* reload the blobstore */
3389 	dev = init_dev();
3390 	spdk_bs_opts_init(&opts);
3391 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3392 	CU_ASSERT(g_bserrno == 0);
3393 
3394 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
3395 	CU_ASSERT(g_bserrno != 0);
3396 	CU_ASSERT(g_blob == NULL);
3397 
3398 	spdk_bs_open_blob(g_bs, blobid3, blob_op_with_handle_complete, NULL);
3399 	CU_ASSERT(g_bserrno == 0);
3400 	CU_ASSERT(g_blob != NULL);
3401 	blob = g_blob;
3402 
3403 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
3404 
3405 	spdk_blob_close(blob, blob_op_complete, NULL);
3406 	blob = NULL;
3407 	g_blob = NULL;
3408 	g_blobid = SPDK_BLOBID_INVALID;
3409 
3410 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3411 	CU_ASSERT(g_bserrno == 0);
3412 	g_bs = NULL;
3413 }
3414 
3415 static void
3416 blob_flags(void)
3417 {
3418 	struct spdk_bs_dev *dev;
3419 	spdk_blob_id blobid_invalid, blobid_data_ro, blobid_md_ro;
3420 	struct spdk_blob *blob_invalid, *blob_data_ro, *blob_md_ro;
3421 	struct spdk_bs_opts opts;
3422 	int rc;
3423 
3424 	dev = init_dev();
3425 	spdk_bs_opts_init(&opts);
3426 
3427 	/* Initialize a new blob store */
3428 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3429 	CU_ASSERT(g_bserrno == 0);
3430 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3431 
3432 	/* Create three blobs - one each for testing invalid, data_ro and md_ro flags. */
3433 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3434 	CU_ASSERT(g_bserrno == 0);
3435 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3436 	blobid_invalid = g_blobid;
3437 
3438 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3439 	CU_ASSERT(g_bserrno == 0);
3440 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3441 	blobid_data_ro = g_blobid;
3442 
3443 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3444 	CU_ASSERT(g_bserrno == 0);
3445 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3446 	blobid_md_ro = g_blobid;
3447 
3448 	spdk_bs_open_blob(g_bs, blobid_invalid, blob_op_with_handle_complete, NULL);
3449 	CU_ASSERT(g_bserrno == 0);
3450 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3451 	blob_invalid = g_blob;
3452 
3453 	spdk_bs_open_blob(g_bs, blobid_data_ro, blob_op_with_handle_complete, NULL);
3454 	CU_ASSERT(g_bserrno == 0);
3455 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3456 	blob_data_ro = g_blob;
3457 
3458 	spdk_bs_open_blob(g_bs, blobid_md_ro, blob_op_with_handle_complete, NULL);
3459 	CU_ASSERT(g_bserrno == 0);
3460 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3461 	blob_md_ro = g_blob;
3462 
3463 	/* Change the size of blob_data_ro to check if flags are serialized
3464 	 * when blob has non zero number of extents */
3465 	spdk_blob_resize(blob_data_ro, 10, blob_op_complete, NULL);
3466 	CU_ASSERT(g_bserrno == 0);
3467 
3468 	/* Set the xattr to check if flags are serialized
3469 	 * when blob has non zero number of xattrs */
3470 	rc = spdk_blob_set_xattr(blob_md_ro, "name", "log.txt", strlen("log.txt") + 1);
3471 	CU_ASSERT(rc == 0);
3472 
3473 	blob_invalid->invalid_flags = (1ULL << 63);
3474 	blob_invalid->state = SPDK_BLOB_STATE_DIRTY;
3475 	blob_data_ro->data_ro_flags = (1ULL << 62);
3476 	blob_data_ro->state = SPDK_BLOB_STATE_DIRTY;
3477 	blob_md_ro->md_ro_flags = (1ULL << 61);
3478 	blob_md_ro->state = SPDK_BLOB_STATE_DIRTY;
3479 
3480 	g_bserrno = -1;
3481 	spdk_blob_sync_md(blob_invalid, blob_op_complete, NULL);
3482 	CU_ASSERT(g_bserrno == 0);
3483 	g_bserrno = -1;
3484 	spdk_blob_sync_md(blob_data_ro, blob_op_complete, NULL);
3485 	CU_ASSERT(g_bserrno == 0);
3486 	g_bserrno = -1;
3487 	spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL);
3488 	CU_ASSERT(g_bserrno == 0);
3489 
3490 	g_bserrno = -1;
3491 	spdk_blob_close(blob_invalid, blob_op_complete, NULL);
3492 	CU_ASSERT(g_bserrno == 0);
3493 	blob_invalid = NULL;
3494 	g_bserrno = -1;
3495 	spdk_blob_close(blob_data_ro, blob_op_complete, NULL);
3496 	CU_ASSERT(g_bserrno == 0);
3497 	blob_data_ro = NULL;
3498 	g_bserrno = -1;
3499 	spdk_blob_close(blob_md_ro, blob_op_complete, NULL);
3500 	CU_ASSERT(g_bserrno == 0);
3501 	blob_md_ro = NULL;
3502 
3503 	g_blob = NULL;
3504 	g_blobid = SPDK_BLOBID_INVALID;
3505 
3506 	/* Unload the blob store */
3507 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3508 	CU_ASSERT(g_bserrno == 0);
3509 	g_bs = NULL;
3510 
3511 	/* Load an existing blob store */
3512 	dev = init_dev();
3513 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3514 	CU_ASSERT(g_bserrno == 0);
3515 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3516 
3517 	g_blob = NULL;
3518 	g_bserrno = 0;
3519 	spdk_bs_open_blob(g_bs, blobid_invalid, blob_op_with_handle_complete, NULL);
3520 	CU_ASSERT(g_bserrno != 0);
3521 	CU_ASSERT(g_blob == NULL);
3522 
3523 	g_blob = NULL;
3524 	g_bserrno = -1;
3525 	spdk_bs_open_blob(g_bs, blobid_data_ro, blob_op_with_handle_complete, NULL);
3526 	CU_ASSERT(g_bserrno == 0);
3527 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3528 	blob_data_ro = g_blob;
3529 	/* If an unknown data_ro flag was found, the blob should be marked both data and md read-only. */
3530 	CU_ASSERT(blob_data_ro->data_ro == true);
3531 	CU_ASSERT(blob_data_ro->md_ro == true);
3532 	CU_ASSERT(spdk_blob_get_num_clusters(blob_data_ro) == 10);
3533 
3534 	g_blob = NULL;
3535 	g_bserrno = -1;
3536 	spdk_bs_open_blob(g_bs, blobid_md_ro, blob_op_with_handle_complete, NULL);
3537 	CU_ASSERT(g_bserrno == 0);
3538 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3539 	blob_md_ro = g_blob;
3540 	CU_ASSERT(blob_md_ro->data_ro == false);
3541 	CU_ASSERT(blob_md_ro->md_ro == true);
3542 
3543 	g_bserrno = -1;
3544 	spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL);
3545 	CU_ASSERT(g_bserrno == 0);
3546 
3547 	spdk_blob_close(blob_data_ro, blob_op_complete, NULL);
3548 	CU_ASSERT(g_bserrno == 0);
3549 	spdk_blob_close(blob_md_ro, blob_op_complete, NULL);
3550 	CU_ASSERT(g_bserrno == 0);
3551 
3552 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3553 	CU_ASSERT(g_bserrno == 0);
3554 }
3555 
3556 static void
3557 bs_version(void)
3558 {
3559 	struct spdk_bs_super_block *super;
3560 	struct spdk_bs_dev *dev;
3561 	struct spdk_bs_opts opts;
3562 	spdk_blob_id blobid;
3563 
3564 	dev = init_dev();
3565 	spdk_bs_opts_init(&opts);
3566 
3567 	/* Initialize a new blob store */
3568 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3569 	CU_ASSERT(g_bserrno == 0);
3570 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3571 
3572 	/* Unload the blob store */
3573 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3574 	CU_ASSERT(g_bserrno == 0);
3575 	g_bs = NULL;
3576 
3577 	/*
3578 	 * Change the bs version on disk.  This will allow us to
3579 	 *  test that the version does not get modified automatically
3580 	 *  when loading and unloading the blobstore.
3581 	 */
3582 	super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
3583 	CU_ASSERT(super->version == SPDK_BS_VERSION);
3584 	CU_ASSERT(super->clean == 1);
3585 	super->version = 2;
3586 	/*
3587 	 * Version 2 metadata does not have a used blobid mask, so clear
3588 	 *  those fields in the super block and zero the corresponding
3589 	 *  region on "disk".  We will use this to ensure blob IDs are
3590 	 *  correctly reconstructed.
3591 	 */
3592 	memset(&g_dev_buffer[super->used_blobid_mask_start * SPDK_BS_PAGE_SIZE], 0,
3593 	       super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE);
3594 	super->used_blobid_mask_start = 0;
3595 	super->used_blobid_mask_len = 0;
3596 	super->crc = _spdk_blob_md_page_calc_crc(super);
3597 
3598 	/* Load an existing blob store */
3599 	dev = init_dev();
3600 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3601 	CU_ASSERT(g_bserrno == 0);
3602 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3603 	CU_ASSERT(super->clean == 1);
3604 
3605 	/*
3606 	 * Create a blob - just to make sure that when we unload it
3607 	 *  results in writing the super block (since metadata pages
3608 	 *  were allocated.
3609 	 */
3610 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3611 	CU_ASSERT(g_bserrno == 0);
3612 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3613 	blobid = g_blobid;
3614 
3615 	/* Unload the blob store */
3616 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3617 	CU_ASSERT(g_bserrno == 0);
3618 	g_bs = NULL;
3619 	CU_ASSERT(super->version == 2);
3620 	CU_ASSERT(super->used_blobid_mask_start == 0);
3621 	CU_ASSERT(super->used_blobid_mask_len == 0);
3622 
3623 	dev = init_dev();
3624 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3625 	CU_ASSERT(g_bserrno == 0);
3626 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3627 
3628 	g_blob = NULL;
3629 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
3630 	CU_ASSERT(g_bserrno == 0);
3631 	CU_ASSERT(g_blob != NULL);
3632 
3633 	spdk_blob_close(g_blob, blob_op_complete, NULL);
3634 	CU_ASSERT(g_bserrno == 0);
3635 
3636 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3637 	CU_ASSERT(g_bserrno == 0);
3638 	g_bs = NULL;
3639 	CU_ASSERT(super->version == 2);
3640 	CU_ASSERT(super->used_blobid_mask_start == 0);
3641 	CU_ASSERT(super->used_blobid_mask_len == 0);
3642 }
3643 
3644 static void
3645 blob_set_xattrs(void)
3646 {
3647 	struct spdk_blob_store *bs;
3648 	struct spdk_bs_dev *dev;
3649 	struct spdk_blob *blob;
3650 	struct spdk_blob_opts opts;
3651 	spdk_blob_id blobid;
3652 	const void *value;
3653 	size_t value_len;
3654 	int rc;
3655 
3656 	dev = init_dev();
3657 
3658 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3659 	CU_ASSERT(g_bserrno == 0);
3660 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3661 	bs = g_bs;
3662 
3663 	/* Create blob with extra attributes */
3664 	spdk_blob_opts_init(&opts);
3665 
3666 	opts.xattrs.names = g_xattr_names;
3667 	opts.xattrs.get_value = _get_xattr_value;
3668 	opts.xattrs.count = 3;
3669 	opts.xattrs.ctx = &g_ctx;
3670 
3671 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3672 	CU_ASSERT(g_bserrno == 0);
3673 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3674 	blobid = g_blobid;
3675 
3676 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3677 	CU_ASSERT(g_bserrno == 0);
3678 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3679 	blob = g_blob;
3680 
3681 	/* Get the xattrs */
3682 	value = NULL;
3683 
3684 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len);
3685 	CU_ASSERT(rc == 0);
3686 	SPDK_CU_ASSERT_FATAL(value != NULL);
3687 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
3688 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
3689 
3690 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len);
3691 	CU_ASSERT(rc == 0);
3692 	SPDK_CU_ASSERT_FATAL(value != NULL);
3693 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
3694 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
3695 
3696 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len);
3697 	CU_ASSERT(rc == 0);
3698 	SPDK_CU_ASSERT_FATAL(value != NULL);
3699 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
3700 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
3701 
3702 	/* Try to get non existing attribute */
3703 
3704 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
3705 	CU_ASSERT(rc == -ENOENT);
3706 
3707 	spdk_blob_close(blob, blob_op_complete, NULL);
3708 	CU_ASSERT(g_bserrno == 0);
3709 	blob = NULL;
3710 	g_blob = NULL;
3711 	g_blobid = SPDK_BLOBID_INVALID;
3712 
3713 	/* NULL callback */
3714 	spdk_blob_opts_init(&opts);
3715 	opts.xattrs.names = g_xattr_names;
3716 	opts.xattrs.get_value = NULL;
3717 	opts.xattrs.count = 1;
3718 	opts.xattrs.ctx = &g_ctx;
3719 
3720 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3721 	CU_ASSERT(g_bserrno == -EINVAL);
3722 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3723 
3724 	/* NULL values */
3725 	spdk_blob_opts_init(&opts);
3726 	opts.xattrs.names = g_xattr_names;
3727 	opts.xattrs.get_value = _get_xattr_value_null;
3728 	opts.xattrs.count = 1;
3729 	opts.xattrs.ctx = NULL;
3730 
3731 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3732 	CU_ASSERT(g_bserrno == -EINVAL);
3733 
3734 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3735 	CU_ASSERT(g_bserrno == 0);
3736 	g_bs = NULL;
3737 
3738 }
3739 
3740 static void
3741 blob_thin_prov_alloc(void)
3742 {
3743 	struct spdk_blob_store *bs;
3744 	struct spdk_bs_dev *dev;
3745 	struct spdk_blob *blob;
3746 	struct spdk_blob_opts opts;
3747 	spdk_blob_id blobid;
3748 	uint64_t free_clusters;
3749 
3750 	dev = init_dev();
3751 
3752 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3753 	CU_ASSERT(g_bserrno == 0);
3754 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3755 	bs = g_bs;
3756 	free_clusters = spdk_bs_free_cluster_count(bs);
3757 
3758 	/* Set blob as thin provisioned */
3759 	spdk_blob_opts_init(&opts);
3760 	opts.thin_provision = true;
3761 
3762 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3763 	CU_ASSERT(g_bserrno == 0);
3764 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3765 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3766 	blobid = g_blobid;
3767 
3768 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3769 	CU_ASSERT(g_bserrno == 0);
3770 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3771 	blob = g_blob;
3772 
3773 	CU_ASSERT(blob->active.num_clusters == 0);
3774 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
3775 
3776 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
3777 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
3778 	CU_ASSERT(g_bserrno == 0);
3779 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3780 	CU_ASSERT(blob->active.num_clusters == 5);
3781 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
3782 
3783 	/* Grow it to 1TB - still unallocated */
3784 	spdk_blob_resize(blob, 262144, blob_op_complete, NULL);
3785 	CU_ASSERT(g_bserrno == 0);
3786 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3787 	CU_ASSERT(blob->active.num_clusters == 262144);
3788 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 262144);
3789 
3790 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
3791 	CU_ASSERT(g_bserrno == 0);
3792 	/* Sync must not change anything */
3793 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3794 	CU_ASSERT(blob->active.num_clusters == 262144);
3795 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 262144);
3796 	/* Since clusters are not allocated,
3797 	 * number of metadata pages is expected to be minimal.
3798 	 */
3799 	CU_ASSERT(blob->active.num_pages == 1);
3800 
3801 	/* Shrink the blob to 3 clusters - still unallocated */
3802 	spdk_blob_resize(blob, 3, blob_op_complete, NULL);
3803 	CU_ASSERT(g_bserrno == 0);
3804 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3805 	CU_ASSERT(blob->active.num_clusters == 3);
3806 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3);
3807 
3808 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
3809 	CU_ASSERT(g_bserrno == 0);
3810 	/* Sync must not change anything */
3811 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3812 	CU_ASSERT(blob->active.num_clusters == 3);
3813 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3);
3814 
3815 	spdk_blob_close(blob, blob_op_complete, NULL);
3816 	CU_ASSERT(g_bserrno == 0);
3817 
3818 	/* Unload the blob store */
3819 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3820 	CU_ASSERT(g_bserrno == 0);
3821 	g_bs = NULL;
3822 	g_blob = NULL;
3823 	g_blobid = 0;
3824 
3825 	/* Load an existing blob store */
3826 	dev = init_dev();
3827 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3828 	CU_ASSERT(g_bserrno == 0);
3829 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3830 
3831 	bs = g_bs;
3832 
3833 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
3834 	CU_ASSERT(g_bserrno == 0);
3835 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3836 	blob = g_blob;
3837 
3838 	/* Check that clusters allocation and size is still the same */
3839 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3840 	CU_ASSERT(blob->active.num_clusters == 3);
3841 
3842 	spdk_blob_close(blob, blob_op_complete, NULL);
3843 	CU_ASSERT(g_bserrno == 0);
3844 
3845 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3846 	CU_ASSERT(g_bserrno == 0);
3847 
3848 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3849 	CU_ASSERT(g_bserrno == 0);
3850 	g_bs = NULL;
3851 }
3852 
3853 static void
3854 blob_insert_cluster_msg(void)
3855 {
3856 	struct spdk_blob_store *bs;
3857 	struct spdk_bs_dev *dev;
3858 	struct spdk_blob *blob;
3859 	struct spdk_blob_opts opts;
3860 	spdk_blob_id blobid;
3861 	uint64_t free_clusters;
3862 
3863 	dev = init_dev();
3864 
3865 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3866 	CU_ASSERT(g_bserrno == 0);
3867 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3868 	bs = g_bs;
3869 	free_clusters = spdk_bs_free_cluster_count(bs);
3870 
3871 	/* Set blob as thin provisioned */
3872 	spdk_blob_opts_init(&opts);
3873 	opts.thin_provision = true;
3874 	opts.num_clusters = 4;
3875 
3876 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3877 	CU_ASSERT(g_bserrno == 0);
3878 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3879 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3880 	blobid = g_blobid;
3881 
3882 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3883 	CU_ASSERT(g_bserrno == 0);
3884 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3885 	blob = g_blob;
3886 
3887 	CU_ASSERT(blob->active.num_clusters == 4);
3888 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 4);
3889 	CU_ASSERT(blob->active.clusters[1] == 0);
3890 
3891 	_spdk_bs_claim_cluster(bs, 0xF);
3892 	_spdk_blob_insert_cluster_on_md_thread(blob, 1, 0xF, blob_op_complete, NULL);
3893 
3894 	CU_ASSERT(blob->active.clusters[1] != 0);
3895 
3896 	spdk_blob_close(blob, blob_op_complete, NULL);
3897 	CU_ASSERT(g_bserrno == 0);
3898 
3899 	/* Unload the blob store */
3900 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3901 	CU_ASSERT(g_bserrno == 0);
3902 	g_bs = NULL;
3903 	g_blob = NULL;
3904 	g_blobid = 0;
3905 
3906 	/* Load an existing blob store */
3907 	dev = init_dev();
3908 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3909 	CU_ASSERT(g_bserrno == 0);
3910 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3911 
3912 	bs = g_bs;
3913 
3914 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
3915 	CU_ASSERT(g_bserrno == 0);
3916 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3917 	blob = g_blob;
3918 
3919 	CU_ASSERT(blob->active.clusters[1] != 0);
3920 
3921 	spdk_blob_close(blob, blob_op_complete, NULL);
3922 	CU_ASSERT(g_bserrno == 0);
3923 
3924 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3925 	CU_ASSERT(g_bserrno == 0);
3926 
3927 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3928 	CU_ASSERT(g_bserrno == 0);
3929 	g_bs = NULL;
3930 }
3931 
3932 static void
3933 blob_thin_prov_rw(void)
3934 {
3935 	static const uint8_t zero[10 * 4096] = { 0 };
3936 	struct spdk_blob_store *bs;
3937 	struct spdk_bs_dev *dev;
3938 	struct spdk_blob *blob;
3939 	struct spdk_io_channel *channel;
3940 	struct spdk_blob_opts opts;
3941 	spdk_blob_id blobid;
3942 	uint64_t free_clusters;
3943 	uint64_t page_size;
3944 	uint8_t payload_read[10 * 4096];
3945 	uint8_t payload_write[10 * 4096];
3946 	uint64_t write_bytes;
3947 	uint64_t read_bytes;
3948 
3949 	dev = init_dev();
3950 
3951 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3952 	CU_ASSERT(g_bserrno == 0);
3953 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3954 	bs = g_bs;
3955 	free_clusters = spdk_bs_free_cluster_count(bs);
3956 	page_size = spdk_bs_get_page_size(bs);
3957 
3958 	channel = spdk_bs_alloc_io_channel(bs);
3959 	CU_ASSERT(channel != NULL);
3960 
3961 	spdk_blob_opts_init(&opts);
3962 	opts.thin_provision = true;
3963 
3964 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3965 	CU_ASSERT(g_bserrno == 0);
3966 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3967 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3968 	blobid = g_blobid;
3969 
3970 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3971 	CU_ASSERT(g_bserrno == 0);
3972 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3973 	blob = g_blob;
3974 
3975 	CU_ASSERT(blob->active.num_clusters == 0);
3976 
3977 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
3978 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
3979 	CU_ASSERT(g_bserrno == 0);
3980 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3981 	CU_ASSERT(blob->active.num_clusters == 5);
3982 
3983 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
3984 	CU_ASSERT(g_bserrno == 0);
3985 	/* Sync must not change anything */
3986 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3987 	CU_ASSERT(blob->active.num_clusters == 5);
3988 
3989 	/* Payload should be all zeros from unallocated clusters */
3990 	memset(payload_read, 0xFF, sizeof(payload_read));
3991 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
3992 	CU_ASSERT(g_bserrno == 0);
3993 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
3994 
3995 	write_bytes = g_dev_write_bytes;
3996 	read_bytes = g_dev_read_bytes;
3997 
3998 	memset(payload_write, 0xE5, sizeof(payload_write));
3999 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4000 	CU_ASSERT(g_bserrno == 0);
4001 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4002 	/* For thin-provisioned blob we need to write 10 pages plus one page metadata and
4003 	 * read 0 bytes */
4004 	CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 11);
4005 	CU_ASSERT(g_dev_read_bytes - read_bytes == 0);
4006 
4007 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4008 	CU_ASSERT(g_bserrno == 0);
4009 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4010 
4011 	spdk_blob_close(blob, blob_op_complete, NULL);
4012 	CU_ASSERT(g_bserrno == 0);
4013 
4014 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
4015 	CU_ASSERT(g_bserrno == 0);
4016 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4017 
4018 	spdk_bs_free_io_channel(channel);
4019 
4020 	/* Unload the blob store */
4021 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4022 	CU_ASSERT(g_bserrno == 0);
4023 	g_bs = NULL;
4024 	g_blob = NULL;
4025 	g_blobid = 0;
4026 }
4027 
4028 static void
4029 blob_thin_prov_rw_iov(void)
4030 {
4031 	static const uint8_t zero[10 * 4096] = { 0 };
4032 	struct spdk_blob_store *bs;
4033 	struct spdk_bs_dev *dev;
4034 	struct spdk_blob *blob;
4035 	struct spdk_io_channel *channel;
4036 	struct spdk_blob_opts opts;
4037 	spdk_blob_id blobid;
4038 	uint64_t free_clusters;
4039 	uint8_t payload_read[10 * 4096];
4040 	uint8_t payload_write[10 * 4096];
4041 	struct iovec iov_read[3];
4042 	struct iovec iov_write[3];
4043 
4044 	dev = init_dev();
4045 
4046 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
4047 	CU_ASSERT(g_bserrno == 0);
4048 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4049 	bs = g_bs;
4050 	free_clusters = spdk_bs_free_cluster_count(bs);
4051 
4052 	channel = spdk_bs_alloc_io_channel(bs);
4053 	CU_ASSERT(channel != NULL);
4054 
4055 	spdk_blob_opts_init(&opts);
4056 	opts.thin_provision = true;
4057 
4058 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4059 	CU_ASSERT(g_bserrno == 0);
4060 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4061 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4062 	blobid = g_blobid;
4063 
4064 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4065 	CU_ASSERT(g_bserrno == 0);
4066 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4067 	blob = g_blob;
4068 
4069 	CU_ASSERT(blob->active.num_clusters == 0);
4070 
4071 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
4072 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
4073 	CU_ASSERT(g_bserrno == 0);
4074 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4075 	CU_ASSERT(blob->active.num_clusters == 5);
4076 
4077 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4078 	CU_ASSERT(g_bserrno == 0);
4079 	/* Sync must not change anything */
4080 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4081 	CU_ASSERT(blob->active.num_clusters == 5);
4082 
4083 	/* Payload should be all zeros from unallocated clusters */
4084 	memset(payload_read, 0xAA, sizeof(payload_read));
4085 	iov_read[0].iov_base = payload_read;
4086 	iov_read[0].iov_len = 3 * 4096;
4087 	iov_read[1].iov_base = payload_read + 3 * 4096;
4088 	iov_read[1].iov_len = 4 * 4096;
4089 	iov_read[2].iov_base = payload_read + 7 * 4096;
4090 	iov_read[2].iov_len = 3 * 4096;
4091 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4092 	CU_ASSERT(g_bserrno == 0);
4093 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4094 
4095 	memset(payload_write, 0xE5, sizeof(payload_write));
4096 	iov_write[0].iov_base = payload_write;
4097 	iov_write[0].iov_len = 1 * 4096;
4098 	iov_write[1].iov_base = payload_write + 1 * 4096;
4099 	iov_write[1].iov_len = 5 * 4096;
4100 	iov_write[2].iov_base = payload_write + 6 * 4096;
4101 	iov_write[2].iov_len = 4 * 4096;
4102 
4103 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
4104 	CU_ASSERT(g_bserrno == 0);
4105 
4106 	memset(payload_read, 0xAA, sizeof(payload_read));
4107 	iov_read[0].iov_base = payload_read;
4108 	iov_read[0].iov_len = 3 * 4096;
4109 	iov_read[1].iov_base = payload_read + 3 * 4096;
4110 	iov_read[1].iov_len = 4 * 4096;
4111 	iov_read[2].iov_base = payload_read + 7 * 4096;
4112 	iov_read[2].iov_len = 3 * 4096;
4113 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4114 	CU_ASSERT(g_bserrno == 0);
4115 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4116 
4117 	spdk_blob_close(blob, blob_op_complete, NULL);
4118 	CU_ASSERT(g_bserrno == 0);
4119 
4120 	spdk_bs_free_io_channel(channel);
4121 
4122 	/* Unload the blob store */
4123 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4124 	CU_ASSERT(g_bserrno == 0);
4125 	g_bs = NULL;
4126 	g_blob = NULL;
4127 	g_blobid = 0;
4128 }
4129 
4130 struct iter_ctx {
4131 	int		current_iter;
4132 	spdk_blob_id	blobid[4];
4133 };
4134 
4135 static void
4136 test_iter(void *arg, struct spdk_blob *blob, int bserrno)
4137 {
4138 	struct iter_ctx *iter_ctx = arg;
4139 	spdk_blob_id blobid;
4140 
4141 	CU_ASSERT(bserrno == 0);
4142 	blobid = spdk_blob_get_id(blob);
4143 	CU_ASSERT(blobid == iter_ctx->blobid[iter_ctx->current_iter++]);
4144 }
4145 
4146 static void
4147 bs_load_iter(void)
4148 {
4149 	struct spdk_bs_dev *dev;
4150 	struct iter_ctx iter_ctx = { 0 };
4151 	struct spdk_blob *blob;
4152 	int i, rc;
4153 	struct spdk_bs_opts opts;
4154 
4155 	dev = init_dev();
4156 	spdk_bs_opts_init(&opts);
4157 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4158 
4159 	/* Initialize a new blob store */
4160 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
4161 	CU_ASSERT(g_bserrno == 0);
4162 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4163 
4164 	for (i = 0; i < 4; i++) {
4165 		g_bserrno = -1;
4166 		g_blobid = SPDK_BLOBID_INVALID;
4167 		spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
4168 		CU_ASSERT(g_bserrno == 0);
4169 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4170 		iter_ctx.blobid[i] = g_blobid;
4171 
4172 		g_bserrno = -1;
4173 		g_blob = NULL;
4174 		spdk_bs_open_blob(g_bs, g_blobid, blob_op_with_handle_complete, NULL);
4175 		CU_ASSERT(g_bserrno == 0);
4176 		CU_ASSERT(g_blob != NULL);
4177 		blob = g_blob;
4178 
4179 		/* Just save the blobid as an xattr for testing purposes. */
4180 		rc = spdk_blob_set_xattr(blob, "blobid", &g_blobid, sizeof(g_blobid));
4181 		CU_ASSERT(rc == 0);
4182 
4183 		/* Resize the blob */
4184 		spdk_blob_resize(blob, i, blob_op_complete, NULL);
4185 		CU_ASSERT(g_bserrno == 0);
4186 
4187 		spdk_blob_close(blob, blob_op_complete, NULL);
4188 		CU_ASSERT(g_bserrno == 0);
4189 	}
4190 
4191 	g_bserrno = -1;
4192 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4193 	CU_ASSERT(g_bserrno == 0);
4194 
4195 	dev = init_dev();
4196 	spdk_bs_opts_init(&opts);
4197 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4198 	opts.iter_cb_fn = test_iter;
4199 	opts.iter_cb_arg = &iter_ctx;
4200 
4201 	/* Test blob iteration during load after a clean shutdown. */
4202 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
4203 	CU_ASSERT(g_bserrno == 0);
4204 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4205 
4206 	/* Dirty shutdown */
4207 	_spdk_bs_free(g_bs);
4208 
4209 	dev = init_dev();
4210 	spdk_bs_opts_init(&opts);
4211 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
4212 	opts.iter_cb_fn = test_iter;
4213 	iter_ctx.current_iter = 0;
4214 	opts.iter_cb_arg = &iter_ctx;
4215 
4216 	/* Test blob iteration during load after a dirty shutdown. */
4217 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
4218 	CU_ASSERT(g_bserrno == 0);
4219 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4220 
4221 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4222 	CU_ASSERT(g_bserrno == 0);
4223 	g_bs = NULL;
4224 }
4225 
4226 static void
4227 blob_snapshot_rw(void)
4228 {
4229 	static const uint8_t zero[10 * 4096] = { 0 };
4230 	struct spdk_blob_store *bs;
4231 	struct spdk_bs_dev *dev;
4232 	struct spdk_blob *blob, *snapshot;
4233 	struct spdk_io_channel *channel;
4234 	struct spdk_blob_opts opts;
4235 	spdk_blob_id blobid, snapshotid;
4236 	uint64_t free_clusters;
4237 	uint64_t cluster_size;
4238 	uint64_t page_size;
4239 	uint8_t payload_read[10 * 4096];
4240 	uint8_t payload_write[10 * 4096];
4241 	uint64_t write_bytes;
4242 	uint64_t read_bytes;
4243 
4244 	dev = init_dev();
4245 
4246 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
4247 	CU_ASSERT(g_bserrno == 0);
4248 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4249 	bs = g_bs;
4250 	free_clusters = spdk_bs_free_cluster_count(bs);
4251 	cluster_size = spdk_bs_get_cluster_size(bs);
4252 	page_size = spdk_bs_get_page_size(bs);
4253 
4254 	channel = spdk_bs_alloc_io_channel(bs);
4255 	CU_ASSERT(channel != NULL);
4256 
4257 	spdk_blob_opts_init(&opts);
4258 	opts.thin_provision = true;
4259 	opts.num_clusters = 5;
4260 
4261 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4262 	CU_ASSERT(g_bserrno == 0);
4263 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4264 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4265 	blobid = g_blobid;
4266 
4267 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4268 	CU_ASSERT(g_bserrno == 0);
4269 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4270 	blob = g_blob;
4271 
4272 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4273 
4274 	memset(payload_read, 0xFF, sizeof(payload_read));
4275 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4276 	CU_ASSERT(g_bserrno == 0);
4277 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4278 
4279 	memset(payload_write, 0xE5, sizeof(payload_write));
4280 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4281 	CU_ASSERT(g_bserrno == 0);
4282 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4283 
4284 	/* Create snapshot from blob */
4285 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4286 	CU_ASSERT(g_bserrno == 0);
4287 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4288 	snapshotid = g_blobid;
4289 
4290 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4291 	CU_ASSERT(g_bserrno == 0);
4292 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4293 	snapshot = g_blob;
4294 	CU_ASSERT(snapshot->data_ro == true)
4295 	CU_ASSERT(snapshot->md_ro == true)
4296 
4297 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5)
4298 
4299 	write_bytes = g_dev_write_bytes;
4300 	read_bytes = g_dev_read_bytes;
4301 
4302 	memset(payload_write, 0xAA, sizeof(payload_write));
4303 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
4304 	CU_ASSERT(g_bserrno == 0);
4305 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4306 
4307 	/* For a clone we need to allocate and copy one cluster, update one page of metadata
4308 	 * and then write 10 pages of payload.
4309 	 */
4310 	CU_ASSERT(g_dev_write_bytes - write_bytes == page_size * 11 + cluster_size);
4311 	CU_ASSERT(g_dev_read_bytes - read_bytes == cluster_size);
4312 
4313 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
4314 	CU_ASSERT(g_bserrno == 0);
4315 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4316 
4317 	/* Data on snapshot should not change after write to clone */
4318 	memset(payload_write, 0xE5, sizeof(payload_write));
4319 	spdk_blob_io_read(snapshot, channel, payload_read, 4, 10, blob_op_complete, NULL);
4320 	CU_ASSERT(g_bserrno == 0);
4321 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4322 
4323 	spdk_blob_close(blob, blob_op_complete, NULL);
4324 	CU_ASSERT(g_bserrno == 0);
4325 
4326 	spdk_blob_close(snapshot, blob_op_complete, NULL);
4327 	CU_ASSERT(g_bserrno == 0);
4328 
4329 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
4330 	CU_ASSERT(g_bserrno == 0);
4331 
4332 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4333 	CU_ASSERT(g_bserrno == 0);
4334 
4335 	spdk_bs_free_io_channel(channel);
4336 
4337 	/* Unload the blob store */
4338 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4339 	CU_ASSERT(g_bserrno == 0);
4340 	g_bs = NULL;
4341 	g_blob = NULL;
4342 	g_blobid = 0;
4343 }
4344 
4345 static void
4346 blob_snapshot_rw_iov(void)
4347 {
4348 	static const uint8_t zero[10 * 4096] = { 0 };
4349 	struct spdk_blob_store *bs;
4350 	struct spdk_bs_dev *dev;
4351 	struct spdk_blob *blob, *snapshot;
4352 	struct spdk_io_channel *channel;
4353 	struct spdk_blob_opts opts;
4354 	spdk_blob_id blobid, snapshotid;
4355 	uint64_t free_clusters;
4356 	uint8_t payload_read[10 * 4096];
4357 	uint8_t payload_write[10 * 4096];
4358 	struct iovec iov_read[3];
4359 	struct iovec iov_write[3];
4360 
4361 	dev = init_dev();
4362 
4363 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
4364 	CU_ASSERT(g_bserrno == 0);
4365 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4366 	bs = g_bs;
4367 	free_clusters = spdk_bs_free_cluster_count(bs);
4368 
4369 	channel = spdk_bs_alloc_io_channel(bs);
4370 	CU_ASSERT(channel != NULL);
4371 
4372 	spdk_blob_opts_init(&opts);
4373 	opts.thin_provision = true;
4374 	opts.num_clusters = 5;
4375 
4376 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4377 	CU_ASSERT(g_bserrno == 0);
4378 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4379 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4380 	blobid = g_blobid;
4381 
4382 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4383 	CU_ASSERT(g_bserrno == 0);
4384 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4385 	blob = g_blob;
4386 
4387 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4388 
4389 	/* Create snapshot from blob */
4390 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4391 	CU_ASSERT(g_bserrno == 0);
4392 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4393 	snapshotid = g_blobid;
4394 
4395 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4396 	CU_ASSERT(g_bserrno == 0);
4397 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4398 	snapshot = g_blob;
4399 	CU_ASSERT(snapshot->data_ro == true)
4400 	CU_ASSERT(snapshot->md_ro == true)
4401 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
4402 
4403 	/* Payload should be all zeros from unallocated clusters */
4404 	memset(payload_read, 0xAA, sizeof(payload_read));
4405 	iov_read[0].iov_base = payload_read;
4406 	iov_read[0].iov_len = 3 * 4096;
4407 	iov_read[1].iov_base = payload_read + 3 * 4096;
4408 	iov_read[1].iov_len = 4 * 4096;
4409 	iov_read[2].iov_base = payload_read + 7 * 4096;
4410 	iov_read[2].iov_len = 3 * 4096;
4411 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4412 	CU_ASSERT(g_bserrno == 0);
4413 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
4414 
4415 	memset(payload_write, 0xE5, sizeof(payload_write));
4416 	iov_write[0].iov_base = payload_write;
4417 	iov_write[0].iov_len = 1 * 4096;
4418 	iov_write[1].iov_base = payload_write + 1 * 4096;
4419 	iov_write[1].iov_len = 5 * 4096;
4420 	iov_write[2].iov_base = payload_write + 6 * 4096;
4421 	iov_write[2].iov_len = 4 * 4096;
4422 
4423 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
4424 	CU_ASSERT(g_bserrno == 0);
4425 
4426 	memset(payload_read, 0xAA, sizeof(payload_read));
4427 	iov_read[0].iov_base = payload_read;
4428 	iov_read[0].iov_len = 3 * 4096;
4429 	iov_read[1].iov_base = payload_read + 3 * 4096;
4430 	iov_read[1].iov_len = 4 * 4096;
4431 	iov_read[2].iov_base = payload_read + 7 * 4096;
4432 	iov_read[2].iov_len = 3 * 4096;
4433 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
4434 	CU_ASSERT(g_bserrno == 0);
4435 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
4436 
4437 	spdk_blob_close(blob, blob_op_complete, NULL);
4438 	CU_ASSERT(g_bserrno == 0);
4439 
4440 	spdk_blob_close(snapshot, blob_op_complete, NULL);
4441 	CU_ASSERT(g_bserrno == 0);
4442 
4443 	spdk_bs_free_io_channel(channel);
4444 
4445 	/* Unload the blob store */
4446 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4447 	CU_ASSERT(g_bserrno == 0);
4448 	g_bs = NULL;
4449 	g_blob = NULL;
4450 	g_blobid = 0;
4451 }
4452 
4453 /**
4454  * Inflate / decouple parent rw unit tests.
4455  *
4456  * --------------
4457  * original blob:         0         1         2         3         4
4458  *                   ,---------+---------+---------+---------+---------.
4459  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
4460  *                   +---------+---------+---------+---------+---------+
4461  *         snapshot2 |    -    |yyyyyyyyy|    -    |yyyyyyyyy|    -    |
4462  *                   +---------+---------+---------+---------+---------+
4463  *         blob      |    -    |zzzzzzzzz|    -    |    -    |    -    |
4464  *                   '---------+---------+---------+---------+---------'
4465  *                   .         .         .         .         .         .
4466  * --------          .         .         .         .         .         .
4467  * inflate:          .         .         .         .         .         .
4468  *                   ,---------+---------+---------+---------+---------.
4469  *         blob      |xxxxxxxxx|zzzzzzzzz|xxxxxxxxx|yyyyyyyyy|000000000|
4470  *                   '---------+---------+---------+---------+---------'
4471  *
4472  *         NOTE: needs to allocate 4 clusters, thin provisioning removed, dependency
4473  *               on snapshot2 and snapshot removed .         .         .
4474  *                   .         .         .         .         .         .
4475  * ----------------  .         .         .         .         .         .
4476  * decouple parent:  .         .         .         .         .         .
4477  *                   ,---------+---------+---------+---------+---------.
4478  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
4479  *                   +---------+---------+---------+---------+---------+
4480  *         blob      |    -    |zzzzzzzzz|    -    |yyyyyyyyy|    -    |
4481  *                   '---------+---------+---------+---------+---------'
4482  *
4483  *         NOTE: needs to allocate 1 cluster, 3 clusters unallocated, dependency
4484  *               on snapshot2 removed and on snapshot still exists. Snapshot2
4485  *               should remain a clone of snapshot.
4486  */
4487 static void
4488 _blob_inflate_rw(bool decouple_parent)
4489 {
4490 	struct spdk_blob_store *bs;
4491 	struct spdk_bs_dev *dev;
4492 	struct spdk_blob *blob, *snapshot, *snapshot2;
4493 	struct spdk_io_channel *channel;
4494 	struct spdk_blob_opts opts;
4495 	spdk_blob_id blobid, snapshotid, snapshot2id;
4496 	uint64_t free_clusters;
4497 	uint64_t cluster_size;
4498 
4499 	uint64_t payload_size;
4500 	uint8_t *payload_read;
4501 	uint8_t *payload_write;
4502 	uint8_t *payload_clone;
4503 
4504 	uint64_t pages_per_cluster;
4505 	uint64_t pages_per_payload;
4506 
4507 	int i;
4508 	spdk_blob_id ids[2];
4509 	size_t count;
4510 
4511 	dev = init_dev();
4512 
4513 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
4514 	CU_ASSERT(g_bserrno == 0);
4515 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4516 	bs = g_bs;
4517 
4518 	free_clusters = spdk_bs_free_cluster_count(bs);
4519 	cluster_size = spdk_bs_get_cluster_size(bs);
4520 	pages_per_cluster = cluster_size / spdk_bs_get_page_size(bs);
4521 	pages_per_payload = pages_per_cluster * 5;
4522 
4523 	payload_size = cluster_size * 5;
4524 
4525 	payload_read = malloc(payload_size);
4526 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
4527 
4528 	payload_write = malloc(payload_size);
4529 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
4530 
4531 	payload_clone = malloc(payload_size);
4532 	SPDK_CU_ASSERT_FATAL(payload_clone != NULL);
4533 
4534 	channel = spdk_bs_alloc_io_channel(bs);
4535 	SPDK_CU_ASSERT_FATAL(channel != NULL);
4536 
4537 	/* Create blob */
4538 	spdk_blob_opts_init(&opts);
4539 	opts.thin_provision = true;
4540 	opts.num_clusters = 5;
4541 
4542 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4543 	CU_ASSERT(g_bserrno == 0);
4544 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4545 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4546 	blobid = g_blobid;
4547 
4548 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4549 	CU_ASSERT(g_bserrno == 0);
4550 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4551 	blob = g_blob;
4552 
4553 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4554 
4555 	/* 1) Initial read should return zeroed payload */
4556 	memset(payload_read, 0xFF, payload_size);
4557 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4558 			  blob_op_complete, NULL);
4559 	CU_ASSERT(g_bserrno == 0);
4560 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
4561 
4562 	/* Fill whole blob with a pattern, except last cluster (to be sure it
4563 	 * isn't allocated) */
4564 	memset(payload_write, 0xE5, payload_size - cluster_size);
4565 	spdk_blob_io_write(blob, channel, payload_write, 0, pages_per_payload -
4566 			   pages_per_cluster, blob_op_complete, NULL);
4567 	CU_ASSERT(g_bserrno == 0);
4568 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4569 
4570 	/* 2) Create snapshot from blob (first level) */
4571 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4572 	CU_ASSERT(g_bserrno == 0);
4573 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4574 	snapshotid = g_blobid;
4575 
4576 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4577 	CU_ASSERT(g_bserrno == 0);
4578 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4579 	snapshot = g_blob;
4580 	CU_ASSERT(snapshot->data_ro == true)
4581 	CU_ASSERT(snapshot->md_ro == true)
4582 
4583 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5)
4584 
4585 	/* Write every second cluster with a pattern.
4586 	 *
4587 	 * Last cluster shouldn't be written, to be sure that snapshot nor clone
4588 	 * doesn't allocate it.
4589 	 *
4590 	 * payload_clone stores expected result on "blob" read at the time and
4591 	 * is used only to check data consistency on clone before and after
4592 	 * inflation. Initially we fill it with a backing snapshots pattern
4593 	 * used before.
4594 	 */
4595 	memset(payload_clone, 0xE5, payload_size - cluster_size);
4596 	memset(payload_clone + payload_size - cluster_size, 0x00, cluster_size);
4597 	memset(payload_write, 0xAA, payload_size);
4598 	for (i = 1; i < 5; i += 2) {
4599 		spdk_blob_io_write(blob, channel, payload_write, i * pages_per_cluster,
4600 				   pages_per_cluster, blob_op_complete, NULL);
4601 		CU_ASSERT(g_bserrno == 0);
4602 
4603 		/* Update expected result */
4604 		memcpy(payload_clone + (cluster_size * i), payload_write,
4605 		       cluster_size);
4606 	}
4607 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4608 
4609 	/* Check data consistency on clone */
4610 	memset(payload_read, 0xFF, payload_size);
4611 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4612 			  blob_op_complete, NULL);
4613 	CU_ASSERT(g_bserrno == 0);
4614 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
4615 
4616 	/* 3) Create second levels snapshot from blob */
4617 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4618 	CU_ASSERT(g_bserrno == 0);
4619 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4620 	snapshot2id = g_blobid;
4621 
4622 	spdk_bs_open_blob(bs, snapshot2id, blob_op_with_handle_complete, NULL);
4623 	CU_ASSERT(g_bserrno == 0);
4624 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4625 	snapshot2 = g_blob;
4626 	CU_ASSERT(snapshot2->data_ro == true)
4627 	CU_ASSERT(snapshot2->md_ro == true)
4628 
4629 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 5)
4630 
4631 	CU_ASSERT(snapshot2->parent_id == snapshotid);
4632 
4633 	/* Write one cluster on the top level blob. This cluster (1) covers
4634 	 * already allocated cluster in the snapshot2, so shouldn't be inflated
4635 	 * at all */
4636 	spdk_blob_io_write(blob, channel, payload_write, pages_per_cluster,
4637 			   pages_per_cluster, blob_op_complete, NULL);
4638 	CU_ASSERT(g_bserrno == 0);
4639 
4640 	/* Update expected result */
4641 	memcpy(payload_clone + cluster_size, payload_write, cluster_size);
4642 
4643 	/* Check data consistency on clone */
4644 	memset(payload_read, 0xFF, payload_size);
4645 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4646 			  blob_op_complete, NULL);
4647 	CU_ASSERT(g_bserrno == 0);
4648 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
4649 
4650 
4651 	/* Close all blobs */
4652 	spdk_blob_close(blob, blob_op_complete, NULL);
4653 	CU_ASSERT(g_bserrno == 0);
4654 
4655 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
4656 	CU_ASSERT(g_bserrno == 0);
4657 
4658 	spdk_blob_close(snapshot, blob_op_complete, NULL);
4659 	CU_ASSERT(g_bserrno == 0);
4660 
4661 	/* Check snapshot-clone relations */
4662 	count = 2;
4663 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
4664 	CU_ASSERT(count == 1);
4665 	CU_ASSERT(ids[0] == snapshot2id);
4666 
4667 	count = 2;
4668 	CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
4669 	CU_ASSERT(count == 1);
4670 	CU_ASSERT(ids[0] == blobid);
4671 
4672 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshot2id);
4673 
4674 	free_clusters = spdk_bs_free_cluster_count(bs);
4675 	if (!decouple_parent) {
4676 		/* Do full blob inflation */
4677 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
4678 		CU_ASSERT(g_bserrno == 0);
4679 
4680 		/* All clusters should be inflated (except one already allocated
4681 		 * in a top level blob) */
4682 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 4);
4683 
4684 		/* Check if relation tree updated correctly */
4685 		count = 2;
4686 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
4687 
4688 		/* snapshotid have one clone */
4689 		CU_ASSERT(count == 1);
4690 		CU_ASSERT(ids[0] == snapshot2id);
4691 
4692 		/* snapshot2id have no clones */
4693 		count = 2;
4694 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
4695 		CU_ASSERT(count == 0);
4696 
4697 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
4698 	} else {
4699 		/* Decouple parent of blob */
4700 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
4701 		CU_ASSERT(g_bserrno == 0);
4702 
4703 		/* Only one cluster from a parent should be inflated (second one
4704 		 * is covered by a cluster written on a top level blob, and
4705 		 * already allocated) */
4706 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 1);
4707 
4708 		/* Check if relation tree updated correctly */
4709 		count = 2;
4710 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
4711 
4712 		/* snapshotid have two clones now */
4713 		CU_ASSERT(count == 2);
4714 		CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
4715 		CU_ASSERT(ids[0] == snapshot2id || ids[1] == snapshot2id);
4716 
4717 		/* snapshot2id have no clones */
4718 		count = 2;
4719 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
4720 		CU_ASSERT(count == 0);
4721 
4722 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
4723 	}
4724 
4725 	/* Try to delete snapshot2 (should pass) */
4726 	spdk_bs_delete_blob(bs, snapshot2id, blob_op_complete, NULL);
4727 	CU_ASSERT(g_bserrno == 0);
4728 
4729 	/* Try to delete base snapshot (for decouple_parent should fail while
4730 	 * dependency still exists) */
4731 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4732 	CU_ASSERT(decouple_parent || g_bserrno == 0);
4733 	CU_ASSERT(!decouple_parent || g_bserrno != 0);
4734 
4735 	/* Reopen blob after snapshot deletion */
4736 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4737 	CU_ASSERT(g_bserrno == 0);
4738 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4739 	blob = g_blob;
4740 
4741 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4742 
4743 	/* Check data consistency on inflated blob */
4744 	memset(payload_read, 0xFF, payload_size);
4745 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4746 			  blob_op_complete, NULL);
4747 	CU_ASSERT(g_bserrno == 0);
4748 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
4749 
4750 	spdk_blob_close(blob, blob_op_complete, NULL);
4751 	CU_ASSERT(g_bserrno == 0);
4752 
4753 	spdk_bs_free_io_channel(channel);
4754 
4755 	/* Unload the blob store */
4756 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4757 	CU_ASSERT(g_bserrno == 0);
4758 	g_bs = NULL;
4759 	g_blob = NULL;
4760 	g_blobid = 0;
4761 
4762 	free(payload_read);
4763 	free(payload_write);
4764 	free(payload_clone);
4765 }
4766 
4767 static void
4768 blob_inflate_rw(void)
4769 {
4770 	_blob_inflate_rw(false);
4771 	_blob_inflate_rw(true);
4772 }
4773 
4774 /**
4775  * Snapshot-clones relation test
4776  *
4777  *         snapshot
4778  *            |
4779  *      +-----+-----+
4780  *      |           |
4781  *   blob(ro)   snapshot2
4782  *      |           |
4783  *   clone2      clone
4784  */
4785 static void
4786 blob_relations(void)
4787 {
4788 	struct spdk_blob_store *bs;
4789 	struct spdk_bs_dev *dev;
4790 	struct spdk_bs_opts bs_opts;
4791 	struct spdk_blob_opts opts;
4792 	struct spdk_blob *blob, *snapshot, *snapshot2, *clone, *clone2;
4793 	spdk_blob_id blobid, cloneid, snapshotid, cloneid2, snapshotid2;
4794 	int rc;
4795 	size_t count;
4796 	spdk_blob_id ids[10] = {};
4797 
4798 	dev = init_dev();
4799 	spdk_bs_opts_init(&bs_opts);
4800 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
4801 
4802 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
4803 	CU_ASSERT(g_bserrno == 0);
4804 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4805 	bs = g_bs;
4806 
4807 	/* 1. Create blob with 10 clusters */
4808 
4809 	spdk_blob_opts_init(&opts);
4810 	opts.num_clusters = 10;
4811 
4812 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4813 	CU_ASSERT(g_bserrno == 0);
4814 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4815 	blobid = g_blobid;
4816 
4817 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4818 	CU_ASSERT(g_bserrno == 0);
4819 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4820 	blob = g_blob;
4821 
4822 	CU_ASSERT(!spdk_blob_is_read_only(blob));
4823 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
4824 	CU_ASSERT(!spdk_blob_is_clone(blob));
4825 	CU_ASSERT(!spdk_blob_is_thin_provisioned(blob));
4826 
4827 	/* blob should not have underlying snapshot nor clones */
4828 	CU_ASSERT(blob->parent_id == SPDK_BLOBID_INVALID);
4829 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
4830 	count = SPDK_COUNTOF(ids);
4831 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
4832 	CU_ASSERT(rc == 0);
4833 	CU_ASSERT(count == 0);
4834 
4835 
4836 	/* 2. Create snapshot */
4837 
4838 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4839 	CU_ASSERT(g_bserrno == 0);
4840 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4841 	snapshotid = g_blobid;
4842 
4843 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4844 	CU_ASSERT(g_bserrno == 0);
4845 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4846 	snapshot = g_blob;
4847 
4848 	CU_ASSERT(spdk_blob_is_read_only(snapshot));
4849 	CU_ASSERT(spdk_blob_is_snapshot(snapshot));
4850 	CU_ASSERT(!spdk_blob_is_clone(snapshot));
4851 	CU_ASSERT(snapshot->parent_id == SPDK_BLOBID_INVALID);
4852 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
4853 
4854 	/* Check if original blob is converted to the clone of snapshot */
4855 	CU_ASSERT(!spdk_blob_is_read_only(blob));
4856 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
4857 	CU_ASSERT(spdk_blob_is_clone(blob));
4858 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
4859 	CU_ASSERT(blob->parent_id == snapshotid);
4860 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
4861 
4862 	count = SPDK_COUNTOF(ids);
4863 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
4864 	CU_ASSERT(rc == 0);
4865 	CU_ASSERT(count == 1);
4866 	CU_ASSERT(ids[0] == blobid);
4867 
4868 
4869 	/* 3. Create clone from snapshot */
4870 
4871 	spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
4872 	CU_ASSERT(g_bserrno == 0);
4873 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4874 	cloneid = g_blobid;
4875 
4876 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
4877 	CU_ASSERT(g_bserrno == 0);
4878 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4879 	clone = g_blob;
4880 
4881 	CU_ASSERT(!spdk_blob_is_read_only(clone));
4882 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
4883 	CU_ASSERT(spdk_blob_is_clone(clone));
4884 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
4885 	CU_ASSERT(clone->parent_id == snapshotid);
4886 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid);
4887 
4888 	count = SPDK_COUNTOF(ids);
4889 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
4890 	CU_ASSERT(rc == 0);
4891 	CU_ASSERT(count == 0);
4892 
4893 	/* Check if clone is on the snapshot's list */
4894 	count = SPDK_COUNTOF(ids);
4895 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
4896 	CU_ASSERT(rc == 0);
4897 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
4898 	CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid);
4899 
4900 
4901 	/* 4. Create snapshot of the clone */
4902 
4903 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
4904 	CU_ASSERT(g_bserrno == 0);
4905 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4906 	snapshotid2 = g_blobid;
4907 
4908 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
4909 	CU_ASSERT(g_bserrno == 0);
4910 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4911 	snapshot2 = g_blob;
4912 
4913 	CU_ASSERT(spdk_blob_is_read_only(snapshot2));
4914 	CU_ASSERT(spdk_blob_is_snapshot(snapshot2));
4915 	CU_ASSERT(spdk_blob_is_clone(snapshot2));
4916 	CU_ASSERT(snapshot2->parent_id == snapshotid);
4917 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
4918 
4919 	/* Check if clone is converted to the clone of snapshot2 and snapshot2
4920 	 * is a child of snapshot */
4921 	CU_ASSERT(!spdk_blob_is_read_only(clone));
4922 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
4923 	CU_ASSERT(spdk_blob_is_clone(clone));
4924 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
4925 	CU_ASSERT(clone->parent_id == snapshotid2);
4926 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
4927 
4928 	count = SPDK_COUNTOF(ids);
4929 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
4930 	CU_ASSERT(rc == 0);
4931 	CU_ASSERT(count == 1);
4932 	CU_ASSERT(ids[0] == cloneid);
4933 
4934 
4935 	/* 5. Try to create clone from read only blob */
4936 
4937 	/* Mark blob as read only */
4938 	spdk_blob_set_read_only(blob);
4939 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4940 	CU_ASSERT(g_bserrno == 0);
4941 
4942 	/* Check if previously created blob is read only clone */
4943 	CU_ASSERT(spdk_blob_is_read_only(blob));
4944 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
4945 	CU_ASSERT(spdk_blob_is_clone(blob));
4946 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
4947 
4948 	/* Create clone from read only blob */
4949 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4950 	CU_ASSERT(g_bserrno == 0);
4951 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4952 	cloneid2 = g_blobid;
4953 
4954 	spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL);
4955 	CU_ASSERT(g_bserrno == 0);
4956 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4957 	clone2 = g_blob;
4958 
4959 	CU_ASSERT(!spdk_blob_is_read_only(clone2));
4960 	CU_ASSERT(!spdk_blob_is_snapshot(clone2));
4961 	CU_ASSERT(spdk_blob_is_clone(clone2));
4962 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone2));
4963 
4964 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
4965 
4966 	count = SPDK_COUNTOF(ids);
4967 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
4968 	CU_ASSERT(rc == 0);
4969 
4970 	CU_ASSERT(count == 1);
4971 	CU_ASSERT(ids[0] == cloneid2);
4972 
4973 	/* Close blobs */
4974 
4975 	spdk_blob_close(clone2, blob_op_complete, NULL);
4976 	CU_ASSERT(g_bserrno == 0);
4977 
4978 	spdk_blob_close(blob, blob_op_complete, NULL);
4979 	CU_ASSERT(g_bserrno == 0);
4980 
4981 	spdk_blob_close(clone, blob_op_complete, NULL);
4982 	CU_ASSERT(g_bserrno == 0);
4983 
4984 	spdk_blob_close(snapshot, blob_op_complete, NULL);
4985 	CU_ASSERT(g_bserrno == 0);
4986 
4987 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
4988 	CU_ASSERT(g_bserrno == 0);
4989 
4990 	/* Try to delete snapshot with created clones */
4991 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4992 	CU_ASSERT(g_bserrno != 0);
4993 
4994 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
4995 	CU_ASSERT(g_bserrno != 0);
4996 
4997 	spdk_bs_unload(bs, bs_op_complete, NULL);
4998 	CU_ASSERT(g_bserrno == 0);
4999 	g_bs = NULL;
5000 
5001 	/* Load an existing blob store */
5002 	dev = init_dev();
5003 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
5004 
5005 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
5006 	CU_ASSERT(g_bserrno == 0);
5007 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
5008 	bs = g_bs;
5009 
5010 
5011 	/* NULL ids array should return number of clones in count */
5012 	count = SPDK_COUNTOF(ids);
5013 	rc = spdk_blob_get_clones(bs, snapshotid, NULL, &count);
5014 	CU_ASSERT(rc == -ENOMEM);
5015 	CU_ASSERT(count == 2);
5016 
5017 	/* incorrect array size */
5018 	count = 1;
5019 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5020 	CU_ASSERT(rc == -ENOMEM);
5021 	CU_ASSERT(count == 2);
5022 
5023 
5024 	/* Verify structure of loaded blob store */
5025 
5026 	/* snapshot */
5027 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
5028 
5029 	count = SPDK_COUNTOF(ids);
5030 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
5031 	CU_ASSERT(rc == 0);
5032 	CU_ASSERT(count == 2);
5033 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
5034 	CU_ASSERT(ids[0] == snapshotid2 || ids[1] == snapshotid2);
5035 
5036 	/* blob */
5037 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
5038 	count = SPDK_COUNTOF(ids);
5039 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
5040 	CU_ASSERT(rc == 0);
5041 	CU_ASSERT(count == 1);
5042 	CU_ASSERT(ids[0] == cloneid2);
5043 
5044 	/* clone */
5045 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
5046 	count = SPDK_COUNTOF(ids);
5047 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
5048 	CU_ASSERT(rc == 0);
5049 	CU_ASSERT(count == 0);
5050 
5051 	/* snapshot2 */
5052 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
5053 	count = SPDK_COUNTOF(ids);
5054 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
5055 	CU_ASSERT(rc == 0);
5056 	CU_ASSERT(count == 1);
5057 	CU_ASSERT(ids[0] == cloneid);
5058 
5059 	/* clone2 */
5060 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
5061 	count = SPDK_COUNTOF(ids);
5062 	rc = spdk_blob_get_clones(bs, cloneid2, ids, &count);
5063 	CU_ASSERT(rc == 0);
5064 	CU_ASSERT(count == 0);
5065 
5066 	/* Try to delete all blobs in the worse possible order */
5067 
5068 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5069 	CU_ASSERT(g_bserrno != 0);
5070 
5071 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
5072 	CU_ASSERT(g_bserrno != 0);
5073 
5074 	spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL);
5075 	CU_ASSERT(g_bserrno == 0);
5076 
5077 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
5078 	CU_ASSERT(g_bserrno == 0);
5079 
5080 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5081 	CU_ASSERT(g_bserrno != 0);
5082 
5083 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
5084 	CU_ASSERT(g_bserrno != 0);
5085 
5086 	spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL);
5087 	CU_ASSERT(g_bserrno == 0);
5088 
5089 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
5090 	CU_ASSERT(g_bserrno == 0);
5091 
5092 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
5093 	CU_ASSERT(g_bserrno == 0);
5094 
5095 	spdk_bs_unload(bs, bs_op_complete, NULL);
5096 	CU_ASSERT(g_bserrno == 0);
5097 
5098 	g_bs = NULL;
5099 }
5100 
5101 int main(int argc, char **argv)
5102 {
5103 	CU_pSuite	suite = NULL;
5104 	unsigned int	num_failures;
5105 
5106 	if (CU_initialize_registry() != CUE_SUCCESS) {
5107 		return CU_get_error();
5108 	}
5109 
5110 	suite = CU_add_suite("blob", NULL, NULL);
5111 	if (suite == NULL) {
5112 		CU_cleanup_registry();
5113 		return CU_get_error();
5114 	}
5115 
5116 	if (
5117 		CU_add_test(suite, "blob_init", blob_init) == NULL ||
5118 		CU_add_test(suite, "blob_open", blob_open) == NULL ||
5119 		CU_add_test(suite, "blob_create", blob_create) == NULL ||
5120 		CU_add_test(suite, "blob_create_internal", blob_create_internal) == NULL ||
5121 		CU_add_test(suite, "blob_thin_provision", blob_thin_provision) == NULL ||
5122 		CU_add_test(suite, "blob_snapshot", blob_snapshot) == NULL ||
5123 		CU_add_test(suite, "blob_clone", blob_clone) == NULL ||
5124 		CU_add_test(suite, "blob_inflate", blob_inflate) == NULL ||
5125 		CU_add_test(suite, "blob_delete", blob_delete) == NULL ||
5126 		CU_add_test(suite, "blob_resize", blob_resize) == NULL ||
5127 		CU_add_test(suite, "blob_read_only", blob_read_only) == NULL ||
5128 		CU_add_test(suite, "channel_ops", channel_ops) == NULL ||
5129 		CU_add_test(suite, "blob_super", blob_super) == NULL ||
5130 		CU_add_test(suite, "blob_write", blob_write) == NULL ||
5131 		CU_add_test(suite, "blob_read", blob_read) == NULL ||
5132 		CU_add_test(suite, "blob_rw_verify", blob_rw_verify) == NULL ||
5133 		CU_add_test(suite, "blob_rw_verify_iov", blob_rw_verify_iov) == NULL ||
5134 		CU_add_test(suite, "blob_rw_verify_iov_nomem", blob_rw_verify_iov_nomem) == NULL ||
5135 		CU_add_test(suite, "blob_rw_iov_read_only", blob_rw_iov_read_only) == NULL ||
5136 		CU_add_test(suite, "blob_unmap", blob_unmap) == NULL ||
5137 		CU_add_test(suite, "blob_iter", blob_iter) == NULL ||
5138 		CU_add_test(suite, "blob_xattr", blob_xattr) == NULL ||
5139 		CU_add_test(suite, "bs_load", bs_load) == NULL ||
5140 		CU_add_test(suite, "bs_load_custom_cluster_size", bs_load_custom_cluster_size) == NULL ||
5141 		CU_add_test(suite, "bs_unload", bs_unload) == NULL ||
5142 		CU_add_test(suite, "bs_cluster_sz", bs_cluster_sz) == NULL ||
5143 		CU_add_test(suite, "bs_usable_clusters", bs_usable_clusters) == NULL ||
5144 		CU_add_test(suite, "bs_resize_md", bs_resize_md) == NULL ||
5145 		CU_add_test(suite, "bs_destroy", bs_destroy) == NULL ||
5146 		CU_add_test(suite, "bs_type", bs_type) == NULL ||
5147 		CU_add_test(suite, "bs_super_block", bs_super_block) == NULL ||
5148 		CU_add_test(suite, "blob_serialize", blob_serialize) == NULL ||
5149 		CU_add_test(suite, "blob_crc", blob_crc) == NULL ||
5150 		CU_add_test(suite, "super_block_crc", super_block_crc) == NULL ||
5151 		CU_add_test(suite, "blob_dirty_shutdown", blob_dirty_shutdown) == NULL ||
5152 		CU_add_test(suite, "blob_flags", blob_flags) == NULL ||
5153 		CU_add_test(suite, "bs_version", bs_version) == NULL ||
5154 		CU_add_test(suite, "blob_set_xattrs", blob_set_xattrs) == NULL ||
5155 		CU_add_test(suite, "blob_thin_prov_alloc", blob_thin_prov_alloc) == NULL ||
5156 		CU_add_test(suite, "blob_insert_cluster_msg", blob_insert_cluster_msg) == NULL ||
5157 		CU_add_test(suite, "blob_thin_prov_rw", blob_thin_prov_rw) == NULL ||
5158 		CU_add_test(suite, "blob_thin_prov_rw_iov", blob_thin_prov_rw_iov) == NULL ||
5159 		CU_add_test(suite, "bs_load_iter", bs_load_iter) == NULL ||
5160 		CU_add_test(suite, "blob_snapshot_rw", blob_snapshot_rw) == NULL ||
5161 		CU_add_test(suite, "blob_snapshot_rw_iov", blob_snapshot_rw_iov) == NULL ||
5162 		CU_add_test(suite, "blob_relations", blob_relations) == NULL ||
5163 		CU_add_test(suite, "blob_inflate_rw", blob_inflate_rw) == NULL ||
5164 		CU_add_test(suite, "blob_snapshot_freeze_io", blob_snapshot_freeze_io) == NULL ||
5165 		CU_add_test(suite, "blob_operation_split_rw", blob_operation_split_rw) == NULL ||
5166 		CU_add_test(suite, "blob_operation_split_rw_iov", blob_operation_split_rw_iov) == NULL
5167 	) {
5168 		CU_cleanup_registry();
5169 		return CU_get_error();
5170 	}
5171 
5172 	g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
5173 	spdk_allocate_thread(_bs_send_msg, NULL, NULL, NULL, "thread0");
5174 	CU_basic_set_mode(CU_BRM_VERBOSE);
5175 	CU_basic_run_tests();
5176 	num_failures = CU_get_number_of_failures();
5177 	CU_cleanup_registry();
5178 	spdk_free_thread();
5179 	free(g_dev_buffer);
5180 	return num_failures;
5181 }
5182