Skip to contentSkip to conversation

add-cow file format

PATCH In review Dong Xu Wang 1 participant 4 messages May 7, 2012 – Jun 4, 2012 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v9 1/3 add-cow file format

Provide a new file format: add-cow. The usage can be found in add-cow.txt of this patch.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 Makefile.objs          |    1 +
 block.c                |    2 +-
 block.h                |    1 +
 block/add-cow-cache.c  |  193 +++++++++++++++++++++
 block/add-cow.c        |  446 ++++++++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h        |   83 +++++++++
 block_int.h            |    1 +
 docs/specs/add-cow.txt |   68 ++++++++
 qemu-img.c             |   39 +++++
 9 files changed, 833 insertions(+), 1 deletion(-)
 create mode 100644 block/add-cow-cache.c
 create mode 100644 block/add-cow.c
 create mode 100644 block/add-cow.h
 create mode 100644 docs/specs/add-cow.txt
Makefile.objs +1 -0
index 70c5c79..10c5c52 100644
@@ -52,6 +52,7 @@ block-nested-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vv
 block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
 block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-nested-y += qed-check.o
+block-nested-y += add-cow.o add-cow-cache.o
 block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o
 block-nested-y += stream.o
 block-nested-$(CONFIG_WIN32) += raw-win32.o
block.c +1 -1
index 43c794c..206860c 100644
@@ -196,7 +196,7 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
 }
 
 /* check if the path starts with "<protocol>:" */
