From nobody Wed Oct 29 17:10:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1526649997110686.118738751666; Fri, 18 May 2018 06:26:37 -0700 (PDT) Received: from localhost ([::1]:38900 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fJfP2-0004Oz-43 for importer@patchew.org; Fri, 18 May 2018 09:26:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59330) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fJfKF-0007yp-Ly for qemu-devel@nongnu.org; Fri, 18 May 2018 09:21:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fJfKE-0005pY-Bt for qemu-devel@nongnu.org; Fri, 18 May 2018 09:21:39 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:36168 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fJfK9-0005lJ-9Y; Fri, 18 May 2018 09:21:33 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E7671859A9; Fri, 18 May 2018 13:21:32 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-117-149.ams2.redhat.com [10.36.117.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9A1AC2026DFD; Fri, 18 May 2018 13:21:31 +0000 (UTC) From: Kevin Wolf To: qemu-block@nongnu.org Date: Fri, 18 May 2018 15:20:41 +0200 Message-Id: <20180518132114.4070-8-kwolf@redhat.com> In-Reply-To: <20180518132114.4070-1-kwolf@redhat.com> References: <20180518132114.4070-1-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 18 May 2018 13:21:32 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Fri, 18 May 2018 13:21:32 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'kwolf@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PATCH v2 07/40] job: Maintain a list of all jobs 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: kwolf@redhat.com, qemu-devel@nongnu.org, jsnow@redhat.com, jcody@redhat.com, armbru@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This moves the job list from BlockJob to Job. Now we can check for duplicate IDs in job_create(). Signed-off-by: Kevin Wolf Reviewed-by: Max Reitz Reviewed-by: John Snow --- include/block/blockjob.h | 3 --- include/qemu/job.h | 19 +++++++++++++++++++ blockjob.c | 46 ++++++++++++++++++++++++--------------------= -- job.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/include/block/blockjob.h b/include/block/blockjob.h index 640e649034..10bd9f7059 100644 --- a/include/block/blockjob.h +++ b/include/block/blockjob.h @@ -105,9 +105,6 @@ typedef struct BlockJob { */ bool deferred_to_main_loop; =20 - /** Element of the list of block jobs */ - QLIST_ENTRY(BlockJob) job_list; - /** Status that is published by the query-block-jobs QMP API */ BlockDeviceIoStatus iostatus; =20 diff --git a/include/qemu/job.h b/include/qemu/job.h index 43dc2e4a7d..bae2b0920c 100644 --- a/include/qemu/job.h +++ b/include/qemu/job.h @@ -27,6 +27,7 @@ #define JOB_H =20 #include "qapi/qapi-types-block-core.h" +#include "qemu/queue.h" =20 typedef struct JobDriver JobDriver; =20 @@ -39,6 +40,9 @@ typedef struct Job { =20 /** The type of this job. */ const JobDriver *driver; + + /** Element of the list of jobs */ + QLIST_ENTRY(Job) job_list; } Job; =20 /** @@ -71,4 +75,19 @@ JobType job_type(const Job *job); /** Returns the enum string for the JobType of a given Job. */ const char *job_type_str(const Job *job); =20 +/** + * Get the next element from the list of block jobs after @job, or the + * first one if @job is %NULL. + * + * Returns the requested job, or %NULL if there are no more jobs left. + */ +Job *job_next(Job *job); + +/** + * Get the job identified by @id (which must not be %NULL). + * + * Returns the requested job, or %NULL if it doesn't exist. + */ +Job *job_get(const char *id); + #endif diff --git a/blockjob.c b/blockjob.c index 430a67ba63..c69b2e7cf5 100644 --- a/blockjob.c +++ b/blockjob.c @@ -129,8 +129,6 @@ struct BlockJobTxn { int refcnt; }; =20 -static QLIST_HEAD(, BlockJob) block_jobs =3D QLIST_HEAD_INITIALIZER(block_= jobs); - /* * The block job API is composed of two categories of functions. * @@ -146,25 +144,34 @@ static QLIST_HEAD(, BlockJob) block_jobs =3D QLIST_HE= AD_INITIALIZER(block_jobs); * blockjob_int.h. */ =20 -BlockJob *block_job_next(BlockJob *job) +static bool is_block_job(Job *job) { - if (!job) { - return QLIST_FIRST(&block_jobs); - } - return QLIST_NEXT(job, job_list); + return job_type(job) =3D=3D JOB_TYPE_BACKUP || + job_type(job) =3D=3D JOB_TYPE_COMMIT || + job_type(job) =3D=3D JOB_TYPE_MIRROR || + job_type(job) =3D=3D JOB_TYPE_STREAM; +} + +BlockJob *block_job_next(BlockJob *bjob) +{ + Job *job =3D bjob ? &bjob->job : NULL; + + do { + job =3D job_next(job); + } while (job && !is_block_job(job)); + + return job ? container_of(job, BlockJob, job) : NULL; } =20 BlockJob *block_job_get(const char *id) { - BlockJob *job; + Job *job =3D job_get(id); =20 - QLIST_FOREACH(job, &block_jobs, job_list) { - if (job->job.id && !strcmp(id, job->job.id)) { - return job; - } + if (job && is_block_job(job)) { + return container_of(job, BlockJob, job); + } else { + return NULL; } - - return NULL; } =20 BlockJobTxn *block_job_txn_new(void) @@ -253,7 +260,6 @@ void block_job_unref(BlockJob *job) assert(job->status =3D=3D BLOCK_JOB_STATUS_NULL); assert(!job->txn); BlockDriverState *bs =3D blk_bs(job->blk); - QLIST_REMOVE(job, job_list); bs->job =3D NULL; block_job_remove_all_bdrv(job); blk_remove_aio_context_notifier(job->blk, @@ -812,7 +818,7 @@ void block_job_cancel_sync_all(void) BlockJob *job; AioContext *aio_context; =20 - while ((job =3D QLIST_FIRST(&block_jobs))) { + while ((job =3D block_job_next(NULL))) { aio_context =3D blk_get_aio_context(job->blk); aio_context_acquire(aio_context); block_job_cancel_sync(job); @@ -942,10 +948,6 @@ void *block_job_create(const char *job_id, const Block= JobDriver *driver, error_setg(errp, "Cannot specify job ID for internal block job= "); return NULL; } - if (block_job_get(job_id)) { - error_setg(errp, "Job ID '%s' already in use", job_id); - return NULL; - } } =20 blk =3D blk_new(perm, shared_perm); @@ -961,6 +963,8 @@ void *block_job_create(const char *job_id, const BlockJ= obDriver *driver, return NULL; } =20 + assert(is_block_job(&job->job)); + job->driver =3D driver; job->blk =3D blk; job->cb =3D cb; @@ -983,8 +987,6 @@ void *block_job_create(const char *job_id, const BlockJ= obDriver *driver, =20 bdrv_op_unblock(bs, BLOCK_OP_TYPE_DATAPLANE, job->blocker); =20 - QLIST_INSERT_HEAD(&block_jobs, job, job_list); - blk_add_aio_context_notifier(blk, block_job_attached_aio_context, block_job_detach_aio_context, job); =20 diff --git a/job.c b/job.c index cfdd008c52..e57303c6bb 100644 --- a/job.c +++ b/job.c @@ -29,6 +29,8 @@ #include "qemu/job.h" #include "qemu/id.h" =20 +static QLIST_HEAD(, Job) jobs =3D QLIST_HEAD_INITIALIZER(jobs); + JobType job_type(const Job *job) { return job->driver->job_type; @@ -39,6 +41,27 @@ const char *job_type_str(const Job *job) return JobType_str(job_type(job)); } =20 +Job *job_next(Job *job) +{ + if (!job) { + return QLIST_FIRST(&jobs); + } + return QLIST_NEXT(job, job_list); +} + +Job *job_get(const char *id) +{ + Job *job; + + QLIST_FOREACH(job, &jobs, job_list) { + if (job->id && !strcmp(id, job->id)) { + return job; + } + } + + return NULL; +} + void *job_create(const char *job_id, const JobDriver *driver, Error **errp) { Job *job; @@ -48,17 +71,25 @@ void *job_create(const char *job_id, const JobDriver *d= river, Error **errp) error_setg(errp, "Invalid job ID '%s'", job_id); return NULL; } + if (job_get(job_id)) { + error_setg(errp, "Job ID '%s' already in use", job_id); + return NULL; + } } =20 job =3D g_malloc0(driver->instance_size); job->driver =3D driver; job->id =3D g_strdup(job_id); =20 + QLIST_INSERT_HEAD(&jobs, job, job_list); + return job; } =20 void job_delete(Job *job) { + QLIST_REMOVE(job, job_list); + g_free(job->id); g_free(job); } --=20 2.13.6