@@ -37,6 +37,9 @@ static AioContext *blk_aiocb_get_aio_context(BlockAIOCB *acb);
/* block backend rehandle timer interval 5s */
#define BLOCK_BACKEND_REHANDLE_TIMER_INTERVAL 5000
+#define BLOCK_BACKEND_REHANDLE_NORMAL 1
+#define BLOCK_BACKEND_REHANDLE_DRAIN_REQUESTED 2
+#define BLOCK_BACKEND_REHANDLE_DRAINED 3
enum BlockIOHangStatus {
BLOCK_IO_HANG_STATUS_NORMAL = 0,
@@ -50,6 +53,8 @@ typedef struct BlockBackendRehandleInfo {
unsigned int in_flight;
QTAILQ_HEAD(, BlkAioEmAIOCB) re_aios;
+
+ int status;
} BlockBackendRehandleInfo;
typedef struct BlockBackendAioNotifier {
@@ -471,6 +476,8 @@ static void blk_delete(BlockBackend *blk)
assert(!blk->refcnt);
assert(!blk->name);
assert(!blk->dev);
+ assert(atomic_read(&blk->reinfo.in_flight) == 0);
+ blk_rehandle_disable(blk);
if (blk->public.throttle_group_member.throttle_state) {
blk_io_limits_disable(blk);
}
@@ -2460,6 +2467,37 @@ static void blk_rehandle_remove_aiocb(BlockBackend *blk, BlkAioEmAIOCB *acb)
atomic_dec(&blk->reinfo.in_flight);
}
+static void blk_rehandle_drain(BlockBackend *blk)
+{
+ if (blk_bs(blk)) {
+ bdrv_drained_begin(blk_bs(blk));
+ BDRV_POLL_WHILE(blk_bs(blk), atomic_read(&blk->reinfo.in_flight) > 0);
+ bdrv_drained_end(blk_bs(blk));
+ }
+}
+
+static bool blk_rehandle_is_paused(BlockBackend *blk)
+{
+ return blk->reinfo.status == BLOCK_BACKEND_REHANDLE_DRAIN_REQUESTED ||
+ blk->reinfo.status == BLOCK_BACKEND_REHANDLE_DRAINED;
+}
+
+static void blk_rehandle_pause(BlockBackend *blk)
+{
+ BlockBackendRehandleInfo *reinfo = &blk->reinfo;
+
+ aio_context_acquire(blk_get_aio_context(blk));
+ if (!reinfo->enable || reinfo->status == BLOCK_BACKEND_REHANDLE_DRAINED) {
+ aio_context_release(blk_get_aio_context(blk));
+ return;
+ }
+
+ reinfo->status = BLOCK_BACKEND_REHANDLE_DRAIN_REQUESTED;
+ blk_rehandle_drain(blk);
+ reinfo->status = BLOCK_BACKEND_REHANDLE_DRAINED;
+ aio_context_release(blk_get_aio_context(blk));
+}
+
static void blk_rehandle_timer_cb(void *opaque)
{
BlockBackend *blk = opaque;
@@ -2559,10 +2597,12 @@ static void blk_rehandle_aio_complete(BlkAioEmAIOCB *acb)
if (acb->has_returned) {
blk_dec_in_flight(acb->rwco.blk);
- need_rehandle = blk_rehandle_aio(acb, &has_timeout);
- if (need_rehandle) {
- blk_rehandle_insert_aiocb(acb->rwco.blk, acb);
- return;
+ if (!blk_rehandle_is_paused(acb->rwco.blk)) {
+ need_rehandle = blk_rehandle_aio(acb, &has_timeout);
+ if (need_rehandle) {
+ blk_rehandle_insert_aiocb(acb->rwco.blk, acb);
+ return;
+ }
}
acb->common.cb(acb->common.opaque, acb->rwco.ret);
@@ -2576,6 +2616,42 @@ static void blk_rehandle_aio_complete(BlkAioEmAIOCB *acb)
}
}
+void blk_rehandle_enable(BlockBackend *blk)
+{
+ BlockBackendRehandleInfo *reinfo = &blk->reinfo;
+
+ aio_context_acquire(blk_get_aio_context(blk));
+ if (reinfo->enable) {
+ aio_context_release(blk_get_aio_context(blk));
+ return;
+ }
+
+ reinfo->ts = aio_timer_new(blk_get_aio_context(blk), QEMU_CLOCK_REALTIME,
+ SCALE_MS, blk_rehandle_timer_cb, blk);
+ reinfo->timer_interval_ms = BLOCK_BACKEND_REHANDLE_TIMER_INTERVAL;
+ reinfo->status = BLOCK_BACKEND_REHANDLE_NORMAL;
+ reinfo->enable = true;
+ aio_context_release(blk_get_aio_context(blk));
+}
+
+void blk_rehandle_disable(BlockBackend *blk)
+{
+ if (!blk->reinfo.enable) {
+ return;
+ }
+
+ blk_rehandle_pause(blk);
+ timer_del(blk->reinfo.ts);
+ timer_free(blk->reinfo.ts);
+ blk->reinfo.ts = NULL;
+ blk->reinfo.enable = false;
+}
+
+bool blk_iohang_is_enabled(BlockBackend *blk)
+{
+ return blk->iohang_timeout != 0;
+}
+
void blk_iohang_init(BlockBackend *blk, int64_t iohang_timeout)
{
if (!blk) {
@@ -2588,6 +2664,7 @@ void blk_iohang_init(BlockBackend *blk, int64_t iohang_timeout)
blk->iohang_status = BLOCK_IO_HANG_STATUS_NORMAL;
if (iohang_timeout > 0) {
blk->iohang_timeout = iohang_timeout;
+ blk_rehandle_enable(blk);
}
}
@@ -268,6 +268,9 @@ const BdrvChild *blk_root(BlockBackend *blk);
int blk_make_empty(BlockBackend *blk, Error **errp);
+void blk_rehandle_enable(BlockBackend *blk);
+void blk_rehandle_disable(BlockBackend *blk);
+bool blk_iohang_is_enabled(BlockBackend *blk);
void blk_iohang_init(BlockBackend *blk, int64_t iohang_timeout);
#endif