From patchwork Sat Jun 8 06:58:01 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 249924 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 177412C02CF for ; Sat, 8 Jun 2013 17:02:30 +1000 (EST) Received: from localhost ([::1]:44247 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlDAR-0004yD-TQ for incoming@patchwork.ozlabs.org; Sat, 08 Jun 2013 03:02:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53495) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlD9I-0003pG-D7 for qemu-devel@nongnu.org; Sat, 08 Jun 2013 03:01:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UlD9H-0000Y1-4p for qemu-devel@nongnu.org; Sat, 08 Jun 2013 03:01:16 -0400 Received: from e23smtp07.au.ibm.com ([202.81.31.140]:34562) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlD9G-0000XW-IJ for qemu-devel@nongnu.org; Sat, 08 Jun 2013 03:01:15 -0400 Received: from /spool/local by e23smtp07.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Sat, 8 Jun 2013 16:50:15 +1000 Received: from d23dlp03.au.ibm.com (202.81.31.214) by e23smtp07.au.ibm.com (202.81.31.204) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Sat, 8 Jun 2013 16:50:14 +1000 Received: from d23relay03.au.ibm.com (d23relay03.au.ibm.com [9.190.235.21]) by d23dlp03.au.ibm.com (Postfix) with ESMTP id DB33D357804E for ; Sat, 8 Jun 2013 17:01:10 +1000 (EST) Received: from d23av03.au.ibm.com (d23av03.au.ibm.com [9.190.234.97]) by d23relay03.au.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r58712aX31064232 for ; Sat, 8 Jun 2013 17:01:02 +1000 Received: from d23av03.au.ibm.com (loopback [127.0.0.1]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r5871Als026582 for ; Sat, 8 Jun 2013 17:01:10 +1000 Received: from RH63Wenchao.localdomain ([9.125.31.72]) by d23av03.au.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r586w9Ix018747; Sat, 8 Jun 2013 17:01:07 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Sat, 8 Jun 2013 14:58:01 +0800 Message-Id: <1370674687-13849-6-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1370674687-13849-1-git-send-email-xiawenc@linux.vnet.ibm.com> References: <1370674687-13849-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13060806-0260-0000-0000-0000031F2251 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 202.81.31.140 Cc: kwolf@redhat.com, phrdina@redhat.com, Wenchao Xia , armbru@redhat.com, lcapitulino@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, dietmar@proxmox.com Subject: [Qemu-devel] [PATCH 05/11] 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 mess them up. 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 mess up. 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 mess up for ID and name. Signed-off-by: Wenchao Xia --- block/qcow2-snapshot.c | 2 +- block/snapshot.c | 33 +++++++++++++++++++++++++++++++++ include/block/snapshot.h | 3 +++ 3 files changed, 37 insertions(+), 1 deletions(-) diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 992a5c8..7108d46 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -284,7 +284,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 0a9af4e..6fa59e0 100644 --- a/block/snapshot.c +++ b/block/snapshot.c @@ -229,3 +229,36 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs, } return -ENOTSUP; } + +/* + * Every internal snapshot have an ID used by qemu block layer, this function + * check whether name used by user mess up with ID. An empty string is also + * invalid. + */ +bool snapshot_name_wellformed(const char *name) +{ + char *p; + /* variable v is used to remove gcc warning of "ignoring return value" and + "set but not used" */ + unsigned long v; + + if (*name == '\0') { + return false; + } + + v = strtoul(name, &p, 10); + v++; + + if (*p == '\0') { + /* Numeric string */ + return false; + } + return true; +} + +/* 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