-static int path_has_protocol(const char *path)
+int path_has_protocol(const char *path)
 {
 #ifdef _WIN32
     if (is_windows_drive(path) ||
block.h +1 -0
index f163e54..f74c79e 100644
@@ -319,6 +319,7 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
 int path_is_absolute(const char *path);
+int path_has_protocol(const char *path);
 void path_combine(char *dest, int dest_size,
                   const char *base_path,
                   const char *filename);
block/add-cow-cache.c new file +193 -0

193 lines not on this page · raw message

block/add-cow.c new file +446 -0

446 lines not on this page · raw message

block/add-cow.h new file +83 -0

83 lines not on this page · raw message

block_int.h +1 -0
index 086832a..83e75ea 100644
@@ -51,6 +51,7 @@
 #define BLOCK_OPT_PREALLOC      "preallocation"
 #define BLOCK_OPT_SUBFMT        "subformat"
 #define BLOCK_OPT_COMPAT_LEVEL  "compat"
+#define BLOCK_OPT_IMAGE_FILE    "image_file"
 
 typedef struct BdrvTrackedRequest BdrvTrackedRequest;
 
docs/specs/add-cow.txt new file +68 -0

68 lines not on this page · raw message

qemu-img.c +39 -0
index 0ae543c..29865b5 100644
@@ -628,7 +628,9 @@ static int img_convert(int argc, char **argv)
     int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
     int progress = 0, flags;
     const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
+    char image_filename[1024], backing_filename[1024];
     BlockDriver *drv, *proto_drv;
+    BlockDriver *image_drv, *backing_drv;
     BlockDriverState **bs = NULL, *out_bs = NULL;
     int64_t total_sectors, nb_sectors, sector_num, bs_offset;
     uint64_t bs_sectors;
@@ -637,6 +639,7 @@ static int img_convert(int argc, char **argv)
     BlockDriverInfo bdi;
     QEMUOptionParameter *param = NULL, *create_options = NULL;
     QEMUOptionParameter *out_baseimg_param;
+    QEMUOptionParameter *image_param = NULL;
     char *options = NULL;
     const char *snapshot_name = NULL;
     float local_progress;
@@ -797,6 +800,7 @@ static int img_convert(int argc, char **argv)
         out_baseimg = out_baseimg_param->value.s;
     }
 
+    image_param = get_option_parameter(param, BLOCK_OPT_SIZE);
     /* Check if compression is supported */
     if (compress) {
         QEMUOptionParameter *encryption =
@@ -828,6 +832,41 @@ static int img_convert(int argc, char **argv)
     }
 
     /* Create the new image */
+
+    if (0 == strcmp(out_fmt, "add-cow")) {
+        image_drv = bdrv_find_format("raw");
+        if (!drv) {
+            ret = -1;
+            goto out;
+        }
+        snprintf(image_filename, sizeof(image_filename),
+            "%s"".ct.raw", out_filename);
+        ret = bdrv_create(image_drv, image_filename, image_param);
+        if (ret < 0) {
+            error_report("%s: error while creating image_file: %s",
+                     image_filename, strerror(-ret));
+            goto out;
+        }
+        set_option_parameter(param, BLOCK_OPT_IMAGE_FILE, image_filename);
+
+        if (!out_baseimg) {
+            backing_drv = bdrv_find_format("qcow2");
+            if (!drv) {
+                ret = -1;
+                goto out;
+            }
+            snprintf(backing_filename, sizeof(backing_filename),
+                "%s"".ct.qcow2", out_filename);
+            ret = bdrv_create(backing_drv, backing_filename, image_param);
+            if (ret < 0) {
+                error_report("%s: error while creating backing_file: %s",
+                         backing_filename, strerror(-ret));
+                goto out;
+            }
+            set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
+                backing_filename);
+        }
+    }
     ret = bdrv_create(drv, out_filename, param);
     if (ret < 0) {
         if (ret == -ENOTSUP) {
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v9 2/3 qemu-iotests: support add-cow file format

Add qemu-iotests test cases for add-cow file format.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 tests/qemu-iotests/017       |    2 +-
 tests/qemu-iotests/018       |    2 +-
 tests/qemu-iotests/019       |    2 +-
 tests/qemu-iotests/020       |    2 +-
 tests/qemu-iotests/check     |    4 ++--
 tests/qemu-iotests/common    |    6 ++++++
 tests/qemu-iotests/common.rc |   19 +++++++++++++++++++
 7 files changed, 31 insertions(+), 6 deletions(-)
tests/qemu-iotests/017 +1 -1
index 66951eb..d31432f 100755
@@ -40,7 +40,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 . ./common.pattern
 
 # Any format supporting backing files
-_supported_fmt qcow qcow2 vmdk qed
+_supported_fmt qcow qcow2 vmdk qed add-cow
 _supported_proto generic
 _supported_os Linux
 
tests/qemu-iotests/018 +1 -1
index bd2db78..aa7444f 100755
@@ -40,7 +40,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 . ./common.pattern
 
 # Any format supporting backing files
-_supported_fmt qcow qcow2 vmdk qed
+_supported_fmt qcow qcow2 vmdk qed add-cow
 _supported_proto generic
 _supported_os Linux
 
tests/qemu-iotests/019 +1 -1
index 8872b30..d938aca 100755
@@ -44,7 +44,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 . ./common.pattern
 
 # Any format supporting backing files
-_supported_fmt qcow qcow2 vmdk qed
+_supported_fmt qcow qcow2 vmdk qed add-cow
 _supported_proto generic
 _supported_os Linux
 
tests/qemu-iotests/020 +1 -1
index 2fb0ff8..3dbb495 100755
@@ -42,7 +42,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 . ./common.pattern
 
 # Any format supporting backing files
-_supported_fmt qcow qcow2 vmdk qed
+_supported_fmt qcow qcow2 vmdk qed add-cow
 _supported_proto generic
 _supported_os Linux
 
tests/qemu-iotests/check +2 -2
index 432732c..8061059 100755
@@ -243,7 +243,7 @@ do
 		echo " - no qualified output"
 		err=true
 	    else
-		if diff -w $seq.out $tmp.out >/dev/null 2>&1
+		if diff -w -I "^Formatting" $seq.out $tmp.out >/dev/null 2>&1
 		then
 		    echo ""
 		    if $err
@@ -255,7 +255,7 @@ do
 		else
 		    echo " - output mismatch (see $seq.out.bad)"
 		    mv $tmp.out $seq.out.bad
-		    $diff -w $seq.out $seq.out.bad
+		    $diff -w -I "^Formatting" $seq.out $seq.out.bad
 		    err=true
 		fi
 	    fi
tests/qemu-iotests/common +6 -0
index eeb70cb..c80d18c 100644
@@ -127,6 +127,7 @@ common options
 check options
     -raw                test raw (default)
     -cow                test cow
+    -add-cow            test add-cow
     -qcow               test qcow
     -qcow2              test qcow2
     -qed                test qed
@@ -162,6 +163,11 @@ testlist options
 	    xpand=false
 	    ;;
 
+    -add-cow)
+        IMGFMT=add-cow
+        xpand=false
+       ;;
+
 	-qcow)
 	    IMGFMT=qcow
 	    xpand=false
tests/qemu-iotests/common.rc +19 -0
index e535874..93595c3 100644
@@ -87,6 +87,18 @@ _make_test_img()
     fi
     if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
         optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
+    elif [ "$IMGFMT" = "add-cow" ]; then
+        local BACKING="$TEST_IMG"".qcow2"
+        local IMG="$TEST_IMG"".raw"
+        if [ "$1" = "-b" ]; then
+            IMG="$IMG"".b"
+            $QEMU_IMG create -f raw $IMG $image_size>/dev/null
+            extra_img_options="-o image_file=$IMG $extra_img_options"
+        else
+            $QEMU_IMG create -f raw $IMG $image_size>/dev/null
+            $QEMU_IMG create -f qcow2 $BACKING $image_size>/dev/null
+            extra_img_options="-o backing_file=$BACKING,image_file=$IMG"
+        fi
     fi
 
     if [ -n "$optstr" ]; then
