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