LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 3564 5002 71.3 %
Date: 2024-11-05 10:06:02 Functions: 353 454 77.8 %

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

Generated by: LCOV version 1.15