|
| 1 | +/* SPDX-License-Identifier: BSD-3-Clause |
| 2 | + * Copyright (C) 2025,2026 IBM, Inc. |
| 3 | + * All rights reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <rbd/asio/ContextWQ.hpp> |
| 7 | +#include <rbd/librbd.h> |
| 8 | +#include <rados/librados.hpp> |
| 9 | +#include <atomic> |
| 10 | + |
| 11 | +#include "bdev_rbd_spdk_context_wq.h" |
| 12 | + |
| 13 | +extern "C" { |
| 14 | +#include "spdk/stdinc.h" |
| 15 | +#include "spdk/thread.h" |
| 16 | +#include "spdk/log.h" |
| 17 | +#include "spdk/env.h" |
| 18 | +} |
| 19 | + |
| 20 | +namespace librbd { |
| 21 | +namespace asio { |
| 22 | + |
| 23 | +SpdkContextWQ::SpdkContextWQ(void* cct, struct spdk_thread* reactor_thread) |
| 24 | + : ContextWQ(cct), m_reactor_thread(reactor_thread) { |
| 25 | + assert(reactor_thread != nullptr); |
| 26 | +} |
| 27 | + |
| 28 | +SpdkContextWQ::~SpdkContextWQ() { |
| 29 | + // Set shutdown flag to reject new operations |
| 30 | + m_shutdown.store(true, std::memory_order_release); |
| 31 | + |
| 32 | + // Wait for all pending messages to complete |
| 33 | + drain(); |
| 34 | + |
| 35 | + // Verify all messages are processed |
| 36 | + uint64_t queued = m_queued_ops.load(std::memory_order_acquire); |
| 37 | + if (queued > 0) { |
| 38 | + SPDK_ERRLOG("SpdkContextWQ::~SpdkContextWQ: Warning: %lu operations still pending during destruction\n", queued); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void SpdkContextWQ::queue(Context *ctx, int r) { |
| 43 | + // Check if shutdown is in progress |
| 44 | + if (m_shutdown.load(std::memory_order_acquire)) { |
| 45 | + // ContextWQ is shutting down, complete with error |
| 46 | + SPDK_ERRLOG("SpdkContextWQ::queue: ContextWQ is shutting down, rejecting new operation\n"); |
| 47 | + rbd_context_complete(ctx, -ESHUTDOWN); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Increment queued operations counter |
| 52 | + m_queued_ops.fetch_add(1, std::memory_order_acq_rel); |
| 53 | + |
| 54 | + // Allocate message structure to pass context and return value |
| 55 | + auto msg = new SpdkContextMsg{ctx, r, this}; |
| 56 | + |
| 57 | + // Schedule work on the SPDK reactor thread |
| 58 | + int rc = spdk_thread_send_msg(m_reactor_thread, spdk_msg_handler, msg); |
| 59 | + if (rc != 0) { |
| 60 | + // If message send failed, we need to clean up and complete with error |
| 61 | + m_queued_ops.fetch_sub(1, std::memory_order_acq_rel); |
| 62 | + delete msg; |
| 63 | + // Complete context with error using public API |
| 64 | + SPDK_ERRLOG("SpdkContextWQ::queue: calling rbd_context_complete(ctx=%p, r=%d) on error path\n", ctx, rc); |
| 65 | + rbd_context_complete(ctx, rc); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void SpdkContextWQ::spdk_msg_handler(void *arg) { |
| 70 | + auto msg = static_cast<SpdkContextMsg*>(arg); |
| 71 | + |
| 72 | + if (msg == nullptr) { |
| 73 | + SPDK_ERRLOG("SpdkContextWQ::spdk_msg_handler: FATAL: msg is nullptr\n"); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (msg->wq == nullptr) { |
| 78 | + SPDK_ERRLOG("SpdkContextWQ::spdk_msg_handler: FATAL: msg->wq is nullptr\n"); |
| 79 | + delete msg; |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + if (msg->ctx == nullptr) { |
| 84 | + SPDK_ERRLOG("SpdkContextWQ::spdk_msg_handler: FATAL: msg->ctx is nullptr\n"); |
| 85 | + delete msg; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Execute the context callback on the reactor thread |
| 90 | + rbd_context_complete(msg->ctx, msg->r); |
| 91 | + |
| 92 | + uint64_t queued_ops_before = msg->wq->m_queued_ops.load(std::memory_order_acquire); |
| 93 | + if (queued_ops_before == 0) { |
| 94 | + SPDK_ERRLOG("SpdkContextWQ::spdk_msg_handler: WARNING: m_queued_ops is 0, expected > 0\n"); |
| 95 | + } |
| 96 | + |
| 97 | + // Update queued ops counter |
| 98 | + msg->wq->m_queued_ops.fetch_sub(1, std::memory_order_acq_rel); |
| 99 | + |
| 100 | + // Free the message structure |
| 101 | + delete msg; |
| 102 | +} |
| 103 | + |
| 104 | +void SpdkContextWQ::drain() { |
| 105 | + // Wait for all pending messages to be processed. |
| 106 | + // Note: This relies on the SPDK reactor thread to be actively polling. |
| 107 | + // TODO: conf parameter, non busy wait implementation |
| 108 | + const int max_iterations = 100000; // 10 seconds at 100us per iteration |
| 109 | + int iterations = 0; |
| 110 | + |
| 111 | + // Wait for all queued operations to complete |
| 112 | + while (m_queued_ops.load(std::memory_order_acquire) > 0 && |
| 113 | + iterations < max_iterations) { |
| 114 | + // Yield to allow SPDK reactor thread to process messages |
| 115 | + spdk_delay_us(100); |
| 116 | + ++iterations; |
| 117 | + } |
| 118 | + |
| 119 | + uint64_t queued = m_queued_ops.load(std::memory_order_acquire); |
| 120 | + if (queued > 0) { |
| 121 | + SPDK_ERRLOG("SpdkContextWQ::drain: Incomplete drain - queued_ops=%lu after %d iterations\n", |
| 122 | + queued, iterations); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace asio |
| 127 | +} // namespace librbd |
| 128 | + |
| 129 | +// C API implementation |
| 130 | +extern "C" { |
| 131 | + |
| 132 | +struct bdev_rbd_spdk_context_wq* bdev_rbd_spdk_context_wq_create_from_ioctx(rados_ioctx_t io_ctx, struct spdk_thread* reactor_thread) |
| 133 | +{ |
| 134 | + if (io_ctx == NULL || reactor_thread == NULL) { |
| 135 | + return NULL; |
| 136 | + } |
| 137 | + |
| 138 | + // Convert rados_ioctx_t to librados::IoCtx to get CephContext |
| 139 | + librados::IoCtx ioctx; |
| 140 | + librados::IoCtx::from_rados_ioctx_t(io_ctx, ioctx); |
| 141 | + void* cct_ptr = ioctx.cct(); |
| 142 | + |
| 143 | + if (cct_ptr == NULL) { |
| 144 | + SPDK_ERRLOG("Failed to get CephContext from rados_ioctx_t\n"); |
| 145 | + return NULL; |
| 146 | + } |
| 147 | + |
| 148 | + // Create SpdkContextWQ |
| 149 | + uint64_t thread_id = spdk_thread_get_id(reactor_thread); |
| 150 | + const char *thread_name = spdk_thread_get_name(reactor_thread); |
| 151 | + try { |
| 152 | + auto wq = new librbd::asio::SpdkContextWQ(cct_ptr, reactor_thread); |
| 153 | + // Cast to opaque struct pointer for type safety |
| 154 | + struct bdev_rbd_spdk_context_wq* result = reinterpret_cast<struct bdev_rbd_spdk_context_wq*>(wq); |
| 155 | + SPDK_NOTICELOG("bdev_rbd_spdk_context_wq_create_from_ioctx: Successfully created SpdkContextWQ=%p with reactor thread=%p (id=%lu, name=%s)\n", |
| 156 | + result, reactor_thread, thread_id, thread_name ? thread_name : "NULL"); |
| 157 | + return result; |
| 158 | + } catch (...) { |
| 159 | + SPDK_ERRLOG("bdev_rbd_spdk_context_wq_create_from_ioctx: Failed to create SpdkContextWQ with reactor thread=%p (id=%lu, name=%s)\n", |
| 160 | + reactor_thread, thread_id, thread_name ? thread_name : "NULL"); |
| 161 | + return NULL; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +void bdev_rbd_spdk_context_wq_destroy(struct bdev_rbd_spdk_context_wq* context_wq) |
| 166 | +{ |
| 167 | + if (context_wq == NULL) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + // Cast back to SpdkContextWQ and delete |
| 172 | + auto wq = reinterpret_cast<librbd::asio::SpdkContextWQ*>(context_wq); |
| 173 | + delete wq; |
| 174 | +} |
| 175 | + |
| 176 | +} // extern "C" |
0 commit comments