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