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