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