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
;
39 BlockDriverState
*top
;
40 BlockDriverState
*base
;
41 BlockdevOnError on_error
;
43 int orig_overlay_flags
;
44 char *backing_file_str
;
47 static int coroutine_fn
commit_populate(BlockDriverState
*bs
,
48 BlockDriverState
*base
,
49 int64_t sector_num
, int nb_sectors
,
54 ret
= bdrv_read(bs
, sector_num
, buf
, nb_sectors
);
59 ret
= bdrv_write(base
, sector_num
, buf
, nb_sectors
);
71 static void commit_complete(BlockJob
*job
, void *opaque
)
73 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
);
74 CommitCompleteData
*data
= opaque
;
75 BlockDriverState
*active
= s
->active
;
76 BlockDriverState
*top
= s
->top
;
77 BlockDriverState
*base
= s
->base
;
78 BlockDriverState
*overlay_bs
;
81 if (!block_job_is_cancelled(&s
->common
) && ret
== 0) {
83 ret
= bdrv_drop_intermediate(active
, top
, base
, s
->backing_file_str
);
86 /* restore base open flags here if appropriate (e.g., change the base back
87 * to r/o). These reopens do not need to be atomic, since we won't abort
88 * even on failure here */
89 if (s
->base_flags
!= bdrv_get_flags(base
)) {
90 bdrv_reopen(base
, s
->base_flags
, NULL
);
92 overlay_bs
= bdrv_find_overlay(active
, top
);
93 if (overlay_bs
&& s
->orig_overlay_flags
!= bdrv_get_flags(overlay_bs
)) {
94 bdrv_reopen(overlay_bs
, s
->orig_overlay_flags
, NULL
);
96 g_free(s
->backing_file_str
);
97 block_job_completed(&s
->common
, ret
);
101 static void coroutine_fn
commit_run(void *opaque
)
103 CommitBlockJob
*s
= opaque
;
104 CommitCompleteData
*data
;
105 BlockDriverState
*top
= s
->top
;
106 BlockDriverState
*base
= s
->base
;
107 int64_t sector_num
, end
;
111 int bytes_written
= 0;
114 ret
= s
->common
.len
= bdrv_getlength(top
);
117 if (s
->common
.len
< 0) {
121 ret
= base_len
= bdrv_getlength(base
);
126 if (base_len
< s
->common
.len
) {
127 ret
= bdrv_truncate(base
, s
->common
.len
);
133 end
= s
->common
.len
>> BDRV_SECTOR_BITS
;
134 buf
= qemu_blockalign(top
, COMMIT_BUFFER_SIZE
);
136 for (sector_num
= 0; sector_num
< end
; sector_num
+= n
) {
137 uint64_t delay_ns
= 0;
141 /* Note that even when no rate limit is applied we need to yield
142 * with no pending I/O here so that bdrv_drain_all() returns.
144 block_job_sleep_ns(&s
->common
, QEMU_CLOCK_REALTIME
, delay_ns
);
145 if (block_job_is_cancelled(&s
->common
)) {
148 /* Copy if allocated above the base */
149 ret
= bdrv_is_allocated_above(top
, base
, sector_num
,
150 COMMIT_BUFFER_SIZE
/ BDRV_SECTOR_SIZE
,
153 trace_commit_one_iteration(s
, sector_num
, n
, ret
);
155 if (s
->common
.speed
) {
156 delay_ns
= ratelimit_calculate_delay(&s
->limit
, n
);
161 ret
= commit_populate(top
, base
, sector_num
, n
, buf
);
162 bytes_written
+= n
* BDRV_SECTOR_SIZE
;
165 if (s
->on_error
== BLOCKDEV_ON_ERROR_STOP
||
166 s
->on_error
== BLOCKDEV_ON_ERROR_REPORT
||
167 (s
->on_error
== BLOCKDEV_ON_ERROR_ENOSPC
&& ret
== -ENOSPC
)) {
174 /* Publish progress */
175 s
->common
.offset
+= n
* BDRV_SECTOR_SIZE
;
183 data
= g_malloc(sizeof(*data
));
185 block_job_defer_to_main_loop(&s
->common
, commit_complete
, data
);
188 static void commit_set_speed(BlockJob
*job
, int64_t speed
, Error
**errp
)
190 CommitBlockJob
*s
= container_of(job
, CommitBlockJob
, common
);
193 error_setg(errp
, QERR_INVALID_PARAMETER
, "speed");
196 ratelimit_set_speed(&s
->limit
, speed
/ BDRV_SECTOR_SIZE
, SLICE_TIME
);
199 static const BlockJobDriver commit_job_driver
= {
200 .instance_size
= sizeof(CommitBlockJob
),
201 .job_type
= BLOCK_JOB_TYPE_COMMIT
,
202 .set_speed
= commit_set_speed
,
205 void commit_start(BlockDriverState
*bs
, BlockDriverState
*base
,
206 BlockDriverState
*top
, int64_t speed
,
207 BlockdevOnError on_error
, BlockCompletionFunc
*cb
,
208 void *opaque
, const char *backing_file_str
, Error
**errp
)
211 BlockReopenQueue
*reopen_queue
= NULL
;
212 int orig_overlay_flags
;
214 BlockDriverState
*overlay_bs
;
215 Error
*local_err
= NULL
;
219 error_setg(errp
, "Invalid files for merge: top and base are the same");
223 overlay_bs
= bdrv_find_overlay(bs
, top
);
225 if (overlay_bs
== NULL
) {
226 error_setg(errp
, "Could not find overlay image for %s:", top
->filename
);
230 orig_base_flags
= bdrv_get_flags(base
);
231 orig_overlay_flags
= bdrv_get_flags(overlay_bs
);
233 /* convert base & overlay_bs to r/w, if necessary */
234 if (!(orig_overlay_flags
& BDRV_O_RDWR
)) {
235 reopen_queue
= bdrv_reopen_queue(reopen_queue
, overlay_bs
, NULL
,
236 orig_overlay_flags
| BDRV_O_RDWR
);
238 if (!(orig_base_flags
& BDRV_O_RDWR
)) {
239 reopen_queue
= bdrv_reopen_queue(reopen_queue
, base
, NULL
,
240 orig_base_flags
| BDRV_O_RDWR
);
243 bdrv_reopen_multiple(reopen_queue
, &local_err
);
244 if (local_err
!= NULL
) {
245 error_propagate(errp
, local_err
);
251 s
= block_job_create(&commit_job_driver
, bs
, speed
, cb
, opaque
, errp
);
260 s
->base_flags
= orig_base_flags
;
261 s
->orig_overlay_flags
= orig_overlay_flags
;
263 s
->backing_file_str
= g_strdup(backing_file_str
);
265 s
->on_error
= on_error
;
266 s
->common
.co
= qemu_coroutine_create(commit_run
);
268 trace_commit_start(bs
, base
, top
, s
, s
->common
.co
, opaque
);
269 qemu_coroutine_enter(s
->common
.co
, s
);