From patchwork Thu May 8 14:34:43 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 347089 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D75581400A0 for ; Fri, 9 May 2014 00:47:45 +1000 (EST) Received: from localhost ([::1]:47396 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WiPbr-0006cv-IQ for incoming@patchwork.ozlabs.org; Thu, 08 May 2014 10:47:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42090) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WiPZe-0003gY-OT for qemu-devel@nongnu.org; Thu, 08 May 2014 10:45:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WiPZX-0006us-23 for qemu-devel@nongnu.org; Thu, 08 May 2014 10:45:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54839) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WiPZW-0006uo-RV for qemu-devel@nongnu.org; Thu, 08 May 2014 10:45:19 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s48EjEnJ027825 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 8 May 2014 10:45:16 -0400 Received: from localhost (ovpn-112-43.ams2.redhat.com [10.36.112.43]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s48EZVP5022915; Thu, 8 May 2014 10:35:32 -0400 From: Stefan Hajnoczi To: Date: Thu, 8 May 2014 16:34:43 +0200 Message-Id: <1399559698-31900-11-git-send-email-stefanha@redhat.com> In-Reply-To: <1399559698-31900-1-git-send-email-stefanha@redhat.com> References: <1399559698-31900-1-git-send-email-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Paolo Bonzini , Stefan Hajnoczi , Christian Borntraeger Subject: [Qemu-devel] [PATCH v3 10/25] nbd: implement .bdrv_detach/attach_aio_context() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Drop the assumption that we're using the main AioContext. Convert qemu_aio_set_fd_handler() calls to aio_set_fd_handler(). The .bdrv_detach/attach_aio_context() interfaces also need to be implemented to move the socket fd handler from the old to the new AioContext. Acked-by: Paolo Bonzini Signed-off-by: Stefan Hajnoczi --- block/nbd-client.c | 24 ++++++++++++--- block/nbd-client.h | 4 +++ block/nbd.c | 87 +++++++++++++++++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 37 deletions(-) diff --git a/block/nbd-client.c b/block/nbd-client.c index 7d698cb..6e1c97c 100644 --- a/block/nbd-client.c +++ b/block/nbd-client.c @@ -49,7 +49,7 @@ static void nbd_teardown_connection(NbdClientSession *client) shutdown(client->sock, 2); nbd_recv_coroutines_enter_all(client); - qemu_aio_set_fd_handler(client->sock, NULL, NULL, NULL); + nbd_client_session_detach_aio_context(client); closesocket(client->sock); client->sock = -1; } @@ -103,11 +103,14 @@ static int nbd_co_send_request(NbdClientSession *s, struct nbd_request *request, QEMUIOVector *qiov, int offset) { + AioContext *aio_context; int rc, ret; qemu_co_mutex_lock(&s->send_mutex); s->send_coroutine = qemu_coroutine_self(); - qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, nbd_restart_write, s); + aio_context = bdrv_get_aio_context(s->bs); + aio_set_fd_handler(aio_context, s->sock, + nbd_reply_ready, nbd_restart_write, s); if (qiov) { if (!s->is_unix) { socket_set_cork(s->sock, 1); @@ -126,7 +129,7 @@ static int nbd_co_send_request(NbdClientSession *s, } else { rc = nbd_send_request(s->sock, request); } - qemu_aio_set_fd_handler(s->sock, nbd_reply_ready, NULL, s); + aio_set_fd_handler(aio_context, s->sock, nbd_reply_ready, NULL, s); s->send_coroutine = NULL; qemu_co_mutex_unlock(&s->send_mutex); return rc; @@ -335,6 +338,19 @@ int nbd_client_session_co_discard(NbdClientSession *client, int64_t sector_num, } +void nbd_client_session_detach_aio_context(NbdClientSession *client) +{ + aio_set_fd_handler(bdrv_get_aio_context(client->bs), client->sock, + NULL, NULL, NULL); +} + +void nbd_client_session_attach_aio_context(NbdClientSession *client, + AioContext *new_context) +{ + aio_set_fd_handler(new_context, client->sock, + nbd_reply_ready, NULL, client); +} + void nbd_client_session_close(NbdClientSession *client) { struct nbd_request request = { @@ -381,7 +397,7 @@ int nbd_client_session_init(NbdClientSession *client, BlockDriverState *bs, /* Now that we're connected, set the socket to be non-blocking and * kick the reply mechanism. */ qemu_set_nonblock(sock); - qemu_aio_set_fd_handler(sock, nbd_reply_ready, NULL, client); + nbd_client_session_attach_aio_context(client, bdrv_get_aio_context(bs)); logout("Established connection with NBD server\n"); return 0; diff --git a/block/nbd-client.h b/block/nbd-client.h index f2a6337..cd478f3 100644 --- a/block/nbd-client.h +++ b/block/nbd-client.h @@ -47,4 +47,8 @@ int nbd_client_session_co_writev(NbdClientSession *client, int64_t sector_num, int nbd_client_session_co_readv(NbdClientSession *client, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); +void nbd_client_session_detach_aio_context(NbdClientSession *client); +void nbd_client_session_attach_aio_context(NbdClientSession *client, + AioContext *new_context); + #endif /* NBD_CLIENT_H */ diff --git a/block/nbd.c b/block/nbd.c index 613f258..4eda095 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -323,46 +323,67 @@ static int64_t nbd_getlength(BlockDriverState *bs) return s->client.size; } +static void nbd_detach_aio_context(BlockDriverState *bs) +{ + BDRVNBDState *s = bs->opaque; + + nbd_client_session_detach_aio_context(&s->client); +} + +static void nbd_attach_aio_context(BlockDriverState *bs, + AioContext *new_context) +{ + BDRVNBDState *s = bs->opaque; + + nbd_client_session_attach_aio_context(&s->client, new_context); +} + static BlockDriver bdrv_nbd = { - .format_name = "nbd", - .protocol_name = "nbd", - .instance_size = sizeof(BDRVNBDState), - .bdrv_parse_filename = nbd_parse_filename, - .bdrv_file_open = nbd_open, - .bdrv_co_readv = nbd_co_readv, - .bdrv_co_writev = nbd_co_writev, - .bdrv_close = nbd_close, - .bdrv_co_flush_to_os = nbd_co_flush, - .bdrv_co_discard = nbd_co_discard, - .bdrv_getlength = nbd_getlength, + .format_name = "nbd", + .protocol_name = "nbd", + .instance_size = sizeof(BDRVNBDState), + .bdrv_parse_filename = nbd_parse_filename, + .bdrv_file_open = nbd_open, + .bdrv_co_readv = nbd_co_readv, + .bdrv_co_writev = nbd_co_writev, + .bdrv_close = nbd_close, + .bdrv_co_flush_to_os = nbd_co_flush, + .bdrv_co_discard = nbd_co_discard, + .bdrv_getlength = nbd_getlength, + .bdrv_detach_aio_context = nbd_detach_aio_context, + .bdrv_attach_aio_context = nbd_attach_aio_context, }; static BlockDriver bdrv_nbd_tcp = { - .format_name = "nbd", - .protocol_name = "nbd+tcp", - .instance_size = sizeof(BDRVNBDState), - .bdrv_parse_filename = nbd_parse_filename, - .bdrv_file_open = nbd_open, - .bdrv_co_readv = nbd_co_readv, - .bdrv_co_writev = nbd_co_writev, - .bdrv_close = nbd_close, - .bdrv_co_flush_to_os = nbd_co_flush, - .bdrv_co_discard = nbd_co_discard, - .bdrv_getlength = nbd_getlength, + .format_name = "nbd", + .protocol_name = "nbd+tcp", + .instance_size = sizeof(BDRVNBDState), + .bdrv_parse_filename = nbd_parse_filename, + .bdrv_file_open = nbd_open, + .bdrv_co_readv = nbd_co_readv, + .bdrv_co_writev = nbd_co_writev, + .bdrv_close = nbd_close, + .bdrv_co_flush_to_os = nbd_co_flush, + .bdrv_co_discard = nbd_co_discard, + .bdrv_getlength = nbd_getlength, + .bdrv_detach_aio_context = nbd_detach_aio_context, + .bdrv_attach_aio_context = nbd_attach_aio_context, }; static BlockDriver bdrv_nbd_unix = { - .format_name = "nbd", - .protocol_name = "nbd+unix", - .instance_size = sizeof(BDRVNBDState), - .bdrv_parse_filename = nbd_parse_filename, - .bdrv_file_open = nbd_open, - .bdrv_co_readv = nbd_co_readv, - .bdrv_co_writev = nbd_co_writev, - .bdrv_close = nbd_close, - .bdrv_co_flush_to_os = nbd_co_flush, - .bdrv_co_discard = nbd_co_discard, - .bdrv_getlength = nbd_getlength, + .format_name = "nbd", + .protocol_name = "nbd+unix", + .instance_size = sizeof(BDRVNBDState), + .bdrv_parse_filename = nbd_parse_filename, + .bdrv_file_open = nbd_open, + .bdrv_co_readv = nbd_co_readv, + .bdrv_co_writev = nbd_co_writev, + .bdrv_close = nbd_close, + .bdrv_co_flush_to_os = nbd_co_flush, + .bdrv_co_discard = nbd_co_discard, + .bdrv_getlength = nbd_getlength, + .bdrv_detach_aio_context = nbd_detach_aio_context, + .bdrv_attach_aio_context = nbd_attach_aio_context, }; static void bdrv_nbd_init(void)