xref: /spdk/lib/bdev/bdev.c (revision fa68a0fdbd11c085a2975699c65a5b7237ccbadb)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 
37 #include "spdk/bdev.h"
38 #include "spdk/conf.h"
39 
40 #include "spdk/env.h"
41 #include "spdk/event.h"
42 #include "spdk/io_channel.h"
43 #include "spdk/likely.h"
44 #include "spdk/queue.h"
45 #include "spdk/nvme_spec.h"
46 #include "spdk/scsi_spec.h"
47 #include "spdk/util.h"
48 
49 #include "spdk_internal/bdev.h"
50 #include "spdk_internal/log.h"
51 #include "spdk/string.h"
52 
53 #ifdef SPDK_CONFIG_VTUNE
54 #include "ittnotify.h"
55 #include "ittnotify_types.h"
56 int __itt_init_ittlib(const char *, __itt_group_id);
57 #endif
58 
59 #define SPDK_BDEV_IO_POOL_SIZE			(64 * 1024)
60 #define SPDK_BDEV_IO_CACHE_SIZE			256
61 #define BUF_SMALL_POOL_SIZE			8192
62 #define BUF_LARGE_POOL_SIZE			1024
63 #define NOMEM_THRESHOLD_COUNT			8
64 #define ZERO_BUFFER_SIZE			0x100000
65 #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC		1000
66 #define SPDK_BDEV_SEC_TO_USEC			1000000ULL
67 #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE	1
68 #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC		10000
69 
70 struct spdk_bdev_mgr {
71 	struct spdk_mempool *bdev_io_pool;
72 
73 	struct spdk_mempool *buf_small_pool;
74 	struct spdk_mempool *buf_large_pool;
75 
76 	void *zero_buffer;
77 
78 	TAILQ_HEAD(, spdk_bdev_module) bdev_modules;
79 
80 	TAILQ_HEAD(, spdk_bdev) bdevs;
81 
82 	bool init_complete;
83 	bool module_init_complete;
84 
85 #ifdef SPDK_CONFIG_VTUNE
86 	__itt_domain	*domain;
87 #endif
88 };
89 
90 static struct spdk_bdev_mgr g_bdev_mgr = {
91 	.bdev_modules = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdev_modules),
92 	.bdevs = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdevs),
93 	.init_complete = false,
94 	.module_init_complete = false,
95 };
96 
97 static spdk_bdev_init_cb	g_init_cb_fn = NULL;
98 static void			*g_init_cb_arg = NULL;
99 
100 static spdk_bdev_fini_cb	g_fini_cb_fn = NULL;
101 static void			*g_fini_cb_arg = NULL;
102 static struct spdk_thread	*g_fini_thread = NULL;
103 
104 struct spdk_bdev_qos {
105 	/** Rate limit, in I/O per second */
106 	uint64_t rate_limit;
107 
108 	/** The channel that all I/O are funneled through */
109 	struct spdk_bdev_channel *ch;
110 
111 	/** The thread on which the poller is running. */
112 	struct spdk_thread *thread;
113 
114 	/** Queue of I/O waiting to be issued. */
115 	bdev_io_tailq_t queued;
116 
117 	/** Maximum allowed IOs to be issued in one timeslice (e.g., 1ms) and
118 	 *  only valid for the master channel which manages the outstanding IOs. */
119 	uint64_t max_ios_per_timeslice;
120 
121 	/** Submitted IO in one timeslice (e.g., 1ms) */
122 	uint64_t io_submitted_this_timeslice;
123 
124 	/** Polller that processes queued I/O commands each time slice. */
125 	struct spdk_poller *poller;
126 };
127 
128 struct spdk_bdev_mgmt_channel {
129 	bdev_io_stailq_t need_buf_small;
130 	bdev_io_stailq_t need_buf_large;
131 
132 	/*
133 	 * Each thread keeps a cache of bdev_io - this allows
134 	 *  bdev threads which are *not* DPDK threads to still
135 	 *  benefit from a per-thread bdev_io cache.  Without
136 	 *  this, non-DPDK threads fetching from the mempool
137 	 *  incur a cmpxchg on get and put.
138 	 */
139 	bdev_io_stailq_t per_thread_cache;
140 	uint32_t	per_thread_cache_count;
141 };
142 
143 /*
144  * Per-module (or per-io_device) channel. Multiple bdevs built on the same io_device
145  * will queue here their IO that awaits retry. It makes it posible to retry sending
146  * IO to one bdev after IO from other bdev completes.
147  */
148 struct spdk_bdev_module_channel {
149 
150 	/* The bdev management channel */
151 	struct spdk_bdev_mgmt_channel *mgmt_ch;
152 
153 	/*
154 	 * Count of I/O submitted to bdev module and waiting for completion.
155 	 * Incremented before submit_request() is called on an spdk_bdev_io.
156 	 */
157 	uint64_t		io_outstanding;
158 
159 	/*
160 	 * Queue of IO awaiting retry because of a previous NOMEM status returned
161 	 *  on this channel.
162 	 */
163 	bdev_io_tailq_t		nomem_io;
164 
165 	/*
166 	 * Threshold which io_outstanding must drop to before retrying nomem_io.
167 	 */
168 	uint64_t		nomem_threshold;
169 
170 	TAILQ_ENTRY(spdk_bdev_module_channel) link;
171 };
172 
173 #define BDEV_CH_RESET_IN_PROGRESS	(1 << 0)
174 #define BDEV_CH_QOS_ENABLED		(1 << 1)
175 
176 struct spdk_bdev_channel {
177 	struct spdk_bdev	*bdev;
178 
179 	/* The channel for the underlying device */
180 	struct spdk_io_channel	*channel;
181 
182 	/* Channel for the bdev module */
183 	struct spdk_bdev_module_channel	*module_ch;
184 
185 	struct spdk_bdev_io_stat stat;
186 
187 	/*
188 	 * Count of I/O submitted through this channel and waiting for completion.
189 	 * Incremented before submit_request() is called on an spdk_bdev_io.
190 	 */
191 	uint64_t		io_outstanding;
192 
193 	bdev_io_tailq_t		queued_resets;
194 
195 	uint32_t		flags;
196 
197 #ifdef SPDK_CONFIG_VTUNE
198 	uint64_t		start_tsc;
199 	uint64_t		interval_tsc;
200 	__itt_string_handle	*handle;
201 #endif
202 
203 };
204 
205 struct spdk_bdev_desc {
206 	struct spdk_bdev		*bdev;
207 	spdk_bdev_remove_cb_t		remove_cb;
208 	void				*remove_ctx;
209 	bool				write;
210 	TAILQ_ENTRY(spdk_bdev_desc)	link;
211 };
212 
213 #define __bdev_to_io_dev(bdev)		(((char *)bdev) + 1)
214 #define __bdev_from_io_dev(io_dev)	((struct spdk_bdev *)(((char *)io_dev) - 1))
215 
216 static void spdk_bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
217 
218 struct spdk_bdev *
219 spdk_bdev_first(void)
220 {
221 	struct spdk_bdev *bdev;
222 
223 	bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs);
224 	if (bdev) {
225 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Starting bdev iteration at %s\n", bdev->name);
226 	}
227 
228 	return bdev;
229 }
230 
231 struct spdk_bdev *
232 spdk_bdev_next(struct spdk_bdev *prev)
233 {
234 	struct spdk_bdev *bdev;
235 
236 	bdev = TAILQ_NEXT(prev, link);
237 	if (bdev) {
238 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Continuing bdev iteration at %s\n", bdev->name);
239 	}
240 
241 	return bdev;
242 }
243 
244 static struct spdk_bdev *
245 _bdev_next_leaf(struct spdk_bdev *bdev)
246 {
247 	while (bdev != NULL) {
248 		if (bdev->claim_module == NULL) {
249 			return bdev;
250 		} else {
251 			bdev = TAILQ_NEXT(bdev, link);
252 		}
253 	}
254 
255 	return bdev;
256 }
257 
258 struct spdk_bdev *
259 spdk_bdev_first_leaf(void)
260 {
261 	struct spdk_bdev *bdev;
262 
263 	bdev = _bdev_next_leaf(TAILQ_FIRST(&g_bdev_mgr.bdevs));
264 
265 	if (bdev) {
266 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Starting bdev iteration at %s\n", bdev->name);
267 	}
268 
269 	return bdev;
270 }
271 
272 struct spdk_bdev *
273 spdk_bdev_next_leaf(struct spdk_bdev *prev)
274 {
275 	struct spdk_bdev *bdev;
276 
277 	bdev = _bdev_next_leaf(TAILQ_NEXT(prev, link));
278 
279 	if (bdev) {
280 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Continuing bdev iteration at %s\n", bdev->name);
281 	}
282 
283 	return bdev;
284 }
285 
286 struct spdk_bdev *
287 spdk_bdev_get_by_name(const char *bdev_name)
288 {
289 	struct spdk_bdev_alias *tmp;
290 	struct spdk_bdev *bdev = spdk_bdev_first();
291 
292 	while (bdev != NULL) {
293 		if (strcmp(bdev_name, bdev->name) == 0) {
294 			return bdev;
295 		}
296 
297 		TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
298 			if (strcmp(bdev_name, tmp->alias) == 0) {
299 				return bdev;
300 			}
301 		}
302 
303 		bdev = spdk_bdev_next(bdev);
304 	}
305 
306 	return NULL;
307 }
308 
309 static void
310 spdk_bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf)
311 {
312 	assert(bdev_io->get_buf_cb != NULL);
313 	assert(buf != NULL);
314 	assert(bdev_io->u.bdev.iovs != NULL);
315 
316 	bdev_io->buf = buf;
317 	bdev_io->u.bdev.iovs[0].iov_base = (void *)((unsigned long)((char *)buf + 512) & ~511UL);
318 	bdev_io->u.bdev.iovs[0].iov_len = bdev_io->buf_len;
319 	bdev_io->get_buf_cb(bdev_io->ch->channel, bdev_io);
320 }
321 
322 static void
323 spdk_bdev_io_put_buf(struct spdk_bdev_io *bdev_io)
324 {
325 	struct spdk_mempool *pool;
326 	struct spdk_bdev_io *tmp;
327 	void *buf;
328 	bdev_io_stailq_t *stailq;
329 	struct spdk_bdev_mgmt_channel *ch;
330 
331 	assert(bdev_io->u.bdev.iovcnt == 1);
332 
333 	buf = bdev_io->buf;
334 	ch = bdev_io->ch->module_ch->mgmt_ch;
335 
336 	if (bdev_io->buf_len <= SPDK_BDEV_SMALL_BUF_MAX_SIZE) {
337 		pool = g_bdev_mgr.buf_small_pool;
338 		stailq = &ch->need_buf_small;
339 	} else {
340 		pool = g_bdev_mgr.buf_large_pool;
341 		stailq = &ch->need_buf_large;
342 	}
343 
344 	if (STAILQ_EMPTY(stailq)) {
345 		spdk_mempool_put(pool, buf);
346 	} else {
347 		tmp = STAILQ_FIRST(stailq);
348 		STAILQ_REMOVE_HEAD(stailq, buf_link);
349 		spdk_bdev_io_set_buf(tmp, buf);
350 	}
351 }
352 
353 void
354 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
355 {
356 	struct spdk_mempool *pool;
357 	bdev_io_stailq_t *stailq;
358 	void *buf = NULL;
359 	struct spdk_bdev_mgmt_channel *mgmt_ch;
360 
361 	assert(cb != NULL);
362 	assert(bdev_io->u.bdev.iovs != NULL);
363 
364 	if (spdk_unlikely(bdev_io->u.bdev.iovs[0].iov_base != NULL)) {
365 		/* Buffer already present */
366 		cb(bdev_io->ch->channel, bdev_io);
367 		return;
368 	}
369 
370 	assert(len <= SPDK_BDEV_LARGE_BUF_MAX_SIZE);
371 	mgmt_ch = bdev_io->ch->module_ch->mgmt_ch;
372 
373 	bdev_io->buf_len = len;
374 	bdev_io->get_buf_cb = cb;
375 	if (len <= SPDK_BDEV_SMALL_BUF_MAX_SIZE) {
376 		pool = g_bdev_mgr.buf_small_pool;
377 		stailq = &mgmt_ch->need_buf_small;
378 	} else {
379 		pool = g_bdev_mgr.buf_large_pool;
380 		stailq = &mgmt_ch->need_buf_large;
381 	}
382 
383 	buf = spdk_mempool_get(pool);
384 
385 	if (!buf) {
386 		STAILQ_INSERT_TAIL(stailq, bdev_io, buf_link);
387 	} else {
388 		spdk_bdev_io_set_buf(bdev_io, buf);
389 	}
390 }
391 
392 static int
393 spdk_bdev_module_get_max_ctx_size(void)
394 {
395 	struct spdk_bdev_module *bdev_module;
396 	int max_bdev_module_size = 0;
397 
398 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, tailq) {
399 		if (bdev_module->get_ctx_size && bdev_module->get_ctx_size() > max_bdev_module_size) {
400 			max_bdev_module_size = bdev_module->get_ctx_size();
401 		}
402 	}
403 
404 	return max_bdev_module_size;
405 }
406 
407 void
408 spdk_bdev_config_text(FILE *fp)
409 {
410 	struct spdk_bdev_module *bdev_module;
411 
412 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, tailq) {
413 		if (bdev_module->config_text) {
414 			bdev_module->config_text(fp);
415 		}
416 	}
417 }
418 
419 void
420 spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w)
421 {
422 	struct spdk_bdev_module *bdev_module;
423 	struct spdk_bdev *bdev;
424 
425 	assert(w != NULL);
426 
427 	spdk_json_write_array_begin(w);
428 
429 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, tailq) {
430 		if (bdev_module->config_json) {
431 			bdev_module->config_json(w);
432 		}
433 	}
434 
435 	TAILQ_FOREACH(bdev, &g_bdev_mgr.bdevs, link) {
436 		spdk_bdev_config_json(bdev, w);
437 	}
438 
439 	spdk_json_write_array_end(w);
440 }
441 
442 static int
443 spdk_bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
444 {
445 	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
446 
447 	STAILQ_INIT(&ch->need_buf_small);
448 	STAILQ_INIT(&ch->need_buf_large);
449 
450 	STAILQ_INIT(&ch->per_thread_cache);
451 	ch->per_thread_cache_count = 0;
452 
453 	return 0;
454 }
455 
456 static void
457 spdk_bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf)
458 {
459 	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
460 	struct spdk_bdev_io *bdev_io;
461 
462 	if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) {
463 		SPDK_ERRLOG("Pending I/O list wasn't empty on channel free\n");
464 	}
465 
466 	while (!STAILQ_EMPTY(&ch->per_thread_cache)) {
467 		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
468 		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, buf_link);
469 		ch->per_thread_cache_count--;
470 		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
471 	}
472 
473 	assert(ch->per_thread_cache_count == 0);
474 }
475 
476 static void
477 spdk_bdev_init_complete(int rc)
478 {
479 	spdk_bdev_init_cb cb_fn = g_init_cb_fn;
480 	void *cb_arg = g_init_cb_arg;
481 	struct spdk_bdev_module *m;
482 
483 	g_bdev_mgr.init_complete = true;
484 	g_init_cb_fn = NULL;
485 	g_init_cb_arg = NULL;
486 
487 	/*
488 	 * For modules that need to know when subsystem init is complete,
489 	 * inform them now.
490 	 */
491 	TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, tailq) {
492 		if (m->init_complete) {
493 			m->init_complete();
494 		}
495 	}
496 
497 	cb_fn(cb_arg, rc);
498 }
499 
500 static void
501 spdk_bdev_module_action_complete(void)
502 {
503 	struct spdk_bdev_module *m;
504 
505 	/*
506 	 * Don't finish bdev subsystem initialization if
507 	 * module pre-initialization is still in progress, or
508 	 * the subsystem been already initialized.
509 	 */
510 	if (!g_bdev_mgr.module_init_complete || g_bdev_mgr.init_complete) {
511 		return;
512 	}
513 
514 	/*
515 	 * Check all bdev modules for inits/examinations in progress. If any
516 	 * exist, return immediately since we cannot finish bdev subsystem
517 	 * initialization until all are completed.
518 	 */
519 	TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, tailq) {
520 		if (m->action_in_progress > 0) {
521 			return;
522 		}
523 	}
524 
525 	/*
526 	 * Modules already finished initialization - now that all
527 	 * the bdev modules have finished their asynchronous I/O
528 	 * processing, the entire bdev layer can be marked as complete.
529 	 */
530 	spdk_bdev_init_complete(0);
531 }
532 
533 static void
534 spdk_bdev_module_action_done(struct spdk_bdev_module *module)
535 {
536 	assert(module->action_in_progress > 0);
537 	module->action_in_progress--;
538 	spdk_bdev_module_action_complete();
539 }
540 
541 void
542 spdk_bdev_module_init_done(struct spdk_bdev_module *module)
543 {
544 	spdk_bdev_module_action_done(module);
545 }
546 
547 void
548 spdk_bdev_module_examine_done(struct spdk_bdev_module *module)
549 {
550 	spdk_bdev_module_action_done(module);
551 }
552 
553 static int
554 spdk_bdev_module_channel_create(void *io_device, void *ctx_buf)
555 {
556 	struct spdk_bdev_module_channel *ch = ctx_buf;
557 	struct spdk_io_channel *mgmt_ch;
558 
559 	ch->io_outstanding = 0;
560 	TAILQ_INIT(&ch->nomem_io);
561 	ch->nomem_threshold = 0;
562 
563 	mgmt_ch = spdk_get_io_channel(&g_bdev_mgr);
564 	if (!mgmt_ch) {
565 		return -1;
566 	}
567 
568 	ch->mgmt_ch = spdk_io_channel_get_ctx(mgmt_ch);
569 
570 	return 0;
571 }
572 
573 static void
574 spdk_bdev_module_channel_destroy(void *io_device, void *ctx_buf)
575 {
576 	struct spdk_bdev_module_channel *ch = ctx_buf;
577 
578 	assert(ch->io_outstanding == 0);
579 	assert(TAILQ_EMPTY(&ch->nomem_io));
580 
581 	spdk_put_io_channel(spdk_io_channel_from_ctx(ch->mgmt_ch));
582 }
583 
584 static int
585 spdk_bdev_modules_init(void)
586 {
587 	struct spdk_bdev_module *module;
588 	int rc = 0;
589 
590 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, tailq) {
591 		spdk_io_device_register(module,
592 					spdk_bdev_module_channel_create,
593 					spdk_bdev_module_channel_destroy,
594 					sizeof(struct spdk_bdev_module_channel));
595 		rc = module->module_init();
596 		if (rc != 0) {
597 			break;
598 		}
599 	}
600 
601 	g_bdev_mgr.module_init_complete = true;
602 	return rc;
603 }
604 void
605 spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg)
606 {
607 	int cache_size;
608 	int rc = 0;
609 	char mempool_name[32];
610 
611 	assert(cb_fn != NULL);
612 
613 	g_init_cb_fn = cb_fn;
614 	g_init_cb_arg = cb_arg;
615 
616 	snprintf(mempool_name, sizeof(mempool_name), "bdev_io_%d", getpid());
617 
618 	g_bdev_mgr.bdev_io_pool = spdk_mempool_create(mempool_name,
619 				  SPDK_BDEV_IO_POOL_SIZE,
620 				  sizeof(struct spdk_bdev_io) +
621 				  spdk_bdev_module_get_max_ctx_size(),
622 				  0,
623 				  SPDK_ENV_SOCKET_ID_ANY);
624 
625 	if (g_bdev_mgr.bdev_io_pool == NULL) {
626 		SPDK_ERRLOG("could not allocate spdk_bdev_io pool\n");
627 		spdk_bdev_init_complete(-1);
628 		return;
629 	}
630 
631 	/**
632 	 * Ensure no more than half of the total buffers end up local caches, by
633 	 *   using spdk_env_get_core_count() to determine how many local caches we need
634 	 *   to account for.
635 	 */
636 	cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_env_get_core_count());
637 	snprintf(mempool_name, sizeof(mempool_name), "buf_small_pool_%d", getpid());
638 
639 	g_bdev_mgr.buf_small_pool = spdk_mempool_create(mempool_name,
640 				    BUF_SMALL_POOL_SIZE,
641 				    SPDK_BDEV_SMALL_BUF_MAX_SIZE + 512,
642 				    cache_size,
643 				    SPDK_ENV_SOCKET_ID_ANY);
644 	if (!g_bdev_mgr.buf_small_pool) {
645 		SPDK_ERRLOG("create rbuf small pool failed\n");
646 		spdk_bdev_init_complete(-1);
647 		return;
648 	}
649 
650 	cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_env_get_core_count());
651 	snprintf(mempool_name, sizeof(mempool_name), "buf_large_pool_%d", getpid());
652 
653 	g_bdev_mgr.buf_large_pool = spdk_mempool_create(mempool_name,
654 				    BUF_LARGE_POOL_SIZE,
655 				    SPDK_BDEV_LARGE_BUF_MAX_SIZE + 512,
656 				    cache_size,
657 				    SPDK_ENV_SOCKET_ID_ANY);
658 	if (!g_bdev_mgr.buf_large_pool) {
659 		SPDK_ERRLOG("create rbuf large pool failed\n");
660 		spdk_bdev_init_complete(-1);
661 		return;
662 	}
663 
664 	g_bdev_mgr.zero_buffer = spdk_dma_zmalloc(ZERO_BUFFER_SIZE, ZERO_BUFFER_SIZE,
665 				 NULL);
666 	if (!g_bdev_mgr.zero_buffer) {
667 		SPDK_ERRLOG("create bdev zero buffer failed\n");
668 		spdk_bdev_init_complete(-1);
669 		return;
670 	}
671 
672 #ifdef SPDK_CONFIG_VTUNE
673 	g_bdev_mgr.domain = __itt_domain_create("spdk_bdev");
674 #endif
675 
676 	spdk_io_device_register(&g_bdev_mgr, spdk_bdev_mgmt_channel_create,
677 				spdk_bdev_mgmt_channel_destroy,
678 				sizeof(struct spdk_bdev_mgmt_channel));
679 
680 	rc = spdk_bdev_modules_init();
681 	if (rc != 0) {
682 		SPDK_ERRLOG("bdev modules init failed\n");
683 		spdk_bdev_init_complete(-1);
684 		return;
685 	}
686 
687 	spdk_bdev_module_action_complete();
688 }
689 
690 static void
691 spdk_bdev_mgr_unregister_cb(void *io_device)
692 {
693 	spdk_bdev_fini_cb cb_fn = g_fini_cb_fn;
694 
695 	if (spdk_mempool_count(g_bdev_mgr.bdev_io_pool) != SPDK_BDEV_IO_POOL_SIZE) {
696 		SPDK_ERRLOG("bdev IO pool count is %zu but should be %u\n",
697 			    spdk_mempool_count(g_bdev_mgr.bdev_io_pool),
698 			    SPDK_BDEV_IO_POOL_SIZE);
699 	}
700 
701 	if (spdk_mempool_count(g_bdev_mgr.buf_small_pool) != BUF_SMALL_POOL_SIZE) {
702 		SPDK_ERRLOG("Small buffer pool count is %zu but should be %u\n",
703 			    spdk_mempool_count(g_bdev_mgr.buf_small_pool),
704 			    BUF_SMALL_POOL_SIZE);
705 		assert(false);
706 	}
707 
708 	if (spdk_mempool_count(g_bdev_mgr.buf_large_pool) != BUF_LARGE_POOL_SIZE) {
709 		SPDK_ERRLOG("Large buffer pool count is %zu but should be %u\n",
710 			    spdk_mempool_count(g_bdev_mgr.buf_large_pool),
711 			    BUF_LARGE_POOL_SIZE);
712 		assert(false);
713 	}
714 
715 	spdk_mempool_free(g_bdev_mgr.bdev_io_pool);
716 	spdk_mempool_free(g_bdev_mgr.buf_small_pool);
717 	spdk_mempool_free(g_bdev_mgr.buf_large_pool);
718 	spdk_dma_free(g_bdev_mgr.zero_buffer);
719 
720 	cb_fn(g_fini_cb_arg);
721 	g_fini_cb_fn = NULL;
722 	g_fini_cb_arg = NULL;
723 }
724 
725 static struct spdk_bdev_module *g_resume_bdev_module = NULL;
726 
727 static void
728 spdk_bdev_module_finish_iter(void *arg)
729 {
730 	struct spdk_bdev_module *bdev_module;
731 
732 	/* Start iterating from the last touched module */
733 	if (!g_resume_bdev_module) {
734 		bdev_module = TAILQ_FIRST(&g_bdev_mgr.bdev_modules);
735 	} else {
736 		bdev_module = TAILQ_NEXT(g_resume_bdev_module, tailq);
737 	}
738 
739 	if (bdev_module) {
740 		/* Save our place so we can resume later. We must
741 		 * save the variable here, before calling module_fini()
742 		 * below, because in some cases the module may immediately
743 		 * call spdk_bdev_module_finish_done() and re-enter
744 		 * this function to continue iterating. */
745 		g_resume_bdev_module = bdev_module;
746 
747 		if (bdev_module->module_fini) {
748 			bdev_module->module_fini();
749 		}
750 
751 		if (!bdev_module->async_fini) {
752 			spdk_bdev_module_finish_done();
753 		}
754 
755 		return;
756 	}
757 
758 	g_resume_bdev_module = NULL;
759 
760 	spdk_io_device_unregister(&g_bdev_mgr, spdk_bdev_mgr_unregister_cb);
761 }
762 
763 static void
764 spdk_bdev_module_unregister_cb(void *io_device)
765 {
766 	if (spdk_get_thread() != g_fini_thread) {
767 		spdk_thread_send_msg(g_fini_thread, spdk_bdev_module_finish_iter, NULL);
768 	} else {
769 		spdk_bdev_module_finish_iter(NULL);
770 	}
771 }
772 
773 void
774 spdk_bdev_module_finish_done(void)
775 {
776 	spdk_io_device_unregister(g_resume_bdev_module, spdk_bdev_module_unregister_cb);
777 }
778 
779 static void
780 _spdk_bdev_finish_unregister_bdevs_iter(void *cb_arg, int bdeverrno)
781 {
782 	struct spdk_bdev *bdev = cb_arg;
783 
784 	if (bdeverrno && bdev) {
785 		SPDK_WARNLOG("Unable to unregister bdev '%s' during spdk_bdev_finish()\n",
786 			     bdev->name);
787 
788 		/*
789 		 * Since the call to spdk_bdev_unregister() failed, we have no way to free this
790 		 *  bdev; try to continue by manually removing this bdev from the list and continue
791 		 *  with the next bdev in the list.
792 		 */
793 		TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, link);
794 	}
795 
796 	if (TAILQ_EMPTY(&g_bdev_mgr.bdevs)) {
797 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Done unregistering bdevs\n");
798 		/*
799 		 * Bdev module finish need to be deffered as we might be in the middle of some context
800 		 * (like bdev part free) that will use this bdev (or private bdev driver ctx data)
801 		 * after returning.
802 		 */
803 		spdk_thread_send_msg(spdk_get_thread(), spdk_bdev_module_finish_iter, NULL);
804 		return;
805 	}
806 
807 	/*
808 	 * Unregister the first bdev in the list.
809 	 *
810 	 * spdk_bdev_unregister() will handle the case where the bdev has open descriptors by
811 	 *  calling the remove_cb of the descriptors first.
812 	 *
813 	 * Once this bdev and all of its open descriptors have been cleaned up, this function
814 	 *  will be called again via the unregister completion callback to continue the cleanup
815 	 *  process with the next bdev.
816 	 */
817 	bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs);
818 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Unregistering bdev '%s'\n", bdev->name);
819 	spdk_bdev_unregister(bdev, _spdk_bdev_finish_unregister_bdevs_iter, bdev);
820 }
821 
822 void
823 spdk_bdev_finish(spdk_bdev_fini_cb cb_fn, void *cb_arg)
824 {
825 	assert(cb_fn != NULL);
826 
827 	g_fini_thread = spdk_get_thread();
828 
829 	g_fini_cb_fn = cb_fn;
830 	g_fini_cb_arg = cb_arg;
831 
832 	_spdk_bdev_finish_unregister_bdevs_iter(NULL, 0);
833 }
834 
835 static struct spdk_bdev_io *
836 spdk_bdev_get_io(struct spdk_bdev_channel *channel)
837 {
838 	struct spdk_bdev_mgmt_channel *ch = channel->module_ch->mgmt_ch;
839 	struct spdk_bdev_io *bdev_io;
840 
841 	if (ch->per_thread_cache_count > 0) {
842 		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
843 		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, buf_link);
844 		ch->per_thread_cache_count--;
845 	} else {
846 		bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
847 		if (!bdev_io) {
848 			SPDK_ERRLOG("Unable to get spdk_bdev_io\n");
849 			return NULL;
850 		}
851 	}
852 
853 	return bdev_io;
854 }
855 
856 static void
857 spdk_bdev_put_io(struct spdk_bdev_io *bdev_io)
858 {
859 	struct spdk_bdev_mgmt_channel *ch = bdev_io->ch->module_ch->mgmt_ch;
860 
861 	if (bdev_io->buf != NULL) {
862 		spdk_bdev_io_put_buf(bdev_io);
863 	}
864 
865 	if (ch->per_thread_cache_count < SPDK_BDEV_IO_CACHE_SIZE) {
866 		ch->per_thread_cache_count++;
867 		STAILQ_INSERT_TAIL(&ch->per_thread_cache, bdev_io, buf_link);
868 	} else {
869 		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
870 	}
871 }
872 
873 static void
874 _spdk_bdev_qos_io_submit(struct spdk_bdev_channel *ch)
875 {
876 	struct spdk_bdev_io		*bdev_io = NULL;
877 	struct spdk_bdev		*bdev = ch->bdev;
878 	struct spdk_bdev_qos		*qos = bdev->qos;
879 	struct spdk_bdev_module_channel *module_ch = ch->module_ch;
880 
881 	while (!TAILQ_EMPTY(&qos->queued)) {
882 		if (qos->io_submitted_this_timeslice < qos->max_ios_per_timeslice) {
883 			bdev_io = TAILQ_FIRST(&qos->queued);
884 			TAILQ_REMOVE(&qos->queued, bdev_io, link);
885 			qos->io_submitted_this_timeslice++;
886 			ch->io_outstanding++;
887 			module_ch->io_outstanding++;
888 			bdev->fn_table->submit_request(ch->channel, bdev_io);
889 		} else {
890 			break;
891 		}
892 	}
893 }
894 
895 static void
896 _spdk_bdev_io_submit(void *ctx)
897 {
898 	struct spdk_bdev_io *bdev_io = ctx;
899 	struct spdk_bdev *bdev = bdev_io->bdev;
900 	struct spdk_bdev_channel *bdev_ch = bdev_io->ch;
901 	struct spdk_io_channel *ch = bdev_ch->channel;
902 	struct spdk_bdev_module_channel	*module_ch = bdev_ch->module_ch;
903 
904 	bdev_io->submit_tsc = spdk_get_ticks();
905 	bdev_ch->io_outstanding++;
906 	module_ch->io_outstanding++;
907 	bdev_io->in_submit_request = true;
908 	if (spdk_likely(bdev_ch->flags == 0)) {
909 		if (spdk_likely(TAILQ_EMPTY(&module_ch->nomem_io))) {
910 			bdev->fn_table->submit_request(ch, bdev_io);
911 		} else {
912 			bdev_ch->io_outstanding--;
913 			module_ch->io_outstanding--;
914 			TAILQ_INSERT_TAIL(&module_ch->nomem_io, bdev_io, link);
915 		}
916 	} else if (bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS) {
917 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
918 	} else if (bdev_ch->flags & BDEV_CH_QOS_ENABLED) {
919 		bdev_ch->io_outstanding--;
920 		module_ch->io_outstanding--;
921 		TAILQ_INSERT_TAIL(&bdev->qos->queued, bdev_io, link);
922 		_spdk_bdev_qos_io_submit(bdev_ch);
923 	} else {
924 		SPDK_ERRLOG("unknown bdev_ch flag %x found\n", bdev_ch->flags);
925 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
926 	}
927 	bdev_io->in_submit_request = false;
928 }
929 
930 static void
931 spdk_bdev_io_submit(struct spdk_bdev_io *bdev_io)
932 {
933 	struct spdk_bdev *bdev = bdev_io->bdev;
934 
935 	assert(bdev_io->status == SPDK_BDEV_IO_STATUS_PENDING);
936 
937 	if (bdev_io->ch->flags & BDEV_CH_QOS_ENABLED) {
938 		bdev_io->io_submit_ch = bdev_io->ch;
939 		bdev_io->ch = bdev->qos->ch;
940 		spdk_thread_send_msg(bdev->qos->thread, _spdk_bdev_io_submit, bdev_io);
941 	} else {
942 		_spdk_bdev_io_submit(bdev_io);
943 	}
944 }
945 
946 static void
947 spdk_bdev_io_submit_reset(struct spdk_bdev_io *bdev_io)
948 {
949 	struct spdk_bdev *bdev = bdev_io->bdev;
950 	struct spdk_bdev_channel *bdev_ch = bdev_io->ch;
951 	struct spdk_io_channel *ch = bdev_ch->channel;
952 
953 	assert(bdev_io->status == SPDK_BDEV_IO_STATUS_PENDING);
954 
955 	bdev_io->in_submit_request = true;
956 	bdev->fn_table->submit_request(ch, bdev_io);
957 	bdev_io->in_submit_request = false;
958 }
959 
960 static void
961 spdk_bdev_io_init(struct spdk_bdev_io *bdev_io,
962 		  struct spdk_bdev *bdev, void *cb_arg,
963 		  spdk_bdev_io_completion_cb cb)
964 {
965 	bdev_io->bdev = bdev;
966 	bdev_io->caller_ctx = cb_arg;
967 	bdev_io->cb = cb;
968 	bdev_io->status = SPDK_BDEV_IO_STATUS_PENDING;
969 	bdev_io->in_submit_request = false;
970 	bdev_io->buf = NULL;
971 	bdev_io->io_submit_ch = NULL;
972 }
973 
974 bool
975 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
976 {
977 	return bdev->fn_table->io_type_supported(bdev->ctxt, io_type);
978 }
979 
980 int
981 spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
982 {
983 	if (bdev->fn_table->dump_info_json) {
984 		return bdev->fn_table->dump_info_json(bdev->ctxt, w);
985 	}
986 
987 	return 0;
988 }
989 
990 void
991 spdk_bdev_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
992 {
993 	assert(bdev != NULL);
994 	assert(w != NULL);
995 
996 	if (bdev->fn_table->write_config_json) {
997 		bdev->fn_table->write_config_json(bdev, w);
998 	} else {
999 		spdk_json_write_object_begin(w);
1000 		spdk_json_write_named_string(w, "name", bdev->name);
1001 		spdk_json_write_object_end(w);
1002 	}
1003 }
1004 
1005 static void
1006 spdk_bdev_qos_update_max_ios_per_timeslice(struct spdk_bdev_qos *qos)
1007 {
1008 	uint64_t max_ios_per_timeslice = 0;
1009 
1010 	max_ios_per_timeslice = qos->rate_limit * SPDK_BDEV_QOS_TIMESLICE_IN_USEC /
1011 				SPDK_BDEV_SEC_TO_USEC;
1012 	qos->max_ios_per_timeslice = spdk_max(max_ios_per_timeslice,
1013 					      SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE);
1014 }
1015 
1016 static int
1017 spdk_bdev_channel_poll_qos(void *arg)
1018 {
1019 	struct spdk_bdev_qos *qos = arg;
1020 
1021 	/* Reset for next round of rate limiting */
1022 	qos->io_submitted_this_timeslice = 0;
1023 
1024 	_spdk_bdev_qos_io_submit(qos->ch);
1025 
1026 	return -1;
1027 }
1028 
1029 static int
1030 _spdk_bdev_channel_create(struct spdk_bdev_channel *ch, void *io_device)
1031 {
1032 	struct spdk_bdev		*bdev = __bdev_from_io_dev(io_device);
1033 
1034 	ch->bdev = bdev;
1035 	ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt);
1036 	if (!ch->channel) {
1037 		return -1;
1038 	}
1039 
1040 	ch->module_ch = spdk_io_channel_get_ctx(spdk_get_io_channel(bdev->module));
1041 
1042 	memset(&ch->stat, 0, sizeof(ch->stat));
1043 	ch->io_outstanding = 0;
1044 	TAILQ_INIT(&ch->queued_resets);
1045 	ch->flags = 0;
1046 
1047 	return 0;
1048 }
1049 
1050 static void
1051 _spdk_bdev_channel_destroy_resource(struct spdk_bdev_channel *ch)
1052 {
1053 	if (!ch) {
1054 		return;
1055 	}
1056 
1057 	if (ch->channel) {
1058 		spdk_put_io_channel(ch->channel);
1059 	}
1060 
1061 	if (ch->module_ch) {
1062 		spdk_put_io_channel(spdk_io_channel_from_ctx(ch->module_ch));
1063 	}
1064 }
1065 
1066 /* Caller must hold bdev->mutex. */
1067 static int
1068 spdk_bdev_qos_channel_create(struct spdk_bdev *bdev)
1069 {
1070 	assert(bdev->qos->ch == NULL);
1071 	assert(bdev->qos->thread == NULL);
1072 
1073 	bdev->qos->ch = calloc(1, sizeof(struct spdk_bdev_channel));
1074 	if (!bdev->qos->ch) {
1075 		return -1;
1076 	}
1077 
1078 	bdev->qos->thread = spdk_get_thread();
1079 	if (!bdev->qos->thread) {
1080 		free(bdev->qos->ch);
1081 		bdev->qos->ch = NULL;
1082 		return -1;
1083 	}
1084 
1085 	if (_spdk_bdev_channel_create(bdev->qos->ch, __bdev_to_io_dev(bdev)) != 0) {
1086 		free(bdev->qos->ch);
1087 		bdev->qos->ch = NULL;
1088 		bdev->qos->thread = NULL;
1089 		return -1;
1090 	}
1091 
1092 	TAILQ_INIT(&bdev->qos->queued);
1093 
1094 	bdev->qos->ch->flags |= BDEV_CH_QOS_ENABLED;
1095 	spdk_bdev_qos_update_max_ios_per_timeslice(bdev->qos);
1096 
1097 	bdev->qos->poller = spdk_poller_register(spdk_bdev_channel_poll_qos,
1098 			    bdev->qos,
1099 			    SPDK_BDEV_QOS_TIMESLICE_IN_USEC);
1100 
1101 	return 0;
1102 }
1103 
1104 /* Caller must hold bdev->mutex */
1105 static int
1106 _spdk_bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch)
1107 {
1108 	/* Rate limiting on this bdev enabled */
1109 	if (bdev->qos) {
1110 		if (bdev->qos->ch == NULL) {
1111 			if (spdk_bdev_qos_channel_create(bdev) != 0) {
1112 				return -1;
1113 			}
1114 		}
1115 		ch->flags |= BDEV_CH_QOS_ENABLED;
1116 	}
1117 
1118 	return 0;
1119 }
1120 
1121 static int
1122 spdk_bdev_channel_create(void *io_device, void *ctx_buf)
1123 {
1124 	struct spdk_bdev		*bdev = __bdev_from_io_dev(io_device);
1125 	struct spdk_bdev_channel	*ch = ctx_buf;
1126 
1127 	if (_spdk_bdev_channel_create(ch, io_device) != 0) {
1128 		_spdk_bdev_channel_destroy_resource(ch);
1129 		return -1;
1130 	}
1131 
1132 #ifdef SPDK_CONFIG_VTUNE
1133 	{
1134 		char *name;
1135 		__itt_init_ittlib(NULL, 0);
1136 		name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch);
1137 		if (!name) {
1138 			_spdk_bdev_channel_destroy_resource(ch);
1139 			return -1;
1140 		}
1141 		ch->handle = __itt_string_handle_create(name);
1142 		free(name);
1143 		ch->start_tsc = spdk_get_ticks();
1144 		ch->interval_tsc = spdk_get_ticks_hz() / 100;
1145 	}
1146 #endif
1147 
1148 	pthread_mutex_lock(&bdev->mutex);
1149 
1150 	if (_spdk_bdev_enable_qos(bdev, ch)) {
1151 		_spdk_bdev_channel_destroy_resource(ch);
1152 		pthread_mutex_unlock(&bdev->mutex);
1153 		return -1;
1154 	}
1155 
1156 	bdev->channel_count++;
1157 
1158 	pthread_mutex_unlock(&bdev->mutex);
1159 
1160 	return 0;
1161 }
1162 
1163 /*
1164  * Abort I/O that are waiting on a data buffer.  These types of I/O are
1165  *  linked using the spdk_bdev_io buf_link TAILQ_ENTRY.
1166  */
1167 static void
1168 _spdk_bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_channel *ch)
1169 {
1170 	bdev_io_stailq_t tmp;
1171 	struct spdk_bdev_io *bdev_io;
1172 
1173 	STAILQ_INIT(&tmp);
1174 
1175 	while (!STAILQ_EMPTY(queue)) {
1176 		bdev_io = STAILQ_FIRST(queue);
1177 		STAILQ_REMOVE_HEAD(queue, buf_link);
1178 		if (bdev_io->ch == ch) {
1179 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
1180 		} else {
1181 			STAILQ_INSERT_TAIL(&tmp, bdev_io, buf_link);
1182 		}
1183 	}
1184 
1185 	STAILQ_SWAP(&tmp, queue, spdk_bdev_io);
1186 }
1187 
1188 /*
1189  * Abort I/O that are queued waiting for submission.  These types of I/O are
1190  *  linked using the spdk_bdev_io link TAILQ_ENTRY.
1191  */
1192 static void
1193 _spdk_bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch)
1194 {
1195 	struct spdk_bdev_io *bdev_io, *tmp;
1196 
1197 	TAILQ_FOREACH_SAFE(bdev_io, queue, link, tmp) {
1198 		if (bdev_io->ch == ch) {
1199 			TAILQ_REMOVE(queue, bdev_io, link);
1200 			/*
1201 			 * spdk_bdev_io_complete() assumes that the completed I/O had
1202 			 *  been submitted to the bdev module.  Since in this case it
1203 			 *  hadn't, bump io_outstanding to account for the decrement
1204 			 *  that spdk_bdev_io_complete() will do.
1205 			 */
1206 			if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) {
1207 				ch->io_outstanding++;
1208 				ch->module_ch->io_outstanding++;
1209 			}
1210 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
1211 		}
1212 	}
1213 }
1214 
1215 static void
1216 _spdk_bdev_channel_destroy(struct spdk_bdev_channel *ch)
1217 {
1218 	struct spdk_bdev_mgmt_channel	*mgmt_ch;
1219 	struct spdk_bdev_module_channel	*module_ch = ch->module_ch;
1220 
1221 	mgmt_ch = module_ch->mgmt_ch;
1222 
1223 	_spdk_bdev_abort_queued_io(&ch->queued_resets, ch);
1224 	_spdk_bdev_abort_queued_io(&module_ch->nomem_io, ch);
1225 	_spdk_bdev_abort_buf_io(&mgmt_ch->need_buf_small, ch);
1226 	_spdk_bdev_abort_buf_io(&mgmt_ch->need_buf_large, ch);
1227 
1228 	_spdk_bdev_channel_destroy_resource(ch);
1229 }
1230 
1231 static void
1232 spdk_bdev_qos_channel_destroy(void *cb_arg)
1233 {
1234 	struct spdk_bdev_qos *qos = cb_arg;
1235 
1236 	_spdk_bdev_channel_destroy(qos->ch);
1237 
1238 	spdk_poller_unregister(&qos->poller);
1239 
1240 	free(qos->ch);
1241 	free(qos);
1242 }
1243 
1244 static void
1245 spdk_bdev_channel_destroy(void *io_device, void *ctx_buf)
1246 {
1247 	struct spdk_bdev_channel	*ch = ctx_buf;
1248 	struct spdk_bdev		*bdev = ch->bdev;
1249 
1250 	_spdk_bdev_channel_destroy(ch);
1251 
1252 	pthread_mutex_lock(&bdev->mutex);
1253 	bdev->channel_count--;
1254 	if (bdev->channel_count == 0 && bdev->qos && bdev->qos->ch != NULL) {
1255 		/*
1256 		 * Cleanly shutting down the QoS poller is tricky, because
1257 		 * during the asynchronous operation the user could open a
1258 		 * new channel, spawning a new QoS poller.
1259 		 *
1260 		 * The strategy is to create a new QoS structure here and swap it
1261 		 * in. The shutdown path then continues to refer to the old one
1262 		 * until it completes and then releases it.
1263 		 */
1264 		struct spdk_bdev_qos *new_qos, *old_qos;
1265 
1266 		old_qos = bdev->qos;
1267 
1268 		new_qos = calloc(1, sizeof(*new_qos));
1269 		if (!new_qos) {
1270 			SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n");
1271 			/* There isn't anything we can do to recover from here. Just let the
1272 			 * old QoS poller keep running. The QoS handling won't change
1273 			 * cores when the user allocates a new channel, but it won't break. */
1274 		} else {
1275 			/* Copy the old QoS data into the newly allocated structure */
1276 			memcpy(new_qos, old_qos, sizeof(*new_qos));
1277 
1278 			/* Zero out the key parts of the QoS structure */
1279 			new_qos->ch = NULL;
1280 			new_qos->thread = NULL;
1281 			new_qos->max_ios_per_timeslice = 0;
1282 			new_qos->io_submitted_this_timeslice = 0;
1283 			new_qos->poller = NULL;
1284 			TAILQ_INIT(&new_qos->queued);
1285 
1286 			bdev->qos = new_qos;
1287 
1288 			spdk_thread_send_msg(old_qos->thread, spdk_bdev_qos_channel_destroy,
1289 					     old_qos);
1290 
1291 			/* It is safe to continue with destroying the bdev even though the QoS channel hasn't
1292 			 * been destroyed yet. The destruction path will end up waiting for the final
1293 			 * channel to be put before it releases resources. */
1294 		}
1295 	}
1296 	pthread_mutex_unlock(&bdev->mutex);
1297 }
1298 
1299 int
1300 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
1301 {
1302 	struct spdk_bdev_alias *tmp;
1303 
1304 	if (alias == NULL) {
1305 		SPDK_ERRLOG("Empty alias passed\n");
1306 		return -EINVAL;
1307 	}
1308 
1309 	if (spdk_bdev_get_by_name(alias)) {
1310 		SPDK_ERRLOG("Bdev name/alias: %s already exists\n", alias);
1311 		return -EEXIST;
1312 	}
1313 
1314 	tmp = calloc(1, sizeof(*tmp));
1315 	if (tmp == NULL) {
1316 		SPDK_ERRLOG("Unable to allocate alias\n");
1317 		return -ENOMEM;
1318 	}
1319 
1320 	tmp->alias = strdup(alias);
1321 	if (tmp->alias == NULL) {
1322 		free(tmp);
1323 		SPDK_ERRLOG("Unable to allocate alias\n");
1324 		return -ENOMEM;
1325 	}
1326 
1327 	TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq);
1328 
1329 	return 0;
1330 }
1331 
1332 int
1333 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias)
1334 {
1335 	struct spdk_bdev_alias *tmp;
1336 
1337 	TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
1338 		if (strcmp(alias, tmp->alias) == 0) {
1339 			TAILQ_REMOVE(&bdev->aliases, tmp, tailq);
1340 			free(tmp->alias);
1341 			free(tmp);
1342 			return 0;
1343 		}
1344 	}
1345 
1346 	SPDK_INFOLOG(SPDK_LOG_BDEV, "Alias %s does not exists\n", alias);
1347 
1348 	return -ENOENT;
1349 }
1350 
1351 struct spdk_io_channel *
1352 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
1353 {
1354 	return spdk_get_io_channel(__bdev_to_io_dev(desc->bdev));
1355 }
1356 
1357 const char *
1358 spdk_bdev_get_name(const struct spdk_bdev *bdev)
1359 {
1360 	return bdev->name;
1361 }
1362 
1363 const char *
1364 spdk_bdev_get_product_name(const struct spdk_bdev *bdev)
1365 {
1366 	return bdev->product_name;
1367 }
1368 
1369 const struct spdk_bdev_aliases_list *
1370 spdk_bdev_get_aliases(const struct spdk_bdev *bdev)
1371 {
1372 	return &bdev->aliases;
1373 }
1374 
1375 uint32_t
1376 spdk_bdev_get_block_size(const struct spdk_bdev *bdev)
1377 {
1378 	return bdev->blocklen;
1379 }
1380 
1381 uint64_t
1382 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
1383 {
1384 	return bdev->blockcnt;
1385 }
1386 
1387 uint64_t
1388 spdk_bdev_get_qos_ios_per_sec(struct spdk_bdev *bdev)
1389 {
1390 	uint64_t rate_limit = 0;
1391 
1392 	pthread_mutex_lock(&bdev->mutex);
1393 	if (bdev->qos) {
1394 		rate_limit = bdev->qos->rate_limit;
1395 	}
1396 	pthread_mutex_unlock(&bdev->mutex);
1397 
1398 	return rate_limit;
1399 }
1400 
1401 size_t
1402 spdk_bdev_get_buf_align(const struct spdk_bdev *bdev)
1403 {
1404 	/* TODO: push this logic down to the bdev modules */
1405 	if (bdev->need_aligned_buffer) {
1406 		return bdev->blocklen;
1407 	}
1408 
1409 	return 1;
1410 }
1411 
1412 uint32_t
1413 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev)
1414 {
1415 	return bdev->optimal_io_boundary;
1416 }
1417 
1418 bool
1419 spdk_bdev_has_write_cache(const struct spdk_bdev *bdev)
1420 {
1421 	return bdev->write_cache;
1422 }
1423 
1424 const struct spdk_uuid *
1425 spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
1426 {
1427 	return &bdev->uuid;
1428 }
1429 
1430 int
1431 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
1432 {
1433 	int ret;
1434 
1435 	pthread_mutex_lock(&bdev->mutex);
1436 
1437 	/* bdev has open descriptors */
1438 	if (!TAILQ_EMPTY(&bdev->open_descs) &&
1439 	    bdev->blockcnt > size) {
1440 		ret = -EBUSY;
1441 	} else {
1442 		bdev->blockcnt = size;
1443 		ret = 0;
1444 	}
1445 
1446 	pthread_mutex_unlock(&bdev->mutex);
1447 
1448 	return ret;
1449 }
1450 
1451 /*
1452  * Convert I/O offset and length from bytes to blocks.
1453  *
1454  * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size.
1455  */
1456 static uint64_t
1457 spdk_bdev_bytes_to_blocks(struct spdk_bdev *bdev, uint64_t offset_bytes, uint64_t *offset_blocks,
1458 			  uint64_t num_bytes, uint64_t *num_blocks)
1459 {
1460 	uint32_t block_size = bdev->blocklen;
1461 
1462 	*offset_blocks = offset_bytes / block_size;
1463 	*num_blocks = num_bytes / block_size;
1464 
1465 	return (offset_bytes % block_size) | (num_bytes % block_size);
1466 }
1467 
1468 static bool
1469 spdk_bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks)
1470 {
1471 	/* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there
1472 	 * has been an overflow and hence the offset has been wrapped around */
1473 	if (offset_blocks + num_blocks < offset_blocks) {
1474 		return false;
1475 	}
1476 
1477 	/* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */
1478 	if (offset_blocks + num_blocks > bdev->blockcnt) {
1479 		return false;
1480 	}
1481 
1482 	return true;
1483 }
1484 
1485 int
1486 spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1487 	       void *buf, uint64_t offset, uint64_t nbytes,
1488 	       spdk_bdev_io_completion_cb cb, void *cb_arg)
1489 {
1490 	uint64_t offset_blocks, num_blocks;
1491 
1492 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
1493 		return -EINVAL;
1494 	}
1495 
1496 	return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
1497 }
1498 
1499 int
1500 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1501 		      void *buf, uint64_t offset_blocks, uint64_t num_blocks,
1502 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
1503 {
1504 	struct spdk_bdev *bdev = desc->bdev;
1505 	struct spdk_bdev_io *bdev_io;
1506 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1507 
1508 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1509 		return -EINVAL;
1510 	}
1511 
1512 	bdev_io = spdk_bdev_get_io(channel);
1513 	if (!bdev_io) {
1514 		SPDK_ERRLOG("spdk_bdev_io memory allocation failed duing read\n");
1515 		return -ENOMEM;
1516 	}
1517 
1518 	bdev_io->ch = channel;
1519 	bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
1520 	bdev_io->u.bdev.iov.iov_base = buf;
1521 	bdev_io->u.bdev.iov.iov_len = num_blocks * bdev->blocklen;
1522 	bdev_io->u.bdev.iovs = &bdev_io->u.bdev.iov;
1523 	bdev_io->u.bdev.iovcnt = 1;
1524 	bdev_io->u.bdev.num_blocks = num_blocks;
1525 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1526 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1527 
1528 	spdk_bdev_io_submit(bdev_io);
1529 	return 0;
1530 }
1531 
1532 int
1533 spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1534 		struct iovec *iov, int iovcnt,
1535 		uint64_t offset, uint64_t nbytes,
1536 		spdk_bdev_io_completion_cb cb, void *cb_arg)
1537 {
1538 	uint64_t offset_blocks, num_blocks;
1539 
1540 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
1541 		return -EINVAL;
1542 	}
1543 
1544 	return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
1545 }
1546 
1547 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1548 			   struct iovec *iov, int iovcnt,
1549 			   uint64_t offset_blocks, uint64_t num_blocks,
1550 			   spdk_bdev_io_completion_cb cb, void *cb_arg)
1551 {
1552 	struct spdk_bdev *bdev = desc->bdev;
1553 	struct spdk_bdev_io *bdev_io;
1554 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1555 
1556 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1557 		return -EINVAL;
1558 	}
1559 
1560 	bdev_io = spdk_bdev_get_io(channel);
1561 	if (!bdev_io) {
1562 		SPDK_ERRLOG("spdk_bdev_io memory allocation failed duing read\n");
1563 		return -ENOMEM;
1564 	}
1565 
1566 	bdev_io->ch = channel;
1567 	bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
1568 	bdev_io->u.bdev.iovs = iov;
1569 	bdev_io->u.bdev.iovcnt = iovcnt;
1570 	bdev_io->u.bdev.num_blocks = num_blocks;
1571 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1572 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1573 
1574 	spdk_bdev_io_submit(bdev_io);
1575 	return 0;
1576 }
1577 
1578 int
1579 spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1580 		void *buf, uint64_t offset, uint64_t nbytes,
1581 		spdk_bdev_io_completion_cb cb, void *cb_arg)
1582 {
1583 	uint64_t offset_blocks, num_blocks;
1584 
1585 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
1586 		return -EINVAL;
1587 	}
1588 
1589 	return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
1590 }
1591 
1592 int
1593 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1594 		       void *buf, uint64_t offset_blocks, uint64_t num_blocks,
1595 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
1596 {
1597 	struct spdk_bdev *bdev = desc->bdev;
1598 	struct spdk_bdev_io *bdev_io;
1599 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1600 
1601 	if (!desc->write) {
1602 		return -EBADF;
1603 	}
1604 
1605 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1606 		return -EINVAL;
1607 	}
1608 
1609 	bdev_io = spdk_bdev_get_io(channel);
1610 	if (!bdev_io) {
1611 		SPDK_ERRLOG("bdev_io memory allocation failed duing write\n");
1612 		return -ENOMEM;
1613 	}
1614 
1615 	bdev_io->ch = channel;
1616 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
1617 	bdev_io->u.bdev.iov.iov_base = buf;
1618 	bdev_io->u.bdev.iov.iov_len = num_blocks * bdev->blocklen;
1619 	bdev_io->u.bdev.iovs = &bdev_io->u.bdev.iov;
1620 	bdev_io->u.bdev.iovcnt = 1;
1621 	bdev_io->u.bdev.num_blocks = num_blocks;
1622 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1623 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1624 
1625 	spdk_bdev_io_submit(bdev_io);
1626 	return 0;
1627 }
1628 
1629 int
1630 spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1631 		 struct iovec *iov, int iovcnt,
1632 		 uint64_t offset, uint64_t len,
1633 		 spdk_bdev_io_completion_cb cb, void *cb_arg)
1634 {
1635 	uint64_t offset_blocks, num_blocks;
1636 
1637 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, len, &num_blocks) != 0) {
1638 		return -EINVAL;
1639 	}
1640 
1641 	return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
1642 }
1643 
1644 int
1645 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1646 			struct iovec *iov, int iovcnt,
1647 			uint64_t offset_blocks, uint64_t num_blocks,
1648 			spdk_bdev_io_completion_cb cb, void *cb_arg)
1649 {
1650 	struct spdk_bdev *bdev = desc->bdev;
1651 	struct spdk_bdev_io *bdev_io;
1652 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1653 
1654 	if (!desc->write) {
1655 		return -EBADF;
1656 	}
1657 
1658 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1659 		return -EINVAL;
1660 	}
1661 
1662 	bdev_io = spdk_bdev_get_io(channel);
1663 	if (!bdev_io) {
1664 		SPDK_ERRLOG("bdev_io memory allocation failed duing writev\n");
1665 		return -ENOMEM;
1666 	}
1667 
1668 	bdev_io->ch = channel;
1669 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
1670 	bdev_io->u.bdev.iovs = iov;
1671 	bdev_io->u.bdev.iovcnt = iovcnt;
1672 	bdev_io->u.bdev.num_blocks = num_blocks;
1673 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1674 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1675 
1676 	spdk_bdev_io_submit(bdev_io);
1677 	return 0;
1678 }
1679 
1680 int
1681 spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1682 		       uint64_t offset, uint64_t len,
1683 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
1684 {
1685 	uint64_t offset_blocks, num_blocks;
1686 
1687 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, len, &num_blocks) != 0) {
1688 		return -EINVAL;
1689 	}
1690 
1691 	return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
1692 }
1693 
1694 int
1695 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1696 			      uint64_t offset_blocks, uint64_t num_blocks,
1697 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
1698 {
1699 	struct spdk_bdev *bdev = desc->bdev;
1700 	struct spdk_bdev_io *bdev_io;
1701 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1702 	uint64_t len;
1703 	bool split_request = false;
1704 
1705 	if (num_blocks > UINT64_MAX / spdk_bdev_get_block_size(bdev)) {
1706 		SPDK_ERRLOG("length argument out of range in write_zeroes\n");
1707 		return -ERANGE;
1708 	}
1709 
1710 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1711 		return -EINVAL;
1712 	}
1713 
1714 	bdev_io = spdk_bdev_get_io(channel);
1715 
1716 	if (!bdev_io) {
1717 		SPDK_ERRLOG("bdev_io memory allocation failed duing write_zeroes\n");
1718 		return -ENOMEM;
1719 	}
1720 
1721 	bdev_io->ch = channel;
1722 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1723 
1724 	if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
1725 		bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES;
1726 		bdev_io->u.bdev.num_blocks = num_blocks;
1727 		bdev_io->u.bdev.iovs = NULL;
1728 		bdev_io->u.bdev.iovcnt = 0;
1729 
1730 	} else {
1731 		assert(spdk_bdev_get_block_size(bdev) <= ZERO_BUFFER_SIZE);
1732 
1733 		len = spdk_bdev_get_block_size(bdev) * num_blocks;
1734 
1735 		if (len > ZERO_BUFFER_SIZE) {
1736 			split_request = true;
1737 			len = ZERO_BUFFER_SIZE;
1738 		}
1739 
1740 		bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
1741 		bdev_io->u.bdev.iov.iov_base = g_bdev_mgr.zero_buffer;
1742 		bdev_io->u.bdev.iov.iov_len = len;
1743 		bdev_io->u.bdev.iovs = &bdev_io->u.bdev.iov;
1744 		bdev_io->u.bdev.iovcnt = 1;
1745 		bdev_io->u.bdev.num_blocks = len / spdk_bdev_get_block_size(bdev);
1746 		bdev_io->u.bdev.split_remaining_num_blocks = num_blocks - bdev_io->u.bdev.num_blocks;
1747 		bdev_io->u.bdev.split_current_offset_blocks = offset_blocks + bdev_io->u.bdev.num_blocks;
1748 	}
1749 
1750 	if (split_request) {
1751 		bdev_io->u.bdev.stored_user_cb = cb;
1752 		spdk_bdev_io_init(bdev_io, bdev, cb_arg, spdk_bdev_write_zeroes_split);
1753 	} else {
1754 		spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1755 	}
1756 	spdk_bdev_io_submit(bdev_io);
1757 	return 0;
1758 }
1759 
1760 int
1761 spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1762 		uint64_t offset, uint64_t nbytes,
1763 		spdk_bdev_io_completion_cb cb, void *cb_arg)
1764 {
1765 	uint64_t offset_blocks, num_blocks;
1766 
1767 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
1768 		return -EINVAL;
1769 	}
1770 
1771 	return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
1772 }
1773 
1774 int
1775 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1776 		       uint64_t offset_blocks, uint64_t num_blocks,
1777 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
1778 {
1779 	struct spdk_bdev *bdev = desc->bdev;
1780 	struct spdk_bdev_io *bdev_io;
1781 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1782 
1783 	if (!desc->write) {
1784 		return -EBADF;
1785 	}
1786 
1787 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1788 		return -EINVAL;
1789 	}
1790 
1791 	if (num_blocks == 0) {
1792 		SPDK_ERRLOG("Can't unmap 0 bytes\n");
1793 		return -EINVAL;
1794 	}
1795 
1796 	bdev_io = spdk_bdev_get_io(channel);
1797 	if (!bdev_io) {
1798 		SPDK_ERRLOG("bdev_io memory allocation failed duing unmap\n");
1799 		return -ENOMEM;
1800 	}
1801 
1802 	bdev_io->ch = channel;
1803 	bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP;
1804 	bdev_io->u.bdev.iov.iov_base = NULL;
1805 	bdev_io->u.bdev.iov.iov_len = 0;
1806 	bdev_io->u.bdev.iovs = &bdev_io->u.bdev.iov;
1807 	bdev_io->u.bdev.iovcnt = 1;
1808 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1809 	bdev_io->u.bdev.num_blocks = num_blocks;
1810 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1811 
1812 	spdk_bdev_io_submit(bdev_io);
1813 	return 0;
1814 }
1815 
1816 int
1817 spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1818 		uint64_t offset, uint64_t length,
1819 		spdk_bdev_io_completion_cb cb, void *cb_arg)
1820 {
1821 	uint64_t offset_blocks, num_blocks;
1822 
1823 	if (spdk_bdev_bytes_to_blocks(desc->bdev, offset, &offset_blocks, length, &num_blocks) != 0) {
1824 		return -EINVAL;
1825 	}
1826 
1827 	return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
1828 }
1829 
1830 int
1831 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1832 		       uint64_t offset_blocks, uint64_t num_blocks,
1833 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
1834 {
1835 	struct spdk_bdev *bdev = desc->bdev;
1836 	struct spdk_bdev_io *bdev_io;
1837 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1838 
1839 	if (!desc->write) {
1840 		return -EBADF;
1841 	}
1842 
1843 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
1844 		return -EINVAL;
1845 	}
1846 
1847 	bdev_io = spdk_bdev_get_io(channel);
1848 	if (!bdev_io) {
1849 		SPDK_ERRLOG("bdev_io memory allocation failed duing flush\n");
1850 		return -ENOMEM;
1851 	}
1852 
1853 	bdev_io->ch = channel;
1854 	bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH;
1855 	bdev_io->u.bdev.iovs = NULL;
1856 	bdev_io->u.bdev.iovcnt = 0;
1857 	bdev_io->u.bdev.offset_blocks = offset_blocks;
1858 	bdev_io->u.bdev.num_blocks = num_blocks;
1859 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1860 
1861 	spdk_bdev_io_submit(bdev_io);
1862 	return 0;
1863 }
1864 
1865 static void
1866 _spdk_bdev_reset_dev(struct spdk_io_channel_iter *i, int status)
1867 {
1868 	struct spdk_bdev_channel *ch = spdk_io_channel_iter_get_ctx(i);
1869 	struct spdk_bdev_io *bdev_io;
1870 
1871 	bdev_io = TAILQ_FIRST(&ch->queued_resets);
1872 	TAILQ_REMOVE(&ch->queued_resets, bdev_io, link);
1873 	spdk_bdev_io_submit_reset(bdev_io);
1874 }
1875 
1876 static void
1877 _spdk_bdev_reset_freeze_channel(struct spdk_io_channel_iter *i)
1878 {
1879 	struct spdk_io_channel		*ch;
1880 	struct spdk_bdev_channel	*channel;
1881 	struct spdk_bdev_mgmt_channel	*mgmt_channel;
1882 	struct spdk_bdev_module_channel	*module_ch;
1883 
1884 	ch = spdk_io_channel_iter_get_channel(i);
1885 	channel = spdk_io_channel_get_ctx(ch);
1886 	module_ch = channel->module_ch;
1887 	mgmt_channel = module_ch->mgmt_ch;
1888 
1889 	channel->flags |= BDEV_CH_RESET_IN_PROGRESS;
1890 
1891 	_spdk_bdev_abort_queued_io(&module_ch->nomem_io, channel);
1892 	_spdk_bdev_abort_buf_io(&mgmt_channel->need_buf_small, channel);
1893 	_spdk_bdev_abort_buf_io(&mgmt_channel->need_buf_large, channel);
1894 
1895 	spdk_for_each_channel_continue(i, 0);
1896 }
1897 
1898 static void
1899 _spdk_bdev_reset_freeze_qos_channel(void *ctx)
1900 {
1901 	struct spdk_bdev		*bdev = ctx;
1902 	struct spdk_bdev_mgmt_channel	*mgmt_channel = NULL;
1903 	struct spdk_bdev_channel	*qos_channel = bdev->qos->ch;
1904 	struct spdk_bdev_module_channel	*module_ch = NULL;
1905 
1906 	if (qos_channel) {
1907 		module_ch = qos_channel->module_ch;
1908 		mgmt_channel = module_ch->mgmt_ch;
1909 
1910 		qos_channel->flags |= BDEV_CH_RESET_IN_PROGRESS;
1911 
1912 		_spdk_bdev_abort_queued_io(&module_ch->nomem_io, qos_channel);
1913 		_spdk_bdev_abort_queued_io(&bdev->qos->queued, qos_channel);
1914 		_spdk_bdev_abort_buf_io(&mgmt_channel->need_buf_small, qos_channel);
1915 		_spdk_bdev_abort_buf_io(&mgmt_channel->need_buf_large, qos_channel);
1916 	}
1917 }
1918 
1919 static void
1920 _spdk_bdev_start_reset(void *ctx)
1921 {
1922 	struct spdk_bdev_channel *ch = ctx;
1923 
1924 	spdk_for_each_channel(__bdev_to_io_dev(ch->bdev), _spdk_bdev_reset_freeze_channel,
1925 			      ch, _spdk_bdev_reset_dev);
1926 }
1927 
1928 static void
1929 _spdk_bdev_channel_start_reset(struct spdk_bdev_channel *ch)
1930 {
1931 	struct spdk_bdev *bdev = ch->bdev;
1932 
1933 	assert(!TAILQ_EMPTY(&ch->queued_resets));
1934 
1935 	pthread_mutex_lock(&bdev->mutex);
1936 	if (bdev->reset_in_progress == NULL) {
1937 		bdev->reset_in_progress = TAILQ_FIRST(&ch->queued_resets);
1938 		/*
1939 		 * Take a channel reference for the target bdev for the life of this
1940 		 *  reset.  This guards against the channel getting destroyed while
1941 		 *  spdk_for_each_channel() calls related to this reset IO are in
1942 		 *  progress.  We will release the reference when this reset is
1943 		 *  completed.
1944 		 */
1945 		bdev->reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev));
1946 		_spdk_bdev_start_reset(ch);
1947 	}
1948 	pthread_mutex_unlock(&bdev->mutex);
1949 }
1950 
1951 int
1952 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
1953 		spdk_bdev_io_completion_cb cb, void *cb_arg)
1954 {
1955 	struct spdk_bdev *bdev = desc->bdev;
1956 	struct spdk_bdev_io *bdev_io;
1957 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1958 
1959 	bdev_io = spdk_bdev_get_io(channel);
1960 	if (!bdev_io) {
1961 		SPDK_ERRLOG("bdev_io memory allocation failed duing reset\n");
1962 		return -ENOMEM;
1963 	}
1964 
1965 	bdev_io->ch = channel;
1966 	bdev_io->type = SPDK_BDEV_IO_TYPE_RESET;
1967 	bdev_io->u.reset.ch_ref = NULL;
1968 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
1969 
1970 	pthread_mutex_lock(&bdev->mutex);
1971 	TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, link);
1972 	pthread_mutex_unlock(&bdev->mutex);
1973 
1974 	_spdk_bdev_channel_start_reset(channel);
1975 
1976 	/* Explicitly handle the QoS bdev channel as no IO channel associated */
1977 	if (bdev->qos && bdev->qos->thread) {
1978 		spdk_thread_send_msg(bdev->qos->thread,
1979 				     _spdk_bdev_reset_freeze_qos_channel, bdev);
1980 	}
1981 
1982 	return 0;
1983 }
1984 
1985 void
1986 spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
1987 		      struct spdk_bdev_io_stat *stat)
1988 {
1989 #ifdef SPDK_CONFIG_VTUNE
1990 	SPDK_ERRLOG("Calling spdk_bdev_get_io_stat is not allowed when VTune integration is enabled.\n");
1991 	memset(stat, 0, sizeof(*stat));
1992 	return;
1993 #endif
1994 
1995 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
1996 
1997 	channel->stat.ticks_rate = spdk_get_ticks_hz();
1998 	*stat = channel->stat;
1999 	memset(&channel->stat, 0, sizeof(channel->stat));
2000 }
2001 
2002 int
2003 spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2004 			      const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
2005 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
2006 {
2007 	struct spdk_bdev *bdev = desc->bdev;
2008 	struct spdk_bdev_io *bdev_io;
2009 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2010 
2011 	if (!desc->write) {
2012 		return -EBADF;
2013 	}
2014 
2015 	bdev_io = spdk_bdev_get_io(channel);
2016 	if (!bdev_io) {
2017 		SPDK_ERRLOG("bdev_io memory allocation failed during nvme_admin_passthru\n");
2018 		return -ENOMEM;
2019 	}
2020 
2021 	bdev_io->ch = channel;
2022 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN;
2023 	bdev_io->u.nvme_passthru.cmd = *cmd;
2024 	bdev_io->u.nvme_passthru.buf = buf;
2025 	bdev_io->u.nvme_passthru.nbytes = nbytes;
2026 	bdev_io->u.nvme_passthru.md_buf = NULL;
2027 	bdev_io->u.nvme_passthru.md_len = 0;
2028 
2029 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2030 
2031 	spdk_bdev_io_submit(bdev_io);
2032 	return 0;
2033 }
2034 
2035 int
2036 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2037 			   const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
2038 			   spdk_bdev_io_completion_cb cb, void *cb_arg)
2039 {
2040 	struct spdk_bdev *bdev = desc->bdev;
2041 	struct spdk_bdev_io *bdev_io;
2042 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2043 
2044 	if (!desc->write) {
2045 		/*
2046 		 * Do not try to parse the NVMe command - we could maybe use bits in the opcode
2047 		 *  to easily determine if the command is a read or write, but for now just
2048 		 *  do not allow io_passthru with a read-only descriptor.
2049 		 */
2050 		return -EBADF;
2051 	}
2052 
2053 	bdev_io = spdk_bdev_get_io(channel);
2054 	if (!bdev_io) {
2055 		SPDK_ERRLOG("bdev_io memory allocation failed during nvme_admin_passthru\n");
2056 		return -ENOMEM;
2057 	}
2058 
2059 	bdev_io->ch = channel;
2060 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO;
2061 	bdev_io->u.nvme_passthru.cmd = *cmd;
2062 	bdev_io->u.nvme_passthru.buf = buf;
2063 	bdev_io->u.nvme_passthru.nbytes = nbytes;
2064 	bdev_io->u.nvme_passthru.md_buf = NULL;
2065 	bdev_io->u.nvme_passthru.md_len = 0;
2066 
2067 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2068 
2069 	spdk_bdev_io_submit(bdev_io);
2070 	return 0;
2071 }
2072 
2073 int
2074 spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2075 			      const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len,
2076 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
2077 {
2078 	struct spdk_bdev *bdev = desc->bdev;
2079 	struct spdk_bdev_io *bdev_io;
2080 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2081 
2082 	if (!desc->write) {
2083 		/*
2084 		 * Do not try to parse the NVMe command - we could maybe use bits in the opcode
2085 		 *  to easily determine if the command is a read or write, but for now just
2086 		 *  do not allow io_passthru with a read-only descriptor.
2087 		 */
2088 		return -EBADF;
2089 	}
2090 
2091 	bdev_io = spdk_bdev_get_io(channel);
2092 	if (!bdev_io) {
2093 		SPDK_ERRLOG("bdev_io memory allocation failed during nvme_admin_passthru\n");
2094 		return -ENOMEM;
2095 	}
2096 
2097 	bdev_io->ch = channel;
2098 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD;
2099 	bdev_io->u.nvme_passthru.cmd = *cmd;
2100 	bdev_io->u.nvme_passthru.buf = buf;
2101 	bdev_io->u.nvme_passthru.nbytes = nbytes;
2102 	bdev_io->u.nvme_passthru.md_buf = md_buf;
2103 	bdev_io->u.nvme_passthru.md_len = md_len;
2104 
2105 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2106 
2107 	spdk_bdev_io_submit(bdev_io);
2108 	return 0;
2109 }
2110 
2111 int
2112 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
2113 {
2114 	if (!bdev_io) {
2115 		SPDK_ERRLOG("bdev_io is NULL\n");
2116 		return -1;
2117 	}
2118 
2119 	if (bdev_io->status == SPDK_BDEV_IO_STATUS_PENDING) {
2120 		SPDK_ERRLOG("bdev_io is in pending state\n");
2121 		assert(false);
2122 		return -1;
2123 	}
2124 
2125 	spdk_bdev_put_io(bdev_io);
2126 
2127 	return 0;
2128 }
2129 
2130 static void
2131 _spdk_bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch)
2132 {
2133 	struct spdk_bdev *bdev = bdev_ch->bdev;
2134 	struct spdk_bdev_module_channel	*module_ch = bdev_ch->module_ch;
2135 	struct spdk_bdev_io *bdev_io;
2136 
2137 	if (module_ch->io_outstanding > module_ch->nomem_threshold) {
2138 		/*
2139 		 * Allow some more I/O to complete before retrying the nomem_io queue.
2140 		 *  Some drivers (such as nvme) cannot immediately take a new I/O in
2141 		 *  the context of a completion, because the resources for the I/O are
2142 		 *  not released until control returns to the bdev poller.  Also, we
2143 		 *  may require several small I/O to complete before a larger I/O
2144 		 *  (that requires splitting) can be submitted.
2145 		 */
2146 		return;
2147 	}
2148 
2149 	while (!TAILQ_EMPTY(&module_ch->nomem_io)) {
2150 		bdev_io = TAILQ_FIRST(&module_ch->nomem_io);
2151 		TAILQ_REMOVE(&module_ch->nomem_io, bdev_io, link);
2152 		bdev_io->ch->io_outstanding++;
2153 		module_ch->io_outstanding++;
2154 		bdev_io->status = SPDK_BDEV_IO_STATUS_PENDING;
2155 		bdev->fn_table->submit_request(bdev_io->ch->channel, bdev_io);
2156 		if (bdev_io->status == SPDK_BDEV_IO_STATUS_NOMEM) {
2157 			break;
2158 		}
2159 	}
2160 }
2161 
2162 static inline void
2163 _spdk_bdev_io_complete(void *ctx)
2164 {
2165 	struct spdk_bdev_io *bdev_io = ctx;
2166 
2167 	if (spdk_unlikely(bdev_io->in_submit_request || bdev_io->io_submit_ch)) {
2168 		/*
2169 		 * Send the completion to the thread that originally submitted the I/O,
2170 		 * which may not be the current thread in the case of QoS.
2171 		 */
2172 		if (bdev_io->io_submit_ch) {
2173 			bdev_io->ch = bdev_io->io_submit_ch;
2174 			bdev_io->io_submit_ch = NULL;
2175 		}
2176 
2177 		/*
2178 		 * Defer completion to avoid potential infinite recursion if the
2179 		 * user's completion callback issues a new I/O.
2180 		 */
2181 		spdk_thread_send_msg(spdk_io_channel_get_thread(bdev_io->ch->channel),
2182 				     _spdk_bdev_io_complete, bdev_io);
2183 		return;
2184 	}
2185 
2186 	if (bdev_io->status == SPDK_BDEV_IO_STATUS_SUCCESS) {
2187 		switch (bdev_io->type) {
2188 		case SPDK_BDEV_IO_TYPE_READ:
2189 			bdev_io->ch->stat.bytes_read += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
2190 			bdev_io->ch->stat.num_read_ops++;
2191 			bdev_io->ch->stat.read_latency_ticks += (spdk_get_ticks() - bdev_io->submit_tsc);
2192 			break;
2193 		case SPDK_BDEV_IO_TYPE_WRITE:
2194 			bdev_io->ch->stat.bytes_written += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
2195 			bdev_io->ch->stat.num_write_ops++;
2196 			bdev_io->ch->stat.write_latency_ticks += (spdk_get_ticks() - bdev_io->submit_tsc);
2197 			break;
2198 		default:
2199 			break;
2200 		}
2201 	}
2202 
2203 #ifdef SPDK_CONFIG_VTUNE
2204 	uint64_t now_tsc = spdk_get_ticks();
2205 	if (now_tsc > (bdev_io->ch->start_tsc + bdev_io->ch->interval_tsc)) {
2206 		uint64_t data[5];
2207 
2208 		data[0] = bdev_io->ch->stat.num_read_ops;
2209 		data[1] = bdev_io->ch->stat.bytes_read;
2210 		data[2] = bdev_io->ch->stat.num_write_ops;
2211 		data[3] = bdev_io->ch->stat.bytes_written;
2212 		data[4] = bdev_io->bdev->fn_table->get_spin_time ?
2213 			  bdev_io->bdev->fn_table->get_spin_time(bdev_io->ch->channel) : 0;
2214 
2215 		__itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->ch->handle,
2216 				   __itt_metadata_u64, 5, data);
2217 
2218 		memset(&bdev_io->ch->stat, 0, sizeof(bdev_io->ch->stat));
2219 		bdev_io->ch->start_tsc = now_tsc;
2220 	}
2221 #endif
2222 
2223 	assert(bdev_io->cb != NULL);
2224 	assert(spdk_get_thread() == spdk_io_channel_get_thread(bdev_io->ch->channel));
2225 
2226 	bdev_io->cb(bdev_io, bdev_io->status == SPDK_BDEV_IO_STATUS_SUCCESS,
2227 		    bdev_io->caller_ctx);
2228 }
2229 
2230 static void
2231 _spdk_bdev_unfreeze_qos_channel(void *ctx)
2232 {
2233 	struct spdk_bdev	*bdev = ctx;
2234 
2235 	if (bdev->qos->ch) {
2236 		bdev->qos->ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS;
2237 		assert(TAILQ_EMPTY(&bdev->qos->ch->queued_resets));
2238 	}
2239 }
2240 
2241 static void
2242 _spdk_bdev_reset_complete(struct spdk_io_channel_iter *i, int status)
2243 {
2244 	struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i);
2245 
2246 	if (bdev_io->u.reset.ch_ref != NULL) {
2247 		spdk_put_io_channel(bdev_io->u.reset.ch_ref);
2248 		bdev_io->u.reset.ch_ref = NULL;
2249 	}
2250 
2251 	_spdk_bdev_io_complete(bdev_io);
2252 }
2253 
2254 static void
2255 _spdk_bdev_unfreeze_channel(struct spdk_io_channel_iter *i)
2256 {
2257 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
2258 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
2259 
2260 	ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS;
2261 	if (!TAILQ_EMPTY(&ch->queued_resets)) {
2262 		_spdk_bdev_channel_start_reset(ch);
2263 	}
2264 
2265 	spdk_for_each_channel_continue(i, 0);
2266 }
2267 
2268 void
2269 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
2270 {
2271 	struct spdk_bdev *bdev = bdev_io->bdev;
2272 	struct spdk_bdev_channel *bdev_ch = bdev_io->ch;
2273 	struct spdk_bdev_module_channel	*module_ch = bdev_ch->module_ch;
2274 
2275 	bdev_io->status = status;
2276 
2277 	if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) {
2278 		bool unlock_channels = false;
2279 
2280 		if (status == SPDK_BDEV_IO_STATUS_NOMEM) {
2281 			SPDK_ERRLOG("NOMEM returned for reset\n");
2282 		}
2283 		pthread_mutex_lock(&bdev->mutex);
2284 		if (bdev_io == bdev->reset_in_progress) {
2285 			bdev->reset_in_progress = NULL;
2286 			unlock_channels = true;
2287 		}
2288 		pthread_mutex_unlock(&bdev->mutex);
2289 
2290 		if (unlock_channels) {
2291 			/* Explicitly handle the QoS bdev channel as no IO channel associated */
2292 			if (bdev->qos && bdev->qos->thread) {
2293 				spdk_thread_send_msg(bdev->qos->thread,
2294 						     _spdk_bdev_unfreeze_qos_channel, bdev);
2295 			}
2296 
2297 			spdk_for_each_channel(__bdev_to_io_dev(bdev), _spdk_bdev_unfreeze_channel,
2298 					      bdev_io, _spdk_bdev_reset_complete);
2299 			return;
2300 		}
2301 	} else {
2302 		assert(bdev_ch->io_outstanding > 0);
2303 		assert(module_ch->io_outstanding > 0);
2304 		bdev_ch->io_outstanding--;
2305 		module_ch->io_outstanding--;
2306 
2307 		if (spdk_unlikely(status == SPDK_BDEV_IO_STATUS_NOMEM)) {
2308 			TAILQ_INSERT_HEAD(&module_ch->nomem_io, bdev_io, link);
2309 			/*
2310 			 * Wait for some of the outstanding I/O to complete before we
2311 			 *  retry any of the nomem_io.  Normally we will wait for
2312 			 *  NOMEM_THRESHOLD_COUNT I/O to complete but for low queue
2313 			 *  depth channels we will instead wait for half to complete.
2314 			 */
2315 			module_ch->nomem_threshold = spdk_max((int64_t)module_ch->io_outstanding / 2,
2316 							      (int64_t)module_ch->io_outstanding - NOMEM_THRESHOLD_COUNT);
2317 			return;
2318 		}
2319 
2320 		if (spdk_unlikely(!TAILQ_EMPTY(&module_ch->nomem_io))) {
2321 			_spdk_bdev_ch_retry_io(bdev_ch);
2322 		}
2323 	}
2324 
2325 	_spdk_bdev_io_complete(bdev_io);
2326 }
2327 
2328 void
2329 spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc,
2330 				  enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq)
2331 {
2332 	if (sc == SPDK_SCSI_STATUS_GOOD) {
2333 		bdev_io->status = SPDK_BDEV_IO_STATUS_SUCCESS;
2334 	} else {
2335 		bdev_io->status = SPDK_BDEV_IO_STATUS_SCSI_ERROR;
2336 		bdev_io->error.scsi.sc = sc;
2337 		bdev_io->error.scsi.sk = sk;
2338 		bdev_io->error.scsi.asc = asc;
2339 		bdev_io->error.scsi.ascq = ascq;
2340 	}
2341 
2342 	spdk_bdev_io_complete(bdev_io, bdev_io->status);
2343 }
2344 
2345 void
2346 spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io,
2347 			     int *sc, int *sk, int *asc, int *ascq)
2348 {
2349 	assert(sc != NULL);
2350 	assert(sk != NULL);
2351 	assert(asc != NULL);
2352 	assert(ascq != NULL);
2353 
2354 	switch (bdev_io->status) {
2355 	case SPDK_BDEV_IO_STATUS_SUCCESS:
2356 		*sc = SPDK_SCSI_STATUS_GOOD;
2357 		*sk = SPDK_SCSI_SENSE_NO_SENSE;
2358 		*asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
2359 		*ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
2360 		break;
2361 	case SPDK_BDEV_IO_STATUS_NVME_ERROR:
2362 		spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq);
2363 		break;
2364 	case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
2365 		*sc = bdev_io->error.scsi.sc;
2366 		*sk = bdev_io->error.scsi.sk;
2367 		*asc = bdev_io->error.scsi.asc;
2368 		*ascq = bdev_io->error.scsi.ascq;
2369 		break;
2370 	default:
2371 		*sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
2372 		*sk = SPDK_SCSI_SENSE_ABORTED_COMMAND;
2373 		*asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
2374 		*ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
2375 		break;
2376 	}
2377 }
2378 
2379 void
2380 spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, int sct, int sc)
2381 {
2382 	if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS) {
2383 		bdev_io->status = SPDK_BDEV_IO_STATUS_SUCCESS;
2384 	} else {
2385 		bdev_io->error.nvme.sct = sct;
2386 		bdev_io->error.nvme.sc = sc;
2387 		bdev_io->status = SPDK_BDEV_IO_STATUS_NVME_ERROR;
2388 	}
2389 
2390 	spdk_bdev_io_complete(bdev_io, bdev_io->status);
2391 }
2392 
2393 void
2394 spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc)
2395 {
2396 	assert(sct != NULL);
2397 	assert(sc != NULL);
2398 
2399 	if (bdev_io->status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
2400 		*sct = bdev_io->error.nvme.sct;
2401 		*sc = bdev_io->error.nvme.sc;
2402 	} else if (bdev_io->status == SPDK_BDEV_IO_STATUS_SUCCESS) {
2403 		*sct = SPDK_NVME_SCT_GENERIC;
2404 		*sc = SPDK_NVME_SC_SUCCESS;
2405 	} else {
2406 		*sct = SPDK_NVME_SCT_GENERIC;
2407 		*sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
2408 	}
2409 }
2410 
2411 struct spdk_thread *
2412 spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io)
2413 {
2414 	return spdk_io_channel_get_thread(bdev_io->ch->channel);
2415 }
2416 
2417 static void
2418 _spdk_bdev_qos_config(struct spdk_bdev *bdev)
2419 {
2420 	struct spdk_conf_section	*sp = NULL;
2421 	const char			*val = NULL;
2422 	uint64_t			ios_per_sec = 0;
2423 	int				i = 0;
2424 
2425 	sp = spdk_conf_find_section(NULL, "QoS");
2426 	if (!sp) {
2427 		return;
2428 	}
2429 
2430 	while (true) {
2431 		val = spdk_conf_section_get_nmval(sp, "Limit_IOPS", i, 0);
2432 		if (!val) {
2433 			break;
2434 		}
2435 
2436 		if (strcmp(bdev->name, val) != 0) {
2437 			i++;
2438 			continue;
2439 		}
2440 
2441 		val = spdk_conf_section_get_nmval(sp, "Limit_IOPS", i, 1);
2442 		if (!val) {
2443 			return;
2444 		}
2445 
2446 		ios_per_sec = strtoull(val, NULL, 10);
2447 		if (ios_per_sec > 0) {
2448 			if (ios_per_sec % SPDK_BDEV_QOS_MIN_IOS_PER_SEC) {
2449 				SPDK_ERRLOG("Assigned IOPS %" PRIu64 " on bdev %s is not multiple of %u\n",
2450 					    ios_per_sec, bdev->name, SPDK_BDEV_QOS_MIN_IOS_PER_SEC);
2451 				SPDK_ERRLOG("Failed to enable QoS on this bdev %s\n", bdev->name);
2452 			} else {
2453 				bdev->qos = calloc(1, sizeof(*bdev->qos));
2454 				if (!bdev->qos) {
2455 					SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
2456 					return;
2457 				}
2458 				bdev->qos->rate_limit = ios_per_sec;
2459 				SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Bdev:%s QoS:%lu\n",
2460 					      bdev->name, bdev->qos->rate_limit);
2461 			}
2462 		}
2463 
2464 		return;
2465 	}
2466 }
2467 
2468 static int
2469 spdk_bdev_init(struct spdk_bdev *bdev)
2470 {
2471 	assert(bdev->module != NULL);
2472 
2473 	if (!bdev->name) {
2474 		SPDK_ERRLOG("Bdev name is NULL\n");
2475 		return -EINVAL;
2476 	}
2477 
2478 	if (spdk_bdev_get_by_name(bdev->name)) {
2479 		SPDK_ERRLOG("Bdev name:%s already exists\n", bdev->name);
2480 		return -EEXIST;
2481 	}
2482 
2483 	bdev->status = SPDK_BDEV_STATUS_READY;
2484 
2485 	TAILQ_INIT(&bdev->open_descs);
2486 
2487 	TAILQ_INIT(&bdev->aliases);
2488 
2489 	bdev->reset_in_progress = NULL;
2490 
2491 	_spdk_bdev_qos_config(bdev);
2492 
2493 	spdk_io_device_register(__bdev_to_io_dev(bdev),
2494 				spdk_bdev_channel_create, spdk_bdev_channel_destroy,
2495 				sizeof(struct spdk_bdev_channel));
2496 
2497 	pthread_mutex_init(&bdev->mutex, NULL);
2498 	return 0;
2499 }
2500 
2501 static void
2502 spdk_bdev_destroy_cb(void *io_device)
2503 {
2504 	int			rc;
2505 	struct spdk_bdev	*bdev;
2506 	spdk_bdev_unregister_cb	cb_fn;
2507 	void			*cb_arg;
2508 
2509 	bdev = __bdev_from_io_dev(io_device);
2510 	cb_fn = bdev->unregister_cb;
2511 	cb_arg = bdev->unregister_ctx;
2512 
2513 	rc = bdev->fn_table->destruct(bdev->ctxt);
2514 	if (rc < 0) {
2515 		SPDK_ERRLOG("destruct failed\n");
2516 	}
2517 	if (rc <= 0 && cb_fn != NULL) {
2518 		cb_fn(cb_arg, rc);
2519 	}
2520 }
2521 
2522 
2523 static void
2524 spdk_bdev_fini(struct spdk_bdev *bdev)
2525 {
2526 	pthread_mutex_destroy(&bdev->mutex);
2527 
2528 	free(bdev->qos);
2529 
2530 	spdk_io_device_unregister(__bdev_to_io_dev(bdev), spdk_bdev_destroy_cb);
2531 }
2532 
2533 static void
2534 spdk_bdev_start(struct spdk_bdev *bdev)
2535 {
2536 	struct spdk_bdev_module *module;
2537 
2538 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Inserting bdev %s into list\n", bdev->name);
2539 	TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, link);
2540 
2541 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, tailq) {
2542 		if (module->examine) {
2543 			module->action_in_progress++;
2544 			module->examine(bdev);
2545 		}
2546 	}
2547 }
2548 
2549 int
2550 spdk_bdev_register(struct spdk_bdev *bdev)
2551 {
2552 	int rc = spdk_bdev_init(bdev);
2553 
2554 	if (rc == 0) {
2555 		spdk_bdev_start(bdev);
2556 	}
2557 
2558 	return rc;
2559 }
2560 
2561 static void
2562 spdk_vbdev_remove_base_bdevs(struct spdk_bdev *vbdev)
2563 {
2564 	struct spdk_bdev **bdevs;
2565 	struct spdk_bdev *base;
2566 	size_t i, j, k;
2567 	bool found;
2568 
2569 	/* Iterate over base bdevs to remove vbdev from them. */
2570 	for (i = 0; i < vbdev->base_bdevs_cnt; i++) {
2571 		found = false;
2572 		base = vbdev->base_bdevs[i];
2573 
2574 		for (j = 0; j < base->vbdevs_cnt; j++) {
2575 			if (base->vbdevs[j] != vbdev) {
2576 				continue;
2577 			}
2578 
2579 			for (k = j; k + 1 < base->vbdevs_cnt; k++) {
2580 				base->vbdevs[k] = base->vbdevs[k + 1];
2581 			}
2582 
2583 			base->vbdevs_cnt--;
2584 			if (base->vbdevs_cnt > 0) {
2585 				bdevs = realloc(base->vbdevs, base->vbdevs_cnt * sizeof(bdevs[0]));
2586 				/* It would be odd if shrinking memory block fail. */
2587 				assert(bdevs);
2588 				base->vbdevs = bdevs;
2589 			} else {
2590 				free(base->vbdevs);
2591 				base->vbdevs = NULL;
2592 			}
2593 
2594 			found = true;
2595 			break;
2596 		}
2597 
2598 		if (!found) {
2599 			SPDK_WARNLOG("Bdev '%s' is not base bdev of '%s'.\n", base->name, vbdev->name);
2600 		}
2601 	}
2602 
2603 	free(vbdev->base_bdevs);
2604 	vbdev->base_bdevs = NULL;
2605 	vbdev->base_bdevs_cnt = 0;
2606 }
2607 
2608 static int
2609 spdk_vbdev_set_base_bdevs(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, size_t cnt)
2610 {
2611 	struct spdk_bdev **vbdevs;
2612 	struct spdk_bdev *base;
2613 	size_t i;
2614 
2615 	/* Adding base bdevs isn't supported (yet?). */
2616 	assert(vbdev->base_bdevs_cnt == 0);
2617 
2618 	vbdev->base_bdevs = malloc(cnt * sizeof(vbdev->base_bdevs[0]));
2619 	if (!vbdev->base_bdevs) {
2620 		SPDK_ERRLOG("%s - realloc() failed\n", vbdev->name);
2621 		return -ENOMEM;
2622 	}
2623 
2624 	memcpy(vbdev->base_bdevs, base_bdevs, cnt * sizeof(vbdev->base_bdevs[0]));
2625 	vbdev->base_bdevs_cnt = cnt;
2626 
2627 	/* Iterate over base bdevs to add this vbdev to them. */
2628 	for (i = 0; i < cnt; i++) {
2629 		base = vbdev->base_bdevs[i];
2630 
2631 		assert(base != NULL);
2632 		assert(base->claim_module != NULL);
2633 
2634 		vbdevs = realloc(base->vbdevs, (base->vbdevs_cnt + 1) * sizeof(vbdevs[0]));
2635 		if (!vbdevs) {
2636 			SPDK_ERRLOG("%s - realloc() failed\n", base->name);
2637 			spdk_vbdev_remove_base_bdevs(vbdev);
2638 			return -ENOMEM;
2639 		}
2640 
2641 		vbdevs[base->vbdevs_cnt] = vbdev;
2642 		base->vbdevs = vbdevs;
2643 		base->vbdevs_cnt++;
2644 	}
2645 
2646 	return 0;
2647 }
2648 
2649 int
2650 spdk_vbdev_register(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, int base_bdev_count)
2651 {
2652 	int rc;
2653 
2654 	rc = spdk_bdev_init(vbdev);
2655 	if (rc) {
2656 		return rc;
2657 	}
2658 
2659 	if (base_bdev_count == 0) {
2660 		spdk_bdev_start(vbdev);
2661 		return 0;
2662 	}
2663 
2664 	rc = spdk_vbdev_set_base_bdevs(vbdev, base_bdevs, base_bdev_count);
2665 	if (rc) {
2666 		spdk_bdev_fini(vbdev);
2667 		return rc;
2668 	}
2669 
2670 	spdk_bdev_start(vbdev);
2671 	return 0;
2672 
2673 }
2674 
2675 void
2676 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
2677 {
2678 	if (bdev->unregister_cb != NULL) {
2679 		bdev->unregister_cb(bdev->unregister_ctx, bdeverrno);
2680 	}
2681 }
2682 
2683 static void
2684 _remove_notify(void *arg)
2685 {
2686 	struct spdk_bdev_desc *desc = arg;
2687 
2688 	desc->remove_cb(desc->remove_ctx);
2689 }
2690 
2691 void
2692 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
2693 {
2694 	struct spdk_bdev_desc	*desc, *tmp;
2695 	bool			do_destruct = true;
2696 	struct spdk_thread	*thread;
2697 
2698 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Removing bdev %s from list\n", bdev->name);
2699 
2700 	thread = spdk_get_thread();
2701 	if (!thread) {
2702 		/* The user called this from a non-SPDK thread. */
2703 		cb_fn(cb_arg, -ENOTSUP);
2704 		return;
2705 	}
2706 
2707 	pthread_mutex_lock(&bdev->mutex);
2708 
2709 	spdk_vbdev_remove_base_bdevs(bdev);
2710 
2711 	bdev->status = SPDK_BDEV_STATUS_REMOVING;
2712 	bdev->unregister_cb = cb_fn;
2713 	bdev->unregister_ctx = cb_arg;
2714 
2715 	TAILQ_FOREACH_SAFE(desc, &bdev->open_descs, link, tmp) {
2716 		if (desc->remove_cb) {
2717 			do_destruct = false;
2718 			/*
2719 			 * Defer invocation of the remove_cb to a separate message that will
2720 			 *  run later on this thread.  This ensures this context unwinds and
2721 			 *  we don't recursively unregister this bdev again if the remove_cb
2722 			 *  immediately closes its descriptor.
2723 			 */
2724 			spdk_thread_send_msg(thread, _remove_notify, desc);
2725 		}
2726 	}
2727 
2728 	if (!do_destruct) {
2729 		pthread_mutex_unlock(&bdev->mutex);
2730 		return;
2731 	}
2732 
2733 	TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, link);
2734 	pthread_mutex_unlock(&bdev->mutex);
2735 
2736 	spdk_bdev_fini(bdev);
2737 }
2738 
2739 int
2740 spdk_bdev_open(struct spdk_bdev *bdev, bool write, spdk_bdev_remove_cb_t remove_cb,
2741 	       void *remove_ctx, struct spdk_bdev_desc **_desc)
2742 {
2743 	struct spdk_bdev_desc *desc;
2744 
2745 	desc = calloc(1, sizeof(*desc));
2746 	if (desc == NULL) {
2747 		SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n");
2748 		return -ENOMEM;
2749 	}
2750 
2751 	pthread_mutex_lock(&bdev->mutex);
2752 
2753 	if (write && bdev->claim_module) {
2754 		SPDK_INFOLOG(SPDK_LOG_BDEV, "Could not open %s - already claimed\n", bdev->name);
2755 		free(desc);
2756 		pthread_mutex_unlock(&bdev->mutex);
2757 		return -EPERM;
2758 	}
2759 
2760 	TAILQ_INSERT_TAIL(&bdev->open_descs, desc, link);
2761 
2762 	desc->bdev = bdev;
2763 	desc->remove_cb = remove_cb;
2764 	desc->remove_ctx = remove_ctx;
2765 	desc->write = write;
2766 	*_desc = desc;
2767 
2768 	pthread_mutex_unlock(&bdev->mutex);
2769 
2770 	return 0;
2771 }
2772 
2773 void
2774 spdk_bdev_close(struct spdk_bdev_desc *desc)
2775 {
2776 	struct spdk_bdev *bdev = desc->bdev;
2777 	bool do_unregister = false;
2778 
2779 	pthread_mutex_lock(&bdev->mutex);
2780 
2781 	TAILQ_REMOVE(&bdev->open_descs, desc, link);
2782 	free(desc);
2783 
2784 	if (bdev->status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->open_descs)) {
2785 		do_unregister = true;
2786 	}
2787 	pthread_mutex_unlock(&bdev->mutex);
2788 
2789 	if (do_unregister == true) {
2790 		spdk_bdev_unregister(bdev, bdev->unregister_cb, bdev->unregister_ctx);
2791 	}
2792 }
2793 
2794 int
2795 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
2796 			    struct spdk_bdev_module *module)
2797 {
2798 	if (bdev->claim_module != NULL) {
2799 		SPDK_ERRLOG("bdev %s already claimed by module %s\n", bdev->name,
2800 			    bdev->claim_module->name);
2801 		return -EPERM;
2802 	}
2803 
2804 	if (desc && !desc->write) {
2805 		desc->write = true;
2806 	}
2807 
2808 	bdev->claim_module = module;
2809 	return 0;
2810 }
2811 
2812 void
2813 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev)
2814 {
2815 	assert(bdev->claim_module != NULL);
2816 	bdev->claim_module = NULL;
2817 }
2818 
2819 struct spdk_bdev *
2820 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
2821 {
2822 	return desc->bdev;
2823 }
2824 
2825 void
2826 spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp)
2827 {
2828 	struct iovec *iovs;
2829 	int iovcnt;
2830 
2831 	if (bdev_io == NULL) {
2832 		return;
2833 	}
2834 
2835 	switch (bdev_io->type) {
2836 	case SPDK_BDEV_IO_TYPE_READ:
2837 		iovs = bdev_io->u.bdev.iovs;
2838 		iovcnt = bdev_io->u.bdev.iovcnt;
2839 		break;
2840 	case SPDK_BDEV_IO_TYPE_WRITE:
2841 		iovs = bdev_io->u.bdev.iovs;
2842 		iovcnt = bdev_io->u.bdev.iovcnt;
2843 		break;
2844 	default:
2845 		iovs = NULL;
2846 		iovcnt = 0;
2847 		break;
2848 	}
2849 
2850 	if (iovp) {
2851 		*iovp = iovs;
2852 	}
2853 	if (iovcntp) {
2854 		*iovcntp = iovcnt;
2855 	}
2856 }
2857 
2858 void
2859 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
2860 {
2861 
2862 	if (spdk_bdev_module_list_find(bdev_module->name)) {
2863 		SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name);
2864 		assert(false);
2865 	}
2866 
2867 	if (bdev_module->async_init) {
2868 		bdev_module->action_in_progress = 1;
2869 	}
2870 
2871 	/*
2872 	 * Modules with examine callbacks must be initialized first, so they are
2873 	 *  ready to handle examine callbacks from later modules that will
2874 	 *  register physical bdevs.
2875 	 */
2876 	if (bdev_module->examine != NULL) {
2877 		TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, tailq);
2878 	} else {
2879 		TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, tailq);
2880 	}
2881 }
2882 
2883 struct spdk_bdev_module *
2884 spdk_bdev_module_list_find(const char *name)
2885 {
2886 	struct spdk_bdev_module *bdev_module;
2887 
2888 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, tailq) {
2889 		if (strcmp(name, bdev_module->name) == 0) {
2890 			break;
2891 		}
2892 	}
2893 
2894 	return bdev_module;
2895 }
2896 
2897 static void
2898 spdk_bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
2899 {
2900 	uint64_t len;
2901 
2902 	if (!success) {
2903 		bdev_io->cb = bdev_io->u.bdev.stored_user_cb;
2904 		_spdk_bdev_io_complete(bdev_io);
2905 		return;
2906 	}
2907 
2908 	/* no need to perform the error checking from write_zeroes_blocks because this request already passed those checks. */
2909 	len = spdk_min(spdk_bdev_get_block_size(bdev_io->bdev) * bdev_io->u.bdev.split_remaining_num_blocks,
2910 		       ZERO_BUFFER_SIZE);
2911 
2912 	bdev_io->u.bdev.offset_blocks = bdev_io->u.bdev.split_current_offset_blocks;
2913 	bdev_io->u.bdev.iov.iov_len = len;
2914 	bdev_io->u.bdev.num_blocks = len / spdk_bdev_get_block_size(bdev_io->bdev);
2915 	bdev_io->u.bdev.split_remaining_num_blocks -= bdev_io->u.bdev.num_blocks;
2916 	bdev_io->u.bdev.split_current_offset_blocks += bdev_io->u.bdev.num_blocks;
2917 
2918 	/* if this round completes the i/o, change the callback to be the original user callback */
2919 	if (bdev_io->u.bdev.split_remaining_num_blocks == 0) {
2920 		spdk_bdev_io_init(bdev_io, bdev_io->bdev, cb_arg, bdev_io->u.bdev.stored_user_cb);
2921 	} else {
2922 		spdk_bdev_io_init(bdev_io, bdev_io->bdev, cb_arg, spdk_bdev_write_zeroes_split);
2923 	}
2924 	spdk_bdev_io_submit(bdev_io);
2925 }
2926 
2927 struct set_qos_limit_ctx {
2928 	void (*cb_fn)(void *cb_arg, int status);
2929 	void *cb_arg;
2930 	struct spdk_bdev *bdev;
2931 };
2932 
2933 static void
2934 _spdk_bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status)
2935 {
2936 	pthread_mutex_lock(&ctx->bdev->mutex);
2937 	ctx->bdev->qos_mod_in_progress = false;
2938 	pthread_mutex_unlock(&ctx->bdev->mutex);
2939 
2940 	ctx->cb_fn(ctx->cb_arg, status);
2941 	free(ctx);
2942 }
2943 
2944 static void
2945 _spdk_bdev_disable_qos_done(void *cb_arg)
2946 {
2947 	struct set_qos_limit_ctx *ctx = cb_arg;
2948 	struct spdk_bdev *bdev = ctx->bdev;
2949 	struct spdk_bdev_qos *qos;
2950 
2951 	pthread_mutex_lock(&bdev->mutex);
2952 	qos = bdev->qos;
2953 	bdev->qos = NULL;
2954 	pthread_mutex_unlock(&bdev->mutex);
2955 
2956 	_spdk_bdev_abort_queued_io(&qos->queued, qos->ch);
2957 	_spdk_bdev_channel_destroy(qos->ch);
2958 	spdk_poller_unregister(&qos->poller);
2959 
2960 	free(qos->ch);
2961 	free(qos);
2962 
2963 	_spdk_bdev_set_qos_limit_done(ctx, 0);
2964 }
2965 
2966 static void
2967 _spdk_bdev_disable_qos_msg_done(struct spdk_io_channel_iter *i, int status)
2968 {
2969 	void *io_device = spdk_io_channel_iter_get_io_device(i);
2970 	struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
2971 	struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
2972 	struct spdk_thread *thread;
2973 
2974 	pthread_mutex_lock(&bdev->mutex);
2975 	thread = bdev->qos->thread;
2976 	pthread_mutex_unlock(&bdev->mutex);
2977 
2978 	spdk_thread_send_msg(thread, _spdk_bdev_disable_qos_done, ctx);
2979 }
2980 
2981 static void
2982 _spdk_bdev_disable_qos_msg(struct spdk_io_channel_iter *i)
2983 {
2984 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
2985 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch);
2986 
2987 	bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED;
2988 
2989 	spdk_for_each_channel_continue(i, 0);
2990 }
2991 
2992 static void
2993 _spdk_bdev_update_qos_limit_iops_msg(void *cb_arg)
2994 {
2995 	struct set_qos_limit_ctx *ctx = cb_arg;
2996 	struct spdk_bdev *bdev = ctx->bdev;
2997 
2998 	pthread_mutex_lock(&bdev->mutex);
2999 	spdk_bdev_qos_update_max_ios_per_timeslice(bdev->qos);
3000 	pthread_mutex_unlock(&bdev->mutex);
3001 
3002 	_spdk_bdev_set_qos_limit_done(ctx, 0);
3003 }
3004 
3005 static void
3006 _spdk_bdev_enable_qos_msg(struct spdk_io_channel_iter *i)
3007 {
3008 	void *io_device = spdk_io_channel_iter_get_io_device(i);
3009 	struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
3010 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
3011 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch);
3012 	int rc;
3013 
3014 	pthread_mutex_lock(&bdev->mutex);
3015 	rc = _spdk_bdev_enable_qos(bdev, bdev_ch);
3016 	pthread_mutex_unlock(&bdev->mutex);
3017 	spdk_for_each_channel_continue(i, rc);
3018 }
3019 
3020 static void
3021 _spdk_bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status)
3022 {
3023 	struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
3024 
3025 	_spdk_bdev_set_qos_limit_done(ctx, status);
3026 }
3027 
3028 void
3029 spdk_bdev_set_qos_limit_iops(struct spdk_bdev *bdev, uint64_t ios_per_sec,
3030 			     void (*cb_fn)(void *cb_arg, int status), void *cb_arg)
3031 {
3032 	struct set_qos_limit_ctx *ctx;
3033 
3034 	if (ios_per_sec > 0 && ios_per_sec % SPDK_BDEV_QOS_MIN_IOS_PER_SEC) {
3035 		SPDK_ERRLOG("Requested ios_per_sec limit %" PRIu64 " is not a multiple of %u\n",
3036 			    ios_per_sec, SPDK_BDEV_QOS_MIN_IOS_PER_SEC);
3037 		cb_fn(cb_arg, -EINVAL);
3038 		return;
3039 	}
3040 
3041 	ctx = calloc(1, sizeof(*ctx));
3042 	if (ctx == NULL) {
3043 		cb_fn(cb_arg, -ENOMEM);
3044 		return;
3045 	}
3046 
3047 	ctx->cb_fn = cb_fn;
3048 	ctx->cb_arg = cb_arg;
3049 	ctx->bdev = bdev;
3050 
3051 	pthread_mutex_lock(&bdev->mutex);
3052 	if (bdev->qos_mod_in_progress) {
3053 		pthread_mutex_unlock(&bdev->mutex);
3054 		free(ctx);
3055 		cb_fn(cb_arg, -EAGAIN);
3056 		return;
3057 	}
3058 	bdev->qos_mod_in_progress = true;
3059 
3060 	if (ios_per_sec > 0) {
3061 		if (bdev->qos == NULL) {
3062 			/* Enabling */
3063 			bdev->qos = calloc(1, sizeof(*bdev->qos));
3064 			if (!bdev->qos) {
3065 				pthread_mutex_unlock(&bdev->mutex);
3066 				SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
3067 				free(ctx);
3068 				cb_fn(cb_arg, -ENOMEM);
3069 				return;
3070 			}
3071 
3072 			bdev->qos->rate_limit = ios_per_sec;
3073 			spdk_for_each_channel(__bdev_to_io_dev(bdev),
3074 					      _spdk_bdev_enable_qos_msg, ctx,
3075 					      _spdk_bdev_enable_qos_done);
3076 		} else {
3077 			/* Updating */
3078 			bdev->qos->rate_limit = ios_per_sec;
3079 			spdk_thread_send_msg(bdev->qos->thread, _spdk_bdev_update_qos_limit_iops_msg, ctx);
3080 		}
3081 	} else {
3082 		if (bdev->qos != NULL) {
3083 			/* Disabling */
3084 			spdk_for_each_channel(__bdev_to_io_dev(bdev),
3085 					      _spdk_bdev_disable_qos_msg, ctx,
3086 					      _spdk_bdev_disable_qos_msg_done);
3087 		} else {
3088 			pthread_mutex_unlock(&bdev->mutex);
3089 			_spdk_bdev_set_qos_limit_done(ctx, 0);
3090 			return;
3091 		}
3092 	}
3093 
3094 	pthread_mutex_unlock(&bdev->mutex);
3095 }
3096 
3097 SPDK_LOG_REGISTER_COMPONENT("bdev", SPDK_LOG_BDEV)
3098