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