LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 4028 5729 70.3 %
Date: 2024-11-15 10:36:55 Functions: 362 471 76.9 %

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

Generated by: LCOV version 1.15