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