From nobody Sun May 5 17:56:49 2024 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 1541596445094107.27624991772586; Wed, 7 Nov 2018 05:14:05 -0800 (PST) Received: from localhost ([::1]:48163 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKNel-0002xi-Ps for importer@patchew.org; Wed, 07 Nov 2018 08:14:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50339) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKNbW-0006jt-4d for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gKNbP-0002if-VA for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:39 -0500 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:43818) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gKNbI-0002Aj-WA for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:33 -0500 Received: by mail-qk1-x742.google.com with SMTP id r71so20572525qkr.10 for ; Wed, 07 Nov 2018 05:10:26 -0800 (PST) Received: from localhost.localdomain ([2804:431:f700:ba45:6a73:9179:662c:d5bc]) by smtp.gmail.com with ESMTPSA id i35-v6sm317282qtb.16.2018.11.07.05.10.23 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Nov 2018 05:10:24 -0800 (PST) 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=rQ4Uhds8rZuHTGB0/Bzx1iZk4vKNUl1teRDp7Krr8Hs=; b=ZIHV/YoMXnri4Ltmvy7AuFmTsL3owygQCgggOwg7PX8caJeY8cf9J5K2JBuXLL+K+z eHYUOJAKzMvRFtv3jqrH5Yn7qykgA64sagyfADkHYDG4kF8Kvpyvcda+ijHAIMOrort+ 98DCCk1hPxyxz8inMNM5Y50XIBOTAf7oQ8qypl+wFhbrsFSMIJl2VwHxWB7GH09t2VGa HNYSENZrPLEMW6iOLJoM+1qGcsY83fCxtubFs3DyFM166hYH435xWoaS4au7nPVJiV1G xqzsy3jKnk9XPpdNFxYP1pYA7zbyKz23xZGgARu6Z/5hL6iV1F8blueIjrlPWY5iAqAf O/mg== 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=rQ4Uhds8rZuHTGB0/Bzx1iZk4vKNUl1teRDp7Krr8Hs=; b=cSK3GnUu0rAPDmc1mkq4yApKGzx8AWC5p+6CEd8eKhYgSZ0DekiAbq2R9f9yrlH05l LubnkPzxko79Kmzces9RFZFkd/mlx71T6gOWDoQim9V/KWgVRIDZ+ix6u2aOISY/Q5Tv Te0DlnYUdmD2flqvSj3OqaRPgLKgdWTaJPpJF+Xo/q+PwWqvt9MmlnKx9SzSLC1LWC74 se1suVdyHbQ9HYBaALlWFoqrVs3ji7iAgBvS9vvnkKC/f3qg9JTdjK2r0YdTcL8yTq0f M+oHCQfNs/1idjdOKtIEEFwPADzJXRBPUL6FaBgSS00oQgdWYiYQ/Irop/m+ix/S+fkF sqkw== X-Gm-Message-State: AGRZ1gLJs56FMAgN/XeBeC7NvTTZRBzecFi88t3BcI2eCeF58W2n5pxz ZhuFxWT+38Wq10SAEfw1jVHYBUGSeQo= X-Google-Smtp-Source: AJdET5dYxaIgyW4usG94A2qLlppYRfdkvEW4DBnTjznLF3kb01Zp9XoOiPzt7eDzcg84iTjd6st5dQ== X-Received: by 2002:ac8:21aa:: with SMTP id 39mr158948qty.122.1541596225395; Wed, 07 Nov 2018 05:10:25 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Nov 2018 11:09:58 -0200 Message-Id: <20181107131000.27744-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181107131000.27744-1-danielhb413@gmail.com> References: <20181107131000.27744-1-danielhb413@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::742 Subject: [Qemu-devel] [PATCH for-3.2 v3 1/3] 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) 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. 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 Acked-by: Dr. David Alan Gilbert --- block/snapshot.c | 5 +++-- hmp-commands.hx | 32 ++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 14 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 db0c681f74..4f96a38890 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -350,49 +350,57 @@ 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}. + +Since 3.2, savevm stopped allowing the snapshot id to be set, accepting +only @var{tag} as parameter. ETEXI =20 { .name =3D "loadvm", .args_type =3D "name:s", - .params =3D "tag|id", - .help =3D "restore a VM snapshot from its tag or id", + .params =3D "tag", + .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}. + +Since 3.2, loadvm stopped accepting snapshot id as parameter. 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", + .params =3D "tag", + .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}. + +Since 3.2, delvm stopped deleting snapshots by snapshot id, accepting +only @var{tag} as parameter. ETEXI =20 { --=20 2.17.2 From nobody Sun May 5 17:56:49 2024 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 1541596558617638.8685660111008; Wed, 7 Nov 2018 05:15:58 -0800 (PST) Received: from localhost ([::1]:48175 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKNgZ-00054L-Vy for importer@patchew.org; Wed, 07 Nov 2018 08:15:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50413) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKNbb-0006pc-CK for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gKNba-0002th-3E for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:47 -0500 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]:35496) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gKNbZ-0002BT-B3 for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:45 -0500 Received: by mail-qk1-x741.google.com with SMTP id v68-v6so20648751qka.2 for ; Wed, 07 Nov 2018 05:10:27 -0800 (PST) Received: from localhost.localdomain ([2804:431:f700:ba45:6a73:9179:662c:d5bc]) by smtp.gmail.com with ESMTPSA id i35-v6sm317282qtb.16.2018.11.07.05.10.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Nov 2018 05:10:26 -0800 (PST) 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=TBubUBsR2Xuuqks167Ohlds4cljvN8VUgNwJt2kmpOI=; b=DjpjPf4LK4EMYxqZp+hPdObJ+t7W1bc42VyxhofYfOSIcgCN+9AFKVuEobFxB0KuYf pF0J/rWwVR2ymYaf9HKpB27rur8k3K9CQ9AgHY6AUJXpCK8i14T9IzrOoeSWDa8El3+g 3EUph2e1cKrbBwb8J61TWny7ZbH5SDE7kbgUF6qyAqkIJfWGVl/CDmwaqF3zgzskrCEK anC/6clWennIZj854+/fQHr/w43P1gNDcWQ0yDIMI6LBzBeIGZnq1UaD5y7hcCslaS/e iSdZhATrYrwkM+LrsDPINBEvxhAnoIC3K3xgd5GzdDqtU7HFrfjCx2uQzJ6SBoePoYKs XSEQ== 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=TBubUBsR2Xuuqks167Ohlds4cljvN8VUgNwJt2kmpOI=; b=lZvJV5FA7X2CMN+8/l1tCMbPcKe8HXTkzGQOsB0CCLzsCb/BZSMQlUn0X2tLpQilz+ tOktsfcfaUeyKO+v3gyuDZv4DHKxarnXR2fa6Ww9soAxIzh/G41Pbt9Aa32xA2zDwjLr /MSHhQX8E3cdolkdncHQHlejDP77olqvKfIe5WhSwsqSAdfe5oYnuH5Xd3D2z9GQzR8B OVKpujyl1RSUlqVEMSUs1Grc02352AqAXUBIKC36FZz4SODpUoSmfaA4vtUBmyVeGbVT HaJ4wi7GI9GyxBLOIKomRrdfJpzgePiI2CgdhgZp26vZZyaGei+ZpITrr+I6b8frkwdZ yspg== X-Gm-Message-State: AGRZ1gLGZiwONIUj1qYuijmaJ9UF/Tbq0nQ6R5GZBFkNSxdm8f2ub4ku 3Xbtn8MfAfDzSiOv6DpovzuunYu+Vvc= X-Google-Smtp-Source: AJdET5ch0DRs8qeKkSwt7L0BL/otsprPQ641PMV8ey2RNtj+QtUcVf9Bdnb5MtM1Fo5Fpqb6HGfkyA== X-Received: by 2002:a0c:a1c6:: with SMTP id e64mr150389qva.196.1541596227129; Wed, 07 Nov 2018 05:10:27 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Nov 2018 11:09:59 -0200 Message-Id: <20181107131000.27744-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181107131000.27744-1-danielhb413@gmail.com> References: <20181107131000.27744-1-danielhb413@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::741 Subject: [Qemu-devel] [PATCH for-3.2 v3 2/3] block/snapshot: remove bdrv_snapshot_delete_by_id_or_name 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) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" After the previous patch, the only instance of this function left is inside qemu-img.c. qemu-img is using it inside the 'img_snapshot' function to delete snapshots in the SNAPSHOT_DELETE case, based on a "snapshot_name" string that refers to the tag, not ID, of the QEMUSnapshotInfo struct. This can be verified by checking the SNAPSHOT_CREATE case that comes shortly before SNAPSHOT_DELETE. In that case, the same "snapshot_name" variable is being strcpy to the 'name' field of the QEMUSnapshotInfo struct sn: pstrcpy(sn.name, sizeof(sn.name), snapshot_name); Based on that, it is unlikely that "snapshot_name" might contain an "id" in SNAPSHOT_DELETE. This patch changes SNAPSHOT_DELETE to use snapshot_find() and snapshot_delete() instead of bdrv_snapshot_delete_by_id_or_name. After that, there is no instances left of bdrv_snapshot_delete_by_id_or_name in the code, so it is safe to remove it entirely. Suggested-by: Murilo Opsfelder Araujo Signed-off-by: Daniel Henrique Barboza --- block/snapshot.c | 20 -------------------- include/block/snapshot.h | 3 --- qemu-img.c | 15 +++++++++++---- 3 files changed, 11 insertions(+), 27 deletions(-) diff --git a/block/snapshot.c b/block/snapshot.c index e371d2243d..f2f48f926a 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -301,26 +301,6 @@ int bdrv_snapshot_delete(BlockDriverState *bs, return ret; } =20 -int bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs, - const char *id_or_name, - Error **errp) -{ - int ret; - Error *local_err =3D NULL; - - ret =3D bdrv_snapshot_delete(bs, id_or_name, NULL, &local_err); - if (ret =3D=3D -ENOENT || ret =3D=3D -EINVAL) { - error_free(local_err); - local_err =3D NULL; - ret =3D bdrv_snapshot_delete(bs, NULL, id_or_name, &local_err); - } - - if (ret < 0) { - error_propagate(errp, local_err); - } - return ret; -} - int bdrv_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_info) { diff --git a/include/block/snapshot.h b/include/block/snapshot.h index f73d1094af..b5d5084a12 100644 --- a/include/block/snapshot.h +++ b/include/block/snapshot.h @@ -61,9 +61,6 @@ int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id, const char *name, Error **errp); -int bdrv_snapshot_delete_by_id_or_name(BlockDriverState *bs, - const char *id_or_name, - Error **errp); int bdrv_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_info); int bdrv_snapshot_load_tmp(BlockDriverState *bs, diff --git a/qemu-img.c b/qemu-img.c index 4c96db7ba4..7b4910adcf 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -3121,11 +3121,18 @@ static int img_snapshot(int argc, char **argv) break; =20 case SNAPSHOT_DELETE: - bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); - if (err) { - error_reportf_err(err, "Could not delete snapshot '%s': ", - snapshot_name); + ret =3D bdrv_snapshot_find(bs, &sn, snapshot_name); + if (ret < 0) { + error_report("Could not delete snapshot '%s': snapshot not " + "found", snapshot_name); ret =3D 1; + } else { + ret =3D bdrv_snapshot_delete(bs, sn.id_str, sn.name, &err); + if (ret < 0) { + error_reportf_err(err, "Could not delete snapshot '%s': ", + snapshot_name); + ret =3D 1; + } } break; } --=20 2.17.2 From nobody Sun May 5 17:56:49 2024 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 1541596348949990.1727555976204; Wed, 7 Nov 2018 05:12:28 -0800 (PST) Received: from localhost ([::1]:48138 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKNdD-0007mE-NQ for importer@patchew.org; Wed, 07 Nov 2018 08:12:27 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50460) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gKNbh-0006t3-7H for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gKNbb-0002uu-9I for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:49 -0500 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]:40382) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gKNba-0002Hr-TN for qemu-devel@nongnu.org; Wed, 07 Nov 2018 08:10:47 -0500 Received: by mail-qk1-x743.google.com with SMTP id y16so19417748qki.7 for ; Wed, 07 Nov 2018 05:10:29 -0800 (PST) Received: from localhost.localdomain ([2804:431:f700:ba45:6a73:9179:662c:d5bc]) by smtp.gmail.com with ESMTPSA id i35-v6sm317282qtb.16.2018.11.07.05.10.27 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Nov 2018 05:10:28 -0800 (PST) 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=3URNUhrANwM9pXjlHDma6571zhyjIg+B7Iv4mkmm0nw=; b=Yj2PSzSOg+rnQpgpZNiJsu5SqMEF0wR6RDY1MVP7dlw27/0iPsHQH1yJUrQ+9+GUoS AfrsdH7Coz8wy+Ri6PSB+NDp/TXXMXcVtZhyTPjBCY9y0Mh0s1Wxw/6ZQ2xAPlDQT9l2 49Eko+icV91ajXhnUcit4K3T1nbsZgKyLRUSX2jw2AjEnx8ALXAGLZu/qKpNRAyKlGiZ ZRUkGnMH2aHMkZql4LVRiN3c8YYbToU6GHPchZ+VFGlY+zDNbP8tajnmh2Repfsmu2RE 3vi/QZecai9lH/YNSaORxEOCJM8XnUXIi+17wMGzqm9X+ZMzIphl8ggQYUu9CgJQQbVB G5MA== 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=3URNUhrANwM9pXjlHDma6571zhyjIg+B7Iv4mkmm0nw=; b=byi0+mnO+njn2wHXUjl4R2WR0J9Yat4nyeiMK0IoVIXIy1EMFFOY8eWTqbw4YdHgbG 8HBvev5ap4F1GSpKzPYhSRLCct6uG4Fju+Nm+0xPQbM9XajYbNeRaY2UHhdd5iAVdmT7 gcTBmqPZ5p31gxrkLP25osGmYoSCBd6HMi6Th55vfXCTz/KYxF0mpYEd2OBs9gj4Yxik 2/H6L8dHt/xz0v3pLdjubCPPX+/PmH5Efl07aW6bSv9M7w+xsiFNLIJnI0OOKYKxkuOD ps7orJWrFBsLEa3eeKLpuurDQNAmVni3IsO9JPeEJZbV31r8tM/uNWEfcbeQjY6Smq5R 9qlA== X-Gm-Message-State: AGRZ1gJQ0jrKfV8cWg8CiJXm1sjvRkXKFxvLEFs/Z3Y4NIgWHVy3xuZ6 H2Bn0MrLp8yv3iDZcEb88JVJK0lYfYg= X-Google-Smtp-Source: AJdET5eJJuM4omzbWlCyZQURLOpcGRu4hZ99SfSbblWGMwrm7dBisUwmkLrnLBVa38ULYjjrzOeFdA== X-Received: by 2002:a0c:d5ba:: with SMTP id g55mr139911qvi.37.1541596228851; Wed, 07 Nov 2018 05:10:28 -0800 (PST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Nov 2018 11:10:00 -0200 Message-Id: <20181107131000.27744-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181107131000.27744-1-danielhb413@gmail.com> References: <20181107131000.27744-1-danielhb413@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::743 Subject: [Qemu-devel] [PATCH for-3.2 v3 3/3] 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) 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.2