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