LCOV - code coverage report
Current view: top level - lib/bdev - bdev.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 3986 5571 71.5 %
Date: 2024-11-20 05:28:32 Functions: 357 453 78.8 %

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

Generated by: LCOV version 1.15