From nobody Sun Feb 8 21:47:34 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.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=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1569595490; cv=none; d=zoho.com; s=zohoarc; b=AIfxUYN8HrJSxXBPPFcY8kzHfH5JFDI7Vme3s2EXtLSaGTkmbPpP+T7qqBlBbiORqL6Ta7lFLwYAE+As4Q4rjg4wu6MivxbnBIG2x1KAbTHYsS1b6fLClRum6ECmz8ihui+OwIjrQB/qC1zmvRyIMPt0QhfpMndffK0EIwLl1yc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569595490; h=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:ARC-Authentication-Results; bh=oI1k/gZGP7JkbsoLvRTVgjDvotwJ9y8i1N/GMnqXq80=; b=BdWfAF66l2qVLyIxDvDTpfS8PW4o97Fe1AS7L1ABTY3yUsZr2N0Ik5tZpJG7H729OA4NOsV0FxfuTb/iO54dBDvJQaZOmBuHhXz7c0yVhXARd/DAzp4WbIl/a4gdEQb3Y465qKUqpK4J20H5Omg8JwVJT3MvZV/cj++oV5VRXf4= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.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 1569595490287547.6999785719029; Fri, 27 Sep 2019 07:44:50 -0700 (PDT) Received: from localhost ([::1]:51880 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iDrUG-0003Fs-BN for importer@patchew.org; Fri, 27 Sep 2019 10:44:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34594) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iDqCu-0003lr-Nm for qemu-devel@nongnu.org; Fri, 27 Sep 2019 09:22:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iDqCt-0000Pb-9c for qemu-devel@nongnu.org; Fri, 27 Sep 2019 09:22:48 -0400 Received: from relay.sw.ru ([185.231.240.75]:49752) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iDqCq-0000IU-Ay; Fri, 27 Sep 2019 09:22:44 -0400 Received: from [10.94.3.0] (helo=kvm.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.92.2) (envelope-from ) id 1iDpHz-0003za-91; Fri, 27 Sep 2019 15:23:59 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v5 8/9] block/qcow2-bitmap: fix and improve qcow2_reopen_bitmaps_rw Date: Fri, 27 Sep 2019 15:23:54 +0300 Message-Id: <20190927122355.7344-9-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190927122355.7344-1-vsementsov@virtuozzo.com> References: <20190927122355.7344-1-vsementsov@virtuozzo.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 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: fam@euphon.net, kwolf@redhat.com, vsementsov@virtuozzo.com, qemu-devel@nongnu.org, mreitz@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" - Correct check for write access to file child, and in correct place (only if we want to write). - Support reopen rw -> rw (which will be used in following commit), for example, !bdrv_dirty_bitmap_readonly() is not a corruption if bitmap is marked IN_USE in the image. - Consider unexpected bitmap as a corruption and check other combinations of in-image and in-RAM bitmaps. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-bitmap.c | 77 +++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c index a636dc50ca..6335d85244 100644 --- a/block/qcow2-bitmap.c +++ b/block/qcow2-bitmap.c @@ -1108,18 +1108,14 @@ int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, E= rror **errp) Qcow2BitmapList *bm_list; Qcow2Bitmap *bm; GSList *ro_dirty_bitmaps =3D NULL; - int ret =3D 0; + int ret =3D -EINVAL; + bool need_header_update =3D false; =20 if (s->nb_bitmaps =3D=3D 0) { /* No bitmaps - nothing to do */ return 0; } =20 - if (!can_write(bs)) { - error_setg(errp, "Can't write to the image on reopening bitmaps rw= "); - return -EINVAL; - } - bm_list =3D bitmap_list_load(bs, s->bitmap_directory_offset, s->bitmap_directory_size, errp); if (bm_list =3D=3D NULL) { @@ -1128,32 +1124,75 @@ int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, E= rror **errp) =20 QSIMPLEQ_FOREACH(bm, bm_list, entry) { BdrvDirtyBitmap *bitmap =3D bdrv_find_dirty_bitmap(bs, bm->name); - if (bitmap =3D=3D NULL) { - continue; - } =20 - if (!bdrv_dirty_bitmap_readonly(bitmap)) { - error_setg(errp, "Bitmap %s was loaded prior to rw-reopen, but= was " - "not marked as readonly. This is a bug, something w= ent " - "wrong. All of the bitmaps may be corrupted", bm->n= ame); - ret =3D -EINVAL; + if (!bitmap) { + error_setg(errp, "Unexpected bitmap '%s' in image '%s'", + bm->name, bs->filename); goto out; } =20 - bm->flags |=3D BME_FLAG_IN_USE; - ro_dirty_bitmaps =3D g_slist_append(ro_dirty_bitmaps, bitmap); + if (!(bm->flags & BME_FLAG_IN_USE)) { + if (!bdrv_dirty_bitmap_readonly(bitmap)) { + error_setg(errp, "Corruption: bitmap '%s' is not marked IN= _USE " + "in the image '%s' and not marked readonly in R= AM", + bm->name, bs->filename); + goto out; + } + if (bdrv_dirty_bitmap_inconsistent(bitmap)) { + error_setg(errp, "Corruption: bitmap '%s' is inconsistent = but " + "is not marked IN_USE in the image '%s'", bm->n= ame, + bs->filename); + goto out; + } + + bm->flags |=3D BME_FLAG_IN_USE; + need_header_update =3D true; + } else { + /* + * What if flags already has BME_FLAG_IN_USE ? + * + * 1. if we are reopening RW -> RW it's OK, of course. + * 2. if we are reopening RO -> RW: + * 2.1 if @bitmap is inconsistent, it's OK. It means that it= was + * inconsistent (IN_USE) when we loaded it + * 2.2 if @bitmap is not inconsistent. This seems to be impo= ssible + * and implies third party interaction. Let's error-out = for + * safety. + */ + if (bdrv_dirty_bitmap_readonly(bitmap) && + !bdrv_dirty_bitmap_inconsistent(bitmap)) + { + error_setg(errp, "Corruption: bitmap '%s' is marked IN_USE= " + "in the image '%s' but it is readonly and " + "consistent in RAM", + bm->name, bs->filename); + goto out; + } + } + + if (bdrv_dirty_bitmap_readonly(bitmap)) { + ro_dirty_bitmaps =3D g_slist_append(ro_dirty_bitmaps, bitmap); + } } =20 - if (ro_dirty_bitmaps !=3D NULL) { + if (need_header_update) { + if (!can_write(bs->file->bs) || !(bs->file->perm & BLK_PERM_WRITE)= ) { + error_setg(errp, "Failed to reopen bitmaps rw: no write access= " + "the protocol file"); + goto out; + } + /* in_use flags must be updated */ ret =3D update_ext_header_and_dir_in_place(bs, bm_list); if (ret < 0) { - error_setg_errno(errp, -ret, "Can't update bitmap directory"); + error_setg_errno(errp, -ret, "Cannot update bitmap directory"); goto out; } - g_slist_foreach(ro_dirty_bitmaps, set_readonly_helper, false); } =20 + g_slist_foreach(ro_dirty_bitmaps, set_readonly_helper, false); + ret =3D 0; + out: g_slist_free(ro_dirty_bitmaps); bitmap_list_free(bm_list); --=20 2.21.0