Line data Source code
1 : /* SPDX-License-Identifier: BSD-3-Clause
2 : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved.
4 : * Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : */
6 :
7 : #include "spdk/stdinc.h"
8 :
9 : #include "spdk/bdev.h"
10 :
11 : #include "spdk/accel.h"
12 : #include "spdk/config.h"
13 : #include "spdk/env.h"
14 : #include "spdk/thread.h"
15 : #include "spdk/likely.h"
16 : #include "spdk/queue.h"
17 : #include "spdk/nvme_spec.h"
18 : #include "spdk/scsi_spec.h"
19 : #include "spdk/notify.h"
20 : #include "spdk/util.h"
21 : #include "spdk/trace.h"
22 : #include "spdk/dma.h"
23 :
24 : #include "spdk/bdev_module.h"
25 : #include "spdk/log.h"
26 : #include "spdk/string.h"
27 :
28 : #include "bdev_internal.h"
29 : #include "spdk_internal/trace_defs.h"
30 : #include "spdk_internal/assert.h"
31 :
32 : #ifdef SPDK_CONFIG_VTUNE
33 : #include "ittnotify.h"
34 : #include "ittnotify_types.h"
35 : int __itt_init_ittlib(const char *, __itt_group_id);
36 : #endif
37 :
38 : #define SPDK_BDEV_IO_POOL_SIZE (64 * 1024 - 1)
39 : #define SPDK_BDEV_IO_CACHE_SIZE 256
40 : #define SPDK_BDEV_AUTO_EXAMINE true
41 : #define BUF_SMALL_CACHE_SIZE 128
42 : #define BUF_LARGE_CACHE_SIZE 16
43 : #define NOMEM_THRESHOLD_COUNT 8
44 :
45 : #define SPDK_BDEV_QOS_TIMESLICE_IN_USEC 1000
46 : #define SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE 1
47 : #define SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE 512
48 : #define SPDK_BDEV_QOS_MIN_IOS_PER_SEC 1000
49 : #define SPDK_BDEV_QOS_MIN_BYTES_PER_SEC (1024 * 1024)
50 : #define SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC (UINT64_MAX / (1024 * 1024))
51 : #define SPDK_BDEV_QOS_LIMIT_NOT_DEFINED UINT64_MAX
52 : #define SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC 1000
53 :
54 : /* The maximum number of children requests for a UNMAP or WRITE ZEROES command
55 : * when splitting into children requests at a time.
56 : */
57 : #define SPDK_BDEV_MAX_CHILDREN_UNMAP_WRITE_ZEROES_REQS (8)
58 : #define BDEV_RESET_CHECK_OUTSTANDING_IO_PERIOD 1000000
59 :
60 : /* The maximum number of children requests for a COPY command
61 : * when splitting into children requests at a time.
62 : */
63 : #define SPDK_BDEV_MAX_CHILDREN_COPY_REQS (8)
64 :
65 : #define LOG_ALREADY_CLAIMED_ERROR(detail, bdev) \
66 : log_already_claimed(SPDK_LOG_ERROR, __LINE__, __func__, detail, bdev)
67 : #ifdef DEBUG
68 : #define LOG_ALREADY_CLAIMED_DEBUG(detail, bdev) \
69 : log_already_claimed(SPDK_LOG_DEBUG, __LINE__, __func__, detail, bdev)
70 : #else
71 : #define LOG_ALREADY_CLAIMED_DEBUG(detail, bdev) do {} while(0)
72 : #endif
73 :
74 : static void log_already_claimed(enum spdk_log_level level, const int line, const char *func,
75 : const char *detail, struct spdk_bdev *bdev);
76 :
77 : static const char *qos_rpc_type[] = {"rw_ios_per_sec",
78 : "rw_mbytes_per_sec", "r_mbytes_per_sec", "w_mbytes_per_sec"
79 : };
80 :
81 : TAILQ_HEAD(spdk_bdev_list, spdk_bdev);
82 :
83 : RB_HEAD(bdev_name_tree, spdk_bdev_name);
84 :
85 : static int
86 546 : bdev_name_cmp(struct spdk_bdev_name *name1, struct spdk_bdev_name *name2)
87 : {
88 546 : return strcmp(name1->name, name2->name);
89 : }
90 :
91 2058 : 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 : bool
4037 0 : spdk_bdev_io_has_no_metadata(struct spdk_bdev_io *bdev_io)
4038 : {
4039 0 : return bdev_io->internal.desc->opts.no_metadata;
4040 : }
4041 :
4042 : int
4043 0 : spdk_bdev_dump_info_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
4044 : {
4045 0 : if (bdev->fn_table->dump_info_json) {
4046 0 : return bdev->fn_table->dump_info_json(bdev->ctxt, w);
4047 : }
4048 :
4049 0 : return 0;
4050 0 : }
4051 :
4052 : static void
4053 10 : bdev_qos_update_max_quota_per_timeslice(struct spdk_bdev_qos *qos)
4054 : {
4055 10 : uint32_t max_per_timeslice = 0;
4056 : int i;
4057 :
4058 50 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4059 40 : if (qos->rate_limits[i].limit == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
4060 15 : qos->rate_limits[i].max_per_timeslice = 0;
4061 15 : continue;
4062 : }
4063 :
4064 50 : max_per_timeslice = qos->rate_limits[i].limit *
4065 25 : SPDK_BDEV_QOS_TIMESLICE_IN_USEC / SPDK_SEC_TO_USEC;
4066 :
4067 25 : qos->rate_limits[i].max_per_timeslice = spdk_max(max_per_timeslice,
4068 : qos->rate_limits[i].min_per_timeslice);
4069 :
4070 50 : __atomic_store_n(&qos->rate_limits[i].remaining_this_timeslice,
4071 25 : qos->rate_limits[i].max_per_timeslice, __ATOMIC_RELEASE);
4072 25 : }
4073 :
4074 10 : bdev_qos_set_ops(qos);
4075 10 : }
4076 :
4077 : static void
4078 4 : bdev_channel_submit_qos_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
4079 : struct spdk_io_channel *io_ch, void *ctx)
4080 : {
4081 4 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
4082 : int status;
4083 :
4084 4 : bdev_qos_io_submit(bdev_ch, bdev->internal.qos);
4085 :
4086 : /* if all IOs were sent then continue the iteration, otherwise - stop it */
4087 : /* TODO: channels round robing */
4088 4 : status = TAILQ_EMPTY(&bdev_ch->qos_queued_io) ? 0 : 1;
4089 :
4090 4 : spdk_bdev_for_each_channel_continue(i, status);
4091 4 : }
4092 :
4093 :
4094 : static void
4095 2 : bdev_channel_submit_qos_io_done(struct spdk_bdev *bdev, void *ctx, int status)
4096 : {
4097 :
4098 2 : }
4099 :
4100 : static int
4101 3 : bdev_channel_poll_qos(void *arg)
4102 : {
4103 3 : struct spdk_bdev *bdev = arg;
4104 3 : struct spdk_bdev_qos *qos = bdev->internal.qos;
4105 3 : uint64_t now = spdk_get_ticks();
4106 : int i;
4107 : int64_t remaining_last_timeslice;
4108 :
4109 3 : if (spdk_unlikely(qos->thread == NULL)) {
4110 : /* Old QoS was unbound to remove and new QoS is not enabled yet. */
4111 1 : return SPDK_POLLER_IDLE;
4112 : }
4113 :
4114 2 : if (now < (qos->last_timeslice + qos->timeslice_size)) {
4115 : /* We received our callback earlier than expected - return
4116 : * immediately and wait to do accounting until at least one
4117 : * timeslice has actually expired. This should never happen
4118 : * with a well-behaved timer implementation.
4119 : */
4120 0 : return SPDK_POLLER_IDLE;
4121 : }
4122 :
4123 : /* Reset for next round of rate limiting */
4124 10 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4125 : /* We may have allowed the IOs or bytes to slightly overrun in the last
4126 : * timeslice. remaining_this_timeslice is signed, so if it's negative
4127 : * here, we'll account for the overrun so that the next timeslice will
4128 : * be appropriately reduced.
4129 : */
4130 8 : remaining_last_timeslice = __atomic_exchange_n(&qos->rate_limits[i].remaining_this_timeslice,
4131 : 0, __ATOMIC_RELAXED);
4132 8 : if (remaining_last_timeslice < 0) {
4133 : /* There could be a race condition here as both bdev_qos_rw_queue_io() and bdev_channel_poll_qos()
4134 : * potentially use 2 atomic ops each, so they can intertwine.
4135 : * This race can potentially cause the limits to be a little fuzzy but won't cause any real damage.
4136 : */
4137 0 : __atomic_store_n(&qos->rate_limits[i].remaining_this_timeslice,
4138 0 : remaining_last_timeslice, __ATOMIC_RELAXED);
4139 0 : }
4140 8 : }
4141 :
4142 4 : while (now >= (qos->last_timeslice + qos->timeslice_size)) {
4143 2 : qos->last_timeslice += qos->timeslice_size;
4144 10 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4145 16 : __atomic_add_fetch(&qos->rate_limits[i].remaining_this_timeslice,
4146 8 : qos->rate_limits[i].max_per_timeslice, __ATOMIC_RELAXED);
4147 8 : }
4148 : }
4149 :
4150 2 : spdk_bdev_for_each_channel(bdev, bdev_channel_submit_qos_io, qos,
4151 : bdev_channel_submit_qos_io_done);
4152 :
4153 2 : return SPDK_POLLER_BUSY;
4154 3 : }
4155 :
4156 : static void
4157 74 : bdev_channel_destroy_resource(struct spdk_bdev_channel *ch)
4158 : {
4159 : struct spdk_bdev_shared_resource *shared_resource;
4160 : struct lba_range *range;
4161 :
4162 74 : bdev_free_io_stat(ch->stat);
4163 : #ifdef SPDK_CONFIG_VTUNE
4164 : bdev_free_io_stat(ch->prev_stat);
4165 : #endif
4166 :
4167 74 : while (!TAILQ_EMPTY(&ch->locked_ranges)) {
4168 0 : range = TAILQ_FIRST(&ch->locked_ranges);
4169 0 : TAILQ_REMOVE(&ch->locked_ranges, range, tailq);
4170 0 : free(range);
4171 : }
4172 :
4173 74 : spdk_put_io_channel(ch->channel);
4174 74 : spdk_put_io_channel(ch->accel_channel);
4175 :
4176 74 : shared_resource = ch->shared_resource;
4177 :
4178 74 : assert(TAILQ_EMPTY(&ch->io_locked));
4179 74 : assert(TAILQ_EMPTY(&ch->io_submitted));
4180 74 : assert(TAILQ_EMPTY(&ch->io_accel_exec));
4181 74 : assert(TAILQ_EMPTY(&ch->io_memory_domain));
4182 74 : assert(ch->io_outstanding == 0);
4183 74 : assert(shared_resource->ref > 0);
4184 74 : shared_resource->ref--;
4185 74 : if (shared_resource->ref == 0) {
4186 73 : assert(shared_resource->io_outstanding == 0);
4187 73 : TAILQ_REMOVE(&shared_resource->mgmt_ch->shared_resources, shared_resource, link);
4188 73 : spdk_put_io_channel(spdk_io_channel_from_ctx(shared_resource->mgmt_ch));
4189 73 : spdk_poller_unregister(&shared_resource->nomem_poller);
4190 73 : free(shared_resource);
4191 73 : }
4192 74 : }
4193 :
4194 : static void
4195 83 : bdev_enable_qos(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch)
4196 : {
4197 83 : struct spdk_bdev_qos *qos = bdev->internal.qos;
4198 : int i;
4199 :
4200 83 : assert(spdk_spin_held(&bdev->internal.spinlock));
4201 :
4202 : /* Rate limiting on this bdev enabled */
4203 83 : if (qos) {
4204 17 : if (qos->ch == NULL) {
4205 : struct spdk_io_channel *io_ch;
4206 :
4207 9 : SPDK_DEBUGLOG(bdev, "Selecting channel %p as QoS channel for bdev %s on thread %p\n", ch,
4208 : bdev->name, spdk_get_thread());
4209 :
4210 : /* No qos channel has been selected, so set one up */
4211 :
4212 : /* Take another reference to ch */
4213 9 : io_ch = spdk_get_io_channel(__bdev_to_io_dev(bdev));
4214 9 : assert(io_ch != NULL);
4215 9 : qos->ch = ch;
4216 :
4217 9 : qos->thread = spdk_io_channel_get_thread(io_ch);
4218 :
4219 45 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4220 36 : if (bdev_qos_is_iops_rate_limit(i) == true) {
4221 9 : qos->rate_limits[i].min_per_timeslice =
4222 : SPDK_BDEV_QOS_MIN_IO_PER_TIMESLICE;
4223 9 : } else {
4224 27 : qos->rate_limits[i].min_per_timeslice =
4225 : SPDK_BDEV_QOS_MIN_BYTE_PER_TIMESLICE;
4226 : }
4227 :
4228 36 : if (qos->rate_limits[i].limit == 0) {
4229 2 : qos->rate_limits[i].limit = SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
4230 2 : }
4231 36 : }
4232 9 : bdev_qos_update_max_quota_per_timeslice(qos);
4233 9 : qos->timeslice_size =
4234 9 : SPDK_BDEV_QOS_TIMESLICE_IN_USEC * spdk_get_ticks_hz() / SPDK_SEC_TO_USEC;
4235 9 : qos->last_timeslice = spdk_get_ticks();
4236 9 : qos->poller = SPDK_POLLER_REGISTER(bdev_channel_poll_qos,
4237 : bdev,
4238 : SPDK_BDEV_QOS_TIMESLICE_IN_USEC);
4239 9 : }
4240 :
4241 17 : ch->flags |= BDEV_CH_QOS_ENABLED;
4242 17 : }
4243 83 : }
4244 :
4245 : struct poll_timeout_ctx {
4246 : struct spdk_bdev_desc *desc;
4247 : uint64_t timeout_in_sec;
4248 : spdk_bdev_io_timeout_cb cb_fn;
4249 : void *cb_arg;
4250 : };
4251 :
4252 : static void
4253 268 : bdev_desc_free(struct spdk_bdev_desc *desc)
4254 : {
4255 268 : spdk_spin_destroy(&desc->spinlock);
4256 268 : free(desc->media_events_buffer);
4257 268 : free(desc);
4258 268 : }
4259 :
4260 : static void
4261 8 : bdev_channel_poll_timeout_io_done(struct spdk_bdev *bdev, void *_ctx, int status)
4262 : {
4263 8 : struct poll_timeout_ctx *ctx = _ctx;
4264 8 : struct spdk_bdev_desc *desc = ctx->desc;
4265 :
4266 8 : free(ctx);
4267 :
4268 8 : spdk_spin_lock(&desc->spinlock);
4269 8 : desc->refs--;
4270 8 : if (desc->closed == true && desc->refs == 0) {
4271 1 : spdk_spin_unlock(&desc->spinlock);
4272 1 : bdev_desc_free(desc);
4273 1 : return;
4274 : }
4275 7 : spdk_spin_unlock(&desc->spinlock);
4276 8 : }
4277 :
4278 : static void
4279 13 : bdev_channel_poll_timeout_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
4280 : struct spdk_io_channel *io_ch, void *_ctx)
4281 : {
4282 13 : struct poll_timeout_ctx *ctx = _ctx;
4283 13 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
4284 13 : struct spdk_bdev_desc *desc = ctx->desc;
4285 : struct spdk_bdev_io *bdev_io;
4286 : uint64_t now;
4287 :
4288 13 : spdk_spin_lock(&desc->spinlock);
4289 13 : if (desc->closed == true) {
4290 1 : spdk_spin_unlock(&desc->spinlock);
4291 1 : spdk_bdev_for_each_channel_continue(i, -1);
4292 1 : return;
4293 : }
4294 12 : spdk_spin_unlock(&desc->spinlock);
4295 :
4296 12 : now = spdk_get_ticks();
4297 22 : TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) {
4298 : /* Exclude any I/O that are generated via splitting. */
4299 15 : if (bdev_io->internal.cb == bdev_io_split_done) {
4300 3 : continue;
4301 : }
4302 :
4303 : /* Once we find an I/O that has not timed out, we can immediately
4304 : * exit the loop.
4305 : */
4306 24 : if (now < (bdev_io->internal.submit_tsc +
4307 12 : ctx->timeout_in_sec * spdk_get_ticks_hz())) {
4308 5 : goto end;
4309 : }
4310 :
4311 7 : if (bdev_io->internal.desc == desc) {
4312 7 : ctx->cb_fn(ctx->cb_arg, bdev_io);
4313 7 : }
4314 14 : }
4315 :
4316 : end:
4317 12 : spdk_bdev_for_each_channel_continue(i, 0);
4318 13 : }
4319 :
4320 : static int
4321 8 : bdev_poll_timeout_io(void *arg)
4322 : {
4323 8 : struct spdk_bdev_desc *desc = arg;
4324 8 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
4325 : struct poll_timeout_ctx *ctx;
4326 :
4327 8 : ctx = calloc(1, sizeof(struct poll_timeout_ctx));
4328 8 : if (!ctx) {
4329 0 : SPDK_ERRLOG("failed to allocate memory\n");
4330 0 : return SPDK_POLLER_BUSY;
4331 : }
4332 8 : ctx->desc = desc;
4333 8 : ctx->cb_arg = desc->cb_arg;
4334 8 : ctx->cb_fn = desc->cb_fn;
4335 8 : ctx->timeout_in_sec = desc->timeout_in_sec;
4336 :
4337 : /* Take a ref on the descriptor in case it gets closed while we are checking
4338 : * all of the channels.
4339 : */
4340 8 : spdk_spin_lock(&desc->spinlock);
4341 8 : desc->refs++;
4342 8 : spdk_spin_unlock(&desc->spinlock);
4343 :
4344 8 : spdk_bdev_for_each_channel(bdev, bdev_channel_poll_timeout_io, ctx,
4345 : bdev_channel_poll_timeout_io_done);
4346 :
4347 8 : return SPDK_POLLER_BUSY;
4348 8 : }
4349 :
4350 : int
4351 5 : spdk_bdev_set_timeout(struct spdk_bdev_desc *desc, uint64_t timeout_in_sec,
4352 : spdk_bdev_io_timeout_cb cb_fn, void *cb_arg)
4353 : {
4354 5 : assert(desc->thread == spdk_get_thread());
4355 :
4356 5 : spdk_poller_unregister(&desc->io_timeout_poller);
4357 :
4358 5 : if (timeout_in_sec) {
4359 4 : assert(cb_fn != NULL);
4360 4 : desc->io_timeout_poller = SPDK_POLLER_REGISTER(bdev_poll_timeout_io,
4361 : desc,
4362 : SPDK_BDEV_IO_POLL_INTERVAL_IN_MSEC * SPDK_SEC_TO_USEC /
4363 : 1000);
4364 4 : if (desc->io_timeout_poller == NULL) {
4365 0 : SPDK_ERRLOG("can not register the desc timeout IO poller\n");
4366 0 : return -1;
4367 : }
4368 4 : }
4369 :
4370 5 : desc->cb_fn = cb_fn;
4371 5 : desc->cb_arg = cb_arg;
4372 5 : desc->timeout_in_sec = timeout_in_sec;
4373 :
4374 5 : return 0;
4375 5 : }
4376 :
4377 : static int
4378 76 : bdev_channel_create(void *io_device, void *ctx_buf)
4379 : {
4380 76 : struct spdk_bdev *bdev = __bdev_from_io_dev(io_device);
4381 76 : struct spdk_bdev_channel *ch = ctx_buf;
4382 : struct spdk_io_channel *mgmt_io_ch;
4383 : struct spdk_bdev_mgmt_channel *mgmt_ch;
4384 : struct spdk_bdev_shared_resource *shared_resource;
4385 : struct lba_range *range;
4386 :
4387 76 : ch->bdev = bdev;
4388 76 : ch->channel = bdev->fn_table->get_io_channel(bdev->ctxt);
4389 76 : if (!ch->channel) {
4390 2 : return -1;
4391 : }
4392 :
4393 74 : ch->accel_channel = spdk_accel_get_io_channel();
4394 74 : if (!ch->accel_channel) {
4395 0 : spdk_put_io_channel(ch->channel);
4396 0 : return -1;
4397 : }
4398 :
4399 74 : spdk_trace_record(TRACE_BDEV_IOCH_CREATE, bdev->internal.trace_id, 0, 0,
4400 : spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel)));
4401 :
4402 74 : assert(ch->histogram == NULL);
4403 74 : if (bdev->internal.histogram_enabled) {
4404 0 : ch->histogram = spdk_histogram_data_alloc();
4405 0 : if (ch->histogram == NULL) {
4406 0 : SPDK_ERRLOG("Could not allocate histogram\n");
4407 0 : }
4408 0 : }
4409 :
4410 74 : mgmt_io_ch = spdk_get_io_channel(&g_bdev_mgr);
4411 74 : if (!mgmt_io_ch) {
4412 0 : spdk_put_io_channel(ch->channel);
4413 0 : spdk_put_io_channel(ch->accel_channel);
4414 0 : return -1;
4415 : }
4416 :
4417 74 : mgmt_ch = __io_ch_to_bdev_mgmt_ch(mgmt_io_ch);
4418 76 : TAILQ_FOREACH(shared_resource, &mgmt_ch->shared_resources, link) {
4419 3 : if (shared_resource->shared_ch == ch->channel) {
4420 1 : spdk_put_io_channel(mgmt_io_ch);
4421 1 : shared_resource->ref++;
4422 1 : break;
4423 : }
4424 2 : }
4425 :
4426 74 : if (shared_resource == NULL) {
4427 73 : shared_resource = calloc(1, sizeof(*shared_resource));
4428 73 : if (shared_resource == NULL) {
4429 0 : spdk_put_io_channel(ch->channel);
4430 0 : spdk_put_io_channel(ch->accel_channel);
4431 0 : spdk_put_io_channel(mgmt_io_ch);
4432 0 : return -1;
4433 : }
4434 :
4435 73 : shared_resource->mgmt_ch = mgmt_ch;
4436 73 : shared_resource->io_outstanding = 0;
4437 73 : TAILQ_INIT(&shared_resource->nomem_io);
4438 73 : shared_resource->nomem_threshold = 0;
4439 73 : shared_resource->shared_ch = ch->channel;
4440 73 : shared_resource->ref = 1;
4441 73 : TAILQ_INSERT_TAIL(&mgmt_ch->shared_resources, shared_resource, link);
4442 73 : }
4443 :
4444 74 : ch->io_outstanding = 0;
4445 74 : TAILQ_INIT(&ch->queued_resets);
4446 74 : TAILQ_INIT(&ch->locked_ranges);
4447 74 : TAILQ_INIT(&ch->qos_queued_io);
4448 74 : ch->flags = 0;
4449 74 : ch->trace_id = bdev->internal.trace_id;
4450 74 : ch->shared_resource = shared_resource;
4451 :
4452 74 : TAILQ_INIT(&ch->io_submitted);
4453 74 : TAILQ_INIT(&ch->io_locked);
4454 74 : TAILQ_INIT(&ch->io_accel_exec);
4455 74 : TAILQ_INIT(&ch->io_memory_domain);
4456 :
4457 74 : ch->stat = bdev_alloc_io_stat(false);
4458 74 : if (ch->stat == NULL) {
4459 0 : bdev_channel_destroy_resource(ch);
4460 0 : return -1;
4461 : }
4462 :
4463 74 : ch->stat->ticks_rate = spdk_get_ticks_hz();
4464 :
4465 : #ifdef SPDK_CONFIG_VTUNE
4466 : {
4467 : char *name;
4468 : __itt_init_ittlib(NULL, 0);
4469 : name = spdk_sprintf_alloc("spdk_bdev_%s_%p", ch->bdev->name, ch);
4470 : if (!name) {
4471 : bdev_channel_destroy_resource(ch);
4472 : return -1;
4473 : }
4474 : ch->handle = __itt_string_handle_create(name);
4475 : free(name);
4476 : ch->start_tsc = spdk_get_ticks();
4477 : ch->interval_tsc = spdk_get_ticks_hz() / 100;
4478 : ch->prev_stat = bdev_alloc_io_stat(false);
4479 : if (ch->prev_stat == NULL) {
4480 : bdev_channel_destroy_resource(ch);
4481 : return -1;
4482 : }
4483 : }
4484 : #endif
4485 :
4486 74 : spdk_spin_lock(&bdev->internal.spinlock);
4487 74 : bdev_enable_qos(bdev, ch);
4488 :
4489 75 : TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) {
4490 : struct lba_range *new_range;
4491 :
4492 1 : new_range = calloc(1, sizeof(*new_range));
4493 1 : if (new_range == NULL) {
4494 0 : spdk_spin_unlock(&bdev->internal.spinlock);
4495 0 : bdev_channel_destroy_resource(ch);
4496 0 : return -1;
4497 : }
4498 1 : new_range->length = range->length;
4499 1 : new_range->offset = range->offset;
4500 1 : new_range->locked_ctx = range->locked_ctx;
4501 1 : TAILQ_INSERT_TAIL(&ch->locked_ranges, new_range, tailq);
4502 1 : }
4503 :
4504 74 : spdk_spin_unlock(&bdev->internal.spinlock);
4505 :
4506 74 : return 0;
4507 76 : }
4508 :
4509 : static int
4510 0 : bdev_abort_all_buf_io_cb(struct spdk_iobuf_channel *ch, struct spdk_iobuf_entry *entry,
4511 : void *cb_ctx)
4512 : {
4513 0 : struct spdk_bdev_channel *bdev_ch = cb_ctx;
4514 : struct spdk_bdev_io *bdev_io;
4515 : uint64_t buf_len;
4516 :
4517 0 : bdev_io = SPDK_CONTAINEROF(entry, struct spdk_bdev_io, internal.iobuf);
4518 0 : if (bdev_io->internal.ch == bdev_ch) {
4519 0 : buf_len = bdev_io_get_max_buf_len(bdev_io, bdev_io->internal.buf.len);
4520 0 : spdk_iobuf_entry_abort(ch, entry, buf_len);
4521 0 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
4522 0 : }
4523 :
4524 0 : return 0;
4525 : }
4526 :
4527 : /*
4528 : * Abort I/O that are waiting on a data buffer.
4529 : */
4530 : static void
4531 96 : bdev_abort_all_buf_io(struct spdk_bdev_mgmt_channel *mgmt_ch, struct spdk_bdev_channel *ch)
4532 : {
4533 96 : spdk_iobuf_for_each_entry(&mgmt_ch->iobuf, bdev_abort_all_buf_io_cb, ch);
4534 96 : }
4535 :
4536 : /*
4537 : * Abort I/O that are queued waiting for submission. These types of I/O are
4538 : * linked using the spdk_bdev_io link TAILQ_ENTRY.
4539 : */
4540 : static void
4541 188 : bdev_abort_all_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_channel *ch)
4542 : {
4543 : struct spdk_bdev_io *bdev_io, *tmp;
4544 :
4545 228 : TAILQ_FOREACH_SAFE(bdev_io, queue, internal.link, tmp) {
4546 40 : if (bdev_io->internal.ch == ch) {
4547 40 : TAILQ_REMOVE(queue, bdev_io, internal.link);
4548 : /*
4549 : * spdk_bdev_io_complete() assumes that the completed I/O had
4550 : * been submitted to the bdev module. Since in this case it
4551 : * hadn't, bump io_outstanding to account for the decrement
4552 : * that spdk_bdev_io_complete() will do.
4553 : */
4554 40 : if (bdev_io->type != SPDK_BDEV_IO_TYPE_RESET) {
4555 39 : bdev_io_increment_outstanding(ch, ch->shared_resource);
4556 39 : }
4557 40 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_ABORTED);
4558 40 : }
4559 40 : }
4560 188 : }
4561 :
4562 : static bool
4563 18 : bdev_abort_queued_io(bdev_io_tailq_t *queue, struct spdk_bdev_io *bio_to_abort)
4564 : {
4565 : struct spdk_bdev_io *bdev_io;
4566 :
4567 18 : TAILQ_FOREACH(bdev_io, queue, internal.link) {
4568 0 : if (bdev_io == bio_to_abort) {
4569 0 : TAILQ_REMOVE(queue, bio_to_abort, internal.link);
4570 0 : spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED);
4571 0 : return true;
4572 : }
4573 0 : }
4574 :
4575 18 : return false;
4576 18 : }
4577 :
4578 : static int
4579 0 : bdev_abort_buf_io_cb(struct spdk_iobuf_channel *ch, struct spdk_iobuf_entry *entry, void *cb_ctx)
4580 : {
4581 0 : struct spdk_bdev_io *bdev_io, *bio_to_abort = cb_ctx;
4582 : uint64_t buf_len;
4583 :
4584 0 : bdev_io = SPDK_CONTAINEROF(entry, struct spdk_bdev_io, internal.iobuf);
4585 0 : if (bdev_io == bio_to_abort) {
4586 0 : buf_len = bdev_io_get_max_buf_len(bdev_io, bdev_io->internal.buf.len);
4587 0 : spdk_iobuf_entry_abort(ch, entry, buf_len);
4588 0 : spdk_bdev_io_complete(bio_to_abort, SPDK_BDEV_IO_STATUS_ABORTED);
4589 0 : return 1;
4590 : }
4591 :
4592 0 : return 0;
4593 0 : }
4594 :
4595 : static bool
4596 16 : bdev_abort_buf_io(struct spdk_bdev_mgmt_channel *mgmt_ch, struct spdk_bdev_io *bio_to_abort)
4597 : {
4598 : int rc;
4599 :
4600 16 : rc = spdk_iobuf_for_each_entry(&mgmt_ch->iobuf, bdev_abort_buf_io_cb, bio_to_abort);
4601 16 : return rc == 1;
4602 : }
4603 :
4604 : static void
4605 7 : bdev_qos_channel_destroy(void *cb_arg)
4606 : {
4607 7 : struct spdk_bdev_qos *qos = cb_arg;
4608 :
4609 7 : spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
4610 7 : spdk_poller_unregister(&qos->poller);
4611 :
4612 7 : SPDK_DEBUGLOG(bdev, "Free QoS %p.\n", qos);
4613 :
4614 7 : free(qos);
4615 7 : }
4616 :
4617 : static int
4618 7 : bdev_qos_destroy(struct spdk_bdev *bdev)
4619 : {
4620 : int i;
4621 :
4622 : /*
4623 : * Cleanly shutting down the QoS poller is tricky, because
4624 : * during the asynchronous operation the user could open
4625 : * a new descriptor and create a new channel, spawning
4626 : * a new QoS poller.
4627 : *
4628 : * The strategy is to create a new QoS structure here and swap it
4629 : * in. The shutdown path then continues to refer to the old one
4630 : * until it completes and then releases it.
4631 : */
4632 : struct spdk_bdev_qos *new_qos, *old_qos;
4633 :
4634 7 : old_qos = bdev->internal.qos;
4635 :
4636 7 : new_qos = calloc(1, sizeof(*new_qos));
4637 7 : if (!new_qos) {
4638 0 : SPDK_ERRLOG("Unable to allocate memory to shut down QoS.\n");
4639 0 : return -ENOMEM;
4640 : }
4641 :
4642 : /* Copy the old QoS data into the newly allocated structure */
4643 7 : memcpy(new_qos, old_qos, sizeof(*new_qos));
4644 :
4645 : /* Zero out the key parts of the QoS structure */
4646 7 : new_qos->ch = NULL;
4647 7 : new_qos->thread = NULL;
4648 7 : new_qos->poller = NULL;
4649 : /*
4650 : * The limit member of spdk_bdev_qos_limit structure is not zeroed.
4651 : * It will be used later for the new QoS structure.
4652 : */
4653 35 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
4654 28 : new_qos->rate_limits[i].remaining_this_timeslice = 0;
4655 28 : new_qos->rate_limits[i].min_per_timeslice = 0;
4656 28 : new_qos->rate_limits[i].max_per_timeslice = 0;
4657 28 : }
4658 :
4659 7 : bdev->internal.qos = new_qos;
4660 :
4661 7 : if (old_qos->thread == NULL) {
4662 0 : free(old_qos);
4663 0 : } else {
4664 7 : spdk_thread_send_msg(old_qos->thread, bdev_qos_channel_destroy, old_qos);
4665 : }
4666 :
4667 : /* It is safe to continue with destroying the bdev even though the QoS channel hasn't
4668 : * been destroyed yet. The destruction path will end up waiting for the final
4669 : * channel to be put before it releases resources. */
4670 :
4671 7 : return 0;
4672 7 : }
4673 :
4674 : void
4675 78 : spdk_bdev_add_io_stat(struct spdk_bdev_io_stat *total, struct spdk_bdev_io_stat *add)
4676 : {
4677 78 : total->bytes_read += add->bytes_read;
4678 78 : total->num_read_ops += add->num_read_ops;
4679 78 : total->bytes_written += add->bytes_written;
4680 78 : total->num_write_ops += add->num_write_ops;
4681 78 : total->bytes_unmapped += add->bytes_unmapped;
4682 78 : total->num_unmap_ops += add->num_unmap_ops;
4683 78 : total->bytes_copied += add->bytes_copied;
4684 78 : total->num_copy_ops += add->num_copy_ops;
4685 78 : total->read_latency_ticks += add->read_latency_ticks;
4686 78 : total->write_latency_ticks += add->write_latency_ticks;
4687 78 : total->unmap_latency_ticks += add->unmap_latency_ticks;
4688 78 : total->copy_latency_ticks += add->copy_latency_ticks;
4689 78 : if (total->max_read_latency_ticks < add->max_read_latency_ticks) {
4690 7 : total->max_read_latency_ticks = add->max_read_latency_ticks;
4691 7 : }
4692 78 : if (total->min_read_latency_ticks > add->min_read_latency_ticks) {
4693 39 : total->min_read_latency_ticks = add->min_read_latency_ticks;
4694 39 : }
4695 78 : if (total->max_write_latency_ticks < add->max_write_latency_ticks) {
4696 4 : total->max_write_latency_ticks = add->max_write_latency_ticks;
4697 4 : }
4698 78 : if (total->min_write_latency_ticks > add->min_write_latency_ticks) {
4699 24 : total->min_write_latency_ticks = add->min_write_latency_ticks;
4700 24 : }
4701 78 : if (total->max_unmap_latency_ticks < add->max_unmap_latency_ticks) {
4702 0 : total->max_unmap_latency_ticks = add->max_unmap_latency_ticks;
4703 0 : }
4704 78 : if (total->min_unmap_latency_ticks > add->min_unmap_latency_ticks) {
4705 3 : total->min_unmap_latency_ticks = add->min_unmap_latency_ticks;
4706 3 : }
4707 78 : if (total->max_copy_latency_ticks < add->max_copy_latency_ticks) {
4708 0 : total->max_copy_latency_ticks = add->max_copy_latency_ticks;
4709 0 : }
4710 78 : if (total->min_copy_latency_ticks > add->min_copy_latency_ticks) {
4711 4 : total->min_copy_latency_ticks = add->min_copy_latency_ticks;
4712 4 : }
4713 78 : }
4714 :
4715 : static void
4716 5 : bdev_get_io_stat(struct spdk_bdev_io_stat *to_stat, struct spdk_bdev_io_stat *from_stat)
4717 : {
4718 5 : memcpy(to_stat, from_stat, offsetof(struct spdk_bdev_io_stat, io_error));
4719 :
4720 5 : if (to_stat->io_error != NULL && from_stat->io_error != NULL) {
4721 0 : memcpy(to_stat->io_error, from_stat->io_error,
4722 : sizeof(struct spdk_bdev_io_error_stat));
4723 0 : }
4724 5 : }
4725 :
4726 : void
4727 211 : spdk_bdev_reset_io_stat(struct spdk_bdev_io_stat *stat, enum spdk_bdev_reset_stat_mode mode)
4728 : {
4729 211 : if (mode == SPDK_BDEV_RESET_STAT_NONE) {
4730 5 : return;
4731 : }
4732 :
4733 206 : stat->max_read_latency_ticks = 0;
4734 206 : stat->min_read_latency_ticks = UINT64_MAX;
4735 206 : stat->max_write_latency_ticks = 0;
4736 206 : stat->min_write_latency_ticks = UINT64_MAX;
4737 206 : stat->max_unmap_latency_ticks = 0;
4738 206 : stat->min_unmap_latency_ticks = UINT64_MAX;
4739 206 : stat->max_copy_latency_ticks = 0;
4740 206 : stat->min_copy_latency_ticks = UINT64_MAX;
4741 :
4742 206 : if (mode != SPDK_BDEV_RESET_STAT_ALL) {
4743 2 : return;
4744 : }
4745 :
4746 204 : stat->bytes_read = 0;
4747 204 : stat->num_read_ops = 0;
4748 204 : stat->bytes_written = 0;
4749 204 : stat->num_write_ops = 0;
4750 204 : stat->bytes_unmapped = 0;
4751 204 : stat->num_unmap_ops = 0;
4752 204 : stat->bytes_copied = 0;
4753 204 : stat->num_copy_ops = 0;
4754 204 : stat->read_latency_ticks = 0;
4755 204 : stat->write_latency_ticks = 0;
4756 204 : stat->unmap_latency_ticks = 0;
4757 204 : stat->copy_latency_ticks = 0;
4758 :
4759 204 : if (stat->io_error != NULL) {
4760 129 : memset(stat->io_error, 0, sizeof(struct spdk_bdev_io_error_stat));
4761 129 : }
4762 211 : }
4763 :
4764 : struct spdk_bdev_io_stat *
4765 202 : bdev_alloc_io_stat(bool io_error_stat)
4766 : {
4767 : struct spdk_bdev_io_stat *stat;
4768 :
4769 202 : stat = malloc(sizeof(struct spdk_bdev_io_stat));
4770 202 : if (stat == NULL) {
4771 0 : return NULL;
4772 : }
4773 :
4774 202 : if (io_error_stat) {
4775 128 : stat->io_error = malloc(sizeof(struct spdk_bdev_io_error_stat));
4776 128 : if (stat->io_error == NULL) {
4777 0 : free(stat);
4778 0 : return NULL;
4779 : }
4780 128 : } else {
4781 74 : stat->io_error = NULL;
4782 : }
4783 :
4784 202 : spdk_bdev_reset_io_stat(stat, SPDK_BDEV_RESET_STAT_ALL);
4785 :
4786 202 : return stat;
4787 202 : }
4788 :
4789 : void
4790 202 : bdev_free_io_stat(struct spdk_bdev_io_stat *stat)
4791 : {
4792 202 : if (stat != NULL) {
4793 202 : free(stat->io_error);
4794 202 : free(stat);
4795 202 : }
4796 202 : }
4797 :
4798 : void
4799 0 : spdk_bdev_dump_io_stat_json(struct spdk_bdev_io_stat *stat, struct spdk_json_write_ctx *w)
4800 : {
4801 : int i;
4802 :
4803 0 : spdk_json_write_named_uint64(w, "bytes_read", stat->bytes_read);
4804 0 : spdk_json_write_named_uint64(w, "num_read_ops", stat->num_read_ops);
4805 0 : spdk_json_write_named_uint64(w, "bytes_written", stat->bytes_written);
4806 0 : spdk_json_write_named_uint64(w, "num_write_ops", stat->num_write_ops);
4807 0 : spdk_json_write_named_uint64(w, "bytes_unmapped", stat->bytes_unmapped);
4808 0 : spdk_json_write_named_uint64(w, "num_unmap_ops", stat->num_unmap_ops);
4809 0 : spdk_json_write_named_uint64(w, "bytes_copied", stat->bytes_copied);
4810 0 : spdk_json_write_named_uint64(w, "num_copy_ops", stat->num_copy_ops);
4811 0 : spdk_json_write_named_uint64(w, "read_latency_ticks", stat->read_latency_ticks);
4812 0 : spdk_json_write_named_uint64(w, "max_read_latency_ticks", stat->max_read_latency_ticks);
4813 0 : spdk_json_write_named_uint64(w, "min_read_latency_ticks",
4814 0 : stat->min_read_latency_ticks != UINT64_MAX ?
4815 0 : stat->min_read_latency_ticks : 0);
4816 0 : spdk_json_write_named_uint64(w, "write_latency_ticks", stat->write_latency_ticks);
4817 0 : spdk_json_write_named_uint64(w, "max_write_latency_ticks", stat->max_write_latency_ticks);
4818 0 : spdk_json_write_named_uint64(w, "min_write_latency_ticks",
4819 0 : stat->min_write_latency_ticks != UINT64_MAX ?
4820 0 : stat->min_write_latency_ticks : 0);
4821 0 : spdk_json_write_named_uint64(w, "unmap_latency_ticks", stat->unmap_latency_ticks);
4822 0 : spdk_json_write_named_uint64(w, "max_unmap_latency_ticks", stat->max_unmap_latency_ticks);
4823 0 : spdk_json_write_named_uint64(w, "min_unmap_latency_ticks",
4824 0 : stat->min_unmap_latency_ticks != UINT64_MAX ?
4825 0 : stat->min_unmap_latency_ticks : 0);
4826 0 : spdk_json_write_named_uint64(w, "copy_latency_ticks", stat->copy_latency_ticks);
4827 0 : spdk_json_write_named_uint64(w, "max_copy_latency_ticks", stat->max_copy_latency_ticks);
4828 0 : spdk_json_write_named_uint64(w, "min_copy_latency_ticks",
4829 0 : stat->min_copy_latency_ticks != UINT64_MAX ?
4830 0 : stat->min_copy_latency_ticks : 0);
4831 :
4832 0 : if (stat->io_error != NULL) {
4833 0 : spdk_json_write_named_object_begin(w, "io_error");
4834 0 : for (i = 0; i < -SPDK_MIN_BDEV_IO_STATUS; i++) {
4835 0 : if (stat->io_error->error_status[i] != 0) {
4836 0 : spdk_json_write_named_uint32(w, bdev_io_status_get_string(-(i + 1)),
4837 0 : stat->io_error->error_status[i]);
4838 0 : }
4839 0 : }
4840 0 : spdk_json_write_object_end(w);
4841 0 : }
4842 0 : }
4843 :
4844 : static void
4845 78 : bdev_channel_abort_queued_ios(struct spdk_bdev_channel *ch)
4846 : {
4847 78 : struct spdk_bdev_shared_resource *shared_resource = ch->shared_resource;
4848 78 : struct spdk_bdev_mgmt_channel *mgmt_ch = shared_resource->mgmt_ch;
4849 :
4850 78 : bdev_abort_all_queued_io(&shared_resource->nomem_io, ch);
4851 78 : bdev_abort_all_buf_io(mgmt_ch, ch);
4852 78 : }
4853 :
4854 : static void
4855 74 : bdev_channel_destroy(void *io_device, void *ctx_buf)
4856 : {
4857 74 : struct spdk_bdev_channel *ch = ctx_buf;
4858 :
4859 74 : SPDK_DEBUGLOG(bdev, "Destroying channel %p for bdev %s on thread %p\n", ch, ch->bdev->name,
4860 : spdk_get_thread());
4861 :
4862 74 : spdk_trace_record(TRACE_BDEV_IOCH_DESTROY, ch->bdev->internal.trace_id, 0, 0,
4863 : spdk_thread_get_id(spdk_io_channel_get_thread(ch->channel)));
4864 :
4865 : /* This channel is going away, so add its statistics into the bdev so that they don't get lost. */
4866 74 : spdk_spin_lock(&ch->bdev->internal.spinlock);
4867 74 : spdk_bdev_add_io_stat(ch->bdev->internal.stat, ch->stat);
4868 74 : spdk_spin_unlock(&ch->bdev->internal.spinlock);
4869 :
4870 74 : bdev_abort_all_queued_io(&ch->queued_resets, ch);
4871 :
4872 74 : bdev_channel_abort_queued_ios(ch);
4873 :
4874 74 : if (ch->histogram) {
4875 0 : spdk_histogram_data_free(ch->histogram);
4876 0 : }
4877 :
4878 74 : bdev_channel_destroy_resource(ch);
4879 74 : }
4880 :
4881 : /*
4882 : * If the name already exists in the global bdev name tree, RB_INSERT() returns a pointer
4883 : * to it. Hence we do not have to call bdev_get_by_name() when using this function.
4884 : */
4885 : static int
4886 259 : bdev_name_add(struct spdk_bdev_name *bdev_name, struct spdk_bdev *bdev, const char *name)
4887 : {
4888 : struct spdk_bdev_name *tmp;
4889 :
4890 259 : bdev_name->name = strdup(name);
4891 259 : if (bdev_name->name == NULL) {
4892 0 : SPDK_ERRLOG("Unable to allocate bdev name\n");
4893 0 : return -ENOMEM;
4894 : }
4895 :
4896 259 : bdev_name->bdev = bdev;
4897 :
4898 259 : spdk_spin_lock(&g_bdev_mgr.spinlock);
4899 259 : tmp = RB_INSERT(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
4900 259 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
4901 :
4902 259 : if (tmp != NULL) {
4903 4 : SPDK_ERRLOG("Bdev name %s already exists\n", name);
4904 4 : free(bdev_name->name);
4905 4 : return -EEXIST;
4906 : }
4907 :
4908 255 : return 0;
4909 259 : }
4910 :
4911 : static void
4912 255 : bdev_name_del_unsafe(struct spdk_bdev_name *bdev_name)
4913 : {
4914 255 : RB_REMOVE(bdev_name_tree, &g_bdev_mgr.bdev_names, bdev_name);
4915 255 : free(bdev_name->name);
4916 255 : }
4917 :
4918 : static void
4919 5 : bdev_name_del(struct spdk_bdev_name *bdev_name)
4920 : {
4921 5 : spdk_spin_lock(&g_bdev_mgr.spinlock);
4922 5 : bdev_name_del_unsafe(bdev_name);
4923 5 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
4924 5 : }
4925 :
4926 : int
4927 134 : spdk_bdev_alias_add(struct spdk_bdev *bdev, const char *alias)
4928 : {
4929 : struct spdk_bdev_alias *tmp;
4930 : int ret;
4931 :
4932 134 : if (alias == NULL) {
4933 1 : SPDK_ERRLOG("Empty alias passed\n");
4934 1 : return -EINVAL;
4935 : }
4936 :
4937 133 : tmp = calloc(1, sizeof(*tmp));
4938 133 : if (tmp == NULL) {
4939 0 : SPDK_ERRLOG("Unable to allocate alias\n");
4940 0 : return -ENOMEM;
4941 : }
4942 :
4943 133 : ret = bdev_name_add(&tmp->alias, bdev, alias);
4944 133 : if (ret != 0) {
4945 4 : free(tmp);
4946 4 : return ret;
4947 : }
4948 :
4949 129 : TAILQ_INSERT_TAIL(&bdev->aliases, tmp, tailq);
4950 :
4951 129 : return 0;
4952 134 : }
4953 :
4954 : static int
4955 130 : bdev_alias_del(struct spdk_bdev *bdev, const char *alias,
4956 : void (*alias_del_fn)(struct spdk_bdev_name *n))
4957 : {
4958 : struct spdk_bdev_alias *tmp;
4959 :
4960 135 : TAILQ_FOREACH(tmp, &bdev->aliases, tailq) {
4961 131 : if (strcmp(alias, tmp->alias.name) == 0) {
4962 126 : TAILQ_REMOVE(&bdev->aliases, tmp, tailq);
4963 126 : alias_del_fn(&tmp->alias);
4964 126 : free(tmp);
4965 126 : return 0;
4966 : }
4967 5 : }
4968 :
4969 4 : return -ENOENT;
4970 130 : }
4971 :
4972 : int
4973 4 : spdk_bdev_alias_del(struct spdk_bdev *bdev, const char *alias)
4974 : {
4975 : int rc;
4976 :
4977 4 : rc = bdev_alias_del(bdev, alias, bdev_name_del);
4978 4 : if (rc == -ENOENT) {
4979 2 : SPDK_INFOLOG(bdev, "Alias %s does not exist\n", alias);
4980 2 : }
4981 :
4982 4 : return rc;
4983 : }
4984 :
4985 : void
4986 2 : spdk_bdev_alias_del_all(struct spdk_bdev *bdev)
4987 : {
4988 : struct spdk_bdev_alias *p, *tmp;
4989 :
4990 5 : TAILQ_FOREACH_SAFE(p, &bdev->aliases, tailq, tmp) {
4991 3 : TAILQ_REMOVE(&bdev->aliases, p, tailq);
4992 3 : bdev_name_del(&p->alias);
4993 3 : free(p);
4994 3 : }
4995 2 : }
4996 :
4997 : struct spdk_io_channel *
4998 76 : spdk_bdev_get_io_channel(struct spdk_bdev_desc *desc)
4999 : {
5000 76 : return spdk_get_io_channel(__bdev_to_io_dev(spdk_bdev_desc_get_bdev(desc)));
5001 : }
5002 :
5003 : void *
5004 0 : spdk_bdev_get_module_ctx(struct spdk_bdev_desc *desc)
5005 : {
5006 0 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5007 0 : void *ctx = NULL;
5008 :
5009 0 : if (bdev->fn_table->get_module_ctx) {
5010 0 : ctx = bdev->fn_table->get_module_ctx(bdev->ctxt);
5011 0 : }
5012 :
5013 0 : return ctx;
5014 : }
5015 :
5016 : const char *
5017 0 : spdk_bdev_get_module_name(const struct spdk_bdev *bdev)
5018 : {
5019 0 : return bdev->module->name;
5020 : }
5021 :
5022 : const char *
5023 255 : spdk_bdev_get_name(const struct spdk_bdev *bdev)
5024 : {
5025 255 : return bdev->name;
5026 : }
5027 :
5028 : const char *
5029 0 : spdk_bdev_get_product_name(const struct spdk_bdev *bdev)
5030 : {
5031 0 : return bdev->product_name;
5032 : }
5033 :
5034 : const struct spdk_bdev_aliases_list *
5035 0 : spdk_bdev_get_aliases(const struct spdk_bdev *bdev)
5036 : {
5037 0 : return &bdev->aliases;
5038 : }
5039 :
5040 : uint32_t
5041 5 : spdk_bdev_get_block_size(const struct spdk_bdev *bdev)
5042 : {
5043 5 : return bdev->blocklen;
5044 : }
5045 :
5046 : uint32_t
5047 0 : spdk_bdev_get_write_unit_size(const struct spdk_bdev *bdev)
5048 : {
5049 0 : return bdev->write_unit_size;
5050 : }
5051 :
5052 : uint64_t
5053 0 : spdk_bdev_get_num_blocks(const struct spdk_bdev *bdev)
5054 : {
5055 0 : return bdev->blockcnt;
5056 : }
5057 :
5058 : const char *
5059 0 : spdk_bdev_get_qos_rpc_type(enum spdk_bdev_qos_rate_limit_type type)
5060 : {
5061 0 : return qos_rpc_type[type];
5062 : }
5063 :
5064 : void
5065 0 : spdk_bdev_get_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
5066 : {
5067 : int i;
5068 :
5069 0 : memset(limits, 0, sizeof(*limits) * SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES);
5070 :
5071 0 : spdk_spin_lock(&bdev->internal.spinlock);
5072 0 : if (bdev->internal.qos) {
5073 0 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
5074 0 : if (bdev->internal.qos->rate_limits[i].limit !=
5075 : SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
5076 0 : limits[i] = bdev->internal.qos->rate_limits[i].limit;
5077 0 : if (bdev_qos_is_iops_rate_limit(i) == false) {
5078 : /* Change from Byte to Megabyte which is user visible. */
5079 0 : limits[i] = limits[i] / 1024 / 1024;
5080 0 : }
5081 0 : }
5082 0 : }
5083 0 : }
5084 0 : spdk_spin_unlock(&bdev->internal.spinlock);
5085 0 : }
5086 :
5087 : size_t
5088 276 : spdk_bdev_get_buf_align(const struct spdk_bdev *bdev)
5089 : {
5090 276 : return 1 << bdev->required_alignment;
5091 : }
5092 :
5093 : uint32_t
5094 0 : spdk_bdev_get_optimal_io_boundary(const struct spdk_bdev *bdev)
5095 : {
5096 0 : return bdev->optimal_io_boundary;
5097 : }
5098 :
5099 : bool
5100 0 : spdk_bdev_has_write_cache(const struct spdk_bdev *bdev)
5101 : {
5102 0 : return bdev->write_cache;
5103 : }
5104 :
5105 : const struct spdk_uuid *
5106 0 : spdk_bdev_get_uuid(const struct spdk_bdev *bdev)
5107 : {
5108 0 : return &bdev->uuid;
5109 : }
5110 :
5111 : uint16_t
5112 0 : spdk_bdev_get_acwu(const struct spdk_bdev *bdev)
5113 : {
5114 0 : return bdev->acwu;
5115 : }
5116 :
5117 : uint32_t
5118 29 : spdk_bdev_get_md_size(const struct spdk_bdev *bdev)
5119 : {
5120 29 : return bdev->md_len;
5121 : }
5122 :
5123 : bool
5124 131 : spdk_bdev_is_md_interleaved(const struct spdk_bdev *bdev)
5125 : {
5126 131 : return (bdev->md_len != 0) && bdev->md_interleave;
5127 : }
5128 :
5129 : bool
5130 117 : spdk_bdev_is_md_separate(const struct spdk_bdev *bdev)
5131 : {
5132 117 : return (bdev->md_len != 0) && !bdev->md_interleave;
5133 : }
5134 :
5135 : bool
5136 0 : spdk_bdev_is_zoned(const struct spdk_bdev *bdev)
5137 : {
5138 0 : return bdev->zoned;
5139 : }
5140 :
5141 : uint32_t
5142 122 : spdk_bdev_get_data_block_size(const struct spdk_bdev *bdev)
5143 : {
5144 122 : if (spdk_bdev_is_md_interleaved(bdev)) {
5145 0 : return bdev->blocklen - bdev->md_len;
5146 : } else {
5147 122 : return bdev->blocklen;
5148 : }
5149 122 : }
5150 :
5151 : uint32_t
5152 0 : spdk_bdev_get_physical_block_size(const struct spdk_bdev *bdev)
5153 : {
5154 0 : return bdev->phys_blocklen;
5155 : }
5156 :
5157 : static uint32_t
5158 9 : _bdev_get_block_size_with_md(const struct spdk_bdev *bdev)
5159 : {
5160 9 : if (!spdk_bdev_is_md_interleaved(bdev)) {
5161 6 : return bdev->blocklen + bdev->md_len;
5162 : } else {
5163 3 : return bdev->blocklen;
5164 : }
5165 9 : }
5166 :
5167 : /* We have to use the typedef in the function declaration to appease astyle. */
5168 : typedef enum spdk_dif_type spdk_dif_type_t;
5169 : typedef enum spdk_dif_pi_format spdk_dif_pi_format_t;
5170 :
5171 : spdk_dif_type_t
5172 0 : spdk_bdev_get_dif_type(const struct spdk_bdev *bdev)
5173 : {
5174 0 : if (bdev->md_len != 0) {
5175 0 : return bdev->dif_type;
5176 : } else {
5177 0 : return SPDK_DIF_DISABLE;
5178 : }
5179 0 : }
5180 :
5181 : spdk_dif_pi_format_t
5182 0 : spdk_bdev_get_dif_pi_format(const struct spdk_bdev *bdev)
5183 : {
5184 0 : return bdev->dif_pi_format;
5185 : }
5186 :
5187 : bool
5188 0 : spdk_bdev_is_dif_head_of_md(const struct spdk_bdev *bdev)
5189 : {
5190 0 : if (spdk_bdev_get_dif_type(bdev) != SPDK_DIF_DISABLE) {
5191 0 : return bdev->dif_is_head_of_md;
5192 : } else {
5193 0 : return false;
5194 : }
5195 0 : }
5196 :
5197 : bool
5198 0 : spdk_bdev_is_dif_check_enabled(const struct spdk_bdev *bdev,
5199 : enum spdk_dif_check_type check_type)
5200 : {
5201 0 : if (spdk_bdev_get_dif_type(bdev) == SPDK_DIF_DISABLE) {
5202 0 : return false;
5203 : }
5204 :
5205 0 : switch (check_type) {
5206 : case SPDK_DIF_CHECK_TYPE_REFTAG:
5207 0 : return (bdev->dif_check_flags & SPDK_DIF_FLAGS_REFTAG_CHECK) != 0;
5208 : case SPDK_DIF_CHECK_TYPE_APPTAG:
5209 0 : return (bdev->dif_check_flags & SPDK_DIF_FLAGS_APPTAG_CHECK) != 0;
5210 : case SPDK_DIF_CHECK_TYPE_GUARD:
5211 0 : return (bdev->dif_check_flags & SPDK_DIF_FLAGS_GUARD_CHECK) != 0;
5212 : default:
5213 0 : return false;
5214 : }
5215 0 : }
5216 :
5217 : static uint32_t
5218 3 : bdev_get_max_write(const struct spdk_bdev *bdev, uint64_t num_bytes)
5219 : {
5220 : uint64_t aligned_length, max_write_blocks;
5221 :
5222 3 : aligned_length = num_bytes - (spdk_bdev_get_buf_align(bdev) - 1);
5223 3 : max_write_blocks = aligned_length / _bdev_get_block_size_with_md(bdev);
5224 3 : max_write_blocks -= max_write_blocks % bdev->write_unit_size;
5225 :
5226 3 : return max_write_blocks;
5227 : }
5228 :
5229 : uint32_t
5230 1 : spdk_bdev_get_max_copy(const struct spdk_bdev *bdev)
5231 : {
5232 1 : return bdev->max_copy;
5233 : }
5234 :
5235 : uint64_t
5236 0 : spdk_bdev_get_qd(const struct spdk_bdev *bdev)
5237 : {
5238 0 : return bdev->internal.measured_queue_depth;
5239 : }
5240 :
5241 : uint64_t
5242 0 : spdk_bdev_get_qd_sampling_period(const struct spdk_bdev *bdev)
5243 : {
5244 0 : return bdev->internal.period;
5245 : }
5246 :
5247 : uint64_t
5248 0 : spdk_bdev_get_weighted_io_time(const struct spdk_bdev *bdev)
5249 : {
5250 0 : return bdev->internal.weighted_io_time;
5251 : }
5252 :
5253 : uint64_t
5254 0 : spdk_bdev_get_io_time(const struct spdk_bdev *bdev)
5255 : {
5256 0 : return bdev->internal.io_time;
5257 : }
5258 :
5259 0 : union spdk_bdev_nvme_ctratt spdk_bdev_get_nvme_ctratt(struct spdk_bdev *bdev)
5260 : {
5261 0 : return bdev->ctratt;
5262 : }
5263 :
5264 : uint32_t
5265 0 : spdk_bdev_get_nvme_nsid(struct spdk_bdev *bdev)
5266 : {
5267 0 : return bdev->nsid;
5268 : }
5269 :
5270 : uint32_t
5271 0 : spdk_bdev_desc_get_block_size(struct spdk_bdev_desc *desc)
5272 : {
5273 0 : struct spdk_bdev *bdev = desc->bdev;
5274 :
5275 0 : return desc->opts.no_metadata ? bdev->blocklen - bdev->md_len : bdev->blocklen;
5276 : }
5277 :
5278 : uint32_t
5279 0 : spdk_bdev_desc_get_md_size(struct spdk_bdev_desc *desc)
5280 : {
5281 0 : struct spdk_bdev *bdev = desc->bdev;
5282 :
5283 0 : return desc->opts.no_metadata ? 0 : bdev->md_len;
5284 : }
5285 :
5286 : bool
5287 0 : spdk_bdev_desc_is_md_interleaved(struct spdk_bdev_desc *desc)
5288 : {
5289 0 : struct spdk_bdev *bdev = desc->bdev;
5290 :
5291 0 : return desc->opts.no_metadata ? false : spdk_bdev_is_md_interleaved(bdev);
5292 : }
5293 :
5294 : bool
5295 0 : spdk_bdev_desc_is_md_separate(struct spdk_bdev_desc *desc)
5296 : {
5297 0 : struct spdk_bdev *bdev = desc->bdev;
5298 :
5299 0 : return desc->opts.no_metadata ? false : spdk_bdev_is_md_separate(bdev);
5300 : }
5301 :
5302 : spdk_dif_type_t
5303 0 : spdk_bdev_desc_get_dif_type(struct spdk_bdev_desc *desc)
5304 : {
5305 0 : struct spdk_bdev *bdev = desc->bdev;
5306 :
5307 0 : return desc->opts.no_metadata ? SPDK_DIF_DISABLE : spdk_bdev_get_dif_type(bdev);
5308 : }
5309 :
5310 : spdk_dif_pi_format_t
5311 0 : spdk_bdev_desc_get_dif_pi_format(struct spdk_bdev_desc *desc)
5312 : {
5313 0 : struct spdk_bdev *bdev = desc->bdev;
5314 :
5315 0 : return desc->opts.no_metadata ? SPDK_DIF_PI_FORMAT_16 : spdk_bdev_get_dif_pi_format(bdev);
5316 : }
5317 :
5318 : bool
5319 0 : spdk_bdev_desc_is_dif_head_of_md(struct spdk_bdev_desc *desc)
5320 : {
5321 0 : struct spdk_bdev *bdev = desc->bdev;
5322 :
5323 0 : return desc->opts.no_metadata ? false : spdk_bdev_is_dif_head_of_md(bdev);
5324 : }
5325 :
5326 : bool
5327 0 : spdk_bdev_desc_is_dif_check_enabled(struct spdk_bdev_desc *desc,
5328 : enum spdk_dif_check_type check_type)
5329 : {
5330 0 : struct spdk_bdev *bdev = desc->bdev;
5331 :
5332 0 : return desc->opts.no_metadata ? false : spdk_bdev_is_dif_check_enabled(bdev, check_type);
5333 : }
5334 :
5335 : static void bdev_update_qd_sampling_period(void *ctx);
5336 :
5337 : static void
5338 1 : _calculate_measured_qd_cpl(struct spdk_bdev *bdev, void *_ctx, int status)
5339 : {
5340 1 : bdev->internal.measured_queue_depth = bdev->internal.temporary_queue_depth;
5341 :
5342 1 : if (bdev->internal.measured_queue_depth) {
5343 0 : bdev->internal.io_time += bdev->internal.period;
5344 0 : bdev->internal.weighted_io_time += bdev->internal.period * bdev->internal.measured_queue_depth;
5345 0 : }
5346 :
5347 1 : bdev->internal.qd_poll_in_progress = false;
5348 :
5349 1 : bdev_update_qd_sampling_period(bdev);
5350 1 : }
5351 :
5352 : static void
5353 1 : _calculate_measured_qd(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
5354 : struct spdk_io_channel *io_ch, void *_ctx)
5355 : {
5356 1 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(io_ch);
5357 :
5358 1 : bdev->internal.temporary_queue_depth += ch->io_outstanding;
5359 1 : spdk_bdev_for_each_channel_continue(i, 0);
5360 1 : }
5361 :
5362 : static int
5363 1 : bdev_calculate_measured_queue_depth(void *ctx)
5364 : {
5365 1 : struct spdk_bdev *bdev = ctx;
5366 :
5367 1 : bdev->internal.qd_poll_in_progress = true;
5368 1 : bdev->internal.temporary_queue_depth = 0;
5369 1 : spdk_bdev_for_each_channel(bdev, _calculate_measured_qd, bdev, _calculate_measured_qd_cpl);
5370 1 : return SPDK_POLLER_BUSY;
5371 : }
5372 :
5373 : static void
5374 5 : bdev_update_qd_sampling_period(void *ctx)
5375 : {
5376 5 : struct spdk_bdev *bdev = ctx;
5377 :
5378 5 : if (bdev->internal.period == bdev->internal.new_period) {
5379 0 : return;
5380 : }
5381 :
5382 5 : if (bdev->internal.qd_poll_in_progress) {
5383 1 : return;
5384 : }
5385 :
5386 4 : bdev->internal.period = bdev->internal.new_period;
5387 :
5388 4 : spdk_poller_unregister(&bdev->internal.qd_poller);
5389 4 : if (bdev->internal.period != 0) {
5390 2 : bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth,
5391 : bdev, bdev->internal.period);
5392 2 : } else {
5393 2 : spdk_bdev_close(bdev->internal.qd_desc);
5394 2 : bdev->internal.qd_desc = NULL;
5395 : }
5396 5 : }
5397 :
5398 : static void
5399 0 : _tmp_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, void *ctx)
5400 : {
5401 0 : SPDK_NOTICELOG("Unexpected event type: %d\n", type);
5402 0 : }
5403 :
5404 : void
5405 131 : spdk_bdev_set_qd_sampling_period(struct spdk_bdev *bdev, uint64_t period)
5406 : {
5407 : int rc;
5408 :
5409 131 : if (bdev->internal.new_period == period) {
5410 125 : return;
5411 : }
5412 :
5413 6 : bdev->internal.new_period = period;
5414 :
5415 6 : if (bdev->internal.qd_desc != NULL) {
5416 4 : assert(bdev->internal.period != 0);
5417 :
5418 8 : spdk_thread_send_msg(bdev->internal.qd_desc->thread,
5419 4 : bdev_update_qd_sampling_period, bdev);
5420 4 : return;
5421 : }
5422 :
5423 2 : assert(bdev->internal.period == 0);
5424 :
5425 4 : rc = spdk_bdev_open_ext(spdk_bdev_get_name(bdev), false, _tmp_bdev_event_cb,
5426 2 : NULL, &bdev->internal.qd_desc);
5427 2 : if (rc != 0) {
5428 0 : return;
5429 : }
5430 :
5431 2 : bdev->internal.period = period;
5432 2 : bdev->internal.qd_poller = SPDK_POLLER_REGISTER(bdev_calculate_measured_queue_depth,
5433 : bdev, period);
5434 131 : }
5435 :
5436 : struct bdev_get_current_qd_ctx {
5437 : uint64_t current_qd;
5438 : spdk_bdev_get_current_qd_cb cb_fn;
5439 : void *cb_arg;
5440 : };
5441 :
5442 : static void
5443 0 : bdev_get_current_qd_done(struct spdk_bdev *bdev, void *_ctx, int status)
5444 : {
5445 0 : struct bdev_get_current_qd_ctx *ctx = _ctx;
5446 :
5447 0 : ctx->cb_fn(bdev, ctx->current_qd, ctx->cb_arg, 0);
5448 :
5449 0 : free(ctx);
5450 0 : }
5451 :
5452 : static void
5453 0 : bdev_get_current_qd(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
5454 : struct spdk_io_channel *io_ch, void *_ctx)
5455 : {
5456 0 : struct bdev_get_current_qd_ctx *ctx = _ctx;
5457 0 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
5458 :
5459 0 : ctx->current_qd += bdev_ch->io_outstanding;
5460 :
5461 0 : spdk_bdev_for_each_channel_continue(i, 0);
5462 0 : }
5463 :
5464 : void
5465 0 : spdk_bdev_get_current_qd(struct spdk_bdev *bdev, spdk_bdev_get_current_qd_cb cb_fn,
5466 : void *cb_arg)
5467 : {
5468 : struct bdev_get_current_qd_ctx *ctx;
5469 :
5470 0 : assert(cb_fn != NULL);
5471 :
5472 0 : ctx = calloc(1, sizeof(*ctx));
5473 0 : if (ctx == NULL) {
5474 0 : cb_fn(bdev, 0, cb_arg, -ENOMEM);
5475 0 : return;
5476 : }
5477 :
5478 0 : ctx->cb_fn = cb_fn;
5479 0 : ctx->cb_arg = cb_arg;
5480 :
5481 0 : spdk_bdev_for_each_channel(bdev, bdev_get_current_qd, ctx, bdev_get_current_qd_done);
5482 0 : }
5483 :
5484 : static void
5485 25 : _event_notify(struct spdk_bdev_desc *desc, enum spdk_bdev_event_type type)
5486 : {
5487 25 : assert(desc->thread == spdk_get_thread());
5488 :
5489 25 : spdk_spin_lock(&desc->spinlock);
5490 25 : desc->refs--;
5491 25 : if (!desc->closed) {
5492 14 : spdk_spin_unlock(&desc->spinlock);
5493 28 : desc->callback.event_fn(type,
5494 14 : desc->bdev,
5495 14 : desc->callback.ctx);
5496 14 : return;
5497 11 : } else if (desc->refs == 0) {
5498 : /* This descriptor was closed after this event_notify message was sent.
5499 : * spdk_bdev_close() could not free the descriptor since this message was
5500 : * in flight, so we free it now using bdev_desc_free().
5501 : */
5502 10 : spdk_spin_unlock(&desc->spinlock);
5503 10 : bdev_desc_free(desc);
5504 10 : return;
5505 : }
5506 1 : spdk_spin_unlock(&desc->spinlock);
5507 25 : }
5508 :
5509 : static void
5510 25 : event_notify(struct spdk_bdev_desc *desc, spdk_msg_fn event_notify_fn)
5511 : {
5512 25 : spdk_spin_lock(&desc->spinlock);
5513 25 : desc->refs++;
5514 25 : spdk_thread_send_msg(desc->thread, event_notify_fn, desc);
5515 25 : spdk_spin_unlock(&desc->spinlock);
5516 25 : }
5517 :
5518 : static void
5519 6 : _resize_notify(void *ctx)
5520 : {
5521 6 : struct spdk_bdev_desc *desc = ctx;
5522 :
5523 6 : _event_notify(desc, SPDK_BDEV_EVENT_RESIZE);
5524 6 : }
5525 :
5526 : int
5527 11 : spdk_bdev_notify_blockcnt_change(struct spdk_bdev *bdev, uint64_t size)
5528 : {
5529 : struct spdk_bdev_desc *desc;
5530 : int ret;
5531 :
5532 11 : if (size == bdev->blockcnt) {
5533 0 : return 0;
5534 : }
5535 :
5536 11 : spdk_spin_lock(&bdev->internal.spinlock);
5537 :
5538 : /* bdev has open descriptors */
5539 11 : if (!TAILQ_EMPTY(&bdev->internal.open_descs) &&
5540 7 : bdev->blockcnt > size) {
5541 1 : ret = -EBUSY;
5542 1 : } else {
5543 10 : bdev->blockcnt = size;
5544 16 : TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
5545 6 : event_notify(desc, _resize_notify);
5546 6 : }
5547 10 : ret = 0;
5548 : }
5549 :
5550 11 : spdk_spin_unlock(&bdev->internal.spinlock);
5551 :
5552 11 : return ret;
5553 11 : }
5554 :
5555 : /*
5556 : * Convert I/O offset and length from bytes to blocks.
5557 : *
5558 : * Returns zero on success or non-zero if the byte parameters aren't divisible by the block size.
5559 : */
5560 : static uint64_t
5561 20 : bdev_bytes_to_blocks(struct spdk_bdev_desc *desc, uint64_t offset_bytes,
5562 : uint64_t *offset_blocks, uint64_t num_bytes, uint64_t *num_blocks)
5563 : {
5564 20 : uint32_t block_size = bdev_desc_get_block_size(desc);
5565 : uint8_t shift_cnt;
5566 :
5567 : /* Avoid expensive div operations if possible. These spdk_u32 functions are very cheap. */
5568 20 : if (spdk_likely(spdk_u32_is_pow2(block_size))) {
5569 17 : shift_cnt = spdk_u32log2(block_size);
5570 17 : *offset_blocks = offset_bytes >> shift_cnt;
5571 17 : *num_blocks = num_bytes >> shift_cnt;
5572 34 : return (offset_bytes - (*offset_blocks << shift_cnt)) |
5573 17 : (num_bytes - (*num_blocks << shift_cnt));
5574 : } else {
5575 3 : *offset_blocks = offset_bytes / block_size;
5576 3 : *num_blocks = num_bytes / block_size;
5577 3 : return (offset_bytes % block_size) | (num_bytes % block_size);
5578 : }
5579 20 : }
5580 :
5581 : static bool
5582 689 : bdev_io_valid_blocks(struct spdk_bdev *bdev, uint64_t offset_blocks, uint64_t num_blocks)
5583 : {
5584 : /* Return failure if offset_blocks + num_blocks is less than offset_blocks; indicates there
5585 : * has been an overflow and hence the offset has been wrapped around */
5586 689 : if (offset_blocks + num_blocks < offset_blocks) {
5587 1 : return false;
5588 : }
5589 :
5590 : /* Return failure if offset_blocks + num_blocks exceeds the size of the bdev */
5591 688 : if (offset_blocks + num_blocks > bdev->blockcnt) {
5592 2 : return false;
5593 : }
5594 :
5595 686 : return true;
5596 689 : }
5597 :
5598 : static void
5599 2 : bdev_seek_complete_cb(void *ctx)
5600 : {
5601 2 : struct spdk_bdev_io *bdev_io = ctx;
5602 :
5603 2 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
5604 2 : bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx);
5605 2 : }
5606 :
5607 : static int
5608 4 : bdev_seek(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5609 : uint64_t offset_blocks, enum spdk_bdev_io_type io_type,
5610 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5611 : {
5612 4 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5613 : struct spdk_bdev_io *bdev_io;
5614 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5615 :
5616 4 : assert(io_type == SPDK_BDEV_IO_TYPE_SEEK_DATA || io_type == SPDK_BDEV_IO_TYPE_SEEK_HOLE);
5617 :
5618 : /* Check if offset_blocks is valid looking at the validity of one block */
5619 4 : if (!bdev_io_valid_blocks(bdev, offset_blocks, 1)) {
5620 0 : return -EINVAL;
5621 : }
5622 :
5623 4 : bdev_io = bdev_channel_get_io(channel);
5624 4 : if (!bdev_io) {
5625 0 : return -ENOMEM;
5626 : }
5627 :
5628 4 : bdev_io->internal.ch = channel;
5629 4 : bdev_io->internal.desc = desc;
5630 4 : bdev_io->type = io_type;
5631 4 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5632 4 : bdev_io->u.bdev.memory_domain = NULL;
5633 4 : bdev_io->u.bdev.memory_domain_ctx = NULL;
5634 4 : bdev_io->u.bdev.accel_sequence = NULL;
5635 4 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5636 :
5637 4 : if (!spdk_bdev_io_type_supported(bdev, io_type)) {
5638 : /* In case bdev doesn't support seek to next data/hole offset,
5639 : * it is assumed that only data and no holes are present */
5640 2 : if (io_type == SPDK_BDEV_IO_TYPE_SEEK_DATA) {
5641 1 : bdev_io->u.bdev.seek.offset = offset_blocks;
5642 1 : } else {
5643 1 : bdev_io->u.bdev.seek.offset = UINT64_MAX;
5644 : }
5645 :
5646 2 : spdk_thread_send_msg(spdk_get_thread(), bdev_seek_complete_cb, bdev_io);
5647 2 : return 0;
5648 : }
5649 :
5650 2 : bdev_io_submit(bdev_io);
5651 2 : return 0;
5652 4 : }
5653 :
5654 : int
5655 2 : spdk_bdev_seek_data(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5656 : uint64_t offset_blocks,
5657 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5658 : {
5659 2 : return bdev_seek(desc, ch, offset_blocks, SPDK_BDEV_IO_TYPE_SEEK_DATA, cb, cb_arg);
5660 : }
5661 :
5662 : int
5663 2 : spdk_bdev_seek_hole(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5664 : uint64_t offset_blocks,
5665 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5666 : {
5667 2 : return bdev_seek(desc, ch, offset_blocks, SPDK_BDEV_IO_TYPE_SEEK_HOLE, cb, cb_arg);
5668 : }
5669 :
5670 : uint64_t
5671 4 : spdk_bdev_io_get_seek_offset(const struct spdk_bdev_io *bdev_io)
5672 : {
5673 4 : return bdev_io->u.bdev.seek.offset;
5674 : }
5675 :
5676 : static int
5677 204 : bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch, void *buf,
5678 : void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5679 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5680 : {
5681 204 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5682 : struct spdk_bdev_io *bdev_io;
5683 204 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5684 :
5685 204 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5686 0 : return -EINVAL;
5687 : }
5688 :
5689 204 : bdev_io = bdev_channel_get_io(channel);
5690 204 : if (!bdev_io) {
5691 1 : return -ENOMEM;
5692 : }
5693 :
5694 203 : bdev_io->internal.ch = channel;
5695 203 : bdev_io->internal.desc = desc;
5696 203 : bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
5697 203 : bdev_io->u.bdev.iovs = &bdev_io->iov;
5698 203 : bdev_io->u.bdev.iovs[0].iov_base = buf;
5699 203 : bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev_desc_get_block_size(desc);
5700 203 : bdev_io->u.bdev.iovcnt = 1;
5701 203 : bdev_io->u.bdev.md_buf = md_buf;
5702 203 : bdev_io->u.bdev.num_blocks = num_blocks;
5703 203 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5704 203 : bdev_io->u.bdev.memory_domain = NULL;
5705 203 : bdev_io->u.bdev.memory_domain_ctx = NULL;
5706 203 : bdev_io->u.bdev.accel_sequence = NULL;
5707 203 : bdev_io->u.bdev.dif_check_flags = bdev->dif_check_flags;
5708 203 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5709 :
5710 203 : bdev_io_submit(bdev_io);
5711 203 : return 0;
5712 204 : }
5713 :
5714 : int
5715 3 : spdk_bdev_read(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5716 : void *buf, uint64_t offset, uint64_t nbytes,
5717 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5718 : {
5719 : uint64_t offset_blocks, num_blocks;
5720 :
5721 3 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
5722 0 : return -EINVAL;
5723 : }
5724 :
5725 3 : return spdk_bdev_read_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
5726 3 : }
5727 :
5728 : int
5729 200 : spdk_bdev_read_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5730 : void *buf, uint64_t offset_blocks, uint64_t num_blocks,
5731 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5732 : {
5733 200 : return bdev_read_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks, cb, cb_arg);
5734 : }
5735 :
5736 : int
5737 4 : spdk_bdev_read_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5738 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5739 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5740 : {
5741 8 : struct iovec iov = {
5742 4 : .iov_base = buf,
5743 : };
5744 :
5745 4 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
5746 0 : return -EINVAL;
5747 : }
5748 :
5749 4 : if ((md_buf || desc->opts.no_metadata) && !_is_buf_allocated(&iov)) {
5750 0 : return -EINVAL;
5751 : }
5752 :
5753 8 : return bdev_read_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
5754 4 : cb, cb_arg);
5755 4 : }
5756 :
5757 : int
5758 5 : spdk_bdev_readv(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5759 : struct iovec *iov, int iovcnt,
5760 : uint64_t offset, uint64_t nbytes,
5761 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5762 : {
5763 : uint64_t offset_blocks, num_blocks;
5764 :
5765 5 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
5766 0 : return -EINVAL;
5767 : }
5768 :
5769 5 : return spdk_bdev_readv_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
5770 5 : }
5771 :
5772 : static int
5773 226 : bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5774 : struct iovec *iov, int iovcnt, void *md_buf, uint64_t offset_blocks,
5775 : uint64_t num_blocks, struct spdk_memory_domain *domain, void *domain_ctx,
5776 : struct spdk_accel_sequence *seq, uint32_t dif_check_flags,
5777 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5778 : {
5779 226 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5780 : struct spdk_bdev_io *bdev_io;
5781 226 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5782 :
5783 226 : if (spdk_unlikely(!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks))) {
5784 0 : return -EINVAL;
5785 : }
5786 :
5787 226 : bdev_io = bdev_channel_get_io(channel);
5788 226 : if (spdk_unlikely(!bdev_io)) {
5789 2 : return -ENOMEM;
5790 : }
5791 :
5792 224 : bdev_io->internal.ch = channel;
5793 224 : bdev_io->internal.desc = desc;
5794 224 : bdev_io->type = SPDK_BDEV_IO_TYPE_READ;
5795 224 : bdev_io->u.bdev.iovs = iov;
5796 224 : bdev_io->u.bdev.iovcnt = iovcnt;
5797 224 : bdev_io->u.bdev.md_buf = md_buf;
5798 224 : bdev_io->u.bdev.num_blocks = num_blocks;
5799 224 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5800 224 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5801 :
5802 224 : if (seq != NULL) {
5803 0 : bdev_io->internal.f.has_accel_sequence = true;
5804 0 : bdev_io->internal.accel_sequence = seq;
5805 0 : }
5806 :
5807 224 : if (domain != NULL) {
5808 2 : bdev_io->internal.f.has_memory_domain = true;
5809 2 : bdev_io->internal.memory_domain = domain;
5810 2 : bdev_io->internal.memory_domain_ctx = domain_ctx;
5811 2 : }
5812 :
5813 224 : bdev_io->u.bdev.memory_domain = domain;
5814 224 : bdev_io->u.bdev.memory_domain_ctx = domain_ctx;
5815 224 : bdev_io->u.bdev.accel_sequence = seq;
5816 224 : bdev_io->u.bdev.dif_check_flags = dif_check_flags;
5817 :
5818 224 : _bdev_io_submit_ext(desc, bdev_io);
5819 :
5820 224 : return 0;
5821 226 : }
5822 :
5823 : int
5824 21 : spdk_bdev_readv_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5825 : struct iovec *iov, int iovcnt,
5826 : uint64_t offset_blocks, uint64_t num_blocks,
5827 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5828 : {
5829 21 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5830 :
5831 42 : return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
5832 21 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, cb, cb_arg);
5833 : }
5834 :
5835 : int
5836 4 : spdk_bdev_readv_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5837 : struct iovec *iov, int iovcnt, void *md_buf,
5838 : uint64_t offset_blocks, uint64_t num_blocks,
5839 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5840 : {
5841 4 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5842 :
5843 4 : if (md_buf && !spdk_bdev_is_md_separate(bdev)) {
5844 0 : return -EINVAL;
5845 : }
5846 :
5847 4 : if (md_buf && !_is_buf_allocated(iov)) {
5848 0 : return -EINVAL;
5849 : }
5850 :
5851 8 : return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
5852 4 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, cb, cb_arg);
5853 4 : }
5854 :
5855 : static inline bool
5856 14 : _bdev_io_check_opts(struct spdk_bdev_ext_io_opts *opts, struct iovec *iov)
5857 : {
5858 : /*
5859 : * We check if opts size is at least of size when we first introduced
5860 : * spdk_bdev_ext_io_opts (ac6f2bdd8d) since access to those members
5861 : * are not checked internal.
5862 : */
5863 24 : return opts->size >= offsetof(struct spdk_bdev_ext_io_opts, metadata) +
5864 14 : sizeof(opts->metadata) &&
5865 10 : opts->size <= sizeof(*opts) &&
5866 : /* When memory domain is used, the user must provide data buffers */
5867 8 : (!opts->memory_domain || (iov && iov[0].iov_base));
5868 : }
5869 :
5870 : int
5871 8 : spdk_bdev_readv_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5872 : struct iovec *iov, int iovcnt,
5873 : uint64_t offset_blocks, uint64_t num_blocks,
5874 : spdk_bdev_io_completion_cb cb, void *cb_arg,
5875 : struct spdk_bdev_ext_io_opts *opts)
5876 : {
5877 8 : struct spdk_memory_domain *domain = NULL;
5878 8 : struct spdk_accel_sequence *seq = NULL;
5879 8 : void *domain_ctx = NULL, *md = NULL;
5880 8 : uint32_t dif_check_flags = 0;
5881 8 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5882 :
5883 8 : if (opts) {
5884 7 : if (spdk_unlikely(!_bdev_io_check_opts(opts, iov))) {
5885 3 : return -EINVAL;
5886 : }
5887 :
5888 4 : md = opts->metadata;
5889 4 : domain = bdev_get_ext_io_opt(opts, memory_domain, NULL);
5890 4 : domain_ctx = bdev_get_ext_io_opt(opts, memory_domain_ctx, NULL);
5891 4 : seq = bdev_get_ext_io_opt(opts, accel_sequence, NULL);
5892 4 : if (md) {
5893 4 : if (spdk_unlikely(!spdk_bdev_is_md_separate(bdev))) {
5894 0 : return -EINVAL;
5895 : }
5896 :
5897 4 : if (spdk_unlikely(!_is_buf_allocated(iov))) {
5898 0 : return -EINVAL;
5899 : }
5900 :
5901 4 : if (spdk_unlikely(seq != NULL)) {
5902 0 : return -EINVAL;
5903 : }
5904 4 : }
5905 4 : }
5906 :
5907 10 : dif_check_flags = bdev->dif_check_flags &
5908 5 : ~(bdev_get_ext_io_opt(opts, dif_check_flags_exclude_mask, 0));
5909 :
5910 10 : return bdev_readv_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks,
5911 5 : num_blocks, domain, domain_ctx, seq, dif_check_flags, cb, cb_arg);
5912 8 : }
5913 :
5914 : static int
5915 36 : bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5916 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5917 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5918 : {
5919 36 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
5920 : struct spdk_bdev_io *bdev_io;
5921 36 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
5922 :
5923 36 : if (!desc->write) {
5924 0 : return -EBADF;
5925 : }
5926 :
5927 36 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
5928 0 : return -EINVAL;
5929 : }
5930 :
5931 36 : bdev_io = bdev_channel_get_io(channel);
5932 36 : if (!bdev_io) {
5933 0 : return -ENOMEM;
5934 : }
5935 :
5936 36 : bdev_io->internal.ch = channel;
5937 36 : bdev_io->internal.desc = desc;
5938 36 : bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
5939 36 : bdev_io->u.bdev.iovs = &bdev_io->iov;
5940 36 : bdev_io->u.bdev.iovs[0].iov_base = buf;
5941 36 : bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev_desc_get_block_size(desc);
5942 36 : bdev_io->u.bdev.iovcnt = 1;
5943 36 : bdev_io->u.bdev.md_buf = md_buf;
5944 36 : bdev_io->u.bdev.num_blocks = num_blocks;
5945 36 : bdev_io->u.bdev.offset_blocks = offset_blocks;
5946 36 : bdev_io->u.bdev.memory_domain = NULL;
5947 36 : bdev_io->u.bdev.memory_domain_ctx = NULL;
5948 36 : bdev_io->u.bdev.accel_sequence = NULL;
5949 36 : bdev_io->u.bdev.dif_check_flags = bdev->dif_check_flags;
5950 36 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
5951 :
5952 36 : bdev_io_submit(bdev_io);
5953 36 : return 0;
5954 36 : }
5955 :
5956 : int
5957 3 : spdk_bdev_write(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5958 : void *buf, uint64_t offset, uint64_t nbytes,
5959 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5960 : {
5961 : uint64_t offset_blocks, num_blocks;
5962 :
5963 3 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
5964 0 : return -EINVAL;
5965 : }
5966 :
5967 3 : return spdk_bdev_write_blocks(desc, ch, buf, offset_blocks, num_blocks, cb, cb_arg);
5968 3 : }
5969 :
5970 : int
5971 27 : spdk_bdev_write_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5972 : void *buf, uint64_t offset_blocks, uint64_t num_blocks,
5973 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5974 : {
5975 54 : return bdev_write_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
5976 27 : cb, cb_arg);
5977 : }
5978 :
5979 : int
5980 3 : spdk_bdev_write_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
5981 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
5982 : spdk_bdev_io_completion_cb cb, void *cb_arg)
5983 : {
5984 6 : struct iovec iov = {
5985 3 : .iov_base = buf,
5986 : };
5987 :
5988 3 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
5989 0 : return -EINVAL;
5990 : }
5991 :
5992 3 : if (md_buf && !_is_buf_allocated(&iov)) {
5993 0 : return -EINVAL;
5994 : }
5995 :
5996 6 : return bdev_write_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
5997 3 : cb, cb_arg);
5998 3 : }
5999 :
6000 : static int
6001 70 : bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6002 : struct iovec *iov, int iovcnt, void *md_buf,
6003 : uint64_t offset_blocks, uint64_t num_blocks,
6004 : struct spdk_memory_domain *domain, void *domain_ctx,
6005 : struct spdk_accel_sequence *seq, uint32_t dif_check_flags,
6006 : uint32_t nvme_cdw12_raw, uint32_t nvme_cdw13_raw,
6007 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6008 : {
6009 70 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6010 : struct spdk_bdev_io *bdev_io;
6011 70 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6012 :
6013 70 : if (spdk_unlikely(!desc->write)) {
6014 0 : return -EBADF;
6015 : }
6016 :
6017 70 : if (spdk_unlikely(!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks))) {
6018 0 : return -EINVAL;
6019 : }
6020 :
6021 70 : bdev_io = bdev_channel_get_io(channel);
6022 70 : if (spdk_unlikely(!bdev_io)) {
6023 2 : return -ENOMEM;
6024 : }
6025 :
6026 68 : bdev_io->internal.ch = channel;
6027 68 : bdev_io->internal.desc = desc;
6028 68 : bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE;
6029 68 : bdev_io->u.bdev.iovs = iov;
6030 68 : bdev_io->u.bdev.iovcnt = iovcnt;
6031 68 : bdev_io->u.bdev.md_buf = md_buf;
6032 68 : bdev_io->u.bdev.num_blocks = num_blocks;
6033 68 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6034 68 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6035 68 : if (seq != NULL) {
6036 0 : bdev_io->internal.f.has_accel_sequence = true;
6037 0 : bdev_io->internal.accel_sequence = seq;
6038 0 : }
6039 :
6040 68 : if (domain != NULL) {
6041 2 : bdev_io->internal.f.has_memory_domain = true;
6042 2 : bdev_io->internal.memory_domain = domain;
6043 2 : bdev_io->internal.memory_domain_ctx = domain_ctx;
6044 2 : }
6045 :
6046 68 : bdev_io->u.bdev.memory_domain = domain;
6047 68 : bdev_io->u.bdev.memory_domain_ctx = domain_ctx;
6048 68 : bdev_io->u.bdev.accel_sequence = seq;
6049 68 : bdev_io->u.bdev.dif_check_flags = dif_check_flags;
6050 68 : bdev_io->u.bdev.nvme_cdw12.raw = nvme_cdw12_raw;
6051 68 : bdev_io->u.bdev.nvme_cdw13.raw = nvme_cdw13_raw;
6052 :
6053 68 : _bdev_io_submit_ext(desc, bdev_io);
6054 :
6055 68 : return 0;
6056 70 : }
6057 :
6058 : int
6059 3 : spdk_bdev_writev(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6060 : struct iovec *iov, int iovcnt,
6061 : uint64_t offset, uint64_t len,
6062 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6063 : {
6064 : uint64_t offset_blocks, num_blocks;
6065 :
6066 3 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, len, &num_blocks) != 0) {
6067 0 : return -EINVAL;
6068 : }
6069 :
6070 3 : return spdk_bdev_writev_blocks(desc, ch, iov, iovcnt, offset_blocks, num_blocks, cb, cb_arg);
6071 3 : }
6072 :
6073 : int
6074 14 : spdk_bdev_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6075 : struct iovec *iov, int iovcnt,
6076 : uint64_t offset_blocks, uint64_t num_blocks,
6077 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6078 : {
6079 14 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6080 :
6081 28 : return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
6082 14 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, 0, 0,
6083 14 : cb, cb_arg);
6084 : }
6085 :
6086 : int
6087 1 : spdk_bdev_writev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6088 : struct iovec *iov, int iovcnt, void *md_buf,
6089 : uint64_t offset_blocks, uint64_t num_blocks,
6090 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6091 : {
6092 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6093 :
6094 1 : if (md_buf && !spdk_bdev_is_md_separate(bdev)) {
6095 0 : return -EINVAL;
6096 : }
6097 :
6098 1 : if (md_buf && !_is_buf_allocated(iov)) {
6099 0 : return -EINVAL;
6100 : }
6101 :
6102 2 : return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
6103 1 : num_blocks, NULL, NULL, NULL, bdev->dif_check_flags, 0, 0,
6104 1 : cb, cb_arg);
6105 1 : }
6106 :
6107 : int
6108 8 : spdk_bdev_writev_blocks_ext(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6109 : struct iovec *iov, int iovcnt,
6110 : uint64_t offset_blocks, uint64_t num_blocks,
6111 : spdk_bdev_io_completion_cb cb, void *cb_arg,
6112 : struct spdk_bdev_ext_io_opts *opts)
6113 : {
6114 8 : struct spdk_memory_domain *domain = NULL;
6115 8 : struct spdk_accel_sequence *seq = NULL;
6116 8 : void *domain_ctx = NULL, *md = NULL;
6117 8 : uint32_t dif_check_flags = 0;
6118 8 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6119 8 : uint32_t nvme_cdw12_raw = 0;
6120 8 : uint32_t nvme_cdw13_raw = 0;
6121 :
6122 8 : if (opts) {
6123 7 : if (spdk_unlikely(!_bdev_io_check_opts(opts, iov))) {
6124 3 : return -EINVAL;
6125 : }
6126 4 : md = opts->metadata;
6127 4 : domain = bdev_get_ext_io_opt(opts, memory_domain, NULL);
6128 4 : domain_ctx = bdev_get_ext_io_opt(opts, memory_domain_ctx, NULL);
6129 4 : seq = bdev_get_ext_io_opt(opts, accel_sequence, NULL);
6130 4 : nvme_cdw12_raw = bdev_get_ext_io_opt(opts, nvme_cdw12.raw, 0);
6131 4 : nvme_cdw13_raw = bdev_get_ext_io_opt(opts, nvme_cdw13.raw, 0);
6132 4 : if (md) {
6133 4 : if (spdk_unlikely(!spdk_bdev_is_md_separate(bdev))) {
6134 0 : return -EINVAL;
6135 : }
6136 :
6137 4 : if (spdk_unlikely(!_is_buf_allocated(iov))) {
6138 0 : return -EINVAL;
6139 : }
6140 :
6141 4 : if (spdk_unlikely(seq != NULL)) {
6142 0 : return -EINVAL;
6143 : }
6144 4 : }
6145 4 : }
6146 :
6147 10 : dif_check_flags = bdev->dif_check_flags &
6148 5 : ~(bdev_get_ext_io_opt(opts, dif_check_flags_exclude_mask, 0));
6149 :
6150 10 : return bdev_writev_blocks_with_md(desc, ch, iov, iovcnt, md, offset_blocks, num_blocks,
6151 5 : domain, domain_ctx, seq, dif_check_flags,
6152 5 : nvme_cdw12_raw, nvme_cdw13_raw, cb, cb_arg);
6153 8 : }
6154 :
6155 : static void
6156 11 : bdev_compare_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
6157 : {
6158 11 : struct spdk_bdev_io *parent_io = cb_arg;
6159 11 : struct spdk_bdev *bdev = parent_io->bdev;
6160 11 : uint8_t *read_buf = bdev_io->u.bdev.iovs[0].iov_base;
6161 11 : int i, rc = 0;
6162 :
6163 11 : if (!success) {
6164 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
6165 0 : parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
6166 0 : spdk_bdev_free_io(bdev_io);
6167 0 : return;
6168 : }
6169 :
6170 17 : for (i = 0; i < parent_io->u.bdev.iovcnt; i++) {
6171 22 : rc = memcmp(read_buf,
6172 11 : parent_io->u.bdev.iovs[i].iov_base,
6173 11 : parent_io->u.bdev.iovs[i].iov_len);
6174 11 : if (rc) {
6175 5 : break;
6176 : }
6177 6 : read_buf += parent_io->u.bdev.iovs[i].iov_len;
6178 6 : }
6179 :
6180 11 : if (rc == 0 && parent_io->u.bdev.md_buf && spdk_bdev_is_md_separate(bdev)) {
6181 4 : rc = memcmp(bdev_io->u.bdev.md_buf,
6182 2 : parent_io->u.bdev.md_buf,
6183 2 : spdk_bdev_get_md_size(bdev));
6184 2 : }
6185 :
6186 11 : spdk_bdev_free_io(bdev_io);
6187 :
6188 11 : if (rc == 0) {
6189 5 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
6190 5 : parent_io->internal.cb(parent_io, true, parent_io->internal.caller_ctx);
6191 5 : } else {
6192 6 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_MISCOMPARE;
6193 6 : parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
6194 : }
6195 11 : }
6196 :
6197 : static void
6198 11 : bdev_compare_do_read(void *_bdev_io)
6199 : {
6200 11 : struct spdk_bdev_io *bdev_io = _bdev_io;
6201 : int rc;
6202 :
6203 22 : rc = spdk_bdev_read_blocks(bdev_io->internal.desc,
6204 11 : spdk_io_channel_from_ctx(bdev_io->internal.ch), NULL,
6205 11 : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6206 11 : bdev_compare_do_read_done, bdev_io);
6207 :
6208 11 : if (rc == -ENOMEM) {
6209 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_do_read);
6210 11 : } else if (rc != 0) {
6211 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
6212 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
6213 0 : }
6214 11 : }
6215 :
6216 : static int
6217 16 : bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6218 : struct iovec *iov, int iovcnt, void *md_buf,
6219 : uint64_t offset_blocks, uint64_t num_blocks,
6220 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6221 : {
6222 16 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6223 : struct spdk_bdev_io *bdev_io;
6224 16 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6225 :
6226 16 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6227 0 : return -EINVAL;
6228 : }
6229 :
6230 16 : bdev_io = bdev_channel_get_io(channel);
6231 16 : if (!bdev_io) {
6232 0 : return -ENOMEM;
6233 : }
6234 :
6235 16 : bdev_io->internal.ch = channel;
6236 16 : bdev_io->internal.desc = desc;
6237 16 : bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE;
6238 16 : bdev_io->u.bdev.iovs = iov;
6239 16 : bdev_io->u.bdev.iovcnt = iovcnt;
6240 16 : bdev_io->u.bdev.md_buf = md_buf;
6241 16 : bdev_io->u.bdev.num_blocks = num_blocks;
6242 16 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6243 16 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6244 16 : bdev_io->u.bdev.memory_domain = NULL;
6245 16 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6246 16 : bdev_io->u.bdev.accel_sequence = NULL;
6247 :
6248 16 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) {
6249 7 : bdev_io_submit(bdev_io);
6250 7 : return 0;
6251 : }
6252 :
6253 9 : bdev_compare_do_read(bdev_io);
6254 :
6255 9 : return 0;
6256 16 : }
6257 :
6258 : int
6259 10 : spdk_bdev_comparev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6260 : struct iovec *iov, int iovcnt,
6261 : uint64_t offset_blocks, uint64_t num_blocks,
6262 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6263 : {
6264 20 : return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, NULL, offset_blocks,
6265 10 : num_blocks, cb, cb_arg);
6266 : }
6267 :
6268 : int
6269 6 : spdk_bdev_comparev_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6270 : struct iovec *iov, int iovcnt, void *md_buf,
6271 : uint64_t offset_blocks, uint64_t num_blocks,
6272 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6273 : {
6274 6 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
6275 0 : return -EINVAL;
6276 : }
6277 :
6278 6 : if (md_buf && !_is_buf_allocated(iov)) {
6279 0 : return -EINVAL;
6280 : }
6281 :
6282 12 : return bdev_comparev_blocks_with_md(desc, ch, iov, iovcnt, md_buf, offset_blocks,
6283 6 : num_blocks, cb, cb_arg);
6284 6 : }
6285 :
6286 : static int
6287 4 : bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6288 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
6289 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6290 : {
6291 4 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6292 : struct spdk_bdev_io *bdev_io;
6293 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6294 :
6295 4 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6296 0 : return -EINVAL;
6297 : }
6298 :
6299 4 : bdev_io = bdev_channel_get_io(channel);
6300 4 : if (!bdev_io) {
6301 0 : return -ENOMEM;
6302 : }
6303 :
6304 4 : bdev_io->internal.ch = channel;
6305 4 : bdev_io->internal.desc = desc;
6306 4 : bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE;
6307 4 : bdev_io->u.bdev.iovs = &bdev_io->iov;
6308 4 : bdev_io->u.bdev.iovs[0].iov_base = buf;
6309 4 : bdev_io->u.bdev.iovs[0].iov_len = num_blocks * bdev_desc_get_block_size(desc);
6310 4 : bdev_io->u.bdev.iovcnt = 1;
6311 4 : bdev_io->u.bdev.md_buf = md_buf;
6312 4 : bdev_io->u.bdev.num_blocks = num_blocks;
6313 4 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6314 4 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6315 4 : bdev_io->u.bdev.memory_domain = NULL;
6316 4 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6317 4 : bdev_io->u.bdev.accel_sequence = NULL;
6318 :
6319 4 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE)) {
6320 2 : bdev_io_submit(bdev_io);
6321 2 : return 0;
6322 : }
6323 :
6324 2 : bdev_compare_do_read(bdev_io);
6325 :
6326 2 : return 0;
6327 4 : }
6328 :
6329 : int
6330 4 : spdk_bdev_compare_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6331 : void *buf, uint64_t offset_blocks, uint64_t num_blocks,
6332 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6333 : {
6334 8 : return bdev_compare_blocks_with_md(desc, ch, buf, NULL, offset_blocks, num_blocks,
6335 4 : cb, cb_arg);
6336 : }
6337 :
6338 : int
6339 0 : spdk_bdev_compare_blocks_with_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6340 : void *buf, void *md_buf, uint64_t offset_blocks, uint64_t num_blocks,
6341 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6342 : {
6343 0 : struct iovec iov = {
6344 0 : .iov_base = buf,
6345 : };
6346 :
6347 0 : if (md_buf && !spdk_bdev_is_md_separate(spdk_bdev_desc_get_bdev(desc))) {
6348 0 : return -EINVAL;
6349 : }
6350 :
6351 0 : if (md_buf && !_is_buf_allocated(&iov)) {
6352 0 : return -EINVAL;
6353 : }
6354 :
6355 0 : return bdev_compare_blocks_with_md(desc, ch, buf, md_buf, offset_blocks, num_blocks,
6356 0 : cb, cb_arg);
6357 0 : }
6358 :
6359 : static void
6360 2 : bdev_comparev_and_writev_blocks_unlocked(struct lba_range *range, void *ctx, int unlock_status)
6361 : {
6362 2 : struct spdk_bdev_io *bdev_io = ctx;
6363 :
6364 2 : if (unlock_status) {
6365 0 : SPDK_ERRLOG("LBA range unlock failed\n");
6366 0 : }
6367 :
6368 4 : bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS ? true :
6369 2 : false, bdev_io->internal.caller_ctx);
6370 2 : }
6371 :
6372 : static void
6373 2 : bdev_comparev_and_writev_blocks_unlock(struct spdk_bdev_io *bdev_io, int status)
6374 : {
6375 2 : bdev_io->internal.status = status;
6376 :
6377 4 : bdev_unlock_lba_range(bdev_io->internal.desc, spdk_io_channel_from_ctx(bdev_io->internal.ch),
6378 2 : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6379 2 : bdev_comparev_and_writev_blocks_unlocked, bdev_io);
6380 2 : }
6381 :
6382 : static void
6383 1 : bdev_compare_and_write_do_write_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
6384 : {
6385 1 : struct spdk_bdev_io *parent_io = cb_arg;
6386 :
6387 1 : if (!success) {
6388 0 : SPDK_ERRLOG("Compare and write operation failed\n");
6389 0 : }
6390 :
6391 1 : spdk_bdev_free_io(bdev_io);
6392 :
6393 2 : bdev_comparev_and_writev_blocks_unlock(parent_io,
6394 1 : success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED);
6395 1 : }
6396 :
6397 : static void
6398 1 : bdev_compare_and_write_do_write(void *_bdev_io)
6399 : {
6400 1 : struct spdk_bdev_io *bdev_io = _bdev_io;
6401 : int rc;
6402 :
6403 2 : rc = spdk_bdev_writev_blocks(bdev_io->internal.desc,
6404 1 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
6405 1 : bdev_io->u.bdev.fused_iovs, bdev_io->u.bdev.fused_iovcnt,
6406 1 : bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6407 1 : bdev_compare_and_write_do_write_done, bdev_io);
6408 :
6409 :
6410 1 : if (rc == -ENOMEM) {
6411 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_write);
6412 1 : } else if (rc != 0) {
6413 0 : bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
6414 0 : }
6415 1 : }
6416 :
6417 : static void
6418 2 : bdev_compare_and_write_do_compare_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
6419 : {
6420 2 : struct spdk_bdev_io *parent_io = cb_arg;
6421 :
6422 2 : spdk_bdev_free_io(bdev_io);
6423 :
6424 2 : if (!success) {
6425 1 : bdev_comparev_and_writev_blocks_unlock(parent_io, SPDK_BDEV_IO_STATUS_MISCOMPARE);
6426 1 : return;
6427 : }
6428 :
6429 1 : bdev_compare_and_write_do_write(parent_io);
6430 2 : }
6431 :
6432 : static void
6433 2 : bdev_compare_and_write_do_compare(void *_bdev_io)
6434 : {
6435 2 : struct spdk_bdev_io *bdev_io = _bdev_io;
6436 : int rc;
6437 :
6438 4 : rc = spdk_bdev_comparev_blocks(bdev_io->internal.desc,
6439 2 : spdk_io_channel_from_ctx(bdev_io->internal.ch), bdev_io->u.bdev.iovs,
6440 2 : bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, bdev_io->u.bdev.num_blocks,
6441 2 : bdev_compare_and_write_do_compare_done, bdev_io);
6442 :
6443 2 : if (rc == -ENOMEM) {
6444 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_compare_and_write_do_compare);
6445 2 : } else if (rc != 0) {
6446 0 : bdev_comparev_and_writev_blocks_unlock(bdev_io, SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED);
6447 0 : }
6448 2 : }
6449 :
6450 : static void
6451 2 : bdev_comparev_and_writev_blocks_locked(struct lba_range *range, void *ctx, int status)
6452 : {
6453 2 : struct spdk_bdev_io *bdev_io = ctx;
6454 :
6455 2 : if (status) {
6456 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED;
6457 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
6458 0 : return;
6459 : }
6460 :
6461 2 : bdev_compare_and_write_do_compare(bdev_io);
6462 2 : }
6463 :
6464 : int
6465 2 : spdk_bdev_comparev_and_writev_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6466 : struct iovec *compare_iov, int compare_iovcnt,
6467 : struct iovec *write_iov, int write_iovcnt,
6468 : uint64_t offset_blocks, uint64_t num_blocks,
6469 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6470 : {
6471 2 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6472 : struct spdk_bdev_io *bdev_io;
6473 2 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6474 :
6475 2 : if (!desc->write) {
6476 0 : return -EBADF;
6477 : }
6478 :
6479 2 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6480 0 : return -EINVAL;
6481 : }
6482 :
6483 2 : if (num_blocks > bdev->acwu) {
6484 0 : return -EINVAL;
6485 : }
6486 :
6487 2 : bdev_io = bdev_channel_get_io(channel);
6488 2 : if (!bdev_io) {
6489 0 : return -ENOMEM;
6490 : }
6491 :
6492 2 : bdev_io->internal.ch = channel;
6493 2 : bdev_io->internal.desc = desc;
6494 2 : bdev_io->type = SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE;
6495 2 : bdev_io->u.bdev.iovs = compare_iov;
6496 2 : bdev_io->u.bdev.iovcnt = compare_iovcnt;
6497 2 : bdev_io->u.bdev.fused_iovs = write_iov;
6498 2 : bdev_io->u.bdev.fused_iovcnt = write_iovcnt;
6499 2 : bdev_io->u.bdev.md_buf = NULL;
6500 2 : bdev_io->u.bdev.num_blocks = num_blocks;
6501 2 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6502 2 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6503 2 : bdev_io->u.bdev.memory_domain = NULL;
6504 2 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6505 2 : bdev_io->u.bdev.accel_sequence = NULL;
6506 :
6507 2 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COMPARE_AND_WRITE)) {
6508 0 : bdev_io_submit(bdev_io);
6509 0 : return 0;
6510 : }
6511 :
6512 4 : return bdev_lock_lba_range(desc, ch, offset_blocks, num_blocks,
6513 2 : bdev_comparev_and_writev_blocks_locked, bdev_io);
6514 2 : }
6515 :
6516 : int
6517 2 : spdk_bdev_zcopy_start(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6518 : struct iovec *iov, int iovcnt,
6519 : uint64_t offset_blocks, uint64_t num_blocks,
6520 : bool populate,
6521 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6522 : {
6523 2 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6524 : struct spdk_bdev_io *bdev_io;
6525 2 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6526 :
6527 2 : if (!desc->write) {
6528 0 : return -EBADF;
6529 : }
6530 :
6531 2 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6532 0 : return -EINVAL;
6533 : }
6534 :
6535 2 : if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ZCOPY)) {
6536 0 : return -ENOTSUP;
6537 : }
6538 :
6539 2 : bdev_io = bdev_channel_get_io(channel);
6540 2 : if (!bdev_io) {
6541 0 : return -ENOMEM;
6542 : }
6543 :
6544 2 : bdev_io->internal.ch = channel;
6545 2 : bdev_io->internal.desc = desc;
6546 2 : bdev_io->type = SPDK_BDEV_IO_TYPE_ZCOPY;
6547 2 : bdev_io->u.bdev.num_blocks = num_blocks;
6548 2 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6549 2 : bdev_io->u.bdev.iovs = iov;
6550 2 : bdev_io->u.bdev.iovcnt = iovcnt;
6551 2 : bdev_io->u.bdev.md_buf = NULL;
6552 2 : bdev_io->u.bdev.zcopy.populate = populate ? 1 : 0;
6553 2 : bdev_io->u.bdev.zcopy.commit = 0;
6554 2 : bdev_io->u.bdev.zcopy.start = 1;
6555 2 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6556 2 : bdev_io->u.bdev.memory_domain = NULL;
6557 2 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6558 2 : bdev_io->u.bdev.accel_sequence = NULL;
6559 :
6560 2 : bdev_io_submit(bdev_io);
6561 :
6562 2 : return 0;
6563 2 : }
6564 :
6565 : int
6566 2 : spdk_bdev_zcopy_end(struct spdk_bdev_io *bdev_io, bool commit,
6567 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6568 : {
6569 2 : if (bdev_io->type != SPDK_BDEV_IO_TYPE_ZCOPY) {
6570 0 : return -EINVAL;
6571 : }
6572 :
6573 2 : bdev_io->u.bdev.zcopy.commit = commit ? 1 : 0;
6574 2 : bdev_io->u.bdev.zcopy.start = 0;
6575 2 : bdev_io->internal.caller_ctx = cb_arg;
6576 2 : bdev_io->internal.cb = cb;
6577 2 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_PENDING;
6578 :
6579 2 : bdev_io_submit(bdev_io);
6580 :
6581 2 : return 0;
6582 2 : }
6583 :
6584 : int
6585 0 : spdk_bdev_write_zeroes(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6586 : uint64_t offset, uint64_t len,
6587 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6588 : {
6589 : uint64_t offset_blocks, num_blocks;
6590 :
6591 0 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, len, &num_blocks) != 0) {
6592 0 : return -EINVAL;
6593 : }
6594 :
6595 0 : return spdk_bdev_write_zeroes_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
6596 0 : }
6597 :
6598 : int
6599 33 : spdk_bdev_write_zeroes_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6600 : uint64_t offset_blocks, uint64_t num_blocks,
6601 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6602 : {
6603 33 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6604 : struct spdk_bdev_io *bdev_io;
6605 33 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6606 :
6607 33 : if (!desc->write) {
6608 0 : return -EBADF;
6609 : }
6610 :
6611 33 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6612 0 : return -EINVAL;
6613 : }
6614 :
6615 33 : if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) &&
6616 10 : !bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE)) {
6617 1 : return -ENOTSUP;
6618 : }
6619 :
6620 32 : bdev_io = bdev_channel_get_io(channel);
6621 :
6622 32 : if (!bdev_io) {
6623 0 : return -ENOMEM;
6624 : }
6625 :
6626 32 : bdev_io->type = SPDK_BDEV_IO_TYPE_WRITE_ZEROES;
6627 32 : bdev_io->internal.ch = channel;
6628 32 : bdev_io->internal.desc = desc;
6629 32 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6630 32 : bdev_io->u.bdev.num_blocks = num_blocks;
6631 32 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6632 32 : bdev_io->u.bdev.memory_domain = NULL;
6633 32 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6634 32 : bdev_io->u.bdev.accel_sequence = NULL;
6635 :
6636 : /* If the write_zeroes size is large and should be split, use the generic split
6637 : * logic regardless of whether SPDK_BDEV_IO_TYPE_WRITE_ZEREOS is supported or not.
6638 : *
6639 : * Then, send the write_zeroes request if SPDK_BDEV_IO_TYPE_WRITE_ZEROES is supported
6640 : * or emulate it using regular write request otherwise.
6641 : */
6642 32 : if (bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES) ||
6643 9 : bdev_io->internal.f.split) {
6644 26 : bdev_io_submit(bdev_io);
6645 26 : return 0;
6646 : }
6647 :
6648 6 : assert(_bdev_get_block_size_with_md(bdev) <= ZERO_BUFFER_SIZE);
6649 :
6650 6 : return bdev_write_zero_buffer(bdev_io);
6651 33 : }
6652 :
6653 : int
6654 0 : spdk_bdev_unmap(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6655 : uint64_t offset, uint64_t nbytes,
6656 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6657 : {
6658 : uint64_t offset_blocks, num_blocks;
6659 :
6660 0 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, nbytes, &num_blocks) != 0) {
6661 0 : return -EINVAL;
6662 : }
6663 :
6664 0 : return spdk_bdev_unmap_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
6665 0 : }
6666 :
6667 : static void
6668 0 : bdev_io_complete_cb(void *ctx)
6669 : {
6670 0 : struct spdk_bdev_io *bdev_io = ctx;
6671 :
6672 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
6673 0 : bdev_io->internal.cb(bdev_io, true, bdev_io->internal.caller_ctx);
6674 0 : }
6675 :
6676 : int
6677 22 : spdk_bdev_unmap_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6678 : uint64_t offset_blocks, uint64_t num_blocks,
6679 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6680 : {
6681 22 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6682 : struct spdk_bdev_io *bdev_io;
6683 22 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6684 :
6685 22 : if (!desc->write) {
6686 0 : return -EBADF;
6687 : }
6688 :
6689 22 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6690 0 : return -EINVAL;
6691 : }
6692 :
6693 22 : bdev_io = bdev_channel_get_io(channel);
6694 22 : if (!bdev_io) {
6695 0 : return -ENOMEM;
6696 : }
6697 :
6698 22 : bdev_io->internal.ch = channel;
6699 22 : bdev_io->internal.desc = desc;
6700 22 : bdev_io->type = SPDK_BDEV_IO_TYPE_UNMAP;
6701 :
6702 22 : bdev_io->u.bdev.iovs = &bdev_io->iov;
6703 22 : bdev_io->u.bdev.iovs[0].iov_base = NULL;
6704 22 : bdev_io->u.bdev.iovs[0].iov_len = 0;
6705 22 : bdev_io->u.bdev.iovcnt = 1;
6706 :
6707 22 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6708 22 : bdev_io->u.bdev.num_blocks = num_blocks;
6709 22 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6710 22 : bdev_io->u.bdev.memory_domain = NULL;
6711 22 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6712 22 : bdev_io->u.bdev.accel_sequence = NULL;
6713 :
6714 22 : if (num_blocks == 0) {
6715 0 : spdk_thread_send_msg(spdk_get_thread(), bdev_io_complete_cb, bdev_io);
6716 0 : return 0;
6717 : }
6718 :
6719 22 : bdev_io_submit(bdev_io);
6720 22 : return 0;
6721 22 : }
6722 :
6723 : int
6724 0 : spdk_bdev_flush(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6725 : uint64_t offset, uint64_t length,
6726 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6727 : {
6728 : uint64_t offset_blocks, num_blocks;
6729 :
6730 0 : if (bdev_bytes_to_blocks(desc, offset, &offset_blocks, length, &num_blocks) != 0) {
6731 0 : return -EINVAL;
6732 : }
6733 :
6734 0 : return spdk_bdev_flush_blocks(desc, ch, offset_blocks, num_blocks, cb, cb_arg);
6735 0 : }
6736 :
6737 : int
6738 2 : spdk_bdev_flush_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6739 : uint64_t offset_blocks, uint64_t num_blocks,
6740 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6741 : {
6742 2 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6743 : struct spdk_bdev_io *bdev_io;
6744 2 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6745 :
6746 2 : if (!desc->write) {
6747 0 : return -EBADF;
6748 : }
6749 :
6750 2 : if (!bdev_io_valid_blocks(bdev, offset_blocks, num_blocks)) {
6751 0 : return -EINVAL;
6752 : }
6753 :
6754 2 : bdev_io = bdev_channel_get_io(channel);
6755 2 : if (!bdev_io) {
6756 0 : return -ENOMEM;
6757 : }
6758 :
6759 2 : bdev_io->internal.ch = channel;
6760 2 : bdev_io->internal.desc = desc;
6761 2 : bdev_io->type = SPDK_BDEV_IO_TYPE_FLUSH;
6762 2 : bdev_io->u.bdev.iovs = NULL;
6763 2 : bdev_io->u.bdev.iovcnt = 0;
6764 2 : bdev_io->u.bdev.offset_blocks = offset_blocks;
6765 2 : bdev_io->u.bdev.num_blocks = num_blocks;
6766 2 : bdev_io->u.bdev.memory_domain = NULL;
6767 2 : bdev_io->u.bdev.memory_domain_ctx = NULL;
6768 2 : bdev_io->u.bdev.accel_sequence = NULL;
6769 2 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6770 :
6771 2 : bdev_io_submit(bdev_io);
6772 2 : return 0;
6773 2 : }
6774 :
6775 : static int bdev_reset_poll_for_outstanding_io(void *ctx);
6776 :
6777 : static void
6778 13 : bdev_reset_check_outstanding_io_done(struct spdk_bdev *bdev, void *_ctx, int status)
6779 : {
6780 13 : struct spdk_bdev_channel *ch = _ctx;
6781 : struct spdk_bdev_io *bdev_io;
6782 :
6783 13 : bdev_io = TAILQ_FIRST(&ch->queued_resets);
6784 :
6785 13 : if (status == -EBUSY) {
6786 9 : if (spdk_get_ticks() < bdev_io->u.reset.wait_poller.stop_time_tsc) {
6787 8 : bdev_io->u.reset.wait_poller.poller = SPDK_POLLER_REGISTER(bdev_reset_poll_for_outstanding_io,
6788 : ch, BDEV_RESET_CHECK_OUTSTANDING_IO_PERIOD);
6789 8 : } else {
6790 1 : TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
6791 :
6792 1 : if (TAILQ_EMPTY(&ch->io_memory_domain) && TAILQ_EMPTY(&ch->io_accel_exec)) {
6793 : /* If outstanding IOs are still present and reset_io_drain_timeout
6794 : * seconds passed, start the reset. */
6795 1 : bdev_io_submit_reset(bdev_io);
6796 1 : } else {
6797 : /* We still have in progress memory domain pull/push or we're
6798 : * executing accel sequence. Since we cannot abort either of those
6799 : * operations, fail the reset request. */
6800 0 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
6801 : }
6802 : }
6803 9 : } else {
6804 4 : TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
6805 4 : SPDK_DEBUGLOG(bdev,
6806 : "Skipping reset for underlying device of bdev: %s - no outstanding I/O.\n",
6807 : ch->bdev->name);
6808 : /* Mark the completion status as a SUCCESS and complete the reset. */
6809 4 : spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
6810 : }
6811 13 : }
6812 :
6813 : static void
6814 13 : bdev_reset_check_outstanding_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6815 : struct spdk_io_channel *io_ch, void *_ctx)
6816 : {
6817 13 : struct spdk_bdev_channel *cur_ch = __io_ch_to_bdev_ch(io_ch);
6818 13 : int status = 0;
6819 :
6820 17 : if (cur_ch->io_outstanding > 0 ||
6821 4 : !TAILQ_EMPTY(&cur_ch->io_memory_domain) ||
6822 4 : !TAILQ_EMPTY(&cur_ch->io_accel_exec)) {
6823 : /* If a channel has outstanding IO, set status to -EBUSY code. This will stop
6824 : * further iteration over the rest of the channels and pass non-zero status
6825 : * to the callback function. */
6826 9 : status = -EBUSY;
6827 9 : }
6828 13 : spdk_bdev_for_each_channel_continue(i, status);
6829 13 : }
6830 :
6831 : static int
6832 8 : bdev_reset_poll_for_outstanding_io(void *ctx)
6833 : {
6834 8 : struct spdk_bdev_channel *ch = ctx;
6835 : struct spdk_bdev_io *bdev_io;
6836 :
6837 8 : bdev_io = TAILQ_FIRST(&ch->queued_resets);
6838 :
6839 8 : spdk_poller_unregister(&bdev_io->u.reset.wait_poller.poller);
6840 8 : spdk_bdev_for_each_channel(ch->bdev, bdev_reset_check_outstanding_io, ch,
6841 : bdev_reset_check_outstanding_io_done);
6842 :
6843 8 : return SPDK_POLLER_BUSY;
6844 : }
6845 :
6846 : static void
6847 15 : bdev_reset_freeze_channel_done(struct spdk_bdev *bdev, void *_ctx, int status)
6848 : {
6849 15 : struct spdk_bdev_channel *ch = _ctx;
6850 : struct spdk_bdev_io *bdev_io;
6851 :
6852 15 : bdev_io = TAILQ_FIRST(&ch->queued_resets);
6853 :
6854 15 : if (bdev->reset_io_drain_timeout == 0) {
6855 10 : TAILQ_REMOVE(&ch->queued_resets, bdev_io, internal.link);
6856 :
6857 10 : bdev_io_submit_reset(bdev_io);
6858 10 : return;
6859 : }
6860 :
6861 10 : bdev_io->u.reset.wait_poller.stop_time_tsc = spdk_get_ticks() +
6862 5 : (ch->bdev->reset_io_drain_timeout * spdk_get_ticks_hz());
6863 :
6864 : /* In case bdev->reset_io_drain_timeout is not equal to zero,
6865 : * submit the reset to the underlying module only if outstanding I/O
6866 : * remain after reset_io_drain_timeout seconds have passed. */
6867 5 : spdk_bdev_for_each_channel(ch->bdev, bdev_reset_check_outstanding_io, ch,
6868 : bdev_reset_check_outstanding_io_done);
6869 15 : }
6870 :
6871 : static void
6872 18 : bdev_reset_freeze_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6873 : struct spdk_io_channel *ch, void *_ctx)
6874 : {
6875 : struct spdk_bdev_channel *channel;
6876 : struct spdk_bdev_mgmt_channel *mgmt_channel;
6877 : struct spdk_bdev_shared_resource *shared_resource;
6878 : bdev_io_tailq_t tmp_queued;
6879 :
6880 18 : TAILQ_INIT(&tmp_queued);
6881 :
6882 18 : channel = __io_ch_to_bdev_ch(ch);
6883 18 : shared_resource = channel->shared_resource;
6884 18 : mgmt_channel = shared_resource->mgmt_ch;
6885 :
6886 18 : channel->flags |= BDEV_CH_RESET_IN_PROGRESS;
6887 :
6888 18 : if ((channel->flags & BDEV_CH_QOS_ENABLED) != 0) {
6889 2 : TAILQ_SWAP(&channel->qos_queued_io, &tmp_queued, spdk_bdev_io, internal.link);
6890 2 : }
6891 :
6892 18 : bdev_abort_all_queued_io(&shared_resource->nomem_io, channel);
6893 18 : bdev_abort_all_buf_io(mgmt_channel, channel);
6894 18 : bdev_abort_all_queued_io(&tmp_queued, channel);
6895 :
6896 18 : spdk_bdev_for_each_channel_continue(i, 0);
6897 18 : }
6898 :
6899 : static void
6900 15 : bdev_start_reset(void *ctx)
6901 : {
6902 15 : struct spdk_bdev_channel *ch = ctx;
6903 :
6904 15 : spdk_bdev_for_each_channel(ch->bdev, bdev_reset_freeze_channel, ch,
6905 : bdev_reset_freeze_channel_done);
6906 15 : }
6907 :
6908 : static void
6909 16 : bdev_channel_start_reset(struct spdk_bdev_channel *ch)
6910 : {
6911 16 : struct spdk_bdev *bdev = ch->bdev;
6912 :
6913 16 : assert(!TAILQ_EMPTY(&ch->queued_resets));
6914 :
6915 16 : spdk_spin_lock(&bdev->internal.spinlock);
6916 16 : if (bdev->internal.reset_in_progress == NULL) {
6917 15 : bdev->internal.reset_in_progress = TAILQ_FIRST(&ch->queued_resets);
6918 : /*
6919 : * Take a channel reference for the target bdev for the life of this
6920 : * reset. This guards against the channel getting destroyed while
6921 : * spdk_bdev_for_each_channel() calls related to this reset IO are in
6922 : * progress. We will release the reference when this reset is
6923 : * completed.
6924 : */
6925 15 : bdev->internal.reset_in_progress->u.reset.ch_ref = spdk_get_io_channel(__bdev_to_io_dev(bdev));
6926 15 : bdev_start_reset(ch);
6927 15 : }
6928 16 : spdk_spin_unlock(&bdev->internal.spinlock);
6929 16 : }
6930 :
6931 : int
6932 16 : spdk_bdev_reset(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
6933 : spdk_bdev_io_completion_cb cb, void *cb_arg)
6934 : {
6935 16 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
6936 : struct spdk_bdev_io *bdev_io;
6937 16 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6938 :
6939 16 : bdev_io = bdev_channel_get_io(channel);
6940 16 : if (!bdev_io) {
6941 0 : return -ENOMEM;
6942 : }
6943 :
6944 16 : bdev_io->internal.ch = channel;
6945 16 : bdev_io->internal.desc = desc;
6946 16 : bdev_io->internal.submit_tsc = spdk_get_ticks();
6947 16 : bdev_io->type = SPDK_BDEV_IO_TYPE_RESET;
6948 16 : bdev_io->u.reset.ch_ref = NULL;
6949 16 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
6950 :
6951 16 : spdk_spin_lock(&bdev->internal.spinlock);
6952 16 : TAILQ_INSERT_TAIL(&channel->queued_resets, bdev_io, internal.link);
6953 16 : spdk_spin_unlock(&bdev->internal.spinlock);
6954 :
6955 16 : bdev_ch_add_to_io_submitted(bdev_io);
6956 :
6957 16 : bdev_channel_start_reset(channel);
6958 :
6959 16 : return 0;
6960 16 : }
6961 :
6962 : void
6963 0 : spdk_bdev_get_io_stat(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
6964 : struct spdk_bdev_io_stat *stat, enum spdk_bdev_reset_stat_mode reset_mode)
6965 : {
6966 0 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6967 :
6968 0 : bdev_get_io_stat(stat, channel->stat);
6969 0 : spdk_bdev_reset_io_stat(stat, reset_mode);
6970 0 : }
6971 :
6972 : static void
6973 5 : bdev_get_device_stat_done(struct spdk_bdev *bdev, void *_ctx, int status)
6974 : {
6975 5 : struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = _ctx;
6976 :
6977 10 : bdev_iostat_ctx->cb(bdev, bdev_iostat_ctx->stat,
6978 5 : bdev_iostat_ctx->cb_arg, 0);
6979 5 : free(bdev_iostat_ctx);
6980 5 : }
6981 :
6982 : static void
6983 4 : bdev_get_each_channel_stat(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
6984 : struct spdk_io_channel *ch, void *_ctx)
6985 : {
6986 4 : struct spdk_bdev_iostat_ctx *bdev_iostat_ctx = _ctx;
6987 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
6988 :
6989 4 : spdk_bdev_add_io_stat(bdev_iostat_ctx->stat, channel->stat);
6990 4 : spdk_bdev_reset_io_stat(channel->stat, bdev_iostat_ctx->reset_mode);
6991 4 : spdk_bdev_for_each_channel_continue(i, 0);
6992 4 : }
6993 :
6994 : void
6995 5 : spdk_bdev_get_device_stat(struct spdk_bdev *bdev, struct spdk_bdev_io_stat *stat,
6996 : enum spdk_bdev_reset_stat_mode reset_mode, spdk_bdev_get_device_stat_cb cb, void *cb_arg)
6997 : {
6998 : struct spdk_bdev_iostat_ctx *bdev_iostat_ctx;
6999 :
7000 5 : assert(bdev != NULL);
7001 5 : assert(stat != NULL);
7002 5 : assert(cb != NULL);
7003 :
7004 5 : bdev_iostat_ctx = calloc(1, sizeof(struct spdk_bdev_iostat_ctx));
7005 5 : if (bdev_iostat_ctx == NULL) {
7006 0 : SPDK_ERRLOG("Unable to allocate memory for spdk_bdev_iostat_ctx\n");
7007 0 : cb(bdev, stat, cb_arg, -ENOMEM);
7008 0 : return;
7009 : }
7010 :
7011 5 : bdev_iostat_ctx->stat = stat;
7012 5 : bdev_iostat_ctx->cb = cb;
7013 5 : bdev_iostat_ctx->cb_arg = cb_arg;
7014 5 : bdev_iostat_ctx->reset_mode = reset_mode;
7015 :
7016 : /* Start with the statistics from previously deleted channels. */
7017 5 : spdk_spin_lock(&bdev->internal.spinlock);
7018 5 : bdev_get_io_stat(bdev_iostat_ctx->stat, bdev->internal.stat);
7019 5 : spdk_bdev_reset_io_stat(bdev->internal.stat, reset_mode);
7020 5 : spdk_spin_unlock(&bdev->internal.spinlock);
7021 :
7022 : /* Then iterate and add the statistics from each existing channel. */
7023 5 : spdk_bdev_for_each_channel(bdev, bdev_get_each_channel_stat, bdev_iostat_ctx,
7024 : bdev_get_device_stat_done);
7025 5 : }
7026 :
7027 : struct bdev_iostat_reset_ctx {
7028 : enum spdk_bdev_reset_stat_mode mode;
7029 : bdev_reset_device_stat_cb cb;
7030 : void *cb_arg;
7031 : };
7032 :
7033 : static void
7034 0 : bdev_reset_device_stat_done(struct spdk_bdev *bdev, void *_ctx, int status)
7035 : {
7036 0 : struct bdev_iostat_reset_ctx *ctx = _ctx;
7037 :
7038 0 : ctx->cb(bdev, ctx->cb_arg, 0);
7039 :
7040 0 : free(ctx);
7041 0 : }
7042 :
7043 : static void
7044 0 : bdev_reset_each_channel_stat(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
7045 : struct spdk_io_channel *ch, void *_ctx)
7046 : {
7047 0 : struct bdev_iostat_reset_ctx *ctx = _ctx;
7048 0 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7049 :
7050 0 : spdk_bdev_reset_io_stat(channel->stat, ctx->mode);
7051 :
7052 0 : spdk_bdev_for_each_channel_continue(i, 0);
7053 0 : }
7054 :
7055 : void
7056 0 : bdev_reset_device_stat(struct spdk_bdev *bdev, enum spdk_bdev_reset_stat_mode mode,
7057 : bdev_reset_device_stat_cb cb, void *cb_arg)
7058 : {
7059 : struct bdev_iostat_reset_ctx *ctx;
7060 :
7061 0 : assert(bdev != NULL);
7062 0 : assert(cb != NULL);
7063 :
7064 0 : ctx = calloc(1, sizeof(*ctx));
7065 0 : if (ctx == NULL) {
7066 0 : SPDK_ERRLOG("Unable to allocate bdev_iostat_reset_ctx.\n");
7067 0 : cb(bdev, cb_arg, -ENOMEM);
7068 0 : return;
7069 : }
7070 :
7071 0 : ctx->mode = mode;
7072 0 : ctx->cb = cb;
7073 0 : ctx->cb_arg = cb_arg;
7074 :
7075 0 : spdk_spin_lock(&bdev->internal.spinlock);
7076 0 : spdk_bdev_reset_io_stat(bdev->internal.stat, mode);
7077 0 : spdk_spin_unlock(&bdev->internal.spinlock);
7078 :
7079 0 : spdk_bdev_for_each_channel(bdev,
7080 : bdev_reset_each_channel_stat,
7081 0 : ctx,
7082 : bdev_reset_device_stat_done);
7083 0 : }
7084 :
7085 : int
7086 1 : spdk_bdev_nvme_admin_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
7087 : const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
7088 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7089 : {
7090 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7091 : struct spdk_bdev_io *bdev_io;
7092 1 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7093 :
7094 1 : if (!desc->write) {
7095 0 : return -EBADF;
7096 : }
7097 :
7098 1 : if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_ADMIN))) {
7099 1 : return -ENOTSUP;
7100 : }
7101 :
7102 0 : bdev_io = bdev_channel_get_io(channel);
7103 0 : if (!bdev_io) {
7104 0 : return -ENOMEM;
7105 : }
7106 :
7107 0 : bdev_io->internal.ch = channel;
7108 0 : bdev_io->internal.desc = desc;
7109 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_ADMIN;
7110 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
7111 0 : bdev_io->u.nvme_passthru.buf = buf;
7112 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
7113 0 : bdev_io->u.nvme_passthru.md_buf = NULL;
7114 0 : bdev_io->u.nvme_passthru.md_len = 0;
7115 :
7116 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7117 :
7118 0 : bdev_io_submit(bdev_io);
7119 0 : return 0;
7120 1 : }
7121 :
7122 : int
7123 1 : spdk_bdev_nvme_io_passthru(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
7124 : const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes,
7125 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7126 : {
7127 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7128 : struct spdk_bdev_io *bdev_io;
7129 1 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7130 :
7131 1 : if (!desc->write) {
7132 : /*
7133 : * Do not try to parse the NVMe command - we could maybe use bits in the opcode
7134 : * to easily determine if the command is a read or write, but for now just
7135 : * do not allow io_passthru with a read-only descriptor.
7136 : */
7137 0 : return -EBADF;
7138 : }
7139 :
7140 1 : if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO))) {
7141 1 : return -ENOTSUP;
7142 : }
7143 :
7144 0 : bdev_io = bdev_channel_get_io(channel);
7145 0 : if (!bdev_io) {
7146 0 : return -ENOMEM;
7147 : }
7148 :
7149 0 : bdev_io->internal.ch = channel;
7150 0 : bdev_io->internal.desc = desc;
7151 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO;
7152 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
7153 0 : bdev_io->u.nvme_passthru.buf = buf;
7154 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
7155 0 : bdev_io->u.nvme_passthru.md_buf = NULL;
7156 0 : bdev_io->u.nvme_passthru.md_len = 0;
7157 :
7158 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7159 :
7160 0 : bdev_io_submit(bdev_io);
7161 0 : return 0;
7162 1 : }
7163 :
7164 : int
7165 1 : spdk_bdev_nvme_io_passthru_md(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
7166 : const struct spdk_nvme_cmd *cmd, void *buf, size_t nbytes, void *md_buf, size_t md_len,
7167 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7168 : {
7169 1 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7170 : struct spdk_bdev_io *bdev_io;
7171 1 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7172 :
7173 1 : if (!desc->write) {
7174 : /*
7175 : * Do not try to parse the NVMe command - we could maybe use bits in the opcode
7176 : * to easily determine if the command is a read or write, but for now just
7177 : * do not allow io_passthru with a read-only descriptor.
7178 : */
7179 0 : return -EBADF;
7180 : }
7181 :
7182 1 : if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO_MD))) {
7183 1 : return -ENOTSUP;
7184 : }
7185 :
7186 0 : bdev_io = bdev_channel_get_io(channel);
7187 0 : if (!bdev_io) {
7188 0 : return -ENOMEM;
7189 : }
7190 :
7191 0 : bdev_io->internal.ch = channel;
7192 0 : bdev_io->internal.desc = desc;
7193 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IO_MD;
7194 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
7195 0 : bdev_io->u.nvme_passthru.buf = buf;
7196 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
7197 0 : bdev_io->u.nvme_passthru.md_buf = md_buf;
7198 0 : bdev_io->u.nvme_passthru.md_len = md_len;
7199 :
7200 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7201 :
7202 0 : bdev_io_submit(bdev_io);
7203 0 : return 0;
7204 1 : }
7205 :
7206 : int
7207 0 : spdk_bdev_nvme_iov_passthru_md(struct spdk_bdev_desc *desc,
7208 : struct spdk_io_channel *ch,
7209 : const struct spdk_nvme_cmd *cmd,
7210 : struct iovec *iov, int iovcnt, size_t nbytes,
7211 : void *md_buf, size_t md_len,
7212 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7213 : {
7214 0 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7215 : struct spdk_bdev_io *bdev_io;
7216 0 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7217 :
7218 0 : if (!desc->write) {
7219 : /*
7220 : * Do not try to parse the NVMe command - we could maybe use bits in the opcode
7221 : * to easily determine if the command is a read or write, but for now just
7222 : * do not allow io_passthru with a read-only descriptor.
7223 : */
7224 0 : return -EBADF;
7225 : }
7226 :
7227 0 : if (md_buf && spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO_MD))) {
7228 0 : return -ENOTSUP;
7229 0 : } else if (spdk_unlikely(!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_NVME_IO))) {
7230 0 : return -ENOTSUP;
7231 : }
7232 :
7233 0 : bdev_io = bdev_channel_get_io(channel);
7234 0 : if (!bdev_io) {
7235 0 : return -ENOMEM;
7236 : }
7237 :
7238 0 : bdev_io->internal.ch = channel;
7239 0 : bdev_io->internal.desc = desc;
7240 0 : bdev_io->type = SPDK_BDEV_IO_TYPE_NVME_IOV_MD;
7241 0 : bdev_io->u.nvme_passthru.cmd = *cmd;
7242 0 : bdev_io->u.nvme_passthru.iovs = iov;
7243 0 : bdev_io->u.nvme_passthru.iovcnt = iovcnt;
7244 0 : bdev_io->u.nvme_passthru.nbytes = nbytes;
7245 0 : bdev_io->u.nvme_passthru.md_buf = md_buf;
7246 0 : bdev_io->u.nvme_passthru.md_len = md_len;
7247 :
7248 0 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7249 :
7250 0 : bdev_io_submit(bdev_io);
7251 0 : return 0;
7252 0 : }
7253 :
7254 : static void bdev_abort_retry(void *ctx);
7255 : static void bdev_abort(struct spdk_bdev_io *parent_io);
7256 :
7257 : static void
7258 22 : bdev_abort_io_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
7259 : {
7260 22 : struct spdk_bdev_channel *channel = bdev_io->internal.ch;
7261 22 : struct spdk_bdev_io *parent_io = cb_arg;
7262 : struct spdk_bdev_io *bio_to_abort, *tmp_io;
7263 :
7264 22 : bio_to_abort = bdev_io->u.abort.bio_to_abort;
7265 :
7266 22 : spdk_bdev_free_io(bdev_io);
7267 :
7268 22 : if (!success) {
7269 : /* Check if the target I/O completed in the meantime. */
7270 2 : TAILQ_FOREACH(tmp_io, &channel->io_submitted, internal.ch_link) {
7271 1 : if (tmp_io == bio_to_abort) {
7272 0 : break;
7273 : }
7274 1 : }
7275 :
7276 : /* If the target I/O still exists, set the parent to failed. */
7277 1 : if (tmp_io != NULL) {
7278 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7279 0 : }
7280 1 : }
7281 :
7282 22 : assert(parent_io->internal.f.split);
7283 :
7284 22 : parent_io->internal.split.outstanding--;
7285 22 : if (parent_io->internal.split.outstanding == 0) {
7286 16 : if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
7287 0 : bdev_abort_retry(parent_io);
7288 0 : } else {
7289 16 : bdev_io_complete(parent_io);
7290 : }
7291 16 : }
7292 22 : }
7293 :
7294 : static int
7295 23 : bdev_abort_io(struct spdk_bdev_desc *desc, struct spdk_bdev_channel *channel,
7296 : struct spdk_bdev_io *bio_to_abort,
7297 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7298 : {
7299 23 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7300 : struct spdk_bdev_io *bdev_io;
7301 :
7302 23 : if (bio_to_abort->type == SPDK_BDEV_IO_TYPE_ABORT ||
7303 23 : bio_to_abort->type == SPDK_BDEV_IO_TYPE_RESET) {
7304 : /* TODO: Abort reset or abort request. */
7305 0 : return -ENOTSUP;
7306 : }
7307 :
7308 23 : bdev_io = bdev_channel_get_io(channel);
7309 23 : if (bdev_io == NULL) {
7310 1 : return -ENOMEM;
7311 : }
7312 :
7313 22 : bdev_io->internal.ch = channel;
7314 22 : bdev_io->internal.desc = desc;
7315 22 : bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT;
7316 22 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7317 :
7318 22 : if (bio_to_abort->internal.f.split) {
7319 6 : assert(bdev_io_should_split(bio_to_abort));
7320 6 : bdev_io->u.bdev.abort.bio_cb_arg = bio_to_abort;
7321 :
7322 : /* Parent abort request is not submitted directly, but to manage its
7323 : * execution add it to the submitted list here.
7324 : */
7325 6 : bdev_io->internal.submit_tsc = spdk_get_ticks();
7326 6 : bdev_ch_add_to_io_submitted(bdev_io);
7327 :
7328 6 : bdev_abort(bdev_io);
7329 :
7330 6 : return 0;
7331 : }
7332 :
7333 16 : bdev_io->u.abort.bio_to_abort = bio_to_abort;
7334 :
7335 : /* Submit the abort request to the underlying bdev module. */
7336 16 : bdev_io_submit(bdev_io);
7337 :
7338 16 : return 0;
7339 23 : }
7340 :
7341 : static bool
7342 46 : bdev_io_on_tailq(struct spdk_bdev_io *bdev_io, bdev_io_tailq_t *tailq)
7343 : {
7344 : struct spdk_bdev_io *iter;
7345 :
7346 46 : TAILQ_FOREACH(iter, tailq, internal.link) {
7347 0 : if (iter == bdev_io) {
7348 0 : return true;
7349 : }
7350 0 : }
7351 :
7352 46 : return false;
7353 46 : }
7354 :
7355 : static uint32_t
7356 18 : _bdev_abort(struct spdk_bdev_io *parent_io)
7357 : {
7358 18 : struct spdk_bdev_desc *desc = parent_io->internal.desc;
7359 18 : struct spdk_bdev_channel *channel = parent_io->internal.ch;
7360 : void *bio_cb_arg;
7361 : struct spdk_bdev_io *bio_to_abort;
7362 : uint32_t matched_ios;
7363 : int rc;
7364 :
7365 18 : bio_cb_arg = parent_io->u.bdev.abort.bio_cb_arg;
7366 :
7367 : /* matched_ios is returned and will be kept by the caller.
7368 : *
7369 : * This function will be used for two cases, 1) the same cb_arg is used for
7370 : * multiple I/Os, 2) a single large I/O is split into smaller ones.
7371 : * Incrementing split_outstanding directly here may confuse readers especially
7372 : * for the 1st case.
7373 : *
7374 : * Completion of I/O abort is processed after stack unwinding. Hence this trick
7375 : * works as expected.
7376 : */
7377 18 : matched_ios = 0;
7378 18 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_SUCCESS;
7379 :
7380 105 : TAILQ_FOREACH(bio_to_abort, &channel->io_submitted, internal.ch_link) {
7381 88 : if (bio_to_abort->internal.caller_ctx != bio_cb_arg) {
7382 65 : continue;
7383 : }
7384 :
7385 23 : if (bio_to_abort->internal.submit_tsc > parent_io->internal.submit_tsc) {
7386 : /* Any I/O which was submitted after this abort command should be excluded. */
7387 0 : continue;
7388 : }
7389 :
7390 : /* We can't abort a request that's being pushed/pulled or executed by accel */
7391 23 : if (bdev_io_on_tailq(bio_to_abort, &channel->io_accel_exec) ||
7392 23 : bdev_io_on_tailq(bio_to_abort, &channel->io_memory_domain)) {
7393 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7394 0 : break;
7395 : }
7396 :
7397 23 : rc = bdev_abort_io(desc, channel, bio_to_abort, bdev_abort_io_done, parent_io);
7398 23 : if (rc != 0) {
7399 1 : if (rc == -ENOMEM) {
7400 1 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_NOMEM;
7401 1 : } else {
7402 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7403 : }
7404 1 : break;
7405 : }
7406 22 : matched_ios++;
7407 22 : }
7408 :
7409 18 : return matched_ios;
7410 : }
7411 :
7412 : static void
7413 1 : bdev_abort_retry(void *ctx)
7414 : {
7415 1 : struct spdk_bdev_io *parent_io = ctx;
7416 : uint32_t matched_ios;
7417 :
7418 1 : matched_ios = _bdev_abort(parent_io);
7419 :
7420 1 : if (matched_ios == 0) {
7421 0 : if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
7422 0 : bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry);
7423 0 : } else {
7424 : /* For retry, the case that no target I/O was found is success
7425 : * because it means target I/Os completed in the meantime.
7426 : */
7427 0 : bdev_io_complete(parent_io);
7428 : }
7429 0 : return;
7430 : }
7431 :
7432 : /* Use split_outstanding to manage the progress of aborting I/Os. */
7433 1 : parent_io->internal.f.split = true;
7434 1 : parent_io->internal.split.outstanding = matched_ios;
7435 1 : }
7436 :
7437 : static void
7438 17 : bdev_abort(struct spdk_bdev_io *parent_io)
7439 : {
7440 : uint32_t matched_ios;
7441 :
7442 17 : matched_ios = _bdev_abort(parent_io);
7443 :
7444 17 : if (matched_ios == 0) {
7445 2 : if (parent_io->internal.status == SPDK_BDEV_IO_STATUS_NOMEM) {
7446 1 : bdev_queue_io_wait_with_cb(parent_io, bdev_abort_retry);
7447 1 : } else {
7448 : /* The case the no target I/O was found is failure. */
7449 1 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7450 1 : bdev_io_complete(parent_io);
7451 : }
7452 2 : return;
7453 : }
7454 :
7455 : /* Use split_outstanding to manage the progress of aborting I/Os. */
7456 15 : parent_io->internal.f.split = true;
7457 15 : parent_io->internal.split.outstanding = matched_ios;
7458 17 : }
7459 :
7460 : int
7461 12 : spdk_bdev_abort(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
7462 : void *bio_cb_arg,
7463 : spdk_bdev_io_completion_cb cb, void *cb_arg)
7464 : {
7465 12 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
7466 12 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7467 : struct spdk_bdev_io *bdev_io;
7468 :
7469 12 : if (bio_cb_arg == NULL) {
7470 0 : return -EINVAL;
7471 : }
7472 :
7473 12 : if (!spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_ABORT)) {
7474 1 : return -ENOTSUP;
7475 : }
7476 :
7477 11 : bdev_io = bdev_channel_get_io(channel);
7478 11 : if (bdev_io == NULL) {
7479 0 : return -ENOMEM;
7480 : }
7481 :
7482 11 : bdev_io->internal.ch = channel;
7483 11 : bdev_io->internal.desc = desc;
7484 11 : bdev_io->internal.submit_tsc = spdk_get_ticks();
7485 11 : bdev_io->type = SPDK_BDEV_IO_TYPE_ABORT;
7486 11 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
7487 :
7488 11 : bdev_io->u.bdev.abort.bio_cb_arg = bio_cb_arg;
7489 :
7490 : /* Parent abort request is not submitted directly, but to manage its execution,
7491 : * add it to the submitted list here.
7492 : */
7493 11 : bdev_ch_add_to_io_submitted(bdev_io);
7494 :
7495 11 : bdev_abort(bdev_io);
7496 :
7497 11 : return 0;
7498 12 : }
7499 :
7500 : int
7501 4 : spdk_bdev_queue_io_wait(struct spdk_bdev *bdev, struct spdk_io_channel *ch,
7502 : struct spdk_bdev_io_wait_entry *entry)
7503 : {
7504 4 : struct spdk_bdev_channel *channel = __io_ch_to_bdev_ch(ch);
7505 4 : struct spdk_bdev_mgmt_channel *mgmt_ch = channel->shared_resource->mgmt_ch;
7506 :
7507 4 : if (bdev != entry->bdev) {
7508 0 : SPDK_ERRLOG("bdevs do not match\n");
7509 0 : return -EINVAL;
7510 : }
7511 :
7512 4 : if (mgmt_ch->per_thread_cache_count > 0) {
7513 0 : SPDK_ERRLOG("Cannot queue io_wait if spdk_bdev_io available in per-thread cache\n");
7514 0 : return -EINVAL;
7515 : }
7516 :
7517 4 : TAILQ_INSERT_TAIL(&mgmt_ch->io_wait_queue, entry, link);
7518 4 : return 0;
7519 4 : }
7520 :
7521 : static inline void
7522 610 : bdev_io_update_io_stat(struct spdk_bdev_io *bdev_io, uint64_t tsc_diff)
7523 : {
7524 610 : enum spdk_bdev_io_status io_status = bdev_io->internal.status;
7525 610 : struct spdk_bdev_io_stat *io_stat = bdev_io->internal.ch->stat;
7526 610 : uint64_t num_blocks = bdev_io->u.bdev.num_blocks;
7527 610 : uint32_t blocklen = bdev_io->bdev->blocklen;
7528 :
7529 610 : if (spdk_likely(io_status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
7530 516 : switch (bdev_io->type) {
7531 : case SPDK_BDEV_IO_TYPE_READ:
7532 321 : io_stat->bytes_read += num_blocks * blocklen;
7533 321 : io_stat->num_read_ops++;
7534 321 : io_stat->read_latency_ticks += tsc_diff;
7535 321 : if (io_stat->max_read_latency_ticks < tsc_diff) {
7536 7 : io_stat->max_read_latency_ticks = tsc_diff;
7537 7 : }
7538 321 : if (io_stat->min_read_latency_ticks > tsc_diff) {
7539 42 : io_stat->min_read_latency_ticks = tsc_diff;
7540 42 : }
7541 321 : break;
7542 : case SPDK_BDEV_IO_TYPE_WRITE:
7543 75 : io_stat->bytes_written += num_blocks * blocklen;
7544 75 : io_stat->num_write_ops++;
7545 75 : io_stat->write_latency_ticks += tsc_diff;
7546 75 : if (io_stat->max_write_latency_ticks < tsc_diff) {
7547 4 : io_stat->max_write_latency_ticks = tsc_diff;
7548 4 : }
7549 75 : if (io_stat->min_write_latency_ticks > tsc_diff) {
7550 25 : io_stat->min_write_latency_ticks = tsc_diff;
7551 25 : }
7552 75 : break;
7553 : case SPDK_BDEV_IO_TYPE_UNMAP:
7554 20 : io_stat->bytes_unmapped += num_blocks * blocklen;
7555 20 : io_stat->num_unmap_ops++;
7556 20 : io_stat->unmap_latency_ticks += tsc_diff;
7557 20 : if (io_stat->max_unmap_latency_ticks < tsc_diff) {
7558 0 : io_stat->max_unmap_latency_ticks = tsc_diff;
7559 0 : }
7560 20 : if (io_stat->min_unmap_latency_ticks > tsc_diff) {
7561 3 : io_stat->min_unmap_latency_ticks = tsc_diff;
7562 3 : }
7563 20 : break;
7564 : case SPDK_BDEV_IO_TYPE_ZCOPY:
7565 : /* Track the data in the start phase only */
7566 4 : if (bdev_io->u.bdev.zcopy.start) {
7567 2 : if (bdev_io->u.bdev.zcopy.populate) {
7568 1 : io_stat->bytes_read += num_blocks * blocklen;
7569 1 : io_stat->num_read_ops++;
7570 1 : io_stat->read_latency_ticks += tsc_diff;
7571 1 : if (io_stat->max_read_latency_ticks < tsc_diff) {
7572 0 : io_stat->max_read_latency_ticks = tsc_diff;
7573 0 : }
7574 1 : if (io_stat->min_read_latency_ticks > tsc_diff) {
7575 1 : io_stat->min_read_latency_ticks = tsc_diff;
7576 1 : }
7577 1 : } else {
7578 1 : io_stat->bytes_written += num_blocks * blocklen;
7579 1 : io_stat->num_write_ops++;
7580 1 : io_stat->write_latency_ticks += tsc_diff;
7581 1 : if (io_stat->max_write_latency_ticks < tsc_diff) {
7582 0 : io_stat->max_write_latency_ticks = tsc_diff;
7583 0 : }
7584 1 : if (io_stat->min_write_latency_ticks > tsc_diff) {
7585 1 : io_stat->min_write_latency_ticks = tsc_diff;
7586 1 : }
7587 : }
7588 2 : }
7589 4 : break;
7590 : case SPDK_BDEV_IO_TYPE_COPY:
7591 21 : io_stat->bytes_copied += num_blocks * blocklen;
7592 21 : io_stat->num_copy_ops++;
7593 21 : bdev_io->internal.ch->stat->copy_latency_ticks += tsc_diff;
7594 21 : if (io_stat->max_copy_latency_ticks < tsc_diff) {
7595 0 : io_stat->max_copy_latency_ticks = tsc_diff;
7596 0 : }
7597 21 : if (io_stat->min_copy_latency_ticks > tsc_diff) {
7598 4 : io_stat->min_copy_latency_ticks = tsc_diff;
7599 4 : }
7600 21 : break;
7601 : default:
7602 75 : break;
7603 : }
7604 610 : } else if (io_status <= SPDK_BDEV_IO_STATUS_FAILED && io_status >= SPDK_MIN_BDEV_IO_STATUS) {
7605 94 : io_stat = bdev_io->bdev->internal.stat;
7606 94 : assert(io_stat->io_error != NULL);
7607 :
7608 94 : spdk_spin_lock(&bdev_io->bdev->internal.spinlock);
7609 94 : io_stat->io_error->error_status[-io_status - 1]++;
7610 94 : spdk_spin_unlock(&bdev_io->bdev->internal.spinlock);
7611 94 : }
7612 :
7613 : #ifdef SPDK_CONFIG_VTUNE
7614 : uint64_t now_tsc = spdk_get_ticks();
7615 : if (now_tsc > (bdev_io->internal.ch->start_tsc + bdev_io->internal.ch->interval_tsc)) {
7616 : uint64_t data[5];
7617 : struct spdk_bdev_io_stat *prev_stat = bdev_io->internal.ch->prev_stat;
7618 :
7619 : data[0] = io_stat->num_read_ops - prev_stat->num_read_ops;
7620 : data[1] = io_stat->bytes_read - prev_stat->bytes_read;
7621 : data[2] = io_stat->num_write_ops - prev_stat->num_write_ops;
7622 : data[3] = io_stat->bytes_written - prev_stat->bytes_written;
7623 : data[4] = bdev_io->bdev->fn_table->get_spin_time ?
7624 : bdev_io->bdev->fn_table->get_spin_time(spdk_bdev_io_get_io_channel(bdev_io)) : 0;
7625 :
7626 : __itt_metadata_add(g_bdev_mgr.domain, __itt_null, bdev_io->internal.ch->handle,
7627 : __itt_metadata_u64, 5, data);
7628 :
7629 : memcpy(prev_stat, io_stat, sizeof(struct spdk_bdev_io_stat));
7630 : bdev_io->internal.ch->start_tsc = now_tsc;
7631 : }
7632 : #endif
7633 610 : }
7634 :
7635 : static inline void
7636 610 : _bdev_io_complete(void *ctx)
7637 : {
7638 610 : struct spdk_bdev_io *bdev_io = ctx;
7639 :
7640 610 : if (spdk_unlikely(bdev_io_use_accel_sequence(bdev_io))) {
7641 0 : assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_SUCCESS);
7642 0 : spdk_accel_sequence_abort(bdev_io->internal.accel_sequence);
7643 0 : }
7644 :
7645 610 : assert(bdev_io->internal.cb != NULL);
7646 610 : assert(spdk_get_thread() == spdk_bdev_io_get_thread(bdev_io));
7647 :
7648 1220 : bdev_io->internal.cb(bdev_io, bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS,
7649 610 : bdev_io->internal.caller_ctx);
7650 610 : }
7651 :
7652 : static inline void
7653 618 : bdev_io_complete(void *ctx)
7654 : {
7655 618 : struct spdk_bdev_io *bdev_io = ctx;
7656 618 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
7657 : uint64_t tsc, tsc_diff;
7658 :
7659 618 : if (spdk_unlikely(bdev_io->internal.f.in_submit_request)) {
7660 : /*
7661 : * Defer completion to avoid potential infinite recursion if the
7662 : * user's completion callback issues a new I/O.
7663 : */
7664 16 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
7665 8 : bdev_io_complete, bdev_io);
7666 8 : return;
7667 : }
7668 :
7669 610 : tsc = spdk_get_ticks();
7670 610 : tsc_diff = tsc - bdev_io->internal.submit_tsc;
7671 :
7672 610 : bdev_ch_remove_from_io_submitted(bdev_io);
7673 610 : spdk_trace_record_tsc(tsc, TRACE_BDEV_IO_DONE, bdev_ch->trace_id, 0, (uintptr_t)bdev_io,
7674 : bdev_io->internal.caller_ctx, bdev_ch->queue_depth);
7675 :
7676 610 : if (bdev_ch->histogram) {
7677 4 : if (bdev_io->bdev->internal.histogram_io_type == 0 ||
7678 0 : bdev_io->bdev->internal.histogram_io_type == bdev_io->type) {
7679 : /*
7680 : * Tally all I/O types if the histogram_io_type is set to 0.
7681 : */
7682 4 : spdk_histogram_data_tally(bdev_ch->histogram, tsc_diff);
7683 4 : }
7684 4 : }
7685 :
7686 610 : bdev_io_update_io_stat(bdev_io, tsc_diff);
7687 610 : _bdev_io_complete(bdev_io);
7688 618 : }
7689 :
7690 : /* The difference between this function and bdev_io_complete() is that this should be called to
7691 : * complete IOs that haven't been submitted via bdev_io_submit(), as they weren't added onto the
7692 : * io_submitted list and don't have submit_tsc updated.
7693 : */
7694 : static inline void
7695 0 : bdev_io_complete_unsubmitted(struct spdk_bdev_io *bdev_io)
7696 : {
7697 : /* Since the IO hasn't been submitted it's bound to be failed */
7698 0 : assert(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_SUCCESS);
7699 :
7700 : /* At this point we don't know if the IO is completed from submission context or not, but,
7701 : * since this is an error path, we can always do an spdk_thread_send_msg(). */
7702 0 : spdk_thread_send_msg(spdk_bdev_io_get_thread(bdev_io),
7703 0 : _bdev_io_complete, bdev_io);
7704 0 : }
7705 :
7706 : static void bdev_destroy_cb(void *io_device);
7707 :
7708 : static void
7709 15 : bdev_reset_complete(struct spdk_bdev *bdev, void *_ctx, int status)
7710 : {
7711 15 : struct spdk_bdev_io *bdev_io = _ctx;
7712 :
7713 15 : if (bdev_io->u.reset.ch_ref != NULL) {
7714 15 : spdk_put_io_channel(bdev_io->u.reset.ch_ref);
7715 15 : bdev_io->u.reset.ch_ref = NULL;
7716 15 : }
7717 :
7718 15 : bdev_io_complete(bdev_io);
7719 :
7720 15 : if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING &&
7721 1 : TAILQ_EMPTY(&bdev->internal.open_descs)) {
7722 1 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
7723 1 : }
7724 15 : }
7725 :
7726 : static void
7727 18 : bdev_unfreeze_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
7728 : struct spdk_io_channel *_ch, void *_ctx)
7729 : {
7730 18 : struct spdk_bdev_io *bdev_io = _ctx;
7731 18 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
7732 : struct spdk_bdev_io *queued_reset;
7733 :
7734 18 : ch->flags &= ~BDEV_CH_RESET_IN_PROGRESS;
7735 18 : while (!TAILQ_EMPTY(&ch->queued_resets)) {
7736 0 : queued_reset = TAILQ_FIRST(&ch->queued_resets);
7737 0 : TAILQ_REMOVE(&ch->queued_resets, queued_reset, internal.link);
7738 0 : spdk_bdev_io_complete(queued_reset, bdev_io->internal.status);
7739 : }
7740 :
7741 18 : spdk_bdev_for_each_channel_continue(i, 0);
7742 18 : }
7743 :
7744 : static void
7745 0 : bdev_io_complete_sequence_cb(void *ctx, int status)
7746 : {
7747 0 : struct spdk_bdev_io *bdev_io = ctx;
7748 :
7749 : /* u.bdev.accel_sequence should have already been cleared at this point */
7750 0 : assert(bdev_io->u.bdev.accel_sequence == NULL);
7751 0 : assert(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS);
7752 0 : bdev_io->internal.f.has_accel_sequence = false;
7753 :
7754 0 : if (spdk_unlikely(status != 0)) {
7755 0 : SPDK_ERRLOG("Failed to execute accel sequence, status=%d\n", status);
7756 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
7757 0 : }
7758 :
7759 0 : bdev_io_complete(bdev_io);
7760 0 : }
7761 :
7762 : void
7763 598 : spdk_bdev_io_complete(struct spdk_bdev_io *bdev_io, enum spdk_bdev_io_status status)
7764 : {
7765 598 : struct spdk_bdev *bdev = bdev_io->bdev;
7766 598 : struct spdk_bdev_channel *bdev_ch = bdev_io->internal.ch;
7767 598 : struct spdk_bdev_shared_resource *shared_resource = bdev_ch->shared_resource;
7768 :
7769 598 : if (spdk_unlikely(bdev_io->internal.status != SPDK_BDEV_IO_STATUS_PENDING)) {
7770 0 : SPDK_ERRLOG("Unexpected completion on IO from %s module, status was %s\n",
7771 : spdk_bdev_get_module_name(bdev),
7772 : bdev_io_status_get_string(bdev_io->internal.status));
7773 0 : assert(false);
7774 : }
7775 598 : bdev_io->internal.status = status;
7776 :
7777 598 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_RESET)) {
7778 16 : bool unlock_channels = false;
7779 :
7780 16 : if (status == SPDK_BDEV_IO_STATUS_NOMEM) {
7781 0 : SPDK_ERRLOG("NOMEM returned for reset\n");
7782 0 : }
7783 16 : spdk_spin_lock(&bdev->internal.spinlock);
7784 16 : if (bdev_io == bdev->internal.reset_in_progress) {
7785 15 : bdev->internal.reset_in_progress = NULL;
7786 15 : unlock_channels = true;
7787 15 : }
7788 16 : spdk_spin_unlock(&bdev->internal.spinlock);
7789 :
7790 16 : if (unlock_channels) {
7791 15 : spdk_bdev_for_each_channel(bdev, bdev_unfreeze_channel, bdev_io,
7792 : bdev_reset_complete);
7793 15 : return;
7794 : }
7795 1 : } else {
7796 582 : bdev_io_decrement_outstanding(bdev_ch, shared_resource);
7797 582 : if (spdk_likely(status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
7798 485 : if (bdev_io_needs_sequence_exec(bdev_io->internal.desc, bdev_io)) {
7799 0 : bdev_io_exec_sequence(bdev_io, bdev_io_complete_sequence_cb);
7800 0 : return;
7801 485 : } else if (spdk_unlikely(bdev_io->internal.f.has_bounce_buf &&
7802 : !bdev_io_use_accel_sequence(bdev_io))) {
7803 26 : _bdev_io_push_bounce_data_buffer(bdev_io,
7804 : _bdev_io_complete_push_bounce_done);
7805 : /* bdev IO will be completed in the callback */
7806 26 : return;
7807 : }
7808 459 : }
7809 :
7810 556 : if (spdk_unlikely(_bdev_io_handle_no_mem(bdev_io, BDEV_IO_RETRY_STATE_SUBMIT))) {
7811 5 : return;
7812 : }
7813 : }
7814 :
7815 552 : bdev_io_complete(bdev_io);
7816 598 : }
7817 :
7818 : void
7819 0 : spdk_bdev_io_complete_scsi_status(struct spdk_bdev_io *bdev_io, enum spdk_scsi_status sc,
7820 : enum spdk_scsi_sense sk, uint8_t asc, uint8_t ascq)
7821 : {
7822 : enum spdk_bdev_io_status status;
7823 :
7824 0 : if (sc == SPDK_SCSI_STATUS_GOOD) {
7825 0 : status = SPDK_BDEV_IO_STATUS_SUCCESS;
7826 0 : } else {
7827 0 : status = SPDK_BDEV_IO_STATUS_SCSI_ERROR;
7828 0 : bdev_io->internal.error.scsi.sc = sc;
7829 0 : bdev_io->internal.error.scsi.sk = sk;
7830 0 : bdev_io->internal.error.scsi.asc = asc;
7831 0 : bdev_io->internal.error.scsi.ascq = ascq;
7832 : }
7833 :
7834 0 : spdk_bdev_io_complete(bdev_io, status);
7835 0 : }
7836 :
7837 : void
7838 0 : spdk_bdev_io_get_scsi_status(const struct spdk_bdev_io *bdev_io,
7839 : int *sc, int *sk, int *asc, int *ascq)
7840 : {
7841 0 : assert(sc != NULL);
7842 0 : assert(sk != NULL);
7843 0 : assert(asc != NULL);
7844 0 : assert(ascq != NULL);
7845 :
7846 0 : switch (bdev_io->internal.status) {
7847 : case SPDK_BDEV_IO_STATUS_SUCCESS:
7848 0 : *sc = SPDK_SCSI_STATUS_GOOD;
7849 0 : *sk = SPDK_SCSI_SENSE_NO_SENSE;
7850 0 : *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
7851 0 : *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
7852 0 : break;
7853 : case SPDK_BDEV_IO_STATUS_NVME_ERROR:
7854 0 : spdk_scsi_nvme_translate(bdev_io, sc, sk, asc, ascq);
7855 0 : break;
7856 : case SPDK_BDEV_IO_STATUS_MISCOMPARE:
7857 0 : *sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
7858 0 : *sk = SPDK_SCSI_SENSE_MISCOMPARE;
7859 0 : *asc = SPDK_SCSI_ASC_MISCOMPARE_DURING_VERIFY_OPERATION;
7860 0 : *ascq = bdev_io->internal.error.scsi.ascq;
7861 0 : break;
7862 : case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
7863 0 : *sc = bdev_io->internal.error.scsi.sc;
7864 0 : *sk = bdev_io->internal.error.scsi.sk;
7865 0 : *asc = bdev_io->internal.error.scsi.asc;
7866 0 : *ascq = bdev_io->internal.error.scsi.ascq;
7867 0 : break;
7868 : default:
7869 0 : *sc = SPDK_SCSI_STATUS_CHECK_CONDITION;
7870 0 : *sk = SPDK_SCSI_SENSE_ABORTED_COMMAND;
7871 0 : *asc = SPDK_SCSI_ASC_NO_ADDITIONAL_SENSE;
7872 0 : *ascq = SPDK_SCSI_ASCQ_CAUSE_NOT_REPORTABLE;
7873 0 : break;
7874 : }
7875 0 : }
7876 :
7877 : void
7878 0 : spdk_bdev_io_complete_aio_status(struct spdk_bdev_io *bdev_io, int aio_result)
7879 : {
7880 : enum spdk_bdev_io_status status;
7881 :
7882 0 : if (aio_result == 0) {
7883 0 : status = SPDK_BDEV_IO_STATUS_SUCCESS;
7884 0 : } else {
7885 0 : status = SPDK_BDEV_IO_STATUS_AIO_ERROR;
7886 : }
7887 :
7888 0 : bdev_io->internal.error.aio_result = aio_result;
7889 :
7890 0 : spdk_bdev_io_complete(bdev_io, status);
7891 0 : }
7892 :
7893 : void
7894 0 : spdk_bdev_io_get_aio_status(const struct spdk_bdev_io *bdev_io, int *aio_result)
7895 : {
7896 0 : assert(aio_result != NULL);
7897 :
7898 0 : if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_AIO_ERROR) {
7899 0 : *aio_result = bdev_io->internal.error.aio_result;
7900 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
7901 0 : *aio_result = 0;
7902 0 : } else {
7903 0 : *aio_result = -EIO;
7904 : }
7905 0 : }
7906 :
7907 : void
7908 0 : spdk_bdev_io_complete_nvme_status(struct spdk_bdev_io *bdev_io, uint32_t cdw0, int sct, int sc)
7909 : {
7910 : enum spdk_bdev_io_status status;
7911 :
7912 0 : if (spdk_likely(sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_SUCCESS)) {
7913 0 : status = SPDK_BDEV_IO_STATUS_SUCCESS;
7914 0 : } else if (sct == SPDK_NVME_SCT_GENERIC && sc == SPDK_NVME_SC_ABORTED_BY_REQUEST) {
7915 0 : status = SPDK_BDEV_IO_STATUS_ABORTED;
7916 0 : } else {
7917 0 : status = SPDK_BDEV_IO_STATUS_NVME_ERROR;
7918 : }
7919 :
7920 0 : bdev_io->internal.error.nvme.cdw0 = cdw0;
7921 0 : bdev_io->internal.error.nvme.sct = sct;
7922 0 : bdev_io->internal.error.nvme.sc = sc;
7923 :
7924 0 : spdk_bdev_io_complete(bdev_io, status);
7925 0 : }
7926 :
7927 : void
7928 0 : spdk_bdev_io_get_nvme_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0, int *sct, int *sc)
7929 : {
7930 0 : assert(sct != NULL);
7931 0 : assert(sc != NULL);
7932 0 : assert(cdw0 != NULL);
7933 :
7934 0 : if (spdk_unlikely(bdev_io->type == SPDK_BDEV_IO_TYPE_ABORT)) {
7935 0 : *sct = SPDK_NVME_SCT_GENERIC;
7936 0 : *sc = SPDK_NVME_SC_SUCCESS;
7937 0 : if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
7938 0 : *cdw0 = 0;
7939 0 : } else {
7940 0 : *cdw0 = 1U;
7941 : }
7942 0 : return;
7943 : }
7944 :
7945 0 : if (spdk_likely(bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS)) {
7946 0 : *sct = SPDK_NVME_SCT_GENERIC;
7947 0 : *sc = SPDK_NVME_SC_SUCCESS;
7948 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
7949 0 : *sct = bdev_io->internal.error.nvme.sct;
7950 0 : *sc = bdev_io->internal.error.nvme.sc;
7951 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) {
7952 0 : *sct = SPDK_NVME_SCT_GENERIC;
7953 0 : *sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
7954 0 : } else {
7955 0 : *sct = SPDK_NVME_SCT_GENERIC;
7956 0 : *sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
7957 : }
7958 :
7959 0 : *cdw0 = bdev_io->internal.error.nvme.cdw0;
7960 0 : }
7961 :
7962 : void
7963 0 : spdk_bdev_io_get_nvme_fused_status(const struct spdk_bdev_io *bdev_io, uint32_t *cdw0,
7964 : int *first_sct, int *first_sc, int *second_sct, int *second_sc)
7965 : {
7966 0 : assert(first_sct != NULL);
7967 0 : assert(first_sc != NULL);
7968 0 : assert(second_sct != NULL);
7969 0 : assert(second_sc != NULL);
7970 0 : assert(cdw0 != NULL);
7971 :
7972 0 : if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_NVME_ERROR) {
7973 0 : if (bdev_io->internal.error.nvme.sct == SPDK_NVME_SCT_MEDIA_ERROR &&
7974 0 : bdev_io->internal.error.nvme.sc == SPDK_NVME_SC_COMPARE_FAILURE) {
7975 0 : *first_sct = bdev_io->internal.error.nvme.sct;
7976 0 : *first_sc = bdev_io->internal.error.nvme.sc;
7977 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7978 0 : *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
7979 0 : } else {
7980 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7981 0 : *first_sc = SPDK_NVME_SC_SUCCESS;
7982 0 : *second_sct = bdev_io->internal.error.nvme.sct;
7983 0 : *second_sc = bdev_io->internal.error.nvme.sc;
7984 : }
7985 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_ABORTED) {
7986 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7987 0 : *first_sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
7988 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7989 0 : *second_sc = SPDK_NVME_SC_ABORTED_BY_REQUEST;
7990 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_SUCCESS) {
7991 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7992 0 : *first_sc = SPDK_NVME_SC_SUCCESS;
7993 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7994 0 : *second_sc = SPDK_NVME_SC_SUCCESS;
7995 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_FIRST_FUSED_FAILED) {
7996 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
7997 0 : *first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
7998 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
7999 0 : *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
8000 0 : } else if (bdev_io->internal.status == SPDK_BDEV_IO_STATUS_MISCOMPARE) {
8001 0 : *first_sct = SPDK_NVME_SCT_MEDIA_ERROR;
8002 0 : *first_sc = SPDK_NVME_SC_COMPARE_FAILURE;
8003 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
8004 0 : *second_sc = SPDK_NVME_SC_ABORTED_FAILED_FUSED;
8005 0 : } else {
8006 0 : *first_sct = SPDK_NVME_SCT_GENERIC;
8007 0 : *first_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
8008 0 : *second_sct = SPDK_NVME_SCT_GENERIC;
8009 0 : *second_sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR;
8010 : }
8011 :
8012 0 : *cdw0 = bdev_io->internal.error.nvme.cdw0;
8013 0 : }
8014 :
8015 : void
8016 0 : spdk_bdev_io_complete_base_io_status(struct spdk_bdev_io *bdev_io,
8017 : const struct spdk_bdev_io *base_io)
8018 : {
8019 0 : switch (base_io->internal.status) {
8020 : case SPDK_BDEV_IO_STATUS_NVME_ERROR:
8021 0 : spdk_bdev_io_complete_nvme_status(bdev_io,
8022 0 : base_io->internal.error.nvme.cdw0,
8023 0 : base_io->internal.error.nvme.sct,
8024 0 : base_io->internal.error.nvme.sc);
8025 0 : break;
8026 : case SPDK_BDEV_IO_STATUS_SCSI_ERROR:
8027 0 : spdk_bdev_io_complete_scsi_status(bdev_io,
8028 0 : base_io->internal.error.scsi.sc,
8029 0 : base_io->internal.error.scsi.sk,
8030 0 : base_io->internal.error.scsi.asc,
8031 0 : base_io->internal.error.scsi.ascq);
8032 0 : break;
8033 : case SPDK_BDEV_IO_STATUS_AIO_ERROR:
8034 0 : spdk_bdev_io_complete_aio_status(bdev_io, base_io->internal.error.aio_result);
8035 0 : break;
8036 : default:
8037 0 : spdk_bdev_io_complete(bdev_io, base_io->internal.status);
8038 0 : break;
8039 : }
8040 0 : }
8041 :
8042 : struct spdk_thread *
8043 660 : spdk_bdev_io_get_thread(struct spdk_bdev_io *bdev_io)
8044 : {
8045 660 : return spdk_io_channel_get_thread(bdev_io->internal.ch->channel);
8046 : }
8047 :
8048 : struct spdk_io_channel *
8049 70 : spdk_bdev_io_get_io_channel(struct spdk_bdev_io *bdev_io)
8050 : {
8051 70 : return bdev_io->internal.ch->channel;
8052 : }
8053 :
8054 : static int
8055 128 : bdev_register(struct spdk_bdev *bdev)
8056 : {
8057 : char *bdev_name;
8058 : char uuid[SPDK_UUID_STRING_LEN];
8059 : struct spdk_iobuf_opts iobuf_opts;
8060 : int ret;
8061 :
8062 128 : assert(bdev->module != NULL);
8063 :
8064 128 : if (!bdev->name) {
8065 0 : SPDK_ERRLOG("Bdev name is NULL\n");
8066 0 : return -EINVAL;
8067 : }
8068 :
8069 128 : if (!strlen(bdev->name)) {
8070 0 : SPDK_ERRLOG("Bdev name must not be an empty string\n");
8071 0 : return -EINVAL;
8072 : }
8073 :
8074 : /* Users often register their own I/O devices using the bdev name. In
8075 : * order to avoid conflicts, prepend bdev_. */
8076 128 : bdev_name = spdk_sprintf_alloc("bdev_%s", bdev->name);
8077 128 : if (!bdev_name) {
8078 0 : SPDK_ERRLOG("Unable to allocate memory for internal bdev name.\n");
8079 0 : return -ENOMEM;
8080 : }
8081 :
8082 128 : bdev->internal.stat = bdev_alloc_io_stat(true);
8083 128 : if (!bdev->internal.stat) {
8084 0 : SPDK_ERRLOG("Unable to allocate I/O statistics structure.\n");
8085 0 : free(bdev_name);
8086 0 : return -ENOMEM;
8087 : }
8088 :
8089 128 : bdev->internal.status = SPDK_BDEV_STATUS_READY;
8090 128 : bdev->internal.measured_queue_depth = UINT64_MAX;
8091 128 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
8092 128 : memset(&bdev->internal.claim, 0, sizeof(bdev->internal.claim));
8093 128 : bdev->internal.qd_poller = NULL;
8094 128 : bdev->internal.qos = NULL;
8095 :
8096 128 : TAILQ_INIT(&bdev->internal.open_descs);
8097 128 : TAILQ_INIT(&bdev->internal.locked_ranges);
8098 128 : TAILQ_INIT(&bdev->internal.pending_locked_ranges);
8099 128 : TAILQ_INIT(&bdev->aliases);
8100 :
8101 : /* UUID may be specified by the user or defined by bdev itself.
8102 : * Otherwise it will be generated here, so this field will never be empty. */
8103 128 : if (spdk_uuid_is_null(&bdev->uuid)) {
8104 42 : spdk_uuid_generate(&bdev->uuid);
8105 42 : }
8106 :
8107 : /* Add the UUID alias only if it's different than the name */
8108 128 : spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid);
8109 128 : if (strcmp(bdev->name, uuid) != 0) {
8110 127 : ret = spdk_bdev_alias_add(bdev, uuid);
8111 127 : if (ret != 0) {
8112 2 : SPDK_ERRLOG("Unable to add uuid:%s alias for bdev %s\n", uuid, bdev->name);
8113 2 : bdev_free_io_stat(bdev->internal.stat);
8114 2 : free(bdev_name);
8115 2 : return ret;
8116 : }
8117 125 : }
8118 :
8119 126 : spdk_iobuf_get_opts(&iobuf_opts, sizeof(iobuf_opts));
8120 126 : if (spdk_bdev_get_buf_align(bdev) > 1) {
8121 0 : bdev->max_rw_size = spdk_min(bdev->max_rw_size ? bdev->max_rw_size : UINT32_MAX,
8122 : iobuf_opts.large_bufsize / bdev->blocklen);
8123 0 : }
8124 :
8125 : /* If the user didn't specify a write unit size, set it to one. */
8126 126 : if (bdev->write_unit_size == 0) {
8127 122 : bdev->write_unit_size = 1;
8128 122 : }
8129 :
8130 : /* Set ACWU value to the write unit size if bdev module did not set it (does not support it natively) */
8131 126 : if (bdev->acwu == 0) {
8132 122 : bdev->acwu = bdev->write_unit_size;
8133 122 : }
8134 :
8135 126 : if (bdev->phys_blocklen == 0) {
8136 122 : bdev->phys_blocklen = spdk_bdev_get_data_block_size(bdev);
8137 122 : }
8138 :
8139 126 : if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COPY)) {
8140 0 : bdev->max_copy = bdev_get_max_write(bdev, iobuf_opts.large_bufsize);
8141 0 : }
8142 :
8143 126 : if (!bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_WRITE_ZEROES)) {
8144 0 : bdev->max_write_zeroes = bdev_get_max_write(bdev, ZERO_BUFFER_SIZE);
8145 0 : }
8146 :
8147 126 : bdev->internal.reset_in_progress = NULL;
8148 126 : bdev->internal.qd_poll_in_progress = false;
8149 126 : bdev->internal.period = 0;
8150 126 : bdev->internal.new_period = 0;
8151 126 : bdev->internal.trace_id = spdk_trace_register_owner(OWNER_TYPE_BDEV, bdev_name);
8152 :
8153 : /*
8154 : * Initialize spinlock before registering IO device because spinlock is used in
8155 : * bdev_channel_create
8156 : */
8157 126 : spdk_spin_init(&bdev->internal.spinlock);
8158 :
8159 252 : spdk_io_device_register(__bdev_to_io_dev(bdev),
8160 : bdev_channel_create, bdev_channel_destroy,
8161 : sizeof(struct spdk_bdev_channel),
8162 126 : bdev_name);
8163 :
8164 : /*
8165 : * Register bdev name only after the bdev object is ready.
8166 : * After bdev_name_add returns, it is possible for other threads to start using the bdev,
8167 : * create IO channels...
8168 : */
8169 126 : ret = bdev_name_add(&bdev->internal.bdev_name, bdev, bdev->name);
8170 126 : if (ret != 0) {
8171 0 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), NULL);
8172 0 : bdev_free_io_stat(bdev->internal.stat);
8173 0 : spdk_spin_destroy(&bdev->internal.spinlock);
8174 0 : free(bdev_name);
8175 0 : return ret;
8176 : }
8177 :
8178 126 : free(bdev_name);
8179 :
8180 126 : SPDK_DEBUGLOG(bdev, "Inserting bdev %s into list\n", bdev->name);
8181 126 : TAILQ_INSERT_TAIL(&g_bdev_mgr.bdevs, bdev, internal.link);
8182 :
8183 126 : return 0;
8184 128 : }
8185 :
8186 : static void
8187 127 : bdev_destroy_cb(void *io_device)
8188 : {
8189 : int rc;
8190 : struct spdk_bdev *bdev;
8191 : spdk_bdev_unregister_cb cb_fn;
8192 : void *cb_arg;
8193 :
8194 127 : bdev = __bdev_from_io_dev(io_device);
8195 :
8196 127 : if (bdev->internal.unregister_td != spdk_get_thread()) {
8197 1 : spdk_thread_send_msg(bdev->internal.unregister_td, bdev_destroy_cb, io_device);
8198 1 : return;
8199 : }
8200 :
8201 126 : cb_fn = bdev->internal.unregister_cb;
8202 126 : cb_arg = bdev->internal.unregister_ctx;
8203 :
8204 126 : spdk_spin_destroy(&bdev->internal.spinlock);
8205 126 : free(bdev->internal.qos);
8206 126 : bdev_free_io_stat(bdev->internal.stat);
8207 126 : spdk_trace_unregister_owner(bdev->internal.trace_id);
8208 :
8209 126 : rc = bdev->fn_table->destruct(bdev->ctxt);
8210 126 : if (rc < 0) {
8211 0 : SPDK_ERRLOG("destruct failed\n");
8212 0 : }
8213 126 : if (rc <= 0 && cb_fn != NULL) {
8214 10 : cb_fn(cb_arg, rc);
8215 10 : }
8216 127 : }
8217 :
8218 : void
8219 2 : spdk_bdev_destruct_done(struct spdk_bdev *bdev, int bdeverrno)
8220 : {
8221 2 : if (bdev->internal.unregister_cb != NULL) {
8222 0 : bdev->internal.unregister_cb(bdev->internal.unregister_ctx, bdeverrno);
8223 0 : }
8224 2 : }
8225 :
8226 : static void
8227 19 : _remove_notify(void *arg)
8228 : {
8229 19 : struct spdk_bdev_desc *desc = arg;
8230 :
8231 19 : _event_notify(desc, SPDK_BDEV_EVENT_REMOVE);
8232 19 : }
8233 :
8234 : /* returns: 0 - bdev removed and ready to be destructed.
8235 : * -EBUSY - bdev can't be destructed yet. */
8236 : static int
8237 141 : bdev_unregister_unsafe(struct spdk_bdev *bdev)
8238 : {
8239 : struct spdk_bdev_desc *desc, *tmp;
8240 141 : int rc = 0;
8241 : char uuid[SPDK_UUID_STRING_LEN];
8242 :
8243 141 : assert(spdk_spin_held(&g_bdev_mgr.spinlock));
8244 141 : assert(spdk_spin_held(&bdev->internal.spinlock));
8245 :
8246 : /* Notify each descriptor about hotremoval */
8247 160 : TAILQ_FOREACH_SAFE(desc, &bdev->internal.open_descs, link, tmp) {
8248 19 : rc = -EBUSY;
8249 : /*
8250 : * Defer invocation of the event_cb to a separate message that will
8251 : * run later on its thread. This ensures this context unwinds and
8252 : * we don't recursively unregister this bdev again if the event_cb
8253 : * immediately closes its descriptor.
8254 : */
8255 19 : event_notify(desc, _remove_notify);
8256 19 : }
8257 :
8258 : /* If there are no descriptors, proceed removing the bdev */
8259 141 : if (rc == 0) {
8260 126 : TAILQ_REMOVE(&g_bdev_mgr.bdevs, bdev, internal.link);
8261 126 : SPDK_DEBUGLOG(bdev, "Removing bdev %s from list done\n", bdev->name);
8262 :
8263 : /* Delete the name and the UUID alias */
8264 126 : spdk_uuid_fmt_lower(uuid, sizeof(uuid), &bdev->uuid);
8265 126 : bdev_name_del_unsafe(&bdev->internal.bdev_name);
8266 126 : bdev_alias_del(bdev, uuid, bdev_name_del_unsafe);
8267 :
8268 126 : spdk_notify_send("bdev_unregister", spdk_bdev_get_name(bdev));
8269 :
8270 126 : if (bdev->internal.reset_in_progress != NULL) {
8271 : /* If reset is in progress, let the completion callback for reset
8272 : * unregister the bdev.
8273 : */
8274 1 : rc = -EBUSY;
8275 1 : }
8276 126 : }
8277 :
8278 141 : return rc;
8279 : }
8280 :
8281 : static void
8282 4 : bdev_unregister_abort_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
8283 : struct spdk_io_channel *io_ch, void *_ctx)
8284 : {
8285 4 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
8286 :
8287 4 : bdev_channel_abort_queued_ios(bdev_ch);
8288 4 : spdk_bdev_for_each_channel_continue(i, 0);
8289 4 : }
8290 :
8291 : static void
8292 126 : bdev_unregister(struct spdk_bdev *bdev, void *_ctx, int status)
8293 : {
8294 : int rc;
8295 :
8296 126 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8297 126 : spdk_spin_lock(&bdev->internal.spinlock);
8298 : /*
8299 : * Set the status to REMOVING after completing to abort channels. Otherwise,
8300 : * the last spdk_bdev_close() may call spdk_io_device_unregister() while
8301 : * spdk_bdev_for_each_channel() is executed and spdk_io_device_unregister()
8302 : * may fail.
8303 : */
8304 126 : bdev->internal.status = SPDK_BDEV_STATUS_REMOVING;
8305 126 : rc = bdev_unregister_unsafe(bdev);
8306 126 : spdk_spin_unlock(&bdev->internal.spinlock);
8307 126 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8308 :
8309 126 : if (rc == 0) {
8310 110 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
8311 110 : }
8312 126 : }
8313 :
8314 : void
8315 133 : spdk_bdev_unregister(struct spdk_bdev *bdev, spdk_bdev_unregister_cb cb_fn, void *cb_arg)
8316 : {
8317 : struct spdk_thread *thread;
8318 :
8319 133 : SPDK_DEBUGLOG(bdev, "Removing bdev %s from list\n", bdev->name);
8320 :
8321 133 : thread = spdk_get_thread();
8322 133 : if (!thread) {
8323 : /* The user called this from a non-SPDK thread. */
8324 0 : if (cb_fn != NULL) {
8325 0 : cb_fn(cb_arg, -ENOTSUP);
8326 0 : }
8327 0 : return;
8328 : }
8329 :
8330 133 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8331 133 : if (bdev->internal.status == SPDK_BDEV_STATUS_UNREGISTERING ||
8332 133 : bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
8333 7 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8334 7 : if (cb_fn) {
8335 0 : cb_fn(cb_arg, -EBUSY);
8336 0 : }
8337 7 : return;
8338 : }
8339 :
8340 126 : spdk_spin_lock(&bdev->internal.spinlock);
8341 126 : bdev->internal.status = SPDK_BDEV_STATUS_UNREGISTERING;
8342 126 : bdev->internal.unregister_cb = cb_fn;
8343 126 : bdev->internal.unregister_ctx = cb_arg;
8344 126 : bdev->internal.unregister_td = thread;
8345 126 : spdk_spin_unlock(&bdev->internal.spinlock);
8346 126 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8347 :
8348 126 : spdk_bdev_set_qd_sampling_period(bdev, 0);
8349 :
8350 126 : spdk_bdev_for_each_channel(bdev, bdev_unregister_abort_channel, bdev,
8351 : bdev_unregister);
8352 133 : }
8353 :
8354 : int
8355 4 : spdk_bdev_unregister_by_name(const char *bdev_name, struct spdk_bdev_module *module,
8356 : spdk_bdev_unregister_cb cb_fn, void *cb_arg)
8357 : {
8358 : struct spdk_bdev_desc *desc;
8359 : struct spdk_bdev *bdev;
8360 : int rc;
8361 :
8362 4 : rc = spdk_bdev_open_ext(bdev_name, false, _tmp_bdev_event_cb, NULL, &desc);
8363 4 : if (rc != 0) {
8364 1 : SPDK_ERRLOG("Failed to open bdev with name: %s\n", bdev_name);
8365 1 : return rc;
8366 : }
8367 :
8368 3 : bdev = spdk_bdev_desc_get_bdev(desc);
8369 :
8370 3 : if (bdev->module != module) {
8371 1 : spdk_bdev_close(desc);
8372 1 : SPDK_ERRLOG("Bdev %s was not registered by the specified module.\n",
8373 : bdev_name);
8374 1 : return -ENODEV;
8375 : }
8376 :
8377 2 : spdk_bdev_unregister(bdev, cb_fn, cb_arg);
8378 :
8379 2 : spdk_bdev_close(desc);
8380 :
8381 2 : return 0;
8382 4 : }
8383 :
8384 : static int
8385 259 : bdev_start_qos(struct spdk_bdev *bdev)
8386 : {
8387 : struct set_qos_limit_ctx *ctx;
8388 :
8389 : /* Enable QoS */
8390 259 : if (bdev->internal.qos && bdev->internal.qos->thread == NULL) {
8391 2 : ctx = calloc(1, sizeof(*ctx));
8392 2 : if (ctx == NULL) {
8393 0 : SPDK_ERRLOG("Failed to allocate memory for QoS context\n");
8394 0 : return -ENOMEM;
8395 : }
8396 2 : ctx->bdev = bdev;
8397 2 : spdk_bdev_for_each_channel(bdev, bdev_enable_qos_msg, ctx, bdev_enable_qos_done);
8398 2 : }
8399 :
8400 259 : return 0;
8401 259 : }
8402 :
8403 : static void
8404 24 : log_already_claimed(enum spdk_log_level level, const int line, const char *func, const char *detail,
8405 : struct spdk_bdev *bdev)
8406 : {
8407 : enum spdk_bdev_claim_type type;
8408 : const char *typename, *modname;
8409 : extern struct spdk_log_flag SPDK_LOG_bdev;
8410 :
8411 24 : assert(spdk_spin_held(&bdev->internal.spinlock));
8412 :
8413 24 : if (level >= SPDK_LOG_INFO && !SPDK_LOG_bdev.enabled) {
8414 0 : return;
8415 : }
8416 :
8417 24 : type = bdev->internal.claim_type;
8418 24 : typename = spdk_bdev_claim_get_name(type);
8419 :
8420 24 : if (type == SPDK_BDEV_CLAIM_EXCL_WRITE) {
8421 6 : modname = bdev->internal.claim.v1.module->name;
8422 12 : spdk_log(level, __FILE__, line, func, "bdev %s %s: type %s by module %s\n",
8423 6 : bdev->name, detail, typename, modname);
8424 6 : return;
8425 : }
8426 :
8427 18 : if (claim_type_is_v2(type)) {
8428 : struct spdk_bdev_module_claim *claim;
8429 :
8430 36 : TAILQ_FOREACH(claim, &bdev->internal.claim.v2.claims, link) {
8431 18 : modname = claim->module->name;
8432 36 : spdk_log(level, __FILE__, line, func, "bdev %s %s: type %s by module %s\n",
8433 18 : bdev->name, detail, typename, modname);
8434 18 : }
8435 18 : return;
8436 : }
8437 :
8438 0 : assert(false);
8439 24 : }
8440 :
8441 : static int
8442 268 : bdev_open(struct spdk_bdev *bdev, bool write, struct spdk_bdev_desc *desc)
8443 : {
8444 : struct spdk_thread *thread;
8445 268 : int rc = 0;
8446 :
8447 268 : thread = spdk_get_thread();
8448 268 : if (!thread) {
8449 0 : SPDK_ERRLOG("Cannot open bdev from non-SPDK thread.\n");
8450 0 : return -ENOTSUP;
8451 : }
8452 :
8453 268 : SPDK_DEBUGLOG(bdev, "Opening descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
8454 : spdk_get_thread());
8455 :
8456 268 : desc->bdev = bdev;
8457 268 : desc->thread = thread;
8458 268 : desc->write = write;
8459 :
8460 268 : spdk_spin_lock(&bdev->internal.spinlock);
8461 268 : if (bdev->internal.status == SPDK_BDEV_STATUS_UNREGISTERING ||
8462 268 : bdev->internal.status == SPDK_BDEV_STATUS_REMOVING) {
8463 3 : spdk_spin_unlock(&bdev->internal.spinlock);
8464 3 : return -ENODEV;
8465 : }
8466 :
8467 265 : if (write && bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
8468 6 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
8469 6 : spdk_spin_unlock(&bdev->internal.spinlock);
8470 6 : return -EPERM;
8471 : }
8472 :
8473 259 : rc = bdev_start_qos(bdev);
8474 259 : if (rc != 0) {
8475 0 : SPDK_ERRLOG("Failed to start QoS on bdev %s\n", bdev->name);
8476 0 : spdk_spin_unlock(&bdev->internal.spinlock);
8477 0 : return rc;
8478 : }
8479 :
8480 259 : TAILQ_INSERT_TAIL(&bdev->internal.open_descs, desc, link);
8481 :
8482 259 : spdk_spin_unlock(&bdev->internal.spinlock);
8483 :
8484 259 : return 0;
8485 268 : }
8486 :
8487 : static void
8488 268 : bdev_open_opts_get_defaults(struct spdk_bdev_open_opts *opts, size_t opts_size)
8489 : {
8490 268 : if (!opts) {
8491 0 : SPDK_ERRLOG("opts should not be NULL.\n");
8492 0 : return;
8493 : }
8494 :
8495 268 : if (!opts_size) {
8496 0 : SPDK_ERRLOG("opts_size should not be zero.\n");
8497 0 : return;
8498 : }
8499 :
8500 268 : memset(opts, 0, opts_size);
8501 268 : opts->size = opts_size;
8502 :
8503 : #define FIELD_OK(field) \
8504 : offsetof(struct spdk_bdev_open_opts, field) + sizeof(opts->field) <= opts_size
8505 :
8506 : #define SET_FIELD(field, value) \
8507 : if (FIELD_OK(field)) { \
8508 : opts->field = value; \
8509 : } \
8510 :
8511 268 : SET_FIELD(no_metadata, false);
8512 :
8513 : #undef FIELD_OK
8514 : #undef SET_FIELD
8515 268 : }
8516 :
8517 : static void
8518 1 : bdev_open_opts_copy(struct spdk_bdev_open_opts *opts,
8519 : const struct spdk_bdev_open_opts *user_opts)
8520 : {
8521 : #define FIELD_OK(field) \
8522 : offsetof(struct spdk_bdev_open_opts, field) + sizeof(opts->field) <= user_opts->size
8523 :
8524 : #define SET_FIELD(field) \
8525 : if (FIELD_OK(field)) { \
8526 : opts->field = user_opts->field; \
8527 : } \
8528 :
8529 1 : SET_FIELD(no_metadata);
8530 :
8531 1 : opts->size = user_opts->size;
8532 :
8533 : /* We should not remove this statement, but need to update the assert statement
8534 : * if we add a new field, and also add a corresponding SET_FIELD statement.
8535 : */
8536 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_open_opts) == 16, "Incorrect size");
8537 :
8538 : #undef FIELD_OK
8539 : #undef SET_FIELD
8540 1 : }
8541 :
8542 : static int
8543 268 : bdev_desc_alloc(struct spdk_bdev *bdev, spdk_bdev_event_cb_t event_cb, void *event_ctx,
8544 : struct spdk_bdev_open_opts *user_opts, struct spdk_bdev_desc **_desc)
8545 : {
8546 : struct spdk_bdev_desc *desc;
8547 : struct spdk_bdev_open_opts opts;
8548 : unsigned int i;
8549 :
8550 268 : bdev_open_opts_get_defaults(&opts, sizeof(opts));
8551 268 : if (user_opts != NULL) {
8552 1 : bdev_open_opts_copy(&opts, user_opts);
8553 1 : }
8554 :
8555 268 : desc = calloc(1, sizeof(*desc));
8556 268 : if (desc == NULL) {
8557 0 : SPDK_ERRLOG("Failed to allocate memory for bdev descriptor\n");
8558 0 : return -ENOMEM;
8559 : }
8560 :
8561 268 : desc->opts = opts;
8562 :
8563 268 : TAILQ_INIT(&desc->pending_media_events);
8564 268 : TAILQ_INIT(&desc->free_media_events);
8565 :
8566 268 : desc->memory_domains_supported = spdk_bdev_get_memory_domains(bdev, NULL, 0) > 0;
8567 268 : desc->callback.event_fn = event_cb;
8568 268 : desc->callback.ctx = event_ctx;
8569 268 : spdk_spin_init(&desc->spinlock);
8570 :
8571 268 : if (desc->opts.no_metadata) {
8572 1 : if (spdk_bdev_is_md_separate(bdev)) {
8573 0 : SPDK_ERRLOG("no_metadata option is not supported with separate metadata.\n");
8574 0 : bdev_desc_free(desc);
8575 0 : return -EINVAL;
8576 : }
8577 1 : }
8578 :
8579 268 : if (bdev->media_events) {
8580 0 : desc->media_events_buffer = calloc(MEDIA_EVENT_POOL_SIZE,
8581 : sizeof(*desc->media_events_buffer));
8582 0 : if (desc->media_events_buffer == NULL) {
8583 0 : SPDK_ERRLOG("Failed to initialize media event pool\n");
8584 0 : bdev_desc_free(desc);
8585 0 : return -ENOMEM;
8586 : }
8587 :
8588 0 : for (i = 0; i < MEDIA_EVENT_POOL_SIZE; ++i) {
8589 0 : TAILQ_INSERT_TAIL(&desc->free_media_events,
8590 : &desc->media_events_buffer[i], tailq);
8591 0 : }
8592 0 : }
8593 :
8594 268 : if (bdev->fn_table->accel_sequence_supported != NULL) {
8595 0 : for (i = 0; i < SPDK_BDEV_NUM_IO_TYPES; ++i) {
8596 0 : desc->accel_sequence_supported[i] =
8597 0 : bdev->fn_table->accel_sequence_supported(bdev->ctxt,
8598 0 : (enum spdk_bdev_io_type)i);
8599 0 : }
8600 0 : }
8601 :
8602 268 : *_desc = desc;
8603 :
8604 268 : return 0;
8605 268 : }
8606 :
8607 : static int
8608 130 : bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8609 : void *event_ctx, struct spdk_bdev_open_opts *opts,
8610 : struct spdk_bdev_desc **_desc)
8611 : {
8612 : struct spdk_bdev_desc *desc;
8613 : struct spdk_bdev *bdev;
8614 : int rc;
8615 :
8616 130 : bdev = bdev_get_by_name(bdev_name);
8617 :
8618 130 : if (bdev == NULL) {
8619 1 : SPDK_NOTICELOG("Currently unable to find bdev with name: %s\n", bdev_name);
8620 1 : return -ENODEV;
8621 : }
8622 :
8623 129 : rc = bdev_desc_alloc(bdev, event_cb, event_ctx, opts, &desc);
8624 129 : if (rc != 0) {
8625 0 : return rc;
8626 : }
8627 :
8628 129 : rc = bdev_open(bdev, write, desc);
8629 129 : if (rc != 0) {
8630 7 : bdev_desc_free(desc);
8631 7 : desc = NULL;
8632 7 : }
8633 :
8634 129 : *_desc = desc;
8635 :
8636 129 : return rc;
8637 130 : }
8638 :
8639 : int
8640 132 : spdk_bdev_open_ext_v2(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8641 : void *event_ctx, struct spdk_bdev_open_opts *opts,
8642 : struct spdk_bdev_desc **_desc)
8643 : {
8644 : int rc;
8645 :
8646 132 : if (event_cb == NULL) {
8647 2 : SPDK_ERRLOG("Missing event callback function\n");
8648 2 : return -EINVAL;
8649 : }
8650 :
8651 130 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8652 130 : rc = bdev_open_ext(bdev_name, write, event_cb, event_ctx, opts, _desc);
8653 130 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8654 :
8655 130 : return rc;
8656 132 : }
8657 :
8658 : int
8659 130 : spdk_bdev_open_ext(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8660 : void *event_ctx, struct spdk_bdev_desc **_desc)
8661 : {
8662 130 : return spdk_bdev_open_ext_v2(bdev_name, write, event_cb, event_ctx, NULL, _desc);
8663 : }
8664 :
8665 : struct spdk_bdev_open_async_ctx {
8666 : char *bdev_name;
8667 : spdk_bdev_event_cb_t event_cb;
8668 : void *event_ctx;
8669 : bool write;
8670 : int rc;
8671 : spdk_bdev_open_async_cb_t cb_fn;
8672 : void *cb_arg;
8673 : struct spdk_bdev_desc *desc;
8674 : struct spdk_bdev_open_async_opts opts;
8675 : uint64_t start_ticks;
8676 : struct spdk_thread *orig_thread;
8677 : struct spdk_poller *poller;
8678 : TAILQ_ENTRY(spdk_bdev_open_async_ctx) tailq;
8679 : };
8680 :
8681 : static void
8682 0 : bdev_open_async_done(void *arg)
8683 : {
8684 0 : struct spdk_bdev_open_async_ctx *ctx = arg;
8685 :
8686 0 : ctx->cb_fn(ctx->desc, ctx->rc, ctx->cb_arg);
8687 :
8688 0 : free(ctx->bdev_name);
8689 0 : free(ctx);
8690 0 : }
8691 :
8692 : static void
8693 0 : bdev_open_async_cancel(void *arg)
8694 : {
8695 0 : struct spdk_bdev_open_async_ctx *ctx = arg;
8696 :
8697 0 : assert(ctx->rc == -ESHUTDOWN);
8698 :
8699 0 : spdk_poller_unregister(&ctx->poller);
8700 :
8701 0 : bdev_open_async_done(ctx);
8702 0 : }
8703 :
8704 : /* This is called when the bdev library finishes at shutdown. */
8705 : static void
8706 65 : bdev_open_async_fini(void)
8707 : {
8708 : struct spdk_bdev_open_async_ctx *ctx, *tmp_ctx;
8709 :
8710 65 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8711 65 : TAILQ_FOREACH_SAFE(ctx, &g_bdev_mgr.async_bdev_opens, tailq, tmp_ctx) {
8712 0 : TAILQ_REMOVE(&g_bdev_mgr.async_bdev_opens, ctx, tailq);
8713 : /*
8714 : * We have to move to ctx->orig_thread to unregister ctx->poller.
8715 : * However, there is a chance that ctx->poller is executed before
8716 : * message is executed, which could result in bdev_open_async_done()
8717 : * being called twice. To avoid such race condition, set ctx->rc to
8718 : * -ESHUTDOWN.
8719 : */
8720 0 : ctx->rc = -ESHUTDOWN;
8721 0 : spdk_thread_send_msg(ctx->orig_thread, bdev_open_async_cancel, ctx);
8722 0 : }
8723 65 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8724 65 : }
8725 :
8726 : static int bdev_open_async(void *arg);
8727 :
8728 : static void
8729 0 : _bdev_open_async(struct spdk_bdev_open_async_ctx *ctx)
8730 : {
8731 : uint64_t timeout_ticks;
8732 :
8733 0 : if (ctx->rc == -ESHUTDOWN) {
8734 : /* This context is being canceled. Do nothing. */
8735 0 : return;
8736 : }
8737 :
8738 0 : ctx->rc = bdev_open_ext(ctx->bdev_name, ctx->write, ctx->event_cb, ctx->event_ctx,
8739 0 : NULL, &ctx->desc);
8740 0 : if (ctx->rc == 0 || ctx->opts.timeout_ms == 0) {
8741 0 : goto exit;
8742 : }
8743 :
8744 0 : timeout_ticks = ctx->start_ticks + ctx->opts.timeout_ms * spdk_get_ticks_hz() / 1000ull;
8745 0 : if (spdk_get_ticks() >= timeout_ticks) {
8746 0 : SPDK_ERRLOG("Timed out while waiting for bdev '%s' to appear\n", ctx->bdev_name);
8747 0 : ctx->rc = -ETIMEDOUT;
8748 0 : goto exit;
8749 : }
8750 :
8751 0 : return;
8752 :
8753 : exit:
8754 0 : spdk_poller_unregister(&ctx->poller);
8755 0 : TAILQ_REMOVE(&g_bdev_mgr.async_bdev_opens, ctx, tailq);
8756 :
8757 : /* Completion callback is processed after stack unwinding. */
8758 0 : spdk_thread_send_msg(ctx->orig_thread, bdev_open_async_done, ctx);
8759 0 : }
8760 :
8761 : static int
8762 0 : bdev_open_async(void *arg)
8763 : {
8764 0 : struct spdk_bdev_open_async_ctx *ctx = arg;
8765 :
8766 0 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8767 :
8768 0 : _bdev_open_async(ctx);
8769 :
8770 0 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8771 :
8772 0 : return SPDK_POLLER_BUSY;
8773 : }
8774 :
8775 : static void
8776 0 : bdev_open_async_opts_copy(struct spdk_bdev_open_async_opts *opts,
8777 : struct spdk_bdev_open_async_opts *opts_src,
8778 : size_t size)
8779 : {
8780 0 : assert(opts);
8781 0 : assert(opts_src);
8782 :
8783 0 : opts->size = size;
8784 :
8785 : #define SET_FIELD(field) \
8786 : if (offsetof(struct spdk_bdev_open_async_opts, field) + sizeof(opts->field) <= size) { \
8787 : opts->field = opts_src->field; \
8788 : } \
8789 :
8790 0 : SET_FIELD(timeout_ms);
8791 :
8792 : /* Do not remove this statement, you should always update this statement when you adding a new field,
8793 : * and do not forget to add the SET_FIELD statement for your added field. */
8794 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_open_async_opts) == 16, "Incorrect size");
8795 :
8796 : #undef SET_FIELD
8797 0 : }
8798 :
8799 : static void
8800 0 : bdev_open_async_opts_get_default(struct spdk_bdev_open_async_opts *opts, size_t size)
8801 : {
8802 0 : assert(opts);
8803 :
8804 0 : opts->size = size;
8805 :
8806 : #define SET_FIELD(field, value) \
8807 : if (offsetof(struct spdk_bdev_open_async_opts, field) + sizeof(opts->field) <= size) { \
8808 : opts->field = value; \
8809 : } \
8810 :
8811 0 : SET_FIELD(timeout_ms, 0);
8812 :
8813 : #undef SET_FIELD
8814 0 : }
8815 :
8816 : int
8817 0 : spdk_bdev_open_async(const char *bdev_name, bool write, spdk_bdev_event_cb_t event_cb,
8818 : void *event_ctx, struct spdk_bdev_open_async_opts *opts,
8819 : spdk_bdev_open_async_cb_t open_cb, void *open_cb_arg)
8820 : {
8821 : struct spdk_bdev_open_async_ctx *ctx;
8822 :
8823 0 : if (event_cb == NULL) {
8824 0 : SPDK_ERRLOG("Missing event callback function\n");
8825 0 : return -EINVAL;
8826 : }
8827 :
8828 0 : if (open_cb == NULL) {
8829 0 : SPDK_ERRLOG("Missing open callback function\n");
8830 0 : return -EINVAL;
8831 : }
8832 :
8833 0 : if (opts != NULL && opts->size == 0) {
8834 0 : SPDK_ERRLOG("size in the options structure should not be zero\n");
8835 0 : return -EINVAL;
8836 : }
8837 :
8838 0 : ctx = calloc(1, sizeof(*ctx));
8839 0 : if (ctx == NULL) {
8840 0 : SPDK_ERRLOG("Failed to allocate open context\n");
8841 0 : return -ENOMEM;
8842 : }
8843 :
8844 0 : ctx->bdev_name = strdup(bdev_name);
8845 0 : if (ctx->bdev_name == NULL) {
8846 0 : SPDK_ERRLOG("Failed to duplicate bdev_name\n");
8847 0 : free(ctx);
8848 0 : return -ENOMEM;
8849 : }
8850 :
8851 0 : ctx->poller = SPDK_POLLER_REGISTER(bdev_open_async, ctx, 100 * 1000);
8852 0 : if (ctx->poller == NULL) {
8853 0 : SPDK_ERRLOG("Failed to register bdev_open_async poller\n");
8854 0 : free(ctx->bdev_name);
8855 0 : free(ctx);
8856 0 : return -ENOMEM;
8857 : }
8858 :
8859 0 : ctx->cb_fn = open_cb;
8860 0 : ctx->cb_arg = open_cb_arg;
8861 0 : ctx->write = write;
8862 0 : ctx->event_cb = event_cb;
8863 0 : ctx->event_ctx = event_ctx;
8864 0 : ctx->orig_thread = spdk_get_thread();
8865 0 : ctx->start_ticks = spdk_get_ticks();
8866 :
8867 0 : bdev_open_async_opts_get_default(&ctx->opts, sizeof(ctx->opts));
8868 0 : if (opts != NULL) {
8869 0 : bdev_open_async_opts_copy(&ctx->opts, opts, opts->size);
8870 0 : }
8871 :
8872 0 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8873 :
8874 0 : TAILQ_INSERT_TAIL(&g_bdev_mgr.async_bdev_opens, ctx, tailq);
8875 0 : _bdev_open_async(ctx);
8876 :
8877 0 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8878 :
8879 0 : return 0;
8880 0 : }
8881 :
8882 : static void
8883 259 : bdev_close(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc)
8884 : {
8885 : int rc;
8886 :
8887 259 : spdk_spin_lock(&bdev->internal.spinlock);
8888 259 : spdk_spin_lock(&desc->spinlock);
8889 :
8890 259 : TAILQ_REMOVE(&bdev->internal.open_descs, desc, link);
8891 :
8892 259 : desc->closed = true;
8893 :
8894 259 : if (desc->claim != NULL) {
8895 16 : bdev_desc_release_claims(desc);
8896 16 : }
8897 :
8898 259 : if (0 == desc->refs) {
8899 248 : spdk_spin_unlock(&desc->spinlock);
8900 248 : bdev_desc_free(desc);
8901 248 : } else {
8902 11 : spdk_spin_unlock(&desc->spinlock);
8903 : }
8904 :
8905 : /* If no more descriptors, kill QoS channel */
8906 259 : if (bdev->internal.qos && TAILQ_EMPTY(&bdev->internal.open_descs)) {
8907 7 : SPDK_DEBUGLOG(bdev, "Closed last descriptor for bdev %s on thread %p. Stopping QoS.\n",
8908 : bdev->name, spdk_get_thread());
8909 :
8910 7 : if (bdev_qos_destroy(bdev)) {
8911 : /* There isn't anything we can do to recover here. Just let the
8912 : * old QoS poller keep running. The QoS handling won't change
8913 : * cores when the user allocates a new channel, but it won't break. */
8914 0 : SPDK_ERRLOG("Unable to shut down QoS poller. It will continue running on the current thread.\n");
8915 0 : }
8916 7 : }
8917 :
8918 259 : if (bdev->internal.status == SPDK_BDEV_STATUS_REMOVING && TAILQ_EMPTY(&bdev->internal.open_descs)) {
8919 15 : rc = bdev_unregister_unsafe(bdev);
8920 15 : spdk_spin_unlock(&bdev->internal.spinlock);
8921 :
8922 15 : if (rc == 0) {
8923 15 : spdk_io_device_unregister(__bdev_to_io_dev(bdev), bdev_destroy_cb);
8924 15 : }
8925 15 : } else {
8926 244 : spdk_spin_unlock(&bdev->internal.spinlock);
8927 : }
8928 259 : }
8929 :
8930 : void
8931 122 : spdk_bdev_close(struct spdk_bdev_desc *desc)
8932 : {
8933 122 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
8934 :
8935 122 : SPDK_DEBUGLOG(bdev, "Closing descriptor %p for bdev %s on thread %p\n", desc, bdev->name,
8936 : spdk_get_thread());
8937 :
8938 122 : assert(desc->thread == spdk_get_thread());
8939 :
8940 122 : spdk_poller_unregister(&desc->io_timeout_poller);
8941 :
8942 122 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8943 :
8944 122 : bdev_close(bdev, desc);
8945 :
8946 122 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8947 122 : }
8948 :
8949 : int32_t
8950 3 : spdk_bdev_get_numa_id(struct spdk_bdev *bdev)
8951 : {
8952 3 : if (bdev->numa.id_valid) {
8953 2 : return bdev->numa.id;
8954 : } else {
8955 1 : return SPDK_ENV_NUMA_ID_ANY;
8956 : }
8957 3 : }
8958 :
8959 : static void
8960 126 : bdev_register_finished(void *arg)
8961 : {
8962 126 : struct spdk_bdev_desc *desc = arg;
8963 126 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
8964 :
8965 126 : spdk_notify_send("bdev_register", spdk_bdev_get_name(bdev));
8966 :
8967 126 : spdk_spin_lock(&g_bdev_mgr.spinlock);
8968 :
8969 126 : bdev_close(bdev, desc);
8970 :
8971 126 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
8972 126 : }
8973 :
8974 : int
8975 129 : spdk_bdev_register(struct spdk_bdev *bdev)
8976 : {
8977 : struct spdk_bdev_desc *desc;
8978 129 : struct spdk_thread *thread = spdk_get_thread();
8979 : int rc;
8980 :
8981 129 : if (spdk_unlikely(!spdk_thread_is_app_thread(NULL))) {
8982 1 : SPDK_ERRLOG("Cannot register bdev %s on thread %p (%s)\n", bdev->name, thread,
8983 : thread ? spdk_thread_get_name(thread) : "null");
8984 1 : return -EINVAL;
8985 : }
8986 :
8987 128 : rc = bdev_register(bdev);
8988 128 : if (rc != 0) {
8989 2 : return rc;
8990 : }
8991 :
8992 : /* A descriptor is opened to prevent bdev deletion during examination */
8993 126 : rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, NULL, &desc);
8994 126 : if (rc != 0) {
8995 0 : spdk_bdev_unregister(bdev, NULL, NULL);
8996 0 : return rc;
8997 : }
8998 :
8999 126 : rc = bdev_open(bdev, false, desc);
9000 126 : if (rc != 0) {
9001 0 : bdev_desc_free(desc);
9002 0 : spdk_bdev_unregister(bdev, NULL, NULL);
9003 0 : return rc;
9004 : }
9005 :
9006 : /* Examine configuration before initializing I/O */
9007 126 : bdev_examine(bdev);
9008 :
9009 126 : rc = spdk_bdev_wait_for_examine(bdev_register_finished, desc);
9010 126 : if (rc != 0) {
9011 0 : bdev_close(bdev, desc);
9012 0 : spdk_bdev_unregister(bdev, NULL, NULL);
9013 0 : }
9014 :
9015 126 : return rc;
9016 129 : }
9017 :
9018 : int
9019 26 : spdk_bdev_module_claim_bdev(struct spdk_bdev *bdev, struct spdk_bdev_desc *desc,
9020 : struct spdk_bdev_module *module)
9021 : {
9022 26 : spdk_spin_lock(&bdev->internal.spinlock);
9023 :
9024 26 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
9025 6 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9026 6 : spdk_spin_unlock(&bdev->internal.spinlock);
9027 6 : return -EPERM;
9028 : }
9029 :
9030 20 : if (desc && !desc->write) {
9031 5 : desc->write = true;
9032 5 : }
9033 :
9034 20 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_EXCL_WRITE;
9035 20 : bdev->internal.claim.v1.module = module;
9036 :
9037 20 : spdk_spin_unlock(&bdev->internal.spinlock);
9038 20 : return 0;
9039 26 : }
9040 :
9041 : void
9042 8 : spdk_bdev_module_release_bdev(struct spdk_bdev *bdev)
9043 : {
9044 8 : spdk_spin_lock(&bdev->internal.spinlock);
9045 :
9046 8 : assert(bdev->internal.claim.v1.module != NULL);
9047 8 : assert(bdev->internal.claim_type == SPDK_BDEV_CLAIM_EXCL_WRITE);
9048 8 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
9049 8 : bdev->internal.claim.v1.module = NULL;
9050 :
9051 8 : spdk_spin_unlock(&bdev->internal.spinlock);
9052 8 : }
9053 :
9054 : /*
9055 : * Start claims v2
9056 : */
9057 :
9058 : const char *
9059 24 : spdk_bdev_claim_get_name(enum spdk_bdev_claim_type type)
9060 : {
9061 24 : switch (type) {
9062 : case SPDK_BDEV_CLAIM_NONE:
9063 0 : return "not_claimed";
9064 : case SPDK_BDEV_CLAIM_EXCL_WRITE:
9065 6 : return "exclusive_write";
9066 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
9067 7 : return "read_many_write_one";
9068 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE:
9069 5 : return "read_many_write_none";
9070 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9071 6 : return "read_many_write_many";
9072 : default:
9073 0 : break;
9074 : }
9075 0 : return "invalid_claim";
9076 24 : }
9077 :
9078 : static bool
9079 96 : claim_type_is_v2(enum spdk_bdev_claim_type type)
9080 : {
9081 96 : switch (type) {
9082 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
9083 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE:
9084 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9085 96 : return true;
9086 : default:
9087 0 : break;
9088 : }
9089 0 : return false;
9090 96 : }
9091 :
9092 : /* Returns true if taking a claim with desc->write == false should make the descriptor writable. */
9093 : static bool
9094 13 : claim_type_promotes_to_write(enum spdk_bdev_claim_type type)
9095 : {
9096 13 : switch (type) {
9097 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
9098 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9099 5 : return true;
9100 : default:
9101 8 : break;
9102 : }
9103 8 : return false;
9104 13 : }
9105 :
9106 : void
9107 44 : spdk_bdev_claim_opts_init(struct spdk_bdev_claim_opts *opts, size_t size)
9108 : {
9109 44 : if (opts == NULL) {
9110 0 : SPDK_ERRLOG("opts should not be NULL\n");
9111 0 : assert(opts != NULL);
9112 0 : return;
9113 : }
9114 44 : if (size == 0) {
9115 0 : SPDK_ERRLOG("size should not be zero\n");
9116 0 : assert(size != 0);
9117 0 : return;
9118 : }
9119 :
9120 44 : memset(opts, 0, size);
9121 44 : opts->opts_size = size;
9122 :
9123 : #define FIELD_OK(field) \
9124 : offsetof(struct spdk_bdev_claim_opts, field) + sizeof(opts->field) <= size
9125 :
9126 : #define SET_FIELD(field, value) \
9127 : if (FIELD_OK(field)) { \
9128 : opts->field = value; \
9129 : } \
9130 :
9131 44 : SET_FIELD(shared_claim_key, 0);
9132 :
9133 : #undef FIELD_OK
9134 : #undef SET_FIELD
9135 44 : }
9136 :
9137 : static int
9138 22 : claim_opts_copy(struct spdk_bdev_claim_opts *src, struct spdk_bdev_claim_opts *dst)
9139 : {
9140 22 : if (src->opts_size == 0) {
9141 0 : SPDK_ERRLOG("size should not be zero\n");
9142 0 : return -1;
9143 : }
9144 :
9145 22 : memset(dst, 0, sizeof(*dst));
9146 22 : dst->opts_size = src->opts_size;
9147 :
9148 : #define FIELD_OK(field) \
9149 : offsetof(struct spdk_bdev_claim_opts, field) + sizeof(src->field) <= src->opts_size
9150 :
9151 : #define SET_FIELD(field) \
9152 : if (FIELD_OK(field)) { \
9153 : dst->field = src->field; \
9154 : } \
9155 :
9156 22 : if (FIELD_OK(name)) {
9157 22 : snprintf(dst->name, sizeof(dst->name), "%s", src->name);
9158 22 : }
9159 :
9160 22 : SET_FIELD(shared_claim_key);
9161 :
9162 : /* You should not remove this statement, but need to update the assert statement
9163 : * if you add a new field, and also add a corresponding SET_FIELD statement */
9164 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_claim_opts) == 48, "Incorrect size");
9165 :
9166 : #undef FIELD_OK
9167 : #undef SET_FIELD
9168 22 : return 0;
9169 22 : }
9170 :
9171 : /* Returns 0 if a read-write-once claim can be taken. */
9172 : static int
9173 9 : claim_verify_rwo(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9174 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9175 : {
9176 9 : struct spdk_bdev *bdev = desc->bdev;
9177 : struct spdk_bdev_desc *open_desc;
9178 :
9179 9 : assert(spdk_spin_held(&bdev->internal.spinlock));
9180 9 : assert(type == SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE);
9181 :
9182 9 : if (opts->shared_claim_key != 0) {
9183 1 : SPDK_ERRLOG("%s: key option not supported with read-write-once claims\n",
9184 : bdev->name);
9185 1 : return -EINVAL;
9186 : }
9187 8 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE) {
9188 1 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9189 1 : return -EPERM;
9190 : }
9191 7 : if (desc->claim != NULL) {
9192 0 : SPDK_NOTICELOG("%s: descriptor already claimed bdev with module %s\n",
9193 : bdev->name, desc->claim->module->name);
9194 0 : return -EPERM;
9195 : }
9196 14 : TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
9197 9 : if (desc != open_desc && open_desc->write) {
9198 2 : SPDK_NOTICELOG("%s: Cannot obtain read-write-once claim while "
9199 : "another descriptor is open for writing\n",
9200 : bdev->name);
9201 2 : return -EPERM;
9202 : }
9203 7 : }
9204 :
9205 5 : return 0;
9206 9 : }
9207 :
9208 : /* Returns 0 if a read-only-many claim can be taken. */
9209 : static int
9210 12 : claim_verify_rom(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9211 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9212 : {
9213 12 : struct spdk_bdev *bdev = desc->bdev;
9214 : struct spdk_bdev_desc *open_desc;
9215 :
9216 12 : assert(spdk_spin_held(&bdev->internal.spinlock));
9217 12 : assert(type == SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE);
9218 12 : assert(desc->claim == NULL);
9219 :
9220 12 : if (desc->write) {
9221 3 : SPDK_ERRLOG("%s: Cannot obtain read-only-many claim with writable descriptor\n",
9222 : bdev->name);
9223 3 : return -EINVAL;
9224 : }
9225 9 : if (opts->shared_claim_key != 0) {
9226 1 : SPDK_ERRLOG("%s: key option not supported with read-only-may claims\n", bdev->name);
9227 1 : return -EINVAL;
9228 : }
9229 8 : if (bdev->internal.claim_type == SPDK_BDEV_CLAIM_NONE) {
9230 15 : TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
9231 9 : if (open_desc->write) {
9232 0 : SPDK_NOTICELOG("%s: Cannot obtain read-only-many claim while "
9233 : "another descriptor is open for writing\n",
9234 : bdev->name);
9235 0 : return -EPERM;
9236 : }
9237 9 : }
9238 6 : }
9239 :
9240 8 : return 0;
9241 12 : }
9242 :
9243 : /* Returns 0 if a read-write-many claim can be taken. */
9244 : static int
9245 8 : claim_verify_rwm(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9246 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9247 : {
9248 8 : struct spdk_bdev *bdev = desc->bdev;
9249 : struct spdk_bdev_desc *open_desc;
9250 :
9251 8 : assert(spdk_spin_held(&bdev->internal.spinlock));
9252 8 : assert(type == SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED);
9253 8 : assert(desc->claim == NULL);
9254 :
9255 8 : if (opts->shared_claim_key == 0) {
9256 2 : SPDK_ERRLOG("%s: shared_claim_key option required with read-write-may claims\n",
9257 : bdev->name);
9258 2 : return -EINVAL;
9259 : }
9260 6 : switch (bdev->internal.claim_type) {
9261 : case SPDK_BDEV_CLAIM_NONE:
9262 7 : TAILQ_FOREACH(open_desc, &bdev->internal.open_descs, link) {
9263 5 : if (open_desc == desc) {
9264 3 : continue;
9265 : }
9266 2 : if (open_desc->write) {
9267 2 : SPDK_NOTICELOG("%s: Cannot obtain read-write-many claim while "
9268 : "another descriptor is open for writing without a "
9269 : "claim\n", bdev->name);
9270 2 : return -EPERM;
9271 : }
9272 0 : }
9273 2 : break;
9274 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9275 2 : if (opts->shared_claim_key != bdev->internal.claim.v2.key) {
9276 1 : LOG_ALREADY_CLAIMED_ERROR("already claimed with another key", bdev);
9277 1 : return -EPERM;
9278 : }
9279 1 : break;
9280 : default:
9281 0 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9282 0 : return -EBUSY;
9283 : }
9284 :
9285 3 : return 0;
9286 8 : }
9287 :
9288 : /* Updates desc and its bdev with a v2 claim. */
9289 : static int
9290 16 : claim_bdev(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9291 : struct spdk_bdev_claim_opts *opts, struct spdk_bdev_module *module)
9292 : {
9293 16 : struct spdk_bdev *bdev = desc->bdev;
9294 : struct spdk_bdev_module_claim *claim;
9295 :
9296 16 : assert(spdk_spin_held(&bdev->internal.spinlock));
9297 16 : assert(claim_type_is_v2(type));
9298 16 : assert(desc->claim == NULL);
9299 :
9300 16 : claim = calloc(1, sizeof(*desc->claim));
9301 16 : if (claim == NULL) {
9302 0 : SPDK_ERRLOG("%s: out of memory while allocating claim\n", bdev->name);
9303 0 : return -ENOMEM;
9304 : }
9305 16 : claim->module = module;
9306 16 : claim->desc = desc;
9307 : SPDK_STATIC_ASSERT(sizeof(claim->name) == sizeof(opts->name), "sizes must match");
9308 16 : memcpy(claim->name, opts->name, sizeof(claim->name));
9309 16 : desc->claim = claim;
9310 :
9311 16 : if (bdev->internal.claim_type == SPDK_BDEV_CLAIM_NONE) {
9312 13 : bdev->internal.claim_type = type;
9313 13 : TAILQ_INIT(&bdev->internal.claim.v2.claims);
9314 13 : bdev->internal.claim.v2.key = opts->shared_claim_key;
9315 13 : }
9316 16 : assert(type == bdev->internal.claim_type);
9317 :
9318 16 : TAILQ_INSERT_TAIL(&bdev->internal.claim.v2.claims, claim, link);
9319 :
9320 16 : if (!desc->write && claim_type_promotes_to_write(type)) {
9321 5 : desc->write = true;
9322 5 : }
9323 :
9324 16 : return 0;
9325 16 : }
9326 :
9327 : int
9328 39 : spdk_bdev_module_claim_bdev_desc(struct spdk_bdev_desc *desc, enum spdk_bdev_claim_type type,
9329 : struct spdk_bdev_claim_opts *_opts,
9330 : struct spdk_bdev_module *module)
9331 : {
9332 : struct spdk_bdev *bdev;
9333 : struct spdk_bdev_claim_opts opts;
9334 39 : int rc = 0;
9335 :
9336 39 : if (desc == NULL) {
9337 0 : SPDK_ERRLOG("descriptor must not be NULL\n");
9338 0 : return -EINVAL;
9339 : }
9340 :
9341 39 : bdev = desc->bdev;
9342 :
9343 39 : if (_opts == NULL) {
9344 17 : spdk_bdev_claim_opts_init(&opts, sizeof(opts));
9345 39 : } else if (claim_opts_copy(_opts, &opts) != 0) {
9346 0 : return -EINVAL;
9347 : }
9348 :
9349 39 : spdk_spin_lock(&bdev->internal.spinlock);
9350 :
9351 39 : if (bdev->internal.claim_type != SPDK_BDEV_CLAIM_NONE &&
9352 15 : bdev->internal.claim_type != type) {
9353 10 : LOG_ALREADY_CLAIMED_ERROR("already claimed", bdev);
9354 10 : spdk_spin_unlock(&bdev->internal.spinlock);
9355 10 : return -EPERM;
9356 : }
9357 :
9358 29 : if (claim_type_is_v2(type) && desc->claim != NULL) {
9359 0 : SPDK_ERRLOG("%s: descriptor already has %s claim with name '%s'\n",
9360 : bdev->name, spdk_bdev_claim_get_name(type), desc->claim->name);
9361 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9362 0 : return -EPERM;
9363 : }
9364 :
9365 29 : switch (type) {
9366 : case SPDK_BDEV_CLAIM_EXCL_WRITE:
9367 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9368 0 : return spdk_bdev_module_claim_bdev(bdev, desc, module);
9369 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_ONE:
9370 9 : rc = claim_verify_rwo(desc, type, &opts, module);
9371 9 : break;
9372 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_NONE:
9373 12 : rc = claim_verify_rom(desc, type, &opts, module);
9374 12 : break;
9375 : case SPDK_BDEV_CLAIM_READ_MANY_WRITE_SHARED:
9376 8 : rc = claim_verify_rwm(desc, type, &opts, module);
9377 8 : break;
9378 : default:
9379 0 : SPDK_ERRLOG("%s: claim type %d not supported\n", bdev->name, type);
9380 0 : rc = -ENOTSUP;
9381 0 : }
9382 :
9383 29 : if (rc == 0) {
9384 16 : rc = claim_bdev(desc, type, &opts, module);
9385 16 : }
9386 :
9387 29 : spdk_spin_unlock(&bdev->internal.spinlock);
9388 29 : return rc;
9389 39 : }
9390 :
9391 : static void
9392 13 : claim_reset(struct spdk_bdev *bdev)
9393 : {
9394 13 : assert(spdk_spin_held(&bdev->internal.spinlock));
9395 13 : assert(claim_type_is_v2(bdev->internal.claim_type));
9396 13 : assert(TAILQ_EMPTY(&bdev->internal.claim.v2.claims));
9397 :
9398 13 : memset(&bdev->internal.claim, 0, sizeof(bdev->internal.claim));
9399 13 : bdev->internal.claim_type = SPDK_BDEV_CLAIM_NONE;
9400 13 : }
9401 :
9402 : static void
9403 16 : bdev_desc_release_claims(struct spdk_bdev_desc *desc)
9404 : {
9405 16 : struct spdk_bdev *bdev = desc->bdev;
9406 :
9407 16 : assert(spdk_spin_held(&bdev->internal.spinlock));
9408 16 : assert(claim_type_is_v2(bdev->internal.claim_type));
9409 :
9410 16 : if (bdev->internal.examine_in_progress == 0) {
9411 16 : TAILQ_REMOVE(&bdev->internal.claim.v2.claims, desc->claim, link);
9412 16 : free(desc->claim);
9413 16 : if (TAILQ_EMPTY(&bdev->internal.claim.v2.claims)) {
9414 13 : claim_reset(bdev);
9415 13 : }
9416 16 : } else {
9417 : /* This is a dead claim that will be cleaned up when bdev_examine() is done. */
9418 0 : desc->claim->module = NULL;
9419 0 : desc->claim->desc = NULL;
9420 : }
9421 16 : desc->claim = NULL;
9422 16 : }
9423 :
9424 : /*
9425 : * End claims v2
9426 : */
9427 :
9428 : struct spdk_bdev *
9429 1541 : spdk_bdev_desc_get_bdev(struct spdk_bdev_desc *desc)
9430 : {
9431 1541 : assert(desc != NULL);
9432 1541 : return desc->bdev;
9433 : }
9434 :
9435 : int
9436 1 : spdk_for_each_bdev(void *ctx, spdk_for_each_bdev_fn fn)
9437 : {
9438 : struct spdk_bdev *bdev, *tmp;
9439 : struct spdk_bdev_desc *desc;
9440 1 : int rc = 0;
9441 :
9442 1 : assert(fn != NULL);
9443 :
9444 1 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9445 1 : bdev = spdk_bdev_first();
9446 9 : while (bdev != NULL) {
9447 8 : rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, NULL, &desc);
9448 8 : if (rc != 0) {
9449 0 : break;
9450 : }
9451 8 : rc = bdev_open(bdev, false, desc);
9452 8 : if (rc != 0) {
9453 1 : bdev_desc_free(desc);
9454 1 : if (rc == -ENODEV) {
9455 : /* Ignore the error and move to the next bdev. */
9456 1 : rc = 0;
9457 1 : bdev = spdk_bdev_next(bdev);
9458 1 : continue;
9459 : }
9460 0 : break;
9461 : }
9462 7 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9463 :
9464 7 : rc = fn(ctx, bdev);
9465 :
9466 7 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9467 7 : tmp = spdk_bdev_next(bdev);
9468 7 : bdev_close(bdev, desc);
9469 7 : if (rc != 0) {
9470 0 : break;
9471 : }
9472 7 : bdev = tmp;
9473 : }
9474 1 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9475 :
9476 1 : return rc;
9477 : }
9478 :
9479 : int
9480 1 : spdk_for_each_bdev_leaf(void *ctx, spdk_for_each_bdev_fn fn)
9481 : {
9482 : struct spdk_bdev *bdev, *tmp;
9483 : struct spdk_bdev_desc *desc;
9484 1 : int rc = 0;
9485 :
9486 1 : assert(fn != NULL);
9487 :
9488 1 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9489 1 : bdev = spdk_bdev_first_leaf();
9490 6 : while (bdev != NULL) {
9491 5 : rc = bdev_desc_alloc(bdev, _tmp_bdev_event_cb, NULL, NULL, &desc);
9492 5 : if (rc != 0) {
9493 0 : break;
9494 : }
9495 5 : rc = bdev_open(bdev, false, desc);
9496 5 : if (rc != 0) {
9497 1 : bdev_desc_free(desc);
9498 1 : if (rc == -ENODEV) {
9499 : /* Ignore the error and move to the next bdev. */
9500 1 : rc = 0;
9501 1 : bdev = spdk_bdev_next_leaf(bdev);
9502 1 : continue;
9503 : }
9504 0 : break;
9505 : }
9506 4 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9507 :
9508 4 : rc = fn(ctx, bdev);
9509 :
9510 4 : spdk_spin_lock(&g_bdev_mgr.spinlock);
9511 4 : tmp = spdk_bdev_next_leaf(bdev);
9512 4 : bdev_close(bdev, desc);
9513 4 : if (rc != 0) {
9514 0 : break;
9515 : }
9516 4 : bdev = tmp;
9517 : }
9518 1 : spdk_spin_unlock(&g_bdev_mgr.spinlock);
9519 :
9520 1 : return rc;
9521 : }
9522 :
9523 : void
9524 0 : spdk_bdev_io_get_iovec(struct spdk_bdev_io *bdev_io, struct iovec **iovp, int *iovcntp)
9525 : {
9526 : struct iovec *iovs;
9527 : int iovcnt;
9528 :
9529 0 : if (bdev_io == NULL) {
9530 0 : return;
9531 : }
9532 :
9533 0 : switch (bdev_io->type) {
9534 : case SPDK_BDEV_IO_TYPE_READ:
9535 : case SPDK_BDEV_IO_TYPE_WRITE:
9536 : case SPDK_BDEV_IO_TYPE_ZCOPY:
9537 0 : iovs = bdev_io->u.bdev.iovs;
9538 0 : iovcnt = bdev_io->u.bdev.iovcnt;
9539 0 : break;
9540 : default:
9541 0 : iovs = NULL;
9542 0 : iovcnt = 0;
9543 0 : break;
9544 : }
9545 :
9546 0 : if (iovp) {
9547 0 : *iovp = iovs;
9548 0 : }
9549 0 : if (iovcntp) {
9550 0 : *iovcntp = iovcnt;
9551 0 : }
9552 0 : }
9553 :
9554 : void *
9555 0 : spdk_bdev_io_get_md_buf(struct spdk_bdev_io *bdev_io)
9556 : {
9557 0 : if (bdev_io == NULL) {
9558 0 : return NULL;
9559 : }
9560 :
9561 0 : if (!spdk_bdev_is_md_separate(bdev_io->bdev)) {
9562 0 : return NULL;
9563 : }
9564 :
9565 0 : if (bdev_io->type == SPDK_BDEV_IO_TYPE_READ ||
9566 0 : bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
9567 0 : return bdev_io->u.bdev.md_buf;
9568 : }
9569 :
9570 0 : return NULL;
9571 0 : }
9572 :
9573 : void *
9574 0 : spdk_bdev_io_get_cb_arg(struct spdk_bdev_io *bdev_io)
9575 : {
9576 0 : if (bdev_io == NULL) {
9577 0 : assert(false);
9578 : return NULL;
9579 : }
9580 :
9581 0 : return bdev_io->internal.caller_ctx;
9582 : }
9583 :
9584 : void
9585 7 : spdk_bdev_module_list_add(struct spdk_bdev_module *bdev_module)
9586 : {
9587 :
9588 7 : if (spdk_bdev_module_list_find(bdev_module->name)) {
9589 0 : SPDK_ERRLOG("ERROR: module '%s' already registered.\n", bdev_module->name);
9590 0 : assert(false);
9591 : }
9592 :
9593 7 : spdk_spin_init(&bdev_module->internal.spinlock);
9594 7 : TAILQ_INIT(&bdev_module->internal.quiesced_ranges);
9595 :
9596 : /*
9597 : * Modules with examine callbacks must be initialized first, so they are
9598 : * ready to handle examine callbacks from later modules that will
9599 : * register physical bdevs.
9600 : */
9601 7 : if (bdev_module->examine_config != NULL || bdev_module->examine_disk != NULL) {
9602 4 : TAILQ_INSERT_HEAD(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
9603 4 : } else {
9604 3 : TAILQ_INSERT_TAIL(&g_bdev_mgr.bdev_modules, bdev_module, internal.tailq);
9605 : }
9606 7 : }
9607 :
9608 : struct spdk_bdev_module *
9609 7 : spdk_bdev_module_list_find(const char *name)
9610 : {
9611 : struct spdk_bdev_module *bdev_module;
9612 :
9613 14 : TAILQ_FOREACH(bdev_module, &g_bdev_mgr.bdev_modules, internal.tailq) {
9614 7 : if (strcmp(name, bdev_module->name) == 0) {
9615 0 : break;
9616 : }
9617 7 : }
9618 :
9619 7 : return bdev_module;
9620 : }
9621 :
9622 : static int
9623 6 : bdev_write_zero_buffer(struct spdk_bdev_io *bdev_io)
9624 : {
9625 : uint64_t num_blocks;
9626 6 : void *md_buf = NULL;
9627 :
9628 6 : num_blocks = bdev_io->u.bdev.num_blocks;
9629 :
9630 6 : if (spdk_bdev_is_md_separate(bdev_io->bdev)) {
9631 4 : md_buf = (char *)g_bdev_mgr.zero_buffer +
9632 2 : spdk_bdev_get_block_size(bdev_io->bdev) * num_blocks;
9633 2 : }
9634 :
9635 12 : return bdev_write_blocks_with_md(bdev_io->internal.desc,
9636 6 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
9637 6 : g_bdev_mgr.zero_buffer, md_buf,
9638 6 : bdev_io->u.bdev.offset_blocks, num_blocks,
9639 6 : bdev_write_zero_buffer_done, bdev_io);
9640 : }
9641 :
9642 : static void
9643 6 : bdev_write_zero_buffer_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
9644 : {
9645 6 : struct spdk_bdev_io *parent_io = cb_arg;
9646 :
9647 6 : spdk_bdev_free_io(bdev_io);
9648 :
9649 6 : parent_io->internal.status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
9650 6 : parent_io->internal.cb(parent_io, success, parent_io->internal.caller_ctx);
9651 6 : }
9652 :
9653 : static void
9654 10 : bdev_set_qos_limit_done(struct set_qos_limit_ctx *ctx, int status)
9655 : {
9656 10 : spdk_spin_lock(&ctx->bdev->internal.spinlock);
9657 10 : ctx->bdev->internal.qos_mod_in_progress = false;
9658 10 : spdk_spin_unlock(&ctx->bdev->internal.spinlock);
9659 :
9660 10 : if (ctx->cb_fn) {
9661 8 : ctx->cb_fn(ctx->cb_arg, status);
9662 8 : }
9663 10 : free(ctx);
9664 10 : }
9665 :
9666 : static void
9667 2 : bdev_disable_qos_done(void *cb_arg)
9668 : {
9669 2 : struct set_qos_limit_ctx *ctx = cb_arg;
9670 2 : struct spdk_bdev *bdev = ctx->bdev;
9671 : struct spdk_bdev_qos *qos;
9672 :
9673 2 : spdk_spin_lock(&bdev->internal.spinlock);
9674 2 : qos = bdev->internal.qos;
9675 2 : bdev->internal.qos = NULL;
9676 2 : spdk_spin_unlock(&bdev->internal.spinlock);
9677 :
9678 2 : if (qos->thread != NULL) {
9679 2 : spdk_put_io_channel(spdk_io_channel_from_ctx(qos->ch));
9680 2 : spdk_poller_unregister(&qos->poller);
9681 2 : }
9682 :
9683 2 : free(qos);
9684 :
9685 2 : bdev_set_qos_limit_done(ctx, 0);
9686 2 : }
9687 :
9688 : static void
9689 2 : bdev_disable_qos_msg_done(struct spdk_bdev *bdev, void *_ctx, int status)
9690 : {
9691 2 : struct set_qos_limit_ctx *ctx = _ctx;
9692 : struct spdk_thread *thread;
9693 :
9694 2 : spdk_spin_lock(&bdev->internal.spinlock);
9695 2 : thread = bdev->internal.qos->thread;
9696 2 : spdk_spin_unlock(&bdev->internal.spinlock);
9697 :
9698 2 : if (thread != NULL) {
9699 2 : spdk_thread_send_msg(thread, bdev_disable_qos_done, ctx);
9700 2 : } else {
9701 0 : bdev_disable_qos_done(ctx);
9702 : }
9703 2 : }
9704 :
9705 : static void
9706 4 : bdev_disable_qos_msg(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9707 : struct spdk_io_channel *ch, void *_ctx)
9708 : {
9709 4 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(ch);
9710 : struct spdk_bdev_io *bdev_io;
9711 :
9712 4 : bdev_ch->flags &= ~BDEV_CH_QOS_ENABLED;
9713 :
9714 6 : while (!TAILQ_EMPTY(&bdev_ch->qos_queued_io)) {
9715 : /* Re-submit the queued I/O. */
9716 2 : bdev_io = TAILQ_FIRST(&bdev_ch->qos_queued_io);
9717 2 : TAILQ_REMOVE(&bdev_ch->qos_queued_io, bdev_io, internal.link);
9718 2 : _bdev_io_submit(bdev_io);
9719 : }
9720 :
9721 4 : spdk_bdev_for_each_channel_continue(i, 0);
9722 4 : }
9723 :
9724 : static void
9725 1 : bdev_update_qos_rate_limit_msg(void *cb_arg)
9726 : {
9727 1 : struct set_qos_limit_ctx *ctx = cb_arg;
9728 1 : struct spdk_bdev *bdev = ctx->bdev;
9729 :
9730 1 : spdk_spin_lock(&bdev->internal.spinlock);
9731 1 : bdev_qos_update_max_quota_per_timeslice(bdev->internal.qos);
9732 1 : spdk_spin_unlock(&bdev->internal.spinlock);
9733 :
9734 1 : bdev_set_qos_limit_done(ctx, 0);
9735 1 : }
9736 :
9737 : static void
9738 9 : bdev_enable_qos_msg(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9739 : struct spdk_io_channel *ch, void *_ctx)
9740 : {
9741 9 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(ch);
9742 :
9743 9 : spdk_spin_lock(&bdev->internal.spinlock);
9744 9 : bdev_enable_qos(bdev, bdev_ch);
9745 9 : spdk_spin_unlock(&bdev->internal.spinlock);
9746 9 : spdk_bdev_for_each_channel_continue(i, 0);
9747 9 : }
9748 :
9749 : static void
9750 6 : bdev_enable_qos_done(struct spdk_bdev *bdev, void *_ctx, int status)
9751 : {
9752 6 : struct set_qos_limit_ctx *ctx = _ctx;
9753 :
9754 6 : bdev_set_qos_limit_done(ctx, status);
9755 6 : }
9756 :
9757 : static void
9758 7 : bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits)
9759 : {
9760 : int i;
9761 :
9762 7 : assert(bdev->internal.qos != NULL);
9763 :
9764 35 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
9765 28 : if (limits[i] != SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
9766 28 : bdev->internal.qos->rate_limits[i].limit = limits[i];
9767 :
9768 28 : if (limits[i] == 0) {
9769 19 : bdev->internal.qos->rate_limits[i].limit =
9770 : SPDK_BDEV_QOS_LIMIT_NOT_DEFINED;
9771 19 : }
9772 28 : }
9773 28 : }
9774 7 : }
9775 :
9776 : void
9777 9 : spdk_bdev_set_qos_rate_limits(struct spdk_bdev *bdev, uint64_t *limits,
9778 : void (*cb_fn)(void *cb_arg, int status), void *cb_arg)
9779 : {
9780 : struct set_qos_limit_ctx *ctx;
9781 : uint32_t limit_set_complement;
9782 : uint64_t min_limit_per_sec;
9783 : int i;
9784 9 : bool disable_rate_limit = true;
9785 :
9786 45 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
9787 36 : if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED) {
9788 0 : continue;
9789 : }
9790 :
9791 36 : if (limits[i] > 0) {
9792 10 : disable_rate_limit = false;
9793 10 : }
9794 :
9795 36 : if (bdev_qos_is_iops_rate_limit(i) == true) {
9796 9 : min_limit_per_sec = SPDK_BDEV_QOS_MIN_IOS_PER_SEC;
9797 9 : } else {
9798 27 : if (limits[i] > SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC) {
9799 0 : SPDK_WARNLOG("Requested rate limit %" PRIu64 " will result in uint64_t overflow, "
9800 : "reset to %" PRIu64 "\n", limits[i], SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC);
9801 0 : limits[i] = SPDK_BDEV_QOS_MAX_MBYTES_PER_SEC;
9802 0 : }
9803 : /* Change from megabyte to byte rate limit */
9804 27 : limits[i] = limits[i] * 1024 * 1024;
9805 27 : min_limit_per_sec = SPDK_BDEV_QOS_MIN_BYTES_PER_SEC;
9806 : }
9807 :
9808 36 : limit_set_complement = limits[i] % min_limit_per_sec;
9809 36 : if (limit_set_complement) {
9810 0 : SPDK_ERRLOG("Requested rate limit %" PRIu64 " is not a multiple of %" PRIu64 "\n",
9811 : limits[i], min_limit_per_sec);
9812 0 : limits[i] += min_limit_per_sec - limit_set_complement;
9813 0 : SPDK_ERRLOG("Round up the rate limit to %" PRIu64 "\n", limits[i]);
9814 0 : }
9815 36 : }
9816 :
9817 9 : ctx = calloc(1, sizeof(*ctx));
9818 9 : if (ctx == NULL) {
9819 0 : cb_fn(cb_arg, -ENOMEM);
9820 0 : return;
9821 : }
9822 :
9823 9 : ctx->cb_fn = cb_fn;
9824 9 : ctx->cb_arg = cb_arg;
9825 9 : ctx->bdev = bdev;
9826 :
9827 9 : spdk_spin_lock(&bdev->internal.spinlock);
9828 9 : if (bdev->internal.qos_mod_in_progress) {
9829 1 : spdk_spin_unlock(&bdev->internal.spinlock);
9830 1 : free(ctx);
9831 1 : cb_fn(cb_arg, -EAGAIN);
9832 1 : return;
9833 : }
9834 8 : bdev->internal.qos_mod_in_progress = true;
9835 :
9836 8 : if (disable_rate_limit == true && bdev->internal.qos) {
9837 10 : for (i = 0; i < SPDK_BDEV_QOS_NUM_RATE_LIMIT_TYPES; i++) {
9838 8 : if (limits[i] == SPDK_BDEV_QOS_LIMIT_NOT_DEFINED &&
9839 0 : (bdev->internal.qos->rate_limits[i].limit > 0 &&
9840 0 : bdev->internal.qos->rate_limits[i].limit !=
9841 : SPDK_BDEV_QOS_LIMIT_NOT_DEFINED)) {
9842 0 : disable_rate_limit = false;
9843 0 : break;
9844 : }
9845 8 : }
9846 2 : }
9847 :
9848 8 : if (disable_rate_limit == false) {
9849 5 : if (bdev->internal.qos == NULL) {
9850 4 : bdev->internal.qos = calloc(1, sizeof(*bdev->internal.qos));
9851 4 : if (!bdev->internal.qos) {
9852 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9853 0 : SPDK_ERRLOG("Unable to allocate memory for QoS tracking\n");
9854 0 : bdev_set_qos_limit_done(ctx, -ENOMEM);
9855 0 : return;
9856 : }
9857 4 : }
9858 :
9859 5 : if (bdev->internal.qos->thread == NULL) {
9860 : /* Enabling */
9861 4 : bdev_set_qos_rate_limits(bdev, limits);
9862 :
9863 4 : spdk_bdev_for_each_channel(bdev, bdev_enable_qos_msg, ctx,
9864 : bdev_enable_qos_done);
9865 4 : } else {
9866 : /* Updating */
9867 1 : bdev_set_qos_rate_limits(bdev, limits);
9868 :
9869 2 : spdk_thread_send_msg(bdev->internal.qos->thread,
9870 1 : bdev_update_qos_rate_limit_msg, ctx);
9871 : }
9872 5 : } else {
9873 3 : if (bdev->internal.qos != NULL) {
9874 2 : bdev_set_qos_rate_limits(bdev, limits);
9875 :
9876 : /* Disabling */
9877 2 : spdk_bdev_for_each_channel(bdev, bdev_disable_qos_msg, ctx,
9878 : bdev_disable_qos_msg_done);
9879 2 : } else {
9880 1 : spdk_spin_unlock(&bdev->internal.spinlock);
9881 1 : bdev_set_qos_limit_done(ctx, 0);
9882 1 : return;
9883 : }
9884 : }
9885 :
9886 7 : spdk_spin_unlock(&bdev->internal.spinlock);
9887 9 : }
9888 :
9889 : struct spdk_bdev_histogram_ctx {
9890 : spdk_bdev_histogram_status_cb cb_fn;
9891 : void *cb_arg;
9892 : struct spdk_bdev *bdev;
9893 : int status;
9894 : };
9895 :
9896 : static void
9897 2 : bdev_histogram_disable_channel_cb(struct spdk_bdev *bdev, void *_ctx, int status)
9898 : {
9899 2 : struct spdk_bdev_histogram_ctx *ctx = _ctx;
9900 :
9901 2 : spdk_spin_lock(&ctx->bdev->internal.spinlock);
9902 2 : ctx->bdev->internal.histogram_in_progress = false;
9903 2 : spdk_spin_unlock(&ctx->bdev->internal.spinlock);
9904 2 : ctx->cb_fn(ctx->cb_arg, ctx->status);
9905 2 : free(ctx);
9906 2 : }
9907 :
9908 : static void
9909 3 : bdev_histogram_disable_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9910 : struct spdk_io_channel *_ch, void *_ctx)
9911 : {
9912 3 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
9913 :
9914 3 : if (ch->histogram != NULL) {
9915 3 : spdk_histogram_data_free(ch->histogram);
9916 3 : ch->histogram = NULL;
9917 3 : }
9918 3 : spdk_bdev_for_each_channel_continue(i, 0);
9919 3 : }
9920 :
9921 : static void
9922 2 : bdev_histogram_enable_channel_cb(struct spdk_bdev *bdev, void *_ctx, int status)
9923 : {
9924 2 : struct spdk_bdev_histogram_ctx *ctx = _ctx;
9925 :
9926 2 : if (status != 0) {
9927 0 : ctx->status = status;
9928 0 : ctx->bdev->internal.histogram_enabled = false;
9929 0 : spdk_bdev_for_each_channel(ctx->bdev, bdev_histogram_disable_channel, ctx,
9930 : bdev_histogram_disable_channel_cb);
9931 0 : } else {
9932 2 : spdk_spin_lock(&ctx->bdev->internal.spinlock);
9933 2 : ctx->bdev->internal.histogram_in_progress = false;
9934 2 : spdk_spin_unlock(&ctx->bdev->internal.spinlock);
9935 2 : ctx->cb_fn(ctx->cb_arg, ctx->status);
9936 2 : free(ctx);
9937 : }
9938 2 : }
9939 :
9940 : static void
9941 3 : bdev_histogram_enable_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
9942 : struct spdk_io_channel *_ch, void *_ctx)
9943 : {
9944 3 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
9945 3 : int status = 0;
9946 :
9947 3 : if (ch->histogram == NULL) {
9948 3 : ch->histogram = spdk_histogram_data_alloc();
9949 3 : if (ch->histogram == NULL) {
9950 0 : status = -ENOMEM;
9951 0 : }
9952 3 : }
9953 :
9954 3 : spdk_bdev_for_each_channel_continue(i, status);
9955 3 : }
9956 :
9957 : void
9958 4 : spdk_bdev_histogram_enable_ext(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn,
9959 : void *cb_arg, bool enable, struct spdk_bdev_enable_histogram_opts *opts)
9960 : {
9961 : struct spdk_bdev_histogram_ctx *ctx;
9962 :
9963 4 : ctx = calloc(1, sizeof(struct spdk_bdev_histogram_ctx));
9964 4 : if (ctx == NULL) {
9965 0 : cb_fn(cb_arg, -ENOMEM);
9966 0 : return;
9967 : }
9968 :
9969 4 : ctx->bdev = bdev;
9970 4 : ctx->status = 0;
9971 4 : ctx->cb_fn = cb_fn;
9972 4 : ctx->cb_arg = cb_arg;
9973 :
9974 4 : spdk_spin_lock(&bdev->internal.spinlock);
9975 4 : if (bdev->internal.histogram_in_progress) {
9976 0 : spdk_spin_unlock(&bdev->internal.spinlock);
9977 0 : free(ctx);
9978 0 : cb_fn(cb_arg, -EAGAIN);
9979 0 : return;
9980 : }
9981 :
9982 4 : bdev->internal.histogram_in_progress = true;
9983 4 : spdk_spin_unlock(&bdev->internal.spinlock);
9984 :
9985 4 : bdev->internal.histogram_enabled = enable;
9986 4 : bdev->internal.histogram_io_type = opts->io_type;
9987 :
9988 4 : if (enable) {
9989 : /* Allocate histogram for each channel */
9990 2 : spdk_bdev_for_each_channel(bdev, bdev_histogram_enable_channel, ctx,
9991 : bdev_histogram_enable_channel_cb);
9992 2 : } else {
9993 2 : spdk_bdev_for_each_channel(bdev, bdev_histogram_disable_channel, ctx,
9994 : bdev_histogram_disable_channel_cb);
9995 : }
9996 4 : }
9997 :
9998 : void
9999 4 : spdk_bdev_enable_histogram_opts_init(struct spdk_bdev_enable_histogram_opts *opts, size_t size)
10000 : {
10001 4 : if (opts == NULL) {
10002 0 : SPDK_ERRLOG("opts should not be NULL\n");
10003 0 : assert(opts != NULL);
10004 0 : return;
10005 : }
10006 4 : if (size == 0) {
10007 0 : SPDK_ERRLOG("size should not be zero\n");
10008 0 : assert(size != 0);
10009 0 : return;
10010 : }
10011 :
10012 4 : memset(opts, 0, size);
10013 4 : opts->size = size;
10014 :
10015 : #define FIELD_OK(field) \
10016 : offsetof(struct spdk_bdev_enable_histogram_opts, field) + sizeof(opts->field) <= size
10017 :
10018 : #define SET_FIELD(field, value) \
10019 : if (FIELD_OK(field)) { \
10020 : opts->field = value; \
10021 : } \
10022 :
10023 4 : SET_FIELD(io_type, 0);
10024 :
10025 : /* You should not remove this statement, but need to update the assert statement
10026 : * if you add a new field, and also add a corresponding SET_FIELD statement */
10027 : SPDK_STATIC_ASSERT(sizeof(struct spdk_bdev_enable_histogram_opts) == 9, "Incorrect size");
10028 :
10029 : #undef FIELD_OK
10030 : #undef SET_FIELD
10031 4 : }
10032 :
10033 : void
10034 4 : spdk_bdev_histogram_enable(struct spdk_bdev *bdev, spdk_bdev_histogram_status_cb cb_fn,
10035 : void *cb_arg, bool enable)
10036 : {
10037 : struct spdk_bdev_enable_histogram_opts opts;
10038 :
10039 4 : spdk_bdev_enable_histogram_opts_init(&opts, sizeof(opts));
10040 4 : spdk_bdev_histogram_enable_ext(bdev, cb_fn, cb_arg, enable, &opts);
10041 4 : }
10042 :
10043 : struct spdk_bdev_histogram_data_ctx {
10044 : spdk_bdev_histogram_data_cb cb_fn;
10045 : void *cb_arg;
10046 : struct spdk_bdev *bdev;
10047 : /** merged histogram data from all channels */
10048 : struct spdk_histogram_data *histogram;
10049 : };
10050 :
10051 : static void
10052 5 : bdev_histogram_get_channel_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10053 : {
10054 5 : struct spdk_bdev_histogram_data_ctx *ctx = _ctx;
10055 :
10056 5 : ctx->cb_fn(ctx->cb_arg, status, ctx->histogram);
10057 5 : free(ctx);
10058 5 : }
10059 :
10060 : static void
10061 7 : bdev_histogram_get_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10062 : struct spdk_io_channel *_ch, void *_ctx)
10063 : {
10064 7 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10065 7 : struct spdk_bdev_histogram_data_ctx *ctx = _ctx;
10066 7 : int status = 0;
10067 :
10068 7 : if (ch->histogram == NULL) {
10069 1 : status = -EFAULT;
10070 1 : } else {
10071 6 : spdk_histogram_data_merge(ctx->histogram, ch->histogram);
10072 : }
10073 :
10074 7 : spdk_bdev_for_each_channel_continue(i, status);
10075 7 : }
10076 :
10077 : void
10078 5 : spdk_bdev_histogram_get(struct spdk_bdev *bdev, struct spdk_histogram_data *histogram,
10079 : spdk_bdev_histogram_data_cb cb_fn,
10080 : void *cb_arg)
10081 : {
10082 : struct spdk_bdev_histogram_data_ctx *ctx;
10083 :
10084 5 : ctx = calloc(1, sizeof(struct spdk_bdev_histogram_data_ctx));
10085 5 : if (ctx == NULL) {
10086 0 : cb_fn(cb_arg, -ENOMEM, NULL);
10087 0 : return;
10088 : }
10089 :
10090 5 : ctx->bdev = bdev;
10091 5 : ctx->cb_fn = cb_fn;
10092 5 : ctx->cb_arg = cb_arg;
10093 :
10094 5 : ctx->histogram = histogram;
10095 :
10096 5 : spdk_bdev_for_each_channel(bdev, bdev_histogram_get_channel, ctx,
10097 : bdev_histogram_get_channel_cb);
10098 5 : }
10099 :
10100 : void
10101 2 : spdk_bdev_channel_get_histogram(struct spdk_io_channel *ch, spdk_bdev_histogram_data_cb cb_fn,
10102 : void *cb_arg)
10103 : {
10104 2 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(ch);
10105 2 : int status = 0;
10106 :
10107 2 : assert(cb_fn != NULL);
10108 :
10109 2 : if (bdev_ch->histogram == NULL) {
10110 1 : status = -EFAULT;
10111 1 : }
10112 2 : cb_fn(cb_arg, status, bdev_ch->histogram);
10113 2 : }
10114 :
10115 : size_t
10116 0 : spdk_bdev_get_media_events(struct spdk_bdev_desc *desc, struct spdk_bdev_media_event *events,
10117 : size_t max_events)
10118 : {
10119 : struct media_event_entry *entry;
10120 0 : size_t num_events = 0;
10121 :
10122 0 : for (; num_events < max_events; ++num_events) {
10123 0 : entry = TAILQ_FIRST(&desc->pending_media_events);
10124 0 : if (entry == NULL) {
10125 0 : break;
10126 : }
10127 :
10128 0 : events[num_events] = entry->event;
10129 0 : TAILQ_REMOVE(&desc->pending_media_events, entry, tailq);
10130 0 : TAILQ_INSERT_TAIL(&desc->free_media_events, entry, tailq);
10131 0 : }
10132 :
10133 0 : return num_events;
10134 : }
10135 :
10136 : int
10137 0 : spdk_bdev_push_media_events(struct spdk_bdev *bdev, const struct spdk_bdev_media_event *events,
10138 : size_t num_events)
10139 : {
10140 : struct spdk_bdev_desc *desc;
10141 : struct media_event_entry *entry;
10142 : size_t event_id;
10143 0 : int rc = 0;
10144 :
10145 0 : assert(bdev->media_events);
10146 :
10147 0 : spdk_spin_lock(&bdev->internal.spinlock);
10148 0 : TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
10149 0 : if (desc->write) {
10150 0 : break;
10151 : }
10152 0 : }
10153 :
10154 0 : if (desc == NULL || desc->media_events_buffer == NULL) {
10155 0 : rc = -ENODEV;
10156 0 : goto out;
10157 : }
10158 :
10159 0 : for (event_id = 0; event_id < num_events; ++event_id) {
10160 0 : entry = TAILQ_FIRST(&desc->free_media_events);
10161 0 : if (entry == NULL) {
10162 0 : break;
10163 : }
10164 :
10165 0 : TAILQ_REMOVE(&desc->free_media_events, entry, tailq);
10166 0 : TAILQ_INSERT_TAIL(&desc->pending_media_events, entry, tailq);
10167 0 : entry->event = events[event_id];
10168 0 : }
10169 :
10170 0 : rc = event_id;
10171 : out:
10172 0 : spdk_spin_unlock(&bdev->internal.spinlock);
10173 0 : return rc;
10174 : }
10175 :
10176 : static void
10177 0 : _media_management_notify(void *arg)
10178 : {
10179 0 : struct spdk_bdev_desc *desc = arg;
10180 :
10181 0 : _event_notify(desc, SPDK_BDEV_EVENT_MEDIA_MANAGEMENT);
10182 0 : }
10183 :
10184 : void
10185 0 : spdk_bdev_notify_media_management(struct spdk_bdev *bdev)
10186 : {
10187 : struct spdk_bdev_desc *desc;
10188 :
10189 0 : spdk_spin_lock(&bdev->internal.spinlock);
10190 0 : TAILQ_FOREACH(desc, &bdev->internal.open_descs, link) {
10191 0 : if (!TAILQ_EMPTY(&desc->pending_media_events)) {
10192 0 : event_notify(desc, _media_management_notify);
10193 0 : }
10194 0 : }
10195 0 : spdk_spin_unlock(&bdev->internal.spinlock);
10196 0 : }
10197 :
10198 : struct locked_lba_range_ctx {
10199 : struct lba_range range;
10200 : struct lba_range *current_range;
10201 : struct lba_range *owner_range;
10202 : struct spdk_poller *poller;
10203 : lock_range_cb cb_fn;
10204 : void *cb_arg;
10205 : };
10206 :
10207 : static void
10208 0 : bdev_lock_error_cleanup_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10209 : {
10210 0 : struct locked_lba_range_ctx *ctx = _ctx;
10211 :
10212 0 : ctx->cb_fn(&ctx->range, ctx->cb_arg, -ENOMEM);
10213 0 : free(ctx);
10214 0 : }
10215 :
10216 : static void bdev_unlock_lba_range_get_channel(struct spdk_bdev_channel_iter *i,
10217 : struct spdk_bdev *bdev, struct spdk_io_channel *ch, void *_ctx);
10218 :
10219 : static void
10220 14 : bdev_lock_lba_range_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10221 : {
10222 14 : struct locked_lba_range_ctx *ctx = _ctx;
10223 :
10224 14 : if (status == -ENOMEM) {
10225 : /* One of the channels could not allocate a range object.
10226 : * So we have to go back and clean up any ranges that were
10227 : * allocated successfully before we return error status to
10228 : * the caller. We can reuse the unlock function to do that
10229 : * clean up.
10230 : */
10231 0 : spdk_bdev_for_each_channel(bdev, bdev_unlock_lba_range_get_channel, ctx,
10232 : bdev_lock_error_cleanup_cb);
10233 0 : return;
10234 : }
10235 :
10236 : /* All channels have locked this range and no I/O overlapping the range
10237 : * are outstanding! Set the owner_ch for the range object for the
10238 : * locking channel, so that this channel will know that it is allowed
10239 : * to write to this range.
10240 : */
10241 14 : if (ctx->owner_range != NULL) {
10242 10 : ctx->owner_range->owner_ch = ctx->range.owner_ch;
10243 10 : }
10244 :
10245 14 : ctx->cb_fn(&ctx->range, ctx->cb_arg, status);
10246 :
10247 : /* Don't free the ctx here. Its range is in the bdev's global list of
10248 : * locked ranges still, and will be removed and freed when this range
10249 : * is later unlocked.
10250 : */
10251 14 : }
10252 :
10253 : static int
10254 17 : bdev_lock_lba_range_check_io(void *_i)
10255 : {
10256 17 : struct spdk_bdev_channel_iter *i = _i;
10257 17 : struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i->i);
10258 17 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10259 17 : struct locked_lba_range_ctx *ctx = i->ctx;
10260 17 : struct lba_range *range = ctx->current_range;
10261 : struct spdk_bdev_io *bdev_io;
10262 :
10263 17 : spdk_poller_unregister(&ctx->poller);
10264 :
10265 : /* The range is now in the locked_ranges, so no new IO can be submitted to this
10266 : * range. But we need to wait until any outstanding IO overlapping with this range
10267 : * are completed.
10268 : */
10269 18 : TAILQ_FOREACH(bdev_io, &ch->io_submitted, internal.ch_link) {
10270 3 : if (bdev_io_range_is_locked(bdev_io, range)) {
10271 2 : ctx->poller = SPDK_POLLER_REGISTER(bdev_lock_lba_range_check_io, i, 100);
10272 2 : return SPDK_POLLER_BUSY;
10273 : }
10274 1 : }
10275 :
10276 15 : spdk_bdev_for_each_channel_continue(i, 0);
10277 15 : return SPDK_POLLER_BUSY;
10278 17 : }
10279 :
10280 : static void
10281 15 : bdev_lock_lba_range_get_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10282 : struct spdk_io_channel *_ch, void *_ctx)
10283 : {
10284 15 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10285 15 : struct locked_lba_range_ctx *ctx = _ctx;
10286 : struct lba_range *range;
10287 :
10288 16 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
10289 1 : if (range->length == ctx->range.length &&
10290 0 : range->offset == ctx->range.offset &&
10291 0 : range->locked_ctx == ctx->range.locked_ctx) {
10292 : /* This range already exists on this channel, so don't add
10293 : * it again. This can happen when a new channel is created
10294 : * while the for_each_channel operation is in progress.
10295 : * Do not check for outstanding I/O in that case, since the
10296 : * range was locked before any I/O could be submitted to the
10297 : * new channel.
10298 : */
10299 0 : spdk_bdev_for_each_channel_continue(i, 0);
10300 0 : return;
10301 : }
10302 1 : }
10303 :
10304 15 : range = calloc(1, sizeof(*range));
10305 15 : if (range == NULL) {
10306 0 : spdk_bdev_for_each_channel_continue(i, -ENOMEM);
10307 0 : return;
10308 : }
10309 :
10310 15 : range->length = ctx->range.length;
10311 15 : range->offset = ctx->range.offset;
10312 15 : range->locked_ctx = ctx->range.locked_ctx;
10313 15 : range->quiesce = ctx->range.quiesce;
10314 15 : ctx->current_range = range;
10315 15 : if (ctx->range.owner_ch == ch) {
10316 : /* This is the range object for the channel that will hold
10317 : * the lock. Store it in the ctx object so that we can easily
10318 : * set its owner_ch after the lock is finally acquired.
10319 : */
10320 10 : ctx->owner_range = range;
10321 10 : }
10322 15 : TAILQ_INSERT_TAIL(&ch->locked_ranges, range, tailq);
10323 15 : bdev_lock_lba_range_check_io(i);
10324 15 : }
10325 :
10326 : static void
10327 14 : bdev_lock_lba_range_ctx(struct spdk_bdev *bdev, struct locked_lba_range_ctx *ctx)
10328 : {
10329 14 : assert(spdk_get_thread() == ctx->range.owner_thread);
10330 14 : assert(ctx->range.owner_ch == NULL ||
10331 : spdk_io_channel_get_thread(ctx->range.owner_ch->channel) == ctx->range.owner_thread);
10332 :
10333 : /* We will add a copy of this range to each channel now. */
10334 14 : spdk_bdev_for_each_channel(bdev, bdev_lock_lba_range_get_channel, ctx,
10335 : bdev_lock_lba_range_cb);
10336 14 : }
10337 :
10338 : static bool
10339 17 : bdev_lba_range_overlaps_tailq(struct lba_range *range, lba_range_tailq_t *tailq)
10340 : {
10341 : struct lba_range *r;
10342 :
10343 18 : TAILQ_FOREACH(r, tailq, tailq) {
10344 4 : if (bdev_lba_range_overlapped(range, r)) {
10345 3 : return true;
10346 : }
10347 1 : }
10348 14 : return false;
10349 17 : }
10350 :
10351 : static void bdev_quiesce_range_locked(struct lba_range *range, void *ctx, int status);
10352 :
10353 : static int
10354 14 : _bdev_lock_lba_range(struct spdk_bdev *bdev, struct spdk_bdev_channel *ch,
10355 : uint64_t offset, uint64_t length,
10356 : lock_range_cb cb_fn, void *cb_arg)
10357 : {
10358 : struct locked_lba_range_ctx *ctx;
10359 :
10360 14 : ctx = calloc(1, sizeof(*ctx));
10361 14 : if (ctx == NULL) {
10362 0 : return -ENOMEM;
10363 : }
10364 :
10365 14 : ctx->range.offset = offset;
10366 14 : ctx->range.length = length;
10367 14 : ctx->range.owner_thread = spdk_get_thread();
10368 14 : ctx->range.owner_ch = ch;
10369 14 : ctx->range.locked_ctx = cb_arg;
10370 14 : ctx->range.bdev = bdev;
10371 14 : ctx->range.quiesce = (cb_fn == bdev_quiesce_range_locked);
10372 14 : ctx->cb_fn = cb_fn;
10373 14 : ctx->cb_arg = cb_arg;
10374 :
10375 14 : spdk_spin_lock(&bdev->internal.spinlock);
10376 14 : if (bdev_lba_range_overlaps_tailq(&ctx->range, &bdev->internal.locked_ranges)) {
10377 : /* There is an active lock overlapping with this range.
10378 : * Put it on the pending list until this range no
10379 : * longer overlaps with another.
10380 : */
10381 2 : TAILQ_INSERT_TAIL(&bdev->internal.pending_locked_ranges, &ctx->range, tailq);
10382 2 : } else {
10383 12 : TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, &ctx->range, tailq);
10384 12 : bdev_lock_lba_range_ctx(bdev, ctx);
10385 : }
10386 14 : spdk_spin_unlock(&bdev->internal.spinlock);
10387 14 : return 0;
10388 14 : }
10389 :
10390 : static int
10391 10 : bdev_lock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
10392 : uint64_t offset, uint64_t length,
10393 : lock_range_cb cb_fn, void *cb_arg)
10394 : {
10395 10 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
10396 10 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10397 :
10398 10 : if (cb_arg == NULL) {
10399 0 : SPDK_ERRLOG("cb_arg must not be NULL\n");
10400 0 : return -EINVAL;
10401 : }
10402 :
10403 10 : return _bdev_lock_lba_range(bdev, ch, offset, length, cb_fn, cb_arg);
10404 10 : }
10405 :
10406 : static void
10407 2 : bdev_lock_lba_range_ctx_msg(void *_ctx)
10408 : {
10409 2 : struct locked_lba_range_ctx *ctx = _ctx;
10410 :
10411 2 : bdev_lock_lba_range_ctx(ctx->range.bdev, ctx);
10412 2 : }
10413 :
10414 : static void
10415 14 : bdev_unlock_lba_range_cb(struct spdk_bdev *bdev, void *_ctx, int status)
10416 : {
10417 14 : struct locked_lba_range_ctx *ctx = _ctx;
10418 : struct locked_lba_range_ctx *pending_ctx;
10419 : struct lba_range *range, *tmp;
10420 :
10421 14 : spdk_spin_lock(&bdev->internal.spinlock);
10422 : /* Check if there are any pending locked ranges that overlap with this range
10423 : * that was just unlocked. If there are, check that it doesn't overlap with any
10424 : * other locked ranges before calling bdev_lock_lba_range_ctx which will start
10425 : * the lock process.
10426 : */
10427 17 : TAILQ_FOREACH_SAFE(range, &bdev->internal.pending_locked_ranges, tailq, tmp) {
10428 3 : if (bdev_lba_range_overlapped(range, &ctx->range) &&
10429 3 : !bdev_lba_range_overlaps_tailq(range, &bdev->internal.locked_ranges)) {
10430 2 : TAILQ_REMOVE(&bdev->internal.pending_locked_ranges, range, tailq);
10431 2 : pending_ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range);
10432 2 : TAILQ_INSERT_TAIL(&bdev->internal.locked_ranges, range, tailq);
10433 4 : spdk_thread_send_msg(pending_ctx->range.owner_thread,
10434 2 : bdev_lock_lba_range_ctx_msg, pending_ctx);
10435 2 : }
10436 3 : }
10437 14 : spdk_spin_unlock(&bdev->internal.spinlock);
10438 :
10439 14 : ctx->cb_fn(&ctx->range, ctx->cb_arg, status);
10440 14 : free(ctx);
10441 14 : }
10442 :
10443 : static void
10444 16 : bdev_unlock_lba_range_get_channel(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10445 : struct spdk_io_channel *_ch, void *_ctx)
10446 : {
10447 16 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10448 16 : struct locked_lba_range_ctx *ctx = _ctx;
10449 : TAILQ_HEAD(, spdk_bdev_io) io_locked;
10450 : struct spdk_bdev_io *bdev_io;
10451 : struct lba_range *range;
10452 :
10453 16 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
10454 32 : if (ctx->range.offset == range->offset &&
10455 16 : ctx->range.length == range->length &&
10456 16 : ctx->range.locked_ctx == range->locked_ctx) {
10457 16 : TAILQ_REMOVE(&ch->locked_ranges, range, tailq);
10458 16 : free(range);
10459 16 : break;
10460 : }
10461 0 : }
10462 :
10463 : /* Note: we should almost always be able to assert that the range specified
10464 : * was found. But there are some very rare corner cases where a new channel
10465 : * gets created simultaneously with a range unlock, where this function
10466 : * would execute on that new channel and wouldn't have the range.
10467 : * We also use this to clean up range allocations when a later allocation
10468 : * fails in the locking path.
10469 : * So we can't actually assert() here.
10470 : */
10471 :
10472 : /* Swap the locked IO into a temporary list, and then try to submit them again.
10473 : * We could hyper-optimize this to only resubmit locked I/O that overlap
10474 : * with the range that was just unlocked, but this isn't a performance path so
10475 : * we go for simplicity here.
10476 : */
10477 16 : TAILQ_INIT(&io_locked);
10478 16 : TAILQ_SWAP(&ch->io_locked, &io_locked, spdk_bdev_io, internal.ch_link);
10479 19 : while (!TAILQ_EMPTY(&io_locked)) {
10480 3 : bdev_io = TAILQ_FIRST(&io_locked);
10481 3 : TAILQ_REMOVE(&io_locked, bdev_io, internal.ch_link);
10482 3 : bdev_io_submit(bdev_io);
10483 : }
10484 :
10485 16 : spdk_bdev_for_each_channel_continue(i, 0);
10486 16 : }
10487 :
10488 : static int
10489 14 : _bdev_unlock_lba_range(struct spdk_bdev *bdev, uint64_t offset, uint64_t length,
10490 : lock_range_cb cb_fn, void *cb_arg)
10491 : {
10492 : struct locked_lba_range_ctx *ctx;
10493 : struct lba_range *range;
10494 :
10495 14 : spdk_spin_lock(&bdev->internal.spinlock);
10496 : /* To start the unlock the process, we find the range in the bdev's locked_ranges
10497 : * and remove it. This ensures new channels don't inherit the locked range.
10498 : * Then we will send a message to each channel to remove the range from its
10499 : * per-channel list.
10500 : */
10501 14 : TAILQ_FOREACH(range, &bdev->internal.locked_ranges, tailq) {
10502 24 : if (range->offset == offset && range->length == length &&
10503 14 : (range->owner_ch == NULL || range->locked_ctx == cb_arg)) {
10504 14 : break;
10505 : }
10506 0 : }
10507 14 : if (range == NULL) {
10508 0 : assert(false);
10509 : spdk_spin_unlock(&bdev->internal.spinlock);
10510 : return -EINVAL;
10511 : }
10512 14 : TAILQ_REMOVE(&bdev->internal.locked_ranges, range, tailq);
10513 14 : ctx = SPDK_CONTAINEROF(range, struct locked_lba_range_ctx, range);
10514 14 : spdk_spin_unlock(&bdev->internal.spinlock);
10515 :
10516 14 : ctx->cb_fn = cb_fn;
10517 14 : ctx->cb_arg = cb_arg;
10518 :
10519 14 : spdk_bdev_for_each_channel(bdev, bdev_unlock_lba_range_get_channel, ctx,
10520 : bdev_unlock_lba_range_cb);
10521 14 : return 0;
10522 : }
10523 :
10524 : static int
10525 12 : bdev_unlock_lba_range(struct spdk_bdev_desc *desc, struct spdk_io_channel *_ch,
10526 : uint64_t offset, uint64_t length,
10527 : lock_range_cb cb_fn, void *cb_arg)
10528 : {
10529 12 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
10530 12 : struct spdk_bdev_channel *ch = __io_ch_to_bdev_ch(_ch);
10531 : struct lba_range *range;
10532 12 : bool range_found = false;
10533 :
10534 : /* Let's make sure the specified channel actually has a lock on
10535 : * the specified range. Note that the range must match exactly.
10536 : */
10537 14 : TAILQ_FOREACH(range, &ch->locked_ranges, tailq) {
10538 22 : if (range->offset == offset && range->length == length &&
10539 11 : range->owner_ch == ch && range->locked_ctx == cb_arg) {
10540 10 : range_found = true;
10541 10 : break;
10542 : }
10543 2 : }
10544 :
10545 12 : if (!range_found) {
10546 2 : return -EINVAL;
10547 : }
10548 :
10549 10 : return _bdev_unlock_lba_range(bdev, offset, length, cb_fn, cb_arg);
10550 12 : }
10551 :
10552 : struct bdev_quiesce_ctx {
10553 : spdk_bdev_quiesce_cb cb_fn;
10554 : void *cb_arg;
10555 : };
10556 :
10557 : static void
10558 4 : bdev_unquiesce_range_unlocked(struct lba_range *range, void *ctx, int status)
10559 : {
10560 4 : struct bdev_quiesce_ctx *quiesce_ctx = ctx;
10561 :
10562 4 : if (quiesce_ctx->cb_fn != NULL) {
10563 4 : quiesce_ctx->cb_fn(quiesce_ctx->cb_arg, status);
10564 4 : }
10565 :
10566 4 : free(quiesce_ctx);
10567 4 : }
10568 :
10569 : static void
10570 4 : bdev_quiesce_range_locked(struct lba_range *range, void *ctx, int status)
10571 : {
10572 4 : struct bdev_quiesce_ctx *quiesce_ctx = ctx;
10573 4 : struct spdk_bdev_module *module = range->bdev->module;
10574 :
10575 4 : if (status != 0) {
10576 0 : if (quiesce_ctx->cb_fn != NULL) {
10577 0 : quiesce_ctx->cb_fn(quiesce_ctx->cb_arg, status);
10578 0 : }
10579 0 : free(quiesce_ctx);
10580 0 : return;
10581 : }
10582 :
10583 4 : spdk_spin_lock(&module->internal.spinlock);
10584 4 : TAILQ_INSERT_TAIL(&module->internal.quiesced_ranges, range, tailq_module);
10585 4 : spdk_spin_unlock(&module->internal.spinlock);
10586 :
10587 4 : if (quiesce_ctx->cb_fn != NULL) {
10588 : /* copy the context in case the range is unlocked by the callback */
10589 4 : struct bdev_quiesce_ctx tmp = *quiesce_ctx;
10590 :
10591 4 : quiesce_ctx->cb_fn = NULL;
10592 4 : quiesce_ctx->cb_arg = NULL;
10593 :
10594 4 : tmp.cb_fn(tmp.cb_arg, status);
10595 4 : }
10596 : /* quiesce_ctx will be freed on unquiesce */
10597 4 : }
10598 :
10599 : static int
10600 9 : _spdk_bdev_quiesce(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10601 : uint64_t offset, uint64_t length,
10602 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg,
10603 : bool unquiesce)
10604 : {
10605 : struct bdev_quiesce_ctx *quiesce_ctx;
10606 : int rc;
10607 :
10608 9 : if (module != bdev->module) {
10609 0 : SPDK_ERRLOG("Bdev does not belong to specified module.\n");
10610 0 : return -EINVAL;
10611 : }
10612 :
10613 9 : if (!bdev_io_valid_blocks(bdev, offset, length)) {
10614 0 : return -EINVAL;
10615 : }
10616 :
10617 9 : if (unquiesce) {
10618 : struct lba_range *range;
10619 :
10620 : /* Make sure the specified range is actually quiesced in the specified module and
10621 : * then remove it from the list. Note that the range must match exactly.
10622 : */
10623 5 : spdk_spin_lock(&module->internal.spinlock);
10624 6 : TAILQ_FOREACH(range, &module->internal.quiesced_ranges, tailq_module) {
10625 5 : if (range->bdev == bdev && range->offset == offset && range->length == length) {
10626 4 : TAILQ_REMOVE(&module->internal.quiesced_ranges, range, tailq_module);
10627 4 : break;
10628 : }
10629 1 : }
10630 5 : spdk_spin_unlock(&module->internal.spinlock);
10631 :
10632 5 : if (range == NULL) {
10633 1 : SPDK_ERRLOG("The range to unquiesce was not found.\n");
10634 1 : return -EINVAL;
10635 : }
10636 :
10637 4 : quiesce_ctx = range->locked_ctx;
10638 4 : quiesce_ctx->cb_fn = cb_fn;
10639 4 : quiesce_ctx->cb_arg = cb_arg;
10640 :
10641 4 : rc = _bdev_unlock_lba_range(bdev, offset, length, bdev_unquiesce_range_unlocked, quiesce_ctx);
10642 4 : } else {
10643 4 : quiesce_ctx = malloc(sizeof(*quiesce_ctx));
10644 4 : if (quiesce_ctx == NULL) {
10645 0 : return -ENOMEM;
10646 : }
10647 :
10648 4 : quiesce_ctx->cb_fn = cb_fn;
10649 4 : quiesce_ctx->cb_arg = cb_arg;
10650 :
10651 4 : rc = _bdev_lock_lba_range(bdev, NULL, offset, length, bdev_quiesce_range_locked, quiesce_ctx);
10652 4 : if (rc != 0) {
10653 0 : free(quiesce_ctx);
10654 0 : }
10655 : }
10656 :
10657 8 : return rc;
10658 9 : }
10659 :
10660 : int
10661 3 : spdk_bdev_quiesce(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10662 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10663 : {
10664 3 : return _spdk_bdev_quiesce(bdev, module, 0, bdev->blockcnt, cb_fn, cb_arg, false);
10665 : }
10666 :
10667 : int
10668 3 : spdk_bdev_unquiesce(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10669 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10670 : {
10671 3 : return _spdk_bdev_quiesce(bdev, module, 0, bdev->blockcnt, cb_fn, cb_arg, true);
10672 : }
10673 :
10674 : int
10675 1 : spdk_bdev_quiesce_range(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10676 : uint64_t offset, uint64_t length,
10677 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10678 : {
10679 1 : return _spdk_bdev_quiesce(bdev, module, offset, length, cb_fn, cb_arg, false);
10680 : }
10681 :
10682 : int
10683 2 : spdk_bdev_unquiesce_range(struct spdk_bdev *bdev, struct spdk_bdev_module *module,
10684 : uint64_t offset, uint64_t length,
10685 : spdk_bdev_quiesce_cb cb_fn, void *cb_arg)
10686 : {
10687 2 : return _spdk_bdev_quiesce(bdev, module, offset, length, cb_fn, cb_arg, true);
10688 : }
10689 :
10690 : int
10691 273 : spdk_bdev_get_memory_domains(struct spdk_bdev *bdev, struct spdk_memory_domain **domains,
10692 : int array_size)
10693 : {
10694 273 : if (!bdev) {
10695 1 : return -EINVAL;
10696 : }
10697 :
10698 272 : if (bdev->fn_table->get_memory_domains) {
10699 3 : return bdev->fn_table->get_memory_domains(bdev->ctxt, domains, array_size);
10700 : }
10701 :
10702 269 : return 0;
10703 273 : }
10704 :
10705 : struct spdk_bdev_for_each_io_ctx {
10706 : void *ctx;
10707 : spdk_bdev_io_fn fn;
10708 : spdk_bdev_for_each_io_cb cb;
10709 : };
10710 :
10711 : static void
10712 0 : bdev_channel_for_each_io(struct spdk_bdev_channel_iter *i, struct spdk_bdev *bdev,
10713 : struct spdk_io_channel *io_ch, void *_ctx)
10714 : {
10715 0 : struct spdk_bdev_for_each_io_ctx *ctx = _ctx;
10716 0 : struct spdk_bdev_channel *bdev_ch = __io_ch_to_bdev_ch(io_ch);
10717 : struct spdk_bdev_io *bdev_io;
10718 0 : int rc = 0;
10719 :
10720 0 : TAILQ_FOREACH(bdev_io, &bdev_ch->io_submitted, internal.ch_link) {
10721 0 : rc = ctx->fn(ctx->ctx, bdev_io);
10722 0 : if (rc != 0) {
10723 0 : break;
10724 : }
10725 0 : }
10726 :
10727 0 : spdk_bdev_for_each_channel_continue(i, rc);
10728 0 : }
10729 :
10730 : static void
10731 0 : bdev_for_each_io_done(struct spdk_bdev *bdev, void *_ctx, int status)
10732 : {
10733 0 : struct spdk_bdev_for_each_io_ctx *ctx = _ctx;
10734 :
10735 0 : ctx->cb(ctx->ctx, status);
10736 :
10737 0 : free(ctx);
10738 0 : }
10739 :
10740 : void
10741 0 : spdk_bdev_for_each_bdev_io(struct spdk_bdev *bdev, void *_ctx, spdk_bdev_io_fn fn,
10742 : spdk_bdev_for_each_io_cb cb)
10743 : {
10744 : struct spdk_bdev_for_each_io_ctx *ctx;
10745 :
10746 0 : assert(fn != NULL && cb != NULL);
10747 :
10748 0 : ctx = calloc(1, sizeof(*ctx));
10749 0 : if (ctx == NULL) {
10750 0 : SPDK_ERRLOG("Failed to allocate context.\n");
10751 0 : cb(_ctx, -ENOMEM);
10752 0 : return;
10753 : }
10754 :
10755 0 : ctx->ctx = _ctx;
10756 0 : ctx->fn = fn;
10757 0 : ctx->cb = cb;
10758 :
10759 0 : spdk_bdev_for_each_channel(bdev, bdev_channel_for_each_io, ctx,
10760 : bdev_for_each_io_done);
10761 0 : }
10762 :
10763 : void
10764 132 : spdk_bdev_for_each_channel_continue(struct spdk_bdev_channel_iter *iter, int status)
10765 : {
10766 132 : spdk_for_each_channel_continue(iter->i, status);
10767 132 : }
10768 :
10769 : static struct spdk_bdev *
10770 362 : io_channel_iter_get_bdev(struct spdk_io_channel_iter *i)
10771 : {
10772 362 : void *io_device = spdk_io_channel_iter_get_io_device(i);
10773 :
10774 362 : return __bdev_from_io_dev(io_device);
10775 : }
10776 :
10777 : static void
10778 132 : bdev_each_channel_msg(struct spdk_io_channel_iter *i)
10779 : {
10780 132 : struct spdk_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
10781 132 : struct spdk_bdev *bdev = io_channel_iter_get_bdev(i);
10782 132 : struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
10783 :
10784 132 : iter->i = i;
10785 132 : iter->fn(iter, bdev, ch, iter->ctx);
10786 132 : }
10787 :
10788 : static void
10789 230 : bdev_each_channel_cpl(struct spdk_io_channel_iter *i, int status)
10790 : {
10791 230 : struct spdk_bdev_channel_iter *iter = spdk_io_channel_iter_get_ctx(i);
10792 230 : struct spdk_bdev *bdev = io_channel_iter_get_bdev(i);
10793 :
10794 230 : iter->i = i;
10795 230 : iter->cpl(bdev, iter->ctx, status);
10796 :
10797 230 : free(iter);
10798 230 : }
10799 :
10800 : void
10801 230 : spdk_bdev_for_each_channel(struct spdk_bdev *bdev, spdk_bdev_for_each_channel_msg fn,
10802 : void *ctx, spdk_bdev_for_each_channel_done cpl)
10803 : {
10804 : struct spdk_bdev_channel_iter *iter;
10805 :
10806 230 : assert(bdev != NULL && fn != NULL && ctx != NULL);
10807 :
10808 230 : iter = calloc(1, sizeof(struct spdk_bdev_channel_iter));
10809 230 : if (iter == NULL) {
10810 0 : SPDK_ERRLOG("Unable to allocate iterator\n");
10811 0 : assert(false);
10812 : return;
10813 : }
10814 :
10815 230 : iter->fn = fn;
10816 230 : iter->cpl = cpl;
10817 230 : iter->ctx = ctx;
10818 :
10819 460 : spdk_for_each_channel(__bdev_to_io_dev(bdev), bdev_each_channel_msg,
10820 230 : iter, bdev_each_channel_cpl);
10821 230 : }
10822 :
10823 : static void
10824 3 : bdev_copy_do_write_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
10825 : {
10826 3 : struct spdk_bdev_io *parent_io = cb_arg;
10827 :
10828 3 : spdk_bdev_free_io(bdev_io);
10829 :
10830 : /* Check return status of write */
10831 3 : parent_io->internal.status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED;
10832 3 : parent_io->internal.cb(parent_io, success, parent_io->internal.caller_ctx);
10833 3 : }
10834 :
10835 : static void
10836 3 : bdev_copy_do_write(void *_bdev_io)
10837 : {
10838 3 : struct spdk_bdev_io *bdev_io = _bdev_io;
10839 : int rc;
10840 :
10841 : /* Write blocks */
10842 6 : rc = spdk_bdev_write_blocks_with_md(bdev_io->internal.desc,
10843 3 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
10844 3 : bdev_io->u.bdev.iovs[0].iov_base,
10845 3 : bdev_io->u.bdev.md_buf, bdev_io->u.bdev.offset_blocks,
10846 3 : bdev_io->u.bdev.num_blocks, bdev_copy_do_write_done, bdev_io);
10847 :
10848 3 : if (rc == -ENOMEM) {
10849 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_copy_do_write);
10850 3 : } else if (rc != 0) {
10851 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10852 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
10853 0 : }
10854 3 : }
10855 :
10856 : static void
10857 3 : bdev_copy_do_read_done(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg)
10858 : {
10859 3 : struct spdk_bdev_io *parent_io = cb_arg;
10860 :
10861 3 : spdk_bdev_free_io(bdev_io);
10862 :
10863 : /* Check return status of read */
10864 3 : if (!success) {
10865 0 : parent_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10866 0 : parent_io->internal.cb(parent_io, false, parent_io->internal.caller_ctx);
10867 0 : return;
10868 : }
10869 :
10870 : /* Do write */
10871 3 : bdev_copy_do_write(parent_io);
10872 3 : }
10873 :
10874 : static void
10875 3 : bdev_copy_do_read(void *_bdev_io)
10876 : {
10877 3 : struct spdk_bdev_io *bdev_io = _bdev_io;
10878 : int rc;
10879 :
10880 : /* Read blocks */
10881 6 : rc = spdk_bdev_read_blocks_with_md(bdev_io->internal.desc,
10882 3 : spdk_io_channel_from_ctx(bdev_io->internal.ch),
10883 3 : bdev_io->u.bdev.iovs[0].iov_base,
10884 3 : bdev_io->u.bdev.md_buf, bdev_io->u.bdev.copy.src_offset_blocks,
10885 3 : bdev_io->u.bdev.num_blocks, bdev_copy_do_read_done, bdev_io);
10886 :
10887 3 : if (rc == -ENOMEM) {
10888 0 : bdev_queue_io_wait_with_cb(bdev_io, bdev_copy_do_read);
10889 3 : } else if (rc != 0) {
10890 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10891 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
10892 0 : }
10893 3 : }
10894 :
10895 : static void
10896 3 : bdev_copy_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success)
10897 : {
10898 3 : if (!success) {
10899 0 : bdev_io->internal.status = SPDK_BDEV_IO_STATUS_FAILED;
10900 0 : bdev_io->internal.cb(bdev_io, false, bdev_io->internal.caller_ctx);
10901 0 : return;
10902 : }
10903 :
10904 3 : bdev_copy_do_read(bdev_io);
10905 3 : }
10906 :
10907 : int
10908 27 : spdk_bdev_copy_blocks(struct spdk_bdev_desc *desc, struct spdk_io_channel *ch,
10909 : uint64_t dst_offset_blocks, uint64_t src_offset_blocks, uint64_t num_blocks,
10910 : spdk_bdev_io_completion_cb cb, void *cb_arg)
10911 : {
10912 27 : struct spdk_bdev *bdev = spdk_bdev_desc_get_bdev(desc);
10913 : struct spdk_bdev_io *bdev_io;
10914 27 : struct spdk_bdev_channel *channel = spdk_io_channel_get_ctx(ch);
10915 :
10916 27 : if (!desc->write) {
10917 0 : return -EBADF;
10918 : }
10919 :
10920 27 : if (!bdev_io_valid_blocks(bdev, dst_offset_blocks, num_blocks) ||
10921 27 : !bdev_io_valid_blocks(bdev, src_offset_blocks, num_blocks)) {
10922 0 : SPDK_DEBUGLOG(bdev,
10923 : "Invalid offset or number of blocks: dst %lu, src %lu, count %lu\n",
10924 : dst_offset_blocks, src_offset_blocks, num_blocks);
10925 0 : return -EINVAL;
10926 : }
10927 :
10928 27 : bdev_io = bdev_channel_get_io(channel);
10929 27 : if (!bdev_io) {
10930 0 : return -ENOMEM;
10931 : }
10932 :
10933 27 : bdev_io->internal.ch = channel;
10934 27 : bdev_io->internal.desc = desc;
10935 27 : bdev_io->type = SPDK_BDEV_IO_TYPE_COPY;
10936 :
10937 27 : bdev_io->u.bdev.offset_blocks = dst_offset_blocks;
10938 27 : bdev_io->u.bdev.copy.src_offset_blocks = src_offset_blocks;
10939 27 : bdev_io->u.bdev.num_blocks = num_blocks;
10940 27 : bdev_io->u.bdev.memory_domain = NULL;
10941 27 : bdev_io->u.bdev.memory_domain_ctx = NULL;
10942 27 : bdev_io->u.bdev.iovs = NULL;
10943 27 : bdev_io->u.bdev.iovcnt = 0;
10944 27 : bdev_io->u.bdev.md_buf = NULL;
10945 27 : bdev_io->u.bdev.accel_sequence = NULL;
10946 27 : bdev_io_init(bdev_io, bdev, cb_arg, cb);
10947 :
10948 27 : if (dst_offset_blocks == src_offset_blocks || num_blocks == 0) {
10949 0 : spdk_thread_send_msg(spdk_get_thread(), bdev_io_complete_cb, bdev_io);
10950 0 : return 0;
10951 : }
10952 :
10953 :
10954 : /* If the copy size is large and should be split, use the generic split logic
10955 : * regardless of whether SPDK_BDEV_IO_TYPE_COPY is supported or not.
10956 : *
10957 : * Then, send the copy request if SPDK_BDEV_IO_TYPE_COPY is supported or
10958 : * emulate it using regular read and write requests otherwise.
10959 : */
10960 27 : if (spdk_bdev_io_type_supported(bdev, SPDK_BDEV_IO_TYPE_COPY) ||
10961 4 : bdev_io->internal.f.split) {
10962 24 : bdev_io_submit(bdev_io);
10963 24 : return 0;
10964 : }
10965 :
10966 3 : spdk_bdev_io_get_buf(bdev_io, bdev_copy_get_buf_cb, num_blocks * spdk_bdev_get_block_size(bdev));
10967 :
10968 3 : return 0;
10969 27 : }
10970 :
10971 3 : SPDK_LOG_REGISTER_COMPONENT(bdev)
10972 :
10973 : static void
10974 0 : bdev_trace(void)
10975 : {
10976 0 : struct spdk_trace_tpoint_opts opts[] = {
10977 : {
10978 : "BDEV_IO_START", TRACE_BDEV_IO_START,
10979 : OWNER_TYPE_BDEV, OBJECT_BDEV_IO, 1,
10980 : {
10981 : { "type", SPDK_TRACE_ARG_TYPE_INT, 8 },
10982 : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
10983 : { "offset", SPDK_TRACE_ARG_TYPE_INT, 8 },
10984 : { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
10985 : }
10986 : },
10987 : {
10988 : "BDEV_IO_DONE", TRACE_BDEV_IO_DONE,
10989 : OWNER_TYPE_BDEV, OBJECT_BDEV_IO, 0,
10990 : {
10991 : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
10992 : { "qd", SPDK_TRACE_ARG_TYPE_INT, 4 }
10993 : }
10994 : },
10995 : {
10996 : "BDEV_IOCH_CREATE", TRACE_BDEV_IOCH_CREATE,
10997 : OWNER_TYPE_BDEV, OBJECT_NONE, 0,
10998 : {
10999 : { "tid", SPDK_TRACE_ARG_TYPE_INT, 8 }
11000 : }
11001 : },
11002 : {
11003 : "BDEV_IOCH_DESTROY", TRACE_BDEV_IOCH_DESTROY,
11004 : OWNER_TYPE_BDEV, OBJECT_NONE, 0,
11005 : {
11006 : { "tid", SPDK_TRACE_ARG_TYPE_INT, 8 }
11007 : }
11008 : },
11009 : };
11010 :
11011 :
11012 0 : spdk_trace_register_owner_type(OWNER_TYPE_BDEV, 'b');
11013 0 : spdk_trace_register_object(OBJECT_BDEV_IO, 'i');
11014 0 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
11015 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_NVME_IO_START, OBJECT_BDEV_IO, 0);
11016 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_NVME_IO_DONE, OBJECT_BDEV_IO, 0);
11017 0 : spdk_trace_tpoint_register_relation(TRACE_BLOB_REQ_SET_START, OBJECT_BDEV_IO, 0);
11018 0 : spdk_trace_tpoint_register_relation(TRACE_BLOB_REQ_SET_COMPLETE, OBJECT_BDEV_IO, 0);
11019 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_RAID_IO_START, OBJECT_BDEV_IO, 0);
11020 0 : spdk_trace_tpoint_register_relation(TRACE_BDEV_RAID_IO_DONE, OBJECT_BDEV_IO, 0);
11021 0 : }
11022 3 : SPDK_TRACE_REGISTER_FN(bdev_trace, "bdev", TRACE_GROUP_BDEV)
|