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