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