xref: /spdk/lib/bdev/bdev.c (revision 18c8b52afa69f39481ebb75711b2f30b11693f9d)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation. All rights reserved.
3  *   Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4  *   Copyright (c) 2021, 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5  */
6 
7 #include "spdk/stdinc.h"
8 
9 #include "spdk/bdev.h"
10 
11 #include "spdk/config.h"
12 #include "spdk/env.h"
13 #include "spdk/thread.h"
14 #include "spdk/likely.h"
15 #include "spdk/queue.h"
16 #include "spdk/nvme_spec.h"
17 #include "spdk/scsi_spec.h"
18 #include "spdk/notify.h"
19 #include "spdk/util.h"
20 #include "spdk/trace.h"
21 #include "spdk/dma.h"
22 
23 #include "spdk/bdev_module.h"
24 #include "spdk/log.h"
25 #include "spdk/string.h"
26 
27 #include "bdev_internal.h"
28 #include "spdk_internal/trace_defs.h"
29 
30 #ifdef SPDK_CONFIG_VTUNE
31 #include "ittnotify.h"
32 #include "ittnotify_types.h"
33 int __itt_init_ittlib(const char *, __itt_group_id);
34 #endif
35 
36 #define SPDK_BDEV_IO_POOL_SIZE			(64 * 1024 - 1)
37 #define SPDK_BDEV_IO_CACHE_SIZE			256
38 #define SPDK_BDEV_AUTO_EXAMINE			true
39 #define BUF_SMALL_POOL_SIZE			8191
40 #define BUF_LARGE_POOL_SIZE			1023
41 #define NOMEM_THRESHOLD_COUNT			8
42 
43 #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC		1000
44 #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE	1
45 #define SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE	512
46 #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC		1000
47 #define SPDK_BDEV_QOS_MIN_BYTES_PER_SEC		(1024 * 1024)
48 #define SPDK_BDEV_QOS_LIMIT_NOT_DEFINED		UINT64_MAX
49 #define SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC	1000
50 
51 #define SPDK_BDEV_POOL_ALIGNMENT 512
52 
53 /* The maximum number of children requests for a UNMAP or WRITE ZEROES command
54  * when splitting into children requests at a time.
55  */
56 #define SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS (8)
57 
58 static const char *qos_rpc_type[] = {"rw_ios_per_sec",
59 				     "rw_mbytes_per_sec", "r_mbytes_per_sec", "w_mbytes_per_sec"
60 				    };
61 
62 TAILQ_HEAD(spdk_bdev_list, spdk_bdev);
63 
64 RB_HEAD(bdev_name_tree, spdk_bdev_name);
65 
66 static int
67 bdev_name_cmp(struct spdk_bdev_name *name1, struct spdk_bdev_name *name2)
68 {
69 	return strcmp(name1->name, name2->name);
70 }
71 
72 RB_GENERATE_STATIC(bdev_name_tree, spdk_bdev_name, node, bdev_name_cmp);
73 
74 struct spdk_bdev_mgr {
75 	struct spdk_mempool *bdev_io_pool;
76 
77 	struct spdk_mempool *buf_small_pool;
78 	struct spdk_mempool *buf_large_pool;
79 
80 	void *zero_buffer;
81 
82 	TAILQ_HEAD(bdev_module_list, spdk_bdev_module) bdev_modules;
83 
84 	struct spdk_bdev_list bdevs;
85 	struct bdev_name_tree bdev_names;
86 
87 	bool init_complete;
88 	bool module_init_complete;
89 
90 	pthread_mutex_t mutex;
91 
92 #ifdef SPDK_CONFIG_VTUNE
93 	__itt_domain	*domain;
94 #endif
95 };
96 
97 static struct spdk_bdev_mgr g_bdev_mgr = {
98 	.bdev_modules = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdev_modules),
99 	.bdevs = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdevs),
100 	.bdev_names = RB_INITIALIZER(g_bdev_mgr.bdev_names),
101 	.init_complete = false,
102 	.module_init_complete = false,
103 	.mutex = PTHREAD_MUTEX_INITIALIZER,
104 };
105 
106 typedef void (*lock_range_cb)(void *ctx, int status);
107 
108 typedef void (*bdev_copy_bounce_buffer_cpl)(void *ctx, int rc);
109 
110 struct lba_range {
111 	uint64_t			offset;
112 	uint64_t			length;
113 	void				*locked_ctx;
114 	struct spdk_bdev_channel	*owner_ch;
115 	TAILQ_ENTRY(lba_range)		tailq;
116 };
117 
118 static struct spdk_bdev_opts	g_bdev_opts = {
119 	.bdev_io_pool_size = SPDK_BDEV_IO_POOL_SIZE,
120 	.bdev_io_cache_size = SPDK_BDEV_IO_CACHE_SIZE,
121 	.bdev_auto_examine = SPDK_BDEV_AUTO_EXAMINE,
122 	.small_buf_pool_size = BUF_SMALL_POOL_SIZE,
123 	.large_buf_pool_size = BUF_LARGE_POOL_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 to the underlying dev module through this channel
250 	 * and waiting for completion.
251 	 */
252 	uint64_t		io_outstanding;
253 
254 	/*
255 	 * List of all submitted I/Os including I/O that are generated via splitting.
256 	 */
257 	bdev_io_tailq_t		io_submitted;
258 
259 	/*
260 	 * List of spdk_bdev_io that are currently queued because they write to a locked
261 	 * LBA range.
262 	 */
263 	bdev_io_tailq_t		io_locked;
264 
265 	uint32_t		flags;
266 
267 	struct spdk_histogram_data *histogram;
268 
269 #ifdef SPDK_CONFIG_VTUNE
270 	uint64_t		start_tsc;
271 	uint64_t		interval_tsc;
272 	__itt_string_handle	*handle;
273 	struct spdk_bdev_io_stat prev_stat;
274 #endif
275 
276 	bdev_io_tailq_t		queued_resets;
277 
278 	lba_range_tailq_t	locked_ranges;
279 };
280 
281 struct media_event_entry {
282 	struct spdk_bdev_media_event	event;
283 	TAILQ_ENTRY(media_event_entry)	tailq;
284 };
285 
286 #define MEDIA_EVENT_POOL_SIZE 64
287 
288 struct spdk_bdev_desc {
289 	struct spdk_bdev		*bdev;
290 	struct spdk_thread		*thread;
291 	struct {
292 		spdk_bdev_event_cb_t event_fn;
293 		void *ctx;
294 	}				callback;
295 	bool				closed;
296 	bool				write;
297 	bool				memory_domains_supported;
298 	pthread_mutex_t			mutex;
299 	uint32_t			refs;
300 	TAILQ_HEAD(, media_event_entry)	pending_media_events;
301 	TAILQ_HEAD(, media_event_entry)	free_media_events;
302 	struct media_event_entry	*media_events_buffer;
303 	TAILQ_ENTRY(spdk_bdev_desc)	link;
304 
305 	uint64_t		timeout_in_sec;
306 	spdk_bdev_io_timeout_cb	cb_fn;
307 	void			*cb_arg;
308 	struct spdk_poller	*io_timeout_poller;
309 };
310 
311 struct spdk_bdev_iostat_ctx {
312 	struct spdk_bdev_io_stat *stat;
313 	spdk_bdev_get_device_stat_cb cb;
314 	void *cb_arg;
315 };
316 
317 struct set_qos_limit_ctx {
318 	void (*cb_fn)(void *cb_arg, int status);
319 	void *cb_arg;
320 	struct spdk_bdev *bdev;
321 };
322 
323 #define __bdev_to_io_dev(bdev)		(((char *)bdev) + 1)
324 #define __bdev_from_io_dev(io_dev)	((struct spdk_bdev *)(((char *)io_dev) - 1))
325 
326 static inline void bdev_io_complete(void *ctx);
327 
328 static void bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
329 static void bdev_write_zero_buffer_next(void *_bdev_io);
330 
331 static void bdev_enable_qos_msg(struct spdk_io_channel_iter *i);
332 static void bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status);
333 
334 static int bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
335 				     struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
336 				     uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg,
337 				     struct spdk_bdev_ext_io_opts *opts, bool copy_opts);
338 static int bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
339 				      struct iovec *iov, int iovcnt, void *md_buf,
340 				      uint64_t offset_blocks, uint64_t num_blocks,
341 				      spdk_bdev_io_completion_cb cb, void *cb_arg,
342 				      struct spdk_bdev_ext_io_opts *opts, bool copy_opts);
343 
344 static int bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
345 			       uint64_t offset, uint64_t length,
346 			       lock_range_cb cb_fn, void *cb_arg);
347 
348 static int bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
349 				 uint64_t offset, uint64_t length,
350 				 lock_range_cb cb_fn, void *cb_arg);
351 
352 static inline void bdev_io_complete(void *ctx);
353 
354 static bool bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort);
355 static bool bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_io *bio_to_abort);
356 
357 void
358 spdk_bdev_get_opts(struct spdk_bdev_opts *opts, size_t opts_size)
359 {
360 	if (!opts) {
361 		SPDK_ERRLOG("opts should not be NULL\n");
362 		return;
363 	}
364 
365 	if (!opts_size) {
366 		SPDK_ERRLOG("opts_size should not be zero value\n");
367 		return;
368 	}
369 
370 	opts->opts_size = opts_size;
371 
372 #define SET_FIELD(field) \
373 	if (offsetof(struct spdk_bdev_opts, field) + sizeof(opts->field) <= opts_size) { \
374 		opts->field = g_bdev_opts.field; \
375 	} \
376 
377 	SET_FIELD(bdev_io_pool_size);
378 	SET_FIELD(bdev_io_cache_size);
379 	SET_FIELD(bdev_auto_examine);
380 	SET_FIELD(small_buf_pool_size);
381 	SET_FIELD(large_buf_pool_size);
382 
383 	/* Do not remove this statement, you should always update this statement when you adding a new field,
384 	 * and do not forget to add the SET_FIELD statement for your added field. */
385 	SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_opts) == 32, "Incorrect size");
386 
387 #undef SET_FIELD
388 }
389 
390 int
391 spdk_bdev_set_opts(struct spdk_bdev_opts *opts)
392 {
393 	uint32_t min_pool_size;
394 
395 	if (!opts) {
396 		SPDK_ERRLOG("opts cannot be NULL\n");
397 		return -1;
398 	}
399 
400 	if (!opts->opts_size) {
401 		SPDK_ERRLOG("opts_size inside opts cannot be zero value\n");
402 		return -1;
403 	}
404 
405 	/*
406 	 * Add 1 to the thread count to account for the extra mgmt_ch that gets created during subsystem
407 	 *  initialization.  A second mgmt_ch will be created on the same thread when the application starts
408 	 *  but before the deferred put_io_channel event is executed for the first mgmt_ch.
409 	 */
410 	min_pool_size = opts->bdev_io_cache_size * (spdk_thread_get_count() + 1);
411 	if (opts->bdev_io_pool_size < min_pool_size) {
412 		SPDK_ERRLOG("bdev_io_pool_size %" PRIu32 " is not compatible with bdev_io_cache_size %" PRIu32
413 			    " and %" PRIu32 " threads\n", opts->bdev_io_pool_size, opts->bdev_io_cache_size,
414 			    spdk_thread_get_count());
415 		SPDK_ERRLOG("bdev_io_pool_size must be at least %" PRIu32 "\n", min_pool_size);
416 		return -1;
417 	}
418 
419 	if (opts->small_buf_pool_size < BUF_SMALL_POOL_SIZE) {
420 		SPDK_ERRLOG("small_buf_pool_size must be at least %" PRIu32 "\n", BUF_SMALL_POOL_SIZE);
421 		return -1;
422 	}
423 
424 	if (opts->large_buf_pool_size < BUF_LARGE_POOL_SIZE) {
425 		SPDK_ERRLOG("large_buf_pool_size must be at least %" PRIu32 "\n", BUF_LARGE_POOL_SIZE);
426 		return -1;
427 	}
428 
429 #define SET_FIELD(field) \
430         if (offsetof(struct spdk_bdev_opts, field) + sizeof(opts->field) <= opts->opts_size) { \
431                 g_bdev_opts.field = opts->field; \
432         } \
433 
434 	SET_FIELD(bdev_io_pool_size);
435 	SET_FIELD(bdev_io_cache_size);
436 	SET_FIELD(bdev_auto_examine);
437 	SET_FIELD(small_buf_pool_size);
438 	SET_FIELD(large_buf_pool_size);
439 
440 	g_bdev_opts.opts_size = opts->opts_size;
441 
442 #undef SET_FIELD
443 
444 	return 0;
445 }
446 
447 static struct spdk_bdev *
448 bdev_get_by_name(const char *bdev_name)
449 {
450 	struct spdk_bdev_name find;
451 	struct spdk_bdev_name *res;
452 
453 	find.name = (char *)bdev_name;
454 	res = RB_FIND(bdev_name_tree, &g_bdev_mgr.bdev_names, &find);
455 	if (res != NULL) {
456 		return res->bdev;
457 	}
458 
459 	return NULL;
460 }
461 
462 struct spdk_bdev *
463 spdk_bdev_get_by_name(const char *bdev_name)
464 {
465 	struct spdk_bdev *bdev;
466 
467 	pthread_mutex_lock(&g_bdev_mgr.mutex);
468 	bdev = bdev_get_by_name(bdev_name);
469 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
470 
471 	return bdev;
472 }
473 
474 struct spdk_bdev_wait_for_examine_ctx {
475 	struct spdk_poller              *poller;
476 	spdk_bdev_wait_for_examine_cb	cb_fn;
477 	void				*cb_arg;
478 };
479 
480 static bool bdev_module_all_actions_completed(void);
481 
482 static int
483 bdev_wait_for_examine_cb(void *arg)
484 {
485 	struct spdk_bdev_wait_for_examine_ctx *ctx = arg;
486 
487 	if (!bdev_module_all_actions_completed()) {
488 		return SPDK_POLLER_IDLE;
489 	}
490 
491 	spdk_poller_unregister(&ctx->poller);
492 	ctx->cb_fn(ctx->cb_arg);
493 	free(ctx);
494 
495 	return SPDK_POLLER_BUSY;
496 }
497 
498 int
499 spdk_bdev_wait_for_examine(spdk_bdev_wait_for_examine_cb cb_fn, void *cb_arg)
500 {
501 	struct spdk_bdev_wait_for_examine_ctx *ctx;
502 
503 	ctx = calloc(1, sizeof(*ctx));
504 	if (ctx == NULL) {
505 		return -ENOMEM;
506 	}
507 	ctx->cb_fn = cb_fn;
508 	ctx->cb_arg = cb_arg;
509 	ctx->poller = SPDK_POLLER_REGISTER(bdev_wait_for_examine_cb, ctx, 0);
510 
511 	return 0;
512 }
513 
514 struct spdk_bdev_examine_item {
515 	char *name;
516 	TAILQ_ENTRY(spdk_bdev_examine_item) link;
517 };
518 
519 TAILQ_HEAD(spdk_bdev_examine_allowlist, spdk_bdev_examine_item);
520 
521 struct spdk_bdev_examine_allowlist g_bdev_examine_allowlist = TAILQ_HEAD_INITIALIZER(
522 			g_bdev_examine_allowlist);
523 
524 static inline bool
525 bdev_examine_allowlist_check(const char *name)
526 {
527 	struct spdk_bdev_examine_item *item;
528 	TAILQ_FOREACH(item, &g_bdev_examine_allowlist, link) {
529 		if (strcmp(name, item->name) == 0) {
530 			return true;
531 		}
532 	}
533 	return false;
534 }
535 
536 static inline void
537 bdev_examine_allowlist_free(void)
538 {
539 	struct spdk_bdev_examine_item *item;
540 	while (!TAILQ_EMPTY(&g_bdev_examine_allowlist)) {
541 		item = TAILQ_FIRST(&g_bdev_examine_allowlist);
542 		TAILQ_REMOVE(&g_bdev_examine_allowlist, item, link);
543 		free(item->name);
544 		free(item);
545 	}
546 }
547 
548 static inline bool
549 bdev_in_examine_allowlist(struct spdk_bdev *bdev)
550 {
551 	struct spdk_bdev_alias *tmp;
552 	if (bdev_examine_allowlist_check(bdev->name)) {
553 		return true;
554 	}
555 	TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
556 		if (bdev_examine_allowlist_check(tmp->alias.name)) {
557 			return true;
558 		}
559 	}
560 	return false;
561 }
562 
563 static inline bool
564 bdev_ok_to_examine(struct spdk_bdev *bdev)
565 {
566 	if (g_bdev_opts.bdev_auto_examine) {
567 		return true;
568 	} else {
569 		return bdev_in_examine_allowlist(bdev);
570 	}
571 }
572 
573 static void
574 bdev_examine(struct spdk_bdev *bdev)
575 {
576 	struct spdk_bdev_module *module;
577 	uint32_t action;
578 
579 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
580 		if (module->examine_config && bdev_ok_to_examine(bdev)) {
581 			action = module->internal.action_in_progress;
582 			module->internal.action_in_progress++;
583 			module->examine_config(bdev);
584 			if (action != module->internal.action_in_progress) {
585 				SPDK_ERRLOG("examine_config for module %s did not call spdk_bdev_module_examine_done()\n",
586 					    module->name);
587 			}
588 		}
589 	}
590 
591 	if (bdev->internal.claim_module && bdev_ok_to_examine(bdev)) {
592 		if (bdev->internal.claim_module->examine_disk) {
593 			bdev->internal.claim_module->internal.action_in_progress++;
594 			bdev->internal.claim_module->examine_disk(bdev);
595 		}
596 		return;
597 	}
598 
599 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
600 		if (module->examine_disk && bdev_ok_to_examine(bdev)) {
601 			module->internal.action_in_progress++;
602 			module->examine_disk(bdev);
603 		}
604 	}
605 }
606 
607 int
608 spdk_bdev_examine(const char *name)
609 {
610 	struct spdk_bdev *bdev;
611 	struct spdk_bdev_examine_item *item;
612 
613 	if (g_bdev_opts.bdev_auto_examine) {
614 		SPDK_ERRLOG("Manual examine is not allowed if auto examine is enabled");
615 		return -EINVAL;
616 	}
617 
618 	if (bdev_examine_allowlist_check(name)) {
619 		SPDK_ERRLOG("Duplicate bdev name for manual examine: %s\n", name);
620 		return -EEXIST;
621 	}
622 
623 	item = calloc(1, sizeof(*item));
624 	if (!item) {
625 		return -ENOMEM;
626 	}
627 	item->name = strdup(name);
628 	if (!item->name) {
629 		free(item);
630 		return -ENOMEM;
631 	}
632 	TAILQ_INSERT_TAIL(&g_bdev_examine_allowlist, item, link);
633 
634 	bdev = spdk_bdev_get_by_name(name);
635 	if (bdev) {
636 		bdev_examine(bdev);
637 	}
638 	return 0;
639 }
640 
641 static inline void
642 bdev_examine_allowlist_config_json(struct spdk_json_write_ctx *w)
643 {
644 	struct spdk_bdev_examine_item *item;
645 	TAILQ_FOREACH(item, &g_bdev_examine_allowlist, link) {
646 		spdk_json_write_object_begin(w);
647 		spdk_json_write_named_string(w, "method", "bdev_examine");
648 		spdk_json_write_named_object_begin(w, "params");
649 		spdk_json_write_named_string(w, "name", item->name);
650 		spdk_json_write_object_end(w);
651 		spdk_json_write_object_end(w);
652 	}
653 }
654 
655 struct spdk_bdev *
656 spdk_bdev_first(void)
657 {
658 	struct spdk_bdev *bdev;
659 
660 	bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs);
661 	if (bdev) {
662 		SPDK_DEBUGLOG(bdev, "Starting bdev iteration at %s\n", bdev->name);
663 	}
664 
665 	return bdev;
666 }
667 
668 struct spdk_bdev *
669 spdk_bdev_next(struct spdk_bdev *prev)
670 {
671 	struct spdk_bdev *bdev;
672 
673 	bdev = TAILQ_NEXT(prev, internal.link);
674 	if (bdev) {
675 		SPDK_DEBUGLOG(bdev, "Continuing bdev iteration at %s\n", bdev->name);
676 	}
677 
678 	return bdev;
679 }
680 
681 static struct spdk_bdev *
682 _bdev_next_leaf(struct spdk_bdev *bdev)
683 {
684 	while (bdev != NULL) {
685 		if (bdev->internal.claim_module == NULL) {
686 			return bdev;
687 		} else {
688 			bdev = TAILQ_NEXT(bdev, internal.link);
689 		}
690 	}
691 
692 	return bdev;
693 }
694 
695 struct spdk_bdev *
696 spdk_bdev_first_leaf(void)
697 {
698 	struct spdk_bdev *bdev;
699 
700 	bdev = _bdev_next_leaf(TAILQ_FIRST(&g_bdev_mgr.bdevs));
701 
702 	if (bdev) {
703 		SPDK_DEBUGLOG(bdev, "Starting bdev iteration at %s\n", bdev->name);
704 	}
705 
706 	return bdev;
707 }
708 
709 struct spdk_bdev *
710 spdk_bdev_next_leaf(struct spdk_bdev *prev)
711 {
712 	struct spdk_bdev *bdev;
713 
714 	bdev = _bdev_next_leaf(TAILQ_NEXT(prev, internal.link));
715 
716 	if (bdev) {
717 		SPDK_DEBUGLOG(bdev, "Continuing bdev iteration at %s\n", bdev->name);
718 	}
719 
720 	return bdev;
721 }
722 
723 static inline bool
724 bdev_io_use_memory_domain(struct spdk_bdev_io *bdev_io)
725 {
726 	return bdev_io->internal.ext_opts && bdev_io->internal.ext_opts->memory_domain;
727 }
728 
729 void
730 spdk_bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len)
731 {
732 	struct iovec *iovs;
733 
734 	if (bdev_io->u.bdev.iovs == NULL) {
735 		bdev_io->u.bdev.iovs = &bdev_io->iov;
736 		bdev_io->u.bdev.iovcnt = 1;
737 	}
738 
739 	iovs = bdev_io->u.bdev.iovs;
740 
741 	assert(iovs != NULL);
742 	assert(bdev_io->u.bdev.iovcnt >= 1);
743 
744 	iovs[0].iov_base = buf;
745 	iovs[0].iov_len = len;
746 }
747 
748 void
749 spdk_bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len)
750 {
751 	assert((len / spdk_bdev_get_md_size(bdev_io->bdev)) >= bdev_io->u.bdev.num_blocks);
752 	bdev_io->u.bdev.md_buf = md_buf;
753 }
754 
755 static bool
756 _is_buf_allocated(const struct iovec *iovs)
757 {
758 	if (iovs == NULL) {
759 		return false;
760 	}
761 
762 	return iovs[0].iov_base != NULL;
763 }
764 
765 static bool
766 _are_iovs_aligned(struct iovec *iovs, int iovcnt, uint32_t alignment)
767 {
768 	int i;
769 	uintptr_t iov_base;
770 
771 	if (spdk_likely(alignment == 1)) {
772 		return true;
773 	}
774 
775 	for (i = 0; i < iovcnt; i++) {
776 		iov_base = (uintptr_t)iovs[i].iov_base;
777 		if ((iov_base & (alignment - 1)) != 0) {
778 			return false;
779 		}
780 	}
781 
782 	return true;
783 }
784 
785 static void
786 bdev_io_get_buf_complete(struct spdk_bdev_io *bdev_io, bool status)
787 {
788 	struct spdk_io_channel *ch = spdk_bdev_io_get_io_channel(bdev_io);
789 	void *buf;
790 
791 	if (spdk_unlikely(bdev_io->internal.get_aux_buf_cb != NULL)) {
792 		buf = bdev_io->internal.buf;
793 		bdev_io->internal.buf = NULL;
794 		bdev_io->internal.get_aux_buf_cb(ch, bdev_io, buf);
795 		bdev_io->internal.get_aux_buf_cb = NULL;
796 	} else {
797 		assert(bdev_io->internal.get_buf_cb != NULL);
798 		bdev_io->internal.get_buf_cb(ch, bdev_io, status);
799 		bdev_io->internal.get_buf_cb = NULL;
800 	}
801 }
802 
803 static void
804 _bdev_io_pull_buffer_cpl(void *ctx, int rc)
805 {
806 	struct spdk_bdev_io *bdev_io = ctx;
807 
808 	if (rc) {
809 		SPDK_ERRLOG("Set bounce buffer failed with rc %d\n", rc);
810 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
811 	}
812 	bdev_io_get_buf_complete(bdev_io, !rc);
813 }
814 
815 static void
816 _bdev_io_pull_bounce_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len)
817 {
818 	int rc = 0;
819 
820 	/* save original md_buf */
821 	bdev_io->internal.orig_md_iov.iov_base = bdev_io->u.bdev.md_buf;
822 	bdev_io->internal.orig_md_iov.iov_len = len;
823 	bdev_io->internal.bounce_md_iov.iov_base = md_buf;
824 	bdev_io->internal.bounce_md_iov.iov_len = len;
825 	/* set bounce md_buf */
826 	bdev_io->u.bdev.md_buf = md_buf;
827 
828 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
829 		if (bdev_io_use_memory_domain(bdev_io)) {
830 			rc = spdk_memory_domain_pull_data(bdev_io->internal.ext_opts->memory_domain,
831 							  bdev_io->internal.ext_opts->memory_domain_ctx,
832 							  &bdev_io->internal.orig_md_iov, 1,
833 							  &bdev_io->internal.bounce_md_iov, 1,
834 							  bdev_io->internal.data_transfer_cpl,
835 							  bdev_io);
836 			if (rc == 0) {
837 				/* Continue to submit IO in completion callback */
838 				return;
839 			}
840 			SPDK_ERRLOG("Failed to pull data from memory domain %s, rc %d\n",
841 				    spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain), rc);
842 		} else {
843 			memcpy(md_buf, bdev_io->internal.orig_md_iov.iov_base, bdev_io->internal.orig_md_iov.iov_len);
844 		}
845 	}
846 
847 	assert(bdev_io->internal.data_transfer_cpl);
848 	bdev_io->internal.data_transfer_cpl(bdev_io, rc);
849 }
850 
851 static void
852 _bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io)
853 {
854 	struct spdk_bdev *bdev = bdev_io->bdev;
855 	uint64_t md_len;
856 	void *buf;
857 
858 	if (spdk_bdev_is_md_separate(bdev)) {
859 		buf = (char *)bdev_io->u.bdev.iovs[0].iov_base + bdev_io->u.bdev.iovs[0].iov_len;
860 		md_len = bdev_io->u.bdev.num_blocks * bdev->md_len;
861 
862 		assert(((uintptr_t)buf & (spdk_bdev_get_buf_align(bdev) - 1)) == 0);
863 
864 		if (bdev_io->u.bdev.md_buf != NULL) {
865 			_bdev_io_pull_bounce_md_buf(bdev_io, buf, md_len);
866 			return;
867 		} else {
868 			spdk_bdev_io_set_md_buf(bdev_io, buf, md_len);
869 		}
870 	}
871 
872 	bdev_io_get_buf_complete(bdev_io, true);
873 }
874 
875 static void
876 _bdev_io_pull_bounce_data_buf_done(void *ctx, int rc)
877 {
878 	struct spdk_bdev_io *bdev_io = ctx;
879 
880 	if (rc) {
881 		SPDK_ERRLOG("Failed to get data buffer\n");
882 		assert(bdev_io->internal.data_transfer_cpl);
883 		bdev_io->internal.data_transfer_cpl(bdev_io, rc);
884 		return;
885 	}
886 
887 	_bdev_io_set_md_buf(bdev_io);
888 }
889 
890 static void
891 _bdev_io_pull_bounce_data_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len,
892 			      bdev_copy_bounce_buffer_cpl cpl_cb)
893 {
894 	int rc = 0;
895 
896 	bdev_io->internal.data_transfer_cpl = cpl_cb;
897 	/* save original iovec */
898 	bdev_io->internal.orig_iovs = bdev_io->u.bdev.iovs;
899 	bdev_io->internal.orig_iovcnt = bdev_io->u.bdev.iovcnt;
900 	/* set bounce iov */
901 	bdev_io->u.bdev.iovs = &bdev_io->internal.bounce_iov;
902 	bdev_io->u.bdev.iovcnt = 1;
903 	/* set bounce buffer for this operation */
904 	bdev_io->u.bdev.iovs[0].iov_base = buf;
905 	bdev_io->u.bdev.iovs[0].iov_len = len;
906 	/* if this is write path, copy data from original buffer to bounce buffer */
907 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
908 		if (bdev_io_use_memory_domain(bdev_io)) {
909 			rc = spdk_memory_domain_pull_data(bdev_io->internal.ext_opts->memory_domain,
910 							  bdev_io->internal.ext_opts->memory_domain_ctx,
911 							  bdev_io->internal.orig_iovs,
912 							  (uint32_t) bdev_io->internal.orig_iovcnt,
913 							  bdev_io->u.bdev.iovs, 1,
914 							  _bdev_io_pull_bounce_data_buf_done,
915 							  bdev_io);
916 			if (rc == 0) {
917 				/* Continue to submit IO in completion callback */
918 				return;
919 			}
920 			SPDK_ERRLOG("Failed to pull data from memory domain %s\n",
921 				    spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain));
922 		} else {
923 			spdk_copy_iovs_to_buf(buf, len, bdev_io->internal.orig_iovs, bdev_io->internal.orig_iovcnt);
924 		}
925 	}
926 
927 	_bdev_io_pull_bounce_data_buf_done(bdev_io, rc);
928 }
929 
930 static void
931 _bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t len)
932 {
933 	struct spdk_bdev *bdev = bdev_io->bdev;
934 	bool buf_allocated;
935 	uint64_t alignment;
936 	void *aligned_buf;
937 
938 	bdev_io->internal.buf = buf;
939 
940 	if (spdk_unlikely(bdev_io->internal.get_aux_buf_cb != NULL)) {
941 		bdev_io_get_buf_complete(bdev_io, true);
942 		return;
943 	}
944 
945 	alignment = spdk_bdev_get_buf_align(bdev);
946 	buf_allocated = _is_buf_allocated(bdev_io->u.bdev.iovs);
947 	aligned_buf = (void *)(((uintptr_t)buf + (alignment - 1)) & ~(alignment - 1));
948 
949 	if (buf_allocated) {
950 		_bdev_io_pull_bounce_data_buf(bdev_io, aligned_buf, len, _bdev_io_pull_buffer_cpl);
951 		/* Continue in completion callback */
952 		return;
953 	} else {
954 		spdk_bdev_io_set_buf(bdev_io, aligned_buf, len);
955 	}
956 
957 	_bdev_io_set_md_buf(bdev_io);
958 }
959 
960 static void
961 _bdev_io_put_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t buf_len)
962 {
963 	struct spdk_bdev *bdev = bdev_io->bdev;
964 	struct spdk_mempool *pool;
965 	struct spdk_bdev_io *tmp;
966 	bdev_io_stailq_t *stailq;
967 	struct spdk_bdev_mgmt_channel *ch;
968 	uint64_t md_len, alignment;
969 
970 	md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0;
971 	alignment = spdk_bdev_get_buf_align(bdev);
972 	ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
973 
974 	if (buf_len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) +
975 	    SPDK_BDEV_POOL_ALIGNMENT) {
976 		pool = g_bdev_mgr.buf_small_pool;
977 		stailq = &ch->need_buf_small;
978 	} else {
979 		pool = g_bdev_mgr.buf_large_pool;
980 		stailq = &ch->need_buf_large;
981 	}
982 
983 	if (STAILQ_EMPTY(stailq)) {
984 		spdk_mempool_put(pool, buf);
985 	} else {
986 		tmp = STAILQ_FIRST(stailq);
987 		STAILQ_REMOVE_HEAD(stailq, internal.buf_link);
988 		_bdev_io_set_buf(tmp, buf, tmp->internal.buf_len);
989 	}
990 }
991 
992 static void
993 bdev_io_put_buf(struct spdk_bdev_io *bdev_io)
994 {
995 	assert(bdev_io->internal.buf != NULL);
996 	_bdev_io_put_buf(bdev_io, bdev_io->internal.buf, bdev_io->internal.buf_len);
997 	bdev_io->internal.buf = NULL;
998 }
999 
1000 void
1001 spdk_bdev_io_put_aux_buf(struct spdk_bdev_io *bdev_io, void *buf)
1002 {
1003 	uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
1004 
1005 	assert(buf != NULL);
1006 	_bdev_io_put_buf(bdev_io, buf, len);
1007 }
1008 
1009 static void
1010 bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch)
1011 {
1012 	struct spdk_bdev *bdev = bdev_ch->bdev;
1013 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
1014 	struct spdk_bdev_io *bdev_io;
1015 
1016 	if (shared_resource->io_outstanding > shared_resource->nomem_threshold) {
1017 		/*
1018 		 * Allow some more I/O to complete before retrying the nomem_io queue.
1019 		 *  Some drivers (such as nvme) cannot immediately take a new I/O in
1020 		 *  the context of a completion, because the resources for the I/O are
1021 		 *  not released until control returns to the bdev poller.  Also, we
1022 		 *  may require several small I/O to complete before a larger I/O
1023 		 *  (that requires splitting) can be submitted.
1024 		 */
1025 		return;
1026 	}
1027 
1028 	while (!TAILQ_EMPTY(&shared_resource->nomem_io)) {
1029 		bdev_io = TAILQ_FIRST(&shared_resource->nomem_io);
1030 		TAILQ_REMOVE(&shared_resource->nomem_io, bdev_io, internal.link);
1031 		bdev_io->internal.ch->io_outstanding++;
1032 		shared_resource->io_outstanding++;
1033 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
1034 		bdev_io->internal.error.nvme.cdw0 = 0;
1035 		bdev_io->num_retries++;
1036 		bdev->fn_table->submit_request(spdk_bdev_io_get_io_channel(bdev_io), bdev_io);
1037 		if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
1038 			break;
1039 		}
1040 	}
1041 }
1042 
1043 static inline void
1044 _bdev_io_decrement_outstanding(struct spdk_bdev_channel *bdev_ch,
1045 			       struct spdk_bdev_shared_resource *shared_resource)
1046 {
1047 	assert(bdev_ch->io_outstanding > 0);
1048 	assert(shared_resource->io_outstanding > 0);
1049 	bdev_ch->io_outstanding--;
1050 	shared_resource->io_outstanding--;
1051 }
1052 
1053 static inline bool
1054 _bdev_io_handle_no_mem(struct spdk_bdev_io *bdev_io)
1055 {
1056 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
1057 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
1058 
1059 	if (spdk_unlikely(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM)) {
1060 		TAILQ_INSERT_HEAD(&shared_resource->nomem_io, bdev_io, internal.link);
1061 		/*
1062 		 * Wait for some of the outstanding I/O to complete before we
1063 		 *  retry any of the nomem_io.  Normally we will wait for
1064 		 *  NOMEM_THRESHOLD_COUNT I/O to complete but for low queue
1065 		 *  depth channels we will instead wait for half to complete.
1066 		 */
1067 		shared_resource->nomem_threshold = spdk_max((int64_t)shared_resource->io_outstanding / 2,
1068 						   (int64_t)shared_resource->io_outstanding - NOMEM_THRESHOLD_COUNT);
1069 		return true;
1070 	}
1071 
1072 	if (spdk_unlikely(!TAILQ_EMPTY(&shared_resource->nomem_io))) {
1073 		bdev_ch_retry_io(bdev_ch);
1074 	}
1075 
1076 	return false;
1077 }
1078 
1079 static void
1080 _bdev_io_complete_push_bounce_done(void *ctx, int rc)
1081 {
1082 	struct spdk_bdev_io *bdev_io = ctx;
1083 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
1084 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
1085 
1086 	if (rc) {
1087 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1088 	}
1089 	/* We want to free the bounce buffer here since we know we're done with it (as opposed
1090 	 * to waiting for the conditional free of internal.buf in spdk_bdev_free_io()).
1091 	 */
1092 	bdev_io_put_buf(bdev_io);
1093 
1094 	/* Continue with IO completion flow */
1095 	_bdev_io_decrement_outstanding(bdev_ch, shared_resource);
1096 	if (spdk_unlikely(_bdev_io_handle_no_mem(bdev_io))) {
1097 		return;
1098 	}
1099 
1100 	bdev_io_complete(bdev_io);
1101 }
1102 
1103 static inline void
1104 _bdev_io_push_bounce_md_buffer(struct spdk_bdev_io *bdev_io)
1105 {
1106 	int rc = 0;
1107 
1108 	/* do the same for metadata buffer */
1109 	if (spdk_unlikely(bdev_io->internal.orig_md_iov.iov_base != NULL)) {
1110 		assert(spdk_bdev_is_md_separate(bdev_io->bdev));
1111 
1112 		if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ &&
1113 		    bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
1114 			if (bdev_io_use_memory_domain(bdev_io)) {
1115 				/* If memory domain is used then we need to call async push function */
1116 				rc = spdk_memory_domain_push_data(bdev_io->internal.ext_opts->memory_domain,
1117 								  bdev_io->internal.ext_opts->memory_domain_ctx,
1118 								  &bdev_io->internal.orig_md_iov,
1119 								  (uint32_t)bdev_io->internal.orig_iovcnt,
1120 								  &bdev_io->internal.bounce_md_iov, 1,
1121 								  bdev_io->internal.data_transfer_cpl,
1122 								  bdev_io);
1123 				if (rc == 0) {
1124 					/* Continue IO completion in async callback */
1125 					return;
1126 				}
1127 				SPDK_ERRLOG("Failed to push md to memory domain %s\n",
1128 					    spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain));
1129 			} else {
1130 				memcpy(bdev_io->internal.orig_md_iov.iov_base, bdev_io->u.bdev.md_buf,
1131 				       bdev_io->internal.orig_md_iov.iov_len);
1132 			}
1133 		}
1134 	}
1135 
1136 	assert(bdev_io->internal.data_transfer_cpl);
1137 	bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1138 }
1139 
1140 static void
1141 _bdev_io_push_bounce_data_buffer_done(void *ctx, int rc)
1142 {
1143 	struct spdk_bdev_io *bdev_io = ctx;
1144 
1145 	assert(bdev_io->internal.data_transfer_cpl);
1146 
1147 	if (rc) {
1148 		bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1149 		return;
1150 	}
1151 
1152 	/* set original buffer for this io */
1153 	bdev_io->u.bdev.iovcnt = bdev_io->internal.orig_iovcnt;
1154 	bdev_io->u.bdev.iovs = bdev_io->internal.orig_iovs;
1155 	/* disable bouncing buffer for this io */
1156 	bdev_io->internal.orig_iovcnt = 0;
1157 	bdev_io->internal.orig_iovs = NULL;
1158 
1159 	_bdev_io_push_bounce_md_buffer(bdev_io);
1160 }
1161 
1162 static inline void
1163 _bdev_io_push_bounce_data_buffer(struct spdk_bdev_io *bdev_io, bdev_copy_bounce_buffer_cpl cpl_cb)
1164 {
1165 	int rc = 0;
1166 
1167 	bdev_io->internal.data_transfer_cpl = cpl_cb;
1168 
1169 	/* if this is read path, copy data from bounce buffer to original buffer */
1170 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ &&
1171 	    bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
1172 		if (bdev_io_use_memory_domain(bdev_io)) {
1173 			/* If memory domain is used then we need to call async push function */
1174 			rc = spdk_memory_domain_push_data(bdev_io->internal.ext_opts->memory_domain,
1175 							  bdev_io->internal.ext_opts->memory_domain_ctx,
1176 							  bdev_io->internal.orig_iovs,
1177 							  (uint32_t)bdev_io->internal.orig_iovcnt,
1178 							  &bdev_io->internal.bounce_iov, 1,
1179 							  _bdev_io_push_bounce_data_buffer_done,
1180 							  bdev_io);
1181 			if (rc == 0) {
1182 				/* Continue IO completion in async callback */
1183 				return;
1184 			}
1185 			SPDK_ERRLOG("Failed to push data to memory domain %s\n",
1186 				    spdk_memory_domain_get_dma_device_id(bdev_io->internal.ext_opts->memory_domain));
1187 		} else {
1188 			spdk_copy_buf_to_iovs(bdev_io->internal.orig_iovs,
1189 					      bdev_io->internal.orig_iovcnt,
1190 					      bdev_io->internal.bounce_iov.iov_base,
1191 					      bdev_io->internal.bounce_iov.iov_len);
1192 		}
1193 	}
1194 
1195 	_bdev_io_push_bounce_data_buffer_done(bdev_io, rc);
1196 }
1197 
1198 static void
1199 bdev_io_get_buf(struct spdk_bdev_io *bdev_io, uint64_t len)
1200 {
1201 	struct spdk_bdev *bdev = bdev_io->bdev;
1202 	struct spdk_mempool *pool;
1203 	bdev_io_stailq_t *stailq;
1204 	struct spdk_bdev_mgmt_channel *mgmt_ch;
1205 	uint64_t alignment, md_len;
1206 	void *buf;
1207 
1208 	alignment = spdk_bdev_get_buf_align(bdev);
1209 	md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0;
1210 
1211 	if (len + alignment + md_len > SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) +
1212 	    SPDK_BDEV_POOL_ALIGNMENT) {
1213 		SPDK_ERRLOG("Length + alignment %" PRIu64 " is larger than allowed\n",
1214 			    len + alignment);
1215 		bdev_io_get_buf_complete(bdev_io, false);
1216 		return;
1217 	}
1218 
1219 	mgmt_ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
1220 
1221 	bdev_io->internal.buf_len = len;
1222 
1223 	if (len + alignment + md_len <= SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) +
1224 	    SPDK_BDEV_POOL_ALIGNMENT) {
1225 		pool = g_bdev_mgr.buf_small_pool;
1226 		stailq = &mgmt_ch->need_buf_small;
1227 	} else {
1228 		pool = g_bdev_mgr.buf_large_pool;
1229 		stailq = &mgmt_ch->need_buf_large;
1230 	}
1231 
1232 	buf = spdk_mempool_get(pool);
1233 	if (!buf) {
1234 		STAILQ_INSERT_TAIL(stailq, bdev_io, internal.buf_link);
1235 	} else {
1236 		_bdev_io_set_buf(bdev_io, buf, len);
1237 	}
1238 }
1239 
1240 void
1241 spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
1242 {
1243 	struct spdk_bdev *bdev = bdev_io->bdev;
1244 	uint64_t alignment;
1245 
1246 	assert(cb != NULL);
1247 	bdev_io->internal.get_buf_cb = cb;
1248 
1249 	alignment = spdk_bdev_get_buf_align(bdev);
1250 
1251 	if (_is_buf_allocated(bdev_io->u.bdev.iovs) &&
1252 	    _are_iovs_aligned(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, alignment)) {
1253 		/* Buffer already present and aligned */
1254 		cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true);
1255 		return;
1256 	}
1257 
1258 	bdev_io_get_buf(bdev_io, len);
1259 }
1260 
1261 static void
1262 _bdev_memory_domain_get_io_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
1263 			      bool success)
1264 {
1265 	if (!success) {
1266 		SPDK_ERRLOG("Failed to get data buffer, completing IO\n");
1267 		bdev_io_complete(bdev_io);
1268 	} else {
1269 		bdev_io_submit(bdev_io);
1270 	}
1271 }
1272 
1273 static void
1274 _bdev_memory_domain_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb,
1275 			       uint64_t len)
1276 {
1277 	assert(cb != NULL);
1278 	bdev_io->internal.get_buf_cb = cb;
1279 
1280 	bdev_io_get_buf(bdev_io, len);
1281 }
1282 
1283 void
1284 spdk_bdev_io_get_aux_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_aux_buf_cb cb)
1285 {
1286 	uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
1287 
1288 	assert(cb != NULL);
1289 	assert(bdev_io->internal.get_aux_buf_cb == NULL);
1290 	bdev_io->internal.get_aux_buf_cb = cb;
1291 	bdev_io_get_buf(bdev_io, len);
1292 }
1293 
1294 static int
1295 bdev_module_get_max_ctx_size(void)
1296 {
1297 	struct spdk_bdev_module *bdev_module;
1298 	int max_bdev_module_size = 0;
1299 
1300 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
1301 		if (bdev_module->get_ctx_size && bdev_module->get_ctx_size() > max_bdev_module_size) {
1302 			max_bdev_module_size = bdev_module->get_ctx_size();
1303 		}
1304 	}
1305 
1306 	return max_bdev_module_size;
1307 }
1308 
1309 static void
1310 bdev_qos_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
1311 {
1312 	int i;
1313 	struct spdk_bdev_qos *qos = bdev->internal.qos;
1314 	uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
1315 
1316 	if (!qos) {
1317 		return;
1318 	}
1319 
1320 	spdk_bdev_get_qos_rate_limits(bdev, limits);
1321 
1322 	spdk_json_write_object_begin(w);
1323 	spdk_json_write_named_string(w, "method", "bdev_set_qos_limit");
1324 
1325 	spdk_json_write_named_object_begin(w, "params");
1326 	spdk_json_write_named_string(w, "name", bdev->name);
1327 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1328 		if (limits[i] > 0) {
1329 			spdk_json_write_named_uint64(w, qos_rpc_type[i], limits[i]);
1330 		}
1331 	}
1332 	spdk_json_write_object_end(w);
1333 
1334 	spdk_json_write_object_end(w);
1335 }
1336 
1337 void
1338 spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w)
1339 {
1340 	struct spdk_bdev_module *bdev_module;
1341 	struct spdk_bdev *bdev;
1342 
1343 	assert(w != NULL);
1344 
1345 	spdk_json_write_array_begin(w);
1346 
1347 	spdk_json_write_object_begin(w);
1348 	spdk_json_write_named_string(w, "method", "bdev_set_options");
1349 	spdk_json_write_named_object_begin(w, "params");
1350 	spdk_json_write_named_uint32(w, "bdev_io_pool_size", g_bdev_opts.bdev_io_pool_size);
1351 	spdk_json_write_named_uint32(w, "bdev_io_cache_size", g_bdev_opts.bdev_io_cache_size);
1352 	spdk_json_write_named_bool(w, "bdev_auto_examine", g_bdev_opts.bdev_auto_examine);
1353 	spdk_json_write_object_end(w);
1354 	spdk_json_write_object_end(w);
1355 
1356 	bdev_examine_allowlist_config_json(w);
1357 
1358 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
1359 		if (bdev_module->config_json) {
1360 			bdev_module->config_json(w);
1361 		}
1362 	}
1363 
1364 	pthread_mutex_lock(&g_bdev_mgr.mutex);
1365 
1366 	TAILQ_FOREACH(bdev, &g_bdev_mgr.bdevs, internal.link) {
1367 		if (bdev->fn_table->write_config_json) {
1368 			bdev->fn_table->write_config_json(bdev, w);
1369 		}
1370 
1371 		bdev_qos_config_json(bdev, w);
1372 	}
1373 
1374 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
1375 
1376 	/* This has to be last RPC in array to make sure all bdevs finished examine */
1377 	spdk_json_write_object_begin(w);
1378 	spdk_json_write_named_string(w, "method", "bdev_wait_for_examine");
1379 	spdk_json_write_object_end(w);
1380 
1381 	spdk_json_write_array_end(w);
1382 }
1383 
1384 static int
1385 bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
1386 {
1387 	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
1388 	struct spdk_bdev_io *bdev_io;
1389 	uint32_t i;
1390 
1391 	STAILQ_INIT(&ch->need_buf_small);
1392 	STAILQ_INIT(&ch->need_buf_large);
1393 
1394 	STAILQ_INIT(&ch->per_thread_cache);
1395 	ch->bdev_io_cache_size = g_bdev_opts.bdev_io_cache_size;
1396 
1397 	/* Pre-populate bdev_io cache to ensure this thread cannot be starved. */
1398 	ch->per_thread_cache_count = 0;
1399 	for (i = 0; i < ch->bdev_io_cache_size; i++) {
1400 		bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
1401 		assert(bdev_io != NULL);
1402 		ch->per_thread_cache_count++;
1403 		STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
1404 	}
1405 
1406 	TAILQ_INIT(&ch->shared_resources);
1407 	TAILQ_INIT(&ch->io_wait_queue);
1408 
1409 	return 0;
1410 }
1411 
1412 static void
1413 bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf)
1414 {
1415 	struct spdk_bdev_mgmt_channel *ch = ctx_buf;
1416 	struct spdk_bdev_io *bdev_io;
1417 
1418 	if (!STAILQ_EMPTY(&ch->need_buf_small) || !STAILQ_EMPTY(&ch->need_buf_large)) {
1419 		SPDK_ERRLOG("Pending I/O list wasn't empty on mgmt channel free\n");
1420 	}
1421 
1422 	if (!TAILQ_EMPTY(&ch->shared_resources)) {
1423 		SPDK_ERRLOG("Module channel list wasn't empty on mgmt channel free\n");
1424 	}
1425 
1426 	while (!STAILQ_EMPTY(&ch->per_thread_cache)) {
1427 		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
1428 		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
1429 		ch->per_thread_cache_count--;
1430 		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
1431 	}
1432 
1433 	assert(ch->per_thread_cache_count == 0);
1434 }
1435 
1436 static void
1437 bdev_init_complete(int rc)
1438 {
1439 	spdk_bdev_init_cb cb_fn = g_init_cb_fn;
1440 	void *cb_arg = g_init_cb_arg;
1441 	struct spdk_bdev_module *m;
1442 
1443 	g_bdev_mgr.init_complete = true;
1444 	g_init_cb_fn = NULL;
1445 	g_init_cb_arg = NULL;
1446 
1447 	/*
1448 	 * For modules that need to know when subsystem init is complete,
1449 	 * inform them now.
1450 	 */
1451 	if (rc == 0) {
1452 		TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
1453 			if (m->init_complete) {
1454 				m->init_complete();
1455 			}
1456 		}
1457 	}
1458 
1459 	cb_fn(cb_arg, rc);
1460 }
1461 
1462 static bool
1463 bdev_module_all_actions_completed(void)
1464 {
1465 	struct spdk_bdev_module *m;
1466 
1467 	TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
1468 		if (m->internal.action_in_progress > 0) {
1469 			return false;
1470 		}
1471 	}
1472 	return true;
1473 }
1474 
1475 static void
1476 bdev_module_action_complete(void)
1477 {
1478 	/*
1479 	 * Don't finish bdev subsystem initialization if
1480 	 * module pre-initialization is still in progress, or
1481 	 * the subsystem been already initialized.
1482 	 */
1483 	if (!g_bdev_mgr.module_init_complete || g_bdev_mgr.init_complete) {
1484 		return;
1485 	}
1486 
1487 	/*
1488 	 * Check all bdev modules for inits/examinations in progress. If any
1489 	 * exist, return immediately since we cannot finish bdev subsystem
1490 	 * initialization until all are completed.
1491 	 */
1492 	if (!bdev_module_all_actions_completed()) {
1493 		return;
1494 	}
1495 
1496 	/*
1497 	 * Modules already finished initialization - now that all
1498 	 * the bdev modules have finished their asynchronous I/O
1499 	 * processing, the entire bdev layer can be marked as complete.
1500 	 */
1501 	bdev_init_complete(0);
1502 }
1503 
1504 static void
1505 bdev_module_action_done(struct spdk_bdev_module *module)
1506 {
1507 	assert(module->internal.action_in_progress > 0);
1508 	module->internal.action_in_progress--;
1509 	bdev_module_action_complete();
1510 }
1511 
1512 void
1513 spdk_bdev_module_init_done(struct spdk_bdev_module *module)
1514 {
1515 	bdev_module_action_done(module);
1516 }
1517 
1518 void
1519 spdk_bdev_module_examine_done(struct spdk_bdev_module *module)
1520 {
1521 	bdev_module_action_done(module);
1522 }
1523 
1524 /** The last initialized bdev module */
1525 static struct spdk_bdev_module *g_resume_bdev_module = NULL;
1526 
1527 static void
1528 bdev_init_failed(void *cb_arg)
1529 {
1530 	struct spdk_bdev_module *module = cb_arg;
1531 
1532 	module->internal.action_in_progress--;
1533 	bdev_init_complete(-1);
1534 }
1535 
1536 static int
1537 bdev_modules_init(void)
1538 {
1539 	struct spdk_bdev_module *module;
1540 	int rc = 0;
1541 
1542 	TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
1543 		g_resume_bdev_module = module;
1544 		if (module->async_init) {
1545 			module->internal.action_in_progress = 1;
1546 		}
1547 		rc = module->module_init();
1548 		if (rc != 0) {
1549 			/* Bump action_in_progress to prevent other modules from completion of modules_init
1550 			 * Send message to defer application shutdown until resources are cleaned up */
1551 			module->internal.action_in_progress = 1;
1552 			spdk_thread_send_msg(spdk_get_thread(), bdev_init_failed, module);
1553 			return rc;
1554 		}
1555 	}
1556 
1557 	g_resume_bdev_module = NULL;
1558 	return 0;
1559 }
1560 
1561 void
1562 spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg)
1563 {
1564 	int cache_size;
1565 	int rc = 0;
1566 	char mempool_name[32];
1567 
1568 	assert(cb_fn != NULL);
1569 
1570 	g_init_cb_fn = cb_fn;
1571 	g_init_cb_arg = cb_arg;
1572 
1573 	spdk_notify_type_register("bdev_register");
1574 	spdk_notify_type_register("bdev_unregister");
1575 
1576 	snprintf(mempool_name, sizeof(mempool_name), "bdev_io_%d", getpid());
1577 
1578 	g_bdev_mgr.bdev_io_pool = spdk_mempool_create(mempool_name,
1579 				  g_bdev_opts.bdev_io_pool_size,
1580 				  sizeof(struct spdk_bdev_io) +
1581 				  bdev_module_get_max_ctx_size(),
1582 				  0,
1583 				  SPDK_ENV_SOCKET_ID_ANY);
1584 
1585 	if (g_bdev_mgr.bdev_io_pool == NULL) {
1586 		SPDK_ERRLOG("could not allocate spdk_bdev_io pool\n");
1587 		bdev_init_complete(-1);
1588 		return;
1589 	}
1590 
1591 	/**
1592 	 * Ensure no more than half of the total buffers end up local caches, by
1593 	 *   using spdk_env_get_core_count() to determine how many local caches we need
1594 	 *   to account for.
1595 	 */
1596 	cache_size = BUF_SMALL_POOL_SIZE / (2 * spdk_env_get_core_count());
1597 	snprintf(mempool_name, sizeof(mempool_name), "buf_small_pool_%d", getpid());
1598 
1599 	g_bdev_mgr.buf_small_pool = spdk_mempool_create(mempool_name,
1600 				    g_bdev_opts.small_buf_pool_size,
1601 				    SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_SMALL_BUF_MAX_SIZE) +
1602 				    SPDK_BDEV_POOL_ALIGNMENT,
1603 				    cache_size,
1604 				    SPDK_ENV_SOCKET_ID_ANY);
1605 	if (!g_bdev_mgr.buf_small_pool) {
1606 		SPDK_ERRLOG("create rbuf small pool failed\n");
1607 		bdev_init_complete(-1);
1608 		return;
1609 	}
1610 
1611 	cache_size = BUF_LARGE_POOL_SIZE / (2 * spdk_env_get_core_count());
1612 	snprintf(mempool_name, sizeof(mempool_name), "buf_large_pool_%d", getpid());
1613 
1614 	g_bdev_mgr.buf_large_pool = spdk_mempool_create(mempool_name,
1615 				    g_bdev_opts.large_buf_pool_size,
1616 				    SPDK_BDEV_BUF_SIZE_WITH_MD(SPDK_BDEV_LARGE_BUF_MAX_SIZE) +
1617 				    SPDK_BDEV_POOL_ALIGNMENT,
1618 				    cache_size,
1619 				    SPDK_ENV_SOCKET_ID_ANY);
1620 	if (!g_bdev_mgr.buf_large_pool) {
1621 		SPDK_ERRLOG("create rbuf large pool failed\n");
1622 		bdev_init_complete(-1);
1623 		return;
1624 	}
1625 
1626 	g_bdev_mgr.zero_buffer = spdk_zmalloc(ZERO_BUFFER_SIZE, ZERO_BUFFER_SIZE,
1627 					      NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
1628 	if (!g_bdev_mgr.zero_buffer) {
1629 		SPDK_ERRLOG("create bdev zero buffer failed\n");
1630 		bdev_init_complete(-1);
1631 		return;
1632 	}
1633 
1634 #ifdef SPDK_CONFIG_VTUNE
1635 	g_bdev_mgr.domain = __itt_domain_create("spdk_bdev");
1636 #endif
1637 
1638 	spdk_io_device_register(&g_bdev_mgr, bdev_mgmt_channel_create,
1639 				bdev_mgmt_channel_destroy,
1640 				sizeof(struct spdk_bdev_mgmt_channel),
1641 				"bdev_mgr");
1642 
1643 	rc = bdev_modules_init();
1644 	g_bdev_mgr.module_init_complete = true;
1645 	if (rc != 0) {
1646 		SPDK_ERRLOG("bdev modules init failed\n");
1647 		return;
1648 	}
1649 
1650 	bdev_module_action_complete();
1651 }
1652 
1653 static void
1654 bdev_mgr_unregister_cb(void *io_device)
1655 {
1656 	spdk_bdev_fini_cb cb_fn = g_fini_cb_fn;
1657 
1658 	if (g_bdev_mgr.bdev_io_pool) {
1659 		if (spdk_mempool_count(g_bdev_mgr.bdev_io_pool) != g_bdev_opts.bdev_io_pool_size) {
1660 			SPDK_ERRLOG("bdev IO pool count is %zu but should be %u\n",
1661 				    spdk_mempool_count(g_bdev_mgr.bdev_io_pool),
1662 				    g_bdev_opts.bdev_io_pool_size);
1663 		}
1664 
1665 		spdk_mempool_free(g_bdev_mgr.bdev_io_pool);
1666 	}
1667 
1668 	if (g_bdev_mgr.buf_small_pool) {
1669 		if (spdk_mempool_count(g_bdev_mgr.buf_small_pool) != g_bdev_opts.small_buf_pool_size) {
1670 			SPDK_ERRLOG("Small buffer pool count is %zu but should be %u\n",
1671 				    spdk_mempool_count(g_bdev_mgr.buf_small_pool),
1672 				    g_bdev_opts.small_buf_pool_size);
1673 			assert(false);
1674 		}
1675 
1676 		spdk_mempool_free(g_bdev_mgr.buf_small_pool);
1677 	}
1678 
1679 	if (g_bdev_mgr.buf_large_pool) {
1680 		if (spdk_mempool_count(g_bdev_mgr.buf_large_pool) != g_bdev_opts.large_buf_pool_size) {
1681 			SPDK_ERRLOG("Large buffer pool count is %zu but should be %u\n",
1682 				    spdk_mempool_count(g_bdev_mgr.buf_large_pool),
1683 				    g_bdev_opts.large_buf_pool_size);
1684 			assert(false);
1685 		}
1686 
1687 		spdk_mempool_free(g_bdev_mgr.buf_large_pool);
1688 	}
1689 
1690 	spdk_free(g_bdev_mgr.zero_buffer);
1691 
1692 	bdev_examine_allowlist_free();
1693 
1694 	cb_fn(g_fini_cb_arg);
1695 	g_fini_cb_fn = NULL;
1696 	g_fini_cb_arg = NULL;
1697 	g_bdev_mgr.init_complete = false;
1698 	g_bdev_mgr.module_init_complete = false;
1699 }
1700 
1701 static void
1702 bdev_module_fini_iter(void *arg)
1703 {
1704 	struct spdk_bdev_module *bdev_module;
1705 
1706 	/* FIXME: Handling initialization failures is broken now,
1707 	 * so we won't even try cleaning up after successfully
1708 	 * initialized modules. if module_init_complete is false,
1709 	 * just call spdk_bdev_mgr_unregister_cb
1710 	 */
1711 	if (!g_bdev_mgr.module_init_complete) {
1712 		bdev_mgr_unregister_cb(NULL);
1713 		return;
1714 	}
1715 
1716 	/* Start iterating from the last touched module */
1717 	if (!g_resume_bdev_module) {
1718 		bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list);
1719 	} else {
1720 		bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list,
1721 					 internal.tailq);
1722 	}
1723 
1724 	while (bdev_module) {
1725 		if (bdev_module->async_fini) {
1726 			/* Save our place so we can resume later. We must
1727 			 * save the variable here, before calling module_fini()
1728 			 * below, because in some cases the module may immediately
1729 			 * call spdk_bdev_module_fini_done() and re-enter
1730 			 * this function to continue iterating. */
1731 			g_resume_bdev_module = bdev_module;
1732 		}
1733 
1734 		if (bdev_module->module_fini) {
1735 			bdev_module->module_fini();
1736 		}
1737 
1738 		if (bdev_module->async_fini) {
1739 			return;
1740 		}
1741 
1742 		bdev_module = TAILQ_PREV(bdev_module, bdev_module_list,
1743 					 internal.tailq);
1744 	}
1745 
1746 	g_resume_bdev_module = NULL;
1747 	spdk_io_device_unregister(&g_bdev_mgr, bdev_mgr_unregister_cb);
1748 }
1749 
1750 void
1751 spdk_bdev_module_fini_done(void)
1752 {
1753 	if (spdk_get_thread() != g_fini_thread) {
1754 		spdk_thread_send_msg(g_fini_thread, bdev_module_fini_iter, NULL);
1755 	} else {
1756 		bdev_module_fini_iter(NULL);
1757 	}
1758 }
1759 
1760 static void
1761 bdev_finish_unregister_bdevs_iter(void *cb_arg, int bdeverrno)
1762 {
1763 	struct spdk_bdev *bdev = cb_arg;
1764 
1765 	if (bdeverrno && bdev) {
1766 		SPDK_WARNLOG("Unable to unregister bdev '%s' during spdk_bdev_finish()\n",
1767 			     bdev->name);
1768 
1769 		/*
1770 		 * Since the call to spdk_bdev_unregister() failed, we have no way to free this
1771 		 *  bdev; try to continue by manually removing this bdev from the list and continue
1772 		 *  with the next bdev in the list.
1773 		 */
1774 		TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
1775 	}
1776 
1777 	if (TAILQ_EMPTY(&g_bdev_mgr.bdevs)) {
1778 		SPDK_DEBUGLOG(bdev, "Done unregistering bdevs\n");
1779 		/*
1780 		 * Bdev module finish need to be deferred as we might be in the middle of some context
1781 		 * (like bdev part free) that will use this bdev (or private bdev driver ctx data)
1782 		 * after returning.
1783 		 */
1784 		spdk_thread_send_msg(spdk_get_thread(), bdev_module_fini_iter, NULL);
1785 		return;
1786 	}
1787 
1788 	/*
1789 	 * Unregister last unclaimed bdev in the list, to ensure that bdev subsystem
1790 	 * shutdown proceeds top-down. The goal is to give virtual bdevs an opportunity
1791 	 * to detect clean shutdown as opposed to run-time hot removal of the underlying
1792 	 * base bdevs.
1793 	 *
1794 	 * Also, walk the list in the reverse order.
1795 	 */
1796 	for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list);
1797 	     bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) {
1798 		if (bdev->internal.claim_module != NULL) {
1799 			SPDK_DEBUGLOG(bdev, "Skipping claimed bdev '%s'(<-'%s').\n",
1800 				      bdev->name, bdev->internal.claim_module->name);
1801 			continue;
1802 		}
1803 
1804 		SPDK_DEBUGLOG(bdev, "Unregistering bdev '%s'\n", bdev->name);
1805 		spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev);
1806 		return;
1807 	}
1808 
1809 	/*
1810 	 * If any bdev fails to unclaim underlying bdev properly, we may face the
1811 	 * case of bdev list consisting of claimed bdevs only (if claims are managed
1812 	 * correctly, this would mean there's a loop in the claims graph which is
1813 	 * clearly impossible). Warn and unregister last bdev on the list then.
1814 	 */
1815 	for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list);
1816 	     bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) {
1817 		SPDK_WARNLOG("Unregistering claimed bdev '%s'!\n", bdev->name);
1818 		spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev);
1819 		return;
1820 	}
1821 }
1822 
1823 static void
1824 bdev_module_fini_start_iter(void *arg)
1825 {
1826 	struct spdk_bdev_module *bdev_module;
1827 
1828 	if (!g_resume_bdev_module) {
1829 		bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list);
1830 	} else {
1831 		bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list, internal.tailq);
1832 	}
1833 
1834 	while (bdev_module) {
1835 		if (bdev_module->async_fini_start) {
1836 			/* Save our place so we can resume later. We must
1837 			 * save the variable here, before calling fini_start()
1838 			 * below, because in some cases the module may immediately
1839 			 * call spdk_bdev_module_fini_start_done() and re-enter
1840 			 * this function to continue iterating. */
1841 			g_resume_bdev_module = bdev_module;
1842 		}
1843 
1844 		if (bdev_module->fini_start) {
1845 			bdev_module->fini_start();
1846 		}
1847 
1848 		if (bdev_module->async_fini_start) {
1849 			return;
1850 		}
1851 
1852 		bdev_module = TAILQ_PREV(bdev_module, bdev_module_list, internal.tailq);
1853 	}
1854 
1855 	g_resume_bdev_module = NULL;
1856 
1857 	bdev_finish_unregister_bdevs_iter(NULL, 0);
1858 }
1859 
1860 void
1861 spdk_bdev_module_fini_start_done(void)
1862 {
1863 	if (spdk_get_thread() != g_fini_thread) {
1864 		spdk_thread_send_msg(g_fini_thread, bdev_module_fini_start_iter, NULL);
1865 	} else {
1866 		bdev_module_fini_start_iter(NULL);
1867 	}
1868 }
1869 
1870 static void
1871 bdev_finish_wait_for_examine_done(void *cb_arg)
1872 {
1873 	bdev_module_fini_start_iter(NULL);
1874 }
1875 
1876 void
1877 spdk_bdev_finish(spdk_bdev_fini_cb cb_fn, void *cb_arg)
1878 {
1879 	int rc;
1880 
1881 	assert(cb_fn != NULL);
1882 
1883 	g_fini_thread = spdk_get_thread();
1884 
1885 	g_fini_cb_fn = cb_fn;
1886 	g_fini_cb_arg = cb_arg;
1887 
1888 	rc = spdk_bdev_wait_for_examine(bdev_finish_wait_for_examine_done, NULL);
1889 	if (rc != 0) {
1890 		SPDK_ERRLOG("wait_for_examine failed: %s\n", spdk_strerror(-rc));
1891 		bdev_finish_wait_for_examine_done(NULL);
1892 	}
1893 }
1894 
1895 struct spdk_bdev_io *
1896 bdev_channel_get_io(struct spdk_bdev_channel *channel)
1897 {
1898 	struct spdk_bdev_mgmt_channel *ch = channel->shared_resource->mgmt_ch;
1899 	struct spdk_bdev_io *bdev_io;
1900 
1901 	if (ch->per_thread_cache_count > 0) {
1902 		bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
1903 		STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
1904 		ch->per_thread_cache_count--;
1905 	} else if (spdk_unlikely(!TAILQ_EMPTY(&ch->io_wait_queue))) {
1906 		/*
1907 		 * Don't try to look for bdev_ios in the global pool if there are
1908 		 * waiters on bdev_ios - we don't want this caller to jump the line.
1909 		 */
1910 		bdev_io = NULL;
1911 	} else {
1912 		bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
1913 	}
1914 
1915 	return bdev_io;
1916 }
1917 
1918 void
1919 spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
1920 {
1921 	struct spdk_bdev_mgmt_channel *ch;
1922 
1923 	assert(bdev_io != NULL);
1924 	assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING);
1925 
1926 	ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
1927 
1928 	if (bdev_io->internal.buf != NULL) {
1929 		bdev_io_put_buf(bdev_io);
1930 	}
1931 
1932 	if (ch->per_thread_cache_count < ch->bdev_io_cache_size) {
1933 		ch->per_thread_cache_count++;
1934 		STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
1935 		while (ch->per_thread_cache_count > 0 && !TAILQ_EMPTY(&ch->io_wait_queue)) {
1936 			struct spdk_bdev_io_wait_entry *entry;
1937 
1938 			entry = TAILQ_FIRST(&ch->io_wait_queue);
1939 			TAILQ_REMOVE(&ch->io_wait_queue, entry, link);
1940 			entry->cb_fn(entry->cb_arg);
1941 		}
1942 	} else {
1943 		/* We should never have a full cache with entries on the io wait queue. */
1944 		assert(TAILQ_EMPTY(&ch->io_wait_queue));
1945 		spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
1946 	}
1947 }
1948 
1949 static bool
1950 bdev_qos_is_iops_rate_limit(enum spdk_bdev_qos_rate_limit_type limit)
1951 {
1952 	assert(limit != SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
1953 
1954 	switch (limit) {
1955 	case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT:
1956 		return true;
1957 	case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT:
1958 	case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT:
1959 	case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT:
1960 		return false;
1961 	case SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES:
1962 	default:
1963 		return false;
1964 	}
1965 }
1966 
1967 static bool
1968 bdev_qos_io_to_limit(struct spdk_bdev_io *bdev_io)
1969 {
1970 	switch (bdev_io->type) {
1971 	case SPDK_BDEV_IO_TYPE_NVME_IO:
1972 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
1973 	case SPDK_BDEV_IO_TYPE_READ:
1974 	case SPDK_BDEV_IO_TYPE_WRITE:
1975 		return true;
1976 	case SPDK_BDEV_IO_TYPE_ZCOPY:
1977 		if (bdev_io->u.bdev.zcopy.start) {
1978 			return true;
1979 		} else {
1980 			return false;
1981 		}
1982 	default:
1983 		return false;
1984 	}
1985 }
1986 
1987 static bool
1988 bdev_is_read_io(struct spdk_bdev_io *bdev_io)
1989 {
1990 	switch (bdev_io->type) {
1991 	case SPDK_BDEV_IO_TYPE_NVME_IO:
1992 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
1993 		/* Bit 1 (0x2) set for read operation */
1994 		if (bdev_io->u.nvme_passthru.cmd.opc & SPDK_NVME_OPC_READ) {
1995 			return true;
1996 		} else {
1997 			return false;
1998 		}
1999 	case SPDK_BDEV_IO_TYPE_READ:
2000 		return true;
2001 	case SPDK_BDEV_IO_TYPE_ZCOPY:
2002 		/* Populate to read from disk */
2003 		if (bdev_io->u.bdev.zcopy.populate) {
2004 			return true;
2005 		} else {
2006 			return false;
2007 		}
2008 	default:
2009 		return false;
2010 	}
2011 }
2012 
2013 static uint64_t
2014 bdev_get_io_size_in_byte(struct spdk_bdev_io *bdev_io)
2015 {
2016 	struct spdk_bdev	*bdev = bdev_io->bdev;
2017 
2018 	switch (bdev_io->type) {
2019 	case SPDK_BDEV_IO_TYPE_NVME_IO:
2020 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
2021 		return bdev_io->u.nvme_passthru.nbytes;
2022 	case SPDK_BDEV_IO_TYPE_READ:
2023 	case SPDK_BDEV_IO_TYPE_WRITE:
2024 		return bdev_io->u.bdev.num_blocks * bdev->blocklen;
2025 	case SPDK_BDEV_IO_TYPE_ZCOPY:
2026 		/* Track the data in the start phase only */
2027 		if (bdev_io->u.bdev.zcopy.start) {
2028 			return bdev_io->u.bdev.num_blocks * bdev->blocklen;
2029 		} else {
2030 			return 0;
2031 		}
2032 	default:
2033 		return 0;
2034 	}
2035 }
2036 
2037 static bool
2038 bdev_qos_rw_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2039 {
2040 	if (limit->max_per_timeslice > 0 && limit->remaining_this_timeslice <= 0) {
2041 		return true;
2042 	} else {
2043 		return false;
2044 	}
2045 }
2046 
2047 static bool
2048 bdev_qos_r_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2049 {
2050 	if (bdev_is_read_io(io) == false) {
2051 		return false;
2052 	}
2053 
2054 	return bdev_qos_rw_queue_io(limit, io);
2055 }
2056 
2057 static bool
2058 bdev_qos_w_queue_io(const struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2059 {
2060 	if (bdev_is_read_io(io) == true) {
2061 		return false;
2062 	}
2063 
2064 	return bdev_qos_rw_queue_io(limit, io);
2065 }
2066 
2067 static void
2068 bdev_qos_rw_iops_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2069 {
2070 	limit->remaining_this_timeslice--;
2071 }
2072 
2073 static void
2074 bdev_qos_rw_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2075 {
2076 	limit->remaining_this_timeslice -= bdev_get_io_size_in_byte(io);
2077 }
2078 
2079 static void
2080 bdev_qos_r_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2081 {
2082 	if (bdev_is_read_io(io) == false) {
2083 		return;
2084 	}
2085 
2086 	return bdev_qos_rw_bps_update_quota(limit, io);
2087 }
2088 
2089 static void
2090 bdev_qos_w_bps_update_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2091 {
2092 	if (bdev_is_read_io(io) == true) {
2093 		return;
2094 	}
2095 
2096 	return bdev_qos_rw_bps_update_quota(limit, io);
2097 }
2098 
2099 static void
2100 bdev_qos_set_ops(struct spdk_bdev_qos *qos)
2101 {
2102 	int i;
2103 
2104 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2105 		if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
2106 			qos->rate_limits[i].queue_io = NULL;
2107 			qos->rate_limits[i].update_quota = NULL;
2108 			continue;
2109 		}
2110 
2111 		switch (i) {
2112 		case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT:
2113 			qos->rate_limits[i].queue_io = bdev_qos_rw_queue_io;
2114 			qos->rate_limits[i].update_quota = bdev_qos_rw_iops_update_quota;
2115 			break;
2116 		case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT:
2117 			qos->rate_limits[i].queue_io = bdev_qos_rw_queue_io;
2118 			qos->rate_limits[i].update_quota = bdev_qos_rw_bps_update_quota;
2119 			break;
2120 		case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT:
2121 			qos->rate_limits[i].queue_io = bdev_qos_r_queue_io;
2122 			qos->rate_limits[i].update_quota = bdev_qos_r_bps_update_quota;
2123 			break;
2124 		case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT:
2125 			qos->rate_limits[i].queue_io = bdev_qos_w_queue_io;
2126 			qos->rate_limits[i].update_quota = bdev_qos_w_bps_update_quota;
2127 			break;
2128 		default:
2129 			break;
2130 		}
2131 	}
2132 }
2133 
2134 static void
2135 _bdev_io_complete_in_submit(struct spdk_bdev_channel *bdev_ch,
2136 			    struct spdk_bdev_io *bdev_io,
2137 			    enum spdk_bdev_io_status status)
2138 {
2139 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
2140 
2141 	bdev_io->internal.in_submit_request = true;
2142 	bdev_ch->io_outstanding++;
2143 	shared_resource->io_outstanding++;
2144 	spdk_bdev_io_complete(bdev_io, status);
2145 	bdev_io->internal.in_submit_request = false;
2146 }
2147 
2148 static inline void
2149 bdev_io_do_submit(struct spdk_bdev_channel *bdev_ch, struct spdk_bdev_io *bdev_io)
2150 {
2151 	struct spdk_bdev *bdev = bdev_io->bdev;
2152 	struct spdk_io_channel *ch = bdev_ch->channel;
2153 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
2154 
2155 	if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) {
2156 		struct spdk_bdev_mgmt_channel *mgmt_channel = shared_resource->mgmt_ch;
2157 		struct spdk_bdev_io *bio_to_abort = bdev_io->u.abort.bio_to_abort;
2158 
2159 		if (bdev_abort_queued_io(&shared_resource->nomem_io, bio_to_abort) ||
2160 		    bdev_abort_buf_io(&mgmt_channel->need_buf_small, bio_to_abort) ||
2161 		    bdev_abort_buf_io(&mgmt_channel->need_buf_large, bio_to_abort)) {
2162 			_bdev_io_complete_in_submit(bdev_ch, bdev_io,
2163 						    SPDK_BDEV_IO_STATUS_SUCCESS);
2164 			return;
2165 		}
2166 	}
2167 
2168 	if (spdk_likely(TAILQ_EMPTY(&shared_resource->nomem_io))) {
2169 		bdev_ch->io_outstanding++;
2170 		shared_resource->io_outstanding++;
2171 		bdev_io->internal.in_submit_request = true;
2172 		bdev->fn_table->submit_request(ch, bdev_io);
2173 		bdev_io->internal.in_submit_request = false;
2174 	} else {
2175 		TAILQ_INSERT_TAIL(&shared_resource->nomem_io, bdev_io, internal.link);
2176 	}
2177 }
2178 
2179 static bool
2180 bdev_qos_queue_io(struct spdk_bdev_qos *qos, struct spdk_bdev_io *bdev_io)
2181 {
2182 	int i;
2183 
2184 	if (bdev_qos_io_to_limit(bdev_io) == true) {
2185 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2186 			if (!qos->rate_limits[i].queue_io) {
2187 				continue;
2188 			}
2189 
2190 			if (qos->rate_limits[i].queue_io(&qos->rate_limits[i],
2191 							 bdev_io) == true) {
2192 				return true;
2193 			}
2194 		}
2195 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2196 			if (!qos->rate_limits[i].update_quota) {
2197 				continue;
2198 			}
2199 
2200 			qos->rate_limits[i].update_quota(&qos->rate_limits[i], bdev_io);
2201 		}
2202 	}
2203 
2204 	return false;
2205 }
2206 
2207 static int
2208 bdev_qos_io_submit(struct spdk_bdev_channel *ch, struct spdk_bdev_qos *qos)
2209 {
2210 	struct spdk_bdev_io		*bdev_io = NULL, *tmp = NULL;
2211 	int				submitted_ios = 0;
2212 
2213 	TAILQ_FOREACH_SAFE(bdev_io, &qos->queued, internal.link, tmp) {
2214 		if (!bdev_qos_queue_io(qos, bdev_io)) {
2215 			TAILQ_REMOVE(&qos->queued, bdev_io, internal.link);
2216 			bdev_io_do_submit(ch, bdev_io);
2217 			submitted_ios++;
2218 		}
2219 	}
2220 
2221 	return submitted_ios;
2222 }
2223 
2224 static void
2225 bdev_queue_io_wait_with_cb(struct spdk_bdev_io *bdev_io, spdk_bdev_io_wait_cb cb_fn)
2226 {
2227 	int rc;
2228 
2229 	bdev_io->internal.waitq_entry.bdev = bdev_io->bdev;
2230 	bdev_io->internal.waitq_entry.cb_fn = cb_fn;
2231 	bdev_io->internal.waitq_entry.cb_arg = bdev_io;
2232 	rc = spdk_bdev_queue_io_wait(bdev_io->bdev, spdk_io_channel_from_ctx(bdev_io->internal.ch),
2233 				     &bdev_io->internal.waitq_entry);
2234 	if (rc != 0) {
2235 		SPDK_ERRLOG("Queue IO failed, rc=%d\n", rc);
2236 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
2237 		bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
2238 	}
2239 }
2240 
2241 static bool
2242 bdev_rw_should_split(struct spdk_bdev_io *bdev_io)
2243 {
2244 	uint32_t io_boundary = bdev_io->bdev->optimal_io_boundary;
2245 	uint32_t max_size = bdev_io->bdev->max_segment_size;
2246 	int max_segs = bdev_io->bdev->max_num_segments;
2247 
2248 	io_boundary = bdev_io->bdev->split_on_optimal_io_boundary ? io_boundary : 0;
2249 
2250 	if (spdk_likely(!io_boundary && !max_segs && !max_size)) {
2251 		return false;
2252 	}
2253 
2254 	if (io_boundary) {
2255 		uint64_t start_stripe, end_stripe;
2256 
2257 		start_stripe = bdev_io->u.bdev.offset_blocks;
2258 		end_stripe = start_stripe + bdev_io->u.bdev.num_blocks - 1;
2259 		/* Avoid expensive div operations if possible.  These spdk_u32 functions are very cheap. */
2260 		if (spdk_likely(spdk_u32_is_pow2(io_boundary))) {
2261 			start_stripe >>= spdk_u32log2(io_boundary);
2262 			end_stripe >>= spdk_u32log2(io_boundary);
2263 		} else {
2264 			start_stripe /= io_boundary;
2265 			end_stripe /= io_boundary;
2266 		}
2267 
2268 		if (start_stripe != end_stripe) {
2269 			return true;
2270 		}
2271 	}
2272 
2273 	if (max_segs) {
2274 		if (bdev_io->u.bdev.iovcnt > max_segs) {
2275 			return true;
2276 		}
2277 	}
2278 
2279 	if (max_size) {
2280 		for (int i = 0; i < bdev_io->u.bdev.iovcnt; i++) {
2281 			if (bdev_io->u.bdev.iovs[i].iov_len > max_size) {
2282 				return true;
2283 			}
2284 		}
2285 	}
2286 
2287 	return false;
2288 }
2289 
2290 static bool
2291 bdev_unmap_should_split(struct spdk_bdev_io *bdev_io)
2292 {
2293 	uint32_t num_unmap_segments;
2294 
2295 	if (!bdev_io->bdev->max_unmap || !bdev_io->bdev->max_unmap_segments) {
2296 		return false;
2297 	}
2298 	num_unmap_segments = spdk_divide_round_up(bdev_io->u.bdev.num_blocks, bdev_io->bdev->max_unmap);
2299 	if (num_unmap_segments > bdev_io->bdev->max_unmap_segments) {
2300 		return true;
2301 	}
2302 
2303 	return false;
2304 }
2305 
2306 static bool
2307 bdev_write_zeroes_should_split(struct spdk_bdev_io *bdev_io)
2308 {
2309 	if (!bdev_io->bdev->max_write_zeroes) {
2310 		return false;
2311 	}
2312 
2313 	if (bdev_io->u.bdev.num_blocks > bdev_io->bdev->max_write_zeroes) {
2314 		return true;
2315 	}
2316 
2317 	return false;
2318 }
2319 
2320 static bool
2321 bdev_io_should_split(struct spdk_bdev_io *bdev_io)
2322 {
2323 	switch (bdev_io->type) {
2324 	case SPDK_BDEV_IO_TYPE_READ:
2325 	case SPDK_BDEV_IO_TYPE_WRITE:
2326 		return bdev_rw_should_split(bdev_io);
2327 	case SPDK_BDEV_IO_TYPE_UNMAP:
2328 		return bdev_unmap_should_split(bdev_io);
2329 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
2330 		return bdev_write_zeroes_should_split(bdev_io);
2331 	default:
2332 		return false;
2333 	}
2334 }
2335 
2336 static uint32_t
2337 _to_next_boundary(uint64_t offset, uint32_t boundary)
2338 {
2339 	return (boundary - (offset % boundary));
2340 }
2341 
2342 static void bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
2343 
2344 static void _bdev_rw_split(void *_bdev_io);
2345 
2346 static void bdev_unmap_split(struct spdk_bdev_io *bdev_io);
2347 
2348 static void
2349 _bdev_unmap_split(void *_bdev_io)
2350 {
2351 	return bdev_unmap_split((struct spdk_bdev_io *)_bdev_io);
2352 }
2353 
2354 static void bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io);
2355 
2356 static void
2357 _bdev_write_zeroes_split(void *_bdev_io)
2358 {
2359 	return bdev_write_zeroes_split((struct spdk_bdev_io *)_bdev_io);
2360 }
2361 
2362 static int
2363 bdev_io_split_submit(struct spdk_bdev_io *bdev_io, struct iovec *iov, int iovcnt, void *md_buf,
2364 		     uint64_t num_blocks, uint64_t *offset, uint64_t *remaining)
2365 {
2366 	int rc;
2367 	uint64_t current_offset, current_remaining;
2368 	spdk_bdev_io_wait_cb io_wait_fn;
2369 
2370 	current_offset = *offset;
2371 	current_remaining = *remaining;
2372 
2373 	bdev_io->u.bdev.split_outstanding++;
2374 
2375 	io_wait_fn = _bdev_rw_split;
2376 	switch (bdev_io->type) {
2377 	case SPDK_BDEV_IO_TYPE_READ:
2378 		rc = bdev_readv_blocks_with_md(bdev_io->internal.desc,
2379 					       spdk_io_channel_from_ctx(bdev_io->internal.ch),
2380 					       iov, iovcnt, md_buf, current_offset,
2381 					       num_blocks,
2382 					       bdev_io_split_done, bdev_io,
2383 					       bdev_io->internal.ext_opts, true);
2384 		break;
2385 	case SPDK_BDEV_IO_TYPE_WRITE:
2386 		rc = bdev_writev_blocks_with_md(bdev_io->internal.desc,
2387 						spdk_io_channel_from_ctx(bdev_io->internal.ch),
2388 						iov, iovcnt, md_buf, current_offset,
2389 						num_blocks,
2390 						bdev_io_split_done, bdev_io,
2391 						bdev_io->internal.ext_opts, true);
2392 		break;
2393 	case SPDK_BDEV_IO_TYPE_UNMAP:
2394 		io_wait_fn = _bdev_unmap_split;
2395 		rc = spdk_bdev_unmap_blocks(bdev_io->internal.desc,
2396 					    spdk_io_channel_from_ctx(bdev_io->internal.ch),
2397 					    current_offset, num_blocks,
2398 					    bdev_io_split_done, bdev_io);
2399 		break;
2400 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
2401 		io_wait_fn = _bdev_write_zeroes_split;
2402 		rc = spdk_bdev_write_zeroes_blocks(bdev_io->internal.desc,
2403 						   spdk_io_channel_from_ctx(bdev_io->internal.ch),
2404 						   current_offset, num_blocks,
2405 						   bdev_io_split_done, bdev_io);
2406 		break;
2407 	default:
2408 		assert(false);
2409 		rc = -EINVAL;
2410 		break;
2411 	}
2412 
2413 	if (rc == 0) {
2414 		current_offset += num_blocks;
2415 		current_remaining -= num_blocks;
2416 		bdev_io->u.bdev.split_current_offset_blocks = current_offset;
2417 		bdev_io->u.bdev.split_remaining_num_blocks = current_remaining;
2418 		*offset = current_offset;
2419 		*remaining = current_remaining;
2420 	} else {
2421 		bdev_io->u.bdev.split_outstanding--;
2422 		if (rc == -ENOMEM) {
2423 			if (bdev_io->u.bdev.split_outstanding == 0) {
2424 				/* No I/O is outstanding. Hence we should wait here. */
2425 				bdev_queue_io_wait_with_cb(bdev_io, io_wait_fn);
2426 			}
2427 		} else {
2428 			bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
2429 			if (bdev_io->u.bdev.split_outstanding == 0) {
2430 				spdk_trace_record(TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, bdev_io->internal.caller_ctx);
2431 				TAILQ_REMOVE(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link);
2432 				bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
2433 			}
2434 		}
2435 	}
2436 
2437 	return rc;
2438 }
2439 
2440 static void
2441 _bdev_rw_split(void *_bdev_io)
2442 {
2443 	struct iovec *parent_iov, *iov;
2444 	struct spdk_bdev_io *bdev_io = _bdev_io;
2445 	struct spdk_bdev *bdev = bdev_io->bdev;
2446 	uint64_t parent_offset, current_offset, remaining;
2447 	uint32_t parent_iov_offset, parent_iovcnt, parent_iovpos, child_iovcnt;
2448 	uint32_t to_next_boundary, to_next_boundary_bytes, to_last_block_bytes;
2449 	uint32_t iovcnt, iov_len, child_iovsize;
2450 	uint32_t blocklen = bdev->blocklen;
2451 	uint32_t io_boundary = bdev->optimal_io_boundary;
2452 	uint32_t max_segment_size = bdev->max_segment_size;
2453 	uint32_t max_child_iovcnt = bdev->max_num_segments;
2454 	void *md_buf = NULL;
2455 	int rc;
2456 
2457 	max_segment_size = max_segment_size ? max_segment_size : UINT32_MAX;
2458 	max_child_iovcnt = max_child_iovcnt ? spdk_min(max_child_iovcnt, BDEV_IO_NUM_CHILD_IOV) :
2459 			   BDEV_IO_NUM_CHILD_IOV;
2460 	io_boundary = bdev->split_on_optimal_io_boundary ? io_boundary : UINT32_MAX;
2461 
2462 	remaining = bdev_io->u.bdev.split_remaining_num_blocks;
2463 	current_offset = bdev_io->u.bdev.split_current_offset_blocks;
2464 	parent_offset = bdev_io->u.bdev.offset_blocks;
2465 	parent_iov_offset = (current_offset - parent_offset) * blocklen;
2466 	parent_iovcnt = bdev_io->u.bdev.iovcnt;
2467 
2468 	for (parent_iovpos = 0; parent_iovpos < parent_iovcnt; parent_iovpos++) {
2469 		parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos];
2470 		if (parent_iov_offset < parent_iov->iov_len) {
2471 			break;
2472 		}
2473 		parent_iov_offset -= parent_iov->iov_len;
2474 	}
2475 
2476 	child_iovcnt = 0;
2477 	while (remaining > 0 && parent_iovpos < parent_iovcnt && child_iovcnt < BDEV_IO_NUM_CHILD_IOV) {
2478 		to_next_boundary = _to_next_boundary(current_offset, io_boundary);
2479 		to_next_boundary = spdk_min(remaining, to_next_boundary);
2480 		to_next_boundary_bytes = to_next_boundary * blocklen;
2481 
2482 		iov = &bdev_io->child_iov[child_iovcnt];
2483 		iovcnt = 0;
2484 
2485 		if (bdev_io->u.bdev.md_buf) {
2486 			md_buf = (char *)bdev_io->u.bdev.md_buf +
2487 				 (current_offset - parent_offset) * spdk_bdev_get_md_size(bdev);
2488 		}
2489 
2490 		child_iovsize = spdk_min(BDEV_IO_NUM_CHILD_IOV - child_iovcnt, max_child_iovcnt);
2491 		while (to_next_boundary_bytes > 0 && parent_iovpos < parent_iovcnt &&
2492 		       iovcnt < child_iovsize) {
2493 			parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos];
2494 			iov_len = parent_iov->iov_len - parent_iov_offset;
2495 
2496 			iov_len = spdk_min(iov_len, max_segment_size);
2497 			iov_len = spdk_min(iov_len, to_next_boundary_bytes);
2498 			to_next_boundary_bytes -= iov_len;
2499 
2500 			bdev_io->child_iov[child_iovcnt].iov_base = parent_iov->iov_base + parent_iov_offset;
2501 			bdev_io->child_iov[child_iovcnt].iov_len = iov_len;
2502 
2503 			if (iov_len < parent_iov->iov_len - parent_iov_offset) {
2504 				parent_iov_offset += iov_len;
2505 			} else {
2506 				parent_iovpos++;
2507 				parent_iov_offset = 0;
2508 			}
2509 			child_iovcnt++;
2510 			iovcnt++;
2511 		}
2512 
2513 		if (to_next_boundary_bytes > 0) {
2514 			/* We had to stop this child I/O early because we ran out of
2515 			 * child_iov space or were limited by max_num_segments.
2516 			 * Ensure the iovs to be aligned with block size and
2517 			 * then adjust to_next_boundary before starting the
2518 			 * child I/O.
2519 			 */
2520 			assert(child_iovcnt == BDEV_IO_NUM_CHILD_IOV ||
2521 			       iovcnt == child_iovsize);
2522 			to_last_block_bytes = to_next_boundary_bytes % blocklen;
2523 			if (to_last_block_bytes != 0) {
2524 				uint32_t child_iovpos = child_iovcnt - 1;
2525 				/* don't decrease child_iovcnt when it equals to BDEV_IO_NUM_CHILD_IOV
2526 				 * so the loop will naturally end
2527 				 */
2528 
2529 				to_last_block_bytes = blocklen - to_last_block_bytes;
2530 				to_next_boundary_bytes += to_last_block_bytes;
2531 				while (to_last_block_bytes > 0 && iovcnt > 0) {
2532 					iov_len = spdk_min(to_last_block_bytes,
2533 							   bdev_io->child_iov[child_iovpos].iov_len);
2534 					bdev_io->child_iov[child_iovpos].iov_len -= iov_len;
2535 					if (bdev_io->child_iov[child_iovpos].iov_len == 0) {
2536 						child_iovpos--;
2537 						if (--iovcnt == 0) {
2538 							/* If the child IO is less than a block size just return.
2539 							 * If the first child IO of any split round is less than
2540 							 * a block size, an error exit.
2541 							 */
2542 							if (bdev_io->u.bdev.split_outstanding == 0) {
2543 								SPDK_ERRLOG("The first child io was less than a block size\n");
2544 								bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
2545 								spdk_trace_record(TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io, bdev_io->internal.caller_ctx);
2546 								TAILQ_REMOVE(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link);
2547 								bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
2548 							}
2549 
2550 							return;
2551 						}
2552 					}
2553 
2554 					to_last_block_bytes -= iov_len;
2555 
2556 					if (parent_iov_offset == 0) {
2557 						parent_iovpos--;
2558 						parent_iov_offset = bdev_io->u.bdev.iovs[parent_iovpos].iov_len;
2559 					}
2560 					parent_iov_offset -= iov_len;
2561 				}
2562 
2563 				assert(to_last_block_bytes == 0);
2564 			}
2565 			to_next_boundary -= to_next_boundary_bytes / blocklen;
2566 		}
2567 
2568 		rc = bdev_io_split_submit(bdev_io, iov, iovcnt, md_buf, to_next_boundary,
2569 					  &current_offset, &remaining);
2570 		if (spdk_unlikely(rc)) {
2571 			return;
2572 		}
2573 	}
2574 }
2575 
2576 static void
2577 bdev_unmap_split(struct spdk_bdev_io *bdev_io)
2578 {
2579 	uint64_t offset, unmap_blocks, remaining, max_unmap_blocks;
2580 	uint32_t num_children_reqs = 0;
2581 	int rc;
2582 
2583 	offset = bdev_io->u.bdev.split_current_offset_blocks;
2584 	remaining = bdev_io->u.bdev.split_remaining_num_blocks;
2585 	max_unmap_blocks = bdev_io->bdev->max_unmap * bdev_io->bdev->max_unmap_segments;
2586 
2587 	while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS)) {
2588 		unmap_blocks = spdk_min(remaining, max_unmap_blocks);
2589 
2590 		rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, unmap_blocks,
2591 					  &offset, &remaining);
2592 		if (spdk_likely(rc == 0)) {
2593 			num_children_reqs++;
2594 		} else {
2595 			return;
2596 		}
2597 	}
2598 }
2599 
2600 static void
2601 bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io)
2602 {
2603 	uint64_t offset, write_zeroes_blocks, remaining;
2604 	uint32_t num_children_reqs = 0;
2605 	int rc;
2606 
2607 	offset = bdev_io->u.bdev.split_current_offset_blocks;
2608 	remaining = bdev_io->u.bdev.split_remaining_num_blocks;
2609 
2610 	while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS)) {
2611 		write_zeroes_blocks = spdk_min(remaining, bdev_io->bdev->max_write_zeroes);
2612 
2613 		rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, write_zeroes_blocks,
2614 					  &offset, &remaining);
2615 		if (spdk_likely(rc == 0)) {
2616 			num_children_reqs++;
2617 		} else {
2618 			return;
2619 		}
2620 	}
2621 }
2622 
2623 static void
2624 parent_bdev_io_complete(void *ctx, int rc)
2625 {
2626 	struct spdk_bdev_io *parent_io = ctx;
2627 
2628 	if (rc) {
2629 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
2630 	}
2631 
2632 	parent_io->internal.cb(parent_io, parent_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
2633 			       parent_io->internal.caller_ctx);
2634 }
2635 
2636 static void
2637 bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
2638 {
2639 	struct spdk_bdev_io *parent_io = cb_arg;
2640 
2641 	spdk_bdev_free_io(bdev_io);
2642 
2643 	if (!success) {
2644 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
2645 		/* If any child I/O failed, stop further splitting process. */
2646 		parent_io->u.bdev.split_current_offset_blocks += parent_io->u.bdev.split_remaining_num_blocks;
2647 		parent_io->u.bdev.split_remaining_num_blocks = 0;
2648 	}
2649 	parent_io->u.bdev.split_outstanding--;
2650 	if (parent_io->u.bdev.split_outstanding != 0) {
2651 		return;
2652 	}
2653 
2654 	/*
2655 	 * Parent I/O finishes when all blocks are consumed.
2656 	 */
2657 	if (parent_io->u.bdev.split_remaining_num_blocks == 0) {
2658 		assert(parent_io->internal.cb != bdev_io_split_done);
2659 		spdk_trace_record(TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)parent_io, bdev_io->internal.caller_ctx);
2660 		TAILQ_REMOVE(&parent_io->internal.ch->io_submitted, parent_io, internal.ch_link);
2661 
2662 		if (parent_io->internal.orig_iovcnt != 0) {
2663 			_bdev_io_push_bounce_data_buffer(parent_io, parent_bdev_io_complete);
2664 			/* bdev IO will be completed in the callback */
2665 		} else {
2666 			parent_bdev_io_complete(parent_io, 0);
2667 		}
2668 		return;
2669 	}
2670 
2671 	/*
2672 	 * Continue with the splitting process.  This function will complete the parent I/O if the
2673 	 * splitting is done.
2674 	 */
2675 	switch (parent_io->type) {
2676 	case SPDK_BDEV_IO_TYPE_READ:
2677 	case SPDK_BDEV_IO_TYPE_WRITE:
2678 		_bdev_rw_split(parent_io);
2679 		break;
2680 	case SPDK_BDEV_IO_TYPE_UNMAP:
2681 		bdev_unmap_split(parent_io);
2682 		break;
2683 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
2684 		bdev_write_zeroes_split(parent_io);
2685 		break;
2686 	default:
2687 		assert(false);
2688 		break;
2689 	}
2690 }
2691 
2692 static void bdev_rw_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
2693 				     bool success);
2694 
2695 static void
2696 bdev_io_split(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
2697 {
2698 	bdev_io->u.bdev.split_current_offset_blocks = bdev_io->u.bdev.offset_blocks;
2699 	bdev_io->u.bdev.split_remaining_num_blocks = bdev_io->u.bdev.num_blocks;
2700 	bdev_io->u.bdev.split_outstanding = 0;
2701 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
2702 
2703 	switch (bdev_io->type) {
2704 	case SPDK_BDEV_IO_TYPE_READ:
2705 	case SPDK_BDEV_IO_TYPE_WRITE:
2706 		if (_is_buf_allocated(bdev_io->u.bdev.iovs)) {
2707 			_bdev_rw_split(bdev_io);
2708 		} else {
2709 			assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
2710 			spdk_bdev_io_get_buf(bdev_io, bdev_rw_split_get_buf_cb,
2711 					     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
2712 		}
2713 		break;
2714 	case SPDK_BDEV_IO_TYPE_UNMAP:
2715 		bdev_unmap_split(bdev_io);
2716 		break;
2717 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
2718 		bdev_write_zeroes_split(bdev_io);
2719 		break;
2720 	default:
2721 		assert(false);
2722 		break;
2723 	}
2724 }
2725 
2726 static void
2727 bdev_rw_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success)
2728 {
2729 	if (!success) {
2730 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
2731 		return;
2732 	}
2733 
2734 	_bdev_rw_split(bdev_io);
2735 }
2736 
2737 /* Explicitly mark this inline, since it's used as a function pointer and otherwise won't
2738  *  be inlined, at least on some compilers.
2739  */
2740 static inline void
2741 _bdev_io_submit(void *ctx)
2742 {
2743 	struct spdk_bdev_io *bdev_io = ctx;
2744 	struct spdk_bdev *bdev = bdev_io->bdev;
2745 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
2746 	uint64_t tsc;
2747 
2748 	tsc = spdk_get_ticks();
2749 	bdev_io->internal.submit_tsc = tsc;
2750 	spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_START, 0, 0, (uintptr_t)bdev_io,
2751 			      (uint64_t)bdev_io->type, bdev_io->internal.caller_ctx,
2752 			      bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks);
2753 
2754 	if (spdk_likely(bdev_ch->flags == 0)) {
2755 		bdev_io_do_submit(bdev_ch, bdev_io);
2756 		return;
2757 	}
2758 
2759 	if (bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS) {
2760 		_bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
2761 	} else if (bdev_ch->flags & BDEV_CH_QOS_ENABLED) {
2762 		if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT) &&
2763 		    bdev_abort_queued_io(&bdev->internal.qos->queued, bdev_io->u.abort.bio_to_abort)) {
2764 			_bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
2765 		} else {
2766 			TAILQ_INSERT_TAIL(&bdev->internal.qos->queued, bdev_io, internal.link);
2767 			bdev_qos_io_submit(bdev_ch, bdev->internal.qos);
2768 		}
2769 	} else {
2770 		SPDK_ERRLOG("unknown bdev_ch flag %x found\n", bdev_ch->flags);
2771 		_bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
2772 	}
2773 }
2774 
2775 bool bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2);
2776 
2777 bool
2778 bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2)
2779 {
2780 	if (range1->length == 0 || range2->length == 0) {
2781 		return false;
2782 	}
2783 
2784 	if (range1->offset + range1->length <= range2->offset) {
2785 		return false;
2786 	}
2787 
2788 	if (range2->offset + range2->length <= range1->offset) {
2789 		return false;
2790 	}
2791 
2792 	return true;
2793 }
2794 
2795 static bool
2796 bdev_io_range_is_locked(struct spdk_bdev_io *bdev_io, struct lba_range *range)
2797 {
2798 	struct spdk_bdev_channel *ch = bdev_io->internal.ch;
2799 	struct lba_range r;
2800 
2801 	switch (bdev_io->type) {
2802 	case SPDK_BDEV_IO_TYPE_NVME_IO:
2803 	case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
2804 		/* Don't try to decode the NVMe command - just assume worst-case and that
2805 		 * it overlaps a locked range.
2806 		 */
2807 		return true;
2808 	case SPDK_BDEV_IO_TYPE_WRITE:
2809 	case SPDK_BDEV_IO_TYPE_UNMAP:
2810 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
2811 	case SPDK_BDEV_IO_TYPE_ZCOPY:
2812 		r.offset = bdev_io->u.bdev.offset_blocks;
2813 		r.length = bdev_io->u.bdev.num_blocks;
2814 		if (!bdev_lba_range_overlapped(range, &r)) {
2815 			/* This I/O doesn't overlap the specified LBA range. */
2816 			return false;
2817 		} else if (range->owner_ch == ch && range->locked_ctx == bdev_io->internal.caller_ctx) {
2818 			/* This I/O overlaps, but the I/O is on the same channel that locked this
2819 			 * range, and the caller_ctx is the same as the locked_ctx.  This means
2820 			 * that this I/O is associated with the lock, and is allowed to execute.
2821 			 */
2822 			return false;
2823 		} else {
2824 			return true;
2825 		}
2826 	default:
2827 		return false;
2828 	}
2829 }
2830 
2831 void
2832 bdev_io_submit(struct spdk_bdev_io *bdev_io)
2833 {
2834 	struct spdk_bdev *bdev = bdev_io->bdev;
2835 	struct spdk_thread *thread = spdk_bdev_io_get_thread(bdev_io);
2836 	struct spdk_bdev_channel *ch = bdev_io->internal.ch;
2837 
2838 	assert(thread != NULL);
2839 	assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING);
2840 
2841 	if (!TAILQ_EMPTY(&ch->locked_ranges)) {
2842 		struct lba_range *range;
2843 
2844 		TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
2845 			if (bdev_io_range_is_locked(bdev_io, range)) {
2846 				TAILQ_INSERT_TAIL(&ch->io_locked, bdev_io, internal.ch_link);
2847 				return;
2848 			}
2849 		}
2850 	}
2851 
2852 	TAILQ_INSERT_TAIL(&ch->io_submitted, bdev_io, internal.ch_link);
2853 
2854 	if (bdev_io_should_split(bdev_io)) {
2855 		bdev_io->internal.submit_tsc = spdk_get_ticks();
2856 		spdk_trace_record_tsc(bdev_io->internal.submit_tsc, TRACE_BDEV_IO_START, 0, 0,
2857 				      (uintptr_t)bdev_io, (uint64_t)bdev_io->type, bdev_io->internal.caller_ctx,
2858 				      bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks);
2859 		bdev_io_split(NULL, bdev_io);
2860 		return;
2861 	}
2862 
2863 	if (ch->flags & BDEV_CH_QOS_ENABLED) {
2864 		if ((thread == bdev->internal.qos->thread) || !bdev->internal.qos->thread) {
2865 			_bdev_io_submit(bdev_io);
2866 		} else {
2867 			bdev_io->internal.io_submit_ch = ch;
2868 			bdev_io->internal.ch = bdev->internal.qos->ch;
2869 			spdk_thread_send_msg(bdev->internal.qos->thread, _bdev_io_submit, bdev_io);
2870 		}
2871 	} else {
2872 		_bdev_io_submit(bdev_io);
2873 	}
2874 }
2875 
2876 static inline void
2877 _bdev_io_copy_ext_opts(struct spdk_bdev_io *bdev_io, struct spdk_bdev_ext_io_opts *opts)
2878 {
2879 	struct spdk_bdev_ext_io_opts *opts_copy = &bdev_io->internal.ext_opts_copy;
2880 
2881 	/* Zero part we don't copy */
2882 	memset(((char *)opts_copy) + opts->size, 0, sizeof(*opts) - opts->size);
2883 	memcpy(opts_copy, opts, opts->size);
2884 	opts_copy->size = sizeof(*opts_copy);
2885 	opts_copy->metadata = bdev_io->u.bdev.md_buf;
2886 	/* Save pointer to the copied ext_opts which will be used by bdev modules */
2887 	bdev_io->u.bdev.ext_opts = opts_copy;
2888 }
2889 
2890 static inline void
2891 _bdev_io_ext_use_bounce_buffer(struct spdk_bdev_io *bdev_io)
2892 {
2893 	/* bdev doesn't support memory domains, thereby buffers in this IO request can't
2894 	 * be accessed directly. It is needed to allocate buffers before issuing IO operation.
2895 	 * For write operation we need to pull buffers from memory domain before submitting IO.
2896 	 * Once read operation completes, we need to use memory_domain push functionality to
2897 	 * update data in original memory domain IO buffer
2898 	 * This IO request will go through a regular IO flow, so clear memory domains pointers in
2899 	 * the copied ext_opts */
2900 	bdev_io->internal.ext_opts_copy.memory_domain = NULL;
2901 	bdev_io->internal.ext_opts_copy.memory_domain_ctx = NULL;
2902 	_bdev_memory_domain_io_get_buf(bdev_io, _bdev_memory_domain_get_io_cb,
2903 				       bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
2904 }
2905 
2906 static inline void
2907 _bdev_io_submit_ext(struct spdk_bdev_desc *desc, struct spdk_bdev_io *bdev_io,
2908 		    struct spdk_bdev_ext_io_opts *opts, bool copy_opts)
2909 {
2910 	if (opts) {
2911 		bool use_pull_push = opts->memory_domain && !desc->memory_domains_supported;
2912 		assert(opts->size <= sizeof(*opts));
2913 		/*
2914 		 * copy if size is smaller than opts struct to avoid having to check size
2915 		 * on every access to bdev_io->u.bdev.ext_opts
2916 		 */
2917 		if (copy_opts || use_pull_push || opts->size < sizeof(*opts)) {
2918 			_bdev_io_copy_ext_opts(bdev_io, opts);
2919 			if (use_pull_push) {
2920 				_bdev_io_ext_use_bounce_buffer(bdev_io);
2921 				return;
2922 			}
2923 		}
2924 	}
2925 	bdev_io_submit(bdev_io);
2926 }
2927 
2928 static void
2929 bdev_io_submit_reset(struct spdk_bdev_io *bdev_io)
2930 {
2931 	struct spdk_bdev *bdev = bdev_io->bdev;
2932 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
2933 	struct spdk_io_channel *ch = bdev_ch->channel;
2934 
2935 	assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING);
2936 
2937 	bdev_io->internal.in_submit_request = true;
2938 	bdev->fn_table->submit_request(ch, bdev_io);
2939 	bdev_io->internal.in_submit_request = false;
2940 }
2941 
2942 void
2943 bdev_io_init(struct spdk_bdev_io *bdev_io,
2944 	     struct spdk_bdev *bdev, void *cb_arg,
2945 	     spdk_bdev_io_completion_cb cb)
2946 {
2947 	bdev_io->bdev = bdev;
2948 	bdev_io->internal.caller_ctx = cb_arg;
2949 	bdev_io->internal.cb = cb;
2950 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
2951 	bdev_io->internal.in_submit_request = false;
2952 	bdev_io->internal.buf = NULL;
2953 	bdev_io->internal.io_submit_ch = NULL;
2954 	bdev_io->internal.orig_iovs = NULL;
2955 	bdev_io->internal.orig_iovcnt = 0;
2956 	bdev_io->internal.orig_md_iov.iov_base = NULL;
2957 	bdev_io->internal.error.nvme.cdw0 = 0;
2958 	bdev_io->num_retries = 0;
2959 	bdev_io->internal.get_buf_cb = NULL;
2960 	bdev_io->internal.get_aux_buf_cb = NULL;
2961 	bdev_io->internal.ext_opts = NULL;
2962 	bdev_io->internal.data_transfer_cpl = NULL;
2963 }
2964 
2965 static bool
2966 bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
2967 {
2968 	return bdev->fn_table->io_type_supported(bdev->ctxt, io_type);
2969 }
2970 
2971 bool
2972 spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
2973 {
2974 	bool supported;
2975 
2976 	supported = bdev_io_type_supported(bdev, io_type);
2977 
2978 	if (!supported) {
2979 		switch (io_type) {
2980 		case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
2981 			/* The bdev layer will emulate write zeroes as long as write is supported. */
2982 			supported = bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE);
2983 			break;
2984 		default:
2985 			break;
2986 		}
2987 	}
2988 
2989 	return supported;
2990 }
2991 
2992 int
2993 spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
2994 {
2995 	if (bdev->fn_table->dump_info_json) {
2996 		return bdev->fn_table->dump_info_json(bdev->ctxt, w);
2997 	}
2998 
2999 	return 0;
3000 }
3001 
3002 static void
3003 bdev_qos_update_max_quota_per_timeslice(struct spdk_bdev_qos *qos)
3004 {
3005 	uint32_t max_per_timeslice = 0;
3006 	int i;
3007 
3008 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3009 		if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
3010 			qos->rate_limits[i].max_per_timeslice = 0;
3011 			continue;
3012 		}
3013 
3014 		max_per_timeslice = qos->rate_limits[i].limit *
3015 				    SPDK_BDEV_QOS_TIMESLICE_IN_USEC / SPDK_SEC_TO_USEC;
3016 
3017 		qos->rate_limits[i].max_per_timeslice = spdk_max(max_per_timeslice,
3018 							qos->rate_limits[i].min_per_timeslice);
3019 
3020 		qos->rate_limits[i].remaining_this_timeslice = qos->rate_limits[i].max_per_timeslice;
3021 	}
3022 
3023 	bdev_qos_set_ops(qos);
3024 }
3025 
3026 static int
3027 bdev_channel_poll_qos(void *arg)
3028 {
3029 	struct spdk_bdev_qos *qos = arg;
3030 	uint64_t now = spdk_get_ticks();
3031 	int i;
3032 
3033 	if (now < (qos->last_timeslice + qos->timeslice_size)) {
3034 		/* We received our callback earlier than expected - return
3035 		 *  immediately and wait to do accounting until at least one
3036 		 *  timeslice has actually expired.  This should never happen
3037 		 *  with a well-behaved timer implementation.
3038 		 */
3039 		return SPDK_POLLER_IDLE;
3040 	}
3041 
3042 	/* Reset for next round of rate limiting */
3043 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3044 		/* We may have allowed the IOs or bytes to slightly overrun in the last
3045 		 * timeslice. remaining_this_timeslice is signed, so if it's negative
3046 		 * here, we'll account for the overrun so that the next timeslice will
3047 		 * be appropriately reduced.
3048 		 */
3049 		if (qos->rate_limits[i].remaining_this_timeslice > 0) {
3050 			qos->rate_limits[i].remaining_this_timeslice = 0;
3051 		}
3052 	}
3053 
3054 	while (now >= (qos->last_timeslice + qos->timeslice_size)) {
3055 		qos->last_timeslice += qos->timeslice_size;
3056 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3057 			qos->rate_limits[i].remaining_this_timeslice +=
3058 				qos->rate_limits[i].max_per_timeslice;
3059 		}
3060 	}
3061 
3062 	return bdev_qos_io_submit(qos->ch, qos);
3063 }
3064 
3065 static void
3066 bdev_channel_destroy_resource(struct spdk_bdev_channel *ch)
3067 {
3068 	struct spdk_bdev_shared_resource *shared_resource;
3069 	struct lba_range *range;
3070 
3071 	while (!TAILQ_EMPTY(&ch->locked_ranges)) {
3072 		range = TAILQ_FIRST(&ch->locked_ranges);
3073 		TAILQ_REMOVE(&ch->locked_ranges, range, tailq);
3074 		free(range);
3075 	}
3076 
3077 	spdk_put_io_channel(ch->channel);
3078 
3079 	shared_resource = ch->shared_resource;
3080 
3081 	assert(TAILQ_EMPTY(&ch->io_locked));
3082 	assert(TAILQ_EMPTY(&ch->io_submitted));
3083 	assert(ch->io_outstanding == 0);
3084 	assert(shared_resource->ref > 0);
3085 	shared_resource->ref--;
3086 	if (shared_resource->ref == 0) {
3087 		assert(shared_resource->io_outstanding == 0);
3088 		TAILQ_REMOVE(&shared_resource->mgmt_ch->shared_resources, shared_resource, link);
3089 		spdk_put_io_channel(spdk_io_channel_from_ctx(shared_resource->mgmt_ch));
3090 		free(shared_resource);
3091 	}
3092 }
3093 
3094 /* Caller must hold bdev->internal.mutex. */
3095 static void
3096 bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch)
3097 {
3098 	struct spdk_bdev_qos	*qos = bdev->internal.qos;
3099 	int			i;
3100 
3101 	/* Rate limiting on this bdev enabled */
3102 	if (qos) {
3103 		if (qos->ch == NULL) {
3104 			struct spdk_io_channel *io_ch;
3105 
3106 			SPDK_DEBUGLOG(bdev, "Selecting channel %p as QoS channel for bdev %s on thread %p\n", ch,
3107 				      bdev->name, spdk_get_thread());
3108 
3109 			/* No qos channel has been selected, so set one up */
3110 
3111 			/* Take another reference to ch */
3112 			io_ch = spdk_get_io_channel(__bdev_to_io_dev(bdev));
3113 			assert(io_ch != NULL);
3114 			qos->ch = ch;
3115 
3116 			qos->thread = spdk_io_channel_get_thread(io_ch);
3117 
3118 			TAILQ_INIT(&qos->queued);
3119 
3120 			for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3121 				if (bdev_qos_is_iops_rate_limit(i) == true) {
3122 					qos->rate_limits[i].min_per_timeslice =
3123 						SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE;
3124 				} else {
3125 					qos->rate_limits[i].min_per_timeslice =
3126 						SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE;
3127 				}
3128 
3129 				if (qos->rate_limits[i].limit == 0) {
3130 					qos->rate_limits[i].limit = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
3131 				}
3132 			}
3133 			bdev_qos_update_max_quota_per_timeslice(qos);
3134 			qos->timeslice_size =
3135 				SPDK_BDEV_QOS_TIMESLICE_IN_USEC * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
3136 			qos->last_timeslice = spdk_get_ticks();
3137 			qos->poller = SPDK_POLLER_REGISTER(bdev_channel_poll_qos,
3138 							   qos,
3139 							   SPDK_BDEV_QOS_TIMESLICE_IN_USEC);
3140 		}
3141 
3142 		ch->flags |= BDEV_CH_QOS_ENABLED;
3143 	}
3144 }
3145 
3146 struct poll_timeout_ctx {
3147 	struct spdk_bdev_desc	*desc;
3148 	uint64_t		timeout_in_sec;
3149 	spdk_bdev_io_timeout_cb	cb_fn;
3150 	void			*cb_arg;
3151 };
3152 
3153 static void
3154 bdev_desc_free(struct spdk_bdev_desc *desc)
3155 {
3156 	pthread_mutex_destroy(&desc->mutex);
3157 	free(desc->media_events_buffer);
3158 	free(desc);
3159 }
3160 
3161 static void
3162 bdev_channel_poll_timeout_io_done(struct spdk_io_channel_iter *i, int status)
3163 {
3164 	struct poll_timeout_ctx *ctx  = spdk_io_channel_iter_get_ctx(i);
3165 	struct spdk_bdev_desc *desc = ctx->desc;
3166 
3167 	free(ctx);
3168 
3169 	pthread_mutex_lock(&desc->mutex);
3170 	desc->refs--;
3171 	if (desc->closed == true && desc->refs == 0) {
3172 		pthread_mutex_unlock(&desc->mutex);
3173 		bdev_desc_free(desc);
3174 		return;
3175 	}
3176 	pthread_mutex_unlock(&desc->mutex);
3177 }
3178 
3179 static void
3180 bdev_channel_poll_timeout_io(struct spdk_io_channel_iter *i)
3181 {
3182 	struct poll_timeout_ctx *ctx  = spdk_io_channel_iter_get_ctx(i);
3183 	struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i);
3184 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(io_ch);
3185 	struct spdk_bdev_desc *desc = ctx->desc;
3186 	struct spdk_bdev_io *bdev_io;
3187 	uint64_t now;
3188 
3189 	pthread_mutex_lock(&desc->mutex);
3190 	if (desc->closed == true) {
3191 		pthread_mutex_unlock(&desc->mutex);
3192 		spdk_for_each_channel_continue(i, -1);
3193 		return;
3194 	}
3195 	pthread_mutex_unlock(&desc->mutex);
3196 
3197 	now = spdk_get_ticks();
3198 	TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) {
3199 		/* Exclude any I/O that are generated via splitting. */
3200 		if (bdev_io->internal.cb == bdev_io_split_done) {
3201 			continue;
3202 		}
3203 
3204 		/* Once we find an I/O that has not timed out, we can immediately
3205 		 * exit the loop.
3206 		 */
3207 		if (now < (bdev_io->internal.submit_tsc +
3208 			   ctx->timeout_in_sec * spdk_get_ticks_hz())) {
3209 			goto end;
3210 		}
3211 
3212 		if (bdev_io->internal.desc == desc) {
3213 			ctx->cb_fn(ctx->cb_arg, bdev_io);
3214 		}
3215 	}
3216 
3217 end:
3218 	spdk_for_each_channel_continue(i, 0);
3219 }
3220 
3221 static int
3222 bdev_poll_timeout_io(void *arg)
3223 {
3224 	struct spdk_bdev_desc *desc = arg;
3225 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3226 	struct poll_timeout_ctx *ctx;
3227 
3228 	ctx = calloc(1, sizeof(struct poll_timeout_ctx));
3229 	if (!ctx) {
3230 		SPDK_ERRLOG("failed to allocate memory\n");
3231 		return SPDK_POLLER_BUSY;
3232 	}
3233 	ctx->desc = desc;
3234 	ctx->cb_arg = desc->cb_arg;
3235 	ctx->cb_fn = desc->cb_fn;
3236 	ctx->timeout_in_sec = desc->timeout_in_sec;
3237 
3238 	/* Take a ref on the descriptor in case it gets closed while we are checking
3239 	 * all of the channels.
3240 	 */
3241 	pthread_mutex_lock(&desc->mutex);
3242 	desc->refs++;
3243 	pthread_mutex_unlock(&desc->mutex);
3244 
3245 	spdk_for_each_channel(__bdev_to_io_dev(bdev),
3246 			      bdev_channel_poll_timeout_io,
3247 			      ctx,
3248 			      bdev_channel_poll_timeout_io_done);
3249 
3250 	return SPDK_POLLER_BUSY;
3251 }
3252 
3253 int
3254 spdk_bdev_set_timeout(struct spdk_bdev_desc *desc, uint64_t timeout_in_sec,
3255 		      spdk_bdev_io_timeout_cb cb_fn, void *cb_arg)
3256 {
3257 	assert(desc->thread == spdk_get_thread());
3258 
3259 	spdk_poller_unregister(&desc->io_timeout_poller);
3260 
3261 	if (timeout_in_sec) {
3262 		assert(cb_fn != NULL);
3263 		desc->io_timeout_poller = SPDK_POLLER_REGISTER(bdev_poll_timeout_io,
3264 					  desc,
3265 					  SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * SPDK_SEC_TO_USEC /
3266 					  1000);
3267 		if (desc->io_timeout_poller == NULL) {
3268 			SPDK_ERRLOG("can not register the desc timeout IO poller\n");
3269 			return -1;
3270 		}
3271 	}
3272 
3273 	desc->cb_fn = cb_fn;
3274 	desc->cb_arg = cb_arg;
3275 	desc->timeout_in_sec = timeout_in_sec;
3276 
3277 	return 0;
3278 }
3279 
3280 static int
3281 bdev_channel_create(void *io_device, void *ctx_buf)
3282 {
3283 	struct spdk_bdev		*bdev = __bdev_from_io_dev(io_device);
3284 	struct spdk_bdev_channel	*ch = ctx_buf;
3285 	struct spdk_io_channel		*mgmt_io_ch;
3286 	struct spdk_bdev_mgmt_channel	*mgmt_ch;
3287 	struct spdk_bdev_shared_resource *shared_resource;
3288 	struct lba_range		*range;
3289 
3290 	ch->bdev = bdev;
3291 	ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt);
3292 	if (!ch->channel) {
3293 		return -1;
3294 	}
3295 
3296 	spdk_trace_record(TRACE_BDEV_IOCH_CREATE, 0, 0, 0, ch->bdev->name,
3297 			  spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel)));
3298 
3299 	assert(ch->histogram == NULL);
3300 	if (bdev->internal.histogram_enabled) {
3301 		ch->histogram = spdk_histogram_data_alloc();
3302 		if (ch->histogram == NULL) {
3303 			SPDK_ERRLOG("Could not allocate histogram\n");
3304 		}
3305 	}
3306 
3307 	mgmt_io_ch = spdk_get_io_channel(&g_bdev_mgr);
3308 	if (!mgmt_io_ch) {
3309 		spdk_put_io_channel(ch->channel);
3310 		return -1;
3311 	}
3312 
3313 	mgmt_ch = spdk_io_channel_get_ctx(mgmt_io_ch);
3314 	TAILQ_FOREACH(shared_resource, &mgmt_ch->shared_resources, link) {
3315 		if (shared_resource->shared_ch == ch->channel) {
3316 			spdk_put_io_channel(mgmt_io_ch);
3317 			shared_resource->ref++;
3318 			break;
3319 		}
3320 	}
3321 
3322 	if (shared_resource == NULL) {
3323 		shared_resource = calloc(1, sizeof(*shared_resource));
3324 		if (shared_resource == NULL) {
3325 			spdk_put_io_channel(ch->channel);
3326 			spdk_put_io_channel(mgmt_io_ch);
3327 			return -1;
3328 		}
3329 
3330 		shared_resource->mgmt_ch = mgmt_ch;
3331 		shared_resource->io_outstanding = 0;
3332 		TAILQ_INIT(&shared_resource->nomem_io);
3333 		shared_resource->nomem_threshold = 0;
3334 		shared_resource->shared_ch = ch->channel;
3335 		shared_resource->ref = 1;
3336 		TAILQ_INSERT_TAIL(&mgmt_ch->shared_resources, shared_resource, link);
3337 	}
3338 
3339 	memset(&ch->stat, 0, sizeof(ch->stat));
3340 	ch->stat.ticks_rate = spdk_get_ticks_hz();
3341 	ch->io_outstanding = 0;
3342 	TAILQ_INIT(&ch->queued_resets);
3343 	TAILQ_INIT(&ch->locked_ranges);
3344 	ch->flags = 0;
3345 	ch->shared_resource = shared_resource;
3346 
3347 	TAILQ_INIT(&ch->io_submitted);
3348 	TAILQ_INIT(&ch->io_locked);
3349 
3350 #ifdef SPDK_CONFIG_VTUNE
3351 	{
3352 		char *name;
3353 		__itt_init_ittlib(NULL, 0);
3354 		name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch);
3355 		if (!name) {
3356 			bdev_channel_destroy_resource(ch);
3357 			return -1;
3358 		}
3359 		ch->handle = __itt_string_handle_create(name);
3360 		free(name);
3361 		ch->start_tsc = spdk_get_ticks();
3362 		ch->interval_tsc = spdk_get_ticks_hz() / 100;
3363 		memset(&ch->prev_stat, 0, sizeof(ch->prev_stat));
3364 	}
3365 #endif
3366 
3367 	pthread_mutex_lock(&bdev->internal.mutex);
3368 	bdev_enable_qos(bdev, ch);
3369 
3370 	TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) {
3371 		struct lba_range *new_range;
3372 
3373 		new_range = calloc(1, sizeof(*new_range));
3374 		if (new_range == NULL) {
3375 			pthread_mutex_unlock(&bdev->internal.mutex);
3376 			bdev_channel_destroy_resource(ch);
3377 			return -1;
3378 		}
3379 		new_range->length = range->length;
3380 		new_range->offset = range->offset;
3381 		new_range->locked_ctx = range->locked_ctx;
3382 		TAILQ_INSERT_TAIL(&ch->locked_ranges, new_range, tailq);
3383 	}
3384 
3385 	pthread_mutex_unlock(&bdev->internal.mutex);
3386 
3387 	return 0;
3388 }
3389 
3390 /*
3391  * Abort I/O that are waiting on a data buffer.  These types of I/O are
3392  *  linked using the spdk_bdev_io internal.buf_link TAILQ_ENTRY.
3393  */
3394 static void
3395 bdev_abort_all_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_channel *ch)
3396 {
3397 	bdev_io_stailq_t tmp;
3398 	struct spdk_bdev_io *bdev_io;
3399 
3400 	STAILQ_INIT(&tmp);
3401 
3402 	while (!STAILQ_EMPTY(queue)) {
3403 		bdev_io = STAILQ_FIRST(queue);
3404 		STAILQ_REMOVE_HEAD(queue, internal.buf_link);
3405 		if (bdev_io->internal.ch == ch) {
3406 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
3407 		} else {
3408 			STAILQ_INSERT_TAIL(&tmp, bdev_io, internal.buf_link);
3409 		}
3410 	}
3411 
3412 	STAILQ_SWAP(&tmp, queue, spdk_bdev_io);
3413 }
3414 
3415 /*
3416  * Abort I/O that are queued waiting for submission.  These types of I/O are
3417  *  linked using the spdk_bdev_io link TAILQ_ENTRY.
3418  */
3419 static void
3420 bdev_abort_all_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch)
3421 {
3422 	struct spdk_bdev_io *bdev_io, *tmp;
3423 
3424 	TAILQ_FOREACH_SAFE(bdev_io, queue, internal.link, tmp) {
3425 		if (bdev_io->internal.ch == ch) {
3426 			TAILQ_REMOVE(queue, bdev_io, internal.link);
3427 			/*
3428 			 * spdk_bdev_io_complete() assumes that the completed I/O had
3429 			 *  been submitted to the bdev module.  Since in this case it
3430 			 *  hadn't, bump io_outstanding to account for the decrement
3431 			 *  that spdk_bdev_io_complete() will do.
3432 			 */
3433 			if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) {
3434 				ch->io_outstanding++;
3435 				ch->shared_resource->io_outstanding++;
3436 			}
3437 			spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
3438 		}
3439 	}
3440 }
3441 
3442 static bool
3443 bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort)
3444 {
3445 	struct spdk_bdev_io *bdev_io;
3446 
3447 	TAILQ_FOREACH(bdev_io, queue, internal.link) {
3448 		if (bdev_io == bio_to_abort) {
3449 			TAILQ_REMOVE(queue, bio_to_abort, internal.link);
3450 			spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED);
3451 			return true;
3452 		}
3453 	}
3454 
3455 	return false;
3456 }
3457 
3458 static bool
3459 bdev_abort_buf_io(bdev_io_stailq_t *queue, struct spdk_bdev_io *bio_to_abort)
3460 {
3461 	struct spdk_bdev_io *bdev_io;
3462 
3463 	STAILQ_FOREACH(bdev_io, queue, internal.buf_link) {
3464 		if (bdev_io == bio_to_abort) {
3465 			STAILQ_REMOVE(queue, bio_to_abort, spdk_bdev_io, internal.buf_link);
3466 			spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED);
3467 			return true;
3468 		}
3469 	}
3470 
3471 	return false;
3472 }
3473 
3474 static void
3475 bdev_qos_channel_destroy(void *cb_arg)
3476 {
3477 	struct spdk_bdev_qos *qos = cb_arg;
3478 
3479 	spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
3480 	spdk_poller_unregister(&qos->poller);
3481 
3482 	SPDK_DEBUGLOG(bdev, "Free QoS %p.\n", qos);
3483 
3484 	free(qos);
3485 }
3486 
3487 static int
3488 bdev_qos_destroy(struct spdk_bdev *bdev)
3489 {
3490 	int i;
3491 
3492 	/*
3493 	 * Cleanly shutting down the QoS poller is tricky, because
3494 	 * during the asynchronous operation the user could open
3495 	 * a new descriptor and create a new channel, spawning
3496 	 * a new QoS poller.
3497 	 *
3498 	 * The strategy is to create a new QoS structure here and swap it
3499 	 * in. The shutdown path then continues to refer to the old one
3500 	 * until it completes and then releases it.
3501 	 */
3502 	struct spdk_bdev_qos *new_qos, *old_qos;
3503 
3504 	old_qos = bdev->internal.qos;
3505 
3506 	new_qos = calloc(1, sizeof(*new_qos));
3507 	if (!new_qos) {
3508 		SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n");
3509 		return -ENOMEM;
3510 	}
3511 
3512 	/* Copy the old QoS data into the newly allocated structure */
3513 	memcpy(new_qos, old_qos, sizeof(*new_qos));
3514 
3515 	/* Zero out the key parts of the QoS structure */
3516 	new_qos->ch = NULL;
3517 	new_qos->thread = NULL;
3518 	new_qos->poller = NULL;
3519 	TAILQ_INIT(&new_qos->queued);
3520 	/*
3521 	 * The limit member of spdk_bdev_qos_limit structure is not zeroed.
3522 	 * It will be used later for the new QoS structure.
3523 	 */
3524 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3525 		new_qos->rate_limits[i].remaining_this_timeslice = 0;
3526 		new_qos->rate_limits[i].min_per_timeslice = 0;
3527 		new_qos->rate_limits[i].max_per_timeslice = 0;
3528 	}
3529 
3530 	bdev->internal.qos = new_qos;
3531 
3532 	if (old_qos->thread == NULL) {
3533 		free(old_qos);
3534 	} else {
3535 		spdk_thread_send_msg(old_qos->thread, bdev_qos_channel_destroy, old_qos);
3536 	}
3537 
3538 	/* It is safe to continue with destroying the bdev even though the QoS channel hasn't
3539 	 * been destroyed yet. The destruction path will end up waiting for the final
3540 	 * channel to be put before it releases resources. */
3541 
3542 	return 0;
3543 }
3544 
3545 static void
3546 bdev_io_stat_add(struct spdk_bdev_io_stat *total, struct spdk_bdev_io_stat *add)
3547 {
3548 	total->bytes_read += add->bytes_read;
3549 	total->num_read_ops += add->num_read_ops;
3550 	total->bytes_written += add->bytes_written;
3551 	total->num_write_ops += add->num_write_ops;
3552 	total->bytes_unmapped += add->bytes_unmapped;
3553 	total->num_unmap_ops += add->num_unmap_ops;
3554 	total->read_latency_ticks += add->read_latency_ticks;
3555 	total->write_latency_ticks += add->write_latency_ticks;
3556 	total->unmap_latency_ticks += add->unmap_latency_ticks;
3557 }
3558 
3559 static void
3560 bdev_channel_abort_queued_ios(struct spdk_bdev_channel *ch)
3561 {
3562 	struct spdk_bdev_shared_resource *shared_resource = ch->shared_resource;
3563 	struct spdk_bdev_mgmt_channel *mgmt_ch = shared_resource->mgmt_ch;
3564 
3565 	bdev_abort_all_queued_io(&shared_resource->nomem_io, ch);
3566 	bdev_abort_all_buf_io(&mgmt_ch->need_buf_small, ch);
3567 	bdev_abort_all_buf_io(&mgmt_ch->need_buf_large, ch);
3568 }
3569 
3570 static void
3571 bdev_channel_destroy(void *io_device, void *ctx_buf)
3572 {
3573 	struct spdk_bdev_channel *ch = ctx_buf;
3574 
3575 	SPDK_DEBUGLOG(bdev, "Destroying channel %p for bdev %s on thread %p\n", ch, ch->bdev->name,
3576 		      spdk_get_thread());
3577 
3578 	spdk_trace_record(TRACE_BDEV_IOCH_DESTROY, 0, 0, 0, ch->bdev->name,
3579 			  spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel)));
3580 
3581 	/* This channel is going away, so add its statistics into the bdev so that they don't get lost. */
3582 	pthread_mutex_lock(&ch->bdev->internal.mutex);
3583 	bdev_io_stat_add(&ch->bdev->internal.stat, &ch->stat);
3584 	pthread_mutex_unlock(&ch->bdev->internal.mutex);
3585 
3586 	bdev_abort_all_queued_io(&ch->queued_resets, ch);
3587 
3588 	bdev_channel_abort_queued_ios(ch);
3589 
3590 	if (ch->histogram) {
3591 		spdk_histogram_data_free(ch->histogram);
3592 	}
3593 
3594 	bdev_channel_destroy_resource(ch);
3595 }
3596 
3597 /*
3598  * If the name already exists in the global bdev name tree, RB_INSERT() returns a pointer
3599  * to it. Hence we do not have to call bdev_get_by_name() when using this function.
3600  */
3601 static int
3602 bdev_name_add(struct spdk_bdev_name *bdev_name, struct spdk_bdev *bdev, const char *name)
3603 {
3604 	struct spdk_bdev_name *tmp;
3605 
3606 	bdev_name->name = strdup(name);
3607 	if (bdev_name->name == NULL) {
3608 		SPDK_ERRLOG("Unable to allocate bdev name\n");
3609 		return -ENOMEM;
3610 	}
3611 
3612 	bdev_name->bdev = bdev;
3613 
3614 	pthread_mutex_lock(&g_bdev_mgr.mutex);
3615 	tmp = RB_INSERT(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
3616 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
3617 
3618 	if (tmp != NULL) {
3619 		SPDK_ERRLOG("Bdev name %s already exists\n", name);
3620 		free(bdev_name->name);
3621 		return -EEXIST;
3622 	}
3623 
3624 	return 0;
3625 }
3626 
3627 static void
3628 bdev_name_del_unsafe(struct spdk_bdev_name *bdev_name)
3629 {
3630 	RB_REMOVE(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
3631 	free(bdev_name->name);
3632 }
3633 
3634 static void
3635 bdev_name_del(struct spdk_bdev_name *bdev_name)
3636 {
3637 	pthread_mutex_lock(&g_bdev_mgr.mutex);
3638 	bdev_name_del_unsafe(bdev_name);
3639 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
3640 }
3641 
3642 int
3643 spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
3644 {
3645 	struct spdk_bdev_alias *tmp;
3646 	int ret;
3647 
3648 	if (alias == NULL) {
3649 		SPDK_ERRLOG("Empty alias passed\n");
3650 		return -EINVAL;
3651 	}
3652 
3653 	tmp = calloc(1, sizeof(*tmp));
3654 	if (tmp == NULL) {
3655 		SPDK_ERRLOG("Unable to allocate alias\n");
3656 		return -ENOMEM;
3657 	}
3658 
3659 	ret = bdev_name_add(&tmp->alias, bdev, alias);
3660 	if (ret != 0) {
3661 		free(tmp);
3662 		return ret;
3663 	}
3664 
3665 	TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq);
3666 
3667 	return 0;
3668 }
3669 
3670 static int
3671 bdev_alias_del(struct spdk_bdev *bdev, const char *alias,
3672 	       void (*alias_del_fn)(struct spdk_bdev_name *n))
3673 {
3674 	struct spdk_bdev_alias *tmp;
3675 
3676 	TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
3677 		if (strcmp(alias, tmp->alias.name) == 0) {
3678 			TAILQ_REMOVE(&bdev->aliases, tmp, tailq);
3679 			alias_del_fn(&tmp->alias);
3680 			free(tmp);
3681 			return 0;
3682 		}
3683 	}
3684 
3685 	return -ENOENT;
3686 }
3687 
3688 int
3689 spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias)
3690 {
3691 	int rc;
3692 
3693 	rc = bdev_alias_del(bdev, alias, bdev_name_del);
3694 	if (rc == -ENOENT) {
3695 		SPDK_INFOLOG(bdev, "Alias %s does not exist\n", alias);
3696 	}
3697 
3698 	return rc;
3699 }
3700 
3701 void
3702 spdk_bdev_alias_del_all(struct spdk_bdev *bdev)
3703 {
3704 	struct spdk_bdev_alias *p, *tmp;
3705 
3706 	TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) {
3707 		TAILQ_REMOVE(&bdev->aliases, p, tailq);
3708 		bdev_name_del(&p->alias);
3709 		free(p);
3710 	}
3711 }
3712 
3713 struct spdk_io_channel *
3714 spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
3715 {
3716 	return spdk_get_io_channel(__bdev_to_io_dev(spdk_bdev_desc_get_bdev(desc)));
3717 }
3718 
3719 void *
3720 spdk_bdev_get_module_ctx(struct spdk_bdev_desc *desc)
3721 {
3722 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
3723 	void *ctx = NULL;
3724 
3725 	if (bdev->fn_table->get_module_ctx) {
3726 		ctx = bdev->fn_table->get_module_ctx(bdev->ctxt);
3727 	}
3728 
3729 	return ctx;
3730 }
3731 
3732 const char *
3733 spdk_bdev_get_module_name(const struct spdk_bdev *bdev)
3734 {
3735 	return bdev->module->name;
3736 }
3737 
3738 const char *
3739 spdk_bdev_get_name(const struct spdk_bdev *bdev)
3740 {
3741 	return bdev->name;
3742 }
3743 
3744 const char *
3745 spdk_bdev_get_product_name(const struct spdk_bdev *bdev)
3746 {
3747 	return bdev->product_name;
3748 }
3749 
3750 const struct spdk_bdev_aliases_list *
3751 spdk_bdev_get_aliases(const struct spdk_bdev *bdev)
3752 {
3753 	return &bdev->aliases;
3754 }
3755 
3756 uint32_t
3757 spdk_bdev_get_block_size(const struct spdk_bdev *bdev)
3758 {
3759 	return bdev->blocklen;
3760 }
3761 
3762 uint32_t
3763 spdk_bdev_get_write_unit_size(const struct spdk_bdev *bdev)
3764 {
3765 	return bdev->write_unit_size;
3766 }
3767 
3768 uint64_t
3769 spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
3770 {
3771 	return bdev->blockcnt;
3772 }
3773 
3774 const char *
3775 spdk_bdev_get_qos_rpc_type(enum spdk_bdev_qos_rate_limit_type type)
3776 {
3777 	return qos_rpc_type[type];
3778 }
3779 
3780 void
3781 spdk_bdev_get_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
3782 {
3783 	int i;
3784 
3785 	memset(limits, 0, sizeof(*limits) * SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
3786 
3787 	pthread_mutex_lock(&bdev->internal.mutex);
3788 	if (bdev->internal.qos) {
3789 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3790 			if (bdev->internal.qos->rate_limits[i].limit !=
3791 			    SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
3792 				limits[i] = bdev->internal.qos->rate_limits[i].limit;
3793 				if (bdev_qos_is_iops_rate_limit(i) == false) {
3794 					/* Change from Byte to Megabyte which is user visible. */
3795 					limits[i] = limits[i] / 1024 / 1024;
3796 				}
3797 			}
3798 		}
3799 	}
3800 	pthread_mutex_unlock(&bdev->internal.mutex);
3801 }
3802 
3803 size_t
3804 spdk_bdev_get_buf_align(const struct spdk_bdev *bdev)
3805 {
3806 	return 1 << bdev->required_alignment;
3807 }
3808 
3809 uint32_t
3810 spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev)
3811 {
3812 	return bdev->optimal_io_boundary;
3813 }
3814 
3815 bool
3816 spdk_bdev_has_write_cache(const struct spdk_bdev *bdev)
3817 {
3818 	return bdev->write_cache;
3819 }
3820 
3821 const struct spdk_uuid *
3822 spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
3823 {
3824 	return &bdev->uuid;
3825 }
3826 
3827 uint16_t
3828 spdk_bdev_get_acwu(const struct spdk_bdev *bdev)
3829 {
3830 	return bdev->acwu;
3831 }
3832 
3833 uint32_t
3834 spdk_bdev_get_md_size(const struct spdk_bdev *bdev)
3835 {
3836 	return bdev->md_len;
3837 }
3838 
3839 bool
3840 spdk_bdev_is_md_interleaved(const struct spdk_bdev *bdev)
3841 {
3842 	return (bdev->md_len != 0) && bdev->md_interleave;
3843 }
3844 
3845 bool
3846 spdk_bdev_is_md_separate(const struct spdk_bdev *bdev)
3847 {
3848 	return (bdev->md_len != 0) && !bdev->md_interleave;
3849 }
3850 
3851 bool
3852 spdk_bdev_is_zoned(const struct spdk_bdev *bdev)
3853 {
3854 	return bdev->zoned;
3855 }
3856 
3857 uint32_t
3858 spdk_bdev_get_data_block_size(const struct spdk_bdev *bdev)
3859 {
3860 	if (spdk_bdev_is_md_interleaved(bdev)) {
3861 		return bdev->blocklen - bdev->md_len;
3862 	} else {
3863 		return bdev->blocklen;
3864 	}
3865 }
3866 
3867 uint32_t
3868 spdk_bdev_get_physical_block_size(const struct spdk_bdev *bdev)
3869 {
3870 	return bdev->phys_blocklen;
3871 }
3872 
3873 static uint32_t
3874 _bdev_get_block_size_with_md(const struct spdk_bdev *bdev)
3875 {
3876 	if (!spdk_bdev_is_md_interleaved(bdev)) {
3877 		return bdev->blocklen + bdev->md_len;
3878 	} else {
3879 		return bdev->blocklen;
3880 	}
3881 }
3882 
3883 /* We have to use the typedef in the function declaration to appease astyle. */
3884 typedef enum spdk_dif_type spdk_dif_type_t;
3885 
3886 spdk_dif_type_t
3887 spdk_bdev_get_dif_type(const struct spdk_bdev *bdev)
3888 {
3889 	if (bdev->md_len != 0) {
3890 		return bdev->dif_type;
3891 	} else {
3892 		return SPDK_DIF_DISABLE;
3893 	}
3894 }
3895 
3896 bool
3897 spdk_bdev_is_dif_head_of_md(const struct spdk_bdev *bdev)
3898 {
3899 	if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
3900 		return bdev->dif_is_head_of_md;
3901 	} else {
3902 		return false;
3903 	}
3904 }
3905 
3906 bool
3907 spdk_bdev_is_dif_check_enabled(const struct spdk_bdev *bdev,
3908 			       enum spdk_dif_check_type check_type)
3909 {
3910 	if (spdk_bdev_get_dif_type(bdev) == SPDK_DIF_DISABLE) {
3911 		return false;
3912 	}
3913 
3914 	switch (check_type) {
3915 	case SPDK_DIF_CHECK_TYPE_REFTAG:
3916 		return (bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) != 0;
3917 	case SPDK_DIF_CHECK_TYPE_APPTAG:
3918 		return (bdev->dif_check_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) != 0;
3919 	case SPDK_DIF_CHECK_TYPE_GUARD:
3920 		return (bdev->dif_check_flags & SPDK_DIF_FLAGS_GUARD_CHECK) != 0;
3921 	default:
3922 		return false;
3923 	}
3924 }
3925 
3926 uint64_t
3927 spdk_bdev_get_qd(const struct spdk_bdev *bdev)
3928 {
3929 	return bdev->internal.measured_queue_depth;
3930 }
3931 
3932 uint64_t
3933 spdk_bdev_get_qd_sampling_period(const struct spdk_bdev *bdev)
3934 {
3935 	return bdev->internal.period;
3936 }
3937 
3938 uint64_t
3939 spdk_bdev_get_weighted_io_time(const struct spdk_bdev *bdev)
3940 {
3941 	return bdev->internal.weighted_io_time;
3942 }
3943 
3944 uint64_t
3945 spdk_bdev_get_io_time(const struct spdk_bdev *bdev)
3946 {
3947 	return bdev->internal.io_time;
3948 }
3949 
3950 static void bdev_update_qd_sampling_period(void *ctx);
3951 
3952 static void
3953 _calculate_measured_qd_cpl(struct spdk_io_channel_iter *i, int status)
3954 {
3955 	struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i);
3956 
3957 	bdev->internal.measured_queue_depth = bdev->internal.temporary_queue_depth;
3958 
3959 	if (bdev->internal.measured_queue_depth) {
3960 		bdev->internal.io_time += bdev->internal.period;
3961 		bdev->internal.weighted_io_time += bdev->internal.period * bdev->internal.measured_queue_depth;
3962 	}
3963 
3964 	bdev->internal.qd_poll_in_progress = false;
3965 
3966 	bdev_update_qd_sampling_period(bdev);
3967 }
3968 
3969 static void
3970 _calculate_measured_qd(struct spdk_io_channel_iter *i)
3971 {
3972 	struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i);
3973 	struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i);
3974 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(io_ch);
3975 
3976 	bdev->internal.temporary_queue_depth += ch->io_outstanding;
3977 	spdk_for_each_channel_continue(i, 0);
3978 }
3979 
3980 static int
3981 bdev_calculate_measured_queue_depth(void *ctx)
3982 {
3983 	struct spdk_bdev *bdev = ctx;
3984 
3985 	bdev->internal.qd_poll_in_progress = true;
3986 	bdev->internal.temporary_queue_depth = 0;
3987 	spdk_for_each_channel(__bdev_to_io_dev(bdev), _calculate_measured_qd, bdev,
3988 			      _calculate_measured_qd_cpl);
3989 	return SPDK_POLLER_BUSY;
3990 }
3991 
3992 static void
3993 bdev_update_qd_sampling_period(void *ctx)
3994 {
3995 	struct spdk_bdev *bdev = ctx;
3996 
3997 	if (bdev->internal.period == bdev->internal.new_period) {
3998 		return;
3999 	}
4000 
4001 	if (bdev->internal.qd_poll_in_progress) {
4002 		return;
4003 	}
4004 
4005 	bdev->internal.period = bdev->internal.new_period;
4006 
4007 	spdk_poller_unregister(&bdev->internal.qd_poller);
4008 	if (bdev->internal.period != 0) {
4009 		bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth,
4010 					   bdev, bdev->internal.period);
4011 	} else {
4012 		spdk_bdev_close(bdev->internal.qd_desc);
4013 		bdev->internal.qd_desc = NULL;
4014 	}
4015 }
4016 
4017 static void
4018 _tmp_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
4019 {
4020 	SPDK_NOTICELOG("Unexpected event type: %d\n", type);
4021 }
4022 
4023 void
4024 spdk_bdev_set_qd_sampling_period(struct spdk_bdev *bdev, uint64_t period)
4025 {
4026 	int rc;
4027 
4028 	if (bdev->internal.new_period == period) {
4029 		return;
4030 	}
4031 
4032 	bdev->internal.new_period = period;
4033 
4034 	if (bdev->internal.qd_desc != NULL) {
4035 		assert(bdev->internal.period != 0);
4036 
4037 		spdk_thread_send_msg(bdev->internal.qd_desc->thread,
4038 				     bdev_update_qd_sampling_period, bdev);
4039 		return;
4040 	}
4041 
4042 	assert(bdev->internal.period == 0);
4043 
4044 	rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, _tmp_bdev_event_cb,
4045 				NULL, &bdev->internal.qd_desc);
4046 	if (rc != 0) {
4047 		return;
4048 	}
4049 
4050 	bdev->internal.period = period;
4051 	bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth,
4052 				   bdev, period);
4053 }
4054 
4055 struct bdev_get_current_qd_ctx {
4056 	uint64_t current_qd;
4057 	spdk_bdev_get_current_qd_cb cb_fn;
4058 	void *cb_arg;
4059 };
4060 
4061 static void
4062 bdev_get_current_qd_done(struct spdk_io_channel_iter *i, int status)
4063 {
4064 	struct bdev_get_current_qd_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4065 	void *io_dev = spdk_io_channel_iter_get_io_device(i);
4066 
4067 	ctx->cb_fn(__bdev_from_io_dev(io_dev), ctx->current_qd, ctx->cb_arg, 0);
4068 
4069 	free(ctx);
4070 }
4071 
4072 static void
4073 bdev_get_current_qd(struct spdk_io_channel_iter *i)
4074 {
4075 	struct bdev_get_current_qd_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
4076 	struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i);
4077 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(io_ch);
4078 
4079 	ctx->current_qd += bdev_ch->io_outstanding;
4080 
4081 	spdk_for_each_channel_continue(i, 0);
4082 }
4083 
4084 void
4085 spdk_bdev_get_current_qd(struct spdk_bdev *bdev, spdk_bdev_get_current_qd_cb cb_fn,
4086 			 void *cb_arg)
4087 {
4088 	struct bdev_get_current_qd_ctx *ctx;
4089 
4090 	assert(cb_fn != NULL);
4091 
4092 	ctx = calloc(1, sizeof(*ctx));
4093 	if (ctx == NULL) {
4094 		cb_fn(bdev, 0, cb_arg, -ENOMEM);
4095 		return;
4096 	}
4097 
4098 	ctx->cb_fn = cb_fn;
4099 	ctx->cb_arg = cb_arg;
4100 
4101 	spdk_for_each_channel(__bdev_to_io_dev(bdev),
4102 			      bdev_get_current_qd,
4103 			      ctx,
4104 			      bdev_get_current_qd_done);
4105 }
4106 
4107 static void
4108 _resize_notify(void *arg)
4109 {
4110 	struct spdk_bdev_desc *desc = arg;
4111 
4112 	pthread_mutex_lock(&desc->mutex);
4113 	desc->refs--;
4114 	if (!desc->closed) {
4115 		pthread_mutex_unlock(&desc->mutex);
4116 		desc->callback.event_fn(SPDK_BDEV_EVENT_RESIZE,
4117 					desc->bdev,
4118 					desc->callback.ctx);
4119 		return;
4120 	} else if (0 == desc->refs) {
4121 		/* This descriptor was closed after this resize_notify message was sent.
4122 		 * spdk_bdev_close() could not free the descriptor since this message was
4123 		 * in flight, so we free it now using bdev_desc_free().
4124 		 */
4125 		pthread_mutex_unlock(&desc->mutex);
4126 		bdev_desc_free(desc);
4127 		return;
4128 	}
4129 	pthread_mutex_unlock(&desc->mutex);
4130 }
4131 
4132 int
4133 spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
4134 {
4135 	struct spdk_bdev_desc *desc;
4136 	int ret;
4137 
4138 	if (size == bdev->blockcnt) {
4139 		return 0;
4140 	}
4141 
4142 	pthread_mutex_lock(&bdev->internal.mutex);
4143 
4144 	/* bdev has open descriptors */
4145 	if (!TAILQ_EMPTY(&bdev->internal.open_descs) &&
4146 	    bdev->blockcnt > size) {
4147 		ret = -EBUSY;
4148 	} else {
4149 		bdev->blockcnt = size;
4150 		TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
4151 			pthread_mutex_lock(&desc->mutex);
4152 			if (!desc->closed) {
4153 				desc->refs++;
4154 				spdk_thread_send_msg(desc->thread, _resize_notify, desc);
4155 			}
4156 			pthread_mutex_unlock(&desc->mutex);
4157 		}
4158 		ret = 0;
4159 	}
4160 
4161 	pthread_mutex_unlock(&bdev->internal.mutex);
4162 
4163 	return ret;
4164 }
4165 
4166 /*
4167  * Convert I/O offset and length from bytes to blocks.
4168  *
4169  * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size.
4170  */
4171 static uint64_t
4172 bdev_bytes_to_blocks(struct spdk_bdev *bdev, uint64_t offset_bytes, uint64_t *offset_blocks,
4173 		     uint64_t num_bytes, uint64_t *num_blocks)
4174 {
4175 	uint32_t block_size = bdev->blocklen;
4176 	uint8_t shift_cnt;
4177 
4178 	/* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */
4179 	if (spdk_likely(spdk_u32_is_pow2(block_size))) {
4180 		shift_cnt = spdk_u32log2(block_size);
4181 		*offset_blocks = offset_bytes >> shift_cnt;
4182 		*num_blocks = num_bytes >> shift_cnt;
4183 		return (offset_bytes - (*offset_blocks << shift_cnt)) |
4184 		       (num_bytes - (*num_blocks << shift_cnt));
4185 	} else {
4186 		*offset_blocks = offset_bytes / block_size;
4187 		*num_blocks = num_bytes / block_size;
4188 		return (offset_bytes % block_size) | (num_bytes % block_size);
4189 	}
4190 }
4191 
4192 static bool
4193 bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks)
4194 {
4195 	/* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there
4196 	 * has been an overflow and hence the offset has been wrapped around */
4197 	if (offset_blocks + num_blocks < offset_blocks) {
4198 		return false;
4199 	}
4200 
4201 	/* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */
4202 	if (offset_blocks + num_blocks > bdev->blockcnt) {
4203 		return false;
4204 	}
4205 
4206 	return true;
4207 }
4208 
4209 static void
4210 bdev_seek_complete_cb(void *ctx)
4211 {
4212 	struct spdk_bdev_io *bdev_io = ctx;
4213 
4214 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
4215 	bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx);
4216 }
4217 
4218 static int
4219 bdev_seek(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4220 	  uint64_t offset_blocks, enum spdk_bdev_io_type io_type,
4221 	  spdk_bdev_io_completion_cb cb, void *cb_arg)
4222 {
4223 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4224 	struct spdk_bdev_io *bdev_io;
4225 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4226 
4227 	assert(io_type == SPDK_BDEV_IO_TYPE_SEEK_DATA || io_type == SPDK_BDEV_IO_TYPE_SEEK_HOLE);
4228 
4229 	/* Check if offset_blocks is valid looking at the validity of one block */
4230 	if (!bdev_io_valid_blocks(bdev, offset_blocks, 1)) {
4231 		return -EINVAL;
4232 	}
4233 
4234 	bdev_io = bdev_channel_get_io(channel);
4235 	if (!bdev_io) {
4236 		return -ENOMEM;
4237 	}
4238 
4239 	bdev_io->internal.ch = channel;
4240 	bdev_io->internal.desc = desc;
4241 	bdev_io->type = io_type;
4242 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4243 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4244 
4245 	if (!spdk_bdev_io_type_supported(bdev, io_type)) {
4246 		/* In case bdev doesn't support seek to next data/hole offset,
4247 		 * it is assumed that only data and no holes are present */
4248 		if (io_type == SPDK_BDEV_IO_TYPE_SEEK_DATA) {
4249 			bdev_io->u.bdev.seek.offset = offset_blocks;
4250 		} else {
4251 			bdev_io->u.bdev.seek.offset = UINT64_MAX;
4252 		}
4253 
4254 		spdk_thread_send_msg(spdk_get_thread(), bdev_seek_complete_cb, bdev_io);
4255 		return 0;
4256 	}
4257 
4258 	bdev_io_submit(bdev_io);
4259 	return 0;
4260 }
4261 
4262 int
4263 spdk_bdev_seek_data(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4264 		    uint64_t offset_blocks,
4265 		    spdk_bdev_io_completion_cb cb, void *cb_arg)
4266 {
4267 	return bdev_seek(desc, ch, offset_blocks, SPDK_BDEV_IO_TYPE_SEEK_DATA, cb, cb_arg);
4268 }
4269 
4270 int
4271 spdk_bdev_seek_hole(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4272 		    uint64_t offset_blocks,
4273 		    spdk_bdev_io_completion_cb cb, void *cb_arg)
4274 {
4275 	return bdev_seek(desc, ch, offset_blocks, SPDK_BDEV_IO_TYPE_SEEK_HOLE, cb, cb_arg);
4276 }
4277 
4278 uint64_t
4279 spdk_bdev_io_get_seek_offset(const struct spdk_bdev_io *bdev_io)
4280 {
4281 	return bdev_io->u.bdev.seek.offset;
4282 }
4283 
4284 static int
4285 bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf,
4286 			 void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
4287 			 spdk_bdev_io_completion_cb cb, void *cb_arg)
4288 {
4289 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4290 	struct spdk_bdev_io *bdev_io;
4291 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4292 
4293 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
4294 		return -EINVAL;
4295 	}
4296 
4297 	bdev_io = bdev_channel_get_io(channel);
4298 	if (!bdev_io) {
4299 		return -ENOMEM;
4300 	}
4301 
4302 	bdev_io->internal.ch = channel;
4303 	bdev_io->internal.desc = desc;
4304 	bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
4305 	bdev_io->u.bdev.iovs = &bdev_io->iov;
4306 	bdev_io->u.bdev.iovs[0].iov_base = buf;
4307 	bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen;
4308 	bdev_io->u.bdev.iovcnt = 1;
4309 	bdev_io->u.bdev.md_buf = md_buf;
4310 	bdev_io->u.bdev.num_blocks = num_blocks;
4311 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4312 	bdev_io->u.bdev.ext_opts = NULL;
4313 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4314 
4315 	bdev_io_submit(bdev_io);
4316 	return 0;
4317 }
4318 
4319 int
4320 spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4321 	       void *buf, uint64_t offset, uint64_t nbytes,
4322 	       spdk_bdev_io_completion_cb cb, void *cb_arg)
4323 {
4324 	uint64_t offset_blocks, num_blocks;
4325 
4326 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
4327 				 nbytes, &num_blocks) != 0) {
4328 		return -EINVAL;
4329 	}
4330 
4331 	return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
4332 }
4333 
4334 int
4335 spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4336 		      void *buf, uint64_t offset_blocks, uint64_t num_blocks,
4337 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
4338 {
4339 	return bdev_read_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, cb, cb_arg);
4340 }
4341 
4342 int
4343 spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4344 			      void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
4345 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
4346 {
4347 	struct iovec iov = {
4348 		.iov_base = buf,
4349 	};
4350 
4351 	if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4352 		return -EINVAL;
4353 	}
4354 
4355 	if (md_buf && !_is_buf_allocated(&iov)) {
4356 		return -EINVAL;
4357 	}
4358 
4359 	return bdev_read_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
4360 					cb, cb_arg);
4361 }
4362 
4363 int
4364 spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4365 		struct iovec *iov, int iovcnt,
4366 		uint64_t offset, uint64_t nbytes,
4367 		spdk_bdev_io_completion_cb cb, void *cb_arg)
4368 {
4369 	uint64_t offset_blocks, num_blocks;
4370 
4371 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
4372 				 nbytes, &num_blocks) != 0) {
4373 		return -EINVAL;
4374 	}
4375 
4376 	return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
4377 }
4378 
4379 static int
4380 bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4381 			  struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
4382 			  uint64_t num_blocks, spdk_bdev_io_completion_cb cb, void *cb_arg,
4383 			  struct spdk_bdev_ext_io_opts *opts, bool copy_opts)
4384 {
4385 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4386 	struct spdk_bdev_io *bdev_io;
4387 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4388 
4389 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
4390 		return -EINVAL;
4391 	}
4392 
4393 	bdev_io = bdev_channel_get_io(channel);
4394 	if (!bdev_io) {
4395 		return -ENOMEM;
4396 	}
4397 
4398 	bdev_io->internal.ch = channel;
4399 	bdev_io->internal.desc = desc;
4400 	bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
4401 	bdev_io->u.bdev.iovs = iov;
4402 	bdev_io->u.bdev.iovcnt = iovcnt;
4403 	bdev_io->u.bdev.md_buf = md_buf;
4404 	bdev_io->u.bdev.num_blocks = num_blocks;
4405 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4406 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4407 	bdev_io->internal.ext_opts = opts;
4408 	bdev_io->u.bdev.ext_opts = opts;
4409 
4410 	_bdev_io_submit_ext(desc, bdev_io, opts, copy_opts);
4411 
4412 	return 0;
4413 }
4414 
4415 int
4416 spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4417 		       struct iovec *iov, int iovcnt,
4418 		       uint64_t offset_blocks, uint64_t num_blocks,
4419 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
4420 {
4421 	return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
4422 					 num_blocks, cb, cb_arg, NULL, false);
4423 }
4424 
4425 int
4426 spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4427 			       struct iovec *iov, int iovcnt, void *md_buf,
4428 			       uint64_t offset_blocks, uint64_t num_blocks,
4429 			       spdk_bdev_io_completion_cb cb, void *cb_arg)
4430 {
4431 	if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4432 		return -EINVAL;
4433 	}
4434 
4435 	if (md_buf && !_is_buf_allocated(iov)) {
4436 		return -EINVAL;
4437 	}
4438 
4439 	return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
4440 					 num_blocks, cb, cb_arg, NULL, false);
4441 }
4442 
4443 static inline bool
4444 _bdev_io_check_opts(struct spdk_bdev_ext_io_opts *opts, struct iovec *iov)
4445 {
4446 	/*
4447 	 * We check if opts size is at least of size when we first introduced
4448 	 * spdk_bdev_ext_io_opts (ac6f2bdd8d) since access to those members
4449 	 * are not checked internal.
4450 	 */
4451 	return opts->size >= offsetof(struct spdk_bdev_ext_io_opts, metadata) +
4452 	       sizeof(opts->metadata) &&
4453 	       opts->size <= sizeof(*opts) &&
4454 	       /* When memory domain is used, the user must provide data buffers */
4455 	       (!opts->memory_domain || (iov && iov[0].iov_base));
4456 }
4457 
4458 int
4459 spdk_bdev_readv_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4460 			   struct iovec *iov, int iovcnt,
4461 			   uint64_t offset_blocks, uint64_t num_blocks,
4462 			   spdk_bdev_io_completion_cb cb, void *cb_arg,
4463 			   struct spdk_bdev_ext_io_opts *opts)
4464 {
4465 	void *md = NULL;
4466 
4467 	if (opts) {
4468 		if (spdk_unlikely(!_bdev_io_check_opts(opts, iov))) {
4469 			return -EINVAL;
4470 		}
4471 		md = opts->metadata;
4472 	}
4473 
4474 	if (md && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4475 		return -EINVAL;
4476 	}
4477 
4478 	if (md && !_is_buf_allocated(iov)) {
4479 		return -EINVAL;
4480 	}
4481 
4482 	return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks,
4483 					 num_blocks, cb, cb_arg, opts, false);
4484 }
4485 
4486 static int
4487 bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4488 			  void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
4489 			  spdk_bdev_io_completion_cb cb, void *cb_arg)
4490 {
4491 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4492 	struct spdk_bdev_io *bdev_io;
4493 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4494 
4495 	if (!desc->write) {
4496 		return -EBADF;
4497 	}
4498 
4499 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
4500 		return -EINVAL;
4501 	}
4502 
4503 	bdev_io = bdev_channel_get_io(channel);
4504 	if (!bdev_io) {
4505 		return -ENOMEM;
4506 	}
4507 
4508 	bdev_io->internal.ch = channel;
4509 	bdev_io->internal.desc = desc;
4510 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
4511 	bdev_io->u.bdev.iovs = &bdev_io->iov;
4512 	bdev_io->u.bdev.iovs[0].iov_base = buf;
4513 	bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen;
4514 	bdev_io->u.bdev.iovcnt = 1;
4515 	bdev_io->u.bdev.md_buf = md_buf;
4516 	bdev_io->u.bdev.num_blocks = num_blocks;
4517 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4518 	bdev_io->u.bdev.ext_opts = NULL;
4519 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4520 
4521 	bdev_io_submit(bdev_io);
4522 	return 0;
4523 }
4524 
4525 int
4526 spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4527 		void *buf, uint64_t offset, uint64_t nbytes,
4528 		spdk_bdev_io_completion_cb cb, void *cb_arg)
4529 {
4530 	uint64_t offset_blocks, num_blocks;
4531 
4532 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
4533 				 nbytes, &num_blocks) != 0) {
4534 		return -EINVAL;
4535 	}
4536 
4537 	return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
4538 }
4539 
4540 int
4541 spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4542 		       void *buf, uint64_t offset_blocks, uint64_t num_blocks,
4543 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
4544 {
4545 	return bdev_write_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
4546 					 cb, cb_arg);
4547 }
4548 
4549 int
4550 spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4551 			       void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
4552 			       spdk_bdev_io_completion_cb cb, void *cb_arg)
4553 {
4554 	struct iovec iov = {
4555 		.iov_base = buf,
4556 	};
4557 
4558 	if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4559 		return -EINVAL;
4560 	}
4561 
4562 	if (md_buf && !_is_buf_allocated(&iov)) {
4563 		return -EINVAL;
4564 	}
4565 
4566 	return bdev_write_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
4567 					 cb, cb_arg);
4568 }
4569 
4570 static int
4571 bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4572 			   struct iovec *iov, int iovcnt, void *md_buf,
4573 			   uint64_t offset_blocks, uint64_t num_blocks,
4574 			   spdk_bdev_io_completion_cb cb, void *cb_arg,
4575 			   struct spdk_bdev_ext_io_opts *opts, bool copy_opts)
4576 {
4577 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4578 	struct spdk_bdev_io *bdev_io;
4579 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4580 
4581 	if (!desc->write) {
4582 		return -EBADF;
4583 	}
4584 
4585 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
4586 		return -EINVAL;
4587 	}
4588 
4589 	bdev_io = bdev_channel_get_io(channel);
4590 	if (!bdev_io) {
4591 		return -ENOMEM;
4592 	}
4593 
4594 	bdev_io->internal.ch = channel;
4595 	bdev_io->internal.desc = desc;
4596 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
4597 	bdev_io->u.bdev.iovs = iov;
4598 	bdev_io->u.bdev.iovcnt = iovcnt;
4599 	bdev_io->u.bdev.md_buf = md_buf;
4600 	bdev_io->u.bdev.num_blocks = num_blocks;
4601 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4602 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4603 	bdev_io->internal.ext_opts = opts;
4604 	bdev_io->u.bdev.ext_opts = opts;
4605 
4606 	_bdev_io_submit_ext(desc, bdev_io, opts, copy_opts);
4607 
4608 	return 0;
4609 }
4610 
4611 int
4612 spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4613 		 struct iovec *iov, int iovcnt,
4614 		 uint64_t offset, uint64_t len,
4615 		 spdk_bdev_io_completion_cb cb, void *cb_arg)
4616 {
4617 	uint64_t offset_blocks, num_blocks;
4618 
4619 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
4620 				 len, &num_blocks) != 0) {
4621 		return -EINVAL;
4622 	}
4623 
4624 	return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
4625 }
4626 
4627 int
4628 spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4629 			struct iovec *iov, int iovcnt,
4630 			uint64_t offset_blocks, uint64_t num_blocks,
4631 			spdk_bdev_io_completion_cb cb, void *cb_arg)
4632 {
4633 	return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
4634 					  num_blocks, cb, cb_arg, NULL, false);
4635 }
4636 
4637 int
4638 spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4639 				struct iovec *iov, int iovcnt, void *md_buf,
4640 				uint64_t offset_blocks, uint64_t num_blocks,
4641 				spdk_bdev_io_completion_cb cb, void *cb_arg)
4642 {
4643 	if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4644 		return -EINVAL;
4645 	}
4646 
4647 	if (md_buf && !_is_buf_allocated(iov)) {
4648 		return -EINVAL;
4649 	}
4650 
4651 	return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
4652 					  num_blocks, cb, cb_arg, NULL, false);
4653 }
4654 
4655 int
4656 spdk_bdev_writev_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4657 			    struct iovec *iov, int iovcnt,
4658 			    uint64_t offset_blocks, uint64_t num_blocks,
4659 			    spdk_bdev_io_completion_cb cb, void *cb_arg,
4660 			    struct spdk_bdev_ext_io_opts *opts)
4661 {
4662 	void *md = NULL;
4663 
4664 	if (opts) {
4665 		if (spdk_unlikely(!_bdev_io_check_opts(opts, iov))) {
4666 			return -EINVAL;
4667 		}
4668 		md = opts->metadata;
4669 	}
4670 
4671 	if (md && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4672 		return -EINVAL;
4673 	}
4674 
4675 	if (md && !_is_buf_allocated(iov)) {
4676 		return -EINVAL;
4677 	}
4678 
4679 	return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks,
4680 					  num_blocks, cb, cb_arg, opts, false);
4681 }
4682 
4683 static void
4684 bdev_compare_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
4685 {
4686 	struct spdk_bdev_io *parent_io = cb_arg;
4687 	struct spdk_bdev *bdev = parent_io->bdev;
4688 	uint8_t *read_buf = bdev_io->u.bdev.iovs[0].iov_base;
4689 	int i, rc = 0;
4690 
4691 	if (!success) {
4692 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
4693 		parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
4694 		spdk_bdev_free_io(bdev_io);
4695 		return;
4696 	}
4697 
4698 	for (i = 0; i < parent_io->u.bdev.iovcnt; i++) {
4699 		rc = memcmp(read_buf,
4700 			    parent_io->u.bdev.iovs[i].iov_base,
4701 			    parent_io->u.bdev.iovs[i].iov_len);
4702 		if (rc) {
4703 			break;
4704 		}
4705 		read_buf += parent_io->u.bdev.iovs[i].iov_len;
4706 	}
4707 
4708 	if (rc == 0 && parent_io->u.bdev.md_buf && spdk_bdev_is_md_separate(bdev)) {
4709 		rc = memcmp(bdev_io->u.bdev.md_buf,
4710 			    parent_io->u.bdev.md_buf,
4711 			    spdk_bdev_get_md_size(bdev));
4712 	}
4713 
4714 	spdk_bdev_free_io(bdev_io);
4715 
4716 	if (rc == 0) {
4717 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
4718 		parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx);
4719 	} else {
4720 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_MISCOMPARE;
4721 		parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
4722 	}
4723 }
4724 
4725 static void
4726 bdev_compare_do_read(void *_bdev_io)
4727 {
4728 	struct spdk_bdev_io *bdev_io = _bdev_io;
4729 	int rc;
4730 
4731 	rc = spdk_bdev_read_blocks(bdev_io->internal.desc,
4732 				   spdk_io_channel_from_ctx(bdev_io->internal.ch), NULL,
4733 				   bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
4734 				   bdev_compare_do_read_done, bdev_io);
4735 
4736 	if (rc == -ENOMEM) {
4737 		bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_do_read);
4738 	} else if (rc != 0) {
4739 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
4740 		bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
4741 	}
4742 }
4743 
4744 static int
4745 bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4746 			     struct iovec *iov, int iovcnt, void *md_buf,
4747 			     uint64_t offset_blocks, uint64_t num_blocks,
4748 			     spdk_bdev_io_completion_cb cb, void *cb_arg)
4749 {
4750 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4751 	struct spdk_bdev_io *bdev_io;
4752 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4753 
4754 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
4755 		return -EINVAL;
4756 	}
4757 
4758 	bdev_io = bdev_channel_get_io(channel);
4759 	if (!bdev_io) {
4760 		return -ENOMEM;
4761 	}
4762 
4763 	bdev_io->internal.ch = channel;
4764 	bdev_io->internal.desc = desc;
4765 	bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE;
4766 	bdev_io->u.bdev.iovs = iov;
4767 	bdev_io->u.bdev.iovcnt = iovcnt;
4768 	bdev_io->u.bdev.md_buf = md_buf;
4769 	bdev_io->u.bdev.num_blocks = num_blocks;
4770 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4771 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4772 	bdev_io->u.bdev.ext_opts = NULL;
4773 
4774 	if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) {
4775 		bdev_io_submit(bdev_io);
4776 		return 0;
4777 	}
4778 
4779 	bdev_compare_do_read(bdev_io);
4780 
4781 	return 0;
4782 }
4783 
4784 int
4785 spdk_bdev_comparev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4786 			  struct iovec *iov, int iovcnt,
4787 			  uint64_t offset_blocks, uint64_t num_blocks,
4788 			  spdk_bdev_io_completion_cb cb, void *cb_arg)
4789 {
4790 	return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
4791 					    num_blocks, cb, cb_arg);
4792 }
4793 
4794 int
4795 spdk_bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4796 				  struct iovec *iov, int iovcnt, void *md_buf,
4797 				  uint64_t offset_blocks, uint64_t num_blocks,
4798 				  spdk_bdev_io_completion_cb cb, void *cb_arg)
4799 {
4800 	if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4801 		return -EINVAL;
4802 	}
4803 
4804 	if (md_buf && !_is_buf_allocated(iov)) {
4805 		return -EINVAL;
4806 	}
4807 
4808 	return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
4809 					    num_blocks, cb, cb_arg);
4810 }
4811 
4812 static int
4813 bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4814 			    void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
4815 			    spdk_bdev_io_completion_cb cb, void *cb_arg)
4816 {
4817 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4818 	struct spdk_bdev_io *bdev_io;
4819 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4820 
4821 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
4822 		return -EINVAL;
4823 	}
4824 
4825 	bdev_io = bdev_channel_get_io(channel);
4826 	if (!bdev_io) {
4827 		return -ENOMEM;
4828 	}
4829 
4830 	bdev_io->internal.ch = channel;
4831 	bdev_io->internal.desc = desc;
4832 	bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE;
4833 	bdev_io->u.bdev.iovs = &bdev_io->iov;
4834 	bdev_io->u.bdev.iovs[0].iov_base = buf;
4835 	bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev->blocklen;
4836 	bdev_io->u.bdev.iovcnt = 1;
4837 	bdev_io->u.bdev.md_buf = md_buf;
4838 	bdev_io->u.bdev.num_blocks = num_blocks;
4839 	bdev_io->u.bdev.offset_blocks = offset_blocks;
4840 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
4841 	bdev_io->u.bdev.ext_opts = NULL;
4842 
4843 	if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) {
4844 		bdev_io_submit(bdev_io);
4845 		return 0;
4846 	}
4847 
4848 	bdev_compare_do_read(bdev_io);
4849 
4850 	return 0;
4851 }
4852 
4853 int
4854 spdk_bdev_compare_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4855 			 void *buf, uint64_t offset_blocks, uint64_t num_blocks,
4856 			 spdk_bdev_io_completion_cb cb, void *cb_arg)
4857 {
4858 	return bdev_compare_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
4859 					   cb, cb_arg);
4860 }
4861 
4862 int
4863 spdk_bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4864 				 void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
4865 				 spdk_bdev_io_completion_cb cb, void *cb_arg)
4866 {
4867 	struct iovec iov = {
4868 		.iov_base = buf,
4869 	};
4870 
4871 	if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
4872 		return -EINVAL;
4873 	}
4874 
4875 	if (md_buf && !_is_buf_allocated(&iov)) {
4876 		return -EINVAL;
4877 	}
4878 
4879 	return bdev_compare_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
4880 					   cb, cb_arg);
4881 }
4882 
4883 static void
4884 bdev_comparev_and_writev_blocks_unlocked(void *ctx, int unlock_status)
4885 {
4886 	struct spdk_bdev_io *bdev_io = ctx;
4887 
4888 	if (unlock_status) {
4889 		SPDK_ERRLOG("LBA range unlock failed\n");
4890 	}
4891 
4892 	bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS ? true :
4893 			     false, bdev_io->internal.caller_ctx);
4894 }
4895 
4896 static void
4897 bdev_comparev_and_writev_blocks_unlock(struct spdk_bdev_io *bdev_io, int status)
4898 {
4899 	bdev_io->internal.status = status;
4900 
4901 	bdev_unlock_lba_range(bdev_io->internal.desc, spdk_io_channel_from_ctx(bdev_io->internal.ch),
4902 			      bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
4903 			      bdev_comparev_and_writev_blocks_unlocked, bdev_io);
4904 }
4905 
4906 static void
4907 bdev_compare_and_write_do_write_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
4908 {
4909 	struct spdk_bdev_io *parent_io = cb_arg;
4910 
4911 	if (!success) {
4912 		SPDK_ERRLOG("Compare and write operation failed\n");
4913 	}
4914 
4915 	spdk_bdev_free_io(bdev_io);
4916 
4917 	bdev_comparev_and_writev_blocks_unlock(parent_io,
4918 					       success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED);
4919 }
4920 
4921 static void
4922 bdev_compare_and_write_do_write(void *_bdev_io)
4923 {
4924 	struct spdk_bdev_io *bdev_io = _bdev_io;
4925 	int rc;
4926 
4927 	rc = spdk_bdev_writev_blocks(bdev_io->internal.desc,
4928 				     spdk_io_channel_from_ctx(bdev_io->internal.ch),
4929 				     bdev_io->u.bdev.fused_iovs, bdev_io->u.bdev.fused_iovcnt,
4930 				     bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
4931 				     bdev_compare_and_write_do_write_done, bdev_io);
4932 
4933 
4934 	if (rc == -ENOMEM) {
4935 		bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_write);
4936 	} else if (rc != 0) {
4937 		bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
4938 	}
4939 }
4940 
4941 static void
4942 bdev_compare_and_write_do_compare_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
4943 {
4944 	struct spdk_bdev_io *parent_io = cb_arg;
4945 
4946 	spdk_bdev_free_io(bdev_io);
4947 
4948 	if (!success) {
4949 		bdev_comparev_and_writev_blocks_unlock(parent_io, SPDK_BDEV_IO_STATUS_MISCOMPARE);
4950 		return;
4951 	}
4952 
4953 	bdev_compare_and_write_do_write(parent_io);
4954 }
4955 
4956 static void
4957 bdev_compare_and_write_do_compare(void *_bdev_io)
4958 {
4959 	struct spdk_bdev_io *bdev_io = _bdev_io;
4960 	int rc;
4961 
4962 	rc = spdk_bdev_comparev_blocks(bdev_io->internal.desc,
4963 				       spdk_io_channel_from_ctx(bdev_io->internal.ch), bdev_io->u.bdev.iovs,
4964 				       bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
4965 				       bdev_compare_and_write_do_compare_done, bdev_io);
4966 
4967 	if (rc == -ENOMEM) {
4968 		bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_compare);
4969 	} else if (rc != 0) {
4970 		bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED);
4971 	}
4972 }
4973 
4974 static void
4975 bdev_comparev_and_writev_blocks_locked(void *ctx, int status)
4976 {
4977 	struct spdk_bdev_io *bdev_io = ctx;
4978 
4979 	if (status) {
4980 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED;
4981 		bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
4982 		return;
4983 	}
4984 
4985 	bdev_compare_and_write_do_compare(bdev_io);
4986 }
4987 
4988 int
4989 spdk_bdev_comparev_and_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
4990 				     struct iovec *compare_iov, int compare_iovcnt,
4991 				     struct iovec *write_iov, int write_iovcnt,
4992 				     uint64_t offset_blocks, uint64_t num_blocks,
4993 				     spdk_bdev_io_completion_cb cb, void *cb_arg)
4994 {
4995 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4996 	struct spdk_bdev_io *bdev_io;
4997 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
4998 
4999 	if (!desc->write) {
5000 		return -EBADF;
5001 	}
5002 
5003 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5004 		return -EINVAL;
5005 	}
5006 
5007 	if (num_blocks > bdev->acwu) {
5008 		return -EINVAL;
5009 	}
5010 
5011 	bdev_io = bdev_channel_get_io(channel);
5012 	if (!bdev_io) {
5013 		return -ENOMEM;
5014 	}
5015 
5016 	bdev_io->internal.ch = channel;
5017 	bdev_io->internal.desc = desc;
5018 	bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE;
5019 	bdev_io->u.bdev.iovs = compare_iov;
5020 	bdev_io->u.bdev.iovcnt = compare_iovcnt;
5021 	bdev_io->u.bdev.fused_iovs = write_iov;
5022 	bdev_io->u.bdev.fused_iovcnt = write_iovcnt;
5023 	bdev_io->u.bdev.md_buf = NULL;
5024 	bdev_io->u.bdev.num_blocks = num_blocks;
5025 	bdev_io->u.bdev.offset_blocks = offset_blocks;
5026 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5027 	bdev_io->u.bdev.ext_opts = NULL;
5028 
5029 	if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE)) {
5030 		bdev_io_submit(bdev_io);
5031 		return 0;
5032 	}
5033 
5034 	return bdev_lock_lba_range(desc, ch, offset_blocks, num_blocks,
5035 				   bdev_comparev_and_writev_blocks_locked, bdev_io);
5036 }
5037 
5038 int
5039 spdk_bdev_zcopy_start(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5040 		      struct iovec *iov, int iovcnt,
5041 		      uint64_t offset_blocks, uint64_t num_blocks,
5042 		      bool populate,
5043 		      spdk_bdev_io_completion_cb cb, void *cb_arg)
5044 {
5045 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5046 	struct spdk_bdev_io *bdev_io;
5047 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5048 
5049 	if (!desc->write) {
5050 		return -EBADF;
5051 	}
5052 
5053 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5054 		return -EINVAL;
5055 	}
5056 
5057 	if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) {
5058 		return -ENOTSUP;
5059 	}
5060 
5061 	bdev_io = bdev_channel_get_io(channel);
5062 	if (!bdev_io) {
5063 		return -ENOMEM;
5064 	}
5065 
5066 	bdev_io->internal.ch = channel;
5067 	bdev_io->internal.desc = desc;
5068 	bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY;
5069 	bdev_io->u.bdev.num_blocks = num_blocks;
5070 	bdev_io->u.bdev.offset_blocks = offset_blocks;
5071 	bdev_io->u.bdev.iovs = iov;
5072 	bdev_io->u.bdev.iovcnt = iovcnt;
5073 	bdev_io->u.bdev.md_buf = NULL;
5074 	bdev_io->u.bdev.zcopy.populate = populate ? 1 : 0;
5075 	bdev_io->u.bdev.zcopy.commit = 0;
5076 	bdev_io->u.bdev.zcopy.start = 1;
5077 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5078 	bdev_io->u.bdev.ext_opts = NULL;
5079 
5080 	bdev_io_submit(bdev_io);
5081 
5082 	return 0;
5083 }
5084 
5085 int
5086 spdk_bdev_zcopy_end(struct spdk_bdev_io *bdev_io, bool commit,
5087 		    spdk_bdev_io_completion_cb cb, void *cb_arg)
5088 {
5089 	if (bdev_io->type != SPDK_BDEV_IO_TYPE_ZCOPY) {
5090 		return -EINVAL;
5091 	}
5092 
5093 	bdev_io->u.bdev.zcopy.commit = commit ? 1 : 0;
5094 	bdev_io->u.bdev.zcopy.start = 0;
5095 	bdev_io->internal.caller_ctx = cb_arg;
5096 	bdev_io->internal.cb = cb;
5097 	bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
5098 
5099 	bdev_io_submit(bdev_io);
5100 
5101 	return 0;
5102 }
5103 
5104 int
5105 spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5106 		       uint64_t offset, uint64_t len,
5107 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
5108 {
5109 	uint64_t offset_blocks, num_blocks;
5110 
5111 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
5112 				 len, &num_blocks) != 0) {
5113 		return -EINVAL;
5114 	}
5115 
5116 	return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
5117 }
5118 
5119 int
5120 spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5121 			      uint64_t offset_blocks, uint64_t num_blocks,
5122 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
5123 {
5124 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5125 	struct spdk_bdev_io *bdev_io;
5126 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5127 
5128 	if (!desc->write) {
5129 		return -EBADF;
5130 	}
5131 
5132 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5133 		return -EINVAL;
5134 	}
5135 
5136 	if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) &&
5137 	    !bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)) {
5138 		return -ENOTSUP;
5139 	}
5140 
5141 	bdev_io = bdev_channel_get_io(channel);
5142 
5143 	if (!bdev_io) {
5144 		return -ENOMEM;
5145 	}
5146 
5147 	bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES;
5148 	bdev_io->internal.ch = channel;
5149 	bdev_io->internal.desc = desc;
5150 	bdev_io->u.bdev.offset_blocks = offset_blocks;
5151 	bdev_io->u.bdev.num_blocks = num_blocks;
5152 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5153 	bdev_io->u.bdev.ext_opts = NULL;
5154 
5155 	if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
5156 		bdev_io_submit(bdev_io);
5157 		return 0;
5158 	}
5159 
5160 	assert(bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE));
5161 	assert(_bdev_get_block_size_with_md(bdev) <= ZERO_BUFFER_SIZE);
5162 	bdev_io->u.bdev.split_remaining_num_blocks = num_blocks;
5163 	bdev_io->u.bdev.split_current_offset_blocks = offset_blocks;
5164 	bdev_write_zero_buffer_next(bdev_io);
5165 
5166 	return 0;
5167 }
5168 
5169 int
5170 spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5171 		uint64_t offset, uint64_t nbytes,
5172 		spdk_bdev_io_completion_cb cb, void *cb_arg)
5173 {
5174 	uint64_t offset_blocks, num_blocks;
5175 
5176 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
5177 				 nbytes, &num_blocks) != 0) {
5178 		return -EINVAL;
5179 	}
5180 
5181 	return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
5182 }
5183 
5184 int
5185 spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5186 		       uint64_t offset_blocks, uint64_t num_blocks,
5187 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
5188 {
5189 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5190 	struct spdk_bdev_io *bdev_io;
5191 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5192 
5193 	if (!desc->write) {
5194 		return -EBADF;
5195 	}
5196 
5197 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5198 		return -EINVAL;
5199 	}
5200 
5201 	if (num_blocks == 0) {
5202 		SPDK_ERRLOG("Can't unmap 0 bytes\n");
5203 		return -EINVAL;
5204 	}
5205 
5206 	bdev_io = bdev_channel_get_io(channel);
5207 	if (!bdev_io) {
5208 		return -ENOMEM;
5209 	}
5210 
5211 	bdev_io->internal.ch = channel;
5212 	bdev_io->internal.desc = desc;
5213 	bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP;
5214 
5215 	bdev_io->u.bdev.iovs = &bdev_io->iov;
5216 	bdev_io->u.bdev.iovs[0].iov_base = NULL;
5217 	bdev_io->u.bdev.iovs[0].iov_len = 0;
5218 	bdev_io->u.bdev.iovcnt = 1;
5219 
5220 	bdev_io->u.bdev.offset_blocks = offset_blocks;
5221 	bdev_io->u.bdev.num_blocks = num_blocks;
5222 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5223 	bdev_io->u.bdev.ext_opts = NULL;
5224 
5225 	bdev_io_submit(bdev_io);
5226 	return 0;
5227 }
5228 
5229 int
5230 spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5231 		uint64_t offset, uint64_t length,
5232 		spdk_bdev_io_completion_cb cb, void *cb_arg)
5233 {
5234 	uint64_t offset_blocks, num_blocks;
5235 
5236 	if (bdev_bytes_to_blocks(spdk_bdev_desc_get_bdev(desc), offset, &offset_blocks,
5237 				 length, &num_blocks) != 0) {
5238 		return -EINVAL;
5239 	}
5240 
5241 	return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
5242 }
5243 
5244 int
5245 spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5246 		       uint64_t offset_blocks, uint64_t num_blocks,
5247 		       spdk_bdev_io_completion_cb cb, void *cb_arg)
5248 {
5249 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5250 	struct spdk_bdev_io *bdev_io;
5251 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5252 
5253 	if (!desc->write) {
5254 		return -EBADF;
5255 	}
5256 
5257 	if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5258 		return -EINVAL;
5259 	}
5260 
5261 	bdev_io = bdev_channel_get_io(channel);
5262 	if (!bdev_io) {
5263 		return -ENOMEM;
5264 	}
5265 
5266 	bdev_io->internal.ch = channel;
5267 	bdev_io->internal.desc = desc;
5268 	bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH;
5269 	bdev_io->u.bdev.iovs = NULL;
5270 	bdev_io->u.bdev.iovcnt = 0;
5271 	bdev_io->u.bdev.offset_blocks = offset_blocks;
5272 	bdev_io->u.bdev.num_blocks = num_blocks;
5273 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5274 
5275 	bdev_io_submit(bdev_io);
5276 	return 0;
5277 }
5278 
5279 static void
5280 bdev_reset_dev(struct spdk_io_channel_iter *i, int status)
5281 {
5282 	struct spdk_bdev_channel *ch = spdk_io_channel_iter_get_ctx(i);
5283 	struct spdk_bdev_io *bdev_io;
5284 
5285 	bdev_io = TAILQ_FIRST(&ch->queued_resets);
5286 	TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
5287 	bdev_io_submit_reset(bdev_io);
5288 }
5289 
5290 static void
5291 bdev_reset_freeze_channel(struct spdk_io_channel_iter *i)
5292 {
5293 	struct spdk_io_channel		*ch;
5294 	struct spdk_bdev_channel	*channel;
5295 	struct spdk_bdev_mgmt_channel	*mgmt_channel;
5296 	struct spdk_bdev_shared_resource *shared_resource;
5297 	bdev_io_tailq_t			tmp_queued;
5298 
5299 	TAILQ_INIT(&tmp_queued);
5300 
5301 	ch = spdk_io_channel_iter_get_channel(i);
5302 	channel = spdk_io_channel_get_ctx(ch);
5303 	shared_resource = channel->shared_resource;
5304 	mgmt_channel = shared_resource->mgmt_ch;
5305 
5306 	channel->flags |= BDEV_CH_RESET_IN_PROGRESS;
5307 
5308 	if ((channel->flags & BDEV_CH_QOS_ENABLED) != 0) {
5309 		/* The QoS object is always valid and readable while
5310 		 * the channel flag is set, so the lock here should not
5311 		 * be necessary. We're not in the fast path though, so
5312 		 * just take it anyway. */
5313 		pthread_mutex_lock(&channel->bdev->internal.mutex);
5314 		if (channel->bdev->internal.qos->ch == channel) {
5315 			TAILQ_SWAP(&channel->bdev->internal.qos->queued, &tmp_queued, spdk_bdev_io, internal.link);
5316 		}
5317 		pthread_mutex_unlock(&channel->bdev->internal.mutex);
5318 	}
5319 
5320 	bdev_abort_all_queued_io(&shared_resource->nomem_io, channel);
5321 	bdev_abort_all_buf_io(&mgmt_channel->need_buf_small, channel);
5322 	bdev_abort_all_buf_io(&mgmt_channel->need_buf_large, channel);
5323 	bdev_abort_all_queued_io(&tmp_queued, channel);
5324 
5325 	spdk_for_each_channel_continue(i, 0);
5326 }
5327 
5328 static void
5329 bdev_start_reset(void *ctx)
5330 {
5331 	struct spdk_bdev_channel *ch = ctx;
5332 
5333 	spdk_for_each_channel(__bdev_to_io_dev(ch->bdev), bdev_reset_freeze_channel,
5334 			      ch, bdev_reset_dev);
5335 }
5336 
5337 static void
5338 bdev_channel_start_reset(struct spdk_bdev_channel *ch)
5339 {
5340 	struct spdk_bdev *bdev = ch->bdev;
5341 
5342 	assert(!TAILQ_EMPTY(&ch->queued_resets));
5343 
5344 	pthread_mutex_lock(&bdev->internal.mutex);
5345 	if (bdev->internal.reset_in_progress == NULL) {
5346 		bdev->internal.reset_in_progress = TAILQ_FIRST(&ch->queued_resets);
5347 		/*
5348 		 * Take a channel reference for the target bdev for the life of this
5349 		 *  reset.  This guards against the channel getting destroyed while
5350 		 *  spdk_for_each_channel() calls related to this reset IO are in
5351 		 *  progress.  We will release the reference when this reset is
5352 		 *  completed.
5353 		 */
5354 		bdev->internal.reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev));
5355 		bdev_start_reset(ch);
5356 	}
5357 	pthread_mutex_unlock(&bdev->internal.mutex);
5358 }
5359 
5360 int
5361 spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5362 		spdk_bdev_io_completion_cb cb, void *cb_arg)
5363 {
5364 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5365 	struct spdk_bdev_io *bdev_io;
5366 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5367 
5368 	bdev_io = bdev_channel_get_io(channel);
5369 	if (!bdev_io) {
5370 		return -ENOMEM;
5371 	}
5372 
5373 	bdev_io->internal.ch = channel;
5374 	bdev_io->internal.desc = desc;
5375 	bdev_io->internal.submit_tsc = spdk_get_ticks();
5376 	bdev_io->type = SPDK_BDEV_IO_TYPE_RESET;
5377 	bdev_io->u.reset.ch_ref = NULL;
5378 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5379 
5380 	pthread_mutex_lock(&bdev->internal.mutex);
5381 	TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, internal.link);
5382 	pthread_mutex_unlock(&bdev->internal.mutex);
5383 
5384 	TAILQ_INSERT_TAIL(&bdev_io->internal.ch->io_submitted, bdev_io,
5385 			  internal.ch_link);
5386 
5387 	bdev_channel_start_reset(channel);
5388 
5389 	return 0;
5390 }
5391 
5392 void
5393 spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
5394 		      struct spdk_bdev_io_stat *stat)
5395 {
5396 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5397 
5398 	*stat = channel->stat;
5399 }
5400 
5401 static void
5402 bdev_get_device_stat_done(struct spdk_io_channel_iter *i, int status)
5403 {
5404 	void *io_device = spdk_io_channel_iter_get_io_device(i);
5405 	struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i);
5406 
5407 	bdev_iostat_ctx->cb(__bdev_from_io_dev(io_device), bdev_iostat_ctx->stat,
5408 			    bdev_iostat_ctx->cb_arg, 0);
5409 	free(bdev_iostat_ctx);
5410 }
5411 
5412 static void
5413 bdev_get_each_channel_stat(struct spdk_io_channel_iter *i)
5414 {
5415 	struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = spdk_io_channel_iter_get_ctx(i);
5416 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
5417 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5418 
5419 	bdev_io_stat_add(bdev_iostat_ctx->stat, &channel->stat);
5420 	spdk_for_each_channel_continue(i, 0);
5421 }
5422 
5423 void
5424 spdk_bdev_get_device_stat(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat,
5425 			  spdk_bdev_get_device_stat_cb cb, void *cb_arg)
5426 {
5427 	struct spdk_bdev_iostat_ctx *bdev_iostat_ctx;
5428 
5429 	assert(bdev != NULL);
5430 	assert(stat != NULL);
5431 	assert(cb != NULL);
5432 
5433 	bdev_iostat_ctx = calloc(1, sizeof(struct spdk_bdev_iostat_ctx));
5434 	if (bdev_iostat_ctx == NULL) {
5435 		SPDK_ERRLOG("Unable to allocate memory for spdk_bdev_iostat_ctx\n");
5436 		cb(bdev, stat, cb_arg, -ENOMEM);
5437 		return;
5438 	}
5439 
5440 	bdev_iostat_ctx->stat = stat;
5441 	bdev_iostat_ctx->cb = cb;
5442 	bdev_iostat_ctx->cb_arg = cb_arg;
5443 
5444 	/* Start with the statistics from previously deleted channels. */
5445 	pthread_mutex_lock(&bdev->internal.mutex);
5446 	bdev_io_stat_add(bdev_iostat_ctx->stat, &bdev->internal.stat);
5447 	pthread_mutex_unlock(&bdev->internal.mutex);
5448 
5449 	/* Then iterate and add the statistics from each existing channel. */
5450 	spdk_for_each_channel(__bdev_to_io_dev(bdev),
5451 			      bdev_get_each_channel_stat,
5452 			      bdev_iostat_ctx,
5453 			      bdev_get_device_stat_done);
5454 }
5455 
5456 int
5457 spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5458 			      const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
5459 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
5460 {
5461 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5462 	struct spdk_bdev_io *bdev_io;
5463 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5464 
5465 	if (!desc->write) {
5466 		return -EBADF;
5467 	}
5468 
5469 	if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN))) {
5470 		return -ENOTSUP;
5471 	}
5472 
5473 	bdev_io = bdev_channel_get_io(channel);
5474 	if (!bdev_io) {
5475 		return -ENOMEM;
5476 	}
5477 
5478 	bdev_io->internal.ch = channel;
5479 	bdev_io->internal.desc = desc;
5480 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN;
5481 	bdev_io->u.nvme_passthru.cmd = *cmd;
5482 	bdev_io->u.nvme_passthru.buf = buf;
5483 	bdev_io->u.nvme_passthru.nbytes = nbytes;
5484 	bdev_io->u.nvme_passthru.md_buf = NULL;
5485 	bdev_io->u.nvme_passthru.md_len = 0;
5486 
5487 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5488 
5489 	bdev_io_submit(bdev_io);
5490 	return 0;
5491 }
5492 
5493 int
5494 spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5495 			   const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
5496 			   spdk_bdev_io_completion_cb cb, void *cb_arg)
5497 {
5498 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5499 	struct spdk_bdev_io *bdev_io;
5500 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5501 
5502 	if (!desc->write) {
5503 		/*
5504 		 * Do not try to parse the NVMe command - we could maybe use bits in the opcode
5505 		 *  to easily determine if the command is a read or write, but for now just
5506 		 *  do not allow io_passthru with a read-only descriptor.
5507 		 */
5508 		return -EBADF;
5509 	}
5510 
5511 	if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO))) {
5512 		return -ENOTSUP;
5513 	}
5514 
5515 	bdev_io = bdev_channel_get_io(channel);
5516 	if (!bdev_io) {
5517 		return -ENOMEM;
5518 	}
5519 
5520 	bdev_io->internal.ch = channel;
5521 	bdev_io->internal.desc = desc;
5522 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO;
5523 	bdev_io->u.nvme_passthru.cmd = *cmd;
5524 	bdev_io->u.nvme_passthru.buf = buf;
5525 	bdev_io->u.nvme_passthru.nbytes = nbytes;
5526 	bdev_io->u.nvme_passthru.md_buf = NULL;
5527 	bdev_io->u.nvme_passthru.md_len = 0;
5528 
5529 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5530 
5531 	bdev_io_submit(bdev_io);
5532 	return 0;
5533 }
5534 
5535 int
5536 spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5537 			      const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len,
5538 			      spdk_bdev_io_completion_cb cb, void *cb_arg)
5539 {
5540 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5541 	struct spdk_bdev_io *bdev_io;
5542 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5543 
5544 	if (!desc->write) {
5545 		/*
5546 		 * Do not try to parse the NVMe command - we could maybe use bits in the opcode
5547 		 *  to easily determine if the command is a read or write, but for now just
5548 		 *  do not allow io_passthru with a read-only descriptor.
5549 		 */
5550 		return -EBADF;
5551 	}
5552 
5553 	if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO_MD))) {
5554 		return -ENOTSUP;
5555 	}
5556 
5557 	bdev_io = bdev_channel_get_io(channel);
5558 	if (!bdev_io) {
5559 		return -ENOMEM;
5560 	}
5561 
5562 	bdev_io->internal.ch = channel;
5563 	bdev_io->internal.desc = desc;
5564 	bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD;
5565 	bdev_io->u.nvme_passthru.cmd = *cmd;
5566 	bdev_io->u.nvme_passthru.buf = buf;
5567 	bdev_io->u.nvme_passthru.nbytes = nbytes;
5568 	bdev_io->u.nvme_passthru.md_buf = md_buf;
5569 	bdev_io->u.nvme_passthru.md_len = md_len;
5570 
5571 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5572 
5573 	bdev_io_submit(bdev_io);
5574 	return 0;
5575 }
5576 
5577 static void bdev_abort_retry(void *ctx);
5578 static void bdev_abort(struct spdk_bdev_io *parent_io);
5579 
5580 static void
5581 bdev_abort_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
5582 {
5583 	struct spdk_bdev_channel *channel = bdev_io->internal.ch;
5584 	struct spdk_bdev_io *parent_io = cb_arg;
5585 	struct spdk_bdev_io *bio_to_abort, *tmp_io;
5586 
5587 	bio_to_abort = bdev_io->u.abort.bio_to_abort;
5588 
5589 	spdk_bdev_free_io(bdev_io);
5590 
5591 	if (!success) {
5592 		/* Check if the target I/O completed in the meantime. */
5593 		TAILQ_FOREACH(tmp_io, &channel->io_submitted, internal.ch_link) {
5594 			if (tmp_io == bio_to_abort) {
5595 				break;
5596 			}
5597 		}
5598 
5599 		/* If the target I/O still exists, set the parent to failed. */
5600 		if (tmp_io != NULL) {
5601 			parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
5602 		}
5603 	}
5604 
5605 	parent_io->u.bdev.split_outstanding--;
5606 	if (parent_io->u.bdev.split_outstanding == 0) {
5607 		if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
5608 			bdev_abort_retry(parent_io);
5609 		} else {
5610 			bdev_io_complete(parent_io);
5611 		}
5612 	}
5613 }
5614 
5615 static int
5616 bdev_abort_io(struct spdk_bdev_desc *desc, struct spdk_bdev_channel *channel,
5617 	      struct spdk_bdev_io *bio_to_abort,
5618 	      spdk_bdev_io_completion_cb cb, void *cb_arg)
5619 {
5620 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5621 	struct spdk_bdev_io *bdev_io;
5622 
5623 	if (bio_to_abort->type == SPDK_BDEV_IO_TYPE_ABORT ||
5624 	    bio_to_abort->type == SPDK_BDEV_IO_TYPE_RESET) {
5625 		/* TODO: Abort reset or abort request. */
5626 		return -ENOTSUP;
5627 	}
5628 
5629 	bdev_io = bdev_channel_get_io(channel);
5630 	if (bdev_io == NULL) {
5631 		return -ENOMEM;
5632 	}
5633 
5634 	bdev_io->internal.ch = channel;
5635 	bdev_io->internal.desc = desc;
5636 	bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT;
5637 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5638 
5639 	if (bdev->split_on_optimal_io_boundary && bdev_io_should_split(bio_to_abort)) {
5640 		bdev_io->u.bdev.abort.bio_cb_arg = bio_to_abort;
5641 
5642 		/* Parent abort request is not submitted directly, but to manage its
5643 		 * execution add it to the submitted list here.
5644 		 */
5645 		bdev_io->internal.submit_tsc = spdk_get_ticks();
5646 		TAILQ_INSERT_TAIL(&channel->io_submitted, bdev_io, internal.ch_link);
5647 
5648 		bdev_abort(bdev_io);
5649 
5650 		return 0;
5651 	}
5652 
5653 	bdev_io->u.abort.bio_to_abort = bio_to_abort;
5654 
5655 	/* Submit the abort request to the underlying bdev module. */
5656 	bdev_io_submit(bdev_io);
5657 
5658 	return 0;
5659 }
5660 
5661 static uint32_t
5662 _bdev_abort(struct spdk_bdev_io *parent_io)
5663 {
5664 	struct spdk_bdev_desc *desc = parent_io->internal.desc;
5665 	struct spdk_bdev_channel *channel = parent_io->internal.ch;
5666 	void *bio_cb_arg;
5667 	struct spdk_bdev_io *bio_to_abort;
5668 	uint32_t matched_ios;
5669 	int rc;
5670 
5671 	bio_cb_arg = parent_io->u.bdev.abort.bio_cb_arg;
5672 
5673 	/* matched_ios is returned and will be kept by the caller.
5674 	 *
5675 	 * This funcion will be used for two cases, 1) the same cb_arg is used for
5676 	 * multiple I/Os, 2) a single large I/O is split into smaller ones.
5677 	 * Incrementing split_outstanding directly here may confuse readers especially
5678 	 * for the 1st case.
5679 	 *
5680 	 * Completion of I/O abort is processed after stack unwinding. Hence this trick
5681 	 * works as expected.
5682 	 */
5683 	matched_ios = 0;
5684 	parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
5685 
5686 	TAILQ_FOREACH(bio_to_abort, &channel->io_submitted, internal.ch_link) {
5687 		if (bio_to_abort->internal.caller_ctx != bio_cb_arg) {
5688 			continue;
5689 		}
5690 
5691 		if (bio_to_abort->internal.submit_tsc > parent_io->internal.submit_tsc) {
5692 			/* Any I/O which was submitted after this abort command should be excluded. */
5693 			continue;
5694 		}
5695 
5696 		rc = bdev_abort_io(desc, channel, bio_to_abort, bdev_abort_io_done, parent_io);
5697 		if (rc != 0) {
5698 			if (rc == -ENOMEM) {
5699 				parent_io->internal.status = SPDK_BDEV_IO_STATUS_NOMEM;
5700 			} else {
5701 				parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
5702 			}
5703 			break;
5704 		}
5705 		matched_ios++;
5706 	}
5707 
5708 	return matched_ios;
5709 }
5710 
5711 static void
5712 bdev_abort_retry(void *ctx)
5713 {
5714 	struct spdk_bdev_io *parent_io = ctx;
5715 	uint32_t matched_ios;
5716 
5717 	matched_ios = _bdev_abort(parent_io);
5718 
5719 	if (matched_ios == 0) {
5720 		if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
5721 			bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry);
5722 		} else {
5723 			/* For retry, the case that no target I/O was found is success
5724 			 * because it means target I/Os completed in the meantime.
5725 			 */
5726 			bdev_io_complete(parent_io);
5727 		}
5728 		return;
5729 	}
5730 
5731 	/* Use split_outstanding to manage the progress of aborting I/Os. */
5732 	parent_io->u.bdev.split_outstanding = matched_ios;
5733 }
5734 
5735 static void
5736 bdev_abort(struct spdk_bdev_io *parent_io)
5737 {
5738 	uint32_t matched_ios;
5739 
5740 	matched_ios = _bdev_abort(parent_io);
5741 
5742 	if (matched_ios == 0) {
5743 		if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
5744 			bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry);
5745 		} else {
5746 			/* The case the no target I/O was found is failure. */
5747 			parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
5748 			bdev_io_complete(parent_io);
5749 		}
5750 		return;
5751 	}
5752 
5753 	/* Use split_outstanding to manage the progress of aborting I/Os. */
5754 	parent_io->u.bdev.split_outstanding = matched_ios;
5755 }
5756 
5757 int
5758 spdk_bdev_abort(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5759 		void *bio_cb_arg,
5760 		spdk_bdev_io_completion_cb cb, void *cb_arg)
5761 {
5762 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5763 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5764 	struct spdk_bdev_io *bdev_io;
5765 
5766 	if (bio_cb_arg == NULL) {
5767 		return -EINVAL;
5768 	}
5769 
5770 	if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT)) {
5771 		return -ENOTSUP;
5772 	}
5773 
5774 	bdev_io = bdev_channel_get_io(channel);
5775 	if (bdev_io == NULL) {
5776 		return -ENOMEM;
5777 	}
5778 
5779 	bdev_io->internal.ch = channel;
5780 	bdev_io->internal.desc = desc;
5781 	bdev_io->internal.submit_tsc = spdk_get_ticks();
5782 	bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT;
5783 	bdev_io_init(bdev_io, bdev, cb_arg, cb);
5784 
5785 	bdev_io->u.bdev.abort.bio_cb_arg = bio_cb_arg;
5786 
5787 	/* Parent abort request is not submitted directly, but to manage its execution,
5788 	 * add it to the submitted list here.
5789 	 */
5790 	TAILQ_INSERT_TAIL(&channel->io_submitted, bdev_io, internal.ch_link);
5791 
5792 	bdev_abort(bdev_io);
5793 
5794 	return 0;
5795 }
5796 
5797 int
5798 spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
5799 			struct spdk_bdev_io_wait_entry *entry)
5800 {
5801 	struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
5802 	struct spdk_bdev_mgmt_channel *mgmt_ch = channel->shared_resource->mgmt_ch;
5803 
5804 	if (bdev != entry->bdev) {
5805 		SPDK_ERRLOG("bdevs do not match\n");
5806 		return -EINVAL;
5807 	}
5808 
5809 	if (mgmt_ch->per_thread_cache_count > 0) {
5810 		SPDK_ERRLOG("Cannot queue io_wait if spdk_bdev_io available in per-thread cache\n");
5811 		return -EINVAL;
5812 	}
5813 
5814 	TAILQ_INSERT_TAIL(&mgmt_ch->io_wait_queue, entry, link);
5815 	return 0;
5816 }
5817 
5818 static inline void
5819 bdev_io_complete(void *ctx)
5820 {
5821 	struct spdk_bdev_io *bdev_io = ctx;
5822 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
5823 	uint64_t tsc, tsc_diff;
5824 
5825 	if (spdk_unlikely(bdev_io->internal.in_submit_request || bdev_io->internal.io_submit_ch)) {
5826 		/*
5827 		 * Send the completion to the thread that originally submitted the I/O,
5828 		 * which may not be the current thread in the case of QoS.
5829 		 */
5830 		if (bdev_io->internal.io_submit_ch) {
5831 			bdev_io->internal.ch = bdev_io->internal.io_submit_ch;
5832 			bdev_io->internal.io_submit_ch = NULL;
5833 		}
5834 
5835 		/*
5836 		 * Defer completion to avoid potential infinite recursion if the
5837 		 * user's completion callback issues a new I/O.
5838 		 */
5839 		spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
5840 				     bdev_io_complete, bdev_io);
5841 		return;
5842 	}
5843 
5844 	tsc = spdk_get_ticks();
5845 	tsc_diff = tsc - bdev_io->internal.submit_tsc;
5846 	spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_DONE, 0, 0, (uintptr_t)bdev_io,
5847 			      bdev_io->internal.caller_ctx);
5848 
5849 	TAILQ_REMOVE(&bdev_ch->io_submitted, bdev_io, internal.ch_link);
5850 
5851 	if (bdev_io->internal.ch->histogram) {
5852 		spdk_histogram_data_tally(bdev_io->internal.ch->histogram, tsc_diff);
5853 	}
5854 
5855 	if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
5856 		switch (bdev_io->type) {
5857 		case SPDK_BDEV_IO_TYPE_READ:
5858 			bdev_io->internal.ch->stat.bytes_read += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
5859 			bdev_io->internal.ch->stat.num_read_ops++;
5860 			bdev_io->internal.ch->stat.read_latency_ticks += tsc_diff;
5861 			break;
5862 		case SPDK_BDEV_IO_TYPE_WRITE:
5863 			bdev_io->internal.ch->stat.bytes_written += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
5864 			bdev_io->internal.ch->stat.num_write_ops++;
5865 			bdev_io->internal.ch->stat.write_latency_ticks += tsc_diff;
5866 			break;
5867 		case SPDK_BDEV_IO_TYPE_UNMAP:
5868 			bdev_io->internal.ch->stat.bytes_unmapped += bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
5869 			bdev_io->internal.ch->stat.num_unmap_ops++;
5870 			bdev_io->internal.ch->stat.unmap_latency_ticks += tsc_diff;
5871 			break;
5872 		case SPDK_BDEV_IO_TYPE_ZCOPY:
5873 			/* Track the data in the start phase only */
5874 			if (bdev_io->u.bdev.zcopy.start) {
5875 				if (bdev_io->u.bdev.zcopy.populate) {
5876 					bdev_io->internal.ch->stat.bytes_read +=
5877 						bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
5878 					bdev_io->internal.ch->stat.num_read_ops++;
5879 					bdev_io->internal.ch->stat.read_latency_ticks += tsc_diff;
5880 				} else {
5881 					bdev_io->internal.ch->stat.bytes_written +=
5882 						bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
5883 					bdev_io->internal.ch->stat.num_write_ops++;
5884 					bdev_io->internal.ch->stat.write_latency_ticks += tsc_diff;
5885 				}
5886 			}
5887 			break;
5888 		default:
5889 			break;
5890 		}
5891 	}
5892 
5893 #ifdef SPDK_CONFIG_VTUNE
5894 	uint64_t now_tsc = spdk_get_ticks();
5895 	if (now_tsc > (bdev_io->internal.ch->start_tsc + bdev_io->internal.ch->interval_tsc)) {
5896 		uint64_t data[5];
5897 
5898 		data[0] = bdev_io->internal.ch->stat.num_read_ops - bdev_io->internal.ch->prev_stat.num_read_ops;
5899 		data[1] = bdev_io->internal.ch->stat.bytes_read - bdev_io->internal.ch->prev_stat.bytes_read;
5900 		data[2] = bdev_io->internal.ch->stat.num_write_ops - bdev_io->internal.ch->prev_stat.num_write_ops;
5901 		data[3] = bdev_io->internal.ch->stat.bytes_written - bdev_io->internal.ch->prev_stat.bytes_written;
5902 		data[4] = bdev_io->bdev->fn_table->get_spin_time ?
5903 			  bdev_io->bdev->fn_table->get_spin_time(spdk_bdev_io_get_io_channel(bdev_io)) : 0;
5904 
5905 		__itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->internal.ch->handle,
5906 				   __itt_metadata_u64, 5, data);
5907 
5908 		bdev_io->internal.ch->prev_stat = bdev_io->internal.ch->stat;
5909 		bdev_io->internal.ch->start_tsc = now_tsc;
5910 	}
5911 #endif
5912 
5913 	assert(bdev_io->internal.cb != NULL);
5914 	assert(spdk_get_thread() == spdk_bdev_io_get_thread(bdev_io));
5915 
5916 	bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
5917 			     bdev_io->internal.caller_ctx);
5918 }
5919 
5920 static void bdev_destroy_cb(void *io_device);
5921 
5922 static void
5923 bdev_reset_complete(struct spdk_io_channel_iter *i, int status)
5924 {
5925 	struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i);
5926 	struct spdk_bdev *bdev = bdev_io->bdev;
5927 
5928 	if (bdev_io->u.reset.ch_ref != NULL) {
5929 		spdk_put_io_channel(bdev_io->u.reset.ch_ref);
5930 		bdev_io->u.reset.ch_ref = NULL;
5931 	}
5932 
5933 	bdev_io_complete(bdev_io);
5934 
5935 	if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING &&
5936 	    TAILQ_EMPTY(&bdev->internal.open_descs)) {
5937 		spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
5938 	}
5939 }
5940 
5941 static void
5942 bdev_unfreeze_channel(struct spdk_io_channel_iter *i)
5943 {
5944 	struct spdk_bdev_io *bdev_io = spdk_io_channel_iter_get_ctx(i);
5945 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
5946 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
5947 	struct spdk_bdev_io *queued_reset;
5948 
5949 	ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS;
5950 	while (!TAILQ_EMPTY(&ch->queued_resets)) {
5951 		queued_reset = TAILQ_FIRST(&ch->queued_resets);
5952 		TAILQ_REMOVE(&ch->queued_resets, queued_reset, internal.link);
5953 		spdk_bdev_io_complete(queued_reset, bdev_io->internal.status);
5954 	}
5955 
5956 	spdk_for_each_channel_continue(i, 0);
5957 }
5958 
5959 void
5960 spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
5961 {
5962 	struct spdk_bdev *bdev = bdev_io->bdev;
5963 	struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
5964 	struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
5965 
5966 	bdev_io->internal.status = status;
5967 
5968 	if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) {
5969 		bool unlock_channels = false;
5970 
5971 		if (status == SPDK_BDEV_IO_STATUS_NOMEM) {
5972 			SPDK_ERRLOG("NOMEM returned for reset\n");
5973 		}
5974 		pthread_mutex_lock(&bdev->internal.mutex);
5975 		if (bdev_io == bdev->internal.reset_in_progress) {
5976 			bdev->internal.reset_in_progress = NULL;
5977 			unlock_channels = true;
5978 		}
5979 		pthread_mutex_unlock(&bdev->internal.mutex);
5980 
5981 		if (unlock_channels) {
5982 			spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_unfreeze_channel,
5983 					      bdev_io, bdev_reset_complete);
5984 			return;
5985 		}
5986 	} else {
5987 		if (spdk_unlikely(bdev_io->internal.orig_iovcnt != 0)) {
5988 			_bdev_io_push_bounce_data_buffer(bdev_io, _bdev_io_complete_push_bounce_done);
5989 			/* bdev IO will be completed in the callback */
5990 			return;
5991 		}
5992 
5993 		_bdev_io_decrement_outstanding(bdev_ch, shared_resource);
5994 		if (spdk_unlikely(_bdev_io_handle_no_mem(bdev_io))) {
5995 			return;
5996 		}
5997 	}
5998 
5999 	bdev_io_complete(bdev_io);
6000 }
6001 
6002 void
6003 spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc,
6004 				  enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq)
6005 {
6006 	if (sc == SPDK_SCSI_STATUS_GOOD) {
6007 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
6008 	} else {
6009 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SCSI_ERROR;
6010 		bdev_io->internal.error.scsi.sc = sc;
6011 		bdev_io->internal.error.scsi.sk = sk;
6012 		bdev_io->internal.error.scsi.asc = asc;
6013 		bdev_io->internal.error.scsi.ascq = ascq;
6014 	}
6015 
6016 	spdk_bdev_io_complete(bdev_io, bdev_io->internal.status);
6017 }
6018 
6019 void
6020 spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io,
6021 			     int *sc, int *sk, int *asc, int *ascq)
6022 {
6023 	assert(sc != NULL);
6024 	assert(sk != NULL);
6025 	assert(asc != NULL);
6026 	assert(ascq != NULL);
6027 
6028 	switch (bdev_io->internal.status) {
6029 	case SPDK_BDEV_IO_STATUS_SUCCESS:
6030 		*sc = SPDK_SCSI_STATUS_GOOD;
6031 		*sk = SPDK_SCSI_SENSE_NO_SENSE;
6032 		*asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
6033 		*ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
6034 		break;
6035 	case SPDK_BDEV_IO_STATUS_NVME_ERROR:
6036 		spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq);
6037 		break;
6038 	case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
6039 		*sc = bdev_io->internal.error.scsi.sc;
6040 		*sk = bdev_io->internal.error.scsi.sk;
6041 		*asc = bdev_io->internal.error.scsi.asc;
6042 		*ascq = bdev_io->internal.error.scsi.ascq;
6043 		break;
6044 	default:
6045 		*sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
6046 		*sk = SPDK_SCSI_SENSE_ABORTED_COMMAND;
6047 		*asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
6048 		*ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
6049 		break;
6050 	}
6051 }
6052 
6053 void
6054 spdk_bdev_io_complete_aio_status(struct spdk_bdev_io *bdev_io, int aio_result)
6055 {
6056 	if (aio_result == 0) {
6057 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
6058 	} else {
6059 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_AIO_ERROR;
6060 	}
6061 
6062 	bdev_io->internal.error.aio_result = aio_result;
6063 
6064 	spdk_bdev_io_complete(bdev_io, bdev_io->internal.status);
6065 }
6066 
6067 void
6068 spdk_bdev_io_get_aio_status(const struct spdk_bdev_io *bdev_io, int *aio_result)
6069 {
6070 	assert(aio_result != NULL);
6071 
6072 	if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_AIO_ERROR) {
6073 		*aio_result = bdev_io->internal.error.aio_result;
6074 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
6075 		*aio_result = 0;
6076 	} else {
6077 		*aio_result = -EIO;
6078 	}
6079 }
6080 
6081 void
6082 spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, uint32_t cdw0, int sct, int sc)
6083 {
6084 	if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS) {
6085 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
6086 	} else if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_ABORTED_BY_REQUEST) {
6087 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_ABORTED;
6088 	} else {
6089 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_NVME_ERROR;
6090 	}
6091 
6092 	bdev_io->internal.error.nvme.cdw0 = cdw0;
6093 	bdev_io->internal.error.nvme.sct = sct;
6094 	bdev_io->internal.error.nvme.sc = sc;
6095 
6096 	spdk_bdev_io_complete(bdev_io, bdev_io->internal.status);
6097 }
6098 
6099 void
6100 spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0, int *sct, int *sc)
6101 {
6102 	assert(sct != NULL);
6103 	assert(sc != NULL);
6104 	assert(cdw0 != NULL);
6105 
6106 	if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) {
6107 		*sct = SPDK_NVME_SCT_GENERIC;
6108 		*sc = SPDK_NVME_SC_SUCCESS;
6109 		if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
6110 			*cdw0 = 0;
6111 		} else {
6112 			*cdw0 = 1U;
6113 		}
6114 		return;
6115 	}
6116 
6117 	if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
6118 		*sct = bdev_io->internal.error.nvme.sct;
6119 		*sc = bdev_io->internal.error.nvme.sc;
6120 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
6121 		*sct = SPDK_NVME_SCT_GENERIC;
6122 		*sc = SPDK_NVME_SC_SUCCESS;
6123 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) {
6124 		*sct = SPDK_NVME_SCT_GENERIC;
6125 		*sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
6126 	} else {
6127 		*sct = SPDK_NVME_SCT_GENERIC;
6128 		*sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
6129 	}
6130 
6131 	*cdw0 = bdev_io->internal.error.nvme.cdw0;
6132 }
6133 
6134 void
6135 spdk_bdev_io_get_nvme_fused_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0,
6136 				   int *first_sct, int *first_sc, int *second_sct, int *second_sc)
6137 {
6138 	assert(first_sct != NULL);
6139 	assert(first_sc != NULL);
6140 	assert(second_sct != NULL);
6141 	assert(second_sc != NULL);
6142 	assert(cdw0 != NULL);
6143 
6144 	if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
6145 		if (bdev_io->internal.error.nvme.sct == SPDK_NVME_SCT_MEDIA_ERROR &&
6146 		    bdev_io->internal.error.nvme.sc == SPDK_NVME_SC_COMPARE_FAILURE) {
6147 			*first_sct = bdev_io->internal.error.nvme.sct;
6148 			*first_sc = bdev_io->internal.error.nvme.sc;
6149 			*second_sct = SPDK_NVME_SCT_GENERIC;
6150 			*second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
6151 		} else {
6152 			*first_sct = SPDK_NVME_SCT_GENERIC;
6153 			*first_sc = SPDK_NVME_SC_SUCCESS;
6154 			*second_sct = bdev_io->internal.error.nvme.sct;
6155 			*second_sc = bdev_io->internal.error.nvme.sc;
6156 		}
6157 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) {
6158 		*first_sct = SPDK_NVME_SCT_GENERIC;
6159 		*first_sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
6160 		*second_sct = SPDK_NVME_SCT_GENERIC;
6161 		*second_sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
6162 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
6163 		*first_sct = SPDK_NVME_SCT_GENERIC;
6164 		*first_sc = SPDK_NVME_SC_SUCCESS;
6165 		*second_sct = SPDK_NVME_SCT_GENERIC;
6166 		*second_sc = SPDK_NVME_SC_SUCCESS;
6167 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED) {
6168 		*first_sct = SPDK_NVME_SCT_GENERIC;
6169 		*first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
6170 		*second_sct = SPDK_NVME_SCT_GENERIC;
6171 		*second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
6172 	} else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_MISCOMPARE) {
6173 		*first_sct = SPDK_NVME_SCT_MEDIA_ERROR;
6174 		*first_sc = SPDK_NVME_SC_COMPARE_FAILURE;
6175 		*second_sct = SPDK_NVME_SCT_GENERIC;
6176 		*second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
6177 	} else {
6178 		*first_sct = SPDK_NVME_SCT_GENERIC;
6179 		*first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
6180 		*second_sct = SPDK_NVME_SCT_GENERIC;
6181 		*second_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
6182 	}
6183 
6184 	*cdw0 = bdev_io->internal.error.nvme.cdw0;
6185 }
6186 
6187 struct spdk_thread *
6188 spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io)
6189 {
6190 	return spdk_io_channel_get_thread(bdev_io->internal.ch->channel);
6191 }
6192 
6193 struct spdk_io_channel *
6194 spdk_bdev_io_get_io_channel(struct spdk_bdev_io *bdev_io)
6195 {
6196 	return bdev_io->internal.ch->channel;
6197 }
6198 
6199 static int
6200 bdev_register(struct spdk_bdev *bdev)
6201 {
6202 	char *bdev_name;
6203 	char uuid[SPDK_UUID_STRING_LEN];
6204 	int ret;
6205 
6206 	assert(bdev->module != NULL);
6207 
6208 	if (!bdev->name) {
6209 		SPDK_ERRLOG("Bdev name is NULL\n");
6210 		return -EINVAL;
6211 	}
6212 
6213 	if (!strlen(bdev->name)) {
6214 		SPDK_ERRLOG("Bdev name must not be an empty string\n");
6215 		return -EINVAL;
6216 	}
6217 
6218 	/* Users often register their own I/O devices using the bdev name. In
6219 	 * order to avoid conflicts, prepend bdev_. */
6220 	bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name);
6221 	if (!bdev_name) {
6222 		SPDK_ERRLOG("Unable to allocate memory for internal bdev name.\n");
6223 		return -ENOMEM;
6224 	}
6225 
6226 	bdev->internal.status = SPDK_BDEV_STATUS_READY;
6227 	bdev->internal.measured_queue_depth = UINT64_MAX;
6228 	bdev->internal.claim_module = NULL;
6229 	bdev->internal.qd_poller = NULL;
6230 	bdev->internal.qos = NULL;
6231 
6232 	TAILQ_INIT(&bdev->internal.open_descs);
6233 	TAILQ_INIT(&bdev->internal.locked_ranges);
6234 	TAILQ_INIT(&bdev->internal.pending_locked_ranges);
6235 	TAILQ_INIT(&bdev->aliases);
6236 
6237 	ret = bdev_name_add(&bdev->internal.bdev_name, bdev, bdev->name);
6238 	if (ret != 0) {
6239 		free(bdev_name);
6240 		return ret;
6241 	}
6242 
6243 	/* If the user didn't specify a uuid, generate one. */
6244 	if (spdk_mem_all_zero(&bdev->uuid, sizeof(bdev->uuid))) {
6245 		spdk_uuid_generate(&bdev->uuid);
6246 	}
6247 
6248 	/* Add the UUID alias only if it's different than the name */
6249 	spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid);
6250 	if (strcmp(bdev->name, uuid) != 0) {
6251 		ret = spdk_bdev_alias_add(bdev, uuid);
6252 		if (ret != 0) {
6253 			SPDK_ERRLOG("Unable to add uuid:%s alias for bdev %s\n", uuid, bdev->name);
6254 			bdev_name_del(&bdev->internal.bdev_name);
6255 			free(bdev_name);
6256 			return ret;
6257 		}
6258 	}
6259 
6260 	if (spdk_bdev_get_buf_align(bdev) > 1) {
6261 		if (bdev->split_on_optimal_io_boundary) {
6262 			bdev->optimal_io_boundary = spdk_min(bdev->optimal_io_boundary,
6263 							     SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen);
6264 		} else {
6265 			bdev->split_on_optimal_io_boundary = true;
6266 			bdev->optimal_io_boundary = SPDK_BDEV_LARGE_BUF_MAX_SIZE / bdev->blocklen;
6267 		}
6268 	}
6269 
6270 	/* If the user didn't specify a write unit size, set it to one. */
6271 	if (bdev->write_unit_size == 0) {
6272 		bdev->write_unit_size = 1;
6273 	}
6274 
6275 	/* Set ACWU value to 1 if bdev module did not set it (does not support it natively) */
6276 	if (bdev->acwu == 0) {
6277 		bdev->acwu = 1;
6278 	}
6279 
6280 	if (bdev->phys_blocklen == 0) {
6281 		bdev->phys_blocklen = spdk_bdev_get_data_block_size(bdev);
6282 	}
6283 
6284 	bdev->internal.reset_in_progress = NULL;
6285 	bdev->internal.qd_poll_in_progress = false;
6286 	bdev->internal.period = 0;
6287 	bdev->internal.new_period = 0;
6288 
6289 	spdk_io_device_register(__bdev_to_io_dev(bdev),
6290 				bdev_channel_create, bdev_channel_destroy,
6291 				sizeof(struct spdk_bdev_channel),
6292 				bdev_name);
6293 
6294 	free(bdev_name);
6295 
6296 	pthread_mutex_init(&bdev->internal.mutex, NULL);
6297 
6298 	SPDK_DEBUGLOG(bdev, "Inserting bdev %s into list\n", bdev->name);
6299 	TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, internal.link);
6300 
6301 	return 0;
6302 }
6303 
6304 static void
6305 bdev_destroy_cb(void *io_device)
6306 {
6307 	int			rc;
6308 	struct spdk_bdev	*bdev;
6309 	spdk_bdev_unregister_cb	cb_fn;
6310 	void			*cb_arg;
6311 
6312 	bdev = __bdev_from_io_dev(io_device);
6313 	cb_fn = bdev->internal.unregister_cb;
6314 	cb_arg = bdev->internal.unregister_ctx;
6315 
6316 	pthread_mutex_destroy(&bdev->internal.mutex);
6317 	free(bdev->internal.qos);
6318 
6319 	rc = bdev->fn_table->destruct(bdev->ctxt);
6320 	if (rc < 0) {
6321 		SPDK_ERRLOG("destruct failed\n");
6322 	}
6323 	if (rc <= 0 && cb_fn != NULL) {
6324 		cb_fn(cb_arg, rc);
6325 	}
6326 }
6327 
6328 void
6329 spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
6330 {
6331 	if (bdev->internal.unregister_cb != NULL) {
6332 		bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno);
6333 	}
6334 }
6335 
6336 static void
6337 _remove_notify(void *arg)
6338 {
6339 	struct spdk_bdev_desc *desc = arg;
6340 
6341 	pthread_mutex_lock(&desc->mutex);
6342 	desc->refs--;
6343 
6344 	if (!desc->closed) {
6345 		pthread_mutex_unlock(&desc->mutex);
6346 		desc->callback.event_fn(SPDK_BDEV_EVENT_REMOVE, desc->bdev, desc->callback.ctx);
6347 		return;
6348 	} else if (0 == desc->refs) {
6349 		/* This descriptor was closed after this remove_notify message was sent.
6350 		 * spdk_bdev_close() could not free the descriptor since this message was
6351 		 * in flight, so we free it now using bdev_desc_free().
6352 		 */
6353 		pthread_mutex_unlock(&desc->mutex);
6354 		bdev_desc_free(desc);
6355 		return;
6356 	}
6357 	pthread_mutex_unlock(&desc->mutex);
6358 }
6359 
6360 /* Must be called while holding g_bdev_mgr.mutex and bdev->internal.mutex.
6361  * returns: 0 - bdev removed and ready to be destructed.
6362  *          -EBUSY - bdev can't be destructed yet.  */
6363 static int
6364 bdev_unregister_unsafe(struct spdk_bdev *bdev)
6365 {
6366 	struct spdk_bdev_desc	*desc, *tmp;
6367 	int			rc = 0;
6368 	char			uuid[SPDK_UUID_STRING_LEN];
6369 
6370 	/* Notify each descriptor about hotremoval */
6371 	TAILQ_FOREACH_SAFE(desc, &bdev->internal.open_descs, link, tmp) {
6372 		rc = -EBUSY;
6373 		pthread_mutex_lock(&desc->mutex);
6374 		/*
6375 		 * Defer invocation of the event_cb to a separate message that will
6376 		 *  run later on its thread.  This ensures this context unwinds and
6377 		 *  we don't recursively unregister this bdev again if the event_cb
6378 		 *  immediately closes its descriptor.
6379 		 */
6380 		desc->refs++;
6381 		spdk_thread_send_msg(desc->thread, _remove_notify, desc);
6382 		pthread_mutex_unlock(&desc->mutex);
6383 	}
6384 
6385 	/* If there are no descriptors, proceed removing the bdev */
6386 	if (rc == 0) {
6387 		TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
6388 		SPDK_DEBUGLOG(bdev, "Removing bdev %s from list done\n", bdev->name);
6389 
6390 		/* Delete the name and the UUID alias */
6391 		spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid);
6392 		bdev_name_del_unsafe(&bdev->internal.bdev_name);
6393 		bdev_alias_del(bdev, uuid, bdev_name_del_unsafe);
6394 
6395 		spdk_notify_send("bdev_unregister", spdk_bdev_get_name(bdev));
6396 
6397 		if (bdev->internal.reset_in_progress != NULL) {
6398 			/* If reset is in progress, let the completion callback for reset
6399 			 * unregister the bdev.
6400 			 */
6401 			rc = -EBUSY;
6402 		}
6403 	}
6404 
6405 	return rc;
6406 }
6407 
6408 static void
6409 bdev_unregister_abort_channel(struct spdk_io_channel_iter *i)
6410 {
6411 	struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i);
6412 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(io_ch);
6413 
6414 	bdev_channel_abort_queued_ios(bdev_ch);
6415 	spdk_for_each_channel_continue(i, 0);
6416 }
6417 
6418 static void
6419 bdev_unregister(struct spdk_io_channel_iter *i, int status)
6420 {
6421 	struct spdk_bdev *bdev = spdk_io_channel_iter_get_ctx(i);
6422 	int rc;
6423 
6424 	pthread_mutex_lock(&g_bdev_mgr.mutex);
6425 	pthread_mutex_lock(&bdev->internal.mutex);
6426 	/*
6427 	 * Set the status to REMOVING after completing to abort channels. Otherwise,
6428 	 * the last spdk_bdev_close() may call spdk_io_device_unregister() while
6429 	 * spdk_for_each_channel() is executed and spdk_io_device_unregister() may fail.
6430 	 */
6431 	bdev->internal.status = SPDK_BDEV_STATUS_REMOVING;
6432 	rc = bdev_unregister_unsafe(bdev);
6433 	pthread_mutex_unlock(&bdev->internal.mutex);
6434 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
6435 
6436 	if (rc == 0) {
6437 		spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
6438 	}
6439 }
6440 
6441 void
6442 spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
6443 {
6444 	struct spdk_thread	*thread;
6445 
6446 	SPDK_DEBUGLOG(bdev, "Removing bdev %s from list\n", bdev->name);
6447 
6448 	thread = spdk_get_thread();
6449 	if (!thread) {
6450 		/* The user called this from a non-SPDK thread. */
6451 		if (cb_fn != NULL) {
6452 			cb_fn(cb_arg, -ENOTSUP);
6453 		}
6454 		return;
6455 	}
6456 
6457 	pthread_mutex_lock(&g_bdev_mgr.mutex);
6458 	if (bdev->internal.status == SPDK_BDEV_STATUS_UNREGISTERING ||
6459 	    bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
6460 		pthread_mutex_unlock(&g_bdev_mgr.mutex);
6461 		if (cb_fn) {
6462 			cb_fn(cb_arg, -EBUSY);
6463 		}
6464 		return;
6465 	}
6466 
6467 	pthread_mutex_lock(&bdev->internal.mutex);
6468 	bdev->internal.status = SPDK_BDEV_STATUS_UNREGISTERING;
6469 	bdev->internal.unregister_cb = cb_fn;
6470 	bdev->internal.unregister_ctx = cb_arg;
6471 	pthread_mutex_unlock(&bdev->internal.mutex);
6472 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
6473 
6474 	spdk_bdev_set_qd_sampling_period(bdev, 0);
6475 
6476 	spdk_for_each_channel(__bdev_to_io_dev(bdev),
6477 			      bdev_unregister_abort_channel,
6478 			      bdev,
6479 			      bdev_unregister);
6480 }
6481 
6482 int
6483 spdk_bdev_unregister_by_name(const char *bdev_name, struct spdk_bdev_module *module,
6484 			     spdk_bdev_unregister_cb cb_fn, void *cb_arg)
6485 {
6486 	struct spdk_bdev_desc *desc;
6487 	struct spdk_bdev *bdev;
6488 	int rc;
6489 
6490 	rc = spdk_bdev_open_ext(bdev_name, false, _tmp_bdev_event_cb, NULL, &desc);
6491 	if (rc != 0) {
6492 		SPDK_ERRLOG("Failed to open bdev with name: %s\n", bdev_name);
6493 		return rc;
6494 	}
6495 
6496 	bdev = spdk_bdev_desc_get_bdev(desc);
6497 
6498 	if (bdev->module != module) {
6499 		spdk_bdev_close(desc);
6500 		SPDK_ERRLOG("Bdev %s was not registered by the specified module.\n",
6501 			    bdev_name);
6502 		return -ENODEV;
6503 	}
6504 
6505 	spdk_bdev_unregister(bdev, cb_fn, cb_arg);
6506 
6507 	spdk_bdev_close(desc);
6508 
6509 	return 0;
6510 }
6511 
6512 static int
6513 bdev_start_qos(struct spdk_bdev *bdev)
6514 {
6515 	struct set_qos_limit_ctx *ctx;
6516 
6517 	/* Enable QoS */
6518 	if (bdev->internal.qos && bdev->internal.qos->thread == NULL) {
6519 		ctx = calloc(1, sizeof(*ctx));
6520 		if (ctx == NULL) {
6521 			SPDK_ERRLOG("Failed to allocate memory for QoS context\n");
6522 			return -ENOMEM;
6523 		}
6524 		ctx->bdev = bdev;
6525 		spdk_for_each_channel(__bdev_to_io_dev(bdev),
6526 				      bdev_enable_qos_msg, ctx,
6527 				      bdev_enable_qos_done);
6528 	}
6529 
6530 	return 0;
6531 }
6532 
6533 static int
6534 bdev_open(struct spdk_bdev *bdev, bool write, struct spdk_bdev_desc *desc)
6535 {
6536 	struct spdk_thread *thread;
6537 	int rc = 0;
6538 
6539 	thread = spdk_get_thread();
6540 	if (!thread) {
6541 		SPDK_ERRLOG("Cannot open bdev from non-SPDK thread.\n");
6542 		return -ENOTSUP;
6543 	}
6544 
6545 	SPDK_DEBUGLOG(bdev, "Opening descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
6546 		      spdk_get_thread());
6547 
6548 	desc->bdev = bdev;
6549 	desc->thread = thread;
6550 	desc->write = write;
6551 
6552 	pthread_mutex_lock(&bdev->internal.mutex);
6553 	if (bdev->internal.status == SPDK_BDEV_STATUS_UNREGISTERING ||
6554 	    bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
6555 		pthread_mutex_unlock(&bdev->internal.mutex);
6556 		return -ENODEV;
6557 	}
6558 
6559 	if (write && bdev->internal.claim_module) {
6560 		SPDK_ERRLOG("Could not open %s - %s module already claimed it\n",
6561 			    bdev->name, bdev->internal.claim_module->name);
6562 		pthread_mutex_unlock(&bdev->internal.mutex);
6563 		return -EPERM;
6564 	}
6565 
6566 	rc = bdev_start_qos(bdev);
6567 	if (rc != 0) {
6568 		SPDK_ERRLOG("Failed to start QoS on bdev %s\n", bdev->name);
6569 		pthread_mutex_unlock(&bdev->internal.mutex);
6570 		return rc;
6571 	}
6572 
6573 	TAILQ_INSERT_TAIL(&bdev->internal.open_descs, desc, link);
6574 
6575 	pthread_mutex_unlock(&bdev->internal.mutex);
6576 
6577 	return 0;
6578 }
6579 
6580 static int
6581 bdev_desc_alloc(struct spdk_bdev *bdev, spdk_bdev_event_cb_t event_cb, void *event_ctx,
6582 		struct spdk_bdev_desc **_desc)
6583 {
6584 	struct spdk_bdev_desc *desc;
6585 	unsigned int event_id;
6586 
6587 	desc = calloc(1, sizeof(*desc));
6588 	if (desc == NULL) {
6589 		SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n");
6590 		return -ENOMEM;
6591 	}
6592 
6593 	TAILQ_INIT(&desc->pending_media_events);
6594 	TAILQ_INIT(&desc->free_media_events);
6595 
6596 	desc->memory_domains_supported = spdk_bdev_get_memory_domains(bdev, NULL, 0) > 0;
6597 	desc->callback.event_fn = event_cb;
6598 	desc->callback.ctx = event_ctx;
6599 	pthread_mutex_init(&desc->mutex, NULL);
6600 
6601 	if (bdev->media_events) {
6602 		desc->media_events_buffer = calloc(MEDIA_EVENT_POOL_SIZE,
6603 						   sizeof(*desc->media_events_buffer));
6604 		if (desc->media_events_buffer == NULL) {
6605 			SPDK_ERRLOG("Failed to initialize media event pool\n");
6606 			bdev_desc_free(desc);
6607 			return -ENOMEM;
6608 		}
6609 
6610 		for (event_id = 0; event_id < MEDIA_EVENT_POOL_SIZE; ++event_id) {
6611 			TAILQ_INSERT_TAIL(&desc->free_media_events,
6612 					  &desc->media_events_buffer[event_id], tailq);
6613 		}
6614 	}
6615 
6616 	*_desc = desc;
6617 
6618 	return 0;
6619 }
6620 
6621 int
6622 spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
6623 		   void *event_ctx, struct spdk_bdev_desc **_desc)
6624 {
6625 	struct spdk_bdev_desc *desc;
6626 	struct spdk_bdev *bdev;
6627 	int rc;
6628 
6629 	if (event_cb == NULL) {
6630 		SPDK_ERRLOG("Missing event callback function\n");
6631 		return -EINVAL;
6632 	}
6633 
6634 	pthread_mutex_lock(&g_bdev_mgr.mutex);
6635 
6636 	bdev = bdev_get_by_name(bdev_name);
6637 
6638 	if (bdev == NULL) {
6639 		SPDK_NOTICELOG("Currently unable to find bdev with name: %s\n", bdev_name);
6640 		pthread_mutex_unlock(&g_bdev_mgr.mutex);
6641 		return -ENODEV;
6642 	}
6643 
6644 	rc = bdev_desc_alloc(bdev, event_cb, event_ctx, &desc);
6645 	if (rc != 0) {
6646 		pthread_mutex_unlock(&g_bdev_mgr.mutex);
6647 		return rc;
6648 	}
6649 
6650 	rc = bdev_open(bdev, write, desc);
6651 	if (rc != 0) {
6652 		bdev_desc_free(desc);
6653 		desc = NULL;
6654 	}
6655 
6656 	*_desc = desc;
6657 
6658 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
6659 
6660 	return rc;
6661 }
6662 
6663 static void
6664 bdev_close(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc)
6665 {
6666 	int rc;
6667 
6668 	pthread_mutex_lock(&bdev->internal.mutex);
6669 	pthread_mutex_lock(&desc->mutex);
6670 
6671 	TAILQ_REMOVE(&bdev->internal.open_descs, desc, link);
6672 
6673 	desc->closed = true;
6674 
6675 	if (0 == desc->refs) {
6676 		pthread_mutex_unlock(&desc->mutex);
6677 		bdev_desc_free(desc);
6678 	} else {
6679 		pthread_mutex_unlock(&desc->mutex);
6680 	}
6681 
6682 	/* If no more descriptors, kill QoS channel */
6683 	if (bdev->internal.qos && TAILQ_EMPTY(&bdev->internal.open_descs)) {
6684 		SPDK_DEBUGLOG(bdev, "Closed last descriptor for bdev %s on thread %p. Stopping QoS.\n",
6685 			      bdev->name, spdk_get_thread());
6686 
6687 		if (bdev_qos_destroy(bdev)) {
6688 			/* There isn't anything we can do to recover here. Just let the
6689 			 * old QoS poller keep running. The QoS handling won't change
6690 			 * cores when the user allocates a new channel, but it won't break. */
6691 			SPDK_ERRLOG("Unable to shut down QoS poller. It will continue running on the current thread.\n");
6692 		}
6693 	}
6694 
6695 	if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->internal.open_descs)) {
6696 		rc = bdev_unregister_unsafe(bdev);
6697 		pthread_mutex_unlock(&bdev->internal.mutex);
6698 
6699 		if (rc == 0) {
6700 			spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
6701 		}
6702 	} else {
6703 		pthread_mutex_unlock(&bdev->internal.mutex);
6704 	}
6705 }
6706 
6707 void
6708 spdk_bdev_close(struct spdk_bdev_desc *desc)
6709 {
6710 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6711 
6712 	SPDK_DEBUGLOG(bdev, "Closing descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
6713 		      spdk_get_thread());
6714 
6715 	assert(desc->thread == spdk_get_thread());
6716 
6717 	spdk_poller_unregister(&desc->io_timeout_poller);
6718 
6719 	pthread_mutex_lock(&g_bdev_mgr.mutex);
6720 
6721 	bdev_close(bdev, desc);
6722 
6723 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
6724 }
6725 
6726 static void
6727 bdev_register_finished(void *arg)
6728 {
6729 	struct spdk_bdev_desc *desc = arg;
6730 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6731 
6732 	spdk_notify_send("bdev_register", spdk_bdev_get_name(bdev));
6733 
6734 	bdev_close(bdev, desc);
6735 }
6736 
6737 int
6738 spdk_bdev_register(struct spdk_bdev *bdev)
6739 {
6740 	struct spdk_bdev_desc *desc;
6741 	int rc;
6742 
6743 	rc = bdev_register(bdev);
6744 	if (rc != 0) {
6745 		return rc;
6746 	}
6747 
6748 	/* A descriptor is opened to prevent bdev deletion during examination */
6749 	rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, &desc);
6750 	if (rc != 0) {
6751 		spdk_bdev_unregister(bdev, NULL, NULL);
6752 		return rc;
6753 	}
6754 
6755 	rc = bdev_open(bdev, false, desc);
6756 	if (rc != 0) {
6757 		bdev_desc_free(desc);
6758 		spdk_bdev_unregister(bdev, NULL, NULL);
6759 		return rc;
6760 	}
6761 
6762 	/* Examine configuration before initializing I/O */
6763 	bdev_examine(bdev);
6764 
6765 	rc = spdk_bdev_wait_for_examine(bdev_register_finished, desc);
6766 	if (rc != 0) {
6767 		bdev_close(bdev, desc);
6768 		spdk_bdev_unregister(bdev, NULL, NULL);
6769 	}
6770 
6771 	return rc;
6772 }
6773 
6774 int
6775 spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
6776 			    struct spdk_bdev_module *module)
6777 {
6778 	if (bdev->internal.claim_module != NULL) {
6779 		SPDK_ERRLOG("bdev %s already claimed by module %s\n", bdev->name,
6780 			    bdev->internal.claim_module->name);
6781 		return -EPERM;
6782 	}
6783 
6784 	if (desc && !desc->write) {
6785 		desc->write = true;
6786 	}
6787 
6788 	bdev->internal.claim_module = module;
6789 	return 0;
6790 }
6791 
6792 void
6793 spdk_bdev_module_release_bdev(struct spdk_bdev *bdev)
6794 {
6795 	assert(bdev->internal.claim_module != NULL);
6796 	bdev->internal.claim_module = NULL;
6797 }
6798 
6799 struct spdk_bdev *
6800 spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
6801 {
6802 	assert(desc != NULL);
6803 	return desc->bdev;
6804 }
6805 
6806 int
6807 spdk_for_each_bdev(void *ctx, spdk_for_each_bdev_fn fn)
6808 {
6809 	struct spdk_bdev *bdev, *tmp;
6810 	struct spdk_bdev_desc *desc;
6811 	int rc = 0;
6812 
6813 	assert(fn != NULL);
6814 
6815 	pthread_mutex_lock(&g_bdev_mgr.mutex);
6816 	bdev = spdk_bdev_first();
6817 	while (bdev != NULL) {
6818 		rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, &desc);
6819 		if (rc != 0) {
6820 			break;
6821 		}
6822 		rc = bdev_open(bdev, false, desc);
6823 		if (rc != 0) {
6824 			bdev_desc_free(desc);
6825 			break;
6826 		}
6827 		pthread_mutex_unlock(&g_bdev_mgr.mutex);
6828 
6829 		rc = fn(ctx, bdev);
6830 
6831 		pthread_mutex_lock(&g_bdev_mgr.mutex);
6832 		tmp = spdk_bdev_next(bdev);
6833 		bdev_close(bdev, desc);
6834 		if (rc != 0) {
6835 			break;
6836 		}
6837 		bdev = tmp;
6838 	}
6839 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
6840 
6841 	return rc;
6842 }
6843 
6844 int
6845 spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn)
6846 {
6847 	struct spdk_bdev *bdev, *tmp;
6848 	struct spdk_bdev_desc *desc;
6849 	int rc = 0;
6850 
6851 	assert(fn != NULL);
6852 
6853 	pthread_mutex_lock(&g_bdev_mgr.mutex);
6854 	bdev = spdk_bdev_first_leaf();
6855 	while (bdev != NULL) {
6856 		rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, &desc);
6857 		if (rc != 0) {
6858 			break;
6859 		}
6860 		rc = bdev_open(bdev, false, desc);
6861 		if (rc != 0) {
6862 			bdev_desc_free(desc);
6863 			break;
6864 		}
6865 		pthread_mutex_unlock(&g_bdev_mgr.mutex);
6866 
6867 		rc = fn(ctx, bdev);
6868 
6869 		pthread_mutex_lock(&g_bdev_mgr.mutex);
6870 		tmp = spdk_bdev_next_leaf(bdev);
6871 		bdev_close(bdev, desc);
6872 		if (rc != 0) {
6873 			break;
6874 		}
6875 		bdev = tmp;
6876 	}
6877 	pthread_mutex_unlock(&g_bdev_mgr.mutex);
6878 
6879 	return rc;
6880 }
6881 
6882 void
6883 spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp)
6884 {
6885 	struct iovec *iovs;
6886 	int iovcnt;
6887 
6888 	if (bdev_io == NULL) {
6889 		return;
6890 	}
6891 
6892 	switch (bdev_io->type) {
6893 	case SPDK_BDEV_IO_TYPE_READ:
6894 	case SPDK_BDEV_IO_TYPE_WRITE:
6895 	case SPDK_BDEV_IO_TYPE_ZCOPY:
6896 		iovs = bdev_io->u.bdev.iovs;
6897 		iovcnt = bdev_io->u.bdev.iovcnt;
6898 		break;
6899 	default:
6900 		iovs = NULL;
6901 		iovcnt = 0;
6902 		break;
6903 	}
6904 
6905 	if (iovp) {
6906 		*iovp = iovs;
6907 	}
6908 	if (iovcntp) {
6909 		*iovcntp = iovcnt;
6910 	}
6911 }
6912 
6913 void *
6914 spdk_bdev_io_get_md_buf(struct spdk_bdev_io *bdev_io)
6915 {
6916 	if (bdev_io == NULL) {
6917 		return NULL;
6918 	}
6919 
6920 	if (!spdk_bdev_is_md_separate(bdev_io->bdev)) {
6921 		return NULL;
6922 	}
6923 
6924 	if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ ||
6925 	    bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
6926 		return bdev_io->u.bdev.md_buf;
6927 	}
6928 
6929 	return NULL;
6930 }
6931 
6932 void *
6933 spdk_bdev_io_get_cb_arg(struct spdk_bdev_io *bdev_io)
6934 {
6935 	if (bdev_io == NULL) {
6936 		assert(false);
6937 		return NULL;
6938 	}
6939 
6940 	return bdev_io->internal.caller_ctx;
6941 }
6942 
6943 void
6944 spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
6945 {
6946 
6947 	if (spdk_bdev_module_list_find(bdev_module->name)) {
6948 		SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name);
6949 		assert(false);
6950 	}
6951 
6952 	/*
6953 	 * Modules with examine callbacks must be initialized first, so they are
6954 	 *  ready to handle examine callbacks from later modules that will
6955 	 *  register physical bdevs.
6956 	 */
6957 	if (bdev_module->examine_config != NULL || bdev_module->examine_disk != NULL) {
6958 		TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
6959 	} else {
6960 		TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
6961 	}
6962 }
6963 
6964 struct spdk_bdev_module *
6965 spdk_bdev_module_list_find(const char *name)
6966 {
6967 	struct spdk_bdev_module *bdev_module;
6968 
6969 	TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
6970 		if (strcmp(name, bdev_module->name) == 0) {
6971 			break;
6972 		}
6973 	}
6974 
6975 	return bdev_module;
6976 }
6977 
6978 static void
6979 bdev_write_zero_buffer_next(void *_bdev_io)
6980 {
6981 	struct spdk_bdev_io *bdev_io = _bdev_io;
6982 	uint64_t num_bytes, num_blocks;
6983 	void *md_buf = NULL;
6984 	int rc;
6985 
6986 	num_bytes = spdk_min(_bdev_get_block_size_with_md(bdev_io->bdev) *
6987 			     bdev_io->u.bdev.split_remaining_num_blocks,
6988 			     ZERO_BUFFER_SIZE);
6989 	num_blocks = num_bytes / _bdev_get_block_size_with_md(bdev_io->bdev);
6990 
6991 	if (spdk_bdev_is_md_separate(bdev_io->bdev)) {
6992 		md_buf = (char *)g_bdev_mgr.zero_buffer +
6993 			 spdk_bdev_get_block_size(bdev_io->bdev) * num_blocks;
6994 	}
6995 
6996 	rc = bdev_write_blocks_with_md(bdev_io->internal.desc,
6997 				       spdk_io_channel_from_ctx(bdev_io->internal.ch),
6998 				       g_bdev_mgr.zero_buffer, md_buf,
6999 				       bdev_io->u.bdev.split_current_offset_blocks, num_blocks,
7000 				       bdev_write_zero_buffer_done, bdev_io);
7001 	if (rc == 0) {
7002 		bdev_io->u.bdev.split_remaining_num_blocks -= num_blocks;
7003 		bdev_io->u.bdev.split_current_offset_blocks += num_blocks;
7004 	} else if (rc == -ENOMEM) {
7005 		bdev_queue_io_wait_with_cb(bdev_io, bdev_write_zero_buffer_next);
7006 	} else {
7007 		bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7008 		bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
7009 	}
7010 }
7011 
7012 static void
7013 bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
7014 {
7015 	struct spdk_bdev_io *parent_io = cb_arg;
7016 
7017 	spdk_bdev_free_io(bdev_io);
7018 
7019 	if (!success) {
7020 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7021 		parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
7022 		return;
7023 	}
7024 
7025 	if (parent_io->u.bdev.split_remaining_num_blocks == 0) {
7026 		parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
7027 		parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx);
7028 		return;
7029 	}
7030 
7031 	bdev_write_zero_buffer_next(parent_io);
7032 }
7033 
7034 static void
7035 bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status)
7036 {
7037 	pthread_mutex_lock(&ctx->bdev->internal.mutex);
7038 	ctx->bdev->internal.qos_mod_in_progress = false;
7039 	pthread_mutex_unlock(&ctx->bdev->internal.mutex);
7040 
7041 	if (ctx->cb_fn) {
7042 		ctx->cb_fn(ctx->cb_arg, status);
7043 	}
7044 	free(ctx);
7045 }
7046 
7047 static void
7048 bdev_disable_qos_done(void *cb_arg)
7049 {
7050 	struct set_qos_limit_ctx *ctx = cb_arg;
7051 	struct spdk_bdev *bdev = ctx->bdev;
7052 	struct spdk_bdev_io *bdev_io;
7053 	struct spdk_bdev_qos *qos;
7054 
7055 	pthread_mutex_lock(&bdev->internal.mutex);
7056 	qos = bdev->internal.qos;
7057 	bdev->internal.qos = NULL;
7058 	pthread_mutex_unlock(&bdev->internal.mutex);
7059 
7060 	while (!TAILQ_EMPTY(&qos->queued)) {
7061 		/* Send queued I/O back to their original thread for resubmission. */
7062 		bdev_io = TAILQ_FIRST(&qos->queued);
7063 		TAILQ_REMOVE(&qos->queued, bdev_io, internal.link);
7064 
7065 		if (bdev_io->internal.io_submit_ch) {
7066 			/*
7067 			 * Channel was changed when sending it to the QoS thread - change it back
7068 			 *  before sending it back to the original thread.
7069 			 */
7070 			bdev_io->internal.ch = bdev_io->internal.io_submit_ch;
7071 			bdev_io->internal.io_submit_ch = NULL;
7072 		}
7073 
7074 		spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
7075 				     _bdev_io_submit, bdev_io);
7076 	}
7077 
7078 	if (qos->thread != NULL) {
7079 		spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
7080 		spdk_poller_unregister(&qos->poller);
7081 	}
7082 
7083 	free(qos);
7084 
7085 	bdev_set_qos_limit_done(ctx, 0);
7086 }
7087 
7088 static void
7089 bdev_disable_qos_msg_done(struct spdk_io_channel_iter *i, int status)
7090 {
7091 	void *io_device = spdk_io_channel_iter_get_io_device(i);
7092 	struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
7093 	struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7094 	struct spdk_thread *thread;
7095 
7096 	pthread_mutex_lock(&bdev->internal.mutex);
7097 	thread = bdev->internal.qos->thread;
7098 	pthread_mutex_unlock(&bdev->internal.mutex);
7099 
7100 	if (thread != NULL) {
7101 		spdk_thread_send_msg(thread, bdev_disable_qos_done, ctx);
7102 	} else {
7103 		bdev_disable_qos_done(ctx);
7104 	}
7105 }
7106 
7107 static void
7108 bdev_disable_qos_msg(struct spdk_io_channel_iter *i)
7109 {
7110 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
7111 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch);
7112 
7113 	bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED;
7114 
7115 	spdk_for_each_channel_continue(i, 0);
7116 }
7117 
7118 static void
7119 bdev_update_qos_rate_limit_msg(void *cb_arg)
7120 {
7121 	struct set_qos_limit_ctx *ctx = cb_arg;
7122 	struct spdk_bdev *bdev = ctx->bdev;
7123 
7124 	pthread_mutex_lock(&bdev->internal.mutex);
7125 	bdev_qos_update_max_quota_per_timeslice(bdev->internal.qos);
7126 	pthread_mutex_unlock(&bdev->internal.mutex);
7127 
7128 	bdev_set_qos_limit_done(ctx, 0);
7129 }
7130 
7131 static void
7132 bdev_enable_qos_msg(struct spdk_io_channel_iter *i)
7133 {
7134 	void *io_device = spdk_io_channel_iter_get_io_device(i);
7135 	struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
7136 	struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
7137 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(ch);
7138 
7139 	pthread_mutex_lock(&bdev->internal.mutex);
7140 	bdev_enable_qos(bdev, bdev_ch);
7141 	pthread_mutex_unlock(&bdev->internal.mutex);
7142 	spdk_for_each_channel_continue(i, 0);
7143 }
7144 
7145 static void
7146 bdev_enable_qos_done(struct spdk_io_channel_iter *i, int status)
7147 {
7148 	struct set_qos_limit_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7149 
7150 	bdev_set_qos_limit_done(ctx, status);
7151 }
7152 
7153 static void
7154 bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
7155 {
7156 	int i;
7157 
7158 	assert(bdev->internal.qos != NULL);
7159 
7160 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
7161 		if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
7162 			bdev->internal.qos->rate_limits[i].limit = limits[i];
7163 
7164 			if (limits[i] == 0) {
7165 				bdev->internal.qos->rate_limits[i].limit =
7166 					SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
7167 			}
7168 		}
7169 	}
7170 }
7171 
7172 void
7173 spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits,
7174 			      void (*cb_fn)(void *cb_arg, int status), void *cb_arg)
7175 {
7176 	struct set_qos_limit_ctx	*ctx;
7177 	uint32_t			limit_set_complement;
7178 	uint64_t			min_limit_per_sec;
7179 	int				i;
7180 	bool				disable_rate_limit = true;
7181 
7182 	for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
7183 		if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
7184 			continue;
7185 		}
7186 
7187 		if (limits[i] > 0) {
7188 			disable_rate_limit = false;
7189 		}
7190 
7191 		if (bdev_qos_is_iops_rate_limit(i) == true) {
7192 			min_limit_per_sec = SPDK_BDEV_QOS_MIN_IOS_PER_SEC;
7193 		} else {
7194 			/* Change from megabyte to byte rate limit */
7195 			limits[i] = limits[i] * 1024 * 1024;
7196 			min_limit_per_sec = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC;
7197 		}
7198 
7199 		limit_set_complement = limits[i] % min_limit_per_sec;
7200 		if (limit_set_complement) {
7201 			SPDK_ERRLOG("Requested rate limit %" PRIu64 " is not a multiple of %" PRIu64 "\n",
7202 				    limits[i], min_limit_per_sec);
7203 			limits[i] += min_limit_per_sec - limit_set_complement;
7204 			SPDK_ERRLOG("Round up the rate limit to %" PRIu64 "\n", limits[i]);
7205 		}
7206 	}
7207 
7208 	ctx = calloc(1, sizeof(*ctx));
7209 	if (ctx == NULL) {
7210 		cb_fn(cb_arg, -ENOMEM);
7211 		return;
7212 	}
7213 
7214 	ctx->cb_fn = cb_fn;
7215 	ctx->cb_arg = cb_arg;
7216 	ctx->bdev = bdev;
7217 
7218 	pthread_mutex_lock(&bdev->internal.mutex);
7219 	if (bdev->internal.qos_mod_in_progress) {
7220 		pthread_mutex_unlock(&bdev->internal.mutex);
7221 		free(ctx);
7222 		cb_fn(cb_arg, -EAGAIN);
7223 		return;
7224 	}
7225 	bdev->internal.qos_mod_in_progress = true;
7226 
7227 	if (disable_rate_limit == true && bdev->internal.qos) {
7228 		for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
7229 			if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED &&
7230 			    (bdev->internal.qos->rate_limits[i].limit > 0 &&
7231 			     bdev->internal.qos->rate_limits[i].limit !=
7232 			     SPDK_BDEV_QOS_LIMIT_NOT_DEFINED)) {
7233 				disable_rate_limit = false;
7234 				break;
7235 			}
7236 		}
7237 	}
7238 
7239 	if (disable_rate_limit == false) {
7240 		if (bdev->internal.qos == NULL) {
7241 			bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
7242 			if (!bdev->internal.qos) {
7243 				pthread_mutex_unlock(&bdev->internal.mutex);
7244 				SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
7245 				bdev_set_qos_limit_done(ctx, -ENOMEM);
7246 				return;
7247 			}
7248 		}
7249 
7250 		if (bdev->internal.qos->thread == NULL) {
7251 			/* Enabling */
7252 			bdev_set_qos_rate_limits(bdev, limits);
7253 
7254 			spdk_for_each_channel(__bdev_to_io_dev(bdev),
7255 					      bdev_enable_qos_msg, ctx,
7256 					      bdev_enable_qos_done);
7257 		} else {
7258 			/* Updating */
7259 			bdev_set_qos_rate_limits(bdev, limits);
7260 
7261 			spdk_thread_send_msg(bdev->internal.qos->thread,
7262 					     bdev_update_qos_rate_limit_msg, ctx);
7263 		}
7264 	} else {
7265 		if (bdev->internal.qos != NULL) {
7266 			bdev_set_qos_rate_limits(bdev, limits);
7267 
7268 			/* Disabling */
7269 			spdk_for_each_channel(__bdev_to_io_dev(bdev),
7270 					      bdev_disable_qos_msg, ctx,
7271 					      bdev_disable_qos_msg_done);
7272 		} else {
7273 			pthread_mutex_unlock(&bdev->internal.mutex);
7274 			bdev_set_qos_limit_done(ctx, 0);
7275 			return;
7276 		}
7277 	}
7278 
7279 	pthread_mutex_unlock(&bdev->internal.mutex);
7280 }
7281 
7282 struct spdk_bdev_histogram_ctx {
7283 	spdk_bdev_histogram_status_cb cb_fn;
7284 	void *cb_arg;
7285 	struct spdk_bdev *bdev;
7286 	int status;
7287 };
7288 
7289 static void
7290 bdev_histogram_disable_channel_cb(struct spdk_io_channel_iter *i, int status)
7291 {
7292 	struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7293 
7294 	pthread_mutex_lock(&ctx->bdev->internal.mutex);
7295 	ctx->bdev->internal.histogram_in_progress = false;
7296 	pthread_mutex_unlock(&ctx->bdev->internal.mutex);
7297 	ctx->cb_fn(ctx->cb_arg, ctx->status);
7298 	free(ctx);
7299 }
7300 
7301 static void
7302 bdev_histogram_disable_channel(struct spdk_io_channel_iter *i)
7303 {
7304 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
7305 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7306 
7307 	if (ch->histogram != NULL) {
7308 		spdk_histogram_data_free(ch->histogram);
7309 		ch->histogram = NULL;
7310 	}
7311 	spdk_for_each_channel_continue(i, 0);
7312 }
7313 
7314 static void
7315 bdev_histogram_enable_channel_cb(struct spdk_io_channel_iter *i, int status)
7316 {
7317 	struct spdk_bdev_histogram_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7318 
7319 	if (status != 0) {
7320 		ctx->status = status;
7321 		ctx->bdev->internal.histogram_enabled = false;
7322 		spdk_for_each_channel(__bdev_to_io_dev(ctx->bdev), bdev_histogram_disable_channel, ctx,
7323 				      bdev_histogram_disable_channel_cb);
7324 	} else {
7325 		pthread_mutex_lock(&ctx->bdev->internal.mutex);
7326 		ctx->bdev->internal.histogram_in_progress = false;
7327 		pthread_mutex_unlock(&ctx->bdev->internal.mutex);
7328 		ctx->cb_fn(ctx->cb_arg, ctx->status);
7329 		free(ctx);
7330 	}
7331 }
7332 
7333 static void
7334 bdev_histogram_enable_channel(struct spdk_io_channel_iter *i)
7335 {
7336 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
7337 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7338 	int status = 0;
7339 
7340 	if (ch->histogram == NULL) {
7341 		ch->histogram = spdk_histogram_data_alloc();
7342 		if (ch->histogram == NULL) {
7343 			status = -ENOMEM;
7344 		}
7345 	}
7346 
7347 	spdk_for_each_channel_continue(i, status);
7348 }
7349 
7350 void
7351 spdk_bdev_histogram_enable(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn,
7352 			   void *cb_arg, bool enable)
7353 {
7354 	struct spdk_bdev_histogram_ctx *ctx;
7355 
7356 	ctx = calloc(1, sizeof(struct spdk_bdev_histogram_ctx));
7357 	if (ctx == NULL) {
7358 		cb_fn(cb_arg, -ENOMEM);
7359 		return;
7360 	}
7361 
7362 	ctx->bdev = bdev;
7363 	ctx->status = 0;
7364 	ctx->cb_fn = cb_fn;
7365 	ctx->cb_arg = cb_arg;
7366 
7367 	pthread_mutex_lock(&bdev->internal.mutex);
7368 	if (bdev->internal.histogram_in_progress) {
7369 		pthread_mutex_unlock(&bdev->internal.mutex);
7370 		free(ctx);
7371 		cb_fn(cb_arg, -EAGAIN);
7372 		return;
7373 	}
7374 
7375 	bdev->internal.histogram_in_progress = true;
7376 	pthread_mutex_unlock(&bdev->internal.mutex);
7377 
7378 	bdev->internal.histogram_enabled = enable;
7379 
7380 	if (enable) {
7381 		/* Allocate histogram for each channel */
7382 		spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_enable_channel, ctx,
7383 				      bdev_histogram_enable_channel_cb);
7384 	} else {
7385 		spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_disable_channel, ctx,
7386 				      bdev_histogram_disable_channel_cb);
7387 	}
7388 }
7389 
7390 struct spdk_bdev_histogram_data_ctx {
7391 	spdk_bdev_histogram_data_cb cb_fn;
7392 	void *cb_arg;
7393 	struct spdk_bdev *bdev;
7394 	/** merged histogram data from all channels */
7395 	struct spdk_histogram_data	*histogram;
7396 };
7397 
7398 static void
7399 bdev_histogram_get_channel_cb(struct spdk_io_channel_iter *i, int status)
7400 {
7401 	struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7402 
7403 	ctx->cb_fn(ctx->cb_arg, status, ctx->histogram);
7404 	free(ctx);
7405 }
7406 
7407 static void
7408 bdev_histogram_get_channel(struct spdk_io_channel_iter *i)
7409 {
7410 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
7411 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7412 	struct spdk_bdev_histogram_data_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7413 	int status = 0;
7414 
7415 	if (ch->histogram == NULL) {
7416 		status = -EFAULT;
7417 	} else {
7418 		spdk_histogram_data_merge(ctx->histogram, ch->histogram);
7419 	}
7420 
7421 	spdk_for_each_channel_continue(i, status);
7422 }
7423 
7424 void
7425 spdk_bdev_histogram_get(struct spdk_bdev *bdev, struct spdk_histogram_data *histogram,
7426 			spdk_bdev_histogram_data_cb cb_fn,
7427 			void *cb_arg)
7428 {
7429 	struct spdk_bdev_histogram_data_ctx *ctx;
7430 
7431 	ctx = calloc(1, sizeof(struct spdk_bdev_histogram_data_ctx));
7432 	if (ctx == NULL) {
7433 		cb_fn(cb_arg, -ENOMEM, NULL);
7434 		return;
7435 	}
7436 
7437 	ctx->bdev = bdev;
7438 	ctx->cb_fn = cb_fn;
7439 	ctx->cb_arg = cb_arg;
7440 
7441 	ctx->histogram = histogram;
7442 
7443 	spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_histogram_get_channel, ctx,
7444 			      bdev_histogram_get_channel_cb);
7445 }
7446 
7447 size_t
7448 spdk_bdev_get_media_events(struct spdk_bdev_desc *desc, struct spdk_bdev_media_event *events,
7449 			   size_t max_events)
7450 {
7451 	struct media_event_entry *entry;
7452 	size_t num_events = 0;
7453 
7454 	for (; num_events < max_events; ++num_events) {
7455 		entry = TAILQ_FIRST(&desc->pending_media_events);
7456 		if (entry == NULL) {
7457 			break;
7458 		}
7459 
7460 		events[num_events] = entry->event;
7461 		TAILQ_REMOVE(&desc->pending_media_events, entry, tailq);
7462 		TAILQ_INSERT_TAIL(&desc->free_media_events, entry, tailq);
7463 	}
7464 
7465 	return num_events;
7466 }
7467 
7468 int
7469 spdk_bdev_push_media_events(struct spdk_bdev *bdev, const struct spdk_bdev_media_event *events,
7470 			    size_t num_events)
7471 {
7472 	struct spdk_bdev_desc *desc;
7473 	struct media_event_entry *entry;
7474 	size_t event_id;
7475 	int rc = 0;
7476 
7477 	assert(bdev->media_events);
7478 
7479 	pthread_mutex_lock(&bdev->internal.mutex);
7480 	TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
7481 		if (desc->write) {
7482 			break;
7483 		}
7484 	}
7485 
7486 	if (desc == NULL || desc->media_events_buffer == NULL) {
7487 		rc = -ENODEV;
7488 		goto out;
7489 	}
7490 
7491 	for (event_id = 0; event_id < num_events; ++event_id) {
7492 		entry = TAILQ_FIRST(&desc->free_media_events);
7493 		if (entry == NULL) {
7494 			break;
7495 		}
7496 
7497 		TAILQ_REMOVE(&desc->free_media_events, entry, tailq);
7498 		TAILQ_INSERT_TAIL(&desc->pending_media_events, entry, tailq);
7499 		entry->event = events[event_id];
7500 	}
7501 
7502 	rc = event_id;
7503 out:
7504 	pthread_mutex_unlock(&bdev->internal.mutex);
7505 	return rc;
7506 }
7507 
7508 void
7509 spdk_bdev_notify_media_management(struct spdk_bdev *bdev)
7510 {
7511 	struct spdk_bdev_desc *desc;
7512 
7513 	pthread_mutex_lock(&bdev->internal.mutex);
7514 	TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
7515 		if (!TAILQ_EMPTY(&desc->pending_media_events)) {
7516 			desc->callback.event_fn(SPDK_BDEV_EVENT_MEDIA_MANAGEMENT, bdev,
7517 						desc->callback.ctx);
7518 		}
7519 	}
7520 	pthread_mutex_unlock(&bdev->internal.mutex);
7521 }
7522 
7523 struct locked_lba_range_ctx {
7524 	struct lba_range		range;
7525 	struct spdk_bdev		*bdev;
7526 	struct lba_range		*current_range;
7527 	struct lba_range		*owner_range;
7528 	struct spdk_poller		*poller;
7529 	lock_range_cb			cb_fn;
7530 	void				*cb_arg;
7531 };
7532 
7533 static void
7534 bdev_lock_error_cleanup_cb(struct spdk_io_channel_iter *i, int status)
7535 {
7536 	struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7537 
7538 	ctx->cb_fn(ctx->cb_arg, -ENOMEM);
7539 	free(ctx);
7540 }
7541 
7542 static void bdev_unlock_lba_range_get_channel(struct spdk_io_channel_iter *i);
7543 
7544 static void
7545 bdev_lock_lba_range_cb(struct spdk_io_channel_iter *i, int status)
7546 {
7547 	struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7548 	struct spdk_bdev *bdev = ctx->bdev;
7549 
7550 	if (status == -ENOMEM) {
7551 		/* One of the channels could not allocate a range object.
7552 		 * So we have to go back and clean up any ranges that were
7553 		 * allocated successfully before we return error status to
7554 		 * the caller.  We can reuse the unlock function to do that
7555 		 * clean up.
7556 		 */
7557 		spdk_for_each_channel(__bdev_to_io_dev(bdev),
7558 				      bdev_unlock_lba_range_get_channel, ctx,
7559 				      bdev_lock_error_cleanup_cb);
7560 		return;
7561 	}
7562 
7563 	/* All channels have locked this range and no I/O overlapping the range
7564 	 * are outstanding!  Set the owner_ch for the range object for the
7565 	 * locking channel, so that this channel will know that it is allowed
7566 	 * to write to this range.
7567 	 */
7568 	ctx->owner_range->owner_ch = ctx->range.owner_ch;
7569 	ctx->cb_fn(ctx->cb_arg, status);
7570 
7571 	/* Don't free the ctx here.  Its range is in the bdev's global list of
7572 	 * locked ranges still, and will be removed and freed when this range
7573 	 * is later unlocked.
7574 	 */
7575 }
7576 
7577 static int
7578 bdev_lock_lba_range_check_io(void *_i)
7579 {
7580 	struct spdk_io_channel_iter *i = _i;
7581 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
7582 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7583 	struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7584 	struct lba_range *range = ctx->current_range;
7585 	struct spdk_bdev_io *bdev_io;
7586 
7587 	spdk_poller_unregister(&ctx->poller);
7588 
7589 	/* The range is now in the locked_ranges, so no new IO can be submitted to this
7590 	 * range.  But we need to wait until any outstanding IO overlapping with this range
7591 	 * are completed.
7592 	 */
7593 	TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) {
7594 		if (bdev_io_range_is_locked(bdev_io, range)) {
7595 			ctx->poller = SPDK_POLLER_REGISTER(bdev_lock_lba_range_check_io, i, 100);
7596 			return SPDK_POLLER_BUSY;
7597 		}
7598 	}
7599 
7600 	spdk_for_each_channel_continue(i, 0);
7601 	return SPDK_POLLER_BUSY;
7602 }
7603 
7604 static void
7605 bdev_lock_lba_range_get_channel(struct spdk_io_channel_iter *i)
7606 {
7607 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
7608 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7609 	struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7610 	struct lba_range *range;
7611 
7612 	TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
7613 		if (range->length == ctx->range.length &&
7614 		    range->offset == ctx->range.offset &&
7615 		    range->locked_ctx == ctx->range.locked_ctx) {
7616 			/* This range already exists on this channel, so don't add
7617 			 * it again.  This can happen when a new channel is created
7618 			 * while the for_each_channel operation is in progress.
7619 			 * Do not check for outstanding I/O in that case, since the
7620 			 * range was locked before any I/O could be submitted to the
7621 			 * new channel.
7622 			 */
7623 			spdk_for_each_channel_continue(i, 0);
7624 			return;
7625 		}
7626 	}
7627 
7628 	range = calloc(1, sizeof(*range));
7629 	if (range == NULL) {
7630 		spdk_for_each_channel_continue(i, -ENOMEM);
7631 		return;
7632 	}
7633 
7634 	range->length = ctx->range.length;
7635 	range->offset = ctx->range.offset;
7636 	range->locked_ctx = ctx->range.locked_ctx;
7637 	ctx->current_range = range;
7638 	if (ctx->range.owner_ch == ch) {
7639 		/* This is the range object for the channel that will hold
7640 		 * the lock.  Store it in the ctx object so that we can easily
7641 		 * set its owner_ch after the lock is finally acquired.
7642 		 */
7643 		ctx->owner_range = range;
7644 	}
7645 	TAILQ_INSERT_TAIL(&ch->locked_ranges, range, tailq);
7646 	bdev_lock_lba_range_check_io(i);
7647 }
7648 
7649 static void
7650 bdev_lock_lba_range_ctx(struct spdk_bdev *bdev, struct locked_lba_range_ctx *ctx)
7651 {
7652 	assert(spdk_get_thread() == spdk_io_channel_get_thread(ctx->range.owner_ch->channel));
7653 
7654 	/* We will add a copy of this range to each channel now. */
7655 	spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_lock_lba_range_get_channel, ctx,
7656 			      bdev_lock_lba_range_cb);
7657 }
7658 
7659 static bool
7660 bdev_lba_range_overlaps_tailq(struct lba_range *range, lba_range_tailq_t *tailq)
7661 {
7662 	struct lba_range *r;
7663 
7664 	TAILQ_FOREACH(r, tailq, tailq) {
7665 		if (bdev_lba_range_overlapped(range, r)) {
7666 			return true;
7667 		}
7668 	}
7669 	return false;
7670 }
7671 
7672 static int
7673 bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
7674 		    uint64_t offset, uint64_t length,
7675 		    lock_range_cb cb_fn, void *cb_arg)
7676 {
7677 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7678 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7679 	struct locked_lba_range_ctx *ctx;
7680 
7681 	if (cb_arg == NULL) {
7682 		SPDK_ERRLOG("cb_arg must not be NULL\n");
7683 		return -EINVAL;
7684 	}
7685 
7686 	ctx = calloc(1, sizeof(*ctx));
7687 	if (ctx == NULL) {
7688 		return -ENOMEM;
7689 	}
7690 
7691 	ctx->range.offset = offset;
7692 	ctx->range.length = length;
7693 	ctx->range.owner_ch = ch;
7694 	ctx->range.locked_ctx = cb_arg;
7695 	ctx->bdev = bdev;
7696 	ctx->cb_fn = cb_fn;
7697 	ctx->cb_arg = cb_arg;
7698 
7699 	pthread_mutex_lock(&bdev->internal.mutex);
7700 	if (bdev_lba_range_overlaps_tailq(&ctx->range, &bdev->internal.locked_ranges)) {
7701 		/* There is an active lock overlapping with this range.
7702 		 * Put it on the pending list until this range no
7703 		 * longer overlaps with another.
7704 		 */
7705 		TAILQ_INSERT_TAIL(&bdev->internal.pending_locked_ranges, &ctx->range, tailq);
7706 	} else {
7707 		TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, &ctx->range, tailq);
7708 		bdev_lock_lba_range_ctx(bdev, ctx);
7709 	}
7710 	pthread_mutex_unlock(&bdev->internal.mutex);
7711 	return 0;
7712 }
7713 
7714 static void
7715 bdev_lock_lba_range_ctx_msg(void *_ctx)
7716 {
7717 	struct locked_lba_range_ctx *ctx = _ctx;
7718 
7719 	bdev_lock_lba_range_ctx(ctx->bdev, ctx);
7720 }
7721 
7722 static void
7723 bdev_unlock_lba_range_cb(struct spdk_io_channel_iter *i, int status)
7724 {
7725 	struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7726 	struct locked_lba_range_ctx *pending_ctx;
7727 	struct spdk_bdev_channel *ch = ctx->range.owner_ch;
7728 	struct spdk_bdev *bdev = ch->bdev;
7729 	struct lba_range *range, *tmp;
7730 
7731 	pthread_mutex_lock(&bdev->internal.mutex);
7732 	/* Check if there are any pending locked ranges that overlap with this range
7733 	 * that was just unlocked.  If there are, check that it doesn't overlap with any
7734 	 * other locked ranges before calling bdev_lock_lba_range_ctx which will start
7735 	 * the lock process.
7736 	 */
7737 	TAILQ_FOREACH_SAFE(range, &bdev->internal.pending_locked_ranges, tailq, tmp) {
7738 		if (bdev_lba_range_overlapped(range, &ctx->range) &&
7739 		    !bdev_lba_range_overlaps_tailq(range, &bdev->internal.locked_ranges)) {
7740 			TAILQ_REMOVE(&bdev->internal.pending_locked_ranges, range, tailq);
7741 			pending_ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range);
7742 			TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, range, tailq);
7743 			spdk_thread_send_msg(spdk_io_channel_get_thread(pending_ctx->range.owner_ch->channel),
7744 					     bdev_lock_lba_range_ctx_msg, pending_ctx);
7745 		}
7746 	}
7747 	pthread_mutex_unlock(&bdev->internal.mutex);
7748 
7749 	ctx->cb_fn(ctx->cb_arg, status);
7750 	free(ctx);
7751 }
7752 
7753 static void
7754 bdev_unlock_lba_range_get_channel(struct spdk_io_channel_iter *i)
7755 {
7756 	struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
7757 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7758 	struct locked_lba_range_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7759 	TAILQ_HEAD(, spdk_bdev_io) io_locked;
7760 	struct spdk_bdev_io *bdev_io;
7761 	struct lba_range *range;
7762 
7763 	TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
7764 		if (ctx->range.offset == range->offset &&
7765 		    ctx->range.length == range->length &&
7766 		    ctx->range.locked_ctx == range->locked_ctx) {
7767 			TAILQ_REMOVE(&ch->locked_ranges, range, tailq);
7768 			free(range);
7769 			break;
7770 		}
7771 	}
7772 
7773 	/* Note: we should almost always be able to assert that the range specified
7774 	 * was found.  But there are some very rare corner cases where a new channel
7775 	 * gets created simultaneously with a range unlock, where this function
7776 	 * would execute on that new channel and wouldn't have the range.
7777 	 * We also use this to clean up range allocations when a later allocation
7778 	 * fails in the locking path.
7779 	 * So we can't actually assert() here.
7780 	 */
7781 
7782 	/* Swap the locked IO into a temporary list, and then try to submit them again.
7783 	 * We could hyper-optimize this to only resubmit locked I/O that overlap
7784 	 * with the range that was just unlocked, but this isn't a performance path so
7785 	 * we go for simplicity here.
7786 	 */
7787 	TAILQ_INIT(&io_locked);
7788 	TAILQ_SWAP(&ch->io_locked, &io_locked, spdk_bdev_io, internal.ch_link);
7789 	while (!TAILQ_EMPTY(&io_locked)) {
7790 		bdev_io = TAILQ_FIRST(&io_locked);
7791 		TAILQ_REMOVE(&io_locked, bdev_io, internal.ch_link);
7792 		bdev_io_submit(bdev_io);
7793 	}
7794 
7795 	spdk_for_each_channel_continue(i, 0);
7796 }
7797 
7798 static int
7799 bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
7800 		      uint64_t offset, uint64_t length,
7801 		      lock_range_cb cb_fn, void *cb_arg)
7802 {
7803 	struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7804 	struct spdk_bdev_channel *ch = spdk_io_channel_get_ctx(_ch);
7805 	struct locked_lba_range_ctx *ctx;
7806 	struct lba_range *range;
7807 	bool range_found = false;
7808 
7809 	/* Let's make sure the specified channel actually has a lock on
7810 	 * the specified range.  Note that the range must match exactly.
7811 	 */
7812 	TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
7813 		if (range->offset == offset && range->length == length &&
7814 		    range->owner_ch == ch && range->locked_ctx == cb_arg) {
7815 			range_found = true;
7816 			break;
7817 		}
7818 	}
7819 
7820 	if (!range_found) {
7821 		return -EINVAL;
7822 	}
7823 
7824 	pthread_mutex_lock(&bdev->internal.mutex);
7825 	/* We confirmed that this channel has locked the specified range.  To
7826 	 * start the unlock the process, we find the range in the bdev's locked_ranges
7827 	 * and remove it.  This ensures new channels don't inherit the locked range.
7828 	 * Then we will send a message to each channel (including the one specified
7829 	 * here) to remove the range from its per-channel list.
7830 	 */
7831 	TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) {
7832 		if (range->offset == offset && range->length == length &&
7833 		    range->locked_ctx == cb_arg) {
7834 			break;
7835 		}
7836 	}
7837 	if (range == NULL) {
7838 		assert(false);
7839 		pthread_mutex_unlock(&bdev->internal.mutex);
7840 		return -EINVAL;
7841 	}
7842 	TAILQ_REMOVE(&bdev->internal.locked_ranges, range, tailq);
7843 	ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range);
7844 	pthread_mutex_unlock(&bdev->internal.mutex);
7845 
7846 	ctx->cb_fn = cb_fn;
7847 	ctx->cb_arg = cb_arg;
7848 
7849 	spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_unlock_lba_range_get_channel, ctx,
7850 			      bdev_unlock_lba_range_cb);
7851 	return 0;
7852 }
7853 
7854 int
7855 spdk_bdev_get_memory_domains(struct spdk_bdev *bdev, struct spdk_memory_domain **domains,
7856 			     int array_size)
7857 {
7858 	if (!bdev) {
7859 		return -EINVAL;
7860 	}
7861 
7862 	if (bdev->fn_table->get_memory_domains) {
7863 		return bdev->fn_table->get_memory_domains(bdev->ctxt, domains, array_size);
7864 	}
7865 
7866 	return 0;
7867 }
7868 
7869 struct spdk_bdev_for_each_io_ctx {
7870 	void *ctx;
7871 	spdk_bdev_io_fn fn;
7872 	spdk_bdev_for_each_io_cb cb;
7873 };
7874 
7875 static void
7876 bdev_channel_for_each_io(struct spdk_io_channel_iter *i)
7877 {
7878 	struct spdk_bdev_for_each_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7879 	struct spdk_io_channel *io_ch = spdk_io_channel_iter_get_channel(i);
7880 	struct spdk_bdev_channel *bdev_ch = spdk_io_channel_get_ctx(io_ch);
7881 	struct spdk_bdev_io *bdev_io;
7882 	int rc = 0;
7883 
7884 	TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) {
7885 		rc = ctx->fn(ctx->ctx, bdev_io);
7886 		if (rc != 0) {
7887 			break;
7888 		}
7889 	}
7890 
7891 	spdk_for_each_channel_continue(i, rc);
7892 }
7893 
7894 static void
7895 bdev_for_each_io_done(struct spdk_io_channel_iter *i, int status)
7896 {
7897 	struct spdk_bdev_for_each_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
7898 
7899 	ctx->cb(ctx->ctx, status);
7900 
7901 	free(ctx);
7902 }
7903 
7904 void
7905 spdk_bdev_for_each_bdev_io(struct spdk_bdev *bdev, void *_ctx, spdk_bdev_io_fn fn,
7906 			   spdk_bdev_for_each_io_cb cb)
7907 {
7908 	struct spdk_bdev_for_each_io_ctx *ctx;
7909 
7910 	assert(fn != NULL && cb != NULL);
7911 
7912 	ctx = calloc(1, sizeof(*ctx));
7913 	if (ctx == NULL) {
7914 		SPDK_ERRLOG("Failed to allocate context.\n");
7915 		cb(_ctx, -ENOMEM);
7916 		return;
7917 	}
7918 
7919 	ctx->ctx = _ctx;
7920 	ctx->fn = fn;
7921 	ctx->cb = cb;
7922 
7923 	spdk_for_each_channel(__bdev_to_io_dev(bdev),
7924 			      bdev_channel_for_each_io,
7925 			      ctx,
7926 			      bdev_for_each_io_done);
7927 }
7928 
7929 SPDK_LOG_REGISTER_COMPONENT(bdev)
7930 
7931 SPDK_TRACE_REGISTER_FN(bdev_trace, "bdev", TRACE_GROUP_BDEV)
7932 {
7933 	struct spdk_trace_tpoint_opts opts[] = {
7934 		{
7935 			"BDEV_IO_START", TRACE_BDEV_IO_START,
7936 			OWNER_BDEV, OBJECT_BDEV_IO, 1,
7937 			{
7938 				{ "type", SPDK_TRACE_ARG_TYPE_INT, 8 },
7939 				{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
7940 				{ "offset", SPDK_TRACE_ARG_TYPE_INT, 8 },
7941 				{ "len", SPDK_TRACE_ARG_TYPE_INT, 8 }
7942 			}
7943 		},
7944 		{
7945 			"BDEV_IO_DONE", TRACE_BDEV_IO_DONE,
7946 			OWNER_BDEV, OBJECT_BDEV_IO, 0,
7947 			{{ "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 }}
7948 		},
7949 		{
7950 			"BDEV_IOCH_CREATE", TRACE_BDEV_IOCH_CREATE,
7951 			OWNER_BDEV, OBJECT_NONE, 1,
7952 			{
7953 				{ "name", SPDK_TRACE_ARG_TYPE_STR, 40 },
7954 				{ "thread_id", SPDK_TRACE_ARG_TYPE_INT, 8}
7955 			}
7956 		},
7957 		{
7958 			"BDEV_IOCH_DESTROY", TRACE_BDEV_IOCH_DESTROY,
7959 			OWNER_BDEV, OBJECT_NONE, 0,
7960 			{
7961 				{ "name", SPDK_TRACE_ARG_TYPE_STR, 40 },
7962 				{ "thread_id", SPDK_TRACE_ARG_TYPE_INT, 8}
7963 			}
7964 		},
7965 	};
7966 
7967 
7968 	spdk_trace_register_owner(OWNER_BDEV, 'b');
7969 	spdk_trace_register_object(OBJECT_BDEV_IO, 'i');
7970 	spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
7971 	spdk_trace_tpoint_register_relation(TRACE_BDEV_NVME_IO_START, OBJECT_BDEV_IO, 0);
7972 	spdk_trace_tpoint_register_relation(TRACE_BDEV_NVME_IO_DONE, OBJECT_BDEV_IO, 0);
7973 }
7974