9ae3a8
From d3ec1ceddceff322c71dae8c6efff7bec166aeb8 Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Wed, 8 Jan 2014 19:43:25 +0000
9ae3a8
Subject: [PATCH 01/37] block: fix backing file segfault
9ae3a8
9ae3a8
Message-id: <1392117622-28812-2-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: 57166
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 01/37] block: fix backing file segfault
9ae3a8
Bugzilla: 748906
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
From: Peter Feiner <peter@gridcentric.ca>
9ae3a8
9ae3a8
When a backing file is opened such that (1) a protocol is directly
9ae3a8
used as the block driver and (2) the block driver has bdrv_file_open,
9ae3a8
bdrv_open_backing_file segfaults. The problem arises because
9ae3a8
bdrv_open_common returns without setting bd->backing_hd->file.
9ae3a8
9ae3a8
To effect (1), you seem to have to use the -F flag in qemu-img. There
9ae3a8
are several block drivers that satisfy (2), such as "file" and "nbd".
9ae3a8
Here are some concrete examples:
9ae3a8
9ae3a8
    #!/bin/bash
9ae3a8
9ae3a8
    echo Test file format
9ae3a8
    ./qemu-img create -f file base.file 1m
9ae3a8
    ./qemu-img create -f qcow2 -F file -o backing_file=base.file\
9ae3a8
        file-overlay.qcow2
9ae3a8
    ./qemu-img convert -O raw file-overlay.qcow2 file-convert.raw
9ae3a8
9ae3a8
    echo Test nbd format
9ae3a8
    SOCK=$PWD/nbd.sock
9ae3a8
    ./qemu-img create -f raw base.raw 1m
9ae3a8
    ./qemu-nbd -t -k $SOCK base.raw &
9ae3a8
    trap "kill $!" EXIT
9ae3a8
    while ! test -e $SOCK; do sleep 1; done
9ae3a8
    ./qemu-img create -f qcow2 -F nbd -o backing_file=nbd:unix:$SOCK\
9ae3a8
        nbd-overlay.qcow2
9ae3a8
    ./qemu-img convert -O raw nbd-overlay.qcow2 nbd-convert.raw
9ae3a8
9ae3a8
Without this patch, the two qemu-img convert commands segfault.
9ae3a8
9ae3a8
This is a regression that was introduced in v1.7 by
9ae3a8
dbecebddfa4932d1c83915bcb9b5ba5984eb91be.
9ae3a8
9ae3a8
Signed-off-by: Peter Feiner <peter@gridcentric.ca>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit d80ac658f2caacfb14ea386211c4a9bea0cea280)
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block.c | 8 ++++++--
9ae3a8
 1 file changed, 6 insertions(+), 2 deletions(-)
9ae3a8
---
9ae3a8
 block.c |    8 ++++++--
9ae3a8
 1 files changed, 6 insertions(+), 2 deletions(-)
9ae3a8
9ae3a8
diff --git a/block.c b/block.c
9ae3a8
index 1433844..df0adf3 100644
9ae3a8
--- a/block.c
9ae3a8
+++ b/block.c
9ae3a8
@@ -978,8 +978,12 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
9ae3a8
         error_propagate(errp, local_err);
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
-    pstrcpy(bs->backing_file, sizeof(bs->backing_file),
9ae3a8
-            bs->backing_hd->file->filename);
9ae3a8
+
9ae3a8
+    if (bs->backing_hd->file) {
9ae3a8
+        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
9ae3a8
+                bs->backing_hd->file->filename);
9ae3a8
+    }
9ae3a8
+
9ae3a8
     return 0;
9ae3a8
 }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8