LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 4062 5745 70.7 %
Date: 2024-11-20 13:36:15 Functions: 365 474 77.0 %

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

Generated by: LCOV version 1.15