@@ -114,6 +126,13 @@ _cleanup_test_img()
             rm -f $TEST_DIR/t.$IMGFMT
             rm -f $TEST_DIR/t.$IMGFMT.orig
             rm -f $TEST_DIR/t.$IMGFMT.base
+            if [ "$IMGFMT" = "add-cow" ]; then
+                rm -f $TEST_DIR/t.$IMGFMT.qcow2
+                rm -f $TEST_DIR/t.$IMGFMT.raw
+                rm -f $TEST_DIR/t.$IMGFMT.raw.b
+                rm -f $TEST_DIR/t.$IMGFMT.ct.qcow2
+                rm -f $TEST_DIR/t.$IMGFMT.ct.raw
+            fi
             ;;
 
         rbd)
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH v9 3/3 add-cow: support snapshot_blkdev

In reply to Dong Xu Wang

Raw does not support snapshot_blkdev, we will use add-cow as metadata.

CC Marcelo Tosatti <[email protected]>
CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 blockdev.c              |   31 +++++++++++++++++++++++++++----
 docs/live-block-ops.txt |   10 +++++++++-
 2 files changed, 36 insertions(+), 5 deletions(-)
blockdev.c +27 -4
index d25ffea..b9939d3 100644
@@ -780,15 +780,38 @@ void qmp_transaction(BlockdevActionList *dev_list, Error **errp)
 
         /* create new image w/backing file */
         if (mode != NEW_IMAGE_MODE_EXISTING) {
-            ret = bdrv_img_create(new_image_file, format,
+            if (strcmp(format, "add-cow")) {
+                ret = bdrv_img_create(new_image_file, format,
                                   states->old_bs->filename,
                                   states->old_bs->drv->format_name,
                                   NULL, -1, flags);
-            if (ret) {
-                error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
-                goto delete_and_fail;
+            } else {
+                char image_file[1024];
+                char option[1024];
+                uint64_t size;
+
+                bdrv_get_geometry(states->old_bs, &size);
+                size *= BDRV_SECTOR_SIZE;
+
+                sprintf(image_file, "%s.raw", new_image_file);
+
+                ret = bdrv_img_create(image_file, "raw", NULL,
+                                      NULL, NULL, size, flags);
+                if (ret) {
+                    error_set(errp, QERR_UNDEFINED_ERROR);
+                    return;
+                }
+                sprintf(option, "image_file=%s.raw", new_image_file);
+                ret = bdrv_img_create(new_image_file, format,
+                                      states->old_bs->filename,
+                                      states->old_bs->drv->format_name,
+                                      option, -1, flags);
             }
         }
+        if (ret) {
+            error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file);
+            goto delete_and_fail;
+        }
 
         /* We will manually add the backing_hd field to the bs later */
         states->new_bs = bdrv_new("");
docs/live-block-ops.txt +9 -1
index a257087..c97344b 100644
@@ -2,7 +2,8 @@ LIVE BLOCK OPERATIONS
 =====================
 
 High level description of live block operations. Note these are not
-supported for use with the raw format at the moment.
+supported for use with the raw format at the moment, but we can use
+add-cow as metadata to suport raw format.
 
 Snapshot live merge
 ===================
@@ -56,3 +57,10 @@ into that image. Example:
 (qemu) block_stream ide0-hd0
 
 
+
+Raw is not supported, but we can use add-cow in the 1st step:
+
+(qemu) snapshot_blkdev ide0-hd0 /new-path/disk.img add-cow
+
+It will create a raw file named disk.img.raw, with the same virtual size of
+ide0-hd0 first, and then create disk.img.
Signature · 1 line
1.7.9.5

A message that is not in the archive (sent off-list or not yet received): CAJSP0QV0H+kta+tzk7hvBcXHM3q1OM=ofz26Nc4ziTb_Sb13Hg@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAGrFBshR4f4oJ7zu2+spkZTG-=ov4S=p_HUkpK_dBcv+mwGNbg@mail.gmail.com

A message that is not in the archive (sent off-list or not yet received): CAJSP0QWjfR_8EbgsV-xXFewPc=2syhHPB2BsRA+zMXkwtm_CMw@mail.gmail.com

#6 lore raw

Okay, thanks all of your comments, if no other comments, I will write next version.

Quoted 115 lines · Stefan Hajnoczi
Quoted 1 line · Dong Xu Wang

On Tue, May 29, 2012 at 11:50 PM, Stefan Hajnoczi <[email protected]> wrote:

I thought a bit more about locking.  Because the metadata is simple not much locking is necessary except when fetching new bitmap clusters from the image file into the cache and when populating untouched sectors during data cluster allocation.  Those are the two cases where parallel requests could put the block driver or image file into a bad state if allowed to run without any locking.

Another way of describing the consequences of parallelism:
1. Coroutines must not duplicate the same add-cow bitmap cluster into the cache if they run at the same time.
2. Coroutines must not hold bitmap tables across blocking operations since the cache entry has no reference count and might be evicted from the cache.
3. Coroutines must not allocate the same data cluster simultaneously because untouched head/tail sectors must never race with guest writes.

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
block/add-cow.c new file +8 -0
+static int bdrv_add_cow_truncate(BlockDriverState *bs, int64_t size)
+{
+    BDRVAddCowState *s = bs->opaque;
+    int sector_per_byte = SECTORS_PER_CLUSTER * 8;
+    int ret;
+    int64_t old_image_sector = s->image_hd->total_sectors;
+    int64_t bitmap_size =
+        (size / BDRV_SECTOR_SIZE + sector_per_byte - 1) / sector_per_byte;
Quoted 8 lines
Quoted 5 lines

+
+    ret = bdrv_truncate(bs->file,
+        sizeof(AddCowHeader) + bitmap_size);
+    if (ret < 0) {
+        bdrv_truncate(s->image_hd, old_image_sector * BDRV_SECTOR_SIZE);

Why truncate image_hd on failure?  We never touch the image_hd size on success either.  I think we can just leave it alone.

Quoted 2 lines

That means whether we truncate add-cow fails or not ,we should not never touch image_hd size?

I thought about this more and I think we should truncate image_hd in the success case only.  In order to resize the image we need to resize the cow bitmap and then resize image_hd.  If resizing the add-cow file failed, then we haven't changed the cow bitmap and we don't need to truncate image_hd.  Do you agree with this or have I missed something?

Quoted 53 lines
Quoted 43 lines
Quoted 29 lines
qemu-img.c +25 -0
@@ -828,6 +832,41 @@ static int img_convert(int argc, char **argv)
     }
 
     /* Create the new image */
+
+    if (0 == strcmp(out_fmt, "add-cow")) {
+        image_drv = bdrv_find_format("raw");
+        if (!drv) {
+            ret = -1;
+            goto out;
+        }
+        snprintf(image_filename, sizeof(image_filename),
+            "%s"".ct.raw", out_filename);
+        ret = bdrv_create(image_drv, image_filename, image_param);
+        if (ret < 0) {
+            error_report("%s: error while creating image_file: %s",
+                     image_filename, strerror(-ret));
+            goto out;
+        }
+        set_option_parameter(param, BLOCK_OPT_IMAGE_FILE, image_filename);
+
+        if (!out_baseimg) {
+            backing_drv = bdrv_find_format("qcow2");
+            if (!drv) {
+                ret = -1;
+                goto out;
+            }
+            snprintf(backing_filename, sizeof(backing_filename),
+                "%s"".ct.qcow2", out_filename);

qemu-img.c static int img_convert(int argc, char **argv) · line 869

Quoted 10 lines
qemu-img.c +10 -0
+            ret = bdrv_create(backing_drv, backing_filename, image_param);
+            if (ret < 0) {
+                error_report("%s: error while creating backing_file: %s",
+                         backing_filename, strerror(-ret));
+                goto out;
+            }
+            set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
+                backing_filename);
+        }
+    }

If this diff hunk is dropped then the user needs to manually create the raw file before running qemu-img convert?

Quoted 5 lines

qemu-img convert -O add-cow seems like a very rare case.  I'm not sure we should add special user-friend hacks for this.

I'm not sure I understand why you create a qcow2 file either.

Yes, if we use "qemu-img convert -O add-cow", we should create 2 other files, raw file and qcow2(I just picked  up qcow2, other formats is also okay) file, as image_file and backing_file, without the two files, .add-cow file can not work properly.

Quoted 6 lines

Although it will occour in very rare cases, I wish to pass all qemu-iotests cases, so I added these code.

Do you think these are not necessary? And some qemu-iotests cases are using "convert" operation, If I do not write previous code, these cases will fail. Can I let these cases do not support add-cow?

If a test uses qemu-img convert then it's probably not that interesting for add-cow.  Converting is not a useful operation because add-cow is an "add-on" block driver that adds a feature on top of raw, rather than a format like vmdk or qcow2 which is used to share disk images.  I see why you did this to make qemu-iotests work, but personally I would drop this special case code and skip those tests.

Stefan