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