LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 3488 4852 71.9 %
Date: 2024-07-14 05:23:08 Functions: 346 440 78.6 %

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

Generated by: LCOV version 1.15