xref: /spdk/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c (revision adb39585efa6b2bcdf2227606563b1e5947d145a)
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);
355 	}
356 	g_lvol_store = NULL;
357 
358 	lvs->bs_dev->destroy(lvs->bs_dev);
359 	free(lvs);
360 
361 	if (cb_fn != NULL) {
362 		cb_fn(cb_arg, 0);
363 	}
364 
365 	return 0;
366 }
367 
368 int
369 spdk_lvs_destroy(struct spdk_lvol_store *lvs, spdk_lvs_op_complete cb_fn,
370 		 void *cb_arg)
371 {
372 	struct spdk_lvol *lvol, *tmp;
373 	char *alias;
374 
375 	TAILQ_FOREACH_SAFE(lvol, &lvs->lvols, link, tmp) {
376 		TAILQ_REMOVE(&lvs->lvols, lvol, link);
377 
378 		alias = spdk_sprintf_alloc("%s/%s", lvs->name, lvol->name);
379 		if (alias == NULL) {
380 			SPDK_ERRLOG("Cannot alloc memory for alias\n");
381 			return -1;
382 		}
383 		spdk_bdev_alias_del(lvol->bdev, alias);
384 
385 		free(alias);
386 		free(lvol);
387 	}
388 	g_lvol_store = NULL;
389 
390 	lvs->bs_dev->destroy(lvs->bs_dev);
391 	free(lvs);
392 
393 	if (cb_fn != NULL) {
394 		cb_fn(cb_arg, 0);
395 	}
396 
397 	return 0;
398 }
399 
400 void
401 spdk_lvol_resize(struct spdk_lvol *lvol, size_t sz,  spdk_lvol_op_complete cb_fn, void *cb_arg)
402 {
403 	cb_fn(cb_arg, 0);
404 }
405 
406 void
407 spdk_lvol_set_read_only(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg)
408 {
409 	cb_fn(cb_arg, 0);
410 }
411 
412 int
413 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
414 {
415 	bdev->blockcnt = size;
416 	return 0;
417 }
418 
419 uint64_t
420 spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
421 {
422 	return g_cluster_size;
423 }
424 
425 struct spdk_bdev *
426 spdk_bdev_get_by_name(const char *bdev_name)
427 {
428 	if (!strcmp(g_base_bdev->name, bdev_name)) {
429 		return g_base_bdev;
430 	}
431 
432 	return NULL;
433 }
434 
435 void
436 spdk_lvol_close(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg)
437 {
438 	lvol->ref_count--;
439 
440 	SPDK_CU_ASSERT_FATAL(cb_fn != NULL);
441 	cb_fn(cb_arg, 0);
442 }
443 
444 bool
445 spdk_lvol_deletable(struct spdk_lvol *lvol)
446 {
447 	return g_lvol_deletable;
448 }
449 
450 void
451 spdk_lvol_destroy(struct spdk_lvol *lvol, spdk_lvol_op_complete cb_fn, void *cb_arg)
452 {
453 	if (lvol->ref_count != 0) {
454 		cb_fn(cb_arg, -ENODEV);
455 	}
456 
457 	TAILQ_REMOVE(&lvol->lvol_store->lvols, lvol, link);
458 
459 	SPDK_CU_ASSERT_FATAL(cb_fn != NULL);
460 	cb_fn(cb_arg, 0);
461 
462 	g_lvol = NULL;
463 	free(lvol);
464 }
465 
466 void
467 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
468 {
469 }
470 
471 struct spdk_io_channel *spdk_lvol_get_io_channel(struct spdk_lvol *lvol)
472 {
473 	CU_ASSERT(lvol == g_lvol);
474 	return g_ch;
475 }
476 
477 void
478 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
479 {
480 	CU_ASSERT(cb == lvol_read);
481 }
482 
483 void
484 spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
485 		  void *payload, uint64_t offset, uint64_t length,
486 		  spdk_blob_op_complete cb_fn, void *cb_arg)
487 {
488 }
489 
490 void
491 spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
492 		   void *payload, uint64_t offset, uint64_t length,
493 		   spdk_blob_op_complete cb_fn, void *cb_arg)
494 {
495 }
496 
497 void
498 spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
499 		   uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
500 {
501 	CU_ASSERT(blob == NULL);
502 	CU_ASSERT(channel == g_ch);
503 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
504 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
505 }
506 
507 void
508 spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
509 			  uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
510 {
511 	CU_ASSERT(blob == NULL);
512 	CU_ASSERT(channel == g_ch);
513 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
514 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
515 }
516 
517 void
518 spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
519 		    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
520 		    spdk_blob_op_complete cb_fn, void *cb_arg)
521 {
522 	CU_ASSERT(blob == NULL);
523 	CU_ASSERT(channel == g_ch);
524 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
525 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
526 }
527 
528 void
529 spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
530 		   struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
531 		   spdk_blob_op_complete cb_fn, void *cb_arg)
532 {
533 	CU_ASSERT(blob == NULL);
534 	CU_ASSERT(channel == g_ch);
535 	CU_ASSERT(offset == g_io->u.bdev.offset_blocks);
536 	CU_ASSERT(length == g_io->u.bdev.num_blocks);
537 }
538 
539 void
540 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
541 {
542 }
543 
544 int
545 spdk_json_write_name(struct spdk_json_write_ctx *w, const char *name)
546 {
547 	return 0;
548 }
549 
550 int
551 spdk_json_write_array_begin(struct spdk_json_write_ctx *w)
552 {
553 	return 0;
554 }
555 
556 int
557 spdk_json_write_array_end(struct spdk_json_write_ctx *w)
558 {
559 	return 0;
560 }
561 
562 int
563 spdk_json_write_string(struct spdk_json_write_ctx *w, const char *val)
564 {
565 	return 0;
566 }
567 
568 int
569 spdk_json_write_bool(struct spdk_json_write_ctx *w, bool val)
570 {
571 	return 0;
572 }
573 
574 int
575 spdk_json_write_object_begin(struct spdk_json_write_ctx *w)
576 {
577 	return 0;
578 }
579 
580 int
581 spdk_json_write_object_end(struct spdk_json_write_ctx *w)
582 {
583 	return 0;
584 }
585 
586 const char *
587 spdk_bdev_get_name(const struct spdk_bdev *bdev)
588 {
589 	return "test";
590 }
591 
592 int
593 spdk_vbdev_register(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, int base_bdev_count)
594 {
595 	TAILQ_INIT(&vbdev->aliases);
596 
597 	g_registered_bdevs++;
598 	return 0;
599 }
600 
601 void
602 spdk_bdev_module_examine_done(struct spdk_bdev_module *module)
603 {
604 	SPDK_CU_ASSERT_FATAL(g_examine_done != true);
605 	g_examine_done = true;
606 }
607 
608 static struct spdk_lvol *
609 _lvol_create(struct spdk_lvol_store *lvs)
610 {
611 	struct spdk_lvol *lvol = calloc(1, sizeof(*lvol));
612 
613 	SPDK_CU_ASSERT_FATAL(lvol != NULL);
614 
615 	lvol->lvol_store = lvs;
616 	lvol->ref_count++;
617 	snprintf(lvol->unique_id, sizeof(lvol->unique_id), "%s", "UNIT_TEST_UUID");
618 
619 	TAILQ_INSERT_TAIL(&lvol->lvol_store->lvols, lvol, link);
620 
621 	return lvol;
622 }
623 
624 int
625 spdk_lvol_create(struct spdk_lvol_store *lvs, const char *name, size_t sz,
626 		 bool thin_provision, enum lvol_clear_method clear_method, spdk_lvol_op_with_handle_complete cb_fn,
627 		 void *cb_arg)
628 {
629 	struct spdk_lvol *lvol;
630 
631 	lvol = _lvol_create(lvs);
632 	snprintf(lvol->name, sizeof(lvol->name), "%s", name);
633 	cb_fn(cb_arg, lvol, 0);
634 
635 	return 0;
636 }
637 
638 void
639 spdk_lvol_create_snapshot(struct spdk_lvol *lvol, const char *snapshot_name,
640 			  spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
641 {
642 	struct spdk_lvol *snap;
643 
644 	snap = _lvol_create(lvol->lvol_store);
645 	snprintf(snap->name, sizeof(snap->name), "%s", snapshot_name);
646 	cb_fn(cb_arg, snap, 0);
647 }
648 
649 void
650 spdk_lvol_create_clone(struct spdk_lvol *lvol, const char *clone_name,
651 		       spdk_lvol_op_with_handle_complete cb_fn, void *cb_arg)
652 {
653 	struct spdk_lvol *clone;
654 
655 	clone = _lvol_create(lvol->lvol_store);
656 	snprintf(clone->name, sizeof(clone->name), "%s", clone_name);
657 	cb_fn(cb_arg, clone, 0);
658 }
659 
660 static void
661 lvol_store_op_complete(void *cb_arg, int lvserrno)
662 {
663 	g_lvserrno = lvserrno;
664 	return;
665 }
666 
667 static void
668 lvol_store_op_with_handle_complete(void *cb_arg, struct spdk_lvol_store *lvs, int lvserrno)
669 {
670 	g_lvserrno = lvserrno;
671 	g_lvol_store = lvs;
672 	return;
673 }
674 
675 static void
676 vbdev_lvol_create_complete(void *cb_arg, struct spdk_lvol *lvol, int lvolerrno)
677 {
678 	g_lvolerrno = lvolerrno;
679 	g_lvol = lvol;
680 }
681 
682 static void
683 vbdev_lvol_resize_complete(void *cb_arg, int lvolerrno)
684 {
685 	g_lvolerrno = lvolerrno;
686 }
687 
688 static void
689 vbdev_lvol_set_read_only_complete(void *cb_arg, int lvolerrno)
690 {
691 	g_lvolerrno = lvolerrno;
692 }
693 
694 static void
695 vbdev_lvol_rename_complete(void *cb_arg, int lvolerrno)
696 {
697 	g_lvolerrno = lvolerrno;
698 }
699 
700 static void
701 ut_lvs_destroy(void)
702 {
703 	int rc = 0;
704 	int sz = 10;
705 	struct spdk_lvol_store *lvs;
706 
707 	/* Lvol store is successfully created */
708 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
709 	CU_ASSERT(rc == 0);
710 	CU_ASSERT(g_lvserrno == 0);
711 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
712 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
713 
714 	lvs = g_lvol_store;
715 	g_lvol_store = NULL;
716 
717 	spdk_uuid_generate(&lvs->uuid);
718 
719 	/* Successfully create lvol, which should be unloaded with lvs later */
720 	g_lvolerrno = -1;
721 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
722 			       NULL);
723 	CU_ASSERT(rc == 0);
724 	CU_ASSERT(g_lvolerrno == 0);
725 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
726 
727 	/* Unload lvol store */
728 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
729 	CU_ASSERT(g_lvserrno == 0);
730 	CU_ASSERT(g_lvol_store == NULL);
731 }
732 
733 static void
734 ut_lvol_init(void)
735 {
736 	struct spdk_lvol_store *lvs;
737 	int sz = 10;
738 	int rc;
739 
740 	/* Lvol store is successfully created */
741 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
742 	CU_ASSERT(rc == 0);
743 	CU_ASSERT(g_lvserrno == 0);
744 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
745 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
746 	lvs = g_lvol_store;
747 
748 	/* Successful lvol create */
749 	g_lvolerrno = -1;
750 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
751 			       NULL);
752 	SPDK_CU_ASSERT_FATAL(rc == 0);
753 	CU_ASSERT(g_lvol != NULL);
754 	CU_ASSERT(g_lvolerrno == 0);
755 
756 	/* Successful lvol destroy */
757 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
758 	CU_ASSERT(g_lvol == NULL);
759 
760 	/* Destroy lvol store */
761 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
762 	CU_ASSERT(g_lvserrno == 0);
763 	CU_ASSERT(g_lvol_store == NULL);
764 }
765 
766 static void
767 ut_lvol_snapshot(void)
768 {
769 	struct spdk_lvol_store *lvs;
770 	int sz = 10;
771 	int rc;
772 	struct spdk_lvol *lvol = NULL;
773 
774 	/* Lvol store is successfully created */
775 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
776 	CU_ASSERT(rc == 0);
777 	CU_ASSERT(g_lvserrno == 0);
778 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
779 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
780 	lvs = g_lvol_store;
781 
782 	/* Successful lvol create */
783 	g_lvolerrno = -1;
784 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
785 			       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, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
834 			       NULL);
835 	SPDK_CU_ASSERT_FATAL(rc == 0);
836 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
837 	CU_ASSERT(g_lvolerrno == 0);
838 
839 	lvol = g_lvol;
840 
841 	/* Successful snap create */
842 	vbdev_lvol_create_snapshot(lvol, "snap", vbdev_lvol_create_complete, NULL);
843 	SPDK_CU_ASSERT_FATAL(rc == 0);
844 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
845 	CU_ASSERT(g_lvolerrno == 0);
846 
847 	snap = g_lvol;
848 
849 	/* Successful clone create */
850 	vbdev_lvol_create_clone(snap, "clone", vbdev_lvol_create_complete, NULL);
851 
852 	SPDK_CU_ASSERT_FATAL(rc == 0);
853 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
854 	CU_ASSERT(g_lvolerrno == 0);
855 
856 	clone = g_lvol;
857 
858 	/* Successful lvol destroy */
859 	g_lvol = lvol;
860 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
861 	CU_ASSERT(g_lvol == NULL);
862 
863 	/* Successful clone destroy */
864 	g_lvol = clone;
865 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
866 	CU_ASSERT(g_lvol == NULL);
867 
868 	/* Successful lvol destroy */
869 	g_lvol = snap;
870 	vbdev_lvol_destroy(g_lvol, lvol_store_op_complete, NULL);
871 	CU_ASSERT(g_lvol == NULL);
872 
873 	/* Destroy lvol store */
874 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
875 	CU_ASSERT(g_lvserrno == 0);
876 	CU_ASSERT(g_lvol_store == NULL);
877 }
878 
879 static void
880 ut_lvol_hotremove(void)
881 {
882 	int rc = 0;
883 
884 	lvol_store_initialize_fail = false;
885 	lvol_store_initialize_cb_fail = false;
886 	lvol_already_opened = false;
887 
888 	/* Lvol store is successfully created */
889 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
890 	CU_ASSERT(rc == 0);
891 	CU_ASSERT(g_lvserrno == 0);
892 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
893 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
894 
895 	/* Hot remove callback with NULL - stability check */
896 	vbdev_lvs_hotremove_cb(NULL);
897 
898 	/* Hot remove lvs on bdev removal */
899 	vbdev_lvs_hotremove_cb(&g_bdev);
900 
901 	CU_ASSERT(g_lvol_store == NULL);
902 	CU_ASSERT(TAILQ_EMPTY(&g_spdk_lvol_pairs));
903 
904 }
905 
906 static void
907 ut_lvs_examine_check(bool success)
908 {
909 	struct lvol_store_bdev *lvs_bdev;
910 
911 	/* Examine was finished regardless of result */
912 	CU_ASSERT(g_examine_done == true);
913 	g_examine_done = false;
914 
915 	if (success) {
916 		SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_spdk_lvol_pairs));
917 		lvs_bdev = TAILQ_FIRST(&g_spdk_lvol_pairs);
918 		SPDK_CU_ASSERT_FATAL(lvs_bdev != NULL);
919 		g_lvol_store = lvs_bdev->lvs;
920 		SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
921 		CU_ASSERT(g_lvol_store->bs_dev != NULL);
922 	} else {
923 		SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&g_spdk_lvol_pairs));
924 		g_lvol_store = NULL;
925 	}
926 }
927 
928 static void
929 ut_lvol_examine(void)
930 {
931 	/* Examine unsuccessfully - bdev already opened */
932 	g_lvserrno = -1;
933 	lvol_already_opened = true;
934 	vbdev_lvs_examine(&g_bdev);
935 	ut_lvs_examine_check(false);
936 
937 	/* Examine unsuccessfully - fail on lvol store */
938 	g_lvserrno = -1;
939 	lvol_already_opened = false;
940 	vbdev_lvs_examine(&g_bdev);
941 	ut_lvs_examine_check(false);
942 
943 	/* Examine successfully
944 	 * - one lvol fails to load
945 	 * - lvs is loaded with no lvols present */
946 	g_lvserrno = 0;
947 	g_lvolerrno = -1;
948 	g_num_lvols = 1;
949 	lvol_already_opened = false;
950 	g_registered_bdevs = 0;
951 	vbdev_lvs_examine(&g_bdev);
952 	ut_lvs_examine_check(true);
953 	CU_ASSERT(g_registered_bdevs == 0);
954 	CU_ASSERT(TAILQ_EMPTY(&g_lvol_store->lvols));
955 	vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL);
956 	CU_ASSERT(g_lvserrno == 0);
957 	CU_ASSERT(g_lvol_store == NULL);
958 
959 	/* Examine successfully */
960 	g_lvserrno = 0;
961 	g_lvolerrno = 0;
962 	g_registered_bdevs = 0;
963 	lvol_already_opened = false;
964 	vbdev_lvs_examine(&g_bdev);
965 	ut_lvs_examine_check(true);
966 	CU_ASSERT(g_registered_bdevs != 0);
967 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&g_lvol_store->lvols));
968 	vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL);
969 	CU_ASSERT(g_lvserrno == 0);
970 }
971 
972 static void
973 ut_lvol_rename(void)
974 {
975 	struct spdk_lvol_store *lvs;
976 	struct spdk_lvol *lvol;
977 	struct spdk_lvol *lvol2;
978 	int sz = 10;
979 	int rc;
980 
981 	/* Lvol store is successfully created */
982 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
983 	CU_ASSERT(rc == 0);
984 	CU_ASSERT(g_lvserrno == 0);
985 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
986 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
987 	lvs = g_lvol_store;
988 
989 	/* Successful lvols create */
990 	g_lvolerrno = -1;
991 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
992 			       NULL);
993 	SPDK_CU_ASSERT_FATAL(rc == 0);
994 	CU_ASSERT(g_lvol != NULL);
995 	CU_ASSERT(g_lvolerrno == 0);
996 	lvol = g_lvol;
997 
998 	g_lvolerrno = -1;
999 	rc = vbdev_lvol_create(lvs, "lvol2", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1000 			       NULL);
1001 	SPDK_CU_ASSERT_FATAL(rc == 0);
1002 	CU_ASSERT(g_lvol != NULL);
1003 	CU_ASSERT(g_lvolerrno == 0);
1004 	lvol2 = g_lvol;
1005 
1006 	/* Successful rename lvol */
1007 	vbdev_lvol_rename(lvol, "new_lvol_name", vbdev_lvol_rename_complete, NULL);
1008 	SPDK_CU_ASSERT_FATAL(g_lvolerrno == 0);
1009 	CU_ASSERT_STRING_EQUAL(lvol->name, "new_lvol_name");
1010 
1011 	/* Renaming lvol with name already existing */
1012 	g_bdev_alias_already_exists = true;
1013 	vbdev_lvol_rename(lvol2, "new_lvol_name", vbdev_lvol_rename_complete, NULL);
1014 	g_bdev_alias_already_exists = false;
1015 	SPDK_CU_ASSERT_FATAL(g_lvolerrno != 0);
1016 	CU_ASSERT_STRING_NOT_EQUAL(lvol2->name, "new_lvol_name");
1017 
1018 	/* Renaming lvol with it's own name */
1019 	vbdev_lvol_rename(lvol, "new_lvol_name", vbdev_lvol_rename_complete, NULL);
1020 	SPDK_CU_ASSERT_FATAL(g_lvolerrno == 0);
1021 	CU_ASSERT_STRING_EQUAL(lvol->name, "new_lvol_name");
1022 
1023 	/* Successful lvols destroy */
1024 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1025 	CU_ASSERT(g_lvol == NULL);
1026 
1027 	vbdev_lvol_destroy(lvol2, lvol_store_op_complete, NULL);
1028 	CU_ASSERT(g_lvol == NULL);
1029 
1030 	/* Destroy lvol store */
1031 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1032 	CU_ASSERT(g_lvserrno == 0);
1033 	CU_ASSERT(g_lvol_store == NULL);
1034 }
1035 
1036 static void
1037 ut_lvol_destroy(void)
1038 {
1039 	struct spdk_lvol_store *lvs;
1040 	struct spdk_lvol *lvol;
1041 	struct spdk_lvol *lvol2;
1042 	int sz = 10;
1043 	int rc;
1044 
1045 	/* Lvol store is successfully created */
1046 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1047 	CU_ASSERT(rc == 0);
1048 	CU_ASSERT(g_lvserrno == 0);
1049 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1050 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1051 	lvs = g_lvol_store;
1052 
1053 	/* Successful lvols create */
1054 	g_lvolerrno = -1;
1055 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1056 			       NULL);
1057 	SPDK_CU_ASSERT_FATAL(rc == 0);
1058 	CU_ASSERT(g_lvol != NULL);
1059 	CU_ASSERT(g_lvolerrno == 0);
1060 	lvol = g_lvol;
1061 
1062 	g_lvolerrno = -1;
1063 	rc = vbdev_lvol_create(lvs, "lvol2", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1064 			       NULL);
1065 	SPDK_CU_ASSERT_FATAL(rc == 0);
1066 	CU_ASSERT(g_lvol != NULL);
1067 	CU_ASSERT(g_lvolerrno == 0);
1068 	lvol2 = g_lvol;
1069 
1070 	/* Unsuccessful lvols destroy */
1071 	g_lvol_deletable = false;
1072 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1073 	CU_ASSERT(g_lvol != NULL);
1074 	CU_ASSERT(g_lvserrno == -EPERM);
1075 
1076 	g_lvol_deletable = true;
1077 	/* Successful lvols destroy */
1078 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1079 	CU_ASSERT(g_lvol == NULL);
1080 	CU_ASSERT(g_lvolerrno == 0);
1081 
1082 	/* Hot remove lvol bdev */
1083 	vbdev_lvol_unregister(lvol2);
1084 
1085 	/* Unload lvol store */
1086 	vbdev_lvs_unload(lvs, lvol_store_op_complete, NULL);
1087 	CU_ASSERT(g_lvserrno == 0);
1088 	CU_ASSERT(g_lvol_store == NULL);
1089 }
1090 
1091 static void
1092 ut_lvol_resize(void)
1093 {
1094 	struct spdk_lvol_store *lvs;
1095 	struct spdk_lvol *lvol;
1096 	int sz = 10;
1097 	int rc = 0;
1098 
1099 	/* Lvol store is successfully created */
1100 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1101 	CU_ASSERT(rc == 0);
1102 	CU_ASSERT(g_lvserrno == 0);
1103 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1104 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1105 	lvs = g_lvol_store;
1106 
1107 	/* Successful lvol create */
1108 	g_lvolerrno = -1;
1109 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1110 			       NULL);
1111 	CU_ASSERT(rc == 0);
1112 	CU_ASSERT(g_lvolerrno == 0);
1113 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1114 	lvol = g_lvol;
1115 
1116 	/* Successful lvol resize */
1117 	g_lvolerrno = -1;
1118 	vbdev_lvol_resize(lvol, 20, vbdev_lvol_resize_complete, NULL);
1119 	CU_ASSERT(g_lvolerrno == 0);
1120 	CU_ASSERT(lvol->bdev->blockcnt == 20 * g_cluster_size / lvol->bdev->blocklen);
1121 
1122 	/* Resize with NULL lvol */
1123 	vbdev_lvol_resize(NULL, 20, vbdev_lvol_resize_complete, NULL);
1124 	CU_ASSERT(g_lvolerrno != 0);
1125 
1126 	/* Successful lvol destroy */
1127 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1128 	CU_ASSERT(g_lvol == NULL);
1129 
1130 	/* Destroy lvol store */
1131 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1132 	CU_ASSERT(g_lvserrno == 0);
1133 	CU_ASSERT(g_lvol_store == NULL);
1134 }
1135 
1136 static void
1137 ut_lvol_set_read_only(void)
1138 {
1139 	struct spdk_lvol_store *lvs;
1140 	struct spdk_lvol *lvol;
1141 	int sz = 10;
1142 	int rc = 0;
1143 
1144 	/* Lvol store is successfully created */
1145 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1146 	CU_ASSERT(rc == 0);
1147 	CU_ASSERT(g_lvserrno == 0);
1148 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1149 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1150 	lvs = g_lvol_store;
1151 
1152 	/* Successful lvol create */
1153 	g_lvolerrno = -1;
1154 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1155 			       NULL);
1156 	CU_ASSERT(rc == 0);
1157 	CU_ASSERT(g_lvolerrno == 0);
1158 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1159 	lvol = g_lvol;
1160 
1161 	/* Successful set lvol as read only */
1162 	g_lvolerrno = -1;
1163 	vbdev_lvol_set_read_only(lvol, vbdev_lvol_set_read_only_complete, NULL);
1164 	CU_ASSERT(g_lvolerrno == 0);
1165 
1166 	/* Successful lvol destroy */
1167 	vbdev_lvol_destroy(lvol, lvol_store_op_complete, NULL);
1168 	CU_ASSERT(g_lvol == NULL);
1169 
1170 	/* Destroy lvol store */
1171 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1172 	CU_ASSERT(g_lvserrno == 0);
1173 	CU_ASSERT(g_lvol_store == NULL);
1174 }
1175 
1176 static void
1177 ut_lvs_unload(void)
1178 {
1179 	int rc = 0;
1180 	int sz = 10;
1181 	struct spdk_lvol_store *lvs;
1182 
1183 	/* Lvol store is successfully created */
1184 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1185 	CU_ASSERT(rc == 0);
1186 	CU_ASSERT(g_lvserrno == 0);
1187 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1188 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1189 
1190 	lvs = g_lvol_store;
1191 	g_lvol_store = NULL;
1192 
1193 	spdk_uuid_generate(&lvs->uuid);
1194 
1195 	/* Successfully create lvol, which should be destroyed with lvs later */
1196 	g_lvolerrno = -1;
1197 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1198 			       NULL);
1199 	CU_ASSERT(rc == 0);
1200 	CU_ASSERT(g_lvolerrno == 0);
1201 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1202 
1203 	/* Unload lvol store */
1204 	vbdev_lvs_unload(lvs, lvol_store_op_complete, NULL);
1205 	CU_ASSERT(g_lvserrno == 0);
1206 	CU_ASSERT(g_lvol_store == NULL);
1207 	CU_ASSERT(g_lvol != NULL);
1208 }
1209 
1210 static void
1211 ut_lvs_init(void)
1212 {
1213 	int rc = 0;
1214 	struct spdk_lvol_store *lvs;
1215 
1216 	/* spdk_lvs_init() fails */
1217 	lvol_store_initialize_fail = true;
1218 
1219 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1220 	CU_ASSERT(rc != 0);
1221 	CU_ASSERT(g_lvserrno == 0);
1222 	CU_ASSERT(g_lvol_store == NULL);
1223 
1224 	lvol_store_initialize_fail = false;
1225 
1226 	/* spdk_lvs_init_cb() fails */
1227 	lvol_store_initialize_cb_fail = true;
1228 
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 	CU_ASSERT(g_lvol_store == NULL);
1233 
1234 	lvol_store_initialize_cb_fail = false;
1235 
1236 	/* Lvol store is successfully created */
1237 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1238 	CU_ASSERT(rc == 0);
1239 	CU_ASSERT(g_lvserrno == 0);
1240 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1241 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1242 
1243 	lvs = g_lvol_store;
1244 	g_lvol_store = NULL;
1245 
1246 	/* Bdev with lvol store already claimed */
1247 	rc = vbdev_lvs_create(&g_bdev, "lvs", 0, lvol_store_op_with_handle_complete, NULL);
1248 	CU_ASSERT(rc != 0);
1249 	CU_ASSERT(g_lvserrno == 0);
1250 	CU_ASSERT(g_lvol_store == NULL);
1251 
1252 	/* Destruct lvol store */
1253 	vbdev_lvs_destruct(lvs, lvol_store_op_complete, NULL);
1254 	CU_ASSERT(g_lvserrno == 0);
1255 	CU_ASSERT(g_lvol_store == NULL);
1256 }
1257 
1258 static void
1259 ut_vbdev_lvol_get_io_channel(void)
1260 {
1261 	struct spdk_io_channel *ch;
1262 
1263 	g_lvol = calloc(1, sizeof(struct spdk_lvol));
1264 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1265 
1266 	ch = vbdev_lvol_get_io_channel(g_lvol);
1267 	CU_ASSERT(ch == g_ch);
1268 
1269 	free(g_lvol);
1270 }
1271 
1272 static void
1273 ut_vbdev_lvol_io_type_supported(void)
1274 {
1275 	struct spdk_lvol *lvol;
1276 	bool ret;
1277 
1278 	lvol = calloc(1, sizeof(struct spdk_lvol));
1279 	SPDK_CU_ASSERT_FATAL(lvol != NULL);
1280 
1281 	g_blob_is_read_only = false;
1282 
1283 	/* Supported types */
1284 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_READ);
1285 	CU_ASSERT(ret == true);
1286 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE);
1287 	CU_ASSERT(ret == true);
1288 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_RESET);
1289 	CU_ASSERT(ret == true);
1290 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_UNMAP);
1291 	CU_ASSERT(ret == true);
1292 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE_ZEROES);
1293 	CU_ASSERT(ret == true);
1294 
1295 	/* Unsupported types */
1296 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_FLUSH);
1297 	CU_ASSERT(ret == false);
1298 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_ADMIN);
1299 	CU_ASSERT(ret == false);
1300 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_IO);
1301 	CU_ASSERT(ret == false);
1302 
1303 	g_blob_is_read_only = true;
1304 
1305 	/* Supported types */
1306 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_READ);
1307 	CU_ASSERT(ret == true);
1308 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_RESET);
1309 	CU_ASSERT(ret == true);
1310 
1311 	/* Unsupported types */
1312 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE);
1313 	CU_ASSERT(ret == false);
1314 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_UNMAP);
1315 	CU_ASSERT(ret == false);
1316 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_WRITE_ZEROES);
1317 	CU_ASSERT(ret == false);
1318 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_FLUSH);
1319 	CU_ASSERT(ret == false);
1320 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_ADMIN);
1321 	CU_ASSERT(ret == false);
1322 	ret = vbdev_lvol_io_type_supported(lvol, SPDK_BDEV_IO_TYPE_NVME_IO);
1323 	CU_ASSERT(ret == false);
1324 
1325 	free(lvol);
1326 }
1327 
1328 static void
1329 ut_lvol_read_write(void)
1330 {
1331 	g_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct lvol_task));
1332 	SPDK_CU_ASSERT_FATAL(g_io != NULL);
1333 	g_base_bdev = calloc(1, sizeof(struct spdk_bdev));
1334 	SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL);
1335 	g_lvol = calloc(1, sizeof(struct spdk_lvol));
1336 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1337 
1338 	g_task = (struct lvol_task *)g_io->driver_ctx;
1339 	g_io->bdev = g_base_bdev;
1340 	g_io->bdev->ctxt = g_lvol;
1341 	g_io->u.bdev.offset_blocks = 20;
1342 	g_io->u.bdev.num_blocks = 20;
1343 
1344 	lvol_read(g_ch, g_io);
1345 	CU_ASSERT(g_task->status == SPDK_BDEV_IO_STATUS_SUCCESS);
1346 
1347 	lvol_write(g_lvol, g_ch, g_io);
1348 	CU_ASSERT(g_task->status == SPDK_BDEV_IO_STATUS_SUCCESS);
1349 
1350 	free(g_io);
1351 	free(g_base_bdev);
1352 	free(g_lvol);
1353 }
1354 
1355 static void
1356 ut_vbdev_lvol_submit_request(void)
1357 {
1358 	struct spdk_lvol request_lvol = {};
1359 	g_io = calloc(1, sizeof(struct spdk_bdev_io) + sizeof(struct lvol_task));
1360 	SPDK_CU_ASSERT_FATAL(g_io != NULL);
1361 	g_base_bdev = calloc(1, sizeof(struct spdk_bdev));
1362 	SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL);
1363 	g_task = (struct lvol_task *)g_io->driver_ctx;
1364 	g_io->bdev = g_base_bdev;
1365 
1366 	g_io->type = SPDK_BDEV_IO_TYPE_READ;
1367 	g_base_bdev->ctxt = &request_lvol;
1368 	vbdev_lvol_submit_request(g_ch, g_io);
1369 
1370 	free(g_io);
1371 	free(g_base_bdev);
1372 }
1373 
1374 static void
1375 ut_lvs_rename(void)
1376 {
1377 	int rc = 0;
1378 	int sz = 10;
1379 	struct spdk_lvol_store *lvs;
1380 
1381 	/* Lvol store is successfully created */
1382 	rc = vbdev_lvs_create(&g_bdev, "old_lvs_name", 0, lvol_store_op_with_handle_complete, NULL);
1383 	CU_ASSERT(rc == 0);
1384 	CU_ASSERT(g_lvserrno == 0);
1385 	SPDK_CU_ASSERT_FATAL(g_lvol_store != NULL);
1386 	CU_ASSERT(g_lvol_store->bs_dev != NULL);
1387 
1388 	lvs = g_lvol_store;
1389 	g_lvol_store = NULL;
1390 
1391 	g_base_bdev = calloc(1, sizeof(*g_base_bdev));
1392 	SPDK_CU_ASSERT_FATAL(g_base_bdev != NULL);
1393 
1394 	/* Successfully create lvol, which should be destroyed with lvs later */
1395 	g_lvolerrno = -1;
1396 	rc = vbdev_lvol_create(lvs, "lvol", sz, false, LVOL_CLEAR_WITH_DEFAULT, vbdev_lvol_create_complete,
1397 			       NULL);
1398 	CU_ASSERT(rc == 0);
1399 	CU_ASSERT(g_lvolerrno == 0);
1400 	SPDK_CU_ASSERT_FATAL(g_lvol != NULL);
1401 
1402 	/* Trying to rename lvs with lvols created */
1403 	vbdev_lvs_rename(lvs, "new_lvs_name", lvol_store_op_complete, NULL);
1404 	CU_ASSERT(g_lvserrno == 0);
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 
1408 	/* Trying to rename lvs with name already used by another lvs */
1409 	/* This is a bdev_lvol test, so g_lvs_with_name_already_exists simulates
1410 	 * existing lvs with name 'another_new_lvs_name' and this name in fact is not compared */
1411 	g_lvs_with_name_already_exists = true;
1412 	vbdev_lvs_rename(lvs, "another_new_lvs_name", lvol_store_op_complete, NULL);
1413 	CU_ASSERT(g_lvserrno == -EEXIST);
1414 	CU_ASSERT_STRING_EQUAL(lvs->name, "new_lvs_name");
1415 	CU_ASSERT_STRING_EQUAL(TAILQ_FIRST(&g_lvol->bdev->aliases)->alias, "new_lvs_name/lvol");
1416 	g_lvs_with_name_already_exists = false;
1417 
1418 	/* Unload lvol store */
1419 	g_lvol_store = lvs;
1420 	vbdev_lvs_destruct(g_lvol_store, lvol_store_op_complete, NULL);
1421 	CU_ASSERT(g_lvserrno == 0);
1422 	CU_ASSERT(g_lvol_store == NULL);
1423 
1424 	free(g_base_bdev->name);
1425 	free(g_base_bdev);
1426 }
1427 
1428 int main(int argc, char **argv)
1429 {
1430 	CU_pSuite	suite = NULL;
1431 	unsigned int	num_failures;
1432 
1433 	if (CU_initialize_registry() != CUE_SUCCESS) {
1434 		return CU_get_error();
1435 	}
1436 
1437 	suite = CU_add_suite("lvol", NULL, NULL);
1438 	if (suite == NULL) {
1439 		CU_cleanup_registry();
1440 		return CU_get_error();
1441 	}
1442 
1443 	if (
1444 		CU_add_test(suite, "ut_lvs_init", ut_lvs_init) == NULL ||
1445 		CU_add_test(suite, "ut_lvol_init", ut_lvol_init) == NULL ||
1446 		CU_add_test(suite, "ut_lvol_snapshot", ut_lvol_snapshot) == NULL ||
1447 		CU_add_test(suite, "ut_lvol_clone", ut_lvol_clone) == NULL ||
1448 		CU_add_test(suite, "ut_lvs_destroy", ut_lvs_destroy) == NULL ||
1449 		CU_add_test(suite, "ut_lvs_unload", ut_lvs_unload) == NULL ||
1450 		CU_add_test(suite, "ut_lvol_resize", ut_lvol_resize) == NULL ||
1451 		CU_add_test(suite, "ut_lvol_set_read_only", ut_lvol_set_read_only) == NULL ||
1452 		CU_add_test(suite, "lvol_hotremove", ut_lvol_hotremove) == NULL ||
1453 		CU_add_test(suite, "ut_vbdev_lvol_get_io_channel", ut_vbdev_lvol_get_io_channel) == NULL ||
1454 		CU_add_test(suite, "ut_vbdev_lvol_io_type_supported", ut_vbdev_lvol_io_type_supported) == NULL ||
1455 		CU_add_test(suite, "ut_lvol_read_write", ut_lvol_read_write) == NULL ||
1456 		CU_add_test(suite, "ut_vbdev_lvol_submit_request", ut_vbdev_lvol_submit_request) == NULL ||
1457 		CU_add_test(suite, "lvol_examine", ut_lvol_examine) == NULL ||
1458 		CU_add_test(suite, "ut_lvol_rename", ut_lvol_rename) == NULL ||
1459 		CU_add_test(suite, "ut_lvol_destroy", ut_lvol_destroy) == NULL ||
1460 		CU_add_test(suite, "ut_lvs_rename", ut_lvs_rename) == NULL
1461 	) {
1462 		CU_cleanup_registry();
1463 		return CU_get_error();
1464 	}
1465 
1466 	CU_basic_set_mode(CU_BRM_VERBOSE);
1467 	CU_basic_run_tests();
1468 	num_failures = CU_get_number_of_failures();
1469 	CU_cleanup_registry();
1470 	return num_failures;
1471 }
1472