xref: /spdk/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c (revision 42dba6047b054d9db99f2a78f1b7100a9b3162a1)
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_cunit.h"
35 #include "spdk/string.h"
36 
37 #include "bdev/lvol/vbdev_lvol.c"
38 
39 #define SPDK_BS_PAGE_SIZE 0x1000
40 
41 int g_lvolerrno;
42 int g_lvserrno;
43 int g_cluster_size;
44 int g_registered_bdevs;
45 int g_num_lvols = 0;
46 struct spdk_lvol_store *g_lvs = NULL;
47 struct spdk_lvol *g_lvol = NULL;
48 struct lvol_store_bdev *g_lvs_bdev = NULL;
49 struct spdk_bdev *g_base_bdev = NULL;
50 struct spdk_bdev_io *g_io = NULL;
51 struct spdk_io_channel *g_ch = NULL;
52 struct lvol_task *g_task = NULL;
53 
54 static struct spdk_bdev g_bdev = {};
55 static struct spdk_lvol_store *g_lvol_store = NULL;
56 bool lvol_store_initialize_fail = false;
57 bool lvol_store_initialize_cb_fail = false;
58 bool lvol_already_opened = false;
59 bool g_examine_done = false;
60 bool g_bdev_alias_already_exists = false;
61 bool g_lvs_with_name_already_exists = false;
62 bool g_lvol_deletable = true;
63 
64 int
65 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
66 {
67 	struct spdk_bdev_alias *tmp;
68 
69 	CU_ASSERT(alias != NULL);
70 	CU_ASSERT(bdev != NULL);
71 	if (g_bdev_alias_already_exists) {
72 		return -EEXIST;
73 	}
74 
75 	tmp = calloc(1, sizeof(*tmp));
76 	SPDK_CU_ASSERT_FATAL(tmp != NULL);
77 
78 	tmp->alias = strdup(alias);
79 	SPDK_CU_ASSERT_FATAL(tmp->alias != NULL);
80 
81 	TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq);
82 
83 	return 0;
84 }
85 
86 int
87 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias)
88 {
89 	struct spdk_bdev_alias *tmp;
90 
91 	CU_ASSERT(alias != NULL);
92 	CU_ASSERT(bdev != NULL);
93 
94 	TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
95 		if (strncmp(alias, tmp->alias, SPDK_LVOL_NAME_MAX) == 0) {
96 			TAILQ_REMOVE(&bdev->aliases, tmp, tailq);
97 			free(tmp->alias);
98 			free(tmp);
99 			return 0;
100 		}
101 	}
102 
103 	return -ENOENT;
104 }
105 
106 void
107 spdk_bdev_alias_del_all(struct spdk_bdev *bdev)
108 {
109 	struct spdk_bdev_alias *p, *tmp;
110 
111 	TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) {
112 		TAILQ_REMOVE(&bdev->aliases, p, tailq);
113 		free(p->alias);
114 		free(p);
115 	}
116 }
117 
118 void
119 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
120 {
121 }
122 
123 void
124 spdk_lvs_rename(struct spdk_lvol_store *lvs, const char *new_name,
125 		spdk_lvs_op_complete cb_fn, void *cb_arg)
126 {
127 	if (g_lvs_with_name_already_exists) {
128 		g_lvolerrno = -EEXIST;
129 	} else {
130 		snprintf(lvs->name, sizeof(lvs->name), "%s", new_name);
131 		g_lvolerrno = 0;
132 	}
133 
134 	cb_fn(cb_arg, g_lvolerrno);
135 }
136 
137 void
138 spdk_lvol_rename(struct spdk_lvol *lvol, const char *new_name,
139 		 spdk_lvol_op_complete cb_fn, void *cb_arg)
140 {
141 	struct spdk_lvol *tmp;
142 
143 	if (strncmp(lvol->name, new_name, SPDK_LVOL_NAME_MAX) == 0) {
144 		cb_fn(cb_arg, 0);
145 		return;
146 	}
147 
148 	TAILQ_FOREACH(tmp, &lvol->lvol_store->lvols, link) {
149 		if (strncmp(tmp->name, new_name, SPDK_LVOL_NAME_MAX) == 0) {
150 			SPDK_ERRLOG("Lvol %s already exists in lvol store %s\n", new_name, lvol->lvol_store->name);
151 			cb_fn(cb_arg, -EEXIST);
152 			return;
153 		}
154 	}
155 
156 	snprintf(lvol->name, sizeof(lvol->name), "%s", new_name);
157 
158 	cb_fn(cb_arg, g_lvolerrno);
159 }
160 
161 void
162 spdk_lvol_open(struct spdk_lvol *lvol, spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
163 {
164 	cb_fn(cb_arg, lvol, g_lvolerrno);
165 }
166 
167 uint64_t
168 spdk_blob_get_num_clusters(struct spdk_blob *b)
169 {
170 	return 0;
171 }
172 
173 int
174 spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids,
175 		     size_t *count)
176 {
177 	*count = 0;
178 	return 0;
179 }
180 
181 spdk_blob_id
182 spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid)
183 {
184 	return 0;
185 }
186 
187 bool g_blob_is_read_only = false;
188 
189 bool
190 spdk_blob_is_read_only(struct spdk_blob *blob)
191 {
192 	return g_blob_is_read_only;
193 }
194 
195 bool
196 spdk_blob_is_snapshot(struct spdk_blob *blob)
197 {
198 	return false;
199 }
200 
201 bool
202 spdk_blob_is_clone(struct spdk_blob *blob)
203 {
204 	return false;
205 }
206 
207 bool
208 spdk_blob_is_thin_provisioned(struct spdk_blob *blob)
209 {
210 	return false;
211 }
212 
213 static struct spdk_lvol *_lvol_create(struct spdk_lvol_store *lvs);
214 
215 void
216 spdk_lvs_load(struct spdk_bs_dev *dev,
217 	      spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg)
218 {
219 	struct spdk_lvol_store *lvs = NULL;
220 	int i;
221 	int lvserrno = g_lvserrno;
222 
223 	if (lvserrno != 0) {
224 		/* On error blobstore destroys bs_dev itself,
225 		 * by puttin back io channels.
226 		 * This operation is asynchronous, and completed
227 		 * after calling the callback for lvol. */
228 		cb_fn(cb_arg, g_lvol_store, lvserrno);
229 		dev->destroy(dev);
230 		return;
231 	}
232 
233 	lvs = calloc(1, sizeof(*lvs));
234 	SPDK_CU_ASSERT_FATAL(lvs != NULL);
235 	TAILQ_INIT(&lvs->lvols);
236 	TAILQ_INIT(&lvs->pending_lvols);
237 	spdk_uuid_generate(&lvs->uuid);
238 	lvs->bs_dev = dev;
239 	for (i = 0; i < g_num_lvols; i++) {
240 		_lvol_create(lvs);
241 	}
242 
243 	cb_fn(cb_arg, lvs, lvserrno);
244 }
245 
246 int
247 spdk_bs_bdev_claim(struct spdk_bs_dev *bs_dev, struct spdk_bdev_module *module)
248 {
249 	if (lvol_already_opened == true) {
250 		return -1;
251 	}
252 
253 	lvol_already_opened = true;
254 
255 	return 0;
256 }
257 
258 void
259 spdk_bdev_unregister(struct spdk_bdev *vbdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
260 {
261 	int rc;
262 
263 	SPDK_CU_ASSERT_FATAL(vbdev != NULL);
264 	rc = vbdev->fn_table->destruct(vbdev->ctxt);
265 
266 	SPDK_CU_ASSERT_FATAL(cb_fn != NULL);
267 	cb_fn(cb_arg, rc);
268 }
269 
270 void
271 spdk_bdev_module_finish_done(void)
272 {
273 	return;
274 }
275 
276 uint64_t
277 spdk_bs_get_page_size(struct spdk_blob_store *bs)
278 {
279 	return SPDK_BS_PAGE_SIZE;
280 }
281 
282 uint64_t
283 spdk_bs_get_io_unit_size(struct spdk_blob_store *bs)
284 {
285 	return SPDK_BS_PAGE_SIZE;
286 }
287 
288 static void
289 bdev_blob_destroy(struct spdk_bs_dev *bs_dev)
290 {
291 	CU_ASSERT(bs_dev != NULL);
292 	free(bs_dev);
293 	lvol_already_opened = false;
294 }
295 
296 struct spdk_bs_dev *
297 spdk_bdev_create_bs_dev(struct spdk_bdev *bdev, spdk_bdev_remove_cb_t remove_cb, void *remove_ctx)
298 {
299 	struct spdk_bs_dev *bs_dev;
300 
301 	if (lvol_already_opened == true || bdev == NULL) {
302 		return NULL;
303 	}
304 
305 	bs_dev = calloc(1, sizeof(*bs_dev));
306 	SPDK_CU_ASSERT_FATAL(bs_dev != NULL);
307 	bs_dev->destroy = bdev_blob_destroy;
308 
309 	return bs_dev;
310 }
311 
312 void
313 spdk_lvs_opts_init(struct spdk_lvs_opts *opts)
314 {
315 }
316 
317 int
318 spdk_lvs_init(struct spdk_bs_dev *bs_dev, struct spdk_lvs_opts *o,
319 	      spdk_lvs_op_with_handle_complete cb_fn, void *cb_arg)
320 {
321 	struct spdk_lvol_store *lvs;
322 	int error = 0;
323 
324 	if (lvol_store_initialize_fail) {
325 		return -1;
326 	}
327 
328 	if (lvol_store_initialize_cb_fail) {
329 		bs_dev->destroy(bs_dev);
330 		lvs = NULL;
331 		error = -1;
332 	} else {
333 		lvs = calloc(1, sizeof(*lvs));
334 		SPDK_CU_ASSERT_FATAL(lvs != NULL);
335 		TAILQ_INIT(&lvs->lvols);
336 		TAILQ_INIT(&lvs->pending_lvols);
337 		spdk_uuid_generate(&lvs->uuid);
338 		snprintf(lvs->name, sizeof(lvs->name), "%s", o->name);
339 		lvs->bs_dev = bs_dev;
340 		error = 0;
341 	}
342 	cb_fn(cb_arg, lvs, error);
343 
344 	return 0;
345 }
346 
347 int
348 spdk_lvs_unload(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn, void *cb_arg)
349 {
350 	struct spdk_lvol *lvol, *tmp;
351 
352 	TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) {
353 		TAILQ_REMOVE(&lvs->lvols, lvol, link);
354 		free(lvol->unique_id);
355 		free(lvol);
356 	}
357 	g_lvol_store = NULL;
358 
359 	lvs->bs_dev->destroy(lvs->bs_dev);
360 	free(lvs);
361 
362 	if (cb_fn != NULL) {
363 		cb_fn(cb_arg, 0);
364 	}
365 
366 	return 0;
367 }
368 
369 int
370 spdk_lvs_destroy(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn,
371 		 void *cb_arg)
372 {
373 	struct spdk_lvol *lvol, *tmp;
374 	char *alias;
375 
376 	TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) {
377 		TAILQ_REMOVE(&lvs->lvols, lvol, link);
378 
379 		alias = spdk_sprintf_alloc("%s/%s", lvs->name, lvol->name);
380 		if (alias == NULL) {
381 			SPDK_ERRLOG("Cannot alloc memory for alias\n");
382 			return -1;
383 		}
384 		spdk_bdev_alias_del(lvol->bdev, alias);
385 
386 		free(alias);
387 		free(lvol->unique_id);
388 		free(lvol);
389 	}
390 	g_lvol_store = NULL;
391 
392 	lvs->bs_dev->destroy(lvs->bs_dev);
393 	free(lvs);
394 
395 	if (cb_fn != NULL) {
396 		cb_fn(cb_arg, 0);
397 	}
398 
399 	return 0;
400 }
401 
402 void
403 spdk_lvol_resize(struct spdk_lvol *lvol, size_t sz,  spdk_lvol_op_complete cb_fn, void *cb_arg)
404 {
405 	cb_fn(cb_arg, 0);
406 }
407 
408 void
409 spdk_lvol_set_read_only(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg)
410 {
411 	cb_fn(cb_arg, 0);
412 }
413 
414 int
415 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
416 {
417 	bdev->blockcnt = size;
418 	return 0;
419 }
420 
421 uint64_t
422 spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
423 {
424 	return g_cluster_size;
425 }
426 
427 struct spdk_bdev *
428 spdk_bdev_get_by_name(const char *bdev_name)
429 {
430 	if (!strcmp(g_base_bdev->name, bdev_name)) {
431 		return g_base_bdev;
432 	}
433 
434 	return NULL;
435 }
436 
437 void
438 spdk_lvol_close(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg)
439 {
440 	lvol->ref_count--;
441 
442 	SPDK_CU_ASSERT_FATAL(cb_fn != NULL);
443 	cb_fn(cb_arg, 0);
444 }
445 
446 bool
447 spdk_lvol_deletable(struct spdk_lvol *lvol)
448 {
449 	return g_lvol_deletable;
450 }
451 
452 void
453 spdk_lvol_destroy(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg)
454 {
455 	if (lvol->ref_count != 0) {
456 		cb_fn(cb_arg, -ENODEV);
457 	}
458 
459 	TAILQ_REMOVE(&lvol->lvol_store->lvols, lvol, link);
460 
461 	SPDK_CU_ASSERT_FATAL(cb_fn != NULL);
462 	cb_fn(cb_arg, 0);
463 
464 	g_lvol = NULL;
465 	free(lvol->unique_id);
466 	free(lvol);
467 }
468 
469 void
470 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
471 {
472 }
473 
474 struct spdk_io_channel *spdk_lvol_get_io_channel(struct spdk_lvol *lvol)
475 {
476 	CU_ASSERT(lvol == g_lvol);
477 	return g_ch;
478 }
479 
480 void
481 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
482 {
483 	CU_ASSERT(cb == lvol_read);
484 }
485 
486 void
487 spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
488 		  void *payload, uint64_t offset, uint64_t length,
489 		  spdk_blob_op_complete cb_fn, void *cb_arg)
490 {
491 }
492 
493 void
494 spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
495 		   void *payload, uint64_t offset, uint64_t length,
496 		   spdk_blob_op_complete cb_fn, void *cb_arg)
497 {
498 }
499 
500 void
501 spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
502 		   uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
503 {
504 	CU_ASSERT(blob == NULL);
505 	CU_ASSERT(channel == g_ch);
506 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
507 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
508 }
509 
510 void
511 spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
512 			  uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
513 {
514 	CU_ASSERT(blob == NULL);
515 	CU_ASSERT(channel == g_ch);
516 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
517 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
518 }
519 
520 void
521 spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
522 		    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
523 		    spdk_blob_op_complete cb_fn, void *cb_arg)
524 {
525 	CU_ASSERT(blob == NULL);
526 	CU_ASSERT(channel == g_ch);
527 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
528 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
529 }
530 
531 void
532 spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
533 		   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
534 		   spdk_blob_op_complete cb_fn, void *cb_arg)
535 {
536 	CU_ASSERT(blob == NULL);
537 	CU_ASSERT(channel == g_ch);
538 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
539 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
540 }
541 
542 void
543 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
544 {
545 }
546 
547 int
548 spdk_json_write_name(struct spdk_json_write_ctx *w, const char *name)
549 {
550 	return 0;
551 }
552 
553 int
554 spdk_json_write_array_begin(struct spdk_json_write_ctx *w)
555 {
556 	return 0;
557 }
558 
559 int
560 spdk_json_write_array_end(struct spdk_json_write_ctx *w)
561 {
562 	return 0;
563 }
564 
565 int
566 spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val)
567 {
568 	return 0;
569 }
570 
571 int
572 spdk_json_write_bool(struct spdk_json_write_ctx *w, bool val)
573 {
574 	return 0;
575 }
576 
577 int
578 spdk_json_write_object_begin(struct spdk_json_write_ctx *w)
579 {
580 	return 0;
581 }
582 
583 int
584 spdk_json_write_object_end(struct spdk_json_write_ctx *w)
585 {
586 	return 0;
587 }
588 
589 const char *
590 spdk_bdev_get_name(const struct spdk_bdev *bdev)
591 {
592 	return "test";
593 }
594 
595 int
596 spdk_vbdev_register(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, int base_bdev_count)
597 {
598 	TAILQ_INIT(&vbdev->aliases);
599 
600 	g_registered_bdevs++;
601 	return 0;
602 }
603 
604 void
605 spdk_bdev_module_examine_done(struct spdk_bdev_module *module)
606 {
607 	SPDK_CU_ASSERT_FATAL(g_examine_done != true);
608 	g_examine_done = true;
609 }
610 
611 static struct spdk_lvol *
612 _lvol_create(struct spdk_lvol_store *lvs)
613 {
614 	struct spdk_lvol *lvol = calloc(1, sizeof(*lvol));
615 
616 	SPDK_CU_ASSERT_FATAL(lvol != NULL);
617 
618 	lvol->lvol_store = lvs;
619 	lvol->ref_count++;
620 	lvol->unique_id = spdk_sprintf_alloc("%s", "UNIT_TEST_UUID");
621 	SPDK_CU_ASSERT_FATAL(lvol->unique_id != NULL);
622 
623 	TAILQ_INSERT_TAIL(&lvol->lvol_store->lvols, lvol, link);
624 
625 	return lvol;
626 }
627 
628 int
629 spdk_lvol_create(struct spdk_lvol_store *lvs, const char *name, size_t sz,
630 		 bool thin_provision, spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
631 {
632 	struct spdk_lvol *lvol;
633 
634 	lvol = _lvol_create(lvs);
635 	snprintf(lvol->name, sizeof(lvol->name), "%s", name);
636 	cb_fn(cb_arg, lvol, 0);
637 
638 	return 0;
639 }
640 
641 void
642 spdk_lvol_create_snapshot(struct spdk_lvol *lvol, const char *snapshot_name,
643 			  spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
644 {
645 	struct spdk_lvol *snap;
646 
647 	snap = _lvol_create(lvol->lvol_store);
648 	snprintf(snap->name, sizeof(snap->name), "%s", snapshot_name);
649 	cb_fn(cb_arg, snap, 0);
650 }
651 
652 void
653 spdk_lvol_create_clone(struct spdk_lvol *lvol, const char *clone_name,
654 		       spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
655 {
656 	struct spdk_lvol *clone;
657 
658 	clone = _lvol_create(lvol->lvol_store);
659 	snprintf(clone->name, sizeof(clone->name), "%s", clone_name);
660 	cb_fn(cb_arg, clone, 0);
661 }
662 
663 static void
664 lvol_store_op_complete(void *cb_arg, int lvserrno)
665 {
666 	g_lvserrno = lvserrno;
667 	return;
668 }
669 
670 static void
671 lvol_store_op_with_handle_complete(void *cb_arg, struct spdk_lvol_store *lvs, int lvserrno)
672 {
673 	g_lvserrno = lvserrno;
674 	g_lvol_store = lvs;
675 	return;
676 }
677 
678 static void
679 vbdev_lvol_create_complete(void *cb_arg, struct spdk_lvol *lvol, int lvolerrno)
680 {
681 	g_lvolerrno = lvolerrno;
682 	g_lvol = lvol;
683 }
684 
685 static void
686 vbdev_lvol_resize_complete(void *cb_arg, int lvolerrno)
687 {
688 	g_lvolerrno = lvolerrno;
689 }
690 
691 static void
692 vbdev_lvol_set_read_only_complete(void *cb_arg, int lvolerrno)
693 {
694 	g_lvolerrno = lvolerrno;
695 }
696 
697 static void
698 vbdev_lvol_rename_complete(void *cb_arg, int lvolerrno)
699 {
700 	g_lvolerrno = lvolerrno;
701 }
702 
703 static void
704 ut_lvs_destroy(void)
705 {
706 	int rc = 0;
707 	int sz = 10;
708 	struct spdk_lvol_store *lvs;
709 
710 	/* Lvol store is successfully created */
711 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
712 	CU_ASSERT(rc == 0);
713 	CU_ASSERT(g_lvserrno == 0);
714 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
715 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
716 
717 	lvs = g_lvol_store;
718 	g_lvol_store = NULL;
719 
720 	spdk_uuid_generate(&lvs->uuid);
721 
722 	/* Successfully create lvol, which should be unloaded with lvs later */
723 	g_lvolerrno = -1;
724 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
725 	CU_ASSERT(rc == 0);
726 	CU_ASSERT(g_lvolerrno == 0);
727 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
728 
729 	/* Unload lvol store */
730 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
731 	CU_ASSERT(g_lvserrno == 0);
732 	CU_ASSERT(g_lvol_store == NULL);
733 }
734 
735 static void
736 ut_lvol_init(void)
737 {
738 	struct spdk_lvol_store *lvs;
739 	int sz = 10;
740 	int rc;
741 
742 	/* Lvol store is successfully created */
743 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
744 	CU_ASSERT(rc == 0);
745 	CU_ASSERT(g_lvserrno == 0);
746 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
747 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
748 	lvs = g_lvol_store;
749 
750 	/* Successful lvol create */
751 	g_lvolerrno = -1;
752 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
753 	SPDK_CU_ASSERT_FATAL(rc == 0);
754 	CU_ASSERT(g_lvol != NULL);
755 	CU_ASSERT(g_lvolerrno == 0);
756 
757 	/* Successful lvol destroy */
758 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
759 	CU_ASSERT(g_lvol == NULL);
760 
761 	/* Destroy lvol store */
762 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
763 	CU_ASSERT(g_lvserrno == 0);
764 	CU_ASSERT(g_lvol_store == NULL);
765 }
766 
767 static void
768 ut_lvol_snapshot(void)
769 {
770 	struct spdk_lvol_store *lvs;
771 	int sz = 10;
772 	int rc;
773 	struct spdk_lvol *lvol = NULL;
774 
775 	/* Lvol store is successfully created */
776 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
777 	CU_ASSERT(rc == 0);
778 	CU_ASSERT(g_lvserrno == 0);
779 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
780 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
781 	lvs = g_lvol_store;
782 
783 	/* Successful lvol create */
784 	g_lvolerrno = -1;
785 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
786 	SPDK_CU_ASSERT_FATAL(rc == 0);
787 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
788 	CU_ASSERT(g_lvolerrno == 0);
789 
790 	lvol = g_lvol;
791 
792 	/* Successful snap create */
793 	vbdev_lvol_create_snapshot(lvol, "snap", vbdev_lvol_create_complete, NULL);
794 	SPDK_CU_ASSERT_FATAL(rc == 0);
795 	CU_ASSERT(g_lvol != NULL);
796 	CU_ASSERT(g_lvolerrno == 0);
797 
798 	/* Successful lvol destroy */
799 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
800 	CU_ASSERT(g_lvol == NULL);
801 
802 	/* Successful snap destroy */
803 	g_lvol = lvol;
804 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
805 	CU_ASSERT(g_lvol == NULL);
806 
807 	/* Destroy lvol store */
808 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
809 	CU_ASSERT(g_lvserrno == 0);
810 	CU_ASSERT(g_lvol_store == NULL);
811 }
812 
813 static void
814 ut_lvol_clone(void)
815 {
816 	struct spdk_lvol_store *lvs;
817 	int sz = 10;
818 	int rc;
819 	struct spdk_lvol *lvol = NULL;
820 	struct spdk_lvol *snap = NULL;
821 	struct spdk_lvol *clone = NULL;
822 
823 	/* Lvol store is successfully created */
824 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
825 	CU_ASSERT(rc == 0);
826 	CU_ASSERT(g_lvserrno == 0);
827 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
828 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
829 	lvs = g_lvol_store;
830 
831 	/* Successful lvol create */
832 	g_lvolerrno = -1;
833 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
834 	SPDK_CU_ASSERT_FATAL(rc == 0);
835 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
836 	CU_ASSERT(g_lvolerrno == 0);
837 
838 	lvol = g_lvol;
839 
840 	/* Successful snap create */
841 	vbdev_lvol_create_snapshot(lvol, "snap", vbdev_lvol_create_complete, NULL);
842 	SPDK_CU_ASSERT_FATAL(rc == 0);
843 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
844 	CU_ASSERT(g_lvolerrno == 0);
845 
846 	snap = g_lvol;
847 
848 	/* Successful clone create */
849 	vbdev_lvol_create_clone(snap, "clone", vbdev_lvol_create_complete, NULL);
850 
851 	SPDK_CU_ASSERT_FATAL(rc == 0);
852 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
853 	CU_ASSERT(g_lvolerrno == 0);
854 
855 	clone = g_lvol;
856 
857 	/* Successful lvol destroy */
858 	g_lvol = lvol;
859 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
860 	CU_ASSERT(g_lvol == NULL);
861 
862 	/* Successful clone destroy */
863 	g_lvol = clone;
864 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
865 	CU_ASSERT(g_lvol == NULL);
866 
867 	/* Successful lvol destroy */
868 	g_lvol = snap;
869 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
870 	CU_ASSERT(g_lvol == NULL);
871 
872 	/* Destroy lvol store */
873 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
874 	CU_ASSERT(g_lvserrno == 0);
875 	CU_ASSERT(g_lvol_store == NULL);
876 }
877 
878 static void
879 ut_lvol_hotremove(void)
880 {
881 	int rc = 0;
882 
883 	lvol_store_initialize_fail = false;
884 	lvol_store_initialize_cb_fail = false;
885 	lvol_already_opened = false;
886 
887 	/* Lvol store is successfully created */
888 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
889 	CU_ASSERT(rc == 0);
890 	CU_ASSERT(g_lvserrno == 0);
891 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
892 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
893 
894 	/* Hot remove callback with NULL - stability check */
895 	vbdev_lvs_hotremove_cb(NULL);
896 
897 	/* Hot remove lvs on bdev removal */
898 	vbdev_lvs_hotremove_cb(&g_bdev);
899 
900 	CU_ASSERT(g_lvol_store == NULL);
901 	CU_ASSERT(TAILQ_EMPTY(&g_spdk_lvol_pairs));
902 
903 }
904 
905 static void
906 ut_lvs_examine_check(bool success)
907 {
908 	struct lvol_store_bdev *lvs_bdev;
909 
910 	/* Examine was finished regardless of result */
911 	CU_ASSERT(g_examine_done == true);
912 	g_examine_done = false;
913 
914 	if (success) {
915 		SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_spdk_lvol_pairs));
916 		lvs_bdev = TAILQ_FIRST(&g_spdk_lvol_pairs);
917 		SPDK_CU_ASSERT_FATAL(lvs_bdev != NULL);
918 		g_lvol_store = lvs_bdev->lvs;
919 		SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
920 		CU_ASSERT(g_lvol_store->bs_dev != NULL);
921 	} else {
922 		SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&g_spdk_lvol_pairs));
923 		g_lvol_store = NULL;
924 	}
925 }
926 
927 static void
928 ut_lvol_examine(void)
929 {
930 	/* Examine unsuccessfully - bdev already opened */
931 	g_lvserrno = -1;
932 	lvol_already_opened = true;
933 	vbdev_lvs_examine(&g_bdev);
934 	ut_lvs_examine_check(false);
935 
936 	/* Examine unsuccessfully - fail on lvol store */
937 	g_lvserrno = -1;
938 	lvol_already_opened = false;
939 	vbdev_lvs_examine(&g_bdev);
940 	ut_lvs_examine_check(false);
941 
942 	/* Examine successfully
943 	 * - one lvol fails to load
944 	 * - lvs is loaded with no lvols present */
945 	g_lvserrno = 0;
946 	g_lvolerrno = -1;
947 	g_num_lvols = 1;
948 	lvol_already_opened = false;
949 	g_registered_bdevs = 0;
950 	vbdev_lvs_examine(&g_bdev);
951 	ut_lvs_examine_check(true);
952 	CU_ASSERT(g_registered_bdevs == 0);
953 	CU_ASSERT(TAILQ_EMPTY(&g_lvol_store->lvols));
954 	vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL);
955 	CU_ASSERT(g_lvserrno == 0);
956 	CU_ASSERT(g_lvol_store == NULL);
957 
958 	/* Examine successfully */
959 	g_lvserrno = 0;
960 	g_lvolerrno = 0;
961 	g_registered_bdevs = 0;
962 	lvol_already_opened = false;
963 	vbdev_lvs_examine(&g_bdev);
964 	ut_lvs_examine_check(true);
965 	CU_ASSERT(g_registered_bdevs != 0);
966 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_lvol_store->lvols));
967 	vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL);
968 	CU_ASSERT(g_lvserrno == 0);
969 }
970 
971 static void
972 ut_lvol_rename(void)
973 {
974 	struct spdk_lvol_store *lvs;
975 	struct spdk_lvol *lvol;
976 	struct spdk_lvol *lvol2;
977 	int sz = 10;
978 	int rc;
979 
980 	/* Lvol store is successfully created */
981 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
982 	CU_ASSERT(rc == 0);
983 	CU_ASSERT(g_lvserrno == 0);
984 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
985 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
986 	lvs = g_lvol_store;
987 
988 	/* Successful lvols create */
989 	g_lvolerrno = -1;
990 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
991 	SPDK_CU_ASSERT_FATAL(rc == 0);
992 	CU_ASSERT(g_lvol != NULL);
993 	CU_ASSERT(g_lvolerrno == 0);
994 	lvol = g_lvol;
995 
996 	g_lvolerrno = -1;
997 	rc = vbdev_lvol_create(lvs, "lvol2", sz, false, vbdev_lvol_create_complete, NULL);
998 	SPDK_CU_ASSERT_FATAL(rc == 0);
999 	CU_ASSERT(g_lvol != NULL);
1000 	CU_ASSERT(g_lvolerrno == 0);
1001 	lvol2 = g_lvol;
1002 
1003 	/* Successful rename lvol */
1004 	vbdev_lvol_rename(lvol, "new_lvol_name", vbdev_lvol_rename_complete, NULL);
1005 	SPDK_CU_ASSERT_FATAL(g_lvolerrno == 0);
1006 	CU_ASSERT_STRING_EQUAL(lvol->name, "new_lvol_name");
1007 
1008 	/* Renaming lvol with name already existing */
1009 	g_bdev_alias_already_exists = true;
1010 	vbdev_lvol_rename(lvol2, "new_lvol_name", vbdev_lvol_rename_complete, NULL);
1011 	g_bdev_alias_already_exists = false;
1012 	SPDK_CU_ASSERT_FATAL(g_lvolerrno != 0);
1013 	CU_ASSERT_STRING_NOT_EQUAL(lvol2->name, "new_lvol_name");
1014 
1015 	/* Renaming lvol with it's own name */
1016 	vbdev_lvol_rename(lvol, "new_lvol_name", vbdev_lvol_rename_complete, NULL);
1017 	SPDK_CU_ASSERT_FATAL(g_lvolerrno == 0);
1018 	CU_ASSERT_STRING_EQUAL(lvol->name, "new_lvol_name");
1019 
1020 	/* Successful lvols destroy */
1021 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1022 	CU_ASSERT(g_lvol == NULL);
1023 
1024 	vbdev_lvol_destroy(lvol2, lvol_store_op_complete, NULL);
1025 	CU_ASSERT(g_lvol == NULL);
1026 
1027 	/* Destroy lvol store */
1028 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1029 	CU_ASSERT(g_lvserrno == 0);
1030 	CU_ASSERT(g_lvol_store == NULL);
1031 }
1032 
1033 static void
1034 ut_lvol_destroy(void)
1035 {
1036 	struct spdk_lvol_store *lvs;
1037 	struct spdk_lvol *lvol;
1038 	struct spdk_lvol *lvol2;
1039 	int sz = 10;
1040 	int rc;
1041 
1042 	/* Lvol store is successfully created */
1043 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1044 	CU_ASSERT(rc == 0);
1045 	CU_ASSERT(g_lvserrno == 0);
1046 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1047 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1048 	lvs = g_lvol_store;
1049 
1050 	/* Successful lvols create */
1051 	g_lvolerrno = -1;
1052 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
1053 	SPDK_CU_ASSERT_FATAL(rc == 0);
1054 	CU_ASSERT(g_lvol != NULL);
1055 	CU_ASSERT(g_lvolerrno == 0);
1056 	lvol = g_lvol;
1057 
1058 	g_lvolerrno = -1;
1059 	rc = vbdev_lvol_create(lvs, "lvol2", sz, false, vbdev_lvol_create_complete, NULL);
1060 	SPDK_CU_ASSERT_FATAL(rc == 0);
1061 	CU_ASSERT(g_lvol != NULL);
1062 	CU_ASSERT(g_lvolerrno == 0);
1063 	lvol2 = g_lvol;
1064 
1065 	/* Unsuccessful lvols destroy */
1066 	g_lvol_deletable = false;
1067 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1068 	CU_ASSERT(g_lvol != NULL);
1069 	CU_ASSERT(g_lvserrno == -EPERM);
1070 
1071 	g_lvol_deletable = true;
1072 	/* Successful lvols destroy */
1073 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1074 	CU_ASSERT(g_lvol == NULL);
1075 	CU_ASSERT(g_lvolerrno == 0);
1076 
1077 	/* Hot remove lvol bdev */
1078 	vbdev_lvol_unregister(lvol2);
1079 
1080 	/* Unload lvol store */
1081 	vbdev_lvs_unload(lvs, lvol_store_op_complete, NULL);
1082 	CU_ASSERT(g_lvserrno == 0);
1083 	CU_ASSERT(g_lvol_store == NULL);
1084 }
1085 
1086 static void
1087 ut_lvol_resize(void)
1088 {
1089 	struct spdk_lvol_store *lvs;
1090 	struct spdk_lvol *lvol;
1091 	int sz = 10;
1092 	int rc = 0;
1093 
1094 	/* Lvol store is successfully created */
1095 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1096 	CU_ASSERT(rc == 0);
1097 	CU_ASSERT(g_lvserrno == 0);
1098 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1099 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1100 	lvs = g_lvol_store;
1101 
1102 	/* Successful lvol create */
1103 	g_lvolerrno = -1;
1104 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
1105 	CU_ASSERT(rc == 0);
1106 	CU_ASSERT(g_lvolerrno == 0);
1107 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1108 	lvol = g_lvol;
1109 
1110 	/* Successful lvol resize */
1111 	g_lvolerrno = -1;
1112 	vbdev_lvol_resize(lvol, 20, vbdev_lvol_resize_complete, NULL);
1113 	CU_ASSERT(g_lvolerrno == 0);
1114 	CU_ASSERT(lvol->bdev->blockcnt == 20 * g_cluster_size / lvol->bdev->blocklen);
1115 
1116 	/* Resize with NULL lvol */
1117 	vbdev_lvol_resize(NULL, 20, vbdev_lvol_resize_complete, NULL);
1118 	CU_ASSERT(g_lvolerrno != 0);
1119 
1120 	/* Successful lvol destroy */
1121 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1122 	CU_ASSERT(g_lvol == NULL);
1123 
1124 	/* Destroy lvol store */
1125 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1126 	CU_ASSERT(g_lvserrno == 0);
1127 	CU_ASSERT(g_lvol_store == NULL);
1128 }
1129 
1130 static void
1131 ut_lvol_set_read_only(void)
1132 {
1133 	struct spdk_lvol_store *lvs;
1134 	struct spdk_lvol *lvol;
1135 	int sz = 10;
1136 	int rc = 0;
1137 
1138 	/* Lvol store is successfully created */
1139 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1140 	CU_ASSERT(rc == 0);
1141 	CU_ASSERT(g_lvserrno == 0);
1142 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1143 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1144 	lvs = g_lvol_store;
1145 
1146 	/* Successful lvol create */
1147 	g_lvolerrno = -1;
1148 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
1149 	CU_ASSERT(rc == 0);
1150 	CU_ASSERT(g_lvolerrno == 0);
1151 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1152 	lvol = g_lvol;
1153 
1154 	/* Successful set lvol as read only */
1155 	g_lvolerrno = -1;
1156 	vbdev_lvol_set_read_only(lvol, vbdev_lvol_set_read_only_complete, NULL);
1157 	CU_ASSERT(g_lvolerrno == 0);
1158 
1159 	/* Successful lvol destroy */
1160 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1161 	CU_ASSERT(g_lvol == NULL);
1162 
1163 	/* Destroy lvol store */
1164 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1165 	CU_ASSERT(g_lvserrno == 0);
1166 	CU_ASSERT(g_lvol_store == NULL);
1167 }
1168 
1169 static void
1170 ut_lvs_unload(void)
1171 {
1172 	int rc = 0;
1173 	int sz = 10;
1174 	struct spdk_lvol_store *lvs;
1175 
1176 	/* Lvol store is successfully created */
1177 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1178 	CU_ASSERT(rc == 0);
1179 	CU_ASSERT(g_lvserrno == 0);
1180 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1181 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1182 
1183 	lvs = g_lvol_store;
1184 	g_lvol_store = NULL;
1185 
1186 	spdk_uuid_generate(&lvs->uuid);
1187 
1188 	/* Successfully create lvol, which should be destroyed with lvs later */
1189 	g_lvolerrno = -1;
1190 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
1191 	CU_ASSERT(rc == 0);
1192 	CU_ASSERT(g_lvolerrno == 0);
1193 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1194 
1195 	/* Unload lvol store */
1196 	vbdev_lvs_unload(lvs, lvol_store_op_complete, NULL);
1197 	CU_ASSERT(g_lvserrno == 0);
1198 	CU_ASSERT(g_lvol_store == NULL);
1199 	CU_ASSERT(g_lvol != NULL);
1200 }
1201 
1202 static void
1203 ut_lvs_init(void)
1204 {
1205 	int rc = 0;
1206 	struct spdk_lvol_store *lvs;
1207 
1208 	/* spdk_lvs_init() fails */
1209 	lvol_store_initialize_fail = true;
1210 
1211 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1212 	CU_ASSERT(rc != 0);
1213 	CU_ASSERT(g_lvserrno == 0);
1214 	CU_ASSERT(g_lvol_store == NULL);
1215 
1216 	lvol_store_initialize_fail = false;
1217 
1218 	/* spdk_lvs_init_cb() fails */
1219 	lvol_store_initialize_cb_fail = true;
1220 
1221 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1222 	CU_ASSERT(rc == 0);
1223 	CU_ASSERT(g_lvserrno != 0);
1224 	CU_ASSERT(g_lvol_store == NULL);
1225 
1226 	lvol_store_initialize_cb_fail = false;
1227 
1228 	/* Lvol store is successfully created */
1229 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1230 	CU_ASSERT(rc == 0);
1231 	CU_ASSERT(g_lvserrno == 0);
1232 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1233 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1234 
1235 	lvs = g_lvol_store;
1236 	g_lvol_store = NULL;
1237 
1238 	/* Bdev with lvol store already claimed */
1239 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1240 	CU_ASSERT(rc != 0);
1241 	CU_ASSERT(g_lvserrno == 0);
1242 	CU_ASSERT(g_lvol_store == NULL);
1243 
1244 	/* Destruct lvol store */
1245 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1246 	CU_ASSERT(g_lvserrno == 0);
1247 	CU_ASSERT(g_lvol_store == NULL);
1248 }
1249 
1250 static void
1251 ut_vbdev_lvol_get_io_channel(void)
1252 {
1253 	struct spdk_io_channel *ch;
1254 
1255 	g_lvol = calloc(1, sizeof(struct spdk_lvol));
1256 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1257 
1258 	ch = vbdev_lvol_get_io_channel(g_lvol);
1259 	CU_ASSERT(ch == g_ch);
1260 
1261 	free(g_lvol);
1262 }
1263 
1264 static void
1265 ut_vbdev_lvol_io_type_supported(void)
1266 {
1267 	struct spdk_lvol *lvol;
1268 	bool ret;
1269 
1270 	lvol = calloc(1, sizeof(struct spdk_lvol));
1271 	SPDK_CU_ASSERT_FATAL(lvol != NULL);
1272 
1273 	g_blob_is_read_only = false;
1274 
1275 	/* Supported types */
1276 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_READ);
1277 	CU_ASSERT(ret == true);
1278 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE);
1279 	CU_ASSERT(ret == true);
1280 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_RESET);
1281 	CU_ASSERT(ret == true);
1282 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_UNMAP);
1283 	CU_ASSERT(ret == true);
1284 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE_ZEROES);
1285 	CU_ASSERT(ret == true);
1286 
1287 	/* Unsupported types */
1288 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_FLUSH);
1289 	CU_ASSERT(ret == false);
1290 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_ADMIN);
1291 	CU_ASSERT(ret == false);
1292 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_IO);
1293 	CU_ASSERT(ret == false);
1294 
1295 	g_blob_is_read_only = true;
1296 
1297 	/* Supported types */
1298 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_READ);
1299 	CU_ASSERT(ret == true);
1300 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_RESET);
1301 	CU_ASSERT(ret == true);
1302 
1303 	/* Unsupported types */
1304 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE);
1305 	CU_ASSERT(ret == false);
1306 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_UNMAP);
1307 	CU_ASSERT(ret == false);
1308 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE_ZEROES);
1309 	CU_ASSERT(ret == false);
1310 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_FLUSH);
1311 	CU_ASSERT(ret == false);
1312 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_ADMIN);
1313 	CU_ASSERT(ret == false);
1314 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_IO);
1315 	CU_ASSERT(ret == false);
1316 
1317 	free(lvol);
1318 }
1319 
1320 static void
1321 ut_lvol_read_write(void)
1322 {
1323 	g_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct lvol_task));
1324 	SPDK_CU_ASSERT_FATAL(g_io != NULL);
1325 	g_base_bdev = calloc(1, sizeof(struct spdk_bdev));
1326 	SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL);
1327 	g_lvol = calloc(1, sizeof(struct spdk_lvol));
1328 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1329 
1330 	g_task = (struct lvol_task *)g_io->driver_ctx;
1331 	g_io->bdev = g_base_bdev;
1332 	g_io->bdev->ctxt = g_lvol;
1333 	g_io->u.bdev.offset_blocks = 20;
1334 	g_io->u.bdev.num_blocks = 20;
1335 
1336 	lvol_read(g_ch, g_io);
1337 	CU_ASSERT(g_task->status == SPDK_BDEV_IO_STATUS_SUCCESS);
1338 
1339 	lvol_write(g_lvol, g_ch, g_io);
1340 	CU_ASSERT(g_task->status == SPDK_BDEV_IO_STATUS_SUCCESS);
1341 
1342 	free(g_io);
1343 	free(g_base_bdev);
1344 	free(g_lvol);
1345 }
1346 
1347 static void
1348 ut_vbdev_lvol_submit_request(void)
1349 {
1350 	struct spdk_lvol request_lvol = {};
1351 	g_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct lvol_task));
1352 	SPDK_CU_ASSERT_FATAL(g_io != NULL);
1353 	g_base_bdev = calloc(1, sizeof(struct spdk_bdev));
1354 	SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL);
1355 	g_task = (struct lvol_task *)g_io->driver_ctx;
1356 	g_io->bdev = g_base_bdev;
1357 
1358 	g_io->type = SPDK_BDEV_IO_TYPE_READ;
1359 	g_base_bdev->ctxt = &request_lvol;
1360 	vbdev_lvol_submit_request(g_ch, g_io);
1361 
1362 	free(g_io);
1363 	free(g_base_bdev);
1364 }
1365 
1366 static void
1367 ut_lvs_rename(void)
1368 {
1369 	int rc = 0;
1370 	int sz = 10;
1371 	struct spdk_lvol_store *lvs;
1372 
1373 	/* Lvol store is successfully created */
1374 	rc = vbdev_lvs_create(&g_bdev, "old_lvs_name", 0, lvol_store_op_with_handle_complete, NULL);
1375 	CU_ASSERT(rc == 0);
1376 	CU_ASSERT(g_lvserrno == 0);
1377 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1378 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1379 
1380 	lvs = g_lvol_store;
1381 	g_lvol_store = NULL;
1382 
1383 	g_base_bdev = calloc(1, sizeof(*g_base_bdev));
1384 	SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL);
1385 
1386 	/* Successfully create lvol, which should be destroyed with lvs later */
1387 	g_lvolerrno = -1;
1388 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, vbdev_lvol_create_complete, NULL);
1389 	CU_ASSERT(rc == 0);
1390 	CU_ASSERT(g_lvolerrno == 0);
1391 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1392 
1393 	/* Trying to rename lvs with lvols created */
1394 	vbdev_lvs_rename(lvs, "new_lvs_name", lvol_store_op_complete, NULL);
1395 	CU_ASSERT(g_lvserrno == 0);
1396 	CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name");
1397 	CU_ASSERT_STRING_EQUAL(TAILQ_FIRST(&g_lvol->bdev->aliases)->alias, "new_lvs_name/lvol");
1398 
1399 	/* Trying to rename lvs with name already used by another lvs */
1400 	/* This is a bdev_lvol test, so g_lvs_with_name_already_exists simulates
1401 	 * existing lvs with name 'another_new_lvs_name' and this name in fact is not compared */
1402 	g_lvs_with_name_already_exists = true;
1403 	vbdev_lvs_rename(lvs, "another_new_lvs_name", lvol_store_op_complete, NULL);
1404 	CU_ASSERT(g_lvserrno == -EEXIST);
1405 	CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name");
1406 	CU_ASSERT_STRING_EQUAL(TAILQ_FIRST(&g_lvol->bdev->aliases)->alias, "new_lvs_name/lvol");
1407 	g_lvs_with_name_already_exists = false;
1408 
1409 	/* Unload lvol store */
1410 	g_lvol_store = lvs;
1411 	vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL);
1412 	CU_ASSERT(g_lvserrno == 0);
1413 	CU_ASSERT(g_lvol_store == NULL);
1414 
1415 	free(g_base_bdev->name);
1416 	free(g_base_bdev);
1417 }
1418 
1419 int main(int argc, char **argv)
1420 {
1421 	CU_pSuite	suite = NULL;
1422 	unsigned int	num_failures;
1423 
1424 	if (CU_initialize_registry() != CUE_SUCCESS) {
1425 		return CU_get_error();
1426 	}
1427 
1428 	suite = CU_add_suite("lvol", NULL, NULL);
1429 	if (suite == NULL) {
1430 		CU_cleanup_registry();
1431 		return CU_get_error();
1432 	}
1433 
1434 	if (
1435 		CU_add_test(suite, "ut_lvs_init", ut_lvs_init) == NULL ||
1436 		CU_add_test(suite, "ut_lvol_init", ut_lvol_init) == NULL ||
1437 		CU_add_test(suite, "ut_lvol_snapshot", ut_lvol_snapshot) == NULL ||
1438 		CU_add_test(suite, "ut_lvol_clone", ut_lvol_clone) == NULL ||
1439 		CU_add_test(suite, "ut_lvs_destroy", ut_lvs_destroy) == NULL ||
1440 		CU_add_test(suite, "ut_lvs_unload", ut_lvs_unload) == NULL ||
1441 		CU_add_test(suite, "ut_lvol_resize", ut_lvol_resize) == NULL ||
1442 		CU_add_test(suite, "ut_lvol_set_read_only", ut_lvol_set_read_only) == NULL ||
1443 		CU_add_test(suite, "lvol_hotremove", ut_lvol_hotremove) == NULL ||
1444 		CU_add_test(suite, "ut_vbdev_lvol_get_io_channel", ut_vbdev_lvol_get_io_channel) == NULL ||
1445 		CU_add_test(suite, "ut_vbdev_lvol_io_type_supported", ut_vbdev_lvol_io_type_supported) == NULL ||
1446 		CU_add_test(suite, "ut_lvol_read_write", ut_lvol_read_write) == NULL ||
1447 		CU_add_test(suite, "ut_vbdev_lvol_submit_request", ut_vbdev_lvol_submit_request) == NULL ||
1448 		CU_add_test(suite, "lvol_examine", ut_lvol_examine) == NULL ||
1449 		CU_add_test(suite, "ut_lvol_rename", ut_lvol_rename) == NULL ||
1450 		CU_add_test(suite, "ut_lvol_destroy", ut_lvol_destroy) == NULL ||
1451 		CU_add_test(suite, "ut_lvs_rename", ut_lvs_rename) == NULL
1452 	) {
1453 		CU_cleanup_registry();
1454 		return CU_get_error();
1455 	}
1456 
1457 	CU_basic_set_mode(CU_BRM_VERBOSE);
1458 	CU_basic_run_tests();
1459 	num_failures = CU_get_number_of_failures();
1460 	CU_cleanup_registry();
1461 	return num_failures;
1462 }
1463