From patchwork Tue Dec 13 13:52:24 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 131100 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2A5641007D4 for ; Wed, 14 Dec 2011 00:56:24 +1100 (EST) Received: from localhost ([::1]:60609 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaSq8-00065g-Px for incoming@patchwork.ozlabs.org; Tue, 13 Dec 2011 08:56:16 -0500 Received: from eggs.gnu.org ([140.186.70.92]:55354) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaSp5-0002fj-GH for qemu-devel@nongnu.org; Tue, 13 Dec 2011 08:55:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RaSoz-0004GI-Md for qemu-devel@nongnu.org; Tue, 13 Dec 2011 08:55:11 -0500 Received: from e06smtp12.uk.ibm.com ([195.75.94.108]:59431) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RaSoz-0004Eu-EC for qemu-devel@nongnu.org; Tue, 13 Dec 2011 08:55:05 -0500 Received: from /spool/local by e06smtp12.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 13 Dec 2011 13:55:00 -0000 Received: from d06nrmr1307.portsmouth.uk.ibm.com ([9.149.38.129]) by e06smtp12.uk.ibm.com ([192.168.101.142]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 13 Dec 2011 13:54:46 -0000 Received: from d06av09.portsmouth.uk.ibm.com (d06av09.portsmouth.uk.ibm.com [9.149.37.250]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pBDDsjpP2597080 for ; Tue, 13 Dec 2011 13:54:45 GMT Received: from d06av09.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av09.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pBDDsjJ7010353 for ; Tue, 13 Dec 2011 06:54:45 -0700 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av09.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pBDDsjJQ010350; Tue, 13 Dec 2011 06:54:45 -0700 From: Stefan Hajnoczi To: Date: Tue, 13 Dec 2011 13:52:24 +0000 Message-Id: <1323784351-25531-3-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1323784351-25531-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1323784351-25531-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 11121313-8372-0000-0000-00000114D5DA X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.108 Cc: Kevin Wolf , Marcelo Tosatti , Stefan Hajnoczi , Luiz Capitulino Subject: [Qemu-devel] [PATCH v3 2/9] block: add BlockJob interface for long-running operations 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 Signed-off-by: Stefan Hajnoczi --- block_int.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 83 insertions(+), 0 deletions(-) diff --git a/block_int.h b/block_int.h index 89a860c..bc397c4 100644 --- a/block_int.h +++ b/block_int.h @@ -69,6 +69,36 @@ typedef struct BlockIOBaseValue { uint64_t ios[2]; } BlockIOBaseValue; +typedef void BlockJobCancelFunc(void *opaque); +typedef struct BlockJob BlockJob; +typedef struct BlockJobType { + /** Derived BlockJob struct size */ + size_t instance_size; + + /** String describing the operation, part of query-block-jobs QMP API */ + const char *job_type; + + /** Optional callback for job types that support setting a speed limit */ + int (*set_speed)(BlockJob *job, int64_t value); +} BlockJobType; + +/** + * Long-running operation on a BlockDriverState + */ +struct BlockJob { + const BlockJobType *job_type; + BlockDriverState *bs; + bool cancelled; + + /* These fields are published by the query-block-jobs QMP API */ + int64_t offset; + int64_t len; + int64_t speed; + + BlockDriverCompletionFunc *cb; + void *opaque; +}; + struct BlockDriver { const char *format_name; int instance_size; @@ -270,6 +300,9 @@ struct BlockDriverState { void *private; QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; + + /* long-running background operation */ + BlockJob *job; }; struct BlockDriverAIOCB { @@ -293,4 +326,54 @@ void bdrv_set_io_limits(BlockDriverState *bs, int is_windows_drive(const char *filename); #endif +static inline void *block_job_create(const BlockJobType *job_type, + BlockDriverState *bs, + BlockDriverCompletionFunc *cb, + void *opaque) +{ + BlockJob *job; + + if (bdrv_in_use(bs)) { + return NULL; + } + bdrv_set_in_use(bs, 1); + + job = g_malloc0(job_type->instance_size); + job->job_type = job_type; + job->bs = bs; + job->cb = cb; + job->opaque = opaque; + bs->job = job; + return job; +} + +static inline void block_job_complete(BlockJob *job, int ret) +{ + BlockDriverState *bs = job->bs; + + assert(bs->job == job); + job->cb(job->opaque, ret); + bs->job = NULL; + g_free(job); + bdrv_set_in_use(bs, 0); +} + +static inline int block_job_set_speed(BlockJob *job, int64_t value) +{ + if (!job->job_type->set_speed) { + return -ENOTSUP; + } + return job->job_type->set_speed(job, value); +} + +static inline void block_job_cancel(BlockJob *job) +{ + job->cancelled = true; +} + +static inline bool block_job_is_cancelled(BlockJob *job) +{ + return job->cancelled; +} + #endif /* BLOCK_INT_H */