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