qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, phrdina@redhat.com, famz@redhat.com,
	Wenchao Xia <xiawenc@linux.vnet.ibm.com>,
	armbru@redhat.com, lcapitulino@redhat.com, stefanha@redhat.com,
	pbonzini@redhat.com, dietmar@proxmox.com
Subject: [Qemu-devel] [PATCH V3 2/9] snapshot: add paired functions for internal snapshot id and name
Date: Thu, 27 Jun 2013 10:41:41 +0800	[thread overview]
Message-ID: <1372300908-7546-3-git-send-email-xiawenc@linux.vnet.ibm.com> (raw)
In-Reply-To: <1372300908-7546-1-git-send-email-xiawenc@linux.vnet.ibm.com>

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 <xiawenc@linux.vnet.ibm.com>
---
 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
-- 
1.7.1

  parent reply	other threads:[~2013-06-26  3:45 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-27  2:41 [Qemu-devel] [PATCH V3 0/9] add internal snapshot support at block device level Wenchao Xia
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 1/9] snapshot: new function bdrv_snapshot_find_by_id_and_name() Wenchao Xia
2013-06-27  2:41 ` Wenchao Xia [this message]
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 3/9] snapshot: distinguish id and name in snapshot delete Wenchao Xia
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 4/9] qmp: add internal snapshot support in qmp_transaction Wenchao Xia
2013-07-04 12:35   ` Stefan Hajnoczi
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 5/9] qmp: add interface blockdev-snapshot-internal-sync Wenchao Xia
2013-07-04 12:37   ` Stefan Hajnoczi
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 6/9] qmp: add interface blockdev-snapshot-delete-internal-sync Wenchao Xia
2013-07-04 12:42   ` Stefan Hajnoczi
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 7/9] hmp: add interface hmp_snapshot_blkdev_internal Wenchao Xia
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 8/9] hmp: add interface hmp_snapshot_delete_blkdev_internal Wenchao Xia
2013-06-27  2:41 ` [Qemu-devel] [PATCH V3 9/9] qemu-iotests: add 056 internal snapshot for block device test case Wenchao Xia
2013-07-03  1:52 ` [Qemu-devel] [PATCH V3 0/9] add internal snapshot support at block device level Wenchao Xia
2013-07-04 12:44   ` Stefan Hajnoczi
2013-07-07 22:38     ` Wenchao Xia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1372300908-7546-3-git-send-email-xiawenc@linux.vnet.ibm.com \
    --to=xiawenc@linux.vnet.ibm.com \
    --cc=armbru@redhat.com \
    --cc=dietmar@proxmox.com \
    --cc=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=phrdina@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).