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

Generated by: LCOV version 1.15