From patchwork Mon Jul 8 08:00:44 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 257488 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 29BCC2C03B4 for ; Mon, 8 Jul 2013 18:06:15 +1000 (EST) Received: from localhost ([::1]:40111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uw6Sa-00032j-DU for incoming@patchwork.ozlabs.org; Mon, 08 Jul 2013 04:06:12 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56630) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uw6R5-0001W1-D4 for qemu-devel@nongnu.org; Mon, 08 Jul 2013 04:04:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uw6R4-0008QJ-6G for qemu-devel@nongnu.org; Mon, 08 Jul 2013 04:04:39 -0400 Received: from e28smtp08.in.ibm.com ([122.248.162.8]:37904) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uw6R3-0008QC-K6 for qemu-devel@nongnu.org; Mon, 08 Jul 2013 04:04:38 -0400 Received: from /spool/local by e28smtp08.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 8 Jul 2013 13:25:22 +0530 Received: from d28dlp02.in.ibm.com (9.184.220.127) by e28smtp08.in.ibm.com (192.168.1.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 8 Jul 2013 13:25:22 +0530 Received: from d28relay02.in.ibm.com (d28relay02.in.ibm.com [9.184.220.59]) by d28dlp02.in.ibm.com (Postfix) with ESMTP id 993473940043 for ; Mon, 8 Jul 2013 13:34:30 +0530 (IST) Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r68854Vf21168348 for ; Mon, 8 Jul 2013 13:35:05 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r6884SUZ006328 for ; Mon, 8 Jul 2013 18:04:29 +1000 Received: from RH63Wenchao.localdomain ([9.77.178.89]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r6882QcE029794; Mon, 8 Jul 2013 18:04:25 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Mon, 8 Jul 2013 16:00:44 +0800 Message-Id: <1373270451-18436-3-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1373270451-18436-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1373270451-18436-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13070807-2000-0000-0000-00000CCBD946 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.8 Cc: kwolf@redhat.com, phrdina@redhat.com, famz@redhat.com, Wenchao Xia , armbru@redhat.com, lcapitulino@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, dietmar@proxmox.com Subject: [Qemu-devel] [PATCH V4 2/9] snapshot: add paired functions for internal snapshot id and name X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Internal snapshot's ID and name concept are both visible in general block level, they are observed by user in "info snapshots", so it is possible to have conflict. Although we can separate the two concept in programming, but if they can be distinguished in string itself, things will be simple and clear, so introduce two functions to do it. The implemention, qcow2 snapshot calls snapshot_id_string_generate() to make sure it follows the rule in driver. If caller or user give a check with snapshot_name_wellformed() before create snapshot, then the ID and name will never conflict. The check can be also taken in qcow2_snapshot_create(), but require it to return error reason. For rbd, it have no ID, so have no impact. For sheepdog, ID can't be determined in qemu, so still can't guarantee that no more conflict for ID and name. Signed-off-by: Wenchao Xia --- block/qcow2-snapshot.c | 2 +- block/snapshot.c | 21 +++++++++++++++++++++ include/block/snapshot.h | 3 +++ 3 files changed, 25 insertions(+), 1 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 0caac90..4f9c524 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -285,7 +285,7 @@ static void find_new_snapshot_id(BlockDriverState *bs, if (id > id_max) id_max = id; } - snprintf(id_str, id_str_size, "%d", id_max + 1); + snapshot_id_string_generate(id_max + 1, id_str, id_str_size); } static int find_snapshot_by_id(BlockDriverState *bs, const char *id_str) diff --git a/block/snapshot.c b/block/snapshot.c index 481a3ee..40e0cc7 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -228,3 +228,24 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs, } return -ENOTSUP; } + +/* + * Return true if the given internal snapshot name is valid, false + * otherwise. + * + * To prevent clashes with internal snapshot IDs, names consisting only + * of digits are rejected. Empty strings are also rejected. + */ +bool snapshot_name_wellformed(const char *name) +{ + return strspn(name, "0123456789") != strlen(name); +} + +/* + * Following function generate id string, used by block driver, such as qcow2. + * Since no better place to go, place the funtion here for now. + */ +void snapshot_id_string_generate(int id, char *id_str, int id_str_size) +{ + snprintf(id_str, id_str_size, "%d", id); +} diff --git a/include/block/snapshot.h b/include/block/snapshot.h index 9d06dc1..3d93719 100644 --- a/include/block/snapshot.h +++ b/include/block/snapshot.h @@ -56,4 +56,7 @@ int bdrv_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_info); int bdrv_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name); + +bool snapshot_name_wellformed(const char *name); +void snapshot_id_string_generate(int id, char *id_str, int id_str_size); #endif