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