LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 4008 5599 71.6 %
Date: 2024-11-20 15:00:29 Functions: 358 462 77.5 %

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

Generated by: LCOV version 1.15