xref: /spdk/lib/bdev/bdev.c (revision 993ab4908c73d337a97f91ecdc9cd0ad493332bb)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/bdev.h"
37 #include "spdk/conf.h"
38 
39 #include "spdk/config.h"
40 #include "spdk/env.h"
41 #include "spdk/event.h"
42 #include "spdk/thread.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/notify.h"
48 #include "spdk/util.h"
49 #include "spdk/trace.h"
50 
51 #include "spdk/bdev_module.h"
52 #include "spdk_internal/log.h"
53 #include "spdk/string.h"
54 
55 #ifdef SPDK_CONFIG_VTUNE
56 #include "ittnotify.h"
57 #include "ittnotify_types.h"
58 int __itt_init_ittlib(const char *, __itt_group_id);
59 #endif
60 
61 #define SPDK_BDEV_IO_POOL_SIZE			(64 * 1024 - 1)
62 #define SPDK_BDEV_IO_CACHE_SIZE			256
63 #define BUF_SMALL_POOL_SIZE			8191
64 #define BUF_LARGE_POOL_SIZE			1023
65 #define NOMEM_THRESHOLD_COUNT			8
66 #define ZERO_BUFFER_SIZE			0x100000
67 
68 #define OWNER_BDEV		0x2
69 
70 #define OBJECT_BDEV_IO		0x2
71 
72 #define TRACE_GROUP_BDEV	0x3
73 #define TRACE_BDEV_IO_START	SPDK_TPOINT_ID(TRACE_GROUP_BDEV, 0x0)
74 #define TRACE_BDEV_IO_DONE	SPDK_TPOINT_ID(TRACE_GROUP_BDEV, 0x1)
75 
76 #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC		1000
77 #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE	1
78 #define SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE	512
79 #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC		1000
80 #define SPDK_BDEV_QOS_MIN_BYTES_PER_SEC		(1024 * 1024)
81 #define SPDK_BDEV_QOS_LIMIT_NOT_DEFINED		UINT64_MAX
82 
83 #define SPDK_BDEV_POOL_ALIGNMENT 512
84 
85 static const char *qos_conf_type[] = {"Limit_IOPS",
86 				      "Limit_BPS", "Limit_Read_BPS", "Limit_Write_BPS"
87 				     };
88 static const char *qos_rpc_type[] = {"rw_ios_per_sec",
89 				     "rw_mbytes_per_sec", "r_mbytes_per_sec", "w_mbytes_per_sec"
90 				    };
91 
92 TAILQ_HEAD(spdk_bdev_list, spdk_bdev);
93 
94 struct spdk_bdev_mgr {
95 	struct spdk_mempool *bdev_io_pool;
96 
97 	struct spdk_mempool *buf_small_pool;
98 	struct spdk_mempool *buf_large_pool;
99 
100 	void *zero_buffer;
101 
102 	TAILQ_HEAD(bdev_module_list, spdk_bdev_module) bdev_modules;
103 
104 	struct spdk_bdev_list bdevs;
105 
106 	bool init_complete;
107 	bool module_init_complete;
108 
109 #ifdef SPDK_CONFIG_VTUNE
110 	__itt_domain	*domain;
111 #endif
112 };
113 
114 static struct spdk_bdev_mgr g_bdev_mgr = {
115 	.bdev_modules = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdev_modules),
116 	.bdevs = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdevs),
117 	.init_complete = false,
118 	.module_init_complete = false,
119 };
120 
121 static struct spdk_bdev_opts	g_bdev_opts = {
122 	.bdev_io_pool_size = SPDK_BDEV_IO_POOL_SIZE,
123 	.bdev_io_cache_size = SPDK_BDEV_IO_CACHE_SIZE,
124 };
125 
126 static spdk_bdev_init_cb	g_init_cb_fn = NULL;
127 static void			*g_init_cb_arg = NULL;
128 
129 static spdk_bdev_fini_cb	g_fini_cb_fn = NULL;
130 static void			*g_fini_cb_arg = NULL;
131 static struct spdk_thread	*g_fini_thread = NULL;
132 
133 struct spdk_bdev_qos_limit {
134 	/** IOs or bytes allowed per second (i.e., 1s). */
135 	uint64_t limit;
136 
137 	/** Remaining IOs or bytes allowed in current timeslice (e.g., 1ms).
138 	 *  For remaining bytes, allowed to run negative if an I/O is submitted when
139 	 *  some bytes are remaining, but the I/O is bigger than that amount. The
140 	 *  excess will be deducted from the next timeslice.
141 	 */
142 	int64_t remaining_this_timeslice;
143 
144 	/** Minimum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */
145 	uint32_t min_per_timeslice;
146 
147 	/** Maximum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */
148 	uint32_t max_per_timeslice;
149 
150 	/** Function to check whether to queue the IO. */
151 	bool (*queue_io)(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io);
152 
153 	/** Function to update for the submitted IO. */
154 	void (*update_quota)(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io);
155 };
156 
157 struct spdk_bdev_qos {
158 	/** Types of structure of rate limits. */
159 	struct spdk_bdev_qos_limit rate_limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
160 
161 	/** The channel that all I/O are funneled through. */
162 	struct spdk_bdev_channel *ch;
163 
164 	/** The thread on which the poller is running. */
165 	struct spdk_thread *thread;
166 
167 	/** Queue of I/O waiting to be issued. */
168 	bdev_io_tailq_t queued;
169 
170 	/** Size of a timeslice in tsc ticks. */
171 	uint64_t timeslice_size;
172 
173 	/** Timestamp of start of last timeslice. */
174 	uint64_t last_timeslice;
175 
176 	/** Poller that processes queued I/O commands each time slice. */
177 	struct spdk_poller *poller;
178 };
179 
180 struct spdk_bdev_mgmt_channel {
181 	bdev_io_stailq_t need_buf_small;
182 	bdev_io_stailq_t need_buf_large;
183 
184 	/*
185 	 * Each thread keeps a cache of bdev_io - this allows
186 	 *  bdev threads which are *not* DPDK threads to still
187 	 *  benefit from a per-thread bdev_io cache.  Without
188 	 *  this, non-DPDK threads fetching from the mempool
189 	 *  incur a cmpxchg on get and put.
190 	 */
191 	bdev_io_stailq_t per_thread_cache;
192 	uint32_t	per_thread_cache_count;
193 	uint32_t	bdev_io_cache_size;
194 
195 	TAILQ_HEAD(, spdk_bdev_shared_resource)	shared_resources;
196 	TAILQ_HEAD(, spdk_bdev_io_wait_entry)	io_wait_queue;
197 };
198 
199 /*
200  * Per-module (or per-io_device) data. Multiple bdevs built on the same io_device
201  * will queue here their IO that awaits retry. It makes it possible to retry sending
202  * IO to one bdev after IO from other bdev completes.
203  */
204 struct spdk_bdev_shared_resource {
205 	/* The bdev management channel */
206 	struct spdk_bdev_mgmt_channel *mgmt_ch;
207 
208 	/*
209 	 * Count of I/O submitted to bdev module and waiting for completion.
210 	 * Incremented before submit_request() is called on an spdk_bdev_io.
211 	 */
212 	uint64_t		io_outstanding;
213 
214 	/*
215 	 * Queue of IO awaiting retry because of a previous NOMEM status returned
216 	 *  on this channel.
217 	 */
218 	bdev_io_tailq_t		nomem_io;
219 
220 	/*
221 	 * Threshold which io_outstanding must drop to before retrying nomem_io.
222 	 */
223 	uint64_t		nomem_threshold;
224 
225 	/* I/O channel allocated by a bdev module */
226 	struct spdk_io_channel	*shared_ch;
227 
228 	/* Refcount of bdev channels using this resource */
229 	uint32_t		ref;
230 
231 	TAILQ_ENTRY(spdk_bdev_shared_resource) link;
232 };
233 
234 #define BDEV_CH_RESET_IN_PROGRESS	(1 << 0)
235 #define BDEV_CH_QOS_ENABLED		(1 << 1)
236 
237 struct spdk_bdev_channel {
238 	struct spdk_bdev	*bdev;
239 
240 	/* The channel for the underlying device */
241 	struct spdk_io_channel	*channel;
242 
243 	/* Per io_device per thread data */
244 	struct spdk_bdev_shared_resource *shared_resource;
245 
246 	struct spdk_bdev_io_stat stat;
247 
248 	/*
249 	 * Count of I/O submitted through this channel and waiting for completion.
250 	 * Incremented before submit_request() is called on an spdk_bdev_io.
251 	 */
252 	uint64_t		io_outstanding;
253 
254 	bdev_io_tailq_t		queued_resets;
255 
256 	uint32_t		flags;
257 
258 	struct spdk_histogram_data *histogram;
259 
260 #ifdef SPDK_CONFIG_VTUNE
261 	uint64_t		start_tsc;
262 	uint64_t		interval_tsc;
263 	__itt_string_handle	*handle;
264 	struct spdk_bdev_io_stat prev_stat;
265 #endif
266 
267 };
268 
269 struct spdk_bdev_desc {
270 	struct spdk_bdev		*bdev;
271 	struct spdk_thread		*thread;
272 	spdk_bdev_remove_cb_t		remove_cb;
273 	void				*remove_ctx;
274 	bool				remove_scheduled;
275 	bool				closed;
276 	bool				write;
277 	TAILQ_ENTRY(spdk_bdev_desc)	link;
278 };
279 
280 struct spdk_bdev_iostat_ctx {
281 	struct spdk_bdev_io_stat *stat;
282 	spdk_bdev_get_device_stat_cb cb;
283 	void *cb_arg;
284 };
285 
286 struct set_qos_limit_ctx {
287 	void (*cb_fn)(void *cb_arg, int status);
288 	void *cb_arg;
289 	struct spdk_bdev *bdev;
290 };
291 
292 #define __bdev_to_io_dev(bdev)		(((char *)bdev) + 1)
293 #define __bdev_from_io_dev(io_dev)	((struct spdk_bdev *)(((char *)io_dev) - 1))
294 
295 static void _spdk_bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success,
296 		void *cb_arg);
297 static void _spdk_bdev_write_zero_buffer_next(void *_bdev_io);
298 
299 static void _spdk_bdev_enable_qos_msg(struct spdk_io_channel_iter *i);
300 static void _spdk_bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status);
301 
302 static int
303 _spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
304 				struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
305 				uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg);
306 static int
307 _spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
308 				 struct iovec *iov, int iovcnt, void *md_buf,
309 				 uint64_t offset_blocks, uint64_t num_blocks,
310 				 spdk_bdev_io_completion_cb cb, void *cb_arg);
311 
312 void
313 spdk_bdev_get_opts(struct spdk_bdev_opts *opts)
314 {
315 	*opts = g_bdev_opts;
316 }
317 
318 int
319 spdk_bdev_set_opts(struct spdk_bdev_opts *opts)
320 {
321 	uint32_t min_pool_size;
322 
323 	/*
324 	 * Add 1 to the thread count to account for the extra mgmt_ch that gets created during subsystem
325 	 *  initialization.  A second mgmt_ch will be created on the same thread when the application starts
326 	 *  but before the deferred put_io_channel event is executed for the first mgmt_ch.
327 	 */
328 	min_pool_size = opts->bdev_io_cache_size * (spdk_thread_get_count() + 1);
329 	if (opts->bdev_io_pool_size < min_pool_size) {
330 		SPDK_ERRLOG("bdev_io_pool_size %" PRIu32 " is not compatible with bdev_io_cache_size %" PRIu32
331 			    " and %" PRIu32 " threads\n", opts->bdev_io_pool_size, opts->bdev_io_cache_size,
332 			    spdk_thread_get_count());
333 		SPDK_ERRLOG("bdev_io_pool_size must be at least %" PRIu32 "\n", min_pool_size);
334 		return -1;
335 	}
336 
337 	g_bdev_opts = *opts;
338 	return 0;
339 }
340 
341 struct spdk_bdev *
342 spdk_bdev_first(void)
343 {
344 	struct spdk_bdev *bdev;
345 
346 	bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs);
347 	if (bdev) {
348 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Starting bdev iteration at %s\n", bdev->name);
349 	}
350 
351 	return bdev;
352 }
353 
354 struct spdk_bdev *
355 spdk_bdev_next(struct spdk_bdev *prev)
356 {
357 	struct spdk_bdev *bdev;
358 
359 	bdev = TAILQ_NEXT(prev, internal.link);
360 	if (bdev) {
361 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Continuing bdev iteration at %s\n", bdev->name);
362 	}
363 
364 	return bdev;
365 }
366 
367 static struct spdk_bdev *
368 _bdev_next_leaf(struct spdk_bdev *bdev)
369 {
370 	while (bdev != NULL) {
371 		if (bdev->internal.claim_module == NULL) {
372 			return bdev;
373 		} else {
374 			bdev = TAILQ_NEXT(bdev, internal.link);
375 		}
376 	}
377 
378 	return bdev;
379 }
380 
381 struct spdk_bdev *
382 spdk_bdev_first_leaf(void)
383 {
384 	struct spdk_bdev *bdev;
385 
386 	bdev = _bdev_next_leaf(TAILQ_FIRST(&g_bdev_mgr.bdevs));
387 
388 	if (bdev) {
389 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Starting bdev iteration at %s\n", bdev->name);
390 	}
391 
392 	return bdev;
393 }
394 
395 struct spdk_bdev *
396 spdk_bdev_next_leaf(struct spdk_bdev *prev)
397 {
398 	struct spdk_bdev *bdev;
399 
400 	bdev = _bdev_next_leaf(TAILQ_NEXT(prev, internal.link));
401 
402 	if (bdev) {
403 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Continuing bdev iteration at %s\n", bdev->name);
404 	}
405 
406 	return bdev;
407 }
408 
409 struct spdk_bdev *
410 spdk_bdev_get_by_name(const char *bdev_name)
411 {
412 	struct spdk_bdev_alias *tmp;
413 	struct spdk_bdev *bdev = spdk_bdev_first();
414 
415 	while (bdev != NULL) {
416 		if (strcmp(bdev_name, bdev->name) == 0) {
417 			return bdev;
418 		}
419 
420 		TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
421 			if (strcmp(bdev_name, tmp->alias) == 0) {
422 				return bdev;
423 			}
424 		}
425 
426 		bdev = spdk_bdev_next(bdev);
427 	}
428 
429 	return NULL;
430 }
431 
432 void
433 spdk_bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len)
434 {
435 	struct iovec *iovs;
436 
437 	if (bdev_io->u.bdev.iovs == NULL) {
438 		bdev_io->u.bdev.iovs = &bdev_io->iov;
439 		bdev_io->u.bdev.iovcnt = 1;
440 	}
441 
442 	iovs = bdev_io->u.bdev.iovs;
443 
444 	assert(iovs != NULL);
445 	assert(bdev_io->u.bdev.iovcnt >= 1);
446 
447 	iovs[0].iov_base = buf;
448 	iovs[0].iov_len = len;
449 }
450 
451 void
452 spdk_bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len)
453 {
454 	assert((len / spdk_bdev_get_md_size(bdev_io->bdev)) >= bdev_io->u.bdev.num_blocks);
455 	bdev_io->u.bdev.md_buf = md_buf;
456 }
457 
458 static bool
459 _is_buf_allocated(const struct iovec *iovs)
460 {
461 	if (iovs == NULL) {
462 		return false;
463 	}
464 
465 	return iovs[0].iov_base != NULL;
466 }
467 
468 static bool
469 _are_iovs_aligned(struct iovec *iovs, int iovcnt, uint32_t alignment)
470 {
471 	int i;
472 	uintptr_t iov_base;
473 
474 	if (spdk_likely(alignment == 1)) {
475 		return true;
476 	}
477 
478 	for (i = 0; i < iovcnt; i++) {
479 		iov_base = (uintptr_t)iovs[i].iov_base;
480 		if ((iov_base & (alignment - 1)) != 0) {
481 			return false;
482 		}
483 	}
484 
485 	return true;
486 }
487 
488 static void
489 _copy_iovs_to_buf(void *buf, size_t buf_len, struct iovec *iovs, int iovcnt)
490 {
491 	int i;
492 	size_t len;
493 
494 	for (i = 0; i < iovcnt; i++) {
495 		len = spdk_min(iovs[i].iov_len, buf_len);
496 		memcpy(buf, iovs[i].iov_base, len);
497 		buf += len;
498 		buf_len -= len;
499 	}
500 }
501 
502 static void
503 _copy_buf_to_iovs(struct iovec *iovs, int iovcnt, void *buf, size_t buf_len)
504 {
505 	int i;
506 	size_t len;
507 
508 	for (i = 0; i < iovcnt; i++) {
509 		len = spdk_min(iovs[i].iov_len, buf_len);
510 		memcpy(iovs[i].iov_base, buf, len);
511 		buf += len;
512 		buf_len -= len;
513 	}
514 }
515 
516 static void
517 _bdev_io_set_bounce_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len)
518 {
519 	/* save original iovec */
520 	bdev_io->internal.orig_iovs = bdev_io->u.bdev.iovs;
521 	bdev_io->internal.orig_iovcnt = bdev_io->u.bdev.iovcnt;
522 	/* set bounce iov */
523 	bdev_io->u.bdev.iovs = &bdev_io->internal.bounce_iov;
524 	bdev_io->u.bdev.iovcnt = 1;
525 	/* set bounce buffer for this operation */
526 	bdev_io->u.bdev.iovs[0].iov_base = buf;
527 	bdev_io->u.bdev.iovs[0].iov_len = len;
528 	/* if this is write path, copy data from original buffer to bounce buffer */
529 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
530 		_copy_iovs_to_buf(buf, len, bdev_io->internal.orig_iovs, bdev_io->internal.orig_iovcnt);
531 	}
532 }
533 
534 static void
535 _bdev_io_set_bounce_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len)
536 {
537 	/* save original md_buf */
538 	bdev_io->internal.orig_md_buf = bdev_io->u.bdev.md_buf;
539 	/* set bounce md_buf */
540 	bdev_io->u.bdev.md_buf = md_buf;
541 
542 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
543 		memcpy(md_buf, bdev_io->internal.orig_md_buf, len);
544 	}
545 }
546 
547 static void
548 _bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t len)
549 {
550 	struct spdk_bdev *bdev = bdev_io->bdev;
551 	bool buf_allocated;
552 	uint64_t md_len, alignment;
553 	void *aligned_buf;
554 
555 	alignment = spdk_bdev_get_buf_align(bdev);
556 	buf_allocated = _is_buf_allocated(bdev_io->u.bdev.iovs);
557 	aligned_buf = (void *)(((uintptr_t)buf + (alignment - 1)) & ~(alignment - 1));
558 
559 	if (buf_allocated) {
560 		_bdev_io_set_bounce_buf(bdev_io, aligned_buf, len);
561 	} else {
562 		spdk_bdev_io_set_buf(bdev_io, aligned_buf, len);
563 	}
564 
565 	if (spdk_bdev_is_md_separate(bdev)) {
566 		aligned_buf = (char *)aligned_buf + len;
567 		md_len = bdev_io->u.bdev.num_blocks * bdev->md_len;
568 
569 		assert(((uintptr_t)aligned_buf & (alignment - 1)) == 0);
570 
571 		if (bdev_io->u.bdev.md_buf != NULL) {
572 			_bdev_io_set_bounce_md_buf(bdev_io, aligned_buf, md_len);
573 		} else {
574 			spdk_bdev_io_set_md_buf(bdev_io, aligned_buf, md_len);
575 		}
576 	}
577 
578 	bdev_io->internal.buf = buf;
579 	bdev_io->internal.get_buf_cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true);
580 }
581 
582 static void
583 spdk_bdev_io_put_buf(struct spdk_bdev_io *bdev_io)
584 {
585 	struct spdk_bdev *bdev = bdev_io->bdev;
586 	struct spdk_mempool *pool;
587 	struct spdk_bdev_io *tmp;
588 	bdev_io_stailq_t *stailq;
589 	struct spdk_bdev_mgmt_channel *ch;
590 	uint64_t buf_len, md_len, alignment;
591 	void *buf;
592 
593 	buf = bdev_io->internal.buf;
594 	buf_len = bdev_io->internal.buf_len;
595 	md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0;
596 	alignment = spdk_bdev_get_buf_align(bdev);
597 	ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
598 
599 	bdev_io->internal.buf = NULL;
600 
601 	if (buf_len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) +
602 	    SPDK_BDEV_POOL_ALIGNMENT) {
603 		pool = g_bdev_mgr.buf_small_pool;
604 		stailq = &ch->need_buf_small;
605 	} else {
606 		pool = g_bdev_mgr.buf_large_pool;
607 		stailq = &ch->need_buf_large;
608 	}
609 
610 	if (STAILQ_EMPTY(stailq)) {
611 		spdk_mempool_put(pool, buf);
612 	} else {
613 		tmp = STAILQ_FIRST(stailq);
614 		STAILQ_REMOVE_HEAD(stailq, internal.buf_link);
615 		_bdev_io_set_buf(tmp, buf, tmp->internal.buf_len);
616 	}
617 }
618 
619 static void
620 _bdev_io_unset_bounce_buf(struct spdk_bdev_io *bdev_io)
621 {
622 	if (spdk_likely(bdev_io->internal.orig_iovcnt == 0)) {
623 		assert(bdev_io->internal.orig_md_buf == NULL);
624 		return;
625 	}
626 
627 	/* if this is read path, copy data from bounce buffer to original buffer */
628 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ &&
629 	    bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
630 		_copy_buf_to_iovs(bdev_io->internal.orig_iovs,
631 				  bdev_io->internal.orig_iovcnt,
632 				  bdev_io->internal.bounce_iov.iov_base,
633 				  bdev_io->internal.bounce_iov.iov_len);
634 	}
635 	/* set orignal buffer for this io */
636 	bdev_io->u.bdev.iovcnt = bdev_io->internal.orig_iovcnt;
637 	bdev_io->u.bdev.iovs = bdev_io->internal.orig_iovs;
638 	/* disable bouncing buffer for this io */
639 	bdev_io->internal.orig_iovcnt = 0;
640 	bdev_io->internal.orig_iovs = NULL;
641 
642 	/* do the same for metadata buffer */
643 	if (spdk_unlikely(bdev_io->internal.orig_md_buf != NULL)) {
644 		assert(spdk_bdev_is_md_separate(bdev_io->bdev));
645 
646 		if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ &&
647 		    bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
648 			memcpy(bdev_io->internal.orig_md_buf, bdev_io->u.bdev.md_buf,
649 			       bdev_io->u.bdev.num_blocks * spdk_bdev_get_md_size(bdev_io->bdev));
650 		}
651 
652 		bdev_io->u.bdev.md_buf = bdev_io->internal.orig_md_buf;
653 		bdev_io->internal.orig_md_buf = NULL;
654 	}
655 
656 	spdk_bdev_io_put_buf(bdev_io);
657 }
658 
659 void
660 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
661 {
662 	struct spdk_bdev *bdev = bdev_io->bdev;
663 	struct spdk_mempool *pool;
664 	bdev_io_stailq_t *stailq;
665 	struct spdk_bdev_mgmt_channel *mgmt_ch;
666 	uint64_t alignment, md_len;
667 	void *buf;
668 
669 	assert(cb != NULL);
670 
671 	alignment = spdk_bdev_get_buf_align(bdev);
672 	md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0;
673 
674 	if (_is_buf_allocated(bdev_io->u.bdev.iovs) &&
675 	    _are_iovs_aligned(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, alignment)) {
676 		/* Buffer already present and aligned */
677 		cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true);
678 		return;
679 	}
680 
681 	if (len + alignment + md_len > SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) +
682 	    SPDK_BDEV_POOL_ALIGNMENT) {
683 		SPDK_ERRLOG("Length + alignment %" PRIu64 " is larger than allowed\n",
684 			    len + alignment);
685 		cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, false);
686 		return;
687 	}
688 
689 	mgmt_ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
690 
691 	bdev_io->internal.buf_len = len;
692 	bdev_io->internal.get_buf_cb = cb;
693 
694 	if (len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) +
695 	    SPDK_BDEV_POOL_ALIGNMENT) {
696 		pool = g_bdev_mgr.buf_small_pool;
697 		stailq = &mgmt_ch->need_buf_small;
698 	} else {
699 		pool = g_bdev_mgr.buf_large_pool;
700 		stailq = &mgmt_ch->need_buf_large;
701 	}
702 
703 	buf = spdk_mempool_get(pool);
704 	if (!buf) {
705 		STAILQ_INSERT_TAIL(stailq, bdev_io, internal.buf_link);
706 	} else {
707 		_bdev_io_set_buf(bdev_io, buf, len);
708 	}
709 }
710 
711 static int
712 spdk_bdev_module_get_max_ctx_size(void)
713 {
714 	struct spdk_bdev_module *bdev_module;
715 	int max_bdev_module_size = 0;
716 
717 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
718 		if (bdev_module->get_ctx_size && bdev_module->get_ctx_size() > max_bdev_module_size) {
719 			max_bdev_module_size = bdev_module->get_ctx_size();
720 		}
721 	}
722 
723 	return max_bdev_module_size;
724 }
725 
726 void
727 spdk_bdev_config_text(FILE *fp)
728 {
729 	struct spdk_bdev_module *bdev_module;
730 
731 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
732 		if (bdev_module->config_text) {
733 			bdev_module->config_text(fp);
734 		}
735 	}
736 }
737 
738 static void
739 spdk_bdev_qos_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
740 {
741 	int i;
742 	struct spdk_bdev_qos *qos = bdev->internal.qos;
743 	uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
744 
745 	if (!qos) {
746 		return;
747 	}
748 
749 	spdk_bdev_get_qos_rate_limits(bdev, limits);
750 
751 	spdk_json_write_object_begin(w);
752 	spdk_json_write_named_string(w, "method", "set_bdev_qos_limit");
753 
754 	spdk_json_write_named_object_begin(w, "params");
755 	spdk_json_write_named_string(w, "name", bdev->name);
756 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
757 		if (limits[i] > 0) {
758 			spdk_json_write_named_uint64(w, qos_rpc_type[i], limits[i]);
759 		}
760 	}
761 	spdk_json_write_object_end(w);
762 
763 	spdk_json_write_object_end(w);
764 }
765 
766 void
767 spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w)
768 {
769 	struct spdk_bdev_module *bdev_module;
770 	struct spdk_bdev *bdev;
771 
772 	assert(w != NULL);
773 
774 	spdk_json_write_array_begin(w);
775 
776 	spdk_json_write_object_begin(w);
777 	spdk_json_write_named_string(w, "method", "set_bdev_options");
778 	spdk_json_write_named_object_begin(w, "params");
779 	spdk_json_write_named_uint32(w, "bdev_io_pool_size", g_bdev_opts.bdev_io_pool_size);
780 	spdk_json_write_named_uint32(w, "bdev_io_cache_size", g_bdev_opts.bdev_io_cache_size);
781 	spdk_json_write_object_end(w);
782 	spdk_json_write_object_end(w);
783 
784 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
785 		if (bdev_module->config_json) {
786 			bdev_module->config_json(w);
787 		}
788 	}
789 
790 	TAILQ_FOREACH(bdev, &g_bdev_mgr.bdevs, internal.link) {
791 		if (bdev->fn_table->write_config_json) {
792 			bdev->fn_table->write_config_json(bdev, w);
793 		}
794 
795 		spdk_bdev_qos_config_json(bdev, w);
796 	}
797 
798 	spdk_json_write_array_end(w);
799 }
800 
801 static int
802 spdk_bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
803 {
804 	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
805 	struct spdk_bdev_io *bdev_io;
806 	uint32_t i;
807 
808 	STAILQ_INIT(&ch->need_buf_small);
809 	STAILQ_INIT(&ch->need_buf_large);
810 
811 	STAILQ_INIT(&ch->per_thread_cache);
812 	ch->bdev_io_cache_size = g_bdev_opts.bdev_io_cache_size;
813 
814 	/* Pre-populate bdev_io cache to ensure this thread cannot be starved. */
815 	ch->per_thread_cache_count = 0;
816 	for (i = 0; i < ch->bdev_io_cache_size; i++) {
817 		bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
818 		assert(bdev_io != NULL);
819 		ch->per_thread_cache_count++;
820 		STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
821 	}
822 
823 	TAILQ_INIT(&ch->shared_resources);
824 	TAILQ_INIT(&ch->io_wait_queue);
825 
826 	return 0;
827 }
828 
829 static void
830 spdk_bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf)
831 {
832 	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
833 	struct spdk_bdev_io *bdev_io;
834 
835 	if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) {
836 		SPDK_ERRLOG("Pending I/O list wasn't empty on mgmt channel free\n");
837 	}
838 
839 	if (!TAILQ_EMPTY(&ch->shared_resources)) {
840 		SPDK_ERRLOG("Module channel list wasn't empty on mgmt channel free\n");
841 	}
842 
843 	while (!STAILQ_EMPTY(&ch->per_thread_cache)) {
844 		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
845 		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
846 		ch->per_thread_cache_count--;
847 		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
848 	}
849 
850 	assert(ch->per_thread_cache_count == 0);
851 }
852 
853 static void
854 spdk_bdev_init_complete(int rc)
855 {
856 	spdk_bdev_init_cb cb_fn = g_init_cb_fn;
857 	void *cb_arg = g_init_cb_arg;
858 	struct spdk_bdev_module *m;
859 
860 	g_bdev_mgr.init_complete = true;
861 	g_init_cb_fn = NULL;
862 	g_init_cb_arg = NULL;
863 
864 	/*
865 	 * For modules that need to know when subsystem init is complete,
866 	 * inform them now.
867 	 */
868 	if (rc == 0) {
869 		TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
870 			if (m->init_complete) {
871 				m->init_complete();
872 			}
873 		}
874 	}
875 
876 	cb_fn(cb_arg, rc);
877 }
878 
879 static void
880 spdk_bdev_module_action_complete(void)
881 {
882 	struct spdk_bdev_module *m;
883 
884 	/*
885 	 * Don't finish bdev subsystem initialization if
886 	 * module pre-initialization is still in progress, or
887 	 * the subsystem been already initialized.
888 	 */
889 	if (!g_bdev_mgr.module_init_complete || g_bdev_mgr.init_complete) {
890 		return;
891 	}
892 
893 	/*
894 	 * Check all bdev modules for inits/examinations in progress. If any
895 	 * exist, return immediately since we cannot finish bdev subsystem
896 	 * initialization until all are completed.
897 	 */
898 	TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
899 		if (m->internal.action_in_progress > 0) {
900 			return;
901 		}
902 	}
903 
904 	/*
905 	 * Modules already finished initialization - now that all
906 	 * the bdev modules have finished their asynchronous I/O
907 	 * processing, the entire bdev layer can be marked as complete.
908 	 */
909 	spdk_bdev_init_complete(0);
910 }
911 
912 static void
913 spdk_bdev_module_action_done(struct spdk_bdev_module *module)
914 {
915 	assert(module->internal.action_in_progress > 0);
916 	module->internal.action_in_progress--;
917 	spdk_bdev_module_action_complete();
918 }
919 
920 void
921 spdk_bdev_module_init_done(struct spdk_bdev_module *module)
922 {
923 	spdk_bdev_module_action_done(module);
924 }
925 
926 void
927 spdk_bdev_module_examine_done(struct spdk_bdev_module *module)
928 {
929 	spdk_bdev_module_action_done(module);
930 }
931 
932 /** The last initialized bdev module */
933 static struct spdk_bdev_module *g_resume_bdev_module = NULL;
934 
935 static int
936 spdk_bdev_modules_init(void)
937 {
938 	struct spdk_bdev_module *module;
939 	int rc = 0;
940 
941 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
942 		g_resume_bdev_module = module;
943 		if (module->async_init) {
944 			module->internal.action_in_progress = 1;
945 		}
946 		rc = module->module_init();
947 		if (rc != 0) {
948 			return rc;
949 		}
950 	}
951 
952 	g_resume_bdev_module = NULL;
953 	return 0;
954 }
955 
956 static void
957 spdk_bdev_init_failed(void *cb_arg)
958 {
959 	spdk_bdev_init_complete(-1);
960 }
961 
962 void
963 spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg)
964 {
965 	struct spdk_conf_section *sp;
966 	struct spdk_bdev_opts bdev_opts;
967 	int32_t bdev_io_pool_size, bdev_io_cache_size;
968 	int cache_size;
969 	int rc = 0;
970 	char mempool_name[32];
971 
972 	assert(cb_fn != NULL);
973 
974 	sp = spdk_conf_find_section(NULL, "Bdev");
975 	if (sp != NULL) {
976 		spdk_bdev_get_opts(&bdev_opts);
977 
978 		bdev_io_pool_size = spdk_conf_section_get_intval(sp, "BdevIoPoolSize");
979 		if (bdev_io_pool_size >= 0) {
980 			bdev_opts.bdev_io_pool_size = bdev_io_pool_size;
981 		}
982 
983 		bdev_io_cache_size = spdk_conf_section_get_intval(sp, "BdevIoCacheSize");
984 		if (bdev_io_cache_size >= 0) {
985 			bdev_opts.bdev_io_cache_size = bdev_io_cache_size;
986 		}
987 
988 		if (spdk_bdev_set_opts(&bdev_opts)) {
989 			spdk_bdev_init_complete(-1);
990 			return;
991 		}
992 
993 		assert(memcmp(&bdev_opts, &g_bdev_opts, sizeof(bdev_opts)) == 0);
994 	}
995 
996 	g_init_cb_fn = cb_fn;
997 	g_init_cb_arg = cb_arg;
998 
999 	spdk_notify_type_register("bdev_register");
1000 	spdk_notify_type_register("bdev_unregister");
1001 
1002 	snprintf(mempool_name, sizeof(mempool_name), "bdev_io_%d", getpid());
1003 
1004 	g_bdev_mgr.bdev_io_pool = spdk_mempool_create(mempool_name,
1005 				  g_bdev_opts.bdev_io_pool_size,
1006 				  sizeof(struct spdk_bdev_io) +
1007 				  spdk_bdev_module_get_max_ctx_size(),
1008 				  0,
1009 				  SPDK_ENV_SOCKET_ID_ANY);
1010 
1011 	if (g_bdev_mgr.bdev_io_pool == NULL) {
1012 		SPDK_ERRLOG("could not allocate spdk_bdev_io pool\n");
1013 		spdk_bdev_init_complete(-1);
1014 		return;
1015 	}
1016 
1017 	/**
1018 	 * Ensure no more than half of the total buffers end up local caches, by
1019 	 *   using spdk_thread_get_count() to determine how many local caches we need
1020 	 *   to account for.
1021 	 */
1022 	cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_thread_get_count());
1023 	snprintf(mempool_name, sizeof(mempool_name), "buf_small_pool_%d", getpid());
1024 
1025 	g_bdev_mgr.buf_small_pool = spdk_mempool_create(mempool_name,
1026 				    BUF_SMALL_POOL_SIZE,
1027 				    SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) +
1028 				    SPDK_BDEV_POOL_ALIGNMENT,
1029 				    cache_size,
1030 				    SPDK_ENV_SOCKET_ID_ANY);
1031 	if (!g_bdev_mgr.buf_small_pool) {
1032 		SPDK_ERRLOG("create rbuf small pool failed\n");
1033 		spdk_bdev_init_complete(-1);
1034 		return;
1035 	}
1036 
1037 	cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_thread_get_count());
1038 	snprintf(mempool_name, sizeof(mempool_name), "buf_large_pool_%d", getpid());
1039 
1040 	g_bdev_mgr.buf_large_pool = spdk_mempool_create(mempool_name,
1041 				    BUF_LARGE_POOL_SIZE,
1042 				    SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) +
1043 				    SPDK_BDEV_POOL_ALIGNMENT,
1044 				    cache_size,
1045 				    SPDK_ENV_SOCKET_ID_ANY);
1046 	if (!g_bdev_mgr.buf_large_pool) {
1047 		SPDK_ERRLOG("create rbuf large pool failed\n");
1048 		spdk_bdev_init_complete(-1);
1049 		return;
1050 	}
1051 
1052 	g_bdev_mgr.zero_buffer = spdk_zmalloc(ZERO_BUFFER_SIZE, ZERO_BUFFER_SIZE,
1053 					      NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
1054 	if (!g_bdev_mgr.zero_buffer) {
1055 		SPDK_ERRLOG("create bdev zero buffer failed\n");
1056 		spdk_bdev_init_complete(-1);
1057 		return;
1058 	}
1059 
1060 #ifdef SPDK_CONFIG_VTUNE
1061 	g_bdev_mgr.domain = __itt_domain_create("spdk_bdev");
1062 #endif
1063 
1064 	spdk_io_device_register(&g_bdev_mgr, spdk_bdev_mgmt_channel_create,
1065 				spdk_bdev_mgmt_channel_destroy,
1066 				sizeof(struct spdk_bdev_mgmt_channel),
1067 				"bdev_mgr");
1068 
1069 	rc = spdk_bdev_modules_init();
1070 	g_bdev_mgr.module_init_complete = true;
1071 	if (rc != 0) {
1072 		SPDK_ERRLOG("bdev modules init failed\n");
1073 		spdk_thread_send_msg(spdk_get_thread(), spdk_bdev_init_failed, NULL);
1074 		return;
1075 	}
1076 
1077 	spdk_bdev_module_action_complete();
1078 }
1079 
1080 static void
1081 spdk_bdev_mgr_unregister_cb(void *io_device)
1082 {
1083 	spdk_bdev_fini_cb cb_fn = g_fini_cb_fn;
1084 
1085 	if (spdk_mempool_count(g_bdev_mgr.bdev_io_pool) != g_bdev_opts.bdev_io_pool_size) {
1086 		SPDK_ERRLOG("bdev IO pool count is %zu but should be %u\n",
1087 			    spdk_mempool_count(g_bdev_mgr.bdev_io_pool),
1088 			    g_bdev_opts.bdev_io_pool_size);
1089 	}
1090 
1091 	if (spdk_mempool_count(g_bdev_mgr.buf_small_pool) != BUF_SMALL_POOL_SIZE) {
1092 		SPDK_ERRLOG("Small buffer pool count is %zu but should be %u\n",
1093 			    spdk_mempool_count(g_bdev_mgr.buf_small_pool),
1094 			    BUF_SMALL_POOL_SIZE);
1095 		assert(false);
1096 	}
1097 
1098 	if (spdk_mempool_count(g_bdev_mgr.buf_large_pool) != BUF_LARGE_POOL_SIZE) {
1099 		SPDK_ERRLOG("Large buffer pool count is %zu but should be %u\n",
1100 			    spdk_mempool_count(g_bdev_mgr.buf_large_pool),
1101 			    BUF_LARGE_POOL_SIZE);
1102 		assert(false);
1103 	}
1104 
1105 	spdk_mempool_free(g_bdev_mgr.bdev_io_pool);
1106 	spdk_mempool_free(g_bdev_mgr.buf_small_pool);
1107 	spdk_mempool_free(g_bdev_mgr.buf_large_pool);
1108 	spdk_free(g_bdev_mgr.zero_buffer);
1109 
1110 	cb_fn(g_fini_cb_arg);
1111 	g_fini_cb_fn = NULL;
1112 	g_fini_cb_arg = NULL;
1113 	g_bdev_mgr.init_complete = false;
1114 	g_bdev_mgr.module_init_complete = false;
1115 }
1116 
1117 static void
1118 spdk_bdev_module_finish_iter(void *arg)
1119 {
1120 	struct spdk_bdev_module *bdev_module;
1121 
1122 	/* Start iterating from the last touched module */
1123 	if (!g_resume_bdev_module) {
1124 		bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list);
1125 	} else {
1126 		bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list,
1127 					 internal.tailq);
1128 	}
1129 
1130 	while (bdev_module) {
1131 		if (bdev_module->async_fini) {
1132 			/* Save our place so we can resume later. We must
1133 			 * save the variable here, before calling module_fini()
1134 			 * below, because in some cases the module may immediately
1135 			 * call spdk_bdev_module_finish_done() and re-enter
1136 			 * this function to continue iterating. */
1137 			g_resume_bdev_module = bdev_module;
1138 		}
1139 
1140 		if (bdev_module->module_fini) {
1141 			bdev_module->module_fini();
1142 		}
1143 
1144 		if (bdev_module->async_fini) {
1145 			return;
1146 		}
1147 
1148 		bdev_module = TAILQ_PREV(bdev_module, bdev_module_list,
1149 					 internal.tailq);
1150 	}
1151 
1152 	g_resume_bdev_module = NULL;
1153 	spdk_io_device_unregister(&g_bdev_mgr, spdk_bdev_mgr_unregister_cb);
1154 }
1155 
1156 void
1157 spdk_bdev_module_finish_done(void)
1158 {
1159 	if (spdk_get_thread() != g_fini_thread) {
1160 		spdk_thread_send_msg(g_fini_thread, spdk_bdev_module_finish_iter, NULL);
1161 	} else {
1162 		spdk_bdev_module_finish_iter(NULL);
1163 	}
1164 }
1165 
1166 static void
1167 _spdk_bdev_finish_unregister_bdevs_iter(void *cb_arg, int bdeverrno)
1168 {
1169 	struct spdk_bdev *bdev = cb_arg;
1170 
1171 	if (bdeverrno && bdev) {
1172 		SPDK_WARNLOG("Unable to unregister bdev '%s' during spdk_bdev_finish()\n",
1173 			     bdev->name);
1174 
1175 		/*
1176 		 * Since the call to spdk_bdev_unregister() failed, we have no way to free this
1177 		 *  bdev; try to continue by manually removing this bdev from the list and continue
1178 		 *  with the next bdev in the list.
1179 		 */
1180 		TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
1181 	}
1182 
1183 	if (TAILQ_EMPTY(&g_bdev_mgr.bdevs)) {
1184 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Done unregistering bdevs\n");
1185 		/*
1186 		 * Bdev module finish need to be deferred as we might be in the middle of some context
1187 		 * (like bdev part free) that will use this bdev (or private bdev driver ctx data)
1188 		 * after returning.
1189 		 */
1190 		spdk_thread_send_msg(spdk_get_thread(), spdk_bdev_module_finish_iter, NULL);
1191 		return;
1192 	}
1193 
1194 	/*
1195 	 * Unregister last unclaimed bdev in the list, to ensure that bdev subsystem
1196 	 * shutdown proceeds top-down. The goal is to give virtual bdevs an opportunity
1197 	 * to detect clean shutdown as opposed to run-time hot removal of the underlying
1198 	 * base bdevs.
1199 	 *
1200 	 * Also, walk the list in the reverse order.
1201 	 */
1202 	for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list);
1203 	     bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) {
1204 		if (bdev->internal.claim_module != NULL) {
1205 			SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Skipping claimed bdev '%s'(<-'%s').\n",
1206 				      bdev->name, bdev->internal.claim_module->name);
1207 			continue;
1208 		}
1209 
1210 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Unregistering bdev '%s'\n", bdev->name);
1211 		spdk_bdev_unregister(bdev, _spdk_bdev_finish_unregister_bdevs_iter, bdev);
1212 		return;
1213 	}
1214 
1215 	/*
1216 	 * If any bdev fails to unclaim underlying bdev properly, we may face the
1217 	 * case of bdev list consisting of claimed bdevs only (if claims are managed
1218 	 * correctly, this would mean there's a loop in the claims graph which is
1219 	 * clearly impossible). Warn and unregister last bdev on the list then.
1220 	 */
1221 	for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list);
1222 	     bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) {
1223 		SPDK_ERRLOG("Unregistering claimed bdev '%s'!\n", bdev->name);
1224 		spdk_bdev_unregister(bdev, _spdk_bdev_finish_unregister_bdevs_iter, bdev);
1225 		return;
1226 	}
1227 }
1228 
1229 void
1230 spdk_bdev_finish(spdk_bdev_fini_cb cb_fn, void *cb_arg)
1231 {
1232 	struct spdk_bdev_module *m;
1233 
1234 	assert(cb_fn != NULL);
1235 
1236 	g_fini_thread = spdk_get_thread();
1237 
1238 	g_fini_cb_fn = cb_fn;
1239 	g_fini_cb_arg = cb_arg;
1240 
1241 	TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
1242 		if (m->fini_start) {
1243 			m->fini_start();
1244 		}
1245 	}
1246 
1247 	_spdk_bdev_finish_unregister_bdevs_iter(NULL, 0);
1248 }
1249 
1250 static struct spdk_bdev_io *
1251 spdk_bdev_get_io(struct spdk_bdev_channel *channel)
1252 {
1253 	struct spdk_bdev_mgmt_channel *ch = channel->shared_resource->mgmt_ch;
1254 	struct spdk_bdev_io *bdev_io;
1255 
1256 	if (ch->per_thread_cache_count > 0) {
1257 		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
1258 		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
1259 		ch->per_thread_cache_count--;
1260 	} else if (spdk_unlikely(!TAILQ_EMPTY(&ch->io_wait_queue))) {
1261 		/*
1262 		 * Don't try to look for bdev_ios in the global pool if there are
1263 		 * waiters on bdev_ios - we don't want this caller to jump the line.
1264 		 */
1265 		bdev_io = NULL;
1266 	} else {
1267 		bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
1268 	}
1269 
1270 	return bdev_io;
1271 }
1272 
1273 void
1274 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
1275 {
1276 	struct spdk_bdev_mgmt_channel *ch;
1277 
1278 	assert(bdev_io != NULL);
1279 	assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING);
1280 
1281 	ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
1282 
1283 	if (bdev_io->internal.buf != NULL) {
1284 		spdk_bdev_io_put_buf(bdev_io);
1285 	}
1286 
1287 	if (ch->per_thread_cache_count < ch->bdev_io_cache_size) {
1288 		ch->per_thread_cache_count++;
1289 		STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
1290 		while (ch->per_thread_cache_count > 0 && !TAILQ_EMPTY(&ch->io_wait_queue)) {
1291 			struct spdk_bdev_io_wait_entry *entry;
1292 
1293 			entry = TAILQ_FIRST(&ch->io_wait_queue);
1294 			TAILQ_REMOVE(&ch->io_wait_queue, entry, link);
1295 			entry->cb_fn(entry->cb_arg);
1296 		}
1297 	} else {
1298 		/* We should never have a full cache with entries on the io wait queue. */
1299 		assert(TAILQ_EMPTY(&ch->io_wait_queue));
1300 		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
1301 	}
1302 }
1303 
1304 static bool
1305 _spdk_bdev_qos_is_iops_rate_limit(enum spdk_bdev_qos_rate_limit_type limit)
1306 {
1307 	assert(limit != SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
1308 
1309 	switch (limit) {
1310 	case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT:
1311 		return true;
1312 	case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT:
1313 	case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT:
1314 	case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT:
1315 		return false;
1316 	case SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES:
1317 	default:
1318 		return false;
1319 	}
1320 }
1321 
1322 static bool
1323 _spdk_bdev_qos_io_to_limit(struct spdk_bdev_io *bdev_io)
1324 {
1325 	switch (bdev_io->type) {
1326 	case SPDK_BDEV_IO_TYPE_NVME_IO:
1327 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
1328 	case SPDK_BDEV_IO_TYPE_READ:
1329 	case SPDK_BDEV_IO_TYPE_WRITE:
1330 		return true;
1331 	default:
1332 		return false;
1333 	}
1334 }
1335 
1336 static bool
1337 _spdk_bdev_is_read_io(struct spdk_bdev_io *bdev_io)
1338 {
1339 	switch (bdev_io->type) {
1340 	case SPDK_BDEV_IO_TYPE_NVME_IO:
1341 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
1342 		/* Bit 1 (0x2) set for read operation */
1343 		if (bdev_io->u.nvme_passthru.cmd.opc & SPDK_NVME_OPC_READ) {
1344 			return true;
1345 		} else {
1346 			return false;
1347 		}
1348 	case SPDK_BDEV_IO_TYPE_READ:
1349 		return true;
1350 	default:
1351 		return false;
1352 	}
1353 }
1354 
1355 static uint64_t
1356 _spdk_bdev_get_io_size_in_byte(struct spdk_bdev_io *bdev_io)
1357 {
1358 	struct spdk_bdev	*bdev = bdev_io->bdev;
1359 
1360 	switch (bdev_io->type) {
1361 	case SPDK_BDEV_IO_TYPE_NVME_IO:
1362 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
1363 		return bdev_io->u.nvme_passthru.nbytes;
1364 	case SPDK_BDEV_IO_TYPE_READ:
1365 	case SPDK_BDEV_IO_TYPE_WRITE:
1366 		return bdev_io->u.bdev.num_blocks * bdev->blocklen;
1367 	default:
1368 		return 0;
1369 	}
1370 }
1371 
1372 static bool
1373 _spdk_bdev_qos_rw_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1374 {
1375 	if (limit->max_per_timeslice > 0 && limit->remaining_this_timeslice <= 0) {
1376 		return true;
1377 	} else {
1378 		return false;
1379 	}
1380 }
1381 
1382 static bool
1383 _spdk_bdev_qos_r_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1384 {
1385 	if (_spdk_bdev_is_read_io(io) == false) {
1386 		return false;
1387 	}
1388 
1389 	return _spdk_bdev_qos_rw_queue_io(limit, io);
1390 }
1391 
1392 static bool
1393 _spdk_bdev_qos_w_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1394 {
1395 	if (_spdk_bdev_is_read_io(io) == true) {
1396 		return false;
1397 	}
1398 
1399 	return _spdk_bdev_qos_rw_queue_io(limit, io);
1400 }
1401 
1402 static void
1403 _spdk_bdev_qos_rw_iops_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1404 {
1405 	limit->remaining_this_timeslice--;
1406 }
1407 
1408 static void
1409 _spdk_bdev_qos_rw_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1410 {
1411 	limit->remaining_this_timeslice -= _spdk_bdev_get_io_size_in_byte(io);
1412 }
1413 
1414 static void
1415 _spdk_bdev_qos_r_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1416 {
1417 	if (_spdk_bdev_is_read_io(io) == false) {
1418 		return;
1419 	}
1420 
1421 	return _spdk_bdev_qos_rw_bps_update_quota(limit, io);
1422 }
1423 
1424 static void
1425 _spdk_bdev_qos_w_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
1426 {
1427 	if (_spdk_bdev_is_read_io(io) == true) {
1428 		return;
1429 	}
1430 
1431 	return _spdk_bdev_qos_rw_bps_update_quota(limit, io);
1432 }
1433 
1434 static void
1435 _spdk_bdev_qos_set_ops(struct spdk_bdev_qos *qos)
1436 {
1437 	int i;
1438 
1439 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1440 		if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
1441 			qos->rate_limits[i].queue_io = NULL;
1442 			qos->rate_limits[i].update_quota = NULL;
1443 			continue;
1444 		}
1445 
1446 		switch (i) {
1447 		case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT:
1448 			qos->rate_limits[i].queue_io = _spdk_bdev_qos_rw_queue_io;
1449 			qos->rate_limits[i].update_quota = _spdk_bdev_qos_rw_iops_update_quota;
1450 			break;
1451 		case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT:
1452 			qos->rate_limits[i].queue_io = _spdk_bdev_qos_rw_queue_io;
1453 			qos->rate_limits[i].update_quota = _spdk_bdev_qos_rw_bps_update_quota;
1454 			break;
1455 		case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT:
1456 			qos->rate_limits[i].queue_io = _spdk_bdev_qos_r_queue_io;
1457 			qos->rate_limits[i].update_quota = _spdk_bdev_qos_r_bps_update_quota;
1458 			break;
1459 		case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT:
1460 			qos->rate_limits[i].queue_io = _spdk_bdev_qos_w_queue_io;
1461 			qos->rate_limits[i].update_quota = _spdk_bdev_qos_w_bps_update_quota;
1462 			break;
1463 		default:
1464 			break;
1465 		}
1466 	}
1467 }
1468 
1469 static inline void
1470 _spdk_bdev_io_do_submit(struct spdk_bdev_channel *bdev_ch, struct spdk_bdev_io *bdev_io)
1471 {
1472 	struct spdk_bdev *bdev = bdev_io->bdev;
1473 	struct spdk_io_channel *ch = bdev_ch->channel;
1474 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
1475 
1476 	if (spdk_likely(TAILQ_EMPTY(&shared_resource->nomem_io))) {
1477 		bdev_ch->io_outstanding++;
1478 		shared_resource->io_outstanding++;
1479 		bdev_io->internal.in_submit_request = true;
1480 		bdev->fn_table->submit_request(ch, bdev_io);
1481 		bdev_io->internal.in_submit_request = false;
1482 	} else {
1483 		TAILQ_INSERT_TAIL(&shared_resource->nomem_io, bdev_io, internal.link);
1484 	}
1485 }
1486 
1487 static int
1488 _spdk_bdev_qos_io_submit(struct spdk_bdev_channel *ch, struct spdk_bdev_qos *qos)
1489 {
1490 	struct spdk_bdev_io		*bdev_io = NULL, *tmp = NULL;
1491 	int				i, submitted_ios = 0;
1492 
1493 	TAILQ_FOREACH_SAFE(bdev_io, &qos->queued, internal.link, tmp) {
1494 		if (_spdk_bdev_qos_io_to_limit(bdev_io) == true) {
1495 			for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1496 				if (!qos->rate_limits[i].queue_io) {
1497 					continue;
1498 				}
1499 
1500 				if (qos->rate_limits[i].queue_io(&qos->rate_limits[i],
1501 								 bdev_io) == true) {
1502 					return submitted_ios;
1503 				}
1504 			}
1505 			for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1506 				if (!qos->rate_limits[i].update_quota) {
1507 					continue;
1508 				}
1509 
1510 				qos->rate_limits[i].update_quota(&qos->rate_limits[i], bdev_io);
1511 			}
1512 		}
1513 
1514 		TAILQ_REMOVE(&qos->queued, bdev_io, internal.link);
1515 		_spdk_bdev_io_do_submit(ch, bdev_io);
1516 		submitted_ios++;
1517 	}
1518 
1519 	return submitted_ios;
1520 }
1521 
1522 static void
1523 _spdk_bdev_queue_io_wait_with_cb(struct spdk_bdev_io *bdev_io, spdk_bdev_io_wait_cb cb_fn)
1524 {
1525 	int rc;
1526 
1527 	bdev_io->internal.waitq_entry.bdev = bdev_io->bdev;
1528 	bdev_io->internal.waitq_entry.cb_fn = cb_fn;
1529 	bdev_io->internal.waitq_entry.cb_arg = bdev_io;
1530 	rc = spdk_bdev_queue_io_wait(bdev_io->bdev, spdk_io_channel_from_ctx(bdev_io->internal.ch),
1531 				     &bdev_io->internal.waitq_entry);
1532 	if (rc != 0) {
1533 		SPDK_ERRLOG("Queue IO failed, rc=%d\n", rc);
1534 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1535 		bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
1536 	}
1537 }
1538 
1539 static bool
1540 _spdk_bdev_io_type_can_split(uint8_t type)
1541 {
1542 	assert(type != SPDK_BDEV_IO_TYPE_INVALID);
1543 	assert(type < SPDK_BDEV_NUM_IO_TYPES);
1544 
1545 	/* Only split READ and WRITE I/O.  Theoretically other types of I/O like
1546 	 * UNMAP could be split, but these types of I/O are typically much larger
1547 	 * in size (sometimes the size of the entire block device), and the bdev
1548 	 * module can more efficiently split these types of I/O.  Plus those types
1549 	 * of I/O do not have a payload, which makes the splitting process simpler.
1550 	 */
1551 	if (type == SPDK_BDEV_IO_TYPE_READ || type == SPDK_BDEV_IO_TYPE_WRITE) {
1552 		return true;
1553 	} else {
1554 		return false;
1555 	}
1556 }
1557 
1558 static bool
1559 _spdk_bdev_io_should_split(struct spdk_bdev_io *bdev_io)
1560 {
1561 	uint64_t start_stripe, end_stripe;
1562 	uint32_t io_boundary = bdev_io->bdev->optimal_io_boundary;
1563 
1564 	if (io_boundary == 0) {
1565 		return false;
1566 	}
1567 
1568 	if (!_spdk_bdev_io_type_can_split(bdev_io->type)) {
1569 		return false;
1570 	}
1571 
1572 	start_stripe = bdev_io->u.bdev.offset_blocks;
1573 	end_stripe = start_stripe + bdev_io->u.bdev.num_blocks - 1;
1574 	/* Avoid expensive div operations if possible.  These spdk_u32 functions are very cheap. */
1575 	if (spdk_likely(spdk_u32_is_pow2(io_boundary))) {
1576 		start_stripe >>= spdk_u32log2(io_boundary);
1577 		end_stripe >>= spdk_u32log2(io_boundary);
1578 	} else {
1579 		start_stripe /= io_boundary;
1580 		end_stripe /= io_boundary;
1581 	}
1582 	return (start_stripe != end_stripe);
1583 }
1584 
1585 static uint32_t
1586 _to_next_boundary(uint64_t offset, uint32_t boundary)
1587 {
1588 	return (boundary - (offset % boundary));
1589 }
1590 
1591 static void
1592 _spdk_bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
1593 
1594 static void
1595 _spdk_bdev_io_split(void *_bdev_io)
1596 {
1597 	struct spdk_bdev_io *bdev_io = _bdev_io;
1598 	uint64_t current_offset, remaining;
1599 	uint32_t blocklen, to_next_boundary, to_next_boundary_bytes, to_last_block_bytes;
1600 	struct iovec *parent_iov, *iov;
1601 	uint64_t parent_iov_offset, iov_len;
1602 	uint32_t parent_iovpos, parent_iovcnt, child_iovcnt, iovcnt;
1603 	void *md_buf = NULL;
1604 	bool child_iov_run_out = false;
1605 	int rc;
1606 
1607 	remaining = bdev_io->u.bdev.split_remaining_num_blocks;
1608 	current_offset = bdev_io->u.bdev.split_current_offset_blocks;
1609 	blocklen = bdev_io->bdev->blocklen;
1610 	parent_iov_offset = (current_offset - bdev_io->u.bdev.offset_blocks) * blocklen;
1611 	parent_iovcnt = bdev_io->u.bdev.iovcnt;
1612 
1613 	for (parent_iovpos = 0; parent_iovpos < parent_iovcnt; parent_iovpos++) {
1614 		parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos];
1615 		if (parent_iov_offset < parent_iov->iov_len) {
1616 			break;
1617 		}
1618 		parent_iov_offset -= parent_iov->iov_len;
1619 	}
1620 
1621 	child_iovcnt = 0;
1622 	while (remaining > 0 && parent_iovpos < parent_iovcnt && child_iovcnt < BDEV_IO_NUM_CHILD_IOV) {
1623 		to_next_boundary = _to_next_boundary(current_offset, bdev_io->bdev->optimal_io_boundary);
1624 		to_next_boundary = spdk_min(remaining, to_next_boundary);
1625 		to_next_boundary_bytes = to_next_boundary * blocklen;
1626 		iov = &bdev_io->child_iov[child_iovcnt];
1627 		iovcnt = 0;
1628 
1629 		if (bdev_io->u.bdev.md_buf) {
1630 			assert((parent_iov_offset % blocklen) > 0);
1631 			md_buf = (char *)bdev_io->u.bdev.md_buf + (parent_iov_offset / blocklen) *
1632 				 spdk_bdev_get_md_size(bdev_io->bdev);
1633 		}
1634 
1635 		while (to_next_boundary_bytes > 0 && parent_iovpos < parent_iovcnt &&
1636 		       child_iovcnt < BDEV_IO_NUM_CHILD_IOV) {
1637 			parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos];
1638 			iov_len = spdk_min(to_next_boundary_bytes, parent_iov->iov_len - parent_iov_offset);
1639 			to_next_boundary_bytes -= iov_len;
1640 
1641 			bdev_io->child_iov[child_iovcnt].iov_base = parent_iov->iov_base + parent_iov_offset;
1642 			bdev_io->child_iov[child_iovcnt].iov_len = iov_len;
1643 
1644 			if (iov_len < parent_iov->iov_len - parent_iov_offset) {
1645 				parent_iov_offset += iov_len;
1646 			} else {
1647 				parent_iovpos++;
1648 				parent_iov_offset = 0;
1649 			}
1650 			child_iovcnt++;
1651 			iovcnt++;
1652 		}
1653 
1654 		if (to_next_boundary_bytes > 0) {
1655 			/* We had to stop this child I/O early because we ran out of
1656 			 * child_iov space.  Ensure the iovs to be aligned with block
1657 			 * size and then adjust to_next_boundary before starting the
1658 			 * child I/O.
1659 			 */
1660 			to_last_block_bytes = to_next_boundary_bytes % blocklen;
1661 			if (to_last_block_bytes != 0) {
1662 				to_next_boundary_bytes += _to_next_boundary(to_next_boundary_bytes, blocklen);;
1663 
1664 				while (to_last_block_bytes > 0 && iovcnt > 0) {
1665 					iov_len = spdk_min(to_last_block_bytes,
1666 							   bdev_io->child_iov[child_iovcnt - 1].iov_len);
1667 					bdev_io->child_iov[child_iovcnt - 1].iov_len -= iov_len;
1668 					if (bdev_io->child_iov[child_iovcnt - 1].iov_len == 0) {
1669 						child_iovcnt--;
1670 						iovcnt--;
1671 					}
1672 					to_last_block_bytes -= iov_len;
1673 				}
1674 
1675 				assert(to_last_block_bytes == 0);
1676 			}
1677 			child_iov_run_out = true;
1678 			to_next_boundary -= to_next_boundary_bytes / blocklen;
1679 		}
1680 
1681 		bdev_io->u.bdev.split_outstanding++;
1682 
1683 		if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
1684 			rc = _spdk_bdev_readv_blocks_with_md(bdev_io->internal.desc,
1685 							     spdk_io_channel_from_ctx(bdev_io->internal.ch),
1686 							     iov, iovcnt, md_buf, current_offset,
1687 							     to_next_boundary,
1688 							     _spdk_bdev_io_split_done, bdev_io);
1689 		} else {
1690 			rc = _spdk_bdev_writev_blocks_with_md(bdev_io->internal.desc,
1691 							      spdk_io_channel_from_ctx(bdev_io->internal.ch),
1692 							      iov, iovcnt, md_buf, current_offset,
1693 							      to_next_boundary,
1694 							      _spdk_bdev_io_split_done, bdev_io);
1695 		}
1696 
1697 		if (rc == 0) {
1698 			current_offset += to_next_boundary;
1699 			remaining -= to_next_boundary;
1700 			bdev_io->u.bdev.split_current_offset_blocks = current_offset;
1701 			bdev_io->u.bdev.split_remaining_num_blocks = remaining;
1702 			/* stop splitting until child_iov is available */
1703 			if (spdk_unlikely(child_iov_run_out)) {
1704 				return;
1705 			}
1706 		} else {
1707 			bdev_io->u.bdev.split_outstanding--;
1708 			if (rc == -ENOMEM) {
1709 				if (bdev_io->u.bdev.split_outstanding == 0) {
1710 					/* No I/O is outstanding. Hence we should wait here. */
1711 					_spdk_bdev_queue_io_wait_with_cb(bdev_io,
1712 									 _spdk_bdev_io_split);
1713 				}
1714 			} else {
1715 				bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1716 				if (bdev_io->u.bdev.split_outstanding == 0) {
1717 					bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
1718 				}
1719 			}
1720 
1721 			return;
1722 		}
1723 	}
1724 }
1725 
1726 static void
1727 _spdk_bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
1728 {
1729 	struct spdk_bdev_io *parent_io = cb_arg;
1730 
1731 	spdk_bdev_free_io(bdev_io);
1732 
1733 	if (!success) {
1734 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1735 	}
1736 	parent_io->u.bdev.split_outstanding--;
1737 	if (parent_io->u.bdev.split_outstanding != 0) {
1738 		return;
1739 	}
1740 
1741 	/*
1742 	 * Parent I/O finishes when all blocks are consumed.
1743 	 */
1744 	if (parent_io->u.bdev.split_remaining_num_blocks == 0) {
1745 		parent_io->internal.cb(parent_io, parent_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
1746 				       parent_io->internal.caller_ctx);
1747 		return;
1748 	}
1749 
1750 	/*
1751 	 * Continue with the splitting process.  This function will complete the parent I/O if the
1752 	 * splitting is done.
1753 	 */
1754 	_spdk_bdev_io_split(parent_io);
1755 }
1756 
1757 static void
1758 _spdk_bdev_io_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
1759 			       bool success);
1760 
1761 static void
1762 spdk_bdev_io_split(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
1763 {
1764 	assert(_spdk_bdev_io_type_can_split(bdev_io->type));
1765 
1766 	bdev_io->u.bdev.split_current_offset_blocks = bdev_io->u.bdev.offset_blocks;
1767 	bdev_io->u.bdev.split_remaining_num_blocks = bdev_io->u.bdev.num_blocks;
1768 	bdev_io->u.bdev.split_outstanding = 0;
1769 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
1770 
1771 	if (_is_buf_allocated(bdev_io->u.bdev.iovs)) {
1772 		_spdk_bdev_io_split(bdev_io);
1773 	} else {
1774 		assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
1775 		spdk_bdev_io_get_buf(bdev_io, _spdk_bdev_io_split_get_buf_cb,
1776 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
1777 	}
1778 }
1779 
1780 static void
1781 _spdk_bdev_io_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
1782 			       bool success)
1783 {
1784 	if (!success) {
1785 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
1786 		return;
1787 	}
1788 
1789 	spdk_bdev_io_split(ch, bdev_io);
1790 }
1791 
1792 /* Explicitly mark this inline, since it's used as a function pointer and otherwise won't
1793  *  be inlined, at least on some compilers.
1794  */
1795 static inline void
1796 _spdk_bdev_io_submit(void *ctx)
1797 {
1798 	struct spdk_bdev_io *bdev_io = ctx;
1799 	struct spdk_bdev *bdev = bdev_io->bdev;
1800 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
1801 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
1802 	uint64_t tsc;
1803 
1804 	tsc = spdk_get_ticks();
1805 	bdev_io->internal.submit_tsc = tsc;
1806 	spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_START, 0, 0, (uintptr_t)bdev_io, bdev_io->type);
1807 
1808 	if (spdk_likely(bdev_ch->flags == 0)) {
1809 		_spdk_bdev_io_do_submit(bdev_ch, bdev_io);
1810 		return;
1811 	}
1812 
1813 	bdev_ch->io_outstanding++;
1814 	shared_resource->io_outstanding++;
1815 	bdev_io->internal.in_submit_request = true;
1816 	if (bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS) {
1817 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
1818 	} else if (bdev_ch->flags & BDEV_CH_QOS_ENABLED) {
1819 		bdev_ch->io_outstanding--;
1820 		shared_resource->io_outstanding--;
1821 		TAILQ_INSERT_TAIL(&bdev->internal.qos->queued, bdev_io, internal.link);
1822 		_spdk_bdev_qos_io_submit(bdev_ch, bdev->internal.qos);
1823 	} else {
1824 		SPDK_ERRLOG("unknown bdev_ch flag %x found\n", bdev_ch->flags);
1825 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
1826 	}
1827 	bdev_io->internal.in_submit_request = false;
1828 }
1829 
1830 static void
1831 spdk_bdev_io_submit(struct spdk_bdev_io *bdev_io)
1832 {
1833 	struct spdk_bdev *bdev = bdev_io->bdev;
1834 	struct spdk_thread *thread = spdk_bdev_io_get_thread(bdev_io);
1835 
1836 	assert(thread != NULL);
1837 	assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING);
1838 
1839 	if (bdev->split_on_optimal_io_boundary && _spdk_bdev_io_should_split(bdev_io)) {
1840 		spdk_bdev_io_split(NULL, bdev_io);
1841 		return;
1842 	}
1843 
1844 	if (bdev_io->internal.ch->flags & BDEV_CH_QOS_ENABLED) {
1845 		if ((thread == bdev->internal.qos->thread) || !bdev->internal.qos->thread) {
1846 			_spdk_bdev_io_submit(bdev_io);
1847 		} else {
1848 			bdev_io->internal.io_submit_ch = bdev_io->internal.ch;
1849 			bdev_io->internal.ch = bdev->internal.qos->ch;
1850 			spdk_thread_send_msg(bdev->internal.qos->thread, _spdk_bdev_io_submit, bdev_io);
1851 		}
1852 	} else {
1853 		_spdk_bdev_io_submit(bdev_io);
1854 	}
1855 }
1856 
1857 static void
1858 spdk_bdev_io_submit_reset(struct spdk_bdev_io *bdev_io)
1859 {
1860 	struct spdk_bdev *bdev = bdev_io->bdev;
1861 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
1862 	struct spdk_io_channel *ch = bdev_ch->channel;
1863 
1864 	assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING);
1865 
1866 	bdev_io->internal.in_submit_request = true;
1867 	bdev->fn_table->submit_request(ch, bdev_io);
1868 	bdev_io->internal.in_submit_request = false;
1869 }
1870 
1871 static void
1872 spdk_bdev_io_init(struct spdk_bdev_io *bdev_io,
1873 		  struct spdk_bdev *bdev, void *cb_arg,
1874 		  spdk_bdev_io_completion_cb cb)
1875 {
1876 	bdev_io->bdev = bdev;
1877 	bdev_io->internal.caller_ctx = cb_arg;
1878 	bdev_io->internal.cb = cb;
1879 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
1880 	bdev_io->internal.in_submit_request = false;
1881 	bdev_io->internal.buf = NULL;
1882 	bdev_io->internal.io_submit_ch = NULL;
1883 	bdev_io->internal.orig_iovs = NULL;
1884 	bdev_io->internal.orig_iovcnt = 0;
1885 	bdev_io->internal.orig_md_buf = NULL;
1886 }
1887 
1888 static bool
1889 _spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
1890 {
1891 	return bdev->fn_table->io_type_supported(bdev->ctxt, io_type);
1892 }
1893 
1894 bool
1895 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
1896 {
1897 	bool supported;
1898 
1899 	supported = _spdk_bdev_io_type_supported(bdev, io_type);
1900 
1901 	if (!supported) {
1902 		switch (io_type) {
1903 		case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
1904 			/* The bdev layer will emulate write zeroes as long as write is supported. */
1905 			supported = _spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE);
1906 			break;
1907 		case SPDK_BDEV_IO_TYPE_ZCOPY:
1908 			/* Zero copy can be emulated with regular read and write */
1909 			supported = _spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_READ) &&
1910 				    _spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE);
1911 			break;
1912 		default:
1913 			break;
1914 		}
1915 	}
1916 
1917 	return supported;
1918 }
1919 
1920 int
1921 spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
1922 {
1923 	if (bdev->fn_table->dump_info_json) {
1924 		return bdev->fn_table->dump_info_json(bdev->ctxt, w);
1925 	}
1926 
1927 	return 0;
1928 }
1929 
1930 static void
1931 spdk_bdev_qos_update_max_quota_per_timeslice(struct spdk_bdev_qos *qos)
1932 {
1933 	uint32_t max_per_timeslice = 0;
1934 	int i;
1935 
1936 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1937 		if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
1938 			qos->rate_limits[i].max_per_timeslice = 0;
1939 			continue;
1940 		}
1941 
1942 		max_per_timeslice = qos->rate_limits[i].limit *
1943 				    SPDK_BDEV_QOS_TIMESLICE_IN_USEC / SPDK_SEC_TO_USEC;
1944 
1945 		qos->rate_limits[i].max_per_timeslice = spdk_max(max_per_timeslice,
1946 							qos->rate_limits[i].min_per_timeslice);
1947 
1948 		qos->rate_limits[i].remaining_this_timeslice = qos->rate_limits[i].max_per_timeslice;
1949 	}
1950 
1951 	_spdk_bdev_qos_set_ops(qos);
1952 }
1953 
1954 static int
1955 spdk_bdev_channel_poll_qos(void *arg)
1956 {
1957 	struct spdk_bdev_qos *qos = arg;
1958 	uint64_t now = spdk_get_ticks();
1959 	int i;
1960 
1961 	if (now < (qos->last_timeslice + qos->timeslice_size)) {
1962 		/* We received our callback earlier than expected - return
1963 		 *  immediately and wait to do accounting until at least one
1964 		 *  timeslice has actually expired.  This should never happen
1965 		 *  with a well-behaved timer implementation.
1966 		 */
1967 		return 0;
1968 	}
1969 
1970 	/* Reset for next round of rate limiting */
1971 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1972 		/* We may have allowed the IOs or bytes to slightly overrun in the last
1973 		 * timeslice. remaining_this_timeslice is signed, so if it's negative
1974 		 * here, we'll account for the overrun so that the next timeslice will
1975 		 * be appropriately reduced.
1976 		 */
1977 		if (qos->rate_limits[i].remaining_this_timeslice > 0) {
1978 			qos->rate_limits[i].remaining_this_timeslice = 0;
1979 		}
1980 	}
1981 
1982 	while (now >= (qos->last_timeslice + qos->timeslice_size)) {
1983 		qos->last_timeslice += qos->timeslice_size;
1984 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1985 			qos->rate_limits[i].remaining_this_timeslice +=
1986 				qos->rate_limits[i].max_per_timeslice;
1987 		}
1988 	}
1989 
1990 	return _spdk_bdev_qos_io_submit(qos->ch, qos);
1991 }
1992 
1993 static void
1994 _spdk_bdev_channel_destroy_resource(struct spdk_bdev_channel *ch)
1995 {
1996 	struct spdk_bdev_shared_resource *shared_resource;
1997 
1998 	spdk_put_io_channel(ch->channel);
1999 
2000 	shared_resource = ch->shared_resource;
2001 
2002 	assert(ch->io_outstanding == 0);
2003 	assert(shared_resource->ref > 0);
2004 	shared_resource->ref--;
2005 	if (shared_resource->ref == 0) {
2006 		assert(shared_resource->io_outstanding == 0);
2007 		TAILQ_REMOVE(&shared_resource->mgmt_ch->shared_resources, shared_resource, link);
2008 		spdk_put_io_channel(spdk_io_channel_from_ctx(shared_resource->mgmt_ch));
2009 		free(shared_resource);
2010 	}
2011 }
2012 
2013 /* Caller must hold bdev->internal.mutex. */
2014 static void
2015 _spdk_bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch)
2016 {
2017 	struct spdk_bdev_qos	*qos = bdev->internal.qos;
2018 	int			i;
2019 
2020 	/* Rate limiting on this bdev enabled */
2021 	if (qos) {
2022 		if (qos->ch == NULL) {
2023 			struct spdk_io_channel *io_ch;
2024 
2025 			SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Selecting channel %p as QoS channel for bdev %s on thread %p\n", ch,
2026 				      bdev->name, spdk_get_thread());
2027 
2028 			/* No qos channel has been selected, so set one up */
2029 
2030 			/* Take another reference to ch */
2031 			io_ch = spdk_get_io_channel(__bdev_to_io_dev(bdev));
2032 			assert(io_ch != NULL);
2033 			qos->ch = ch;
2034 
2035 			qos->thread = spdk_io_channel_get_thread(io_ch);
2036 
2037 			TAILQ_INIT(&qos->queued);
2038 
2039 			for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2040 				if (_spdk_bdev_qos_is_iops_rate_limit(i) == true) {
2041 					qos->rate_limits[i].min_per_timeslice =
2042 						SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE;
2043 				} else {
2044 					qos->rate_limits[i].min_per_timeslice =
2045 						SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE;
2046 				}
2047 
2048 				if (qos->rate_limits[i].limit == 0) {
2049 					qos->rate_limits[i].limit = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
2050 				}
2051 			}
2052 			spdk_bdev_qos_update_max_quota_per_timeslice(qos);
2053 			qos->timeslice_size =
2054 				SPDK_BDEV_QOS_TIMESLICE_IN_USEC * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
2055 			qos->last_timeslice = spdk_get_ticks();
2056 			qos->poller = spdk_poller_register(spdk_bdev_channel_poll_qos,
2057 							   qos,
2058 							   SPDK_BDEV_QOS_TIMESLICE_IN_USEC);
2059 		}
2060 
2061 		ch->flags |= BDEV_CH_QOS_ENABLED;
2062 	}
2063 }
2064 
2065 static int
2066 spdk_bdev_channel_create(void *io_device, void *ctx_buf)
2067 {
2068 	struct spdk_bdev		*bdev = __bdev_from_io_dev(io_device);
2069 	struct spdk_bdev_channel	*ch = ctx_buf;
2070 	struct spdk_io_channel		*mgmt_io_ch;
2071 	struct spdk_bdev_mgmt_channel	*mgmt_ch;
2072 	struct spdk_bdev_shared_resource *shared_resource;
2073 
2074 	ch->bdev = bdev;
2075 	ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt);
2076 	if (!ch->channel) {
2077 		return -1;
2078 	}
2079 
2080 	assert(ch->histogram == NULL);
2081 	if (bdev->internal.histogram_enabled) {
2082 		ch->histogram = spdk_histogram_data_alloc();
2083 		if (ch->histogram == NULL) {
2084 			SPDK_ERRLOG("Could not allocate histogram\n");
2085 		}
2086 	}
2087 
2088 	mgmt_io_ch = spdk_get_io_channel(&g_bdev_mgr);
2089 	if (!mgmt_io_ch) {
2090 		spdk_put_io_channel(ch->channel);
2091 		return -1;
2092 	}
2093 
2094 	mgmt_ch = spdk_io_channel_get_ctx(mgmt_io_ch);
2095 	TAILQ_FOREACH(shared_resource, &mgmt_ch->shared_resources, link) {
2096 		if (shared_resource->shared_ch == ch->channel) {
2097 			spdk_put_io_channel(mgmt_io_ch);
2098 			shared_resource->ref++;
2099 			break;
2100 		}
2101 	}
2102 
2103 	if (shared_resource == NULL) {
2104 		shared_resource = calloc(1, sizeof(*shared_resource));
2105 		if (shared_resource == NULL) {
2106 			spdk_put_io_channel(ch->channel);
2107 			spdk_put_io_channel(mgmt_io_ch);
2108 			return -1;
2109 		}
2110 
2111 		shared_resource->mgmt_ch = mgmt_ch;
2112 		shared_resource->io_outstanding = 0;
2113 		TAILQ_INIT(&shared_resource->nomem_io);
2114 		shared_resource->nomem_threshold = 0;
2115 		shared_resource->shared_ch = ch->channel;
2116 		shared_resource->ref = 1;
2117 		TAILQ_INSERT_TAIL(&mgmt_ch->shared_resources, shared_resource, link);
2118 	}
2119 
2120 	memset(&ch->stat, 0, sizeof(ch->stat));
2121 	ch->stat.ticks_rate = spdk_get_ticks_hz();
2122 	ch->io_outstanding = 0;
2123 	TAILQ_INIT(&ch->queued_resets);
2124 	ch->flags = 0;
2125 	ch->shared_resource = shared_resource;
2126 
2127 #ifdef SPDK_CONFIG_VTUNE
2128 	{
2129 		char *name;
2130 		__itt_init_ittlib(NULL, 0);
2131 		name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch);
2132 		if (!name) {
2133 			_spdk_bdev_channel_destroy_resource(ch);
2134 			return -1;
2135 		}
2136 		ch->handle = __itt_string_handle_create(name);
2137 		free(name);
2138 		ch->start_tsc = spdk_get_ticks();
2139 		ch->interval_tsc = spdk_get_ticks_hz() / 100;
2140 		memset(&ch->prev_stat, 0, sizeof(ch->prev_stat));
2141 	}
2142 #endif
2143 
2144 	pthread_mutex_lock(&bdev->internal.mutex);
2145 	_spdk_bdev_enable_qos(bdev, ch);
2146 	pthread_mutex_unlock(&bdev->internal.mutex);
2147 
2148 	return 0;
2149 }
2150 
2151 /*
2152  * Abort I/O that are waiting on a data buffer.  These types of I/O are
2153  *  linked using the spdk_bdev_io internal.buf_link TAILQ_ENTRY.
2154  */
2155 static void
2156 _spdk_bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_channel *ch)
2157 {
2158 	bdev_io_stailq_t tmp;
2159 	struct spdk_bdev_io *bdev_io;
2160 
2161 	STAILQ_INIT(&tmp);
2162 
2163 	while (!STAILQ_EMPTY(queue)) {
2164 		bdev_io = STAILQ_FIRST(queue);
2165 		STAILQ_REMOVE_HEAD(queue, internal.buf_link);
2166 		if (bdev_io->internal.ch == ch) {
2167 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
2168 		} else {
2169 			STAILQ_INSERT_TAIL(&tmp, bdev_io, internal.buf_link);
2170 		}
2171 	}
2172 
2173 	STAILQ_SWAP(&tmp, queue, spdk_bdev_io);
2174 }
2175 
2176 /*
2177  * Abort I/O that are queued waiting for submission.  These types of I/O are
2178  *  linked using the spdk_bdev_io link TAILQ_ENTRY.
2179  */
2180 static void
2181 _spdk_bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch)
2182 {
2183 	struct spdk_bdev_io *bdev_io, *tmp;
2184 
2185 	TAILQ_FOREACH_SAFE(bdev_io, queue, internal.link, tmp) {
2186 		if (bdev_io->internal.ch == ch) {
2187 			TAILQ_REMOVE(queue, bdev_io, internal.link);
2188 			/*
2189 			 * spdk_bdev_io_complete() assumes that the completed I/O had
2190 			 *  been submitted to the bdev module.  Since in this case it
2191 			 *  hadn't, bump io_outstanding to account for the decrement
2192 			 *  that spdk_bdev_io_complete() will do.
2193 			 */
2194 			if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) {
2195 				ch->io_outstanding++;
2196 				ch->shared_resource->io_outstanding++;
2197 			}
2198 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
2199 		}
2200 	}
2201 }
2202 
2203 static void
2204 spdk_bdev_qos_channel_destroy(void *cb_arg)
2205 {
2206 	struct spdk_bdev_qos *qos = cb_arg;
2207 
2208 	spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
2209 	spdk_poller_unregister(&qos->poller);
2210 
2211 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Free QoS %p.\n", qos);
2212 
2213 	free(qos);
2214 }
2215 
2216 static int
2217 spdk_bdev_qos_destroy(struct spdk_bdev *bdev)
2218 {
2219 	int i;
2220 
2221 	/*
2222 	 * Cleanly shutting down the QoS poller is tricky, because
2223 	 * during the asynchronous operation the user could open
2224 	 * a new descriptor and create a new channel, spawning
2225 	 * a new QoS poller.
2226 	 *
2227 	 * The strategy is to create a new QoS structure here and swap it
2228 	 * in. The shutdown path then continues to refer to the old one
2229 	 * until it completes and then releases it.
2230 	 */
2231 	struct spdk_bdev_qos *new_qos, *old_qos;
2232 
2233 	old_qos = bdev->internal.qos;
2234 
2235 	new_qos = calloc(1, sizeof(*new_qos));
2236 	if (!new_qos) {
2237 		SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n");
2238 		return -ENOMEM;
2239 	}
2240 
2241 	/* Copy the old QoS data into the newly allocated structure */
2242 	memcpy(new_qos, old_qos, sizeof(*new_qos));
2243 
2244 	/* Zero out the key parts of the QoS structure */
2245 	new_qos->ch = NULL;
2246 	new_qos->thread = NULL;
2247 	new_qos->poller = NULL;
2248 	TAILQ_INIT(&new_qos->queued);
2249 	/*
2250 	 * The limit member of spdk_bdev_qos_limit structure is not zeroed.
2251 	 * It will be used later for the new QoS structure.
2252 	 */
2253 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2254 		new_qos->rate_limits[i].remaining_this_timeslice = 0;
2255 		new_qos->rate_limits[i].min_per_timeslice = 0;
2256 		new_qos->rate_limits[i].max_per_timeslice = 0;
2257 	}
2258 
2259 	bdev->internal.qos = new_qos;
2260 
2261 	if (old_qos->thread == NULL) {
2262 		free(old_qos);
2263 	} else {
2264 		spdk_thread_send_msg(old_qos->thread, spdk_bdev_qos_channel_destroy,
2265 				     old_qos);
2266 	}
2267 
2268 	/* It is safe to continue with destroying the bdev even though the QoS channel hasn't
2269 	 * been destroyed yet. The destruction path will end up waiting for the final
2270 	 * channel to be put before it releases resources. */
2271 
2272 	return 0;
2273 }
2274 
2275 static void
2276 _spdk_bdev_io_stat_add(struct spdk_bdev_io_stat *total, struct spdk_bdev_io_stat *add)
2277 {
2278 	total->bytes_read += add->bytes_read;
2279 	total->num_read_ops += add->num_read_ops;
2280 	total->bytes_written += add->bytes_written;
2281 	total->num_write_ops += add->num_write_ops;
2282 	total->bytes_unmapped += add->bytes_unmapped;
2283 	total->num_unmap_ops += add->num_unmap_ops;
2284 	total->read_latency_ticks += add->read_latency_ticks;
2285 	total->write_latency_ticks += add->write_latency_ticks;
2286 	total->unmap_latency_ticks += add->unmap_latency_ticks;
2287 }
2288 
2289 static void
2290 spdk_bdev_channel_destroy(void *io_device, void *ctx_buf)
2291 {
2292 	struct spdk_bdev_channel	*ch = ctx_buf;
2293 	struct spdk_bdev_mgmt_channel	*mgmt_ch;
2294 	struct spdk_bdev_shared_resource *shared_resource = ch->shared_resource;
2295 
2296 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Destroying channel %p for bdev %s on thread %p\n", ch, ch->bdev->name,
2297 		      spdk_get_thread());
2298 
2299 	/* This channel is going away, so add its statistics into the bdev so that they don't get lost. */
2300 	pthread_mutex_lock(&ch->bdev->internal.mutex);
2301 	_spdk_bdev_io_stat_add(&ch->bdev->internal.stat, &ch->stat);
2302 	pthread_mutex_unlock(&ch->bdev->internal.mutex);
2303 
2304 	mgmt_ch = shared_resource->mgmt_ch;
2305 
2306 	_spdk_bdev_abort_queued_io(&ch->queued_resets, ch);
2307 	_spdk_bdev_abort_queued_io(&shared_resource->nomem_io, ch);
2308 	_spdk_bdev_abort_buf_io(&mgmt_ch->need_buf_small, ch);
2309 	_spdk_bdev_abort_buf_io(&mgmt_ch->need_buf_large, ch);
2310 
2311 	if (ch->histogram) {
2312 		spdk_histogram_data_free(ch->histogram);
2313 	}
2314 
2315 	_spdk_bdev_channel_destroy_resource(ch);
2316 }
2317 
2318 int
2319 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
2320 {
2321 	struct spdk_bdev_alias *tmp;
2322 
2323 	if (alias == NULL) {
2324 		SPDK_ERRLOG("Empty alias passed\n");
2325 		return -EINVAL;
2326 	}
2327 
2328 	if (spdk_bdev_get_by_name(alias)) {
2329 		SPDK_ERRLOG("Bdev name/alias: %s already exists\n", alias);
2330 		return -EEXIST;
2331 	}
2332 
2333 	tmp = calloc(1, sizeof(*tmp));
2334 	if (tmp == NULL) {
2335 		SPDK_ERRLOG("Unable to allocate alias\n");
2336 		return -ENOMEM;
2337 	}
2338 
2339 	tmp->alias = strdup(alias);
2340 	if (tmp->alias == NULL) {
2341 		free(tmp);
2342 		SPDK_ERRLOG("Unable to allocate alias\n");
2343 		return -ENOMEM;
2344 	}
2345 
2346 	TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq);
2347 
2348 	return 0;
2349 }
2350 
2351 int
2352 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias)
2353 {
2354 	struct spdk_bdev_alias *tmp;
2355 
2356 	TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
2357 		if (strcmp(alias, tmp->alias) == 0) {
2358 			TAILQ_REMOVE(&bdev->aliases, tmp, tailq);
2359 			free(tmp->alias);
2360 			free(tmp);
2361 			return 0;
2362 		}
2363 	}
2364 
2365 	SPDK_INFOLOG(SPDK_LOG_BDEV, "Alias %s does not exists\n", alias);
2366 
2367 	return -ENOENT;
2368 }
2369 
2370 void
2371 spdk_bdev_alias_del_all(struct spdk_bdev *bdev)
2372 {
2373 	struct spdk_bdev_alias *p, *tmp;
2374 
2375 	TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) {
2376 		TAILQ_REMOVE(&bdev->aliases, p, tailq);
2377 		free(p->alias);
2378 		free(p);
2379 	}
2380 }
2381 
2382 struct spdk_io_channel *
2383 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
2384 {
2385 	return spdk_get_io_channel(__bdev_to_io_dev(spdk_bdev_desc_get_bdev(desc)));
2386 }
2387 
2388 const char *
2389 spdk_bdev_get_name(const struct spdk_bdev *bdev)
2390 {
2391 	return bdev->name;
2392 }
2393 
2394 const char *
2395 spdk_bdev_get_product_name(const struct spdk_bdev *bdev)
2396 {
2397 	return bdev->product_name;
2398 }
2399 
2400 const struct spdk_bdev_aliases_list *
2401 spdk_bdev_get_aliases(const struct spdk_bdev *bdev)
2402 {
2403 	return &bdev->aliases;
2404 }
2405 
2406 uint32_t
2407 spdk_bdev_get_block_size(const struct spdk_bdev *bdev)
2408 {
2409 	return bdev->blocklen;
2410 }
2411 
2412 uint64_t
2413 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
2414 {
2415 	return bdev->blockcnt;
2416 }
2417 
2418 const char *
2419 spdk_bdev_get_qos_rpc_type(enum spdk_bdev_qos_rate_limit_type type)
2420 {
2421 	return qos_rpc_type[type];
2422 }
2423 
2424 void
2425 spdk_bdev_get_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
2426 {
2427 	int i;
2428 
2429 	memset(limits, 0, sizeof(*limits) * SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
2430 
2431 	pthread_mutex_lock(&bdev->internal.mutex);
2432 	if (bdev->internal.qos) {
2433 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2434 			if (bdev->internal.qos->rate_limits[i].limit !=
2435 			    SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
2436 				limits[i] = bdev->internal.qos->rate_limits[i].limit;
2437 				if (_spdk_bdev_qos_is_iops_rate_limit(i) == false) {
2438 					/* Change from Byte to Megabyte which is user visible. */
2439 					limits[i] = limits[i] / 1024 / 1024;
2440 				}
2441 			}
2442 		}
2443 	}
2444 	pthread_mutex_unlock(&bdev->internal.mutex);
2445 }
2446 
2447 size_t
2448 spdk_bdev_get_buf_align(const struct spdk_bdev *bdev)
2449 {
2450 	return 1 << bdev->required_alignment;
2451 }
2452 
2453 uint32_t
2454 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev)
2455 {
2456 	return bdev->optimal_io_boundary;
2457 }
2458 
2459 bool
2460 spdk_bdev_has_write_cache(const struct spdk_bdev *bdev)
2461 {
2462 	return bdev->write_cache;
2463 }
2464 
2465 const struct spdk_uuid *
2466 spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
2467 {
2468 	return &bdev->uuid;
2469 }
2470 
2471 uint32_t
2472 spdk_bdev_get_md_size(const struct spdk_bdev *bdev)
2473 {
2474 	return bdev->md_len;
2475 }
2476 
2477 bool
2478 spdk_bdev_is_md_interleaved(const struct spdk_bdev *bdev)
2479 {
2480 	return (bdev->md_len != 0) && bdev->md_interleave;
2481 }
2482 
2483 bool
2484 spdk_bdev_is_md_separate(const struct spdk_bdev *bdev)
2485 {
2486 	return (bdev->md_len != 0) && !bdev->md_interleave;
2487 }
2488 
2489 uint32_t
2490 spdk_bdev_get_data_block_size(const struct spdk_bdev *bdev)
2491 {
2492 	if (spdk_bdev_is_md_interleaved(bdev)) {
2493 		return bdev->blocklen - bdev->md_len;
2494 	} else {
2495 		return bdev->blocklen;
2496 	}
2497 }
2498 
2499 static uint32_t
2500 _bdev_get_block_size_with_md(const struct spdk_bdev *bdev)
2501 {
2502 	if (!spdk_bdev_is_md_interleaved(bdev)) {
2503 		return bdev->blocklen + bdev->md_len;
2504 	} else {
2505 		return bdev->blocklen;
2506 	}
2507 }
2508 
2509 enum spdk_dif_type spdk_bdev_get_dif_type(const struct spdk_bdev *bdev)
2510 {
2511 	if (bdev->md_len != 0) {
2512 		return bdev->dif_type;
2513 	} else {
2514 		return SPDK_DIF_DISABLE;
2515 	}
2516 }
2517 
2518 bool
2519 spdk_bdev_is_dif_head_of_md(const struct spdk_bdev *bdev)
2520 {
2521 	if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
2522 		return bdev->dif_is_head_of_md;
2523 	} else {
2524 		return false;
2525 	}
2526 }
2527 
2528 bool
2529 spdk_bdev_is_dif_check_enabled(const struct spdk_bdev *bdev,
2530 			       enum spdk_dif_check_type check_type)
2531 {
2532 	if (spdk_bdev_get_dif_type(bdev) == SPDK_DIF_DISABLE) {
2533 		return false;
2534 	}
2535 
2536 	switch (check_type) {
2537 	case SPDK_DIF_CHECK_TYPE_REFTAG:
2538 		return (bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) != 0;
2539 	case SPDK_DIF_CHECK_TYPE_APPTAG:
2540 		return (bdev->dif_check_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) != 0;
2541 	case SPDK_DIF_CHECK_TYPE_GUARD:
2542 		return (bdev->dif_check_flags & SPDK_DIF_FLAGS_GUARD_CHECK) != 0;
2543 	default:
2544 		return false;
2545 	}
2546 }
2547 
2548 uint64_t
2549 spdk_bdev_get_qd(const struct spdk_bdev *bdev)
2550 {
2551 	return bdev->internal.measured_queue_depth;
2552 }
2553 
2554 uint64_t
2555 spdk_bdev_get_qd_sampling_period(const struct spdk_bdev *bdev)
2556 {
2557 	return bdev->internal.period;
2558 }
2559 
2560 uint64_t
2561 spdk_bdev_get_weighted_io_time(const struct spdk_bdev *bdev)
2562 {
2563 	return bdev->internal.weighted_io_time;
2564 }
2565 
2566 uint64_t
2567 spdk_bdev_get_io_time(const struct spdk_bdev *bdev)
2568 {
2569 	return bdev->internal.io_time;
2570 }
2571 
2572 static void
2573 _calculate_measured_qd_cpl(struct spdk_io_channel_iter *i, int status)
2574 {
2575 	struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i);
2576 
2577 	bdev->internal.measured_queue_depth = bdev->internal.temporary_queue_depth;
2578 
2579 	if (bdev->internal.measured_queue_depth) {
2580 		bdev->internal.io_time += bdev->internal.period;
2581 		bdev->internal.weighted_io_time += bdev->internal.period * bdev->internal.measured_queue_depth;
2582 	}
2583 }
2584 
2585 static void
2586 _calculate_measured_qd(struct spdk_io_channel_iter *i)
2587 {
2588 	struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i);
2589 	struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i);
2590 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(io_ch);
2591 
2592 	bdev->internal.temporary_queue_depth += ch->io_outstanding;
2593 	spdk_for_each_channel_continue(i, 0);
2594 }
2595 
2596 static int
2597 spdk_bdev_calculate_measured_queue_depth(void *ctx)
2598 {
2599 	struct spdk_bdev *bdev = ctx;
2600 	bdev->internal.temporary_queue_depth = 0;
2601 	spdk_for_each_channel(__bdev_to_io_dev(bdev), _calculate_measured_qd, bdev,
2602 			      _calculate_measured_qd_cpl);
2603 	return 0;
2604 }
2605 
2606 void
2607 spdk_bdev_set_qd_sampling_period(struct spdk_bdev *bdev, uint64_t period)
2608 {
2609 	bdev->internal.period = period;
2610 
2611 	if (bdev->internal.qd_poller != NULL) {
2612 		spdk_poller_unregister(&bdev->internal.qd_poller);
2613 		bdev->internal.measured_queue_depth = UINT64_MAX;
2614 	}
2615 
2616 	if (period != 0) {
2617 		bdev->internal.qd_poller = spdk_poller_register(spdk_bdev_calculate_measured_queue_depth, bdev,
2618 					   period);
2619 	}
2620 }
2621 
2622 int
2623 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
2624 {
2625 	int ret;
2626 
2627 	pthread_mutex_lock(&bdev->internal.mutex);
2628 
2629 	/* bdev has open descriptors */
2630 	if (!TAILQ_EMPTY(&bdev->internal.open_descs) &&
2631 	    bdev->blockcnt > size) {
2632 		ret = -EBUSY;
2633 	} else {
2634 		bdev->blockcnt = size;
2635 		ret = 0;
2636 	}
2637 
2638 	pthread_mutex_unlock(&bdev->internal.mutex);
2639 
2640 	return ret;
2641 }
2642 
2643 /*
2644  * Convert I/O offset and length from bytes to blocks.
2645  *
2646  * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size.
2647  */
2648 static uint64_t
2649 spdk_bdev_bytes_to_blocks(struct spdk_bdev *bdev, uint64_t offset_bytes, uint64_t *offset_blocks,
2650 			  uint64_t num_bytes, uint64_t *num_blocks)
2651 {
2652 	uint32_t block_size = bdev->blocklen;
2653 	uint8_t shift_cnt;
2654 
2655 	/* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */
2656 	if (spdk_likely(spdk_u32_is_pow2(block_size))) {
2657 		shift_cnt = spdk_u32log2(block_size);
2658 		*offset_blocks = offset_bytes >> shift_cnt;
2659 		*num_blocks = num_bytes >> shift_cnt;
2660 		return (offset_bytes - (*offset_blocks << shift_cnt)) |
2661 		       (num_bytes - (*num_blocks << shift_cnt));
2662 	} else {
2663 		*offset_blocks = offset_bytes / block_size;
2664 		*num_blocks = num_bytes / block_size;
2665 		return (offset_bytes % block_size) | (num_bytes % block_size);
2666 	}
2667 }
2668 
2669 static bool
2670 spdk_bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks)
2671 {
2672 	/* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there
2673 	 * has been an overflow and hence the offset has been wrapped around */
2674 	if (offset_blocks + num_blocks < offset_blocks) {
2675 		return false;
2676 	}
2677 
2678 	/* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */
2679 	if (offset_blocks + num_blocks > bdev->blockcnt) {
2680 		return false;
2681 	}
2682 
2683 	return true;
2684 }
2685 
2686 static bool
2687 _bdev_io_check_md_buf(const struct iovec *iovs, const void *md_buf)
2688 {
2689 	return _is_buf_allocated(iovs) == (md_buf != NULL);
2690 }
2691 
2692 static int
2693 _spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf,
2694 			       void *md_buf, int64_t offset_blocks, uint64_t num_blocks,
2695 			       spdk_bdev_io_completion_cb cb, void *cb_arg)
2696 {
2697 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
2698 	struct spdk_bdev_io *bdev_io;
2699 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2700 
2701 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
2702 		return -EINVAL;
2703 	}
2704 
2705 	bdev_io = spdk_bdev_get_io(channel);
2706 	if (!bdev_io) {
2707 		return -ENOMEM;
2708 	}
2709 
2710 	bdev_io->internal.ch = channel;
2711 	bdev_io->internal.desc = desc;
2712 	bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
2713 	bdev_io->u.bdev.iovs = &bdev_io->iov;
2714 	bdev_io->u.bdev.iovs[0].iov_base = buf;
2715 	bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen;
2716 	bdev_io->u.bdev.iovcnt = 1;
2717 	bdev_io->u.bdev.md_buf = md_buf;
2718 	bdev_io->u.bdev.num_blocks = num_blocks;
2719 	bdev_io->u.bdev.offset_blocks = offset_blocks;
2720 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2721 
2722 	spdk_bdev_io_submit(bdev_io);
2723 	return 0;
2724 }
2725 
2726 int
2727 spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2728 	       void *buf, uint64_t offset, uint64_t nbytes,
2729 	       spdk_bdev_io_completion_cb cb, void *cb_arg)
2730 {
2731 	uint64_t offset_blocks, num_blocks;
2732 
2733 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
2734 				      nbytes, &num_blocks) != 0) {
2735 		return -EINVAL;
2736 	}
2737 
2738 	return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
2739 }
2740 
2741 int
2742 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2743 		      void *buf, uint64_t offset_blocks, uint64_t num_blocks,
2744 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
2745 {
2746 	return _spdk_bdev_read_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
2747 					      cb, cb_arg);
2748 }
2749 
2750 int
2751 spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2752 			      void *buf, void *md_buf, int64_t offset_blocks, uint64_t num_blocks,
2753 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
2754 {
2755 	struct iovec iov = {
2756 		.iov_base = buf,
2757 	};
2758 
2759 	if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
2760 		return -EINVAL;
2761 	}
2762 
2763 	if (!_bdev_io_check_md_buf(&iov, md_buf)) {
2764 		return -EINVAL;
2765 	}
2766 
2767 	return _spdk_bdev_read_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
2768 					      cb, cb_arg);
2769 }
2770 
2771 int
2772 spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2773 		struct iovec *iov, int iovcnt,
2774 		uint64_t offset, uint64_t nbytes,
2775 		spdk_bdev_io_completion_cb cb, void *cb_arg)
2776 {
2777 	uint64_t offset_blocks, num_blocks;
2778 
2779 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
2780 				      nbytes, &num_blocks) != 0) {
2781 		return -EINVAL;
2782 	}
2783 
2784 	return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
2785 }
2786 
2787 static int
2788 _spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2789 				struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
2790 				uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg)
2791 {
2792 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
2793 	struct spdk_bdev_io *bdev_io;
2794 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2795 
2796 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
2797 		return -EINVAL;
2798 	}
2799 
2800 	bdev_io = spdk_bdev_get_io(channel);
2801 	if (!bdev_io) {
2802 		return -ENOMEM;
2803 	}
2804 
2805 	bdev_io->internal.ch = channel;
2806 	bdev_io->internal.desc = desc;
2807 	bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
2808 	bdev_io->u.bdev.iovs = iov;
2809 	bdev_io->u.bdev.iovcnt = iovcnt;
2810 	bdev_io->u.bdev.md_buf = md_buf;
2811 	bdev_io->u.bdev.num_blocks = num_blocks;
2812 	bdev_io->u.bdev.offset_blocks = offset_blocks;
2813 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2814 
2815 	spdk_bdev_io_submit(bdev_io);
2816 	return 0;
2817 }
2818 
2819 int spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2820 			   struct iovec *iov, int iovcnt,
2821 			   uint64_t offset_blocks, uint64_t num_blocks,
2822 			   spdk_bdev_io_completion_cb cb, void *cb_arg)
2823 {
2824 	return _spdk_bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
2825 					       num_blocks, cb, cb_arg);
2826 }
2827 
2828 int
2829 spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2830 			       struct iovec *iov, int iovcnt, void *md_buf,
2831 			       uint64_t offset_blocks, uint64_t num_blocks,
2832 			       spdk_bdev_io_completion_cb cb, void *cb_arg)
2833 {
2834 	if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
2835 		return -EINVAL;
2836 	}
2837 
2838 	if (!_bdev_io_check_md_buf(iov, md_buf)) {
2839 		return -EINVAL;
2840 	}
2841 
2842 	return _spdk_bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
2843 					       num_blocks, cb, cb_arg);
2844 }
2845 
2846 static int
2847 _spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2848 				void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
2849 				spdk_bdev_io_completion_cb cb, void *cb_arg)
2850 {
2851 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
2852 	struct spdk_bdev_io *bdev_io;
2853 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2854 
2855 	if (!desc->write) {
2856 		return -EBADF;
2857 	}
2858 
2859 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
2860 		return -EINVAL;
2861 	}
2862 
2863 	bdev_io = spdk_bdev_get_io(channel);
2864 	if (!bdev_io) {
2865 		return -ENOMEM;
2866 	}
2867 
2868 	bdev_io->internal.ch = channel;
2869 	bdev_io->internal.desc = desc;
2870 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
2871 	bdev_io->u.bdev.iovs = &bdev_io->iov;
2872 	bdev_io->u.bdev.iovs[0].iov_base = buf;
2873 	bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen;
2874 	bdev_io->u.bdev.iovcnt = 1;
2875 	bdev_io->u.bdev.md_buf = md_buf;
2876 	bdev_io->u.bdev.num_blocks = num_blocks;
2877 	bdev_io->u.bdev.offset_blocks = offset_blocks;
2878 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2879 
2880 	spdk_bdev_io_submit(bdev_io);
2881 	return 0;
2882 }
2883 
2884 int
2885 spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2886 		void *buf, uint64_t offset, uint64_t nbytes,
2887 		spdk_bdev_io_completion_cb cb, void *cb_arg)
2888 {
2889 	uint64_t offset_blocks, num_blocks;
2890 
2891 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
2892 				      nbytes, &num_blocks) != 0) {
2893 		return -EINVAL;
2894 	}
2895 
2896 	return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
2897 }
2898 
2899 int
2900 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2901 		       void *buf, uint64_t offset_blocks, uint64_t num_blocks,
2902 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
2903 {
2904 	return _spdk_bdev_write_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
2905 					       cb, cb_arg);
2906 }
2907 
2908 int
2909 spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2910 			       void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
2911 			       spdk_bdev_io_completion_cb cb, void *cb_arg)
2912 {
2913 	struct iovec iov = {
2914 		.iov_base = buf,
2915 	};
2916 
2917 	if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
2918 		return -EINVAL;
2919 	}
2920 
2921 	if (!_bdev_io_check_md_buf(&iov, md_buf)) {
2922 		return -EINVAL;
2923 	}
2924 
2925 	return _spdk_bdev_write_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
2926 					       cb, cb_arg);
2927 }
2928 
2929 static int
2930 _spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2931 				 struct iovec *iov, int iovcnt, void *md_buf,
2932 				 uint64_t offset_blocks, uint64_t num_blocks,
2933 				 spdk_bdev_io_completion_cb cb, void *cb_arg)
2934 {
2935 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
2936 	struct spdk_bdev_io *bdev_io;
2937 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
2938 
2939 	if (!desc->write) {
2940 		return -EBADF;
2941 	}
2942 
2943 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
2944 		return -EINVAL;
2945 	}
2946 
2947 	bdev_io = spdk_bdev_get_io(channel);
2948 	if (!bdev_io) {
2949 		return -ENOMEM;
2950 	}
2951 
2952 	bdev_io->internal.ch = channel;
2953 	bdev_io->internal.desc = desc;
2954 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
2955 	bdev_io->u.bdev.iovs = iov;
2956 	bdev_io->u.bdev.iovcnt = iovcnt;
2957 	bdev_io->u.bdev.md_buf = md_buf;
2958 	bdev_io->u.bdev.num_blocks = num_blocks;
2959 	bdev_io->u.bdev.offset_blocks = offset_blocks;
2960 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
2961 
2962 	spdk_bdev_io_submit(bdev_io);
2963 	return 0;
2964 }
2965 
2966 int
2967 spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2968 		 struct iovec *iov, int iovcnt,
2969 		 uint64_t offset, uint64_t len,
2970 		 spdk_bdev_io_completion_cb cb, void *cb_arg)
2971 {
2972 	uint64_t offset_blocks, num_blocks;
2973 
2974 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
2975 				      len, &num_blocks) != 0) {
2976 		return -EINVAL;
2977 	}
2978 
2979 	return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
2980 }
2981 
2982 int
2983 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2984 			struct iovec *iov, int iovcnt,
2985 			uint64_t offset_blocks, uint64_t num_blocks,
2986 			spdk_bdev_io_completion_cb cb, void *cb_arg)
2987 {
2988 	return _spdk_bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
2989 						num_blocks, cb, cb_arg);
2990 }
2991 
2992 int
2993 spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
2994 				struct iovec *iov, int iovcnt, void *md_buf,
2995 				uint64_t offset_blocks, uint64_t num_blocks,
2996 				spdk_bdev_io_completion_cb cb, void *cb_arg)
2997 {
2998 	if (!spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
2999 		return -EINVAL;
3000 	}
3001 
3002 	if (!_bdev_io_check_md_buf(iov, md_buf)) {
3003 		return -EINVAL;
3004 	}
3005 
3006 	return _spdk_bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
3007 						num_blocks, cb, cb_arg);
3008 }
3009 
3010 static void
3011 bdev_zcopy_get_buf(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success)
3012 {
3013 	if (!success) {
3014 		/* Don't use spdk_bdev_io_complete here - this bdev_io was never actually submitted. */
3015 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_NOMEM;
3016 		bdev_io->internal.cb(bdev_io, success, bdev_io->internal.caller_ctx);
3017 		return;
3018 	}
3019 
3020 	if (bdev_io->u.bdev.zcopy.populate) {
3021 		/* Read the real data into the buffer */
3022 		bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
3023 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
3024 		spdk_bdev_io_submit(bdev_io);
3025 		return;
3026 	}
3027 
3028 	/* Don't use spdk_bdev_io_complete here - this bdev_io was never actually submitted. */
3029 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
3030 	bdev_io->internal.cb(bdev_io, success, bdev_io->internal.caller_ctx);
3031 }
3032 
3033 int
3034 spdk_bdev_zcopy_start(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3035 		      uint64_t offset_blocks, uint64_t num_blocks,
3036 		      bool populate,
3037 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
3038 {
3039 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3040 	struct spdk_bdev_io *bdev_io;
3041 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3042 
3043 	if (!desc->write) {
3044 		return -EBADF;
3045 	}
3046 
3047 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
3048 		return -EINVAL;
3049 	}
3050 
3051 	if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) {
3052 		return -ENOTSUP;
3053 	}
3054 
3055 	bdev_io = spdk_bdev_get_io(channel);
3056 	if (!bdev_io) {
3057 		return -ENOMEM;
3058 	}
3059 
3060 	bdev_io->internal.ch = channel;
3061 	bdev_io->internal.desc = desc;
3062 	bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY;
3063 	bdev_io->u.bdev.num_blocks = num_blocks;
3064 	bdev_io->u.bdev.offset_blocks = offset_blocks;
3065 	bdev_io->u.bdev.iovs = NULL;
3066 	bdev_io->u.bdev.iovcnt = 0;
3067 	bdev_io->u.bdev.zcopy.populate = populate ? 1 : 0;
3068 	bdev_io->u.bdev.zcopy.commit = 0;
3069 	bdev_io->u.bdev.zcopy.start = 1;
3070 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3071 
3072 	if (_spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) {
3073 		spdk_bdev_io_submit(bdev_io);
3074 	} else {
3075 		/* Emulate zcopy by allocating a buffer */
3076 		spdk_bdev_io_get_buf(bdev_io, bdev_zcopy_get_buf,
3077 				     bdev_io->u.bdev.num_blocks * bdev->blocklen);
3078 	}
3079 
3080 	return 0;
3081 }
3082 
3083 int
3084 spdk_bdev_zcopy_end(struct spdk_bdev_io *bdev_io, bool commit,
3085 		    spdk_bdev_io_completion_cb cb, void *cb_arg)
3086 {
3087 	struct spdk_bdev *bdev = bdev_io->bdev;
3088 
3089 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
3090 		/* This can happen if the zcopy was emulated in start */
3091 		if (bdev_io->u.bdev.zcopy.start != 1) {
3092 			return -EINVAL;
3093 		}
3094 		bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY;
3095 	}
3096 
3097 	if (bdev_io->type != SPDK_BDEV_IO_TYPE_ZCOPY) {
3098 		return -EINVAL;
3099 	}
3100 
3101 	bdev_io->u.bdev.zcopy.commit = commit ? 1 : 0;
3102 	bdev_io->u.bdev.zcopy.start = 0;
3103 	bdev_io->internal.caller_ctx = cb_arg;
3104 	bdev_io->internal.cb = cb;
3105 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
3106 
3107 	if (_spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) {
3108 		spdk_bdev_io_submit(bdev_io);
3109 		return 0;
3110 	}
3111 
3112 	if (!bdev_io->u.bdev.zcopy.commit) {
3113 		/* Don't use spdk_bdev_io_complete here - this bdev_io was never actually submitted. */
3114 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
3115 		bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx);
3116 		return 0;
3117 	}
3118 
3119 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
3120 	spdk_bdev_io_submit(bdev_io);
3121 
3122 	return 0;
3123 }
3124 
3125 int
3126 spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3127 		       uint64_t offset, uint64_t len,
3128 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
3129 {
3130 	uint64_t offset_blocks, num_blocks;
3131 
3132 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
3133 				      len, &num_blocks) != 0) {
3134 		return -EINVAL;
3135 	}
3136 
3137 	return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
3138 }
3139 
3140 int
3141 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3142 			      uint64_t offset_blocks, uint64_t num_blocks,
3143 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
3144 {
3145 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3146 	struct spdk_bdev_io *bdev_io;
3147 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3148 
3149 	if (!desc->write) {
3150 		return -EBADF;
3151 	}
3152 
3153 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
3154 		return -EINVAL;
3155 	}
3156 
3157 	if (!_spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) &&
3158 	    !_spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)) {
3159 		return -ENOTSUP;
3160 	}
3161 
3162 	bdev_io = spdk_bdev_get_io(channel);
3163 
3164 	if (!bdev_io) {
3165 		return -ENOMEM;
3166 	}
3167 
3168 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES;
3169 	bdev_io->internal.ch = channel;
3170 	bdev_io->internal.desc = desc;
3171 	bdev_io->u.bdev.offset_blocks = offset_blocks;
3172 	bdev_io->u.bdev.num_blocks = num_blocks;
3173 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3174 
3175 	if (_spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
3176 		spdk_bdev_io_submit(bdev_io);
3177 		return 0;
3178 	}
3179 
3180 	assert(_spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE));
3181 	assert(_bdev_get_block_size_with_md(bdev) <= ZERO_BUFFER_SIZE);
3182 	bdev_io->u.bdev.split_remaining_num_blocks = num_blocks;
3183 	bdev_io->u.bdev.split_current_offset_blocks = offset_blocks;
3184 	_spdk_bdev_write_zero_buffer_next(bdev_io);
3185 
3186 	return 0;
3187 }
3188 
3189 int
3190 spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3191 		uint64_t offset, uint64_t nbytes,
3192 		spdk_bdev_io_completion_cb cb, void *cb_arg)
3193 {
3194 	uint64_t offset_blocks, num_blocks;
3195 
3196 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
3197 				      nbytes, &num_blocks) != 0) {
3198 		return -EINVAL;
3199 	}
3200 
3201 	return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
3202 }
3203 
3204 int
3205 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3206 		       uint64_t offset_blocks, uint64_t num_blocks,
3207 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
3208 {
3209 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3210 	struct spdk_bdev_io *bdev_io;
3211 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3212 
3213 	if (!desc->write) {
3214 		return -EBADF;
3215 	}
3216 
3217 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
3218 		return -EINVAL;
3219 	}
3220 
3221 	if (num_blocks == 0) {
3222 		SPDK_ERRLOG("Can't unmap 0 bytes\n");
3223 		return -EINVAL;
3224 	}
3225 
3226 	bdev_io = spdk_bdev_get_io(channel);
3227 	if (!bdev_io) {
3228 		return -ENOMEM;
3229 	}
3230 
3231 	bdev_io->internal.ch = channel;
3232 	bdev_io->internal.desc = desc;
3233 	bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP;
3234 
3235 	bdev_io->u.bdev.iovs = &bdev_io->iov;
3236 	bdev_io->u.bdev.iovs[0].iov_base = NULL;
3237 	bdev_io->u.bdev.iovs[0].iov_len = 0;
3238 	bdev_io->u.bdev.iovcnt = 1;
3239 
3240 	bdev_io->u.bdev.offset_blocks = offset_blocks;
3241 	bdev_io->u.bdev.num_blocks = num_blocks;
3242 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3243 
3244 	spdk_bdev_io_submit(bdev_io);
3245 	return 0;
3246 }
3247 
3248 int
3249 spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3250 		uint64_t offset, uint64_t length,
3251 		spdk_bdev_io_completion_cb cb, void *cb_arg)
3252 {
3253 	uint64_t offset_blocks, num_blocks;
3254 
3255 	if (spdk_bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
3256 				      length, &num_blocks) != 0) {
3257 		return -EINVAL;
3258 	}
3259 
3260 	return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
3261 }
3262 
3263 int
3264 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3265 		       uint64_t offset_blocks, uint64_t num_blocks,
3266 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
3267 {
3268 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3269 	struct spdk_bdev_io *bdev_io;
3270 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3271 
3272 	if (!desc->write) {
3273 		return -EBADF;
3274 	}
3275 
3276 	if (!spdk_bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
3277 		return -EINVAL;
3278 	}
3279 
3280 	bdev_io = spdk_bdev_get_io(channel);
3281 	if (!bdev_io) {
3282 		return -ENOMEM;
3283 	}
3284 
3285 	bdev_io->internal.ch = channel;
3286 	bdev_io->internal.desc = desc;
3287 	bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH;
3288 	bdev_io->u.bdev.iovs = NULL;
3289 	bdev_io->u.bdev.iovcnt = 0;
3290 	bdev_io->u.bdev.offset_blocks = offset_blocks;
3291 	bdev_io->u.bdev.num_blocks = num_blocks;
3292 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3293 
3294 	spdk_bdev_io_submit(bdev_io);
3295 	return 0;
3296 }
3297 
3298 static void
3299 _spdk_bdev_reset_dev(struct spdk_io_channel_iter *i, int status)
3300 {
3301 	struct spdk_bdev_channel *ch = spdk_io_channel_iter_get_ctx(i);
3302 	struct spdk_bdev_io *bdev_io;
3303 
3304 	bdev_io = TAILQ_FIRST(&ch->queued_resets);
3305 	TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
3306 	spdk_bdev_io_submit_reset(bdev_io);
3307 }
3308 
3309 static void
3310 _spdk_bdev_reset_freeze_channel(struct spdk_io_channel_iter *i)
3311 {
3312 	struct spdk_io_channel		*ch;
3313 	struct spdk_bdev_channel	*channel;
3314 	struct spdk_bdev_mgmt_channel	*mgmt_channel;
3315 	struct spdk_bdev_shared_resource *shared_resource;
3316 	bdev_io_tailq_t			tmp_queued;
3317 
3318 	TAILQ_INIT(&tmp_queued);
3319 
3320 	ch = spdk_io_channel_iter_get_channel(i);
3321 	channel = spdk_io_channel_get_ctx(ch);
3322 	shared_resource = channel->shared_resource;
3323 	mgmt_channel = shared_resource->mgmt_ch;
3324 
3325 	channel->flags |= BDEV_CH_RESET_IN_PROGRESS;
3326 
3327 	if ((channel->flags & BDEV_CH_QOS_ENABLED) != 0) {
3328 		/* The QoS object is always valid and readable while
3329 		 * the channel flag is set, so the lock here should not
3330 		 * be necessary. We're not in the fast path though, so
3331 		 * just take it anyway. */
3332 		pthread_mutex_lock(&channel->bdev->internal.mutex);
3333 		if (channel->bdev->internal.qos->ch == channel) {
3334 			TAILQ_SWAP(&channel->bdev->internal.qos->queued, &tmp_queued, spdk_bdev_io, internal.link);
3335 		}
3336 		pthread_mutex_unlock(&channel->bdev->internal.mutex);
3337 	}
3338 
3339 	_spdk_bdev_abort_queued_io(&shared_resource->nomem_io, channel);
3340 	_spdk_bdev_abort_buf_io(&mgmt_channel->need_buf_small, channel);
3341 	_spdk_bdev_abort_buf_io(&mgmt_channel->need_buf_large, channel);
3342 	_spdk_bdev_abort_queued_io(&tmp_queued, channel);
3343 
3344 	spdk_for_each_channel_continue(i, 0);
3345 }
3346 
3347 static void
3348 _spdk_bdev_start_reset(void *ctx)
3349 {
3350 	struct spdk_bdev_channel *ch = ctx;
3351 
3352 	spdk_for_each_channel(__bdev_to_io_dev(ch->bdev), _spdk_bdev_reset_freeze_channel,
3353 			      ch, _spdk_bdev_reset_dev);
3354 }
3355 
3356 static void
3357 _spdk_bdev_channel_start_reset(struct spdk_bdev_channel *ch)
3358 {
3359 	struct spdk_bdev *bdev = ch->bdev;
3360 
3361 	assert(!TAILQ_EMPTY(&ch->queued_resets));
3362 
3363 	pthread_mutex_lock(&bdev->internal.mutex);
3364 	if (bdev->internal.reset_in_progress == NULL) {
3365 		bdev->internal.reset_in_progress = TAILQ_FIRST(&ch->queued_resets);
3366 		/*
3367 		 * Take a channel reference for the target bdev for the life of this
3368 		 *  reset.  This guards against the channel getting destroyed while
3369 		 *  spdk_for_each_channel() calls related to this reset IO are in
3370 		 *  progress.  We will release the reference when this reset is
3371 		 *  completed.
3372 		 */
3373 		bdev->internal.reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev));
3374 		_spdk_bdev_start_reset(ch);
3375 	}
3376 	pthread_mutex_unlock(&bdev->internal.mutex);
3377 }
3378 
3379 int
3380 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3381 		spdk_bdev_io_completion_cb cb, void *cb_arg)
3382 {
3383 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3384 	struct spdk_bdev_io *bdev_io;
3385 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3386 
3387 	bdev_io = spdk_bdev_get_io(channel);
3388 	if (!bdev_io) {
3389 		return -ENOMEM;
3390 	}
3391 
3392 	bdev_io->internal.ch = channel;
3393 	bdev_io->internal.desc = desc;
3394 	bdev_io->type = SPDK_BDEV_IO_TYPE_RESET;
3395 	bdev_io->u.reset.ch_ref = NULL;
3396 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3397 
3398 	pthread_mutex_lock(&bdev->internal.mutex);
3399 	TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, internal.link);
3400 	pthread_mutex_unlock(&bdev->internal.mutex);
3401 
3402 	_spdk_bdev_channel_start_reset(channel);
3403 
3404 	return 0;
3405 }
3406 
3407 void
3408 spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
3409 		      struct spdk_bdev_io_stat *stat)
3410 {
3411 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3412 
3413 	*stat = channel->stat;
3414 }
3415 
3416 static void
3417 _spdk_bdev_get_device_stat_done(struct spdk_io_channel_iter *i, int status)
3418 {
3419 	void *io_device = spdk_io_channel_iter_get_io_device(i);
3420 	struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i);
3421 
3422 	bdev_iostat_ctx->cb(__bdev_from_io_dev(io_device), bdev_iostat_ctx->stat,
3423 			    bdev_iostat_ctx->cb_arg, 0);
3424 	free(bdev_iostat_ctx);
3425 }
3426 
3427 static void
3428 _spdk_bdev_get_each_channel_stat(struct spdk_io_channel_iter *i)
3429 {
3430 	struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i);
3431 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
3432 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3433 
3434 	_spdk_bdev_io_stat_add(bdev_iostat_ctx->stat, &channel->stat);
3435 	spdk_for_each_channel_continue(i, 0);
3436 }
3437 
3438 void
3439 spdk_bdev_get_device_stat(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat,
3440 			  spdk_bdev_get_device_stat_cb cb, void *cb_arg)
3441 {
3442 	struct spdk_bdev_iostat_ctx *bdev_iostat_ctx;
3443 
3444 	assert(bdev != NULL);
3445 	assert(stat != NULL);
3446 	assert(cb != NULL);
3447 
3448 	bdev_iostat_ctx = calloc(1, sizeof(struct spdk_bdev_iostat_ctx));
3449 	if (bdev_iostat_ctx == NULL) {
3450 		SPDK_ERRLOG("Unable to allocate memory for spdk_bdev_iostat_ctx\n");
3451 		cb(bdev, stat, cb_arg, -ENOMEM);
3452 		return;
3453 	}
3454 
3455 	bdev_iostat_ctx->stat = stat;
3456 	bdev_iostat_ctx->cb = cb;
3457 	bdev_iostat_ctx->cb_arg = cb_arg;
3458 
3459 	/* Start with the statistics from previously deleted channels. */
3460 	pthread_mutex_lock(&bdev->internal.mutex);
3461 	_spdk_bdev_io_stat_add(bdev_iostat_ctx->stat, &bdev->internal.stat);
3462 	pthread_mutex_unlock(&bdev->internal.mutex);
3463 
3464 	/* Then iterate and add the statistics from each existing channel. */
3465 	spdk_for_each_channel(__bdev_to_io_dev(bdev),
3466 			      _spdk_bdev_get_each_channel_stat,
3467 			      bdev_iostat_ctx,
3468 			      _spdk_bdev_get_device_stat_done);
3469 }
3470 
3471 int
3472 spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3473 			      const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
3474 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
3475 {
3476 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3477 	struct spdk_bdev_io *bdev_io;
3478 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3479 
3480 	if (!desc->write) {
3481 		return -EBADF;
3482 	}
3483 
3484 	bdev_io = spdk_bdev_get_io(channel);
3485 	if (!bdev_io) {
3486 		return -ENOMEM;
3487 	}
3488 
3489 	bdev_io->internal.ch = channel;
3490 	bdev_io->internal.desc = desc;
3491 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN;
3492 	bdev_io->u.nvme_passthru.cmd = *cmd;
3493 	bdev_io->u.nvme_passthru.buf = buf;
3494 	bdev_io->u.nvme_passthru.nbytes = nbytes;
3495 	bdev_io->u.nvme_passthru.md_buf = NULL;
3496 	bdev_io->u.nvme_passthru.md_len = 0;
3497 
3498 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3499 
3500 	spdk_bdev_io_submit(bdev_io);
3501 	return 0;
3502 }
3503 
3504 int
3505 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3506 			   const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
3507 			   spdk_bdev_io_completion_cb cb, void *cb_arg)
3508 {
3509 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3510 	struct spdk_bdev_io *bdev_io;
3511 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3512 
3513 	if (!desc->write) {
3514 		/*
3515 		 * Do not try to parse the NVMe command - we could maybe use bits in the opcode
3516 		 *  to easily determine if the command is a read or write, but for now just
3517 		 *  do not allow io_passthru with a read-only descriptor.
3518 		 */
3519 		return -EBADF;
3520 	}
3521 
3522 	bdev_io = spdk_bdev_get_io(channel);
3523 	if (!bdev_io) {
3524 		return -ENOMEM;
3525 	}
3526 
3527 	bdev_io->internal.ch = channel;
3528 	bdev_io->internal.desc = desc;
3529 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO;
3530 	bdev_io->u.nvme_passthru.cmd = *cmd;
3531 	bdev_io->u.nvme_passthru.buf = buf;
3532 	bdev_io->u.nvme_passthru.nbytes = nbytes;
3533 	bdev_io->u.nvme_passthru.md_buf = NULL;
3534 	bdev_io->u.nvme_passthru.md_len = 0;
3535 
3536 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3537 
3538 	spdk_bdev_io_submit(bdev_io);
3539 	return 0;
3540 }
3541 
3542 int
3543 spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
3544 			      const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len,
3545 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
3546 {
3547 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3548 	struct spdk_bdev_io *bdev_io;
3549 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3550 
3551 	if (!desc->write) {
3552 		/*
3553 		 * Do not try to parse the NVMe command - we could maybe use bits in the opcode
3554 		 *  to easily determine if the command is a read or write, but for now just
3555 		 *  do not allow io_passthru with a read-only descriptor.
3556 		 */
3557 		return -EBADF;
3558 	}
3559 
3560 	bdev_io = spdk_bdev_get_io(channel);
3561 	if (!bdev_io) {
3562 		return -ENOMEM;
3563 	}
3564 
3565 	bdev_io->internal.ch = channel;
3566 	bdev_io->internal.desc = desc;
3567 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD;
3568 	bdev_io->u.nvme_passthru.cmd = *cmd;
3569 	bdev_io->u.nvme_passthru.buf = buf;
3570 	bdev_io->u.nvme_passthru.nbytes = nbytes;
3571 	bdev_io->u.nvme_passthru.md_buf = md_buf;
3572 	bdev_io->u.nvme_passthru.md_len = md_len;
3573 
3574 	spdk_bdev_io_init(bdev_io, bdev, cb_arg, cb);
3575 
3576 	spdk_bdev_io_submit(bdev_io);
3577 	return 0;
3578 }
3579 
3580 int
3581 spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
3582 			struct spdk_bdev_io_wait_entry *entry)
3583 {
3584 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
3585 	struct spdk_bdev_mgmt_channel *mgmt_ch = channel->shared_resource->mgmt_ch;
3586 
3587 	if (bdev != entry->bdev) {
3588 		SPDK_ERRLOG("bdevs do not match\n");
3589 		return -EINVAL;
3590 	}
3591 
3592 	if (mgmt_ch->per_thread_cache_count > 0) {
3593 		SPDK_ERRLOG("Cannot queue io_wait if spdk_bdev_io available in per-thread cache\n");
3594 		return -EINVAL;
3595 	}
3596 
3597 	TAILQ_INSERT_TAIL(&mgmt_ch->io_wait_queue, entry, link);
3598 	return 0;
3599 }
3600 
3601 static void
3602 _spdk_bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch)
3603 {
3604 	struct spdk_bdev *bdev = bdev_ch->bdev;
3605 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
3606 	struct spdk_bdev_io *bdev_io;
3607 
3608 	if (shared_resource->io_outstanding > shared_resource->nomem_threshold) {
3609 		/*
3610 		 * Allow some more I/O to complete before retrying the nomem_io queue.
3611 		 *  Some drivers (such as nvme) cannot immediately take a new I/O in
3612 		 *  the context of a completion, because the resources for the I/O are
3613 		 *  not released until control returns to the bdev poller.  Also, we
3614 		 *  may require several small I/O to complete before a larger I/O
3615 		 *  (that requires splitting) can be submitted.
3616 		 */
3617 		return;
3618 	}
3619 
3620 	while (!TAILQ_EMPTY(&shared_resource->nomem_io)) {
3621 		bdev_io = TAILQ_FIRST(&shared_resource->nomem_io);
3622 		TAILQ_REMOVE(&shared_resource->nomem_io, bdev_io, internal.link);
3623 		bdev_io->internal.ch->io_outstanding++;
3624 		shared_resource->io_outstanding++;
3625 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
3626 		bdev->fn_table->submit_request(spdk_bdev_io_get_io_channel(bdev_io), bdev_io);
3627 		if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
3628 			break;
3629 		}
3630 	}
3631 }
3632 
3633 static inline void
3634 _spdk_bdev_io_complete(void *ctx)
3635 {
3636 	struct spdk_bdev_io *bdev_io = ctx;
3637 	uint64_t tsc, tsc_diff;
3638 
3639 	if (spdk_unlikely(bdev_io->internal.in_submit_request || bdev_io->internal.io_submit_ch)) {
3640 		/*
3641 		 * Send the completion to the thread that originally submitted the I/O,
3642 		 * which may not be the current thread in the case of QoS.
3643 		 */
3644 		if (bdev_io->internal.io_submit_ch) {
3645 			bdev_io->internal.ch = bdev_io->internal.io_submit_ch;
3646 			bdev_io->internal.io_submit_ch = NULL;
3647 		}
3648 
3649 		/*
3650 		 * Defer completion to avoid potential infinite recursion if the
3651 		 * user's completion callback issues a new I/O.
3652 		 */
3653 		spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
3654 				     _spdk_bdev_io_complete, bdev_io);
3655 		return;
3656 	}
3657 
3658 	tsc = spdk_get_ticks();
3659 	tsc_diff = tsc - bdev_io->internal.submit_tsc;
3660 	spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, 0);
3661 
3662 	if (bdev_io->internal.ch->histogram) {
3663 		spdk_histogram_data_tally(bdev_io->internal.ch->histogram, tsc_diff);
3664 	}
3665 
3666 	if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
3667 		switch (bdev_io->type) {
3668 		case SPDK_BDEV_IO_TYPE_READ:
3669 			bdev_io->internal.ch->stat.bytes_read += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
3670 			bdev_io->internal.ch->stat.num_read_ops++;
3671 			bdev_io->internal.ch->stat.read_latency_ticks += tsc_diff;
3672 			break;
3673 		case SPDK_BDEV_IO_TYPE_WRITE:
3674 			bdev_io->internal.ch->stat.bytes_written += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
3675 			bdev_io->internal.ch->stat.num_write_ops++;
3676 			bdev_io->internal.ch->stat.write_latency_ticks += tsc_diff;
3677 			break;
3678 		case SPDK_BDEV_IO_TYPE_UNMAP:
3679 			bdev_io->internal.ch->stat.bytes_unmapped += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
3680 			bdev_io->internal.ch->stat.num_unmap_ops++;
3681 			bdev_io->internal.ch->stat.unmap_latency_ticks += tsc_diff;
3682 		default:
3683 			break;
3684 		}
3685 	}
3686 
3687 #ifdef SPDK_CONFIG_VTUNE
3688 	uint64_t now_tsc = spdk_get_ticks();
3689 	if (now_tsc > (bdev_io->internal.ch->start_tsc + bdev_io->internal.ch->interval_tsc)) {
3690 		uint64_t data[5];
3691 
3692 		data[0] = bdev_io->internal.ch->stat.num_read_ops - bdev_io->internal.ch->prev_stat.num_read_ops;
3693 		data[1] = bdev_io->internal.ch->stat.bytes_read - bdev_io->internal.ch->prev_stat.bytes_read;
3694 		data[2] = bdev_io->internal.ch->stat.num_write_ops - bdev_io->internal.ch->prev_stat.num_write_ops;
3695 		data[3] = bdev_io->internal.ch->stat.bytes_written - bdev_io->internal.ch->prev_stat.bytes_written;
3696 		data[4] = bdev_io->bdev->fn_table->get_spin_time ?
3697 			  bdev_io->bdev->fn_table->get_spin_time(spdk_bdev_io_get_io_channel(bdev_io)) : 0;
3698 
3699 		__itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->internal.ch->handle,
3700 				   __itt_metadata_u64, 5, data);
3701 
3702 		bdev_io->internal.ch->prev_stat = bdev_io->internal.ch->stat;
3703 		bdev_io->internal.ch->start_tsc = now_tsc;
3704 	}
3705 #endif
3706 
3707 	assert(bdev_io->internal.cb != NULL);
3708 	assert(spdk_get_thread() == spdk_bdev_io_get_thread(bdev_io));
3709 
3710 	bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
3711 			     bdev_io->internal.caller_ctx);
3712 }
3713 
3714 static void
3715 _spdk_bdev_reset_complete(struct spdk_io_channel_iter *i, int status)
3716 {
3717 	struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i);
3718 
3719 	if (bdev_io->u.reset.ch_ref != NULL) {
3720 		spdk_put_io_channel(bdev_io->u.reset.ch_ref);
3721 		bdev_io->u.reset.ch_ref = NULL;
3722 	}
3723 
3724 	_spdk_bdev_io_complete(bdev_io);
3725 }
3726 
3727 static void
3728 _spdk_bdev_unfreeze_channel(struct spdk_io_channel_iter *i)
3729 {
3730 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
3731 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
3732 
3733 	ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS;
3734 	if (!TAILQ_EMPTY(&ch->queued_resets)) {
3735 		_spdk_bdev_channel_start_reset(ch);
3736 	}
3737 
3738 	spdk_for_each_channel_continue(i, 0);
3739 }
3740 
3741 void
3742 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
3743 {
3744 	struct spdk_bdev *bdev = bdev_io->bdev;
3745 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
3746 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
3747 
3748 	bdev_io->internal.status = status;
3749 
3750 	if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) {
3751 		bool unlock_channels = false;
3752 
3753 		if (status == SPDK_BDEV_IO_STATUS_NOMEM) {
3754 			SPDK_ERRLOG("NOMEM returned for reset\n");
3755 		}
3756 		pthread_mutex_lock(&bdev->internal.mutex);
3757 		if (bdev_io == bdev->internal.reset_in_progress) {
3758 			bdev->internal.reset_in_progress = NULL;
3759 			unlock_channels = true;
3760 		}
3761 		pthread_mutex_unlock(&bdev->internal.mutex);
3762 
3763 		if (unlock_channels) {
3764 			spdk_for_each_channel(__bdev_to_io_dev(bdev), _spdk_bdev_unfreeze_channel,
3765 					      bdev_io, _spdk_bdev_reset_complete);
3766 			return;
3767 		}
3768 	} else {
3769 		_bdev_io_unset_bounce_buf(bdev_io);
3770 
3771 		assert(bdev_ch->io_outstanding > 0);
3772 		assert(shared_resource->io_outstanding > 0);
3773 		bdev_ch->io_outstanding--;
3774 		shared_resource->io_outstanding--;
3775 
3776 		if (spdk_unlikely(status == SPDK_BDEV_IO_STATUS_NOMEM)) {
3777 			TAILQ_INSERT_HEAD(&shared_resource->nomem_io, bdev_io, internal.link);
3778 			/*
3779 			 * Wait for some of the outstanding I/O to complete before we
3780 			 *  retry any of the nomem_io.  Normally we will wait for
3781 			 *  NOMEM_THRESHOLD_COUNT I/O to complete but for low queue
3782 			 *  depth channels we will instead wait for half to complete.
3783 			 */
3784 			shared_resource->nomem_threshold = spdk_max((int64_t)shared_resource->io_outstanding / 2,
3785 							   (int64_t)shared_resource->io_outstanding - NOMEM_THRESHOLD_COUNT);
3786 			return;
3787 		}
3788 
3789 		if (spdk_unlikely(!TAILQ_EMPTY(&shared_resource->nomem_io))) {
3790 			_spdk_bdev_ch_retry_io(bdev_ch);
3791 		}
3792 	}
3793 
3794 	_spdk_bdev_io_complete(bdev_io);
3795 }
3796 
3797 void
3798 spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc,
3799 				  enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq)
3800 {
3801 	if (sc == SPDK_SCSI_STATUS_GOOD) {
3802 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
3803 	} else {
3804 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SCSI_ERROR;
3805 		bdev_io->internal.error.scsi.sc = sc;
3806 		bdev_io->internal.error.scsi.sk = sk;
3807 		bdev_io->internal.error.scsi.asc = asc;
3808 		bdev_io->internal.error.scsi.ascq = ascq;
3809 	}
3810 
3811 	spdk_bdev_io_complete(bdev_io, bdev_io->internal.status);
3812 }
3813 
3814 void
3815 spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io,
3816 			     int *sc, int *sk, int *asc, int *ascq)
3817 {
3818 	assert(sc != NULL);
3819 	assert(sk != NULL);
3820 	assert(asc != NULL);
3821 	assert(ascq != NULL);
3822 
3823 	switch (bdev_io->internal.status) {
3824 	case SPDK_BDEV_IO_STATUS_SUCCESS:
3825 		*sc = SPDK_SCSI_STATUS_GOOD;
3826 		*sk = SPDK_SCSI_SENSE_NO_SENSE;
3827 		*asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
3828 		*ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
3829 		break;
3830 	case SPDK_BDEV_IO_STATUS_NVME_ERROR:
3831 		spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq);
3832 		break;
3833 	case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
3834 		*sc = bdev_io->internal.error.scsi.sc;
3835 		*sk = bdev_io->internal.error.scsi.sk;
3836 		*asc = bdev_io->internal.error.scsi.asc;
3837 		*ascq = bdev_io->internal.error.scsi.ascq;
3838 		break;
3839 	default:
3840 		*sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
3841 		*sk = SPDK_SCSI_SENSE_ABORTED_COMMAND;
3842 		*asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
3843 		*ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
3844 		break;
3845 	}
3846 }
3847 
3848 void
3849 spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, int sct, int sc)
3850 {
3851 	if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS) {
3852 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
3853 	} else {
3854 		bdev_io->internal.error.nvme.sct = sct;
3855 		bdev_io->internal.error.nvme.sc = sc;
3856 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_NVME_ERROR;
3857 	}
3858 
3859 	spdk_bdev_io_complete(bdev_io, bdev_io->internal.status);
3860 }
3861 
3862 void
3863 spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, int *sct, int *sc)
3864 {
3865 	assert(sct != NULL);
3866 	assert(sc != NULL);
3867 
3868 	if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
3869 		*sct = bdev_io->internal.error.nvme.sct;
3870 		*sc = bdev_io->internal.error.nvme.sc;
3871 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
3872 		*sct = SPDK_NVME_SCT_GENERIC;
3873 		*sc = SPDK_NVME_SC_SUCCESS;
3874 	} else {
3875 		*sct = SPDK_NVME_SCT_GENERIC;
3876 		*sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
3877 	}
3878 }
3879 
3880 struct spdk_thread *
3881 spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io)
3882 {
3883 	return spdk_io_channel_get_thread(bdev_io->internal.ch->channel);
3884 }
3885 
3886 struct spdk_io_channel *
3887 spdk_bdev_io_get_io_channel(struct spdk_bdev_io *bdev_io)
3888 {
3889 	return bdev_io->internal.ch->channel;
3890 }
3891 
3892 static void
3893 _spdk_bdev_qos_config_limit(struct spdk_bdev *bdev, uint64_t *limits)
3894 {
3895 	uint64_t	min_qos_set;
3896 	int		i;
3897 
3898 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3899 		if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
3900 			break;
3901 		}
3902 	}
3903 
3904 	if (i == SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) {
3905 		SPDK_ERRLOG("Invalid rate limits set.\n");
3906 		return;
3907 	}
3908 
3909 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3910 		if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
3911 			continue;
3912 		}
3913 
3914 		if (_spdk_bdev_qos_is_iops_rate_limit(i) == true) {
3915 			min_qos_set = SPDK_BDEV_QOS_MIN_IOS_PER_SEC;
3916 		} else {
3917 			min_qos_set = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC;
3918 		}
3919 
3920 		if (limits[i] == 0 || limits[i] % min_qos_set) {
3921 			SPDK_ERRLOG("Assigned limit %" PRIu64 " on bdev %s is not multiple of %" PRIu64 "\n",
3922 				    limits[i], bdev->name, min_qos_set);
3923 			SPDK_ERRLOG("Failed to enable QoS on this bdev %s\n", bdev->name);
3924 			return;
3925 		}
3926 	}
3927 
3928 	if (!bdev->internal.qos) {
3929 		bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
3930 		if (!bdev->internal.qos) {
3931 			SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
3932 			return;
3933 		}
3934 	}
3935 
3936 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3937 		bdev->internal.qos->rate_limits[i].limit = limits[i];
3938 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Bdev:%s QoS type:%d set:%lu\n",
3939 			      bdev->name, i, limits[i]);
3940 	}
3941 
3942 	return;
3943 }
3944 
3945 static void
3946 _spdk_bdev_qos_config(struct spdk_bdev *bdev)
3947 {
3948 	struct spdk_conf_section	*sp = NULL;
3949 	const char			*val = NULL;
3950 	int				i = 0, j = 0;
3951 	uint64_t			limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES] = {};
3952 	bool				config_qos = false;
3953 
3954 	sp = spdk_conf_find_section(NULL, "QoS");
3955 	if (!sp) {
3956 		return;
3957 	}
3958 
3959 	while (j < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES) {
3960 		limits[j] = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
3961 
3962 		i = 0;
3963 		while (true) {
3964 			val = spdk_conf_section_get_nmval(sp, qos_conf_type[j], i, 0);
3965 			if (!val) {
3966 				break;
3967 			}
3968 
3969 			if (strcmp(bdev->name, val) != 0) {
3970 				i++;
3971 				continue;
3972 			}
3973 
3974 			val = spdk_conf_section_get_nmval(sp, qos_conf_type[j], i, 1);
3975 			if (val) {
3976 				if (_spdk_bdev_qos_is_iops_rate_limit(j) == true) {
3977 					limits[j] = strtoull(val, NULL, 10);
3978 				} else {
3979 					limits[j] = strtoull(val, NULL, 10) * 1024 * 1024;
3980 				}
3981 				config_qos = true;
3982 			}
3983 
3984 			break;
3985 		}
3986 
3987 		j++;
3988 	}
3989 
3990 	if (config_qos == true) {
3991 		_spdk_bdev_qos_config_limit(bdev, limits);
3992 	}
3993 
3994 	return;
3995 }
3996 
3997 static int
3998 spdk_bdev_init(struct spdk_bdev *bdev)
3999 {
4000 	char *bdev_name;
4001 
4002 	assert(bdev->module != NULL);
4003 
4004 	if (!bdev->name) {
4005 		SPDK_ERRLOG("Bdev name is NULL\n");
4006 		return -EINVAL;
4007 	}
4008 
4009 	if (spdk_bdev_get_by_name(bdev->name)) {
4010 		SPDK_ERRLOG("Bdev name:%s already exists\n", bdev->name);
4011 		return -EEXIST;
4012 	}
4013 
4014 	/* Users often register their own I/O devices using the bdev name. In
4015 	 * order to avoid conflicts, prepend bdev_. */
4016 	bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name);
4017 	if (!bdev_name) {
4018 		SPDK_ERRLOG("Unable to allocate memory for internal bdev name.\n");
4019 		return -ENOMEM;
4020 	}
4021 
4022 	bdev->internal.status = SPDK_BDEV_STATUS_READY;
4023 	bdev->internal.measured_queue_depth = UINT64_MAX;
4024 	bdev->internal.claim_module = NULL;
4025 	bdev->internal.qd_poller = NULL;
4026 	bdev->internal.qos = NULL;
4027 
4028 	if (spdk_bdev_get_buf_align(bdev) > 1) {
4029 		if (bdev->split_on_optimal_io_boundary) {
4030 			bdev->optimal_io_boundary = spdk_min(bdev->optimal_io_boundary,
4031 							     SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen);
4032 		} else {
4033 			bdev->split_on_optimal_io_boundary = true;
4034 			bdev->optimal_io_boundary = SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen;
4035 		}
4036 	}
4037 
4038 	TAILQ_INIT(&bdev->internal.open_descs);
4039 
4040 	TAILQ_INIT(&bdev->aliases);
4041 
4042 	bdev->internal.reset_in_progress = NULL;
4043 
4044 	_spdk_bdev_qos_config(bdev);
4045 
4046 	spdk_io_device_register(__bdev_to_io_dev(bdev),
4047 				spdk_bdev_channel_create, spdk_bdev_channel_destroy,
4048 				sizeof(struct spdk_bdev_channel),
4049 				bdev_name);
4050 
4051 	free(bdev_name);
4052 
4053 	pthread_mutex_init(&bdev->internal.mutex, NULL);
4054 	return 0;
4055 }
4056 
4057 static void
4058 spdk_bdev_destroy_cb(void *io_device)
4059 {
4060 	int			rc;
4061 	struct spdk_bdev	*bdev;
4062 	spdk_bdev_unregister_cb	cb_fn;
4063 	void			*cb_arg;
4064 
4065 	bdev = __bdev_from_io_dev(io_device);
4066 	cb_fn = bdev->internal.unregister_cb;
4067 	cb_arg = bdev->internal.unregister_ctx;
4068 
4069 	rc = bdev->fn_table->destruct(bdev->ctxt);
4070 	if (rc < 0) {
4071 		SPDK_ERRLOG("destruct failed\n");
4072 	}
4073 	if (rc <= 0 && cb_fn != NULL) {
4074 		cb_fn(cb_arg, rc);
4075 	}
4076 }
4077 
4078 
4079 static void
4080 spdk_bdev_fini(struct spdk_bdev *bdev)
4081 {
4082 	pthread_mutex_destroy(&bdev->internal.mutex);
4083 
4084 	free(bdev->internal.qos);
4085 
4086 	spdk_io_device_unregister(__bdev_to_io_dev(bdev), spdk_bdev_destroy_cb);
4087 }
4088 
4089 static void
4090 spdk_bdev_start(struct spdk_bdev *bdev)
4091 {
4092 	struct spdk_bdev_module *module;
4093 	uint32_t action;
4094 
4095 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Inserting bdev %s into list\n", bdev->name);
4096 	TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, internal.link);
4097 
4098 	/* Examine configuration before initializing I/O */
4099 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
4100 		if (module->examine_config) {
4101 			action = module->internal.action_in_progress;
4102 			module->internal.action_in_progress++;
4103 			module->examine_config(bdev);
4104 			if (action != module->internal.action_in_progress) {
4105 				SPDK_ERRLOG("examine_config for module %s did not call spdk_bdev_module_examine_done()\n",
4106 					    module->name);
4107 			}
4108 		}
4109 	}
4110 
4111 	if (bdev->internal.claim_module) {
4112 		if (bdev->internal.claim_module->examine_disk) {
4113 			bdev->internal.claim_module->internal.action_in_progress++;
4114 			bdev->internal.claim_module->examine_disk(bdev);
4115 		}
4116 		return;
4117 	}
4118 
4119 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
4120 		if (module->examine_disk) {
4121 			module->internal.action_in_progress++;
4122 			module->examine_disk(bdev);
4123 		}
4124 	}
4125 }
4126 
4127 int
4128 spdk_bdev_register(struct spdk_bdev *bdev)
4129 {
4130 	int rc = spdk_bdev_init(bdev);
4131 
4132 	if (rc == 0) {
4133 		spdk_bdev_start(bdev);
4134 	}
4135 
4136 	spdk_notify_send("bdev_register", spdk_bdev_get_name(bdev));
4137 	return rc;
4138 }
4139 
4140 int
4141 spdk_vbdev_register(struct spdk_bdev *vbdev, struct spdk_bdev **base_bdevs, int base_bdev_count)
4142 {
4143 	SPDK_ERRLOG("This function is deprecated.  Use spdk_bdev_register() instead.\n");
4144 	return spdk_bdev_register(vbdev);
4145 }
4146 
4147 void
4148 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
4149 {
4150 	if (bdev->internal.unregister_cb != NULL) {
4151 		bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno);
4152 	}
4153 }
4154 
4155 static void
4156 _remove_notify(void *arg)
4157 {
4158 	struct spdk_bdev_desc *desc = arg;
4159 
4160 	desc->remove_scheduled = false;
4161 
4162 	if (desc->closed) {
4163 		free(desc);
4164 	} else {
4165 		desc->remove_cb(desc->remove_ctx);
4166 	}
4167 }
4168 
4169 /* Must be called while holding bdev->internal.mutex.
4170  * returns: 0 - bdev removed and ready to be destructed.
4171  *          -EBUSY - bdev can't be destructed yet.  */
4172 static int
4173 spdk_bdev_unregister_unsafe(struct spdk_bdev *bdev)
4174 {
4175 	struct spdk_bdev_desc	*desc, *tmp;
4176 	int			rc = 0;
4177 
4178 	/* Notify each descriptor about hotremoval */
4179 	TAILQ_FOREACH_SAFE(desc, &bdev->internal.open_descs, link, tmp) {
4180 		rc = -EBUSY;
4181 		if (desc->remove_cb) {
4182 			/*
4183 			 * Defer invocation of the remove_cb to a separate message that will
4184 			 *  run later on its thread.  This ensures this context unwinds and
4185 			 *  we don't recursively unregister this bdev again if the remove_cb
4186 			 *  immediately closes its descriptor.
4187 			 */
4188 			if (!desc->remove_scheduled) {
4189 				/* Avoid scheduling removal of the same descriptor multiple times. */
4190 				desc->remove_scheduled = true;
4191 				spdk_thread_send_msg(desc->thread, _remove_notify, desc);
4192 			}
4193 		}
4194 	}
4195 
4196 	/* If there are no descriptors, proceed removing the bdev */
4197 	if (rc == 0) {
4198 		TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
4199 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Removing bdev %s from list done\n", bdev->name);
4200 		spdk_notify_send("bdev_unregister", spdk_bdev_get_name(bdev));
4201 	}
4202 
4203 	return rc;
4204 }
4205 
4206 void
4207 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
4208 {
4209 	struct spdk_thread	*thread;
4210 	int			rc;
4211 
4212 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Removing bdev %s from list\n", bdev->name);
4213 
4214 	thread = spdk_get_thread();
4215 	if (!thread) {
4216 		/* The user called this from a non-SPDK thread. */
4217 		if (cb_fn != NULL) {
4218 			cb_fn(cb_arg, -ENOTSUP);
4219 		}
4220 		return;
4221 	}
4222 
4223 	pthread_mutex_lock(&bdev->internal.mutex);
4224 	if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
4225 		pthread_mutex_unlock(&bdev->internal.mutex);
4226 		if (cb_fn) {
4227 			cb_fn(cb_arg, -EBUSY);
4228 		}
4229 		return;
4230 	}
4231 
4232 	bdev->internal.status = SPDK_BDEV_STATUS_REMOVING;
4233 	bdev->internal.unregister_cb = cb_fn;
4234 	bdev->internal.unregister_ctx = cb_arg;
4235 
4236 	/* Call under lock. */
4237 	rc = spdk_bdev_unregister_unsafe(bdev);
4238 	pthread_mutex_unlock(&bdev->internal.mutex);
4239 
4240 	if (rc == 0) {
4241 		spdk_bdev_fini(bdev);
4242 	}
4243 }
4244 
4245 int
4246 spdk_bdev_open(struct spdk_bdev *bdev, bool write, spdk_bdev_remove_cb_t remove_cb,
4247 	       void *remove_ctx, struct spdk_bdev_desc **_desc)
4248 {
4249 	struct spdk_bdev_desc *desc;
4250 	struct spdk_thread *thread;
4251 	struct set_qos_limit_ctx *ctx;
4252 
4253 	thread = spdk_get_thread();
4254 	if (!thread) {
4255 		SPDK_ERRLOG("Cannot open bdev from non-SPDK thread.\n");
4256 		return -ENOTSUP;
4257 	}
4258 
4259 	desc = calloc(1, sizeof(*desc));
4260 	if (desc == NULL) {
4261 		SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n");
4262 		return -ENOMEM;
4263 	}
4264 
4265 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Opening descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
4266 		      spdk_get_thread());
4267 
4268 	desc->bdev = bdev;
4269 	desc->thread = thread;
4270 	desc->remove_cb = remove_cb;
4271 	desc->remove_ctx = remove_ctx;
4272 	desc->write = write;
4273 	*_desc = desc;
4274 
4275 	pthread_mutex_lock(&bdev->internal.mutex);
4276 
4277 	if (write && bdev->internal.claim_module) {
4278 		SPDK_ERRLOG("Could not open %s - %s module already claimed it\n",
4279 			    bdev->name, bdev->internal.claim_module->name);
4280 		pthread_mutex_unlock(&bdev->internal.mutex);
4281 		free(desc);
4282 		*_desc = NULL;
4283 		return -EPERM;
4284 	}
4285 
4286 	/* Enable QoS */
4287 	if (bdev->internal.qos && bdev->internal.qos->thread == NULL) {
4288 		ctx = calloc(1, sizeof(*ctx));
4289 		if (ctx == NULL) {
4290 			SPDK_ERRLOG("Failed to allocate memory for QoS context\n");
4291 			pthread_mutex_unlock(&bdev->internal.mutex);
4292 			free(desc);
4293 			*_desc = NULL;
4294 			return -ENOMEM;
4295 		}
4296 		ctx->bdev = bdev;
4297 		spdk_for_each_channel(__bdev_to_io_dev(bdev),
4298 				      _spdk_bdev_enable_qos_msg, ctx,
4299 				      _spdk_bdev_enable_qos_done);
4300 	}
4301 
4302 	TAILQ_INSERT_TAIL(&bdev->internal.open_descs, desc, link);
4303 
4304 	pthread_mutex_unlock(&bdev->internal.mutex);
4305 
4306 	return 0;
4307 }
4308 
4309 void
4310 spdk_bdev_close(struct spdk_bdev_desc *desc)
4311 {
4312 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4313 	int rc;
4314 
4315 	SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Closing descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
4316 		      spdk_get_thread());
4317 
4318 	if (desc->thread != spdk_get_thread()) {
4319 		SPDK_ERRLOG("Descriptor %p for bdev %s closed on wrong thread (%p, expected %p)\n",
4320 			    desc, bdev->name, spdk_get_thread(), desc->thread);
4321 	}
4322 
4323 	pthread_mutex_lock(&bdev->internal.mutex);
4324 
4325 	TAILQ_REMOVE(&bdev->internal.open_descs, desc, link);
4326 
4327 	desc->closed = true;
4328 
4329 	if (!desc->remove_scheduled) {
4330 		free(desc);
4331 	}
4332 
4333 	/* If no more descriptors, kill QoS channel */
4334 	if (bdev->internal.qos && TAILQ_EMPTY(&bdev->internal.open_descs)) {
4335 		SPDK_DEBUGLOG(SPDK_LOG_BDEV, "Closed last descriptor for bdev %s on thread %p. Stopping QoS.\n",
4336 			      bdev->name, spdk_get_thread());
4337 
4338 		if (spdk_bdev_qos_destroy(bdev)) {
4339 			/* There isn't anything we can do to recover here. Just let the
4340 			 * old QoS poller keep running. The QoS handling won't change
4341 			 * cores when the user allocates a new channel, but it won't break. */
4342 			SPDK_ERRLOG("Unable to shut down QoS poller. It will continue running on the current thread.\n");
4343 		}
4344 	}
4345 
4346 	spdk_bdev_set_qd_sampling_period(bdev, 0);
4347 
4348 	if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->internal.open_descs)) {
4349 		rc = spdk_bdev_unregister_unsafe(bdev);
4350 		pthread_mutex_unlock(&bdev->internal.mutex);
4351 
4352 		if (rc == 0) {
4353 			spdk_bdev_fini(bdev);
4354 		}
4355 	} else {
4356 		pthread_mutex_unlock(&bdev->internal.mutex);
4357 	}
4358 }
4359 
4360 int
4361 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
4362 			    struct spdk_bdev_module *module)
4363 {
4364 	if (bdev->internal.claim_module != NULL) {
4365 		SPDK_ERRLOG("bdev %s already claimed by module %s\n", bdev->name,
4366 			    bdev->internal.claim_module->name);
4367 		return -EPERM;
4368 	}
4369 
4370 	if (desc && !desc->write) {
4371 		desc->write = true;
4372 	}
4373 
4374 	bdev->internal.claim_module = module;
4375 	return 0;
4376 }
4377 
4378 void
4379 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev)
4380 {
4381 	assert(bdev->internal.claim_module != NULL);
4382 	bdev->internal.claim_module = NULL;
4383 }
4384 
4385 struct spdk_bdev *
4386 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
4387 {
4388 	return desc->bdev;
4389 }
4390 
4391 void
4392 spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp)
4393 {
4394 	struct iovec *iovs;
4395 	int iovcnt;
4396 
4397 	if (bdev_io == NULL) {
4398 		return;
4399 	}
4400 
4401 	switch (bdev_io->type) {
4402 	case SPDK_BDEV_IO_TYPE_READ:
4403 	case SPDK_BDEV_IO_TYPE_WRITE:
4404 	case SPDK_BDEV_IO_TYPE_ZCOPY:
4405 		iovs = bdev_io->u.bdev.iovs;
4406 		iovcnt = bdev_io->u.bdev.iovcnt;
4407 		break;
4408 	default:
4409 		iovs = NULL;
4410 		iovcnt = 0;
4411 		break;
4412 	}
4413 
4414 	if (iovp) {
4415 		*iovp = iovs;
4416 	}
4417 	if (iovcntp) {
4418 		*iovcntp = iovcnt;
4419 	}
4420 }
4421 
4422 void *
4423 spdk_bdev_io_get_md_buf(struct spdk_bdev_io *bdev_io)
4424 {
4425 	if (bdev_io == NULL) {
4426 		return NULL;
4427 	}
4428 
4429 	if (!spdk_bdev_is_md_separate(bdev_io->bdev)) {
4430 		return NULL;
4431 	}
4432 
4433 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ ||
4434 	    bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
4435 		return bdev_io->u.bdev.md_buf;
4436 	}
4437 
4438 	return NULL;
4439 }
4440 
4441 void
4442 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
4443 {
4444 
4445 	if (spdk_bdev_module_list_find(bdev_module->name)) {
4446 		SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name);
4447 		assert(false);
4448 	}
4449 
4450 	/*
4451 	 * Modules with examine callbacks must be initialized first, so they are
4452 	 *  ready to handle examine callbacks from later modules that will
4453 	 *  register physical bdevs.
4454 	 */
4455 	if (bdev_module->examine_config != NULL || bdev_module->examine_disk != NULL) {
4456 		TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
4457 	} else {
4458 		TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
4459 	}
4460 }
4461 
4462 struct spdk_bdev_module *
4463 spdk_bdev_module_list_find(const char *name)
4464 {
4465 	struct spdk_bdev_module *bdev_module;
4466 
4467 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
4468 		if (strcmp(name, bdev_module->name) == 0) {
4469 			break;
4470 		}
4471 	}
4472 
4473 	return bdev_module;
4474 }
4475 
4476 static void
4477 _spdk_bdev_write_zero_buffer_next(void *_bdev_io)
4478 {
4479 	struct spdk_bdev_io *bdev_io = _bdev_io;
4480 	uint64_t num_bytes, num_blocks;
4481 	void *md_buf = NULL;
4482 	int rc;
4483 
4484 	num_bytes = spdk_min(_bdev_get_block_size_with_md(bdev_io->bdev) *
4485 			     bdev_io->u.bdev.split_remaining_num_blocks,
4486 			     ZERO_BUFFER_SIZE);
4487 	num_blocks = num_bytes / _bdev_get_block_size_with_md(bdev_io->bdev);
4488 
4489 	if (spdk_bdev_is_md_separate(bdev_io->bdev)) {
4490 		md_buf = (char *)g_bdev_mgr.zero_buffer +
4491 			 spdk_bdev_get_block_size(bdev_io->bdev) * num_blocks;
4492 	}
4493 
4494 	rc = _spdk_bdev_write_blocks_with_md(bdev_io->internal.desc,
4495 					     spdk_io_channel_from_ctx(bdev_io->internal.ch),
4496 					     g_bdev_mgr.zero_buffer, md_buf,
4497 					     bdev_io->u.bdev.split_current_offset_blocks, num_blocks,
4498 					     _spdk_bdev_write_zero_buffer_done, bdev_io);
4499 	if (rc == 0) {
4500 		bdev_io->u.bdev.split_remaining_num_blocks -= num_blocks;
4501 		bdev_io->u.bdev.split_current_offset_blocks += num_blocks;
4502 	} else if (rc == -ENOMEM) {
4503 		_spdk_bdev_queue_io_wait_with_cb(bdev_io, _spdk_bdev_write_zero_buffer_next);
4504 	} else {
4505 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
4506 		bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
4507 	}
4508 }
4509 
4510 static void
4511 _spdk_bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
4512 {
4513 	struct spdk_bdev_io *parent_io = cb_arg;
4514 
4515 	spdk_bdev_free_io(bdev_io);
4516 
4517 	if (!success) {
4518 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
4519 		parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
4520 		return;
4521 	}
4522 
4523 	if (parent_io->u.bdev.split_remaining_num_blocks == 0) {
4524 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
4525 		parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx);
4526 		return;
4527 	}
4528 
4529 	_spdk_bdev_write_zero_buffer_next(parent_io);
4530 }
4531 
4532 static void
4533 _spdk_bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status)
4534 {
4535 	pthread_mutex_lock(&ctx->bdev->internal.mutex);
4536 	ctx->bdev->internal.qos_mod_in_progress = false;
4537 	pthread_mutex_unlock(&ctx->bdev->internal.mutex);
4538 
4539 	if (ctx->cb_fn) {
4540 		ctx->cb_fn(ctx->cb_arg, status);
4541 	}
4542 	free(ctx);
4543 }
4544 
4545 static void
4546 _spdk_bdev_disable_qos_done(void *cb_arg)
4547 {
4548 	struct set_qos_limit_ctx *ctx = cb_arg;
4549 	struct spdk_bdev *bdev = ctx->bdev;
4550 	struct spdk_bdev_io *bdev_io;
4551 	struct spdk_bdev_qos *qos;
4552 
4553 	pthread_mutex_lock(&bdev->internal.mutex);
4554 	qos = bdev->internal.qos;
4555 	bdev->internal.qos = NULL;
4556 	pthread_mutex_unlock(&bdev->internal.mutex);
4557 
4558 	while (!TAILQ_EMPTY(&qos->queued)) {
4559 		/* Send queued I/O back to their original thread for resubmission. */
4560 		bdev_io = TAILQ_FIRST(&qos->queued);
4561 		TAILQ_REMOVE(&qos->queued, bdev_io, internal.link);
4562 
4563 		if (bdev_io->internal.io_submit_ch) {
4564 			/*
4565 			 * Channel was changed when sending it to the QoS thread - change it back
4566 			 *  before sending it back to the original thread.
4567 			 */
4568 			bdev_io->internal.ch = bdev_io->internal.io_submit_ch;
4569 			bdev_io->internal.io_submit_ch = NULL;
4570 		}
4571 
4572 		spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
4573 				     _spdk_bdev_io_submit, bdev_io);
4574 	}
4575 
4576 	if (qos->thread != NULL) {
4577 		spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
4578 		spdk_poller_unregister(&qos->poller);
4579 	}
4580 
4581 	free(qos);
4582 
4583 	_spdk_bdev_set_qos_limit_done(ctx, 0);
4584 }
4585 
4586 static void
4587 _spdk_bdev_disable_qos_msg_done(struct spdk_io_channel_iter *i, int status)
4588 {
4589 	void *io_device = spdk_io_channel_iter_get_io_device(i);
4590 	struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
4591 	struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4592 	struct spdk_thread *thread;
4593 
4594 	pthread_mutex_lock(&bdev->internal.mutex);
4595 	thread = bdev->internal.qos->thread;
4596 	pthread_mutex_unlock(&bdev->internal.mutex);
4597 
4598 	if (thread != NULL) {
4599 		spdk_thread_send_msg(thread, _spdk_bdev_disable_qos_done, ctx);
4600 	} else {
4601 		_spdk_bdev_disable_qos_done(ctx);
4602 	}
4603 }
4604 
4605 static void
4606 _spdk_bdev_disable_qos_msg(struct spdk_io_channel_iter *i)
4607 {
4608 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
4609 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch);
4610 
4611 	bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED;
4612 
4613 	spdk_for_each_channel_continue(i, 0);
4614 }
4615 
4616 static void
4617 _spdk_bdev_update_qos_rate_limit_msg(void *cb_arg)
4618 {
4619 	struct set_qos_limit_ctx *ctx = cb_arg;
4620 	struct spdk_bdev *bdev = ctx->bdev;
4621 
4622 	pthread_mutex_lock(&bdev->internal.mutex);
4623 	spdk_bdev_qos_update_max_quota_per_timeslice(bdev->internal.qos);
4624 	pthread_mutex_unlock(&bdev->internal.mutex);
4625 
4626 	_spdk_bdev_set_qos_limit_done(ctx, 0);
4627 }
4628 
4629 static void
4630 _spdk_bdev_enable_qos_msg(struct spdk_io_channel_iter *i)
4631 {
4632 	void *io_device = spdk_io_channel_iter_get_io_device(i);
4633 	struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
4634 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
4635 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch);
4636 
4637 	pthread_mutex_lock(&bdev->internal.mutex);
4638 	_spdk_bdev_enable_qos(bdev, bdev_ch);
4639 	pthread_mutex_unlock(&bdev->internal.mutex);
4640 	spdk_for_each_channel_continue(i, 0);
4641 }
4642 
4643 static void
4644 _spdk_bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status)
4645 {
4646 	struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4647 
4648 	_spdk_bdev_set_qos_limit_done(ctx, status);
4649 }
4650 
4651 static void
4652 _spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
4653 {
4654 	int i;
4655 
4656 	assert(bdev->internal.qos != NULL);
4657 
4658 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4659 		if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
4660 			bdev->internal.qos->rate_limits[i].limit = limits[i];
4661 
4662 			if (limits[i] == 0) {
4663 				bdev->internal.qos->rate_limits[i].limit =
4664 					SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
4665 			}
4666 		}
4667 	}
4668 }
4669 
4670 void
4671 spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits,
4672 			      void (*cb_fn)(void *cb_arg, int status), void *cb_arg)
4673 {
4674 	struct set_qos_limit_ctx	*ctx;
4675 	uint32_t			limit_set_complement;
4676 	uint64_t			min_limit_per_sec;
4677 	int				i;
4678 	bool				disable_rate_limit = true;
4679 
4680 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4681 		if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
4682 			continue;
4683 		}
4684 
4685 		if (limits[i] > 0) {
4686 			disable_rate_limit = false;
4687 		}
4688 
4689 		if (_spdk_bdev_qos_is_iops_rate_limit(i) == true) {
4690 			min_limit_per_sec = SPDK_BDEV_QOS_MIN_IOS_PER_SEC;
4691 		} else {
4692 			/* Change from megabyte to byte rate limit */
4693 			limits[i] = limits[i] * 1024 * 1024;
4694 			min_limit_per_sec = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC;
4695 		}
4696 
4697 		limit_set_complement = limits[i] % min_limit_per_sec;
4698 		if (limit_set_complement) {
4699 			SPDK_ERRLOG("Requested rate limit %" PRIu64 " is not a multiple of %" PRIu64 "\n",
4700 				    limits[i], min_limit_per_sec);
4701 			limits[i] += min_limit_per_sec - limit_set_complement;
4702 			SPDK_ERRLOG("Round up the rate limit to %" PRIu64 "\n", limits[i]);
4703 		}
4704 	}
4705 
4706 	ctx = calloc(1, sizeof(*ctx));
4707 	if (ctx == NULL) {
4708 		cb_fn(cb_arg, -ENOMEM);
4709 		return;
4710 	}
4711 
4712 	ctx->cb_fn = cb_fn;
4713 	ctx->cb_arg = cb_arg;
4714 	ctx->bdev = bdev;
4715 
4716 	pthread_mutex_lock(&bdev->internal.mutex);
4717 	if (bdev->internal.qos_mod_in_progress) {
4718 		pthread_mutex_unlock(&bdev->internal.mutex);
4719 		free(ctx);
4720 		cb_fn(cb_arg, -EAGAIN);
4721 		return;
4722 	}
4723 	bdev->internal.qos_mod_in_progress = true;
4724 
4725 	if (disable_rate_limit == true && bdev->internal.qos) {
4726 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4727 			if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED &&
4728 			    (bdev->internal.qos->rate_limits[i].limit > 0 &&
4729 			     bdev->internal.qos->rate_limits[i].limit !=
4730 			     SPDK_BDEV_QOS_LIMIT_NOT_DEFINED)) {
4731 				disable_rate_limit = false;
4732 				break;
4733 			}
4734 		}
4735 	}
4736 
4737 	if (disable_rate_limit == false) {
4738 		if (bdev->internal.qos == NULL) {
4739 			bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
4740 			if (!bdev->internal.qos) {
4741 				pthread_mutex_unlock(&bdev->internal.mutex);
4742 				SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
4743 				free(ctx);
4744 				cb_fn(cb_arg, -ENOMEM);
4745 				return;
4746 			}
4747 		}
4748 
4749 		if (bdev->internal.qos->thread == NULL) {
4750 			/* Enabling */
4751 			_spdk_bdev_set_qos_rate_limits(bdev, limits);
4752 
4753 			spdk_for_each_channel(__bdev_to_io_dev(bdev),
4754 					      _spdk_bdev_enable_qos_msg, ctx,
4755 					      _spdk_bdev_enable_qos_done);
4756 		} else {
4757 			/* Updating */
4758 			_spdk_bdev_set_qos_rate_limits(bdev, limits);
4759 
4760 			spdk_thread_send_msg(bdev->internal.qos->thread,
4761 					     _spdk_bdev_update_qos_rate_limit_msg, ctx);
4762 		}
4763 	} else {
4764 		if (bdev->internal.qos != NULL) {
4765 			_spdk_bdev_set_qos_rate_limits(bdev, limits);
4766 
4767 			/* Disabling */
4768 			spdk_for_each_channel(__bdev_to_io_dev(bdev),
4769 					      _spdk_bdev_disable_qos_msg, ctx,
4770 					      _spdk_bdev_disable_qos_msg_done);
4771 		} else {
4772 			pthread_mutex_unlock(&bdev->internal.mutex);
4773 			_spdk_bdev_set_qos_limit_done(ctx, 0);
4774 			return;
4775 		}
4776 	}
4777 
4778 	pthread_mutex_unlock(&bdev->internal.mutex);
4779 }
4780 
4781 struct spdk_bdev_histogram_ctx {
4782 	spdk_bdev_histogram_status_cb cb_fn;
4783 	void *cb_arg;
4784 	struct spdk_bdev *bdev;
4785 	int status;
4786 };
4787 
4788 static void
4789 _spdk_bdev_histogram_disable_channel_cb(struct spdk_io_channel_iter *i, int status)
4790 {
4791 	struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4792 
4793 	pthread_mutex_lock(&ctx->bdev->internal.mutex);
4794 	ctx->bdev->internal.histogram_in_progress = false;
4795 	pthread_mutex_unlock(&ctx->bdev->internal.mutex);
4796 	ctx->cb_fn(ctx->cb_arg, ctx->status);
4797 	free(ctx);
4798 }
4799 
4800 static void
4801 _spdk_bdev_histogram_disable_channel(struct spdk_io_channel_iter *i)
4802 {
4803 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
4804 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
4805 
4806 	if (ch->histogram != NULL) {
4807 		spdk_histogram_data_free(ch->histogram);
4808 		ch->histogram = NULL;
4809 	}
4810 	spdk_for_each_channel_continue(i, 0);
4811 }
4812 
4813 static void
4814 _spdk_bdev_histogram_enable_channel_cb(struct spdk_io_channel_iter *i, int status)
4815 {
4816 	struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4817 
4818 	if (status != 0) {
4819 		ctx->status = status;
4820 		ctx->bdev->internal.histogram_enabled = false;
4821 		spdk_for_each_channel(__bdev_to_io_dev(ctx->bdev), _spdk_bdev_histogram_disable_channel, ctx,
4822 				      _spdk_bdev_histogram_disable_channel_cb);
4823 	} else {
4824 		pthread_mutex_lock(&ctx->bdev->internal.mutex);
4825 		ctx->bdev->internal.histogram_in_progress = false;
4826 		pthread_mutex_unlock(&ctx->bdev->internal.mutex);
4827 		ctx->cb_fn(ctx->cb_arg, ctx->status);
4828 		free(ctx);
4829 	}
4830 }
4831 
4832 static void
4833 _spdk_bdev_histogram_enable_channel(struct spdk_io_channel_iter *i)
4834 {
4835 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
4836 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
4837 	int status = 0;
4838 
4839 	if (ch->histogram == NULL) {
4840 		ch->histogram = spdk_histogram_data_alloc();
4841 		if (ch->histogram == NULL) {
4842 			status = -ENOMEM;
4843 		}
4844 	}
4845 
4846 	spdk_for_each_channel_continue(i, status);
4847 }
4848 
4849 void
4850 spdk_bdev_histogram_enable(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn,
4851 			   void *cb_arg, bool enable)
4852 {
4853 	struct spdk_bdev_histogram_ctx *ctx;
4854 
4855 	ctx = calloc(1, sizeof(struct spdk_bdev_histogram_ctx));
4856 	if (ctx == NULL) {
4857 		cb_fn(cb_arg, -ENOMEM);
4858 		return;
4859 	}
4860 
4861 	ctx->bdev = bdev;
4862 	ctx->status = 0;
4863 	ctx->cb_fn = cb_fn;
4864 	ctx->cb_arg = cb_arg;
4865 
4866 	pthread_mutex_lock(&bdev->internal.mutex);
4867 	if (bdev->internal.histogram_in_progress) {
4868 		pthread_mutex_unlock(&bdev->internal.mutex);
4869 		free(ctx);
4870 		cb_fn(cb_arg, -EAGAIN);
4871 		return;
4872 	}
4873 
4874 	bdev->internal.histogram_in_progress = true;
4875 	pthread_mutex_unlock(&bdev->internal.mutex);
4876 
4877 	bdev->internal.histogram_enabled = enable;
4878 
4879 	if (enable) {
4880 		/* Allocate histogram for each channel */
4881 		spdk_for_each_channel(__bdev_to_io_dev(bdev), _spdk_bdev_histogram_enable_channel, ctx,
4882 				      _spdk_bdev_histogram_enable_channel_cb);
4883 	} else {
4884 		spdk_for_each_channel(__bdev_to_io_dev(bdev), _spdk_bdev_histogram_disable_channel, ctx,
4885 				      _spdk_bdev_histogram_disable_channel_cb);
4886 	}
4887 }
4888 
4889 struct spdk_bdev_histogram_data_ctx {
4890 	spdk_bdev_histogram_data_cb cb_fn;
4891 	void *cb_arg;
4892 	struct spdk_bdev *bdev;
4893 	/** merged histogram data from all channels */
4894 	struct spdk_histogram_data	*histogram;
4895 };
4896 
4897 static void
4898 _spdk_bdev_histogram_get_channel_cb(struct spdk_io_channel_iter *i, int status)
4899 {
4900 	struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4901 
4902 	ctx->cb_fn(ctx->cb_arg, status, ctx->histogram);
4903 	free(ctx);
4904 }
4905 
4906 static void
4907 _spdk_bdev_histogram_get_channel(struct spdk_io_channel_iter *i)
4908 {
4909 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
4910 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
4911 	struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4912 	int status = 0;
4913 
4914 	if (ch->histogram == NULL) {
4915 		status = -EFAULT;
4916 	} else {
4917 		spdk_histogram_data_merge(ctx->histogram, ch->histogram);
4918 	}
4919 
4920 	spdk_for_each_channel_continue(i, status);
4921 }
4922 
4923 void
4924 spdk_bdev_histogram_get(struct spdk_bdev *bdev, struct spdk_histogram_data *histogram,
4925 			spdk_bdev_histogram_data_cb cb_fn,
4926 			void *cb_arg)
4927 {
4928 	struct spdk_bdev_histogram_data_ctx *ctx;
4929 
4930 	ctx = calloc(1, sizeof(struct spdk_bdev_histogram_data_ctx));
4931 	if (ctx == NULL) {
4932 		cb_fn(cb_arg, -ENOMEM, NULL);
4933 		return;
4934 	}
4935 
4936 	ctx->bdev = bdev;
4937 	ctx->cb_fn = cb_fn;
4938 	ctx->cb_arg = cb_arg;
4939 
4940 	ctx->histogram = histogram;
4941 
4942 	spdk_for_each_channel(__bdev_to_io_dev(bdev), _spdk_bdev_histogram_get_channel, ctx,
4943 			      _spdk_bdev_histogram_get_channel_cb);
4944 }
4945 
4946 SPDK_LOG_REGISTER_COMPONENT("bdev", SPDK_LOG_BDEV)
4947 
4948 SPDK_TRACE_REGISTER_FN(bdev_trace, "bdev", TRACE_GROUP_BDEV)
4949 {
4950 	spdk_trace_register_owner(OWNER_BDEV, 'b');
4951 	spdk_trace_register_object(OBJECT_BDEV_IO, 'i');
4952 	spdk_trace_register_description("BDEV_IO_START", TRACE_BDEV_IO_START, OWNER_BDEV,
4953 					OBJECT_BDEV_IO, 1, 0, "type:   ");
4954 	spdk_trace_register_description("BDEV_IO_DONE", TRACE_BDEV_IO_DONE, OWNER_BDEV,
4955 					OBJECT_BDEV_IO, 0, 0, "");
4956 }
4957