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