xref: /spdk/test/unit/lib/bdev/mt/bdev.c/bdev_ut.c (revision 5b1e154a5ce814461851d6e70b66bcf1939c63d8)
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/ut_multithread.c"
37 #include "unit/lib/json_mock.c"
38 
39 #include "spdk/config.h"
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 struct spdk_trace_histories *g_trace_histories;
48 DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn));
49 DEFINE_STUB_V(spdk_trace_register_owner, (uint8_t type, char id_prefix));
50 DEFINE_STUB_V(spdk_trace_register_object, (uint8_t type, char id_prefix));
51 DEFINE_STUB_V(spdk_trace_register_description, (const char *name,
52 		uint16_t tpoint_id, uint8_t owner_type,
53 		uint8_t object_type, uint8_t new_object,
54 		uint8_t arg1_type, const char *arg1_name));
55 DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
56 				   uint32_t size, uint64_t object_id, uint64_t arg1));
57 DEFINE_STUB(spdk_notify_send, uint64_t, (const char *type, const char *ctx), 0);
58 DEFINE_STUB(spdk_notify_type_register, struct spdk_notify_type *, (const char *type), NULL);
59 
60 struct ut_bdev {
61 	struct spdk_bdev	bdev;
62 	void			*io_target;
63 };
64 
65 struct ut_bdev_channel {
66 	TAILQ_HEAD(, spdk_bdev_io)	outstanding_io;
67 	uint32_t			outstanding_cnt;
68 	uint32_t			avail_cnt;
69 };
70 
71 int g_io_device;
72 struct ut_bdev g_bdev;
73 struct spdk_bdev_desc *g_desc;
74 bool g_teardown_done = false;
75 bool g_get_io_channel = true;
76 bool g_create_ch = true;
77 bool g_init_complete_called = false;
78 bool g_fini_start_called = true;
79 int g_status = 0;
80 int g_count = 0;
81 struct spdk_histogram_data *g_histogram = NULL;
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 	struct spdk_bdev_io *io;
132 
133 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_RESET) {
134 		while (!TAILQ_EMPTY(&ch->outstanding_io)) {
135 			io = TAILQ_FIRST(&ch->outstanding_io);
136 			TAILQ_REMOVE(&ch->outstanding_io, io, module_link);
137 			ch->outstanding_cnt--;
138 			spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_ABORTED);
139 			ch->avail_cnt++;
140 		}
141 	} else if (bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT) {
142 		TAILQ_FOREACH(io, &ch->outstanding_io, module_link) {
143 			if (io == bdev_io->u.abort.bio_to_abort) {
144 				TAILQ_REMOVE(&ch->outstanding_io, io, module_link);
145 				ch->outstanding_cnt--;
146 				spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_ABORTED);
147 				ch->avail_cnt++;
148 
149 				spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
150 				return;
151 			}
152 		}
153 
154 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
155 		return;
156 	}
157 
158 	if (ch->avail_cnt > 0) {
159 		TAILQ_INSERT_TAIL(&ch->outstanding_io, bdev_io, module_link);
160 		ch->outstanding_cnt++;
161 		ch->avail_cnt--;
162 	} else {
163 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
164 	}
165 }
166 
167 static uint32_t
168 stub_complete_io(void *io_target, uint32_t num_to_complete)
169 {
170 	struct spdk_io_channel *_ch = spdk_get_io_channel(io_target);
171 	struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
172 	struct spdk_bdev_io *io;
173 	bool complete_all = (num_to_complete == 0);
174 	uint32_t num_completed = 0;
175 
176 	while (complete_all || num_completed < num_to_complete) {
177 		if (TAILQ_EMPTY(&ch->outstanding_io)) {
178 			break;
179 		}
180 		io = TAILQ_FIRST(&ch->outstanding_io);
181 		TAILQ_REMOVE(&ch->outstanding_io, io, module_link);
182 		ch->outstanding_cnt--;
183 		spdk_bdev_io_complete(io, SPDK_BDEV_IO_STATUS_SUCCESS);
184 		ch->avail_cnt++;
185 		num_completed++;
186 	}
187 
188 	spdk_put_io_channel(_ch);
189 	return num_completed;
190 }
191 
192 static bool
193 stub_io_type_supported(void *ctx, enum spdk_bdev_io_type type)
194 {
195 	return true;
196 }
197 
198 static struct spdk_bdev_fn_table fn_table = {
199 	.get_io_channel =	stub_get_io_channel,
200 	.destruct =		stub_destruct,
201 	.submit_request =	stub_submit_request,
202 	.io_type_supported =	stub_io_type_supported,
203 };
204 
205 struct spdk_bdev_module bdev_ut_if;
206 
207 static int
208 module_init(void)
209 {
210 	spdk_bdev_module_init_done(&bdev_ut_if);
211 	return 0;
212 }
213 
214 static void
215 module_fini(void)
216 {
217 }
218 
219 static void
220 init_complete(void)
221 {
222 	g_init_complete_called = true;
223 }
224 
225 static void
226 fini_start(void)
227 {
228 	g_fini_start_called = true;
229 }
230 
231 struct spdk_bdev_module bdev_ut_if = {
232 	.name = "bdev_ut",
233 	.module_init = module_init,
234 	.module_fini = module_fini,
235 	.async_init = true,
236 	.init_complete = init_complete,
237 	.fini_start = fini_start,
238 };
239 
240 SPDK_BDEV_MODULE_REGISTER(bdev_ut, &bdev_ut_if)
241 
242 static void
243 register_bdev(struct ut_bdev *ut_bdev, char *name, void *io_target)
244 {
245 	memset(ut_bdev, 0, sizeof(*ut_bdev));
246 
247 	ut_bdev->io_target = io_target;
248 	ut_bdev->bdev.ctxt = ut_bdev;
249 	ut_bdev->bdev.name = name;
250 	ut_bdev->bdev.fn_table = &fn_table;
251 	ut_bdev->bdev.module = &bdev_ut_if;
252 	ut_bdev->bdev.blocklen = 4096;
253 	ut_bdev->bdev.blockcnt = 1024;
254 
255 	spdk_bdev_register(&ut_bdev->bdev);
256 }
257 
258 static void
259 unregister_bdev(struct ut_bdev *ut_bdev)
260 {
261 	/* Handle any deferred messages. */
262 	poll_threads();
263 	spdk_bdev_unregister(&ut_bdev->bdev, NULL, NULL);
264 }
265 
266 static void
267 bdev_init_cb(void *done, int rc)
268 {
269 	CU_ASSERT(rc == 0);
270 	*(bool *)done = true;
271 }
272 
273 static void
274 setup_test(void)
275 {
276 	bool done = false;
277 
278 	allocate_cores(BDEV_UT_NUM_THREADS);
279 	allocate_threads(BDEV_UT_NUM_THREADS);
280 	set_thread(0);
281 	spdk_bdev_initialize(bdev_init_cb, &done);
282 	spdk_io_device_register(&g_io_device, stub_create_ch, stub_destroy_ch,
283 				sizeof(struct ut_bdev_channel), NULL);
284 	register_bdev(&g_bdev, "ut_bdev", &g_io_device);
285 	spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc);
286 }
287 
288 static void
289 finish_cb(void *cb_arg)
290 {
291 	g_teardown_done = true;
292 }
293 
294 static void
295 teardown_test(void)
296 {
297 	set_thread(0);
298 	g_teardown_done = false;
299 	spdk_bdev_close(g_desc);
300 	g_desc = NULL;
301 	unregister_bdev(&g_bdev);
302 	spdk_io_device_unregister(&g_io_device, NULL);
303 	spdk_bdev_finish(finish_cb, NULL);
304 	poll_threads();
305 	memset(&g_bdev, 0, sizeof(g_bdev));
306 	CU_ASSERT(g_teardown_done == true);
307 	g_teardown_done = false;
308 	free_threads();
309 	free_cores();
310 }
311 
312 static uint32_t
313 bdev_io_tailq_cnt(bdev_io_tailq_t *tailq)
314 {
315 	struct spdk_bdev_io *io;
316 	uint32_t cnt = 0;
317 
318 	TAILQ_FOREACH(io, tailq, internal.link) {
319 		cnt++;
320 	}
321 
322 	return cnt;
323 }
324 
325 static void
326 basic(void)
327 {
328 	g_init_complete_called = false;
329 	setup_test();
330 	CU_ASSERT(g_init_complete_called == true);
331 
332 	set_thread(0);
333 
334 	g_get_io_channel = false;
335 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
336 	CU_ASSERT(g_ut_threads[0].ch == NULL);
337 
338 	g_get_io_channel = true;
339 	g_create_ch = false;
340 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
341 	CU_ASSERT(g_ut_threads[0].ch == NULL);
342 
343 	g_get_io_channel = true;
344 	g_create_ch = true;
345 	g_ut_threads[0].ch = spdk_bdev_get_io_channel(g_desc);
346 	CU_ASSERT(g_ut_threads[0].ch != NULL);
347 	spdk_put_io_channel(g_ut_threads[0].ch);
348 
349 	g_fini_start_called = false;
350 	teardown_test();
351 	CU_ASSERT(g_fini_start_called == true);
352 }
353 
354 static void
355 _bdev_removed(void *done)
356 {
357 	*(bool *)done = true;
358 }
359 
360 static void
361 _bdev_unregistered(void *done, int rc)
362 {
363 	CU_ASSERT(rc == 0);
364 	*(bool *)done = true;
365 }
366 
367 static void
368 unregister_and_close(void)
369 {
370 	bool done, remove_notify;
371 	struct spdk_bdev_desc *desc = NULL;
372 
373 	setup_test();
374 	set_thread(0);
375 
376 	/* setup_test() automatically opens the bdev,
377 	 * but this test needs to do that in a different
378 	 * way. */
379 	spdk_bdev_close(g_desc);
380 	poll_threads();
381 
382 	/* Try hotremoving a bdev with descriptors which don't provide
383 	 * the notification callback */
384 	spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &desc);
385 	SPDK_CU_ASSERT_FATAL(desc != NULL);
386 
387 	/* There is an open descriptor on the device. Unregister it,
388 	 * which can't proceed until the descriptor is closed. */
389 	done = false;
390 	spdk_bdev_unregister(&g_bdev.bdev, _bdev_unregistered, &done);
391 
392 	/* Poll the threads to allow all events to be processed */
393 	poll_threads();
394 
395 	/* Make sure the bdev was not unregistered. We still have a
396 	 * descriptor open */
397 	CU_ASSERT(done == false);
398 
399 	spdk_bdev_close(desc);
400 	poll_threads();
401 	desc = NULL;
402 
403 	/* The unregister should have completed */
404 	CU_ASSERT(done == true);
405 
406 
407 	/* Register the bdev again */
408 	register_bdev(&g_bdev, "ut_bdev", &g_io_device);
409 
410 	remove_notify = false;
411 	spdk_bdev_open(&g_bdev.bdev, true, _bdev_removed, &remove_notify, &desc);
412 	SPDK_CU_ASSERT_FATAL(desc != NULL);
413 	CU_ASSERT(remove_notify == false);
414 
415 	/* There is an open descriptor on the device. Unregister it,
416 	 * which can't proceed until the descriptor is closed. */
417 	done = false;
418 	spdk_bdev_unregister(&g_bdev.bdev, _bdev_unregistered, &done);
419 	/* No polling has occurred, so neither of these should execute */
420 	CU_ASSERT(remove_notify == false);
421 	CU_ASSERT(done == false);
422 
423 	/* Prior to the unregister completing, close the descriptor */
424 	spdk_bdev_close(desc);
425 
426 	/* Poll the threads to allow all events to be processed */
427 	poll_threads();
428 
429 	/* Remove notify should not have been called because the
430 	 * descriptor is already closed. */
431 	CU_ASSERT(remove_notify == false);
432 
433 	/* The unregister should have completed */
434 	CU_ASSERT(done == true);
435 
436 	/* Restore the original g_bdev so that we can use teardown_test(). */
437 	register_bdev(&g_bdev, "ut_bdev", &g_io_device);
438 	spdk_bdev_open(&g_bdev.bdev, true, NULL, NULL, &g_desc);
439 	teardown_test();
440 }
441 
442 static void
443 reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
444 {
445 	bool *done = cb_arg;
446 
447 	CU_ASSERT(success == true);
448 	*done = true;
449 	spdk_bdev_free_io(bdev_io);
450 }
451 
452 static void
453 put_channel_during_reset(void)
454 {
455 	struct spdk_io_channel *io_ch;
456 	bool done = false;
457 
458 	setup_test();
459 
460 	set_thread(0);
461 	io_ch = spdk_bdev_get_io_channel(g_desc);
462 	CU_ASSERT(io_ch != NULL);
463 
464 	/*
465 	 * Start a reset, but then put the I/O channel before
466 	 *  the deferred messages for the reset get a chance to
467 	 *  execute.
468 	 */
469 	spdk_bdev_reset(g_desc, io_ch, reset_done, &done);
470 	spdk_put_io_channel(io_ch);
471 	poll_threads();
472 	stub_complete_io(g_bdev.io_target, 0);
473 
474 	teardown_test();
475 }
476 
477 static void
478 aborted_reset_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
479 {
480 	enum spdk_bdev_io_status *status = cb_arg;
481 
482 	*status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
483 	spdk_bdev_free_io(bdev_io);
484 }
485 
486 static void
487 aborted_reset(void)
488 {
489 	struct spdk_io_channel *io_ch[2];
490 	enum spdk_bdev_io_status status1 = SPDK_BDEV_IO_STATUS_PENDING,
491 				 status2 = SPDK_BDEV_IO_STATUS_PENDING;
492 
493 	setup_test();
494 
495 	set_thread(0);
496 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
497 	CU_ASSERT(io_ch[0] != NULL);
498 	spdk_bdev_reset(g_desc, io_ch[0], aborted_reset_done, &status1);
499 	poll_threads();
500 	CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL);
501 
502 	/*
503 	 * First reset has been submitted on ch0.  Now submit a second
504 	 *  reset on ch1 which will get queued since there is already a
505 	 *  reset in progress.
506 	 */
507 	set_thread(1);
508 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
509 	CU_ASSERT(io_ch[1] != NULL);
510 	spdk_bdev_reset(g_desc, io_ch[1], aborted_reset_done, &status2);
511 	poll_threads();
512 	CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL);
513 
514 	/*
515 	 * Now destroy ch1.  This will abort the queued reset.  Check that
516 	 *  the second reset was completed with failed status.  Also check
517 	 *  that bdev->internal.reset_in_progress != NULL, since the
518 	 *  original reset has not been completed yet.  This ensures that
519 	 *  the bdev code is correctly noticing that the failed reset is
520 	 *  *not* the one that had been submitted to the bdev module.
521 	 */
522 	set_thread(1);
523 	spdk_put_io_channel(io_ch[1]);
524 	poll_threads();
525 	CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_FAILED);
526 	CU_ASSERT(g_bdev.bdev.internal.reset_in_progress != NULL);
527 
528 	/*
529 	 * Now complete the first reset, verify that it completed with SUCCESS
530 	 *  status and that bdev->internal.reset_in_progress is also set back to NULL.
531 	 */
532 	set_thread(0);
533 	spdk_put_io_channel(io_ch[0]);
534 	stub_complete_io(g_bdev.io_target, 0);
535 	poll_threads();
536 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
537 	CU_ASSERT(g_bdev.bdev.internal.reset_in_progress == NULL);
538 
539 	teardown_test();
540 }
541 
542 static void
543 io_during_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
544 {
545 	enum spdk_bdev_io_status *status = cb_arg;
546 
547 	*status = bdev_io->internal.status;
548 	spdk_bdev_free_io(bdev_io);
549 }
550 
551 static void
552 io_during_reset(void)
553 {
554 	struct spdk_io_channel *io_ch[2];
555 	struct spdk_bdev_channel *bdev_ch[2];
556 	enum spdk_bdev_io_status status0, status1, status_reset;
557 	int rc;
558 
559 	setup_test();
560 
561 	/*
562 	 * First test normal case - submit an I/O on each of two channels (with no resets)
563 	 *  and verify they complete successfully.
564 	 */
565 	set_thread(0);
566 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
567 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
568 	CU_ASSERT(bdev_ch[0]->flags == 0);
569 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
570 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
571 	CU_ASSERT(rc == 0);
572 
573 	set_thread(1);
574 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
575 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
576 	CU_ASSERT(bdev_ch[1]->flags == 0);
577 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
578 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
579 	CU_ASSERT(rc == 0);
580 
581 	poll_threads();
582 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
583 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
584 
585 	set_thread(0);
586 	stub_complete_io(g_bdev.io_target, 0);
587 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
588 
589 	set_thread(1);
590 	stub_complete_io(g_bdev.io_target, 0);
591 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
592 
593 	/*
594 	 * Now submit a reset, and leave it pending while we submit I/O on two different
595 	 *  channels.  These I/O should be failed by the bdev layer since the reset is in
596 	 *  progress.
597 	 */
598 	set_thread(0);
599 	status_reset = SPDK_BDEV_IO_STATUS_PENDING;
600 	rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &status_reset);
601 	CU_ASSERT(rc == 0);
602 
603 	CU_ASSERT(bdev_ch[0]->flags == 0);
604 	CU_ASSERT(bdev_ch[1]->flags == 0);
605 	poll_threads();
606 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_RESET_IN_PROGRESS);
607 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_RESET_IN_PROGRESS);
608 
609 	set_thread(0);
610 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
611 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
612 	CU_ASSERT(rc == 0);
613 
614 	set_thread(1);
615 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
616 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
617 	CU_ASSERT(rc == 0);
618 
619 	/*
620 	 * A reset is in progress so these read I/O should complete with aborted.  Note that we
621 	 *  need to poll_threads() since I/O completed inline have their completion deferred.
622 	 */
623 	poll_threads();
624 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING);
625 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_ABORTED);
626 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_ABORTED);
627 
628 	/*
629 	 * Complete the reset
630 	 */
631 	set_thread(0);
632 	stub_complete_io(g_bdev.io_target, 0);
633 
634 	/*
635 	 * Only poll thread 0. We should not get a completion.
636 	 */
637 	poll_thread(0);
638 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_PENDING);
639 
640 	/*
641 	 * Poll both thread 0 and 1 so the messages can propagate and we
642 	 * get a completion.
643 	 */
644 	poll_threads();
645 	CU_ASSERT(status_reset == SPDK_BDEV_IO_STATUS_SUCCESS);
646 
647 	spdk_put_io_channel(io_ch[0]);
648 	set_thread(1);
649 	spdk_put_io_channel(io_ch[1]);
650 	poll_threads();
651 
652 	teardown_test();
653 }
654 
655 static void
656 basic_qos(void)
657 {
658 	struct spdk_io_channel *io_ch[2];
659 	struct spdk_bdev_channel *bdev_ch[2];
660 	struct spdk_bdev *bdev;
661 	enum spdk_bdev_io_status status, abort_status;
662 	int rc;
663 
664 	setup_test();
665 
666 	/* Enable QoS */
667 	bdev = &g_bdev.bdev;
668 	bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
669 	SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL);
670 	TAILQ_INIT(&bdev->internal.qos->queued);
671 	/*
672 	 * Enable read/write IOPS, read only byte per second and
673 	 * read/write byte per second rate limits.
674 	 * In this case, all rate limits will take equal effect.
675 	 */
676 	/* 2000 read/write I/O per second, or 2 per millisecond */
677 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 2000;
678 	/* 8K read/write byte per millisecond with 4K block size */
679 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 8192000;
680 	/* 8K read only byte per millisecond with 4K block size */
681 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT].limit = 8192000;
682 
683 	g_get_io_channel = true;
684 
685 	set_thread(0);
686 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
687 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
688 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
689 
690 	set_thread(1);
691 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
692 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
693 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
694 
695 	/*
696 	 * Send an I/O on thread 0, which is where the QoS thread is running.
697 	 */
698 	set_thread(0);
699 	status = SPDK_BDEV_IO_STATUS_PENDING;
700 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status);
701 	CU_ASSERT(rc == 0);
702 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
703 	poll_threads();
704 	stub_complete_io(g_bdev.io_target, 0);
705 	poll_threads();
706 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS);
707 
708 	/* Send an I/O on thread 1. The QoS thread is not running here. */
709 	status = SPDK_BDEV_IO_STATUS_PENDING;
710 	set_thread(1);
711 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status);
712 	CU_ASSERT(rc == 0);
713 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
714 	poll_threads();
715 	/* Complete I/O on thread 1. This should not complete the I/O we submitted */
716 	stub_complete_io(g_bdev.io_target, 0);
717 	poll_threads();
718 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
719 	/* Now complete I/O on thread 0 */
720 	set_thread(0);
721 	poll_threads();
722 	stub_complete_io(g_bdev.io_target, 0);
723 	poll_threads();
724 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_SUCCESS);
725 
726 	/* Reset rate limit for the next test cases. */
727 	spdk_delay_us(SPDK_BDEV_QOS_TIMESLICE_IN_USEC);
728 	poll_threads();
729 
730 	/*
731 	 * Test abort request when QoS is enabled.
732 	 */
733 
734 	/* Send an I/O on thread 0, which is where the QoS thread is running. */
735 	set_thread(0);
736 	status = SPDK_BDEV_IO_STATUS_PENDING;
737 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status);
738 	CU_ASSERT(rc == 0);
739 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
740 	/* Send an abort to the I/O on the same thread. */
741 	abort_status = SPDK_BDEV_IO_STATUS_PENDING;
742 	rc = spdk_bdev_abort(g_desc, io_ch[0], &status, io_during_io_done, &abort_status);
743 	CU_ASSERT(rc == 0);
744 	CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_PENDING);
745 	poll_threads();
746 	CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_SUCCESS);
747 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_ABORTED);
748 
749 	/* Send an I/O on thread 1. The QoS thread is not running here. */
750 	status = SPDK_BDEV_IO_STATUS_PENDING;
751 	set_thread(1);
752 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status);
753 	CU_ASSERT(rc == 0);
754 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_PENDING);
755 	poll_threads();
756 	/* Send an abort to the I/O on the same thread. */
757 	abort_status = SPDK_BDEV_IO_STATUS_PENDING;
758 	rc = spdk_bdev_abort(g_desc, io_ch[1], &status, io_during_io_done, &abort_status);
759 	CU_ASSERT(rc == 0);
760 	CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_PENDING);
761 	poll_threads();
762 	/* Complete the I/O with failure and the abort with success on thread 1. */
763 	CU_ASSERT(abort_status == SPDK_BDEV_IO_STATUS_SUCCESS);
764 	CU_ASSERT(status == SPDK_BDEV_IO_STATUS_ABORTED);
765 
766 	set_thread(0);
767 
768 	/*
769 	 * Close the descriptor only, which should stop the qos channel as
770 	 * the last descriptor removed.
771 	 */
772 	spdk_bdev_close(g_desc);
773 	poll_threads();
774 	CU_ASSERT(bdev->internal.qos->ch == NULL);
775 
776 	/*
777 	 * Open the bdev again which shall setup the qos channel as the
778 	 * channels are valid.
779 	 */
780 	spdk_bdev_open(bdev, true, NULL, NULL, &g_desc);
781 	poll_threads();
782 	CU_ASSERT(bdev->internal.qos->ch != NULL);
783 
784 	/* Tear down the channels */
785 	set_thread(0);
786 	spdk_put_io_channel(io_ch[0]);
787 	set_thread(1);
788 	spdk_put_io_channel(io_ch[1]);
789 	poll_threads();
790 	set_thread(0);
791 
792 	/* Close the descriptor, which should stop the qos channel */
793 	spdk_bdev_close(g_desc);
794 	poll_threads();
795 	CU_ASSERT(bdev->internal.qos->ch == NULL);
796 
797 	/* Open the bdev again, no qos channel setup without valid channels. */
798 	spdk_bdev_open(bdev, true, NULL, NULL, &g_desc);
799 	poll_threads();
800 	CU_ASSERT(bdev->internal.qos->ch == NULL);
801 
802 	/* Create the channels in reverse order. */
803 	set_thread(1);
804 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
805 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
806 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
807 
808 	set_thread(0);
809 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
810 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
811 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
812 
813 	/* Confirm that the qos thread is now thread 1 */
814 	CU_ASSERT(bdev->internal.qos->ch == bdev_ch[1]);
815 
816 	/* Tear down the channels */
817 	set_thread(0);
818 	spdk_put_io_channel(io_ch[0]);
819 	set_thread(1);
820 	spdk_put_io_channel(io_ch[1]);
821 	poll_threads();
822 
823 	set_thread(0);
824 
825 	teardown_test();
826 }
827 
828 static void
829 io_during_qos_queue(void)
830 {
831 	struct spdk_io_channel *io_ch[2];
832 	struct spdk_bdev_channel *bdev_ch[2];
833 	struct spdk_bdev *bdev;
834 	enum spdk_bdev_io_status status0, status1, status2;
835 	int rc;
836 
837 	setup_test();
838 	MOCK_SET(spdk_get_ticks, 0);
839 
840 	/* Enable QoS */
841 	bdev = &g_bdev.bdev;
842 	bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
843 	SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL);
844 	TAILQ_INIT(&bdev->internal.qos->queued);
845 	/*
846 	 * Enable read/write IOPS, read only byte per sec, write only
847 	 * byte per sec and read/write byte per sec rate limits.
848 	 * In this case, both read only and write only byte per sec
849 	 * rate limit will take effect.
850 	 */
851 	/* 4000 read/write I/O per second, or 4 per millisecond */
852 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 4000;
853 	/* 8K byte per millisecond with 4K block size */
854 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 8192000;
855 	/* 4K byte per millisecond with 4K block size */
856 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT].limit = 4096000;
857 	/* 4K byte per millisecond with 4K block size */
858 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT].limit = 4096000;
859 
860 	g_get_io_channel = true;
861 
862 	/* Create channels */
863 	set_thread(0);
864 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
865 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
866 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
867 
868 	set_thread(1);
869 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
870 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
871 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
872 
873 	/* Send two read I/Os */
874 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
875 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
876 	CU_ASSERT(rc == 0);
877 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
878 	set_thread(0);
879 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
880 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
881 	CU_ASSERT(rc == 0);
882 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
883 	/* Send one write I/O */
884 	status2 = SPDK_BDEV_IO_STATUS_PENDING;
885 	rc = spdk_bdev_write_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status2);
886 	CU_ASSERT(rc == 0);
887 	CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_PENDING);
888 
889 	/* Complete any I/O that arrived at the disk */
890 	poll_threads();
891 	set_thread(1);
892 	stub_complete_io(g_bdev.io_target, 0);
893 	set_thread(0);
894 	stub_complete_io(g_bdev.io_target, 0);
895 	poll_threads();
896 
897 	/* Only one of the two read I/Os should complete. (logical XOR) */
898 	if (status0 == SPDK_BDEV_IO_STATUS_SUCCESS) {
899 		CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
900 	} else {
901 		CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
902 	}
903 	/* The write I/O should complete. */
904 	CU_ASSERT(status2 == SPDK_BDEV_IO_STATUS_SUCCESS);
905 
906 	/* Advance in time by a millisecond */
907 	spdk_delay_us(1000);
908 
909 	/* Complete more I/O */
910 	poll_threads();
911 	set_thread(1);
912 	stub_complete_io(g_bdev.io_target, 0);
913 	set_thread(0);
914 	stub_complete_io(g_bdev.io_target, 0);
915 	poll_threads();
916 
917 	/* Now the second read I/O should be done */
918 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_SUCCESS);
919 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_SUCCESS);
920 
921 	/* Tear down the channels */
922 	set_thread(1);
923 	spdk_put_io_channel(io_ch[1]);
924 	set_thread(0);
925 	spdk_put_io_channel(io_ch[0]);
926 	poll_threads();
927 
928 	teardown_test();
929 }
930 
931 static void
932 io_during_qos_reset(void)
933 {
934 	struct spdk_io_channel *io_ch[2];
935 	struct spdk_bdev_channel *bdev_ch[2];
936 	struct spdk_bdev *bdev;
937 	enum spdk_bdev_io_status status0, status1, reset_status;
938 	int rc;
939 
940 	setup_test();
941 	MOCK_SET(spdk_get_ticks, 0);
942 
943 	/* Enable QoS */
944 	bdev = &g_bdev.bdev;
945 	bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
946 	SPDK_CU_ASSERT_FATAL(bdev->internal.qos != NULL);
947 	TAILQ_INIT(&bdev->internal.qos->queued);
948 	/*
949 	 * Enable read/write IOPS, write only byte per sec and
950 	 * read/write byte per second rate limits.
951 	 * In this case, read/write byte per second rate limit will
952 	 * take effect first.
953 	 */
954 	/* 2000 read/write I/O per second, or 2 per millisecond */
955 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT].limit = 2000;
956 	/* 4K byte per millisecond with 4K block size */
957 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT].limit = 4096000;
958 	/* 8K byte per millisecond with 4K block size */
959 	bdev->internal.qos->rate_limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT].limit = 8192000;
960 
961 	g_get_io_channel = true;
962 
963 	/* Create channels */
964 	set_thread(0);
965 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
966 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
967 	CU_ASSERT(bdev_ch[0]->flags == BDEV_CH_QOS_ENABLED);
968 
969 	set_thread(1);
970 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
971 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
972 	CU_ASSERT(bdev_ch[1]->flags == BDEV_CH_QOS_ENABLED);
973 
974 	/* Send two I/O. One of these gets queued by QoS. The other is sitting at the disk. */
975 	status1 = SPDK_BDEV_IO_STATUS_PENDING;
976 	rc = spdk_bdev_write_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &status1);
977 	CU_ASSERT(rc == 0);
978 	set_thread(0);
979 	status0 = SPDK_BDEV_IO_STATUS_PENDING;
980 	rc = spdk_bdev_write_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &status0);
981 	CU_ASSERT(rc == 0);
982 
983 	poll_threads();
984 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_PENDING);
985 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_PENDING);
986 
987 	/* Reset the bdev. */
988 	reset_status = SPDK_BDEV_IO_STATUS_PENDING;
989 	rc = spdk_bdev_reset(g_desc, io_ch[0], io_during_io_done, &reset_status);
990 	CU_ASSERT(rc == 0);
991 
992 	/* Complete any I/O that arrived at the disk */
993 	poll_threads();
994 	set_thread(1);
995 	stub_complete_io(g_bdev.io_target, 0);
996 	set_thread(0);
997 	stub_complete_io(g_bdev.io_target, 0);
998 	poll_threads();
999 
1000 	CU_ASSERT(reset_status == SPDK_BDEV_IO_STATUS_SUCCESS);
1001 	CU_ASSERT(status0 == SPDK_BDEV_IO_STATUS_ABORTED);
1002 	CU_ASSERT(status1 == SPDK_BDEV_IO_STATUS_ABORTED);
1003 
1004 	/* Tear down the channels */
1005 	set_thread(1);
1006 	spdk_put_io_channel(io_ch[1]);
1007 	set_thread(0);
1008 	spdk_put_io_channel(io_ch[0]);
1009 	poll_threads();
1010 
1011 	teardown_test();
1012 }
1013 
1014 static void
1015 enomem_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1016 {
1017 	enum spdk_bdev_io_status *status = cb_arg;
1018 
1019 	*status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
1020 	spdk_bdev_free_io(bdev_io);
1021 }
1022 
1023 static void
1024 enomem(void)
1025 {
1026 	struct spdk_io_channel *io_ch;
1027 	struct spdk_bdev_channel *bdev_ch;
1028 	struct spdk_bdev_shared_resource *shared_resource;
1029 	struct ut_bdev_channel *ut_ch;
1030 	const uint32_t IO_ARRAY_SIZE = 64;
1031 	const uint32_t AVAIL = 20;
1032 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE], status_reset;
1033 	uint32_t nomem_cnt, i;
1034 	struct spdk_bdev_io *first_io;
1035 	int rc;
1036 
1037 	setup_test();
1038 
1039 	set_thread(0);
1040 	io_ch = spdk_bdev_get_io_channel(g_desc);
1041 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1042 	shared_resource = bdev_ch->shared_resource;
1043 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1044 	ut_ch->avail_cnt = AVAIL;
1045 
1046 	/* First submit a number of IOs equal to what the channel can support. */
1047 	for (i = 0; i < AVAIL; i++) {
1048 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1049 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1050 		CU_ASSERT(rc == 0);
1051 	}
1052 	CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io));
1053 
1054 	/*
1055 	 * Next, submit one additional I/O.  This one should fail with ENOMEM and then go onto
1056 	 *  the enomem_io list.
1057 	 */
1058 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1059 	rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1060 	CU_ASSERT(rc == 0);
1061 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io));
1062 	first_io = TAILQ_FIRST(&shared_resource->nomem_io);
1063 
1064 	/*
1065 	 * Now submit a bunch more I/O.  These should all fail with ENOMEM and get queued behind
1066 	 *  the first_io above.
1067 	 */
1068 	for (i = AVAIL + 1; i < IO_ARRAY_SIZE; i++) {
1069 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1070 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1071 		CU_ASSERT(rc == 0);
1072 	}
1073 
1074 	/* Assert that first_io is still at the head of the list. */
1075 	CU_ASSERT(TAILQ_FIRST(&shared_resource->nomem_io) == first_io);
1076 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == (IO_ARRAY_SIZE - AVAIL));
1077 	nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io);
1078 	CU_ASSERT(shared_resource->nomem_threshold == (AVAIL - NOMEM_THRESHOLD_COUNT));
1079 
1080 	/*
1081 	 * Complete 1 I/O only.  The key check here is bdev_io_tailq_cnt - this should not have
1082 	 *  changed since completing just 1 I/O should not trigger retrying the queued nomem_io
1083 	 *  list.
1084 	 */
1085 	stub_complete_io(g_bdev.io_target, 1);
1086 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt);
1087 
1088 	/*
1089 	 * Complete enough I/O to hit the nomem_theshold.  This should trigger retrying nomem_io,
1090 	 *  and we should see I/O get resubmitted to the test bdev module.
1091 	 */
1092 	stub_complete_io(g_bdev.io_target, NOMEM_THRESHOLD_COUNT - 1);
1093 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) < nomem_cnt);
1094 	nomem_cnt = bdev_io_tailq_cnt(&shared_resource->nomem_io);
1095 
1096 	/* Complete 1 I/O only.  This should not trigger retrying the queued nomem_io. */
1097 	stub_complete_io(g_bdev.io_target, 1);
1098 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == nomem_cnt);
1099 
1100 	/*
1101 	 * Send a reset and confirm that all I/O are completed, including the ones that
1102 	 *  were queued on the nomem_io list.
1103 	 */
1104 	status_reset = SPDK_BDEV_IO_STATUS_PENDING;
1105 	rc = spdk_bdev_reset(g_desc, io_ch, enomem_done, &status_reset);
1106 	poll_threads();
1107 	CU_ASSERT(rc == 0);
1108 	/* This will complete the reset. */
1109 	stub_complete_io(g_bdev.io_target, 0);
1110 
1111 	CU_ASSERT(bdev_io_tailq_cnt(&shared_resource->nomem_io) == 0);
1112 	CU_ASSERT(shared_resource->io_outstanding == 0);
1113 
1114 	spdk_put_io_channel(io_ch);
1115 	poll_threads();
1116 	teardown_test();
1117 }
1118 
1119 static void
1120 enomem_multi_bdev(void)
1121 {
1122 	struct spdk_io_channel *io_ch;
1123 	struct spdk_bdev_channel *bdev_ch;
1124 	struct spdk_bdev_shared_resource *shared_resource;
1125 	struct ut_bdev_channel *ut_ch;
1126 	const uint32_t IO_ARRAY_SIZE = 64;
1127 	const uint32_t AVAIL = 20;
1128 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE];
1129 	uint32_t i;
1130 	struct ut_bdev *second_bdev;
1131 	struct spdk_bdev_desc *second_desc = NULL;
1132 	struct spdk_bdev_channel *second_bdev_ch;
1133 	struct spdk_io_channel *second_ch;
1134 	int rc;
1135 
1136 	setup_test();
1137 
1138 	/* Register second bdev with the same io_target  */
1139 	second_bdev = calloc(1, sizeof(*second_bdev));
1140 	SPDK_CU_ASSERT_FATAL(second_bdev != NULL);
1141 	register_bdev(second_bdev, "ut_bdev2", g_bdev.io_target);
1142 	spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc);
1143 	SPDK_CU_ASSERT_FATAL(second_desc != NULL);
1144 
1145 	set_thread(0);
1146 	io_ch = spdk_bdev_get_io_channel(g_desc);
1147 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1148 	shared_resource = bdev_ch->shared_resource;
1149 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1150 	ut_ch->avail_cnt = AVAIL;
1151 
1152 	second_ch = spdk_bdev_get_io_channel(second_desc);
1153 	second_bdev_ch = spdk_io_channel_get_ctx(second_ch);
1154 	SPDK_CU_ASSERT_FATAL(shared_resource == second_bdev_ch->shared_resource);
1155 
1156 	/* Saturate io_target through bdev A. */
1157 	for (i = 0; i < AVAIL; i++) {
1158 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1159 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1160 		CU_ASSERT(rc == 0);
1161 	}
1162 	CU_ASSERT(TAILQ_EMPTY(&shared_resource->nomem_io));
1163 
1164 	/*
1165 	 * Now submit I/O through the second bdev. This should fail with ENOMEM
1166 	 * and then go onto the nomem_io list.
1167 	 */
1168 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1169 	rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1170 	CU_ASSERT(rc == 0);
1171 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&shared_resource->nomem_io));
1172 
1173 	/* Complete first bdev's I/O. This should retry sending second bdev's nomem_io */
1174 	stub_complete_io(g_bdev.io_target, AVAIL);
1175 
1176 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&shared_resource->nomem_io));
1177 	CU_ASSERT(shared_resource->io_outstanding == 1);
1178 
1179 	/* Now complete our retried I/O  */
1180 	stub_complete_io(g_bdev.io_target, 1);
1181 	SPDK_CU_ASSERT_FATAL(shared_resource->io_outstanding == 0);
1182 
1183 	spdk_put_io_channel(io_ch);
1184 	spdk_put_io_channel(second_ch);
1185 	spdk_bdev_close(second_desc);
1186 	unregister_bdev(second_bdev);
1187 	poll_threads();
1188 	free(second_bdev);
1189 	teardown_test();
1190 }
1191 
1192 
1193 static void
1194 enomem_multi_io_target(void)
1195 {
1196 	struct spdk_io_channel *io_ch;
1197 	struct spdk_bdev_channel *bdev_ch;
1198 	struct ut_bdev_channel *ut_ch;
1199 	const uint32_t IO_ARRAY_SIZE = 64;
1200 	const uint32_t AVAIL = 20;
1201 	enum spdk_bdev_io_status status[IO_ARRAY_SIZE];
1202 	uint32_t i;
1203 	int new_io_device;
1204 	struct ut_bdev *second_bdev;
1205 	struct spdk_bdev_desc *second_desc = NULL;
1206 	struct spdk_bdev_channel *second_bdev_ch;
1207 	struct spdk_io_channel *second_ch;
1208 	int rc;
1209 
1210 	setup_test();
1211 
1212 	/* Create new io_target and a second bdev using it */
1213 	spdk_io_device_register(&new_io_device, stub_create_ch, stub_destroy_ch,
1214 				sizeof(struct ut_bdev_channel), NULL);
1215 	second_bdev = calloc(1, sizeof(*second_bdev));
1216 	SPDK_CU_ASSERT_FATAL(second_bdev != NULL);
1217 	register_bdev(second_bdev, "ut_bdev2", &new_io_device);
1218 	spdk_bdev_open(&second_bdev->bdev, true, NULL, NULL, &second_desc);
1219 	SPDK_CU_ASSERT_FATAL(second_desc != NULL);
1220 
1221 	set_thread(0);
1222 	io_ch = spdk_bdev_get_io_channel(g_desc);
1223 	bdev_ch = spdk_io_channel_get_ctx(io_ch);
1224 	ut_ch = spdk_io_channel_get_ctx(bdev_ch->channel);
1225 	ut_ch->avail_cnt = AVAIL;
1226 
1227 	/* Different io_target should imply a different shared_resource */
1228 	second_ch = spdk_bdev_get_io_channel(second_desc);
1229 	second_bdev_ch = spdk_io_channel_get_ctx(second_ch);
1230 	SPDK_CU_ASSERT_FATAL(bdev_ch->shared_resource != second_bdev_ch->shared_resource);
1231 
1232 	/* Saturate io_target through bdev A. */
1233 	for (i = 0; i < AVAIL; i++) {
1234 		status[i] = SPDK_BDEV_IO_STATUS_PENDING;
1235 		rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[i]);
1236 		CU_ASSERT(rc == 0);
1237 	}
1238 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1239 
1240 	/* Issue one more I/O to fill ENOMEM list. */
1241 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1242 	rc = spdk_bdev_read_blocks(g_desc, io_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1243 	CU_ASSERT(rc == 0);
1244 	SPDK_CU_ASSERT_FATAL(!TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1245 
1246 	/*
1247 	 * Now submit I/O through the second bdev. This should go through and complete
1248 	 * successfully because we're using a different io_device underneath.
1249 	 */
1250 	status[AVAIL] = SPDK_BDEV_IO_STATUS_PENDING;
1251 	rc = spdk_bdev_read_blocks(second_desc, second_ch, NULL, 0, 1, enomem_done, &status[AVAIL]);
1252 	CU_ASSERT(rc == 0);
1253 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&second_bdev_ch->shared_resource->nomem_io));
1254 	stub_complete_io(second_bdev->io_target, 1);
1255 
1256 	/* Cleanup; Complete outstanding I/O. */
1257 	stub_complete_io(g_bdev.io_target, AVAIL);
1258 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1259 	/* Complete the ENOMEM I/O */
1260 	stub_complete_io(g_bdev.io_target, 1);
1261 	CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0);
1262 
1263 	SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&bdev_ch->shared_resource->nomem_io));
1264 	CU_ASSERT(bdev_ch->shared_resource->io_outstanding == 0);
1265 	spdk_put_io_channel(io_ch);
1266 	spdk_put_io_channel(second_ch);
1267 	spdk_bdev_close(second_desc);
1268 	unregister_bdev(second_bdev);
1269 	spdk_io_device_unregister(&new_io_device, NULL);
1270 	poll_threads();
1271 	free(second_bdev);
1272 	teardown_test();
1273 }
1274 
1275 static void
1276 qos_dynamic_enable_done(void *cb_arg, int status)
1277 {
1278 	int *rc = cb_arg;
1279 	*rc = status;
1280 }
1281 
1282 static void
1283 qos_dynamic_enable(void)
1284 {
1285 	struct spdk_io_channel *io_ch[2];
1286 	struct spdk_bdev_channel *bdev_ch[2];
1287 	struct spdk_bdev *bdev;
1288 	enum spdk_bdev_io_status bdev_io_status[2];
1289 	uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES] = {};
1290 	int status, second_status, rc, i;
1291 
1292 	setup_test();
1293 	MOCK_SET(spdk_get_ticks, 0);
1294 
1295 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1296 		limits[i] = UINT64_MAX;
1297 	}
1298 
1299 	bdev = &g_bdev.bdev;
1300 
1301 	g_get_io_channel = true;
1302 
1303 	/* Create channels */
1304 	set_thread(0);
1305 	io_ch[0] = spdk_bdev_get_io_channel(g_desc);
1306 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
1307 	CU_ASSERT(bdev_ch[0]->flags == 0);
1308 
1309 	set_thread(1);
1310 	io_ch[1] = spdk_bdev_get_io_channel(g_desc);
1311 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
1312 	CU_ASSERT(bdev_ch[1]->flags == 0);
1313 
1314 	set_thread(0);
1315 
1316 	/*
1317 	 * Enable QoS: Read/Write IOPS, Read/Write byte,
1318 	 * Read only byte and Write only byte per second
1319 	 * rate limits.
1320 	 * More than 10 I/Os allowed per timeslice.
1321 	 */
1322 	status = -1;
1323 	limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000;
1324 	limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 100;
1325 	limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT] = 100;
1326 	limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT] = 10;
1327 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1328 	poll_threads();
1329 	CU_ASSERT(status == 0);
1330 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1331 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1332 
1333 	/*
1334 	 * Submit and complete 10 I/O to fill the QoS allotment for this timeslice.
1335 	 * Additional I/O will then be queued.
1336 	 */
1337 	set_thread(0);
1338 	for (i = 0; i < 10; i++) {
1339 		bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING;
1340 		rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]);
1341 		CU_ASSERT(rc == 0);
1342 		CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING);
1343 		poll_thread(0);
1344 		stub_complete_io(g_bdev.io_target, 0);
1345 		CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS);
1346 	}
1347 
1348 	/*
1349 	 * Send two more I/O.  These I/O will be queued since the current timeslice allotment has been
1350 	 * filled already.  We want to test that when QoS is disabled that these two I/O:
1351 	 *  1) are not aborted
1352 	 *  2) are sent back to their original thread for resubmission
1353 	 */
1354 	bdev_io_status[0] = SPDK_BDEV_IO_STATUS_PENDING;
1355 	rc = spdk_bdev_read_blocks(g_desc, io_ch[0], NULL, 0, 1, io_during_io_done, &bdev_io_status[0]);
1356 	CU_ASSERT(rc == 0);
1357 	CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_PENDING);
1358 	set_thread(1);
1359 	bdev_io_status[1] = SPDK_BDEV_IO_STATUS_PENDING;
1360 	rc = spdk_bdev_read_blocks(g_desc, io_ch[1], NULL, 0, 1, io_during_io_done, &bdev_io_status[1]);
1361 	CU_ASSERT(rc == 0);
1362 	CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING);
1363 	poll_threads();
1364 
1365 	/*
1366 	 * Disable QoS: Read/Write IOPS, Read/Write byte,
1367 	 * Read only byte rate limits
1368 	 */
1369 	status = -1;
1370 	limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0;
1371 	limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 0;
1372 	limits[SPDK_BDEV_QOS_R_BPS_RATE_LIMIT] = 0;
1373 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1374 	poll_threads();
1375 	CU_ASSERT(status == 0);
1376 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1377 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1378 
1379 	/* Disable QoS: Write only Byte per second rate limit */
1380 	status = -1;
1381 	limits[SPDK_BDEV_QOS_W_BPS_RATE_LIMIT] = 0;
1382 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1383 	poll_threads();
1384 	CU_ASSERT(status == 0);
1385 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0);
1386 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0);
1387 
1388 	/*
1389 	 * All I/O should have been resubmitted back on their original thread.  Complete
1390 	 *  all I/O on thread 0, and ensure that only the thread 0 I/O was completed.
1391 	 */
1392 	set_thread(0);
1393 	stub_complete_io(g_bdev.io_target, 0);
1394 	poll_threads();
1395 	CU_ASSERT(bdev_io_status[0] == SPDK_BDEV_IO_STATUS_SUCCESS);
1396 	CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_PENDING);
1397 
1398 	/* Now complete all I/O on thread 1 and ensure the thread 1 I/O was completed. */
1399 	set_thread(1);
1400 	stub_complete_io(g_bdev.io_target, 0);
1401 	poll_threads();
1402 	CU_ASSERT(bdev_io_status[1] == SPDK_BDEV_IO_STATUS_SUCCESS);
1403 
1404 	/* Disable QoS again */
1405 	status = -1;
1406 	limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0;
1407 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1408 	poll_threads();
1409 	CU_ASSERT(status == 0); /* This should succeed */
1410 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0);
1411 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0);
1412 
1413 	/* Enable QoS on thread 0 */
1414 	status = -1;
1415 	limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000;
1416 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1417 	poll_threads();
1418 	CU_ASSERT(status == 0);
1419 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1420 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1421 
1422 	/* Disable QoS on thread 1 */
1423 	set_thread(1);
1424 	status = -1;
1425 	limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 0;
1426 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1427 	/* Don't poll yet. This should leave the channels with QoS enabled */
1428 	CU_ASSERT(status == -1);
1429 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1430 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1431 
1432 	/* Enable QoS. This should immediately fail because the previous disable QoS hasn't completed. */
1433 	second_status = 0;
1434 	limits[SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT] = 10;
1435 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &second_status);
1436 	poll_threads();
1437 	CU_ASSERT(status == 0); /* The disable should succeed */
1438 	CU_ASSERT(second_status < 0); /* The enable should fail */
1439 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) == 0);
1440 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) == 0);
1441 
1442 	/* Enable QoS on thread 1. This should succeed now that the disable has completed. */
1443 	status = -1;
1444 	limits[SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT] = 10000;
1445 	spdk_bdev_set_qos_rate_limits(bdev, limits, qos_dynamic_enable_done, &status);
1446 	poll_threads();
1447 	CU_ASSERT(status == 0);
1448 	CU_ASSERT((bdev_ch[0]->flags & BDEV_CH_QOS_ENABLED) != 0);
1449 	CU_ASSERT((bdev_ch[1]->flags & BDEV_CH_QOS_ENABLED) != 0);
1450 
1451 	/* Tear down the channels */
1452 	set_thread(0);
1453 	spdk_put_io_channel(io_ch[0]);
1454 	set_thread(1);
1455 	spdk_put_io_channel(io_ch[1]);
1456 	poll_threads();
1457 
1458 	set_thread(0);
1459 	teardown_test();
1460 }
1461 
1462 static void
1463 histogram_status_cb(void *cb_arg, int status)
1464 {
1465 	g_status = status;
1466 }
1467 
1468 static void
1469 histogram_data_cb(void *cb_arg, int status, struct spdk_histogram_data *histogram)
1470 {
1471 	g_status = status;
1472 	g_histogram = histogram;
1473 }
1474 
1475 static void
1476 histogram_io_count(void *ctx, uint64_t start, uint64_t end, uint64_t count,
1477 		   uint64_t total, uint64_t so_far)
1478 {
1479 	g_count += count;
1480 }
1481 
1482 static void
1483 bdev_histograms_mt(void)
1484 {
1485 	struct spdk_io_channel *ch[2];
1486 	struct spdk_histogram_data *histogram;
1487 	uint8_t buf[4096];
1488 	int status = false;
1489 	int rc;
1490 
1491 
1492 	setup_test();
1493 
1494 	set_thread(0);
1495 	ch[0] = spdk_bdev_get_io_channel(g_desc);
1496 	CU_ASSERT(ch[0] != NULL);
1497 
1498 	set_thread(1);
1499 	ch[1] = spdk_bdev_get_io_channel(g_desc);
1500 	CU_ASSERT(ch[1] != NULL);
1501 
1502 
1503 	/* Enable histogram */
1504 	spdk_bdev_histogram_enable(&g_bdev.bdev, histogram_status_cb, NULL, true);
1505 	poll_threads();
1506 	CU_ASSERT(g_status == 0);
1507 	CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == true);
1508 
1509 	/* Allocate histogram */
1510 	histogram = spdk_histogram_data_alloc();
1511 
1512 	/* Check if histogram is zeroed */
1513 	spdk_bdev_histogram_get(&g_bdev.bdev, histogram, histogram_data_cb, NULL);
1514 	poll_threads();
1515 	CU_ASSERT(g_status == 0);
1516 	SPDK_CU_ASSERT_FATAL(g_histogram != NULL);
1517 
1518 	g_count = 0;
1519 	spdk_histogram_data_iterate(g_histogram, histogram_io_count, NULL);
1520 
1521 	CU_ASSERT(g_count == 0);
1522 
1523 	set_thread(0);
1524 	rc = spdk_bdev_write_blocks(g_desc, ch[0], &buf, 0, 1, io_during_io_done, &status);
1525 	CU_ASSERT(rc == 0);
1526 
1527 	spdk_delay_us(10);
1528 	stub_complete_io(g_bdev.io_target, 1);
1529 	poll_threads();
1530 	CU_ASSERT(status == true);
1531 
1532 
1533 	set_thread(1);
1534 	rc = spdk_bdev_read_blocks(g_desc, ch[1], &buf, 0, 1, io_during_io_done, &status);
1535 	CU_ASSERT(rc == 0);
1536 
1537 	spdk_delay_us(10);
1538 	stub_complete_io(g_bdev.io_target, 1);
1539 	poll_threads();
1540 	CU_ASSERT(status == true);
1541 
1542 	set_thread(0);
1543 
1544 	/* Check if histogram gathered data from all I/O channels */
1545 	spdk_bdev_histogram_get(&g_bdev.bdev, histogram, histogram_data_cb, NULL);
1546 	poll_threads();
1547 	CU_ASSERT(g_status == 0);
1548 	CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == true);
1549 	SPDK_CU_ASSERT_FATAL(g_histogram != NULL);
1550 
1551 	g_count = 0;
1552 	spdk_histogram_data_iterate(g_histogram, histogram_io_count, NULL);
1553 	CU_ASSERT(g_count == 2);
1554 
1555 	/* Disable histogram */
1556 	spdk_bdev_histogram_enable(&g_bdev.bdev, histogram_status_cb, NULL, false);
1557 	poll_threads();
1558 	CU_ASSERT(g_status == 0);
1559 	CU_ASSERT(g_bdev.bdev.internal.histogram_enabled == false);
1560 
1561 	spdk_histogram_data_free(histogram);
1562 
1563 	/* Tear down the channels */
1564 	set_thread(0);
1565 	spdk_put_io_channel(ch[0]);
1566 	set_thread(1);
1567 	spdk_put_io_channel(ch[1]);
1568 	poll_threads();
1569 	set_thread(0);
1570 	teardown_test();
1571 
1572 }
1573 
1574 struct timeout_io_cb_arg {
1575 	struct iovec iov;
1576 	uint8_t type;
1577 };
1578 
1579 static int
1580 bdev_channel_count_submitted_io(struct spdk_bdev_channel *ch)
1581 {
1582 	struct spdk_bdev_io *bdev_io;
1583 	int n = 0;
1584 
1585 	if (!ch) {
1586 		return -1;
1587 	}
1588 
1589 	TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) {
1590 		n++;
1591 	}
1592 
1593 	return n;
1594 }
1595 
1596 static void
1597 bdev_channel_io_timeout_cb(void *cb_arg, struct spdk_bdev_io *bdev_io)
1598 {
1599 	struct timeout_io_cb_arg *ctx = cb_arg;
1600 
1601 	ctx->type = bdev_io->type;
1602 	ctx->iov.iov_base = bdev_io->iov.iov_base;
1603 	ctx->iov.iov_len = bdev_io->iov.iov_len;
1604 }
1605 
1606 static bool g_io_done;
1607 
1608 static void
1609 io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1610 {
1611 	g_io_done = true;
1612 	spdk_bdev_free_io(bdev_io);
1613 }
1614 
1615 static void
1616 bdev_set_io_timeout_mt(void)
1617 {
1618 	struct spdk_io_channel *ch[3];
1619 	struct spdk_bdev_channel *bdev_ch[3];
1620 	struct timeout_io_cb_arg cb_arg;
1621 
1622 	setup_test();
1623 
1624 	g_bdev.bdev.optimal_io_boundary = 16;
1625 	g_bdev.bdev.split_on_optimal_io_boundary = true;
1626 
1627 	set_thread(0);
1628 	ch[0] = spdk_bdev_get_io_channel(g_desc);
1629 	CU_ASSERT(ch[0] != NULL);
1630 
1631 	set_thread(1);
1632 	ch[1] = spdk_bdev_get_io_channel(g_desc);
1633 	CU_ASSERT(ch[1] != NULL);
1634 
1635 	set_thread(2);
1636 	ch[2] = spdk_bdev_get_io_channel(g_desc);
1637 	CU_ASSERT(ch[2] != NULL);
1638 
1639 	/* Multi-thread mode
1640 	 * 1, Check the poller was registered successfully
1641 	 * 2, Check the timeout IO and ensure the IO was the submitted by user
1642 	 * 3, Check the link int the bdev_ch works right.
1643 	 * 4, Close desc and put io channel during the timeout poller is polling
1644 	 */
1645 
1646 	/* In desc thread set the timeout */
1647 	set_thread(0);
1648 	CU_ASSERT(spdk_bdev_set_timeout(g_desc, 5, bdev_channel_io_timeout_cb, &cb_arg) == 0);
1649 	CU_ASSERT(g_desc->io_timeout_poller != NULL);
1650 	CU_ASSERT(g_desc->cb_fn == bdev_channel_io_timeout_cb);
1651 	CU_ASSERT(g_desc->cb_arg == &cb_arg);
1652 
1653 	/* check the IO submitted list and timeout handler */
1654 	CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[0], (void *)0x2000, 0, 1, io_done, NULL) == 0);
1655 	bdev_ch[0] = spdk_io_channel_get_ctx(ch[0]);
1656 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[0]) == 1);
1657 
1658 	set_thread(1);
1659 	CU_ASSERT(spdk_bdev_write_blocks(g_desc, ch[1], (void *)0x1000, 0, 1, io_done, NULL) == 0);
1660 	bdev_ch[1] = spdk_io_channel_get_ctx(ch[1]);
1661 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[1]) == 1);
1662 
1663 	/* Now test that a single-vector command is split correctly.
1664 	 * Offset 14, length 8, payload 0xF000
1665 	 *  Child - Offset 14, length 2, payload 0xF000
1666 	 *  Child - Offset 16, length 6, payload 0xF000 + 2 * 512
1667 	 *
1668 	 * Set up the expected values before calling spdk_bdev_read_blocks
1669 	 */
1670 	set_thread(2);
1671 	CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[2], (void *)0xF000, 14, 8, io_done, NULL) == 0);
1672 	bdev_ch[2] = spdk_io_channel_get_ctx(ch[2]);
1673 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[2]) == 3);
1674 
1675 	set_thread(0);
1676 	memset(&cb_arg, 0, sizeof(cb_arg));
1677 	spdk_delay_us(3 * spdk_get_ticks_hz());
1678 	poll_threads();
1679 	CU_ASSERT(cb_arg.type == 0);
1680 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0x0);
1681 	CU_ASSERT(cb_arg.iov.iov_len == 0);
1682 
1683 	/* Now the time reach the limit */
1684 	spdk_delay_us(3 * spdk_get_ticks_hz());
1685 	poll_thread(0);
1686 	CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_READ);
1687 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0x2000);
1688 	CU_ASSERT(cb_arg.iov.iov_len == 1 * g_bdev.bdev.blocklen);
1689 	stub_complete_io(g_bdev.io_target, 1);
1690 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[0]) == 0);
1691 
1692 	memset(&cb_arg, 0, sizeof(cb_arg));
1693 	set_thread(1);
1694 	poll_thread(1);
1695 	CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_WRITE);
1696 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0x1000);
1697 	CU_ASSERT(cb_arg.iov.iov_len == 1 * g_bdev.bdev.blocklen);
1698 	stub_complete_io(g_bdev.io_target, 1);
1699 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[1]) == 0);
1700 
1701 	memset(&cb_arg, 0, sizeof(cb_arg));
1702 	set_thread(2);
1703 	poll_thread(2);
1704 	CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_READ);
1705 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0xF000);
1706 	CU_ASSERT(cb_arg.iov.iov_len == 8 * g_bdev.bdev.blocklen);
1707 	stub_complete_io(g_bdev.io_target, 1);
1708 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[2]) == 2);
1709 	stub_complete_io(g_bdev.io_target, 1);
1710 	CU_ASSERT(bdev_channel_count_submitted_io(bdev_ch[2]) == 0);
1711 
1712 	/* Run poll_timeout_done() it means complete the timeout poller */
1713 	set_thread(0);
1714 	poll_thread(0);
1715 	CU_ASSERT(g_desc->refs == 0);
1716 	CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[0], (void *)0x1000, 0, 1, io_done, NULL) == 0);
1717 	set_thread(1);
1718 	CU_ASSERT(spdk_bdev_write_blocks(g_desc, ch[1], (void *)0x2000, 0, 2, io_done, NULL) == 0);
1719 	set_thread(2);
1720 	CU_ASSERT(spdk_bdev_read_blocks(g_desc, ch[2], (void *)0x3000, 0, 3, io_done, NULL) == 0);
1721 
1722 	/* Trigger timeout poller to run again, desc->refs is incremented.
1723 	 * In thread 0 we destroy the io channel before timeout poller runs.
1724 	 * Timeout callback is not called on thread 0.
1725 	 */
1726 	spdk_delay_us(6 * spdk_get_ticks_hz());
1727 	memset(&cb_arg, 0, sizeof(cb_arg));
1728 	set_thread(0);
1729 	stub_complete_io(g_bdev.io_target, 1);
1730 	spdk_put_io_channel(ch[0]);
1731 	poll_thread(0);
1732 	CU_ASSERT(g_desc->refs == 1)
1733 	CU_ASSERT(cb_arg.type == 0);
1734 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0x0);
1735 	CU_ASSERT(cb_arg.iov.iov_len == 0);
1736 
1737 	/* In thread 1 timeout poller runs then we destroy the io channel
1738 	 * Timeout callback is called on thread 1.
1739 	 */
1740 	memset(&cb_arg, 0, sizeof(cb_arg));
1741 	set_thread(1);
1742 	poll_thread(1);
1743 	stub_complete_io(g_bdev.io_target, 1);
1744 	spdk_put_io_channel(ch[1]);
1745 	poll_thread(1);
1746 	CU_ASSERT(cb_arg.type == SPDK_BDEV_IO_TYPE_WRITE);
1747 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0x2000);
1748 	CU_ASSERT(cb_arg.iov.iov_len == 2 * g_bdev.bdev.blocklen);
1749 
1750 	/* Close the desc.
1751 	 * Unregister the timeout poller first.
1752 	 * Then decrement desc->refs but it's not zero yet so desc is not freed.
1753 	 */
1754 	set_thread(0);
1755 	spdk_bdev_close(g_desc);
1756 	CU_ASSERT(g_desc->refs == 1);
1757 	CU_ASSERT(g_desc->io_timeout_poller == NULL);
1758 
1759 	/* Timeout poller runs on thread 2 then we destroy the io channel.
1760 	 * Desc is closed so we would exit the timeout poller directly.
1761 	 * timeout callback is not called on thread 2.
1762 	 */
1763 	memset(&cb_arg, 0, sizeof(cb_arg));
1764 	set_thread(2);
1765 	poll_thread(2);
1766 	stub_complete_io(g_bdev.io_target, 1);
1767 	spdk_put_io_channel(ch[2]);
1768 	poll_thread(2);
1769 	CU_ASSERT(cb_arg.type == 0);
1770 	CU_ASSERT(cb_arg.iov.iov_base == (void *)0x0);
1771 	CU_ASSERT(cb_arg.iov.iov_len == 0);
1772 
1773 	set_thread(0);
1774 	poll_thread(0);
1775 	g_teardown_done = false;
1776 	unregister_bdev(&g_bdev);
1777 	spdk_io_device_unregister(&g_io_device, NULL);
1778 	spdk_bdev_finish(finish_cb, NULL);
1779 	poll_threads();
1780 	memset(&g_bdev, 0, sizeof(g_bdev));
1781 	CU_ASSERT(g_teardown_done == true);
1782 	g_teardown_done = false;
1783 	free_threads();
1784 	free_cores();
1785 }
1786 
1787 static bool g_io_done2;
1788 static bool g_lock_lba_range_done;
1789 static bool g_unlock_lba_range_done;
1790 
1791 static void
1792 io_done2(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1793 {
1794 	g_io_done2 = true;
1795 	spdk_bdev_free_io(bdev_io);
1796 }
1797 
1798 static void
1799 lock_lba_range_done(void *ctx, int status)
1800 {
1801 	g_lock_lba_range_done = true;
1802 }
1803 
1804 static void
1805 unlock_lba_range_done(void *ctx, int status)
1806 {
1807 	g_unlock_lba_range_done = true;
1808 }
1809 
1810 static uint32_t
1811 stub_channel_outstanding_cnt(void *io_target)
1812 {
1813 	struct spdk_io_channel *_ch = spdk_get_io_channel(io_target);
1814 	struct ut_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
1815 	uint32_t outstanding_cnt;
1816 
1817 	outstanding_cnt = ch->outstanding_cnt;
1818 
1819 	spdk_put_io_channel(_ch);
1820 	return outstanding_cnt;
1821 }
1822 
1823 static void
1824 lock_lba_range_then_submit_io(void)
1825 {
1826 	struct spdk_bdev_desc *desc = NULL;
1827 	void *io_target;
1828 	struct spdk_io_channel *io_ch[3];
1829 	struct spdk_bdev_channel *bdev_ch[3];
1830 	struct lba_range *range;
1831 	char buf[4096];
1832 	int ctx0, ctx1, ctx2;
1833 	int rc;
1834 
1835 	setup_test();
1836 
1837 	io_target = g_bdev.io_target;
1838 	desc = g_desc;
1839 
1840 	set_thread(0);
1841 	io_ch[0] = spdk_bdev_get_io_channel(desc);
1842 	bdev_ch[0] = spdk_io_channel_get_ctx(io_ch[0]);
1843 	CU_ASSERT(io_ch[0] != NULL);
1844 
1845 	set_thread(1);
1846 	io_ch[1] = spdk_bdev_get_io_channel(desc);
1847 	bdev_ch[1] = spdk_io_channel_get_ctx(io_ch[1]);
1848 	CU_ASSERT(io_ch[1] != NULL);
1849 
1850 	set_thread(0);
1851 	g_lock_lba_range_done = false;
1852 	rc = bdev_lock_lba_range(desc, io_ch[0], 20, 10, lock_lba_range_done, &ctx0);
1853 	CU_ASSERT(rc == 0);
1854 	poll_threads();
1855 
1856 	/* The lock should immediately become valid, since there are no outstanding
1857 	 * write I/O.
1858 	 */
1859 	CU_ASSERT(g_lock_lba_range_done == true);
1860 	range = TAILQ_FIRST(&bdev_ch[0]->locked_ranges);
1861 	SPDK_CU_ASSERT_FATAL(range != NULL);
1862 	CU_ASSERT(range->offset == 20);
1863 	CU_ASSERT(range->length == 10);
1864 	CU_ASSERT(range->owner_ch == bdev_ch[0]);
1865 
1866 	g_io_done = false;
1867 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked));
1868 	rc = spdk_bdev_read_blocks(desc, io_ch[0], buf, 20, 1, io_done, &ctx0);
1869 	CU_ASSERT(rc == 0);
1870 	CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1);
1871 
1872 	stub_complete_io(io_target, 1);
1873 	poll_threads();
1874 	CU_ASSERT(g_io_done == true);
1875 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked));
1876 
1877 	/* Try a write I/O.  This should actually be allowed to execute, since the channel
1878 	 * holding the lock is submitting the write I/O.
1879 	 */
1880 	g_io_done = false;
1881 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked));
1882 	rc = spdk_bdev_write_blocks(desc, io_ch[0], buf, 20, 1, io_done, &ctx0);
1883 	CU_ASSERT(rc == 0);
1884 	CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1);
1885 
1886 	stub_complete_io(io_target, 1);
1887 	poll_threads();
1888 	CU_ASSERT(g_io_done == true);
1889 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->io_locked));
1890 
1891 	/* Try a write I/O.  This should get queued in the io_locked tailq. */
1892 	set_thread(1);
1893 	g_io_done = false;
1894 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[1]->io_locked));
1895 	rc = spdk_bdev_write_blocks(desc, io_ch[1], buf, 20, 1, io_done, &ctx1);
1896 	CU_ASSERT(rc == 0);
1897 	poll_threads();
1898 	CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 0);
1899 	CU_ASSERT(!TAILQ_EMPTY(&bdev_ch[1]->io_locked));
1900 	CU_ASSERT(g_io_done == false);
1901 
1902 	/* Try to unlock the lba range using thread 1's io_ch.  This should fail. */
1903 	rc = bdev_unlock_lba_range(desc, io_ch[1], 20, 10, unlock_lba_range_done, &ctx1);
1904 	CU_ASSERT(rc == -EINVAL);
1905 
1906 	/* Now create a new channel and submit a write I/O with it.  This should also be queued.
1907 	 * The new channel should inherit the active locks from the bdev's internal list.
1908 	 */
1909 	set_thread(2);
1910 	io_ch[2] = spdk_bdev_get_io_channel(desc);
1911 	bdev_ch[2] = spdk_io_channel_get_ctx(io_ch[2]);
1912 	CU_ASSERT(io_ch[2] != NULL);
1913 
1914 	g_io_done2 = false;
1915 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[2]->io_locked));
1916 	rc = spdk_bdev_write_blocks(desc, io_ch[2], buf, 22, 2, io_done2, &ctx2);
1917 	CU_ASSERT(rc == 0);
1918 	poll_threads();
1919 	CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 0);
1920 	CU_ASSERT(!TAILQ_EMPTY(&bdev_ch[2]->io_locked));
1921 	CU_ASSERT(g_io_done2 == false);
1922 
1923 	set_thread(0);
1924 	rc = bdev_unlock_lba_range(desc, io_ch[0], 20, 10, unlock_lba_range_done, &ctx0);
1925 	CU_ASSERT(rc == 0);
1926 	poll_threads();
1927 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[0]->locked_ranges));
1928 
1929 	/* The LBA range is unlocked, so the write IOs should now have started execution. */
1930 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[1]->io_locked));
1931 	CU_ASSERT(TAILQ_EMPTY(&bdev_ch[2]->io_locked));
1932 
1933 	set_thread(1);
1934 	CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1);
1935 	stub_complete_io(io_target, 1);
1936 	set_thread(2);
1937 	CU_ASSERT(stub_channel_outstanding_cnt(io_target) == 1);
1938 	stub_complete_io(io_target, 1);
1939 
1940 	poll_threads();
1941 	CU_ASSERT(g_io_done == true);
1942 	CU_ASSERT(g_io_done2 == true);
1943 
1944 	/* Tear down the channels */
1945 	set_thread(0);
1946 	spdk_put_io_channel(io_ch[0]);
1947 	set_thread(1);
1948 	spdk_put_io_channel(io_ch[1]);
1949 	set_thread(2);
1950 	spdk_put_io_channel(io_ch[2]);
1951 	poll_threads();
1952 	set_thread(0);
1953 	teardown_test();
1954 }
1955 
1956 int
1957 main(int argc, char **argv)
1958 {
1959 	CU_pSuite	suite = NULL;
1960 	unsigned int	num_failures;
1961 
1962 	CU_set_error_action(CUEA_ABORT);
1963 	CU_initialize_registry();
1964 
1965 	suite = CU_add_suite("bdev", NULL, NULL);
1966 
1967 	CU_ADD_TEST(suite, basic);
1968 	CU_ADD_TEST(suite, unregister_and_close);
1969 	CU_ADD_TEST(suite, basic_qos);
1970 	CU_ADD_TEST(suite, put_channel_during_reset);
1971 	CU_ADD_TEST(suite, aborted_reset);
1972 	CU_ADD_TEST(suite, io_during_reset);
1973 	CU_ADD_TEST(suite, io_during_qos_queue);
1974 	CU_ADD_TEST(suite, io_during_qos_reset);
1975 	CU_ADD_TEST(suite, enomem);
1976 	CU_ADD_TEST(suite, enomem_multi_bdev);
1977 	CU_ADD_TEST(suite, enomem_multi_io_target);
1978 	CU_ADD_TEST(suite, qos_dynamic_enable);
1979 	CU_ADD_TEST(suite, bdev_histograms_mt);
1980 	CU_ADD_TEST(suite, bdev_set_io_timeout_mt);
1981 	CU_ADD_TEST(suite, lock_lba_range_then_submit_io);
1982 
1983 	CU_basic_set_mode(CU_BRM_VERBOSE);
1984 	CU_basic_run_tests();
1985 	num_failures = CU_get_number_of_failures();
1986 	CU_cleanup_registry();
1987 	return num_failures;
1988 }
1989