From nobody Wed Nov 5 12:35:14 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; dkim=fail; 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=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534885337910201.40582831301276; Tue, 21 Aug 2018 14:02:17 -0700 (PDT) Received: from localhost ([::1]:55866 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsDn6-00044G-8H for importer@patchew.org; Tue, 21 Aug 2018 17:02:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45609) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsDli-0003Cc-3F for qemu-devel@nongnu.org; Tue, 21 Aug 2018 17:00:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fsDld-0005h8-Fu for qemu-devel@nongnu.org; Tue, 21 Aug 2018 17:00:49 -0400 Received: from mail-qk0-x230.google.com ([2607:f8b0:400d:c09::230]:45575) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fsDld-0005fg-4M for qemu-devel@nongnu.org; Tue, 21 Aug 2018 17:00:45 -0400 Received: by mail-qk0-x230.google.com with SMTP id z125-v6so2223737qkb.12 for ; Tue, 21 Aug 2018 14:00:44 -0700 (PDT) Received: from salty.br.ibm.com ([32.104.18.202]) by smtp.gmail.com with ESMTPSA id e29-v6sm10139383qte.47.2018.08.21.14.00.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 21 Aug 2018 14:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NLBCf6SA7DwCYPb5Ppt59E5zK+Mr9CTzXdl68lbRUg8=; b=h5KIQ8ClZe2CusJ2ODHosTCujBTij4cTfnNUyA1DIgxLZC5jvLCeH7DisN9JnVnXQh H6YwMcf8v7RxOxpz7dK1BKso4IfZrjjDOirzdNxFRWUVYxmOZb67BpQCawExizOGfyep oWR7kwqltfVQGToucZSrWkz2K65p/7cs8Oi0CxMp8t6ZmwnHCpUBb/eN+ok8bdewWQIi gl9/edbePmgIJGNlspBC6F3P7La9qNkYL+PoEM7pT8bU5iILh5IWgVRjRggdf6YjlUGq hLD2WUXfitWpMNezG8e+GVjETecgvs7IVYLFKFU0Y0Aks7HcjvUI3lYzd+DrAx5smH2B Hzjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NLBCf6SA7DwCYPb5Ppt59E5zK+Mr9CTzXdl68lbRUg8=; b=c3GOVLtS0/h93KknJlUCWlliXp75p1xH8fok/fqM+ENW6C44Gr1UfoCpxou71YzRko sV7KSEIfgnI2e3lq7j85s5t1LeCau2TZY6FxLLblNAl0uVlsbSlZqfuM6lSGaU9TPxcW rHPhbp+8/mBKAvW6dAzsBvlZsYUNH2YHFhkZQWUytwS8Imcb62NNFOyUTgyBz+57Zrrb y8iqQjicYG2+nmBXdZMynR4gDR4O2k7cTM5TkLUFVEJdXmUIEMCDedcSxr3XCAproZEp MTlICbgVzxmErqHgsrsFHlSyABT8yOzVGqfUyD2fmhx+s6ibakS0qFwwrQ/fsSHAYRF1 kZZQ== X-Gm-Message-State: AOUpUlEIFLri8j4gy/ITBDPLGj5foDFG0dM272zz8sPRbSaKn9W2ZQs5 xFqpTeChlQFgVOWX2EstlVZpTMNfGrE= X-Google-Smtp-Source: AA+uWPxnpc1LC3pBJjAURnOsBlJ28g3H16u9KNeymthwCh41z1OzFnD9p/8U+oknlz41YPOaHFR8MQ== X-Received: by 2002:ae9:d885:: with SMTP id u127-v6mr46396151qkf.219.1534885244078; Tue, 21 Aug 2018 14:00:44 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 21 Aug 2018 18:00:23 -0300 Message-Id: <20180821210024.3587-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180821210024.3587-1-danielhb413@gmail.com> References: <20180821210024.3587-1-danielhb413@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c09::230 Subject: [Qemu-devel] [PATCH v1 1/2] block/snapshot.c: eliminate use of ID input in snapshot operations 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, armbru@redhat.com, Daniel Henrique Barboza , dgilbert@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" At this moment, QEMU attempts to create/load/delete snapshots by using either an ID (id_str) or a name. The problem is that the code isn't consistent of whether the entered argument is an ID or a name, causing unexpected behaviors. For example, when creating snapshots via savevm , what happens is that "arg" is treated as both name and id_str. In a guest without snapshots, cre= ate a single snapshot via savevm: (qemu) savevm 0 (qemu) info snapshots List of snapshots present on all disks: ID TAG VM SIZE DATE VM CLOCK -- 0 741M 2018-07-31 13:39:56 00:41:25.313 A snapshot with name "0" is created. ID is hidden from the user, but the ID is a non-zero integer that starts at "1". Thus, this snapshot has id_str=3D1, TAG=3D"0". Creating a second snapshot with arg =3D 1, the first= one is deleted: (qemu) savevm 1 (qemu) info snapshots List of snapshots present on all disks: ID TAG VM SIZE DATE VM CLOCK -- 1 741M 2018-07-31 13:42:14 00:41:55.252 What happened? - when creating the second snapshot, a verification is done inside bdrv_all_delete_snapshot to delete any existing snapshots that matches an string argument. Here, the code calls bdrv_all_delete_snapshot("1", ...); - bdrv_all_delete_snapshot calls bdrv_snapshot_find(..., "1") for each BlockDriverState of the guest. And this is where things goes tilting: bdrv_snapshot_find does a search by both id_str and name. It finds out that there is a snapshot that has id_str =3D 1, stores a reference to the snapshot in the sn_info pointer and then returns match found; - since a match was found, a call to bdrv_snapshot_delete_by_id_or_name() is made. This function ignores the pointer written by bdrv_snapshot_find. Inst= ead, it deletes the snapshot using bdrv_snapshot_delete() calling it first with id_str =3D 1. If it fails to delete, then it calls it again with name =3D 1. - after all that, QEMU creates the new snapshot, that has id_str =3D 1 and name =3D 1. The user is left wondering that happened with the first snapshot created. Similar bugs can be triggered when using loadvm and delvm. Before contemplating discarding the use of ID input in these operations, I've searched the code of what would be the implications. My findings are: - the RBD and Sheepdog drivers don't care. Both uses the 'name' field as key in their logic, making id_str =3D name when appropriate. replay-snapshot.c does not make any special use of id_str; - qcow2 uses id_str as an unique identifier but it is automatically calculated, not being influenced by user input. Other than that, there are no distinguish operations made only with id_str; - in blockdev.c, the delete operation uses a match of both id_str AND name. Given that id_str is either a copy of 'name' or auto-generated, we're fine here. This gives motivation to not consider ID as a valid user input in HMP commands - sticking with 'name' input only is more consistent. To accomplish that, the following changes were made in this patch: - bdrv_snapshot_find() does not match for id_str anymore, only 'name'. The function is called in save_snapshot(), load_snapshot(), bdrv_all_delete_sna= pshot() and bdrv_all_find_snapshot(). This change makes the search function more predictable and does not change the behavior of any underlying code that us= es these affected functions, which are related to HMP (which is fine) and the main loop inside vl.c (which doesn't care about it anyways); - bdrv_all_delete_snapshot() does not call bdrv_snapshot_delete_by_id_or_na= me anymore. Instead, it uses the pointer returned by bdrv_snapshot_find to erase the snapshot with the exact match of id_str an name. This function is called in save_snapshot and hmp_delvm, thus this change produces the intended effect; - documentation changes to reflect the new behavior. I consider this to be an API fix instead of an API change - the user was already creating snapshots using 'name', but now he/she will also enjoy a consistent behavior. Libvirt does not care about this change either, as far as I've tested. Ideally we would get rid of the id_str field entirely, but this would have repercussions on existing snapshots. Another day perhaps. Signed-off-by: Daniel Henrique Barboza --- block/snapshot.c | 5 +++-- hmp-commands.hx | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/block/snapshot.c b/block/snapshot.c index 3218a542df..e371d2243d 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -63,7 +63,7 @@ int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshot= Info *sn_info, } for (i =3D 0; i < nb_sns; i++) { sn =3D &sn_tab[i]; - if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) { + if (!strcmp(sn->name, name)) { *sn_info =3D *sn; ret =3D 0; break; @@ -448,7 +448,8 @@ int bdrv_all_delete_snapshot(const char *name, BlockDri= verState **first_bad_bs, aio_context_acquire(ctx); if (bdrv_can_snapshot(bs) && bdrv_snapshot_find(bs, snapshot, name) >=3D 0) { - ret =3D bdrv_snapshot_delete_by_id_or_name(bs, name, err); + ret =3D bdrv_snapshot_delete(bs, snapshot->id_str, + snapshot->name, err); } aio_context_release(ctx); if (ret < 0) { diff --git a/hmp-commands.hx b/hmp-commands.hx index c1fc747403..e55b889868 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -350,17 +350,17 @@ ETEXI { .name =3D "savevm", .args_type =3D "name:s?", - .params =3D "[tag|id]", - .help =3D "save a VM snapshot. If no tag or id are provided,= a new snapshot is created", + .params =3D "tag", + .help =3D "save a VM snapshot. If no tag is provided, a new = snapshot is created", .cmd =3D hmp_savevm, }, =20 STEXI -@item savevm [@var{tag}|@var{id}] +@item savevm @var{tag} @findex savevm Create a snapshot of the whole virtual machine. If @var{tag} is provided, it is used as human readable identifier. If there is already -a snapshot with the same tag or ID, it is replaced. More info at +a snapshot with the same tag, it is replaced. More info at @ref{vm_snapshots}. ETEXI =20 @@ -368,31 +368,31 @@ ETEXI .name =3D "loadvm", .args_type =3D "name:s", .params =3D "tag|id", - .help =3D "restore a VM snapshot from its tag or id", + .help =3D "restore a VM snapshot from its tag", .cmd =3D hmp_loadvm, .command_completion =3D loadvm_completion, }, =20 STEXI -@item loadvm @var{tag}|@var{id} +@item loadvm @var{tag} @findex loadvm Set the whole virtual machine to the snapshot identified by the tag -@var{tag} or the unique snapshot ID @var{id}. +@var{tag}. ETEXI =20 { .name =3D "delvm", .args_type =3D "name:s", .params =3D "tag|id", - .help =3D "delete a VM snapshot from its tag or id", + .help =3D "delete a VM snapshot from its tag", .cmd =3D hmp_delvm, .command_completion =3D delvm_completion, }, =20 STEXI -@item delvm @var{tag}|@var{id} +@item delvm @var{tag} @findex delvm -Delete the snapshot identified by @var{tag} or @var{id}. +Delete the snapshot identified by @var{tag}. ETEXI =20 { --=20 2.17.1 From nobody Wed Nov 5 12:35:14 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; dkim=fail; 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=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534885433900140.13901295023447; Tue, 21 Aug 2018 14:03:53 -0700 (PDT) Received: from localhost ([::1]:55872 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsDoe-0004x4-VV for importer@patchew.org; Tue, 21 Aug 2018 17:03:52 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45613) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fsDli-0003Ce-4V for qemu-devel@nongnu.org; Tue, 21 Aug 2018 17:00:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fsDlf-0005jZ-1N for qemu-devel@nongnu.org; Tue, 21 Aug 2018 17:00:50 -0400 Received: from mail-qt0-x241.google.com ([2607:f8b0:400d:c0d::241]:36575) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fsDle-0005j3-QD for qemu-devel@nongnu.org; Tue, 21 Aug 2018 17:00:46 -0400 Received: by mail-qt0-x241.google.com with SMTP id t5-v6so21949456qtn.3 for ; Tue, 21 Aug 2018 14:00:46 -0700 (PDT) Received: from salty.br.ibm.com ([32.104.18.202]) by smtp.gmail.com with ESMTPSA id e29-v6sm10139383qte.47.2018.08.21.14.00.44 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 21 Aug 2018 14:00:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bT2r8V74033xk+DGVr172Gu8HP1ZinVaO+w5cMZosb0=; b=iSIZfOjAq4+aSI57/9dmomd24ltIGfidlOEJmKUJlDmgh8rUpsOwrO8jLJvyM4jpW/ nIh52xKsrvocSPg5ujztQ+KvIBKU3g8faBc9i37oIV7HROhTZhk1vi58Y+/XZ9OZE1q+ SO2WAtbrqHoXPSS5B7bdES1Nzzbz9YhJWna/pmMElOGVh8P069lRm1YWEv5oFsSTy0iT t4l+bXwBp2ZaSXrCg5bC0a7UWPYLHaf9HLRq1u0EI4gCztWTqYDODH3dutwK5ltGnoQb vRr7nTfUH5YTBACYGMx/iF/Z9L2TyufP72KkSfS8ixa+FGB0IuJE5De4Dz60TlQQuib8 F8GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bT2r8V74033xk+DGVr172Gu8HP1ZinVaO+w5cMZosb0=; b=Q1Bs8Z1kebUyVl2LNwGfrtfkaA/598XARhcUrXK+f5C/DxuFuIZRGsq57b7eprpTdi jjbzCToRf3oAHAjfKMFZQk7aAqhq+QBOOVwACuCa1q3KulSj/9BuWvEtiSsG6bnMNkHR CVBJY3P6/HkvhoOlwMg3Ue67A6JVd0p3Psd65WO79TZU+epM67ML1u5MBP7M9FjtY4HY JG96/IB28DA7XU8wBRyS4HsT5NSqYbncQskEEc9yeXBRCJHELPu4MySkLnJZVOnW4fxl M65bRYNCaCMRX48NaRbfVc/fubm4hHg1+tD1Q+zOD+KXEBm/h0lhkeU4mrZNhKGa3ztK /vLg== X-Gm-Message-State: AOUpUlFs8Gn8xB2+sbcvXTSTdtgcpPMqnC+Qb4vIRVP3X4hv07acKwBQ QSxjoJQofHMg2SHPNrZKKn5K5Pi5tt4= X-Google-Smtp-Source: AA+uWPxtZRj4m94PuhfBWjVQ/um3lYXVgD9OzyE9c55Ac84IqMYj4L/ps4E4O6+arg7qDS4uiNFI7w== X-Received: by 2002:a0c:fa10:: with SMTP id q16-v6mr48258070qvn.46.1534885246113; Tue, 21 Aug 2018 14:00:46 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 21 Aug 2018 18:00:24 -0300 Message-Id: <20180821210024.3587-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180821210024.3587-1-danielhb413@gmail.com> References: <20180821210024.3587-1-danielhb413@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::241 Subject: [Qemu-devel] [PATCH v1 2/2] qcow2-snapshot: remove redundant find_snapshot_by_id_and_name call 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, armbru@redhat.com, Daniel Henrique Barboza , dgilbert@redhat.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_1 RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In qcow2_snapshot_create there is the following code block: /* Generate an ID */ find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str)); /* Check that the ID is unique */ if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >=3D 0) { return -EEXIST; } find_new_snapshot_id cycles through all snapshots, getting the id_str as an unsigned long int, calculating the max id_max value of all the existing id_strs and writing in the id_str pointer id_max + 1: for(i =3D 0; i < s->nb_snapshots; i++) { sn =3D s->snapshots + i; id =3D strtoul(sn->id_str, NULL, 10); if (id > id_max) id_max =3D id; } snprintf(id_str, id_str_size, "%lu", id_max + 1); Here, sn_info->id_str will have the unique value id_max + 1. Right after that, find_snapshot_by_id_and_name is called with id =3D sn_info->id_str and name =3D NULL. This will cause the function to execute the following: } else if (id) { for (i =3D 0; i < s->nb_snapshots; i++) { if (!strcmp(s->snapshots[i].id_str, id)) { return i; } } } In short, we're searching the existing snapshots to see if sn_info->id_str matches any existing id, right after we set in the previous line a sn_info->id_str value that is already unique. The first code block goes way back to commit 585f8587ad, a 2006 commit from Fabrice Bellard that simply says "new qcow2 disk image format". No more info is provided about this logic in any subsequent commits that moved this code block around. I can't say about the original design, but the current logic is redundant. bdrv_snapshot_create is called in aio_context lock, forbidding any concurrent call to accidentally create a new snapshot between the find_new_snapshot_id and find_snapshot_by_id_and_name calls. What we're ending up doing is to cycle through the snapshots two times for no viable reason. This patch eliminates the redundancy by removing the 'id is unique' check that calls find_snapshot_by_id_and_name. Signed-off-by: Daniel Henrique Barboza --- block/qcow2-snapshot.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index bb6a5b7516..20e8472191 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -358,11 +358,6 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSn= apshotInfo *sn_info) /* Generate an ID */ find_new_snapshot_id(bs, sn_info->id_str, sizeof(sn_info->id_str)); =20 - /* Check that the ID is unique */ - if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >=3D 0) { - return -EEXIST; - } - /* Populate sn with passed data */ sn->id_str =3D g_strdup(sn_info->id_str); sn->name =3D g_strdup(sn_info->name); --=20 2.17.1