From patchwork Wed Jun 22 12:25:01 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 9192721 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2442960756 for ; Wed, 22 Jun 2016 12:58:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1335920072 for ; Wed, 22 Jun 2016 12:58:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 06A5F283FD; Wed, 22 Jun 2016 12:58:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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 mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6B13120072 for ; Wed, 22 Jun 2016 12:58:10 +0000 (UTC) Received: from localhost ([::1]:58062 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFhjN-0001tW-Ff for patchwork-qemu-devel@patchwork.kernel.org; Wed, 22 Jun 2016 08:58:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50048) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFhEH-0003b0-0d for qemu-devel@nongnu.org; Wed, 22 Jun 2016 08:26:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bFhE9-0001xI-OR for qemu-devel@nongnu.org; Wed, 22 Jun 2016 08:26:00 -0400 Received: from smtp3.mundo-r.com ([212.51.32.191]:26461 helo=smtp4.mundo-r.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bFhE9-0001we-7w; Wed, 22 Jun 2016 08:25:53 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2DJBgCmgmpX/5tjdVtUCh0BgyCBU6VYAQEBAQEBBQGBDwGRMYIPgXqGFwKBLDkTAQEBAQEBAWUnhE0CBCdSED8SPBsZiDQBwwoBAQgnhV+CR4ZmhgMFiBSFZHCKFY4tiUaFXUiPNCACMoIIHIFPa4pxAQEB X-IPAS-Result: A2DJBgCmgmpX/5tjdVtUCh0BgyCBU6VYAQEBAQEBBQGBDwGRMYIPgXqGFwKBLDkTAQEBAQEBAWUnhE0CBCdSED8SPBsZiDQBwwoBAQgnhV+CR4ZmhgMFiBSFZHCKFY4tiUaFXUiPNCACMoIIHIFPa4pxAQEB X-IronPort-AV: E=Sophos;i="5.26,509,1459807200"; d="scan'208";a="76201815" Received: from fanzine.igalia.com ([91.117.99.155]) by smtp4.mundo-r.com with ESMTP; 22 Jun 2016 14:25:48 +0200 Received: from [194.100.51.2] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim) id 1bFhE4-0004kV-7E; Wed, 22 Jun 2016 14:25:48 +0200 Received: from berto by perseus.local with local (Exim 4.87) (envelope-from ) id 1bFhDo-0007FN-Db; Wed, 22 Jun 2016 15:25:32 +0300 From: Alberto Garcia To: qemu-devel@nongnu.org Date: Wed, 22 Jun 2016 15:25:01 +0300 Message-Id: <3952362ff5a519e20e7b18bda8e861cd5d69142b.1466598035.git.berto@igalia.com> X-Mailer: git-send-email 2.8.1 In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 212.51.32.191 Subject: [Qemu-devel] [PATCH v2 04/15] block: Simplify find_block_job() and make it accept a job ID X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Jeff Cody , Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP find_block_job() looks for a block backend with a specified name, checks whether it has a block job and acquires its AioContext. This patch uses block_job_next() and iterate directly over the block jobs. In addition to that we want to identify jobs primarily by their ID, so this patch updates find_block_job() to allow IDs too. Only one of ID and device name can be specified when looking for a block job. Signed-off-by: Alberto Garcia --- blockdev.c | 77 +++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 33 deletions(-) diff --git a/blockdev.c b/blockdev.c index 3a104a0..ffb4383 100644 --- a/blockdev.c +++ b/blockdev.c @@ -3704,48 +3704,59 @@ void qmp_blockdev_mirror(const char *device, const char *target, aio_context_release(aio_context); } -/* Get the block job for a given device name and acquire its AioContext */ -static BlockJob *find_block_job(const char *device, AioContext **aio_context, - Error **errp) +/* Get a block job using its ID or device name and acquire its AioContext. + * Only one of ID and device name can be specified. */ +static BlockJob *find_block_job(const char *id, const char *device, + AioContext **aio_context, Error **errp) { - BlockBackend *blk; - BlockDriverState *bs; + BlockJob *job = NULL; *aio_context = NULL; - blk = blk_by_name(device); - if (!blk) { - goto notfound; + if ((id && device) || (!id && !device)) { + error_setg(errp, "Only one of ID or device name " + "must be specified when looking for a block job"); + return NULL; } - *aio_context = blk_get_aio_context(blk); + if (id) { + job = block_job_get(id); + } else { + BlockJob *j; + for (j = block_job_next(NULL); j; j = block_job_next(j)) { + if (!strcmp(device, j->device)) { + if (job) { + /* This scenario is currently not possible, but it + * could theoretically happen in the future. */ + error_setg(errp, "More than one job on device '%s', " + "use the job ID instead", device); + return NULL; + } + job = j; + } + } + } + + if (!job) { + if (id) { + error_setg(errp, "Block job '%s' not found", id); + } else { + error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, + "No active block job on device '%s'", device); + } + return NULL; + } + + *aio_context = blk_get_aio_context(job->blk); aio_context_acquire(*aio_context); - if (!blk_is_available(blk)) { - goto notfound; - } - bs = blk_bs(blk); - - if (!bs->job) { - goto notfound; - } - - return bs->job; - -notfound: - error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, - "No active block job on device '%s'", device); - if (*aio_context) { - aio_context_release(*aio_context); - *aio_context = NULL; - } - return NULL; + return job; } void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) { AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(NULL, device, &aio_context, errp); if (!job) { return; @@ -3759,7 +3770,7 @@ void qmp_block_job_cancel(const char *device, bool has_force, bool force, Error **errp) { AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(NULL, device, &aio_context, errp); if (!job) { return; @@ -3784,7 +3795,7 @@ out: void qmp_block_job_pause(const char *device, Error **errp) { AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(NULL, device, &aio_context, errp); if (!job || job->user_paused) { return; @@ -3799,7 +3810,7 @@ void qmp_block_job_pause(const char *device, Error **errp) void qmp_block_job_resume(const char *device, Error **errp) { AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(NULL, device, &aio_context, errp); if (!job || !job->user_paused) { return; @@ -3815,7 +3826,7 @@ void qmp_block_job_resume(const char *device, Error **errp) void qmp_block_job_complete(const char *device, Error **errp) { AioContext *aio_context; - BlockJob *job = find_block_job(device, &aio_context, errp); + BlockJob *job = find_block_job(NULL, device, &aio_context, errp); if (!job) { return;