xref: /spdk/test/unit/lib/blob/blob.c/blob_ut.c (revision 8a0a98d35e21f282088edf28b9e8da66ec390e3a)
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, void *, 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_SET(calloc, void *, (void *)MOCK_PASS_THRU);
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_unmap(void)
1618 {
1619 	struct spdk_blob_store *bs;
1620 	struct spdk_bs_dev *dev;
1621 	struct spdk_blob *blob;
1622 	struct spdk_io_channel *channel;
1623 	spdk_blob_id blobid;
1624 	struct spdk_blob_opts opts;
1625 	uint8_t payload[4096];
1626 	int i;
1627 
1628 	dev = init_dev();
1629 
1630 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1631 	CU_ASSERT(g_bserrno == 0);
1632 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1633 	bs = g_bs;
1634 
1635 	channel = spdk_bs_alloc_io_channel(bs);
1636 	CU_ASSERT(channel != NULL);
1637 
1638 	spdk_blob_opts_init(&opts);
1639 	opts.num_clusters = 10;
1640 
1641 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
1642 	CU_ASSERT(g_bserrno == 0);
1643 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1644 	blobid = g_blobid;
1645 
1646 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1647 	CU_ASSERT(g_bserrno == 0);
1648 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1649 	blob = g_blob;
1650 
1651 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
1652 	CU_ASSERT(g_bserrno == 0);
1653 
1654 	memset(payload, 0, sizeof(payload));
1655 	payload[0] = 0xFF;
1656 
1657 	/*
1658 	 * Set first byte of every cluster to 0xFF.
1659 	 * First cluster on device is reserved so let's start from cluster number 1
1660 	 */
1661 	for (i = 1; i < 11; i++) {
1662 		g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] = 0xFF;
1663 	}
1664 
1665 	/* Confirm writes */
1666 	for (i = 0; i < 10; i++) {
1667 		payload[0] = 0;
1668 		spdk_blob_io_read(blob, channel, &payload, i * SPDK_BLOB_OPTS_CLUSTER_SZ / 4096, 1,
1669 				  blob_op_complete, NULL);
1670 		CU_ASSERT(g_bserrno == 0);
1671 		CU_ASSERT(payload[0] == 0xFF);
1672 	}
1673 
1674 	/* Mark some clusters as unallocated */
1675 	blob->active.clusters[1] = 0;
1676 	blob->active.clusters[2] = 0;
1677 	blob->active.clusters[3] = 0;
1678 	blob->active.clusters[6] = 0;
1679 	blob->active.clusters[8] = 0;
1680 
1681 	/* Unmap clusters by resizing to 0 */
1682 	spdk_blob_resize(blob, 0, blob_op_complete, NULL);
1683 	CU_ASSERT(g_bserrno == 0);
1684 
1685 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
1686 	CU_ASSERT(g_bserrno == 0);
1687 
1688 	/* Confirm that only 'allocated' clusters were unmaped */
1689 	for (i = 1; i < 11; i++) {
1690 		switch (i) {
1691 		case 2:
1692 		case 3:
1693 		case 4:
1694 		case 7:
1695 		case 9:
1696 			CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0xFF);
1697 			break;
1698 		default:
1699 			CU_ASSERT(g_dev_buffer[i * SPDK_BLOB_OPTS_CLUSTER_SZ] == 0);
1700 			break;
1701 		}
1702 	}
1703 
1704 	spdk_blob_close(blob, blob_op_complete, NULL);
1705 	CU_ASSERT(g_bserrno == 0);
1706 
1707 	spdk_bs_free_io_channel(channel);
1708 
1709 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1710 	CU_ASSERT(g_bserrno == 0);
1711 	g_bs = NULL;
1712 }
1713 
1714 
1715 static void
1716 blob_iter(void)
1717 {
1718 	struct spdk_blob_store *bs;
1719 	struct spdk_bs_dev *dev;
1720 	struct spdk_blob *blob;
1721 	spdk_blob_id blobid;
1722 
1723 	dev = init_dev();
1724 
1725 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1726 	CU_ASSERT(g_bserrno == 0);
1727 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1728 	bs = g_bs;
1729 
1730 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
1731 	CU_ASSERT(g_blob == NULL);
1732 	CU_ASSERT(g_bserrno == -ENOENT);
1733 
1734 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1735 	CU_ASSERT(g_bserrno == 0);
1736 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1737 	blobid = g_blobid;
1738 
1739 	spdk_bs_iter_first(bs, blob_op_with_handle_complete, NULL);
1740 	CU_ASSERT(g_blob != NULL);
1741 	CU_ASSERT(g_bserrno == 0);
1742 	blob = g_blob;
1743 	CU_ASSERT(spdk_blob_get_id(blob) == blobid);
1744 
1745 	spdk_bs_iter_next(bs, blob, blob_op_with_handle_complete, NULL);
1746 	CU_ASSERT(g_blob == NULL);
1747 	CU_ASSERT(g_bserrno == -ENOENT);
1748 
1749 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1750 	CU_ASSERT(g_bserrno == 0);
1751 	g_bs = NULL;
1752 }
1753 
1754 static void
1755 blob_xattr(void)
1756 {
1757 	struct spdk_blob_store *bs;
1758 	struct spdk_bs_dev *dev;
1759 	struct spdk_blob *blob;
1760 	spdk_blob_id blobid;
1761 	uint64_t length;
1762 	int rc;
1763 	const char *name1, *name2;
1764 	const void *value;
1765 	size_t value_len;
1766 	struct spdk_xattr_names *names;
1767 
1768 	dev = init_dev();
1769 
1770 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
1771 	CU_ASSERT(g_bserrno == 0);
1772 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1773 	bs = g_bs;
1774 
1775 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
1776 	CU_ASSERT(g_bserrno == 0);
1777 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1778 	blobid = g_blobid;
1779 
1780 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1781 	CU_ASSERT(g_bserrno == 0);
1782 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1783 	blob = g_blob;
1784 
1785 	/* Test that set_xattr fails if md_ro flag is set. */
1786 	blob->md_ro = true;
1787 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
1788 	CU_ASSERT(rc == -EPERM);
1789 
1790 	blob->md_ro = false;
1791 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
1792 	CU_ASSERT(rc == 0);
1793 
1794 	length = 2345;
1795 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
1796 	CU_ASSERT(rc == 0);
1797 
1798 	/* Overwrite "length" xattr. */
1799 	length = 3456;
1800 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
1801 	CU_ASSERT(rc == 0);
1802 
1803 	/* get_xattr should still work even if md_ro flag is set. */
1804 	value = NULL;
1805 	blob->md_ro = true;
1806 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
1807 	CU_ASSERT(rc == 0);
1808 	SPDK_CU_ASSERT_FATAL(value != NULL);
1809 	CU_ASSERT(*(uint64_t *)value == length);
1810 	CU_ASSERT(value_len == 8);
1811 	blob->md_ro = false;
1812 
1813 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
1814 	CU_ASSERT(rc == -ENOENT);
1815 
1816 	names = NULL;
1817 	rc = spdk_blob_get_xattr_names(blob, &names);
1818 	CU_ASSERT(rc == 0);
1819 	SPDK_CU_ASSERT_FATAL(names != NULL);
1820 	CU_ASSERT(spdk_xattr_names_get_count(names) == 2);
1821 	name1 = spdk_xattr_names_get_name(names, 0);
1822 	SPDK_CU_ASSERT_FATAL(name1 != NULL);
1823 	CU_ASSERT(!strcmp(name1, "name") || !strcmp(name1, "length"));
1824 	name2 = spdk_xattr_names_get_name(names, 1);
1825 	SPDK_CU_ASSERT_FATAL(name2 != NULL);
1826 	CU_ASSERT(!strcmp(name2, "name") || !strcmp(name2, "length"));
1827 	CU_ASSERT(strcmp(name1, name2));
1828 	spdk_xattr_names_free(names);
1829 
1830 	/* Confirm that remove_xattr fails if md_ro is set to true. */
1831 	blob->md_ro = true;
1832 	rc = spdk_blob_remove_xattr(blob, "name");
1833 	CU_ASSERT(rc == -EPERM);
1834 
1835 	blob->md_ro = false;
1836 	rc = spdk_blob_remove_xattr(blob, "name");
1837 	CU_ASSERT(rc == 0);
1838 
1839 	rc = spdk_blob_remove_xattr(blob, "foobar");
1840 	CU_ASSERT(rc == -ENOENT);
1841 
1842 	/* Set internal xattr */
1843 	length = 7898;
1844 	rc = _spdk_blob_set_xattr(blob, "internal", &length, sizeof(length), true);
1845 	CU_ASSERT(rc == 0);
1846 	rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, true);
1847 	CU_ASSERT(rc == 0);
1848 	CU_ASSERT(*(uint64_t *)value == length);
1849 	/* try to get public xattr with same name */
1850 	rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len);
1851 	CU_ASSERT(rc != 0);
1852 	rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, false);
1853 	CU_ASSERT(rc != 0);
1854 	/* Check if SPDK_BLOB_INTERNAL_XATTR is set */
1855 	CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) ==
1856 		  SPDK_BLOB_INTERNAL_XATTR)
1857 
1858 	spdk_blob_close(blob, blob_op_complete, NULL);
1859 
1860 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1861 
1862 	/* Check if xattrs are persisted */
1863 	dev = init_dev();
1864 
1865 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
1866 	CU_ASSERT(g_bserrno == 0);
1867 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1868 
1869 	bs = g_bs;
1870 
1871 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
1872 	CU_ASSERT(g_bserrno == 0);
1873 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
1874 	blob = g_blob;
1875 
1876 	rc = _spdk_blob_get_xattr_value(blob, "internal", &value, &value_len, true);
1877 	CU_ASSERT(rc == 0);
1878 	CU_ASSERT(*(uint64_t *)value == length);
1879 
1880 	/* try to get internal xattr trough public call */
1881 	rc = spdk_blob_get_xattr_value(blob, "internal", &value, &value_len);
1882 	CU_ASSERT(rc != 0);
1883 
1884 	rc = _spdk_blob_remove_xattr(blob, "internal", true);
1885 	CU_ASSERT(rc == 0);
1886 
1887 	CU_ASSERT((blob->invalid_flags & SPDK_BLOB_INTERNAL_XATTR) == 0);
1888 
1889 	CU_ASSERT(g_bserrno == 0);
1890 	g_bs = NULL;
1891 }
1892 
1893 static void
1894 bs_load(void)
1895 {
1896 	struct spdk_bs_dev *dev;
1897 	spdk_blob_id blobid;
1898 	struct spdk_blob *blob;
1899 	struct spdk_bs_super_block *super_block;
1900 	uint64_t length;
1901 	int rc;
1902 	const void *value;
1903 	size_t value_len;
1904 	struct spdk_bs_opts opts;
1905 
1906 	dev = init_dev();
1907 	spdk_bs_opts_init(&opts);
1908 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
1909 
1910 	/* Initialize a new blob store */
1911 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
1912 	CU_ASSERT(g_bserrno == 0);
1913 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1914 
1915 	/* Try to open a blobid that does not exist */
1916 	spdk_bs_open_blob(g_bs, 0, blob_op_with_handle_complete, NULL);
1917 	CU_ASSERT(g_bserrno == -ENOENT);
1918 	CU_ASSERT(g_blob == NULL);
1919 
1920 	/* Create a blob */
1921 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
1922 	CU_ASSERT(g_bserrno == 0);
1923 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
1924 	blobid = g_blobid;
1925 
1926 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
1927 	CU_ASSERT(g_bserrno == 0);
1928 	CU_ASSERT(g_blob != NULL);
1929 	blob = g_blob;
1930 
1931 	/* Try again to open valid blob but without the upper bit set */
1932 	spdk_bs_open_blob(g_bs, blobid & 0xFFFFFFFF, blob_op_with_handle_complete, NULL);
1933 	CU_ASSERT(g_bserrno == -ENOENT);
1934 	CU_ASSERT(g_blob == NULL);
1935 
1936 	/* Set some xattrs */
1937 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
1938 	CU_ASSERT(rc == 0);
1939 
1940 	length = 2345;
1941 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
1942 	CU_ASSERT(rc == 0);
1943 
1944 	/* Resize the blob */
1945 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
1946 	CU_ASSERT(g_bserrno == 0);
1947 
1948 	spdk_blob_close(blob, blob_op_complete, NULL);
1949 	CU_ASSERT(g_bserrno == 0);
1950 	blob = NULL;
1951 	g_blob = NULL;
1952 	g_blobid = SPDK_BLOBID_INVALID;
1953 
1954 	/* Unload the blob store */
1955 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
1956 	CU_ASSERT(g_bserrno == 0);
1957 	g_bs = NULL;
1958 	g_blob = NULL;
1959 	g_blobid = 0;
1960 
1961 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
1962 	CU_ASSERT(super_block->clean == 1);
1963 
1964 	/* Load should fail for device with an unsupported blocklen */
1965 	dev = init_dev();
1966 	dev->blocklen = SPDK_BS_PAGE_SIZE * 2;
1967 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
1968 	CU_ASSERT(g_bserrno == -EINVAL);
1969 
1970 	/* Load should when max_md_ops is set to zero */
1971 	dev = init_dev();
1972 	spdk_bs_opts_init(&opts);
1973 	opts.max_md_ops = 0;
1974 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
1975 	CU_ASSERT(g_bserrno == -EINVAL);
1976 
1977 	/* Load should when max_channel_ops is set to zero */
1978 	dev = init_dev();
1979 	spdk_bs_opts_init(&opts);
1980 	opts.max_channel_ops = 0;
1981 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
1982 	CU_ASSERT(g_bserrno == -EINVAL);
1983 
1984 	/* Load an existing blob store */
1985 	dev = init_dev();
1986 	spdk_bs_opts_init(&opts);
1987 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
1988 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
1989 	CU_ASSERT(g_bserrno == 0);
1990 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
1991 
1992 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
1993 	CU_ASSERT(super_block->clean == 1);
1994 
1995 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
1996 	CU_ASSERT(g_bserrno == 0);
1997 	CU_ASSERT(g_blob != NULL);
1998 	blob = g_blob;
1999 
2000 	/* Verify that blobstore is marked dirty after first metadata sync */
2001 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
2002 	CU_ASSERT(super_block->clean == 1);
2003 
2004 	/* Get the xattrs */
2005 	value = NULL;
2006 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2007 	CU_ASSERT(rc == 0);
2008 	SPDK_CU_ASSERT_FATAL(value != NULL);
2009 	CU_ASSERT(*(uint64_t *)value == length);
2010 	CU_ASSERT(value_len == 8);
2011 
2012 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
2013 	CU_ASSERT(rc == -ENOENT);
2014 
2015 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
2016 
2017 	spdk_blob_close(blob, blob_op_complete, NULL);
2018 	CU_ASSERT(g_bserrno == 0);
2019 	blob = NULL;
2020 	g_blob = NULL;
2021 	g_blobid = SPDK_BLOBID_INVALID;
2022 
2023 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2024 	CU_ASSERT(g_bserrno == 0);
2025 	g_bs = NULL;
2026 }
2027 
2028 static void
2029 bs_type(void)
2030 {
2031 	struct spdk_bs_dev *dev;
2032 	struct spdk_bs_opts opts;
2033 
2034 	dev = init_dev();
2035 	spdk_bs_opts_init(&opts);
2036 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2037 
2038 	/* Initialize a new blob store */
2039 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2040 	CU_ASSERT(g_bserrno == 0);
2041 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2042 
2043 	/* Unload the blob store */
2044 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2045 	CU_ASSERT(g_bserrno == 0);
2046 	g_bs = NULL;
2047 	g_blob = NULL;
2048 	g_blobid = 0;
2049 
2050 	/* Load non existing blobstore type */
2051 	dev = init_dev();
2052 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING");
2053 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2054 	CU_ASSERT(g_bserrno != 0);
2055 
2056 	/* Load with empty blobstore type */
2057 	dev = init_dev();
2058 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2059 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2060 	CU_ASSERT(g_bserrno == 0);
2061 
2062 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2063 	CU_ASSERT(g_bserrno == 0);
2064 	g_bs = NULL;
2065 
2066 	/* Initialize a new blob store with empty bstype */
2067 	dev = init_dev();
2068 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2069 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2070 	CU_ASSERT(g_bserrno == 0);
2071 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2072 
2073 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2074 	CU_ASSERT(g_bserrno == 0);
2075 	g_bs = NULL;
2076 
2077 	/* Load non existing blobstore type */
2078 	dev = init_dev();
2079 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "NONEXISTING");
2080 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2081 	CU_ASSERT(g_bserrno != 0);
2082 
2083 	/* Load with empty blobstore type */
2084 	dev = init_dev();
2085 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2086 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2087 	CU_ASSERT(g_bserrno == 0);
2088 
2089 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2090 	CU_ASSERT(g_bserrno == 0);
2091 	g_bs = NULL;
2092 }
2093 
2094 static void
2095 bs_super_block(void)
2096 {
2097 	struct spdk_bs_dev *dev;
2098 	struct spdk_bs_super_block *super_block;
2099 	struct spdk_bs_opts opts;
2100 	struct spdk_bs_super_block_ver1 super_block_v1;
2101 
2102 	dev = init_dev();
2103 	spdk_bs_opts_init(&opts);
2104 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
2105 
2106 	/* Initialize a new blob store */
2107 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2108 	CU_ASSERT(g_bserrno == 0);
2109 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2110 
2111 	/* Unload the blob store */
2112 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2113 	CU_ASSERT(g_bserrno == 0);
2114 	g_bs = NULL;
2115 	g_blob = NULL;
2116 	g_blobid = 0;
2117 
2118 	/* Load an existing blob store with version newer than supported */
2119 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2120 	super_block->version++;
2121 
2122 	dev = init_dev();
2123 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2124 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2125 	CU_ASSERT(g_bserrno != 0);
2126 
2127 	/* Create a new blob store with super block version 1 */
2128 	dev = init_dev();
2129 	super_block_v1.version = 1;
2130 	memcpy(super_block_v1.signature, "SPDKBLOB", sizeof(super_block_v1.signature));
2131 	super_block_v1.length = 0x1000;
2132 	super_block_v1.clean = 1;
2133 	super_block_v1.super_blob = 0xFFFFFFFFFFFFFFFF;
2134 	super_block_v1.cluster_size = 0x100000;
2135 	super_block_v1.used_page_mask_start = 0x01;
2136 	super_block_v1.used_page_mask_len = 0x01;
2137 	super_block_v1.used_cluster_mask_start = 0x02;
2138 	super_block_v1.used_cluster_mask_len = 0x01;
2139 	super_block_v1.md_start = 0x03;
2140 	super_block_v1.md_len = 0x40;
2141 	memset(super_block_v1.reserved, 0, 4036);
2142 	super_block_v1.crc = _spdk_blob_md_page_calc_crc(&super_block_v1);
2143 	memcpy(g_dev_buffer, &super_block_v1, sizeof(struct spdk_bs_super_block_ver1));
2144 
2145 	memset(opts.bstype.bstype, 0, sizeof(opts.bstype.bstype));
2146 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2147 	CU_ASSERT(g_bserrno == 0);
2148 
2149 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2150 	CU_ASSERT(g_bserrno == 0);
2151 	g_bs = NULL;
2152 }
2153 
2154 /*
2155  * Create a blobstore and then unload it.
2156  */
2157 static void
2158 bs_unload(void)
2159 {
2160 	struct spdk_bs_dev *dev;
2161 	struct spdk_blob_store *bs;
2162 	spdk_blob_id blobid;
2163 	struct spdk_blob *blob;
2164 
2165 	dev = init_dev();
2166 
2167 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2168 	CU_ASSERT(g_bserrno == 0);
2169 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2170 	bs = g_bs;
2171 
2172 	/* Create a blob and open it. */
2173 	g_bserrno = -1;
2174 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2175 	CU_ASSERT(g_bserrno == 0);
2176 	CU_ASSERT(g_blobid > 0);
2177 	blobid = g_blobid;
2178 
2179 	g_bserrno = -1;
2180 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2181 	CU_ASSERT(g_bserrno == 0);
2182 	CU_ASSERT(g_blob != NULL);
2183 	blob = g_blob;
2184 
2185 	/* Try to unload blobstore, should fail with open blob */
2186 	g_bserrno = -1;
2187 	spdk_bs_unload(bs, bs_op_complete, NULL);
2188 	CU_ASSERT(g_bserrno == -EBUSY);
2189 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2190 
2191 	/* Close the blob, then successfully unload blobstore */
2192 	g_bserrno = -1;
2193 	spdk_blob_close(blob, blob_op_complete, NULL);
2194 	CU_ASSERT(g_bserrno == 0);
2195 
2196 	g_bserrno = -1;
2197 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2198 	CU_ASSERT(g_bserrno == 0);
2199 	g_bs = NULL;
2200 }
2201 
2202 /*
2203  * Create a blobstore with a cluster size different than the default, and ensure it is
2204  *  persisted.
2205  */
2206 static void
2207 bs_cluster_sz(void)
2208 {
2209 	struct spdk_bs_dev *dev;
2210 	struct spdk_bs_opts opts;
2211 	uint32_t cluster_sz;
2212 
2213 	/* Set cluster size to zero */
2214 	dev = init_dev();
2215 	spdk_bs_opts_init(&opts);
2216 	opts.cluster_sz = 0;
2217 
2218 	/* Initialize a new blob store */
2219 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2220 	CU_ASSERT(g_bserrno == -EINVAL);
2221 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
2222 
2223 	/*
2224 	 * Set cluster size to blobstore page size,
2225 	 * to work it is required to be at least twice the blobstore page size.
2226 	 */
2227 	dev = init_dev();
2228 	spdk_bs_opts_init(&opts);
2229 	opts.cluster_sz = SPDK_BS_PAGE_SIZE;
2230 
2231 	/* Initialize a new blob store */
2232 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2233 	CU_ASSERT(g_bserrno == -ENOMEM);
2234 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
2235 
2236 	/*
2237 	 * Set cluster size to lower than page size,
2238 	 * to work it is required to be at least twice the blobstore page size.
2239 	 */
2240 	dev = init_dev();
2241 	spdk_bs_opts_init(&opts);
2242 	opts.cluster_sz = SPDK_BS_PAGE_SIZE - 1;
2243 
2244 	/* Initialize a new blob store */
2245 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2246 	CU_ASSERT(g_bserrno == -ENOMEM);
2247 	SPDK_CU_ASSERT_FATAL(g_bs == NULL);
2248 
2249 	/* Set cluster size to twice the default */
2250 	dev = init_dev();
2251 	spdk_bs_opts_init(&opts);
2252 	opts.cluster_sz *= 2;
2253 	cluster_sz = opts.cluster_sz;
2254 
2255 	/* Initialize a new blob store */
2256 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2257 	CU_ASSERT(g_bserrno == 0);
2258 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2259 
2260 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2261 
2262 	/* Unload the blob store */
2263 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2264 	CU_ASSERT(g_bserrno == 0);
2265 	g_bs = NULL;
2266 	g_blob = NULL;
2267 	g_blobid = 0;
2268 
2269 	dev = init_dev();
2270 	/* Load an existing blob store */
2271 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2272 	CU_ASSERT(g_bserrno == 0);
2273 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2274 
2275 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2276 
2277 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2278 	CU_ASSERT(g_bserrno == 0);
2279 	g_bs = NULL;
2280 }
2281 
2282 /*
2283  * Create a blobstore, reload it and ensure total usable cluster count
2284  *  stays the same.
2285  */
2286 static void
2287 bs_usable_clusters(void)
2288 {
2289 	struct spdk_bs_dev *dev;
2290 	struct spdk_bs_opts opts;
2291 	uint32_t clusters;
2292 	int i;
2293 
2294 	/* Init blobstore */
2295 	dev = init_dev();
2296 	spdk_bs_opts_init(&opts);
2297 
2298 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2299 	CU_ASSERT(g_bserrno == 0);
2300 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2301 
2302 	clusters = spdk_bs_total_data_cluster_count(g_bs);
2303 
2304 	/* Unload the blob store */
2305 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2306 	CU_ASSERT(g_bserrno == 0);
2307 	g_bs = NULL;
2308 
2309 	dev = init_dev();
2310 	/* Load an existing blob store */
2311 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2312 	CU_ASSERT(g_bserrno == 0);
2313 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2314 
2315 	CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters);
2316 
2317 	/* Create and resize blobs to make sure that useable cluster count won't change */
2318 	for (i = 0; i < 4; i++) {
2319 		g_bserrno = -1;
2320 		g_blobid = SPDK_BLOBID_INVALID;
2321 		spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2322 		CU_ASSERT(g_bserrno == 0);
2323 		CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
2324 
2325 		g_bserrno = -1;
2326 		g_blob = NULL;
2327 		spdk_bs_open_blob(g_bs, g_blobid, blob_op_with_handle_complete, NULL);
2328 		CU_ASSERT(g_bserrno == 0);
2329 		CU_ASSERT(g_blob !=  NULL);
2330 
2331 		spdk_blob_resize(g_blob, 10, blob_op_complete, NULL);
2332 		CU_ASSERT(g_bserrno == 0);
2333 
2334 		g_bserrno = -1;
2335 		spdk_blob_close(g_blob, blob_op_complete, NULL);
2336 		CU_ASSERT(g_bserrno == 0);
2337 
2338 		CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters);
2339 	}
2340 
2341 	/* Reload the blob store to make sure that nothing changed */
2342 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2343 	CU_ASSERT(g_bserrno == 0);
2344 	g_bs = NULL;
2345 
2346 	dev = init_dev();
2347 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2348 	CU_ASSERT(g_bserrno == 0);
2349 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2350 
2351 	CU_ASSERT(spdk_bs_total_data_cluster_count(g_bs) == clusters);
2352 
2353 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2354 	CU_ASSERT(g_bserrno == 0);
2355 	g_bs = NULL;
2356 }
2357 
2358 /*
2359  * Test resizing of the metadata blob.  This requires creating enough blobs
2360  *  so that one cluster is not enough to fit the metadata for those blobs.
2361  *  To induce this condition to happen more quickly, we reduce the cluster
2362  *  size to 16KB, which means only 4 4KB blob metadata pages can fit.
2363  */
2364 static void
2365 bs_resize_md(void)
2366 {
2367 	const int CLUSTER_PAGE_COUNT = 4;
2368 	const int NUM_BLOBS = CLUSTER_PAGE_COUNT * 4;
2369 	struct spdk_bs_dev *dev;
2370 	struct spdk_bs_opts opts;
2371 	uint32_t cluster_sz;
2372 	spdk_blob_id blobids[NUM_BLOBS];
2373 	int i;
2374 
2375 
2376 	dev = init_dev();
2377 	spdk_bs_opts_init(&opts);
2378 	opts.cluster_sz = CLUSTER_PAGE_COUNT * 4096;
2379 	cluster_sz = opts.cluster_sz;
2380 
2381 	/* Initialize a new blob store */
2382 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2383 	CU_ASSERT(g_bserrno == 0);
2384 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2385 
2386 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2387 
2388 	for (i = 0; i < NUM_BLOBS; i++) {
2389 		g_bserrno = -1;
2390 		g_blobid = SPDK_BLOBID_INVALID;
2391 		spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2392 		CU_ASSERT(g_bserrno == 0);
2393 		CU_ASSERT(g_blobid !=  SPDK_BLOBID_INVALID);
2394 		blobids[i] = g_blobid;
2395 	}
2396 
2397 	/* Unload the blob store */
2398 	g_bserrno = -1;
2399 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2400 	CU_ASSERT(g_bserrno == 0);
2401 
2402 	/* Load an existing blob store */
2403 	g_bserrno = -1;
2404 	g_bs = NULL;
2405 	dev = init_dev();
2406 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2407 	CU_ASSERT(g_bserrno == 0);
2408 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2409 
2410 	CU_ASSERT(spdk_bs_get_cluster_size(g_bs) == cluster_sz);
2411 
2412 	for (i = 0; i < NUM_BLOBS; i++) {
2413 		g_bserrno = -1;
2414 		g_blob = NULL;
2415 		spdk_bs_open_blob(g_bs, blobids[i], blob_op_with_handle_complete, NULL);
2416 		CU_ASSERT(g_bserrno == 0);
2417 		CU_ASSERT(g_blob !=  NULL);
2418 		g_bserrno = -1;
2419 		spdk_blob_close(g_blob, blob_op_complete, NULL);
2420 		CU_ASSERT(g_bserrno == 0);
2421 	}
2422 
2423 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2424 	CU_ASSERT(g_bserrno == 0);
2425 	g_bs = NULL;
2426 }
2427 
2428 static void
2429 bs_destroy(void)
2430 {
2431 	struct spdk_bs_dev *dev;
2432 	struct spdk_bs_opts opts;
2433 
2434 	/* Initialize a new blob store */
2435 	dev = init_dev();
2436 	spdk_bs_opts_init(&opts);
2437 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2438 	CU_ASSERT(g_bserrno == 0);
2439 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2440 
2441 	/* Destroy the blob store */
2442 	g_bserrno = -1;
2443 	spdk_bs_destroy(g_bs, bs_op_complete, NULL);
2444 	CU_ASSERT(g_bserrno == 0);
2445 
2446 	/* Loading an non-existent blob store should fail. */
2447 	g_bs = NULL;
2448 	dev = init_dev();
2449 
2450 	g_bserrno = 0;
2451 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2452 	CU_ASSERT(g_bserrno != 0);
2453 }
2454 
2455 /* Try to hit all of the corner cases associated with serializing
2456  * a blob to disk
2457  */
2458 static void
2459 blob_serialize(void)
2460 {
2461 	struct spdk_bs_dev *dev;
2462 	struct spdk_bs_opts opts;
2463 	struct spdk_blob_store *bs;
2464 	spdk_blob_id blobid[2];
2465 	struct spdk_blob *blob[2];
2466 	uint64_t i;
2467 	char *value;
2468 	int rc;
2469 
2470 	dev = init_dev();
2471 
2472 	/* Initialize a new blobstore with very small clusters */
2473 	spdk_bs_opts_init(&opts);
2474 	opts.cluster_sz = dev->blocklen * 8;
2475 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2476 	CU_ASSERT(g_bserrno == 0);
2477 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2478 	bs = g_bs;
2479 
2480 	/* Create and open two blobs */
2481 	for (i = 0; i < 2; i++) {
2482 		spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2483 		CU_ASSERT(g_bserrno == 0);
2484 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2485 		blobid[i] = g_blobid;
2486 
2487 		/* Open a blob */
2488 		spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL);
2489 		CU_ASSERT(g_bserrno == 0);
2490 		CU_ASSERT(g_blob != NULL);
2491 		blob[i] = g_blob;
2492 
2493 		/* Set a fairly large xattr on both blobs to eat up
2494 		 * metadata space
2495 		 */
2496 		value = calloc(dev->blocklen - 64, sizeof(char));
2497 		SPDK_CU_ASSERT_FATAL(value != NULL);
2498 		memset(value, i, dev->blocklen / 2);
2499 		rc = spdk_blob_set_xattr(blob[i], "name", value, dev->blocklen - 64);
2500 		CU_ASSERT(rc == 0);
2501 		free(value);
2502 	}
2503 
2504 	/* Resize the blobs, alternating 1 cluster at a time.
2505 	 * This thwarts run length encoding and will cause spill
2506 	 * over of the extents.
2507 	 */
2508 	for (i = 0; i < 6; i++) {
2509 		spdk_blob_resize(blob[i % 2], (i / 2) + 1, blob_op_complete, NULL);
2510 		CU_ASSERT(g_bserrno == 0);
2511 	}
2512 
2513 	for (i = 0; i < 2; i++) {
2514 		spdk_blob_sync_md(blob[i], blob_op_complete, NULL);
2515 		CU_ASSERT(g_bserrno == 0);
2516 	}
2517 
2518 	/* Close the blobs */
2519 	for (i = 0; i < 2; i++) {
2520 		spdk_blob_close(blob[i], blob_op_complete, NULL);
2521 		CU_ASSERT(g_bserrno == 0);
2522 	}
2523 
2524 	/* Unload the blobstore */
2525 	spdk_bs_unload(bs, bs_op_complete, NULL);
2526 	CU_ASSERT(g_bserrno == 0);
2527 	g_bs = NULL;
2528 	g_blob = NULL;
2529 	g_blobid = 0;
2530 	bs = NULL;
2531 
2532 	dev = init_dev();
2533 	/* Load an existing blob store */
2534 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2535 	CU_ASSERT(g_bserrno == 0);
2536 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2537 	bs = g_bs;
2538 
2539 	for (i = 0; i < 2; i++) {
2540 		blob[i] = NULL;
2541 
2542 		spdk_bs_open_blob(bs, blobid[i], blob_op_with_handle_complete, NULL);
2543 		CU_ASSERT(g_bserrno == 0);
2544 		CU_ASSERT(g_blob != NULL);
2545 		blob[i] = g_blob;
2546 
2547 		CU_ASSERT(spdk_blob_get_num_clusters(blob[i]) == 3);
2548 
2549 		spdk_blob_close(blob[i], blob_op_complete, NULL);
2550 		CU_ASSERT(g_bserrno == 0);
2551 	}
2552 
2553 	spdk_bs_unload(bs, bs_op_complete, NULL);
2554 	CU_ASSERT(g_bserrno == 0);
2555 	g_bs = NULL;
2556 }
2557 
2558 static void
2559 blob_crc(void)
2560 {
2561 	struct spdk_blob_store *bs;
2562 	struct spdk_bs_dev *dev;
2563 	struct spdk_blob *blob;
2564 	spdk_blob_id blobid;
2565 	uint32_t page_num;
2566 	int index;
2567 	struct spdk_blob_md_page *page;
2568 
2569 	dev = init_dev();
2570 
2571 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2572 	CU_ASSERT(g_bserrno == 0);
2573 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2574 	bs = g_bs;
2575 
2576 	spdk_bs_create_blob(bs, blob_op_with_id_complete, NULL);
2577 	CU_ASSERT(g_bserrno == 0);
2578 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2579 	blobid = g_blobid;
2580 
2581 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2582 	CU_ASSERT(g_bserrno == 0);
2583 	CU_ASSERT(g_blob != NULL);
2584 	blob = g_blob;
2585 
2586 	spdk_blob_close(blob, blob_op_complete, NULL);
2587 	CU_ASSERT(g_bserrno == 0);
2588 
2589 	page_num = _spdk_bs_blobid_to_page(blobid);
2590 	index = DEV_BUFFER_BLOCKLEN * (bs->md_start + page_num);
2591 	page = (struct spdk_blob_md_page *)&g_dev_buffer[index];
2592 	page->crc = 0;
2593 
2594 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
2595 	CU_ASSERT(g_bserrno == -EINVAL);
2596 	CU_ASSERT(g_blob == NULL);
2597 	g_bserrno = 0;
2598 
2599 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
2600 	CU_ASSERT(g_bserrno == -EINVAL);
2601 
2602 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2603 	CU_ASSERT(g_bserrno == 0);
2604 	g_bs = NULL;
2605 }
2606 
2607 static void
2608 super_block_crc(void)
2609 {
2610 	struct spdk_bs_dev *dev;
2611 	struct spdk_bs_super_block *super_block;
2612 	struct spdk_bs_opts opts;
2613 
2614 	dev = init_dev();
2615 	spdk_bs_opts_init(&opts);
2616 
2617 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2618 	CU_ASSERT(g_bserrno == 0);
2619 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2620 
2621 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2622 	CU_ASSERT(g_bserrno == 0);
2623 	g_bs = NULL;
2624 
2625 	super_block = (struct spdk_bs_super_block *)g_dev_buffer;
2626 	super_block->crc = 0;
2627 	dev = init_dev();
2628 
2629 	/* Load an existing blob store */
2630 	g_bserrno = 0;
2631 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2632 	CU_ASSERT(g_bserrno == -EILSEQ);
2633 }
2634 
2635 /* For blob dirty shutdown test case we do the following sub-test cases:
2636  * 1 Initialize new blob store and create 1 super blob with some xattrs, then we
2637  *   dirty shutdown and reload the blob store and verify the xattrs.
2638  * 2 Resize the blob from 10 clusters to 20 clusters and then dirty shutdown,
2639  *   reload the blob store and verify the clusters number.
2640  * 3 Create the second blob and then dirty shutdown, reload the blob store
2641  *   and verify the second blob.
2642  * 4 Delete the second blob and then dirty shutdown, reload the blob store
2643  *   and verify the second blob is invalid.
2644  * 5 Create the second blob again and also create the third blob, modify the
2645  *   md of second blob which makes the md invalid, and then dirty shutdown,
2646  *   reload the blob store verify the second blob, it should invalid and also
2647  *   verify the third blob, it should correct.
2648  */
2649 static void
2650 blob_dirty_shutdown(void)
2651 {
2652 	int rc;
2653 	int index;
2654 	struct spdk_bs_dev *dev;
2655 	spdk_blob_id blobid1, blobid2, blobid3;
2656 	struct spdk_blob *blob;
2657 	uint64_t length;
2658 	uint64_t free_clusters;
2659 	const void *value;
2660 	size_t value_len;
2661 	uint32_t page_num;
2662 	struct spdk_blob_md_page *page;
2663 	struct spdk_bs_opts opts;
2664 
2665 	dev = init_dev();
2666 	spdk_bs_opts_init(&opts);
2667 	/* Initialize a new blob store */
2668 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
2669 	CU_ASSERT(g_bserrno == 0);
2670 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2671 
2672 	/* Create first blob */
2673 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2674 	CU_ASSERT(g_bserrno == 0);
2675 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2676 	blobid1 = g_blobid;
2677 
2678 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
2679 	CU_ASSERT(g_bserrno == 0);
2680 	CU_ASSERT(g_blob != NULL);
2681 	blob = g_blob;
2682 
2683 	/* Set some xattrs */
2684 	rc = spdk_blob_set_xattr(blob, "name", "log.txt", strlen("log.txt") + 1);
2685 	CU_ASSERT(rc == 0);
2686 
2687 	length = 2345;
2688 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2689 	CU_ASSERT(rc == 0);
2690 
2691 	/* Resize the blob */
2692 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
2693 	CU_ASSERT(g_bserrno == 0);
2694 
2695 	/* Set the blob as the super blob */
2696 	spdk_bs_set_super(g_bs, blobid1, blob_op_complete, NULL);
2697 	CU_ASSERT(g_bserrno == 0);
2698 
2699 	free_clusters = spdk_bs_free_cluster_count(g_bs);
2700 
2701 	spdk_blob_close(blob, blob_op_complete, NULL);
2702 	blob = NULL;
2703 	g_blob = NULL;
2704 	g_blobid = SPDK_BLOBID_INVALID;
2705 
2706 	/* Dirty shutdown */
2707 	_spdk_bs_free(g_bs);
2708 
2709 	/* reload blobstore */
2710 	dev = init_dev();
2711 	spdk_bs_opts_init(&opts);
2712 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2713 	CU_ASSERT(g_bserrno == 0);
2714 
2715 	/* Get the super blob */
2716 	spdk_bs_get_super(g_bs, blob_op_with_id_complete, NULL);
2717 	CU_ASSERT(g_bserrno == 0);
2718 	CU_ASSERT(blobid1 == g_blobid);
2719 
2720 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
2721 	CU_ASSERT(g_bserrno == 0);
2722 	CU_ASSERT(g_blob != NULL);
2723 	blob = g_blob;
2724 
2725 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
2726 
2727 	/* Get the xattrs */
2728 	value = NULL;
2729 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2730 	CU_ASSERT(rc == 0);
2731 	SPDK_CU_ASSERT_FATAL(value != NULL);
2732 	CU_ASSERT(*(uint64_t *)value == length);
2733 	CU_ASSERT(value_len == 8);
2734 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
2735 
2736 	/* Resize the blob */
2737 	spdk_blob_resize(blob, 20, blob_op_complete, NULL);
2738 	CU_ASSERT(g_bserrno == 0);
2739 
2740 	free_clusters = spdk_bs_free_cluster_count(g_bs);
2741 
2742 	spdk_blob_close(blob, blob_op_complete, NULL);
2743 	CU_ASSERT(g_bserrno == 0);
2744 	blob = NULL;
2745 	g_blob = NULL;
2746 	g_blobid = SPDK_BLOBID_INVALID;
2747 
2748 	/* Dirty shutdown */
2749 	_spdk_bs_free(g_bs);
2750 
2751 	/* reload the blobstore */
2752 	dev = init_dev();
2753 	spdk_bs_opts_init(&opts);
2754 	/* Load an existing blob store */
2755 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2756 	CU_ASSERT(g_bserrno == 0);
2757 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2758 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
2759 	CU_ASSERT(g_bserrno == 0);
2760 	CU_ASSERT(g_blob != NULL);
2761 	blob = g_blob;
2762 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 20);
2763 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
2764 
2765 	spdk_blob_close(blob, blob_op_complete, NULL);
2766 	CU_ASSERT(g_bserrno == 0);
2767 	blob = NULL;
2768 	g_blob = NULL;
2769 	g_blobid = SPDK_BLOBID_INVALID;
2770 
2771 	/* Create second blob */
2772 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2773 	CU_ASSERT(g_bserrno == 0);
2774 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2775 	blobid2 = g_blobid;
2776 
2777 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
2778 	CU_ASSERT(g_bserrno == 0);
2779 	CU_ASSERT(g_blob != NULL);
2780 	blob = g_blob;
2781 
2782 	/* Set some xattrs */
2783 	rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1);
2784 	CU_ASSERT(rc == 0);
2785 
2786 	length = 5432;
2787 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2788 	CU_ASSERT(rc == 0);
2789 
2790 	/* Resize the blob */
2791 	spdk_blob_resize(blob, 10, blob_op_complete, NULL);
2792 	CU_ASSERT(g_bserrno == 0);
2793 
2794 	free_clusters = spdk_bs_free_cluster_count(g_bs);
2795 
2796 	spdk_blob_close(blob, blob_op_complete, NULL);
2797 	blob = NULL;
2798 	g_blob = NULL;
2799 	g_blobid = SPDK_BLOBID_INVALID;
2800 
2801 	/* Dirty shutdown */
2802 	_spdk_bs_free(g_bs);
2803 
2804 	/* reload the blobstore */
2805 	dev = init_dev();
2806 	spdk_bs_opts_init(&opts);
2807 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2808 	CU_ASSERT(g_bserrno == 0);
2809 
2810 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
2811 	CU_ASSERT(g_bserrno == 0);
2812 	CU_ASSERT(g_blob != NULL);
2813 	blob = g_blob;
2814 
2815 	/* Get the xattrs */
2816 	value = NULL;
2817 	rc = spdk_blob_get_xattr_value(blob, "length", &value, &value_len);
2818 	CU_ASSERT(rc == 0);
2819 	SPDK_CU_ASSERT_FATAL(value != NULL);
2820 	CU_ASSERT(*(uint64_t *)value == length);
2821 	CU_ASSERT(value_len == 8);
2822 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 10);
2823 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
2824 
2825 	spdk_blob_close(blob, blob_op_complete, NULL);
2826 	CU_ASSERT(g_bserrno == 0);
2827 	spdk_bs_delete_blob(g_bs, blobid2, blob_op_complete, NULL);
2828 	CU_ASSERT(g_bserrno == 0);
2829 
2830 	free_clusters = spdk_bs_free_cluster_count(g_bs);
2831 
2832 	/* Dirty shutdown */
2833 	_spdk_bs_free(g_bs);
2834 	/* reload the blobstore */
2835 	dev = init_dev();
2836 	spdk_bs_opts_init(&opts);
2837 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2838 	CU_ASSERT(g_bserrno == 0);
2839 
2840 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
2841 	CU_ASSERT(g_bserrno != 0);
2842 	CU_ASSERT(g_blob == NULL);
2843 
2844 	spdk_bs_open_blob(g_bs, blobid1, blob_op_with_handle_complete, NULL);
2845 	CU_ASSERT(g_bserrno == 0);
2846 	CU_ASSERT(g_blob != NULL);
2847 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
2848 	spdk_blob_close(g_blob, blob_op_complete, NULL);
2849 	CU_ASSERT(g_bserrno == 0);
2850 
2851 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2852 	CU_ASSERT(g_bserrno == 0);
2853 	g_bs = NULL;
2854 
2855 	/* reload the blobstore */
2856 	dev = init_dev();
2857 	spdk_bs_opts_init(&opts);
2858 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2859 	CU_ASSERT(g_bserrno == 0);
2860 
2861 	/* Create second blob */
2862 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2863 	CU_ASSERT(g_bserrno == 0);
2864 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2865 	blobid2 = g_blobid;
2866 
2867 	/* Create third blob */
2868 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2869 	CU_ASSERT(g_bserrno == 0);
2870 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2871 	blobid3 = g_blobid;
2872 
2873 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
2874 	CU_ASSERT(g_bserrno == 0);
2875 	CU_ASSERT(g_blob != NULL);
2876 	blob = g_blob;
2877 
2878 	/* Set some xattrs for second blob */
2879 	rc = spdk_blob_set_xattr(blob, "name", "log1.txt", strlen("log1.txt") + 1);
2880 	CU_ASSERT(rc == 0);
2881 
2882 	length = 5432;
2883 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2884 	CU_ASSERT(rc == 0);
2885 
2886 	spdk_blob_close(blob, blob_op_complete, NULL);
2887 	blob = NULL;
2888 	g_blob = NULL;
2889 	g_blobid = SPDK_BLOBID_INVALID;
2890 
2891 	spdk_bs_open_blob(g_bs, blobid3, blob_op_with_handle_complete, NULL);
2892 	CU_ASSERT(g_bserrno == 0);
2893 	CU_ASSERT(g_blob != NULL);
2894 	blob = g_blob;
2895 
2896 	/* Set some xattrs for third blob */
2897 	rc = spdk_blob_set_xattr(blob, "name", "log2.txt", strlen("log2.txt") + 1);
2898 	CU_ASSERT(rc == 0);
2899 
2900 	length = 5432;
2901 	rc = spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
2902 	CU_ASSERT(rc == 0);
2903 
2904 	spdk_blob_close(blob, blob_op_complete, NULL);
2905 	blob = NULL;
2906 	g_blob = NULL;
2907 	g_blobid = SPDK_BLOBID_INVALID;
2908 
2909 	/* Mark second blob as invalid */
2910 	page_num = _spdk_bs_blobid_to_page(blobid2);
2911 
2912 	index = DEV_BUFFER_BLOCKLEN * (g_bs->md_start + page_num);
2913 	page = (struct spdk_blob_md_page *)&g_dev_buffer[index];
2914 	page->sequence_num = 1;
2915 	page->crc = _spdk_blob_md_page_calc_crc(page);
2916 
2917 	free_clusters = spdk_bs_free_cluster_count(g_bs);
2918 
2919 	/* Dirty shutdown */
2920 	_spdk_bs_free(g_bs);
2921 	/* reload the blobstore */
2922 	dev = init_dev();
2923 	spdk_bs_opts_init(&opts);
2924 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
2925 	CU_ASSERT(g_bserrno == 0);
2926 
2927 	spdk_bs_open_blob(g_bs, blobid2, blob_op_with_handle_complete, NULL);
2928 	CU_ASSERT(g_bserrno != 0);
2929 	CU_ASSERT(g_blob == NULL);
2930 
2931 	spdk_bs_open_blob(g_bs, blobid3, blob_op_with_handle_complete, NULL);
2932 	CU_ASSERT(g_bserrno == 0);
2933 	CU_ASSERT(g_blob != NULL);
2934 	blob = g_blob;
2935 
2936 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(g_bs));
2937 
2938 	spdk_blob_close(blob, blob_op_complete, NULL);
2939 	blob = NULL;
2940 	g_blob = NULL;
2941 	g_blobid = SPDK_BLOBID_INVALID;
2942 
2943 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
2944 	CU_ASSERT(g_bserrno == 0);
2945 	g_bs = NULL;
2946 }
2947 
2948 static void
2949 blob_flags(void)
2950 {
2951 	struct spdk_bs_dev *dev;
2952 	spdk_blob_id blobid_invalid, blobid_data_ro, blobid_md_ro;
2953 	struct spdk_blob *blob_invalid, *blob_data_ro, *blob_md_ro;
2954 	struct spdk_bs_opts opts;
2955 	int rc;
2956 
2957 	dev = init_dev();
2958 	spdk_bs_opts_init(&opts);
2959 
2960 	/* Initialize a new blob store */
2961 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
2962 	CU_ASSERT(g_bserrno == 0);
2963 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
2964 
2965 	/* Create three blobs - one each for testing invalid, data_ro and md_ro flags. */
2966 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2967 	CU_ASSERT(g_bserrno == 0);
2968 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2969 	blobid_invalid = g_blobid;
2970 
2971 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2972 	CU_ASSERT(g_bserrno == 0);
2973 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2974 	blobid_data_ro = g_blobid;
2975 
2976 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
2977 	CU_ASSERT(g_bserrno == 0);
2978 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
2979 	blobid_md_ro = g_blobid;
2980 
2981 	spdk_bs_open_blob(g_bs, blobid_invalid, blob_op_with_handle_complete, NULL);
2982 	CU_ASSERT(g_bserrno == 0);
2983 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2984 	blob_invalid = g_blob;
2985 
2986 	spdk_bs_open_blob(g_bs, blobid_data_ro, blob_op_with_handle_complete, NULL);
2987 	CU_ASSERT(g_bserrno == 0);
2988 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2989 	blob_data_ro = g_blob;
2990 
2991 	spdk_bs_open_blob(g_bs, blobid_md_ro, blob_op_with_handle_complete, NULL);
2992 	CU_ASSERT(g_bserrno == 0);
2993 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
2994 	blob_md_ro = g_blob;
2995 
2996 	/* Change the size of blob_data_ro to check if flags are serialized
2997 	 * when blob has non zero number of extents */
2998 	spdk_blob_resize(blob_data_ro, 10, blob_op_complete, NULL);
2999 	CU_ASSERT(g_bserrno == 0);
3000 
3001 	/* Set the xattr to check if flags are serialized
3002 	 * when blob has non zero number of xattrs */
3003 	rc = spdk_blob_set_xattr(blob_md_ro, "name", "log.txt", strlen("log.txt") + 1);
3004 	CU_ASSERT(rc == 0);
3005 
3006 	blob_invalid->invalid_flags = (1ULL << 63);
3007 	blob_invalid->state = SPDK_BLOB_STATE_DIRTY;
3008 	blob_data_ro->data_ro_flags = (1ULL << 62);
3009 	blob_data_ro->state = SPDK_BLOB_STATE_DIRTY;
3010 	blob_md_ro->md_ro_flags = (1ULL << 61);
3011 	blob_md_ro->state = SPDK_BLOB_STATE_DIRTY;
3012 
3013 	g_bserrno = -1;
3014 	spdk_blob_sync_md(blob_invalid, blob_op_complete, NULL);
3015 	CU_ASSERT(g_bserrno == 0);
3016 	g_bserrno = -1;
3017 	spdk_blob_sync_md(blob_data_ro, blob_op_complete, NULL);
3018 	CU_ASSERT(g_bserrno == 0);
3019 	g_bserrno = -1;
3020 	spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL);
3021 	CU_ASSERT(g_bserrno == 0);
3022 
3023 	g_bserrno = -1;
3024 	spdk_blob_close(blob_invalid, blob_op_complete, NULL);
3025 	CU_ASSERT(g_bserrno == 0);
3026 	blob_invalid = NULL;
3027 	g_bserrno = -1;
3028 	spdk_blob_close(blob_data_ro, blob_op_complete, NULL);
3029 	CU_ASSERT(g_bserrno == 0);
3030 	blob_data_ro = NULL;
3031 	g_bserrno = -1;
3032 	spdk_blob_close(blob_md_ro, blob_op_complete, NULL);
3033 	CU_ASSERT(g_bserrno == 0);
3034 	blob_md_ro = NULL;
3035 
3036 	g_blob = NULL;
3037 	g_blobid = SPDK_BLOBID_INVALID;
3038 
3039 	/* Unload the blob store */
3040 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3041 	CU_ASSERT(g_bserrno == 0);
3042 	g_bs = NULL;
3043 
3044 	/* Load an existing blob store */
3045 	dev = init_dev();
3046 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3047 	CU_ASSERT(g_bserrno == 0);
3048 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3049 
3050 	g_blob = NULL;
3051 	g_bserrno = 0;
3052 	spdk_bs_open_blob(g_bs, blobid_invalid, blob_op_with_handle_complete, NULL);
3053 	CU_ASSERT(g_bserrno != 0);
3054 	CU_ASSERT(g_blob == NULL);
3055 
3056 	g_blob = NULL;
3057 	g_bserrno = -1;
3058 	spdk_bs_open_blob(g_bs, blobid_data_ro, blob_op_with_handle_complete, NULL);
3059 	CU_ASSERT(g_bserrno == 0);
3060 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3061 	blob_data_ro = g_blob;
3062 	/* If an unknown data_ro flag was found, the blob should be marked both data and md read-only. */
3063 	CU_ASSERT(blob_data_ro->data_ro == true);
3064 	CU_ASSERT(blob_data_ro->md_ro == true);
3065 	CU_ASSERT(spdk_blob_get_num_clusters(blob_data_ro) == 10);
3066 
3067 	g_blob = NULL;
3068 	g_bserrno = -1;
3069 	spdk_bs_open_blob(g_bs, blobid_md_ro, blob_op_with_handle_complete, NULL);
3070 	CU_ASSERT(g_bserrno == 0);
3071 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3072 	blob_md_ro = g_blob;
3073 	CU_ASSERT(blob_md_ro->data_ro == false);
3074 	CU_ASSERT(blob_md_ro->md_ro == true);
3075 
3076 	g_bserrno = -1;
3077 	spdk_blob_sync_md(blob_md_ro, blob_op_complete, NULL);
3078 	CU_ASSERT(g_bserrno == 0);
3079 
3080 	spdk_blob_close(blob_data_ro, blob_op_complete, NULL);
3081 	CU_ASSERT(g_bserrno == 0);
3082 	spdk_blob_close(blob_md_ro, blob_op_complete, NULL);
3083 	CU_ASSERT(g_bserrno == 0);
3084 
3085 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3086 	CU_ASSERT(g_bserrno == 0);
3087 }
3088 
3089 static void
3090 bs_version(void)
3091 {
3092 	struct spdk_bs_super_block *super;
3093 	struct spdk_bs_dev *dev;
3094 	struct spdk_bs_opts opts;
3095 	spdk_blob_id blobid;
3096 
3097 	dev = init_dev();
3098 	spdk_bs_opts_init(&opts);
3099 
3100 	/* Initialize a new blob store */
3101 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3102 	CU_ASSERT(g_bserrno == 0);
3103 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3104 
3105 	/* Unload the blob store */
3106 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3107 	CU_ASSERT(g_bserrno == 0);
3108 	g_bs = NULL;
3109 
3110 	/*
3111 	 * Change the bs version on disk.  This will allow us to
3112 	 *  test that the version does not get modified automatically
3113 	 *  when loading and unloading the blobstore.
3114 	 */
3115 	super = (struct spdk_bs_super_block *)&g_dev_buffer[0];
3116 	CU_ASSERT(super->version == SPDK_BS_VERSION);
3117 	CU_ASSERT(super->clean == 1);
3118 	super->version = 2;
3119 	/*
3120 	 * Version 2 metadata does not have a used blobid mask, so clear
3121 	 *  those fields in the super block and zero the corresponding
3122 	 *  region on "disk".  We will use this to ensure blob IDs are
3123 	 *  correctly reconstructed.
3124 	 */
3125 	memset(&g_dev_buffer[super->used_blobid_mask_start * SPDK_BS_PAGE_SIZE], 0,
3126 	       super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE);
3127 	super->used_blobid_mask_start = 0;
3128 	super->used_blobid_mask_len = 0;
3129 	super->crc = _spdk_blob_md_page_calc_crc(super);
3130 
3131 	/* Load an existing blob store */
3132 	dev = init_dev();
3133 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3134 	CU_ASSERT(g_bserrno == 0);
3135 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3136 	CU_ASSERT(super->clean == 0);
3137 
3138 	/*
3139 	 * Create a blob - just to make sure that when we unload it
3140 	 *  results in writing the super block (since metadata pages
3141 	 *  were allocated.
3142 	 */
3143 	spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3144 	CU_ASSERT(g_bserrno == 0);
3145 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3146 	blobid = g_blobid;
3147 
3148 	/* Unload the blob store */
3149 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3150 	CU_ASSERT(g_bserrno == 0);
3151 	g_bs = NULL;
3152 	CU_ASSERT(super->version == 2);
3153 	CU_ASSERT(super->used_blobid_mask_start == 0);
3154 	CU_ASSERT(super->used_blobid_mask_len == 0);
3155 
3156 	dev = init_dev();
3157 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3158 	CU_ASSERT(g_bserrno == 0);
3159 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3160 
3161 	g_blob = NULL;
3162 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
3163 	CU_ASSERT(g_bserrno == 0);
3164 	CU_ASSERT(g_blob != NULL);
3165 
3166 	spdk_blob_close(g_blob, blob_op_complete, NULL);
3167 	CU_ASSERT(g_bserrno == 0);
3168 
3169 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3170 	CU_ASSERT(g_bserrno == 0);
3171 	g_bs = NULL;
3172 	CU_ASSERT(super->version == 2);
3173 	CU_ASSERT(super->used_blobid_mask_start == 0);
3174 	CU_ASSERT(super->used_blobid_mask_len == 0);
3175 }
3176 
3177 static void
3178 blob_set_xattrs(void)
3179 {
3180 	struct spdk_blob_store *bs;
3181 	struct spdk_bs_dev *dev;
3182 	struct spdk_blob *blob;
3183 	struct spdk_blob_opts opts;
3184 	spdk_blob_id blobid;
3185 	const void *value;
3186 	size_t value_len;
3187 	int rc;
3188 
3189 	dev = init_dev();
3190 
3191 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3192 	CU_ASSERT(g_bserrno == 0);
3193 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3194 	bs = g_bs;
3195 
3196 	/* Create blob with extra attributes */
3197 	spdk_blob_opts_init(&opts);
3198 
3199 	opts.xattrs.names = g_xattr_names;
3200 	opts.xattrs.get_value = _get_xattr_value;
3201 	opts.xattrs.count = 3;
3202 	opts.xattrs.ctx = &g_ctx;
3203 
3204 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3205 	CU_ASSERT(g_bserrno == 0);
3206 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3207 	blobid = g_blobid;
3208 
3209 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3210 	CU_ASSERT(g_bserrno == 0);
3211 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3212 	blob = g_blob;
3213 
3214 	/* Get the xattrs */
3215 	value = NULL;
3216 
3217 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[0], &value, &value_len);
3218 	CU_ASSERT(rc == 0);
3219 	SPDK_CU_ASSERT_FATAL(value != NULL);
3220 	CU_ASSERT(value_len == strlen(g_xattr_values[0]));
3221 	CU_ASSERT_NSTRING_EQUAL_FATAL(value, g_xattr_values[0], value_len);
3222 
3223 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[1], &value, &value_len);
3224 	CU_ASSERT(rc == 0);
3225 	SPDK_CU_ASSERT_FATAL(value != NULL);
3226 	CU_ASSERT(value_len == strlen(g_xattr_values[1]));
3227 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[1], value_len);
3228 
3229 	rc = spdk_blob_get_xattr_value(blob, g_xattr_names[2], &value, &value_len);
3230 	CU_ASSERT(rc == 0);
3231 	SPDK_CU_ASSERT_FATAL(value != NULL);
3232 	CU_ASSERT(value_len == strlen(g_xattr_values[2]));
3233 	CU_ASSERT_NSTRING_EQUAL((char *)value, g_xattr_values[2], value_len);
3234 
3235 	/* Try to get non existing attribute */
3236 
3237 	rc = spdk_blob_get_xattr_value(blob, "foobar", &value, &value_len);
3238 	CU_ASSERT(rc == -ENOENT);
3239 
3240 	spdk_blob_close(blob, blob_op_complete, NULL);
3241 	CU_ASSERT(g_bserrno == 0);
3242 	blob = NULL;
3243 	g_blob = NULL;
3244 	g_blobid = SPDK_BLOBID_INVALID;
3245 
3246 	/* NULL callback */
3247 	spdk_blob_opts_init(&opts);
3248 	opts.xattrs.names = g_xattr_names;
3249 	opts.xattrs.get_value = NULL;
3250 	opts.xattrs.count = 1;
3251 	opts.xattrs.ctx = &g_ctx;
3252 
3253 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3254 	CU_ASSERT(g_bserrno == -EINVAL);
3255 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3256 
3257 	/* NULL values */
3258 	spdk_blob_opts_init(&opts);
3259 	opts.xattrs.names = g_xattr_names;
3260 	opts.xattrs.get_value = _get_xattr_value_null;
3261 	opts.xattrs.count = 1;
3262 	opts.xattrs.ctx = NULL;
3263 
3264 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3265 	CU_ASSERT(g_bserrno == -EINVAL);
3266 
3267 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3268 	CU_ASSERT(g_bserrno == 0);
3269 	g_bs = NULL;
3270 
3271 }
3272 
3273 static void
3274 blob_thin_prov_alloc(void)
3275 {
3276 	struct spdk_blob_store *bs;
3277 	struct spdk_bs_dev *dev;
3278 	struct spdk_blob *blob;
3279 	struct spdk_blob_opts opts;
3280 	spdk_blob_id blobid;
3281 	uint64_t free_clusters;
3282 
3283 	dev = init_dev();
3284 
3285 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3286 	CU_ASSERT(g_bserrno == 0);
3287 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3288 	bs = g_bs;
3289 	free_clusters = spdk_bs_free_cluster_count(bs);
3290 
3291 	/* Set blob as thin provisioned */
3292 	spdk_blob_opts_init(&opts);
3293 	opts.thin_provision = true;
3294 
3295 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3296 	CU_ASSERT(g_bserrno == 0);
3297 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3298 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3299 	blobid = g_blobid;
3300 
3301 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3302 	CU_ASSERT(g_bserrno == 0);
3303 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3304 	blob = g_blob;
3305 
3306 	CU_ASSERT(blob->active.num_clusters == 0);
3307 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 0);
3308 
3309 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
3310 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
3311 	CU_ASSERT(g_bserrno == 0);
3312 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3313 	CU_ASSERT(blob->active.num_clusters == 5);
3314 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
3315 
3316 	/* Shrink the blob to 3 clusters - still unallocated */
3317 	spdk_blob_resize(blob, 3, blob_op_complete, NULL);
3318 	CU_ASSERT(g_bserrno == 0);
3319 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3320 	CU_ASSERT(blob->active.num_clusters == 3);
3321 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3);
3322 
3323 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
3324 	CU_ASSERT(g_bserrno == 0);
3325 	/* Sync must not change anything */
3326 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3327 	CU_ASSERT(blob->active.num_clusters == 3);
3328 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 3);
3329 
3330 	spdk_blob_close(blob, blob_op_complete, NULL);
3331 	CU_ASSERT(g_bserrno == 0);
3332 
3333 	/* Unload the blob store */
3334 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3335 	CU_ASSERT(g_bserrno == 0);
3336 	g_bs = NULL;
3337 	g_blob = NULL;
3338 	g_blobid = 0;
3339 
3340 	/* Load an existing blob store */
3341 	dev = init_dev();
3342 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3343 	CU_ASSERT(g_bserrno == 0);
3344 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3345 
3346 	bs = g_bs;
3347 
3348 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
3349 	CU_ASSERT(g_bserrno == 0);
3350 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3351 	blob = g_blob;
3352 
3353 	/* Check that clusters allocation and size is still the same */
3354 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3355 	CU_ASSERT(blob->active.num_clusters == 3);
3356 
3357 	spdk_blob_close(blob, blob_op_complete, NULL);
3358 	CU_ASSERT(g_bserrno == 0);
3359 
3360 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3361 	CU_ASSERT(g_bserrno == 0);
3362 
3363 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3364 	CU_ASSERT(g_bserrno == 0);
3365 	g_bs = NULL;
3366 }
3367 
3368 static void
3369 blob_insert_cluster_msg(void)
3370 {
3371 	struct spdk_blob_store *bs;
3372 	struct spdk_bs_dev *dev;
3373 	struct spdk_blob *blob;
3374 	struct spdk_blob_opts opts;
3375 	spdk_blob_id blobid;
3376 	uint64_t free_clusters;
3377 
3378 	dev = init_dev();
3379 
3380 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3381 	CU_ASSERT(g_bserrno == 0);
3382 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3383 	bs = g_bs;
3384 	free_clusters = spdk_bs_free_cluster_count(bs);
3385 
3386 	/* Set blob as thin provisioned */
3387 	spdk_blob_opts_init(&opts);
3388 	opts.thin_provision = true;
3389 	opts.num_clusters = 4;
3390 
3391 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3392 	CU_ASSERT(g_bserrno == 0);
3393 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3394 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3395 	blobid = g_blobid;
3396 
3397 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3398 	CU_ASSERT(g_bserrno == 0);
3399 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3400 	blob = g_blob;
3401 
3402 	CU_ASSERT(blob->active.num_clusters == 4);
3403 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 4);
3404 	CU_ASSERT(blob->active.clusters[1] == 0);
3405 
3406 	_spdk_bs_claim_cluster(bs, 0xF);
3407 	_spdk_blob_insert_cluster_on_md_thread(blob, 1, 0xF, blob_op_complete, NULL);
3408 
3409 	CU_ASSERT(blob->active.clusters[1] != 0);
3410 
3411 	spdk_blob_close(blob, blob_op_complete, NULL);
3412 	CU_ASSERT(g_bserrno == 0);
3413 
3414 	/* Unload the blob store */
3415 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3416 	CU_ASSERT(g_bserrno == 0);
3417 	g_bs = NULL;
3418 	g_blob = NULL;
3419 	g_blobid = 0;
3420 
3421 	/* Load an existing blob store */
3422 	dev = init_dev();
3423 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
3424 	CU_ASSERT(g_bserrno == 0);
3425 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3426 
3427 	bs = g_bs;
3428 
3429 	spdk_bs_open_blob(g_bs, blobid, blob_op_with_handle_complete, NULL);
3430 	CU_ASSERT(g_bserrno == 0);
3431 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3432 	blob = g_blob;
3433 
3434 	CU_ASSERT(blob->active.clusters[1] != 0);
3435 
3436 	spdk_blob_close(blob, blob_op_complete, NULL);
3437 	CU_ASSERT(g_bserrno == 0);
3438 
3439 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3440 	CU_ASSERT(g_bserrno == 0);
3441 
3442 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3443 	CU_ASSERT(g_bserrno == 0);
3444 	g_bs = NULL;
3445 }
3446 
3447 static void
3448 blob_thin_prov_rw(void)
3449 {
3450 	static const uint8_t zero[10 * 4096] = { 0 };
3451 	struct spdk_blob_store *bs;
3452 	struct spdk_bs_dev *dev;
3453 	struct spdk_blob *blob;
3454 	struct spdk_io_channel *channel;
3455 	struct spdk_blob_opts opts;
3456 	spdk_blob_id blobid;
3457 	uint64_t free_clusters;
3458 	uint8_t payload_read[10 * 4096];
3459 	uint8_t payload_write[10 * 4096];
3460 
3461 	dev = init_dev();
3462 
3463 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3464 	CU_ASSERT(g_bserrno == 0);
3465 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3466 	bs = g_bs;
3467 	free_clusters = spdk_bs_free_cluster_count(bs);
3468 
3469 	channel = spdk_bs_alloc_io_channel(bs);
3470 	CU_ASSERT(channel != NULL);
3471 
3472 	spdk_blob_opts_init(&opts);
3473 	opts.thin_provision = true;
3474 
3475 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3476 	CU_ASSERT(g_bserrno == 0);
3477 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3478 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3479 	blobid = g_blobid;
3480 
3481 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3482 	CU_ASSERT(g_bserrno == 0);
3483 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3484 	blob = g_blob;
3485 
3486 	CU_ASSERT(blob->active.num_clusters == 0);
3487 
3488 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
3489 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
3490 	CU_ASSERT(g_bserrno == 0);
3491 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3492 	CU_ASSERT(blob->active.num_clusters == 5);
3493 
3494 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
3495 	CU_ASSERT(g_bserrno == 0);
3496 	/* Sync must not change anything */
3497 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3498 	CU_ASSERT(blob->active.num_clusters == 5);
3499 
3500 	/* Payload should be all zeros from unallocated clusters */
3501 	memset(payload_read, 0xFF, sizeof(payload_read));
3502 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
3503 	CU_ASSERT(g_bserrno == 0);
3504 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
3505 
3506 	memset(payload_write, 0xE5, sizeof(payload_write));
3507 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
3508 	CU_ASSERT(g_bserrno == 0);
3509 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
3510 
3511 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
3512 	CU_ASSERT(g_bserrno == 0);
3513 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
3514 
3515 	spdk_blob_close(blob, blob_op_complete, NULL);
3516 	CU_ASSERT(g_bserrno == 0);
3517 
3518 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3519 	CU_ASSERT(g_bserrno == 0);
3520 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3521 
3522 	spdk_bs_free_io_channel(channel);
3523 
3524 	/* Unload the blob store */
3525 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3526 	CU_ASSERT(g_bserrno == 0);
3527 	g_bs = NULL;
3528 	g_blob = NULL;
3529 	g_blobid = 0;
3530 }
3531 
3532 static void
3533 blob_thin_prov_rw_iov(void)
3534 {
3535 	static const uint8_t zero[10 * 4096] = { 0 };
3536 	struct spdk_blob_store *bs;
3537 	struct spdk_bs_dev *dev;
3538 	struct spdk_blob *blob;
3539 	struct spdk_io_channel *channel;
3540 	struct spdk_blob_opts opts;
3541 	spdk_blob_id blobid;
3542 	uint64_t free_clusters;
3543 	uint8_t payload_read[10 * 4096];
3544 	uint8_t payload_write[10 * 4096];
3545 	struct iovec iov_read[3];
3546 	struct iovec iov_write[3];
3547 
3548 	dev = init_dev();
3549 
3550 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3551 	CU_ASSERT(g_bserrno == 0);
3552 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3553 	bs = g_bs;
3554 	free_clusters = spdk_bs_free_cluster_count(bs);
3555 
3556 	channel = spdk_bs_alloc_io_channel(bs);
3557 	CU_ASSERT(channel != NULL);
3558 
3559 	spdk_blob_opts_init(&opts);
3560 	opts.thin_provision = true;
3561 
3562 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3563 	CU_ASSERT(g_bserrno == 0);
3564 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3565 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3566 	blobid = g_blobid;
3567 
3568 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3569 	CU_ASSERT(g_bserrno == 0);
3570 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3571 	blob = g_blob;
3572 
3573 	CU_ASSERT(blob->active.num_clusters == 0);
3574 
3575 	/* The blob started at 0 clusters. Resize it to be 5, but still unallocated. */
3576 	spdk_blob_resize(blob, 5, blob_op_complete, NULL);
3577 	CU_ASSERT(g_bserrno == 0);
3578 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3579 	CU_ASSERT(blob->active.num_clusters == 5);
3580 
3581 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
3582 	CU_ASSERT(g_bserrno == 0);
3583 	/* Sync must not change anything */
3584 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3585 	CU_ASSERT(blob->active.num_clusters == 5);
3586 
3587 	/* Payload should be all zeros from unallocated clusters */
3588 	memset(payload_read, 0xAA, sizeof(payload_read));
3589 	iov_read[0].iov_base = payload_read;
3590 	iov_read[0].iov_len = 3 * 4096;
3591 	iov_read[1].iov_base = payload_read + 3 * 4096;
3592 	iov_read[1].iov_len = 4 * 4096;
3593 	iov_read[2].iov_base = payload_read + 7 * 4096;
3594 	iov_read[2].iov_len = 3 * 4096;
3595 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
3596 	CU_ASSERT(g_bserrno == 0);
3597 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
3598 
3599 	memset(payload_write, 0xE5, sizeof(payload_write));
3600 	iov_write[0].iov_base = payload_write;
3601 	iov_write[0].iov_len = 1 * 4096;
3602 	iov_write[1].iov_base = payload_write + 1 * 4096;
3603 	iov_write[1].iov_len = 5 * 4096;
3604 	iov_write[2].iov_base = payload_write + 6 * 4096;
3605 	iov_write[2].iov_len = 4 * 4096;
3606 
3607 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
3608 	CU_ASSERT(g_bserrno == 0);
3609 
3610 	memset(payload_read, 0xAA, sizeof(payload_read));
3611 	iov_read[0].iov_base = payload_read;
3612 	iov_read[0].iov_len = 3 * 4096;
3613 	iov_read[1].iov_base = payload_read + 3 * 4096;
3614 	iov_read[1].iov_len = 4 * 4096;
3615 	iov_read[2].iov_base = payload_read + 7 * 4096;
3616 	iov_read[2].iov_len = 3 * 4096;
3617 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
3618 	CU_ASSERT(g_bserrno == 0);
3619 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
3620 
3621 	spdk_blob_close(blob, blob_op_complete, NULL);
3622 	CU_ASSERT(g_bserrno == 0);
3623 
3624 	spdk_bs_free_io_channel(channel);
3625 
3626 	/* Unload the blob store */
3627 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3628 	CU_ASSERT(g_bserrno == 0);
3629 	g_bs = NULL;
3630 	g_blob = NULL;
3631 	g_blobid = 0;
3632 }
3633 
3634 struct iter_ctx {
3635 	int		current_iter;
3636 	spdk_blob_id	blobid[4];
3637 };
3638 
3639 static void
3640 test_iter(void *arg, struct spdk_blob *blob, int bserrno)
3641 {
3642 	struct iter_ctx *iter_ctx = arg;
3643 	spdk_blob_id blobid;
3644 
3645 	CU_ASSERT(bserrno == 0);
3646 	blobid = spdk_blob_get_id(blob);
3647 	CU_ASSERT(blobid == iter_ctx->blobid[iter_ctx->current_iter++]);
3648 }
3649 
3650 static void
3651 bs_load_iter(void)
3652 {
3653 	struct spdk_bs_dev *dev;
3654 	struct iter_ctx iter_ctx = { 0 };
3655 	struct spdk_blob *blob;
3656 	int i, rc;
3657 	struct spdk_bs_opts opts;
3658 
3659 	dev = init_dev();
3660 	spdk_bs_opts_init(&opts);
3661 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
3662 
3663 	/* Initialize a new blob store */
3664 	spdk_bs_init(dev, &opts, bs_op_with_handle_complete, NULL);
3665 	CU_ASSERT(g_bserrno == 0);
3666 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3667 
3668 	for (i = 0; i < 4; i++) {
3669 		g_bserrno = -1;
3670 		g_blobid = SPDK_BLOBID_INVALID;
3671 		spdk_bs_create_blob(g_bs, blob_op_with_id_complete, NULL);
3672 		CU_ASSERT(g_bserrno == 0);
3673 		CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3674 		iter_ctx.blobid[i] = g_blobid;
3675 
3676 		g_bserrno = -1;
3677 		g_blob = NULL;
3678 		spdk_bs_open_blob(g_bs, g_blobid, blob_op_with_handle_complete, NULL);
3679 		CU_ASSERT(g_bserrno == 0);
3680 		CU_ASSERT(g_blob != NULL);
3681 		blob = g_blob;
3682 
3683 		/* Just save the blobid as an xattr for testing purposes. */
3684 		rc = spdk_blob_set_xattr(blob, "blobid", &g_blobid, sizeof(g_blobid));
3685 		CU_ASSERT(rc == 0);
3686 
3687 		/* Resize the blob */
3688 		spdk_blob_resize(blob, i, blob_op_complete, NULL);
3689 		CU_ASSERT(g_bserrno == 0);
3690 
3691 		spdk_blob_close(blob, blob_op_complete, NULL);
3692 		CU_ASSERT(g_bserrno == 0);
3693 	}
3694 
3695 	g_bserrno = -1;
3696 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3697 	CU_ASSERT(g_bserrno == 0);
3698 
3699 	dev = init_dev();
3700 	spdk_bs_opts_init(&opts);
3701 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
3702 	opts.iter_cb_fn = test_iter;
3703 	opts.iter_cb_arg = &iter_ctx;
3704 
3705 	/* Test blob iteration during load after a clean shutdown. */
3706 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3707 	CU_ASSERT(g_bserrno == 0);
3708 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3709 
3710 	/* Dirty shutdown */
3711 	_spdk_bs_free(g_bs);
3712 
3713 	dev = init_dev();
3714 	spdk_bs_opts_init(&opts);
3715 	snprintf(opts.bstype.bstype, sizeof(opts.bstype.bstype), "TESTTYPE");
3716 	opts.iter_cb_fn = test_iter;
3717 	iter_ctx.current_iter = 0;
3718 	opts.iter_cb_arg = &iter_ctx;
3719 
3720 	/* Test blob iteration during load after a dirty shutdown. */
3721 	spdk_bs_load(dev, &opts, bs_op_with_handle_complete, NULL);
3722 	CU_ASSERT(g_bserrno == 0);
3723 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3724 
3725 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3726 	CU_ASSERT(g_bserrno == 0);
3727 	g_bs = NULL;
3728 }
3729 
3730 static void
3731 blob_snapshot_rw(void)
3732 {
3733 	static const uint8_t zero[10 * 4096] = { 0 };
3734 	struct spdk_blob_store *bs;
3735 	struct spdk_bs_dev *dev;
3736 	struct spdk_blob *blob, *snapshot;
3737 	struct spdk_io_channel *channel;
3738 	struct spdk_blob_opts opts;
3739 	spdk_blob_id blobid, snapshotid;
3740 	uint64_t free_clusters;
3741 	uint8_t payload_read[10 * 4096];
3742 	uint8_t payload_write[10 * 4096];
3743 
3744 	dev = init_dev();
3745 
3746 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3747 	CU_ASSERT(g_bserrno == 0);
3748 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3749 	bs = g_bs;
3750 	free_clusters = spdk_bs_free_cluster_count(bs);
3751 
3752 	channel = spdk_bs_alloc_io_channel(bs);
3753 	CU_ASSERT(channel != NULL);
3754 
3755 	spdk_blob_opts_init(&opts);
3756 	opts.thin_provision = true;
3757 	opts.num_clusters = 5;
3758 
3759 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3760 	CU_ASSERT(g_bserrno == 0);
3761 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3762 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3763 	blobid = g_blobid;
3764 
3765 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3766 	CU_ASSERT(g_bserrno == 0);
3767 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3768 	blob = g_blob;
3769 
3770 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
3771 
3772 	memset(payload_read, 0xFF, sizeof(payload_read));
3773 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
3774 	CU_ASSERT(g_bserrno == 0);
3775 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
3776 
3777 	memset(payload_write, 0xE5, sizeof(payload_write));
3778 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
3779 	CU_ASSERT(g_bserrno == 0);
3780 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
3781 
3782 	/* Create snapshot from blob */
3783 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
3784 	CU_ASSERT(g_bserrno == 0);
3785 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3786 	snapshotid = g_blobid;
3787 
3788 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
3789 	CU_ASSERT(g_bserrno == 0);
3790 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3791 	snapshot = g_blob;
3792 	CU_ASSERT(snapshot->data_ro == true)
3793 	CU_ASSERT(snapshot->md_ro == true)
3794 
3795 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5)
3796 
3797 	memset(payload_write, 0xAA, sizeof(payload_write));
3798 	spdk_blob_io_write(blob, channel, payload_write, 4, 10, blob_op_complete, NULL);
3799 	CU_ASSERT(g_bserrno == 0);
3800 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
3801 
3802 	spdk_blob_io_read(blob, channel, payload_read, 4, 10, blob_op_complete, NULL);
3803 	CU_ASSERT(g_bserrno == 0);
3804 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
3805 
3806 	/* Data on snapshot should not change after write to clone */
3807 	memset(payload_write, 0xE5, sizeof(payload_write));
3808 	spdk_blob_io_read(snapshot, channel, payload_read, 4, 10, blob_op_complete, NULL);
3809 	CU_ASSERT(g_bserrno == 0);
3810 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
3811 
3812 	spdk_blob_close(blob, blob_op_complete, NULL);
3813 	CU_ASSERT(g_bserrno == 0);
3814 
3815 	spdk_blob_close(snapshot, blob_op_complete, NULL);
3816 	CU_ASSERT(g_bserrno == 0);
3817 
3818 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
3819 	CU_ASSERT(g_bserrno == 0);
3820 
3821 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
3822 	CU_ASSERT(g_bserrno == 0);
3823 
3824 	spdk_bs_free_io_channel(channel);
3825 
3826 	/* Unload the blob store */
3827 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3828 	CU_ASSERT(g_bserrno == 0);
3829 	g_bs = NULL;
3830 	g_blob = NULL;
3831 	g_blobid = 0;
3832 }
3833 
3834 static void
3835 blob_snapshot_rw_iov(void)
3836 {
3837 	static const uint8_t zero[10 * 4096] = { 0 };
3838 	struct spdk_blob_store *bs;
3839 	struct spdk_bs_dev *dev;
3840 	struct spdk_blob *blob, *snapshot;
3841 	struct spdk_io_channel *channel;
3842 	struct spdk_blob_opts opts;
3843 	spdk_blob_id blobid, snapshotid;
3844 	uint64_t free_clusters;
3845 	uint8_t payload_read[10 * 4096];
3846 	uint8_t payload_write[10 * 4096];
3847 	struct iovec iov_read[3];
3848 	struct iovec iov_write[3];
3849 
3850 	dev = init_dev();
3851 
3852 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
3853 	CU_ASSERT(g_bserrno == 0);
3854 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
3855 	bs = g_bs;
3856 	free_clusters = spdk_bs_free_cluster_count(bs);
3857 
3858 	channel = spdk_bs_alloc_io_channel(bs);
3859 	CU_ASSERT(channel != NULL);
3860 
3861 	spdk_blob_opts_init(&opts);
3862 	opts.thin_provision = true;
3863 	opts.num_clusters = 5;
3864 
3865 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
3866 	CU_ASSERT(g_bserrno == 0);
3867 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3868 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
3869 	blobid = g_blobid;
3870 
3871 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
3872 	CU_ASSERT(g_bserrno == 0);
3873 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3874 	blob = g_blob;
3875 
3876 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
3877 
3878 	/* Create snapshot from blob */
3879 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
3880 	CU_ASSERT(g_bserrno == 0);
3881 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
3882 	snapshotid = g_blobid;
3883 
3884 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
3885 	CU_ASSERT(g_bserrno == 0);
3886 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
3887 	snapshot = g_blob;
3888 	CU_ASSERT(snapshot->data_ro == true)
3889 	CU_ASSERT(snapshot->md_ro == true)
3890 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5);
3891 
3892 	/* Payload should be all zeros from unallocated clusters */
3893 	memset(payload_read, 0xAA, sizeof(payload_read));
3894 	iov_read[0].iov_base = payload_read;
3895 	iov_read[0].iov_len = 3 * 4096;
3896 	iov_read[1].iov_base = payload_read + 3 * 4096;
3897 	iov_read[1].iov_len = 4 * 4096;
3898 	iov_read[2].iov_base = payload_read + 7 * 4096;
3899 	iov_read[2].iov_len = 3 * 4096;
3900 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
3901 	CU_ASSERT(g_bserrno == 0);
3902 	CU_ASSERT(memcmp(zero, payload_read, 10 * 4096) == 0);
3903 
3904 	memset(payload_write, 0xE5, sizeof(payload_write));
3905 	iov_write[0].iov_base = payload_write;
3906 	iov_write[0].iov_len = 1 * 4096;
3907 	iov_write[1].iov_base = payload_write + 1 * 4096;
3908 	iov_write[1].iov_len = 5 * 4096;
3909 	iov_write[2].iov_base = payload_write + 6 * 4096;
3910 	iov_write[2].iov_len = 4 * 4096;
3911 
3912 	spdk_blob_io_writev(blob, channel, iov_write, 3, 250, 10, blob_op_complete, NULL);
3913 	CU_ASSERT(g_bserrno == 0);
3914 
3915 	memset(payload_read, 0xAA, sizeof(payload_read));
3916 	iov_read[0].iov_base = payload_read;
3917 	iov_read[0].iov_len = 3 * 4096;
3918 	iov_read[1].iov_base = payload_read + 3 * 4096;
3919 	iov_read[1].iov_len = 4 * 4096;
3920 	iov_read[2].iov_base = payload_read + 7 * 4096;
3921 	iov_read[2].iov_len = 3 * 4096;
3922 	spdk_blob_io_readv(blob, channel, iov_read, 3, 250, 10, blob_op_complete, NULL);
3923 	CU_ASSERT(g_bserrno == 0);
3924 	CU_ASSERT(memcmp(payload_write, payload_read, 10 * 4096) == 0);
3925 
3926 	spdk_blob_close(blob, blob_op_complete, NULL);
3927 	CU_ASSERT(g_bserrno == 0);
3928 
3929 	spdk_blob_close(snapshot, blob_op_complete, NULL);
3930 	CU_ASSERT(g_bserrno == 0);
3931 
3932 	spdk_bs_free_io_channel(channel);
3933 
3934 	/* Unload the blob store */
3935 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
3936 	CU_ASSERT(g_bserrno == 0);
3937 	g_bs = NULL;
3938 	g_blob = NULL;
3939 	g_blobid = 0;
3940 }
3941 
3942 /**
3943  * Inflate / decouple parent rw unit tests.
3944  *
3945  * --------------
3946  * original blob:         0         1         2         3         4
3947  *                   ,---------+---------+---------+---------+---------.
3948  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
3949  *                   +---------+---------+---------+---------+---------+
3950  *         snapshot2 |    -    |yyyyyyyyy|    -    |yyyyyyyyy|    -    |
3951  *                   +---------+---------+---------+---------+---------+
3952  *         blob      |    -    |zzzzzzzzz|    -    |    -    |    -    |
3953  *                   '---------+---------+---------+---------+---------'
3954  *                   .         .         .         .         .         .
3955  * --------          .         .         .         .         .         .
3956  * inflate:          .         .         .         .         .         .
3957  *                   ,---------+---------+---------+---------+---------.
3958  *         blob      |xxxxxxxxx|zzzzzzzzz|xxxxxxxxx|yyyyyyyyy|000000000|
3959  *                   '---------+---------+---------+---------+---------'
3960  *
3961  *         NOTE: needs to allocate 4 clusters, thin provisioning removed, dependency
3962  *               on snapshot2 and snapshot removed .         .         .
3963  *                   .         .         .         .         .         .
3964  * ----------------  .         .         .         .         .         .
3965  * decouple parent:  .         .         .         .         .         .
3966  *                   ,---------+---------+---------+---------+---------.
3967  *         snapshot  |xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|xxxxxxxxx|    -    |
3968  *                   +---------+---------+---------+---------+---------+
3969  *         blob      |    -    |zzzzzzzzz|    -    |yyyyyyyyy|    -    |
3970  *                   '---------+---------+---------+---------+---------'
3971  *
3972  *         NOTE: needs to allocate 1 cluster, 3 clusters unallocated, dependency
3973  *               on snapshot2 removed and on snapshot still exists. Snapshot2
3974  *               should remain a clone of snapshot.
3975  */
3976 static void
3977 _blob_inflate_rw(bool decouple_parent)
3978 {
3979 	struct spdk_blob_store *bs;
3980 	struct spdk_bs_dev *dev;
3981 	struct spdk_blob *blob, *snapshot, *snapshot2;
3982 	struct spdk_io_channel *channel;
3983 	struct spdk_blob_opts opts;
3984 	spdk_blob_id blobid, snapshotid, snapshot2id;
3985 	uint64_t free_clusters;
3986 	uint64_t cluster_size;
3987 
3988 	uint64_t payload_size;
3989 	uint8_t *payload_read;
3990 	uint8_t *payload_write;
3991 	uint8_t *payload_clone;
3992 
3993 	uint64_t pages_per_cluster;
3994 	uint64_t pages_per_payload;
3995 
3996 	int i;
3997 	spdk_blob_id ids[2];
3998 	size_t count;
3999 
4000 	dev = init_dev();
4001 
4002 	spdk_bs_init(dev, NULL, bs_op_with_handle_complete, NULL);
4003 	CU_ASSERT(g_bserrno == 0);
4004 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4005 	bs = g_bs;
4006 
4007 	free_clusters = spdk_bs_free_cluster_count(bs);
4008 	cluster_size = spdk_bs_get_cluster_size(bs);
4009 	pages_per_cluster = cluster_size / spdk_bs_get_page_size(bs);
4010 	pages_per_payload = pages_per_cluster * 5;
4011 
4012 	payload_size = cluster_size * 5;
4013 
4014 	payload_read = malloc(payload_size);
4015 	SPDK_CU_ASSERT_FATAL(payload_read != NULL);
4016 
4017 	payload_write = malloc(payload_size);
4018 	SPDK_CU_ASSERT_FATAL(payload_write != NULL);
4019 
4020 	payload_clone = malloc(payload_size);
4021 	SPDK_CU_ASSERT_FATAL(payload_clone != NULL);
4022 
4023 	channel = spdk_bs_alloc_io_channel(bs);
4024 	SPDK_CU_ASSERT_FATAL(channel != NULL);
4025 
4026 	/* Create blob */
4027 	spdk_blob_opts_init(&opts);
4028 	opts.thin_provision = true;
4029 	opts.num_clusters = 5;
4030 
4031 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4032 	CU_ASSERT(g_bserrno == 0);
4033 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4034 	CU_ASSERT(free_clusters == spdk_bs_free_cluster_count(bs));
4035 	blobid = g_blobid;
4036 
4037 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4038 	CU_ASSERT(g_bserrno == 0);
4039 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4040 	blob = g_blob;
4041 
4042 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4043 
4044 	/* 1) Initial read should return zeroed payload */
4045 	memset(payload_read, 0xFF, payload_size);
4046 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4047 			  blob_op_complete, NULL);
4048 	CU_ASSERT(g_bserrno == 0);
4049 	CU_ASSERT(spdk_mem_all_zero(payload_read, payload_size));
4050 
4051 	/* Fill whole blob with a pattern, except last cluster (to be sure it
4052 	 * isn't allocated) */
4053 	memset(payload_write, 0xE5, payload_size - cluster_size);
4054 	spdk_blob_io_write(blob, channel, payload_write, 0, pages_per_payload -
4055 			   pages_per_cluster, blob_op_complete, NULL);
4056 	CU_ASSERT(g_bserrno == 0);
4057 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4058 
4059 	/* 2) Create snapshot from blob (first level) */
4060 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4061 	CU_ASSERT(g_bserrno == 0);
4062 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4063 	snapshotid = g_blobid;
4064 
4065 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4066 	CU_ASSERT(g_bserrno == 0);
4067 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4068 	snapshot = g_blob;
4069 	CU_ASSERT(snapshot->data_ro == true)
4070 	CU_ASSERT(snapshot->md_ro == true)
4071 
4072 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot) == 5)
4073 
4074 	/* Write every second cluster with a pattern.
4075 	 *
4076 	 * Last cluster shouldn't be written, to be sure that snapshot nor clone
4077 	 * doesn't allocate it.
4078 	 *
4079 	 * payload_clone stores expected result on "blob" read at the time and
4080 	 * is used only to check data consistency on clone before and after
4081 	 * inflation. Initially we fill it with a backing snapshots pattern
4082 	 * used before.
4083 	 */
4084 	memset(payload_clone, 0xE5, payload_size - cluster_size);
4085 	memset(payload_clone + payload_size - cluster_size, 0x00, cluster_size);
4086 	memset(payload_write, 0xAA, payload_size);
4087 	for (i = 1; i < 5; i += 2) {
4088 		spdk_blob_io_write(blob, channel, payload_write, i * pages_per_cluster,
4089 				   pages_per_cluster, blob_op_complete, NULL);
4090 		CU_ASSERT(g_bserrno == 0);
4091 
4092 		/* Update expected result */
4093 		memcpy(payload_clone + (cluster_size * i), payload_write,
4094 		       cluster_size);
4095 	}
4096 	CU_ASSERT(free_clusters != spdk_bs_free_cluster_count(bs));
4097 
4098 	/* Check data consistency on clone */
4099 	memset(payload_read, 0xFF, payload_size);
4100 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4101 			  blob_op_complete, NULL);
4102 	CU_ASSERT(g_bserrno == 0);
4103 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
4104 
4105 	/* 3) Create second levels snapshot from blob */
4106 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4107 	CU_ASSERT(g_bserrno == 0);
4108 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4109 	snapshot2id = g_blobid;
4110 
4111 	spdk_bs_open_blob(bs, snapshot2id, blob_op_with_handle_complete, NULL);
4112 	CU_ASSERT(g_bserrno == 0);
4113 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4114 	snapshot2 = g_blob;
4115 	CU_ASSERT(snapshot2->data_ro == true)
4116 	CU_ASSERT(snapshot2->md_ro == true)
4117 
4118 	CU_ASSERT(spdk_blob_get_num_clusters(snapshot2) == 5)
4119 
4120 	CU_ASSERT(snapshot2->parent_id == snapshotid);
4121 
4122 	/* Write one cluster on the top level blob. This cluster (1) covers
4123 	 * already allocated cluster in the snapshot2, so shouldn't be inflated
4124 	 * at all */
4125 	spdk_blob_io_write(blob, channel, payload_write, pages_per_cluster,
4126 			   pages_per_cluster, blob_op_complete, NULL);
4127 	CU_ASSERT(g_bserrno == 0);
4128 
4129 	/* Update expected result */
4130 	memcpy(payload_clone + cluster_size, payload_write, cluster_size);
4131 
4132 	/* Check data consistency on clone */
4133 	memset(payload_read, 0xFF, payload_size);
4134 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4135 			  blob_op_complete, NULL);
4136 	CU_ASSERT(g_bserrno == 0);
4137 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
4138 
4139 
4140 	/* Close all blobs */
4141 	spdk_blob_close(blob, blob_op_complete, NULL);
4142 	CU_ASSERT(g_bserrno == 0);
4143 
4144 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
4145 	CU_ASSERT(g_bserrno == 0);
4146 
4147 	spdk_blob_close(snapshot, blob_op_complete, NULL);
4148 	CU_ASSERT(g_bserrno == 0);
4149 
4150 	/* Check snapshot-clone relations */
4151 	count = 2;
4152 	CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
4153 	CU_ASSERT(count == 1);
4154 	CU_ASSERT(ids[0] == snapshot2id);
4155 
4156 	count = 2;
4157 	CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
4158 	CU_ASSERT(count == 1);
4159 	CU_ASSERT(ids[0] == blobid);
4160 
4161 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshot2id);
4162 
4163 	free_clusters = spdk_bs_free_cluster_count(bs);
4164 	if (!decouple_parent) {
4165 		/* Do full blob inflation */
4166 		spdk_bs_inflate_blob(bs, channel, blobid, blob_op_complete, NULL);
4167 		CU_ASSERT(g_bserrno == 0);
4168 
4169 		/* All clusters should be inflated (except one already allocated
4170 		 * in a top level blob) */
4171 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 4);
4172 
4173 		/* Check if relation tree updated correctly */
4174 		count = 2;
4175 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
4176 
4177 		/* snapshotid have one clone */
4178 		CU_ASSERT(count == 1);
4179 		CU_ASSERT(ids[0] == snapshot2id);
4180 
4181 		/* snapshot2id have no clones */
4182 		count = 2;
4183 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
4184 		CU_ASSERT(count == 0);
4185 
4186 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
4187 	} else {
4188 		/* Decouple parent of blob */
4189 		spdk_bs_blob_decouple_parent(bs, channel, blobid, blob_op_complete, NULL);
4190 		CU_ASSERT(g_bserrno == 0);
4191 
4192 		/* Only one cluster from a parent should be inflated (second one
4193 		 * is covered by a cluster written on a top level blob, and
4194 		 * already allocated) */
4195 		CU_ASSERT(spdk_bs_free_cluster_count(bs) == free_clusters - 1);
4196 
4197 		/* Check if relation tree updated correctly */
4198 		count = 2;
4199 		CU_ASSERT(spdk_blob_get_clones(bs, snapshotid, ids, &count) == 0);
4200 
4201 		/* snapshotid have two clones now */
4202 		CU_ASSERT(count == 2);
4203 		CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
4204 		CU_ASSERT(ids[0] == snapshot2id || ids[1] == snapshot2id);
4205 
4206 		/* snapshot2id have no clones */
4207 		count = 2;
4208 		CU_ASSERT(spdk_blob_get_clones(bs, snapshot2id, ids, &count) == 0);
4209 		CU_ASSERT(count == 0);
4210 
4211 		CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
4212 	}
4213 
4214 	/* Try to delete snapshot2 (should pass) */
4215 	spdk_bs_delete_blob(bs, snapshot2id, blob_op_complete, NULL);
4216 	CU_ASSERT(g_bserrno == 0);
4217 
4218 	/* Try to delete base snapshot (for decouple_parent should fail while
4219 	 * dependency still exists) */
4220 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4221 	CU_ASSERT(decouple_parent || g_bserrno == 0);
4222 	CU_ASSERT(!decouple_parent || g_bserrno != 0);
4223 
4224 	/* Reopen blob after snapshot deletion */
4225 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4226 	CU_ASSERT(g_bserrno == 0);
4227 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4228 	blob = g_blob;
4229 
4230 	CU_ASSERT(spdk_blob_get_num_clusters(blob) == 5);
4231 
4232 	/* Check data consistency on inflated blob */
4233 	memset(payload_read, 0xFF, payload_size);
4234 	spdk_blob_io_read(blob, channel, payload_read, 0, pages_per_payload,
4235 			  blob_op_complete, NULL);
4236 	CU_ASSERT(g_bserrno == 0);
4237 	CU_ASSERT(memcmp(payload_clone, payload_read, payload_size) == 0);
4238 
4239 	spdk_blob_close(blob, blob_op_complete, NULL);
4240 	CU_ASSERT(g_bserrno == 0);
4241 
4242 	spdk_bs_free_io_channel(channel);
4243 
4244 	/* Unload the blob store */
4245 	spdk_bs_unload(g_bs, bs_op_complete, NULL);
4246 	CU_ASSERT(g_bserrno == 0);
4247 	g_bs = NULL;
4248 	g_blob = NULL;
4249 	g_blobid = 0;
4250 
4251 	free(payload_read);
4252 	free(payload_write);
4253 	free(payload_clone);
4254 }
4255 
4256 static void
4257 blob_inflate_rw(void)
4258 {
4259 	_blob_inflate_rw(false);
4260 	_blob_inflate_rw(true);
4261 }
4262 
4263 /**
4264  * Snapshot-clones relation test
4265  *
4266  *         snapshot
4267  *            |
4268  *      +-----+-----+
4269  *      |           |
4270  *   blob(ro)   snapshot2
4271  *      |           |
4272  *   clone2      clone
4273  */
4274 static void
4275 blob_relations(void)
4276 {
4277 	struct spdk_blob_store *bs;
4278 	struct spdk_bs_dev *dev;
4279 	struct spdk_bs_opts bs_opts;
4280 	struct spdk_blob_opts opts;
4281 	struct spdk_blob *blob, *snapshot, *snapshot2, *clone, *clone2;
4282 	spdk_blob_id blobid, cloneid, snapshotid, cloneid2, snapshotid2;
4283 	int rc;
4284 	size_t count;
4285 	spdk_blob_id ids[10];
4286 
4287 	dev = init_dev();
4288 	spdk_bs_opts_init(&bs_opts);
4289 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
4290 
4291 	spdk_bs_init(dev, &bs_opts, bs_op_with_handle_complete, NULL);
4292 	CU_ASSERT(g_bserrno == 0);
4293 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4294 	bs = g_bs;
4295 
4296 	/* 1. Create blob with 10 clusters */
4297 
4298 	spdk_blob_opts_init(&opts);
4299 	opts.num_clusters = 10;
4300 
4301 	spdk_bs_create_blob_ext(bs, &opts, blob_op_with_id_complete, NULL);
4302 	CU_ASSERT(g_bserrno == 0);
4303 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4304 	blobid = g_blobid;
4305 
4306 	spdk_bs_open_blob(bs, blobid, blob_op_with_handle_complete, NULL);
4307 	CU_ASSERT(g_bserrno == 0);
4308 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4309 	blob = g_blob;
4310 
4311 	CU_ASSERT(!spdk_blob_is_read_only(blob));
4312 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
4313 	CU_ASSERT(!spdk_blob_is_clone(blob));
4314 	CU_ASSERT(!spdk_blob_is_thin_provisioned(blob));
4315 
4316 	/* blob should not have underlying snapshot nor clones */
4317 	CU_ASSERT(blob->parent_id == SPDK_BLOBID_INVALID);
4318 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == SPDK_BLOBID_INVALID);
4319 	count = SPDK_COUNTOF(ids);
4320 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
4321 	CU_ASSERT(rc == 0);
4322 	CU_ASSERT(count == 0);
4323 
4324 
4325 	/* 2. Create snapshot */
4326 
4327 	spdk_bs_create_snapshot(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4328 	CU_ASSERT(g_bserrno == 0);
4329 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4330 	snapshotid = g_blobid;
4331 
4332 	spdk_bs_open_blob(bs, snapshotid, blob_op_with_handle_complete, NULL);
4333 	CU_ASSERT(g_bserrno == 0);
4334 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4335 	snapshot = g_blob;
4336 
4337 	CU_ASSERT(spdk_blob_is_read_only(snapshot));
4338 	CU_ASSERT(spdk_blob_is_snapshot(snapshot));
4339 	CU_ASSERT(!spdk_blob_is_clone(snapshot));
4340 	CU_ASSERT(snapshot->parent_id == SPDK_BLOBID_INVALID);
4341 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
4342 
4343 	/* Check if original blob is converted to the clone of snapshot */
4344 	CU_ASSERT(!spdk_blob_is_read_only(blob));
4345 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
4346 	CU_ASSERT(spdk_blob_is_clone(blob));
4347 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
4348 	CU_ASSERT(blob->parent_id == snapshotid);
4349 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
4350 
4351 	count = SPDK_COUNTOF(ids);
4352 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
4353 	CU_ASSERT(rc == 0);
4354 	CU_ASSERT(count == 1);
4355 	CU_ASSERT(ids[0] == blobid);
4356 
4357 
4358 	/* 3. Create clone from snapshot */
4359 
4360 	spdk_bs_create_clone(bs, snapshotid, NULL, blob_op_with_id_complete, NULL);
4361 	CU_ASSERT(g_bserrno == 0);
4362 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4363 	cloneid = g_blobid;
4364 
4365 	spdk_bs_open_blob(bs, cloneid, blob_op_with_handle_complete, NULL);
4366 	CU_ASSERT(g_bserrno == 0);
4367 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4368 	clone = g_blob;
4369 
4370 	CU_ASSERT(!spdk_blob_is_read_only(clone));
4371 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
4372 	CU_ASSERT(spdk_blob_is_clone(clone));
4373 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
4374 	CU_ASSERT(clone->parent_id == snapshotid);
4375 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid);
4376 
4377 	count = SPDK_COUNTOF(ids);
4378 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
4379 	CU_ASSERT(rc == 0);
4380 	CU_ASSERT(count == 0);
4381 
4382 	/* Check if clone is on the snapshot's list */
4383 	count = SPDK_COUNTOF(ids);
4384 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
4385 	CU_ASSERT(rc == 0);
4386 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
4387 	CU_ASSERT(ids[0] == cloneid || ids[1] == cloneid);
4388 
4389 
4390 	/* 4. Create snapshot of the clone */
4391 
4392 	spdk_bs_create_snapshot(bs, cloneid, NULL, blob_op_with_id_complete, NULL);
4393 	CU_ASSERT(g_bserrno == 0);
4394 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4395 	snapshotid2 = g_blobid;
4396 
4397 	spdk_bs_open_blob(bs, snapshotid2, blob_op_with_handle_complete, NULL);
4398 	CU_ASSERT(g_bserrno == 0);
4399 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4400 	snapshot2 = g_blob;
4401 
4402 	CU_ASSERT(spdk_blob_is_read_only(snapshot2));
4403 	CU_ASSERT(spdk_blob_is_snapshot(snapshot2));
4404 	CU_ASSERT(spdk_blob_is_clone(snapshot2));
4405 	CU_ASSERT(snapshot2->parent_id == snapshotid);
4406 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
4407 
4408 	/* Check if clone is converted to the clone of snapshot2 and snapshot2
4409 	 * is a child of snapshot */
4410 	CU_ASSERT(!spdk_blob_is_read_only(clone));
4411 	CU_ASSERT(!spdk_blob_is_snapshot(clone));
4412 	CU_ASSERT(spdk_blob_is_clone(clone));
4413 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone));
4414 	CU_ASSERT(clone->parent_id == snapshotid2);
4415 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
4416 
4417 	count = SPDK_COUNTOF(ids);
4418 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
4419 	CU_ASSERT(rc == 0);
4420 	CU_ASSERT(count == 1);
4421 	CU_ASSERT(ids[0] == cloneid);
4422 
4423 
4424 	/* 5. Try to create clone from read only blob */
4425 
4426 	/* Mark blob as read only */
4427 	spdk_blob_set_read_only(blob);
4428 	spdk_blob_sync_md(blob, blob_op_complete, NULL);
4429 	CU_ASSERT(g_bserrno == 0);
4430 
4431 	/* Check if previously created blob is read only clone */
4432 	CU_ASSERT(spdk_blob_is_read_only(blob));
4433 	CU_ASSERT(!spdk_blob_is_snapshot(blob));
4434 	CU_ASSERT(spdk_blob_is_clone(blob));
4435 	CU_ASSERT(spdk_blob_is_thin_provisioned(blob));
4436 
4437 	/* Create clone from read only blob */
4438 	spdk_bs_create_clone(bs, blobid, NULL, blob_op_with_id_complete, NULL);
4439 	CU_ASSERT(g_bserrno == 0);
4440 	CU_ASSERT(g_blobid != SPDK_BLOBID_INVALID);
4441 	cloneid2 = g_blobid;
4442 
4443 	spdk_bs_open_blob(bs, cloneid2, blob_op_with_handle_complete, NULL);
4444 	CU_ASSERT(g_bserrno == 0);
4445 	SPDK_CU_ASSERT_FATAL(g_blob != NULL);
4446 	clone2 = g_blob;
4447 
4448 	CU_ASSERT(!spdk_blob_is_read_only(clone2));
4449 	CU_ASSERT(!spdk_blob_is_snapshot(clone2));
4450 	CU_ASSERT(spdk_blob_is_clone(clone2));
4451 	CU_ASSERT(spdk_blob_is_thin_provisioned(clone2));
4452 
4453 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
4454 
4455 	count = SPDK_COUNTOF(ids);
4456 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
4457 	CU_ASSERT(rc == 0);
4458 
4459 	CU_ASSERT(count == 1);
4460 	CU_ASSERT(ids[0] == cloneid2);
4461 
4462 	/* Close blobs */
4463 
4464 	spdk_blob_close(clone2, blob_op_complete, NULL);
4465 	CU_ASSERT(g_bserrno == 0);
4466 
4467 	spdk_blob_close(blob, blob_op_complete, NULL);
4468 	CU_ASSERT(g_bserrno == 0);
4469 
4470 	spdk_blob_close(clone, blob_op_complete, NULL);
4471 	CU_ASSERT(g_bserrno == 0);
4472 
4473 	spdk_blob_close(snapshot, blob_op_complete, NULL);
4474 	CU_ASSERT(g_bserrno == 0);
4475 
4476 	spdk_blob_close(snapshot2, blob_op_complete, NULL);
4477 	CU_ASSERT(g_bserrno == 0);
4478 
4479 	/* Try to delete snapshot with created clones */
4480 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4481 	CU_ASSERT(g_bserrno != 0);
4482 
4483 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
4484 	CU_ASSERT(g_bserrno != 0);
4485 
4486 	spdk_bs_unload(bs, bs_op_complete, NULL);
4487 	CU_ASSERT(g_bserrno == 0);
4488 	g_bs = NULL;
4489 
4490 	/* Load an existing blob store */
4491 	dev = init_dev();
4492 	snprintf(bs_opts.bstype.bstype, sizeof(bs_opts.bstype.bstype), "TESTTYPE");
4493 
4494 	spdk_bs_load(dev, NULL, bs_op_with_handle_complete, NULL);
4495 	CU_ASSERT(g_bserrno == 0);
4496 	SPDK_CU_ASSERT_FATAL(g_bs != NULL);
4497 	bs = g_bs;
4498 
4499 
4500 	/* NULL ids array should return number of clones in count */
4501 	count = SPDK_COUNTOF(ids);
4502 	rc = spdk_blob_get_clones(bs, snapshotid, NULL, &count);
4503 	CU_ASSERT(rc == -ENOMEM);
4504 	CU_ASSERT(count == 2);
4505 
4506 	/* incorrect array size */
4507 	count = 1;
4508 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
4509 	CU_ASSERT(rc == -ENOMEM);
4510 	CU_ASSERT(count == 2);
4511 
4512 
4513 	/* Verify structure of loaded blob store */
4514 
4515 	/* snapshot */
4516 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid) == SPDK_BLOBID_INVALID);
4517 
4518 	count = SPDK_COUNTOF(ids);
4519 	rc = spdk_blob_get_clones(bs, snapshotid, ids, &count);
4520 	CU_ASSERT(rc == 0);
4521 	CU_ASSERT(count == 2);
4522 	CU_ASSERT(ids[0] == blobid || ids[1] == blobid);
4523 	CU_ASSERT(ids[0] == snapshotid2 || ids[1] == snapshotid2);
4524 
4525 	/* blob */
4526 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, blobid) == snapshotid);
4527 	count = SPDK_COUNTOF(ids);
4528 	rc = spdk_blob_get_clones(bs, blobid, ids, &count);
4529 	CU_ASSERT(rc == 0);
4530 	CU_ASSERT(count == 1);
4531 	CU_ASSERT(ids[0] == cloneid2);
4532 
4533 	/* clone */
4534 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid) == snapshotid2);
4535 	count = SPDK_COUNTOF(ids);
4536 	rc = spdk_blob_get_clones(bs, cloneid, ids, &count);
4537 	CU_ASSERT(rc == 0);
4538 	CU_ASSERT(count == 0);
4539 
4540 	/* snapshot2 */
4541 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, snapshotid2) == snapshotid);
4542 	count = SPDK_COUNTOF(ids);
4543 	rc = spdk_blob_get_clones(bs, snapshotid2, ids, &count);
4544 	CU_ASSERT(rc == 0);
4545 	CU_ASSERT(count == 1);
4546 	CU_ASSERT(ids[0] == cloneid);
4547 
4548 	/* clone2 */
4549 	CU_ASSERT(spdk_blob_get_parent_snapshot(bs, cloneid2) == blobid);
4550 	count = SPDK_COUNTOF(ids);
4551 	rc = spdk_blob_get_clones(bs, cloneid2, ids, &count);
4552 	CU_ASSERT(rc == 0);
4553 	CU_ASSERT(count == 0);
4554 
4555 	/* Try to delete all blobs in the worse possible order */
4556 
4557 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4558 	CU_ASSERT(g_bserrno != 0);
4559 
4560 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
4561 	CU_ASSERT(g_bserrno != 0);
4562 
4563 	spdk_bs_delete_blob(bs, cloneid, blob_op_complete, NULL);
4564 	CU_ASSERT(g_bserrno == 0);
4565 
4566 	spdk_bs_delete_blob(bs, snapshotid2, blob_op_complete, NULL);
4567 	CU_ASSERT(g_bserrno == 0);
4568 
4569 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4570 	CU_ASSERT(g_bserrno != 0);
4571 
4572 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
4573 	CU_ASSERT(g_bserrno != 0);
4574 
4575 	spdk_bs_delete_blob(bs, cloneid2, blob_op_complete, NULL);
4576 	CU_ASSERT(g_bserrno == 0);
4577 
4578 	spdk_bs_delete_blob(bs, blobid, blob_op_complete, NULL);
4579 	CU_ASSERT(g_bserrno == 0);
4580 
4581 	spdk_bs_delete_blob(bs, snapshotid, blob_op_complete, NULL);
4582 	CU_ASSERT(g_bserrno == 0);
4583 
4584 	spdk_bs_unload(bs, bs_op_complete, NULL);
4585 	CU_ASSERT(g_bserrno == 0);
4586 
4587 	g_bs = NULL;
4588 }
4589 
4590 int main(int argc, char **argv)
4591 {
4592 	CU_pSuite	suite = NULL;
4593 	unsigned int	num_failures;
4594 
4595 	if (CU_initialize_registry() != CUE_SUCCESS) {
4596 		return CU_get_error();
4597 	}
4598 
4599 	suite = CU_add_suite("blob", NULL, NULL);
4600 	if (suite == NULL) {
4601 		CU_cleanup_registry();
4602 		return CU_get_error();
4603 	}
4604 
4605 	if (
4606 		CU_add_test(suite, "blob_init", blob_init) == NULL ||
4607 		CU_add_test(suite, "blob_open", blob_open) == NULL ||
4608 		CU_add_test(suite, "blob_create", blob_create) == NULL ||
4609 		CU_add_test(suite, "blob_create_internal", blob_create_internal) == NULL ||
4610 		CU_add_test(suite, "blob_thin_provision", blob_thin_provision) == NULL ||
4611 		CU_add_test(suite, "blob_snapshot", blob_snapshot) == NULL ||
4612 		CU_add_test(suite, "blob_clone", blob_clone) == NULL ||
4613 		CU_add_test(suite, "blob_inflate", blob_inflate) == NULL ||
4614 		CU_add_test(suite, "blob_delete", blob_delete) == NULL ||
4615 		CU_add_test(suite, "blob_resize", blob_resize) == NULL ||
4616 		CU_add_test(suite, "blob_read_only", blob_read_only) == NULL ||
4617 		CU_add_test(suite, "channel_ops", channel_ops) == NULL ||
4618 		CU_add_test(suite, "blob_super", blob_super) == NULL ||
4619 		CU_add_test(suite, "blob_write", blob_write) == NULL ||
4620 		CU_add_test(suite, "blob_read", blob_read) == NULL ||
4621 		CU_add_test(suite, "blob_rw_verify", blob_rw_verify) == NULL ||
4622 		CU_add_test(suite, "blob_rw_verify_iov", blob_rw_verify_iov) == NULL ||
4623 		CU_add_test(suite, "blob_rw_verify_iov_nomem", blob_rw_verify_iov_nomem) == NULL ||
4624 		CU_add_test(suite, "blob_rw_iov_read_only", blob_rw_iov_read_only) == NULL ||
4625 		CU_add_test(suite, "blob_unmap", blob_unmap) == NULL ||
4626 		CU_add_test(suite, "blob_iter", blob_iter) == NULL ||
4627 		CU_add_test(suite, "blob_xattr", blob_xattr) == NULL ||
4628 		CU_add_test(suite, "bs_load", bs_load) == NULL ||
4629 		CU_add_test(suite, "bs_unload", bs_unload) == NULL ||
4630 		CU_add_test(suite, "bs_cluster_sz", bs_cluster_sz) == NULL ||
4631 		CU_add_test(suite, "bs_usable_clusters", bs_usable_clusters) == NULL ||
4632 		CU_add_test(suite, "bs_resize_md", bs_resize_md) == NULL ||
4633 		CU_add_test(suite, "bs_destroy", bs_destroy) == NULL ||
4634 		CU_add_test(suite, "bs_type", bs_type) == NULL ||
4635 		CU_add_test(suite, "bs_super_block", bs_super_block) == NULL ||
4636 		CU_add_test(suite, "blob_serialize", blob_serialize) == NULL ||
4637 		CU_add_test(suite, "blob_crc", blob_crc) == NULL ||
4638 		CU_add_test(suite, "super_block_crc", super_block_crc) == NULL ||
4639 		CU_add_test(suite, "blob_dirty_shutdown", blob_dirty_shutdown) == NULL ||
4640 		CU_add_test(suite, "blob_flags", blob_flags) == NULL ||
4641 		CU_add_test(suite, "bs_version", bs_version) == NULL ||
4642 		CU_add_test(suite, "blob_set_xattrs", blob_set_xattrs) == NULL ||
4643 		CU_add_test(suite, "blob_thin_prov_alloc", blob_thin_prov_alloc) == NULL ||
4644 		CU_add_test(suite, "blob_insert_cluster_msg", blob_insert_cluster_msg) == NULL ||
4645 		CU_add_test(suite, "blob_thin_prov_rw", blob_thin_prov_rw) == NULL ||
4646 		CU_add_test(suite, "blob_thin_prov_rw_iov", blob_thin_prov_rw_iov) == NULL ||
4647 		CU_add_test(suite, "bs_load_iter", bs_load_iter) == NULL ||
4648 		CU_add_test(suite, "blob_snapshot_rw", blob_snapshot_rw) == NULL ||
4649 		CU_add_test(suite, "blob_snapshot_rw_iov", blob_snapshot_rw_iov) == NULL ||
4650 		CU_add_test(suite, "blob_relations", blob_relations) == NULL ||
4651 		CU_add_test(suite, "blob_inflate_rw", blob_inflate_rw) == NULL ||
4652 		CU_add_test(suite, "blob_snapshot_freeze_io", blob_snapshot_freeze_io) == NULL
4653 	) {
4654 		CU_cleanup_registry();
4655 		return CU_get_error();
4656 	}
4657 
4658 	g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
4659 	spdk_allocate_thread(_bs_send_msg, NULL, NULL, NULL, "thread0");
4660 	CU_basic_set_mode(CU_BRM_VERBOSE);
4661 	CU_basic_run_tests();
4662 	num_failures = CU_get_number_of_failures();
4663 	CU_cleanup_registry();
4664 	spdk_free_thread();
4665 	free(g_dev_buffer);
4666 	return num_failures;
4667 }
4668