From nobody Mon Feb 9 17:36:10 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1589369078; cv=none; d=zohomail.com; s=zohoarc; b=S1B1CUIdSAG4VYM3Hk+KLeDGC7sdTKPKzvpSl9Uey/Jyd9OLEgwPTnbszEW8NhZOmGe+Po+T9ZeeDfADyOybZsYVaZB9zkDfC2gHf84w/TCqcNOBjFcdPo+KqyHbHLC15S5Df4c9CoLh7HtynYKWKRk/QUOUjLGYVvSyqemxAa8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589369078; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Y0hMSx+psIQ+rlHFKOJP2UFr6KU9b+WirKBdrbWwhWI=; b=VFgrWZA7bn2IeC5C3MXcJOWJ8WQJHr9htaWNTFy7CSXjrXpNvNCXJriyFM2Dy8mMUwhNEhMfLb+r01PAXBnqWGJgyO8MEk4Q/RkuDtnN8mTGJvGJqPZWRtoSDGFxvyt8Uc4HIHtJgQCoJQd5CW7P9Ec0RAPeE66GMG2gqzEKKec= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1589369078123123.47204349188758; Wed, 13 May 2020 04:24:38 -0700 (PDT) Received: from localhost ([::1]:56460 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jYpV6-0006u9-QP for importer@patchew.org; Wed, 13 May 2020 07:24:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54858) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jYpDr-0002Lx-6o for qemu-devel@nongnu.org; Wed, 13 May 2020 07:06:47 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:55012 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jYpDo-0003Us-S2 for qemu-devel@nongnu.org; Wed, 13 May 2020 07:06:46 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-490-verTINHyN6mOLDoavIHM7Q-1; Wed, 13 May 2020 07:06:42 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8DDA91009610; Wed, 13 May 2020 11:06:40 +0000 (UTC) Received: from localhost (unknown [10.40.193.218]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 202BE1002382; Wed, 13 May 2020 11:06:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589368004; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y0hMSx+psIQ+rlHFKOJP2UFr6KU9b+WirKBdrbWwhWI=; b=BsPkcGoqxmUc9c73j8SUxBokmSieczB227jtYd4GDoZQR3oEsoPzLd0fzC/kB6ts5wze6k FXV+g+Xgj9sVOdb1qNnkBNJwIC4Xc/zhKydlP0BRi9m4Yke+Be0yB4lsnIpAdvHA2TuEPj cUZguw1kSNCdzBLuP/g2KhZP+S+EVLI= X-MC-Unique: verTINHyN6mOLDoavIHM7Q-1 From: Max Reitz To: qemu-block@nongnu.org Subject: [PATCH v4 20/34] raw-format: Split raw_read_options() Date: Wed, 13 May 2020 13:05:30 +0200 Message-Id: <20200513110544.176672-21-mreitz@redhat.com> In-Reply-To: <20200513110544.176672-1-mreitz@redhat.com> References: <20200513110544.176672-1-mreitz@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=205.139.110.120; envelope-from=mreitz@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/13 01:56:38 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Split raw_read_options() into one function that actually just reads the options, and another that applies them. This will allow us to detect whether the user has specified any options before attaching the file child (so we can decide on its role based on the options). Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- block/raw-format.c | 110 ++++++++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 45 deletions(-) diff --git a/block/raw-format.c b/block/raw-format.c index 4b8d4ce8be..824fe70686 100644 --- a/block/raw-format.c +++ b/block/raw-format.c @@ -71,20 +71,13 @@ static QemuOptsList raw_create_opts =3D { } }; =20 -static int raw_read_options(QDict *options, BlockDriverState *bs, - BDRVRawState *s, Error **errp) +static int raw_read_options(QDict *options, uint64_t *offset, bool *has_si= ze, + uint64_t *size, Error **errp) { Error *local_err =3D NULL; QemuOpts *opts =3D NULL; - int64_t real_size =3D 0; int ret; =20 - real_size =3D bdrv_getlength(bs->file->bs); - if (real_size < 0) { - error_setg_errno(errp, -real_size, "Could not get image size"); - return real_size; - } - opts =3D qemu_opts_create(&raw_runtime_opts, NULL, 0, &error_abort); qemu_opts_absorb_qdict(opts, options, &local_err); if (local_err) { @@ -93,64 +86,84 @@ static int raw_read_options(QDict *options, BlockDriver= State *bs, goto end; } =20 - s->offset =3D qemu_opt_get_size(opts, "offset", 0); - if (s->offset > real_size) { - error_setg(errp, "Offset (%" PRIu64 ") cannot be greater than " - "size of the containing file (%" PRId64 ")", - s->offset, real_size); - ret =3D -EINVAL; - goto end; - } + *offset =3D qemu_opt_get_size(opts, "offset", 0); + *has_size =3D qemu_opt_find(opts, "size"); + *size =3D qemu_opt_get_size(opts, "size", 0); =20 - if (qemu_opt_find(opts, "size") !=3D NULL) { - s->size =3D qemu_opt_get_size(opts, "size", 0); - s->has_size =3D true; - } else { - s->has_size =3D false; - s->size =3D real_size - s->offset; + ret =3D 0; +end: + qemu_opts_del(opts); + return ret; +} + +static int raw_apply_options(BlockDriverState *bs, BDRVRawState *s, + uint64_t offset, bool has_size, uint64_t size, + Error **errp) +{ + int64_t real_size =3D 0; + + real_size =3D bdrv_getlength(bs->file->bs); + if (real_size < 0) { + error_setg_errno(errp, -real_size, "Could not get image size"); + return real_size; } =20 /* Check size and offset */ - if ((real_size - s->offset) < s->size) { + if (offset > real_size) { + error_setg(errp, "Offset (%" PRIu64 ") cannot be greater than " + "size of the containing file (%" PRId64 ")", + s->offset, real_size); + return -EINVAL; + } + + if (has_size && (real_size - offset) < size) { error_setg(errp, "The sum of offset (%" PRIu64 ") and size " - "(%" PRIu64 ") has to be smaller or equal to the " - " actual size of the containing file (%" PRId64 ")", - s->offset, s->size, real_size); - ret =3D -EINVAL; - goto end; + "(%" PRIu64 ") has to be smaller or equal to the " + " actual size of the containing file (%" PRId64 ")", + s->offset, s->size, real_size); + return -EINVAL; } =20 /* Make sure size is multiple of BDRV_SECTOR_SIZE to prevent rounding * up and leaking out of the specified area. */ - if (s->has_size && !QEMU_IS_ALIGNED(s->size, BDRV_SECTOR_SIZE)) { + if (has_size && !QEMU_IS_ALIGNED(size, BDRV_SECTOR_SIZE)) { error_setg(errp, "Specified size is not multiple of %llu", - BDRV_SECTOR_SIZE); - ret =3D -EINVAL; - goto end; + BDRV_SECTOR_SIZE); + return -EINVAL; } =20 - ret =3D 0; - -end: + s->offset =3D offset; + s->has_size =3D has_size; + s->size =3D has_size ? size : real_size - offset; =20 - qemu_opts_del(opts); - - return ret; + return 0; } =20 static int raw_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, Error **errp) { + bool has_size; + uint64_t offset, size; + int ret; + assert(reopen_state !=3D NULL); assert(reopen_state->bs !=3D NULL); =20 reopen_state->opaque =3D g_new0(BDRVRawState, 1); =20 - return raw_read_options( - reopen_state->options, - reopen_state->bs, - reopen_state->opaque, - errp); + ret =3D raw_read_options(reopen_state->options, &offset, &has_size, &s= ize, + errp); + if (ret < 0) { + return ret; + } + + ret =3D raw_apply_options(reopen_state->bs, reopen_state->opaque, + offset, has_size, size, errp); + if (ret < 0) { + return ret; + } + + return 0; } =20 static void raw_reopen_commit(BDRVReopenState *state) @@ -426,8 +439,15 @@ static int raw_open(BlockDriverState *bs, QDict *optio= ns, int flags, Error **errp) { BDRVRawState *s =3D bs->opaque; + bool has_size; + uint64_t offset, size; int ret; =20 + ret =3D raw_read_options(options, &offset, &has_size, &size, errp); + if (ret < 0) { + return ret; + } + bs->file =3D bdrv_open_child(NULL, options, "file", bs, &child_file, 0, false, errp); if (!bs->file) { @@ -455,7 +475,7 @@ static int raw_open(BlockDriverState *bs, QDict *option= s, int flags, bs->file->bs->filename); } =20 - ret =3D raw_read_options(options, bs, s, errp); + ret =3D raw_apply_options(bs, s, offset, has_size, size, errp); if (ret < 0) { return ret; } --=20 2.26.2