LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 4034 5633 71.6 %
Date: 2024-11-20 17:34:19 Functions: 363 467 77.7 %

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

Generated by: LCOV version 1.15