2bc292
From 6d9d86cc4e6149d4c0793e8ceb65dab7535a4561 Mon Sep 17 00:00:00 2001
2bc292
From: Hanna Reitz <hreitz@redhat.com>
2bc292
Date: Fri, 4 Feb 2022 12:10:11 +0100
2bc292
Subject: [PATCH 7/8] block/nbd: Move s->ioc on AioContext change
2bc292
2bc292
RH-Author: Hanna Reitz <hreitz@redhat.com>
2bc292
RH-MergeRequest: 74: block/nbd: Handle AioContext changes
2bc292
RH-Commit: [5/6] b3c1eb21ac70d64fdac6094468a72cfbe50a30a9 (hreitz/qemu-kvm-c-9-s)
2bc292
RH-Bugzilla: 2033626
2bc292
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
2bc292
RH-Acked-by: Eric Blake <eblake@redhat.com>
2bc292
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
2bc292
2bc292
s->ioc must always be attached to the NBD node's AioContext.  If that
2bc292
context changes, s->ioc must be attached to the new context.
2bc292
2bc292
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2033626
2bc292
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2bc292
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2bc292
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
2bc292
(cherry picked from commit e15f3a66c830e3fce99c9d56c493c2f7078a1225)
2bc292
2bc292
Conflict:
2bc292
- block/nbd.c: open_timer was added after the 6.2 release, so we need
2bc292
  not (and cannot) assert it is NULL here.
2bc292
2bc292
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
2bc292
---
2bc292
 block/nbd.c | 41 +++++++++++++++++++++++++++++++++++++++++
2bc292
 1 file changed, 41 insertions(+)
2bc292
2bc292
diff --git a/block/nbd.c b/block/nbd.c
2bc292
index aab20125d8..a3896c7f5f 100644
2bc292
--- a/block/nbd.c
2bc292
+++ b/block/nbd.c
2bc292
@@ -2003,6 +2003,38 @@ static void nbd_cancel_in_flight(BlockDriverState *bs)
2bc292
     nbd_co_establish_connection_cancel(s->conn);
2bc292
 }
2bc292
 
2bc292
+static void nbd_attach_aio_context(BlockDriverState *bs,
2bc292
+                                   AioContext *new_context)
2bc292
+{
2bc292
+    BDRVNBDState *s = bs->opaque;
2bc292
+
2bc292
+    /*
2bc292
+     * The reconnect_delay_timer is scheduled in I/O paths when the
2bc292
+     * connection is lost, to cancel the reconnection attempt after a
2bc292
+     * given time.  Once this attempt is done (successfully or not),
2bc292
+     * nbd_reconnect_attempt() ensures the timer is deleted before the
2bc292
+     * respective I/O request is resumed.
2bc292
+     * Since the AioContext can only be changed when a node is drained,
2bc292
+     * the reconnect_delay_timer cannot be active here.
2bc292
+     */
2bc292
+    assert(!s->reconnect_delay_timer);
2bc292
+
2bc292
+    if (s->ioc) {
2bc292
+        qio_channel_attach_aio_context(s->ioc, new_context);
2bc292
+    }
2bc292
+}
2bc292
+
2bc292
+static void nbd_detach_aio_context(BlockDriverState *bs)
2bc292
+{
2bc292
+    BDRVNBDState *s = bs->opaque;
2bc292
+
2bc292
+    assert(!s->reconnect_delay_timer);
2bc292
+
2bc292
+    if (s->ioc) {
2bc292
+        qio_channel_detach_aio_context(s->ioc);
2bc292
+    }
2bc292
+}
2bc292
+
2bc292
 static BlockDriver bdrv_nbd = {
2bc292
     .format_name                = "nbd",
2bc292
     .protocol_name              = "nbd",
2bc292
@@ -2026,6 +2058,9 @@ static BlockDriver bdrv_nbd = {
2bc292
     .bdrv_dirname               = nbd_dirname,
2bc292
     .strong_runtime_opts        = nbd_strong_runtime_opts,
2bc292
     .bdrv_cancel_in_flight      = nbd_cancel_in_flight,
2bc292
+
2bc292
+    .bdrv_attach_aio_context    = nbd_attach_aio_context,
2bc292
+    .bdrv_detach_aio_context    = nbd_detach_aio_context,
2bc292
 };
2bc292
 
2bc292
 static BlockDriver bdrv_nbd_tcp = {
2bc292
@@ -2051,6 +2086,9 @@ static BlockDriver bdrv_nbd_tcp = {
2bc292
     .bdrv_dirname               = nbd_dirname,
2bc292
     .strong_runtime_opts        = nbd_strong_runtime_opts,
2bc292
     .bdrv_cancel_in_flight      = nbd_cancel_in_flight,
2bc292
+
2bc292
+    .bdrv_attach_aio_context    = nbd_attach_aio_context,
2bc292
+    .bdrv_detach_aio_context    = nbd_detach_aio_context,
2bc292
 };
2bc292
 
2bc292
 static BlockDriver bdrv_nbd_unix = {
2bc292
@@ -2076,6 +2114,9 @@ static BlockDriver bdrv_nbd_unix = {
2bc292
     .bdrv_dirname               = nbd_dirname,
2bc292
     .strong_runtime_opts        = nbd_strong_runtime_opts,
2bc292
     .bdrv_cancel_in_flight      = nbd_cancel_in_flight,
2bc292
+
2bc292
+    .bdrv_attach_aio_context    = nbd_attach_aio_context,
2bc292
+    .bdrv_detach_aio_context    = nbd_detach_aio_context,
2bc292
 };
2bc292
 
2bc292
 static void bdrv_nbd_init(void)
2bc292
-- 
2bc292
2.27.0
2bc292