From patchwork Thu Oct 27 15:22:49 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 122166 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 79EAD1007D8 for ; Fri, 28 Oct 2011 02:23:58 +1100 (EST) Received: from localhost ([::1]:54438 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJRo7-0000UH-FB for incoming@patchwork.ozlabs.org; Thu, 27 Oct 2011 11:23:51 -0400 Received: from eggs.gnu.org ([140.186.70.92]:39737) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJRnr-0000GK-Tz for qemu-devel@nongnu.org; Thu, 27 Oct 2011 11:23:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RJRnl-0005I6-9o for qemu-devel@nongnu.org; Thu, 27 Oct 2011 11:23:35 -0400 Received: from mtagate3.uk.ibm.com ([194.196.100.163]:46269) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJRnl-0005Et-0h for qemu-devel@nongnu.org; Thu, 27 Oct 2011 11:23:29 -0400 Received: from d06nrmr1507.portsmouth.uk.ibm.com (d06nrmr1507.portsmouth.uk.ibm.com [9.149.38.233]) by mtagate3.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p9RFNEah012196 for ; Thu, 27 Oct 2011 15:23:14 GMT Received: from d06av03.portsmouth.uk.ibm.com (d06av03.portsmouth.uk.ibm.com [9.149.37.213]) by d06nrmr1507.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p9RFNETk1921232 for ; Thu, 27 Oct 2011 16:23:14 +0100 Received: from d06av03.portsmouth.uk.ibm.com (localhost.localdomain [127.0.0.1]) by d06av03.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p9RFNAw8007776 for ; Thu, 27 Oct 2011 09:23:10 -0600 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av03.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p9RFN9E9007755; Thu, 27 Oct 2011 09:23:09 -0600 From: Stefan Hajnoczi To: Date: Thu, 27 Oct 2011 16:22:49 +0100 Message-Id: <1319728975-6069-3-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7 In-Reply-To: <1319728975-6069-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1319728975-6069-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.163 Cc: Kevin Wolf , Anthony Liguori , Marcelo Tosatti , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 2/8] 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 | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 95 insertions(+), 0 deletions(-) diff --git a/block_int.h b/block_int.h index 8295dae..4da1b74 100644 --- a/block_int.h +++ b/block_int.h @@ -51,6 +51,37 @@ typedef struct AIOPool { BlockDriverAIOCB *free_aiocb; } AIOPool; +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; + + /** Is the block_job_set_speed() rate-limiting interface supported? */ + bool set_speed_supported; +} BlockJobType; + +/** + * Long-running operation on a BlockDriverState + */ +struct BlockJob { + const BlockJobType *job_type; + BlockDriverState *bs; + + /* These fields are published by the query-block-jobs QMP API */ + int64_t offset; + int64_t len; + int64_t speed; + + BlockDriverCompletionFunc *cb; + void *opaque; + BlockJobCancelFunc *cancel_cb; + void *cancel_opaque; +}; + struct BlockDriver { const char *format_name; int instance_size; @@ -228,6 +259,9 @@ struct BlockDriverState { int request_tracking; /* reference count, 0 means off */ QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; + + /* long-running background operation */ + BlockJob *job; }; struct BlockDriverAIOCB { @@ -251,4 +285,65 @@ void qemu_aio_release(void *p); 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); + if (job->cancel_cb) { + job->cancel_cb(job->cancel_opaque); + } else { + 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_supported) { + return -ENOTSUP; + } + job->speed = value; + return 0; +} + +/* + * Caller must wait for the completion callback to be invoked + */ +static inline void block_job_cancel(BlockJob *job, BlockJobCancelFunc *cb, + void *opaque) +{ + assert(!job->cancel_cb); + job->cancel_cb = cb; + job->cancel_opaque = opaque; +} + +static inline bool block_job_is_cancelled(BlockJob *job) +{ + return job->cancel_cb; +} + #endif /* BLOCK_INT_H */