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