~ubuntu-branches/ubuntu/trusty/qemu/trusty

« back to all changes in this revision

Viewing changes to .pc/1.6.1.patch/block/stream.c

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2013-10-22 22:47:07 UTC
  • mfrom: (1.8.3) (10.1.42 sid)
  • Revision ID: package-import@ubuntu.com-20131022224707-1lya34fw3k3f24tv
Tags: 1.6.0+dfsg-2ubuntu1
* Merge 1.6.0~rc0+dfsg-2exp from debian experimental.  Remaining changes:
  - debian/control
    * update maintainer
    * remove libiscsi, usb-redir, vde, vnc-jpeg, and libssh2-1-dev
      from build-deps
    * enable rbd
    * add qemu-system and qemu-common B/R to qemu-keymaps
    * add D:udev, R:qemu, R:qemu-common and B:qemu-common to
      qemu-system-common
    * qemu-system-arm, qemu-system-ppc, qemu-system-sparc:
      - add qemu-kvm to Provides
      - add qemu-common, qemu-kvm, kvm to B/R
      - remove openbios-sparc from qemu-system-sparc D
      - drop openbios-ppc and openhackware Depends to Suggests (for now)
    * qemu-system-x86:
      - add qemu-common to Breaks/Replaces.
      - add cpu-checker to Recommends.
    * qemu-user: add B/R:qemu-kvm
    * qemu-kvm:
      - add armhf armel powerpc sparc to Architecture
      - C/R/P: qemu-kvm-spice
    * add qemu-common package
    * drop qemu-slof which is not packaged in ubuntu
  - add qemu-system-common.links for tap ifup/down scripts and OVMF link.
  - qemu-system-x86.links:
    * remove pxe rom links which are in kvm-ipxe
    * add symlink for kvm.1 manpage
  - debian/rules
    * add kvm-spice symlink to qemu-kvm
    * call dh_installmodules for qemu-system-x86
    * update dh_installinit to install upstart script
    * run dh_installman (Closes: #709241) (cherrypicked from 1.5.0+dfsg-2)
  - Add qemu-utils.links for kvm-* symlinks.
  - Add qemu-system-x86.qemu-kvm.upstart and .default
  - Add qemu-system-x86.modprobe to set nesting=1
  - Add qemu-system-common.preinst to add kvm group
  - qemu-system-common.postinst: remove bad group acl if there, then have
    udev relabel /dev/kvm.
  - New linaro patches from qemu-linaro rebasing branch
  - Dropped patches:
    * xen-simplify-xen_enabled.patch
    * sparc-linux-user-fix-missing-symbols-in-.rel-.rela.plt-sections.patch
    * main_loop-do-not-set-nonblocking-if-xen_enabled.patch
    * xen_machine_pv-do-not-create-a-dummy-CPU-in-machine-.patch
    * virtio-rng-fix-crash
  - Kept patches:
    * expose_vms_qemu64cpu.patch - updated
    * linaro arm patches from qemu-linaro rebasing branch
  - New patches:
    * fix-pci-add: change CONFIG variable in ifdef to make sure that
      pci_add is defined.
* Add linaro patches
* Add experimental mach-virt patches for arm virtualization.
* qemu-system-common.install: add debian/tmp/usr/lib to install the
  qemu-bridge-helper

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Image streaming
 
3
 *
 
4
 * Copyright IBM, Corp. 2011
 
5
 *
 
6
 * Authors:
 
7
 *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
 
8
 *
 
9
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
 
10
 * See the COPYING.LIB file in the top-level directory.
 
11
 *
 
12
 */
 
13
 
 
14
#include "trace.h"
 
15
#include "block/block_int.h"
 
16
#include "block/blockjob.h"
 
17
#include "qemu/ratelimit.h"
 
18
 
 
19
enum {
 
20
    /*
 
21
     * Size of data buffer for populating the image file.  This should be large
 
22
     * enough to process multiple clusters in a single call, so that populating
 
23
     * contiguous regions of the image is efficient.
 
24
     */
 
25
    STREAM_BUFFER_SIZE = 512 * 1024, /* in bytes */
 
26
};
 
27
 
 
28
#define SLICE_TIME 100000000ULL /* ns */
 
29
 
 
30
typedef struct StreamBlockJob {
 
31
    BlockJob common;
 
32
    RateLimit limit;
 
33
    BlockDriverState *base;
 
34
    BlockdevOnError on_error;
 
35
    char backing_file_id[1024];
 
36
} StreamBlockJob;
 
37
 
 
38
static int coroutine_fn stream_populate(BlockDriverState *bs,
 
39
                                        int64_t sector_num, int nb_sectors,
 
40
                                        void *buf)
 
41
{
 
42
    struct iovec iov = {
 
43
        .iov_base = buf,
 
44
        .iov_len  = nb_sectors * BDRV_SECTOR_SIZE,
 
45
    };
 
46
    QEMUIOVector qiov;
 
47
 
 
48
    qemu_iovec_init_external(&qiov, &iov, 1);
 
49
 
 
50
    /* Copy-on-read the unallocated clusters */
 
51
    return bdrv_co_copy_on_readv(bs, sector_num, nb_sectors, &qiov);
 
52
}
 
53
 
 
54
static void close_unused_images(BlockDriverState *top, BlockDriverState *base,
 
55
                                const char *base_id)
 
56
{
 
57
    BlockDriverState *intermediate;
 
58
    intermediate = top->backing_hd;
 
59
 
 
60
    while (intermediate) {
 
61
        BlockDriverState *unused;
 
62
 
 
63
        /* reached base */
 
64
        if (intermediate == base) {
 
65
            break;
 
66
        }
 
67
 
 
68
        unused = intermediate;
 
69
        intermediate = intermediate->backing_hd;
 
70
        unused->backing_hd = NULL;
 
71
        bdrv_delete(unused);
 
72
    }
 
73
    top->backing_hd = base;
 
74
}
 
75
 
 
76
static void coroutine_fn stream_run(void *opaque)
 
77
{
 
78
    StreamBlockJob *s = opaque;
 
79
    BlockDriverState *bs = s->common.bs;
 
80
    BlockDriverState *base = s->base;
 
81
    int64_t sector_num, end;
 
82
    int error = 0;
 
83
    int ret = 0;
 
84
    int n = 0;
 
85
    void *buf;
 
86
 
 
87
    s->common.len = bdrv_getlength(bs);
 
88
    if (s->common.len < 0) {
 
89
        block_job_completed(&s->common, s->common.len);
 
90
        return;
 
91
    }
 
92
 
 
93
    end = s->common.len >> BDRV_SECTOR_BITS;
 
94
    buf = qemu_blockalign(bs, STREAM_BUFFER_SIZE);
 
95
 
 
96
    /* Turn on copy-on-read for the whole block device so that guest read
 
97
     * requests help us make progress.  Only do this when copying the entire
 
98
     * backing chain since the copy-on-read operation does not take base into
 
99
     * account.
 
100
     */
 
101
    if (!base) {
 
102
        bdrv_enable_copy_on_read(bs);
 
103
    }
 
104
 
 
105
    for (sector_num = 0; sector_num < end; sector_num += n) {
 
106
        uint64_t delay_ns = 0;
 
107
        bool copy;
 
108
 
 
109
wait:
 
110
        /* Note that even when no rate limit is applied we need to yield
 
111
         * with no pending I/O here so that bdrv_drain_all() returns.
 
112
         */
 
113
        block_job_sleep_ns(&s->common, rt_clock, delay_ns);
 
114
        if (block_job_is_cancelled(&s->common)) {
 
115
            break;
 
116
        }
 
117
 
 
118
        ret = bdrv_co_is_allocated(bs, sector_num,
 
119
                                   STREAM_BUFFER_SIZE / BDRV_SECTOR_SIZE, &n);
 
120
        if (ret == 1) {
 
121
            /* Allocated in the top, no need to copy.  */
 
122
            copy = false;
 
123
        } else {
 
124
            /* Copy if allocated in the intermediate images.  Limit to the
 
125
             * known-unallocated area [sector_num, sector_num+n).  */
 
126
            ret = bdrv_co_is_allocated_above(bs->backing_hd, base,
 
127
                                             sector_num, n, &n);
 
128
 
 
129
            /* Finish early if end of backing file has been reached */
 
130
            if (ret == 0 && n == 0) {
 
131
                n = end - sector_num;
 
132
            }
 
133
 
 
134
            copy = (ret == 1);
 
135
        }
 
136
        trace_stream_one_iteration(s, sector_num, n, ret);
 
137
        if (ret >= 0 && copy) {
 
138
            if (s->common.speed) {
 
139
                delay_ns = ratelimit_calculate_delay(&s->limit, n);
 
140
                if (delay_ns > 0) {
 
141
                    goto wait;
 
142
                }
 
143
            }
 
144
            ret = stream_populate(bs, sector_num, n, buf);
 
145
        }
 
146
        if (ret < 0) {
 
147
            BlockErrorAction action =
 
148
                block_job_error_action(&s->common, s->common.bs, s->on_error,
 
149
                                       true, -ret);
 
150
            if (action == BDRV_ACTION_STOP) {
 
151
                n = 0;
 
152
                continue;
 
153
            }
 
154
            if (error == 0) {
 
155
                error = ret;
 
156
            }
 
157
            if (action == BDRV_ACTION_REPORT) {
 
158
                break;
 
159
            }
 
160
        }
 
161
        ret = 0;
 
162
 
 
163
        /* Publish progress */
 
164
        s->common.offset += n * BDRV_SECTOR_SIZE;
 
165
    }
 
166
 
 
167
    if (!base) {
 
168
        bdrv_disable_copy_on_read(bs);
 
169
    }
 
170
 
 
171
    /* Do not remove the backing file if an error was there but ignored.  */
 
172
    ret = error;
 
173
 
 
174
    if (!block_job_is_cancelled(&s->common) && sector_num == end && ret == 0) {
 
175
        const char *base_id = NULL, *base_fmt = NULL;
 
176
        if (base) {
 
177
            base_id = s->backing_file_id;
 
178
            if (base->drv) {
 
179
                base_fmt = base->drv->format_name;
 
180
            }
 
181
        }
 
182
        ret = bdrv_change_backing_file(bs, base_id, base_fmt);
 
183
        close_unused_images(bs, base, base_id);
 
184
    }
 
185
 
 
186
    qemu_vfree(buf);
 
187
    block_job_completed(&s->common, ret);
 
188
}
 
189
 
 
190
static void stream_set_speed(BlockJob *job, int64_t speed, Error **errp)
 
191
{
 
192
    StreamBlockJob *s = container_of(job, StreamBlockJob, common);
 
193
 
 
194
    if (speed < 0) {
 
195
        error_set(errp, QERR_INVALID_PARAMETER, "speed");
 
196
        return;
 
197
    }
 
198
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 
199
}
 
200
 
 
201
static const BlockJobType stream_job_type = {
 
202
    .instance_size = sizeof(StreamBlockJob),
 
203
    .job_type      = "stream",
 
204
    .set_speed     = stream_set_speed,
 
205
};
 
206
 
 
207
void stream_start(BlockDriverState *bs, BlockDriverState *base,
 
208
                  const char *base_id, int64_t speed,
 
209
                  BlockdevOnError on_error,
 
210
                  BlockDriverCompletionFunc *cb,
 
211
                  void *opaque, Error **errp)
 
212
{
 
213
    StreamBlockJob *s;
 
214
 
 
215
    if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
 
216
         on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
 
217
        !bdrv_iostatus_is_enabled(bs)) {
 
218
        error_set(errp, QERR_INVALID_PARAMETER, "on-error");
 
219
        return;
 
220
    }
 
221
 
 
222
    s = block_job_create(&stream_job_type, bs, speed, cb, opaque, errp);
 
223
    if (!s) {
 
224
        return;
 
225
    }
 
226
 
 
227
    s->base = base;
 
228
    if (base_id) {
 
229
        pstrcpy(s->backing_file_id, sizeof(s->backing_file_id), base_id);
 
230
    }
 
231
 
 
232
    s->on_error = on_error;
 
233
    s->common.co = qemu_coroutine_create(stream_run);
 
234
    trace_stream_start(bs, base, s, s->common.co, opaque);
 
235
    qemu_coroutine_enter(s->common.co, s);
 
236
}