Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4 : * Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : */
6 :
7 : #include "spdk/stdinc.h"
8 :
9 : #include "spdk/bdev.h"
10 :
11 : #include "spdk/accel.h"
12 : #include "spdk/config.h"
13 : #include "spdk/env.h"
14 : #include "spdk/thread.h"
15 : #include "spdk/likely.h"
16 : #include "spdk/queue.h"
17 : #include "spdk/nvme_spec.h"
18 : #include "spdk/scsi_spec.h"
19 : #include "spdk/notify.h"
20 : #include "spdk/util.h"
21 : #include "spdk/trace.h"
22 : #include "spdk/dma.h"
23 :
24 : #include "spdk/bdev_module.h"
25 : #include "spdk/log.h"
26 : #include "spdk/string.h"
27 :
28 : #include "bdev_internal.h"
29 : #include "spdk_internal/trace_defs.h"
30 : #include "spdk_internal/assert.h"
31 :
32 : #ifdef SPDK_CONFIG_VTUNE
33 : #include "ittnotify.h"
34 : #include "ittnotify_types.h"
35 : int __itt_init_ittlib(const char *, __itt_group_id);
36 : #endif
37 :
38 : #define SPDK_BDEV_IO_POOL_SIZE (64 * 1024 - 1)
39 : #define SPDK_BDEV_IO_CACHE_SIZE 256
40 : #define SPDK_BDEV_AUTO_EXAMINE true
41 : #define BUF_SMALL_CACHE_SIZE 128
42 : #define BUF_LARGE_CACHE_SIZE 16
43 : #define NOMEM_THRESHOLD_COUNT 8
44 :
45 : #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC 1000
46 : #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE 1
47 : #define SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE 512
48 : #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC 1000
49 : #define SPDK_BDEV_QOS_MIN_BYTES_PER_SEC (1024 * 1024)
50 : #define SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC (UINT64_MAX / (1024 * 1024))
51 : #define SPDK_BDEV_QOS_LIMIT_NOT_DEFINED UINT64_MAX
52 : #define SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC 1000
53 :
54 : /* The maximum number of children requests for a UNMAP or WRITE ZEROES command
55 : * when splitting into children requests at a time.
56 : */
57 : #define SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS (8)
58 : #define BDEV_RESET_CHECK_OUTSTANDING_IO_PERIOD 1000000
59 :
60 : /* The maximum number of children requests for a COPY command
61 : * when splitting into children requests at a time.
62 : */
63 : #define SPDK_BDEV_MAX_CHILDREN_COPY_REQS (8)
64 :
65 : #define LOG_ALREADY_CLAIMED_ERROR(detail, bdev) \
66 : log_already_claimed(SPDK_LOG_ERROR, __LINE__, __func__, detail, bdev)
67 : #ifdef DEBUG
68 : #define LOG_ALREADY_CLAIMED_DEBUG(detail, bdev) \
69 : log_already_claimed(SPDK_LOG_DEBUG, __LINE__, __func__, detail, bdev)
70 : #else
71 : #define LOG_ALREADY_CLAIMED_DEBUG(detail, bdev) do {} while(0)
72 : #endif
73 :
74 : static void log_already_claimed(enum spdk_log_level level, const int line, const char *func,
75 : const char *detail, struct spdk_bdev *bdev);
76 :
77 : static const char *qos_rpc_type[] = {"rw_ios_per_sec",
78 : "rw_mbytes_per_sec", "r_mbytes_per_sec", "w_mbytes_per_sec"
79 : };
80 :
81 : TAILQ_HEAD(spdk_bdev_list, spdk_bdev);
82 :
83 : RB_HEAD(bdev_name_tree, spdk_bdev_name);
84 :
85 : static int
86 551 : bdev_name_cmp(struct spdk_bdev_name *name1, struct spdk_bdev_name *name2)
87 : {
88 551 : return strcmp(name1->name, name2->name);
89 : }
90 :
91 2037 : RB_GENERATE_STATIC(bdev_name_tree, spdk_bdev_name, node, bdev_name_cmp);
92 :
93 : struct spdk_bdev_mgr {
94 : struct spdk_mempool *bdev_io_pool;
95 :
96 : void *zero_buffer;
97 :
98 : TAILQ_HEAD(bdev_module_list, spdk_bdev_module) bdev_modules;
99 :
100 : struct spdk_bdev_list bdevs;
101 : struct bdev_name_tree bdev_names;
102 :
103 : bool init_complete;
104 : bool module_init_complete;
105 :
106 : struct spdk_spinlock spinlock;
107 :
108 : TAILQ_HEAD(, spdk_bdev_open_async_ctx) async_bdev_opens;
109 :
110 : #ifdef SPDK_CONFIG_VTUNE
111 : __itt_domain *domain;
112 : #endif
113 : };
114 :
115 : static struct spdk_bdev_mgr g_bdev_mgr = {
116 : .bdev_modules = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdev_modules),
117 : .bdevs = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.bdevs),
118 : .bdev_names = RB_INITIALIZER(g_bdev_mgr.bdev_names),
119 : .init_complete = false,
120 : .module_init_complete = false,
121 : .async_bdev_opens = TAILQ_HEAD_INITIALIZER(g_bdev_mgr.async_bdev_opens),
122 : };
123 :
124 : static void
125 : __attribute__((constructor))
126 3 : _bdev_init(void)
127 : {
128 3 : spdk_spin_init(&g_bdev_mgr.spinlock);
129 3 : }
130 :
131 : typedef void (*lock_range_cb)(struct lba_range *range, void *ctx, int status);
132 :
133 : typedef void (*bdev_copy_bounce_buffer_cpl)(void *ctx, int rc);
134 :
135 : struct lba_range {
136 : struct spdk_bdev *bdev;
137 : uint64_t offset;
138 : uint64_t length;
139 : bool quiesce;
140 : void *locked_ctx;
141 : struct spdk_thread *owner_thread;
142 : struct spdk_bdev_channel *owner_ch;
143 : TAILQ_ENTRY(lba_range) tailq;
144 : TAILQ_ENTRY(lba_range) tailq_module;
145 : };
146 :
147 : static struct spdk_bdev_opts g_bdev_opts = {
148 : .bdev_io_pool_size = SPDK_BDEV_IO_POOL_SIZE,
149 : .bdev_io_cache_size = SPDK_BDEV_IO_CACHE_SIZE,
150 : .bdev_auto_examine = SPDK_BDEV_AUTO_EXAMINE,
151 : .iobuf_small_cache_size = BUF_SMALL_CACHE_SIZE,
152 : .iobuf_large_cache_size = BUF_LARGE_CACHE_SIZE,
153 : };
154 :
155 : static spdk_bdev_init_cb g_init_cb_fn = NULL;
156 : static void *g_init_cb_arg = NULL;
157 :
158 : static spdk_bdev_fini_cb g_fini_cb_fn = NULL;
159 : static void *g_fini_cb_arg = NULL;
160 : static struct spdk_thread *g_fini_thread = NULL;
161 :
162 : struct spdk_bdev_qos_limit {
163 : /** IOs or bytes allowed per second (i.e., 1s). */
164 : uint64_t limit;
165 :
166 : /** Remaining IOs or bytes allowed in current timeslice (e.g., 1ms).
167 : * For remaining bytes, allowed to run negative if an I/O is submitted when
168 : * some bytes are remaining, but the I/O is bigger than that amount. The
169 : * excess will be deducted from the next timeslice.
170 : */
171 : int64_t remaining_this_timeslice;
172 :
173 : /** Minimum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */
174 : uint32_t min_per_timeslice;
175 :
176 : /** Maximum allowed IOs or bytes to be issued in one timeslice (e.g., 1ms). */
177 : uint32_t max_per_timeslice;
178 :
179 : /** Function to check whether to queue the IO.
180 : * If The IO is allowed to pass, the quota will be reduced correspondingly.
181 : */
182 : bool (*queue_io)(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io);
183 :
184 : /** Function to rewind the quota once the IO was allowed to be sent by this
185 : * limit but queued due to one of the further limits.
186 : */
187 : void (*rewind_quota)(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io);
188 : };
189 :
190 : struct spdk_bdev_qos {
191 : /** Types of structure of rate limits. */
192 : struct spdk_bdev_qos_limit rate_limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
193 :
194 : /** The channel that all I/O are funneled through. */
195 : struct spdk_bdev_channel *ch;
196 :
197 : /** The thread on which the poller is running. */
198 : struct spdk_thread *thread;
199 :
200 : /** Size of a timeslice in tsc ticks. */
201 : uint64_t timeslice_size;
202 :
203 : /** Timestamp of start of last timeslice. */
204 : uint64_t last_timeslice;
205 :
206 : /** Poller that processes queued I/O commands each time slice. */
207 : struct spdk_poller *poller;
208 : };
209 :
210 : struct spdk_bdev_mgmt_channel {
211 : /*
212 : * Each thread keeps a cache of bdev_io - this allows
213 : * bdev threads which are *not* DPDK threads to still
214 : * benefit from a per-thread bdev_io cache. Without
215 : * this, non-DPDK threads fetching from the mempool
216 : * incur a cmpxchg on get and put.
217 : */
218 : bdev_io_stailq_t per_thread_cache;
219 : uint32_t per_thread_cache_count;
220 : uint32_t bdev_io_cache_size;
221 :
222 : struct spdk_iobuf_channel iobuf;
223 :
224 : TAILQ_HEAD(, spdk_bdev_shared_resource) shared_resources;
225 : TAILQ_HEAD(, spdk_bdev_io_wait_entry) io_wait_queue;
226 : };
227 :
228 : /*
229 : * Per-module (or per-io_device) data. Multiple bdevs built on the same io_device
230 : * will queue here their IO that awaits retry. It makes it possible to retry sending
231 : * IO to one bdev after IO from other bdev completes.
232 : */
233 : struct spdk_bdev_shared_resource {
234 : /* The bdev management channel */
235 : struct spdk_bdev_mgmt_channel *mgmt_ch;
236 :
237 : /*
238 : * Count of I/O submitted to bdev module and waiting for completion.
239 : * Incremented before submit_request() is called on an spdk_bdev_io.
240 : */
241 : uint64_t io_outstanding;
242 :
243 : /*
244 : * Queue of IO awaiting retry because of a previous NOMEM status returned
245 : * on this channel.
246 : */
247 : bdev_io_tailq_t nomem_io;
248 :
249 : /*
250 : * Threshold which io_outstanding must drop to before retrying nomem_io.
251 : */
252 : uint64_t nomem_threshold;
253 :
254 : /* I/O channel allocated by a bdev module */
255 : struct spdk_io_channel *shared_ch;
256 :
257 : struct spdk_poller *nomem_poller;
258 :
259 : /* Refcount of bdev channels using this resource */
260 : uint32_t ref;
261 :
262 : TAILQ_ENTRY(spdk_bdev_shared_resource) link;
263 : };
264 :
265 : #define BDEV_CH_RESET_IN_PROGRESS (1 << 0)
266 : #define BDEV_CH_QOS_ENABLED (1 << 1)
267 :
268 : struct spdk_bdev_channel {
269 : struct spdk_bdev *bdev;
270 :
271 : /* The channel for the underlying device */
272 : struct spdk_io_channel *channel;
273 :
274 : /* Accel channel */
275 : struct spdk_io_channel *accel_channel;
276 :
277 : /* Per io_device per thread data */
278 : struct spdk_bdev_shared_resource *shared_resource;
279 :
280 : struct spdk_bdev_io_stat *stat;
281 :
282 : /*
283 : * Count of I/O submitted to the underlying dev module through this channel
284 : * and waiting for completion.
285 : */
286 : uint64_t io_outstanding;
287 :
288 : /*
289 : * List of all submitted I/Os including I/O that are generated via splitting.
290 : */
291 : bdev_io_tailq_t io_submitted;
292 :
293 : /*
294 : * List of spdk_bdev_io that are currently queued because they write to a locked
295 : * LBA range.
296 : */
297 : bdev_io_tailq_t io_locked;
298 :
299 : /* List of I/Os with accel sequence being currently executed */
300 : bdev_io_tailq_t io_accel_exec;
301 :
302 : /* List of I/Os doing memory domain pull/push */
303 : bdev_io_tailq_t io_memory_domain;
304 :
305 : uint32_t flags;
306 :
307 : /* Counts number of bdev_io in the io_submitted TAILQ */
308 : uint16_t queue_depth;
309 :
310 : uint16_t trace_id;
311 :
312 : struct spdk_histogram_data *histogram;
313 :
314 : #ifdef SPDK_CONFIG_VTUNE
315 : uint64_t start_tsc;
316 : uint64_t interval_tsc;
317 : __itt_string_handle *handle;
318 : struct spdk_bdev_io_stat *prev_stat;
319 : #endif
320 :
321 : bdev_io_tailq_t queued_resets;
322 :
323 : lba_range_tailq_t locked_ranges;
324 :
325 : /** List of I/Os queued by QoS. */
326 : bdev_io_tailq_t qos_queued_io;
327 : };
328 :
329 : struct media_event_entry {
330 : struct spdk_bdev_media_event event;
331 : TAILQ_ENTRY(media_event_entry) tailq;
332 : };
333 :
334 : #define MEDIA_EVENT_POOL_SIZE 64
335 :
336 : struct spdk_bdev_desc {
337 : struct spdk_bdev *bdev;
338 : bool write;
339 : bool memory_domains_supported;
340 : bool accel_sequence_supported[SPDK_BDEV_NUM_IO_TYPES];
341 : struct spdk_bdev_open_opts opts;
342 : struct spdk_thread *thread;
343 : struct {
344 : spdk_bdev_event_cb_t event_fn;
345 : void *ctx;
346 : } callback;
347 : bool closed;
348 : struct spdk_spinlock spinlock;
349 : uint32_t refs;
350 : TAILQ_HEAD(, media_event_entry) pending_media_events;
351 : TAILQ_HEAD(, media_event_entry) free_media_events;
352 : struct media_event_entry *media_events_buffer;
353 : TAILQ_ENTRY(spdk_bdev_desc) link;
354 :
355 : uint64_t timeout_in_sec;
356 : spdk_bdev_io_timeout_cb cb_fn;
357 : void *cb_arg;
358 : struct spdk_poller *io_timeout_poller;
359 : struct spdk_bdev_module_claim *claim;
360 : };
361 :
362 : struct spdk_bdev_iostat_ctx {
363 : struct spdk_bdev_io_stat *stat;
364 : enum spdk_bdev_reset_stat_mode reset_mode;
365 : spdk_bdev_get_device_stat_cb cb;
366 : void *cb_arg;
367 : };
368 :
369 : struct set_qos_limit_ctx {
370 : void (*cb_fn)(void *cb_arg, int status);
371 : void *cb_arg;
372 : struct spdk_bdev *bdev;
373 : };
374 :
375 : struct spdk_bdev_channel_iter {
376 : spdk_bdev_for_each_channel_msg fn;
377 : spdk_bdev_for_each_channel_done cpl;
378 : struct spdk_io_channel_iter *i;
379 : void *ctx;
380 : };
381 :
382 : struct spdk_bdev_io_error_stat {
383 : uint32_t error_status[-SPDK_MIN_BDEV_IO_STATUS];
384 : };
385 :
386 : enum bdev_io_retry_state {
387 : BDEV_IO_RETRY_STATE_INVALID,
388 : BDEV_IO_RETRY_STATE_PULL,
389 : BDEV_IO_RETRY_STATE_PULL_MD,
390 : BDEV_IO_RETRY_STATE_SUBMIT,
391 : BDEV_IO_RETRY_STATE_PUSH,
392 : BDEV_IO_RETRY_STATE_PUSH_MD,
393 : };
394 :
395 : #define __bdev_to_io_dev(bdev) (((char *)bdev) + 1)
396 : #define __bdev_from_io_dev(io_dev) ((struct spdk_bdev *)(((char *)io_dev) - 1))
397 : #define __io_ch_to_bdev_ch(io_ch) ((struct spdk_bdev_channel *)spdk_io_channel_get_ctx(io_ch))
398 : #define __io_ch_to_bdev_mgmt_ch(io_ch) ((struct spdk_bdev_mgmt_channel *)spdk_io_channel_get_ctx(io_ch))
399 :
400 : static inline void bdev_io_complete(void *ctx);
401 : static inline void bdev_io_complete_unsubmitted(struct spdk_bdev_io *bdev_io);
402 : static void bdev_io_push_bounce_md_buf(struct spdk_bdev_io *bdev_io);
403 : static void bdev_io_push_bounce_data(struct spdk_bdev_io *bdev_io);
404 :
405 : static void bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
406 : static int bdev_write_zero_buffer(struct spdk_bdev_io *bdev_io);
407 :
408 : static void bdev_enable_qos_msg(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
409 : struct spdk_io_channel *ch, void *_ctx);
410 : static void bdev_enable_qos_done(struct spdk_bdev *bdev, void *_ctx, int status);
411 :
412 : static int bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
413 : struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
414 : uint64_t num_blocks,
415 : struct spdk_memory_domain *domain, void *domain_ctx,
416 : struct spdk_accel_sequence *seq, uint32_t dif_check_flags,
417 : spdk_bdev_io_completion_cb cb, void *cb_arg);
418 : static int bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
419 : struct iovec *iov, int iovcnt, void *md_buf,
420 : uint64_t offset_blocks, uint64_t num_blocks,
421 : struct spdk_memory_domain *domain, void *domain_ctx,
422 : struct spdk_accel_sequence *seq, uint32_t dif_check_flags,
423 : uint32_t nvme_cdw12_raw, uint32_t nvme_cdw13_raw,
424 : spdk_bdev_io_completion_cb cb, void *cb_arg);
425 :
426 : static int bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
427 : uint64_t offset, uint64_t length,
428 : lock_range_cb cb_fn, void *cb_arg);
429 :
430 : static int bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
431 : uint64_t offset, uint64_t length,
432 : lock_range_cb cb_fn, void *cb_arg);
433 :
434 : static bool bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort);
435 : static bool bdev_abort_buf_io(struct spdk_bdev_mgmt_channel *ch, struct spdk_bdev_io *bio_to_abort);
436 :
437 : static bool claim_type_is_v2(enum spdk_bdev_claim_type type);
438 : static void bdev_desc_release_claims(struct spdk_bdev_desc *desc);
439 : static void claim_reset(struct spdk_bdev *bdev);
440 :
441 : static void bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch);
442 :
443 : static bool bdev_io_should_split(struct spdk_bdev_io *bdev_io);
444 :
445 : #define bdev_get_ext_io_opt(opts, field, defval) \
446 : ((opts) != NULL ? SPDK_GET_FIELD(opts, field, defval) : (defval))
447 :
448 : static inline void
449 669 : bdev_ch_add_to_io_submitted(struct spdk_bdev_io *bdev_io)
450 : {
451 669 : TAILQ_INSERT_TAIL(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link);
452 669 : bdev_io->internal.ch->queue_depth++;
453 669 : }
454 :
455 : static inline void
456 669 : bdev_ch_remove_from_io_submitted(struct spdk_bdev_io *bdev_io)
457 : {
458 669 : TAILQ_REMOVE(&bdev_io->internal.ch->io_submitted, bdev_io, internal.ch_link);
459 669 : bdev_io->internal.ch->queue_depth--;
460 669 : }
461 :
462 : void
463 14 : spdk_bdev_get_opts(struct spdk_bdev_opts *opts, size_t opts_size)
464 : {
465 14 : if (!opts) {
466 0 : SPDK_ERRLOG("opts should not be NULL\n");
467 0 : return;
468 : }
469 :
470 14 : if (!opts_size) {
471 0 : SPDK_ERRLOG("opts_size should not be zero value\n");
472 0 : return;
473 : }
474 :
475 14 : opts->opts_size = opts_size;
476 :
477 : #define SET_FIELD(field) \
478 : if (offsetof(struct spdk_bdev_opts, field) + sizeof(opts->field) <= opts_size) { \
479 : opts->field = g_bdev_opts.field; \
480 : } \
481 :
482 14 : SET_FIELD(bdev_io_pool_size);
483 14 : SET_FIELD(bdev_io_cache_size);
484 14 : SET_FIELD(bdev_auto_examine);
485 14 : SET_FIELD(iobuf_small_cache_size);
486 14 : SET_FIELD(iobuf_large_cache_size);
487 :
488 : /* Do not remove this statement, you should always update this statement when you adding a new field,
489 : * and do not forget to add the SET_FIELD statement for your added field. */
490 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_opts) == 32, "Incorrect size");
491 :
492 : #undef SET_FIELD
493 14 : }
494 :
495 : int
496 15 : spdk_bdev_set_opts(struct spdk_bdev_opts *opts)
497 : {
498 : uint32_t min_pool_size;
499 :
500 15 : if (!opts) {
501 0 : SPDK_ERRLOG("opts cannot be NULL\n");
502 0 : return -1;
503 : }
504 :
505 15 : if (!opts->opts_size) {
506 1 : SPDK_ERRLOG("opts_size inside opts cannot be zero value\n");
507 1 : return -1;
508 : }
509 :
510 : /*
511 : * Add 1 to the thread count to account for the extra mgmt_ch that gets created during subsystem
512 : * initialization. A second mgmt_ch will be created on the same thread when the application starts
513 : * but before the deferred put_io_channel event is executed for the first mgmt_ch.
514 : */
515 14 : min_pool_size = opts->bdev_io_cache_size * (spdk_thread_get_count() + 1);
516 14 : if (opts->bdev_io_pool_size < min_pool_size) {
517 0 : SPDK_ERRLOG("bdev_io_pool_size %" PRIu32 " is not compatible with bdev_io_cache_size %" PRIu32
518 : " and %" PRIu32 " threads\n", opts->bdev_io_pool_size, opts->bdev_io_cache_size,
519 : spdk_thread_get_count());
520 0 : SPDK_ERRLOG("bdev_io_pool_size must be at least %" PRIu32 "\n", min_pool_size);
521 0 : return -1;
522 : }
523 :
524 : #define SET_FIELD(field) \
525 : if (offsetof(struct spdk_bdev_opts, field) + sizeof(opts->field) <= opts->opts_size) { \
526 : g_bdev_opts.field = opts->field; \
527 : } \
528 :
529 14 : SET_FIELD(bdev_io_pool_size);
530 14 : SET_FIELD(bdev_io_cache_size);
531 14 : SET_FIELD(bdev_auto_examine);
532 14 : SET_FIELD(iobuf_small_cache_size);
533 14 : SET_FIELD(iobuf_large_cache_size);
534 :
535 14 : g_bdev_opts.opts_size = opts->opts_size;
536 :
537 : #undef SET_FIELD
538 :
539 14 : return 0;
540 15 : }
541 :
542 : static struct spdk_bdev *
543 146 : bdev_get_by_name(const char *bdev_name)
544 : {
545 : struct spdk_bdev_name find;
546 : struct spdk_bdev_name *res;
547 :
548 146 : find.name = (char *)bdev_name;
549 146 : res = RB_FIND(bdev_name_tree, &g_bdev_mgr.bdev_names, &find);
550 146 : if (res != NULL) {
551 139 : return res->bdev;
552 : }
553 :
554 7 : return NULL;
555 146 : }
556 :
557 : struct spdk_bdev *
558 16 : spdk_bdev_get_by_name(const char *bdev_name)
559 : {
560 : struct spdk_bdev *bdev;
561 :
562 16 : spdk_spin_lock(&g_bdev_mgr.spinlock);
563 16 : bdev = bdev_get_by_name(bdev_name);
564 16 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
565 :
566 16 : return bdev;
567 : }
568 :
569 : struct bdev_io_status_string {
570 : enum spdk_bdev_io_status status;
571 : const char *str;
572 : };
573 :
574 : static const struct bdev_io_status_string bdev_io_status_strings[] = {
575 : { SPDK_BDEV_IO_STATUS_AIO_ERROR, "aio_error" },
576 : { SPDK_BDEV_IO_STATUS_ABORTED, "aborted" },
577 : { SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED, "first_fused_failed" },
578 : { SPDK_BDEV_IO_STATUS_MISCOMPARE, "miscompare" },
579 : { SPDK_BDEV_IO_STATUS_NOMEM, "nomem" },
580 : { SPDK_BDEV_IO_STATUS_SCSI_ERROR, "scsi_error" },
581 : { SPDK_BDEV_IO_STATUS_NVME_ERROR, "nvme_error" },
582 : { SPDK_BDEV_IO_STATUS_FAILED, "failed" },
583 : { SPDK_BDEV_IO_STATUS_PENDING, "pending" },
584 : { SPDK_BDEV_IO_STATUS_SUCCESS, "success" },
585 : };
586 :
587 : static const char *
588 0 : bdev_io_status_get_string(enum spdk_bdev_io_status status)
589 : {
590 : uint32_t i;
591 :
592 0 : for (i = 0; i < SPDK_COUNTOF(bdev_io_status_strings); i++) {
593 0 : if (bdev_io_status_strings[i].status == status) {
594 0 : return bdev_io_status_strings[i].str;
595 : }
596 0 : }
597 :
598 0 : return "reserved";
599 0 : }
600 :
601 : struct spdk_bdev_wait_for_examine_ctx {
602 : struct spdk_poller *poller;
603 : spdk_bdev_wait_for_examine_cb cb_fn;
604 : void *cb_arg;
605 : };
606 :
607 : static bool bdev_module_all_actions_completed(void);
608 :
609 : static int
610 192 : bdev_wait_for_examine_cb(void *arg)
611 : {
612 192 : struct spdk_bdev_wait_for_examine_ctx *ctx = arg;
613 :
614 192 : if (!bdev_module_all_actions_completed()) {
615 0 : return SPDK_POLLER_IDLE;
616 : }
617 :
618 192 : spdk_poller_unregister(&ctx->poller);
619 192 : ctx->cb_fn(ctx->cb_arg);
620 192 : free(ctx);
621 :
622 192 : return SPDK_POLLER_BUSY;
623 192 : }
624 :
625 : int
626 192 : spdk_bdev_wait_for_examine(spdk_bdev_wait_for_examine_cb cb_fn, void *cb_arg)
627 : {
628 : struct spdk_bdev_wait_for_examine_ctx *ctx;
629 :
630 192 : ctx = calloc(1, sizeof(*ctx));
631 192 : if (ctx == NULL) {
632 0 : return -ENOMEM;
633 : }
634 192 : ctx->cb_fn = cb_fn;
635 192 : ctx->cb_arg = cb_arg;
636 192 : ctx->poller = SPDK_POLLER_REGISTER(bdev_wait_for_examine_cb, ctx, 0);
637 :
638 192 : return 0;
639 192 : }
640 :
641 : struct spdk_bdev_examine_item {
642 : char *name;
643 : TAILQ_ENTRY(spdk_bdev_examine_item) link;
644 : };
645 :
646 : TAILQ_HEAD(spdk_bdev_examine_allowlist, spdk_bdev_examine_item);
647 :
648 : struct spdk_bdev_examine_allowlist g_bdev_examine_allowlist = TAILQ_HEAD_INITIALIZER(
649 : g_bdev_examine_allowlist);
650 :
651 : static inline bool
652 6 : bdev_examine_allowlist_check(const char *name)
653 : {
654 : struct spdk_bdev_examine_item *item;
655 6 : TAILQ_FOREACH(item, &g_bdev_examine_allowlist, link) {
656 0 : if (strcmp(name, item->name) == 0) {
657 0 : return true;
658 : }
659 0 : }
660 6 : return false;
661 6 : }
662 :
663 : static inline void
664 65 : bdev_examine_allowlist_free(void)
665 : {
666 : struct spdk_bdev_examine_item *item;
667 65 : while (!TAILQ_EMPTY(&g_bdev_examine_allowlist)) {
668 0 : item = TAILQ_FIRST(&g_bdev_examine_allowlist);
669 0 : TAILQ_REMOVE(&g_bdev_examine_allowlist, item, link);
670 0 : free(item->name);
671 0 : free(item);
672 : }
673 65 : }
674 :
675 : static inline bool
676 3 : bdev_in_examine_allowlist(struct spdk_bdev *bdev)
677 : {
678 : struct spdk_bdev_alias *tmp;
679 3 : if (bdev_examine_allowlist_check(bdev->name)) {
680 0 : return true;
681 : }
682 6 : TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
683 3 : if (bdev_examine_allowlist_check(tmp->alias.name)) {
684 0 : return true;
685 : }
686 3 : }
687 3 : return false;
688 3 : }
689 :
690 : static inline bool
691 125 : bdev_ok_to_examine(struct spdk_bdev *bdev)
692 : {
693 : /* Some bdevs may not support the READ command.
694 : * Do not try to examine them.
695 : */
696 125 : if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_READ)) {
697 0 : return false;
698 : }
699 :
700 125 : if (g_bdev_opts.bdev_auto_examine) {
701 122 : return true;
702 : } else {
703 3 : return bdev_in_examine_allowlist(bdev);
704 : }
705 125 : }
706 :
707 : static void
708 125 : bdev_examine(struct spdk_bdev *bdev)
709 : {
710 : struct spdk_bdev_module *module;
711 : struct spdk_bdev_module_claim *claim, *tmpclaim;
712 : uint32_t action;
713 :
714 125 : if (!bdev_ok_to_examine(bdev)) {
715 3 : return;
716 : }
717 :
718 499 : TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
719 377 : if (module->examine_config) {
720 255 : spdk_spin_lock(&module->internal.spinlock);
721 255 : action = module->internal.action_in_progress;
722 255 : module->internal.action_in_progress++;
723 255 : spdk_spin_unlock(&module->internal.spinlock);
724 255 : module->examine_config(bdev);
725 255 : if (action != module->internal.action_in_progress) {
726 0 : SPDK_ERRLOG("examine_config for module %s did not call "
727 : "spdk_bdev_module_examine_done()\n", module->name);
728 0 : }
729 255 : }
730 377 : }
731 :
732 122 : spdk_spin_lock(&bdev->internal.spinlock);
733 :
734 122 : switch (bdev->internal.claim_type) {
735 : case SPDK_BDEV_CLAIM_NONE:
736 : /* Examine by all bdev modules */
737 474 : TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
738 357 : if (module->examine_disk) {
739 231 : spdk_spin_lock(&module->internal.spinlock);
740 231 : module->internal.action_in_progress++;
741 231 : spdk_spin_unlock(&module->internal.spinlock);
742 231 : spdk_spin_unlock(&bdev->internal.spinlock);
743 231 : module->examine_disk(bdev);
744 231 : spdk_spin_lock(&bdev->internal.spinlock);
745 231 : }
746 357 : }
747 117 : break;
748 : case SPDK_BDEV_CLAIM_EXCL_WRITE:
749 : /* Examine by the one bdev module with a v1 claim */
750 1 : module = bdev->internal.claim.v1.module;
751 1 : if (module->examine_disk) {
752 1 : spdk_spin_lock(&module->internal.spinlock);
753 1 : module->internal.action_in_progress++;
754 1 : spdk_spin_unlock(&module->internal.spinlock);
755 1 : spdk_spin_unlock(&bdev->internal.spinlock);
756 1 : module->examine_disk(bdev);
757 1 : return;
758 : }
759 0 : break;
760 : default:
761 : /* Examine by all bdev modules with a v2 claim */
762 4 : assert(claim_type_is_v2(bdev->internal.claim_type));
763 : /*
764 : * Removal of tailq nodes while iterating can cause the iteration to jump out of the
765 : * list, perhaps accessing freed memory. Without protection, this could happen
766 : * while the lock is dropped during the examine callback.
767 : */
768 4 : bdev->internal.examine_in_progress++;
769 :
770 9 : TAILQ_FOREACH(claim, &bdev->internal.claim.v2.claims, link) {
771 5 : module = claim->module;
772 :
773 5 : if (module == NULL) {
774 : /* This is a vestigial claim, held by examine_count */
775 0 : continue;
776 : }
777 :
778 5 : if (module->examine_disk == NULL) {
779 0 : continue;
780 : }
781 :
782 5 : spdk_spin_lock(&module->internal.spinlock);
783 5 : module->internal.action_in_progress++;
784 5 : spdk_spin_unlock(&module->internal.spinlock);
785 :
786 : /* Call examine_disk without holding internal.spinlock. */
787 5 : spdk_spin_unlock(&bdev->internal.spinlock);
788 5 : module->examine_disk(bdev);
789 5 : spdk_spin_lock(&bdev->internal.spinlock);
790 5 : }
791 :
792 4 : assert(bdev->internal.examine_in_progress > 0);
793 4 : bdev->internal.examine_in_progress--;
794 4 : if (bdev->internal.examine_in_progress == 0) {
795 : /* Remove any claims that were released during examine_disk */
796 9 : TAILQ_FOREACH_SAFE(claim, &bdev->internal.claim.v2.claims, link, tmpclaim) {
797 5 : if (claim->desc != NULL) {
798 5 : continue;
799 : }
800 :
801 0 : TAILQ_REMOVE(&bdev->internal.claim.v2.claims, claim, link);
802 0 : free(claim);
803 0 : }
804 4 : if (TAILQ_EMPTY(&bdev->internal.claim.v2.claims)) {
805 0 : claim_reset(bdev);
806 0 : }
807 4 : }
808 4 : }
809 :
810 121 : spdk_spin_unlock(&bdev->internal.spinlock);
811 125 : }
812 :
813 : int
814 1 : spdk_bdev_examine(const char *name)
815 : {
816 : struct spdk_bdev *bdev;
817 : struct spdk_bdev_examine_item *item;
818 1 : struct spdk_thread *thread = spdk_get_thread();
819 :
820 1 : if (spdk_unlikely(!spdk_thread_is_app_thread(thread))) {
821 1 : SPDK_ERRLOG("Cannot examine bdev %s on thread %p (%s)\n", name, thread,
822 : thread ? spdk_thread_get_name(thread) : "null");
823 1 : return -EINVAL;
824 : }
825 :
826 0 : if (g_bdev_opts.bdev_auto_examine) {
827 0 : SPDK_ERRLOG("Manual examine is not allowed if auto examine is enabled\n");
828 0 : return -EINVAL;
829 : }
830 :
831 0 : if (bdev_examine_allowlist_check(name)) {
832 0 : SPDK_ERRLOG("Duplicate bdev name for manual examine: %s\n", name);
833 0 : return -EEXIST;
834 : }
835 :
836 0 : item = calloc(1, sizeof(*item));
837 0 : if (!item) {
838 0 : return -ENOMEM;
839 : }
840 0 : item->name = strdup(name);
841 0 : if (!item->name) {
842 0 : free(item);
843 0 : return -ENOMEM;
844 : }
845 0 : TAILQ_INSERT_TAIL(&g_bdev_examine_allowlist, item, link);
846 :
847 0 : bdev = spdk_bdev_get_by_name(name);
848 0 : if (bdev) {
849 0 : bdev_examine(bdev);
850 0 : }
851 0 : return 0;
852 1 : }
853 :
854 : static inline void
855 0 : bdev_examine_allowlist_config_json(struct spdk_json_write_ctx *w)
856 : {
857 : struct spdk_bdev_examine_item *item;
858 0 : TAILQ_FOREACH(item, &g_bdev_examine_allowlist, link) {
859 0 : spdk_json_write_object_begin(w);
860 0 : spdk_json_write_named_string(w, "method", "bdev_examine");
861 0 : spdk_json_write_named_object_begin(w, "params");
862 0 : spdk_json_write_named_string(w, "name", item->name);
863 0 : spdk_json_write_object_end(w);
864 0 : spdk_json_write_object_end(w);
865 0 : }
866 0 : }
867 :
868 : struct spdk_bdev *
869 1 : spdk_bdev_first(void)
870 : {
871 : struct spdk_bdev *bdev;
872 :
873 1 : bdev = TAILQ_FIRST(&g_bdev_mgr.bdevs);
874 1 : if (bdev) {
875 1 : SPDK_DEBUGLOG(bdev, "Starting bdev iteration at %s\n", bdev->name);
876 1 : }
877 :
878 1 : return bdev;
879 : }
880 :
881 : struct spdk_bdev *
882 8 : spdk_bdev_next(struct spdk_bdev *prev)
883 : {
884 : struct spdk_bdev *bdev;
885 :
886 8 : bdev = TAILQ_NEXT(prev, internal.link);
887 8 : if (bdev) {
888 7 : SPDK_DEBUGLOG(bdev, "Continuing bdev iteration at %s\n", bdev->name);
889 7 : }
890 :
891 8 : return bdev;
892 : }
893 :
894 : static struct spdk_bdev *
895 6 : _bdev_next_leaf(struct spdk_bdev *bdev)
896 : {
897 9 : while (bdev != NULL) {
898 8 : if (bdev->internal.claim_type == SPDK_BDEV_CLAIM_NONE) {
899 5 : return bdev;
900 : } else {
901 3 : bdev = TAILQ_NEXT(bdev, internal.link);
902 : }
903 : }
904 :
905 1 : return bdev;
906 6 : }
907 :
908 : struct spdk_bdev *
909 1 : spdk_bdev_first_leaf(void)
910 : {
911 : struct spdk_bdev *bdev;
912 :
913 1 : bdev = _bdev_next_leaf(TAILQ_FIRST(&g_bdev_mgr.bdevs));
914 :
915 1 : if (bdev) {
916 1 : SPDK_DEBUGLOG(bdev, "Starting bdev iteration at %s\n", bdev->name);
917 1 : }
918 :
919 1 : return bdev;
920 : }
921 :
922 : struct spdk_bdev *
923 5 : spdk_bdev_next_leaf(struct spdk_bdev *prev)
924 : {
925 : struct spdk_bdev *bdev;
926 :
927 5 : bdev = _bdev_next_leaf(TAILQ_NEXT(prev, internal.link));
928 :
929 5 : if (bdev) {
930 4 : SPDK_DEBUGLOG(bdev, "Continuing bdev iteration at %s\n", bdev->name);
931 4 : }
932 :
933 5 : return bdev;
934 : }
935 :
936 : static inline bool
937 816 : bdev_io_use_memory_domain(struct spdk_bdev_io *bdev_io)
938 : {
939 816 : return bdev_io->internal.f.has_memory_domain;
940 : }
941 :
942 : static inline bool
943 1553 : bdev_io_use_accel_sequence(struct spdk_bdev_io *bdev_io)
944 : {
945 1553 : return bdev_io->internal.f.has_accel_sequence;
946 : }
947 :
948 : static inline uint32_t
949 373 : bdev_desc_get_block_size(struct spdk_bdev_desc *desc)
950 : {
951 373 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
952 :
953 373 : if (spdk_unlikely(desc->opts.hide_metadata)) {
954 0 : return bdev->blocklen - bdev->md_len;
955 : } else {
956 373 : return bdev->blocklen;
957 : }
958 373 : }
959 :
960 : static inline uint32_t
961 110 : bdev_io_get_block_size(struct spdk_bdev_io *bdev_io)
962 : {
963 110 : return bdev_desc_get_block_size(bdev_io->internal.desc);
964 : }
965 :
966 : static inline void
967 7 : bdev_queue_nomem_io_head(struct spdk_bdev_shared_resource *shared_resource,
968 : struct spdk_bdev_io *bdev_io, enum bdev_io_retry_state state)
969 : {
970 : /* Wait for some of the outstanding I/O to complete before we retry any of the nomem_io.
971 : * Normally we will wait for NOMEM_THRESHOLD_COUNT I/O to complete but for low queue depth
972 : * channels we will instead wait for half to complete.
973 : */
974 7 : shared_resource->nomem_threshold = spdk_max((int64_t)shared_resource->io_outstanding / 2,
975 : (int64_t)shared_resource->io_outstanding - NOMEM_THRESHOLD_COUNT);
976 :
977 7 : assert(state != BDEV_IO_RETRY_STATE_INVALID);
978 7 : bdev_io->internal.retry_state = state;
979 7 : TAILQ_INSERT_HEAD(&shared_resource->nomem_io, bdev_io, internal.link);
980 7 : }
981 :
982 : static inline void
983 43 : bdev_queue_nomem_io_tail(struct spdk_bdev_shared_resource *shared_resource,
984 : struct spdk_bdev_io *bdev_io, enum bdev_io_retry_state state)
985 : {
986 : /* We only queue IOs at the end of the nomem_io queue if they're submitted by the user while
987 : * the queue isn't empty, so we don't need to update the nomem_threshold here */
988 43 : assert(!TAILQ_EMPTY(&shared_resource->nomem_io));
989 :
990 43 : assert(state != BDEV_IO_RETRY_STATE_INVALID);
991 43 : bdev_io->internal.retry_state = state;
992 43 : TAILQ_INSERT_TAIL(&shared_resource->nomem_io, bdev_io, internal.link);
993 43 : }
994 :
995 : void
996 16 : spdk_bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len)
997 : {
998 : struct iovec *iovs;
999 :
1000 16 : if (bdev_io->u.bdev.iovs == NULL) {
1001 3 : bdev_io->u.bdev.iovs = &bdev_io->iov;
1002 3 : bdev_io->u.bdev.iovcnt = 1;
1003 3 : }
1004 :
1005 16 : iovs = bdev_io->u.bdev.iovs;
1006 :
1007 16 : assert(iovs != NULL);
1008 16 : assert(bdev_io->u.bdev.iovcnt >= 1);
1009 :
1010 16 : iovs[0].iov_base = buf;
1011 16 : iovs[0].iov_len = len;
1012 16 : }
1013 :
1014 : void
1015 3 : spdk_bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len)
1016 : {
1017 3 : assert((len / spdk_bdev_get_md_size(bdev_io->bdev)) >= bdev_io->u.bdev.num_blocks);
1018 3 : bdev_io->u.bdev.md_buf = md_buf;
1019 3 : }
1020 :
1021 : static bool
1022 167 : _is_buf_allocated(const struct iovec *iovs)
1023 : {
1024 167 : if (iovs == NULL) {
1025 6 : return false;
1026 : }
1027 :
1028 161 : return iovs[0].iov_base != NULL;
1029 167 : }
1030 :
1031 : static bool
1032 50 : _are_iovs_aligned(struct iovec *iovs, int iovcnt, uint32_t alignment)
1033 : {
1034 : int i;
1035 : uintptr_t iov_base;
1036 :
1037 50 : if (spdk_likely(alignment == 1)) {
1038 21 : return true;
1039 : }
1040 :
1041 36 : for (i = 0; i < iovcnt; i++) {
1042 29 : iov_base = (uintptr_t)iovs[i].iov_base;
1043 29 : if ((iov_base & (alignment - 1)) != 0) {
1044 22 : return false;
1045 : }
1046 7 : }
1047 :
1048 7 : return true;
1049 50 : }
1050 :
1051 : static inline bool
1052 856 : bdev_io_needs_sequence_exec(struct spdk_bdev_desc *desc, struct spdk_bdev_io *bdev_io)
1053 : {
1054 856 : if (!bdev_io_use_accel_sequence(bdev_io)) {
1055 856 : return false;
1056 : }
1057 :
1058 : /* For now, we don't allow splitting IOs with an accel sequence and will treat them as if
1059 : * bdev module didn't support accel sequences */
1060 0 : return !desc->accel_sequence_supported[bdev_io->type] || bdev_io->internal.f.split;
1061 856 : }
1062 :
1063 : static inline void
1064 592 : bdev_io_increment_outstanding(struct spdk_bdev_channel *bdev_ch,
1065 : struct spdk_bdev_shared_resource *shared_resource)
1066 : {
1067 592 : bdev_ch->io_outstanding++;
1068 592 : shared_resource->io_outstanding++;
1069 592 : }
1070 :
1071 : static inline void
1072 592 : bdev_io_decrement_outstanding(struct spdk_bdev_channel *bdev_ch,
1073 : struct spdk_bdev_shared_resource *shared_resource)
1074 : {
1075 592 : assert(bdev_ch->io_outstanding > 0);
1076 592 : assert(shared_resource->io_outstanding > 0);
1077 592 : bdev_ch->io_outstanding--;
1078 592 : shared_resource->io_outstanding--;
1079 592 : }
1080 :
1081 : static void
1082 0 : bdev_io_submit_sequence_cb(void *ctx, int status)
1083 : {
1084 0 : struct spdk_bdev_io *bdev_io = ctx;
1085 :
1086 0 : assert(bdev_io_use_accel_sequence(bdev_io));
1087 :
1088 0 : bdev_io->u.bdev.accel_sequence = NULL;
1089 0 : bdev_io->internal.f.has_accel_sequence = false;
1090 :
1091 0 : if (spdk_unlikely(status != 0)) {
1092 0 : SPDK_ERRLOG("Failed to execute accel sequence, status=%d\n", status);
1093 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1094 0 : bdev_io_complete_unsubmitted(bdev_io);
1095 0 : return;
1096 : }
1097 :
1098 0 : bdev_io_submit(bdev_io);
1099 0 : }
1100 :
1101 : static void
1102 0 : bdev_io_exec_sequence_cb(void *ctx, int status)
1103 : {
1104 0 : struct spdk_bdev_io *bdev_io = ctx;
1105 0 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1106 :
1107 0 : TAILQ_REMOVE(&bdev_io->internal.ch->io_accel_exec, bdev_io, internal.link);
1108 0 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1109 :
1110 0 : if (spdk_unlikely(!TAILQ_EMPTY(&ch->shared_resource->nomem_io))) {
1111 0 : bdev_ch_retry_io(ch);
1112 0 : }
1113 :
1114 0 : bdev_io->internal.data_transfer_cpl(bdev_io, status);
1115 0 : }
1116 :
1117 : static void
1118 0 : bdev_io_exec_sequence(struct spdk_bdev_io *bdev_io, void (*cb_fn)(void *ctx, int status))
1119 : {
1120 0 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1121 :
1122 0 : assert(bdev_io_needs_sequence_exec(bdev_io->internal.desc, bdev_io));
1123 0 : assert(bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE || bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
1124 0 : assert(bdev_io_use_accel_sequence(bdev_io));
1125 :
1126 : /* Since the operations are appended during submission, they're in the opposite order than
1127 : * how we want to execute them for reads (i.e. we need to execute the most recently added
1128 : * operation first), so reverse the sequence before executing it.
1129 : */
1130 0 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
1131 0 : spdk_accel_sequence_reverse(bdev_io->internal.accel_sequence);
1132 0 : }
1133 :
1134 0 : TAILQ_INSERT_TAIL(&bdev_io->internal.ch->io_accel_exec, bdev_io, internal.link);
1135 0 : bdev_io_increment_outstanding(ch, ch->shared_resource);
1136 0 : bdev_io->internal.data_transfer_cpl = cb_fn;
1137 :
1138 0 : spdk_accel_sequence_finish(bdev_io->internal.accel_sequence,
1139 0 : bdev_io_exec_sequence_cb, bdev_io);
1140 0 : }
1141 :
1142 : static void
1143 42 : bdev_io_get_buf_complete(struct spdk_bdev_io *bdev_io, bool status)
1144 : {
1145 42 : struct spdk_io_channel *ch = spdk_bdev_io_get_io_channel(bdev_io);
1146 : void *buf;
1147 :
1148 42 : if (spdk_unlikely(bdev_io->internal.get_aux_buf_cb != NULL)) {
1149 0 : buf = bdev_io->internal.buf.ptr;
1150 0 : bdev_io->internal.buf.ptr = NULL;
1151 0 : bdev_io->internal.f.has_buf = false;
1152 0 : bdev_io->internal.get_aux_buf_cb(ch, bdev_io, buf);
1153 0 : bdev_io->internal.get_aux_buf_cb = NULL;
1154 0 : } else {
1155 42 : assert(bdev_io->internal.get_buf_cb != NULL);
1156 42 : bdev_io->internal.get_buf_cb(ch, bdev_io, status);
1157 42 : bdev_io->internal.get_buf_cb = NULL;
1158 : }
1159 42 : }
1160 :
1161 : static void
1162 4 : _bdev_io_pull_buffer_cpl(void *ctx, int rc)
1163 : {
1164 4 : struct spdk_bdev_io *bdev_io = ctx;
1165 :
1166 4 : if (rc) {
1167 0 : SPDK_ERRLOG("Set bounce buffer failed with rc %d\n", rc);
1168 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1169 0 : }
1170 4 : bdev_io_get_buf_complete(bdev_io, !rc);
1171 4 : }
1172 :
1173 : static void
1174 2 : bdev_io_pull_md_buf_done(void *ctx, int status)
1175 : {
1176 2 : struct spdk_bdev_io *bdev_io = ctx;
1177 2 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1178 :
1179 2 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1180 2 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1181 :
1182 2 : if (spdk_unlikely(!TAILQ_EMPTY(&ch->shared_resource->nomem_io))) {
1183 0 : bdev_ch_retry_io(ch);
1184 0 : }
1185 :
1186 2 : assert(bdev_io->internal.data_transfer_cpl);
1187 2 : bdev_io->internal.data_transfer_cpl(bdev_io, status);
1188 2 : }
1189 :
1190 : static void
1191 4 : bdev_io_pull_md_buf(struct spdk_bdev_io *bdev_io)
1192 : {
1193 4 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1194 4 : int rc = 0;
1195 :
1196 4 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
1197 2 : assert(bdev_io->internal.f.has_bounce_buf);
1198 2 : if (bdev_io_use_memory_domain(bdev_io)) {
1199 2 : TAILQ_INSERT_TAIL(&ch->io_memory_domain, bdev_io, internal.link);
1200 2 : bdev_io_increment_outstanding(ch, ch->shared_resource);
1201 4 : rc = spdk_memory_domain_pull_data(bdev_io->internal.memory_domain,
1202 2 : bdev_io->internal.memory_domain_ctx,
1203 2 : &bdev_io->internal.bounce_buf.orig_md_iov, 1,
1204 2 : &bdev_io->internal.bounce_buf.md_iov, 1,
1205 2 : bdev_io_pull_md_buf_done, bdev_io);
1206 2 : if (rc == 0) {
1207 : /* Continue to submit IO in completion callback */
1208 2 : return;
1209 : }
1210 0 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1211 0 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1212 0 : if (rc != -ENOMEM) {
1213 0 : SPDK_ERRLOG("Failed to pull data from memory domain %s, rc %d\n",
1214 : spdk_memory_domain_get_dma_device_id(
1215 : bdev_io->internal.memory_domain), rc);
1216 0 : }
1217 0 : } else {
1218 0 : memcpy(bdev_io->internal.bounce_buf.md_iov.iov_base,
1219 0 : bdev_io->internal.bounce_buf.orig_md_iov.iov_base,
1220 0 : bdev_io->internal.bounce_buf.orig_md_iov.iov_len);
1221 : }
1222 0 : }
1223 :
1224 2 : if (spdk_unlikely(rc == -ENOMEM)) {
1225 0 : bdev_queue_nomem_io_head(ch->shared_resource, bdev_io, BDEV_IO_RETRY_STATE_PULL_MD);
1226 0 : } else {
1227 2 : assert(bdev_io->internal.data_transfer_cpl);
1228 2 : bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1229 : }
1230 4 : }
1231 :
1232 : static void
1233 4 : _bdev_io_pull_bounce_md_buf(struct spdk_bdev_io *bdev_io, void *md_buf, size_t len)
1234 : {
1235 4 : assert(bdev_io->internal.f.has_bounce_buf);
1236 :
1237 : /* save original md_buf */
1238 4 : bdev_io->internal.bounce_buf.orig_md_iov.iov_base = bdev_io->u.bdev.md_buf;
1239 4 : bdev_io->internal.bounce_buf.orig_md_iov.iov_len = len;
1240 4 : bdev_io->internal.bounce_buf.md_iov.iov_base = md_buf;
1241 4 : bdev_io->internal.bounce_buf.md_iov.iov_len = len;
1242 : /* set bounce md_buf */
1243 4 : bdev_io->u.bdev.md_buf = md_buf;
1244 :
1245 4 : bdev_io_pull_md_buf(bdev_io);
1246 4 : }
1247 :
1248 : static void
1249 42 : _bdev_io_set_md_buf(struct spdk_bdev_io *bdev_io)
1250 : {
1251 42 : struct spdk_bdev *bdev = bdev_io->bdev;
1252 : uint64_t md_len;
1253 : void *buf;
1254 :
1255 42 : if (spdk_bdev_is_md_separate(bdev)) {
1256 7 : assert(!bdev_io_use_accel_sequence(bdev_io));
1257 :
1258 7 : buf = (char *)bdev_io->u.bdev.iovs[0].iov_base + bdev_io->u.bdev.iovs[0].iov_len;
1259 7 : md_len = bdev_io->u.bdev.num_blocks * bdev->md_len;
1260 :
1261 7 : assert(((uintptr_t)buf & (spdk_bdev_get_buf_align(bdev) - 1)) == 0);
1262 :
1263 7 : if (bdev_io->u.bdev.md_buf != NULL) {
1264 4 : _bdev_io_pull_bounce_md_buf(bdev_io, buf, md_len);
1265 4 : return;
1266 : } else {
1267 3 : spdk_bdev_io_set_md_buf(bdev_io, buf, md_len);
1268 : }
1269 3 : }
1270 :
1271 38 : bdev_io_get_buf_complete(bdev_io, true);
1272 42 : }
1273 :
1274 : static inline void
1275 26 : bdev_io_pull_data_done(struct spdk_bdev_io *bdev_io, int rc)
1276 : {
1277 26 : if (rc) {
1278 0 : SPDK_ERRLOG("Failed to get data buffer\n");
1279 0 : assert(bdev_io->internal.data_transfer_cpl);
1280 0 : bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1281 0 : return;
1282 : }
1283 :
1284 26 : _bdev_io_set_md_buf(bdev_io);
1285 26 : }
1286 :
1287 : static void
1288 2 : bdev_io_pull_data_done_and_track(void *ctx, int status)
1289 : {
1290 2 : struct spdk_bdev_io *bdev_io = ctx;
1291 2 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1292 :
1293 2 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1294 2 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1295 :
1296 2 : if (spdk_unlikely(!TAILQ_EMPTY(&ch->shared_resource->nomem_io))) {
1297 0 : bdev_ch_retry_io(ch);
1298 0 : }
1299 :
1300 2 : bdev_io_pull_data_done(bdev_io, status);
1301 2 : }
1302 :
1303 : static void
1304 27 : bdev_io_pull_data(struct spdk_bdev_io *bdev_io)
1305 : {
1306 27 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1307 27 : int rc = 0;
1308 :
1309 : /* If we need to exec an accel sequence or the IO uses a memory domain buffer and has a
1310 : * sequence, append a copy operation making accel change the src/dst buffers of the previous
1311 : * operation */
1312 27 : if (bdev_io_needs_sequence_exec(bdev_io->internal.desc, bdev_io) ||
1313 27 : (bdev_io_use_accel_sequence(bdev_io) && bdev_io_use_memory_domain(bdev_io))) {
1314 0 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
1315 0 : assert(bdev_io_use_accel_sequence(bdev_io));
1316 0 : assert(bdev_io->internal.f.has_bounce_buf);
1317 0 : rc = spdk_accel_append_copy(&bdev_io->internal.accel_sequence, ch->accel_channel,
1318 0 : bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
1319 : NULL, NULL,
1320 0 : bdev_io->internal.bounce_buf.orig_iovs,
1321 0 : bdev_io->internal.bounce_buf.orig_iovcnt,
1322 0 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain : NULL,
1323 0 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain_ctx : NULL,
1324 : NULL, NULL);
1325 0 : } else {
1326 : /* We need to reverse the src/dst for reads */
1327 0 : assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
1328 0 : assert(bdev_io_use_accel_sequence(bdev_io));
1329 0 : assert(bdev_io->internal.f.has_bounce_buf);
1330 0 : rc = spdk_accel_append_copy(&bdev_io->internal.accel_sequence, ch->accel_channel,
1331 0 : bdev_io->internal.bounce_buf.orig_iovs,
1332 0 : bdev_io->internal.bounce_buf.orig_iovcnt,
1333 0 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain : NULL,
1334 0 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain_ctx : NULL,
1335 0 : bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt,
1336 : NULL, NULL, NULL, NULL);
1337 : }
1338 :
1339 0 : if (spdk_unlikely(rc != 0 && rc != -ENOMEM)) {
1340 0 : SPDK_ERRLOG("Failed to append copy to accel sequence: %p\n",
1341 : bdev_io->internal.accel_sequence);
1342 0 : }
1343 27 : } else if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
1344 : /* if this is write path, copy data from original buffer to bounce buffer */
1345 17 : if (bdev_io_use_memory_domain(bdev_io)) {
1346 3 : assert(bdev_io->internal.f.has_bounce_buf);
1347 3 : TAILQ_INSERT_TAIL(&ch->io_memory_domain, bdev_io, internal.link);
1348 3 : bdev_io_increment_outstanding(ch, ch->shared_resource);
1349 6 : rc = spdk_memory_domain_pull_data(bdev_io->internal.memory_domain,
1350 3 : bdev_io->internal.memory_domain_ctx,
1351 3 : bdev_io->internal.bounce_buf.orig_iovs,
1352 3 : (uint32_t)bdev_io->internal.bounce_buf.orig_iovcnt,
1353 3 : bdev_io->u.bdev.iovs, 1,
1354 : bdev_io_pull_data_done_and_track,
1355 3 : bdev_io);
1356 3 : if (rc == 0) {
1357 : /* Continue to submit IO in completion callback */
1358 2 : return;
1359 : }
1360 1 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1361 1 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1362 1 : if (rc != -ENOMEM) {
1363 0 : SPDK_ERRLOG("Failed to pull data from memory domain %s\n",
1364 : spdk_memory_domain_get_dma_device_id(
1365 : bdev_io->internal.memory_domain));
1366 0 : }
1367 1 : } else {
1368 14 : assert(bdev_io->u.bdev.iovcnt == 1);
1369 14 : assert(bdev_io->internal.f.has_bounce_buf);
1370 28 : spdk_copy_iovs_to_buf(bdev_io->u.bdev.iovs[0].iov_base,
1371 14 : bdev_io->u.bdev.iovs[0].iov_len,
1372 14 : bdev_io->internal.bounce_buf.orig_iovs,
1373 14 : bdev_io->internal.bounce_buf.orig_iovcnt);
1374 : }
1375 15 : }
1376 :
1377 25 : if (spdk_unlikely(rc == -ENOMEM)) {
1378 1 : bdev_queue_nomem_io_head(ch->shared_resource, bdev_io, BDEV_IO_RETRY_STATE_PULL);
1379 1 : } else {
1380 24 : bdev_io_pull_data_done(bdev_io, rc);
1381 : }
1382 27 : }
1383 :
1384 : static void
1385 26 : _bdev_io_pull_bounce_data_buf(struct spdk_bdev_io *bdev_io, void *buf, size_t len,
1386 : bdev_copy_bounce_buffer_cpl cpl_cb)
1387 : {
1388 26 : struct spdk_bdev_shared_resource *shared_resource = bdev_io->internal.ch->shared_resource;
1389 :
1390 26 : assert(bdev_io->internal.f.has_bounce_buf == false);
1391 :
1392 26 : bdev_io->internal.data_transfer_cpl = cpl_cb;
1393 26 : bdev_io->internal.f.has_bounce_buf = true;
1394 : /* save original iovec */
1395 26 : bdev_io->internal.bounce_buf.orig_iovs = bdev_io->u.bdev.iovs;
1396 26 : bdev_io->internal.bounce_buf.orig_iovcnt = bdev_io->u.bdev.iovcnt;
1397 : /* zero the other data members */
1398 26 : bdev_io->internal.bounce_buf.iov.iov_base = NULL;
1399 26 : bdev_io->internal.bounce_buf.md_iov.iov_base = NULL;
1400 26 : bdev_io->internal.bounce_buf.orig_md_iov.iov_base = NULL;
1401 : /* set bounce iov */
1402 26 : bdev_io->u.bdev.iovs = &bdev_io->internal.bounce_buf.iov;
1403 26 : bdev_io->u.bdev.iovcnt = 1;
1404 : /* set bounce buffer for this operation */
1405 26 : bdev_io->u.bdev.iovs[0].iov_base = buf;
1406 26 : bdev_io->u.bdev.iovs[0].iov_len = len;
1407 : /* Now we use 1 iov, the split condition could have been changed */
1408 26 : bdev_io->internal.f.split = bdev_io_should_split(bdev_io);
1409 :
1410 26 : if (spdk_unlikely(!TAILQ_EMPTY(&shared_resource->nomem_io))) {
1411 0 : bdev_queue_nomem_io_tail(shared_resource, bdev_io, BDEV_IO_RETRY_STATE_PULL);
1412 0 : } else {
1413 26 : bdev_io_pull_data(bdev_io);
1414 : }
1415 26 : }
1416 :
1417 : static void
1418 42 : _bdev_io_set_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t len)
1419 : {
1420 42 : struct spdk_bdev *bdev = bdev_io->bdev;
1421 : bool buf_allocated;
1422 : uint64_t alignment;
1423 : void *aligned_buf;
1424 :
1425 42 : bdev_io->internal.buf.ptr = buf;
1426 42 : bdev_io->internal.f.has_buf = true;
1427 :
1428 42 : if (spdk_unlikely(bdev_io->internal.get_aux_buf_cb != NULL)) {
1429 0 : bdev_io_get_buf_complete(bdev_io, true);
1430 0 : return;
1431 : }
1432 :
1433 42 : alignment = spdk_bdev_get_buf_align(bdev);
1434 42 : buf_allocated = _is_buf_allocated(bdev_io->u.bdev.iovs);
1435 42 : aligned_buf = (void *)(((uintptr_t)buf + (alignment - 1)) & ~(alignment - 1));
1436 :
1437 42 : if (buf_allocated) {
1438 26 : _bdev_io_pull_bounce_data_buf(bdev_io, aligned_buf, len, _bdev_io_pull_buffer_cpl);
1439 : /* Continue in completion callback */
1440 26 : return;
1441 : } else {
1442 16 : spdk_bdev_io_set_buf(bdev_io, aligned_buf, len);
1443 : }
1444 :
1445 16 : _bdev_io_set_md_buf(bdev_io);
1446 42 : }
1447 :
1448 : static inline uint64_t
1449 84 : bdev_io_get_max_buf_len(struct spdk_bdev_io *bdev_io, uint64_t len)
1450 : {
1451 84 : struct spdk_bdev *bdev = bdev_io->bdev;
1452 : uint64_t md_len, alignment;
1453 :
1454 84 : md_len = spdk_bdev_is_md_separate(bdev) ? bdev_io->u.bdev.num_blocks * bdev->md_len : 0;
1455 :
1456 : /* 1 byte alignment needs 0 byte of extra space, 64 bytes alignment needs 63 bytes of extra space, etc. */
1457 84 : alignment = spdk_bdev_get_buf_align(bdev) - 1;
1458 :
1459 84 : return len + alignment + md_len;
1460 : }
1461 :
1462 : static void
1463 42 : _bdev_io_put_buf(struct spdk_bdev_io *bdev_io, void *buf, uint64_t buf_len)
1464 : {
1465 : struct spdk_bdev_mgmt_channel *ch;
1466 :
1467 42 : ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
1468 42 : spdk_iobuf_put(&ch->iobuf, buf, bdev_io_get_max_buf_len(bdev_io, buf_len));
1469 42 : }
1470 :
1471 : static void
1472 42 : bdev_io_put_buf(struct spdk_bdev_io *bdev_io)
1473 : {
1474 42 : assert(bdev_io->internal.f.has_buf);
1475 42 : _bdev_io_put_buf(bdev_io, bdev_io->internal.buf.ptr, bdev_io->internal.buf.len);
1476 42 : bdev_io->internal.buf.ptr = NULL;
1477 42 : bdev_io->internal.f.has_buf = false;
1478 42 : }
1479 :
1480 3 : SPDK_LOG_DEPRECATION_REGISTER(spdk_bdev_io_put_aux_buf,
1481 : "spdk_bdev_io_put_aux_buf is deprecated", "v25.01", 0);
1482 :
1483 : void
1484 0 : spdk_bdev_io_put_aux_buf(struct spdk_bdev_io *bdev_io, void *buf)
1485 : {
1486 0 : uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
1487 :
1488 0 : SPDK_LOG_DEPRECATED(spdk_bdev_io_put_aux_buf);
1489 :
1490 0 : assert(buf != NULL);
1491 0 : _bdev_io_put_buf(bdev_io, buf, len);
1492 0 : }
1493 :
1494 : static inline void
1495 548 : bdev_submit_request(struct spdk_bdev *bdev, struct spdk_io_channel *ioch,
1496 : struct spdk_bdev_io *bdev_io)
1497 : {
1498 : /* After a request is submitted to a bdev module, the ownership of an accel sequence
1499 : * associated with that bdev_io is transferred to the bdev module. So, clear the internal
1500 : * sequence pointer to make sure we won't touch it anymore. */
1501 1014 : if ((bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE ||
1502 548 : bdev_io->type == SPDK_BDEV_IO_TYPE_READ) && bdev_io->u.bdev.accel_sequence != NULL) {
1503 0 : assert(!bdev_io_needs_sequence_exec(bdev_io->internal.desc, bdev_io));
1504 0 : bdev_io->internal.f.has_accel_sequence = false;
1505 0 : }
1506 :
1507 548 : bdev->fn_table->submit_request(ioch, bdev_io);
1508 548 : }
1509 :
1510 : static inline void
1511 10 : bdev_ch_resubmit_io(struct spdk_bdev_shared_resource *shared_resource, struct spdk_bdev_io *bdev_io)
1512 : {
1513 10 : struct spdk_bdev *bdev = bdev_io->bdev;
1514 :
1515 10 : bdev_io_increment_outstanding(bdev_io->internal.ch, shared_resource);
1516 10 : bdev_io->internal.error.nvme.cdw0 = 0;
1517 10 : bdev_io->num_retries++;
1518 10 : bdev_submit_request(bdev, spdk_bdev_io_get_io_channel(bdev_io), bdev_io);
1519 10 : }
1520 :
1521 : static void
1522 63 : bdev_shared_ch_retry_io(struct spdk_bdev_shared_resource *shared_resource)
1523 : {
1524 : struct spdk_bdev_io *bdev_io;
1525 :
1526 63 : if (shared_resource->io_outstanding > shared_resource->nomem_threshold) {
1527 : /*
1528 : * Allow some more I/O to complete before retrying the nomem_io queue.
1529 : * Some drivers (such as nvme) cannot immediately take a new I/O in
1530 : * the context of a completion, because the resources for the I/O are
1531 : * not released until control returns to the bdev poller. Also, we
1532 : * may require several small I/O to complete before a larger I/O
1533 : * (that requires splitting) can be submitted.
1534 : */
1535 58 : return;
1536 : }
1537 :
1538 16 : while (!TAILQ_EMPTY(&shared_resource->nomem_io)) {
1539 12 : bdev_io = TAILQ_FIRST(&shared_resource->nomem_io);
1540 12 : TAILQ_REMOVE(&shared_resource->nomem_io, bdev_io, internal.link);
1541 :
1542 12 : switch (bdev_io->internal.retry_state) {
1543 : case BDEV_IO_RETRY_STATE_SUBMIT:
1544 10 : bdev_ch_resubmit_io(shared_resource, bdev_io);
1545 10 : break;
1546 : case BDEV_IO_RETRY_STATE_PULL:
1547 1 : bdev_io_pull_data(bdev_io);
1548 1 : break;
1549 : case BDEV_IO_RETRY_STATE_PULL_MD:
1550 0 : bdev_io_pull_md_buf(bdev_io);
1551 0 : break;
1552 : case BDEV_IO_RETRY_STATE_PUSH:
1553 1 : bdev_io_push_bounce_data(bdev_io);
1554 1 : break;
1555 : case BDEV_IO_RETRY_STATE_PUSH_MD:
1556 0 : bdev_io_push_bounce_md_buf(bdev_io);
1557 0 : break;
1558 : default:
1559 0 : assert(0 && "invalid retry state");
1560 : break;
1561 : }
1562 :
1563 12 : if (bdev_io == TAILQ_FIRST(&shared_resource->nomem_io)) {
1564 : /* This IO completed again with NOMEM status, so break the loop and
1565 : * don't try anymore. Note that a bdev_io that fails with NOMEM
1566 : * always gets requeued at the front of the list, to maintain
1567 : * ordering.
1568 : */
1569 1 : break;
1570 : }
1571 : }
1572 63 : }
1573 :
1574 : static void
1575 63 : bdev_ch_retry_io(struct spdk_bdev_channel *bdev_ch)
1576 : {
1577 63 : bdev_shared_ch_retry_io(bdev_ch->shared_resource);
1578 63 : }
1579 :
1580 : static int
1581 0 : bdev_no_mem_poller(void *ctx)
1582 : {
1583 0 : struct spdk_bdev_shared_resource *shared_resource = ctx;
1584 :
1585 0 : spdk_poller_unregister(&shared_resource->nomem_poller);
1586 :
1587 0 : if (!TAILQ_EMPTY(&shared_resource->nomem_io)) {
1588 0 : bdev_shared_ch_retry_io(shared_resource);
1589 0 : }
1590 : /* the retry cb may re-register the poller so double check */
1591 0 : if (!TAILQ_EMPTY(&shared_resource->nomem_io) &&
1592 0 : shared_resource->io_outstanding == 0 && shared_resource->nomem_poller == NULL) {
1593 : /* No IOs were submitted, try again */
1594 0 : shared_resource->nomem_poller = SPDK_POLLER_REGISTER(bdev_no_mem_poller, shared_resource,
1595 : SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * 10);
1596 0 : }
1597 :
1598 0 : return SPDK_POLLER_BUSY;
1599 : }
1600 :
1601 : static inline bool
1602 556 : _bdev_io_handle_no_mem(struct spdk_bdev_io *bdev_io, enum bdev_io_retry_state state)
1603 : {
1604 556 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
1605 556 : struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
1606 :
1607 556 : if (spdk_unlikely(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM)) {
1608 5 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
1609 5 : bdev_queue_nomem_io_head(shared_resource, bdev_io, state);
1610 :
1611 5 : if (shared_resource->io_outstanding == 0 && !shared_resource->nomem_poller) {
1612 : /* Special case when we have nomem IOs and no outstanding IOs which completions
1613 : * could trigger retry of queued IOs
1614 : * Any IOs submitted may trigger retry of queued IOs. This poller handles a case when no
1615 : * new IOs submitted, e.g. qd==1 */
1616 0 : shared_resource->nomem_poller = SPDK_POLLER_REGISTER(bdev_no_mem_poller, shared_resource,
1617 : SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * 10);
1618 0 : }
1619 : /* If bdev module completed an I/O that has an accel sequence with NOMEM status, the
1620 : * ownership of that sequence is transferred back to the bdev layer, so we need to
1621 : * restore internal.accel_sequence to make sure that the sequence is handled
1622 : * correctly in case the I/O is later aborted. */
1623 5 : if ((bdev_io->type == SPDK_BDEV_IO_TYPE_READ ||
1624 5 : bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) && bdev_io->u.bdev.accel_sequence) {
1625 0 : assert(!bdev_io_use_accel_sequence(bdev_io));
1626 0 : bdev_io->internal.f.has_accel_sequence = true;
1627 0 : bdev_io->internal.accel_sequence = bdev_io->u.bdev.accel_sequence;
1628 0 : }
1629 :
1630 5 : return true;
1631 : }
1632 :
1633 551 : if (spdk_unlikely(!TAILQ_EMPTY(&shared_resource->nomem_io))) {
1634 63 : bdev_ch_retry_io(bdev_ch);
1635 63 : }
1636 :
1637 551 : return false;
1638 556 : }
1639 :
1640 : static void
1641 26 : _bdev_io_complete_push_bounce_done(void *ctx, int rc)
1642 : {
1643 26 : struct spdk_bdev_io *bdev_io = ctx;
1644 26 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1645 :
1646 26 : if (rc) {
1647 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1648 0 : }
1649 : /* We want to free the bounce buffer here since we know we're done with it (as opposed
1650 : * to waiting for the conditional free of internal.buf.ptr in spdk_bdev_free_io()).
1651 : */
1652 26 : bdev_io_put_buf(bdev_io);
1653 :
1654 26 : if (spdk_unlikely(!TAILQ_EMPTY(&ch->shared_resource->nomem_io))) {
1655 0 : bdev_ch_retry_io(ch);
1656 0 : }
1657 :
1658 : /* Continue with IO completion flow */
1659 26 : bdev_io_complete(bdev_io);
1660 26 : }
1661 :
1662 : static void
1663 2 : bdev_io_push_bounce_md_buf_done(void *ctx, int rc)
1664 : {
1665 2 : struct spdk_bdev_io *bdev_io = ctx;
1666 2 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1667 :
1668 2 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1669 2 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1670 2 : bdev_io->internal.f.has_bounce_buf = false;
1671 :
1672 2 : if (spdk_unlikely(!TAILQ_EMPTY(&ch->shared_resource->nomem_io))) {
1673 0 : bdev_ch_retry_io(ch);
1674 0 : }
1675 :
1676 2 : bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1677 2 : }
1678 :
1679 : static inline void
1680 26 : bdev_io_push_bounce_md_buf(struct spdk_bdev_io *bdev_io)
1681 : {
1682 26 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1683 26 : int rc = 0;
1684 :
1685 26 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS);
1686 26 : assert(bdev_io->internal.f.has_bounce_buf);
1687 :
1688 : /* do the same for metadata buffer */
1689 26 : if (spdk_unlikely(bdev_io->internal.bounce_buf.orig_md_iov.iov_base != NULL)) {
1690 4 : assert(spdk_bdev_is_md_separate(bdev_io->bdev));
1691 :
1692 4 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
1693 2 : if (bdev_io_use_memory_domain(bdev_io)) {
1694 2 : TAILQ_INSERT_TAIL(&ch->io_memory_domain, bdev_io, internal.link);
1695 2 : bdev_io_increment_outstanding(ch, ch->shared_resource);
1696 : /* If memory domain is used then we need to call async push function */
1697 4 : rc = spdk_memory_domain_push_data(bdev_io->internal.memory_domain,
1698 2 : bdev_io->internal.memory_domain_ctx,
1699 2 : &bdev_io->internal.bounce_buf.orig_md_iov,
1700 2 : (uint32_t)bdev_io->internal.bounce_buf.orig_iovcnt,
1701 2 : &bdev_io->internal.bounce_buf.md_iov, 1,
1702 : bdev_io_push_bounce_md_buf_done,
1703 2 : bdev_io);
1704 2 : if (rc == 0) {
1705 : /* Continue IO completion in async callback */
1706 2 : return;
1707 : }
1708 0 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1709 0 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1710 0 : if (rc != -ENOMEM) {
1711 0 : SPDK_ERRLOG("Failed to push md to memory domain %s\n",
1712 : spdk_memory_domain_get_dma_device_id(
1713 : bdev_io->internal.memory_domain));
1714 0 : }
1715 0 : } else {
1716 0 : memcpy(bdev_io->internal.bounce_buf.orig_md_iov.iov_base, bdev_io->u.bdev.md_buf,
1717 0 : bdev_io->internal.bounce_buf.orig_md_iov.iov_len);
1718 : }
1719 0 : }
1720 2 : }
1721 :
1722 24 : if (spdk_unlikely(rc == -ENOMEM)) {
1723 0 : bdev_queue_nomem_io_head(ch->shared_resource, bdev_io, BDEV_IO_RETRY_STATE_PUSH_MD);
1724 0 : } else {
1725 24 : assert(bdev_io->internal.data_transfer_cpl);
1726 24 : bdev_io->internal.f.has_bounce_buf = false;
1727 24 : bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1728 : }
1729 26 : }
1730 :
1731 : static inline void
1732 26 : bdev_io_push_bounce_data_done(struct spdk_bdev_io *bdev_io, int rc)
1733 : {
1734 26 : assert(bdev_io->internal.data_transfer_cpl);
1735 26 : if (rc) {
1736 0 : bdev_io->internal.data_transfer_cpl(bdev_io, rc);
1737 0 : return;
1738 : }
1739 :
1740 : /* set original buffer for this io */
1741 26 : bdev_io->u.bdev.iovcnt = bdev_io->internal.bounce_buf.orig_iovcnt;
1742 26 : bdev_io->u.bdev.iovs = bdev_io->internal.bounce_buf.orig_iovs;
1743 :
1744 : /* We don't set bdev_io->internal.f.has_bounce_buf to false here because
1745 : * we still need to clear the md buf */
1746 :
1747 26 : bdev_io_push_bounce_md_buf(bdev_io);
1748 26 : }
1749 :
1750 : static void
1751 2 : bdev_io_push_bounce_data_done_and_track(void *ctx, int status)
1752 : {
1753 2 : struct spdk_bdev_io *bdev_io = ctx;
1754 2 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1755 :
1756 2 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1757 2 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1758 :
1759 2 : if (spdk_unlikely(!TAILQ_EMPTY(&ch->shared_resource->nomem_io))) {
1760 0 : bdev_ch_retry_io(ch);
1761 0 : }
1762 :
1763 2 : bdev_io_push_bounce_data_done(bdev_io, status);
1764 2 : }
1765 :
1766 : static inline void
1767 27 : bdev_io_push_bounce_data(struct spdk_bdev_io *bdev_io)
1768 : {
1769 27 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
1770 27 : int rc = 0;
1771 :
1772 27 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS);
1773 27 : assert(!bdev_io_use_accel_sequence(bdev_io));
1774 27 : assert(bdev_io->internal.f.has_bounce_buf);
1775 :
1776 : /* if this is read path, copy data from bounce buffer to original buffer */
1777 27 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ) {
1778 11 : if (bdev_io_use_memory_domain(bdev_io)) {
1779 3 : TAILQ_INSERT_TAIL(&ch->io_memory_domain, bdev_io, internal.link);
1780 3 : bdev_io_increment_outstanding(ch, ch->shared_resource);
1781 : /* If memory domain is used then we need to call async push function */
1782 6 : rc = spdk_memory_domain_push_data(bdev_io->internal.memory_domain,
1783 3 : bdev_io->internal.memory_domain_ctx,
1784 3 : bdev_io->internal.bounce_buf.orig_iovs,
1785 3 : (uint32_t)bdev_io->internal.bounce_buf.orig_iovcnt,
1786 3 : &bdev_io->internal.bounce_buf.iov, 1,
1787 : bdev_io_push_bounce_data_done_and_track,
1788 3 : bdev_io);
1789 3 : if (rc == 0) {
1790 : /* Continue IO completion in async callback */
1791 2 : return;
1792 : }
1793 :
1794 1 : TAILQ_REMOVE(&ch->io_memory_domain, bdev_io, internal.link);
1795 1 : bdev_io_decrement_outstanding(ch, ch->shared_resource);
1796 1 : if (rc != -ENOMEM) {
1797 0 : SPDK_ERRLOG("Failed to push data to memory domain %s\n",
1798 : spdk_memory_domain_get_dma_device_id(
1799 : bdev_io->internal.memory_domain));
1800 0 : }
1801 1 : } else {
1802 16 : spdk_copy_buf_to_iovs(bdev_io->internal.bounce_buf.orig_iovs,
1803 8 : bdev_io->internal.bounce_buf.orig_iovcnt,
1804 8 : bdev_io->internal.bounce_buf.iov.iov_base,
1805 8 : bdev_io->internal.bounce_buf.iov.iov_len);
1806 : }
1807 9 : }
1808 :
1809 25 : if (spdk_unlikely(rc == -ENOMEM)) {
1810 1 : bdev_queue_nomem_io_head(ch->shared_resource, bdev_io, BDEV_IO_RETRY_STATE_PUSH);
1811 1 : } else {
1812 24 : bdev_io_push_bounce_data_done(bdev_io, rc);
1813 : }
1814 27 : }
1815 :
1816 : static inline void
1817 26 : _bdev_io_push_bounce_data_buffer(struct spdk_bdev_io *bdev_io, bdev_copy_bounce_buffer_cpl cpl_cb)
1818 : {
1819 26 : bdev_io->internal.data_transfer_cpl = cpl_cb;
1820 26 : bdev_io_push_bounce_data(bdev_io);
1821 26 : }
1822 :
1823 : static void
1824 0 : bdev_io_get_iobuf_cb(struct spdk_iobuf_entry *iobuf, void *buf)
1825 : {
1826 : struct spdk_bdev_io *bdev_io;
1827 :
1828 0 : bdev_io = SPDK_CONTAINEROF(iobuf, struct spdk_bdev_io, internal.iobuf);
1829 0 : _bdev_io_set_buf(bdev_io, buf, bdev_io->internal.buf.len);
1830 0 : }
1831 :
1832 : static void
1833 42 : bdev_io_get_buf(struct spdk_bdev_io *bdev_io, uint64_t len)
1834 : {
1835 : struct spdk_bdev_mgmt_channel *mgmt_ch;
1836 : uint64_t max_len;
1837 : void *buf;
1838 :
1839 42 : assert(spdk_bdev_io_get_thread(bdev_io) == spdk_get_thread());
1840 42 : mgmt_ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
1841 42 : max_len = bdev_io_get_max_buf_len(bdev_io, len);
1842 :
1843 42 : if (spdk_unlikely(max_len > mgmt_ch->iobuf.cache[0].large.bufsize)) {
1844 0 : SPDK_ERRLOG("Length %" PRIu64 " is larger than allowed\n", max_len);
1845 0 : bdev_io_get_buf_complete(bdev_io, false);
1846 0 : return;
1847 : }
1848 :
1849 42 : bdev_io->internal.buf.len = len;
1850 42 : buf = spdk_iobuf_get(&mgmt_ch->iobuf, max_len, &bdev_io->internal.iobuf,
1851 : bdev_io_get_iobuf_cb);
1852 42 : if (buf != NULL) {
1853 42 : _bdev_io_set_buf(bdev_io, buf, len);
1854 42 : }
1855 42 : }
1856 :
1857 : void
1858 56 : spdk_bdev_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb, uint64_t len)
1859 : {
1860 56 : struct spdk_bdev *bdev = bdev_io->bdev;
1861 : uint64_t alignment;
1862 :
1863 56 : assert(cb != NULL);
1864 56 : bdev_io->internal.get_buf_cb = cb;
1865 :
1866 56 : alignment = spdk_bdev_get_buf_align(bdev);
1867 :
1868 56 : if (_is_buf_allocated(bdev_io->u.bdev.iovs) &&
1869 40 : _are_iovs_aligned(bdev_io->u.bdev.iovs, bdev_io->u.bdev.iovcnt, alignment)) {
1870 : /* Buffer already present and aligned */
1871 18 : cb(spdk_bdev_io_get_io_channel(bdev_io), bdev_io, true);
1872 18 : return;
1873 : }
1874 :
1875 38 : bdev_io_get_buf(bdev_io, len);
1876 56 : }
1877 :
1878 : static void
1879 4 : _bdev_memory_domain_get_io_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
1880 : bool success)
1881 : {
1882 4 : if (!success) {
1883 0 : SPDK_ERRLOG("Failed to get data buffer, completing IO\n");
1884 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
1885 0 : bdev_io_complete_unsubmitted(bdev_io);
1886 0 : return;
1887 : }
1888 :
1889 4 : if (bdev_io_needs_sequence_exec(bdev_io->internal.desc, bdev_io)) {
1890 0 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
1891 0 : bdev_io_exec_sequence(bdev_io, bdev_io_submit_sequence_cb);
1892 0 : return;
1893 : }
1894 : /* For reads we'll execute the sequence after the data is read, so, for now, only
1895 : * clear out accel_sequence pointer and submit the IO */
1896 0 : assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
1897 0 : bdev_io->u.bdev.accel_sequence = NULL;
1898 0 : }
1899 :
1900 4 : bdev_io_submit(bdev_io);
1901 4 : }
1902 :
1903 : static void
1904 4 : _bdev_memory_domain_io_get_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_buf_cb cb,
1905 : uint64_t len)
1906 : {
1907 4 : assert(cb != NULL);
1908 4 : bdev_io->internal.get_buf_cb = cb;
1909 :
1910 4 : bdev_io_get_buf(bdev_io, len);
1911 4 : }
1912 :
1913 :
1914 3 : SPDK_LOG_DEPRECATION_REGISTER(spdk_bdev_io_get_aux_buf,
1915 : "spdk_bdev_io_get_aux_buf is deprecated", "v25.01", 0);
1916 :
1917 : void
1918 0 : spdk_bdev_io_get_aux_buf(struct spdk_bdev_io *bdev_io, spdk_bdev_io_get_aux_buf_cb cb)
1919 : {
1920 0 : uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
1921 :
1922 0 : SPDK_LOG_DEPRECATED(spdk_bdev_io_get_aux_buf);
1923 :
1924 0 : assert(cb != NULL);
1925 0 : assert(bdev_io->internal.get_aux_buf_cb == NULL);
1926 0 : bdev_io->internal.get_aux_buf_cb = cb;
1927 0 : bdev_io_get_buf(bdev_io, len);
1928 0 : }
1929 :
1930 : static int
1931 65 : bdev_module_get_max_ctx_size(void)
1932 : {
1933 : struct spdk_bdev_module *bdev_module;
1934 65 : int max_bdev_module_size = 0;
1935 :
1936 254 : TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
1937 189 : if (bdev_module->get_ctx_size && bdev_module->get_ctx_size() > max_bdev_module_size) {
1938 64 : max_bdev_module_size = bdev_module->get_ctx_size();
1939 64 : }
1940 189 : }
1941 :
1942 65 : return max_bdev_module_size;
1943 : }
1944 :
1945 : static void
1946 0 : bdev_enable_histogram_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
1947 : {
1948 0 : if (!bdev->internal.histogram_enabled) {
1949 0 : return;
1950 : }
1951 :
1952 0 : spdk_json_write_object_begin(w);
1953 0 : spdk_json_write_named_string(w, "method", "bdev_enable_histogram");
1954 :
1955 0 : spdk_json_write_named_object_begin(w, "params");
1956 0 : spdk_json_write_named_string(w, "name", bdev->name);
1957 :
1958 0 : spdk_json_write_named_bool(w, "enable", bdev->internal.histogram_enabled);
1959 :
1960 0 : if (bdev->internal.histogram_io_type) {
1961 0 : spdk_json_write_named_string(w, "opc",
1962 0 : spdk_bdev_get_io_type_name(bdev->internal.histogram_io_type));
1963 0 : }
1964 :
1965 0 : spdk_json_write_object_end(w);
1966 :
1967 0 : spdk_json_write_object_end(w);
1968 0 : }
1969 :
1970 : static void
1971 0 : bdev_qos_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
1972 : {
1973 : int i;
1974 0 : struct spdk_bdev_qos *qos = bdev->internal.qos;
1975 : uint64_t limits[SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES];
1976 :
1977 0 : if (!qos) {
1978 0 : return;
1979 : }
1980 :
1981 0 : spdk_bdev_get_qos_rate_limits(bdev, limits);
1982 :
1983 0 : spdk_json_write_object_begin(w);
1984 0 : spdk_json_write_named_string(w, "method", "bdev_set_qos_limit");
1985 :
1986 0 : spdk_json_write_named_object_begin(w, "params");
1987 0 : spdk_json_write_named_string(w, "name", bdev->name);
1988 0 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
1989 0 : if (limits[i] > 0) {
1990 0 : spdk_json_write_named_uint64(w, qos_rpc_type[i], limits[i]);
1991 0 : }
1992 0 : }
1993 0 : spdk_json_write_object_end(w);
1994 :
1995 0 : spdk_json_write_object_end(w);
1996 0 : }
1997 :
1998 : void
1999 0 : spdk_bdev_subsystem_config_json(struct spdk_json_write_ctx *w)
2000 : {
2001 : struct spdk_bdev_module *bdev_module;
2002 : struct spdk_bdev *bdev;
2003 :
2004 0 : assert(w != NULL);
2005 :
2006 0 : spdk_json_write_array_begin(w);
2007 :
2008 0 : spdk_json_write_object_begin(w);
2009 0 : spdk_json_write_named_string(w, "method", "bdev_set_options");
2010 0 : spdk_json_write_named_object_begin(w, "params");
2011 0 : spdk_json_write_named_uint32(w, "bdev_io_pool_size", g_bdev_opts.bdev_io_pool_size);
2012 0 : spdk_json_write_named_uint32(w, "bdev_io_cache_size", g_bdev_opts.bdev_io_cache_size);
2013 0 : spdk_json_write_named_bool(w, "bdev_auto_examine", g_bdev_opts.bdev_auto_examine);
2014 0 : spdk_json_write_named_uint32(w, "iobuf_small_cache_size", g_bdev_opts.iobuf_small_cache_size);
2015 0 : spdk_json_write_named_uint32(w, "iobuf_large_cache_size", g_bdev_opts.iobuf_large_cache_size);
2016 0 : spdk_json_write_object_end(w);
2017 0 : spdk_json_write_object_end(w);
2018 :
2019 0 : bdev_examine_allowlist_config_json(w);
2020 :
2021 0 : TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
2022 0 : if (bdev_module->config_json) {
2023 0 : bdev_module->config_json(w);
2024 0 : }
2025 0 : }
2026 :
2027 0 : spdk_spin_lock(&g_bdev_mgr.spinlock);
2028 :
2029 0 : TAILQ_FOREACH(bdev, &g_bdev_mgr.bdevs, internal.link) {
2030 0 : if (bdev->fn_table->write_config_json) {
2031 0 : bdev->fn_table->write_config_json(bdev, w);
2032 0 : }
2033 :
2034 0 : bdev_qos_config_json(bdev, w);
2035 0 : bdev_enable_histogram_config_json(bdev, w);
2036 0 : }
2037 :
2038 0 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
2039 :
2040 : /* This has to be last RPC in array to make sure all bdevs finished examine */
2041 0 : spdk_json_write_object_begin(w);
2042 0 : spdk_json_write_named_string(w, "method", "bdev_wait_for_examine");
2043 0 : spdk_json_write_object_end(w);
2044 :
2045 0 : spdk_json_write_array_end(w);
2046 0 : }
2047 :
2048 : static void
2049 71 : bdev_mgmt_channel_destroy(void *io_device, void *ctx_buf)
2050 : {
2051 71 : struct spdk_bdev_mgmt_channel *ch = ctx_buf;
2052 : struct spdk_bdev_io *bdev_io;
2053 :
2054 71 : spdk_iobuf_channel_fini(&ch->iobuf);
2055 :
2056 9969 : while (!STAILQ_EMPTY(&ch->per_thread_cache)) {
2057 9898 : bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
2058 9898 : STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
2059 9898 : ch->per_thread_cache_count--;
2060 9898 : spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
2061 : }
2062 :
2063 71 : assert(ch->per_thread_cache_count == 0);
2064 71 : }
2065 :
2066 : static int
2067 71 : bdev_mgmt_channel_create(void *io_device, void *ctx_buf)
2068 : {
2069 71 : struct spdk_bdev_mgmt_channel *ch = ctx_buf;
2070 : struct spdk_bdev_io *bdev_io;
2071 : uint32_t i;
2072 : int rc;
2073 :
2074 142 : rc = spdk_iobuf_channel_init(&ch->iobuf, "bdev",
2075 71 : g_bdev_opts.iobuf_small_cache_size,
2076 71 : g_bdev_opts.iobuf_large_cache_size);
2077 71 : if (rc != 0) {
2078 0 : SPDK_ERRLOG("Failed to create iobuf channel: %s\n", spdk_strerror(-rc));
2079 0 : return -1;
2080 : }
2081 :
2082 71 : STAILQ_INIT(&ch->per_thread_cache);
2083 71 : ch->bdev_io_cache_size = g_bdev_opts.bdev_io_cache_size;
2084 :
2085 : /* Pre-populate bdev_io cache to ensure this thread cannot be starved. */
2086 71 : ch->per_thread_cache_count = 0;
2087 9969 : for (i = 0; i < ch->bdev_io_cache_size; i++) {
2088 9898 : bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
2089 9898 : if (bdev_io == NULL) {
2090 0 : SPDK_ERRLOG("You need to increase bdev_io_pool_size using bdev_set_options RPC.\n");
2091 0 : assert(false);
2092 : bdev_mgmt_channel_destroy(io_device, ctx_buf);
2093 : return -1;
2094 : }
2095 9898 : ch->per_thread_cache_count++;
2096 9898 : STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
2097 9898 : }
2098 :
2099 71 : TAILQ_INIT(&ch->shared_resources);
2100 71 : TAILQ_INIT(&ch->io_wait_queue);
2101 :
2102 71 : return 0;
2103 71 : }
2104 :
2105 : static void
2106 65 : bdev_init_complete(int rc)
2107 : {
2108 65 : spdk_bdev_init_cb cb_fn = g_init_cb_fn;
2109 65 : void *cb_arg = g_init_cb_arg;
2110 : struct spdk_bdev_module *m;
2111 :
2112 65 : g_bdev_mgr.init_complete = true;
2113 65 : g_init_cb_fn = NULL;
2114 65 : g_init_cb_arg = NULL;
2115 :
2116 : /*
2117 : * For modules that need to know when subsystem init is complete,
2118 : * inform them now.
2119 : */
2120 65 : if (rc == 0) {
2121 254 : TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
2122 189 : if (m->init_complete) {
2123 23 : m->init_complete();
2124 23 : }
2125 189 : }
2126 65 : }
2127 :
2128 65 : cb_fn(cb_arg, rc);
2129 65 : }
2130 :
2131 : static bool
2132 257 : bdev_module_all_actions_completed(void)
2133 : {
2134 : struct spdk_bdev_module *m;
2135 :
2136 1017 : TAILQ_FOREACH(m, &g_bdev_mgr.bdev_modules, internal.tailq) {
2137 760 : if (m->internal.action_in_progress > 0) {
2138 0 : return false;
2139 : }
2140 760 : }
2141 257 : return true;
2142 257 : }
2143 :
2144 : static void
2145 622 : bdev_module_action_complete(void)
2146 : {
2147 : /*
2148 : * Don't finish bdev subsystem initialization if
2149 : * module pre-initialization is still in progress, or
2150 : * the subsystem been already initialized.
2151 : */
2152 622 : if (!g_bdev_mgr.module_init_complete || g_bdev_mgr.init_complete) {
2153 557 : return;
2154 : }
2155 :
2156 : /*
2157 : * Check all bdev modules for inits/examinations in progress. If any
2158 : * exist, return immediately since we cannot finish bdev subsystem
2159 : * initialization until all are completed.
2160 : */
2161 65 : if (!bdev_module_all_actions_completed()) {
2162 0 : return;
2163 : }
2164 :
2165 : /*
2166 : * Modules already finished initialization - now that all
2167 : * the bdev modules have finished their asynchronous I/O
2168 : * processing, the entire bdev layer can be marked as complete.
2169 : */
2170 65 : bdev_init_complete(0);
2171 622 : }
2172 :
2173 : static void
2174 557 : bdev_module_action_done(struct spdk_bdev_module *module)
2175 : {
2176 557 : spdk_spin_lock(&module->internal.spinlock);
2177 557 : assert(module->internal.action_in_progress > 0);
2178 557 : module->internal.action_in_progress--;
2179 557 : spdk_spin_unlock(&module->internal.spinlock);
2180 557 : bdev_module_action_complete();
2181 557 : }
2182 :
2183 : void
2184 65 : spdk_bdev_module_init_done(struct spdk_bdev_module *module)
2185 : {
2186 65 : assert(module->async_init);
2187 65 : bdev_module_action_done(module);
2188 65 : }
2189 :
2190 : void
2191 492 : spdk_bdev_module_examine_done(struct spdk_bdev_module *module)
2192 : {
2193 492 : bdev_module_action_done(module);
2194 492 : }
2195 :
2196 : /** The last initialized bdev module */
2197 : static struct spdk_bdev_module *g_resume_bdev_module = NULL;
2198 :
2199 : static void
2200 0 : bdev_init_failed(void *cb_arg)
2201 : {
2202 0 : struct spdk_bdev_module *module = cb_arg;
2203 :
2204 0 : spdk_spin_lock(&module->internal.spinlock);
2205 0 : assert(module->internal.action_in_progress > 0);
2206 0 : module->internal.action_in_progress--;
2207 0 : spdk_spin_unlock(&module->internal.spinlock);
2208 0 : bdev_init_complete(-1);
2209 0 : }
2210 :
2211 : static int
2212 65 : bdev_modules_init(void)
2213 : {
2214 : struct spdk_bdev_module *module;
2215 65 : int rc = 0;
2216 :
2217 254 : TAILQ_FOREACH(module, &g_bdev_mgr.bdev_modules, internal.tailq) {
2218 189 : g_resume_bdev_module = module;
2219 189 : if (module->async_init) {
2220 65 : spdk_spin_lock(&module->internal.spinlock);
2221 65 : module->internal.action_in_progress = 1;
2222 65 : spdk_spin_unlock(&module->internal.spinlock);
2223 65 : }
2224 189 : rc = module->module_init();
2225 189 : if (rc != 0) {
2226 : /* Bump action_in_progress to prevent other modules from completion of modules_init
2227 : * Send message to defer application shutdown until resources are cleaned up */
2228 0 : spdk_spin_lock(&module->internal.spinlock);
2229 0 : module->internal.action_in_progress = 1;
2230 0 : spdk_spin_unlock(&module->internal.spinlock);
2231 0 : spdk_thread_send_msg(spdk_get_thread(), bdev_init_failed, module);
2232 0 : return rc;
2233 : }
2234 189 : }
2235 :
2236 65 : g_resume_bdev_module = NULL;
2237 65 : return 0;
2238 65 : }
2239 :
2240 : void
2241 65 : spdk_bdev_initialize(spdk_bdev_init_cb cb_fn, void *cb_arg)
2242 : {
2243 65 : int rc = 0;
2244 : char mempool_name[32];
2245 :
2246 65 : assert(cb_fn != NULL);
2247 :
2248 65 : g_init_cb_fn = cb_fn;
2249 65 : g_init_cb_arg = cb_arg;
2250 :
2251 65 : spdk_notify_type_register("bdev_register");
2252 65 : spdk_notify_type_register("bdev_unregister");
2253 :
2254 65 : snprintf(mempool_name, sizeof(mempool_name), "bdev_io_%d", getpid());
2255 :
2256 65 : rc = spdk_iobuf_register_module("bdev");
2257 65 : if (rc != 0) {
2258 0 : SPDK_ERRLOG("could not register bdev iobuf module: %s\n", spdk_strerror(-rc));
2259 0 : bdev_init_complete(-1);
2260 0 : return;
2261 : }
2262 :
2263 130 : g_bdev_mgr.bdev_io_pool = spdk_mempool_create(mempool_name,
2264 65 : g_bdev_opts.bdev_io_pool_size,
2265 65 : sizeof(struct spdk_bdev_io) +
2266 65 : bdev_module_get_max_ctx_size(),
2267 : 0,
2268 : SPDK_ENV_NUMA_ID_ANY);
2269 :
2270 65 : if (g_bdev_mgr.bdev_io_pool == NULL) {
2271 0 : SPDK_ERRLOG("could not allocate spdk_bdev_io pool\n");
2272 0 : bdev_init_complete(-1);
2273 0 : return;
2274 : }
2275 :
2276 65 : g_bdev_mgr.zero_buffer = spdk_zmalloc(ZERO_BUFFER_SIZE, ZERO_BUFFER_SIZE,
2277 : NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
2278 65 : if (!g_bdev_mgr.zero_buffer) {
2279 0 : SPDK_ERRLOG("create bdev zero buffer failed\n");
2280 0 : bdev_init_complete(-1);
2281 0 : return;
2282 : }
2283 :
2284 : #ifdef SPDK_CONFIG_VTUNE
2285 : g_bdev_mgr.domain = __itt_domain_create("spdk_bdev");
2286 : #endif
2287 :
2288 65 : spdk_io_device_register(&g_bdev_mgr, bdev_mgmt_channel_create,
2289 : bdev_mgmt_channel_destroy,
2290 : sizeof(struct spdk_bdev_mgmt_channel),
2291 : "bdev_mgr");
2292 :
2293 65 : rc = bdev_modules_init();
2294 65 : g_bdev_mgr.module_init_complete = true;
2295 65 : if (rc != 0) {
2296 0 : SPDK_ERRLOG("bdev modules init failed\n");
2297 0 : return;
2298 : }
2299 :
2300 65 : bdev_module_action_complete();
2301 65 : }
2302 :
2303 : static void
2304 65 : bdev_mgr_unregister_cb(void *io_device)
2305 : {
2306 65 : spdk_bdev_fini_cb cb_fn = g_fini_cb_fn;
2307 :
2308 65 : if (g_bdev_mgr.bdev_io_pool) {
2309 65 : if (spdk_mempool_count(g_bdev_mgr.bdev_io_pool) != g_bdev_opts.bdev_io_pool_size) {
2310 0 : SPDK_ERRLOG("bdev IO pool count is %zu but should be %u\n",
2311 : spdk_mempool_count(g_bdev_mgr.bdev_io_pool),
2312 : g_bdev_opts.bdev_io_pool_size);
2313 0 : }
2314 :
2315 65 : spdk_mempool_free(g_bdev_mgr.bdev_io_pool);
2316 65 : }
2317 :
2318 65 : spdk_free(g_bdev_mgr.zero_buffer);
2319 :
2320 65 : bdev_examine_allowlist_free();
2321 :
2322 65 : cb_fn(g_fini_cb_arg);
2323 65 : g_fini_cb_fn = NULL;
2324 65 : g_fini_cb_arg = NULL;
2325 65 : g_bdev_mgr.init_complete = false;
2326 65 : g_bdev_mgr.module_init_complete = false;
2327 65 : }
2328 :
2329 : static void
2330 65 : bdev_module_fini_iter(void *arg)
2331 : {
2332 : struct spdk_bdev_module *bdev_module;
2333 :
2334 : /* FIXME: Handling initialization failures is broken now,
2335 : * so we won't even try cleaning up after successfully
2336 : * initialized modules. if module_init_complete is false,
2337 : * just call spdk_bdev_mgr_unregister_cb
2338 : */
2339 65 : if (!g_bdev_mgr.module_init_complete) {
2340 0 : bdev_mgr_unregister_cb(NULL);
2341 0 : return;
2342 : }
2343 :
2344 : /* Start iterating from the last touched module */
2345 65 : if (!g_resume_bdev_module) {
2346 65 : bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list);
2347 65 : } else {
2348 0 : bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list,
2349 : internal.tailq);
2350 : }
2351 :
2352 254 : while (bdev_module) {
2353 189 : if (bdev_module->async_fini) {
2354 : /* Save our place so we can resume later. We must
2355 : * save the variable here, before calling module_fini()
2356 : * below, because in some cases the module may immediately
2357 : * call spdk_bdev_module_fini_done() and re-enter
2358 : * this function to continue iterating. */
2359 0 : g_resume_bdev_module = bdev_module;
2360 0 : }
2361 :
2362 189 : if (bdev_module->module_fini) {
2363 189 : bdev_module->module_fini();
2364 189 : }
2365 :
2366 189 : if (bdev_module->async_fini) {
2367 0 : return;
2368 : }
2369 :
2370 189 : bdev_module = TAILQ_PREV(bdev_module, bdev_module_list,
2371 : internal.tailq);
2372 : }
2373 :
2374 65 : g_resume_bdev_module = NULL;
2375 65 : spdk_io_device_unregister(&g_bdev_mgr, bdev_mgr_unregister_cb);
2376 65 : }
2377 :
2378 : void
2379 0 : spdk_bdev_module_fini_done(void)
2380 : {
2381 0 : if (spdk_get_thread() != g_fini_thread) {
2382 0 : spdk_thread_send_msg(g_fini_thread, bdev_module_fini_iter, NULL);
2383 0 : } else {
2384 0 : bdev_module_fini_iter(NULL);
2385 : }
2386 0 : }
2387 :
2388 : static void
2389 65 : bdev_finish_unregister_bdevs_iter(void *cb_arg, int bdeverrno)
2390 : {
2391 65 : struct spdk_bdev *bdev = cb_arg;
2392 :
2393 65 : if (bdeverrno && bdev) {
2394 0 : SPDK_WARNLOG("Unable to unregister bdev '%s' during spdk_bdev_finish()\n",
2395 : bdev->name);
2396 :
2397 : /*
2398 : * Since the call to spdk_bdev_unregister() failed, we have no way to free this
2399 : * bdev; try to continue by manually removing this bdev from the list and continue
2400 : * with the next bdev in the list.
2401 : */
2402 0 : TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
2403 0 : }
2404 :
2405 65 : if (TAILQ_EMPTY(&g_bdev_mgr.bdevs)) {
2406 65 : SPDK_DEBUGLOG(bdev, "Done unregistering bdevs\n");
2407 : /*
2408 : * Bdev module finish need to be deferred as we might be in the middle of some context
2409 : * (like bdev part free) that will use this bdev (or private bdev driver ctx data)
2410 : * after returning.
2411 : */
2412 65 : spdk_thread_send_msg(spdk_get_thread(), bdev_module_fini_iter, NULL);
2413 65 : return;
2414 : }
2415 :
2416 : /*
2417 : * Unregister last unclaimed bdev in the list, to ensure that bdev subsystem
2418 : * shutdown proceeds top-down. The goal is to give virtual bdevs an opportunity
2419 : * to detect clean shutdown as opposed to run-time hot removal of the underlying
2420 : * base bdevs.
2421 : *
2422 : * Also, walk the list in the reverse order.
2423 : */
2424 0 : for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list);
2425 0 : bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) {
2426 0 : spdk_spin_lock(&bdev->internal.spinlock);
2427 0 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
2428 0 : LOG_ALREADY_CLAIMED_DEBUG("claimed, skipping", bdev);
2429 0 : spdk_spin_unlock(&bdev->internal.spinlock);
2430 0 : continue;
2431 : }
2432 0 : spdk_spin_unlock(&bdev->internal.spinlock);
2433 :
2434 0 : SPDK_DEBUGLOG(bdev, "Unregistering bdev '%s'\n", bdev->name);
2435 0 : spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev);
2436 0 : return;
2437 : }
2438 :
2439 : /*
2440 : * If any bdev fails to unclaim underlying bdev properly, we may face the
2441 : * case of bdev list consisting of claimed bdevs only (if claims are managed
2442 : * correctly, this would mean there's a loop in the claims graph which is
2443 : * clearly impossible). Warn and unregister last bdev on the list then.
2444 : */
2445 0 : for (bdev = TAILQ_LAST(&g_bdev_mgr.bdevs, spdk_bdev_list);
2446 0 : bdev; bdev = TAILQ_PREV(bdev, spdk_bdev_list, internal.link)) {
2447 0 : SPDK_WARNLOG("Unregistering claimed bdev '%s'!\n", bdev->name);
2448 0 : spdk_bdev_unregister(bdev, bdev_finish_unregister_bdevs_iter, bdev);
2449 0 : return;
2450 : }
2451 65 : }
2452 :
2453 : static void
2454 65 : bdev_module_fini_start_iter(void *arg)
2455 : {
2456 : struct spdk_bdev_module *bdev_module;
2457 :
2458 65 : if (!g_resume_bdev_module) {
2459 65 : bdev_module = TAILQ_LAST(&g_bdev_mgr.bdev_modules, bdev_module_list);
2460 65 : } else {
2461 0 : bdev_module = TAILQ_PREV(g_resume_bdev_module, bdev_module_list, internal.tailq);
2462 : }
2463 :
2464 254 : while (bdev_module) {
2465 189 : if (bdev_module->async_fini_start) {
2466 : /* Save our place so we can resume later. We must
2467 : * save the variable here, before calling fini_start()
2468 : * below, because in some cases the module may immediately
2469 : * call spdk_bdev_module_fini_start_done() and re-enter
2470 : * this function to continue iterating. */
2471 0 : g_resume_bdev_module = bdev_module;
2472 0 : }
2473 :
2474 189 : if (bdev_module->fini_start) {
2475 23 : bdev_module->fini_start();
2476 23 : }
2477 :
2478 189 : if (bdev_module->async_fini_start) {
2479 0 : return;
2480 : }
2481 :
2482 189 : bdev_module = TAILQ_PREV(bdev_module, bdev_module_list, internal.tailq);
2483 : }
2484 :
2485 65 : g_resume_bdev_module = NULL;
2486 :
2487 65 : bdev_finish_unregister_bdevs_iter(NULL, 0);
2488 65 : }
2489 :
2490 : void
2491 0 : spdk_bdev_module_fini_start_done(void)
2492 : {
2493 0 : if (spdk_get_thread() != g_fini_thread) {
2494 0 : spdk_thread_send_msg(g_fini_thread, bdev_module_fini_start_iter, NULL);
2495 0 : } else {
2496 0 : bdev_module_fini_start_iter(NULL);
2497 : }
2498 0 : }
2499 :
2500 : static void
2501 65 : bdev_finish_wait_for_examine_done(void *cb_arg)
2502 : {
2503 65 : bdev_module_fini_start_iter(NULL);
2504 65 : }
2505 :
2506 : static void bdev_open_async_fini(void);
2507 :
2508 : void
2509 65 : spdk_bdev_finish(spdk_bdev_fini_cb cb_fn, void *cb_arg)
2510 : {
2511 : int rc;
2512 :
2513 65 : assert(cb_fn != NULL);
2514 :
2515 65 : g_fini_thread = spdk_get_thread();
2516 :
2517 65 : g_fini_cb_fn = cb_fn;
2518 65 : g_fini_cb_arg = cb_arg;
2519 :
2520 65 : bdev_open_async_fini();
2521 :
2522 65 : rc = spdk_bdev_wait_for_examine(bdev_finish_wait_for_examine_done, NULL);
2523 65 : if (rc != 0) {
2524 0 : SPDK_ERRLOG("wait_for_examine failed: %s\n", spdk_strerror(-rc));
2525 0 : bdev_finish_wait_for_examine_done(NULL);
2526 0 : }
2527 65 : }
2528 :
2529 : struct spdk_bdev_io *
2530 697 : bdev_channel_get_io(struct spdk_bdev_channel *channel)
2531 : {
2532 697 : struct spdk_bdev_mgmt_channel *ch = channel->shared_resource->mgmt_ch;
2533 : struct spdk_bdev_io *bdev_io;
2534 :
2535 697 : if (ch->per_thread_cache_count > 0) {
2536 637 : bdev_io = STAILQ_FIRST(&ch->per_thread_cache);
2537 637 : STAILQ_REMOVE_HEAD(&ch->per_thread_cache, internal.buf_link);
2538 637 : ch->per_thread_cache_count--;
2539 697 : } else if (spdk_unlikely(!TAILQ_EMPTY(&ch->io_wait_queue))) {
2540 : /*
2541 : * Don't try to look for bdev_ios in the global pool if there are
2542 : * waiters on bdev_ios - we don't want this caller to jump the line.
2543 : */
2544 0 : bdev_io = NULL;
2545 0 : } else {
2546 60 : bdev_io = spdk_mempool_get(g_bdev_mgr.bdev_io_pool);
2547 : }
2548 :
2549 697 : return bdev_io;
2550 : }
2551 :
2552 : void
2553 691 : spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
2554 : {
2555 : struct spdk_bdev_mgmt_channel *ch;
2556 :
2557 691 : assert(bdev_io != NULL);
2558 691 : assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING);
2559 :
2560 691 : ch = bdev_io->internal.ch->shared_resource->mgmt_ch;
2561 :
2562 691 : if (bdev_io->internal.f.has_buf) {
2563 16 : bdev_io_put_buf(bdev_io);
2564 16 : }
2565 :
2566 691 : if (ch->per_thread_cache_count < ch->bdev_io_cache_size) {
2567 637 : ch->per_thread_cache_count++;
2568 637 : STAILQ_INSERT_HEAD(&ch->per_thread_cache, bdev_io, internal.buf_link);
2569 641 : while (ch->per_thread_cache_count > 0 && !TAILQ_EMPTY(&ch->io_wait_queue)) {
2570 : struct spdk_bdev_io_wait_entry *entry;
2571 :
2572 4 : entry = TAILQ_FIRST(&ch->io_wait_queue);
2573 4 : TAILQ_REMOVE(&ch->io_wait_queue, entry, link);
2574 4 : entry->cb_fn(entry->cb_arg);
2575 : }
2576 637 : } else {
2577 : /* We should never have a full cache with entries on the io wait queue. */
2578 54 : assert(TAILQ_EMPTY(&ch->io_wait_queue));
2579 54 : spdk_mempool_put(g_bdev_mgr.bdev_io_pool, (void *)bdev_io);
2580 : }
2581 691 : }
2582 :
2583 : static bool
2584 72 : bdev_qos_is_iops_rate_limit(enum spdk_bdev_qos_rate_limit_type limit)
2585 : {
2586 72 : assert(limit != SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
2587 :
2588 72 : switch (limit) {
2589 : case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT:
2590 18 : return true;
2591 : case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT:
2592 : case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT:
2593 : case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT:
2594 54 : return false;
2595 0 : case SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES:
2596 : default:
2597 0 : return false;
2598 : }
2599 72 : }
2600 :
2601 : static bool
2602 25 : bdev_qos_io_to_limit(struct spdk_bdev_io *bdev_io)
2603 : {
2604 25 : switch (bdev_io->type) {
2605 : case SPDK_BDEV_IO_TYPE_NVME_IO:
2606 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
2607 : case SPDK_BDEV_IO_TYPE_READ:
2608 : case SPDK_BDEV_IO_TYPE_WRITE:
2609 23 : return true;
2610 : case SPDK_BDEV_IO_TYPE_ZCOPY:
2611 0 : if (bdev_io->u.bdev.zcopy.start) {
2612 0 : return true;
2613 : } else {
2614 0 : return false;
2615 : }
2616 : default:
2617 2 : return false;
2618 : }
2619 25 : }
2620 :
2621 : static bool
2622 33 : bdev_is_read_io(struct spdk_bdev_io *bdev_io)
2623 : {
2624 33 : switch (bdev_io->type) {
2625 : case SPDK_BDEV_IO_TYPE_NVME_IO:
2626 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
2627 : /* Bit 1 (0x2) set for read operation */
2628 0 : if (bdev_io->u.nvme_passthru.cmd.opc & SPDK_NVME_OPC_READ) {
2629 0 : return true;
2630 : } else {
2631 0 : return false;
2632 : }
2633 : case SPDK_BDEV_IO_TYPE_READ:
2634 30 : return true;
2635 : case SPDK_BDEV_IO_TYPE_ZCOPY:
2636 : /* Populate to read from disk */
2637 0 : if (bdev_io->u.bdev.zcopy.populate) {
2638 0 : return true;
2639 : } else {
2640 0 : return false;
2641 : }
2642 : default:
2643 3 : return false;
2644 : }
2645 33 : }
2646 :
2647 : static uint64_t
2648 43 : bdev_get_io_size_in_byte(struct spdk_bdev_io *bdev_io)
2649 : {
2650 43 : uint32_t blocklen = bdev_io_get_block_size(bdev_io);
2651 :
2652 43 : switch (bdev_io->type) {
2653 : case SPDK_BDEV_IO_TYPE_NVME_IO:
2654 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
2655 0 : return bdev_io->u.nvme_passthru.nbytes;
2656 : case SPDK_BDEV_IO_TYPE_READ:
2657 : case SPDK_BDEV_IO_TYPE_WRITE:
2658 43 : return bdev_io->u.bdev.num_blocks * blocklen;
2659 : case SPDK_BDEV_IO_TYPE_ZCOPY:
2660 : /* Track the data in the start phase only */
2661 0 : if (bdev_io->u.bdev.zcopy.start) {
2662 0 : return bdev_io->u.bdev.num_blocks * blocklen;
2663 : } else {
2664 0 : return 0;
2665 : }
2666 : default:
2667 0 : return 0;
2668 : }
2669 43 : }
2670 :
2671 : static inline bool
2672 64 : bdev_qos_rw_queue_io(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io, uint64_t delta)
2673 : {
2674 : int64_t remaining_this_timeslice;
2675 :
2676 64 : if (!limit->max_per_timeslice) {
2677 : /* The QoS is disabled */
2678 0 : return false;
2679 : }
2680 :
2681 64 : remaining_this_timeslice = __atomic_sub_fetch(&limit->remaining_this_timeslice, delta,
2682 : __ATOMIC_RELAXED);
2683 64 : if (remaining_this_timeslice + (int64_t)delta > 0) {
2684 : /* There was still a quota for this delta -> the IO shouldn't be queued
2685 : *
2686 : * We allow a slight quota overrun here so an IO bigger than the per-timeslice
2687 : * quota can be allowed once a while. Such overrun then taken into account in
2688 : * the QoS poller, where the next timeslice quota is calculated.
2689 : */
2690 59 : return false;
2691 : }
2692 :
2693 : /* There was no quota for this delta -> the IO should be queued
2694 : * The remaining_this_timeslice must be rewinded so it reflects the real
2695 : * amount of IOs or bytes allowed.
2696 : */
2697 5 : __atomic_add_fetch(
2698 5 : &limit->remaining_this_timeslice, delta, __ATOMIC_RELAXED);
2699 5 : return true;
2700 64 : }
2701 :
2702 : static inline void
2703 5 : bdev_qos_rw_rewind_io(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io, uint64_t delta)
2704 : {
2705 5 : __atomic_add_fetch(&limit->remaining_this_timeslice, delta, __ATOMIC_RELAXED);
2706 5 : }
2707 :
2708 : static bool
2709 23 : bdev_qos_rw_iops_queue(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2710 : {
2711 23 : return bdev_qos_rw_queue_io(limit, io, 1);
2712 : }
2713 :
2714 : static void
2715 3 : bdev_qos_rw_iops_rewind_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2716 : {
2717 3 : bdev_qos_rw_rewind_io(limit, io, 1);
2718 3 : }
2719 :
2720 : static bool
2721 41 : bdev_qos_rw_bps_queue(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2722 : {
2723 41 : return bdev_qos_rw_queue_io(limit, io, bdev_get_io_size_in_byte(io));
2724 : }
2725 :
2726 : static void
2727 2 : bdev_qos_rw_bps_rewind_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2728 : {
2729 2 : bdev_qos_rw_rewind_io(limit, io, bdev_get_io_size_in_byte(io));
2730 2 : }
2731 :
2732 : static bool
2733 19 : bdev_qos_r_bps_queue(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2734 : {
2735 19 : if (bdev_is_read_io(io) == false) {
2736 1 : return false;
2737 : }
2738 :
2739 18 : return bdev_qos_rw_bps_queue(limit, io);
2740 19 : }
2741 :
2742 : static void
2743 0 : bdev_qos_r_bps_rewind_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2744 : {
2745 0 : if (bdev_is_read_io(io) != false) {
2746 0 : bdev_qos_rw_rewind_io(limit, io, bdev_get_io_size_in_byte(io));
2747 0 : }
2748 0 : }
2749 :
2750 : static bool
2751 14 : bdev_qos_w_bps_queue(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2752 : {
2753 14 : if (bdev_is_read_io(io) == true) {
2754 12 : return false;
2755 : }
2756 :
2757 2 : return bdev_qos_rw_bps_queue(limit, io);
2758 14 : }
2759 :
2760 : static void
2761 0 : bdev_qos_w_bps_rewind_quota(struct spdk_bdev_qos_limit *limit, struct spdk_bdev_io *io)
2762 : {
2763 0 : if (bdev_is_read_io(io) != true) {
2764 0 : bdev_qos_rw_rewind_io(limit, io, bdev_get_io_size_in_byte(io));
2765 0 : }
2766 0 : }
2767 :
2768 : static void
2769 10 : bdev_qos_set_ops(struct spdk_bdev_qos *qos)
2770 : {
2771 : int i;
2772 :
2773 50 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2774 40 : if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
2775 15 : qos->rate_limits[i].queue_io = NULL;
2776 15 : continue;
2777 : }
2778 :
2779 25 : switch (i) {
2780 : case SPDK_BDEV_QOS_RW_IOPS_RATE_LIMIT:
2781 9 : qos->rate_limits[i].queue_io = bdev_qos_rw_iops_queue;
2782 9 : qos->rate_limits[i].rewind_quota = bdev_qos_rw_iops_rewind_quota;
2783 9 : break;
2784 : case SPDK_BDEV_QOS_RW_BPS_RATE_LIMIT:
2785 7 : qos->rate_limits[i].queue_io = bdev_qos_rw_bps_queue;
2786 7 : qos->rate_limits[i].rewind_quota = bdev_qos_rw_bps_rewind_quota;
2787 7 : break;
2788 : case SPDK_BDEV_QOS_R_BPS_RATE_LIMIT:
2789 5 : qos->rate_limits[i].queue_io = bdev_qos_r_bps_queue;
2790 5 : qos->rate_limits[i].rewind_quota = bdev_qos_r_bps_rewind_quota;
2791 5 : break;
2792 : case SPDK_BDEV_QOS_W_BPS_RATE_LIMIT:
2793 4 : qos->rate_limits[i].queue_io = bdev_qos_w_bps_queue;
2794 4 : qos->rate_limits[i].rewind_quota = bdev_qos_w_bps_rewind_quota;
2795 4 : break;
2796 : default:
2797 0 : break;
2798 : }
2799 25 : }
2800 10 : }
2801 :
2802 : static void
2803 6 : _bdev_io_complete_in_submit(struct spdk_bdev_channel *bdev_ch,
2804 : struct spdk_bdev_io *bdev_io,
2805 : enum spdk_bdev_io_status status)
2806 : {
2807 6 : bdev_io->internal.f.in_submit_request = true;
2808 6 : bdev_io_increment_outstanding(bdev_ch, bdev_ch->shared_resource);
2809 6 : spdk_bdev_io_complete(bdev_io, status);
2810 6 : bdev_io->internal.f.in_submit_request = false;
2811 6 : }
2812 :
2813 : static inline void
2814 574 : bdev_io_do_submit(struct spdk_bdev_channel *bdev_ch, struct spdk_bdev_io *bdev_io)
2815 : {
2816 574 : struct spdk_bdev *bdev = bdev_io->bdev;
2817 574 : struct spdk_io_channel *ch = bdev_ch->channel;
2818 574 : struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
2819 :
2820 574 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) {
2821 16 : struct spdk_bdev_mgmt_channel *mgmt_channel = shared_resource->mgmt_ch;
2822 16 : struct spdk_bdev_io *bio_to_abort = bdev_io->u.abort.bio_to_abort;
2823 :
2824 16 : if (bdev_abort_queued_io(&shared_resource->nomem_io, bio_to_abort) ||
2825 16 : bdev_abort_buf_io(mgmt_channel, bio_to_abort)) {
2826 0 : _bdev_io_complete_in_submit(bdev_ch, bdev_io,
2827 : SPDK_BDEV_IO_STATUS_SUCCESS);
2828 0 : return;
2829 : }
2830 16 : }
2831 :
2832 574 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE &&
2833 : bdev_io->bdev->split_on_write_unit &&
2834 : bdev_io->u.bdev.num_blocks < bdev_io->bdev->write_unit_size)) {
2835 4 : SPDK_ERRLOG("IO num_blocks %lu does not match the write_unit_size %u\n",
2836 : bdev_io->u.bdev.num_blocks, bdev_io->bdev->write_unit_size);
2837 4 : _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
2838 4 : return;
2839 : }
2840 :
2841 570 : if (spdk_likely(TAILQ_EMPTY(&shared_resource->nomem_io))) {
2842 527 : bdev_io_increment_outstanding(bdev_ch, shared_resource);
2843 527 : bdev_io->internal.f.in_submit_request = true;
2844 527 : bdev_submit_request(bdev, ch, bdev_io);
2845 527 : bdev_io->internal.f.in_submit_request = false;
2846 527 : } else {
2847 43 : bdev_queue_nomem_io_tail(shared_resource, bdev_io, BDEV_IO_RETRY_STATE_SUBMIT);
2848 43 : if (shared_resource->nomem_threshold == 0 && shared_resource->io_outstanding == 0) {
2849 : /* Special case when we have nomem IOs and no outstanding IOs which completions
2850 : * could trigger retry of queued IOs */
2851 0 : bdev_shared_ch_retry_io(shared_resource);
2852 0 : }
2853 : }
2854 574 : }
2855 :
2856 : static bool
2857 25 : bdev_qos_queue_io(struct spdk_bdev_qos *qos, struct spdk_bdev_io *bdev_io)
2858 : {
2859 : int i;
2860 :
2861 25 : if (bdev_qos_io_to_limit(bdev_io) == true) {
2862 100 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
2863 82 : if (!qos->rate_limits[i].queue_io) {
2864 5 : continue;
2865 : }
2866 :
2867 231 : if (qos->rate_limits[i].queue_io(&qos->rate_limits[i],
2868 154 : bdev_io) == true) {
2869 10 : for (i -= 1; i >= 0 ; i--) {
2870 5 : if (!qos->rate_limits[i].queue_io) {
2871 0 : continue;
2872 : }
2873 :
2874 5 : qos->rate_limits[i].rewind_quota(&qos->rate_limits[i], bdev_io);
2875 5 : }
2876 5 : return true;
2877 : }
2878 72 : }
2879 18 : }
2880 :
2881 20 : return false;
2882 25 : }
2883 :
2884 : static int
2885 27 : bdev_qos_io_submit(struct spdk_bdev_channel *ch, struct spdk_bdev_qos *qos)
2886 : {
2887 27 : struct spdk_bdev_io *bdev_io = NULL, *tmp = NULL;
2888 27 : int submitted_ios = 0;
2889 :
2890 52 : TAILQ_FOREACH_SAFE(bdev_io, &ch->qos_queued_io, internal.link, tmp) {
2891 25 : if (!bdev_qos_queue_io(qos, bdev_io)) {
2892 20 : TAILQ_REMOVE(&ch->qos_queued_io, bdev_io, internal.link);
2893 20 : bdev_io_do_submit(ch, bdev_io);
2894 :
2895 20 : submitted_ios++;
2896 20 : }
2897 25 : }
2898 :
2899 27 : return submitted_ios;
2900 : }
2901 :
2902 : static void
2903 2 : bdev_queue_io_wait_with_cb(struct spdk_bdev_io *bdev_io, spdk_bdev_io_wait_cb cb_fn)
2904 : {
2905 : int rc;
2906 :
2907 2 : bdev_io->internal.waitq_entry.bdev = bdev_io->bdev;
2908 2 : bdev_io->internal.waitq_entry.cb_fn = cb_fn;
2909 2 : bdev_io->internal.waitq_entry.cb_arg = bdev_io;
2910 4 : rc = spdk_bdev_queue_io_wait(bdev_io->bdev, spdk_io_channel_from_ctx(bdev_io->internal.ch),
2911 2 : &bdev_io->internal.waitq_entry);
2912 2 : if (rc != 0) {
2913 0 : SPDK_ERRLOG("Queue IO failed, rc=%d\n", rc);
2914 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
2915 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
2916 0 : }
2917 2 : }
2918 :
2919 : static bool
2920 621 : bdev_rw_should_split(struct spdk_bdev_io *bdev_io)
2921 : {
2922 : uint32_t io_boundary;
2923 621 : struct spdk_bdev *bdev = bdev_io->bdev;
2924 621 : uint32_t max_segment_size = bdev->max_segment_size;
2925 621 : uint32_t max_size = bdev->max_rw_size;
2926 621 : int max_segs = bdev->max_num_segments;
2927 :
2928 621 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE && bdev->split_on_write_unit) {
2929 24 : io_boundary = bdev->write_unit_size;
2930 621 : } else if (bdev->split_on_optimal_io_boundary) {
2931 168 : io_boundary = bdev->optimal_io_boundary;
2932 168 : } else {
2933 429 : io_boundary = 0;
2934 : }
2935 :
2936 621 : if (spdk_likely(!io_boundary && !max_segs && !max_segment_size && !max_size)) {
2937 243 : return false;
2938 : }
2939 :
2940 378 : if (io_boundary) {
2941 : uint64_t start_stripe, end_stripe;
2942 :
2943 192 : start_stripe = bdev_io->u.bdev.offset_blocks;
2944 192 : end_stripe = start_stripe + bdev_io->u.bdev.num_blocks - 1;
2945 : /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */
2946 192 : if (spdk_likely(spdk_u32_is_pow2(io_boundary))) {
2947 192 : start_stripe >>= spdk_u32log2(io_boundary);
2948 192 : end_stripe >>= spdk_u32log2(io_boundary);
2949 192 : } else {
2950 0 : start_stripe /= io_boundary;
2951 0 : end_stripe /= io_boundary;
2952 : }
2953 :
2954 192 : if (start_stripe != end_stripe) {
2955 75 : return true;
2956 : }
2957 117 : }
2958 :
2959 303 : if (max_segs) {
2960 150 : if (bdev_io->u.bdev.iovcnt > max_segs) {
2961 15 : return true;
2962 : }
2963 135 : }
2964 :
2965 288 : if (max_segment_size) {
2966 470 : for (int i = 0; i < bdev_io->u.bdev.iovcnt; i++) {
2967 346 : if (bdev_io->u.bdev.iovs[i].iov_len > max_segment_size) {
2968 12 : return true;
2969 : }
2970 334 : }
2971 124 : }
2972 :
2973 276 : if (max_size) {
2974 52 : if (bdev_io->u.bdev.num_blocks > max_size) {
2975 7 : return true;
2976 : }
2977 45 : }
2978 :
2979 269 : return false;
2980 621 : }
2981 :
2982 : static bool
2983 24 : bdev_unmap_should_split(struct spdk_bdev_io *bdev_io)
2984 : {
2985 : uint32_t num_unmap_segments;
2986 :
2987 24 : if (!bdev_io->bdev->max_unmap || !bdev_io->bdev->max_unmap_segments) {
2988 3 : return false;
2989 : }
2990 21 : num_unmap_segments = spdk_divide_round_up(bdev_io->u.bdev.num_blocks, bdev_io->bdev->max_unmap);
2991 21 : if (num_unmap_segments > bdev_io->bdev->max_unmap_segments) {
2992 4 : return true;
2993 : }
2994 :
2995 17 : return false;
2996 24 : }
2997 :
2998 : static bool
2999 37 : bdev_write_zeroes_should_split(struct spdk_bdev_io *bdev_io)
3000 : {
3001 37 : if (!bdev_io->bdev->max_write_zeroes) {
3002 4 : return false;
3003 : }
3004 :
3005 33 : if (bdev_io->u.bdev.num_blocks > bdev_io->bdev->max_write_zeroes) {
3006 10 : return true;
3007 : }
3008 :
3009 23 : return false;
3010 37 : }
3011 :
3012 : static bool
3013 30 : bdev_copy_should_split(struct spdk_bdev_io *bdev_io)
3014 : {
3015 30 : if (bdev_io->bdev->max_copy != 0 &&
3016 25 : bdev_io->u.bdev.num_blocks > bdev_io->bdev->max_copy) {
3017 6 : return true;
3018 : }
3019 :
3020 24 : return false;
3021 30 : }
3022 :
3023 : static bool
3024 792 : bdev_io_should_split(struct spdk_bdev_io *bdev_io)
3025 : {
3026 792 : switch (bdev_io->type) {
3027 : case SPDK_BDEV_IO_TYPE_READ:
3028 : case SPDK_BDEV_IO_TYPE_WRITE:
3029 621 : return bdev_rw_should_split(bdev_io);
3030 : case SPDK_BDEV_IO_TYPE_UNMAP:
3031 24 : return bdev_unmap_should_split(bdev_io);
3032 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3033 37 : return bdev_write_zeroes_should_split(bdev_io);
3034 : case SPDK_BDEV_IO_TYPE_COPY:
3035 30 : return bdev_copy_should_split(bdev_io);
3036 : default:
3037 80 : return false;
3038 : }
3039 792 : }
3040 :
3041 : static uint32_t
3042 249 : _to_next_boundary(uint64_t offset, uint32_t boundary)
3043 : {
3044 249 : return (boundary - (offset % boundary));
3045 : }
3046 :
3047 : static void bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg);
3048 :
3049 : static void _bdev_rw_split(void *_bdev_io);
3050 :
3051 : static void bdev_unmap_split(struct spdk_bdev_io *bdev_io);
3052 :
3053 : static void
3054 0 : _bdev_unmap_split(void *_bdev_io)
3055 : {
3056 0 : return bdev_unmap_split((struct spdk_bdev_io *)_bdev_io);
3057 : }
3058 :
3059 : static void bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io);
3060 :
3061 : static void
3062 0 : _bdev_write_zeroes_split(void *_bdev_io)
3063 : {
3064 0 : return bdev_write_zeroes_split((struct spdk_bdev_io *)_bdev_io);
3065 : }
3066 :
3067 : static void bdev_copy_split(struct spdk_bdev_io *bdev_io);
3068 :
3069 : static void
3070 0 : _bdev_copy_split(void *_bdev_io)
3071 : {
3072 0 : return bdev_copy_split((struct spdk_bdev_io *)_bdev_io);
3073 : }
3074 :
3075 : static int
3076 305 : bdev_io_split_submit(struct spdk_bdev_io *bdev_io, struct iovec *iov, int iovcnt, void *md_buf,
3077 : uint64_t num_blocks, uint64_t *offset, uint64_t *remaining)
3078 : {
3079 : int rc;
3080 : uint64_t current_offset, current_remaining, current_src_offset;
3081 : spdk_bdev_io_wait_cb io_wait_fn;
3082 :
3083 305 : current_offset = *offset;
3084 305 : current_remaining = *remaining;
3085 :
3086 305 : assert(bdev_io->internal.f.split);
3087 :
3088 305 : bdev_io->internal.split.outstanding++;
3089 :
3090 305 : io_wait_fn = _bdev_rw_split;
3091 305 : switch (bdev_io->type) {
3092 : case SPDK_BDEV_IO_TYPE_READ:
3093 196 : assert(bdev_io->u.bdev.accel_sequence == NULL);
3094 392 : rc = bdev_readv_blocks_with_md(bdev_io->internal.desc,
3095 196 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
3096 196 : iov, iovcnt, md_buf, current_offset,
3097 196 : num_blocks,
3098 196 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain : NULL,
3099 196 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain_ctx : NULL,
3100 : NULL,
3101 196 : bdev_io->u.bdev.dif_check_flags,
3102 196 : bdev_io_split_done, bdev_io);
3103 196 : break;
3104 : case SPDK_BDEV_IO_TYPE_WRITE:
3105 50 : assert(bdev_io->u.bdev.accel_sequence == NULL);
3106 100 : rc = bdev_writev_blocks_with_md(bdev_io->internal.desc,
3107 50 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
3108 50 : iov, iovcnt, md_buf, current_offset,
3109 50 : num_blocks,
3110 50 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain : NULL,
3111 50 : bdev_io_use_memory_domain(bdev_io) ? bdev_io->internal.memory_domain_ctx : NULL,
3112 : NULL,
3113 50 : bdev_io->u.bdev.dif_check_flags,
3114 50 : bdev_io->u.bdev.nvme_cdw12.raw,
3115 50 : bdev_io->u.bdev.nvme_cdw13.raw,
3116 50 : bdev_io_split_done, bdev_io);
3117 50 : break;
3118 : case SPDK_BDEV_IO_TYPE_UNMAP:
3119 17 : io_wait_fn = _bdev_unmap_split;
3120 34 : rc = spdk_bdev_unmap_blocks(bdev_io->internal.desc,
3121 17 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
3122 17 : current_offset, num_blocks,
3123 17 : bdev_io_split_done, bdev_io);
3124 17 : break;
3125 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3126 23 : io_wait_fn = _bdev_write_zeroes_split;
3127 46 : rc = spdk_bdev_write_zeroes_blocks(bdev_io->internal.desc,
3128 23 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
3129 23 : current_offset, num_blocks,
3130 23 : bdev_io_split_done, bdev_io);
3131 23 : break;
3132 : case SPDK_BDEV_IO_TYPE_COPY:
3133 19 : io_wait_fn = _bdev_copy_split;
3134 38 : current_src_offset = bdev_io->u.bdev.copy.src_offset_blocks +
3135 19 : (current_offset - bdev_io->u.bdev.offset_blocks);
3136 38 : rc = spdk_bdev_copy_blocks(bdev_io->internal.desc,
3137 19 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
3138 19 : current_offset, current_src_offset, num_blocks,
3139 19 : bdev_io_split_done, bdev_io);
3140 19 : break;
3141 : default:
3142 0 : assert(false);
3143 : rc = -EINVAL;
3144 : break;
3145 : }
3146 :
3147 305 : if (rc == 0) {
3148 301 : current_offset += num_blocks;
3149 301 : current_remaining -= num_blocks;
3150 301 : bdev_io->internal.split.current_offset_blocks = current_offset;
3151 301 : bdev_io->internal.split.remaining_num_blocks = current_remaining;
3152 301 : *offset = current_offset;
3153 301 : *remaining = current_remaining;
3154 301 : } else {
3155 4 : bdev_io->internal.split.outstanding--;
3156 4 : if (rc == -ENOMEM) {
3157 4 : if (bdev_io->internal.split.outstanding == 0) {
3158 : /* No I/O is outstanding. Hence we should wait here. */
3159 1 : bdev_queue_io_wait_with_cb(bdev_io, io_wait_fn);
3160 1 : }
3161 4 : } else {
3162 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
3163 0 : if (bdev_io->internal.split.outstanding == 0) {
3164 0 : bdev_ch_remove_from_io_submitted(bdev_io);
3165 0 : spdk_trace_record(TRACE_BDEV_IO_DONE, bdev_io->internal.ch->trace_id,
3166 : 0, (uintptr_t)bdev_io, bdev_io->internal.caller_ctx,
3167 : bdev_io->internal.ch->queue_depth);
3168 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
3169 0 : }
3170 : }
3171 : }
3172 :
3173 305 : return rc;
3174 : }
3175 :
3176 : static void
3177 67 : _bdev_rw_split(void *_bdev_io)
3178 : {
3179 : struct iovec *parent_iov, *iov;
3180 67 : struct spdk_bdev_io *bdev_io = _bdev_io;
3181 67 : struct spdk_bdev *bdev = bdev_io->bdev;
3182 : uint64_t parent_offset, current_offset, remaining;
3183 : uint32_t parent_iov_offset, parent_iovcnt, parent_iovpos, child_iovcnt;
3184 : uint32_t to_next_boundary, to_next_boundary_bytes, to_last_block_bytes;
3185 : uint32_t iovcnt, iov_len, child_iovsize;
3186 : uint32_t blocklen;
3187 : uint32_t io_boundary;
3188 67 : uint32_t max_segment_size = bdev->max_segment_size;
3189 67 : uint32_t max_child_iovcnt = bdev->max_num_segments;
3190 67 : uint32_t max_size = bdev->max_rw_size;
3191 67 : void *md_buf = NULL;
3192 : int rc;
3193 :
3194 67 : blocklen = bdev_io_get_block_size(bdev_io);
3195 :
3196 67 : max_size = max_size ? max_size : UINT32_MAX;
3197 67 : max_segment_size = max_segment_size ? max_segment_size : UINT32_MAX;
3198 67 : max_child_iovcnt = max_child_iovcnt ? spdk_min(max_child_iovcnt, SPDK_BDEV_IO_NUM_CHILD_IOV) :
3199 : SPDK_BDEV_IO_NUM_CHILD_IOV;
3200 :
3201 67 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE && bdev->split_on_write_unit) {
3202 5 : io_boundary = bdev->write_unit_size;
3203 67 : } else if (bdev->split_on_optimal_io_boundary) {
3204 40 : io_boundary = bdev->optimal_io_boundary;
3205 40 : } else {
3206 22 : io_boundary = UINT32_MAX;
3207 : }
3208 :
3209 67 : assert(bdev_io->internal.f.split);
3210 :
3211 67 : remaining = bdev_io->internal.split.remaining_num_blocks;
3212 67 : current_offset = bdev_io->internal.split.current_offset_blocks;
3213 67 : parent_offset = bdev_io->u.bdev.offset_blocks;
3214 67 : parent_iov_offset = (current_offset - parent_offset) * blocklen;
3215 67 : parent_iovcnt = bdev_io->u.bdev.iovcnt;
3216 :
3217 420 : for (parent_iovpos = 0; parent_iovpos < parent_iovcnt; parent_iovpos++) {
3218 420 : parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos];
3219 420 : if (parent_iov_offset < parent_iov->iov_len) {
3220 67 : break;
3221 : }
3222 353 : parent_iov_offset -= parent_iov->iov_len;
3223 353 : }
3224 :
3225 67 : child_iovcnt = 0;
3226 573 : while (remaining > 0 && parent_iovpos < parent_iovcnt &&
3227 264 : child_iovcnt < SPDK_BDEV_IO_NUM_CHILD_IOV) {
3228 249 : to_next_boundary = _to_next_boundary(current_offset, io_boundary);
3229 249 : to_next_boundary = spdk_min(remaining, to_next_boundary);
3230 249 : to_next_boundary = spdk_min(max_size, to_next_boundary);
3231 249 : to_next_boundary_bytes = to_next_boundary * blocklen;
3232 :
3233 249 : iov = &bdev_io->child_iov[child_iovcnt];
3234 249 : iovcnt = 0;
3235 :
3236 249 : if (bdev_io->u.bdev.md_buf) {
3237 48 : md_buf = (char *)bdev_io->u.bdev.md_buf +
3238 24 : (current_offset - parent_offset) * spdk_bdev_get_md_size(bdev);
3239 24 : }
3240 :
3241 249 : child_iovsize = spdk_min(SPDK_BDEV_IO_NUM_CHILD_IOV - child_iovcnt, max_child_iovcnt);
3242 1810 : while (to_next_boundary_bytes > 0 && parent_iovpos < parent_iovcnt &&
3243 836 : iovcnt < child_iovsize) {
3244 725 : parent_iov = &bdev_io->u.bdev.iovs[parent_iovpos];
3245 725 : iov_len = parent_iov->iov_len - parent_iov_offset;
3246 :
3247 725 : iov_len = spdk_min(iov_len, max_segment_size);
3248 725 : iov_len = spdk_min(iov_len, to_next_boundary_bytes);
3249 725 : to_next_boundary_bytes -= iov_len;
3250 :
3251 725 : bdev_io->child_iov[child_iovcnt].iov_base = parent_iov->iov_base + parent_iov_offset;
3252 725 : bdev_io->child_iov[child_iovcnt].iov_len = iov_len;
3253 :
3254 725 : if (iov_len < parent_iov->iov_len - parent_iov_offset) {
3255 183 : parent_iov_offset += iov_len;
3256 183 : } else {
3257 542 : parent_iovpos++;
3258 542 : parent_iov_offset = 0;
3259 : }
3260 725 : child_iovcnt++;
3261 725 : iovcnt++;
3262 : }
3263 :
3264 249 : if (to_next_boundary_bytes > 0) {
3265 : /* We had to stop this child I/O early because we ran out of
3266 : * child_iov space or were limited by max_num_segments.
3267 : * Ensure the iovs to be aligned with block size and
3268 : * then adjust to_next_boundary before starting the
3269 : * child I/O.
3270 : */
3271 111 : assert(child_iovcnt == SPDK_BDEV_IO_NUM_CHILD_IOV ||
3272 : iovcnt == child_iovsize);
3273 111 : to_last_block_bytes = to_next_boundary_bytes % blocklen;
3274 111 : if (to_last_block_bytes != 0) {
3275 24 : uint32_t child_iovpos = child_iovcnt - 1;
3276 : /* don't decrease child_iovcnt when it equals to SPDK_BDEV_IO_NUM_CHILD_IOV
3277 : * so the loop will naturally end
3278 : */
3279 :
3280 24 : to_last_block_bytes = blocklen - to_last_block_bytes;
3281 24 : to_next_boundary_bytes += to_last_block_bytes;
3282 53 : while (to_last_block_bytes > 0 && iovcnt > 0) {
3283 32 : iov_len = spdk_min(to_last_block_bytes,
3284 : bdev_io->child_iov[child_iovpos].iov_len);
3285 32 : bdev_io->child_iov[child_iovpos].iov_len -= iov_len;
3286 32 : if (bdev_io->child_iov[child_iovpos].iov_len == 0) {
3287 15 : child_iovpos--;
3288 15 : if (--iovcnt == 0) {
3289 : /* If the child IO is less than a block size just return.
3290 : * If the first child IO of any split round is less than
3291 : * a block size, an error exit.
3292 : */
3293 3 : if (bdev_io->internal.split.outstanding == 0) {
3294 1 : SPDK_ERRLOG("The first child io was less than a block size\n");
3295 1 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
3296 1 : bdev_ch_remove_from_io_submitted(bdev_io);
3297 1 : spdk_trace_record(TRACE_BDEV_IO_DONE, bdev_io->internal.ch->trace_id,
3298 : 0, (uintptr_t)bdev_io, bdev_io->internal.caller_ctx,
3299 : bdev_io->internal.ch->queue_depth);
3300 1 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
3301 1 : }
3302 :
3303 3 : return;
3304 : }
3305 12 : }
3306 :
3307 29 : to_last_block_bytes -= iov_len;
3308 :
3309 29 : if (parent_iov_offset == 0) {
3310 14 : parent_iovpos--;
3311 14 : parent_iov_offset = bdev_io->u.bdev.iovs[parent_iovpos].iov_len;
3312 14 : }
3313 29 : parent_iov_offset -= iov_len;
3314 : }
3315 :
3316 21 : assert(to_last_block_bytes == 0);
3317 21 : }
3318 108 : to_next_boundary -= to_next_boundary_bytes / blocklen;
3319 108 : }
3320 :
3321 246 : rc = bdev_io_split_submit(bdev_io, iov, iovcnt, md_buf, to_next_boundary,
3322 : ¤t_offset, &remaining);
3323 246 : if (spdk_unlikely(rc)) {
3324 4 : return;
3325 : }
3326 : }
3327 67 : }
3328 :
3329 : static void
3330 3 : bdev_unmap_split(struct spdk_bdev_io *bdev_io)
3331 : {
3332 : uint64_t offset, unmap_blocks, remaining, max_unmap_blocks;
3333 3 : uint32_t num_children_reqs = 0;
3334 : int rc;
3335 :
3336 3 : assert(bdev_io->internal.f.split);
3337 :
3338 3 : offset = bdev_io->internal.split.current_offset_blocks;
3339 3 : remaining = bdev_io->internal.split.remaining_num_blocks;
3340 3 : max_unmap_blocks = bdev_io->bdev->max_unmap * bdev_io->bdev->max_unmap_segments;
3341 :
3342 20 : while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS)) {
3343 17 : unmap_blocks = spdk_min(remaining, max_unmap_blocks);
3344 :
3345 17 : rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, unmap_blocks,
3346 : &offset, &remaining);
3347 17 : if (spdk_likely(rc == 0)) {
3348 17 : num_children_reqs++;
3349 17 : } else {
3350 0 : return;
3351 : }
3352 : }
3353 3 : }
3354 :
3355 : static void
3356 6 : bdev_write_zeroes_split(struct spdk_bdev_io *bdev_io)
3357 : {
3358 : uint64_t offset, write_zeroes_blocks, remaining;
3359 6 : uint32_t num_children_reqs = 0;
3360 : int rc;
3361 :
3362 6 : assert(bdev_io->internal.f.split);
3363 :
3364 6 : offset = bdev_io->internal.split.current_offset_blocks;
3365 6 : remaining = bdev_io->internal.split.remaining_num_blocks;
3366 :
3367 29 : while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS)) {
3368 23 : write_zeroes_blocks = spdk_min(remaining, bdev_io->bdev->max_write_zeroes);
3369 :
3370 23 : rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, write_zeroes_blocks,
3371 : &offset, &remaining);
3372 23 : if (spdk_likely(rc == 0)) {
3373 23 : num_children_reqs++;
3374 23 : } else {
3375 0 : return;
3376 : }
3377 : }
3378 6 : }
3379 :
3380 : static void
3381 4 : bdev_copy_split(struct spdk_bdev_io *bdev_io)
3382 : {
3383 : uint64_t offset, copy_blocks, remaining;
3384 4 : uint32_t num_children_reqs = 0;
3385 : int rc;
3386 :
3387 4 : assert(bdev_io->internal.f.split);
3388 :
3389 4 : offset = bdev_io->internal.split.current_offset_blocks;
3390 4 : remaining = bdev_io->internal.split.remaining_num_blocks;
3391 :
3392 4 : assert(bdev_io->bdev->max_copy != 0);
3393 23 : while (remaining && (num_children_reqs < SPDK_BDEV_MAX_CHILDREN_COPY_REQS)) {
3394 19 : copy_blocks = spdk_min(remaining, bdev_io->bdev->max_copy);
3395 :
3396 19 : rc = bdev_io_split_submit(bdev_io, NULL, 0, NULL, copy_blocks,
3397 : &offset, &remaining);
3398 19 : if (spdk_likely(rc == 0)) {
3399 19 : num_children_reqs++;
3400 19 : } else {
3401 0 : return;
3402 : }
3403 : }
3404 4 : }
3405 :
3406 : static void
3407 58 : parent_bdev_io_complete(void *ctx, int rc)
3408 : {
3409 58 : struct spdk_bdev_io *parent_io = ctx;
3410 :
3411 58 : if (rc) {
3412 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
3413 0 : }
3414 :
3415 116 : parent_io->internal.cb(parent_io, parent_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
3416 58 : parent_io->internal.caller_ctx);
3417 58 : }
3418 :
3419 : static void
3420 0 : bdev_io_complete_parent_sequence_cb(void *ctx, int status)
3421 : {
3422 0 : struct spdk_bdev_io *bdev_io = ctx;
3423 :
3424 : /* u.bdev.accel_sequence should have already been cleared at this point */
3425 0 : assert(bdev_io->u.bdev.accel_sequence == NULL);
3426 0 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS);
3427 0 : bdev_io->internal.f.has_accel_sequence = false;
3428 :
3429 0 : if (spdk_unlikely(status != 0)) {
3430 0 : SPDK_ERRLOG("Failed to execute accel sequence, status=%d\n", status);
3431 0 : }
3432 :
3433 0 : parent_bdev_io_complete(bdev_io, status);
3434 0 : }
3435 :
3436 : static void
3437 301 : bdev_io_split_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
3438 : {
3439 301 : struct spdk_bdev_io *parent_io = cb_arg;
3440 :
3441 301 : spdk_bdev_free_io(bdev_io);
3442 :
3443 301 : assert(parent_io->internal.f.split);
3444 :
3445 301 : if (!success) {
3446 21 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
3447 : /* If any child I/O failed, stop further splitting process. */
3448 21 : parent_io->internal.split.current_offset_blocks += parent_io->internal.split.remaining_num_blocks;
3449 21 : parent_io->internal.split.remaining_num_blocks = 0;
3450 21 : }
3451 301 : parent_io->internal.split.outstanding--;
3452 301 : if (parent_io->internal.split.outstanding != 0) {
3453 223 : return;
3454 : }
3455 :
3456 : /*
3457 : * Parent I/O finishes when all blocks are consumed.
3458 : */
3459 78 : if (parent_io->internal.split.remaining_num_blocks == 0) {
3460 58 : assert(parent_io->internal.cb != bdev_io_split_done);
3461 58 : bdev_ch_remove_from_io_submitted(parent_io);
3462 58 : spdk_trace_record(TRACE_BDEV_IO_DONE, parent_io->internal.ch->trace_id,
3463 : 0, (uintptr_t)parent_io, bdev_io->internal.caller_ctx,
3464 : parent_io->internal.ch->queue_depth);
3465 :
3466 58 : if (spdk_likely(parent_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
3467 48 : if (bdev_io_needs_sequence_exec(parent_io->internal.desc, parent_io)) {
3468 0 : bdev_io_exec_sequence(parent_io, bdev_io_complete_parent_sequence_cb);
3469 0 : return;
3470 48 : } else if (parent_io->internal.f.has_bounce_buf &&
3471 0 : !bdev_io_use_accel_sequence(bdev_io)) {
3472 : /* bdev IO will be completed in the callback */
3473 0 : _bdev_io_push_bounce_data_buffer(parent_io, parent_bdev_io_complete);
3474 0 : return;
3475 : }
3476 48 : }
3477 :
3478 58 : parent_bdev_io_complete(parent_io, 0);
3479 58 : return;
3480 : }
3481 :
3482 : /*
3483 : * Continue with the splitting process. This function will complete the parent I/O if the
3484 : * splitting is done.
3485 : */
3486 20 : switch (parent_io->type) {
3487 : case SPDK_BDEV_IO_TYPE_READ:
3488 : case SPDK_BDEV_IO_TYPE_WRITE:
3489 17 : _bdev_rw_split(parent_io);
3490 17 : break;
3491 : case SPDK_BDEV_IO_TYPE_UNMAP:
3492 1 : bdev_unmap_split(parent_io);
3493 1 : break;
3494 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3495 1 : bdev_write_zeroes_split(parent_io);
3496 1 : break;
3497 : case SPDK_BDEV_IO_TYPE_COPY:
3498 1 : bdev_copy_split(parent_io);
3499 1 : break;
3500 : default:
3501 0 : assert(false);
3502 : break;
3503 : }
3504 301 : }
3505 :
3506 : static void bdev_rw_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
3507 : bool success);
3508 :
3509 : static void
3510 59 : bdev_io_split(struct spdk_bdev_io *bdev_io)
3511 : {
3512 59 : assert(bdev_io_should_split(bdev_io));
3513 59 : assert(bdev_io->internal.f.split);
3514 :
3515 59 : bdev_io->internal.split.current_offset_blocks = bdev_io->u.bdev.offset_blocks;
3516 59 : bdev_io->internal.split.remaining_num_blocks = bdev_io->u.bdev.num_blocks;
3517 59 : bdev_io->internal.split.outstanding = 0;
3518 59 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
3519 :
3520 59 : switch (bdev_io->type) {
3521 : case SPDK_BDEV_IO_TYPE_READ:
3522 : case SPDK_BDEV_IO_TYPE_WRITE:
3523 49 : if (_is_buf_allocated(bdev_io->u.bdev.iovs)) {
3524 49 : _bdev_rw_split(bdev_io);
3525 49 : } else {
3526 0 : assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
3527 0 : spdk_bdev_io_get_buf(bdev_io, bdev_rw_split_get_buf_cb,
3528 0 : bdev_io->u.bdev.num_blocks * bdev_io_get_block_size(bdev_io));
3529 : }
3530 49 : break;
3531 : case SPDK_BDEV_IO_TYPE_UNMAP:
3532 2 : bdev_unmap_split(bdev_io);
3533 2 : break;
3534 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3535 5 : bdev_write_zeroes_split(bdev_io);
3536 5 : break;
3537 : case SPDK_BDEV_IO_TYPE_COPY:
3538 3 : bdev_copy_split(bdev_io);
3539 3 : break;
3540 : default:
3541 0 : assert(false);
3542 : break;
3543 : }
3544 59 : }
3545 :
3546 : static void
3547 0 : bdev_rw_split_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success)
3548 : {
3549 0 : if (!success) {
3550 0 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
3551 0 : return;
3552 : }
3553 :
3554 0 : _bdev_rw_split(bdev_io);
3555 0 : }
3556 :
3557 : static inline void
3558 579 : _bdev_io_submit(struct spdk_bdev_io *bdev_io)
3559 : {
3560 579 : struct spdk_bdev *bdev = bdev_io->bdev;
3561 579 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
3562 :
3563 579 : if (spdk_likely(bdev_ch->flags == 0)) {
3564 554 : bdev_io_do_submit(bdev_ch, bdev_io);
3565 554 : return;
3566 : }
3567 :
3568 25 : if (bdev_ch->flags & BDEV_CH_RESET_IN_PROGRESS) {
3569 2 : _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
3570 25 : } else if (bdev_ch->flags & BDEV_CH_QOS_ENABLED) {
3571 23 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT) &&
3572 2 : bdev_abort_queued_io(&bdev_ch->qos_queued_io, bdev_io->u.abort.bio_to_abort)) {
3573 0 : _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
3574 0 : } else {
3575 23 : TAILQ_INSERT_TAIL(&bdev_ch->qos_queued_io, bdev_io, internal.link);
3576 23 : bdev_qos_io_submit(bdev_ch, bdev->internal.qos);
3577 : }
3578 23 : } else {
3579 0 : SPDK_ERRLOG("unknown bdev_ch flag %x found\n", bdev_ch->flags);
3580 0 : _bdev_io_complete_in_submit(bdev_ch, bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
3581 : }
3582 579 : }
3583 :
3584 : bool bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2);
3585 :
3586 : bool
3587 23 : bdev_lba_range_overlapped(struct lba_range *range1, struct lba_range *range2)
3588 : {
3589 23 : if (range1->length == 0 || range2->length == 0) {
3590 1 : return false;
3591 : }
3592 :
3593 22 : if (range1->offset + range1->length <= range2->offset) {
3594 1 : return false;
3595 : }
3596 :
3597 21 : if (range2->offset + range2->length <= range1->offset) {
3598 3 : return false;
3599 : }
3600 :
3601 18 : return true;
3602 23 : }
3603 :
3604 : static bool
3605 11 : bdev_io_range_is_locked(struct spdk_bdev_io *bdev_io, struct lba_range *range)
3606 : {
3607 11 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
3608 : struct lba_range r;
3609 :
3610 11 : switch (bdev_io->type) {
3611 : case SPDK_BDEV_IO_TYPE_NVME_IO:
3612 : case SPDK_BDEV_IO_TYPE_NVME_IO_MD:
3613 : /* Don't try to decode the NVMe command - just assume worst-case and that
3614 : * it overlaps a locked range.
3615 : */
3616 0 : return true;
3617 : case SPDK_BDEV_IO_TYPE_READ:
3618 6 : if (!range->quiesce) {
3619 4 : return false;
3620 : }
3621 : /* fallthrough */
3622 : case SPDK_BDEV_IO_TYPE_WRITE:
3623 : case SPDK_BDEV_IO_TYPE_UNMAP:
3624 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3625 : case SPDK_BDEV_IO_TYPE_ZCOPY:
3626 : case SPDK_BDEV_IO_TYPE_COPY:
3627 7 : r.offset = bdev_io->u.bdev.offset_blocks;
3628 7 : r.length = bdev_io->u.bdev.num_blocks;
3629 7 : if (!bdev_lba_range_overlapped(range, &r)) {
3630 : /* This I/O doesn't overlap the specified LBA range. */
3631 0 : return false;
3632 7 : } else if (range->owner_ch == ch && range->locked_ctx == bdev_io->internal.caller_ctx) {
3633 : /* This I/O overlaps, but the I/O is on the same channel that locked this
3634 : * range, and the caller_ctx is the same as the locked_ctx. This means
3635 : * that this I/O is associated with the lock, and is allowed to execute.
3636 : */
3637 2 : return false;
3638 : } else {
3639 5 : return true;
3640 : }
3641 : default:
3642 0 : return false;
3643 : }
3644 11 : }
3645 :
3646 : void
3647 639 : bdev_io_submit(struct spdk_bdev_io *bdev_io)
3648 : {
3649 639 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
3650 :
3651 639 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING);
3652 :
3653 639 : if (!TAILQ_EMPTY(&ch->locked_ranges)) {
3654 : struct lba_range *range;
3655 :
3656 13 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
3657 8 : if (bdev_io_range_is_locked(bdev_io, range)) {
3658 3 : TAILQ_INSERT_TAIL(&ch->io_locked, bdev_io, internal.ch_link);
3659 3 : return;
3660 : }
3661 5 : }
3662 5 : }
3663 :
3664 636 : bdev_ch_add_to_io_submitted(bdev_io);
3665 :
3666 636 : bdev_io->internal.submit_tsc = spdk_get_ticks();
3667 636 : spdk_trace_record_tsc(bdev_io->internal.submit_tsc, TRACE_BDEV_IO_START,
3668 : ch->trace_id, bdev_io->u.bdev.num_blocks,
3669 : (uintptr_t)bdev_io, (uint64_t)bdev_io->type, bdev_io->internal.caller_ctx,
3670 : bdev_io->u.bdev.offset_blocks, ch->queue_depth);
3671 :
3672 636 : if (bdev_io->internal.f.split) {
3673 59 : bdev_io_split(bdev_io);
3674 59 : return;
3675 : }
3676 :
3677 577 : _bdev_io_submit(bdev_io);
3678 639 : }
3679 :
3680 : static inline void
3681 4 : _bdev_io_ext_use_bounce_buffer(struct spdk_bdev_io *bdev_io)
3682 : {
3683 : /* bdev doesn't support memory domains, thereby buffers in this IO request can't
3684 : * be accessed directly. It is needed to allocate buffers before issuing IO operation.
3685 : * For write operation we need to pull buffers from memory domain before submitting IO.
3686 : * Once read operation completes, we need to use memory_domain push functionality to
3687 : * update data in original memory domain IO buffer
3688 : * This IO request will go through a regular IO flow, so clear memory domains pointers */
3689 4 : assert(bdev_io->internal.f.has_memory_domain);
3690 4 : bdev_io->u.bdev.memory_domain = NULL;
3691 4 : bdev_io->u.bdev.memory_domain_ctx = NULL;
3692 8 : _bdev_memory_domain_io_get_buf(bdev_io, _bdev_memory_domain_get_io_cb,
3693 4 : bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
3694 4 : }
3695 :
3696 : static inline void
3697 292 : _bdev_io_submit_ext(struct spdk_bdev_desc *desc, struct spdk_bdev_io *bdev_io)
3698 : {
3699 292 : struct spdk_bdev_channel *ch = bdev_io->internal.ch;
3700 292 : bool needs_exec = bdev_io_needs_sequence_exec(desc, bdev_io);
3701 :
3702 292 : if (spdk_unlikely(ch->flags & BDEV_CH_RESET_IN_PROGRESS)) {
3703 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_ABORTED;
3704 0 : bdev_io_complete_unsubmitted(bdev_io);
3705 0 : return;
3706 : }
3707 :
3708 : /* We need to allocate bounce buffer if bdev doesn't support memory domains, or if it does
3709 : * support them, but we need to execute an accel sequence and the data buffer is from accel
3710 : * memory domain (to avoid doing a push/pull from that domain).
3711 : */
3712 292 : if (bdev_io_use_memory_domain(bdev_io)) {
3713 4 : if (!desc->memory_domains_supported ||
3714 0 : (needs_exec && bdev_io->internal.memory_domain == spdk_accel_get_memory_domain())) {
3715 4 : _bdev_io_ext_use_bounce_buffer(bdev_io);
3716 4 : return;
3717 : }
3718 0 : }
3719 :
3720 288 : if (needs_exec) {
3721 0 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
3722 0 : bdev_io_exec_sequence(bdev_io, bdev_io_submit_sequence_cb);
3723 0 : return;
3724 : }
3725 : /* For reads we'll execute the sequence after the data is read, so, for now, only
3726 : * clear out accel_sequence pointer and submit the IO */
3727 0 : assert(bdev_io->type == SPDK_BDEV_IO_TYPE_READ);
3728 0 : bdev_io->u.bdev.accel_sequence = NULL;
3729 0 : }
3730 :
3731 288 : bdev_io_submit(bdev_io);
3732 292 : }
3733 :
3734 : static void
3735 11 : bdev_io_submit_reset(struct spdk_bdev_io *bdev_io)
3736 : {
3737 11 : struct spdk_bdev *bdev = bdev_io->bdev;
3738 11 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
3739 11 : struct spdk_io_channel *ch = bdev_ch->channel;
3740 :
3741 11 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_PENDING);
3742 :
3743 11 : bdev_io->internal.f.in_submit_request = true;
3744 11 : bdev_submit_request(bdev, ch, bdev_io);
3745 11 : bdev_io->internal.f.in_submit_request = false;
3746 11 : }
3747 :
3748 : void
3749 691 : bdev_io_init(struct spdk_bdev_io *bdev_io,
3750 : struct spdk_bdev *bdev, void *cb_arg,
3751 : spdk_bdev_io_completion_cb cb)
3752 : {
3753 691 : bdev_io->bdev = bdev;
3754 691 : bdev_io->internal.f.raw = 0;
3755 691 : bdev_io->internal.caller_ctx = cb_arg;
3756 691 : bdev_io->internal.cb = cb;
3757 691 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
3758 691 : bdev_io->internal.f.in_submit_request = false;
3759 691 : bdev_io->internal.error.nvme.cdw0 = 0;
3760 691 : bdev_io->num_retries = 0;
3761 691 : bdev_io->internal.get_buf_cb = NULL;
3762 691 : bdev_io->internal.get_aux_buf_cb = NULL;
3763 691 : bdev_io->internal.data_transfer_cpl = NULL;
3764 691 : bdev_io->internal.f.split = bdev_io_should_split(bdev_io);
3765 691 : }
3766 :
3767 : static bool
3768 520 : bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
3769 : {
3770 520 : return bdev->fn_table->io_type_supported(bdev->ctxt, io_type);
3771 : }
3772 :
3773 : bool
3774 170 : spdk_bdev_io_type_supported(struct spdk_bdev *bdev, enum spdk_bdev_io_type io_type)
3775 : {
3776 : bool supported;
3777 :
3778 170 : supported = bdev_io_type_supported(bdev, io_type);
3779 :
3780 170 : if (!supported) {
3781 7 : switch (io_type) {
3782 : case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
3783 : /* The bdev layer will emulate write zeroes as long as write is supported. */
3784 0 : supported = bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE);
3785 0 : break;
3786 : default:
3787 7 : break;
3788 : }
3789 7 : }
3790 :
3791 170 : return supported;
3792 : }
3793 :
3794 : static const char *g_io_type_strings[] = {
3795 : [SPDK_BDEV_IO_TYPE_READ] = "read",
3796 : [SPDK_BDEV_IO_TYPE_WRITE] = "write",
3797 : [SPDK_BDEV_IO_TYPE_UNMAP] = "unmap",
3798 : [SPDK_BDEV_IO_TYPE_FLUSH] = "flush",
3799 : [SPDK_BDEV_IO_TYPE_RESET] = "reset",
3800 : [SPDK_BDEV_IO_TYPE_NVME_ADMIN] = "nvme_admin",
3801 : [SPDK_BDEV_IO_TYPE_NVME_IO] = "nvme_io",
3802 : [SPDK_BDEV_IO_TYPE_NVME_IO_MD] = "nvme_io_md",
3803 : [SPDK_BDEV_IO_TYPE_WRITE_ZEROES] = "write_zeroes",
3804 : [SPDK_BDEV_IO_TYPE_ZCOPY] = "zcopy",
3805 : [SPDK_BDEV_IO_TYPE_GET_ZONE_INFO] = "get_zone_info",
3806 : [SPDK_BDEV_IO_TYPE_ZONE_MANAGEMENT] = "zone_management",
3807 : [SPDK_BDEV_IO_TYPE_ZONE_APPEND] = "zone_append",
3808 : [SPDK_BDEV_IO_TYPE_COMPARE] = "compare",
3809 : [SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE] = "compare_and_write",
3810 : [SPDK_BDEV_IO_TYPE_ABORT] = "abort",
3811 : [SPDK_BDEV_IO_TYPE_SEEK_HOLE] = "seek_hole",
3812 : [SPDK_BDEV_IO_TYPE_SEEK_DATA] = "seek_data",
3813 : [SPDK_BDEV_IO_TYPE_COPY] = "copy",
3814 : [SPDK_BDEV_IO_TYPE_NVME_IOV_MD] = "nvme_iov_md",
3815 : };
3816 :
3817 : const char *
3818 0 : spdk_bdev_get_io_type_name(enum spdk_bdev_io_type io_type)
3819 : {
3820 0 : if (io_type <= SPDK_BDEV_IO_TYPE_INVALID || io_type >= SPDK_BDEV_NUM_IO_TYPES) {
3821 0 : return NULL;
3822 : }
3823 :
3824 0 : return g_io_type_strings[io_type];
3825 0 : }
3826 :
3827 : int
3828 0 : spdk_bdev_get_io_type(const char *io_type_string)
3829 : {
3830 : int i;
3831 :
3832 0 : for (i = SPDK_BDEV_IO_TYPE_READ; i < SPDK_BDEV_NUM_IO_TYPES; ++i) {
3833 0 : if (!strcmp(io_type_string, g_io_type_strings[i])) {
3834 0 : return i;
3835 : }
3836 0 : }
3837 :
3838 0 : return -1;
3839 0 : }
3840 :
3841 : uint64_t
3842 0 : spdk_bdev_io_get_submit_tsc(struct spdk_bdev_io *bdev_io)
3843 : {
3844 0 : return bdev_io->internal.submit_tsc;
3845 : }
3846 :
3847 : int
3848 0 : spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
3849 : {
3850 0 : if (bdev->fn_table->dump_info_json) {
3851 0 : return bdev->fn_table->dump_info_json(bdev->ctxt, w);
3852 : }
3853 :
3854 0 : return 0;
3855 0 : }
3856 :
3857 : static void
3858 10 : bdev_qos_update_max_quota_per_timeslice(struct spdk_bdev_qos *qos)
3859 : {
3860 10 : uint32_t max_per_timeslice = 0;
3861 : int i;
3862 :
3863 50 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3864 40 : if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
3865 15 : qos->rate_limits[i].max_per_timeslice = 0;
3866 15 : continue;
3867 : }
3868 :
3869 50 : max_per_timeslice = qos->rate_limits[i].limit *
3870 25 : SPDK_BDEV_QOS_TIMESLICE_IN_USEC / SPDK_SEC_TO_USEC;
3871 :
3872 25 : qos->rate_limits[i].max_per_timeslice = spdk_max(max_per_timeslice,
3873 : qos->rate_limits[i].min_per_timeslice);
3874 :
3875 50 : __atomic_store_n(&qos->rate_limits[i].remaining_this_timeslice,
3876 25 : qos->rate_limits[i].max_per_timeslice, __ATOMIC_RELEASE);
3877 25 : }
3878 :
3879 10 : bdev_qos_set_ops(qos);
3880 10 : }
3881 :
3882 : static void
3883 4 : bdev_channel_submit_qos_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
3884 : struct spdk_io_channel *io_ch, void *ctx)
3885 : {
3886 4 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
3887 : int status;
3888 :
3889 4 : bdev_qos_io_submit(bdev_ch, bdev->internal.qos);
3890 :
3891 : /* if all IOs were sent then continue the iteration, otherwise - stop it */
3892 : /* TODO: channels round robing */
3893 4 : status = TAILQ_EMPTY(&bdev_ch->qos_queued_io) ? 0 : 1;
3894 :
3895 4 : spdk_bdev_for_each_channel_continue(i, status);
3896 4 : }
3897 :
3898 :
3899 : static void
3900 2 : bdev_channel_submit_qos_io_done(struct spdk_bdev *bdev, void *ctx, int status)
3901 : {
3902 :
3903 2 : }
3904 :
3905 : static int
3906 3 : bdev_channel_poll_qos(void *arg)
3907 : {
3908 3 : struct spdk_bdev *bdev = arg;
3909 3 : struct spdk_bdev_qos *qos = bdev->internal.qos;
3910 3 : uint64_t now = spdk_get_ticks();
3911 : int i;
3912 : int64_t remaining_last_timeslice;
3913 :
3914 3 : if (spdk_unlikely(qos->thread == NULL)) {
3915 : /* Old QoS was unbound to remove and new QoS is not enabled yet. */
3916 1 : return SPDK_POLLER_IDLE;
3917 : }
3918 :
3919 2 : if (now < (qos->last_timeslice + qos->timeslice_size)) {
3920 : /* We received our callback earlier than expected - return
3921 : * immediately and wait to do accounting until at least one
3922 : * timeslice has actually expired. This should never happen
3923 : * with a well-behaved timer implementation.
3924 : */
3925 0 : return SPDK_POLLER_IDLE;
3926 : }
3927 :
3928 : /* Reset for next round of rate limiting */
3929 10 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3930 : /* We may have allowed the IOs or bytes to slightly overrun in the last
3931 : * timeslice. remaining_this_timeslice is signed, so if it's negative
3932 : * here, we'll account for the overrun so that the next timeslice will
3933 : * be appropriately reduced.
3934 : */
3935 8 : remaining_last_timeslice = __atomic_exchange_n(&qos->rate_limits[i].remaining_this_timeslice,
3936 : 0, __ATOMIC_RELAXED);
3937 8 : if (remaining_last_timeslice < 0) {
3938 : /* There could be a race condition here as both bdev_qos_rw_queue_io() and bdev_channel_poll_qos()
3939 : * potentially use 2 atomic ops each, so they can intertwine.
3940 : * This race can potentially cause the limits to be a little fuzzy but won't cause any real damage.
3941 : */
3942 0 : __atomic_store_n(&qos->rate_limits[i].remaining_this_timeslice,
3943 0 : remaining_last_timeslice, __ATOMIC_RELAXED);
3944 0 : }
3945 8 : }
3946 :
3947 4 : while (now >= (qos->last_timeslice + qos->timeslice_size)) {
3948 2 : qos->last_timeslice += qos->timeslice_size;
3949 10 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
3950 16 : __atomic_add_fetch(&qos->rate_limits[i].remaining_this_timeslice,
3951 8 : qos->rate_limits[i].max_per_timeslice, __ATOMIC_RELAXED);
3952 8 : }
3953 : }
3954 :
3955 2 : spdk_bdev_for_each_channel(bdev, bdev_channel_submit_qos_io, qos,
3956 : bdev_channel_submit_qos_io_done);
3957 :
3958 2 : return SPDK_POLLER_BUSY;
3959 3 : }
3960 :
3961 : static void
3962 74 : bdev_channel_destroy_resource(struct spdk_bdev_channel *ch)
3963 : {
3964 : struct spdk_bdev_shared_resource *shared_resource;
3965 : struct lba_range *range;
3966 :
3967 74 : bdev_free_io_stat(ch->stat);
3968 : #ifdef SPDK_CONFIG_VTUNE
3969 : bdev_free_io_stat(ch->prev_stat);
3970 : #endif
3971 :
3972 74 : while (!TAILQ_EMPTY(&ch->locked_ranges)) {
3973 0 : range = TAILQ_FIRST(&ch->locked_ranges);
3974 0 : TAILQ_REMOVE(&ch->locked_ranges, range, tailq);
3975 0 : free(range);
3976 : }
3977 :
3978 74 : spdk_put_io_channel(ch->channel);
3979 74 : spdk_put_io_channel(ch->accel_channel);
3980 :
3981 74 : shared_resource = ch->shared_resource;
3982 :
3983 74 : assert(TAILQ_EMPTY(&ch->io_locked));
3984 74 : assert(TAILQ_EMPTY(&ch->io_submitted));
3985 74 : assert(TAILQ_EMPTY(&ch->io_accel_exec));
3986 74 : assert(TAILQ_EMPTY(&ch->io_memory_domain));
3987 74 : assert(ch->io_outstanding == 0);
3988 74 : assert(shared_resource->ref > 0);
3989 74 : shared_resource->ref--;
3990 74 : if (shared_resource->ref == 0) {
3991 73 : assert(shared_resource->io_outstanding == 0);
3992 73 : TAILQ_REMOVE(&shared_resource->mgmt_ch->shared_resources, shared_resource, link);
3993 73 : spdk_put_io_channel(spdk_io_channel_from_ctx(shared_resource->mgmt_ch));
3994 73 : spdk_poller_unregister(&shared_resource->nomem_poller);
3995 73 : free(shared_resource);
3996 73 : }
3997 74 : }
3998 :
3999 : static void
4000 83 : bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch)
4001 : {
4002 83 : struct spdk_bdev_qos *qos = bdev->internal.qos;
4003 : int i;
4004 :
4005 83 : assert(spdk_spin_held(&bdev->internal.spinlock));
4006 :
4007 : /* Rate limiting on this bdev enabled */
4008 83 : if (qos) {
4009 17 : if (qos->ch == NULL) {
4010 : struct spdk_io_channel *io_ch;
4011 :
4012 9 : SPDK_DEBUGLOG(bdev, "Selecting channel %p as QoS channel for bdev %s on thread %p\n", ch,
4013 : bdev->name, spdk_get_thread());
4014 :
4015 : /* No qos channel has been selected, so set one up */
4016 :
4017 : /* Take another reference to ch */
4018 9 : io_ch = spdk_get_io_channel(__bdev_to_io_dev(bdev));
4019 9 : assert(io_ch != NULL);
4020 9 : qos->ch = ch;
4021 :
4022 9 : qos->thread = spdk_io_channel_get_thread(io_ch);
4023 :
4024 45 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4025 36 : if (bdev_qos_is_iops_rate_limit(i) == true) {
4026 9 : qos->rate_limits[i].min_per_timeslice =
4027 : SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE;
4028 9 : } else {
4029 27 : qos->rate_limits[i].min_per_timeslice =
4030 : SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE;
4031 : }
4032 :
4033 36 : if (qos->rate_limits[i].limit == 0) {
4034 2 : qos->rate_limits[i].limit = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
4035 2 : }
4036 36 : }
4037 9 : bdev_qos_update_max_quota_per_timeslice(qos);
4038 9 : qos->timeslice_size =
4039 9 : SPDK_BDEV_QOS_TIMESLICE_IN_USEC * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
4040 9 : qos->last_timeslice = spdk_get_ticks();
4041 9 : qos->poller = SPDK_POLLER_REGISTER(bdev_channel_poll_qos,
4042 : bdev,
4043 : SPDK_BDEV_QOS_TIMESLICE_IN_USEC);
4044 9 : }
4045 :
4046 17 : ch->flags |= BDEV_CH_QOS_ENABLED;
4047 17 : }
4048 83 : }
4049 :
4050 : struct poll_timeout_ctx {
4051 : struct spdk_bdev_desc *desc;
4052 : uint64_t timeout_in_sec;
4053 : spdk_bdev_io_timeout_cb cb_fn;
4054 : void *cb_arg;
4055 : };
4056 :
4057 : static void
4058 267 : bdev_desc_free(struct spdk_bdev_desc *desc)
4059 : {
4060 267 : spdk_spin_destroy(&desc->spinlock);
4061 267 : free(desc->media_events_buffer);
4062 267 : free(desc);
4063 267 : }
4064 :
4065 : static void
4066 8 : bdev_channel_poll_timeout_io_done(struct spdk_bdev *bdev, void *_ctx, int status)
4067 : {
4068 8 : struct poll_timeout_ctx *ctx = _ctx;
4069 8 : struct spdk_bdev_desc *desc = ctx->desc;
4070 :
4071 8 : free(ctx);
4072 :
4073 8 : spdk_spin_lock(&desc->spinlock);
4074 8 : desc->refs--;
4075 8 : if (desc->closed == true && desc->refs == 0) {
4076 1 : spdk_spin_unlock(&desc->spinlock);
4077 1 : bdev_desc_free(desc);
4078 1 : return;
4079 : }
4080 7 : spdk_spin_unlock(&desc->spinlock);
4081 8 : }
4082 :
4083 : static void
4084 13 : bdev_channel_poll_timeout_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
4085 : struct spdk_io_channel *io_ch, void *_ctx)
4086 : {
4087 13 : struct poll_timeout_ctx *ctx = _ctx;
4088 13 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
4089 13 : struct spdk_bdev_desc *desc = ctx->desc;
4090 : struct spdk_bdev_io *bdev_io;
4091 : uint64_t now;
4092 :
4093 13 : spdk_spin_lock(&desc->spinlock);
4094 13 : if (desc->closed == true) {
4095 1 : spdk_spin_unlock(&desc->spinlock);
4096 1 : spdk_bdev_for_each_channel_continue(i, -1);
4097 1 : return;
4098 : }
4099 12 : spdk_spin_unlock(&desc->spinlock);
4100 :
4101 12 : now = spdk_get_ticks();
4102 22 : TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) {
4103 : /* Exclude any I/O that are generated via splitting. */
4104 15 : if (bdev_io->internal.cb == bdev_io_split_done) {
4105 3 : continue;
4106 : }
4107 :
4108 : /* Once we find an I/O that has not timed out, we can immediately
4109 : * exit the loop.
4110 : */
4111 24 : if (now < (bdev_io->internal.submit_tsc +
4112 12 : ctx->timeout_in_sec * spdk_get_ticks_hz())) {
4113 5 : goto end;
4114 : }
4115 :
4116 7 : if (bdev_io->internal.desc == desc) {
4117 7 : ctx->cb_fn(ctx->cb_arg, bdev_io);
4118 7 : }
4119 14 : }
4120 :
4121 : end:
4122 12 : spdk_bdev_for_each_channel_continue(i, 0);
4123 13 : }
4124 :
4125 : static int
4126 8 : bdev_poll_timeout_io(void *arg)
4127 : {
4128 8 : struct spdk_bdev_desc *desc = arg;
4129 8 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4130 : struct poll_timeout_ctx *ctx;
4131 :
4132 8 : ctx = calloc(1, sizeof(struct poll_timeout_ctx));
4133 8 : if (!ctx) {
4134 0 : SPDK_ERRLOG("failed to allocate memory\n");
4135 0 : return SPDK_POLLER_BUSY;
4136 : }
4137 8 : ctx->desc = desc;
4138 8 : ctx->cb_arg = desc->cb_arg;
4139 8 : ctx->cb_fn = desc->cb_fn;
4140 8 : ctx->timeout_in_sec = desc->timeout_in_sec;
4141 :
4142 : /* Take a ref on the descriptor in case it gets closed while we are checking
4143 : * all of the channels.
4144 : */
4145 8 : spdk_spin_lock(&desc->spinlock);
4146 8 : desc->refs++;
4147 8 : spdk_spin_unlock(&desc->spinlock);
4148 :
4149 8 : spdk_bdev_for_each_channel(bdev, bdev_channel_poll_timeout_io, ctx,
4150 : bdev_channel_poll_timeout_io_done);
4151 :
4152 8 : return SPDK_POLLER_BUSY;
4153 8 : }
4154 :
4155 : int
4156 5 : spdk_bdev_set_timeout(struct spdk_bdev_desc *desc, uint64_t timeout_in_sec,
4157 : spdk_bdev_io_timeout_cb cb_fn, void *cb_arg)
4158 : {
4159 5 : assert(desc->thread == spdk_get_thread());
4160 :
4161 5 : spdk_poller_unregister(&desc->io_timeout_poller);
4162 :
4163 5 : if (timeout_in_sec) {
4164 4 : assert(cb_fn != NULL);
4165 4 : desc->io_timeout_poller = SPDK_POLLER_REGISTER(bdev_poll_timeout_io,
4166 : desc,
4167 : SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * SPDK_SEC_TO_USEC /
4168 : 1000);
4169 4 : if (desc->io_timeout_poller == NULL) {
4170 0 : SPDK_ERRLOG("can not register the desc timeout IO poller\n");
4171 0 : return -1;
4172 : }
4173 4 : }
4174 :
4175 5 : desc->cb_fn = cb_fn;
4176 5 : desc->cb_arg = cb_arg;
4177 5 : desc->timeout_in_sec = timeout_in_sec;
4178 :
4179 5 : return 0;
4180 5 : }
4181 :
4182 : static int
4183 76 : bdev_channel_create(void *io_device, void *ctx_buf)
4184 : {
4185 76 : struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
4186 76 : struct spdk_bdev_channel *ch = ctx_buf;
4187 : struct spdk_io_channel *mgmt_io_ch;
4188 : struct spdk_bdev_mgmt_channel *mgmt_ch;
4189 : struct spdk_bdev_shared_resource *shared_resource;
4190 : struct lba_range *range;
4191 :
4192 76 : ch->bdev = bdev;
4193 76 : ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt);
4194 76 : if (!ch->channel) {
4195 2 : return -1;
4196 : }
4197 :
4198 74 : ch->accel_channel = spdk_accel_get_io_channel();
4199 74 : if (!ch->accel_channel) {
4200 0 : spdk_put_io_channel(ch->channel);
4201 0 : return -1;
4202 : }
4203 :
4204 74 : spdk_trace_record(TRACE_BDEV_IOCH_CREATE, bdev->internal.trace_id, 0, 0,
4205 : spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel)));
4206 :
4207 74 : assert(ch->histogram == NULL);
4208 74 : if (bdev->internal.histogram_enabled) {
4209 0 : ch->histogram = spdk_histogram_data_alloc();
4210 0 : if (ch->histogram == NULL) {
4211 0 : SPDK_ERRLOG("Could not allocate histogram\n");
4212 0 : }
4213 0 : }
4214 :
4215 74 : mgmt_io_ch = spdk_get_io_channel(&g_bdev_mgr);
4216 74 : if (!mgmt_io_ch) {
4217 0 : spdk_put_io_channel(ch->channel);
4218 0 : spdk_put_io_channel(ch->accel_channel);
4219 0 : return -1;
4220 : }
4221 :
4222 74 : mgmt_ch = __io_ch_to_bdev_mgmt_ch(mgmt_io_ch);
4223 76 : TAILQ_FOREACH(shared_resource, &mgmt_ch->shared_resources, link) {
4224 3 : if (shared_resource->shared_ch == ch->channel) {
4225 1 : spdk_put_io_channel(mgmt_io_ch);
4226 1 : shared_resource->ref++;
4227 1 : break;
4228 : }
4229 2 : }
4230 :
4231 74 : if (shared_resource == NULL) {
4232 73 : shared_resource = calloc(1, sizeof(*shared_resource));
4233 73 : if (shared_resource == NULL) {
4234 0 : spdk_put_io_channel(ch->channel);
4235 0 : spdk_put_io_channel(ch->accel_channel);
4236 0 : spdk_put_io_channel(mgmt_io_ch);
4237 0 : return -1;
4238 : }
4239 :
4240 73 : shared_resource->mgmt_ch = mgmt_ch;
4241 73 : shared_resource->io_outstanding = 0;
4242 73 : TAILQ_INIT(&shared_resource->nomem_io);
4243 73 : shared_resource->nomem_threshold = 0;
4244 73 : shared_resource->shared_ch = ch->channel;
4245 73 : shared_resource->ref = 1;
4246 73 : TAILQ_INSERT_TAIL(&mgmt_ch->shared_resources, shared_resource, link);
4247 73 : }
4248 :
4249 74 : ch->io_outstanding = 0;
4250 74 : TAILQ_INIT(&ch->queued_resets);
4251 74 : TAILQ_INIT(&ch->locked_ranges);
4252 74 : TAILQ_INIT(&ch->qos_queued_io);
4253 74 : ch->flags = 0;
4254 74 : ch->trace_id = bdev->internal.trace_id;
4255 74 : ch->shared_resource = shared_resource;
4256 :
4257 74 : TAILQ_INIT(&ch->io_submitted);
4258 74 : TAILQ_INIT(&ch->io_locked);
4259 74 : TAILQ_INIT(&ch->io_accel_exec);
4260 74 : TAILQ_INIT(&ch->io_memory_domain);
4261 :
4262 74 : ch->stat = bdev_alloc_io_stat(false);
4263 74 : if (ch->stat == NULL) {
4264 0 : bdev_channel_destroy_resource(ch);
4265 0 : return -1;
4266 : }
4267 :
4268 74 : ch->stat->ticks_rate = spdk_get_ticks_hz();
4269 :
4270 : #ifdef SPDK_CONFIG_VTUNE
4271 : {
4272 : char *name;
4273 : __itt_init_ittlib(NULL, 0);
4274 : name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch);
4275 : if (!name) {
4276 : bdev_channel_destroy_resource(ch);
4277 : return -1;
4278 : }
4279 : ch->handle = __itt_string_handle_create(name);
4280 : free(name);
4281 : ch->start_tsc = spdk_get_ticks();
4282 : ch->interval_tsc = spdk_get_ticks_hz() / 100;
4283 : ch->prev_stat = bdev_alloc_io_stat(false);
4284 : if (ch->prev_stat == NULL) {
4285 : bdev_channel_destroy_resource(ch);
4286 : return -1;
4287 : }
4288 : }
4289 : #endif
4290 :
4291 74 : spdk_spin_lock(&bdev->internal.spinlock);
4292 74 : bdev_enable_qos(bdev, ch);
4293 :
4294 75 : TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) {
4295 : struct lba_range *new_range;
4296 :
4297 1 : new_range = calloc(1, sizeof(*new_range));
4298 1 : if (new_range == NULL) {
4299 0 : spdk_spin_unlock(&bdev->internal.spinlock);
4300 0 : bdev_channel_destroy_resource(ch);
4301 0 : return -1;
4302 : }
4303 1 : new_range->length = range->length;
4304 1 : new_range->offset = range->offset;
4305 1 : new_range->locked_ctx = range->locked_ctx;
4306 1 : TAILQ_INSERT_TAIL(&ch->locked_ranges, new_range, tailq);
4307 1 : }
4308 :
4309 74 : spdk_spin_unlock(&bdev->internal.spinlock);
4310 :
4311 74 : return 0;
4312 76 : }
4313 :
4314 : static int
4315 0 : bdev_abort_all_buf_io_cb(struct spdk_iobuf_channel *ch, struct spdk_iobuf_entry *entry,
4316 : void *cb_ctx)
4317 : {
4318 0 : struct spdk_bdev_channel *bdev_ch = cb_ctx;
4319 : struct spdk_bdev_io *bdev_io;
4320 : uint64_t buf_len;
4321 :
4322 0 : bdev_io = SPDK_CONTAINEROF(entry, struct spdk_bdev_io, internal.iobuf);
4323 0 : if (bdev_io->internal.ch == bdev_ch) {
4324 0 : buf_len = bdev_io_get_max_buf_len(bdev_io, bdev_io->internal.buf.len);
4325 0 : spdk_iobuf_entry_abort(ch, entry, buf_len);
4326 0 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
4327 0 : }
4328 :
4329 0 : return 0;
4330 : }
4331 :
4332 : /*
4333 : * Abort I/O that are waiting on a data buffer.
4334 : */
4335 : static void
4336 96 : bdev_abort_all_buf_io(struct spdk_bdev_mgmt_channel *mgmt_ch, struct spdk_bdev_channel *ch)
4337 : {
4338 96 : spdk_iobuf_for_each_entry(&mgmt_ch->iobuf, bdev_abort_all_buf_io_cb, ch);
4339 96 : }
4340 :
4341 : /*
4342 : * Abort I/O that are queued waiting for submission. These types of I/O are
4343 : * linked using the spdk_bdev_io link TAILQ_ENTRY.
4344 : */
4345 : static void
4346 188 : bdev_abort_all_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch)
4347 : {
4348 : struct spdk_bdev_io *bdev_io, *tmp;
4349 :
4350 228 : TAILQ_FOREACH_SAFE(bdev_io, queue, internal.link, tmp) {
4351 40 : if (bdev_io->internal.ch == ch) {
4352 40 : TAILQ_REMOVE(queue, bdev_io, internal.link);
4353 : /*
4354 : * spdk_bdev_io_complete() assumes that the completed I/O had
4355 : * been submitted to the bdev module. Since in this case it
4356 : * hadn't, bump io_outstanding to account for the decrement
4357 : * that spdk_bdev_io_complete() will do.
4358 : */
4359 40 : if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) {
4360 39 : bdev_io_increment_outstanding(ch, ch->shared_resource);
4361 39 : }
4362 40 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
4363 40 : }
4364 40 : }
4365 188 : }
4366 :
4367 : static bool
4368 18 : bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort)
4369 : {
4370 : struct spdk_bdev_io *bdev_io;
4371 :
4372 18 : TAILQ_FOREACH(bdev_io, queue, internal.link) {
4373 0 : if (bdev_io == bio_to_abort) {
4374 0 : TAILQ_REMOVE(queue, bio_to_abort, internal.link);
4375 0 : spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED);
4376 0 : return true;
4377 : }
4378 0 : }
4379 :
4380 18 : return false;
4381 18 : }
4382 :
4383 : static int
4384 0 : bdev_abort_buf_io_cb(struct spdk_iobuf_channel *ch, struct spdk_iobuf_entry *entry, void *cb_ctx)
4385 : {
4386 0 : struct spdk_bdev_io *bdev_io, *bio_to_abort = cb_ctx;
4387 : uint64_t buf_len;
4388 :
4389 0 : bdev_io = SPDK_CONTAINEROF(entry, struct spdk_bdev_io, internal.iobuf);
4390 0 : if (bdev_io == bio_to_abort) {
4391 0 : buf_len = bdev_io_get_max_buf_len(bdev_io, bdev_io->internal.buf.len);
4392 0 : spdk_iobuf_entry_abort(ch, entry, buf_len);
4393 0 : spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED);
4394 0 : return 1;
4395 : }
4396 :
4397 0 : return 0;
4398 0 : }
4399 :
4400 : static bool
4401 16 : bdev_abort_buf_io(struct spdk_bdev_mgmt_channel *mgmt_ch, struct spdk_bdev_io *bio_to_abort)
4402 : {
4403 : int rc;
4404 :
4405 16 : rc = spdk_iobuf_for_each_entry(&mgmt_ch->iobuf, bdev_abort_buf_io_cb, bio_to_abort);
4406 16 : return rc == 1;
4407 : }
4408 :
4409 : static void
4410 7 : bdev_qos_channel_destroy(void *cb_arg)
4411 : {
4412 7 : struct spdk_bdev_qos *qos = cb_arg;
4413 :
4414 7 : spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
4415 7 : spdk_poller_unregister(&qos->poller);
4416 :
4417 7 : SPDK_DEBUGLOG(bdev, "Free QoS %p.\n", qos);
4418 :
4419 7 : free(qos);
4420 7 : }
4421 :
4422 : static int
4423 7 : bdev_qos_destroy(struct spdk_bdev *bdev)
4424 : {
4425 : int i;
4426 :
4427 : /*
4428 : * Cleanly shutting down the QoS poller is tricky, because
4429 : * during the asynchronous operation the user could open
4430 : * a new descriptor and create a new channel, spawning
4431 : * a new QoS poller.
4432 : *
4433 : * The strategy is to create a new QoS structure here and swap it
4434 : * in. The shutdown path then continues to refer to the old one
4435 : * until it completes and then releases it.
4436 : */
4437 : struct spdk_bdev_qos *new_qos, *old_qos;
4438 :
4439 7 : old_qos = bdev->internal.qos;
4440 :
4441 7 : new_qos = calloc(1, sizeof(*new_qos));
4442 7 : if (!new_qos) {
4443 0 : SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n");
4444 0 : return -ENOMEM;
4445 : }
4446 :
4447 : /* Copy the old QoS data into the newly allocated structure */
4448 7 : memcpy(new_qos, old_qos, sizeof(*new_qos));
4449 :
4450 : /* Zero out the key parts of the QoS structure */
4451 7 : new_qos->ch = NULL;
4452 7 : new_qos->thread = NULL;
4453 7 : new_qos->poller = NULL;
4454 : /*
4455 : * The limit member of spdk_bdev_qos_limit structure is not zeroed.
4456 : * It will be used later for the new QoS structure.
4457 : */
4458 35 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4459 28 : new_qos->rate_limits[i].remaining_this_timeslice = 0;
4460 28 : new_qos->rate_limits[i].min_per_timeslice = 0;
4461 28 : new_qos->rate_limits[i].max_per_timeslice = 0;
4462 28 : }
4463 :
4464 7 : bdev->internal.qos = new_qos;
4465 :
4466 7 : if (old_qos->thread == NULL) {
4467 0 : free(old_qos);
4468 0 : } else {
4469 7 : spdk_thread_send_msg(old_qos->thread, bdev_qos_channel_destroy, old_qos);
4470 : }
4471 :
4472 : /* It is safe to continue with destroying the bdev even though the QoS channel hasn't
4473 : * been destroyed yet. The destruction path will end up waiting for the final
4474 : * channel to be put before it releases resources. */
4475 :
4476 7 : return 0;
4477 7 : }
4478 :
4479 : void
4480 78 : spdk_bdev_add_io_stat(struct spdk_bdev_io_stat *total, struct spdk_bdev_io_stat *add)
4481 : {
4482 78 : total->bytes_read += add->bytes_read;
4483 78 : total->num_read_ops += add->num_read_ops;
4484 78 : total->bytes_written += add->bytes_written;
4485 78 : total->num_write_ops += add->num_write_ops;
4486 78 : total->bytes_unmapped += add->bytes_unmapped;
4487 78 : total->num_unmap_ops += add->num_unmap_ops;
4488 78 : total->bytes_copied += add->bytes_copied;
4489 78 : total->num_copy_ops += add->num_copy_ops;
4490 78 : total->read_latency_ticks += add->read_latency_ticks;
4491 78 : total->write_latency_ticks += add->write_latency_ticks;
4492 78 : total->unmap_latency_ticks += add->unmap_latency_ticks;
4493 78 : total->copy_latency_ticks += add->copy_latency_ticks;
4494 78 : if (total->max_read_latency_ticks < add->max_read_latency_ticks) {
4495 7 : total->max_read_latency_ticks = add->max_read_latency_ticks;
4496 7 : }
4497 78 : if (total->min_read_latency_ticks > add->min_read_latency_ticks) {
4498 39 : total->min_read_latency_ticks = add->min_read_latency_ticks;
4499 39 : }
4500 78 : if (total->max_write_latency_ticks < add->max_write_latency_ticks) {
4501 4 : total->max_write_latency_ticks = add->max_write_latency_ticks;
4502 4 : }
4503 78 : if (total->min_write_latency_ticks > add->min_write_latency_ticks) {
4504 24 : total->min_write_latency_ticks = add->min_write_latency_ticks;
4505 24 : }
4506 78 : if (total->max_unmap_latency_ticks < add->max_unmap_latency_ticks) {
4507 0 : total->max_unmap_latency_ticks = add->max_unmap_latency_ticks;
4508 0 : }
4509 78 : if (total->min_unmap_latency_ticks > add->min_unmap_latency_ticks) {
4510 3 : total->min_unmap_latency_ticks = add->min_unmap_latency_ticks;
4511 3 : }
4512 78 : if (total->max_copy_latency_ticks < add->max_copy_latency_ticks) {
4513 0 : total->max_copy_latency_ticks = add->max_copy_latency_ticks;
4514 0 : }
4515 78 : if (total->min_copy_latency_ticks > add->min_copy_latency_ticks) {
4516 4 : total->min_copy_latency_ticks = add->min_copy_latency_ticks;
4517 4 : }
4518 78 : }
4519 :
4520 : static void
4521 5 : bdev_get_io_stat(struct spdk_bdev_io_stat *to_stat, struct spdk_bdev_io_stat *from_stat)
4522 : {
4523 5 : memcpy(to_stat, from_stat, offsetof(struct spdk_bdev_io_stat, io_error));
4524 :
4525 5 : if (to_stat->io_error != NULL && from_stat->io_error != NULL) {
4526 0 : memcpy(to_stat->io_error, from_stat->io_error,
4527 : sizeof(struct spdk_bdev_io_error_stat));
4528 0 : }
4529 5 : }
4530 :
4531 : void
4532 210 : spdk_bdev_reset_io_stat(struct spdk_bdev_io_stat *stat, enum spdk_bdev_reset_stat_mode mode)
4533 : {
4534 210 : if (mode == SPDK_BDEV_RESET_STAT_NONE) {
4535 5 : return;
4536 : }
4537 :
4538 205 : stat->max_read_latency_ticks = 0;
4539 205 : stat->min_read_latency_ticks = UINT64_MAX;
4540 205 : stat->max_write_latency_ticks = 0;
4541 205 : stat->min_write_latency_ticks = UINT64_MAX;
4542 205 : stat->max_unmap_latency_ticks = 0;
4543 205 : stat->min_unmap_latency_ticks = UINT64_MAX;
4544 205 : stat->max_copy_latency_ticks = 0;
4545 205 : stat->min_copy_latency_ticks = UINT64_MAX;
4546 :
4547 205 : if (mode != SPDK_BDEV_RESET_STAT_ALL) {
4548 2 : return;
4549 : }
4550 :
4551 203 : stat->bytes_read = 0;
4552 203 : stat->num_read_ops = 0;
4553 203 : stat->bytes_written = 0;
4554 203 : stat->num_write_ops = 0;
4555 203 : stat->bytes_unmapped = 0;
4556 203 : stat->num_unmap_ops = 0;
4557 203 : stat->bytes_copied = 0;
4558 203 : stat->num_copy_ops = 0;
4559 203 : stat->read_latency_ticks = 0;
4560 203 : stat->write_latency_ticks = 0;
4561 203 : stat->unmap_latency_ticks = 0;
4562 203 : stat->copy_latency_ticks = 0;
4563 :
4564 203 : if (stat->io_error != NULL) {
4565 128 : memset(stat->io_error, 0, sizeof(struct spdk_bdev_io_error_stat));
4566 128 : }
4567 210 : }
4568 :
4569 : struct spdk_bdev_io_stat *
4570 201 : bdev_alloc_io_stat(bool io_error_stat)
4571 : {
4572 : struct spdk_bdev_io_stat *stat;
4573 :
4574 201 : stat = malloc(sizeof(struct spdk_bdev_io_stat));
4575 201 : if (stat == NULL) {
4576 0 : return NULL;
4577 : }
4578 :
4579 201 : if (io_error_stat) {
4580 127 : stat->io_error = malloc(sizeof(struct spdk_bdev_io_error_stat));
4581 127 : if (stat->io_error == NULL) {
4582 0 : free(stat);
4583 0 : return NULL;
4584 : }
4585 127 : } else {
4586 74 : stat->io_error = NULL;
4587 : }
4588 :
4589 201 : spdk_bdev_reset_io_stat(stat, SPDK_BDEV_RESET_STAT_ALL);
4590 :
4591 201 : return stat;
4592 201 : }
4593 :
4594 : void
4595 201 : bdev_free_io_stat(struct spdk_bdev_io_stat *stat)
4596 : {
4597 201 : if (stat != NULL) {
4598 201 : free(stat->io_error);
4599 201 : free(stat);
4600 201 : }
4601 201 : }
4602 :
4603 : void
4604 0 : spdk_bdev_dump_io_stat_json(struct spdk_bdev_io_stat *stat, struct spdk_json_write_ctx *w)
4605 : {
4606 : int i;
4607 :
4608 0 : spdk_json_write_named_uint64(w, "bytes_read", stat->bytes_read);
4609 0 : spdk_json_write_named_uint64(w, "num_read_ops", stat->num_read_ops);
4610 0 : spdk_json_write_named_uint64(w, "bytes_written", stat->bytes_written);
4611 0 : spdk_json_write_named_uint64(w, "num_write_ops", stat->num_write_ops);
4612 0 : spdk_json_write_named_uint64(w, "bytes_unmapped", stat->bytes_unmapped);
4613 0 : spdk_json_write_named_uint64(w, "num_unmap_ops", stat->num_unmap_ops);
4614 0 : spdk_json_write_named_uint64(w, "bytes_copied", stat->bytes_copied);
4615 0 : spdk_json_write_named_uint64(w, "num_copy_ops", stat->num_copy_ops);
4616 0 : spdk_json_write_named_uint64(w, "read_latency_ticks", stat->read_latency_ticks);
4617 0 : spdk_json_write_named_uint64(w, "max_read_latency_ticks", stat->max_read_latency_ticks);
4618 0 : spdk_json_write_named_uint64(w, "min_read_latency_ticks",
4619 0 : stat->min_read_latency_ticks != UINT64_MAX ?
4620 0 : stat->min_read_latency_ticks : 0);
4621 0 : spdk_json_write_named_uint64(w, "write_latency_ticks", stat->write_latency_ticks);
4622 0 : spdk_json_write_named_uint64(w, "max_write_latency_ticks", stat->max_write_latency_ticks);
4623 0 : spdk_json_write_named_uint64(w, "min_write_latency_ticks",
4624 0 : stat->min_write_latency_ticks != UINT64_MAX ?
4625 0 : stat->min_write_latency_ticks : 0);
4626 0 : spdk_json_write_named_uint64(w, "unmap_latency_ticks", stat->unmap_latency_ticks);
4627 0 : spdk_json_write_named_uint64(w, "max_unmap_latency_ticks", stat->max_unmap_latency_ticks);
4628 0 : spdk_json_write_named_uint64(w, "min_unmap_latency_ticks",
4629 0 : stat->min_unmap_latency_ticks != UINT64_MAX ?
4630 0 : stat->min_unmap_latency_ticks : 0);
4631 0 : spdk_json_write_named_uint64(w, "copy_latency_ticks", stat->copy_latency_ticks);
4632 0 : spdk_json_write_named_uint64(w, "max_copy_latency_ticks", stat->max_copy_latency_ticks);
4633 0 : spdk_json_write_named_uint64(w, "min_copy_latency_ticks",
4634 0 : stat->min_copy_latency_ticks != UINT64_MAX ?
4635 0 : stat->min_copy_latency_ticks : 0);
4636 :
4637 0 : if (stat->io_error != NULL) {
4638 0 : spdk_json_write_named_object_begin(w, "io_error");
4639 0 : for (i = 0; i < -SPDK_MIN_BDEV_IO_STATUS; i++) {
4640 0 : if (stat->io_error->error_status[i] != 0) {
4641 0 : spdk_json_write_named_uint32(w, bdev_io_status_get_string(-(i + 1)),
4642 0 : stat->io_error->error_status[i]);
4643 0 : }
4644 0 : }
4645 0 : spdk_json_write_object_end(w);
4646 0 : }
4647 0 : }
4648 :
4649 : static void
4650 78 : bdev_channel_abort_queued_ios(struct spdk_bdev_channel *ch)
4651 : {
4652 78 : struct spdk_bdev_shared_resource *shared_resource = ch->shared_resource;
4653 78 : struct spdk_bdev_mgmt_channel *mgmt_ch = shared_resource->mgmt_ch;
4654 :
4655 78 : bdev_abort_all_queued_io(&shared_resource->nomem_io, ch);
4656 78 : bdev_abort_all_buf_io(mgmt_ch, ch);
4657 78 : }
4658 :
4659 : static void
4660 74 : bdev_channel_destroy(void *io_device, void *ctx_buf)
4661 : {
4662 74 : struct spdk_bdev_channel *ch = ctx_buf;
4663 :
4664 74 : SPDK_DEBUGLOG(bdev, "Destroying channel %p for bdev %s on thread %p\n", ch, ch->bdev->name,
4665 : spdk_get_thread());
4666 :
4667 74 : spdk_trace_record(TRACE_BDEV_IOCH_DESTROY, ch->bdev->internal.trace_id, 0, 0,
4668 : spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel)));
4669 :
4670 : /* This channel is going away, so add its statistics into the bdev so that they don't get lost. */
4671 74 : spdk_spin_lock(&ch->bdev->internal.spinlock);
4672 74 : spdk_bdev_add_io_stat(ch->bdev->internal.stat, ch->stat);
4673 74 : spdk_spin_unlock(&ch->bdev->internal.spinlock);
4674 :
4675 74 : bdev_abort_all_queued_io(&ch->queued_resets, ch);
4676 :
4677 74 : bdev_channel_abort_queued_ios(ch);
4678 :
4679 74 : if (ch->histogram) {
4680 0 : spdk_histogram_data_free(ch->histogram);
4681 0 : }
4682 :
4683 74 : bdev_channel_destroy_resource(ch);
4684 74 : }
4685 :
4686 : /*
4687 : * If the name already exists in the global bdev name tree, RB_INSERT() returns a pointer
4688 : * to it. Hence we do not have to call bdev_get_by_name() when using this function.
4689 : */
4690 : static int
4691 257 : bdev_name_add(struct spdk_bdev_name *bdev_name, struct spdk_bdev *bdev, const char *name)
4692 : {
4693 : struct spdk_bdev_name *tmp;
4694 :
4695 257 : bdev_name->name = strdup(name);
4696 257 : if (bdev_name->name == NULL) {
4697 0 : SPDK_ERRLOG("Unable to allocate bdev name\n");
4698 0 : return -ENOMEM;
4699 : }
4700 :
4701 257 : bdev_name->bdev = bdev;
4702 :
4703 257 : spdk_spin_lock(&g_bdev_mgr.spinlock);
4704 257 : tmp = RB_INSERT(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
4705 257 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
4706 :
4707 257 : if (tmp != NULL) {
4708 4 : SPDK_ERRLOG("Bdev name %s already exists\n", name);
4709 4 : free(bdev_name->name);
4710 4 : return -EEXIST;
4711 : }
4712 :
4713 253 : return 0;
4714 257 : }
4715 :
4716 : static void
4717 253 : bdev_name_del_unsafe(struct spdk_bdev_name *bdev_name)
4718 : {
4719 253 : RB_REMOVE(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
4720 253 : free(bdev_name->name);
4721 253 : }
4722 :
4723 : static void
4724 5 : bdev_name_del(struct spdk_bdev_name *bdev_name)
4725 : {
4726 5 : spdk_spin_lock(&g_bdev_mgr.spinlock);
4727 5 : bdev_name_del_unsafe(bdev_name);
4728 5 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
4729 5 : }
4730 :
4731 : int
4732 133 : spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
4733 : {
4734 : struct spdk_bdev_alias *tmp;
4735 : int ret;
4736 :
4737 133 : if (alias == NULL) {
4738 1 : SPDK_ERRLOG("Empty alias passed\n");
4739 1 : return -EINVAL;
4740 : }
4741 :
4742 132 : tmp = calloc(1, sizeof(*tmp));
4743 132 : if (tmp == NULL) {
4744 0 : SPDK_ERRLOG("Unable to allocate alias\n");
4745 0 : return -ENOMEM;
4746 : }
4747 :
4748 132 : ret = bdev_name_add(&tmp->alias, bdev, alias);
4749 132 : if (ret != 0) {
4750 4 : free(tmp);
4751 4 : return ret;
4752 : }
4753 :
4754 128 : TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq);
4755 :
4756 128 : return 0;
4757 133 : }
4758 :
4759 : static int
4760 129 : bdev_alias_del(struct spdk_bdev *bdev, const char *alias,
4761 : void (*alias_del_fn)(struct spdk_bdev_name *n))
4762 : {
4763 : struct spdk_bdev_alias *tmp;
4764 :
4765 134 : TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
4766 130 : if (strcmp(alias, tmp->alias.name) == 0) {
4767 125 : TAILQ_REMOVE(&bdev->aliases, tmp, tailq);
4768 125 : alias_del_fn(&tmp->alias);
4769 125 : free(tmp);
4770 125 : return 0;
4771 : }
4772 5 : }
4773 :
4774 4 : return -ENOENT;
4775 129 : }
4776 :
4777 : int
4778 4 : spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias)
4779 : {
4780 : int rc;
4781 :
4782 4 : rc = bdev_alias_del(bdev, alias, bdev_name_del);
4783 4 : if (rc == -ENOENT) {
4784 2 : SPDK_INFOLOG(bdev, "Alias %s does not exist\n", alias);
4785 2 : }
4786 :
4787 4 : return rc;
4788 : }
4789 :
4790 : void
4791 2 : spdk_bdev_alias_del_all(struct spdk_bdev *bdev)
4792 : {
4793 : struct spdk_bdev_alias *p, *tmp;
4794 :
4795 5 : TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) {
4796 3 : TAILQ_REMOVE(&bdev->aliases, p, tailq);
4797 3 : bdev_name_del(&p->alias);
4798 3 : free(p);
4799 3 : }
4800 2 : }
4801 :
4802 : struct spdk_io_channel *
4803 76 : spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
4804 : {
4805 76 : return spdk_get_io_channel(__bdev_to_io_dev(spdk_bdev_desc_get_bdev(desc)));
4806 : }
4807 :
4808 : void *
4809 0 : spdk_bdev_get_module_ctx(struct spdk_bdev_desc *desc)
4810 : {
4811 0 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4812 0 : void *ctx = NULL;
4813 :
4814 0 : if (bdev->fn_table->get_module_ctx) {
4815 0 : ctx = bdev->fn_table->get_module_ctx(bdev->ctxt);
4816 0 : }
4817 :
4818 0 : return ctx;
4819 : }
4820 :
4821 : const char *
4822 0 : spdk_bdev_get_module_name(const struct spdk_bdev *bdev)
4823 : {
4824 0 : return bdev->module->name;
4825 : }
4826 :
4827 : const char *
4828 253 : spdk_bdev_get_name(const struct spdk_bdev *bdev)
4829 : {
4830 253 : return bdev->name;
4831 : }
4832 :
4833 : const char *
4834 0 : spdk_bdev_get_product_name(const struct spdk_bdev *bdev)
4835 : {
4836 0 : return bdev->product_name;
4837 : }
4838 :
4839 : const struct spdk_bdev_aliases_list *
4840 0 : spdk_bdev_get_aliases(const struct spdk_bdev *bdev)
4841 : {
4842 0 : return &bdev->aliases;
4843 : }
4844 :
4845 : uint32_t
4846 5 : spdk_bdev_get_block_size(const struct spdk_bdev *bdev)
4847 : {
4848 5 : return bdev->blocklen;
4849 : }
4850 :
4851 : uint32_t
4852 0 : spdk_bdev_get_write_unit_size(const struct spdk_bdev *bdev)
4853 : {
4854 0 : return bdev->write_unit_size;
4855 : }
4856 :
4857 : uint64_t
4858 0 : spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
4859 : {
4860 0 : return bdev->blockcnt;
4861 : }
4862 :
4863 : const char *
4864 0 : spdk_bdev_get_qos_rpc_type(enum spdk_bdev_qos_rate_limit_type type)
4865 : {
4866 0 : return qos_rpc_type[type];
4867 : }
4868 :
4869 : void
4870 0 : spdk_bdev_get_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
4871 : {
4872 : int i;
4873 :
4874 0 : memset(limits, 0, sizeof(*limits) * SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
4875 :
4876 0 : spdk_spin_lock(&bdev->internal.spinlock);
4877 0 : if (bdev->internal.qos) {
4878 0 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4879 0 : if (bdev->internal.qos->rate_limits[i].limit !=
4880 : SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
4881 0 : limits[i] = bdev->internal.qos->rate_limits[i].limit;
4882 0 : if (bdev_qos_is_iops_rate_limit(i) == false) {
4883 : /* Change from Byte to Megabyte which is user visible. */
4884 0 : limits[i] = limits[i] / 1024 / 1024;
4885 0 : }
4886 0 : }
4887 0 : }
4888 0 : }
4889 0 : spdk_spin_unlock(&bdev->internal.spinlock);
4890 0 : }
4891 :
4892 : size_t
4893 317 : spdk_bdev_get_buf_align(const struct spdk_bdev *bdev)
4894 : {
4895 317 : return 1 << bdev->required_alignment;
4896 : }
4897 :
4898 : uint32_t
4899 0 : spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev)
4900 : {
4901 0 : return bdev->optimal_io_boundary;
4902 : }
4903 :
4904 : bool
4905 0 : spdk_bdev_has_write_cache(const struct spdk_bdev *bdev)
4906 : {
4907 0 : return bdev->write_cache;
4908 : }
4909 :
4910 : const struct spdk_uuid *
4911 0 : spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
4912 : {
4913 0 : return &bdev->uuid;
4914 : }
4915 :
4916 : uint16_t
4917 0 : spdk_bdev_get_acwu(const struct spdk_bdev *bdev)
4918 : {
4919 0 : return bdev->acwu;
4920 : }
4921 :
4922 : uint32_t
4923 29 : spdk_bdev_get_md_size(const struct spdk_bdev *bdev)
4924 : {
4925 29 : return bdev->md_len;
4926 : }
4927 :
4928 : bool
4929 130 : spdk_bdev_is_md_interleaved(const struct spdk_bdev *bdev)
4930 : {
4931 130 : return (bdev->md_len != 0) && bdev->md_interleave;
4932 : }
4933 :
4934 : bool
4935 159 : spdk_bdev_is_md_separate(const struct spdk_bdev *bdev)
4936 : {
4937 159 : return (bdev->md_len != 0) && !bdev->md_interleave;
4938 : }
4939 :
4940 : bool
4941 0 : spdk_bdev_is_zoned(const struct spdk_bdev *bdev)
4942 : {
4943 0 : return bdev->zoned;
4944 : }
4945 :
4946 : uint32_t
4947 121 : spdk_bdev_get_data_block_size(const struct spdk_bdev *bdev)
4948 : {
4949 121 : if (spdk_bdev_is_md_interleaved(bdev)) {
4950 0 : return bdev->blocklen - bdev->md_len;
4951 : } else {
4952 121 : return bdev->blocklen;
4953 : }
4954 121 : }
4955 :
4956 : uint32_t
4957 0 : spdk_bdev_get_physical_block_size(const struct spdk_bdev *bdev)
4958 : {
4959 0 : return bdev->phys_blocklen;
4960 : }
4961 :
4962 : static uint32_t
4963 9 : _bdev_get_block_size_with_md(const struct spdk_bdev *bdev)
4964 : {
4965 9 : if (!spdk_bdev_is_md_interleaved(bdev)) {
4966 6 : return bdev->blocklen + bdev->md_len;
4967 : } else {
4968 3 : return bdev->blocklen;
4969 : }
4970 9 : }
4971 :
4972 : /* We have to use the typedef in the function declaration to appease astyle. */
4973 : typedef enum spdk_dif_type spdk_dif_type_t;
4974 : typedef enum spdk_dif_pi_format spdk_dif_pi_format_t;
4975 :
4976 : spdk_dif_type_t
4977 0 : spdk_bdev_get_dif_type(const struct spdk_bdev *bdev)
4978 : {
4979 0 : if (bdev->md_len != 0) {
4980 0 : return bdev->dif_type;
4981 : } else {
4982 0 : return SPDK_DIF_DISABLE;
4983 : }
4984 0 : }
4985 :
4986 : spdk_dif_pi_format_t
4987 0 : spdk_bdev_get_dif_pi_format(const struct spdk_bdev *bdev)
4988 : {
4989 0 : return bdev->dif_pi_format;
4990 : }
4991 :
4992 : bool
4993 0 : spdk_bdev_is_dif_head_of_md(const struct spdk_bdev *bdev)
4994 : {
4995 0 : if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
4996 0 : return bdev->dif_is_head_of_md;
4997 : } else {
4998 0 : return false;
4999 : }
5000 0 : }
5001 :
5002 : bool
5003 0 : spdk_bdev_is_dif_check_enabled(const struct spdk_bdev *bdev,
5004 : enum spdk_dif_check_type check_type)
5005 : {
5006 0 : if (spdk_bdev_get_dif_type(bdev) == SPDK_DIF_DISABLE) {
5007 0 : return false;
5008 : }
5009 :
5010 0 : switch (check_type) {
5011 : case SPDK_DIF_CHECK_TYPE_REFTAG:
5012 0 : return (bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) != 0;
5013 : case SPDK_DIF_CHECK_TYPE_APPTAG:
5014 0 : return (bdev->dif_check_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) != 0;
5015 : case SPDK_DIF_CHECK_TYPE_GUARD:
5016 0 : return (bdev->dif_check_flags & SPDK_DIF_FLAGS_GUARD_CHECK) != 0;
5017 : default:
5018 0 : return false;
5019 : }
5020 0 : }
5021 :
5022 : static uint32_t
5023 3 : bdev_get_max_write(const struct spdk_bdev *bdev, uint64_t num_bytes)
5024 : {
5025 : uint64_t aligned_length, max_write_blocks;
5026 :
5027 3 : aligned_length = num_bytes - (spdk_bdev_get_buf_align(bdev) - 1);
5028 3 : max_write_blocks = aligned_length / _bdev_get_block_size_with_md(bdev);
5029 3 : max_write_blocks -= max_write_blocks % bdev->write_unit_size;
5030 :
5031 3 : return max_write_blocks;
5032 : }
5033 :
5034 : uint32_t
5035 1 : spdk_bdev_get_max_copy(const struct spdk_bdev *bdev)
5036 : {
5037 1 : return bdev->max_copy;
5038 : }
5039 :
5040 : uint64_t
5041 0 : spdk_bdev_get_qd(const struct spdk_bdev *bdev)
5042 : {
5043 0 : return bdev->internal.measured_queue_depth;
5044 : }
5045 :
5046 : uint64_t
5047 0 : spdk_bdev_get_qd_sampling_period(const struct spdk_bdev *bdev)
5048 : {
5049 0 : return bdev->internal.period;
5050 : }
5051 :
5052 : uint64_t
5053 0 : spdk_bdev_get_weighted_io_time(const struct spdk_bdev *bdev)
5054 : {
5055 0 : return bdev->internal.weighted_io_time;
5056 : }
5057 :
5058 : uint64_t
5059 0 : spdk_bdev_get_io_time(const struct spdk_bdev *bdev)
5060 : {
5061 0 : return bdev->internal.io_time;
5062 : }
5063 :
5064 0 : union spdk_bdev_nvme_ctratt spdk_bdev_get_nvme_ctratt(struct spdk_bdev *bdev)
5065 : {
5066 0 : return bdev->ctratt;
5067 : }
5068 :
5069 : uint32_t
5070 0 : spdk_bdev_get_nvme_nsid(struct spdk_bdev *bdev)
5071 : {
5072 0 : return bdev->nsid;
5073 : }
5074 :
5075 : uint32_t
5076 0 : spdk_bdev_desc_get_block_size(struct spdk_bdev_desc *desc)
5077 : {
5078 0 : struct spdk_bdev *bdev = desc->bdev;
5079 :
5080 0 : return desc->opts.hide_metadata ? bdev->blocklen - bdev->md_len : bdev->blocklen;
5081 : }
5082 :
5083 : uint32_t
5084 0 : spdk_bdev_desc_get_md_size(struct spdk_bdev_desc *desc)
5085 : {
5086 0 : struct spdk_bdev *bdev = desc->bdev;
5087 :
5088 0 : return desc->opts.hide_metadata ? 0 : bdev->md_len;
5089 : }
5090 :
5091 : bool
5092 0 : spdk_bdev_desc_is_md_interleaved(struct spdk_bdev_desc *desc)
5093 : {
5094 0 : struct spdk_bdev *bdev = desc->bdev;
5095 :
5096 0 : return desc->opts.hide_metadata ? false : spdk_bdev_is_md_interleaved(bdev);
5097 : }
5098 :
5099 : bool
5100 0 : spdk_bdev_desc_is_md_separate(struct spdk_bdev_desc *desc)
5101 : {
5102 0 : struct spdk_bdev *bdev = desc->bdev;
5103 :
5104 0 : return desc->opts.hide_metadata ? false : spdk_bdev_is_md_separate(bdev);
5105 : }
5106 :
5107 : spdk_dif_type_t
5108 0 : spdk_bdev_desc_get_dif_type(struct spdk_bdev_desc *desc)
5109 : {
5110 0 : struct spdk_bdev *bdev = desc->bdev;
5111 :
5112 0 : return desc->opts.hide_metadata ? SPDK_DIF_DISABLE : spdk_bdev_get_dif_type(bdev);
5113 : }
5114 :
5115 : spdk_dif_pi_format_t
5116 0 : spdk_bdev_desc_get_dif_pi_format(struct spdk_bdev_desc *desc)
5117 : {
5118 0 : struct spdk_bdev *bdev = desc->bdev;
5119 :
5120 0 : return desc->opts.hide_metadata ? SPDK_DIF_PI_FORMAT_16 : spdk_bdev_get_dif_pi_format(bdev);
5121 : }
5122 :
5123 : bool
5124 0 : spdk_bdev_desc_is_dif_head_of_md(struct spdk_bdev_desc *desc)
5125 : {
5126 0 : struct spdk_bdev *bdev = desc->bdev;
5127 :
5128 0 : return desc->opts.hide_metadata ? false : spdk_bdev_is_dif_head_of_md(bdev);
5129 : }
5130 :
5131 : bool
5132 0 : spdk_bdev_desc_is_dif_check_enabled(struct spdk_bdev_desc *desc,
5133 : enum spdk_dif_check_type check_type)
5134 : {
5135 0 : struct spdk_bdev *bdev = desc->bdev;
5136 :
5137 0 : return desc->opts.hide_metadata ? false : spdk_bdev_is_dif_check_enabled(bdev, check_type);
5138 : }
5139 :
5140 : static void bdev_update_qd_sampling_period(void *ctx);
5141 :
5142 : static void
5143 1 : _calculate_measured_qd_cpl(struct spdk_bdev *bdev, void *_ctx, int status)
5144 : {
5145 1 : bdev->internal.measured_queue_depth = bdev->internal.temporary_queue_depth;
5146 :
5147 1 : if (bdev->internal.measured_queue_depth) {
5148 0 : bdev->internal.io_time += bdev->internal.period;
5149 0 : bdev->internal.weighted_io_time += bdev->internal.period * bdev->internal.measured_queue_depth;
5150 0 : }
5151 :
5152 1 : bdev->internal.qd_poll_in_progress = false;
5153 :
5154 1 : bdev_update_qd_sampling_period(bdev);
5155 1 : }
5156 :
5157 : static void
5158 1 : _calculate_measured_qd(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
5159 : struct spdk_io_channel *io_ch, void *_ctx)
5160 : {
5161 1 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(io_ch);
5162 :
5163 1 : bdev->internal.temporary_queue_depth += ch->io_outstanding;
5164 1 : spdk_bdev_for_each_channel_continue(i, 0);
5165 1 : }
5166 :
5167 : static int
5168 1 : bdev_calculate_measured_queue_depth(void *ctx)
5169 : {
5170 1 : struct spdk_bdev *bdev = ctx;
5171 :
5172 1 : bdev->internal.qd_poll_in_progress = true;
5173 1 : bdev->internal.temporary_queue_depth = 0;
5174 1 : spdk_bdev_for_each_channel(bdev, _calculate_measured_qd, bdev, _calculate_measured_qd_cpl);
5175 1 : return SPDK_POLLER_BUSY;
5176 : }
5177 :
5178 : static void
5179 5 : bdev_update_qd_sampling_period(void *ctx)
5180 : {
5181 5 : struct spdk_bdev *bdev = ctx;
5182 :
5183 5 : if (bdev->internal.period == bdev->internal.new_period) {
5184 0 : return;
5185 : }
5186 :
5187 5 : if (bdev->internal.qd_poll_in_progress) {
5188 1 : return;
5189 : }
5190 :
5191 4 : bdev->internal.period = bdev->internal.new_period;
5192 :
5193 4 : spdk_poller_unregister(&bdev->internal.qd_poller);
5194 4 : if (bdev->internal.period != 0) {
5195 2 : bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth,
5196 : bdev, bdev->internal.period);
5197 2 : } else {
5198 2 : spdk_bdev_close(bdev->internal.qd_desc);
5199 2 : bdev->internal.qd_desc = NULL;
5200 : }
5201 5 : }
5202 :
5203 : static void
5204 0 : _tmp_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
5205 : {
5206 0 : SPDK_NOTICELOG("Unexpected event type: %d\n", type);
5207 0 : }
5208 :
5209 : void
5210 130 : spdk_bdev_set_qd_sampling_period(struct spdk_bdev *bdev, uint64_t period)
5211 : {
5212 : int rc;
5213 :
5214 130 : if (bdev->internal.new_period == period) {
5215 124 : return;
5216 : }
5217 :
5218 6 : bdev->internal.new_period = period;
5219 :
5220 6 : if (bdev->internal.qd_desc != NULL) {
5221 4 : assert(bdev->internal.period != 0);
5222 :
5223 8 : spdk_thread_send_msg(bdev->internal.qd_desc->thread,
5224 4 : bdev_update_qd_sampling_period, bdev);
5225 4 : return;
5226 : }
5227 :
5228 2 : assert(bdev->internal.period == 0);
5229 :
5230 4 : rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, _tmp_bdev_event_cb,
5231 2 : NULL, &bdev->internal.qd_desc);
5232 2 : if (rc != 0) {
5233 0 : return;
5234 : }
5235 :
5236 2 : bdev->internal.period = period;
5237 2 : bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth,
5238 : bdev, period);
5239 130 : }
5240 :
5241 : struct bdev_get_current_qd_ctx {
5242 : uint64_t current_qd;
5243 : spdk_bdev_get_current_qd_cb cb_fn;
5244 : void *cb_arg;
5245 : };
5246 :
5247 : static void
5248 0 : bdev_get_current_qd_done(struct spdk_bdev *bdev, void *_ctx, int status)
5249 : {
5250 0 : struct bdev_get_current_qd_ctx *ctx = _ctx;
5251 :
5252 0 : ctx->cb_fn(bdev, ctx->current_qd, ctx->cb_arg, 0);
5253 :
5254 0 : free(ctx);
5255 0 : }
5256 :
5257 : static void
5258 0 : bdev_get_current_qd(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
5259 : struct spdk_io_channel *io_ch, void *_ctx)
5260 : {
5261 0 : struct bdev_get_current_qd_ctx *ctx = _ctx;
5262 0 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
5263 :
5264 0 : ctx->current_qd += bdev_ch->io_outstanding;
5265 :
5266 0 : spdk_bdev_for_each_channel_continue(i, 0);
5267 0 : }
5268 :
5269 : void
5270 0 : spdk_bdev_get_current_qd(struct spdk_bdev *bdev, spdk_bdev_get_current_qd_cb cb_fn,
5271 : void *cb_arg)
5272 : {
5273 : struct bdev_get_current_qd_ctx *ctx;
5274 :
5275 0 : assert(cb_fn != NULL);
5276 :
5277 0 : ctx = calloc(1, sizeof(*ctx));
5278 0 : if (ctx == NULL) {
5279 0 : cb_fn(bdev, 0, cb_arg, -ENOMEM);
5280 0 : return;
5281 : }
5282 :
5283 0 : ctx->cb_fn = cb_fn;
5284 0 : ctx->cb_arg = cb_arg;
5285 :
5286 0 : spdk_bdev_for_each_channel(bdev, bdev_get_current_qd, ctx, bdev_get_current_qd_done);
5287 0 : }
5288 :
5289 : static void
5290 25 : _event_notify(struct spdk_bdev_desc *desc, enum spdk_bdev_event_type type)
5291 : {
5292 25 : assert(desc->thread == spdk_get_thread());
5293 :
5294 25 : spdk_spin_lock(&desc->spinlock);
5295 25 : desc->refs--;
5296 25 : if (!desc->closed) {
5297 14 : spdk_spin_unlock(&desc->spinlock);
5298 28 : desc->callback.event_fn(type,
5299 14 : desc->bdev,
5300 14 : desc->callback.ctx);
5301 14 : return;
5302 11 : } else if (desc->refs == 0) {
5303 : /* This descriptor was closed after this event_notify message was sent.
5304 : * spdk_bdev_close() could not free the descriptor since this message was
5305 : * in flight, so we free it now using bdev_desc_free().
5306 : */
5307 10 : spdk_spin_unlock(&desc->spinlock);
5308 10 : bdev_desc_free(desc);
5309 10 : return;
5310 : }
5311 1 : spdk_spin_unlock(&desc->spinlock);
5312 25 : }
5313 :
5314 : static void
5315 25 : event_notify(struct spdk_bdev_desc *desc, spdk_msg_fn event_notify_fn)
5316 : {
5317 25 : spdk_spin_lock(&desc->spinlock);
5318 25 : desc->refs++;
5319 25 : spdk_thread_send_msg(desc->thread, event_notify_fn, desc);
5320 25 : spdk_spin_unlock(&desc->spinlock);
5321 25 : }
5322 :
5323 : static void
5324 6 : _resize_notify(void *ctx)
5325 : {
5326 6 : struct spdk_bdev_desc *desc = ctx;
5327 :
5328 6 : _event_notify(desc, SPDK_BDEV_EVENT_RESIZE);
5329 6 : }
5330 :
5331 : int
5332 11 : spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
5333 : {
5334 : struct spdk_bdev_desc *desc;
5335 : int ret;
5336 :
5337 11 : if (size == bdev->blockcnt) {
5338 0 : return 0;
5339 : }
5340 :
5341 11 : spdk_spin_lock(&bdev->internal.spinlock);
5342 :
5343 : /* bdev has open descriptors */
5344 11 : if (!TAILQ_EMPTY(&bdev->internal.open_descs) &&
5345 7 : bdev->blockcnt > size) {
5346 1 : ret = -EBUSY;
5347 1 : } else {
5348 10 : bdev->blockcnt = size;
5349 16 : TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
5350 6 : event_notify(desc, _resize_notify);
5351 6 : }
5352 10 : ret = 0;
5353 : }
5354 :
5355 11 : spdk_spin_unlock(&bdev->internal.spinlock);
5356 :
5357 11 : return ret;
5358 11 : }
5359 :
5360 : /*
5361 : * Convert I/O offset and length from bytes to blocks.
5362 : *
5363 : * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size.
5364 : */
5365 : static uint64_t
5366 20 : bdev_bytes_to_blocks(struct spdk_bdev_desc *desc, uint64_t offset_bytes,
5367 : uint64_t *offset_blocks, uint64_t num_bytes, uint64_t *num_blocks)
5368 : {
5369 20 : uint32_t block_size = bdev_desc_get_block_size(desc);
5370 : uint8_t shift_cnt;
5371 :
5372 : /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */
5373 20 : if (spdk_likely(spdk_u32_is_pow2(block_size))) {
5374 17 : shift_cnt = spdk_u32log2(block_size);
5375 17 : *offset_blocks = offset_bytes >> shift_cnt;
5376 17 : *num_blocks = num_bytes >> shift_cnt;
5377 34 : return (offset_bytes - (*offset_blocks << shift_cnt)) |
5378 17 : (num_bytes - (*num_blocks << shift_cnt));
5379 : } else {
5380 3 : *offset_blocks = offset_bytes / block_size;
5381 3 : *num_blocks = num_bytes / block_size;
5382 3 : return (offset_bytes % block_size) | (num_bytes % block_size);
5383 : }
5384 20 : }
5385 :
5386 : static bool
5387 689 : bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks)
5388 : {
5389 : /* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there
5390 : * has been an overflow and hence the offset has been wrapped around */
5391 689 : if (offset_blocks + num_blocks < offset_blocks) {
5392 1 : return false;
5393 : }
5394 :
5395 : /* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */
5396 688 : if (offset_blocks + num_blocks > bdev->blockcnt) {
5397 2 : return false;
5398 : }
5399 :
5400 686 : return true;
5401 689 : }
5402 :
5403 : static void
5404 2 : bdev_seek_complete_cb(void *ctx)
5405 : {
5406 2 : struct spdk_bdev_io *bdev_io = ctx;
5407 :
5408 2 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
5409 2 : bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx);
5410 2 : }
5411 :
5412 : static int
5413 4 : bdev_seek(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5414 : uint64_t offset_blocks, enum spdk_bdev_io_type io_type,
5415 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5416 : {
5417 4 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5418 : struct spdk_bdev_io *bdev_io;
5419 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5420 :
5421 4 : assert(io_type == SPDK_BDEV_IO_TYPE_SEEK_DATA || io_type == SPDK_BDEV_IO_TYPE_SEEK_HOLE);
5422 :
5423 : /* Check if offset_blocks is valid looking at the validity of one block */
5424 4 : if (!bdev_io_valid_blocks(bdev, offset_blocks, 1)) {
5425 0 : return -EINVAL;
5426 : }
5427 :
5428 4 : bdev_io = bdev_channel_get_io(channel);
5429 4 : if (!bdev_io) {
5430 0 : return -ENOMEM;
5431 : }
5432 :
5433 4 : bdev_io->internal.ch = channel;
5434 4 : bdev_io->internal.desc = desc;
5435 4 : bdev_io->type = io_type;
5436 4 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5437 4 : bdev_io->u.bdev.memory_domain = NULL;
5438 4 : bdev_io->u.bdev.memory_domain_ctx = NULL;
5439 4 : bdev_io->u.bdev.accel_sequence = NULL;
5440 4 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5441 :
5442 4 : if (!spdk_bdev_io_type_supported(bdev, io_type)) {
5443 : /* In case bdev doesn't support seek to next data/hole offset,
5444 : * it is assumed that only data and no holes are present */
5445 2 : if (io_type == SPDK_BDEV_IO_TYPE_SEEK_DATA) {
5446 1 : bdev_io->u.bdev.seek.offset = offset_blocks;
5447 1 : } else {
5448 1 : bdev_io->u.bdev.seek.offset = UINT64_MAX;
5449 : }
5450 :
5451 2 : spdk_thread_send_msg(spdk_get_thread(), bdev_seek_complete_cb, bdev_io);
5452 2 : return 0;
5453 : }
5454 :
5455 2 : bdev_io_submit(bdev_io);
5456 2 : return 0;
5457 4 : }
5458 :
5459 : int
5460 2 : spdk_bdev_seek_data(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5461 : uint64_t offset_blocks,
5462 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5463 : {
5464 2 : return bdev_seek(desc, ch, offset_blocks, SPDK_BDEV_IO_TYPE_SEEK_DATA, cb, cb_arg);
5465 : }
5466 :
5467 : int
5468 2 : spdk_bdev_seek_hole(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5469 : uint64_t offset_blocks,
5470 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5471 : {
5472 2 : return bdev_seek(desc, ch, offset_blocks, SPDK_BDEV_IO_TYPE_SEEK_HOLE, cb, cb_arg);
5473 : }
5474 :
5475 : uint64_t
5476 4 : spdk_bdev_io_get_seek_offset(const struct spdk_bdev_io *bdev_io)
5477 : {
5478 4 : return bdev_io->u.bdev.seek.offset;
5479 : }
5480 :
5481 : static int
5482 204 : bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf,
5483 : void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5484 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5485 : {
5486 204 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5487 : struct spdk_bdev_io *bdev_io;
5488 204 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5489 :
5490 204 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5491 0 : return -EINVAL;
5492 : }
5493 :
5494 204 : bdev_io = bdev_channel_get_io(channel);
5495 204 : if (!bdev_io) {
5496 1 : return -ENOMEM;
5497 : }
5498 :
5499 203 : bdev_io->internal.ch = channel;
5500 203 : bdev_io->internal.desc = desc;
5501 203 : bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
5502 203 : bdev_io->u.bdev.iovs = &bdev_io->iov;
5503 203 : bdev_io->u.bdev.iovs[0].iov_base = buf;
5504 203 : bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev_desc_get_block_size(desc);
5505 203 : bdev_io->u.bdev.iovcnt = 1;
5506 203 : bdev_io->u.bdev.md_buf = md_buf;
5507 203 : bdev_io->u.bdev.num_blocks = num_blocks;
5508 203 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5509 203 : bdev_io->u.bdev.memory_domain = NULL;
5510 203 : bdev_io->u.bdev.memory_domain_ctx = NULL;
5511 203 : bdev_io->u.bdev.accel_sequence = NULL;
5512 203 : bdev_io->u.bdev.dif_check_flags = bdev->dif_check_flags;
5513 203 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5514 :
5515 203 : bdev_io_submit(bdev_io);
5516 203 : return 0;
5517 204 : }
5518 :
5519 : int
5520 3 : spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5521 : void *buf, uint64_t offset, uint64_t nbytes,
5522 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5523 : {
5524 : uint64_t offset_blocks, num_blocks;
5525 :
5526 3 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
5527 0 : return -EINVAL;
5528 : }
5529 :
5530 3 : return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
5531 3 : }
5532 :
5533 : int
5534 200 : spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5535 : void *buf, uint64_t offset_blocks, uint64_t num_blocks,
5536 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5537 : {
5538 200 : return bdev_read_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, cb, cb_arg);
5539 : }
5540 :
5541 : int
5542 4 : spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5543 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5544 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5545 : {
5546 8 : struct iovec iov = {
5547 4 : .iov_base = buf,
5548 : };
5549 :
5550 4 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
5551 0 : return -EINVAL;
5552 : }
5553 :
5554 4 : if (md_buf && !_is_buf_allocated(&iov)) {
5555 0 : return -EINVAL;
5556 : }
5557 :
5558 8 : return bdev_read_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
5559 4 : cb, cb_arg);
5560 4 : }
5561 :
5562 : int
5563 5 : spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5564 : struct iovec *iov, int iovcnt,
5565 : uint64_t offset, uint64_t nbytes,
5566 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5567 : {
5568 : uint64_t offset_blocks, num_blocks;
5569 :
5570 5 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
5571 0 : return -EINVAL;
5572 : }
5573 :
5574 5 : return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
5575 5 : }
5576 :
5577 : static int
5578 226 : bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5579 : struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
5580 : uint64_t num_blocks, struct spdk_memory_domain *domain, void *domain_ctx,
5581 : struct spdk_accel_sequence *seq, uint32_t dif_check_flags,
5582 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5583 : {
5584 226 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5585 : struct spdk_bdev_io *bdev_io;
5586 226 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5587 :
5588 226 : if (spdk_unlikely(!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks))) {
5589 0 : return -EINVAL;
5590 : }
5591 :
5592 226 : bdev_io = bdev_channel_get_io(channel);
5593 226 : if (spdk_unlikely(!bdev_io)) {
5594 2 : return -ENOMEM;
5595 : }
5596 :
5597 224 : bdev_io->internal.ch = channel;
5598 224 : bdev_io->internal.desc = desc;
5599 224 : bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
5600 224 : bdev_io->u.bdev.iovs = iov;
5601 224 : bdev_io->u.bdev.iovcnt = iovcnt;
5602 224 : bdev_io->u.bdev.md_buf = md_buf;
5603 224 : bdev_io->u.bdev.num_blocks = num_blocks;
5604 224 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5605 224 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5606 :
5607 224 : if (seq != NULL) {
5608 0 : bdev_io->internal.f.has_accel_sequence = true;
5609 0 : bdev_io->internal.accel_sequence = seq;
5610 0 : }
5611 :
5612 224 : if (domain != NULL) {
5613 2 : bdev_io->internal.f.has_memory_domain = true;
5614 2 : bdev_io->internal.memory_domain = domain;
5615 2 : bdev_io->internal.memory_domain_ctx = domain_ctx;
5616 2 : }
5617 :
5618 224 : bdev_io->u.bdev.memory_domain = domain;
5619 224 : bdev_io->u.bdev.memory_domain_ctx = domain_ctx;
5620 224 : bdev_io->u.bdev.accel_sequence = seq;
5621 224 : bdev_io->u.bdev.dif_check_flags = dif_check_flags;
5622 :
5623 224 : _bdev_io_submit_ext(desc, bdev_io);
5624 :
5625 224 : return 0;
5626 226 : }
5627 :
5628 : int
5629 21 : spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5630 : struct iovec *iov, int iovcnt,
5631 : uint64_t offset_blocks, uint64_t num_blocks,
5632 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5633 : {
5634 21 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5635 :
5636 42 : return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
5637 21 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, cb, cb_arg);
5638 : }
5639 :
5640 : int
5641 4 : spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5642 : struct iovec *iov, int iovcnt, void *md_buf,
5643 : uint64_t offset_blocks, uint64_t num_blocks,
5644 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5645 : {
5646 4 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5647 :
5648 4 : if (md_buf && !spdk_bdev_is_md_separate(bdev)) {
5649 0 : return -EINVAL;
5650 : }
5651 :
5652 4 : if (md_buf && !_is_buf_allocated(iov)) {
5653 0 : return -EINVAL;
5654 : }
5655 :
5656 8 : return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
5657 4 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, cb, cb_arg);
5658 4 : }
5659 :
5660 : static inline bool
5661 14 : _bdev_io_check_opts(struct spdk_bdev_ext_io_opts *opts, struct iovec *iov)
5662 : {
5663 : /*
5664 : * We check if opts size is at least of size when we first introduced
5665 : * spdk_bdev_ext_io_opts (ac6f2bdd8d) since access to those members
5666 : * are not checked internal.
5667 : */
5668 24 : return opts->size >= offsetof(struct spdk_bdev_ext_io_opts, metadata) +
5669 14 : sizeof(opts->metadata) &&
5670 10 : opts->size <= sizeof(*opts) &&
5671 : /* When memory domain is used, the user must provide data buffers */
5672 8 : (!opts->memory_domain || (iov && iov[0].iov_base));
5673 : }
5674 :
5675 : int
5676 8 : spdk_bdev_readv_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5677 : struct iovec *iov, int iovcnt,
5678 : uint64_t offset_blocks, uint64_t num_blocks,
5679 : spdk_bdev_io_completion_cb cb, void *cb_arg,
5680 : struct spdk_bdev_ext_io_opts *opts)
5681 : {
5682 8 : struct spdk_memory_domain *domain = NULL;
5683 8 : struct spdk_accel_sequence *seq = NULL;
5684 8 : void *domain_ctx = NULL, *md = NULL;
5685 8 : uint32_t dif_check_flags = 0;
5686 8 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5687 :
5688 8 : if (opts) {
5689 7 : if (spdk_unlikely(!_bdev_io_check_opts(opts, iov))) {
5690 3 : return -EINVAL;
5691 : }
5692 :
5693 4 : md = opts->metadata;
5694 4 : domain = bdev_get_ext_io_opt(opts, memory_domain, NULL);
5695 4 : domain_ctx = bdev_get_ext_io_opt(opts, memory_domain_ctx, NULL);
5696 4 : seq = bdev_get_ext_io_opt(opts, accel_sequence, NULL);
5697 4 : if (md) {
5698 4 : if (spdk_unlikely(!spdk_bdev_is_md_separate(bdev))) {
5699 0 : return -EINVAL;
5700 : }
5701 :
5702 4 : if (spdk_unlikely(!_is_buf_allocated(iov))) {
5703 0 : return -EINVAL;
5704 : }
5705 :
5706 4 : if (spdk_unlikely(seq != NULL)) {
5707 0 : return -EINVAL;
5708 : }
5709 4 : }
5710 4 : }
5711 :
5712 10 : dif_check_flags = bdev->dif_check_flags &
5713 5 : ~(bdev_get_ext_io_opt(opts, dif_check_flags_exclude_mask, 0));
5714 :
5715 10 : return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks,
5716 5 : num_blocks, domain, domain_ctx, seq, dif_check_flags, cb, cb_arg);
5717 8 : }
5718 :
5719 : static int
5720 36 : bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5721 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5722 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5723 : {
5724 36 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5725 : struct spdk_bdev_io *bdev_io;
5726 36 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5727 :
5728 36 : if (!desc->write) {
5729 0 : return -EBADF;
5730 : }
5731 :
5732 36 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5733 0 : return -EINVAL;
5734 : }
5735 :
5736 36 : bdev_io = bdev_channel_get_io(channel);
5737 36 : if (!bdev_io) {
5738 0 : return -ENOMEM;
5739 : }
5740 :
5741 36 : bdev_io->internal.ch = channel;
5742 36 : bdev_io->internal.desc = desc;
5743 36 : bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
5744 36 : bdev_io->u.bdev.iovs = &bdev_io->iov;
5745 36 : bdev_io->u.bdev.iovs[0].iov_base = buf;
5746 36 : bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev_desc_get_block_size(desc);
5747 36 : bdev_io->u.bdev.iovcnt = 1;
5748 36 : bdev_io->u.bdev.md_buf = md_buf;
5749 36 : bdev_io->u.bdev.num_blocks = num_blocks;
5750 36 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5751 36 : bdev_io->u.bdev.memory_domain = NULL;
5752 36 : bdev_io->u.bdev.memory_domain_ctx = NULL;
5753 36 : bdev_io->u.bdev.accel_sequence = NULL;
5754 36 : bdev_io->u.bdev.dif_check_flags = bdev->dif_check_flags;
5755 36 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5756 :
5757 36 : bdev_io_submit(bdev_io);
5758 36 : return 0;
5759 36 : }
5760 :
5761 : int
5762 3 : spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5763 : void *buf, uint64_t offset, uint64_t nbytes,
5764 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5765 : {
5766 : uint64_t offset_blocks, num_blocks;
5767 :
5768 3 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
5769 0 : return -EINVAL;
5770 : }
5771 :
5772 3 : return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
5773 3 : }
5774 :
5775 : int
5776 27 : spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5777 : void *buf, uint64_t offset_blocks, uint64_t num_blocks,
5778 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5779 : {
5780 54 : return bdev_write_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
5781 27 : cb, cb_arg);
5782 : }
5783 :
5784 : int
5785 3 : spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5786 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5787 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5788 : {
5789 6 : struct iovec iov = {
5790 3 : .iov_base = buf,
5791 : };
5792 :
5793 3 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
5794 0 : return -EINVAL;
5795 : }
5796 :
5797 3 : if (md_buf && !_is_buf_allocated(&iov)) {
5798 0 : return -EINVAL;
5799 : }
5800 :
5801 6 : return bdev_write_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
5802 3 : cb, cb_arg);
5803 3 : }
5804 :
5805 : static int
5806 70 : bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5807 : struct iovec *iov, int iovcnt, void *md_buf,
5808 : uint64_t offset_blocks, uint64_t num_blocks,
5809 : struct spdk_memory_domain *domain, void *domain_ctx,
5810 : struct spdk_accel_sequence *seq, uint32_t dif_check_flags,
5811 : uint32_t nvme_cdw12_raw, uint32_t nvme_cdw13_raw,
5812 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5813 : {
5814 70 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5815 : struct spdk_bdev_io *bdev_io;
5816 70 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5817 :
5818 70 : if (spdk_unlikely(!desc->write)) {
5819 0 : return -EBADF;
5820 : }
5821 :
5822 70 : if (spdk_unlikely(!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks))) {
5823 0 : return -EINVAL;
5824 : }
5825 :
5826 70 : bdev_io = bdev_channel_get_io(channel);
5827 70 : if (spdk_unlikely(!bdev_io)) {
5828 2 : return -ENOMEM;
5829 : }
5830 :
5831 68 : bdev_io->internal.ch = channel;
5832 68 : bdev_io->internal.desc = desc;
5833 68 : bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
5834 68 : bdev_io->u.bdev.iovs = iov;
5835 68 : bdev_io->u.bdev.iovcnt = iovcnt;
5836 68 : bdev_io->u.bdev.md_buf = md_buf;
5837 68 : bdev_io->u.bdev.num_blocks = num_blocks;
5838 68 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5839 68 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5840 68 : if (seq != NULL) {
5841 0 : bdev_io->internal.f.has_accel_sequence = true;
5842 0 : bdev_io->internal.accel_sequence = seq;
5843 0 : }
5844 :
5845 68 : if (domain != NULL) {
5846 2 : bdev_io->internal.f.has_memory_domain = true;
5847 2 : bdev_io->internal.memory_domain = domain;
5848 2 : bdev_io->internal.memory_domain_ctx = domain_ctx;
5849 2 : }
5850 :
5851 68 : bdev_io->u.bdev.memory_domain = domain;
5852 68 : bdev_io->u.bdev.memory_domain_ctx = domain_ctx;
5853 68 : bdev_io->u.bdev.accel_sequence = seq;
5854 68 : bdev_io->u.bdev.dif_check_flags = dif_check_flags;
5855 68 : bdev_io->u.bdev.nvme_cdw12.raw = nvme_cdw12_raw;
5856 68 : bdev_io->u.bdev.nvme_cdw13.raw = nvme_cdw13_raw;
5857 :
5858 68 : _bdev_io_submit_ext(desc, bdev_io);
5859 :
5860 68 : return 0;
5861 70 : }
5862 :
5863 : int
5864 3 : spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5865 : struct iovec *iov, int iovcnt,
5866 : uint64_t offset, uint64_t len,
5867 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5868 : {
5869 : uint64_t offset_blocks, num_blocks;
5870 :
5871 3 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, len, &num_blocks) != 0) {
5872 0 : return -EINVAL;
5873 : }
5874 :
5875 3 : return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
5876 3 : }
5877 :
5878 : int
5879 14 : spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5880 : struct iovec *iov, int iovcnt,
5881 : uint64_t offset_blocks, uint64_t num_blocks,
5882 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5883 : {
5884 14 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5885 :
5886 28 : return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
5887 14 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, 0, 0,
5888 14 : cb, cb_arg);
5889 : }
5890 :
5891 : int
5892 1 : spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5893 : struct iovec *iov, int iovcnt, void *md_buf,
5894 : uint64_t offset_blocks, uint64_t num_blocks,
5895 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5896 : {
5897 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5898 :
5899 1 : if (md_buf && !spdk_bdev_is_md_separate(bdev)) {
5900 0 : return -EINVAL;
5901 : }
5902 :
5903 1 : if (md_buf && !_is_buf_allocated(iov)) {
5904 0 : return -EINVAL;
5905 : }
5906 :
5907 2 : return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
5908 1 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, 0, 0,
5909 1 : cb, cb_arg);
5910 1 : }
5911 :
5912 : int
5913 8 : spdk_bdev_writev_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5914 : struct iovec *iov, int iovcnt,
5915 : uint64_t offset_blocks, uint64_t num_blocks,
5916 : spdk_bdev_io_completion_cb cb, void *cb_arg,
5917 : struct spdk_bdev_ext_io_opts *opts)
5918 : {
5919 8 : struct spdk_memory_domain *domain = NULL;
5920 8 : struct spdk_accel_sequence *seq = NULL;
5921 8 : void *domain_ctx = NULL, *md = NULL;
5922 8 : uint32_t dif_check_flags = 0;
5923 8 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5924 8 : uint32_t nvme_cdw12_raw = 0;
5925 8 : uint32_t nvme_cdw13_raw = 0;
5926 :
5927 8 : if (opts) {
5928 7 : if (spdk_unlikely(!_bdev_io_check_opts(opts, iov))) {
5929 3 : return -EINVAL;
5930 : }
5931 4 : md = opts->metadata;
5932 4 : domain = bdev_get_ext_io_opt(opts, memory_domain, NULL);
5933 4 : domain_ctx = bdev_get_ext_io_opt(opts, memory_domain_ctx, NULL);
5934 4 : seq = bdev_get_ext_io_opt(opts, accel_sequence, NULL);
5935 4 : nvme_cdw12_raw = bdev_get_ext_io_opt(opts, nvme_cdw12.raw, 0);
5936 4 : nvme_cdw13_raw = bdev_get_ext_io_opt(opts, nvme_cdw13.raw, 0);
5937 4 : if (md) {
5938 4 : if (spdk_unlikely(!spdk_bdev_is_md_separate(bdev))) {
5939 0 : return -EINVAL;
5940 : }
5941 :
5942 4 : if (spdk_unlikely(!_is_buf_allocated(iov))) {
5943 0 : return -EINVAL;
5944 : }
5945 :
5946 4 : if (spdk_unlikely(seq != NULL)) {
5947 0 : return -EINVAL;
5948 : }
5949 4 : }
5950 4 : }
5951 :
5952 10 : dif_check_flags = bdev->dif_check_flags &
5953 5 : ~(bdev_get_ext_io_opt(opts, dif_check_flags_exclude_mask, 0));
5954 :
5955 10 : return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks, num_blocks,
5956 5 : domain, domain_ctx, seq, dif_check_flags,
5957 5 : nvme_cdw12_raw, nvme_cdw13_raw, cb, cb_arg);
5958 8 : }
5959 :
5960 : static void
5961 11 : bdev_compare_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
5962 : {
5963 11 : struct spdk_bdev_io *parent_io = cb_arg;
5964 11 : struct spdk_bdev *bdev = parent_io->bdev;
5965 11 : uint8_t *read_buf = bdev_io->u.bdev.iovs[0].iov_base;
5966 11 : int i, rc = 0;
5967 :
5968 11 : if (!success) {
5969 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
5970 0 : parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
5971 0 : spdk_bdev_free_io(bdev_io);
5972 0 : return;
5973 : }
5974 :
5975 17 : for (i = 0; i < parent_io->u.bdev.iovcnt; i++) {
5976 22 : rc = memcmp(read_buf,
5977 11 : parent_io->u.bdev.iovs[i].iov_base,
5978 11 : parent_io->u.bdev.iovs[i].iov_len);
5979 11 : if (rc) {
5980 5 : break;
5981 : }
5982 6 : read_buf += parent_io->u.bdev.iovs[i].iov_len;
5983 6 : }
5984 :
5985 11 : if (rc == 0 && parent_io->u.bdev.md_buf && spdk_bdev_is_md_separate(bdev)) {
5986 4 : rc = memcmp(bdev_io->u.bdev.md_buf,
5987 2 : parent_io->u.bdev.md_buf,
5988 2 : spdk_bdev_get_md_size(bdev));
5989 2 : }
5990 :
5991 11 : spdk_bdev_free_io(bdev_io);
5992 :
5993 11 : if (rc == 0) {
5994 5 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
5995 5 : parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx);
5996 5 : } else {
5997 6 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_MISCOMPARE;
5998 6 : parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
5999 : }
6000 11 : }
6001 :
6002 : static void
6003 11 : bdev_compare_do_read(void *_bdev_io)
6004 : {
6005 11 : struct spdk_bdev_io *bdev_io = _bdev_io;
6006 : int rc;
6007 :
6008 22 : rc = spdk_bdev_read_blocks(bdev_io->internal.desc,
6009 11 : spdk_io_channel_from_ctx(bdev_io->internal.ch), NULL,
6010 11 : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6011 11 : bdev_compare_do_read_done, bdev_io);
6012 :
6013 11 : if (rc == -ENOMEM) {
6014 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_do_read);
6015 11 : } else if (rc != 0) {
6016 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
6017 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
6018 0 : }
6019 11 : }
6020 :
6021 : static int
6022 16 : bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6023 : struct iovec *iov, int iovcnt, void *md_buf,
6024 : uint64_t offset_blocks, uint64_t num_blocks,
6025 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6026 : {
6027 16 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6028 : struct spdk_bdev_io *bdev_io;
6029 16 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6030 :
6031 16 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6032 0 : return -EINVAL;
6033 : }
6034 :
6035 16 : bdev_io = bdev_channel_get_io(channel);
6036 16 : if (!bdev_io) {
6037 0 : return -ENOMEM;
6038 : }
6039 :
6040 16 : bdev_io->internal.ch = channel;
6041 16 : bdev_io->internal.desc = desc;
6042 16 : bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE;
6043 16 : bdev_io->u.bdev.iovs = iov;
6044 16 : bdev_io->u.bdev.iovcnt = iovcnt;
6045 16 : bdev_io->u.bdev.md_buf = md_buf;
6046 16 : bdev_io->u.bdev.num_blocks = num_blocks;
6047 16 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6048 16 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6049 16 : bdev_io->u.bdev.memory_domain = NULL;
6050 16 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6051 16 : bdev_io->u.bdev.accel_sequence = NULL;
6052 :
6053 16 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) {
6054 7 : bdev_io_submit(bdev_io);
6055 7 : return 0;
6056 : }
6057 :
6058 9 : bdev_compare_do_read(bdev_io);
6059 :
6060 9 : return 0;
6061 16 : }
6062 :
6063 : int
6064 10 : spdk_bdev_comparev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6065 : struct iovec *iov, int iovcnt,
6066 : uint64_t offset_blocks, uint64_t num_blocks,
6067 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6068 : {
6069 20 : return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
6070 10 : num_blocks, cb, cb_arg);
6071 : }
6072 :
6073 : int
6074 6 : spdk_bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6075 : struct iovec *iov, int iovcnt, void *md_buf,
6076 : uint64_t offset_blocks, uint64_t num_blocks,
6077 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6078 : {
6079 6 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
6080 0 : return -EINVAL;
6081 : }
6082 :
6083 6 : if (md_buf && !_is_buf_allocated(iov)) {
6084 0 : return -EINVAL;
6085 : }
6086 :
6087 12 : return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
6088 6 : num_blocks, cb, cb_arg);
6089 6 : }
6090 :
6091 : static int
6092 4 : bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6093 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
6094 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6095 : {
6096 4 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6097 : struct spdk_bdev_io *bdev_io;
6098 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6099 :
6100 4 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6101 0 : return -EINVAL;
6102 : }
6103 :
6104 4 : bdev_io = bdev_channel_get_io(channel);
6105 4 : if (!bdev_io) {
6106 0 : return -ENOMEM;
6107 : }
6108 :
6109 4 : bdev_io->internal.ch = channel;
6110 4 : bdev_io->internal.desc = desc;
6111 4 : bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE;
6112 4 : bdev_io->u.bdev.iovs = &bdev_io->iov;
6113 4 : bdev_io->u.bdev.iovs[0].iov_base = buf;
6114 4 : bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev_desc_get_block_size(desc);
6115 4 : bdev_io->u.bdev.iovcnt = 1;
6116 4 : bdev_io->u.bdev.md_buf = md_buf;
6117 4 : bdev_io->u.bdev.num_blocks = num_blocks;
6118 4 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6119 4 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6120 4 : bdev_io->u.bdev.memory_domain = NULL;
6121 4 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6122 4 : bdev_io->u.bdev.accel_sequence = NULL;
6123 :
6124 4 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) {
6125 2 : bdev_io_submit(bdev_io);
6126 2 : return 0;
6127 : }
6128 :
6129 2 : bdev_compare_do_read(bdev_io);
6130 :
6131 2 : return 0;
6132 4 : }
6133 :
6134 : int
6135 4 : spdk_bdev_compare_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6136 : void *buf, uint64_t offset_blocks, uint64_t num_blocks,
6137 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6138 : {
6139 8 : return bdev_compare_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
6140 4 : cb, cb_arg);
6141 : }
6142 :
6143 : int
6144 0 : spdk_bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6145 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
6146 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6147 : {
6148 0 : struct iovec iov = {
6149 0 : .iov_base = buf,
6150 : };
6151 :
6152 0 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
6153 0 : return -EINVAL;
6154 : }
6155 :
6156 0 : if (md_buf && !_is_buf_allocated(&iov)) {
6157 0 : return -EINVAL;
6158 : }
6159 :
6160 0 : return bdev_compare_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
6161 0 : cb, cb_arg);
6162 0 : }
6163 :
6164 : static void
6165 2 : bdev_comparev_and_writev_blocks_unlocked(struct lba_range *range, void *ctx, int unlock_status)
6166 : {
6167 2 : struct spdk_bdev_io *bdev_io = ctx;
6168 :
6169 2 : if (unlock_status) {
6170 0 : SPDK_ERRLOG("LBA range unlock failed\n");
6171 0 : }
6172 :
6173 4 : bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS ? true :
6174 2 : false, bdev_io->internal.caller_ctx);
6175 2 : }
6176 :
6177 : static void
6178 2 : bdev_comparev_and_writev_blocks_unlock(struct spdk_bdev_io *bdev_io, int status)
6179 : {
6180 2 : bdev_io->internal.status = status;
6181 :
6182 4 : bdev_unlock_lba_range(bdev_io->internal.desc, spdk_io_channel_from_ctx(bdev_io->internal.ch),
6183 2 : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6184 2 : bdev_comparev_and_writev_blocks_unlocked, bdev_io);
6185 2 : }
6186 :
6187 : static void
6188 1 : bdev_compare_and_write_do_write_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
6189 : {
6190 1 : struct spdk_bdev_io *parent_io = cb_arg;
6191 :
6192 1 : if (!success) {
6193 0 : SPDK_ERRLOG("Compare and write operation failed\n");
6194 0 : }
6195 :
6196 1 : spdk_bdev_free_io(bdev_io);
6197 :
6198 2 : bdev_comparev_and_writev_blocks_unlock(parent_io,
6199 1 : success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED);
6200 1 : }
6201 :
6202 : static void
6203 1 : bdev_compare_and_write_do_write(void *_bdev_io)
6204 : {
6205 1 : struct spdk_bdev_io *bdev_io = _bdev_io;
6206 : int rc;
6207 :
6208 2 : rc = spdk_bdev_writev_blocks(bdev_io->internal.desc,
6209 1 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
6210 1 : bdev_io->u.bdev.fused_iovs, bdev_io->u.bdev.fused_iovcnt,
6211 1 : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6212 1 : bdev_compare_and_write_do_write_done, bdev_io);
6213 :
6214 :
6215 1 : if (rc == -ENOMEM) {
6216 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_write);
6217 1 : } else if (rc != 0) {
6218 0 : bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
6219 0 : }
6220 1 : }
6221 :
6222 : static void
6223 2 : bdev_compare_and_write_do_compare_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
6224 : {
6225 2 : struct spdk_bdev_io *parent_io = cb_arg;
6226 :
6227 2 : spdk_bdev_free_io(bdev_io);
6228 :
6229 2 : if (!success) {
6230 1 : bdev_comparev_and_writev_blocks_unlock(parent_io, SPDK_BDEV_IO_STATUS_MISCOMPARE);
6231 1 : return;
6232 : }
6233 :
6234 1 : bdev_compare_and_write_do_write(parent_io);
6235 2 : }
6236 :
6237 : static void
6238 2 : bdev_compare_and_write_do_compare(void *_bdev_io)
6239 : {
6240 2 : struct spdk_bdev_io *bdev_io = _bdev_io;
6241 : int rc;
6242 :
6243 4 : rc = spdk_bdev_comparev_blocks(bdev_io->internal.desc,
6244 2 : spdk_io_channel_from_ctx(bdev_io->internal.ch), bdev_io->u.bdev.iovs,
6245 2 : bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6246 2 : bdev_compare_and_write_do_compare_done, bdev_io);
6247 :
6248 2 : if (rc == -ENOMEM) {
6249 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_compare);
6250 2 : } else if (rc != 0) {
6251 0 : bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED);
6252 0 : }
6253 2 : }
6254 :
6255 : static void
6256 2 : bdev_comparev_and_writev_blocks_locked(struct lba_range *range, void *ctx, int status)
6257 : {
6258 2 : struct spdk_bdev_io *bdev_io = ctx;
6259 :
6260 2 : if (status) {
6261 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED;
6262 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
6263 0 : return;
6264 : }
6265 :
6266 2 : bdev_compare_and_write_do_compare(bdev_io);
6267 2 : }
6268 :
6269 : int
6270 2 : spdk_bdev_comparev_and_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6271 : struct iovec *compare_iov, int compare_iovcnt,
6272 : struct iovec *write_iov, int write_iovcnt,
6273 : uint64_t offset_blocks, uint64_t num_blocks,
6274 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6275 : {
6276 2 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6277 : struct spdk_bdev_io *bdev_io;
6278 2 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6279 :
6280 2 : if (!desc->write) {
6281 0 : return -EBADF;
6282 : }
6283 :
6284 2 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6285 0 : return -EINVAL;
6286 : }
6287 :
6288 2 : if (num_blocks > bdev->acwu) {
6289 0 : return -EINVAL;
6290 : }
6291 :
6292 2 : bdev_io = bdev_channel_get_io(channel);
6293 2 : if (!bdev_io) {
6294 0 : return -ENOMEM;
6295 : }
6296 :
6297 2 : bdev_io->internal.ch = channel;
6298 2 : bdev_io->internal.desc = desc;
6299 2 : bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE;
6300 2 : bdev_io->u.bdev.iovs = compare_iov;
6301 2 : bdev_io->u.bdev.iovcnt = compare_iovcnt;
6302 2 : bdev_io->u.bdev.fused_iovs = write_iov;
6303 2 : bdev_io->u.bdev.fused_iovcnt = write_iovcnt;
6304 2 : bdev_io->u.bdev.md_buf = NULL;
6305 2 : bdev_io->u.bdev.num_blocks = num_blocks;
6306 2 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6307 2 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6308 2 : bdev_io->u.bdev.memory_domain = NULL;
6309 2 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6310 2 : bdev_io->u.bdev.accel_sequence = NULL;
6311 :
6312 2 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE)) {
6313 0 : bdev_io_submit(bdev_io);
6314 0 : return 0;
6315 : }
6316 :
6317 4 : return bdev_lock_lba_range(desc, ch, offset_blocks, num_blocks,
6318 2 : bdev_comparev_and_writev_blocks_locked, bdev_io);
6319 2 : }
6320 :
6321 : int
6322 2 : spdk_bdev_zcopy_start(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6323 : struct iovec *iov, int iovcnt,
6324 : uint64_t offset_blocks, uint64_t num_blocks,
6325 : bool populate,
6326 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6327 : {
6328 2 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6329 : struct spdk_bdev_io *bdev_io;
6330 2 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6331 :
6332 2 : if (!desc->write) {
6333 0 : return -EBADF;
6334 : }
6335 :
6336 2 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6337 0 : return -EINVAL;
6338 : }
6339 :
6340 2 : if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) {
6341 0 : return -ENOTSUP;
6342 : }
6343 :
6344 2 : bdev_io = bdev_channel_get_io(channel);
6345 2 : if (!bdev_io) {
6346 0 : return -ENOMEM;
6347 : }
6348 :
6349 2 : bdev_io->internal.ch = channel;
6350 2 : bdev_io->internal.desc = desc;
6351 2 : bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY;
6352 2 : bdev_io->u.bdev.num_blocks = num_blocks;
6353 2 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6354 2 : bdev_io->u.bdev.iovs = iov;
6355 2 : bdev_io->u.bdev.iovcnt = iovcnt;
6356 2 : bdev_io->u.bdev.md_buf = NULL;
6357 2 : bdev_io->u.bdev.zcopy.populate = populate ? 1 : 0;
6358 2 : bdev_io->u.bdev.zcopy.commit = 0;
6359 2 : bdev_io->u.bdev.zcopy.start = 1;
6360 2 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6361 2 : bdev_io->u.bdev.memory_domain = NULL;
6362 2 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6363 2 : bdev_io->u.bdev.accel_sequence = NULL;
6364 :
6365 2 : bdev_io_submit(bdev_io);
6366 :
6367 2 : return 0;
6368 2 : }
6369 :
6370 : int
6371 2 : spdk_bdev_zcopy_end(struct spdk_bdev_io *bdev_io, bool commit,
6372 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6373 : {
6374 2 : if (bdev_io->type != SPDK_BDEV_IO_TYPE_ZCOPY) {
6375 0 : return -EINVAL;
6376 : }
6377 :
6378 2 : bdev_io->u.bdev.zcopy.commit = commit ? 1 : 0;
6379 2 : bdev_io->u.bdev.zcopy.start = 0;
6380 2 : bdev_io->internal.caller_ctx = cb_arg;
6381 2 : bdev_io->internal.cb = cb;
6382 2 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
6383 :
6384 2 : bdev_io_submit(bdev_io);
6385 :
6386 2 : return 0;
6387 2 : }
6388 :
6389 : int
6390 0 : spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6391 : uint64_t offset, uint64_t len,
6392 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6393 : {
6394 : uint64_t offset_blocks, num_blocks;
6395 :
6396 0 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, len, &num_blocks) != 0) {
6397 0 : return -EINVAL;
6398 : }
6399 :
6400 0 : return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
6401 0 : }
6402 :
6403 : int
6404 33 : spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6405 : uint64_t offset_blocks, uint64_t num_blocks,
6406 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6407 : {
6408 33 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6409 : struct spdk_bdev_io *bdev_io;
6410 33 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6411 :
6412 33 : if (!desc->write) {
6413 0 : return -EBADF;
6414 : }
6415 :
6416 33 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6417 0 : return -EINVAL;
6418 : }
6419 :
6420 33 : if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) &&
6421 10 : !bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)) {
6422 1 : return -ENOTSUP;
6423 : }
6424 :
6425 32 : bdev_io = bdev_channel_get_io(channel);
6426 :
6427 32 : if (!bdev_io) {
6428 0 : return -ENOMEM;
6429 : }
6430 :
6431 32 : bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES;
6432 32 : bdev_io->internal.ch = channel;
6433 32 : bdev_io->internal.desc = desc;
6434 32 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6435 32 : bdev_io->u.bdev.num_blocks = num_blocks;
6436 32 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6437 32 : bdev_io->u.bdev.memory_domain = NULL;
6438 32 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6439 32 : bdev_io->u.bdev.accel_sequence = NULL;
6440 :
6441 : /* If the write_zeroes size is large and should be split, use the generic split
6442 : * logic regardless of whether SPDK_BDEV_IO_TYPE_WRITE_ZEREOS is supported or not.
6443 : *
6444 : * Then, send the write_zeroes request if SPDK_BDEV_IO_TYPE_WRITE_ZEROES is supported
6445 : * or emulate it using regular write request otherwise.
6446 : */
6447 32 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) ||
6448 9 : bdev_io->internal.f.split) {
6449 26 : bdev_io_submit(bdev_io);
6450 26 : return 0;
6451 : }
6452 :
6453 6 : assert(_bdev_get_block_size_with_md(bdev) <= ZERO_BUFFER_SIZE);
6454 :
6455 6 : return bdev_write_zero_buffer(bdev_io);
6456 33 : }
6457 :
6458 : int
6459 0 : spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6460 : uint64_t offset, uint64_t nbytes,
6461 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6462 : {
6463 : uint64_t offset_blocks, num_blocks;
6464 :
6465 0 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
6466 0 : return -EINVAL;
6467 : }
6468 :
6469 0 : return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
6470 0 : }
6471 :
6472 : static void
6473 0 : bdev_io_complete_cb(void *ctx)
6474 : {
6475 0 : struct spdk_bdev_io *bdev_io = ctx;
6476 :
6477 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
6478 0 : bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx);
6479 0 : }
6480 :
6481 : int
6482 22 : spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6483 : uint64_t offset_blocks, uint64_t num_blocks,
6484 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6485 : {
6486 22 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6487 : struct spdk_bdev_io *bdev_io;
6488 22 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6489 :
6490 22 : if (!desc->write) {
6491 0 : return -EBADF;
6492 : }
6493 :
6494 22 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6495 0 : return -EINVAL;
6496 : }
6497 :
6498 22 : bdev_io = bdev_channel_get_io(channel);
6499 22 : if (!bdev_io) {
6500 0 : return -ENOMEM;
6501 : }
6502 :
6503 22 : bdev_io->internal.ch = channel;
6504 22 : bdev_io->internal.desc = desc;
6505 22 : bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP;
6506 :
6507 22 : bdev_io->u.bdev.iovs = &bdev_io->iov;
6508 22 : bdev_io->u.bdev.iovs[0].iov_base = NULL;
6509 22 : bdev_io->u.bdev.iovs[0].iov_len = 0;
6510 22 : bdev_io->u.bdev.iovcnt = 1;
6511 :
6512 22 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6513 22 : bdev_io->u.bdev.num_blocks = num_blocks;
6514 22 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6515 22 : bdev_io->u.bdev.memory_domain = NULL;
6516 22 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6517 22 : bdev_io->u.bdev.accel_sequence = NULL;
6518 :
6519 22 : if (num_blocks == 0) {
6520 0 : spdk_thread_send_msg(spdk_get_thread(), bdev_io_complete_cb, bdev_io);
6521 0 : return 0;
6522 : }
6523 :
6524 22 : bdev_io_submit(bdev_io);
6525 22 : return 0;
6526 22 : }
6527 :
6528 : int
6529 0 : spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6530 : uint64_t offset, uint64_t length,
6531 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6532 : {
6533 : uint64_t offset_blocks, num_blocks;
6534 :
6535 0 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, length, &num_blocks) != 0) {
6536 0 : return -EINVAL;
6537 : }
6538 :
6539 0 : return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
6540 0 : }
6541 :
6542 : int
6543 2 : spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6544 : uint64_t offset_blocks, uint64_t num_blocks,
6545 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6546 : {
6547 2 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6548 : struct spdk_bdev_io *bdev_io;
6549 2 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6550 :
6551 2 : if (!desc->write) {
6552 0 : return -EBADF;
6553 : }
6554 :
6555 2 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6556 0 : return -EINVAL;
6557 : }
6558 :
6559 2 : bdev_io = bdev_channel_get_io(channel);
6560 2 : if (!bdev_io) {
6561 0 : return -ENOMEM;
6562 : }
6563 :
6564 2 : bdev_io->internal.ch = channel;
6565 2 : bdev_io->internal.desc = desc;
6566 2 : bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH;
6567 2 : bdev_io->u.bdev.iovs = NULL;
6568 2 : bdev_io->u.bdev.iovcnt = 0;
6569 2 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6570 2 : bdev_io->u.bdev.num_blocks = num_blocks;
6571 2 : bdev_io->u.bdev.memory_domain = NULL;
6572 2 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6573 2 : bdev_io->u.bdev.accel_sequence = NULL;
6574 2 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6575 :
6576 2 : bdev_io_submit(bdev_io);
6577 2 : return 0;
6578 2 : }
6579 :
6580 : static int bdev_reset_poll_for_outstanding_io(void *ctx);
6581 :
6582 : static void
6583 13 : bdev_reset_check_outstanding_io_done(struct spdk_bdev *bdev, void *_ctx, int status)
6584 : {
6585 13 : struct spdk_bdev_channel *ch = _ctx;
6586 : struct spdk_bdev_io *bdev_io;
6587 :
6588 13 : bdev_io = TAILQ_FIRST(&ch->queued_resets);
6589 :
6590 13 : if (status == -EBUSY) {
6591 9 : if (spdk_get_ticks() < bdev_io->u.reset.wait_poller.stop_time_tsc) {
6592 8 : bdev_io->u.reset.wait_poller.poller = SPDK_POLLER_REGISTER(bdev_reset_poll_for_outstanding_io,
6593 : ch, BDEV_RESET_CHECK_OUTSTANDING_IO_PERIOD);
6594 8 : } else {
6595 1 : TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
6596 :
6597 1 : if (TAILQ_EMPTY(&ch->io_memory_domain) && TAILQ_EMPTY(&ch->io_accel_exec)) {
6598 : /* If outstanding IOs are still present and reset_io_drain_timeout
6599 : * seconds passed, start the reset. */
6600 1 : bdev_io_submit_reset(bdev_io);
6601 1 : } else {
6602 : /* We still have in progress memory domain pull/push or we're
6603 : * executing accel sequence. Since we cannot abort either of those
6604 : * operations, fail the reset request. */
6605 0 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
6606 : }
6607 : }
6608 9 : } else {
6609 4 : TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
6610 4 : SPDK_DEBUGLOG(bdev,
6611 : "Skipping reset for underlying device of bdev: %s - no outstanding I/O.\n",
6612 : ch->bdev->name);
6613 : /* Mark the completion status as a SUCCESS and complete the reset. */
6614 4 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
6615 : }
6616 13 : }
6617 :
6618 : static void
6619 13 : bdev_reset_check_outstanding_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6620 : struct spdk_io_channel *io_ch, void *_ctx)
6621 : {
6622 13 : struct spdk_bdev_channel *cur_ch = __io_ch_to_bdev_ch(io_ch);
6623 13 : int status = 0;
6624 :
6625 17 : if (cur_ch->io_outstanding > 0 ||
6626 4 : !TAILQ_EMPTY(&cur_ch->io_memory_domain) ||
6627 4 : !TAILQ_EMPTY(&cur_ch->io_accel_exec)) {
6628 : /* If a channel has outstanding IO, set status to -EBUSY code. This will stop
6629 : * further iteration over the rest of the channels and pass non-zero status
6630 : * to the callback function. */
6631 9 : status = -EBUSY;
6632 9 : }
6633 13 : spdk_bdev_for_each_channel_continue(i, status);
6634 13 : }
6635 :
6636 : static int
6637 8 : bdev_reset_poll_for_outstanding_io(void *ctx)
6638 : {
6639 8 : struct spdk_bdev_channel *ch = ctx;
6640 : struct spdk_bdev_io *bdev_io;
6641 :
6642 8 : bdev_io = TAILQ_FIRST(&ch->queued_resets);
6643 :
6644 8 : spdk_poller_unregister(&bdev_io->u.reset.wait_poller.poller);
6645 8 : spdk_bdev_for_each_channel(ch->bdev, bdev_reset_check_outstanding_io, ch,
6646 : bdev_reset_check_outstanding_io_done);
6647 :
6648 8 : return SPDK_POLLER_BUSY;
6649 : }
6650 :
6651 : static void
6652 15 : bdev_reset_freeze_channel_done(struct spdk_bdev *bdev, void *_ctx, int status)
6653 : {
6654 15 : struct spdk_bdev_channel *ch = _ctx;
6655 : struct spdk_bdev_io *bdev_io;
6656 :
6657 15 : bdev_io = TAILQ_FIRST(&ch->queued_resets);
6658 :
6659 15 : if (bdev->reset_io_drain_timeout == 0) {
6660 10 : TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
6661 :
6662 10 : bdev_io_submit_reset(bdev_io);
6663 10 : return;
6664 : }
6665 :
6666 10 : bdev_io->u.reset.wait_poller.stop_time_tsc = spdk_get_ticks() +
6667 5 : (ch->bdev->reset_io_drain_timeout * spdk_get_ticks_hz());
6668 :
6669 : /* In case bdev->reset_io_drain_timeout is not equal to zero,
6670 : * submit the reset to the underlying module only if outstanding I/O
6671 : * remain after reset_io_drain_timeout seconds have passed. */
6672 5 : spdk_bdev_for_each_channel(ch->bdev, bdev_reset_check_outstanding_io, ch,
6673 : bdev_reset_check_outstanding_io_done);
6674 15 : }
6675 :
6676 : static void
6677 18 : bdev_reset_freeze_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6678 : struct spdk_io_channel *ch, void *_ctx)
6679 : {
6680 : struct spdk_bdev_channel *channel;
6681 : struct spdk_bdev_mgmt_channel *mgmt_channel;
6682 : struct spdk_bdev_shared_resource *shared_resource;
6683 : bdev_io_tailq_t tmp_queued;
6684 :
6685 18 : TAILQ_INIT(&tmp_queued);
6686 :
6687 18 : channel = __io_ch_to_bdev_ch(ch);
6688 18 : shared_resource = channel->shared_resource;
6689 18 : mgmt_channel = shared_resource->mgmt_ch;
6690 :
6691 18 : channel->flags |= BDEV_CH_RESET_IN_PROGRESS;
6692 :
6693 18 : if ((channel->flags & BDEV_CH_QOS_ENABLED) != 0) {
6694 2 : TAILQ_SWAP(&channel->qos_queued_io, &tmp_queued, spdk_bdev_io, internal.link);
6695 2 : }
6696 :
6697 18 : bdev_abort_all_queued_io(&shared_resource->nomem_io, channel);
6698 18 : bdev_abort_all_buf_io(mgmt_channel, channel);
6699 18 : bdev_abort_all_queued_io(&tmp_queued, channel);
6700 :
6701 18 : spdk_bdev_for_each_channel_continue(i, 0);
6702 18 : }
6703 :
6704 : static void
6705 15 : bdev_start_reset(void *ctx)
6706 : {
6707 15 : struct spdk_bdev_channel *ch = ctx;
6708 :
6709 15 : spdk_bdev_for_each_channel(ch->bdev, bdev_reset_freeze_channel, ch,
6710 : bdev_reset_freeze_channel_done);
6711 15 : }
6712 :
6713 : static void
6714 16 : bdev_channel_start_reset(struct spdk_bdev_channel *ch)
6715 : {
6716 16 : struct spdk_bdev *bdev = ch->bdev;
6717 :
6718 16 : assert(!TAILQ_EMPTY(&ch->queued_resets));
6719 :
6720 16 : spdk_spin_lock(&bdev->internal.spinlock);
6721 16 : if (bdev->internal.reset_in_progress == NULL) {
6722 15 : bdev->internal.reset_in_progress = TAILQ_FIRST(&ch->queued_resets);
6723 : /*
6724 : * Take a channel reference for the target bdev for the life of this
6725 : * reset. This guards against the channel getting destroyed while
6726 : * spdk_bdev_for_each_channel() calls related to this reset IO are in
6727 : * progress. We will release the reference when this reset is
6728 : * completed.
6729 : */
6730 15 : bdev->internal.reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev));
6731 15 : bdev_start_reset(ch);
6732 15 : }
6733 16 : spdk_spin_unlock(&bdev->internal.spinlock);
6734 16 : }
6735 :
6736 : int
6737 16 : spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6738 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6739 : {
6740 16 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6741 : struct spdk_bdev_io *bdev_io;
6742 16 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6743 :
6744 16 : bdev_io = bdev_channel_get_io(channel);
6745 16 : if (!bdev_io) {
6746 0 : return -ENOMEM;
6747 : }
6748 :
6749 16 : bdev_io->internal.ch = channel;
6750 16 : bdev_io->internal.desc = desc;
6751 16 : bdev_io->internal.submit_tsc = spdk_get_ticks();
6752 16 : bdev_io->type = SPDK_BDEV_IO_TYPE_RESET;
6753 16 : bdev_io->u.reset.ch_ref = NULL;
6754 16 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6755 :
6756 16 : spdk_spin_lock(&bdev->internal.spinlock);
6757 16 : TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, internal.link);
6758 16 : spdk_spin_unlock(&bdev->internal.spinlock);
6759 :
6760 16 : bdev_ch_add_to_io_submitted(bdev_io);
6761 :
6762 16 : bdev_channel_start_reset(channel);
6763 :
6764 16 : return 0;
6765 16 : }
6766 :
6767 : void
6768 0 : spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
6769 : struct spdk_bdev_io_stat *stat, enum spdk_bdev_reset_stat_mode reset_mode)
6770 : {
6771 0 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6772 :
6773 0 : bdev_get_io_stat(stat, channel->stat);
6774 0 : spdk_bdev_reset_io_stat(stat, reset_mode);
6775 0 : }
6776 :
6777 : static void
6778 5 : bdev_get_device_stat_done(struct spdk_bdev *bdev, void *_ctx, int status)
6779 : {
6780 5 : struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = _ctx;
6781 :
6782 10 : bdev_iostat_ctx->cb(bdev, bdev_iostat_ctx->stat,
6783 5 : bdev_iostat_ctx->cb_arg, 0);
6784 5 : free(bdev_iostat_ctx);
6785 5 : }
6786 :
6787 : static void
6788 4 : bdev_get_each_channel_stat(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6789 : struct spdk_io_channel *ch, void *_ctx)
6790 : {
6791 4 : struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = _ctx;
6792 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6793 :
6794 4 : spdk_bdev_add_io_stat(bdev_iostat_ctx->stat, channel->stat);
6795 4 : spdk_bdev_reset_io_stat(channel->stat, bdev_iostat_ctx->reset_mode);
6796 4 : spdk_bdev_for_each_channel_continue(i, 0);
6797 4 : }
6798 :
6799 : void
6800 5 : spdk_bdev_get_device_stat(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat,
6801 : enum spdk_bdev_reset_stat_mode reset_mode, spdk_bdev_get_device_stat_cb cb, void *cb_arg)
6802 : {
6803 : struct spdk_bdev_iostat_ctx *bdev_iostat_ctx;
6804 :
6805 5 : assert(bdev != NULL);
6806 5 : assert(stat != NULL);
6807 5 : assert(cb != NULL);
6808 :
6809 5 : bdev_iostat_ctx = calloc(1, sizeof(struct spdk_bdev_iostat_ctx));
6810 5 : if (bdev_iostat_ctx == NULL) {
6811 0 : SPDK_ERRLOG("Unable to allocate memory for spdk_bdev_iostat_ctx\n");
6812 0 : cb(bdev, stat, cb_arg, -ENOMEM);
6813 0 : return;
6814 : }
6815 :
6816 5 : bdev_iostat_ctx->stat = stat;
6817 5 : bdev_iostat_ctx->cb = cb;
6818 5 : bdev_iostat_ctx->cb_arg = cb_arg;
6819 5 : bdev_iostat_ctx->reset_mode = reset_mode;
6820 :
6821 : /* Start with the statistics from previously deleted channels. */
6822 5 : spdk_spin_lock(&bdev->internal.spinlock);
6823 5 : bdev_get_io_stat(bdev_iostat_ctx->stat, bdev->internal.stat);
6824 5 : spdk_bdev_reset_io_stat(bdev->internal.stat, reset_mode);
6825 5 : spdk_spin_unlock(&bdev->internal.spinlock);
6826 :
6827 : /* Then iterate and add the statistics from each existing channel. */
6828 5 : spdk_bdev_for_each_channel(bdev, bdev_get_each_channel_stat, bdev_iostat_ctx,
6829 : bdev_get_device_stat_done);
6830 5 : }
6831 :
6832 : struct bdev_iostat_reset_ctx {
6833 : enum spdk_bdev_reset_stat_mode mode;
6834 : bdev_reset_device_stat_cb cb;
6835 : void *cb_arg;
6836 : };
6837 :
6838 : static void
6839 0 : bdev_reset_device_stat_done(struct spdk_bdev *bdev, void *_ctx, int status)
6840 : {
6841 0 : struct bdev_iostat_reset_ctx *ctx = _ctx;
6842 :
6843 0 : ctx->cb(bdev, ctx->cb_arg, 0);
6844 :
6845 0 : free(ctx);
6846 0 : }
6847 :
6848 : static void
6849 0 : bdev_reset_each_channel_stat(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6850 : struct spdk_io_channel *ch, void *_ctx)
6851 : {
6852 0 : struct bdev_iostat_reset_ctx *ctx = _ctx;
6853 0 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6854 :
6855 0 : spdk_bdev_reset_io_stat(channel->stat, ctx->mode);
6856 :
6857 0 : spdk_bdev_for_each_channel_continue(i, 0);
6858 0 : }
6859 :
6860 : void
6861 0 : bdev_reset_device_stat(struct spdk_bdev *bdev, enum spdk_bdev_reset_stat_mode mode,
6862 : bdev_reset_device_stat_cb cb, void *cb_arg)
6863 : {
6864 : struct bdev_iostat_reset_ctx *ctx;
6865 :
6866 0 : assert(bdev != NULL);
6867 0 : assert(cb != NULL);
6868 :
6869 0 : ctx = calloc(1, sizeof(*ctx));
6870 0 : if (ctx == NULL) {
6871 0 : SPDK_ERRLOG("Unable to allocate bdev_iostat_reset_ctx.\n");
6872 0 : cb(bdev, cb_arg, -ENOMEM);
6873 0 : return;
6874 : }
6875 :
6876 0 : ctx->mode = mode;
6877 0 : ctx->cb = cb;
6878 0 : ctx->cb_arg = cb_arg;
6879 :
6880 0 : spdk_spin_lock(&bdev->internal.spinlock);
6881 0 : spdk_bdev_reset_io_stat(bdev->internal.stat, mode);
6882 0 : spdk_spin_unlock(&bdev->internal.spinlock);
6883 :
6884 0 : spdk_bdev_for_each_channel(bdev,
6885 : bdev_reset_each_channel_stat,
6886 0 : ctx,
6887 : bdev_reset_device_stat_done);
6888 0 : }
6889 :
6890 : int
6891 1 : spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6892 : const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
6893 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6894 : {
6895 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6896 : struct spdk_bdev_io *bdev_io;
6897 1 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6898 :
6899 1 : if (!desc->write) {
6900 0 : return -EBADF;
6901 : }
6902 :
6903 1 : if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN))) {
6904 1 : return -ENOTSUP;
6905 : }
6906 :
6907 0 : bdev_io = bdev_channel_get_io(channel);
6908 0 : if (!bdev_io) {
6909 0 : return -ENOMEM;
6910 : }
6911 :
6912 0 : bdev_io->internal.ch = channel;
6913 0 : bdev_io->internal.desc = desc;
6914 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN;
6915 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
6916 0 : bdev_io->u.nvme_passthru.buf = buf;
6917 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
6918 0 : bdev_io->u.nvme_passthru.md_buf = NULL;
6919 0 : bdev_io->u.nvme_passthru.md_len = 0;
6920 :
6921 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6922 :
6923 0 : bdev_io_submit(bdev_io);
6924 0 : return 0;
6925 1 : }
6926 :
6927 : int
6928 1 : spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6929 : const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
6930 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6931 : {
6932 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6933 : struct spdk_bdev_io *bdev_io;
6934 1 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6935 :
6936 1 : if (!desc->write) {
6937 : /*
6938 : * Do not try to parse the NVMe command - we could maybe use bits in the opcode
6939 : * to easily determine if the command is a read or write, but for now just
6940 : * do not allow io_passthru with a read-only descriptor.
6941 : */
6942 0 : return -EBADF;
6943 : }
6944 :
6945 1 : if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO))) {
6946 1 : return -ENOTSUP;
6947 : }
6948 :
6949 0 : bdev_io = bdev_channel_get_io(channel);
6950 0 : if (!bdev_io) {
6951 0 : return -ENOMEM;
6952 : }
6953 :
6954 0 : bdev_io->internal.ch = channel;
6955 0 : bdev_io->internal.desc = desc;
6956 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO;
6957 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
6958 0 : bdev_io->u.nvme_passthru.buf = buf;
6959 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
6960 0 : bdev_io->u.nvme_passthru.md_buf = NULL;
6961 0 : bdev_io->u.nvme_passthru.md_len = 0;
6962 :
6963 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6964 :
6965 0 : bdev_io_submit(bdev_io);
6966 0 : return 0;
6967 1 : }
6968 :
6969 : int
6970 1 : spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6971 : const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len,
6972 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6973 : {
6974 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6975 : struct spdk_bdev_io *bdev_io;
6976 1 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6977 :
6978 1 : if (!desc->write) {
6979 : /*
6980 : * Do not try to parse the NVMe command - we could maybe use bits in the opcode
6981 : * to easily determine if the command is a read or write, but for now just
6982 : * do not allow io_passthru with a read-only descriptor.
6983 : */
6984 0 : return -EBADF;
6985 : }
6986 :
6987 1 : if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO_MD))) {
6988 1 : return -ENOTSUP;
6989 : }
6990 :
6991 0 : bdev_io = bdev_channel_get_io(channel);
6992 0 : if (!bdev_io) {
6993 0 : return -ENOMEM;
6994 : }
6995 :
6996 0 : bdev_io->internal.ch = channel;
6997 0 : bdev_io->internal.desc = desc;
6998 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD;
6999 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
7000 0 : bdev_io->u.nvme_passthru.buf = buf;
7001 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
7002 0 : bdev_io->u.nvme_passthru.md_buf = md_buf;
7003 0 : bdev_io->u.nvme_passthru.md_len = md_len;
7004 :
7005 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7006 :
7007 0 : bdev_io_submit(bdev_io);
7008 0 : return 0;
7009 1 : }
7010 :
7011 : int
7012 0 : spdk_bdev_nvme_iov_passthru_md(struct spdk_bdev_desc *desc,
7013 : struct spdk_io_channel *ch,
7014 : const struct spdk_nvme_cmd *cmd,
7015 : struct iovec *iov, int iovcnt, size_t nbytes,
7016 : void *md_buf, size_t md_len,
7017 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7018 : {
7019 0 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7020 : struct spdk_bdev_io *bdev_io;
7021 0 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7022 :
7023 0 : if (!desc->write) {
7024 : /*
7025 : * Do not try to parse the NVMe command - we could maybe use bits in the opcode
7026 : * to easily determine if the command is a read or write, but for now just
7027 : * do not allow io_passthru with a read-only descriptor.
7028 : */
7029 0 : return -EBADF;
7030 : }
7031 :
7032 0 : if (md_buf && spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO_MD))) {
7033 0 : return -ENOTSUP;
7034 0 : } else if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO))) {
7035 0 : return -ENOTSUP;
7036 : }
7037 :
7038 0 : bdev_io = bdev_channel_get_io(channel);
7039 0 : if (!bdev_io) {
7040 0 : return -ENOMEM;
7041 : }
7042 :
7043 0 : bdev_io->internal.ch = channel;
7044 0 : bdev_io->internal.desc = desc;
7045 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IOV_MD;
7046 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
7047 0 : bdev_io->u.nvme_passthru.iovs = iov;
7048 0 : bdev_io->u.nvme_passthru.iovcnt = iovcnt;
7049 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
7050 0 : bdev_io->u.nvme_passthru.md_buf = md_buf;
7051 0 : bdev_io->u.nvme_passthru.md_len = md_len;
7052 :
7053 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7054 :
7055 0 : bdev_io_submit(bdev_io);
7056 0 : return 0;
7057 0 : }
7058 :
7059 : static void bdev_abort_retry(void *ctx);
7060 : static void bdev_abort(struct spdk_bdev_io *parent_io);
7061 :
7062 : static void
7063 22 : bdev_abort_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
7064 : {
7065 22 : struct spdk_bdev_channel *channel = bdev_io->internal.ch;
7066 22 : struct spdk_bdev_io *parent_io = cb_arg;
7067 : struct spdk_bdev_io *bio_to_abort, *tmp_io;
7068 :
7069 22 : bio_to_abort = bdev_io->u.abort.bio_to_abort;
7070 :
7071 22 : spdk_bdev_free_io(bdev_io);
7072 :
7073 22 : if (!success) {
7074 : /* Check if the target I/O completed in the meantime. */
7075 2 : TAILQ_FOREACH(tmp_io, &channel->io_submitted, internal.ch_link) {
7076 1 : if (tmp_io == bio_to_abort) {
7077 0 : break;
7078 : }
7079 1 : }
7080 :
7081 : /* If the target I/O still exists, set the parent to failed. */
7082 1 : if (tmp_io != NULL) {
7083 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7084 0 : }
7085 1 : }
7086 :
7087 22 : assert(parent_io->internal.f.split);
7088 :
7089 22 : parent_io->internal.split.outstanding--;
7090 22 : if (parent_io->internal.split.outstanding == 0) {
7091 16 : if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
7092 0 : bdev_abort_retry(parent_io);
7093 0 : } else {
7094 16 : bdev_io_complete(parent_io);
7095 : }
7096 16 : }
7097 22 : }
7098 :
7099 : static int
7100 23 : bdev_abort_io(struct spdk_bdev_desc *desc, struct spdk_bdev_channel *channel,
7101 : struct spdk_bdev_io *bio_to_abort,
7102 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7103 : {
7104 23 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7105 : struct spdk_bdev_io *bdev_io;
7106 :
7107 23 : if (bio_to_abort->type == SPDK_BDEV_IO_TYPE_ABORT ||
7108 23 : bio_to_abort->type == SPDK_BDEV_IO_TYPE_RESET) {
7109 : /* TODO: Abort reset or abort request. */
7110 0 : return -ENOTSUP;
7111 : }
7112 :
7113 23 : bdev_io = bdev_channel_get_io(channel);
7114 23 : if (bdev_io == NULL) {
7115 1 : return -ENOMEM;
7116 : }
7117 :
7118 22 : bdev_io->internal.ch = channel;
7119 22 : bdev_io->internal.desc = desc;
7120 22 : bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT;
7121 22 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7122 :
7123 22 : if (bio_to_abort->internal.f.split) {
7124 6 : assert(bdev_io_should_split(bio_to_abort));
7125 6 : bdev_io->u.bdev.abort.bio_cb_arg = bio_to_abort;
7126 :
7127 : /* Parent abort request is not submitted directly, but to manage its
7128 : * execution add it to the submitted list here.
7129 : */
7130 6 : bdev_io->internal.submit_tsc = spdk_get_ticks();
7131 6 : bdev_ch_add_to_io_submitted(bdev_io);
7132 :
7133 6 : bdev_abort(bdev_io);
7134 :
7135 6 : return 0;
7136 : }
7137 :
7138 16 : bdev_io->u.abort.bio_to_abort = bio_to_abort;
7139 :
7140 : /* Submit the abort request to the underlying bdev module. */
7141 16 : bdev_io_submit(bdev_io);
7142 :
7143 16 : return 0;
7144 23 : }
7145 :
7146 : static bool
7147 46 : bdev_io_on_tailq(struct spdk_bdev_io *bdev_io, bdev_io_tailq_t *tailq)
7148 : {
7149 : struct spdk_bdev_io *iter;
7150 :
7151 46 : TAILQ_FOREACH(iter, tailq, internal.link) {
7152 0 : if (iter == bdev_io) {
7153 0 : return true;
7154 : }
7155 0 : }
7156 :
7157 46 : return false;
7158 46 : }
7159 :
7160 : static uint32_t
7161 18 : _bdev_abort(struct spdk_bdev_io *parent_io)
7162 : {
7163 18 : struct spdk_bdev_desc *desc = parent_io->internal.desc;
7164 18 : struct spdk_bdev_channel *channel = parent_io->internal.ch;
7165 : void *bio_cb_arg;
7166 : struct spdk_bdev_io *bio_to_abort;
7167 : uint32_t matched_ios;
7168 : int rc;
7169 :
7170 18 : bio_cb_arg = parent_io->u.bdev.abort.bio_cb_arg;
7171 :
7172 : /* matched_ios is returned and will be kept by the caller.
7173 : *
7174 : * This function will be used for two cases, 1) the same cb_arg is used for
7175 : * multiple I/Os, 2) a single large I/O is split into smaller ones.
7176 : * Incrementing split_outstanding directly here may confuse readers especially
7177 : * for the 1st case.
7178 : *
7179 : * Completion of I/O abort is processed after stack unwinding. Hence this trick
7180 : * works as expected.
7181 : */
7182 18 : matched_ios = 0;
7183 18 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
7184 :
7185 105 : TAILQ_FOREACH(bio_to_abort, &channel->io_submitted, internal.ch_link) {
7186 88 : if (bio_to_abort->internal.caller_ctx != bio_cb_arg) {
7187 65 : continue;
7188 : }
7189 :
7190 23 : if (bio_to_abort->internal.submit_tsc > parent_io->internal.submit_tsc) {
7191 : /* Any I/O which was submitted after this abort command should be excluded. */
7192 0 : continue;
7193 : }
7194 :
7195 : /* We can't abort a request that's being pushed/pulled or executed by accel */
7196 23 : if (bdev_io_on_tailq(bio_to_abort, &channel->io_accel_exec) ||
7197 23 : bdev_io_on_tailq(bio_to_abort, &channel->io_memory_domain)) {
7198 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7199 0 : break;
7200 : }
7201 :
7202 23 : rc = bdev_abort_io(desc, channel, bio_to_abort, bdev_abort_io_done, parent_io);
7203 23 : if (rc != 0) {
7204 1 : if (rc == -ENOMEM) {
7205 1 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_NOMEM;
7206 1 : } else {
7207 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7208 : }
7209 1 : break;
7210 : }
7211 22 : matched_ios++;
7212 22 : }
7213 :
7214 18 : return matched_ios;
7215 : }
7216 :
7217 : static void
7218 1 : bdev_abort_retry(void *ctx)
7219 : {
7220 1 : struct spdk_bdev_io *parent_io = ctx;
7221 : uint32_t matched_ios;
7222 :
7223 1 : matched_ios = _bdev_abort(parent_io);
7224 :
7225 1 : if (matched_ios == 0) {
7226 0 : if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
7227 0 : bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry);
7228 0 : } else {
7229 : /* For retry, the case that no target I/O was found is success
7230 : * because it means target I/Os completed in the meantime.
7231 : */
7232 0 : bdev_io_complete(parent_io);
7233 : }
7234 0 : return;
7235 : }
7236 :
7237 : /* Use split_outstanding to manage the progress of aborting I/Os. */
7238 1 : parent_io->internal.f.split = true;
7239 1 : parent_io->internal.split.outstanding = matched_ios;
7240 1 : }
7241 :
7242 : static void
7243 17 : bdev_abort(struct spdk_bdev_io *parent_io)
7244 : {
7245 : uint32_t matched_ios;
7246 :
7247 17 : matched_ios = _bdev_abort(parent_io);
7248 :
7249 17 : if (matched_ios == 0) {
7250 2 : if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
7251 1 : bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry);
7252 1 : } else {
7253 : /* The case the no target I/O was found is failure. */
7254 1 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7255 1 : bdev_io_complete(parent_io);
7256 : }
7257 2 : return;
7258 : }
7259 :
7260 : /* Use split_outstanding to manage the progress of aborting I/Os. */
7261 15 : parent_io->internal.f.split = true;
7262 15 : parent_io->internal.split.outstanding = matched_ios;
7263 17 : }
7264 :
7265 : int
7266 12 : spdk_bdev_abort(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
7267 : void *bio_cb_arg,
7268 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7269 : {
7270 12 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7271 12 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7272 : struct spdk_bdev_io *bdev_io;
7273 :
7274 12 : if (bio_cb_arg == NULL) {
7275 0 : return -EINVAL;
7276 : }
7277 :
7278 12 : if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT)) {
7279 1 : return -ENOTSUP;
7280 : }
7281 :
7282 11 : bdev_io = bdev_channel_get_io(channel);
7283 11 : if (bdev_io == NULL) {
7284 0 : return -ENOMEM;
7285 : }
7286 :
7287 11 : bdev_io->internal.ch = channel;
7288 11 : bdev_io->internal.desc = desc;
7289 11 : bdev_io->internal.submit_tsc = spdk_get_ticks();
7290 11 : bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT;
7291 11 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7292 :
7293 11 : bdev_io->u.bdev.abort.bio_cb_arg = bio_cb_arg;
7294 :
7295 : /* Parent abort request is not submitted directly, but to manage its execution,
7296 : * add it to the submitted list here.
7297 : */
7298 11 : bdev_ch_add_to_io_submitted(bdev_io);
7299 :
7300 11 : bdev_abort(bdev_io);
7301 :
7302 11 : return 0;
7303 12 : }
7304 :
7305 : int
7306 4 : spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
7307 : struct spdk_bdev_io_wait_entry *entry)
7308 : {
7309 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7310 4 : struct spdk_bdev_mgmt_channel *mgmt_ch = channel->shared_resource->mgmt_ch;
7311 :
7312 4 : if (bdev != entry->bdev) {
7313 0 : SPDK_ERRLOG("bdevs do not match\n");
7314 0 : return -EINVAL;
7315 : }
7316 :
7317 4 : if (mgmt_ch->per_thread_cache_count > 0) {
7318 0 : SPDK_ERRLOG("Cannot queue io_wait if spdk_bdev_io available in per-thread cache\n");
7319 0 : return -EINVAL;
7320 : }
7321 :
7322 4 : TAILQ_INSERT_TAIL(&mgmt_ch->io_wait_queue, entry, link);
7323 4 : return 0;
7324 4 : }
7325 :
7326 : static inline void
7327 610 : bdev_io_update_io_stat(struct spdk_bdev_io *bdev_io, uint64_t tsc_diff)
7328 : {
7329 610 : enum spdk_bdev_io_status io_status = bdev_io->internal.status;
7330 610 : struct spdk_bdev_io_stat *io_stat = bdev_io->internal.ch->stat;
7331 610 : uint64_t num_blocks = bdev_io->u.bdev.num_blocks;
7332 610 : uint32_t blocklen = bdev_io->bdev->blocklen;
7333 :
7334 610 : if (spdk_likely(io_status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
7335 516 : switch (bdev_io->type) {
7336 : case SPDK_BDEV_IO_TYPE_READ:
7337 321 : io_stat->bytes_read += num_blocks * blocklen;
7338 321 : io_stat->num_read_ops++;
7339 321 : io_stat->read_latency_ticks += tsc_diff;
7340 321 : if (io_stat->max_read_latency_ticks < tsc_diff) {
7341 7 : io_stat->max_read_latency_ticks = tsc_diff;
7342 7 : }
7343 321 : if (io_stat->min_read_latency_ticks > tsc_diff) {
7344 42 : io_stat->min_read_latency_ticks = tsc_diff;
7345 42 : }
7346 321 : break;
7347 : case SPDK_BDEV_IO_TYPE_WRITE:
7348 75 : io_stat->bytes_written += num_blocks * blocklen;
7349 75 : io_stat->num_write_ops++;
7350 75 : io_stat->write_latency_ticks += tsc_diff;
7351 75 : if (io_stat->max_write_latency_ticks < tsc_diff) {
7352 4 : io_stat->max_write_latency_ticks = tsc_diff;
7353 4 : }
7354 75 : if (io_stat->min_write_latency_ticks > tsc_diff) {
7355 25 : io_stat->min_write_latency_ticks = tsc_diff;
7356 25 : }
7357 75 : break;
7358 : case SPDK_BDEV_IO_TYPE_UNMAP:
7359 20 : io_stat->bytes_unmapped += num_blocks * blocklen;
7360 20 : io_stat->num_unmap_ops++;
7361 20 : io_stat->unmap_latency_ticks += tsc_diff;
7362 20 : if (io_stat->max_unmap_latency_ticks < tsc_diff) {
7363 0 : io_stat->max_unmap_latency_ticks = tsc_diff;
7364 0 : }
7365 20 : if (io_stat->min_unmap_latency_ticks > tsc_diff) {
7366 3 : io_stat->min_unmap_latency_ticks = tsc_diff;
7367 3 : }
7368 20 : break;
7369 : case SPDK_BDEV_IO_TYPE_ZCOPY:
7370 : /* Track the data in the start phase only */
7371 4 : if (bdev_io->u.bdev.zcopy.start) {
7372 2 : if (bdev_io->u.bdev.zcopy.populate) {
7373 1 : io_stat->bytes_read += num_blocks * blocklen;
7374 1 : io_stat->num_read_ops++;
7375 1 : io_stat->read_latency_ticks += tsc_diff;
7376 1 : if (io_stat->max_read_latency_ticks < tsc_diff) {
7377 0 : io_stat->max_read_latency_ticks = tsc_diff;
7378 0 : }
7379 1 : if (io_stat->min_read_latency_ticks > tsc_diff) {
7380 1 : io_stat->min_read_latency_ticks = tsc_diff;
7381 1 : }
7382 1 : } else {
7383 1 : io_stat->bytes_written += num_blocks * blocklen;
7384 1 : io_stat->num_write_ops++;
7385 1 : io_stat->write_latency_ticks += tsc_diff;
7386 1 : if (io_stat->max_write_latency_ticks < tsc_diff) {
7387 0 : io_stat->max_write_latency_ticks = tsc_diff;
7388 0 : }
7389 1 : if (io_stat->min_write_latency_ticks > tsc_diff) {
7390 1 : io_stat->min_write_latency_ticks = tsc_diff;
7391 1 : }
7392 : }
7393 2 : }
7394 4 : break;
7395 : case SPDK_BDEV_IO_TYPE_COPY:
7396 21 : io_stat->bytes_copied += num_blocks * blocklen;
7397 21 : io_stat->num_copy_ops++;
7398 21 : bdev_io->internal.ch->stat->copy_latency_ticks += tsc_diff;
7399 21 : if (io_stat->max_copy_latency_ticks < tsc_diff) {
7400 0 : io_stat->max_copy_latency_ticks = tsc_diff;
7401 0 : }
7402 21 : if (io_stat->min_copy_latency_ticks > tsc_diff) {
7403 4 : io_stat->min_copy_latency_ticks = tsc_diff;
7404 4 : }
7405 21 : break;
7406 : default:
7407 75 : break;
7408 : }
7409 610 : } else if (io_status <= SPDK_BDEV_IO_STATUS_FAILED && io_status >= SPDK_MIN_BDEV_IO_STATUS) {
7410 94 : io_stat = bdev_io->bdev->internal.stat;
7411 94 : assert(io_stat->io_error != NULL);
7412 :
7413 94 : spdk_spin_lock(&bdev_io->bdev->internal.spinlock);
7414 94 : io_stat->io_error->error_status[-io_status - 1]++;
7415 94 : spdk_spin_unlock(&bdev_io->bdev->internal.spinlock);
7416 94 : }
7417 :
7418 : #ifdef SPDK_CONFIG_VTUNE
7419 : uint64_t now_tsc = spdk_get_ticks();
7420 : if (now_tsc > (bdev_io->internal.ch->start_tsc + bdev_io->internal.ch->interval_tsc)) {
7421 : uint64_t data[5];
7422 : struct spdk_bdev_io_stat *prev_stat = bdev_io->internal.ch->prev_stat;
7423 :
7424 : data[0] = io_stat->num_read_ops - prev_stat->num_read_ops;
7425 : data[1] = io_stat->bytes_read - prev_stat->bytes_read;
7426 : data[2] = io_stat->num_write_ops - prev_stat->num_write_ops;
7427 : data[3] = io_stat->bytes_written - prev_stat->bytes_written;
7428 : data[4] = bdev_io->bdev->fn_table->get_spin_time ?
7429 : bdev_io->bdev->fn_table->get_spin_time(spdk_bdev_io_get_io_channel(bdev_io)) : 0;
7430 :
7431 : __itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->internal.ch->handle,
7432 : __itt_metadata_u64, 5, data);
7433 :
7434 : memcpy(prev_stat, io_stat, sizeof(struct spdk_bdev_io_stat));
7435 : bdev_io->internal.ch->start_tsc = now_tsc;
7436 : }
7437 : #endif
7438 610 : }
7439 :
7440 : static inline void
7441 610 : _bdev_io_complete(void *ctx)
7442 : {
7443 610 : struct spdk_bdev_io *bdev_io = ctx;
7444 :
7445 610 : if (spdk_unlikely(bdev_io_use_accel_sequence(bdev_io))) {
7446 0 : assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_SUCCESS);
7447 0 : spdk_accel_sequence_abort(bdev_io->internal.accel_sequence);
7448 0 : }
7449 :
7450 610 : assert(bdev_io->internal.cb != NULL);
7451 610 : assert(spdk_get_thread() == spdk_bdev_io_get_thread(bdev_io));
7452 :
7453 1220 : bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
7454 610 : bdev_io->internal.caller_ctx);
7455 610 : }
7456 :
7457 : static inline void
7458 618 : bdev_io_complete(void *ctx)
7459 : {
7460 618 : struct spdk_bdev_io *bdev_io = ctx;
7461 618 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
7462 : uint64_t tsc, tsc_diff;
7463 :
7464 618 : if (spdk_unlikely(bdev_io->internal.f.in_submit_request)) {
7465 : /*
7466 : * Defer completion to avoid potential infinite recursion if the
7467 : * user's completion callback issues a new I/O.
7468 : */
7469 16 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
7470 8 : bdev_io_complete, bdev_io);
7471 8 : return;
7472 : }
7473 :
7474 610 : tsc = spdk_get_ticks();
7475 610 : tsc_diff = tsc - bdev_io->internal.submit_tsc;
7476 :
7477 610 : bdev_ch_remove_from_io_submitted(bdev_io);
7478 610 : spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_DONE, bdev_ch->trace_id, 0, (uintptr_t)bdev_io,
7479 : bdev_io->internal.caller_ctx, bdev_ch->queue_depth);
7480 :
7481 610 : if (bdev_ch->histogram) {
7482 4 : if (bdev_io->bdev->internal.histogram_io_type == 0 ||
7483 0 : bdev_io->bdev->internal.histogram_io_type == bdev_io->type) {
7484 : /*
7485 : * Tally all I/O types if the histogram_io_type is set to 0.
7486 : */
7487 4 : spdk_histogram_data_tally(bdev_ch->histogram, tsc_diff);
7488 4 : }
7489 4 : }
7490 :
7491 610 : bdev_io_update_io_stat(bdev_io, tsc_diff);
7492 610 : _bdev_io_complete(bdev_io);
7493 618 : }
7494 :
7495 : /* The difference between this function and bdev_io_complete() is that this should be called to
7496 : * complete IOs that haven't been submitted via bdev_io_submit(), as they weren't added onto the
7497 : * io_submitted list and don't have submit_tsc updated.
7498 : */
7499 : static inline void
7500 0 : bdev_io_complete_unsubmitted(struct spdk_bdev_io *bdev_io)
7501 : {
7502 : /* Since the IO hasn't been submitted it's bound to be failed */
7503 0 : assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_SUCCESS);
7504 :
7505 : /* At this point we don't know if the IO is completed from submission context or not, but,
7506 : * since this is an error path, we can always do an spdk_thread_send_msg(). */
7507 0 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
7508 0 : _bdev_io_complete, bdev_io);
7509 0 : }
7510 :
7511 : static void bdev_destroy_cb(void *io_device);
7512 :
7513 : static void
7514 15 : bdev_reset_complete(struct spdk_bdev *bdev, void *_ctx, int status)
7515 : {
7516 15 : struct spdk_bdev_io *bdev_io = _ctx;
7517 :
7518 15 : if (bdev_io->u.reset.ch_ref != NULL) {
7519 15 : spdk_put_io_channel(bdev_io->u.reset.ch_ref);
7520 15 : bdev_io->u.reset.ch_ref = NULL;
7521 15 : }
7522 :
7523 15 : bdev_io_complete(bdev_io);
7524 :
7525 15 : if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING &&
7526 1 : TAILQ_EMPTY(&bdev->internal.open_descs)) {
7527 1 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
7528 1 : }
7529 15 : }
7530 :
7531 : static void
7532 18 : bdev_unfreeze_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
7533 : struct spdk_io_channel *_ch, void *_ctx)
7534 : {
7535 18 : struct spdk_bdev_io *bdev_io = _ctx;
7536 18 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
7537 : struct spdk_bdev_io *queued_reset;
7538 :
7539 18 : ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS;
7540 18 : while (!TAILQ_EMPTY(&ch->queued_resets)) {
7541 0 : queued_reset = TAILQ_FIRST(&ch->queued_resets);
7542 0 : TAILQ_REMOVE(&ch->queued_resets, queued_reset, internal.link);
7543 0 : spdk_bdev_io_complete(queued_reset, bdev_io->internal.status);
7544 : }
7545 :
7546 18 : spdk_bdev_for_each_channel_continue(i, 0);
7547 18 : }
7548 :
7549 : static void
7550 0 : bdev_io_complete_sequence_cb(void *ctx, int status)
7551 : {
7552 0 : struct spdk_bdev_io *bdev_io = ctx;
7553 :
7554 : /* u.bdev.accel_sequence should have already been cleared at this point */
7555 0 : assert(bdev_io->u.bdev.accel_sequence == NULL);
7556 0 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS);
7557 0 : bdev_io->internal.f.has_accel_sequence = false;
7558 :
7559 0 : if (spdk_unlikely(status != 0)) {
7560 0 : SPDK_ERRLOG("Failed to execute accel sequence, status=%d\n", status);
7561 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7562 0 : }
7563 :
7564 0 : bdev_io_complete(bdev_io);
7565 0 : }
7566 :
7567 : void
7568 598 : spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
7569 : {
7570 598 : struct spdk_bdev *bdev = bdev_io->bdev;
7571 598 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
7572 598 : struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
7573 :
7574 598 : if (spdk_unlikely(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING)) {
7575 0 : SPDK_ERRLOG("Unexpected completion on IO from %s module, status was %s\n",
7576 : spdk_bdev_get_module_name(bdev),
7577 : bdev_io_status_get_string(bdev_io->internal.status));
7578 0 : assert(false);
7579 : }
7580 598 : bdev_io->internal.status = status;
7581 :
7582 598 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) {
7583 16 : bool unlock_channels = false;
7584 :
7585 16 : if (status == SPDK_BDEV_IO_STATUS_NOMEM) {
7586 0 : SPDK_ERRLOG("NOMEM returned for reset\n");
7587 0 : }
7588 16 : spdk_spin_lock(&bdev->internal.spinlock);
7589 16 : if (bdev_io == bdev->internal.reset_in_progress) {
7590 15 : bdev->internal.reset_in_progress = NULL;
7591 15 : unlock_channels = true;
7592 15 : }
7593 16 : spdk_spin_unlock(&bdev->internal.spinlock);
7594 :
7595 16 : if (unlock_channels) {
7596 15 : spdk_bdev_for_each_channel(bdev, bdev_unfreeze_channel, bdev_io,
7597 : bdev_reset_complete);
7598 15 : return;
7599 : }
7600 1 : } else {
7601 582 : bdev_io_decrement_outstanding(bdev_ch, shared_resource);
7602 582 : if (spdk_likely(status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
7603 485 : if (bdev_io_needs_sequence_exec(bdev_io->internal.desc, bdev_io)) {
7604 0 : bdev_io_exec_sequence(bdev_io, bdev_io_complete_sequence_cb);
7605 0 : return;
7606 485 : } else if (spdk_unlikely(bdev_io->internal.f.has_bounce_buf &&
7607 : !bdev_io_use_accel_sequence(bdev_io))) {
7608 26 : _bdev_io_push_bounce_data_buffer(bdev_io,
7609 : _bdev_io_complete_push_bounce_done);
7610 : /* bdev IO will be completed in the callback */
7611 26 : return;
7612 : }
7613 459 : }
7614 :
7615 556 : if (spdk_unlikely(_bdev_io_handle_no_mem(bdev_io, BDEV_IO_RETRY_STATE_SUBMIT))) {
7616 5 : return;
7617 : }
7618 : }
7619 :
7620 552 : bdev_io_complete(bdev_io);
7621 598 : }
7622 :
7623 : void
7624 0 : spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc,
7625 : enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq)
7626 : {
7627 : enum spdk_bdev_io_status status;
7628 :
7629 0 : if (sc == SPDK_SCSI_STATUS_GOOD) {
7630 0 : status = SPDK_BDEV_IO_STATUS_SUCCESS;
7631 0 : } else {
7632 0 : status = SPDK_BDEV_IO_STATUS_SCSI_ERROR;
7633 0 : bdev_io->internal.error.scsi.sc = sc;
7634 0 : bdev_io->internal.error.scsi.sk = sk;
7635 0 : bdev_io->internal.error.scsi.asc = asc;
7636 0 : bdev_io->internal.error.scsi.ascq = ascq;
7637 : }
7638 :
7639 0 : spdk_bdev_io_complete(bdev_io, status);
7640 0 : }
7641 :
7642 : void
7643 0 : spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io,
7644 : int *sc, int *sk, int *asc, int *ascq)
7645 : {
7646 0 : assert(sc != NULL);
7647 0 : assert(sk != NULL);
7648 0 : assert(asc != NULL);
7649 0 : assert(ascq != NULL);
7650 :
7651 0 : switch (bdev_io->internal.status) {
7652 : case SPDK_BDEV_IO_STATUS_SUCCESS:
7653 0 : *sc = SPDK_SCSI_STATUS_GOOD;
7654 0 : *sk = SPDK_SCSI_SENSE_NO_SENSE;
7655 0 : *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
7656 0 : *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
7657 0 : break;
7658 : case SPDK_BDEV_IO_STATUS_NVME_ERROR:
7659 0 : spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq);
7660 0 : break;
7661 : case SPDK_BDEV_IO_STATUS_MISCOMPARE:
7662 0 : *sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
7663 0 : *sk = SPDK_SCSI_SENSE_MISCOMPARE;
7664 0 : *asc = SPDK_SCSI_ASC_MISCOMPARE_DURING_VERIFY_OPERATION;
7665 0 : *ascq = bdev_io->internal.error.scsi.ascq;
7666 0 : break;
7667 : case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
7668 0 : *sc = bdev_io->internal.error.scsi.sc;
7669 0 : *sk = bdev_io->internal.error.scsi.sk;
7670 0 : *asc = bdev_io->internal.error.scsi.asc;
7671 0 : *ascq = bdev_io->internal.error.scsi.ascq;
7672 0 : break;
7673 : default:
7674 0 : *sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
7675 0 : *sk = SPDK_SCSI_SENSE_ABORTED_COMMAND;
7676 0 : *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
7677 0 : *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
7678 0 : break;
7679 : }
7680 0 : }
7681 :
7682 : void
7683 0 : spdk_bdev_io_complete_aio_status(struct spdk_bdev_io *bdev_io, int aio_result)
7684 : {
7685 : enum spdk_bdev_io_status status;
7686 :
7687 0 : if (aio_result == 0) {
7688 0 : status = SPDK_BDEV_IO_STATUS_SUCCESS;
7689 0 : } else {
7690 0 : status = SPDK_BDEV_IO_STATUS_AIO_ERROR;
7691 : }
7692 :
7693 0 : bdev_io->internal.error.aio_result = aio_result;
7694 :
7695 0 : spdk_bdev_io_complete(bdev_io, status);
7696 0 : }
7697 :
7698 : void
7699 0 : spdk_bdev_io_get_aio_status(const struct spdk_bdev_io *bdev_io, int *aio_result)
7700 : {
7701 0 : assert(aio_result != NULL);
7702 :
7703 0 : if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_AIO_ERROR) {
7704 0 : *aio_result = bdev_io->internal.error.aio_result;
7705 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
7706 0 : *aio_result = 0;
7707 0 : } else {
7708 0 : *aio_result = -EIO;
7709 : }
7710 0 : }
7711 :
7712 : void
7713 0 : spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, uint32_t cdw0, int sct, int sc)
7714 : {
7715 : enum spdk_bdev_io_status status;
7716 :
7717 0 : if (spdk_likely(sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS)) {
7718 0 : status = SPDK_BDEV_IO_STATUS_SUCCESS;
7719 0 : } else if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_ABORTED_BY_REQUEST) {
7720 0 : status = SPDK_BDEV_IO_STATUS_ABORTED;
7721 0 : } else {
7722 0 : status = SPDK_BDEV_IO_STATUS_NVME_ERROR;
7723 : }
7724 :
7725 0 : bdev_io->internal.error.nvme.cdw0 = cdw0;
7726 0 : bdev_io->internal.error.nvme.sct = sct;
7727 0 : bdev_io->internal.error.nvme.sc = sc;
7728 :
7729 0 : spdk_bdev_io_complete(bdev_io, status);
7730 0 : }
7731 :
7732 : void
7733 0 : spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0, int *sct, int *sc)
7734 : {
7735 0 : assert(sct != NULL);
7736 0 : assert(sc != NULL);
7737 0 : assert(cdw0 != NULL);
7738 :
7739 0 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) {
7740 0 : *sct = SPDK_NVME_SCT_GENERIC;
7741 0 : *sc = SPDK_NVME_SC_SUCCESS;
7742 0 : if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
7743 0 : *cdw0 = 0;
7744 0 : } else {
7745 0 : *cdw0 = 1U;
7746 : }
7747 0 : return;
7748 : }
7749 :
7750 0 : if (spdk_likely(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
7751 0 : *sct = SPDK_NVME_SCT_GENERIC;
7752 0 : *sc = SPDK_NVME_SC_SUCCESS;
7753 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
7754 0 : *sct = bdev_io->internal.error.nvme.sct;
7755 0 : *sc = bdev_io->internal.error.nvme.sc;
7756 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) {
7757 0 : *sct = SPDK_NVME_SCT_GENERIC;
7758 0 : *sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
7759 0 : } else {
7760 0 : *sct = SPDK_NVME_SCT_GENERIC;
7761 0 : *sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
7762 : }
7763 :
7764 0 : *cdw0 = bdev_io->internal.error.nvme.cdw0;
7765 0 : }
7766 :
7767 : void
7768 0 : spdk_bdev_io_get_nvme_fused_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0,
7769 : int *first_sct, int *first_sc, int *second_sct, int *second_sc)
7770 : {
7771 0 : assert(first_sct != NULL);
7772 0 : assert(first_sc != NULL);
7773 0 : assert(second_sct != NULL);
7774 0 : assert(second_sc != NULL);
7775 0 : assert(cdw0 != NULL);
7776 :
7777 0 : if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
7778 0 : if (bdev_io->internal.error.nvme.sct == SPDK_NVME_SCT_MEDIA_ERROR &&
7779 0 : bdev_io->internal.error.nvme.sc == SPDK_NVME_SC_COMPARE_FAILURE) {
7780 0 : *first_sct = bdev_io->internal.error.nvme.sct;
7781 0 : *first_sc = bdev_io->internal.error.nvme.sc;
7782 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7783 0 : *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
7784 0 : } else {
7785 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7786 0 : *first_sc = SPDK_NVME_SC_SUCCESS;
7787 0 : *second_sct = bdev_io->internal.error.nvme.sct;
7788 0 : *second_sc = bdev_io->internal.error.nvme.sc;
7789 : }
7790 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) {
7791 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7792 0 : *first_sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
7793 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7794 0 : *second_sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
7795 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
7796 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7797 0 : *first_sc = SPDK_NVME_SC_SUCCESS;
7798 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7799 0 : *second_sc = SPDK_NVME_SC_SUCCESS;
7800 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED) {
7801 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7802 0 : *first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
7803 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7804 0 : *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
7805 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_MISCOMPARE) {
7806 0 : *first_sct = SPDK_NVME_SCT_MEDIA_ERROR;
7807 0 : *first_sc = SPDK_NVME_SC_COMPARE_FAILURE;
7808 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7809 0 : *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
7810 0 : } else {
7811 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7812 0 : *first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
7813 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7814 0 : *second_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
7815 : }
7816 :
7817 0 : *cdw0 = bdev_io->internal.error.nvme.cdw0;
7818 0 : }
7819 :
7820 : void
7821 0 : spdk_bdev_io_complete_base_io_status(struct spdk_bdev_io *bdev_io,
7822 : const struct spdk_bdev_io *base_io)
7823 : {
7824 0 : switch (base_io->internal.status) {
7825 : case SPDK_BDEV_IO_STATUS_NVME_ERROR:
7826 0 : spdk_bdev_io_complete_nvme_status(bdev_io,
7827 0 : base_io->internal.error.nvme.cdw0,
7828 0 : base_io->internal.error.nvme.sct,
7829 0 : base_io->internal.error.nvme.sc);
7830 0 : break;
7831 : case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
7832 0 : spdk_bdev_io_complete_scsi_status(bdev_io,
7833 0 : base_io->internal.error.scsi.sc,
7834 0 : base_io->internal.error.scsi.sk,
7835 0 : base_io->internal.error.scsi.asc,
7836 0 : base_io->internal.error.scsi.ascq);
7837 0 : break;
7838 : case SPDK_BDEV_IO_STATUS_AIO_ERROR:
7839 0 : spdk_bdev_io_complete_aio_status(bdev_io, base_io->internal.error.aio_result);
7840 0 : break;
7841 : default:
7842 0 : spdk_bdev_io_complete(bdev_io, base_io->internal.status);
7843 0 : break;
7844 : }
7845 0 : }
7846 :
7847 : struct spdk_thread *
7848 660 : spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io)
7849 : {
7850 660 : return spdk_io_channel_get_thread(bdev_io->internal.ch->channel);
7851 : }
7852 :
7853 : struct spdk_io_channel *
7854 70 : spdk_bdev_io_get_io_channel(struct spdk_bdev_io *bdev_io)
7855 : {
7856 70 : return bdev_io->internal.ch->channel;
7857 : }
7858 :
7859 : static int
7860 127 : bdev_register(struct spdk_bdev *bdev)
7861 : {
7862 : char *bdev_name;
7863 : char uuid[SPDK_UUID_STRING_LEN];
7864 : struct spdk_iobuf_opts iobuf_opts;
7865 : int ret;
7866 :
7867 127 : assert(bdev->module != NULL);
7868 :
7869 127 : if (!bdev->name) {
7870 0 : SPDK_ERRLOG("Bdev name is NULL\n");
7871 0 : return -EINVAL;
7872 : }
7873 :
7874 127 : if (!strlen(bdev->name)) {
7875 0 : SPDK_ERRLOG("Bdev name must not be an empty string\n");
7876 0 : return -EINVAL;
7877 : }
7878 :
7879 : /* Users often register their own I/O devices using the bdev name. In
7880 : * order to avoid conflicts, prepend bdev_. */
7881 127 : bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name);
7882 127 : if (!bdev_name) {
7883 0 : SPDK_ERRLOG("Unable to allocate memory for internal bdev name.\n");
7884 0 : return -ENOMEM;
7885 : }
7886 :
7887 127 : bdev->internal.stat = bdev_alloc_io_stat(true);
7888 127 : if (!bdev->internal.stat) {
7889 0 : SPDK_ERRLOG("Unable to allocate I/O statistics structure.\n");
7890 0 : free(bdev_name);
7891 0 : return -ENOMEM;
7892 : }
7893 :
7894 127 : bdev->internal.status = SPDK_BDEV_STATUS_READY;
7895 127 : bdev->internal.measured_queue_depth = UINT64_MAX;
7896 127 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
7897 127 : memset(&bdev->internal.claim, 0, sizeof(bdev->internal.claim));
7898 127 : bdev->internal.qd_poller = NULL;
7899 127 : bdev->internal.qos = NULL;
7900 :
7901 127 : TAILQ_INIT(&bdev->internal.open_descs);
7902 127 : TAILQ_INIT(&bdev->internal.locked_ranges);
7903 127 : TAILQ_INIT(&bdev->internal.pending_locked_ranges);
7904 127 : TAILQ_INIT(&bdev->aliases);
7905 :
7906 : /* UUID may be specified by the user or defined by bdev itself.
7907 : * Otherwise it will be generated here, so this field will never be empty. */
7908 127 : if (spdk_uuid_is_null(&bdev->uuid)) {
7909 42 : spdk_uuid_generate(&bdev->uuid);
7910 42 : }
7911 :
7912 : /* Add the UUID alias only if it's different than the name */
7913 127 : spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid);
7914 127 : if (strcmp(bdev->name, uuid) != 0) {
7915 126 : ret = spdk_bdev_alias_add(bdev, uuid);
7916 126 : if (ret != 0) {
7917 2 : SPDK_ERRLOG("Unable to add uuid:%s alias for bdev %s\n", uuid, bdev->name);
7918 2 : bdev_free_io_stat(bdev->internal.stat);
7919 2 : free(bdev_name);
7920 2 : return ret;
7921 : }
7922 124 : }
7923 :
7924 125 : spdk_iobuf_get_opts(&iobuf_opts, sizeof(iobuf_opts));
7925 125 : if (spdk_bdev_get_buf_align(bdev) > 1) {
7926 0 : bdev->max_rw_size = spdk_min(bdev->max_rw_size ? bdev->max_rw_size : UINT32_MAX,
7927 : iobuf_opts.large_bufsize / bdev->blocklen);
7928 0 : }
7929 :
7930 : /* If the user didn't specify a write unit size, set it to one. */
7931 125 : if (bdev->write_unit_size == 0) {
7932 121 : bdev->write_unit_size = 1;
7933 121 : }
7934 :
7935 : /* Set ACWU value to the write unit size if bdev module did not set it (does not support it natively) */
7936 125 : if (bdev->acwu == 0) {
7937 121 : bdev->acwu = bdev->write_unit_size;
7938 121 : }
7939 :
7940 125 : if (bdev->phys_blocklen == 0) {
7941 121 : bdev->phys_blocklen = spdk_bdev_get_data_block_size(bdev);
7942 121 : }
7943 :
7944 125 : if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COPY)) {
7945 0 : bdev->max_copy = bdev_get_max_write(bdev, iobuf_opts.large_bufsize);
7946 0 : }
7947 :
7948 125 : if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
7949 0 : bdev->max_write_zeroes = bdev_get_max_write(bdev, ZERO_BUFFER_SIZE);
7950 0 : }
7951 :
7952 125 : bdev->internal.reset_in_progress = NULL;
7953 125 : bdev->internal.qd_poll_in_progress = false;
7954 125 : bdev->internal.period = 0;
7955 125 : bdev->internal.new_period = 0;
7956 125 : bdev->internal.trace_id = spdk_trace_register_owner(OWNER_TYPE_BDEV, bdev_name);
7957 :
7958 : /*
7959 : * Initialize spinlock before registering IO device because spinlock is used in
7960 : * bdev_channel_create
7961 : */
7962 125 : spdk_spin_init(&bdev->internal.spinlock);
7963 :
7964 250 : spdk_io_device_register(__bdev_to_io_dev(bdev),
7965 : bdev_channel_create, bdev_channel_destroy,
7966 : sizeof(struct spdk_bdev_channel),
7967 125 : bdev_name);
7968 :
7969 : /*
7970 : * Register bdev name only after the bdev object is ready.
7971 : * After bdev_name_add returns, it is possible for other threads to start using the bdev,
7972 : * create IO channels...
7973 : */
7974 125 : ret = bdev_name_add(&bdev->internal.bdev_name, bdev, bdev->name);
7975 125 : if (ret != 0) {
7976 0 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), NULL);
7977 0 : bdev_free_io_stat(bdev->internal.stat);
7978 0 : spdk_spin_destroy(&bdev->internal.spinlock);
7979 0 : free(bdev_name);
7980 0 : return ret;
7981 : }
7982 :
7983 125 : free(bdev_name);
7984 :
7985 125 : SPDK_DEBUGLOG(bdev, "Inserting bdev %s into list\n", bdev->name);
7986 125 : TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, internal.link);
7987 :
7988 125 : return 0;
7989 127 : }
7990 :
7991 : static void
7992 126 : bdev_destroy_cb(void *io_device)
7993 : {
7994 : int rc;
7995 : struct spdk_bdev *bdev;
7996 : spdk_bdev_unregister_cb cb_fn;
7997 : void *cb_arg;
7998 :
7999 126 : bdev = __bdev_from_io_dev(io_device);
8000 :
8001 126 : if (bdev->internal.unregister_td != spdk_get_thread()) {
8002 1 : spdk_thread_send_msg(bdev->internal.unregister_td, bdev_destroy_cb, io_device);
8003 1 : return;
8004 : }
8005 :
8006 125 : cb_fn = bdev->internal.unregister_cb;
8007 125 : cb_arg = bdev->internal.unregister_ctx;
8008 :
8009 125 : spdk_spin_destroy(&bdev->internal.spinlock);
8010 125 : free(bdev->internal.qos);
8011 125 : bdev_free_io_stat(bdev->internal.stat);
8012 125 : spdk_trace_unregister_owner(bdev->internal.trace_id);
8013 :
8014 125 : rc = bdev->fn_table->destruct(bdev->ctxt);
8015 125 : if (rc < 0) {
8016 0 : SPDK_ERRLOG("destruct failed\n");
8017 0 : }
8018 125 : if (rc <= 0 && cb_fn != NULL) {
8019 10 : cb_fn(cb_arg, rc);
8020 10 : }
8021 126 : }
8022 :
8023 : void
8024 2 : spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
8025 : {
8026 2 : if (bdev->internal.unregister_cb != NULL) {
8027 0 : bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno);
8028 0 : }
8029 2 : }
8030 :
8031 : static void
8032 19 : _remove_notify(void *arg)
8033 : {
8034 19 : struct spdk_bdev_desc *desc = arg;
8035 :
8036 19 : _event_notify(desc, SPDK_BDEV_EVENT_REMOVE);
8037 19 : }
8038 :
8039 : /* returns: 0 - bdev removed and ready to be destructed.
8040 : * -EBUSY - bdev can't be destructed yet. */
8041 : static int
8042 140 : bdev_unregister_unsafe(struct spdk_bdev *bdev)
8043 : {
8044 : struct spdk_bdev_desc *desc, *tmp;
8045 140 : int rc = 0;
8046 : char uuid[SPDK_UUID_STRING_LEN];
8047 :
8048 140 : assert(spdk_spin_held(&g_bdev_mgr.spinlock));
8049 140 : assert(spdk_spin_held(&bdev->internal.spinlock));
8050 :
8051 : /* Notify each descriptor about hotremoval */
8052 159 : TAILQ_FOREACH_SAFE(desc, &bdev->internal.open_descs, link, tmp) {
8053 19 : rc = -EBUSY;
8054 : /*
8055 : * Defer invocation of the event_cb to a separate message that will
8056 : * run later on its thread. This ensures this context unwinds and
8057 : * we don't recursively unregister this bdev again if the event_cb
8058 : * immediately closes its descriptor.
8059 : */
8060 19 : event_notify(desc, _remove_notify);
8061 19 : }
8062 :
8063 : /* If there are no descriptors, proceed removing the bdev */
8064 140 : if (rc == 0) {
8065 125 : TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
8066 125 : SPDK_DEBUGLOG(bdev, "Removing bdev %s from list done\n", bdev->name);
8067 :
8068 : /* Delete the name and the UUID alias */
8069 125 : spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid);
8070 125 : bdev_name_del_unsafe(&bdev->internal.bdev_name);
8071 125 : bdev_alias_del(bdev, uuid, bdev_name_del_unsafe);
8072 :
8073 125 : spdk_notify_send("bdev_unregister", spdk_bdev_get_name(bdev));
8074 :
8075 125 : if (bdev->internal.reset_in_progress != NULL) {
8076 : /* If reset is in progress, let the completion callback for reset
8077 : * unregister the bdev.
8078 : */
8079 1 : rc = -EBUSY;
8080 1 : }
8081 125 : }
8082 :
8083 140 : return rc;
8084 : }
8085 :
8086 : static void
8087 4 : bdev_unregister_abort_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
8088 : struct spdk_io_channel *io_ch, void *_ctx)
8089 : {
8090 4 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
8091 :
8092 4 : bdev_channel_abort_queued_ios(bdev_ch);
8093 4 : spdk_bdev_for_each_channel_continue(i, 0);
8094 4 : }
8095 :
8096 : static void
8097 125 : bdev_unregister(struct spdk_bdev *bdev, void *_ctx, int status)
8098 : {
8099 : int rc;
8100 :
8101 125 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8102 125 : spdk_spin_lock(&bdev->internal.spinlock);
8103 : /*
8104 : * Set the status to REMOVING after completing to abort channels. Otherwise,
8105 : * the last spdk_bdev_close() may call spdk_io_device_unregister() while
8106 : * spdk_bdev_for_each_channel() is executed and spdk_io_device_unregister()
8107 : * may fail.
8108 : */
8109 125 : bdev->internal.status = SPDK_BDEV_STATUS_REMOVING;
8110 125 : rc = bdev_unregister_unsafe(bdev);
8111 125 : spdk_spin_unlock(&bdev->internal.spinlock);
8112 125 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8113 :
8114 125 : if (rc == 0) {
8115 109 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
8116 109 : }
8117 125 : }
8118 :
8119 : void
8120 132 : spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
8121 : {
8122 : struct spdk_thread *thread;
8123 :
8124 132 : SPDK_DEBUGLOG(bdev, "Removing bdev %s from list\n", bdev->name);
8125 :
8126 132 : thread = spdk_get_thread();
8127 132 : if (!thread) {
8128 : /* The user called this from a non-SPDK thread. */
8129 0 : if (cb_fn != NULL) {
8130 0 : cb_fn(cb_arg, -ENOTSUP);
8131 0 : }
8132 0 : return;
8133 : }
8134 :
8135 132 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8136 132 : if (bdev->internal.status == SPDK_BDEV_STATUS_UNREGISTERING ||
8137 132 : bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
8138 7 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8139 7 : if (cb_fn) {
8140 0 : cb_fn(cb_arg, -EBUSY);
8141 0 : }
8142 7 : return;
8143 : }
8144 :
8145 125 : spdk_spin_lock(&bdev->internal.spinlock);
8146 125 : bdev->internal.status = SPDK_BDEV_STATUS_UNREGISTERING;
8147 125 : bdev->internal.unregister_cb = cb_fn;
8148 125 : bdev->internal.unregister_ctx = cb_arg;
8149 125 : bdev->internal.unregister_td = thread;
8150 125 : spdk_spin_unlock(&bdev->internal.spinlock);
8151 125 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8152 :
8153 125 : spdk_bdev_set_qd_sampling_period(bdev, 0);
8154 :
8155 125 : spdk_bdev_for_each_channel(bdev, bdev_unregister_abort_channel, bdev,
8156 : bdev_unregister);
8157 132 : }
8158 :
8159 : int
8160 4 : spdk_bdev_unregister_by_name(const char *bdev_name, struct spdk_bdev_module *module,
8161 : spdk_bdev_unregister_cb cb_fn, void *cb_arg)
8162 : {
8163 : struct spdk_bdev_desc *desc;
8164 : struct spdk_bdev *bdev;
8165 : int rc;
8166 :
8167 4 : rc = spdk_bdev_open_ext(bdev_name, false, _tmp_bdev_event_cb, NULL, &desc);
8168 4 : if (rc != 0) {
8169 1 : SPDK_ERRLOG("Failed to open bdev with name: %s\n", bdev_name);
8170 1 : return rc;
8171 : }
8172 :
8173 3 : bdev = spdk_bdev_desc_get_bdev(desc);
8174 :
8175 3 : if (bdev->module != module) {
8176 1 : spdk_bdev_close(desc);
8177 1 : SPDK_ERRLOG("Bdev %s was not registered by the specified module.\n",
8178 : bdev_name);
8179 1 : return -ENODEV;
8180 : }
8181 :
8182 2 : spdk_bdev_unregister(bdev, cb_fn, cb_arg);
8183 :
8184 2 : spdk_bdev_close(desc);
8185 :
8186 2 : return 0;
8187 4 : }
8188 :
8189 : static int
8190 258 : bdev_start_qos(struct spdk_bdev *bdev)
8191 : {
8192 : struct set_qos_limit_ctx *ctx;
8193 :
8194 : /* Enable QoS */
8195 258 : if (bdev->internal.qos && bdev->internal.qos->thread == NULL) {
8196 2 : ctx = calloc(1, sizeof(*ctx));
8197 2 : if (ctx == NULL) {
8198 0 : SPDK_ERRLOG("Failed to allocate memory for QoS context\n");
8199 0 : return -ENOMEM;
8200 : }
8201 2 : ctx->bdev = bdev;
8202 2 : spdk_bdev_for_each_channel(bdev, bdev_enable_qos_msg, ctx, bdev_enable_qos_done);
8203 2 : }
8204 :
8205 258 : return 0;
8206 258 : }
8207 :
8208 : static void
8209 24 : log_already_claimed(enum spdk_log_level level, const int line, const char *func, const char *detail,
8210 : struct spdk_bdev *bdev)
8211 : {
8212 : enum spdk_bdev_claim_type type;
8213 : const char *typename, *modname;
8214 : extern struct spdk_log_flag SPDK_LOG_bdev;
8215 :
8216 24 : assert(spdk_spin_held(&bdev->internal.spinlock));
8217 :
8218 24 : if (level >= SPDK_LOG_INFO && !SPDK_LOG_bdev.enabled) {
8219 0 : return;
8220 : }
8221 :
8222 24 : type = bdev->internal.claim_type;
8223 24 : typename = spdk_bdev_claim_get_name(type);
8224 :
8225 24 : if (type == SPDK_BDEV_CLAIM_EXCL_WRITE) {
8226 6 : modname = bdev->internal.claim.v1.module->name;
8227 12 : spdk_log(level, __FILE__, line, func, "bdev %s %s: type %s by module %s\n",
8228 6 : bdev->name, detail, typename, modname);
8229 6 : return;
8230 : }
8231 :
8232 18 : if (claim_type_is_v2(type)) {
8233 : struct spdk_bdev_module_claim *claim;
8234 :
8235 36 : TAILQ_FOREACH(claim, &bdev->internal.claim.v2.claims, link) {
8236 18 : modname = claim->module->name;
8237 36 : spdk_log(level, __FILE__, line, func, "bdev %s %s: type %s by module %s\n",
8238 18 : bdev->name, detail, typename, modname);
8239 18 : }
8240 18 : return;
8241 : }
8242 :
8243 0 : assert(false);
8244 24 : }
8245 :
8246 : static int
8247 267 : bdev_open(struct spdk_bdev *bdev, bool write, struct spdk_bdev_desc *desc)
8248 : {
8249 : struct spdk_thread *thread;
8250 267 : int rc = 0;
8251 :
8252 267 : thread = spdk_get_thread();
8253 267 : if (!thread) {
8254 0 : SPDK_ERRLOG("Cannot open bdev from non-SPDK thread.\n");
8255 0 : return -ENOTSUP;
8256 : }
8257 :
8258 267 : SPDK_DEBUGLOG(bdev, "Opening descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
8259 : spdk_get_thread());
8260 :
8261 267 : desc->bdev = bdev;
8262 267 : desc->thread = thread;
8263 267 : desc->write = write;
8264 :
8265 267 : spdk_spin_lock(&bdev->internal.spinlock);
8266 267 : if (bdev->internal.status == SPDK_BDEV_STATUS_UNREGISTERING ||
8267 267 : bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
8268 3 : spdk_spin_unlock(&bdev->internal.spinlock);
8269 3 : return -ENODEV;
8270 : }
8271 :
8272 264 : if (write && bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
8273 6 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
8274 6 : spdk_spin_unlock(&bdev->internal.spinlock);
8275 6 : return -EPERM;
8276 : }
8277 :
8278 258 : rc = bdev_start_qos(bdev);
8279 258 : if (rc != 0) {
8280 0 : SPDK_ERRLOG("Failed to start QoS on bdev %s\n", bdev->name);
8281 0 : spdk_spin_unlock(&bdev->internal.spinlock);
8282 0 : return rc;
8283 : }
8284 :
8285 258 : TAILQ_INSERT_TAIL(&bdev->internal.open_descs, desc, link);
8286 :
8287 258 : spdk_spin_unlock(&bdev->internal.spinlock);
8288 :
8289 258 : return 0;
8290 267 : }
8291 :
8292 : static void
8293 268 : bdev_open_opts_get_defaults(struct spdk_bdev_open_opts *opts, size_t opts_size)
8294 : {
8295 268 : if (!opts) {
8296 0 : SPDK_ERRLOG("opts should not be NULL.\n");
8297 0 : return;
8298 : }
8299 :
8300 268 : if (!opts_size) {
8301 0 : SPDK_ERRLOG("opts_size should not be zero.\n");
8302 0 : return;
8303 : }
8304 :
8305 268 : memset(opts, 0, opts_size);
8306 268 : opts->size = opts_size;
8307 :
8308 : #define FIELD_OK(field) \
8309 : offsetof(struct spdk_bdev_open_opts, field) + sizeof(opts->field) <= opts_size
8310 :
8311 : #define SET_FIELD(field, value) \
8312 : if (FIELD_OK(field)) { \
8313 : opts->field = value; \
8314 : } \
8315 :
8316 268 : SET_FIELD(hide_metadata, false);
8317 :
8318 : #undef FIELD_OK
8319 : #undef SET_FIELD
8320 268 : }
8321 :
8322 : static void
8323 2 : bdev_open_opts_copy(struct spdk_bdev_open_opts *opts,
8324 : const struct spdk_bdev_open_opts *opts_src, size_t opts_size)
8325 : {
8326 2 : assert(opts);
8327 2 : assert(opts_src);
8328 :
8329 : #define SET_FIELD(field) \
8330 : if (offsetof(struct spdk_bdev_open_opts, field) + sizeof(opts->field) <= opts_size) { \
8331 : opts->field = opts_src->field; \
8332 : } \
8333 :
8334 2 : SET_FIELD(hide_metadata);
8335 :
8336 2 : opts->size = opts_src->size;
8337 :
8338 : /* We should not remove this statement, but need to update the assert statement
8339 : * if we add a new field, and also add a corresponding SET_FIELD statement.
8340 : */
8341 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_open_opts) == 16, "Incorrect size");
8342 :
8343 : #undef SET_FIELD
8344 2 : }
8345 :
8346 : void
8347 1 : spdk_bdev_open_opts_init(struct spdk_bdev_open_opts *opts, size_t opts_size)
8348 : {
8349 : struct spdk_bdev_open_opts opts_local;
8350 :
8351 1 : bdev_open_opts_get_defaults(&opts_local, sizeof(opts_local));
8352 1 : bdev_open_opts_copy(opts, &opts_local, opts_size);
8353 1 : }
8354 :
8355 : static int
8356 267 : bdev_desc_alloc(struct spdk_bdev *bdev, spdk_bdev_event_cb_t event_cb, void *event_ctx,
8357 : struct spdk_bdev_open_opts *user_opts, struct spdk_bdev_desc **_desc)
8358 : {
8359 : struct spdk_bdev_desc *desc;
8360 : struct spdk_bdev_open_opts opts;
8361 : unsigned int i;
8362 :
8363 267 : bdev_open_opts_get_defaults(&opts, sizeof(opts));
8364 267 : if (user_opts != NULL) {
8365 1 : bdev_open_opts_copy(&opts, user_opts, user_opts->size);
8366 1 : }
8367 :
8368 267 : desc = calloc(1, sizeof(*desc));
8369 267 : if (desc == NULL) {
8370 0 : SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n");
8371 0 : return -ENOMEM;
8372 : }
8373 :
8374 267 : desc->opts = opts;
8375 :
8376 267 : TAILQ_INIT(&desc->pending_media_events);
8377 267 : TAILQ_INIT(&desc->free_media_events);
8378 :
8379 267 : desc->memory_domains_supported = spdk_bdev_get_memory_domains(bdev, NULL, 0) > 0;
8380 267 : desc->callback.event_fn = event_cb;
8381 267 : desc->callback.ctx = event_ctx;
8382 267 : spdk_spin_init(&desc->spinlock);
8383 :
8384 267 : if (desc->opts.hide_metadata) {
8385 1 : if (spdk_bdev_is_md_separate(bdev)) {
8386 0 : SPDK_ERRLOG("hide_metadata option is not supported with separate metadata.\n");
8387 0 : bdev_desc_free(desc);
8388 0 : return -EINVAL;
8389 : }
8390 1 : }
8391 :
8392 267 : if (bdev->media_events) {
8393 0 : desc->media_events_buffer = calloc(MEDIA_EVENT_POOL_SIZE,
8394 : sizeof(*desc->media_events_buffer));
8395 0 : if (desc->media_events_buffer == NULL) {
8396 0 : SPDK_ERRLOG("Failed to initialize media event pool\n");
8397 0 : bdev_desc_free(desc);
8398 0 : return -ENOMEM;
8399 : }
8400 :
8401 0 : for (i = 0; i < MEDIA_EVENT_POOL_SIZE; ++i) {
8402 0 : TAILQ_INSERT_TAIL(&desc->free_media_events,
8403 : &desc->media_events_buffer[i], tailq);
8404 0 : }
8405 0 : }
8406 :
8407 267 : if (bdev->fn_table->accel_sequence_supported != NULL) {
8408 0 : for (i = 0; i < SPDK_BDEV_NUM_IO_TYPES; ++i) {
8409 0 : desc->accel_sequence_supported[i] =
8410 0 : bdev->fn_table->accel_sequence_supported(bdev->ctxt,
8411 0 : (enum spdk_bdev_io_type)i);
8412 0 : }
8413 0 : }
8414 :
8415 267 : *_desc = desc;
8416 :
8417 267 : return 0;
8418 267 : }
8419 :
8420 : static int
8421 130 : bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8422 : void *event_ctx, struct spdk_bdev_open_opts *opts,
8423 : struct spdk_bdev_desc **_desc)
8424 : {
8425 : struct spdk_bdev_desc *desc;
8426 : struct spdk_bdev *bdev;
8427 : int rc;
8428 :
8429 130 : bdev = bdev_get_by_name(bdev_name);
8430 :
8431 130 : if (bdev == NULL) {
8432 1 : SPDK_NOTICELOG("Currently unable to find bdev with name: %s\n", bdev_name);
8433 1 : return -ENODEV;
8434 : }
8435 :
8436 129 : rc = bdev_desc_alloc(bdev, event_cb, event_ctx, opts, &desc);
8437 129 : if (rc != 0) {
8438 0 : return rc;
8439 : }
8440 :
8441 129 : rc = bdev_open(bdev, write, desc);
8442 129 : if (rc != 0) {
8443 7 : bdev_desc_free(desc);
8444 7 : desc = NULL;
8445 7 : }
8446 :
8447 129 : *_desc = desc;
8448 :
8449 129 : return rc;
8450 130 : }
8451 :
8452 : int
8453 132 : spdk_bdev_open_ext_v2(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8454 : void *event_ctx, struct spdk_bdev_open_opts *opts,
8455 : struct spdk_bdev_desc **_desc)
8456 : {
8457 : int rc;
8458 :
8459 132 : if (event_cb == NULL) {
8460 2 : SPDK_ERRLOG("Missing event callback function\n");
8461 2 : return -EINVAL;
8462 : }
8463 :
8464 130 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8465 130 : rc = bdev_open_ext(bdev_name, write, event_cb, event_ctx, opts, _desc);
8466 130 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8467 :
8468 130 : return rc;
8469 132 : }
8470 :
8471 : int
8472 130 : spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8473 : void *event_ctx, struct spdk_bdev_desc **_desc)
8474 : {
8475 130 : return spdk_bdev_open_ext_v2(bdev_name, write, event_cb, event_ctx, NULL, _desc);
8476 : }
8477 :
8478 : struct spdk_bdev_open_async_ctx {
8479 : char *bdev_name;
8480 : spdk_bdev_event_cb_t event_cb;
8481 : void *event_ctx;
8482 : bool write;
8483 : int rc;
8484 : spdk_bdev_open_async_cb_t cb_fn;
8485 : void *cb_arg;
8486 : struct spdk_bdev_desc *desc;
8487 : struct spdk_bdev_open_async_opts opts;
8488 : uint64_t start_ticks;
8489 : struct spdk_thread *orig_thread;
8490 : struct spdk_poller *poller;
8491 : TAILQ_ENTRY(spdk_bdev_open_async_ctx) tailq;
8492 : };
8493 :
8494 : static void
8495 0 : bdev_open_async_done(void *arg)
8496 : {
8497 0 : struct spdk_bdev_open_async_ctx *ctx = arg;
8498 :
8499 0 : ctx->cb_fn(ctx->desc, ctx->rc, ctx->cb_arg);
8500 :
8501 0 : free(ctx->bdev_name);
8502 0 : free(ctx);
8503 0 : }
8504 :
8505 : static void
8506 0 : bdev_open_async_cancel(void *arg)
8507 : {
8508 0 : struct spdk_bdev_open_async_ctx *ctx = arg;
8509 :
8510 0 : assert(ctx->rc == -ESHUTDOWN);
8511 :
8512 0 : spdk_poller_unregister(&ctx->poller);
8513 :
8514 0 : bdev_open_async_done(ctx);
8515 0 : }
8516 :
8517 : /* This is called when the bdev library finishes at shutdown. */
8518 : static void
8519 65 : bdev_open_async_fini(void)
8520 : {
8521 : struct spdk_bdev_open_async_ctx *ctx, *tmp_ctx;
8522 :
8523 65 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8524 65 : TAILQ_FOREACH_SAFE(ctx, &g_bdev_mgr.async_bdev_opens, tailq, tmp_ctx) {
8525 0 : TAILQ_REMOVE(&g_bdev_mgr.async_bdev_opens, ctx, tailq);
8526 : /*
8527 : * We have to move to ctx->orig_thread to unregister ctx->poller.
8528 : * However, there is a chance that ctx->poller is executed before
8529 : * message is executed, which could result in bdev_open_async_done()
8530 : * being called twice. To avoid such race condition, set ctx->rc to
8531 : * -ESHUTDOWN.
8532 : */
8533 0 : ctx->rc = -ESHUTDOWN;
8534 0 : spdk_thread_send_msg(ctx->orig_thread, bdev_open_async_cancel, ctx);
8535 0 : }
8536 65 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8537 65 : }
8538 :
8539 : static int bdev_open_async(void *arg);
8540 :
8541 : static void
8542 0 : _bdev_open_async(struct spdk_bdev_open_async_ctx *ctx)
8543 : {
8544 : uint64_t timeout_ticks;
8545 :
8546 0 : if (ctx->rc == -ESHUTDOWN) {
8547 : /* This context is being canceled. Do nothing. */
8548 0 : return;
8549 : }
8550 :
8551 0 : ctx->rc = bdev_open_ext(ctx->bdev_name, ctx->write, ctx->event_cb, ctx->event_ctx,
8552 0 : NULL, &ctx->desc);
8553 0 : if (ctx->rc == 0 || ctx->opts.timeout_ms == 0) {
8554 0 : goto exit;
8555 : }
8556 :
8557 0 : timeout_ticks = ctx->start_ticks + ctx->opts.timeout_ms * spdk_get_ticks_hz() / 1000ull;
8558 0 : if (spdk_get_ticks() >= timeout_ticks) {
8559 0 : SPDK_ERRLOG("Timed out while waiting for bdev '%s' to appear\n", ctx->bdev_name);
8560 0 : ctx->rc = -ETIMEDOUT;
8561 0 : goto exit;
8562 : }
8563 :
8564 0 : return;
8565 :
8566 : exit:
8567 0 : spdk_poller_unregister(&ctx->poller);
8568 0 : TAILQ_REMOVE(&g_bdev_mgr.async_bdev_opens, ctx, tailq);
8569 :
8570 : /* Completion callback is processed after stack unwinding. */
8571 0 : spdk_thread_send_msg(ctx->orig_thread, bdev_open_async_done, ctx);
8572 0 : }
8573 :
8574 : static int
8575 0 : bdev_open_async(void *arg)
8576 : {
8577 0 : struct spdk_bdev_open_async_ctx *ctx = arg;
8578 :
8579 0 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8580 :
8581 0 : _bdev_open_async(ctx);
8582 :
8583 0 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8584 :
8585 0 : return SPDK_POLLER_BUSY;
8586 : }
8587 :
8588 : static void
8589 0 : bdev_open_async_opts_copy(struct spdk_bdev_open_async_opts *opts,
8590 : struct spdk_bdev_open_async_opts *opts_src,
8591 : size_t size)
8592 : {
8593 0 : assert(opts);
8594 0 : assert(opts_src);
8595 :
8596 0 : opts->size = size;
8597 :
8598 : #define SET_FIELD(field) \
8599 : if (offsetof(struct spdk_bdev_open_async_opts, field) + sizeof(opts->field) <= size) { \
8600 : opts->field = opts_src->field; \
8601 : } \
8602 :
8603 0 : SET_FIELD(timeout_ms);
8604 :
8605 : /* Do not remove this statement, you should always update this statement when you adding a new field,
8606 : * and do not forget to add the SET_FIELD statement for your added field. */
8607 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_open_async_opts) == 16, "Incorrect size");
8608 :
8609 : #undef SET_FIELD
8610 0 : }
8611 :
8612 : static void
8613 0 : bdev_open_async_opts_get_default(struct spdk_bdev_open_async_opts *opts, size_t size)
8614 : {
8615 0 : assert(opts);
8616 :
8617 0 : opts->size = size;
8618 :
8619 : #define SET_FIELD(field, value) \
8620 : if (offsetof(struct spdk_bdev_open_async_opts, field) + sizeof(opts->field) <= size) { \
8621 : opts->field = value; \
8622 : } \
8623 :
8624 0 : SET_FIELD(timeout_ms, 0);
8625 :
8626 : #undef SET_FIELD
8627 0 : }
8628 :
8629 : int
8630 0 : spdk_bdev_open_async(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8631 : void *event_ctx, struct spdk_bdev_open_async_opts *opts,
8632 : spdk_bdev_open_async_cb_t open_cb, void *open_cb_arg)
8633 : {
8634 : struct spdk_bdev_open_async_ctx *ctx;
8635 :
8636 0 : if (event_cb == NULL) {
8637 0 : SPDK_ERRLOG("Missing event callback function\n");
8638 0 : return -EINVAL;
8639 : }
8640 :
8641 0 : if (open_cb == NULL) {
8642 0 : SPDK_ERRLOG("Missing open callback function\n");
8643 0 : return -EINVAL;
8644 : }
8645 :
8646 0 : if (opts != NULL && opts->size == 0) {
8647 0 : SPDK_ERRLOG("size in the options structure should not be zero\n");
8648 0 : return -EINVAL;
8649 : }
8650 :
8651 0 : ctx = calloc(1, sizeof(*ctx));
8652 0 : if (ctx == NULL) {
8653 0 : SPDK_ERRLOG("Failed to allocate open context\n");
8654 0 : return -ENOMEM;
8655 : }
8656 :
8657 0 : ctx->bdev_name = strdup(bdev_name);
8658 0 : if (ctx->bdev_name == NULL) {
8659 0 : SPDK_ERRLOG("Failed to duplicate bdev_name\n");
8660 0 : free(ctx);
8661 0 : return -ENOMEM;
8662 : }
8663 :
8664 0 : ctx->poller = SPDK_POLLER_REGISTER(bdev_open_async, ctx, 100 * 1000);
8665 0 : if (ctx->poller == NULL) {
8666 0 : SPDK_ERRLOG("Failed to register bdev_open_async poller\n");
8667 0 : free(ctx->bdev_name);
8668 0 : free(ctx);
8669 0 : return -ENOMEM;
8670 : }
8671 :
8672 0 : ctx->cb_fn = open_cb;
8673 0 : ctx->cb_arg = open_cb_arg;
8674 0 : ctx->write = write;
8675 0 : ctx->event_cb = event_cb;
8676 0 : ctx->event_ctx = event_ctx;
8677 0 : ctx->orig_thread = spdk_get_thread();
8678 0 : ctx->start_ticks = spdk_get_ticks();
8679 :
8680 0 : bdev_open_async_opts_get_default(&ctx->opts, sizeof(ctx->opts));
8681 0 : if (opts != NULL) {
8682 0 : bdev_open_async_opts_copy(&ctx->opts, opts, opts->size);
8683 0 : }
8684 :
8685 0 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8686 :
8687 0 : TAILQ_INSERT_TAIL(&g_bdev_mgr.async_bdev_opens, ctx, tailq);
8688 0 : _bdev_open_async(ctx);
8689 :
8690 0 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8691 :
8692 0 : return 0;
8693 0 : }
8694 :
8695 : static void
8696 258 : bdev_close(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc)
8697 : {
8698 : int rc;
8699 :
8700 258 : spdk_spin_lock(&bdev->internal.spinlock);
8701 258 : spdk_spin_lock(&desc->spinlock);
8702 :
8703 258 : TAILQ_REMOVE(&bdev->internal.open_descs, desc, link);
8704 :
8705 258 : desc->closed = true;
8706 :
8707 258 : if (desc->claim != NULL) {
8708 16 : bdev_desc_release_claims(desc);
8709 16 : }
8710 :
8711 258 : if (0 == desc->refs) {
8712 247 : spdk_spin_unlock(&desc->spinlock);
8713 247 : bdev_desc_free(desc);
8714 247 : } else {
8715 11 : spdk_spin_unlock(&desc->spinlock);
8716 : }
8717 :
8718 : /* If no more descriptors, kill QoS channel */
8719 258 : if (bdev->internal.qos && TAILQ_EMPTY(&bdev->internal.open_descs)) {
8720 7 : SPDK_DEBUGLOG(bdev, "Closed last descriptor for bdev %s on thread %p. Stopping QoS.\n",
8721 : bdev->name, spdk_get_thread());
8722 :
8723 7 : if (bdev_qos_destroy(bdev)) {
8724 : /* There isn't anything we can do to recover here. Just let the
8725 : * old QoS poller keep running. The QoS handling won't change
8726 : * cores when the user allocates a new channel, but it won't break. */
8727 0 : SPDK_ERRLOG("Unable to shut down QoS poller. It will continue running on the current thread.\n");
8728 0 : }
8729 7 : }
8730 :
8731 258 : if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->internal.open_descs)) {
8732 15 : rc = bdev_unregister_unsafe(bdev);
8733 15 : spdk_spin_unlock(&bdev->internal.spinlock);
8734 :
8735 15 : if (rc == 0) {
8736 15 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
8737 15 : }
8738 15 : } else {
8739 243 : spdk_spin_unlock(&bdev->internal.spinlock);
8740 : }
8741 258 : }
8742 :
8743 : void
8744 122 : spdk_bdev_close(struct spdk_bdev_desc *desc)
8745 : {
8746 122 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
8747 :
8748 122 : SPDK_DEBUGLOG(bdev, "Closing descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
8749 : spdk_get_thread());
8750 :
8751 122 : assert(desc->thread == spdk_get_thread());
8752 :
8753 122 : spdk_poller_unregister(&desc->io_timeout_poller);
8754 :
8755 122 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8756 :
8757 122 : bdev_close(bdev, desc);
8758 :
8759 122 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8760 122 : }
8761 :
8762 : int32_t
8763 3 : spdk_bdev_get_numa_id(struct spdk_bdev *bdev)
8764 : {
8765 3 : if (bdev->numa.id_valid) {
8766 2 : return bdev->numa.id;
8767 : } else {
8768 1 : return SPDK_ENV_NUMA_ID_ANY;
8769 : }
8770 3 : }
8771 :
8772 : static void
8773 125 : bdev_register_finished(void *arg)
8774 : {
8775 125 : struct spdk_bdev_desc *desc = arg;
8776 125 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
8777 :
8778 125 : spdk_notify_send("bdev_register", spdk_bdev_get_name(bdev));
8779 :
8780 125 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8781 :
8782 125 : bdev_close(bdev, desc);
8783 :
8784 125 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8785 125 : }
8786 :
8787 : int
8788 128 : spdk_bdev_register(struct spdk_bdev *bdev)
8789 : {
8790 : struct spdk_bdev_desc *desc;
8791 128 : struct spdk_thread *thread = spdk_get_thread();
8792 : int rc;
8793 :
8794 128 : if (spdk_unlikely(!spdk_thread_is_app_thread(NULL))) {
8795 1 : SPDK_ERRLOG("Cannot register bdev %s on thread %p (%s)\n", bdev->name, thread,
8796 : thread ? spdk_thread_get_name(thread) : "null");
8797 1 : return -EINVAL;
8798 : }
8799 :
8800 127 : rc = bdev_register(bdev);
8801 127 : if (rc != 0) {
8802 2 : return rc;
8803 : }
8804 :
8805 : /* A descriptor is opened to prevent bdev deletion during examination */
8806 125 : rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, NULL, &desc);
8807 125 : if (rc != 0) {
8808 0 : spdk_bdev_unregister(bdev, NULL, NULL);
8809 0 : return rc;
8810 : }
8811 :
8812 125 : rc = bdev_open(bdev, false, desc);
8813 125 : if (rc != 0) {
8814 0 : bdev_desc_free(desc);
8815 0 : spdk_bdev_unregister(bdev, NULL, NULL);
8816 0 : return rc;
8817 : }
8818 :
8819 : /* Examine configuration before initializing I/O */
8820 125 : bdev_examine(bdev);
8821 :
8822 125 : rc = spdk_bdev_wait_for_examine(bdev_register_finished, desc);
8823 125 : if (rc != 0) {
8824 0 : bdev_close(bdev, desc);
8825 0 : spdk_bdev_unregister(bdev, NULL, NULL);
8826 0 : }
8827 :
8828 125 : return rc;
8829 128 : }
8830 :
8831 : int
8832 26 : spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
8833 : struct spdk_bdev_module *module)
8834 : {
8835 26 : spdk_spin_lock(&bdev->internal.spinlock);
8836 :
8837 26 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
8838 6 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
8839 6 : spdk_spin_unlock(&bdev->internal.spinlock);
8840 6 : return -EPERM;
8841 : }
8842 :
8843 20 : if (desc && !desc->write) {
8844 5 : desc->write = true;
8845 5 : }
8846 :
8847 20 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_EXCL_WRITE;
8848 20 : bdev->internal.claim.v1.module = module;
8849 :
8850 20 : spdk_spin_unlock(&bdev->internal.spinlock);
8851 20 : return 0;
8852 26 : }
8853 :
8854 : void
8855 8 : spdk_bdev_module_release_bdev(struct spdk_bdev *bdev)
8856 : {
8857 8 : spdk_spin_lock(&bdev->internal.spinlock);
8858 :
8859 8 : assert(bdev->internal.claim.v1.module != NULL);
8860 8 : assert(bdev->internal.claim_type == SPDK_BDEV_CLAIM_EXCL_WRITE);
8861 8 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
8862 8 : bdev->internal.claim.v1.module = NULL;
8863 :
8864 8 : spdk_spin_unlock(&bdev->internal.spinlock);
8865 8 : }
8866 :
8867 : /*
8868 : * Start claims v2
8869 : */
8870 :
8871 : const char *
8872 24 : spdk_bdev_claim_get_name(enum spdk_bdev_claim_type type)
8873 : {
8874 24 : switch (type) {
8875 : case SPDK_BDEV_CLAIM_NONE:
8876 0 : return "not_claimed";
8877 : case SPDK_BDEV_CLAIM_EXCL_WRITE:
8878 6 : return "exclusive_write";
8879 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
8880 7 : return "read_many_write_one";
8881 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE:
8882 5 : return "read_many_write_none";
8883 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
8884 6 : return "read_many_write_many";
8885 : default:
8886 0 : break;
8887 : }
8888 0 : return "invalid_claim";
8889 24 : }
8890 :
8891 : static bool
8892 96 : claim_type_is_v2(enum spdk_bdev_claim_type type)
8893 : {
8894 96 : switch (type) {
8895 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
8896 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE:
8897 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
8898 96 : return true;
8899 : default:
8900 0 : break;
8901 : }
8902 0 : return false;
8903 96 : }
8904 :
8905 : /* Returns true if taking a claim with desc->write == false should make the descriptor writable. */
8906 : static bool
8907 13 : claim_type_promotes_to_write(enum spdk_bdev_claim_type type)
8908 : {
8909 13 : switch (type) {
8910 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
8911 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
8912 5 : return true;
8913 : default:
8914 8 : break;
8915 : }
8916 8 : return false;
8917 13 : }
8918 :
8919 : void
8920 44 : spdk_bdev_claim_opts_init(struct spdk_bdev_claim_opts *opts, size_t size)
8921 : {
8922 44 : if (opts == NULL) {
8923 0 : SPDK_ERRLOG("opts should not be NULL\n");
8924 0 : assert(opts != NULL);
8925 0 : return;
8926 : }
8927 44 : if (size == 0) {
8928 0 : SPDK_ERRLOG("size should not be zero\n");
8929 0 : assert(size != 0);
8930 0 : return;
8931 : }
8932 :
8933 44 : memset(opts, 0, size);
8934 44 : opts->opts_size = size;
8935 :
8936 : #define FIELD_OK(field) \
8937 : offsetof(struct spdk_bdev_claim_opts, field) + sizeof(opts->field) <= size
8938 :
8939 : #define SET_FIELD(field, value) \
8940 : if (FIELD_OK(field)) { \
8941 : opts->field = value; \
8942 : } \
8943 :
8944 44 : SET_FIELD(shared_claim_key, 0);
8945 :
8946 : #undef FIELD_OK
8947 : #undef SET_FIELD
8948 44 : }
8949 :
8950 : static int
8951 22 : claim_opts_copy(struct spdk_bdev_claim_opts *src, struct spdk_bdev_claim_opts *dst)
8952 : {
8953 22 : if (src->opts_size == 0) {
8954 0 : SPDK_ERRLOG("size should not be zero\n");
8955 0 : return -1;
8956 : }
8957 :
8958 22 : memset(dst, 0, sizeof(*dst));
8959 22 : dst->opts_size = src->opts_size;
8960 :
8961 : #define FIELD_OK(field) \
8962 : offsetof(struct spdk_bdev_claim_opts, field) + sizeof(src->field) <= src->opts_size
8963 :
8964 : #define SET_FIELD(field) \
8965 : if (FIELD_OK(field)) { \
8966 : dst->field = src->field; \
8967 : } \
8968 :
8969 22 : if (FIELD_OK(name)) {
8970 22 : snprintf(dst->name, sizeof(dst->name), "%s", src->name);
8971 22 : }
8972 :
8973 22 : SET_FIELD(shared_claim_key);
8974 :
8975 : /* You should not remove this statement, but need to update the assert statement
8976 : * if you add a new field, and also add a corresponding SET_FIELD statement */
8977 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_claim_opts) == 48, "Incorrect size");
8978 :
8979 : #undef FIELD_OK
8980 : #undef SET_FIELD
8981 22 : return 0;
8982 22 : }
8983 :
8984 : /* Returns 0 if a read-write-once claim can be taken. */
8985 : static int
8986 9 : claim_verify_rwo(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
8987 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
8988 : {
8989 9 : struct spdk_bdev *bdev = desc->bdev;
8990 : struct spdk_bdev_desc *open_desc;
8991 :
8992 9 : assert(spdk_spin_held(&bdev->internal.spinlock));
8993 9 : assert(type == SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE);
8994 :
8995 9 : if (opts->shared_claim_key != 0) {
8996 1 : SPDK_ERRLOG("%s: key option not supported with read-write-once claims\n",
8997 : bdev->name);
8998 1 : return -EINVAL;
8999 : }
9000 8 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
9001 1 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9002 1 : return -EPERM;
9003 : }
9004 7 : if (desc->claim != NULL) {
9005 0 : SPDK_NOTICELOG("%s: descriptor already claimed bdev with module %s\n",
9006 : bdev->name, desc->claim->module->name);
9007 0 : return -EPERM;
9008 : }
9009 14 : TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
9010 9 : if (desc != open_desc && open_desc->write) {
9011 2 : SPDK_NOTICELOG("%s: Cannot obtain read-write-once claim while "
9012 : "another descriptor is open for writing\n",
9013 : bdev->name);
9014 2 : return -EPERM;
9015 : }
9016 7 : }
9017 :
9018 5 : return 0;
9019 9 : }
9020 :
9021 : /* Returns 0 if a read-only-many claim can be taken. */
9022 : static int
9023 12 : claim_verify_rom(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9024 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9025 : {
9026 12 : struct spdk_bdev *bdev = desc->bdev;
9027 : struct spdk_bdev_desc *open_desc;
9028 :
9029 12 : assert(spdk_spin_held(&bdev->internal.spinlock));
9030 12 : assert(type == SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE);
9031 12 : assert(desc->claim == NULL);
9032 :
9033 12 : if (desc->write) {
9034 3 : SPDK_ERRLOG("%s: Cannot obtain read-only-many claim with writable descriptor\n",
9035 : bdev->name);
9036 3 : return -EINVAL;
9037 : }
9038 9 : if (opts->shared_claim_key != 0) {
9039 1 : SPDK_ERRLOG("%s: key option not supported with read-only-may claims\n", bdev->name);
9040 1 : return -EINVAL;
9041 : }
9042 8 : if (bdev->internal.claim_type == SPDK_BDEV_CLAIM_NONE) {
9043 15 : TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
9044 9 : if (open_desc->write) {
9045 0 : SPDK_NOTICELOG("%s: Cannot obtain read-only-many claim while "
9046 : "another descriptor is open for writing\n",
9047 : bdev->name);
9048 0 : return -EPERM;
9049 : }
9050 9 : }
9051 6 : }
9052 :
9053 8 : return 0;
9054 12 : }
9055 :
9056 : /* Returns 0 if a read-write-many claim can be taken. */
9057 : static int
9058 8 : claim_verify_rwm(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9059 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9060 : {
9061 8 : struct spdk_bdev *bdev = desc->bdev;
9062 : struct spdk_bdev_desc *open_desc;
9063 :
9064 8 : assert(spdk_spin_held(&bdev->internal.spinlock));
9065 8 : assert(type == SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED);
9066 8 : assert(desc->claim == NULL);
9067 :
9068 8 : if (opts->shared_claim_key == 0) {
9069 2 : SPDK_ERRLOG("%s: shared_claim_key option required with read-write-may claims\n",
9070 : bdev->name);
9071 2 : return -EINVAL;
9072 : }
9073 6 : switch (bdev->internal.claim_type) {
9074 : case SPDK_BDEV_CLAIM_NONE:
9075 7 : TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
9076 5 : if (open_desc == desc) {
9077 3 : continue;
9078 : }
9079 2 : if (open_desc->write) {
9080 2 : SPDK_NOTICELOG("%s: Cannot obtain read-write-many claim while "
9081 : "another descriptor is open for writing without a "
9082 : "claim\n", bdev->name);
9083 2 : return -EPERM;
9084 : }
9085 0 : }
9086 2 : break;
9087 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9088 2 : if (opts->shared_claim_key != bdev->internal.claim.v2.key) {
9089 1 : LOG_ALREADY_CLAIMED_ERROR("already claimed with another key", bdev);
9090 1 : return -EPERM;
9091 : }
9092 1 : break;
9093 : default:
9094 0 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9095 0 : return -EBUSY;
9096 : }
9097 :
9098 3 : return 0;
9099 8 : }
9100 :
9101 : /* Updates desc and its bdev with a v2 claim. */
9102 : static int
9103 16 : claim_bdev(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9104 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9105 : {
9106 16 : struct spdk_bdev *bdev = desc->bdev;
9107 : struct spdk_bdev_module_claim *claim;
9108 :
9109 16 : assert(spdk_spin_held(&bdev->internal.spinlock));
9110 16 : assert(claim_type_is_v2(type));
9111 16 : assert(desc->claim == NULL);
9112 :
9113 16 : claim = calloc(1, sizeof(*desc->claim));
9114 16 : if (claim == NULL) {
9115 0 : SPDK_ERRLOG("%s: out of memory while allocating claim\n", bdev->name);
9116 0 : return -ENOMEM;
9117 : }
9118 16 : claim->module = module;
9119 16 : claim->desc = desc;
9120 : SPDK_STATIC_ASSERT(sizeof(claim->name) == sizeof(opts->name), "sizes must match");
9121 16 : memcpy(claim->name, opts->name, sizeof(claim->name));
9122 16 : desc->claim = claim;
9123 :
9124 16 : if (bdev->internal.claim_type == SPDK_BDEV_CLAIM_NONE) {
9125 13 : bdev->internal.claim_type = type;
9126 13 : TAILQ_INIT(&bdev->internal.claim.v2.claims);
9127 13 : bdev->internal.claim.v2.key = opts->shared_claim_key;
9128 13 : }
9129 16 : assert(type == bdev->internal.claim_type);
9130 :
9131 16 : TAILQ_INSERT_TAIL(&bdev->internal.claim.v2.claims, claim, link);
9132 :
9133 16 : if (!desc->write && claim_type_promotes_to_write(type)) {
9134 5 : desc->write = true;
9135 5 : }
9136 :
9137 16 : return 0;
9138 16 : }
9139 :
9140 : int
9141 39 : spdk_bdev_module_claim_bdev_desc(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9142 : struct spdk_bdev_claim_opts *_opts,
9143 : struct spdk_bdev_module *module)
9144 : {
9145 : struct spdk_bdev *bdev;
9146 : struct spdk_bdev_claim_opts opts;
9147 39 : int rc = 0;
9148 :
9149 39 : if (desc == NULL) {
9150 0 : SPDK_ERRLOG("descriptor must not be NULL\n");
9151 0 : return -EINVAL;
9152 : }
9153 :
9154 39 : bdev = desc->bdev;
9155 :
9156 39 : if (_opts == NULL) {
9157 17 : spdk_bdev_claim_opts_init(&opts, sizeof(opts));
9158 39 : } else if (claim_opts_copy(_opts, &opts) != 0) {
9159 0 : return -EINVAL;
9160 : }
9161 :
9162 39 : spdk_spin_lock(&bdev->internal.spinlock);
9163 :
9164 39 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE &&
9165 15 : bdev->internal.claim_type != type) {
9166 10 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9167 10 : spdk_spin_unlock(&bdev->internal.spinlock);
9168 10 : return -EPERM;
9169 : }
9170 :
9171 29 : if (claim_type_is_v2(type) && desc->claim != NULL) {
9172 0 : SPDK_ERRLOG("%s: descriptor already has %s claim with name '%s'\n",
9173 : bdev->name, spdk_bdev_claim_get_name(type), desc->claim->name);
9174 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9175 0 : return -EPERM;
9176 : }
9177 :
9178 29 : switch (type) {
9179 : case SPDK_BDEV_CLAIM_EXCL_WRITE:
9180 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9181 0 : return spdk_bdev_module_claim_bdev(bdev, desc, module);
9182 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
9183 9 : rc = claim_verify_rwo(desc, type, &opts, module);
9184 9 : break;
9185 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE:
9186 12 : rc = claim_verify_rom(desc, type, &opts, module);
9187 12 : break;
9188 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9189 8 : rc = claim_verify_rwm(desc, type, &opts, module);
9190 8 : break;
9191 : default:
9192 0 : SPDK_ERRLOG("%s: claim type %d not supported\n", bdev->name, type);
9193 0 : rc = -ENOTSUP;
9194 0 : }
9195 :
9196 29 : if (rc == 0) {
9197 16 : rc = claim_bdev(desc, type, &opts, module);
9198 16 : }
9199 :
9200 29 : spdk_spin_unlock(&bdev->internal.spinlock);
9201 29 : return rc;
9202 39 : }
9203 :
9204 : static void
9205 13 : claim_reset(struct spdk_bdev *bdev)
9206 : {
9207 13 : assert(spdk_spin_held(&bdev->internal.spinlock));
9208 13 : assert(claim_type_is_v2(bdev->internal.claim_type));
9209 13 : assert(TAILQ_EMPTY(&bdev->internal.claim.v2.claims));
9210 :
9211 13 : memset(&bdev->internal.claim, 0, sizeof(bdev->internal.claim));
9212 13 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
9213 13 : }
9214 :
9215 : static void
9216 16 : bdev_desc_release_claims(struct spdk_bdev_desc *desc)
9217 : {
9218 16 : struct spdk_bdev *bdev = desc->bdev;
9219 :
9220 16 : assert(spdk_spin_held(&bdev->internal.spinlock));
9221 16 : assert(claim_type_is_v2(bdev->internal.claim_type));
9222 :
9223 16 : if (bdev->internal.examine_in_progress == 0) {
9224 16 : TAILQ_REMOVE(&bdev->internal.claim.v2.claims, desc->claim, link);
9225 16 : free(desc->claim);
9226 16 : if (TAILQ_EMPTY(&bdev->internal.claim.v2.claims)) {
9227 13 : claim_reset(bdev);
9228 13 : }
9229 16 : } else {
9230 : /* This is a dead claim that will be cleaned up when bdev_examine() is done. */
9231 0 : desc->claim->module = NULL;
9232 0 : desc->claim->desc = NULL;
9233 : }
9234 16 : desc->claim = NULL;
9235 16 : }
9236 :
9237 : /*
9238 : * End claims v2
9239 : */
9240 :
9241 : struct spdk_bdev *
9242 1540 : spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
9243 : {
9244 1540 : assert(desc != NULL);
9245 1540 : return desc->bdev;
9246 : }
9247 :
9248 : int
9249 1 : spdk_for_each_bdev(void *ctx, spdk_for_each_bdev_fn fn)
9250 : {
9251 : struct spdk_bdev *bdev, *tmp;
9252 : struct spdk_bdev_desc *desc;
9253 1 : int rc = 0;
9254 :
9255 1 : assert(fn != NULL);
9256 :
9257 1 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9258 1 : bdev = spdk_bdev_first();
9259 9 : while (bdev != NULL) {
9260 8 : rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, NULL, &desc);
9261 8 : if (rc != 0) {
9262 0 : break;
9263 : }
9264 8 : rc = bdev_open(bdev, false, desc);
9265 8 : if (rc != 0) {
9266 1 : bdev_desc_free(desc);
9267 1 : if (rc == -ENODEV) {
9268 : /* Ignore the error and move to the next bdev. */
9269 1 : rc = 0;
9270 1 : bdev = spdk_bdev_next(bdev);
9271 1 : continue;
9272 : }
9273 0 : break;
9274 : }
9275 7 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9276 :
9277 7 : rc = fn(ctx, bdev);
9278 :
9279 7 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9280 7 : tmp = spdk_bdev_next(bdev);
9281 7 : bdev_close(bdev, desc);
9282 7 : if (rc != 0) {
9283 0 : break;
9284 : }
9285 7 : bdev = tmp;
9286 : }
9287 1 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9288 :
9289 1 : return rc;
9290 : }
9291 :
9292 : int
9293 1 : spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn)
9294 : {
9295 : struct spdk_bdev *bdev, *tmp;
9296 : struct spdk_bdev_desc *desc;
9297 1 : int rc = 0;
9298 :
9299 1 : assert(fn != NULL);
9300 :
9301 1 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9302 1 : bdev = spdk_bdev_first_leaf();
9303 6 : while (bdev != NULL) {
9304 5 : rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, NULL, &desc);
9305 5 : if (rc != 0) {
9306 0 : break;
9307 : }
9308 5 : rc = bdev_open(bdev, false, desc);
9309 5 : if (rc != 0) {
9310 1 : bdev_desc_free(desc);
9311 1 : if (rc == -ENODEV) {
9312 : /* Ignore the error and move to the next bdev. */
9313 1 : rc = 0;
9314 1 : bdev = spdk_bdev_next_leaf(bdev);
9315 1 : continue;
9316 : }
9317 0 : break;
9318 : }
9319 4 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9320 :
9321 4 : rc = fn(ctx, bdev);
9322 :
9323 4 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9324 4 : tmp = spdk_bdev_next_leaf(bdev);
9325 4 : bdev_close(bdev, desc);
9326 4 : if (rc != 0) {
9327 0 : break;
9328 : }
9329 4 : bdev = tmp;
9330 : }
9331 1 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9332 :
9333 1 : return rc;
9334 : }
9335 :
9336 : void
9337 0 : spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp)
9338 : {
9339 : struct iovec *iovs;
9340 : int iovcnt;
9341 :
9342 0 : if (bdev_io == NULL) {
9343 0 : return;
9344 : }
9345 :
9346 0 : switch (bdev_io->type) {
9347 : case SPDK_BDEV_IO_TYPE_READ:
9348 : case SPDK_BDEV_IO_TYPE_WRITE:
9349 : case SPDK_BDEV_IO_TYPE_ZCOPY:
9350 0 : iovs = bdev_io->u.bdev.iovs;
9351 0 : iovcnt = bdev_io->u.bdev.iovcnt;
9352 0 : break;
9353 : default:
9354 0 : iovs = NULL;
9355 0 : iovcnt = 0;
9356 0 : break;
9357 : }
9358 :
9359 0 : if (iovp) {
9360 0 : *iovp = iovs;
9361 0 : }
9362 0 : if (iovcntp) {
9363 0 : *iovcntp = iovcnt;
9364 0 : }
9365 0 : }
9366 :
9367 : void *
9368 0 : spdk_bdev_io_get_md_buf(struct spdk_bdev_io *bdev_io)
9369 : {
9370 0 : if (bdev_io == NULL) {
9371 0 : return NULL;
9372 : }
9373 :
9374 0 : if (!spdk_bdev_is_md_separate(bdev_io->bdev)) {
9375 0 : return NULL;
9376 : }
9377 :
9378 0 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ ||
9379 0 : bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
9380 0 : return bdev_io->u.bdev.md_buf;
9381 : }
9382 :
9383 0 : return NULL;
9384 0 : }
9385 :
9386 : void *
9387 0 : spdk_bdev_io_get_cb_arg(struct spdk_bdev_io *bdev_io)
9388 : {
9389 0 : if (bdev_io == NULL) {
9390 0 : assert(false);
9391 : return NULL;
9392 : }
9393 :
9394 0 : return bdev_io->internal.caller_ctx;
9395 : }
9396 :
9397 : void
9398 7 : spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
9399 : {
9400 :
9401 7 : if (spdk_bdev_module_list_find(bdev_module->name)) {
9402 0 : SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name);
9403 0 : assert(false);
9404 : }
9405 :
9406 7 : spdk_spin_init(&bdev_module->internal.spinlock);
9407 7 : TAILQ_INIT(&bdev_module->internal.quiesced_ranges);
9408 :
9409 : /*
9410 : * Modules with examine callbacks must be initialized first, so they are
9411 : * ready to handle examine callbacks from later modules that will
9412 : * register physical bdevs.
9413 : */
9414 7 : if (bdev_module->examine_config != NULL || bdev_module->examine_disk != NULL) {
9415 4 : TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
9416 4 : } else {
9417 3 : TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
9418 : }
9419 7 : }
9420 :
9421 : struct spdk_bdev_module *
9422 7 : spdk_bdev_module_list_find(const char *name)
9423 : {
9424 : struct spdk_bdev_module *bdev_module;
9425 :
9426 14 : TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
9427 7 : if (strcmp(name, bdev_module->name) == 0) {
9428 0 : break;
9429 : }
9430 7 : }
9431 :
9432 7 : return bdev_module;
9433 : }
9434 :
9435 : static int
9436 6 : bdev_write_zero_buffer(struct spdk_bdev_io *bdev_io)
9437 : {
9438 : uint64_t num_blocks;
9439 6 : void *md_buf = NULL;
9440 :
9441 6 : num_blocks = bdev_io->u.bdev.num_blocks;
9442 :
9443 6 : if (spdk_bdev_is_md_separate(bdev_io->bdev)) {
9444 4 : md_buf = (char *)g_bdev_mgr.zero_buffer +
9445 2 : spdk_bdev_get_block_size(bdev_io->bdev) * num_blocks;
9446 2 : }
9447 :
9448 12 : return bdev_write_blocks_with_md(bdev_io->internal.desc,
9449 6 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
9450 6 : g_bdev_mgr.zero_buffer, md_buf,
9451 6 : bdev_io->u.bdev.offset_blocks, num_blocks,
9452 6 : bdev_write_zero_buffer_done, bdev_io);
9453 : }
9454 :
9455 : static void
9456 6 : bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
9457 : {
9458 6 : struct spdk_bdev_io *parent_io = cb_arg;
9459 :
9460 6 : spdk_bdev_free_io(bdev_io);
9461 :
9462 6 : parent_io->internal.status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
9463 6 : parent_io->internal.cb(parent_io, success, parent_io->internal.caller_ctx);
9464 6 : }
9465 :
9466 : static void
9467 10 : bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status)
9468 : {
9469 10 : spdk_spin_lock(&ctx->bdev->internal.spinlock);
9470 10 : ctx->bdev->internal.qos_mod_in_progress = false;
9471 10 : spdk_spin_unlock(&ctx->bdev->internal.spinlock);
9472 :
9473 10 : if (ctx->cb_fn) {
9474 8 : ctx->cb_fn(ctx->cb_arg, status);
9475 8 : }
9476 10 : free(ctx);
9477 10 : }
9478 :
9479 : static void
9480 2 : bdev_disable_qos_done(void *cb_arg)
9481 : {
9482 2 : struct set_qos_limit_ctx *ctx = cb_arg;
9483 2 : struct spdk_bdev *bdev = ctx->bdev;
9484 : struct spdk_bdev_qos *qos;
9485 :
9486 2 : spdk_spin_lock(&bdev->internal.spinlock);
9487 2 : qos = bdev->internal.qos;
9488 2 : bdev->internal.qos = NULL;
9489 2 : spdk_spin_unlock(&bdev->internal.spinlock);
9490 :
9491 2 : if (qos->thread != NULL) {
9492 2 : spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
9493 2 : spdk_poller_unregister(&qos->poller);
9494 2 : }
9495 :
9496 2 : free(qos);
9497 :
9498 2 : bdev_set_qos_limit_done(ctx, 0);
9499 2 : }
9500 :
9501 : static void
9502 2 : bdev_disable_qos_msg_done(struct spdk_bdev *bdev, void *_ctx, int status)
9503 : {
9504 2 : struct set_qos_limit_ctx *ctx = _ctx;
9505 : struct spdk_thread *thread;
9506 :
9507 2 : spdk_spin_lock(&bdev->internal.spinlock);
9508 2 : thread = bdev->internal.qos->thread;
9509 2 : spdk_spin_unlock(&bdev->internal.spinlock);
9510 :
9511 2 : if (thread != NULL) {
9512 2 : spdk_thread_send_msg(thread, bdev_disable_qos_done, ctx);
9513 2 : } else {
9514 0 : bdev_disable_qos_done(ctx);
9515 : }
9516 2 : }
9517 :
9518 : static void
9519 4 : bdev_disable_qos_msg(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9520 : struct spdk_io_channel *ch, void *_ctx)
9521 : {
9522 4 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(ch);
9523 : struct spdk_bdev_io *bdev_io;
9524 :
9525 4 : bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED;
9526 :
9527 6 : while (!TAILQ_EMPTY(&bdev_ch->qos_queued_io)) {
9528 : /* Re-submit the queued I/O. */
9529 2 : bdev_io = TAILQ_FIRST(&bdev_ch->qos_queued_io);
9530 2 : TAILQ_REMOVE(&bdev_ch->qos_queued_io, bdev_io, internal.link);
9531 2 : _bdev_io_submit(bdev_io);
9532 : }
9533 :
9534 4 : spdk_bdev_for_each_channel_continue(i, 0);
9535 4 : }
9536 :
9537 : static void
9538 1 : bdev_update_qos_rate_limit_msg(void *cb_arg)
9539 : {
9540 1 : struct set_qos_limit_ctx *ctx = cb_arg;
9541 1 : struct spdk_bdev *bdev = ctx->bdev;
9542 :
9543 1 : spdk_spin_lock(&bdev->internal.spinlock);
9544 1 : bdev_qos_update_max_quota_per_timeslice(bdev->internal.qos);
9545 1 : spdk_spin_unlock(&bdev->internal.spinlock);
9546 :
9547 1 : bdev_set_qos_limit_done(ctx, 0);
9548 1 : }
9549 :
9550 : static void
9551 9 : bdev_enable_qos_msg(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9552 : struct spdk_io_channel *ch, void *_ctx)
9553 : {
9554 9 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(ch);
9555 :
9556 9 : spdk_spin_lock(&bdev->internal.spinlock);
9557 9 : bdev_enable_qos(bdev, bdev_ch);
9558 9 : spdk_spin_unlock(&bdev->internal.spinlock);
9559 9 : spdk_bdev_for_each_channel_continue(i, 0);
9560 9 : }
9561 :
9562 : static void
9563 6 : bdev_enable_qos_done(struct spdk_bdev *bdev, void *_ctx, int status)
9564 : {
9565 6 : struct set_qos_limit_ctx *ctx = _ctx;
9566 :
9567 6 : bdev_set_qos_limit_done(ctx, status);
9568 6 : }
9569 :
9570 : static void
9571 7 : bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
9572 : {
9573 : int i;
9574 :
9575 7 : assert(bdev->internal.qos != NULL);
9576 :
9577 35 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
9578 28 : if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
9579 28 : bdev->internal.qos->rate_limits[i].limit = limits[i];
9580 :
9581 28 : if (limits[i] == 0) {
9582 19 : bdev->internal.qos->rate_limits[i].limit =
9583 : SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
9584 19 : }
9585 28 : }
9586 28 : }
9587 7 : }
9588 :
9589 : void
9590 9 : spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits,
9591 : void (*cb_fn)(void *cb_arg, int status), void *cb_arg)
9592 : {
9593 : struct set_qos_limit_ctx *ctx;
9594 : uint32_t limit_set_complement;
9595 : uint64_t min_limit_per_sec;
9596 : int i;
9597 9 : bool disable_rate_limit = true;
9598 :
9599 45 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
9600 36 : if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
9601 0 : continue;
9602 : }
9603 :
9604 36 : if (limits[i] > 0) {
9605 10 : disable_rate_limit = false;
9606 10 : }
9607 :
9608 36 : if (bdev_qos_is_iops_rate_limit(i) == true) {
9609 9 : min_limit_per_sec = SPDK_BDEV_QOS_MIN_IOS_PER_SEC;
9610 9 : } else {
9611 27 : if (limits[i] > SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC) {
9612 0 : SPDK_WARNLOG("Requested rate limit %" PRIu64 " will result in uint64_t overflow, "
9613 : "reset to %" PRIu64 "\n", limits[i], SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC);
9614 0 : limits[i] = SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC;
9615 0 : }
9616 : /* Change from megabyte to byte rate limit */
9617 27 : limits[i] = limits[i] * 1024 * 1024;
9618 27 : min_limit_per_sec = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC;
9619 : }
9620 :
9621 36 : limit_set_complement = limits[i] % min_limit_per_sec;
9622 36 : if (limit_set_complement) {
9623 0 : SPDK_ERRLOG("Requested rate limit %" PRIu64 " is not a multiple of %" PRIu64 "\n",
9624 : limits[i], min_limit_per_sec);
9625 0 : limits[i] += min_limit_per_sec - limit_set_complement;
9626 0 : SPDK_ERRLOG("Round up the rate limit to %" PRIu64 "\n", limits[i]);
9627 0 : }
9628 36 : }
9629 :
9630 9 : ctx = calloc(1, sizeof(*ctx));
9631 9 : if (ctx == NULL) {
9632 0 : cb_fn(cb_arg, -ENOMEM);
9633 0 : return;
9634 : }
9635 :
9636 9 : ctx->cb_fn = cb_fn;
9637 9 : ctx->cb_arg = cb_arg;
9638 9 : ctx->bdev = bdev;
9639 :
9640 9 : spdk_spin_lock(&bdev->internal.spinlock);
9641 9 : if (bdev->internal.qos_mod_in_progress) {
9642 1 : spdk_spin_unlock(&bdev->internal.spinlock);
9643 1 : free(ctx);
9644 1 : cb_fn(cb_arg, -EAGAIN);
9645 1 : return;
9646 : }
9647 8 : bdev->internal.qos_mod_in_progress = true;
9648 :
9649 8 : if (disable_rate_limit == true && bdev->internal.qos) {
9650 10 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
9651 8 : if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED &&
9652 0 : (bdev->internal.qos->rate_limits[i].limit > 0 &&
9653 0 : bdev->internal.qos->rate_limits[i].limit !=
9654 : SPDK_BDEV_QOS_LIMIT_NOT_DEFINED)) {
9655 0 : disable_rate_limit = false;
9656 0 : break;
9657 : }
9658 8 : }
9659 2 : }
9660 :
9661 8 : if (disable_rate_limit == false) {
9662 5 : if (bdev->internal.qos == NULL) {
9663 4 : bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
9664 4 : if (!bdev->internal.qos) {
9665 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9666 0 : SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
9667 0 : bdev_set_qos_limit_done(ctx, -ENOMEM);
9668 0 : return;
9669 : }
9670 4 : }
9671 :
9672 5 : if (bdev->internal.qos->thread == NULL) {
9673 : /* Enabling */
9674 4 : bdev_set_qos_rate_limits(bdev, limits);
9675 :
9676 4 : spdk_bdev_for_each_channel(bdev, bdev_enable_qos_msg, ctx,
9677 : bdev_enable_qos_done);
9678 4 : } else {
9679 : /* Updating */
9680 1 : bdev_set_qos_rate_limits(bdev, limits);
9681 :
9682 2 : spdk_thread_send_msg(bdev->internal.qos->thread,
9683 1 : bdev_update_qos_rate_limit_msg, ctx);
9684 : }
9685 5 : } else {
9686 3 : if (bdev->internal.qos != NULL) {
9687 2 : bdev_set_qos_rate_limits(bdev, limits);
9688 :
9689 : /* Disabling */
9690 2 : spdk_bdev_for_each_channel(bdev, bdev_disable_qos_msg, ctx,
9691 : bdev_disable_qos_msg_done);
9692 2 : } else {
9693 1 : spdk_spin_unlock(&bdev->internal.spinlock);
9694 1 : bdev_set_qos_limit_done(ctx, 0);
9695 1 : return;
9696 : }
9697 : }
9698 :
9699 7 : spdk_spin_unlock(&bdev->internal.spinlock);
9700 9 : }
9701 :
9702 : struct spdk_bdev_histogram_ctx {
9703 : spdk_bdev_histogram_status_cb cb_fn;
9704 : void *cb_arg;
9705 : struct spdk_bdev *bdev;
9706 : int status;
9707 : };
9708 :
9709 : static void
9710 2 : bdev_histogram_disable_channel_cb(struct spdk_bdev *bdev, void *_ctx, int status)
9711 : {
9712 2 : struct spdk_bdev_histogram_ctx *ctx = _ctx;
9713 :
9714 2 : spdk_spin_lock(&ctx->bdev->internal.spinlock);
9715 2 : ctx->bdev->internal.histogram_in_progress = false;
9716 2 : spdk_spin_unlock(&ctx->bdev->internal.spinlock);
9717 2 : ctx->cb_fn(ctx->cb_arg, ctx->status);
9718 2 : free(ctx);
9719 2 : }
9720 :
9721 : static void
9722 3 : bdev_histogram_disable_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9723 : struct spdk_io_channel *_ch, void *_ctx)
9724 : {
9725 3 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
9726 :
9727 3 : if (ch->histogram != NULL) {
9728 3 : spdk_histogram_data_free(ch->histogram);
9729 3 : ch->histogram = NULL;
9730 3 : }
9731 3 : spdk_bdev_for_each_channel_continue(i, 0);
9732 3 : }
9733 :
9734 : static void
9735 2 : bdev_histogram_enable_channel_cb(struct spdk_bdev *bdev, void *_ctx, int status)
9736 : {
9737 2 : struct spdk_bdev_histogram_ctx *ctx = _ctx;
9738 :
9739 2 : if (status != 0) {
9740 0 : ctx->status = status;
9741 0 : ctx->bdev->internal.histogram_enabled = false;
9742 0 : spdk_bdev_for_each_channel(ctx->bdev, bdev_histogram_disable_channel, ctx,
9743 : bdev_histogram_disable_channel_cb);
9744 0 : } else {
9745 2 : spdk_spin_lock(&ctx->bdev->internal.spinlock);
9746 2 : ctx->bdev->internal.histogram_in_progress = false;
9747 2 : spdk_spin_unlock(&ctx->bdev->internal.spinlock);
9748 2 : ctx->cb_fn(ctx->cb_arg, ctx->status);
9749 2 : free(ctx);
9750 : }
9751 2 : }
9752 :
9753 : static void
9754 3 : bdev_histogram_enable_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9755 : struct spdk_io_channel *_ch, void *_ctx)
9756 : {
9757 3 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
9758 3 : int status = 0;
9759 :
9760 3 : if (ch->histogram == NULL) {
9761 3 : ch->histogram = spdk_histogram_data_alloc();
9762 3 : if (ch->histogram == NULL) {
9763 0 : status = -ENOMEM;
9764 0 : }
9765 3 : }
9766 :
9767 3 : spdk_bdev_for_each_channel_continue(i, status);
9768 3 : }
9769 :
9770 : void
9771 4 : spdk_bdev_histogram_enable_ext(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn,
9772 : void *cb_arg, bool enable, struct spdk_bdev_enable_histogram_opts *opts)
9773 : {
9774 : struct spdk_bdev_histogram_ctx *ctx;
9775 :
9776 4 : ctx = calloc(1, sizeof(struct spdk_bdev_histogram_ctx));
9777 4 : if (ctx == NULL) {
9778 0 : cb_fn(cb_arg, -ENOMEM);
9779 0 : return;
9780 : }
9781 :
9782 4 : ctx->bdev = bdev;
9783 4 : ctx->status = 0;
9784 4 : ctx->cb_fn = cb_fn;
9785 4 : ctx->cb_arg = cb_arg;
9786 :
9787 4 : spdk_spin_lock(&bdev->internal.spinlock);
9788 4 : if (bdev->internal.histogram_in_progress) {
9789 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9790 0 : free(ctx);
9791 0 : cb_fn(cb_arg, -EAGAIN);
9792 0 : return;
9793 : }
9794 :
9795 4 : bdev->internal.histogram_in_progress = true;
9796 4 : spdk_spin_unlock(&bdev->internal.spinlock);
9797 :
9798 4 : bdev->internal.histogram_enabled = enable;
9799 4 : bdev->internal.histogram_io_type = opts->io_type;
9800 :
9801 4 : if (enable) {
9802 : /* Allocate histogram for each channel */
9803 2 : spdk_bdev_for_each_channel(bdev, bdev_histogram_enable_channel, ctx,
9804 : bdev_histogram_enable_channel_cb);
9805 2 : } else {
9806 2 : spdk_bdev_for_each_channel(bdev, bdev_histogram_disable_channel, ctx,
9807 : bdev_histogram_disable_channel_cb);
9808 : }
9809 4 : }
9810 :
9811 : void
9812 4 : spdk_bdev_enable_histogram_opts_init(struct spdk_bdev_enable_histogram_opts *opts, size_t size)
9813 : {
9814 4 : if (opts == NULL) {
9815 0 : SPDK_ERRLOG("opts should not be NULL\n");
9816 0 : assert(opts != NULL);
9817 0 : return;
9818 : }
9819 4 : if (size == 0) {
9820 0 : SPDK_ERRLOG("size should not be zero\n");
9821 0 : assert(size != 0);
9822 0 : return;
9823 : }
9824 :
9825 4 : memset(opts, 0, size);
9826 4 : opts->size = size;
9827 :
9828 : #define FIELD_OK(field) \
9829 : offsetof(struct spdk_bdev_enable_histogram_opts, field) + sizeof(opts->field) <= size
9830 :
9831 : #define SET_FIELD(field, value) \
9832 : if (FIELD_OK(field)) { \
9833 : opts->field = value; \
9834 : } \
9835 :
9836 4 : SET_FIELD(io_type, 0);
9837 :
9838 : /* You should not remove this statement, but need to update the assert statement
9839 : * if you add a new field, and also add a corresponding SET_FIELD statement */
9840 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_enable_histogram_opts) == 9, "Incorrect size");
9841 :
9842 : #undef FIELD_OK
9843 : #undef SET_FIELD
9844 4 : }
9845 :
9846 : void
9847 4 : spdk_bdev_histogram_enable(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn,
9848 : void *cb_arg, bool enable)
9849 : {
9850 : struct spdk_bdev_enable_histogram_opts opts;
9851 :
9852 4 : spdk_bdev_enable_histogram_opts_init(&opts, sizeof(opts));
9853 4 : spdk_bdev_histogram_enable_ext(bdev, cb_fn, cb_arg, enable, &opts);
9854 4 : }
9855 :
9856 : struct spdk_bdev_histogram_data_ctx {
9857 : spdk_bdev_histogram_data_cb cb_fn;
9858 : void *cb_arg;
9859 : struct spdk_bdev *bdev;
9860 : /** merged histogram data from all channels */
9861 : struct spdk_histogram_data *histogram;
9862 : };
9863 :
9864 : static void
9865 5 : bdev_histogram_get_channel_cb(struct spdk_bdev *bdev, void *_ctx, int status)
9866 : {
9867 5 : struct spdk_bdev_histogram_data_ctx *ctx = _ctx;
9868 :
9869 5 : ctx->cb_fn(ctx->cb_arg, status, ctx->histogram);
9870 5 : free(ctx);
9871 5 : }
9872 :
9873 : static void
9874 7 : bdev_histogram_get_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9875 : struct spdk_io_channel *_ch, void *_ctx)
9876 : {
9877 7 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
9878 7 : struct spdk_bdev_histogram_data_ctx *ctx = _ctx;
9879 7 : int status = 0;
9880 :
9881 7 : if (ch->histogram == NULL) {
9882 1 : status = -EFAULT;
9883 1 : } else {
9884 6 : spdk_histogram_data_merge(ctx->histogram, ch->histogram);
9885 : }
9886 :
9887 7 : spdk_bdev_for_each_channel_continue(i, status);
9888 7 : }
9889 :
9890 : void
9891 5 : spdk_bdev_histogram_get(struct spdk_bdev *bdev, struct spdk_histogram_data *histogram,
9892 : spdk_bdev_histogram_data_cb cb_fn,
9893 : void *cb_arg)
9894 : {
9895 : struct spdk_bdev_histogram_data_ctx *ctx;
9896 :
9897 5 : ctx = calloc(1, sizeof(struct spdk_bdev_histogram_data_ctx));
9898 5 : if (ctx == NULL) {
9899 0 : cb_fn(cb_arg, -ENOMEM, NULL);
9900 0 : return;
9901 : }
9902 :
9903 5 : ctx->bdev = bdev;
9904 5 : ctx->cb_fn = cb_fn;
9905 5 : ctx->cb_arg = cb_arg;
9906 :
9907 5 : ctx->histogram = histogram;
9908 :
9909 5 : spdk_bdev_for_each_channel(bdev, bdev_histogram_get_channel, ctx,
9910 : bdev_histogram_get_channel_cb);
9911 5 : }
9912 :
9913 : void
9914 2 : spdk_bdev_channel_get_histogram(struct spdk_io_channel *ch, spdk_bdev_histogram_data_cb cb_fn,
9915 : void *cb_arg)
9916 : {
9917 2 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(ch);
9918 2 : int status = 0;
9919 :
9920 2 : assert(cb_fn != NULL);
9921 :
9922 2 : if (bdev_ch->histogram == NULL) {
9923 1 : status = -EFAULT;
9924 1 : }
9925 2 : cb_fn(cb_arg, status, bdev_ch->histogram);
9926 2 : }
9927 :
9928 : size_t
9929 0 : spdk_bdev_get_media_events(struct spdk_bdev_desc *desc, struct spdk_bdev_media_event *events,
9930 : size_t max_events)
9931 : {
9932 : struct media_event_entry *entry;
9933 0 : size_t num_events = 0;
9934 :
9935 0 : for (; num_events < max_events; ++num_events) {
9936 0 : entry = TAILQ_FIRST(&desc->pending_media_events);
9937 0 : if (entry == NULL) {
9938 0 : break;
9939 : }
9940 :
9941 0 : events[num_events] = entry->event;
9942 0 : TAILQ_REMOVE(&desc->pending_media_events, entry, tailq);
9943 0 : TAILQ_INSERT_TAIL(&desc->free_media_events, entry, tailq);
9944 0 : }
9945 :
9946 0 : return num_events;
9947 : }
9948 :
9949 : int
9950 0 : spdk_bdev_push_media_events(struct spdk_bdev *bdev, const struct spdk_bdev_media_event *events,
9951 : size_t num_events)
9952 : {
9953 : struct spdk_bdev_desc *desc;
9954 : struct media_event_entry *entry;
9955 : size_t event_id;
9956 0 : int rc = 0;
9957 :
9958 0 : assert(bdev->media_events);
9959 :
9960 0 : spdk_spin_lock(&bdev->internal.spinlock);
9961 0 : TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
9962 0 : if (desc->write) {
9963 0 : break;
9964 : }
9965 0 : }
9966 :
9967 0 : if (desc == NULL || desc->media_events_buffer == NULL) {
9968 0 : rc = -ENODEV;
9969 0 : goto out;
9970 : }
9971 :
9972 0 : for (event_id = 0; event_id < num_events; ++event_id) {
9973 0 : entry = TAILQ_FIRST(&desc->free_media_events);
9974 0 : if (entry == NULL) {
9975 0 : break;
9976 : }
9977 :
9978 0 : TAILQ_REMOVE(&desc->free_media_events, entry, tailq);
9979 0 : TAILQ_INSERT_TAIL(&desc->pending_media_events, entry, tailq);
9980 0 : entry->event = events[event_id];
9981 0 : }
9982 :
9983 0 : rc = event_id;
9984 : out:
9985 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9986 0 : return rc;
9987 : }
9988 :
9989 : static void
9990 0 : _media_management_notify(void *arg)
9991 : {
9992 0 : struct spdk_bdev_desc *desc = arg;
9993 :
9994 0 : _event_notify(desc, SPDK_BDEV_EVENT_MEDIA_MANAGEMENT);
9995 0 : }
9996 :
9997 : void
9998 0 : spdk_bdev_notify_media_management(struct spdk_bdev *bdev)
9999 : {
10000 : struct spdk_bdev_desc *desc;
10001 :
10002 0 : spdk_spin_lock(&bdev->internal.spinlock);
10003 0 : TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
10004 0 : if (!TAILQ_EMPTY(&desc->pending_media_events)) {
10005 0 : event_notify(desc, _media_management_notify);
10006 0 : }
10007 0 : }
10008 0 : spdk_spin_unlock(&bdev->internal.spinlock);
10009 0 : }
10010 :
10011 : struct locked_lba_range_ctx {
10012 : struct lba_range range;
10013 : struct lba_range *current_range;
10014 : struct lba_range *owner_range;
10015 : struct spdk_poller *poller;
10016 : lock_range_cb cb_fn;
10017 : void *cb_arg;
10018 : };
10019 :
10020 : static void
10021 0 : bdev_lock_error_cleanup_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10022 : {
10023 0 : struct locked_lba_range_ctx *ctx = _ctx;
10024 :
10025 0 : ctx->cb_fn(&ctx->range, ctx->cb_arg, -ENOMEM);
10026 0 : free(ctx);
10027 0 : }
10028 :
10029 : static void bdev_unlock_lba_range_get_channel(struct spdk_bdev_channel_iter *i,
10030 : struct spdk_bdev *bdev, struct spdk_io_channel *ch, void *_ctx);
10031 :
10032 : static void
10033 14 : bdev_lock_lba_range_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10034 : {
10035 14 : struct locked_lba_range_ctx *ctx = _ctx;
10036 :
10037 14 : if (status == -ENOMEM) {
10038 : /* One of the channels could not allocate a range object.
10039 : * So we have to go back and clean up any ranges that were
10040 : * allocated successfully before we return error status to
10041 : * the caller. We can reuse the unlock function to do that
10042 : * clean up.
10043 : */
10044 0 : spdk_bdev_for_each_channel(bdev, bdev_unlock_lba_range_get_channel, ctx,
10045 : bdev_lock_error_cleanup_cb);
10046 0 : return;
10047 : }
10048 :
10049 : /* All channels have locked this range and no I/O overlapping the range
10050 : * are outstanding! Set the owner_ch for the range object for the
10051 : * locking channel, so that this channel will know that it is allowed
10052 : * to write to this range.
10053 : */
10054 14 : if (ctx->owner_range != NULL) {
10055 10 : ctx->owner_range->owner_ch = ctx->range.owner_ch;
10056 10 : }
10057 :
10058 14 : ctx->cb_fn(&ctx->range, ctx->cb_arg, status);
10059 :
10060 : /* Don't free the ctx here. Its range is in the bdev's global list of
10061 : * locked ranges still, and will be removed and freed when this range
10062 : * is later unlocked.
10063 : */
10064 14 : }
10065 :
10066 : static int
10067 17 : bdev_lock_lba_range_check_io(void *_i)
10068 : {
10069 17 : struct spdk_bdev_channel_iter *i = _i;
10070 17 : struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i->i);
10071 17 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10072 17 : struct locked_lba_range_ctx *ctx = i->ctx;
10073 17 : struct lba_range *range = ctx->current_range;
10074 : struct spdk_bdev_io *bdev_io;
10075 :
10076 17 : spdk_poller_unregister(&ctx->poller);
10077 :
10078 : /* The range is now in the locked_ranges, so no new IO can be submitted to this
10079 : * range. But we need to wait until any outstanding IO overlapping with this range
10080 : * are completed.
10081 : */
10082 18 : TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) {
10083 3 : if (bdev_io_range_is_locked(bdev_io, range)) {
10084 2 : ctx->poller = SPDK_POLLER_REGISTER(bdev_lock_lba_range_check_io, i, 100);
10085 2 : return SPDK_POLLER_BUSY;
10086 : }
10087 1 : }
10088 :
10089 15 : spdk_bdev_for_each_channel_continue(i, 0);
10090 15 : return SPDK_POLLER_BUSY;
10091 17 : }
10092 :
10093 : static void
10094 15 : bdev_lock_lba_range_get_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10095 : struct spdk_io_channel *_ch, void *_ctx)
10096 : {
10097 15 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10098 15 : struct locked_lba_range_ctx *ctx = _ctx;
10099 : struct lba_range *range;
10100 :
10101 16 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
10102 1 : if (range->length == ctx->range.length &&
10103 0 : range->offset == ctx->range.offset &&
10104 0 : range->locked_ctx == ctx->range.locked_ctx) {
10105 : /* This range already exists on this channel, so don't add
10106 : * it again. This can happen when a new channel is created
10107 : * while the for_each_channel operation is in progress.
10108 : * Do not check for outstanding I/O in that case, since the
10109 : * range was locked before any I/O could be submitted to the
10110 : * new channel.
10111 : */
10112 0 : spdk_bdev_for_each_channel_continue(i, 0);
10113 0 : return;
10114 : }
10115 1 : }
10116 :
10117 15 : range = calloc(1, sizeof(*range));
10118 15 : if (range == NULL) {
10119 0 : spdk_bdev_for_each_channel_continue(i, -ENOMEM);
10120 0 : return;
10121 : }
10122 :
10123 15 : range->length = ctx->range.length;
10124 15 : range->offset = ctx->range.offset;
10125 15 : range->locked_ctx = ctx->range.locked_ctx;
10126 15 : range->quiesce = ctx->range.quiesce;
10127 15 : ctx->current_range = range;
10128 15 : if (ctx->range.owner_ch == ch) {
10129 : /* This is the range object for the channel that will hold
10130 : * the lock. Store it in the ctx object so that we can easily
10131 : * set its owner_ch after the lock is finally acquired.
10132 : */
10133 10 : ctx->owner_range = range;
10134 10 : }
10135 15 : TAILQ_INSERT_TAIL(&ch->locked_ranges, range, tailq);
10136 15 : bdev_lock_lba_range_check_io(i);
10137 15 : }
10138 :
10139 : static void
10140 14 : bdev_lock_lba_range_ctx(struct spdk_bdev *bdev, struct locked_lba_range_ctx *ctx)
10141 : {
10142 14 : assert(spdk_get_thread() == ctx->range.owner_thread);
10143 14 : assert(ctx->range.owner_ch == NULL ||
10144 : spdk_io_channel_get_thread(ctx->range.owner_ch->channel) == ctx->range.owner_thread);
10145 :
10146 : /* We will add a copy of this range to each channel now. */
10147 14 : spdk_bdev_for_each_channel(bdev, bdev_lock_lba_range_get_channel, ctx,
10148 : bdev_lock_lba_range_cb);
10149 14 : }
10150 :
10151 : static bool
10152 17 : bdev_lba_range_overlaps_tailq(struct lba_range *range, lba_range_tailq_t *tailq)
10153 : {
10154 : struct lba_range *r;
10155 :
10156 18 : TAILQ_FOREACH(r, tailq, tailq) {
10157 4 : if (bdev_lba_range_overlapped(range, r)) {
10158 3 : return true;
10159 : }
10160 1 : }
10161 14 : return false;
10162 17 : }
10163 :
10164 : static void bdev_quiesce_range_locked(struct lba_range *range, void *ctx, int status);
10165 :
10166 : static int
10167 14 : _bdev_lock_lba_range(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch,
10168 : uint64_t offset, uint64_t length,
10169 : lock_range_cb cb_fn, void *cb_arg)
10170 : {
10171 : struct locked_lba_range_ctx *ctx;
10172 :
10173 14 : ctx = calloc(1, sizeof(*ctx));
10174 14 : if (ctx == NULL) {
10175 0 : return -ENOMEM;
10176 : }
10177 :
10178 14 : ctx->range.offset = offset;
10179 14 : ctx->range.length = length;
10180 14 : ctx->range.owner_thread = spdk_get_thread();
10181 14 : ctx->range.owner_ch = ch;
10182 14 : ctx->range.locked_ctx = cb_arg;
10183 14 : ctx->range.bdev = bdev;
10184 14 : ctx->range.quiesce = (cb_fn == bdev_quiesce_range_locked);
10185 14 : ctx->cb_fn = cb_fn;
10186 14 : ctx->cb_arg = cb_arg;
10187 :
10188 14 : spdk_spin_lock(&bdev->internal.spinlock);
10189 14 : if (bdev_lba_range_overlaps_tailq(&ctx->range, &bdev->internal.locked_ranges)) {
10190 : /* There is an active lock overlapping with this range.
10191 : * Put it on the pending list until this range no
10192 : * longer overlaps with another.
10193 : */
10194 2 : TAILQ_INSERT_TAIL(&bdev->internal.pending_locked_ranges, &ctx->range, tailq);
10195 2 : } else {
10196 12 : TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, &ctx->range, tailq);
10197 12 : bdev_lock_lba_range_ctx(bdev, ctx);
10198 : }
10199 14 : spdk_spin_unlock(&bdev->internal.spinlock);
10200 14 : return 0;
10201 14 : }
10202 :
10203 : static int
10204 10 : bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
10205 : uint64_t offset, uint64_t length,
10206 : lock_range_cb cb_fn, void *cb_arg)
10207 : {
10208 10 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
10209 10 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10210 :
10211 10 : if (cb_arg == NULL) {
10212 0 : SPDK_ERRLOG("cb_arg must not be NULL\n");
10213 0 : return -EINVAL;
10214 : }
10215 :
10216 10 : return _bdev_lock_lba_range(bdev, ch, offset, length, cb_fn, cb_arg);
10217 10 : }
10218 :
10219 : static void
10220 2 : bdev_lock_lba_range_ctx_msg(void *_ctx)
10221 : {
10222 2 : struct locked_lba_range_ctx *ctx = _ctx;
10223 :
10224 2 : bdev_lock_lba_range_ctx(ctx->range.bdev, ctx);
10225 2 : }
10226 :
10227 : static void
10228 14 : bdev_unlock_lba_range_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10229 : {
10230 14 : struct locked_lba_range_ctx *ctx = _ctx;
10231 : struct locked_lba_range_ctx *pending_ctx;
10232 : struct lba_range *range, *tmp;
10233 :
10234 14 : spdk_spin_lock(&bdev->internal.spinlock);
10235 : /* Check if there are any pending locked ranges that overlap with this range
10236 : * that was just unlocked. If there are, check that it doesn't overlap with any
10237 : * other locked ranges before calling bdev_lock_lba_range_ctx which will start
10238 : * the lock process.
10239 : */
10240 17 : TAILQ_FOREACH_SAFE(range, &bdev->internal.pending_locked_ranges, tailq, tmp) {
10241 3 : if (bdev_lba_range_overlapped(range, &ctx->range) &&
10242 3 : !bdev_lba_range_overlaps_tailq(range, &bdev->internal.locked_ranges)) {
10243 2 : TAILQ_REMOVE(&bdev->internal.pending_locked_ranges, range, tailq);
10244 2 : pending_ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range);
10245 2 : TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, range, tailq);
10246 4 : spdk_thread_send_msg(pending_ctx->range.owner_thread,
10247 2 : bdev_lock_lba_range_ctx_msg, pending_ctx);
10248 2 : }
10249 3 : }
10250 14 : spdk_spin_unlock(&bdev->internal.spinlock);
10251 :
10252 14 : ctx->cb_fn(&ctx->range, ctx->cb_arg, status);
10253 14 : free(ctx);
10254 14 : }
10255 :
10256 : static void
10257 16 : bdev_unlock_lba_range_get_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10258 : struct spdk_io_channel *_ch, void *_ctx)
10259 : {
10260 16 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10261 16 : struct locked_lba_range_ctx *ctx = _ctx;
10262 : TAILQ_HEAD(, spdk_bdev_io) io_locked;
10263 : struct spdk_bdev_io *bdev_io;
10264 : struct lba_range *range;
10265 :
10266 16 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
10267 32 : if (ctx->range.offset == range->offset &&
10268 16 : ctx->range.length == range->length &&
10269 16 : ctx->range.locked_ctx == range->locked_ctx) {
10270 16 : TAILQ_REMOVE(&ch->locked_ranges, range, tailq);
10271 16 : free(range);
10272 16 : break;
10273 : }
10274 0 : }
10275 :
10276 : /* Note: we should almost always be able to assert that the range specified
10277 : * was found. But there are some very rare corner cases where a new channel
10278 : * gets created simultaneously with a range unlock, where this function
10279 : * would execute on that new channel and wouldn't have the range.
10280 : * We also use this to clean up range allocations when a later allocation
10281 : * fails in the locking path.
10282 : * So we can't actually assert() here.
10283 : */
10284 :
10285 : /* Swap the locked IO into a temporary list, and then try to submit them again.
10286 : * We could hyper-optimize this to only resubmit locked I/O that overlap
10287 : * with the range that was just unlocked, but this isn't a performance path so
10288 : * we go for simplicity here.
10289 : */
10290 16 : TAILQ_INIT(&io_locked);
10291 16 : TAILQ_SWAP(&ch->io_locked, &io_locked, spdk_bdev_io, internal.ch_link);
10292 19 : while (!TAILQ_EMPTY(&io_locked)) {
10293 3 : bdev_io = TAILQ_FIRST(&io_locked);
10294 3 : TAILQ_REMOVE(&io_locked, bdev_io, internal.ch_link);
10295 3 : bdev_io_submit(bdev_io);
10296 : }
10297 :
10298 16 : spdk_bdev_for_each_channel_continue(i, 0);
10299 16 : }
10300 :
10301 : static int
10302 14 : _bdev_unlock_lba_range(struct spdk_bdev *bdev, uint64_t offset, uint64_t length,
10303 : lock_range_cb cb_fn, void *cb_arg)
10304 : {
10305 : struct locked_lba_range_ctx *ctx;
10306 : struct lba_range *range;
10307 :
10308 14 : spdk_spin_lock(&bdev->internal.spinlock);
10309 : /* To start the unlock the process, we find the range in the bdev's locked_ranges
10310 : * and remove it. This ensures new channels don't inherit the locked range.
10311 : * Then we will send a message to each channel to remove the range from its
10312 : * per-channel list.
10313 : */
10314 14 : TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) {
10315 24 : if (range->offset == offset && range->length == length &&
10316 14 : (range->owner_ch == NULL || range->locked_ctx == cb_arg)) {
10317 14 : break;
10318 : }
10319 0 : }
10320 14 : if (range == NULL) {
10321 0 : assert(false);
10322 : spdk_spin_unlock(&bdev->internal.spinlock);
10323 : return -EINVAL;
10324 : }
10325 14 : TAILQ_REMOVE(&bdev->internal.locked_ranges, range, tailq);
10326 14 : ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range);
10327 14 : spdk_spin_unlock(&bdev->internal.spinlock);
10328 :
10329 14 : ctx->cb_fn = cb_fn;
10330 14 : ctx->cb_arg = cb_arg;
10331 :
10332 14 : spdk_bdev_for_each_channel(bdev, bdev_unlock_lba_range_get_channel, ctx,
10333 : bdev_unlock_lba_range_cb);
10334 14 : return 0;
10335 : }
10336 :
10337 : static int
10338 12 : bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
10339 : uint64_t offset, uint64_t length,
10340 : lock_range_cb cb_fn, void *cb_arg)
10341 : {
10342 12 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
10343 12 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10344 : struct lba_range *range;
10345 12 : bool range_found = false;
10346 :
10347 : /* Let's make sure the specified channel actually has a lock on
10348 : * the specified range. Note that the range must match exactly.
10349 : */
10350 14 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
10351 22 : if (range->offset == offset && range->length == length &&
10352 11 : range->owner_ch == ch && range->locked_ctx == cb_arg) {
10353 10 : range_found = true;
10354 10 : break;
10355 : }
10356 2 : }
10357 :
10358 12 : if (!range_found) {
10359 2 : return -EINVAL;
10360 : }
10361 :
10362 10 : return _bdev_unlock_lba_range(bdev, offset, length, cb_fn, cb_arg);
10363 12 : }
10364 :
10365 : struct bdev_quiesce_ctx {
10366 : spdk_bdev_quiesce_cb cb_fn;
10367 : void *cb_arg;
10368 : };
10369 :
10370 : static void
10371 4 : bdev_unquiesce_range_unlocked(struct lba_range *range, void *ctx, int status)
10372 : {
10373 4 : struct bdev_quiesce_ctx *quiesce_ctx = ctx;
10374 :
10375 4 : if (quiesce_ctx->cb_fn != NULL) {
10376 4 : quiesce_ctx->cb_fn(quiesce_ctx->cb_arg, status);
10377 4 : }
10378 :
10379 4 : free(quiesce_ctx);
10380 4 : }
10381 :
10382 : static void
10383 4 : bdev_quiesce_range_locked(struct lba_range *range, void *ctx, int status)
10384 : {
10385 4 : struct bdev_quiesce_ctx *quiesce_ctx = ctx;
10386 4 : struct spdk_bdev_module *module = range->bdev->module;
10387 :
10388 4 : if (status != 0) {
10389 0 : if (quiesce_ctx->cb_fn != NULL) {
10390 0 : quiesce_ctx->cb_fn(quiesce_ctx->cb_arg, status);
10391 0 : }
10392 0 : free(quiesce_ctx);
10393 0 : return;
10394 : }
10395 :
10396 4 : spdk_spin_lock(&module->internal.spinlock);
10397 4 : TAILQ_INSERT_TAIL(&module->internal.quiesced_ranges, range, tailq_module);
10398 4 : spdk_spin_unlock(&module->internal.spinlock);
10399 :
10400 4 : if (quiesce_ctx->cb_fn != NULL) {
10401 : /* copy the context in case the range is unlocked by the callback */
10402 4 : struct bdev_quiesce_ctx tmp = *quiesce_ctx;
10403 :
10404 4 : quiesce_ctx->cb_fn = NULL;
10405 4 : quiesce_ctx->cb_arg = NULL;
10406 :
10407 4 : tmp.cb_fn(tmp.cb_arg, status);
10408 4 : }
10409 : /* quiesce_ctx will be freed on unquiesce */
10410 4 : }
10411 :
10412 : static int
10413 9 : _spdk_bdev_quiesce(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10414 : uint64_t offset, uint64_t length,
10415 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg,
10416 : bool unquiesce)
10417 : {
10418 : struct bdev_quiesce_ctx *quiesce_ctx;
10419 : int rc;
10420 :
10421 9 : if (module != bdev->module) {
10422 0 : SPDK_ERRLOG("Bdev does not belong to specified module.\n");
10423 0 : return -EINVAL;
10424 : }
10425 :
10426 9 : if (!bdev_io_valid_blocks(bdev, offset, length)) {
10427 0 : return -EINVAL;
10428 : }
10429 :
10430 9 : if (unquiesce) {
10431 : struct lba_range *range;
10432 :
10433 : /* Make sure the specified range is actually quiesced in the specified module and
10434 : * then remove it from the list. Note that the range must match exactly.
10435 : */
10436 5 : spdk_spin_lock(&module->internal.spinlock);
10437 6 : TAILQ_FOREACH(range, &module->internal.quiesced_ranges, tailq_module) {
10438 5 : if (range->bdev == bdev && range->offset == offset && range->length == length) {
10439 4 : TAILQ_REMOVE(&module->internal.quiesced_ranges, range, tailq_module);
10440 4 : break;
10441 : }
10442 1 : }
10443 5 : spdk_spin_unlock(&module->internal.spinlock);
10444 :
10445 5 : if (range == NULL) {
10446 1 : SPDK_ERRLOG("The range to unquiesce was not found.\n");
10447 1 : return -EINVAL;
10448 : }
10449 :
10450 4 : quiesce_ctx = range->locked_ctx;
10451 4 : quiesce_ctx->cb_fn = cb_fn;
10452 4 : quiesce_ctx->cb_arg = cb_arg;
10453 :
10454 4 : rc = _bdev_unlock_lba_range(bdev, offset, length, bdev_unquiesce_range_unlocked, quiesce_ctx);
10455 4 : } else {
10456 4 : quiesce_ctx = malloc(sizeof(*quiesce_ctx));
10457 4 : if (quiesce_ctx == NULL) {
10458 0 : return -ENOMEM;
10459 : }
10460 :
10461 4 : quiesce_ctx->cb_fn = cb_fn;
10462 4 : quiesce_ctx->cb_arg = cb_arg;
10463 :
10464 4 : rc = _bdev_lock_lba_range(bdev, NULL, offset, length, bdev_quiesce_range_locked, quiesce_ctx);
10465 4 : if (rc != 0) {
10466 0 : free(quiesce_ctx);
10467 0 : }
10468 : }
10469 :
10470 8 : return rc;
10471 9 : }
10472 :
10473 : int
10474 3 : spdk_bdev_quiesce(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10475 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10476 : {
10477 3 : return _spdk_bdev_quiesce(bdev, module, 0, bdev->blockcnt, cb_fn, cb_arg, false);
10478 : }
10479 :
10480 : int
10481 3 : spdk_bdev_unquiesce(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10482 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10483 : {
10484 3 : return _spdk_bdev_quiesce(bdev, module, 0, bdev->blockcnt, cb_fn, cb_arg, true);
10485 : }
10486 :
10487 : int
10488 1 : spdk_bdev_quiesce_range(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10489 : uint64_t offset, uint64_t length,
10490 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10491 : {
10492 1 : return _spdk_bdev_quiesce(bdev, module, offset, length, cb_fn, cb_arg, false);
10493 : }
10494 :
10495 : int
10496 2 : spdk_bdev_unquiesce_range(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10497 : uint64_t offset, uint64_t length,
10498 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10499 : {
10500 2 : return _spdk_bdev_quiesce(bdev, module, offset, length, cb_fn, cb_arg, true);
10501 : }
10502 :
10503 : int
10504 272 : spdk_bdev_get_memory_domains(struct spdk_bdev *bdev, struct spdk_memory_domain **domains,
10505 : int array_size)
10506 : {
10507 272 : if (!bdev) {
10508 1 : return -EINVAL;
10509 : }
10510 :
10511 271 : if (bdev->fn_table->get_memory_domains) {
10512 3 : return bdev->fn_table->get_memory_domains(bdev->ctxt, domains, array_size);
10513 : }
10514 :
10515 268 : return 0;
10516 272 : }
10517 :
10518 : struct spdk_bdev_for_each_io_ctx {
10519 : void *ctx;
10520 : spdk_bdev_io_fn fn;
10521 : spdk_bdev_for_each_io_cb cb;
10522 : };
10523 :
10524 : static void
10525 0 : bdev_channel_for_each_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10526 : struct spdk_io_channel *io_ch, void *_ctx)
10527 : {
10528 0 : struct spdk_bdev_for_each_io_ctx *ctx = _ctx;
10529 0 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
10530 : struct spdk_bdev_io *bdev_io;
10531 0 : int rc = 0;
10532 :
10533 0 : TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) {
10534 0 : rc = ctx->fn(ctx->ctx, bdev_io);
10535 0 : if (rc != 0) {
10536 0 : break;
10537 : }
10538 0 : }
10539 :
10540 0 : spdk_bdev_for_each_channel_continue(i, rc);
10541 0 : }
10542 :
10543 : static void
10544 0 : bdev_for_each_io_done(struct spdk_bdev *bdev, void *_ctx, int status)
10545 : {
10546 0 : struct spdk_bdev_for_each_io_ctx *ctx = _ctx;
10547 :
10548 0 : ctx->cb(ctx->ctx, status);
10549 :
10550 0 : free(ctx);
10551 0 : }
10552 :
10553 : void
10554 0 : spdk_bdev_for_each_bdev_io(struct spdk_bdev *bdev, void *_ctx, spdk_bdev_io_fn fn,
10555 : spdk_bdev_for_each_io_cb cb)
10556 : {
10557 : struct spdk_bdev_for_each_io_ctx *ctx;
10558 :
10559 0 : assert(fn != NULL && cb != NULL);
10560 :
10561 0 : ctx = calloc(1, sizeof(*ctx));
10562 0 : if (ctx == NULL) {
10563 0 : SPDK_ERRLOG("Failed to allocate context.\n");
10564 0 : cb(_ctx, -ENOMEM);
10565 0 : return;
10566 : }
10567 :
10568 0 : ctx->ctx = _ctx;
10569 0 : ctx->fn = fn;
10570 0 : ctx->cb = cb;
10571 :
10572 0 : spdk_bdev_for_each_channel(bdev, bdev_channel_for_each_io, ctx,
10573 : bdev_for_each_io_done);
10574 0 : }
10575 :
10576 : void
10577 132 : spdk_bdev_for_each_channel_continue(struct spdk_bdev_channel_iter *iter, int status)
10578 : {
10579 132 : spdk_for_each_channel_continue(iter->i, status);
10580 132 : }
10581 :
10582 : static struct spdk_bdev *
10583 361 : io_channel_iter_get_bdev(struct spdk_io_channel_iter *i)
10584 : {
10585 361 : void *io_device = spdk_io_channel_iter_get_io_device(i);
10586 :
10587 361 : return __bdev_from_io_dev(io_device);
10588 : }
10589 :
10590 : static void
10591 132 : bdev_each_channel_msg(struct spdk_io_channel_iter *i)
10592 : {
10593 132 : struct spdk_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
10594 132 : struct spdk_bdev *bdev = io_channel_iter_get_bdev(i);
10595 132 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
10596 :
10597 132 : iter->i = i;
10598 132 : iter->fn(iter, bdev, ch, iter->ctx);
10599 132 : }
10600 :
10601 : static void
10602 229 : bdev_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
10603 : {
10604 229 : struct spdk_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
10605 229 : struct spdk_bdev *bdev = io_channel_iter_get_bdev(i);
10606 :
10607 229 : iter->i = i;
10608 229 : iter->cpl(bdev, iter->ctx, status);
10609 :
10610 229 : free(iter);
10611 229 : }
10612 :
10613 : void
10614 229 : spdk_bdev_for_each_channel(struct spdk_bdev *bdev, spdk_bdev_for_each_channel_msg fn,
10615 : void *ctx, spdk_bdev_for_each_channel_done cpl)
10616 : {
10617 : struct spdk_bdev_channel_iter *iter;
10618 :
10619 229 : assert(bdev != NULL && fn != NULL && ctx != NULL);
10620 :
10621 229 : iter = calloc(1, sizeof(struct spdk_bdev_channel_iter));
10622 229 : if (iter == NULL) {
10623 0 : SPDK_ERRLOG("Unable to allocate iterator\n");
10624 0 : assert(false);
10625 : return;
10626 : }
10627 :
10628 229 : iter->fn = fn;
10629 229 : iter->cpl = cpl;
10630 229 : iter->ctx = ctx;
10631 :
10632 458 : spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_each_channel_msg,
10633 229 : iter, bdev_each_channel_cpl);
10634 229 : }
10635 :
10636 : static void
10637 3 : bdev_copy_do_write_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
10638 : {
10639 3 : struct spdk_bdev_io *parent_io = cb_arg;
10640 :
10641 3 : spdk_bdev_free_io(bdev_io);
10642 :
10643 : /* Check return status of write */
10644 3 : parent_io->internal.status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
10645 3 : parent_io->internal.cb(parent_io, success, parent_io->internal.caller_ctx);
10646 3 : }
10647 :
10648 : static void
10649 3 : bdev_copy_do_write(void *_bdev_io)
10650 : {
10651 3 : struct spdk_bdev_io *bdev_io = _bdev_io;
10652 : int rc;
10653 :
10654 : /* Write blocks */
10655 6 : rc = spdk_bdev_write_blocks_with_md(bdev_io->internal.desc,
10656 3 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
10657 3 : bdev_io->u.bdev.iovs[0].iov_base,
10658 3 : bdev_io->u.bdev.md_buf, bdev_io->u.bdev.offset_blocks,
10659 3 : bdev_io->u.bdev.num_blocks, bdev_copy_do_write_done, bdev_io);
10660 :
10661 3 : if (rc == -ENOMEM) {
10662 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_copy_do_write);
10663 3 : } else if (rc != 0) {
10664 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10665 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
10666 0 : }
10667 3 : }
10668 :
10669 : static void
10670 3 : bdev_copy_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
10671 : {
10672 3 : struct spdk_bdev_io *parent_io = cb_arg;
10673 :
10674 3 : spdk_bdev_free_io(bdev_io);
10675 :
10676 : /* Check return status of read */
10677 3 : if (!success) {
10678 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10679 0 : parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
10680 0 : return;
10681 : }
10682 :
10683 : /* Do write */
10684 3 : bdev_copy_do_write(parent_io);
10685 3 : }
10686 :
10687 : static void
10688 3 : bdev_copy_do_read(void *_bdev_io)
10689 : {
10690 3 : struct spdk_bdev_io *bdev_io = _bdev_io;
10691 : int rc;
10692 :
10693 : /* Read blocks */
10694 6 : rc = spdk_bdev_read_blocks_with_md(bdev_io->internal.desc,
10695 3 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
10696 3 : bdev_io->u.bdev.iovs[0].iov_base,
10697 3 : bdev_io->u.bdev.md_buf, bdev_io->u.bdev.copy.src_offset_blocks,
10698 3 : bdev_io->u.bdev.num_blocks, bdev_copy_do_read_done, bdev_io);
10699 :
10700 3 : if (rc == -ENOMEM) {
10701 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_copy_do_read);
10702 3 : } else if (rc != 0) {
10703 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10704 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
10705 0 : }
10706 3 : }
10707 :
10708 : static void
10709 3 : bdev_copy_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success)
10710 : {
10711 3 : if (!success) {
10712 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10713 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
10714 0 : return;
10715 : }
10716 :
10717 3 : bdev_copy_do_read(bdev_io);
10718 3 : }
10719 :
10720 : int
10721 27 : spdk_bdev_copy_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
10722 : uint64_t dst_offset_blocks, uint64_t src_offset_blocks, uint64_t num_blocks,
10723 : spdk_bdev_io_completion_cb cb, void *cb_arg)
10724 : {
10725 27 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
10726 : struct spdk_bdev_io *bdev_io;
10727 27 : struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
10728 :
10729 27 : if (!desc->write) {
10730 0 : return -EBADF;
10731 : }
10732 :
10733 27 : if (!bdev_io_valid_blocks(bdev, dst_offset_blocks, num_blocks) ||
10734 27 : !bdev_io_valid_blocks(bdev, src_offset_blocks, num_blocks)) {
10735 0 : SPDK_DEBUGLOG(bdev,
10736 : "Invalid offset or number of blocks: dst %lu, src %lu, count %lu\n",
10737 : dst_offset_blocks, src_offset_blocks, num_blocks);
10738 0 : return -EINVAL;
10739 : }
10740 :
10741 27 : bdev_io = bdev_channel_get_io(channel);
10742 27 : if (!bdev_io) {
10743 0 : return -ENOMEM;
10744 : }
10745 :
10746 27 : bdev_io->internal.ch = channel;
10747 27 : bdev_io->internal.desc = desc;
10748 27 : bdev_io->type = SPDK_BDEV_IO_TYPE_COPY;
10749 :
10750 27 : bdev_io->u.bdev.offset_blocks = dst_offset_blocks;
10751 27 : bdev_io->u.bdev.copy.src_offset_blocks = src_offset_blocks;
10752 27 : bdev_io->u.bdev.num_blocks = num_blocks;
10753 27 : bdev_io->u.bdev.memory_domain = NULL;
10754 27 : bdev_io->u.bdev.memory_domain_ctx = NULL;
10755 27 : bdev_io->u.bdev.iovs = NULL;
10756 27 : bdev_io->u.bdev.iovcnt = 0;
10757 27 : bdev_io->u.bdev.md_buf = NULL;
10758 27 : bdev_io->u.bdev.accel_sequence = NULL;
10759 27 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
10760 :
10761 27 : if (dst_offset_blocks == src_offset_blocks || num_blocks == 0) {
10762 0 : spdk_thread_send_msg(spdk_get_thread(), bdev_io_complete_cb, bdev_io);
10763 0 : return 0;
10764 : }
10765 :
10766 :
10767 : /* If the copy size is large and should be split, use the generic split logic
10768 : * regardless of whether SPDK_BDEV_IO_TYPE_COPY is supported or not.
10769 : *
10770 : * Then, send the copy request if SPDK_BDEV_IO_TYPE_COPY is supported or
10771 : * emulate it using regular read and write requests otherwise.
10772 : */
10773 27 : if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COPY) ||
10774 4 : bdev_io->internal.f.split) {
10775 24 : bdev_io_submit(bdev_io);
10776 24 : return 0;
10777 : }
10778 :
10779 3 : spdk_bdev_io_get_buf(bdev_io, bdev_copy_get_buf_cb, num_blocks * spdk_bdev_get_block_size(bdev));
10780 :
10781 3 : return 0;
10782 27 : }
10783 :
10784 3 : SPDK_LOG_REGISTER_COMPONENT(bdev)
10785 :
10786 : static void
10787 0 : bdev_trace(void)
10788 : {
10789 0 : struct spdk_trace_tpoint_opts opts[] = {
10790 : {
10791 : "BDEV_IO_START", TRACE_BDEV_IO_START,
10792 : OWNER_TYPE_BDEV, OBJECT_BDEV_IO, 1,
10793 : {
10794 : { "type", SPDK_TRACE_ARG_TYPE_INT, 8 },
10795 : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
10796 : { "offset", SPDK_TRACE_ARG_TYPE_INT, 8 },
10797 : { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
10798 : }
10799 : },
10800 : {
10801 : "BDEV_IO_DONE", TRACE_BDEV_IO_DONE,
10802 : OWNER_TYPE_BDEV, OBJECT_BDEV_IO, 0,
10803 : {
10804 : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
10805 : { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
10806 : }
10807 : },
10808 : {
10809 : "BDEV_IOCH_CREATE", TRACE_BDEV_IOCH_CREATE,
10810 : OWNER_TYPE_BDEV, OBJECT_NONE, 0,
10811 : {
10812 : { "tid", SPDK_TRACE_ARG_TYPE_INT, 8 }
10813 : }
10814 : },
10815 : {
10816 : "BDEV_IOCH_DESTROY", TRACE_BDEV_IOCH_DESTROY,
10817 : OWNER_TYPE_BDEV, OBJECT_NONE, 0,
10818 : {
10819 : { "tid", SPDK_TRACE_ARG_TYPE_INT, 8 }
10820 : }
10821 : },
10822 : };
10823 :
10824 :
10825 0 : spdk_trace_register_owner_type(OWNER_TYPE_BDEV, 'b');
10826 0 : spdk_trace_register_object(OBJECT_BDEV_IO, 'i');
10827 0 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
10828 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_NVME_IO_START, OBJECT_BDEV_IO, 0);
10829 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_NVME_IO_DONE, OBJECT_BDEV_IO, 0);
10830 0 : spdk_trace_tpoint_register_relation(TRACE_BLOB_REQ_SET_START, OBJECT_BDEV_IO, 0);
10831 0 : spdk_trace_tpoint_register_relation(TRACE_BLOB_REQ_SET_COMPLETE, OBJECT_BDEV_IO, 0);
10832 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_RAID_IO_START, OBJECT_BDEV_IO, 0);
10833 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_RAID_IO_DONE, OBJECT_BDEV_IO, 0);
10834 0 : }
10835 3 : SPDK_TRACE_REGISTER_FN(bdev_trace, "bdev", TRACE_GROUP_BDEV)
|