4 * Copyright Red Hat, Inc. 2012
7 * Jeff Cody <jcody@redhat.com>
8 * Based on stream.c by Stefan Hajnoczi
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
17 #include "block/block_int.h"
18 #include "block/blockjob.h"
19 #include "qapi/error.h"
20 #include "qapi/qmp/qerror.h"
21 #include "qemu/ratelimit.h"
22 #include "sysemu/block-backend.h"
26 * Size of data buffer for populating the image file. This should be large
27 * enough to process multiple clusters in a single call, so that populating
28 * contiguous regions of the image is efficient.
30 COMMIT_BUFFER_SIZE
= 512 * 1024, /* in bytes */
33 #define SLICE_TIME 100000000ULL /* ns */
35 typedef struct CommitBlockJob
{
38 BlockDriverState
*active
;
41 BlockdevOnError on_error
;
43 int orig_overlay_flags
;
44 char *backing_file_str
;
47 static int coroutine_fn
commit_populate(BlockBackend
*bs
, BlockBackend
*base
,
48 int64_t sector_num
, int nb_sectors
,
55 .iov_len
= nb_sectors
* BDRV_SECTOR_SIZE
,
58 qemu_iovec_init_external(&qiov
, &iov
, 1);
60 ret
= blk_co_preadv(bs
, sector_num
* BDRV_SECTOR_SIZE
,
66 ret
= blk_co_pwritev(base
, sector_num
* BDRV_SECTOR_SIZE
,
79 static void commit_complete(BlockJob
*job
, void *opaque
)
81 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
);
82 CommitCompleteData
*data
= opaque
;
83 BlockDriverState
*active
= s
->active
;
84 BlockDriverState
*top
= blk_bs(s
->top
);
85 BlockDriverState
*base
= blk_bs(s
->base
);
86 BlockDriverState
*overlay_bs
;
89 if (!block_job_is_cancelled(&s
->common
) && ret
== 0) {
91 ret
= bdrv_drop_intermediate(active
, top
, base
, s
->backing_file_str
);
94 /* restore base open flags here if appropriate (e.g., change the base back
95 * to r/o). These reopens do not need to be atomic, since we won't abort
96 * even on failure here */
97 if (s
->base_flags
!= bdrv_get_flags(base
)) {
98 bdrv_reopen(base
, s
->base_flags
, NULL
);
100 overlay_bs
= bdrv_find_overlay(active
, top
);
101 if (overlay_bs
&& s
->orig_overlay_flags
!= bdrv_get_flags(overlay_bs
)) {
102 bdrv_reopen(overlay_bs
, s
->orig_overlay_flags
, NULL
);
104 g_free(s
->backing_file_str
);
107 block_job_completed(&s
->common
, ret
);
111 static void coroutine_fn
commit_run(void *opaque
)
113 CommitBlockJob
*s
= opaque
;
114 CommitCompleteData
*data
;
115 int64_t sector_num
, end
;
116 uint64_t delay_ns
= 0;
120 int bytes_written
= 0;
123 ret
= s
->common
.len
= blk_getlength(s
->top
);
126 if (s
->common
.len
< 0) {
130 ret
= base_len
= blk_getlength(s
->base
);
135 if (base_len
< s
->common
.len
) {
136 ret
= blk_truncate(s
->base
, s
->common
.len
);
142 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
143 buf
= blk_blockalign(s
->top
, COMMIT_BUFFER_SIZE
);
145 for (sector_num
= 0; sector_num
< end
; sector_num
+= n
) {
148 /* Note that even when no rate limit is applied we need to yield
149 * with no pending I/O here so that bdrv_drain_all() returns.
151 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
152 if (block_job_is_cancelled(&s
->common
)) {
155 /* Copy if allocated above the base */
156 ret
= bdrv_is_allocated_above(blk_bs(s
->top
), blk_bs(s
->base
),
158 COMMIT_BUFFER_SIZE
/ BDRV_SECTOR_SIZE
,
161 trace_commit_one_iteration(s
, sector_num
, n
, ret
);
163 ret
= commit_populate(s
->top
, s
->base
, sector_num
, n
, buf
);
164 bytes_written
+= n
* BDRV_SECTOR_SIZE
;
167 BlockErrorAction action
=
168 block_job_error_action(&s
->common
, false, s
->on_error
, -ret
);
169 if (action
== BLOCK_ERROR_ACTION_REPORT
) {
176 /* Publish progress */
177 s
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
179 if (copy
&& s
->common
.speed
) {
180 delay_ns
= ratelimit_calculate_delay(&s
->limit
, n
);
189 data
= g_malloc(sizeof(*data
));
191 block_job_defer_to_main_loop(&s
->common
, commit_complete
, data
);
194 static void commit_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
196 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
);
199 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
202 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
205 static const BlockJobDriver commit_job_driver
= {
206 .instance_size
= sizeof(CommitBlockJob
),
207 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
208 .set_speed
= commit_set_speed
,
211 void commit_start(const char *job_id
, BlockDriverState
*bs
,
212 BlockDriverState
*base
, BlockDriverState
*top
, int64_t speed
,
213 BlockdevOnError on_error
, BlockCompletionFunc
*cb
,
214 void *opaque
, const char *backing_file_str
, Error
**errp
)
217 BlockReopenQueue
*reopen_queue
= NULL
;
218 int orig_overlay_flags
;
220 BlockDriverState
*overlay_bs
;
221 Error
*local_err
= NULL
;
225 error_setg(errp
, "Invalid files for merge: top and base are the same");
229 overlay_bs
= bdrv_find_overlay(bs
, top
);
231 if (overlay_bs
== NULL
) {
232 error_setg(errp
, "Could not find overlay image for %s:", top
->filename
);
236 s
= block_job_create(job_id
, &commit_job_driver
, bs
, speed
,
242 orig_base_flags
= bdrv_get_flags(base
);
243 orig_overlay_flags
= bdrv_get_flags(overlay_bs
);
245 /* convert base & overlay_bs to r/w, if necessary */
246 if (!(orig_overlay_flags
& BDRV_O_RDWR
)) {
247 reopen_queue
= bdrv_reopen_queue(reopen_queue
, overlay_bs
, NULL
,
248 orig_overlay_flags
| BDRV_O_RDWR
);
250 if (!(orig_base_flags
& BDRV_O_RDWR
)) {
251 reopen_queue
= bdrv_reopen_queue(reopen_queue
, base
, NULL
,
252 orig_base_flags
| BDRV_O_RDWR
);
255 bdrv_reopen_multiple(reopen_queue
, &local_err
);
256 if (local_err
!= NULL
) {
257 error_propagate(errp
, local_err
);
258 block_job_unref(&s
->common
);
265 blk_insert_bs(s
->base
, base
);
268 blk_insert_bs(s
->top
, top
);
272 s
->base_flags
= orig_base_flags
;
273 s
->orig_overlay_flags
= orig_overlay_flags
;
275 s
->backing_file_str
= g_strdup(backing_file_str
);
277 s
->on_error
= on_error
;
278 s
->common
.co
= qemu_coroutine_create(commit_run
, s
);
280 trace_commit_start(bs
, base
, top
, s
, s
->common
.co
, opaque
);
281 qemu_coroutine_enter(s
->common
.co
);
285 #define COMMIT_BUF_SECTORS 2048
287 /* commit COW file into the raw image */
288 int bdrv_commit(BlockDriverState
*bs
)
290 BlockBackend
*src
, *backing
;
291 BlockDriver
*drv
= bs
->drv
;
292 int64_t sector
, total_sectors
, length
, backing_length
;
293 int n
, ro
, open_flags
;
304 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, NULL
) ||
305 bdrv_op_is_blocked(bs
->backing
->bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, NULL
)) {
309 ro
= bs
->backing
->bs
->read_only
;
310 open_flags
= bs
->backing
->bs
->open_flags
;
313 if (bdrv_reopen(bs
->backing
->bs
, open_flags
| BDRV_O_RDWR
, NULL
)) {
319 blk_insert_bs(src
, bs
);
322 blk_insert_bs(backing
, bs
->backing
->bs
);
324 length
= blk_getlength(src
);
330 backing_length
= blk_getlength(backing
);
331 if (backing_length
< 0) {
332 ret
= backing_length
;
336 /* If our top snapshot is larger than the backing file image,
337 * grow the backing file image if possible. If not possible,
338 * we must return an error */
339 if (length
> backing_length
) {
340 ret
= blk_truncate(backing
, length
);
346 total_sectors
= length
>> BDRV_SECTOR_BITS
;
348 /* blk_try_blockalign() for src will choose an alignment that works for
349 * backing as well, so no need to compare the alignment manually. */
350 buf
= blk_try_blockalign(src
, COMMIT_BUF_SECTORS
* BDRV_SECTOR_SIZE
);
356 for (sector
= 0; sector
< total_sectors
; sector
+= n
) {
357 ret
= bdrv_is_allocated(bs
, sector
, COMMIT_BUF_SECTORS
, &n
);
362 ret
= blk_pread(src
, sector
* BDRV_SECTOR_SIZE
, buf
,
363 n
* BDRV_SECTOR_SIZE
);
368 ret
= blk_pwrite(backing
, sector
* BDRV_SECTOR_SIZE
, buf
,
369 n
* BDRV_SECTOR_SIZE
, 0);
376 if (drv
->bdrv_make_empty
) {
377 ret
= drv
->bdrv_make_empty(bs
);
385 * Make sure all data we wrote to the backing device is actually
398 /* ignoring error return here */
399 bdrv_reopen(bs
->backing
->bs
, open_flags
& ~BDRV_O_RDWR
, NULL
);