xref: /spdk/test/unit/lib/bdev/mt/bdev.c/bdev_ut.c (revision a4a497d5b0f1dc7f63ebf52bc273f46f24cd9dcf)
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 
36 #include "common/lib/test_env.c"
37 #include "common/lib/ut_multithread.c"
38 #include "unit/lib/json_mock.c"
39 
40 /* HACK: disable VTune integration so the unit test doesn't need VTune headers and libs to build */
41 #undef SPDK_CONFIG_VTUNE
42 
43 #include "bdev/bdev.c"
44 
45 #define BDEV_UT_NUM_THREADS 3
46 
47 DEFINE_STUB_V(spdk_scsi_nvme_translate, (const struct spdk_bdev_io *bdev_io,
48 		int *sc, int *sk, int *asc, int *ascq));
49 
50 /* Return NULL to test hardcoded defaults. */
51 struct spdk_conf_section *
52 spdk_conf_find_section(struct spdk_conf *cp, const char *name)
53 {
54 	return NULL;
55 }
56 
57 /* Return NULL to test hardcoded defaults. */
58 char *
59 spdk_conf_section_get_nmval(struct spdk_conf_section *sp, const char *key, int idx1, int idx2)
60 {
61 	return NULL;
62 }
63 
64 struct ut_bdev {
65 	struct spdk_bdev	bdev;
66 	void			*io_target;
67 };
68 
69 struct ut_bdev_channel {
70 	TAILQ_HEAD(, spdk_bdev_io)	outstanding_io;
71 	uint32_t			outstanding_cnt;
72 	uint32_t			avail_cnt;
73 };
74 
75 int g_io_device;
76 struct ut_bdev g_bdev;
77 struct spdk_bdev_desc *g_desc;
78 bool g_teardown_done = false;
79 bool g_get_io_channel = true;
80 bool g_create_ch = true;
81 bool g_init_complete_called = false;
82 
83 static int
84 stub_create_ch(void *io_device, void *ctx_buf)
85 {
86 	struct ut_bdev_channel *ch = ctx_buf;
87 
88 	if (g_create_ch == false) {
89 		return -1;
90 	}
91 
92 	TAILQ_INIT(&ch->outstanding_io);
93 	ch->outstanding_cnt = 0;
94 	/*
95 	 * When avail gets to 0, the submit_request function will return ENOMEM.
96 	 *  Most tests to not want ENOMEM to occur, so by default set this to a
97 	 *  big value that won't get hit.  The ENOMEM tests can then override this
98 	 *  value to something much smaller to induce ENOMEM conditions.
99 	 */
100 	ch->avail_cnt = 2048;
101 	return 0;
102 }
103 
104 static void
105 stub_destroy_ch(void *io_device, void *ctx_buf)
106 {
107 }
108 
109 static struct spdk_io_channel *
110 stub_get_io_channel(void *ctx)
111 {
112 	struct ut_bdev *ut_bdev = ctx;
113 
114 	if (g_get_io_channel == true) {
115 		return spdk_get_io_channel(ut_bdev->io_target);
116 	} else {
117 		return NULL;
118 	}
119 }
120 
121 static int
122 stub_destruct(void *ctx)
123 {
124 	return 0;
125 }
126 
127 static void
128 stub_submit_request(struct spdk_io_channel *_ch, struct spdk_bdev_io *bdev_io)
129 {
130 	struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
131 
132 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) {
133 		struct spdk_bdev_io *io;
134 
135 		while (!TAILQ_EMPTY(&ch->outstanding_io)) {
136 			io = TAILQ_FIRST(&ch->outstanding_io);
137 			TAILQ_REMOVE(&ch->outstanding_io, io, module_link);
138 			ch->outstanding_cnt--;
139 			spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_FAILED);
140 			ch->avail_cnt++;
141 		}
142 	}
143 
144 	if (ch->avail_cnt > 0) {
145 		TAILQ_INSERT_TAIL(&ch->outstanding_io, bdev_io, module_link);
146 		ch->outstanding_cnt++;
147 		ch->avail_cnt--;
148 	} else {
149 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
150 	}
151 }
152 
153 static uint32_t
154 stub_complete_io(void *io_target, uint32_t num_to_complete)
155 {
156 	struct spdk_io_channel *_ch = spdk_get_io_channel(io_target);
157 	struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
158 	struct spdk_bdev_io *io;
159 	bool complete_all = (num_to_complete == 0);
160 	uint32_t num_completed = 0;
161 
162 	while (complete_all || num_completed < num_to_complete) {
163 		if (TAILQ_EMPTY(&ch->outstanding_io)) {
164 			break;
165 		}
166 		io = TAILQ_FIRST(&ch->outstanding_io);
167 		TAILQ_REMOVE(&ch->outstanding_io, io, module_link);
168 		ch->outstanding_cnt--;
169 		spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_SUCCESS);
170 		ch->avail_cnt++;
171 		num_completed++;
172 	}
173 
174 	spdk_put_io_channel(_ch);
175 	return num_completed;
176 }
177 
178 static struct spdk_bdev_fn_table fn_table = {
179 	.get_io_channel =	stub_get_io_channel,
180 	.destruct =		stub_destruct,
181 	.submit_request =	stub_submit_request,
182 };
183 
184 static int
185 module_init(void)
186 {
187 	return 0;
188 }
189 
190 static void
191 module_fini(void)
192 {
193 }
194 
195 static void
196 init_complete(void)
197 {
198 	g_init_complete_called = true;
199 }
200 
201 struct spdk_bdev_module bdev_ut_if = {
202 	.name = "bdev_ut",
203 	.module_init = module_init,
204 	.module_fini = module_fini,
205 	.init_complete = init_complete,
206 };
207 
208 SPDK_BDEV_MODULE_REGISTER(&bdev_ut_if)
209 
210 static void
211 register_bdev(struct ut_bdev *ut_bdev, char *name, void *io_target)
212 {
213 	memset(ut_bdev, 0, sizeof(*ut_bdev));
214 
215 	ut_bdev->io_target = io_target;
216 	ut_bdev->bdev.ctxt = ut_bdev;
217 	ut_bdev->bdev.name = name;
218 	ut_bdev->bdev.fn_table = &fn_table;
219 	ut_bdev->bdev.module = &bdev_ut_if;
220 	ut_bdev->bdev.blocklen = 4096;
221 	ut_bdev->bdev.blockcnt = 1024;
222 
223 	spdk_bdev_register(&ut_bdev->bdev);
224 }
225 
226 static void
227 unregister_bdev(struct ut_bdev *ut_bdev)
228 {
229 	/* Handle any deferred messages. */
230 	poll_threads();
231 	spdk_bdev_unregister(&ut_bdev->bdev, NULL, NULL);
232 }
233 
234 static void
235 bdev_init_cb(void *done, int rc)
236 {
237 	CU_ASSERT(rc == 0);
238 	*(bool *)done = true;
239 }
240 
241 static void
242 setup_test(void)
243 {
244 	bool done = false;
245 
246 	allocate_threads(BDEV_UT_NUM_THREADS);
247 	spdk_bdev_initialize(bdev_init_cb, &done);
248 	spdk_io_device_register(&g_io_device, stub_create_ch, stub_destroy_ch,
249 				sizeof(struct ut_bdev_channel));
250 	register_bdev(&g_bdev, "ut_bdev", &g_io_device);
251 	spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc);
252 }
253 
254 static void
255 finish_cb(void *cb_arg)
256 {
257 	g_teardown_done = true;
258 }
259 
260 static void
261 teardown_test(void)
262 {
263 	g_teardown_done = false;
264 	spdk_bdev_close(g_desc);
265 	g_desc = NULL;
266 	unregister_bdev(&g_bdev);
267 	spdk_io_device_unregister(&g_io_device, NULL);
268 	spdk_bdev_finish(finish_cb, NULL);
269 	poll_threads();
270 	memset(&g_bdev, 0, sizeof(g_bdev));
271 	CU_ASSERT(g_teardown_done == true);
272 	g_teardown_done = false;
273 	free_threads();
274 }
275 
276 static uint32_t
277 bdev_io_tailq_cnt(bdev_io_tailq_t *tailq)
278 {
279 	struct spdk_bdev_io *io;
280 	uint32_t cnt = 0;
281 
282 	TAILQ_FOREACH(io, tailq, link) {
283 		cnt++;
284 	}
285 
286 	return cnt;
287 }
288 
289 static void
290 basic(void)
291 {
292 	g_init_complete_called = false;
293 	setup_test();
294 	CU_ASSERT(g_init_complete_called == true);
295 
296 	set_thread(0);
297 
298 	g_get_io_channel = false;
299 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
300 	CU_ASSERT(g_ut_threads[0].ch == NULL);
301 
302 	g_get_io_channel = true;
303 	g_create_ch = false;
304 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
305 	CU_ASSERT(g_ut_threads[0].ch == NULL);
306 
307 	g_get_io_channel = true;
308 	g_create_ch = true;
309 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
310 	CU_ASSERT(g_ut_threads[0].ch != NULL);
311 	spdk_put_io_channel(g_ut_threads[0].ch);
312 
313 	teardown_test();
314 }
315 
316 static int
317 poller_run_done(void *ctx)
318 {
319 	bool	*poller_run = ctx;
320 
321 	*poller_run = true;
322 
323 	return -1;
324 }
325 
326 static int
327 poller_run_times_done(void *ctx)
328 {
329 	int	*poller_run_times = ctx;
330 
331 	(*poller_run_times)++;
332 
333 	return -1;
334 }
335 
336 static void
337 basic_poller(void)
338 {
339 	struct spdk_poller	*poller = NULL;
340 	bool			poller_run = false;
341 	int			poller_run_times = 0;
342 
343 	setup_test();
344 
345 	set_thread(0);
346 	reset_time();
347 	/* Register a poller with no-wait time and test execution */
348 	poller = spdk_poller_register(poller_run_done, &poller_run, 0);
349 	CU_ASSERT(poller != NULL);
350 
351 	poll_threads();
352 	CU_ASSERT(poller_run == true);
353 
354 	spdk_poller_unregister(&poller);
355 	CU_ASSERT(poller == NULL);
356 
357 	/* Register a poller with 1000us wait time and test single execution */
358 	poller_run = false;
359 	poller = spdk_poller_register(poller_run_done, &poller_run, 1000);
360 	CU_ASSERT(poller != NULL);
361 
362 	poll_threads();
363 	CU_ASSERT(poller_run == false);
364 
365 	increment_time(1000);
366 	poll_threads();
367 	CU_ASSERT(poller_run == true);
368 
369 	reset_time();
370 	poller_run = false;
371 	poll_threads();
372 	CU_ASSERT(poller_run == false);
373 
374 	increment_time(1000);
375 	poll_threads();
376 	CU_ASSERT(poller_run == true);
377 
378 	spdk_poller_unregister(&poller);
379 	CU_ASSERT(poller == NULL);
380 
381 	reset_time();
382 	/* Register a poller with 1000us wait time and test multiple execution */
383 	poller = spdk_poller_register(poller_run_times_done, &poller_run_times, 1000);
384 	CU_ASSERT(poller != NULL);
385 
386 	poll_threads();
387 	CU_ASSERT(poller_run_times == 0);
388 
389 	increment_time(1000);
390 	poll_threads();
391 	CU_ASSERT(poller_run_times == 1);
392 
393 	poller_run_times = 0;
394 	increment_time(2000);
395 	poll_threads();
396 	CU_ASSERT(poller_run_times == 2);
397 
398 	spdk_poller_unregister(&poller);
399 	CU_ASSERT(poller == NULL);
400 
401 	teardown_test();
402 }
403 
404 static void
405 reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
406 {
407 	bool *done = cb_arg;
408 
409 	CU_ASSERT(success == true);
410 	*done = true;
411 	spdk_bdev_free_io(bdev_io);
412 }
413 
414 static void
415 put_channel_during_reset(void)
416 {
417 	struct spdk_io_channel *io_ch;
418 	bool done = false;
419 
420 	setup_test();
421 
422 	set_thread(0);
423 	io_ch = spdk_bdev_get_io_channel(g_desc);
424 	CU_ASSERT(io_ch != NULL);
425 
426 	/*
427 	 * Start a reset, but then put the I/O channel before
428 	 *  the deferred messages for the reset get a chance to
429 	 *  execute.
430 	 */
431 	spdk_bdev_reset(g_desc, io_ch, reset_done, &done);
432 	spdk_put_io_channel(io_ch);
433 	poll_threads();
434 	stub_complete_io(g_bdev.io_target, 0);
435 
436 	teardown_test();
437 }
438 
439 static void
440 aborted_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
441 {
442 	enum spdk_bdev_io_status *status = cb_arg;
443 
444 	*status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
445 	spdk_bdev_free_io(bdev_io);
446 }
447 
448 static void
449 aborted_reset(void)
450 {
451 	struct spdk_io_channel *io_ch[2];
452 	enum spdk_bdev_io_status status1, status2;
453 
454 	setup_test();
455 
456 	set_thread(0);
457 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
458 	CU_ASSERT(io_ch[0] != NULL);
459 	spdk_bdev_reset(g_desc, io_ch[0], aborted_reset_done, &status1);
460 	poll_threads();
461 	CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL);
462 
463 	/*
464 	 * First reset has been submitted on ch0.  Now submit a second
465 	 *  reset on ch1 which will get queued since there is already a
466 	 *  reset in progress.
467 	 */
468 	set_thread(1);
469 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
470 	CU_ASSERT(io_ch[1] != NULL);
471 	spdk_bdev_reset(g_desc, io_ch[1], aborted_reset_done, &status2);
472 	poll_threads();
473 	CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL);
474 
475 	/*
476 	 * Now destroy ch1.  This will abort the queued reset.  Check that
477 	 *  the second reset was completed with failed status.  Also check
478 	 *  that bdev->reset_in_progress != NULL, since the original reset
479 	 *  has not been completed yet.  This ensures that the bdev code is
480 	 *  correctly noticing that the failed reset is *not* the one that
481 	 *  had been submitted to the bdev module.
482 	 */
483 	set_thread(1);
484 	spdk_put_io_channel(io_ch[1]);
485 	poll_threads();
486 	CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_FAILED);
487 	CU_ASSERT(g_bdev.bdev.reset_in_progress != NULL);
488 
489 	/*
490 	 * Now complete the first reset, verify that it completed with SUCCESS
491 	 *  status and that bdev->reset_in_progress is also set back to NULL.
492 	 */
493 	set_thread(0);
494 	spdk_put_io_channel(io_ch[0]);
495 	stub_complete_io(g_bdev.io_target, 0);
496 	poll_threads();
497 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
498 	CU_ASSERT(g_bdev.bdev.reset_in_progress == NULL);
499 
500 	teardown_test();
501 }
502 
503 static void
504 io_during_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
505 {
506 	enum spdk_bdev_io_status *status = cb_arg;
507 
508 	*status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
509 	spdk_bdev_free_io(bdev_io);
510 }
511 
512 static void
513 io_during_reset(void)
514 {
515 	struct spdk_io_channel *io_ch[2];
516 	struct spdk_bdev_channel *bdev_ch[2];
517 	enum spdk_bdev_io_status status0, status1, status_reset;
518 	int rc;
519 
520 	setup_test();
521 
522 	/*
523 	 * First test normal case - submit an I/O on each of two channels (with no resets)
524 	 *  and verify they complete successfully.
525 	 */
526 	set_thread(0);
527 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
528 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
529 	CU_ASSERT(bdev_ch[0]->flags == 0);
530 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
531 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
532 	CU_ASSERT(rc == 0);
533 
534 	set_thread(1);
535 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
536 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
537 	CU_ASSERT(bdev_ch[1]->flags == 0);
538 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
539 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
540 	CU_ASSERT(rc == 0);
541 
542 	poll_threads();
543 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
544 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
545 
546 	set_thread(0);
547 	stub_complete_io(g_bdev.io_target, 0);
548 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
549 
550 	set_thread(1);
551 	stub_complete_io(g_bdev.io_target, 0);
552 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
553 
554 	/*
555 	 * Now submit a reset, and leave it pending while we submit I/O on two different
556 	 *  channels.  These I/O should be failed by the bdev layer since the reset is in
557 	 *  progress.
558 	 */
559 	set_thread(0);
560 	status_reset = SPDK_BDEV_IO_STATUS_PENDING;
561 	rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &status_reset);
562 	CU_ASSERT(rc == 0);
563 
564 	CU_ASSERT(bdev_ch[0]->flags == 0);
565 	CU_ASSERT(bdev_ch[1]->flags == 0);
566 	poll_threads();
567 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS);
568 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS);
569 
570 	set_thread(0);
571 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
572 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
573 	CU_ASSERT(rc == 0);
574 
575 	set_thread(1);
576 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
577 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
578 	CU_ASSERT(rc == 0);
579 
580 	/*
581 	 * A reset is in progress so these read I/O should complete with failure.  Note that we
582 	 *  need to poll_threads() since I/O completed inline have their completion deferred.
583 	 */
584 	poll_threads();
585 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING);
586 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED);
587 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED);
588 
589 	/*
590 	 * Complete the reset
591 	 */
592 	set_thread(0);
593 	stub_complete_io(g_bdev.io_target, 0);
594 
595 	/*
596 	 * Only poll thread 0. We should not get a completion.
597 	 */
598 	poll_thread(0);
599 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING);
600 
601 	/*
602 	 * Poll both thread 0 and 1 so the messages can propagate and we
603 	 * get a completion.
604 	 */
605 	poll_threads();
606 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS);
607 
608 	spdk_put_io_channel(io_ch[0]);
609 	set_thread(1);
610 	spdk_put_io_channel(io_ch[1]);
611 	poll_threads();
612 
613 	teardown_test();
614 }
615 
616 static void
617 basic_qos(void)
618 {
619 	struct spdk_io_channel *io_ch[3];
620 	struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch;
621 	struct spdk_bdev *bdev;
622 	enum spdk_bdev_io_status status;
623 	struct spdk_bdev_module_channel *module_ch;
624 	int rc;
625 
626 	setup_test();
627 
628 	/*
629 	 * First test normal case - submit an I/O on the channel (QoS not enabled)
630 	 *  and verify it completes successfully.
631 	 */
632 	set_thread(0);
633 	g_get_io_channel = false;
634 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
635 	CU_ASSERT(io_ch[0] == NULL);
636 	g_get_io_channel = true;
637 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
638 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
639 	status = SPDK_BDEV_IO_STATUS_PENDING;
640 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status);
641 	CU_ASSERT(rc == 0);
642 	CU_ASSERT(bdev_ch[0]->flags == 0);
643 
644 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
645 
646 	set_thread(0);
647 	stub_complete_io(g_bdev.io_target, 0);
648 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS);
649 
650 	poll_threads();
651 
652 	set_thread(1);
653 	bdev = &g_bdev.bdev;
654 	bdev->ios_per_sec = 2000;
655 	g_get_io_channel = false;
656 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
657 	CU_ASSERT(io_ch[1] == NULL);
658 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
659 	qos_bdev_ch = bdev->qos_channel;
660 	CU_ASSERT(qos_bdev_ch == NULL);
661 	g_get_io_channel = true;
662 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
663 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
664 	qos_bdev_ch = bdev->qos_channel;
665 	CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED);
666 	CU_ASSERT(qos_bdev_ch != NULL);
667 	module_ch = qos_bdev_ch->module_ch;
668 	CU_ASSERT(module_ch->io_outstanding == 0);
669 	CU_ASSERT(g_ut_threads[1].thread == bdev->qos_thread);
670 
671 	/*
672 	 * Now sending one I/O on first channel
673 	 */
674 	set_thread(0);
675 	status = SPDK_BDEV_IO_STATUS_PENDING;
676 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status);
677 	CU_ASSERT(rc == 0);
678 
679 	poll_threads();
680 	CU_ASSERT(module_ch->io_outstanding == 1);
681 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
682 
683 	/*
684 	 * IO is operated on thread_id(1) via the QoS thread
685 	 */
686 	set_thread(1);
687 	stub_complete_io(g_bdev.io_target, 1);
688 
689 	poll_threads();
690 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS);
691 
692 	/*
693 	 * QoS thread is on thread 1. Put I/O channel on thread 1 first
694 	 * to trigger an async destruction of QoS bdev channel.
695 	 */
696 	set_thread(1);
697 	spdk_put_io_channel(io_ch[0]);
698 	set_thread(0);
699 	spdk_put_io_channel(io_ch[1]);
700 
701 	/*
702 	 * Handle the messages on thread 1 first so that the QoS bdev
703 	 * channel destroy message from thread 0 handling will be active
704 	 * there.
705 	 */
706 	poll_thread(1);
707 	poll_thread(0);
708 
709 	/*
710 	 * Create a new I/O channel when the async destruction of QoS
711 	 * bdev channel is on going. The expected result is the QoS bdev
712 	 * channel will be properly setup again.
713 	 */
714 	set_thread(2);
715 	io_ch[2] = spdk_bdev_get_io_channel(g_desc);
716 	bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]);
717 
718 	poll_threads();
719 
720 	qos_bdev_ch = bdev->qos_channel;
721 	CU_ASSERT(qos_bdev_ch->flags == BDEV_CH_QOS_ENABLED);
722 	CU_ASSERT(qos_bdev_ch != NULL);
723 	module_ch = qos_bdev_ch->module_ch;
724 	CU_ASSERT(module_ch->io_outstanding == 0);
725 	CU_ASSERT(g_ut_threads[2].thread == bdev->qos_thread);
726 
727 	/*
728 	 * Destroy the last I/O channel so that the QoS bdev channel
729 	 * will be destroyed.
730 	 */
731 	set_thread(2);
732 	spdk_put_io_channel(io_ch[2]);
733 
734 	poll_threads();
735 
736 	teardown_test();
737 }
738 
739 static void
740 io_during_qos(void)
741 {
742 	struct spdk_io_channel *io_ch[3];
743 	struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch;
744 	struct spdk_bdev *bdev;
745 	enum spdk_bdev_io_status status0, status1;
746 	struct spdk_bdev_module_channel *module_ch;
747 	int rc;
748 
749 	setup_test();
750 
751 	/*
752 	 * First test normal case - submit an I/O on each of two channels (QoS not enabled)
753 	 *  and verify they complete successfully.
754 	 */
755 	set_thread(0);
756 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
757 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
758 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
759 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
760 	CU_ASSERT(rc == 0);
761 	CU_ASSERT(bdev_ch[0]->flags == 0);
762 
763 	set_thread(1);
764 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
765 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
766 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
767 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
768 	CU_ASSERT(rc == 0);
769 	CU_ASSERT(bdev_ch[1]->flags == 0);
770 
771 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
772 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
773 
774 	set_thread(0);
775 	stub_complete_io(g_bdev.io_target, 0);
776 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
777 
778 	set_thread(1);
779 	stub_complete_io(g_bdev.io_target, 0);
780 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
781 
782 	poll_threads();
783 
784 	set_thread(2);
785 	bdev = &g_bdev.bdev;
786 	/*
787 	 * 10 IOs allowed per millisecond
788 	 */
789 	bdev->ios_per_sec = 10000;
790 	io_ch[2] = spdk_bdev_get_io_channel(g_desc);
791 	bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]);
792 	qos_bdev_ch = bdev->qos_channel;
793 	CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED);
794 	CU_ASSERT(qos_bdev_ch != NULL);
795 	module_ch = qos_bdev_ch->module_ch;
796 	CU_ASSERT(module_ch->io_outstanding == 0);
797 
798 	/*
799 	 * Now sending some I/Os on different channels when QoS has been enabled
800 	 */
801 	set_thread(0);
802 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
803 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
804 	CU_ASSERT(rc == 0);
805 
806 	set_thread(1);
807 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
808 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
809 	CU_ASSERT(rc == 0);
810 
811 	poll_threads();
812 	CU_ASSERT(module_ch->io_outstanding == 2);
813 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
814 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
815 
816 	/*
817 	 * IOs are operated on thread_id(2) via the QoS thread
818 	 */
819 	set_thread(2);
820 	stub_complete_io(g_bdev.io_target, 2);
821 
822 	poll_threads();
823 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
824 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
825 
826 	set_thread(0);
827 	spdk_put_io_channel(io_ch[0]);
828 	set_thread(1);
829 	spdk_put_io_channel(io_ch[1]);
830 	set_thread(2);
831 	spdk_put_io_channel(io_ch[2]);
832 
833 	poll_threads();
834 
835 	teardown_test();
836 }
837 
838 static void
839 io_during_qos_queue(void)
840 {
841 	struct spdk_io_channel *io_ch[3];
842 	struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch;
843 	struct spdk_bdev *bdev;
844 	enum spdk_bdev_io_status status0, status1;
845 	struct spdk_bdev_module_channel *module_ch;
846 	int rc;
847 
848 	setup_test();
849 	reset_time();
850 
851 	/*
852 	 * First test normal case - submit an I/O on each of two channels (QoS not enabled)
853 	 *  and verify they complete successfully.
854 	 */
855 	set_thread(0);
856 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
857 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
858 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
859 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
860 	CU_ASSERT(rc == 0);
861 	CU_ASSERT(bdev_ch[0]->flags == 0);
862 
863 	set_thread(1);
864 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
865 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
866 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
867 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
868 	CU_ASSERT(rc == 0);
869 	CU_ASSERT(bdev_ch[1]->flags == 0);
870 
871 	poll_threads();
872 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
873 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
874 
875 	set_thread(0);
876 	stub_complete_io(g_bdev.io_target, 0);
877 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
878 
879 	set_thread(1);
880 	stub_complete_io(g_bdev.io_target, 0);
881 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
882 
883 	poll_threads();
884 
885 	set_thread(2);
886 	bdev = bdev_ch[0]->bdev;
887 	/*
888 	 * Only 1 IO allowed per millisecond. More IOs will be queued.
889 	 */
890 	bdev->ios_per_sec = 1000;
891 	io_ch[2] = spdk_bdev_get_io_channel(g_desc);
892 	bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]);
893 	qos_bdev_ch = bdev->qos_channel;
894 	CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED);
895 	CU_ASSERT(qos_bdev_ch != NULL);
896 	module_ch = qos_bdev_ch->module_ch;
897 	CU_ASSERT(module_ch->io_outstanding == 0);
898 
899 	/*
900 	 * Now sending some I/Os on different channels when QoS has been enabled
901 	 */
902 	set_thread(0);
903 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
904 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
905 	CU_ASSERT(rc == 0);
906 
907 	set_thread(1);
908 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
909 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
910 	CU_ASSERT(rc == 0);
911 
912 	/*
913 	 * Poll the QoS thread to send the allowed I/O down
914 	 */
915 	poll_threads();
916 	CU_ASSERT(module_ch->io_outstanding == 1);
917 	CU_ASSERT(bdev_io_tailq_cnt(&qos_bdev_ch->qos_io) == 1);
918 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
919 
920 	/*
921 	 * Increase the time and poll the QoS thread to run the periodical poller
922 	 */
923 	increment_time(1000);
924 	poll_threads();
925 	CU_ASSERT(module_ch->io_outstanding == 2);
926 	CU_ASSERT(bdev_io_tailq_cnt(&qos_bdev_ch->qos_io) == 0);
927 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
928 
929 	/*
930 	 * IOs are handled on the thread(2) as the master thread
931 	 */
932 	set_thread(2);
933 	stub_complete_io(g_bdev.io_target, 0);
934 	spdk_put_io_channel(io_ch[0]);
935 	spdk_put_io_channel(io_ch[1]);
936 	spdk_put_io_channel(io_ch[2]);
937 
938 	poll_threads();
939 
940 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
941 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
942 
943 	teardown_test();
944 }
945 
946 static void
947 io_during_qos_reset(void)
948 {
949 	struct spdk_io_channel *io_ch[3];
950 	struct spdk_bdev_channel *bdev_ch[3], *qos_bdev_ch;
951 	struct spdk_bdev *bdev;
952 	enum spdk_bdev_io_status status0, status1, status_reset;
953 	struct spdk_bdev_module_channel *module_ch;
954 	int rc;
955 
956 	setup_test();
957 
958 	/*
959 	 * First test normal case - submit an I/O on each of two channels (QoS disabled and no reset)
960 	 *  and verify they complete successfully.
961 	 */
962 	set_thread(0);
963 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
964 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
965 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
966 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
967 	CU_ASSERT(rc == 0);
968 	CU_ASSERT(bdev_ch[0]->flags == 0);
969 
970 	set_thread(1);
971 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
972 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
973 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
974 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
975 	CU_ASSERT(rc == 0);
976 	CU_ASSERT(bdev_ch[1]->flags == 0);
977 
978 	poll_threads();
979 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
980 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
981 
982 	set_thread(0);
983 	stub_complete_io(g_bdev.io_target, 0);
984 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
985 
986 	set_thread(1);
987 	stub_complete_io(g_bdev.io_target, 0);
988 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
989 
990 	/*
991 	 * Enable QoS on the bdev
992 	 */
993 	set_thread(2);
994 	bdev = bdev_ch[0]->bdev;
995 	bdev->ios_per_sec = 2000;
996 	io_ch[2] = spdk_bdev_get_io_channel(g_desc);
997 	bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]);
998 	qos_bdev_ch = bdev->qos_channel;
999 	module_ch = qos_bdev_ch->module_ch;
1000 	CU_ASSERT(bdev->qos_channel->flags == BDEV_CH_QOS_ENABLED);
1001 	CU_ASSERT(qos_bdev_ch != NULL);
1002 	CU_ASSERT(module_ch != NULL);
1003 
1004 	/*
1005 	 * Now submit a reset, and leave it pending while we submit I/O on two different
1006 	 *  channels.  These I/O should be failed by the bdev layer since the reset is in
1007 	 *  progress.
1008 	 */
1009 	set_thread(0);
1010 	status_reset = SPDK_BDEV_IO_STATUS_PENDING;
1011 	rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &status_reset);
1012 	CU_ASSERT(rc == 0);
1013 
1014 	CU_ASSERT(bdev_ch[0]->flags == 0);
1015 	CU_ASSERT(bdev_ch[1]->flags == 0);
1016 	CU_ASSERT(bdev_ch[2]->flags == 0);
1017 	CU_ASSERT(qos_bdev_ch->flags & BDEV_CH_QOS_ENABLED);
1018 	poll_threads();
1019 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS);
1020 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS);
1021 	CU_ASSERT(bdev_ch[2]->flags == BDEV_CH_RESET_IN_PROGRESS);
1022 	CU_ASSERT(qos_bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS);
1023 
1024 	set_thread(0);
1025 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
1026 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
1027 	CU_ASSERT(rc == 0);
1028 
1029 	set_thread(1);
1030 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
1031 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
1032 	CU_ASSERT(rc == 0);
1033 
1034 	/*
1035 	 * A reset is in progress so these read I/O should complete with failure when QoS has been
1036 	 * enabled. Note that we need to poll_threads() since I/O completed inline have their
1037 	 *  completion deferred.
1038 	 */
1039 	poll_threads();
1040 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING);
1041 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_FAILED);
1042 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_FAILED);
1043 
1044 	set_thread(0);
1045 	stub_complete_io(g_bdev.io_target, 0);
1046 	spdk_put_io_channel(io_ch[0]);
1047 	set_thread(1);
1048 	stub_complete_io(g_bdev.io_target, 0);
1049 	spdk_put_io_channel(io_ch[1]);
1050 	set_thread(2);
1051 	stub_complete_io(g_bdev.io_target, 0);
1052 	spdk_put_io_channel(io_ch[2]);
1053 	poll_threads();
1054 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS);
1055 
1056 	teardown_test();
1057 }
1058 
1059 static void
1060 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1061 {
1062 	enum spdk_bdev_io_status *status = cb_arg;
1063 
1064 	*status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
1065 	spdk_bdev_free_io(bdev_io);
1066 }
1067 
1068 static void
1069 enomem(void)
1070 {
1071 	struct spdk_io_channel *io_ch;
1072 	struct spdk_bdev_channel *bdev_ch;
1073 	struct spdk_bdev_module_channel *module_ch;
1074 	struct ut_bdev_channel *ut_ch;
1075 	const uint32_t IO_ARRAY_SIZE = 64;
1076 	const uint32_t AVAIL = 20;
1077 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset;
1078 	uint32_t nomem_cnt, i;
1079 	struct spdk_bdev_io *first_io;
1080 	int rc;
1081 
1082 	setup_test();
1083 
1084 	set_thread(0);
1085 	io_ch = spdk_bdev_get_io_channel(g_desc);
1086 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1087 	module_ch = bdev_ch->module_ch;
1088 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1089 	ut_ch->avail_cnt = AVAIL;
1090 
1091 	/* First submit a number of IOs equal to what the channel can support. */
1092 	for (i = 0; i < AVAIL; i++) {
1093 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1094 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1095 		CU_ASSERT(rc == 0);
1096 	}
1097 	CU_ASSERT(TAILQ_EMPTY(&module_ch->nomem_io));
1098 
1099 	/*
1100 	 * Next, submit one additional I/O.  This one should fail with ENOMEM and then go onto
1101 	 *  the enomem_io list.
1102 	 */
1103 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1104 	rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1105 	CU_ASSERT(rc == 0);
1106 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&module_ch->nomem_io));
1107 	first_io = TAILQ_FIRST(&module_ch->nomem_io);
1108 
1109 	/*
1110 	 * Now submit a bunch more I/O.  These should all fail with ENOMEM and get queued behind
1111 	 *  the first_io above.
1112 	 */
1113 	for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) {
1114 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1115 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1116 		CU_ASSERT(rc == 0);
1117 	}
1118 
1119 	/* Assert that first_io is still at the head of the list. */
1120 	CU_ASSERT(TAILQ_FIRST(&module_ch->nomem_io) == first_io);
1121 	CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == (IO_ARRAY_SIZE - AVAIL));
1122 	nomem_cnt = bdev_io_tailq_cnt(&module_ch->nomem_io);
1123 	CU_ASSERT(module_ch->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT));
1124 
1125 	/*
1126 	 * Complete 1 I/O only.  The key check here is bdev_io_tailq_cnt - this should not have
1127 	 *  changed since completing just 1 I/O should not trigger retrying the queued nomem_io
1128 	 *  list.
1129 	 */
1130 	stub_complete_io(g_bdev.io_target, 1);
1131 	CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == nomem_cnt);
1132 
1133 	/*
1134 	 * Complete enough I/O to hit the nomem_theshold.  This should trigger retrying nomem_io,
1135 	 *  and we should see I/O get resubmitted to the test bdev module.
1136 	 */
1137 	stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1);
1138 	CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) < nomem_cnt);
1139 	nomem_cnt = bdev_io_tailq_cnt(&module_ch->nomem_io);
1140 
1141 	/* Complete 1 I/O only.  This should not trigger retrying the queued nomem_io. */
1142 	stub_complete_io(g_bdev.io_target, 1);
1143 	CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == nomem_cnt);
1144 
1145 	/*
1146 	 * Send a reset and confirm that all I/O are completed, including the ones that
1147 	 *  were queued on the nomem_io list.
1148 	 */
1149 	status_reset = SPDK_BDEV_IO_STATUS_PENDING;
1150 	rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset);
1151 	poll_threads();
1152 	CU_ASSERT(rc == 0);
1153 	/* This will complete the reset. */
1154 	stub_complete_io(g_bdev.io_target, 0);
1155 
1156 	CU_ASSERT(bdev_io_tailq_cnt(&module_ch->nomem_io) == 0);
1157 	CU_ASSERT(module_ch->io_outstanding == 0);
1158 
1159 	spdk_put_io_channel(io_ch);
1160 	poll_threads();
1161 	teardown_test();
1162 }
1163 
1164 static void
1165 enomem_multi_bdev(void)
1166 {
1167 	struct spdk_io_channel *io_ch;
1168 	struct spdk_bdev_channel *bdev_ch;
1169 	struct spdk_bdev_module_channel *module_ch;
1170 	struct ut_bdev_channel *ut_ch;
1171 	const uint32_t IO_ARRAY_SIZE = 64;
1172 	const uint32_t AVAIL = 20;
1173 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE];
1174 	uint32_t i;
1175 	struct ut_bdev *second_bdev;
1176 	struct spdk_bdev_desc *second_desc;
1177 	struct spdk_bdev_channel *second_bdev_ch;
1178 	struct spdk_io_channel *second_ch;
1179 	int rc;
1180 
1181 	setup_test();
1182 
1183 	/* Register second bdev with the same io_target  */
1184 	second_bdev = calloc(1, sizeof(*second_bdev));
1185 	SPDK_CU_ASSERT_FATAL(second_bdev != NULL);
1186 	register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target);
1187 	spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc);
1188 
1189 	set_thread(0);
1190 	io_ch = spdk_bdev_get_io_channel(g_desc);
1191 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1192 	module_ch = bdev_ch->module_ch;
1193 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1194 	ut_ch->avail_cnt = AVAIL;
1195 
1196 	second_ch = spdk_bdev_get_io_channel(second_desc);
1197 	second_bdev_ch = spdk_io_channel_get_ctx(second_ch);
1198 	SPDK_CU_ASSERT_FATAL(module_ch == second_bdev_ch->module_ch);
1199 
1200 	/* Saturate io_target through bdev A. */
1201 	for (i = 0; i < AVAIL; i++) {
1202 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1203 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1204 		CU_ASSERT(rc == 0);
1205 	}
1206 	CU_ASSERT(TAILQ_EMPTY(&module_ch->nomem_io));
1207 
1208 	/*
1209 	 * Now submit I/O through the second bdev. This should fail with ENOMEM
1210 	 * and then go onto the nomem_io list.
1211 	 */
1212 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1213 	rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1214 	CU_ASSERT(rc == 0);
1215 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&module_ch->nomem_io));
1216 
1217 	/* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */
1218 	stub_complete_io(g_bdev.io_target, AVAIL);
1219 
1220 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&module_ch->nomem_io));
1221 	CU_ASSERT(module_ch->io_outstanding == 1);
1222 
1223 	/* Now complete our retried I/O  */
1224 	stub_complete_io(g_bdev.io_target, 1);
1225 	SPDK_CU_ASSERT_FATAL(module_ch->io_outstanding == 0);
1226 
1227 	spdk_put_io_channel(io_ch);
1228 	spdk_put_io_channel(second_ch);
1229 	spdk_bdev_close(second_desc);
1230 	unregister_bdev(second_bdev);
1231 	poll_threads();
1232 	free(second_bdev);
1233 	teardown_test();
1234 }
1235 
1236 int
1237 main(int argc, char **argv)
1238 {
1239 	CU_pSuite	suite = NULL;
1240 	unsigned int	num_failures;
1241 
1242 	if (CU_initialize_registry() != CUE_SUCCESS) {
1243 		return CU_get_error();
1244 	}
1245 
1246 	suite = CU_add_suite("bdev", NULL, NULL);
1247 	if (suite == NULL) {
1248 		CU_cleanup_registry();
1249 		return CU_get_error();
1250 	}
1251 
1252 	if (
1253 		CU_add_test(suite, "basic", basic) == NULL ||
1254 		CU_add_test(suite, "basic_poller", basic_poller) == NULL ||
1255 		CU_add_test(suite, "basic_qos", basic_qos) == NULL ||
1256 		CU_add_test(suite, "put_channel_during_reset", put_channel_during_reset) == NULL ||
1257 		CU_add_test(suite, "aborted_reset", aborted_reset) == NULL ||
1258 		CU_add_test(suite, "io_during_reset", io_during_reset) == NULL ||
1259 		CU_add_test(suite, "io_during_qos", io_during_qos) == NULL ||
1260 		CU_add_test(suite, "io_during_qos_queue", io_during_qos_queue) == NULL ||
1261 		CU_add_test(suite, "io_during_qos_reset", io_during_qos_reset) == NULL ||
1262 		CU_add_test(suite, "enomem", enomem) == NULL ||
1263 		CU_add_test(suite, "enomem_multi_bdev", enomem_multi_bdev) == NULL
1264 	) {
1265 		CU_cleanup_registry();
1266 		return CU_get_error();
1267 	}
1268 
1269 	CU_basic_set_mode(CU_BRM_VERBOSE);
1270 	CU_basic_run_tests();
1271 	num_failures = CU_get_number_of_failures();
1272 	CU_cleanup_registry();
1273 	return num_failures;
1274 }
1275