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