]>
Commit | Line | Data |
---|---|---|
666daa68 MA |
1 | /* |
2 | * QEMU host block devices | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
7 | * later. See the COPYING file in the top-level directory. | |
3618a094 MA |
8 | * |
9 | * This file incorporates work covered by the following copyright and | |
10 | * permission notice: | |
11 | * | |
12 | * Copyright (c) 2003-2008 Fabrice Bellard | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
666daa68 MA |
31 | */ |
32 | ||
d38ea87a | 33 | #include "qemu/osdep.h" |
26f54e9a | 34 | #include "sysemu/block-backend.h" |
9c17d615 | 35 | #include "sysemu/blockdev.h" |
0d09e41a | 36 | #include "hw/block/block.h" |
737e150e | 37 | #include "block/blockjob.h" |
609f45ea | 38 | #include "block/qdict.h" |
76f4afb4 | 39 | #include "block/throttle-groups.h" |
83c9089e | 40 | #include "monitor/monitor.h" |
d49b6836 | 41 | #include "qemu/error-report.h" |
1de7afc9 | 42 | #include "qemu/option.h" |
cdcd4361 | 43 | #include "qemu/qemu-print.h" |
1de7afc9 | 44 | #include "qemu/config-file.h" |
9af23989 MA |
45 | #include "qapi/qapi-commands-block.h" |
46 | #include "qapi/qapi-commands-transaction.h" | |
47 | #include "qapi/qapi-visit-block-core.h" | |
452fcdbc | 48 | #include "qapi/qmp/qdict.h" |
15280c36 | 49 | #include "qapi/qmp/qnum.h" |
6b673957 | 50 | #include "qapi/qmp/qstring.h" |
e688df6b | 51 | #include "qapi/error.h" |
cc7a8ea7 | 52 | #include "qapi/qmp/qerror.h" |
47e6b297 | 53 | #include "qapi/qmp/qlist.h" |
b3db211f | 54 | #include "qapi/qobject-output-visitor.h" |
9c17d615 | 55 | #include "sysemu/sysemu.h" |
ca00bbb1 | 56 | #include "sysemu/iothread.h" |
737e150e | 57 | #include "block/block_int.h" |
0ab8ed18 | 58 | #include "block/trace.h" |
9c17d615 | 59 | #include "sysemu/arch_init.h" |
c616f16e | 60 | #include "sysemu/qtest.h" |
54d31236 | 61 | #include "sysemu/runstate.h" |
f348b6d1 VB |
62 | #include "qemu/cutils.h" |
63 | #include "qemu/help_option.h" | |
db725815 | 64 | #include "qemu/main-loop.h" |
a2a7862c | 65 | #include "qemu/throttle-options.h" |
666daa68 | 66 | |
89802d5a | 67 | QTAILQ_HEAD(, BlockDriverState) monitor_bdrv_states = |
9c4218e9 HR |
68 | QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states); |
69 | ||
89802d5a ML |
70 | void bdrv_set_monitor_owned(BlockDriverState *bs) |
71 | { | |
72 | QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); | |
73 | } | |
74 | ||
1960966d MA |
75 | static const char *const if_name[IF_COUNT] = { |
76 | [IF_NONE] = "none", | |
77 | [IF_IDE] = "ide", | |
78 | [IF_SCSI] = "scsi", | |
79 | [IF_FLOPPY] = "floppy", | |
80 | [IF_PFLASH] = "pflash", | |
81 | [IF_MTD] = "mtd", | |
82 | [IF_SD] = "sd", | |
83 | [IF_VIRTIO] = "virtio", | |
84 | [IF_XEN] = "xen", | |
85 | }; | |
86 | ||
21dff8cf | 87 | static int if_max_devs[IF_COUNT] = { |
27d6bf40 MA |
88 | /* |
89 | * Do not change these numbers! They govern how drive option | |
90 | * index maps to unit and bus. That mapping is ABI. | |
91 | * | |
547cb157 | 92 | * All controllers used to implement if=T drives need to support |
27d6bf40 MA |
93 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. |
94 | * Otherwise, some index values map to "impossible" bus, unit | |
95 | * values. | |
96 | * | |
97 | * For instance, if you change [IF_SCSI] to 255, -drive | |
98 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
99 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
100 | * the drive can't be set up. Regression. | |
101 | */ | |
102 | [IF_IDE] = 2, | |
103 | [IF_SCSI] = 7, | |
1960966d MA |
104 | }; |
105 | ||
21dff8cf JS |
106 | /** |
107 | * Boards may call this to offer board-by-board overrides | |
108 | * of the default, global values. | |
109 | */ | |
110 | void override_max_devs(BlockInterfaceType type, int max_devs) | |
111 | { | |
18e46a03 | 112 | BlockBackend *blk; |
21dff8cf JS |
113 | DriveInfo *dinfo; |
114 | ||
115 | if (max_devs <= 0) { | |
116 | return; | |
117 | } | |
118 | ||
18e46a03 MA |
119 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
120 | dinfo = blk_legacy_dinfo(blk); | |
21dff8cf JS |
121 | if (dinfo->type == type) { |
122 | fprintf(stderr, "Cannot override units-per-bus property of" | |
123 | " the %s interface, because a drive of that type has" | |
124 | " already been added.\n", if_name[type]); | |
125 | g_assert_not_reached(); | |
126 | } | |
127 | } | |
128 | ||
129 | if_max_devs[type] = max_devs; | |
130 | } | |
131 | ||
14bafc54 MA |
132 | /* |
133 | * We automatically delete the drive when a device using it gets | |
134 | * unplugged. Questionable feature, but we can't just drop it. | |
135 | * Device models call blockdev_mark_auto_del() to schedule the | |
136 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
137 | * when deletion is actually safe. | |
138 | */ | |
4be74634 | 139 | void blockdev_mark_auto_del(BlockBackend *blk) |
14bafc54 | 140 | { |
18e46a03 | 141 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
8164102f | 142 | BlockJob *job; |
14bafc54 | 143 | |
26f8b3a8 | 144 | if (!dinfo) { |
2d246f01 KW |
145 | return; |
146 | } | |
147 | ||
8164102f VSO |
148 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
149 | if (block_job_has_bdrv(job, blk_bs(blk))) { | |
150 | AioContext *aio_context = job->job.aio_context; | |
151 | aio_context_acquire(aio_context); | |
91fddb0d | 152 | |
8164102f | 153 | job_cancel(&job->job, false); |
91fddb0d | 154 | |
8164102f VSO |
155 | aio_context_release(aio_context); |
156 | } | |
5433c24f | 157 | } |
91fddb0d | 158 | |
26f8b3a8 | 159 | dinfo->auto_del = 1; |
14bafc54 MA |
160 | } |
161 | ||
4be74634 | 162 | void blockdev_auto_del(BlockBackend *blk) |
14bafc54 | 163 | { |
18e46a03 | 164 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
14bafc54 | 165 | |
0fc0f1fa | 166 | if (dinfo && dinfo->auto_del) { |
efaa7c4e | 167 | monitor_remove_blk(blk); |
b9fe8a7a | 168 | blk_unref(blk); |
14bafc54 MA |
169 | } |
170 | } | |
171 | ||
d8f94e1b JS |
172 | /** |
173 | * Returns the current mapping of how many units per bus | |
174 | * a particular interface can support. | |
175 | * | |
176 | * A positive integer indicates n units per bus. | |
177 | * 0 implies the mapping has not been established. | |
178 | * -1 indicates an invalid BlockInterfaceType was given. | |
179 | */ | |
180 | int drive_get_max_devs(BlockInterfaceType type) | |
181 | { | |
182 | if (type >= IF_IDE && type < IF_COUNT) { | |
183 | return if_max_devs[type]; | |
184 | } | |
185 | ||
186 | return -1; | |
187 | } | |
188 | ||
505a7fb1 MA |
189 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
190 | { | |
191 | int max_devs = if_max_devs[type]; | |
192 | return max_devs ? index / max_devs : 0; | |
193 | } | |
194 | ||
195 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
196 | { | |
197 | int max_devs = if_max_devs[type]; | |
198 | return max_devs ? index % max_devs : index; | |
199 | } | |
200 | ||
2292ddae MA |
201 | QemuOpts *drive_def(const char *optstr) |
202 | { | |
70b94331 | 203 | return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); |
2292ddae MA |
204 | } |
205 | ||
206 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 207 | const char *optstr) |
666daa68 | 208 | { |
666daa68 MA |
209 | QemuOpts *opts; |
210 | ||
2292ddae | 211 | opts = drive_def(optstr); |
666daa68 MA |
212 | if (!opts) { |
213 | return NULL; | |
214 | } | |
2292ddae | 215 | if (type != IF_DEFAULT) { |
f43e47db | 216 | qemu_opt_set(opts, "if", if_name[type], &error_abort); |
2292ddae MA |
217 | } |
218 | if (index >= 0) { | |
a8b18f8f | 219 | qemu_opt_set_number(opts, "index", index, &error_abort); |
2292ddae | 220 | } |
666daa68 | 221 | if (file) |
f43e47db | 222 | qemu_opt_set(opts, "file", file, &error_abort); |
666daa68 MA |
223 | return opts; |
224 | } | |
225 | ||
226 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
227 | { | |
18e46a03 | 228 | BlockBackend *blk; |
666daa68 MA |
229 | DriveInfo *dinfo; |
230 | ||
18e46a03 MA |
231 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
232 | dinfo = blk_legacy_dinfo(blk); | |
233 | if (dinfo && dinfo->type == type | |
234 | && dinfo->bus == bus && dinfo->unit == unit) { | |
666daa68 | 235 | return dinfo; |
18e46a03 | 236 | } |
666daa68 MA |
237 | } |
238 | ||
239 | return NULL; | |
240 | } | |
241 | ||
720b8dc0 | 242 | void drive_check_orphaned(void) |
a66c9dc7 | 243 | { |
18e46a03 | 244 | BlockBackend *blk; |
a66c9dc7 | 245 | DriveInfo *dinfo; |
664cc623 | 246 | Location loc; |
720b8dc0 | 247 | bool orphans = false; |
a66c9dc7 | 248 | |
18e46a03 MA |
249 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
250 | dinfo = blk_legacy_dinfo(blk); | |
a7f53e26 | 251 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && |
a66c9dc7 | 252 | dinfo->type != IF_NONE) { |
664cc623 MA |
253 | loc_push_none(&loc); |
254 | qemu_opts_loc_restore(dinfo->opts); | |
720b8dc0 | 255 | error_report("machine type does not support" |
664cc623 MA |
256 | " if=%s,bus=%d,unit=%d", |
257 | if_name[dinfo->type], dinfo->bus, dinfo->unit); | |
258 | loc_pop(&loc); | |
720b8dc0 | 259 | orphans = true; |
a66c9dc7 JS |
260 | } |
261 | } | |
262 | ||
720b8dc0 MA |
263 | if (orphans) { |
264 | exit(1); | |
265 | } | |
a66c9dc7 JS |
266 | } |
267 | ||
f1bd51ac MA |
268 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
269 | { | |
270 | return drive_get(type, | |
271 | drive_index_to_bus_id(type, index), | |
272 | drive_index_to_unit_id(type, index)); | |
273 | } | |
274 | ||
666daa68 MA |
275 | int drive_get_max_bus(BlockInterfaceType type) |
276 | { | |
277 | int max_bus; | |
18e46a03 | 278 | BlockBackend *blk; |
666daa68 MA |
279 | DriveInfo *dinfo; |
280 | ||
281 | max_bus = -1; | |
18e46a03 MA |
282 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
283 | dinfo = blk_legacy_dinfo(blk); | |
284 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { | |
666daa68 | 285 | max_bus = dinfo->bus; |
18e46a03 | 286 | } |
666daa68 MA |
287 | } |
288 | return max_bus; | |
289 | } | |
290 | ||
13839974 MA |
291 | /* Get a block device. This should only be used for single-drive devices |
292 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
293 | appropriate bus. */ | |
294 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
295 | { | |
296 | static int next_block_unit[IF_COUNT]; | |
297 | ||
298 | return drive_get(type, 0, next_block_unit[type]++); | |
299 | } | |
300 | ||
666daa68 MA |
301 | static void bdrv_format_print(void *opaque, const char *name) |
302 | { | |
cdcd4361 | 303 | qemu_printf(" %s", name); |
666daa68 MA |
304 | } |
305 | ||
aa398a5c SH |
306 | typedef struct { |
307 | QEMUBH *bh; | |
fa510ebf FZ |
308 | BlockDriverState *bs; |
309 | } BDRVPutRefBH; | |
aa398a5c | 310 | |
b681072d | 311 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
312 | { |
313 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 314 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 315 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 316 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 317 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 318 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 319 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 320 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 321 | } else { |
b681072d KW |
322 | error_setg(errp, "'%s' invalid %s error action", |
323 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
324 | return -1; |
325 | } | |
326 | } | |
327 | ||
40119eff AG |
328 | static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, |
329 | Error **errp) | |
330 | { | |
331 | const QListEntry *entry; | |
332 | for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { | |
333 | switch (qobject_type(entry->value)) { | |
334 | ||
335 | case QTYPE_QSTRING: { | |
336 | unsigned long long length; | |
7dc847eb HR |
337 | const char *str = qstring_get_str(qobject_to(QString, |
338 | entry->value)); | |
40119eff AG |
339 | if (parse_uint_full(str, &length, 10) == 0 && |
340 | length > 0 && length <= UINT_MAX) { | |
341 | block_acct_add_interval(stats, (unsigned) length); | |
342 | } else { | |
343 | error_setg(errp, "Invalid interval length: %s", str); | |
344 | return false; | |
345 | } | |
346 | break; | |
347 | } | |
348 | ||
01b2ffce | 349 | case QTYPE_QNUM: { |
7dc847eb | 350 | int64_t length = qnum_get_int(qobject_to(QNum, entry->value)); |
01b2ffce | 351 | |
40119eff AG |
352 | if (length > 0 && length <= UINT_MAX) { |
353 | block_acct_add_interval(stats, (unsigned) length); | |
354 | } else { | |
355 | error_setg(errp, "Invalid interval length: %" PRId64, length); | |
356 | return false; | |
357 | } | |
358 | break; | |
359 | } | |
360 | ||
361 | default: | |
362 | error_setg(errp, "The specification of stats-intervals is invalid"); | |
363 | return false; | |
364 | } | |
365 | } | |
366 | return true; | |
367 | } | |
368 | ||
33cb7dc8 KW |
369 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
370 | ||
fbf8175e HR |
371 | /* All parameters but @opts are optional and may be set to NULL. */ |
372 | static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, | |
373 | const char **throttling_group, ThrottleConfig *throttle_cfg, | |
374 | BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) | |
375 | { | |
fbf8175e HR |
376 | Error *local_error = NULL; |
377 | const char *aio; | |
378 | ||
379 | if (bdrv_flags) { | |
fbf8175e HR |
380 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { |
381 | *bdrv_flags |= BDRV_O_COPY_ON_READ; | |
382 | } | |
383 | ||
fbf8175e | 384 | if ((aio = qemu_opt_get(opts, "aio")) != NULL) { |
f80f2673 AM |
385 | if (bdrv_parse_aio(aio, bdrv_flags) < 0) { |
386 | error_setg(errp, "invalid aio option"); | |
387 | return; | |
fbf8175e HR |
388 | } |
389 | } | |
390 | } | |
391 | ||
392 | /* disk I/O throttling */ | |
393 | if (throttling_group) { | |
394 | *throttling_group = qemu_opt_get(opts, "throttling.group"); | |
395 | } | |
396 | ||
397 | if (throttle_cfg) { | |
1588ab5d | 398 | throttle_config_init(throttle_cfg); |
fbf8175e HR |
399 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = |
400 | qemu_opt_get_number(opts, "throttling.bps-total", 0); | |
401 | throttle_cfg->buckets[THROTTLE_BPS_READ].avg = | |
402 | qemu_opt_get_number(opts, "throttling.bps-read", 0); | |
403 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = | |
404 | qemu_opt_get_number(opts, "throttling.bps-write", 0); | |
405 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = | |
406 | qemu_opt_get_number(opts, "throttling.iops-total", 0); | |
407 | throttle_cfg->buckets[THROTTLE_OPS_READ].avg = | |
408 | qemu_opt_get_number(opts, "throttling.iops-read", 0); | |
409 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = | |
410 | qemu_opt_get_number(opts, "throttling.iops-write", 0); | |
411 | ||
412 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = | |
413 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
414 | throttle_cfg->buckets[THROTTLE_BPS_READ].max = | |
415 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
416 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = | |
417 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
418 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = | |
419 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
420 | throttle_cfg->buckets[THROTTLE_OPS_READ].max = | |
421 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
422 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = | |
423 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
424 | ||
8a0fc18d AG |
425 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].burst_length = |
426 | qemu_opt_get_number(opts, "throttling.bps-total-max-length", 1); | |
427 | throttle_cfg->buckets[THROTTLE_BPS_READ].burst_length = | |
428 | qemu_opt_get_number(opts, "throttling.bps-read-max-length", 1); | |
429 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].burst_length = | |
430 | qemu_opt_get_number(opts, "throttling.bps-write-max-length", 1); | |
431 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].burst_length = | |
432 | qemu_opt_get_number(opts, "throttling.iops-total-max-length", 1); | |
433 | throttle_cfg->buckets[THROTTLE_OPS_READ].burst_length = | |
434 | qemu_opt_get_number(opts, "throttling.iops-read-max-length", 1); | |
435 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].burst_length = | |
436 | qemu_opt_get_number(opts, "throttling.iops-write-max-length", 1); | |
437 | ||
fbf8175e HR |
438 | throttle_cfg->op_size = |
439 | qemu_opt_get_number(opts, "throttling.iops-size", 0); | |
440 | ||
d5851089 | 441 | if (!throttle_is_valid(throttle_cfg, errp)) { |
fbf8175e HR |
442 | return; |
443 | } | |
444 | } | |
445 | ||
446 | if (detect_zeroes) { | |
447 | *detect_zeroes = | |
f7abe0ec | 448 | qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup, |
fbf8175e | 449 | qemu_opt_get(opts, "detect-zeroes"), |
fbf8175e HR |
450 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, |
451 | &local_error); | |
452 | if (local_error) { | |
453 | error_propagate(errp, local_error); | |
454 | return; | |
455 | } | |
fbf8175e HR |
456 | } |
457 | } | |
458 | ||
f298d071 | 459 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
460 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
461 | Error **errp) | |
666daa68 MA |
462 | { |
463 | const char *buf; | |
666daa68 MA |
464 | int bdrv_flags = 0; |
465 | int on_read_error, on_write_error; | |
362e9299 | 466 | bool account_invalid, account_failed; |
f87a0e29 | 467 | bool writethrough, read_only; |
26f54e9a | 468 | BlockBackend *blk; |
a0f1eab1 | 469 | BlockDriverState *bs; |
cc0681c4 | 470 | ThrottleConfig cfg; |
666daa68 | 471 | int snapshot = 0; |
c546194f | 472 | Error *error = NULL; |
0006383e | 473 | QemuOpts *opts; |
40119eff AG |
474 | QDict *interval_dict = NULL; |
475 | QList *interval_list = NULL; | |
0006383e | 476 | const char *id; |
fbf8175e HR |
477 | BlockdevDetectZeroesOptions detect_zeroes = |
478 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; | |
479 | const char *throttling_group = NULL; | |
666daa68 | 480 | |
f298d071 KW |
481 | /* Check common options by copying from bs_opts to opts, all other options |
482 | * stay in bs_opts for processing by bdrv_open(). */ | |
483 | id = qdict_get_try_str(bs_opts, "id"); | |
0006383e | 484 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
84d18f06 | 485 | if (error) { |
b681072d | 486 | error_propagate(errp, error); |
6376f952 | 487 | goto err_no_opts; |
0006383e KW |
488 | } |
489 | ||
0006383e | 490 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
84d18f06 | 491 | if (error) { |
b681072d | 492 | error_propagate(errp, error); |
ec9c10d2 | 493 | goto early_err; |
0006383e KW |
494 | } |
495 | ||
496 | if (id) { | |
497 | qdict_del(bs_opts, "id"); | |
498 | } | |
499 | ||
666daa68 | 500 | /* extract parameters */ |
666daa68 | 501 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
666daa68 | 502 | |
362e9299 AG |
503 | account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); |
504 | account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); | |
505 | ||
e4b24b49 KW |
506 | writethrough = !qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true); |
507 | ||
ff356ee4 AG |
508 | id = qemu_opts_id(opts); |
509 | ||
40119eff AG |
510 | qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); |
511 | qdict_array_split(interval_dict, &interval_list); | |
512 | ||
513 | if (qdict_size(interval_dict) != 0) { | |
514 | error_setg(errp, "Invalid option stats-intervals.%s", | |
515 | qdict_first(interval_dict)->key); | |
516 | goto early_err; | |
517 | } | |
2be5506f | 518 | |
fbf8175e HR |
519 | extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, |
520 | &detect_zeroes, &error); | |
521 | if (error) { | |
522 | error_propagate(errp, error); | |
523 | goto early_err; | |
666daa68 | 524 | } |
666daa68 MA |
525 | |
526 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 | 527 | if (is_help_option(buf)) { |
cdcd4361 | 528 | qemu_printf("Supported formats:"); |
9ac404c5 | 529 | bdrv_iterate_format(bdrv_format_print, NULL, false); |
cdcd4361 | 530 | qemu_printf("\nSupported formats (read-only):"); |
9ac404c5 | 531 | bdrv_iterate_format(bdrv_format_print, NULL, true); |
cdcd4361 | 532 | qemu_printf("\n"); |
ec9c10d2 | 533 | goto early_err; |
666daa68 | 534 | } |
74fe54f2 | 535 | |
e4342ce5 HR |
536 | if (qdict_haskey(bs_opts, "driver")) { |
537 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); | |
ec9c10d2 | 538 | goto early_err; |
6db5f5d6 | 539 | } |
46f5ac20 | 540 | qdict_put_str(bs_opts, "driver", buf); |
666daa68 MA |
541 | } |
542 | ||
92aa5c6d | 543 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 544 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 545 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 546 | if (error) { |
b681072d | 547 | error_propagate(errp, error); |
ec9c10d2 | 548 | goto early_err; |
666daa68 MA |
549 | } |
550 | } | |
551 | ||
92aa5c6d | 552 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 553 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 554 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 555 | if (error) { |
b681072d | 556 | error_propagate(errp, error); |
ec9c10d2 | 557 | goto early_err; |
666daa68 MA |
558 | } |
559 | } | |
560 | ||
5ec18f8c | 561 | if (snapshot) { |
91a097e7 | 562 | bdrv_flags |= BDRV_O_SNAPSHOT; |
5ec18f8c HR |
563 | } |
564 | ||
f87a0e29 AG |
565 | read_only = qemu_opt_get_bool(opts, BDRV_OPT_READ_ONLY, false); |
566 | ||
326642bc | 567 | /* init */ |
39c4ae94 | 568 | if ((!file || !*file) && !qdict_size(bs_opts)) { |
5ec18f8c HR |
569 | BlockBackendRootState *blk_rs; |
570 | ||
d861ab3a | 571 | blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
5ec18f8c HR |
572 | blk_rs = blk_get_root_state(blk); |
573 | blk_rs->open_flags = bdrv_flags; | |
f87a0e29 | 574 | blk_rs->read_only = read_only; |
5ec18f8c HR |
575 | blk_rs->detect_zeroes = detect_zeroes; |
576 | ||
cb3e7f08 | 577 | qobject_unref(bs_opts); |
e4342ce5 HR |
578 | } else { |
579 | if (file && !*file) { | |
c2ad1b0c | 580 | file = NULL; |
c2ad1b0c | 581 | } |
666daa68 | 582 | |
91a097e7 KW |
583 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
584 | * with other callers) rather than what we want as the real defaults. | |
585 | * Apply the defaults here instead. */ | |
91a097e7 KW |
586 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
587 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
f87a0e29 AG |
588 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, |
589 | read_only ? "on" : "off"); | |
9384a444 | 590 | qdict_set_default_str(bs_opts, BDRV_OPT_AUTO_READ_ONLY, "on"); |
72e775c7 | 591 | assert((bdrv_flags & BDRV_O_CACHE_MASK) == 0); |
91a097e7 | 592 | |
12d5ee3a KW |
593 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
594 | bdrv_flags |= BDRV_O_INACTIVE; | |
595 | } | |
596 | ||
efaa7c4e | 597 | blk = blk_new_open(file, NULL, bs_opts, bdrv_flags, errp); |
e4342ce5 HR |
598 | if (!blk) { |
599 | goto err_no_bs_opts; | |
600 | } | |
601 | bs = blk_bs(blk); | |
ed9d4205 | 602 | |
5ec18f8c | 603 | bs->detect_zeroes = detect_zeroes; |
666daa68 | 604 | |
9caa6f3d | 605 | block_acct_setup(blk_get_stats(blk), account_invalid, account_failed); |
2be5506f | 606 | |
40119eff AG |
607 | if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { |
608 | blk_unref(blk); | |
609 | blk = NULL; | |
610 | goto err_no_bs_opts; | |
2be5506f | 611 | } |
666daa68 MA |
612 | } |
613 | ||
7ca7f0f6 KW |
614 | /* disk I/O throttling */ |
615 | if (throttle_enabled(&cfg)) { | |
616 | if (!throttling_group) { | |
ff356ee4 | 617 | throttling_group = id; |
7ca7f0f6 KW |
618 | } |
619 | blk_io_limits_enable(blk, throttling_group); | |
620 | blk_set_io_limits(blk, &cfg); | |
621 | } | |
622 | ||
e4b24b49 | 623 | blk_set_enable_write_cache(blk, !writethrough); |
5ec18f8c | 624 | blk_set_on_error(blk, on_read_error, on_write_error); |
0006383e | 625 | |
ff356ee4 | 626 | if (!monitor_add_blk(blk, id, errp)) { |
efaa7c4e HR |
627 | blk_unref(blk); |
628 | blk = NULL; | |
629 | goto err_no_bs_opts; | |
630 | } | |
631 | ||
e4342ce5 | 632 | err_no_bs_opts: |
0006383e | 633 | qemu_opts_del(opts); |
cb3e7f08 MAL |
634 | qobject_unref(interval_dict); |
635 | qobject_unref(interval_list); | |
18e46a03 | 636 | return blk; |
a9ae2bff | 637 | |
ec9c10d2 | 638 | early_err: |
ec9c10d2 | 639 | qemu_opts_del(opts); |
cb3e7f08 MAL |
640 | qobject_unref(interval_dict); |
641 | qobject_unref(interval_list); | |
6376f952 | 642 | err_no_opts: |
cb3e7f08 | 643 | qobject_unref(bs_opts); |
a9ae2bff | 644 | return NULL; |
666daa68 MA |
645 | } |
646 | ||
bd745e23 | 647 | /* Takes the ownership of bs_opts */ |
89802d5a | 648 | BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) |
bd745e23 | 649 | { |
bd745e23 HR |
650 | int bdrv_flags = 0; |
651 | ||
a81d6164 KW |
652 | /* bdrv_open() defaults to the values in bdrv_flags (for compatibility |
653 | * with other callers) rather than what we want as the real defaults. | |
654 | * Apply the defaults here instead. */ | |
a81d6164 KW |
655 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); |
656 | qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); | |
f87a0e29 | 657 | qdict_set_default_str(bs_opts, BDRV_OPT_READ_ONLY, "off"); |
a81d6164 | 658 | |
12d5ee3a KW |
659 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
660 | bdrv_flags |= BDRV_O_INACTIVE; | |
661 | } | |
662 | ||
74e1ae7c | 663 | return bdrv_open(NULL, NULL, bs_opts, bdrv_flags, errp); |
bd745e23 HR |
664 | } |
665 | ||
9c4218e9 HR |
666 | void blockdev_close_all_bdrv_states(void) |
667 | { | |
668 | BlockDriverState *bs, *next_bs; | |
669 | ||
670 | QTAILQ_FOREACH_SAFE(bs, &monitor_bdrv_states, monitor_list, next_bs) { | |
671 | AioContext *ctx = bdrv_get_aio_context(bs); | |
672 | ||
673 | aio_context_acquire(ctx); | |
674 | bdrv_unref(bs); | |
675 | aio_context_release(ctx); | |
676 | } | |
677 | } | |
678 | ||
262b4e8f HR |
679 | /* Iterates over the list of monitor-owned BlockDriverStates */ |
680 | BlockDriverState *bdrv_next_monitor_owned(BlockDriverState *bs) | |
681 | { | |
682 | return bs ? QTAILQ_NEXT(bs, monitor_list) | |
683 | : QTAILQ_FIRST(&monitor_bdrv_states); | |
684 | } | |
685 | ||
5abbf0ee KW |
686 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
687 | Error **errp) | |
57975222 KW |
688 | { |
689 | const char *value; | |
690 | ||
691 | value = qemu_opt_get(opts, from); | |
692 | if (value) { | |
5abbf0ee KW |
693 | if (qemu_opt_find(opts, to)) { |
694 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
695 | "same time", to, from); | |
696 | return; | |
697 | } | |
20d6cd47 JL |
698 | } |
699 | ||
700 | /* rename all items in opts */ | |
701 | while ((value = qemu_opt_get(opts, from))) { | |
f43e47db | 702 | qemu_opt_set(opts, to, value, &error_abort); |
57975222 KW |
703 | qemu_opt_unset(opts, from); |
704 | } | |
705 | } | |
706 | ||
33cb7dc8 KW |
707 | QemuOptsList qemu_legacy_drive_opts = { |
708 | .name = "drive", | |
709 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
710 | .desc = { | |
711 | { | |
87a899c5 KW |
712 | .name = "bus", |
713 | .type = QEMU_OPT_NUMBER, | |
714 | .help = "bus number", | |
715 | },{ | |
716 | .name = "unit", | |
717 | .type = QEMU_OPT_NUMBER, | |
718 | .help = "unit number (i.e. lun for scsi)", | |
719 | },{ | |
720 | .name = "index", | |
721 | .type = QEMU_OPT_NUMBER, | |
722 | .help = "index number", | |
723 | },{ | |
33cb7dc8 KW |
724 | .name = "media", |
725 | .type = QEMU_OPT_STRING, | |
726 | .help = "media type (disk, cdrom)", | |
593d464b KW |
727 | },{ |
728 | .name = "if", | |
729 | .type = QEMU_OPT_STRING, | |
730 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
d095b465 HR |
731 | },{ |
732 | .name = "file", | |
733 | .type = QEMU_OPT_STRING, | |
734 | .help = "file name", | |
33cb7dc8 | 735 | }, |
0ebd24e0 KW |
736 | |
737 | /* Options that are passed on, but have special semantics with -drive */ | |
738 | { | |
4e200cf8 | 739 | .name = BDRV_OPT_READ_ONLY, |
0ebd24e0 KW |
740 | .type = QEMU_OPT_BOOL, |
741 | .help = "open drive file as read-only", | |
ee13ed1c KW |
742 | },{ |
743 | .name = "rerror", | |
744 | .type = QEMU_OPT_STRING, | |
745 | .help = "read error action", | |
746 | },{ | |
747 | .name = "werror", | |
748 | .type = QEMU_OPT_STRING, | |
749 | .help = "write error action", | |
0ebd24e0 KW |
750 | },{ |
751 | .name = "copy-on-read", | |
752 | .type = QEMU_OPT_BOOL, | |
753 | .help = "copy read data from backing file into image file", | |
754 | }, | |
755 | ||
33cb7dc8 KW |
756 | { /* end of list */ } |
757 | }, | |
758 | }; | |
759 | ||
c4f26c9f MA |
760 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type, |
761 | Error **errp) | |
57975222 | 762 | { |
29c4e2b5 | 763 | const char *value; |
18e46a03 | 764 | BlockBackend *blk; |
33cb7dc8 | 765 | DriveInfo *dinfo = NULL; |
f298d071 | 766 | QDict *bs_opts; |
33cb7dc8 KW |
767 | QemuOpts *legacy_opts; |
768 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 769 | BlockInterfaceType type; |
87a899c5 | 770 | int max_devs, bus_id, unit_id, index; |
ee13ed1c | 771 | const char *werror, *rerror; |
a7fdbcf0 FZ |
772 | bool read_only = false; |
773 | bool copy_on_read; | |
d095b465 | 774 | const char *filename; |
33cb7dc8 | 775 | Error *local_err = NULL; |
247147fb | 776 | int i; |
29c4e2b5 | 777 | |
57975222 | 778 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
779 | static const struct { |
780 | const char *from; | |
781 | const char *to; | |
782 | } opt_renames[] = { | |
783 | { "iops", "throttling.iops-total" }, | |
784 | { "iops_rd", "throttling.iops-read" }, | |
785 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 786 | |
247147fb KW |
787 | { "bps", "throttling.bps-total" }, |
788 | { "bps_rd", "throttling.bps-read" }, | |
789 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 790 | |
247147fb KW |
791 | { "iops_max", "throttling.iops-total-max" }, |
792 | { "iops_rd_max", "throttling.iops-read-max" }, | |
793 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 794 | |
247147fb KW |
795 | { "bps_max", "throttling.bps-total-max" }, |
796 | { "bps_rd_max", "throttling.bps-read-max" }, | |
797 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 798 | |
247147fb | 799 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 800 | |
76f4afb4 AG |
801 | { "group", "throttling.group" }, |
802 | ||
4e200cf8 | 803 | { "readonly", BDRV_OPT_READ_ONLY }, |
247147fb KW |
804 | }; |
805 | ||
806 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
5abbf0ee KW |
807 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
808 | &local_err); | |
809 | if (local_err) { | |
c4f26c9f | 810 | error_propagate(errp, local_err); |
5abbf0ee KW |
811 | return NULL; |
812 | } | |
247147fb | 813 | } |
0f227a94 | 814 | |
29c4e2b5 KW |
815 | value = qemu_opt_get(all_opts, "cache"); |
816 | if (value) { | |
817 | int flags = 0; | |
04feb4a5 | 818 | bool writethrough; |
29c4e2b5 | 819 | |
04feb4a5 | 820 | if (bdrv_parse_cache_mode(value, &flags, &writethrough) != 0) { |
c4f26c9f | 821 | error_setg(errp, "invalid cache option"); |
29c4e2b5 KW |
822 | return NULL; |
823 | } | |
824 | ||
825 | /* Specific options take precedence */ | |
54861b92 KW |
826 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { |
827 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, | |
04feb4a5 | 828 | !writethrough, &error_abort); |
29c4e2b5 | 829 | } |
54861b92 KW |
830 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { |
831 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, | |
cccb7967 | 832 | !!(flags & BDRV_O_NOCACHE), &error_abort); |
29c4e2b5 | 833 | } |
54861b92 KW |
834 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { |
835 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, | |
cccb7967 | 836 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); |
29c4e2b5 KW |
837 | } |
838 | qemu_opt_unset(all_opts, "cache"); | |
839 | } | |
840 | ||
f298d071 KW |
841 | /* Get a QDict for processing the options */ |
842 | bs_opts = qdict_new(); | |
843 | qemu_opts_to_qdict(all_opts, bs_opts); | |
844 | ||
87ea75d5 PC |
845 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
846 | &error_abort); | |
33cb7dc8 | 847 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
84d18f06 | 848 | if (local_err) { |
c4f26c9f | 849 | error_propagate(errp, local_err); |
33cb7dc8 KW |
850 | goto fail; |
851 | } | |
852 | ||
853 | /* Media type */ | |
854 | value = qemu_opt_get(legacy_opts, "media"); | |
855 | if (value) { | |
856 | if (!strcmp(value, "disk")) { | |
857 | media = MEDIA_DISK; | |
858 | } else if (!strcmp(value, "cdrom")) { | |
859 | media = MEDIA_CDROM; | |
a7fdbcf0 | 860 | read_only = true; |
33cb7dc8 | 861 | } else { |
c4f26c9f | 862 | error_setg(errp, "'%s' invalid media", value); |
33cb7dc8 KW |
863 | goto fail; |
864 | } | |
865 | } | |
866 | ||
0ebd24e0 | 867 | /* copy-on-read is disabled with a warning for read-only devices */ |
4e200cf8 | 868 | read_only |= qemu_opt_get_bool(legacy_opts, BDRV_OPT_READ_ONLY, false); |
0ebd24e0 KW |
869 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
870 | ||
871 | if (read_only && copy_on_read) { | |
3dc6f869 | 872 | warn_report("disabling copy-on-read on read-only drive"); |
0ebd24e0 KW |
873 | copy_on_read = false; |
874 | } | |
875 | ||
46f5ac20 EB |
876 | qdict_put_str(bs_opts, BDRV_OPT_READ_ONLY, read_only ? "on" : "off"); |
877 | qdict_put_str(bs_opts, "copy-on-read", copy_on_read ? "on" : "off"); | |
0ebd24e0 | 878 | |
593d464b KW |
879 | /* Controller type */ |
880 | value = qemu_opt_get(legacy_opts, "if"); | |
881 | if (value) { | |
882 | for (type = 0; | |
883 | type < IF_COUNT && strcmp(value, if_name[type]); | |
884 | type++) { | |
885 | } | |
886 | if (type == IF_COUNT) { | |
c4f26c9f | 887 | error_setg(errp, "unsupported bus type '%s'", value); |
593d464b KW |
888 | goto fail; |
889 | } | |
890 | } else { | |
891 | type = block_default_type; | |
892 | } | |
893 | ||
87a899c5 KW |
894 | /* Device address specified by bus/unit or index. |
895 | * If none was specified, try to find the first free one. */ | |
896 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
897 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
898 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
899 | ||
900 | max_devs = if_max_devs[type]; | |
901 | ||
902 | if (index != -1) { | |
903 | if (bus_id != 0 || unit_id != -1) { | |
c4f26c9f | 904 | error_setg(errp, "index cannot be used with bus and unit"); |
87a899c5 KW |
905 | goto fail; |
906 | } | |
907 | bus_id = drive_index_to_bus_id(type, index); | |
908 | unit_id = drive_index_to_unit_id(type, index); | |
909 | } | |
910 | ||
911 | if (unit_id == -1) { | |
912 | unit_id = 0; | |
913 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
914 | unit_id++; | |
915 | if (max_devs && unit_id >= max_devs) { | |
916 | unit_id -= max_devs; | |
917 | bus_id++; | |
918 | } | |
919 | } | |
920 | } | |
921 | ||
922 | if (max_devs && unit_id >= max_devs) { | |
c4f26c9f | 923 | error_setg(errp, "unit %d too big (max is %d)", unit_id, max_devs - 1); |
87a899c5 KW |
924 | goto fail; |
925 | } | |
926 | ||
927 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
c4f26c9f MA |
928 | error_setg(errp, "drive with bus=%d, unit=%d (index=%d) exists", |
929 | bus_id, unit_id, index); | |
87a899c5 KW |
930 | goto fail; |
931 | } | |
932 | ||
933 | /* no id supplied -> create one */ | |
934 | if (qemu_opts_id(all_opts) == NULL) { | |
935 | char *new_id; | |
936 | const char *mediastr = ""; | |
937 | if (type == IF_IDE || type == IF_SCSI) { | |
938 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
939 | } | |
940 | if (max_devs) { | |
941 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
942 | mediastr, unit_id); | |
943 | } else { | |
944 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
945 | mediastr, unit_id); | |
946 | } | |
46f5ac20 | 947 | qdict_put_str(bs_opts, "id", new_id); |
87a899c5 KW |
948 | g_free(new_id); |
949 | } | |
950 | ||
394c7d4d | 951 | /* Add virtio block device */ |
394c7d4d KW |
952 | if (type == IF_VIRTIO) { |
953 | QemuOpts *devopts; | |
87ea75d5 PC |
954 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
955 | &error_abort); | |
394c7d4d | 956 | if (arch_type == QEMU_ARCH_S390X) { |
1f68f1d3 | 957 | qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); |
394c7d4d | 958 | } else { |
f43e47db | 959 | qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); |
394c7d4d | 960 | } |
f43e47db MA |
961 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), |
962 | &error_abort); | |
394c7d4d KW |
963 | } |
964 | ||
d095b465 HR |
965 | filename = qemu_opt_get(legacy_opts, "file"); |
966 | ||
ee13ed1c KW |
967 | /* Check werror/rerror compatibility with if=... */ |
968 | werror = qemu_opt_get(legacy_opts, "werror"); | |
969 | if (werror != NULL) { | |
970 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
971 | type != IF_NONE) { | |
c4f26c9f | 972 | error_setg(errp, "werror is not supported by this bus type"); |
ee13ed1c KW |
973 | goto fail; |
974 | } | |
46f5ac20 | 975 | qdict_put_str(bs_opts, "werror", werror); |
ee13ed1c KW |
976 | } |
977 | ||
978 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
979 | if (rerror != NULL) { | |
980 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
981 | type != IF_NONE) { | |
c4f26c9f | 982 | error_setg(errp, "rerror is not supported by this bus type"); |
ee13ed1c KW |
983 | goto fail; |
984 | } | |
46f5ac20 | 985 | qdict_put_str(bs_opts, "rerror", rerror); |
ee13ed1c KW |
986 | } |
987 | ||
2d246f01 | 988 | /* Actual block device init: Functionality shared with blockdev-add */ |
18e46a03 | 989 | blk = blockdev_init(filename, bs_opts, &local_err); |
3cb0e25c | 990 | bs_opts = NULL; |
18e46a03 | 991 | if (!blk) { |
b2322003 | 992 | error_propagate(errp, local_err); |
2d246f01 | 993 | goto fail; |
b681072d | 994 | } else { |
84d18f06 | 995 | assert(!local_err); |
2d246f01 KW |
996 | } |
997 | ||
26f8b3a8 MA |
998 | /* Create legacy DriveInfo */ |
999 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 1000 | dinfo->opts = all_opts; |
2d246f01 | 1001 | |
ee13ed1c | 1002 | dinfo->type = type; |
87a899c5 KW |
1003 | dinfo->bus = bus_id; |
1004 | dinfo->unit = unit_id; | |
bcf83158 | 1005 | |
26f8b3a8 MA |
1006 | blk_set_legacy_dinfo(blk, dinfo); |
1007 | ||
e34ef046 KW |
1008 | switch(type) { |
1009 | case IF_IDE: | |
1010 | case IF_SCSI: | |
1011 | case IF_XEN: | |
1012 | case IF_NONE: | |
1013 | dinfo->media_cd = media == MEDIA_CDROM; | |
1014 | break; | |
1015 | default: | |
1016 | break; | |
1017 | } | |
1018 | ||
2d246f01 | 1019 | fail: |
33cb7dc8 | 1020 | qemu_opts_del(legacy_opts); |
cb3e7f08 | 1021 | qobject_unref(bs_opts); |
2d246f01 | 1022 | return dinfo; |
57975222 KW |
1023 | } |
1024 | ||
b6c1bae5 KW |
1025 | static BlockDriverState *qmp_get_root_bs(const char *name, Error **errp) |
1026 | { | |
1027 | BlockDriverState *bs; | |
1028 | ||
1029 | bs = bdrv_lookup_bs(name, name, errp); | |
1030 | if (bs == NULL) { | |
1031 | return NULL; | |
1032 | } | |
1033 | ||
1034 | if (!bdrv_is_root_node(bs)) { | |
1035 | error_setg(errp, "Need a root block node"); | |
1036 | return NULL; | |
1037 | } | |
1038 | ||
1039 | if (!bdrv_is_inserted(bs)) { | |
1040 | error_setg(errp, "Device has no medium"); | |
1041 | return NULL; | |
1042 | } | |
1043 | ||
1044 | return bs; | |
1045 | } | |
1046 | ||
10f75907 | 1047 | static void blockdev_do_action(TransactionAction *action, Error **errp) |
6cc2a415 | 1048 | { |
c8a83e85 | 1049 | TransactionActionList list; |
6cc2a415 | 1050 | |
10f75907 | 1051 | list.value = action; |
6cc2a415 | 1052 | list.next = NULL; |
94d16a64 | 1053 | qmp_transaction(&list, false, NULL, errp); |
6cc2a415 PB |
1054 | } |
1055 | ||
0901f67e BC |
1056 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1057 | bool has_node_name, const char *node_name, | |
1058 | const char *snapshot_file, | |
1059 | bool has_snapshot_node_name, | |
1060 | const char *snapshot_node_name, | |
6106e249 | 1061 | bool has_format, const char *format, |
0901f67e | 1062 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1063 | { |
a911e6ae | 1064 | BlockdevSnapshotSync snapshot = { |
0901f67e | 1065 | .has_device = has_device, |
6cc2a415 | 1066 | .device = (char *) device, |
0901f67e BC |
1067 | .has_node_name = has_node_name, |
1068 | .node_name = (char *) node_name, | |
6cc2a415 | 1069 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1070 | .has_snapshot_node_name = has_snapshot_node_name, |
1071 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1072 | .has_format = has_format, |
1073 | .format = (char *) format, | |
1074 | .has_mode = has_mode, | |
1075 | .mode = mode, | |
1076 | }; | |
10f75907 EB |
1077 | TransactionAction action = { |
1078 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, | |
32bafa8f | 1079 | .u.blockdev_snapshot_sync.data = &snapshot, |
10f75907 EB |
1080 | }; |
1081 | blockdev_do_action(&action, errp); | |
f8882568 JS |
1082 | } |
1083 | ||
43de7e2d AG |
1084 | void qmp_blockdev_snapshot(const char *node, const char *overlay, |
1085 | Error **errp) | |
1086 | { | |
1087 | BlockdevSnapshot snapshot_data = { | |
1088 | .node = (char *) node, | |
1089 | .overlay = (char *) overlay | |
1090 | }; | |
10f75907 EB |
1091 | TransactionAction action = { |
1092 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, | |
32bafa8f | 1093 | .u.blockdev_snapshot.data = &snapshot_data, |
10f75907 EB |
1094 | }; |
1095 | blockdev_do_action(&action, errp); | |
43de7e2d AG |
1096 | } |
1097 | ||
f323bc9e WX |
1098 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1099 | const char *name, | |
1100 | Error **errp) | |
1101 | { | |
1102 | BlockdevSnapshotInternal snapshot = { | |
1103 | .device = (char *) device, | |
1104 | .name = (char *) name | |
1105 | }; | |
10f75907 EB |
1106 | TransactionAction action = { |
1107 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
32bafa8f | 1108 | .u.blockdev_snapshot_internal_sync.data = &snapshot, |
10f75907 EB |
1109 | }; |
1110 | blockdev_do_action(&action, errp); | |
f323bc9e WX |
1111 | } |
1112 | ||
44e3e053 WX |
1113 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1114 | bool has_id, | |
1115 | const char *id, | |
1116 | bool has_name, | |
1117 | const char *name, | |
1118 | Error **errp) | |
1119 | { | |
a0e8544c | 1120 | BlockDriverState *bs; |
4ef3982a | 1121 | AioContext *aio_context; |
44e3e053 WX |
1122 | QEMUSnapshotInfo sn; |
1123 | Error *local_err = NULL; | |
1124 | SnapshotInfo *info = NULL; | |
1125 | int ret; | |
1126 | ||
2dfb4c03 KW |
1127 | bs = qmp_get_root_bs(device, errp); |
1128 | if (!bs) { | |
44e3e053 WX |
1129 | return NULL; |
1130 | } | |
2dfb4c03 | 1131 | aio_context = bdrv_get_aio_context(bs); |
5433c24f | 1132 | aio_context_acquire(aio_context); |
44e3e053 WX |
1133 | |
1134 | if (!has_id) { | |
1135 | id = NULL; | |
1136 | } | |
1137 | ||
1138 | if (!has_name) { | |
1139 | name = NULL; | |
1140 | } | |
1141 | ||
1142 | if (!id && !name) { | |
1143 | error_setg(errp, "Name or id must be provided"); | |
5433c24f | 1144 | goto out_aio_context; |
44e3e053 WX |
1145 | } |
1146 | ||
0b928854 SH |
1147 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
1148 | goto out_aio_context; | |
1149 | } | |
1150 | ||
44e3e053 | 1151 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
84d18f06 | 1152 | if (local_err) { |
44e3e053 | 1153 | error_propagate(errp, local_err); |
4ef3982a | 1154 | goto out_aio_context; |
44e3e053 WX |
1155 | } |
1156 | if (!ret) { | |
1157 | error_setg(errp, | |
1158 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1159 | "device '%s'", | |
1160 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
4ef3982a | 1161 | goto out_aio_context; |
44e3e053 WX |
1162 | } |
1163 | ||
1164 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1165 | if (local_err) { |
44e3e053 | 1166 | error_propagate(errp, local_err); |
4ef3982a | 1167 | goto out_aio_context; |
44e3e053 WX |
1168 | } |
1169 | ||
4ef3982a SH |
1170 | aio_context_release(aio_context); |
1171 | ||
5839e53b | 1172 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1173 | info->id = g_strdup(sn.id_str); |
1174 | info->name = g_strdup(sn.name); | |
1175 | info->date_nsec = sn.date_nsec; | |
1176 | info->date_sec = sn.date_sec; | |
1177 | info->vm_state_size = sn.vm_state_size; | |
1178 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1179 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
1180 | ||
1181 | return info; | |
4ef3982a SH |
1182 | |
1183 | out_aio_context: | |
1184 | aio_context_release(aio_context); | |
1185 | return NULL; | |
44e3e053 | 1186 | } |
8802d1fd | 1187 | |
341ebc2f JS |
1188 | /** |
1189 | * block_dirty_bitmap_lookup: | |
1190 | * Return a dirty bitmap (if present), after validating | |
1191 | * the node reference and bitmap names. | |
1192 | * | |
1193 | * @node: The name of the BDS node to search for bitmaps | |
1194 | * @name: The name of the bitmap to search for | |
1195 | * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. | |
341ebc2f JS |
1196 | * @errp: Output pointer for error information. Can be NULL. |
1197 | * | |
1198 | * @return: A bitmap object on success, or NULL on failure. | |
1199 | */ | |
1200 | static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, | |
1201 | const char *name, | |
1202 | BlockDriverState **pbs, | |
341ebc2f JS |
1203 | Error **errp) |
1204 | { | |
1205 | BlockDriverState *bs; | |
1206 | BdrvDirtyBitmap *bitmap; | |
341ebc2f JS |
1207 | |
1208 | if (!node) { | |
1209 | error_setg(errp, "Node cannot be NULL"); | |
1210 | return NULL; | |
1211 | } | |
1212 | if (!name) { | |
1213 | error_setg(errp, "Bitmap name cannot be NULL"); | |
1214 | return NULL; | |
1215 | } | |
1216 | bs = bdrv_lookup_bs(node, node, NULL); | |
1217 | if (!bs) { | |
1218 | error_setg(errp, "Node '%s' not found", node); | |
1219 | return NULL; | |
1220 | } | |
1221 | ||
341ebc2f JS |
1222 | bitmap = bdrv_find_dirty_bitmap(bs, name); |
1223 | if (!bitmap) { | |
1224 | error_setg(errp, "Dirty bitmap '%s' not found", name); | |
2119882c | 1225 | return NULL; |
341ebc2f JS |
1226 | } |
1227 | ||
1228 | if (pbs) { | |
1229 | *pbs = bs; | |
1230 | } | |
341ebc2f JS |
1231 | |
1232 | return bitmap; | |
341ebc2f JS |
1233 | } |
1234 | ||
b756b9ce | 1235 | /* New and old BlockDriverState structs for atomic group operations */ |
ba0c86a3 | 1236 | |
50f43f0f | 1237 | typedef struct BlkActionState BlkActionState; |
ba0c86a3 | 1238 | |
50f43f0f JS |
1239 | /** |
1240 | * BlkActionOps: | |
1241 | * Table of operations that define an Action. | |
1242 | * | |
1243 | * @instance_size: Size of state struct, in bytes. | |
1244 | * @prepare: Prepare the work, must NOT be NULL. | |
1245 | * @commit: Commit the changes, can be NULL. | |
1246 | * @abort: Abort the changes on fail, can be NULL. | |
1247 | * @clean: Clean up resources after all transaction actions have called | |
1248 | * commit() or abort(). Can be NULL. | |
1249 | * | |
1250 | * Only prepare() may fail. In a single transaction, only one of commit() or | |
1251 | * abort() will be called. clean() will always be called if it is present. | |
1252 | */ | |
1253 | typedef struct BlkActionOps { | |
ba0c86a3 | 1254 | size_t instance_size; |
50f43f0f JS |
1255 | void (*prepare)(BlkActionState *common, Error **errp); |
1256 | void (*commit)(BlkActionState *common); | |
1257 | void (*abort)(BlkActionState *common); | |
1258 | void (*clean)(BlkActionState *common); | |
1259 | } BlkActionOps; | |
ba0c86a3 | 1260 | |
50f43f0f JS |
1261 | /** |
1262 | * BlkActionState: | |
1263 | * Describes one Action's state within a Transaction. | |
1264 | * | |
1265 | * @action: QAPI-defined enum identifying which Action to perform. | |
1266 | * @ops: Table of ActionOps this Action can perform. | |
94d16a64 | 1267 | * @block_job_txn: Transaction which this action belongs to. |
50f43f0f JS |
1268 | * @entry: List membership for all Actions in this Transaction. |
1269 | * | |
1270 | * This structure must be arranged as first member in a subclassed type, | |
1271 | * assuming that the compiler will also arrange it to the same offsets as the | |
1272 | * base class. | |
ba0c86a3 | 1273 | */ |
50f43f0f | 1274 | struct BlkActionState { |
c8a83e85 | 1275 | TransactionAction *action; |
50f43f0f | 1276 | const BlkActionOps *ops; |
62c9e416 | 1277 | JobTxn *block_job_txn; |
94d16a64 | 1278 | TransactionProperties *txn_props; |
f4de0f8c | 1279 | QTAILQ_ENTRY(BlkActionState) entry; |
ba0c86a3 WX |
1280 | }; |
1281 | ||
bbe86010 WX |
1282 | /* internal snapshot private data */ |
1283 | typedef struct InternalSnapshotState { | |
50f43f0f | 1284 | BlkActionState common; |
bbe86010 WX |
1285 | BlockDriverState *bs; |
1286 | QEMUSnapshotInfo sn; | |
507306cc | 1287 | bool created; |
bbe86010 WX |
1288 | } InternalSnapshotState; |
1289 | ||
94d16a64 JS |
1290 | |
1291 | static int action_check_completion_mode(BlkActionState *s, Error **errp) | |
1292 | { | |
1293 | if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
1294 | error_setg(errp, | |
1295 | "Action '%s' does not support Transaction property " | |
1296 | "completion-mode = %s", | |
977c736f MA |
1297 | TransactionActionKind_str(s->action->type), |
1298 | ActionCompletionMode_str(s->txn_props->completion_mode)); | |
94d16a64 JS |
1299 | return -1; |
1300 | } | |
1301 | return 0; | |
1302 | } | |
1303 | ||
50f43f0f | 1304 | static void internal_snapshot_prepare(BlkActionState *common, |
bbe86010 WX |
1305 | Error **errp) |
1306 | { | |
f70edf99 | 1307 | Error *local_err = NULL; |
bbe86010 WX |
1308 | const char *device; |
1309 | const char *name; | |
1310 | BlockDriverState *bs; | |
1311 | QEMUSnapshotInfo old_sn, *sn; | |
1312 | bool ret; | |
1313 | qemu_timeval tv; | |
1314 | BlockdevSnapshotInternal *internal; | |
1315 | InternalSnapshotState *state; | |
a36e458c | 1316 | AioContext *aio_context; |
bbe86010 WX |
1317 | int ret1; |
1318 | ||
6a8f9661 | 1319 | g_assert(common->action->type == |
bbe86010 | 1320 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); |
32bafa8f | 1321 | internal = common->action->u.blockdev_snapshot_internal_sync.data; |
bbe86010 WX |
1322 | state = DO_UPCAST(InternalSnapshotState, common, common); |
1323 | ||
1324 | /* 1. parse input */ | |
1325 | device = internal->device; | |
1326 | name = internal->name; | |
1327 | ||
1328 | /* 2. check for validation */ | |
94d16a64 JS |
1329 | if (action_check_completion_mode(common, errp) < 0) { |
1330 | return; | |
1331 | } | |
1332 | ||
75dfd402 KW |
1333 | bs = qmp_get_root_bs(device, errp); |
1334 | if (!bs) { | |
bbe86010 WX |
1335 | return; |
1336 | } | |
1337 | ||
a36e458c SH |
1338 | aio_context = bdrv_get_aio_context(bs); |
1339 | aio_context_acquire(aio_context); | |
5d6e96ef | 1340 | |
507306cc | 1341 | state->bs = bs; |
a36e458c SH |
1342 | |
1343 | /* Paired with .clean() */ | |
507306cc FZ |
1344 | bdrv_drained_begin(bs); |
1345 | ||
3dc7ca3c | 1346 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { |
a36e458c | 1347 | goto out; |
3dc7ca3c SH |
1348 | } |
1349 | ||
bbe86010 | 1350 | if (bdrv_is_read_only(bs)) { |
81e5f78a | 1351 | error_setg(errp, "Device '%s' is read only", device); |
a36e458c | 1352 | goto out; |
bbe86010 WX |
1353 | } |
1354 | ||
1355 | if (!bdrv_can_snapshot(bs)) { | |
81e5f78a AG |
1356 | error_setg(errp, "Block format '%s' used by device '%s' " |
1357 | "does not support internal snapshots", | |
1358 | bs->drv->format_name, device); | |
a36e458c | 1359 | goto out; |
bbe86010 WX |
1360 | } |
1361 | ||
1362 | if (!strlen(name)) { | |
1363 | error_setg(errp, "Name is empty"); | |
a36e458c | 1364 | goto out; |
bbe86010 WX |
1365 | } |
1366 | ||
1367 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1368 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1369 | &local_err); | |
1370 | if (local_err) { | |
1371 | error_propagate(errp, local_err); | |
a36e458c | 1372 | goto out; |
bbe86010 WX |
1373 | } else if (ret) { |
1374 | error_setg(errp, | |
1375 | "Snapshot with name '%s' already exists on device '%s'", | |
1376 | name, device); | |
a36e458c | 1377 | goto out; |
bbe86010 WX |
1378 | } |
1379 | ||
1380 | /* 3. take the snapshot */ | |
1381 | sn = &state->sn; | |
1382 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1383 | qemu_gettimeofday(&tv); | |
1384 | sn->date_sec = tv.tv_sec; | |
1385 | sn->date_nsec = tv.tv_usec * 1000; | |
1386 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1387 | ||
1388 | ret1 = bdrv_snapshot_create(bs, sn); | |
1389 | if (ret1 < 0) { | |
1390 | error_setg_errno(errp, -ret1, | |
1391 | "Failed to create snapshot '%s' on device '%s'", | |
1392 | name, device); | |
a36e458c | 1393 | goto out; |
bbe86010 WX |
1394 | } |
1395 | ||
1396 | /* 4. succeed, mark a snapshot is created */ | |
507306cc | 1397 | state->created = true; |
a36e458c SH |
1398 | |
1399 | out: | |
1400 | aio_context_release(aio_context); | |
bbe86010 WX |
1401 | } |
1402 | ||
50f43f0f | 1403 | static void internal_snapshot_abort(BlkActionState *common) |
bbe86010 WX |
1404 | { |
1405 | InternalSnapshotState *state = | |
1406 | DO_UPCAST(InternalSnapshotState, common, common); | |
1407 | BlockDriverState *bs = state->bs; | |
1408 | QEMUSnapshotInfo *sn = &state->sn; | |
a36e458c | 1409 | AioContext *aio_context; |
bbe86010 WX |
1410 | Error *local_error = NULL; |
1411 | ||
507306cc | 1412 | if (!state->created) { |
bbe86010 WX |
1413 | return; |
1414 | } | |
1415 | ||
a36e458c SH |
1416 | aio_context = bdrv_get_aio_context(state->bs); |
1417 | aio_context_acquire(aio_context); | |
1418 | ||
bbe86010 | 1419 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { |
c29b77f9 MA |
1420 | error_reportf_err(local_error, |
1421 | "Failed to delete snapshot with id '%s' and " | |
1422 | "name '%s' on device '%s' in abort: ", | |
1423 | sn->id_str, sn->name, | |
1424 | bdrv_get_device_name(bs)); | |
bbe86010 | 1425 | } |
a36e458c SH |
1426 | |
1427 | aio_context_release(aio_context); | |
bbe86010 WX |
1428 | } |
1429 | ||
50f43f0f | 1430 | static void internal_snapshot_clean(BlkActionState *common) |
5d6e96ef SH |
1431 | { |
1432 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, | |
1433 | common, common); | |
a36e458c | 1434 | AioContext *aio_context; |
5d6e96ef | 1435 | |
a36e458c SH |
1436 | if (!state->bs) { |
1437 | return; | |
5d6e96ef | 1438 | } |
a36e458c SH |
1439 | |
1440 | aio_context = bdrv_get_aio_context(state->bs); | |
1441 | aio_context_acquire(aio_context); | |
1442 | ||
1443 | bdrv_drained_end(state->bs); | |
1444 | ||
1445 | aio_context_release(aio_context); | |
5d6e96ef SH |
1446 | } |
1447 | ||
ba0c86a3 | 1448 | /* external snapshot private data */ |
ba5d6ab6 | 1449 | typedef struct ExternalSnapshotState { |
50f43f0f | 1450 | BlkActionState common; |
8802d1fd JC |
1451 | BlockDriverState *old_bs; |
1452 | BlockDriverState *new_bs; | |
067acf28 | 1453 | bool overlay_appended; |
ba5d6ab6 | 1454 | } ExternalSnapshotState; |
8802d1fd | 1455 | |
50f43f0f | 1456 | static void external_snapshot_prepare(BlkActionState *common, |
9b9877ee WX |
1457 | Error **errp) |
1458 | { | |
5b363937 | 1459 | int flags = 0; |
43de7e2d | 1460 | QDict *options = NULL; |
9b9877ee | 1461 | Error *local_err = NULL; |
43de7e2d | 1462 | /* Device and node name of the image to generate the snapshot from */ |
e2a31e87 | 1463 | const char *device; |
0901f67e | 1464 | const char *node_name; |
43de7e2d AG |
1465 | /* Reference to the new image (for 'blockdev-snapshot') */ |
1466 | const char *snapshot_ref; | |
1467 | /* File name of the new image (for 'blockdev-snapshot-sync') */ | |
e2a31e87 | 1468 | const char *new_image_file; |
ba5d6ab6 SH |
1469 | ExternalSnapshotState *state = |
1470 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1471 | TransactionAction *action = common->action; |
2d24b60b | 1472 | AioContext *aio_context; |
d29d3d1f | 1473 | uint64_t perm, shared; |
9b9877ee | 1474 | |
43de7e2d AG |
1475 | /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar |
1476 | * purpose but a different set of parameters */ | |
1477 | switch (action->type) { | |
1478 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: | |
1479 | { | |
32bafa8f | 1480 | BlockdevSnapshot *s = action->u.blockdev_snapshot.data; |
43de7e2d AG |
1481 | device = s->node; |
1482 | node_name = s->node; | |
1483 | new_image_file = NULL; | |
1484 | snapshot_ref = s->overlay; | |
1485 | } | |
1486 | break; | |
1487 | case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: | |
1488 | { | |
32bafa8f | 1489 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
43de7e2d AG |
1490 | device = s->has_device ? s->device : NULL; |
1491 | node_name = s->has_node_name ? s->node_name : NULL; | |
1492 | new_image_file = s->snapshot_file; | |
1493 | snapshot_ref = NULL; | |
1494 | } | |
1495 | break; | |
1496 | default: | |
1497 | g_assert_not_reached(); | |
e2a31e87 WX |
1498 | } |
1499 | ||
1500 | /* start processing */ | |
94d16a64 JS |
1501 | if (action_check_completion_mode(common, errp) < 0) { |
1502 | return; | |
1503 | } | |
1504 | ||
43de7e2d AG |
1505 | state->old_bs = bdrv_lookup_bs(device, node_name, errp); |
1506 | if (!state->old_bs) { | |
9b9877ee WX |
1507 | return; |
1508 | } | |
1509 | ||
2d24b60b SH |
1510 | aio_context = bdrv_get_aio_context(state->old_bs); |
1511 | aio_context_acquire(aio_context); | |
1512 | ||
1513 | /* Paired with .clean() */ | |
da763e83 | 1514 | bdrv_drained_begin(state->old_bs); |
5d6e96ef | 1515 | |
ba5d6ab6 | 1516 | if (!bdrv_is_inserted(state->old_bs)) { |
c6bd8c70 | 1517 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
2d24b60b | 1518 | goto out; |
9b9877ee WX |
1519 | } |
1520 | ||
3718d8ab FZ |
1521 | if (bdrv_op_is_blocked(state->old_bs, |
1522 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
2d24b60b | 1523 | goto out; |
9b9877ee WX |
1524 | } |
1525 | ||
ba5d6ab6 SH |
1526 | if (!bdrv_is_read_only(state->old_bs)) { |
1527 | if (bdrv_flush(state->old_bs)) { | |
c6bd8c70 | 1528 | error_setg(errp, QERR_IO_ERROR); |
2d24b60b | 1529 | goto out; |
9b9877ee WX |
1530 | } |
1531 | } | |
1532 | ||
43de7e2d | 1533 | if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { |
32bafa8f | 1534 | BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; |
43de7e2d AG |
1535 | const char *format = s->has_format ? s->format : "qcow2"; |
1536 | enum NewImageMode mode; | |
1537 | const char *snapshot_node_name = | |
1538 | s->has_snapshot_node_name ? s->snapshot_node_name : NULL; | |
9b9877ee | 1539 | |
43de7e2d | 1540 | if (node_name && !snapshot_node_name) { |
a2bb6f8c | 1541 | error_setg(errp, "New overlay node name missing"); |
2d24b60b | 1542 | goto out; |
9b9877ee | 1543 | } |
9b9877ee | 1544 | |
43de7e2d AG |
1545 | if (snapshot_node_name && |
1546 | bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { | |
a2bb6f8c | 1547 | error_setg(errp, "New overlay node name already in use"); |
2d24b60b | 1548 | goto out; |
43de7e2d AG |
1549 | } |
1550 | ||
1551 | flags = state->old_bs->open_flags; | |
2a32c6e8 JS |
1552 | flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_COPY_ON_READ); |
1553 | flags |= BDRV_O_NO_BACKING; | |
43de7e2d AG |
1554 | |
1555 | /* create new image w/backing file */ | |
1556 | mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1557 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
f86b8b58 KW |
1558 | int64_t size = bdrv_getlength(state->old_bs); |
1559 | if (size < 0) { | |
1560 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
2d24b60b | 1561 | goto out; |
f86b8b58 | 1562 | } |
f30c66ba | 1563 | bdrv_refresh_filename(state->old_bs); |
43de7e2d AG |
1564 | bdrv_img_create(new_image_file, format, |
1565 | state->old_bs->filename, | |
1566 | state->old_bs->drv->format_name, | |
9217283d | 1567 | NULL, size, flags, false, &local_err); |
43de7e2d AG |
1568 | if (local_err) { |
1569 | error_propagate(errp, local_err); | |
2d24b60b | 1570 | goto out; |
43de7e2d AG |
1571 | } |
1572 | } | |
1573 | ||
1574 | options = qdict_new(); | |
d52e1a0e | 1575 | if (snapshot_node_name) { |
46f5ac20 | 1576 | qdict_put_str(options, "node-name", snapshot_node_name); |
43de7e2d | 1577 | } |
46f5ac20 | 1578 | qdict_put_str(options, "driver", format); |
0901f67e BC |
1579 | } |
1580 | ||
5b363937 HR |
1581 | state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, |
1582 | errp); | |
f67503e5 | 1583 | /* We will manually add the backing_hd field to the bs later */ |
5b363937 | 1584 | if (!state->new_bs) { |
2d24b60b | 1585 | goto out; |
43de7e2d AG |
1586 | } |
1587 | ||
d29d3d1f KW |
1588 | /* |
1589 | * Allow attaching a backing file to an overlay that's already in use only | |
1590 | * if the parents don't assume that they are already seeing a valid image. | |
1591 | * (Specifically, allow it as a mirror target, which is write-only access.) | |
1592 | */ | |
1593 | bdrv_get_cumulative_perm(state->new_bs, &perm, &shared); | |
1594 | if (perm & BLK_PERM_CONSISTENT_READ) { | |
a2bb6f8c | 1595 | error_setg(errp, "The overlay is already in use"); |
2d24b60b | 1596 | goto out; |
43de7e2d AG |
1597 | } |
1598 | ||
43de7e2d | 1599 | if (state->new_bs->backing != NULL) { |
a2bb6f8c | 1600 | error_setg(errp, "The overlay already has a backing image"); |
2d24b60b | 1601 | goto out; |
08b24cfe AG |
1602 | } |
1603 | ||
1604 | if (!state->new_bs->drv->supports_backing) { | |
a2bb6f8c | 1605 | error_setg(errp, "The overlay does not support backing images"); |
2d24b60b | 1606 | goto out; |
b2c2832c KW |
1607 | } |
1608 | ||
1609 | /* This removes our old bs and adds the new bs. This is an operation that | |
1610 | * can fail, so we need to do it in .prepare; undoing it for abort is | |
1611 | * always possible. */ | |
1612 | bdrv_ref(state->new_bs); | |
1613 | bdrv_append(state->new_bs, state->old_bs, &local_err); | |
1614 | if (local_err) { | |
1615 | error_propagate(errp, local_err); | |
2d24b60b | 1616 | goto out; |
9b9877ee | 1617 | } |
067acf28 | 1618 | state->overlay_appended = true; |
2d24b60b SH |
1619 | |
1620 | out: | |
1621 | aio_context_release(aio_context); | |
9b9877ee WX |
1622 | } |
1623 | ||
50f43f0f | 1624 | static void external_snapshot_commit(BlkActionState *common) |
3b0047e8 | 1625 | { |
ba5d6ab6 SH |
1626 | ExternalSnapshotState *state = |
1627 | DO_UPCAST(ExternalSnapshotState, common, common); | |
2d24b60b SH |
1628 | AioContext *aio_context; |
1629 | ||
1630 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1631 | aio_context_acquire(aio_context); | |
ba0c86a3 | 1632 | |
3b0047e8 WX |
1633 | /* We don't need (or want) to use the transactional |
1634 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1635 | * don't want to abort all of them if one of them fails the reopen */ | |
d3faa13e | 1636 | if (!atomic_read(&state->old_bs->copy_on_read)) { |
1b57774f | 1637 | bdrv_reopen_set_read_only(state->old_bs, true, NULL); |
4c844983 | 1638 | } |
2d24b60b SH |
1639 | |
1640 | aio_context_release(aio_context); | |
3b0047e8 WX |
1641 | } |
1642 | ||
50f43f0f | 1643 | static void external_snapshot_abort(BlkActionState *common) |
96b86bf7 | 1644 | { |
ba5d6ab6 SH |
1645 | ExternalSnapshotState *state = |
1646 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1647 | if (state->new_bs) { | |
067acf28 | 1648 | if (state->overlay_appended) { |
2d24b60b | 1649 | AioContext *aio_context; |
377410f6 SL |
1650 | AioContext *tmp_context; |
1651 | int ret; | |
2d24b60b SH |
1652 | |
1653 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1654 | aio_context_acquire(aio_context); | |
1655 | ||
719fc28c JC |
1656 | bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() |
1657 | close state->old_bs; we need it */ | |
1658 | bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); | |
377410f6 SL |
1659 | |
1660 | /* | |
1661 | * The call to bdrv_set_backing_hd() above returns state->old_bs to | |
1662 | * the main AioContext. As we're still going to be using it, return | |
1663 | * it to the AioContext it was before. | |
1664 | */ | |
1665 | tmp_context = bdrv_get_aio_context(state->old_bs); | |
1666 | if (aio_context != tmp_context) { | |
1667 | aio_context_release(aio_context); | |
1668 | aio_context_acquire(tmp_context); | |
1669 | ||
1670 | ret = bdrv_try_set_aio_context(state->old_bs, | |
1671 | aio_context, NULL); | |
1672 | assert(ret == 0); | |
1673 | ||
1674 | aio_context_release(tmp_context); | |
1675 | aio_context_acquire(aio_context); | |
1676 | } | |
1677 | ||
5fe31c25 | 1678 | bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); |
719fc28c | 1679 | bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ |
2d24b60b SH |
1680 | |
1681 | aio_context_release(aio_context); | |
b2c2832c | 1682 | } |
96b86bf7 | 1683 | } |
da763e83 FZ |
1684 | } |
1685 | ||
50f43f0f | 1686 | static void external_snapshot_clean(BlkActionState *common) |
da763e83 FZ |
1687 | { |
1688 | ExternalSnapshotState *state = | |
1689 | DO_UPCAST(ExternalSnapshotState, common, common); | |
2d24b60b SH |
1690 | AioContext *aio_context; |
1691 | ||
1692 | if (!state->old_bs) { | |
1693 | return; | |
5d6e96ef | 1694 | } |
2d24b60b SH |
1695 | |
1696 | aio_context = bdrv_get_aio_context(state->old_bs); | |
1697 | aio_context_acquire(aio_context); | |
1698 | ||
1699 | bdrv_drained_end(state->old_bs); | |
1700 | bdrv_unref(state->new_bs); | |
1701 | ||
1702 | aio_context_release(aio_context); | |
96b86bf7 WX |
1703 | } |
1704 | ||
3037f364 | 1705 | typedef struct DriveBackupState { |
50f43f0f | 1706 | BlkActionState common; |
3037f364 SH |
1707 | BlockDriverState *bs; |
1708 | BlockJob *job; | |
1709 | } DriveBackupState; | |
1710 | ||
2288ccfa SL |
1711 | static BlockJob *do_backup_common(BackupCommon *backup, |
1712 | BlockDriverState *bs, | |
1713 | BlockDriverState *target_bs, | |
1714 | AioContext *aio_context, | |
1715 | JobTxn *txn, Error **errp); | |
78f51fde | 1716 | |
50f43f0f | 1717 | static void drive_backup_prepare(BlkActionState *common, Error **errp) |
3037f364 SH |
1718 | { |
1719 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1720 | DriveBackup *backup; | |
2288ccfa SL |
1721 | BlockDriverState *bs; |
1722 | BlockDriverState *target_bs; | |
1723 | BlockDriverState *source = NULL; | |
66d56054 | 1724 | AioContext *aio_context; |
3ea67e08 | 1725 | AioContext *old_context; |
2288ccfa | 1726 | QDict *options; |
3037f364 | 1727 | Error *local_err = NULL; |
2288ccfa SL |
1728 | int flags; |
1729 | int64_t size; | |
1730 | bool set_backing_hd = false; | |
3ea67e08 | 1731 | int ret; |
3037f364 | 1732 | |
6a8f9661 | 1733 | assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); |
32bafa8f | 1734 | backup = common->action->u.drive_backup.data; |
3037f364 | 1735 | |
2288ccfa SL |
1736 | if (!backup->has_mode) { |
1737 | backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1738 | } | |
1739 | ||
85c9d133 | 1740 | bs = bdrv_lookup_bs(backup->device, backup->device, errp); |
b7e4fa22 | 1741 | if (!bs) { |
1fdd4b7b FZ |
1742 | return; |
1743 | } | |
1744 | ||
2288ccfa SL |
1745 | if (!bs->drv) { |
1746 | error_setg(errp, "Device has no medium"); | |
1747 | return; | |
1748 | } | |
1749 | ||
66d56054 SH |
1750 | aio_context = bdrv_get_aio_context(bs); |
1751 | aio_context_acquire(aio_context); | |
1752 | ||
1753 | /* Paired with .clean() */ | |
b7e4fa22 | 1754 | bdrv_drained_begin(bs); |
66d56054 | 1755 | |
2288ccfa SL |
1756 | if (!backup->has_format) { |
1757 | backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? | |
1758 | NULL : (char *) bs->drv->format_name; | |
1759 | } | |
1760 | ||
1761 | /* Early check to avoid creating target */ | |
1762 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { | |
1763 | goto out; | |
1764 | } | |
1765 | ||
1766 | flags = bs->open_flags | BDRV_O_RDWR; | |
1767 | ||
1768 | /* | |
1769 | * See if we have a backing HD we can use to create our new image | |
1770 | * on top of. | |
1771 | */ | |
1772 | if (backup->sync == MIRROR_SYNC_MODE_TOP) { | |
1773 | source = backing_bs(bs); | |
1774 | if (!source) { | |
1775 | backup->sync = MIRROR_SYNC_MODE_FULL; | |
1776 | } | |
1777 | } | |
1778 | if (backup->sync == MIRROR_SYNC_MODE_NONE) { | |
1779 | source = bs; | |
1780 | flags |= BDRV_O_NO_BACKING; | |
1781 | set_backing_hd = true; | |
1782 | } | |
1783 | ||
1784 | size = bdrv_getlength(bs); | |
1785 | if (size < 0) { | |
1786 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
1787 | goto out; | |
1788 | } | |
1789 | ||
1790 | if (backup->mode != NEW_IMAGE_MODE_EXISTING) { | |
1791 | assert(backup->format); | |
1792 | if (source) { | |
1793 | bdrv_refresh_filename(source); | |
1794 | bdrv_img_create(backup->target, backup->format, source->filename, | |
1795 | source->drv->format_name, NULL, | |
1796 | size, flags, false, &local_err); | |
1797 | } else { | |
1798 | bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, | |
1799 | size, flags, false, &local_err); | |
1800 | } | |
1801 | } | |
5d6e96ef | 1802 | |
84d18f06 | 1803 | if (local_err) { |
3037f364 | 1804 | error_propagate(errp, local_err); |
66d56054 | 1805 | goto out; |
3037f364 | 1806 | } |
66d56054 | 1807 | |
2288ccfa SL |
1808 | options = qdict_new(); |
1809 | qdict_put_str(options, "discard", "unmap"); | |
1810 | qdict_put_str(options, "detect-zeroes", "unmap"); | |
1811 | if (backup->format) { | |
1812 | qdict_put_str(options, "driver", backup->format); | |
1813 | } | |
1814 | ||
1815 | target_bs = bdrv_open(backup->target, NULL, options, flags, errp); | |
1816 | if (!target_bs) { | |
1817 | goto out; | |
1818 | } | |
1819 | ||
3ea67e08 SL |
1820 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ |
1821 | old_context = bdrv_get_aio_context(target_bs); | |
1822 | aio_context_release(aio_context); | |
1823 | aio_context_acquire(old_context); | |
1824 | ||
1825 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); | |
1826 | if (ret < 0) { | |
1827 | bdrv_unref(target_bs); | |
1828 | aio_context_release(old_context); | |
1829 | return; | |
1830 | } | |
1831 | ||
1832 | aio_context_release(old_context); | |
1833 | aio_context_acquire(aio_context); | |
1834 | ||
2288ccfa SL |
1835 | if (set_backing_hd) { |
1836 | bdrv_set_backing_hd(target_bs, source, &local_err); | |
1837 | if (local_err) { | |
1838 | goto unref; | |
1839 | } | |
1840 | } | |
1841 | ||
1842 | state->bs = bs; | |
1843 | ||
1844 | state->job = do_backup_common(qapi_DriveBackup_base(backup), | |
1845 | bs, target_bs, aio_context, | |
1846 | common->block_job_txn, errp); | |
1847 | ||
1848 | unref: | |
1849 | bdrv_unref(target_bs); | |
66d56054 SH |
1850 | out: |
1851 | aio_context_release(aio_context); | |
111049a4 | 1852 | } |
3037f364 | 1853 | |
111049a4 JS |
1854 | static void drive_backup_commit(BlkActionState *common) |
1855 | { | |
1856 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
66d56054 SH |
1857 | AioContext *aio_context; |
1858 | ||
1859 | aio_context = bdrv_get_aio_context(state->bs); | |
1860 | aio_context_acquire(aio_context); | |
1861 | ||
111049a4 | 1862 | assert(state->job); |
da01ff7f | 1863 | job_start(&state->job->job); |
66d56054 SH |
1864 | |
1865 | aio_context_release(aio_context); | |
3037f364 SH |
1866 | } |
1867 | ||
50f43f0f | 1868 | static void drive_backup_abort(BlkActionState *common) |
3037f364 SH |
1869 | { |
1870 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
3037f364 | 1871 | |
111049a4 | 1872 | if (state->job) { |
66d56054 SH |
1873 | AioContext *aio_context; |
1874 | ||
1875 | aio_context = bdrv_get_aio_context(state->bs); | |
1876 | aio_context_acquire(aio_context); | |
1877 | ||
3d70ff53 | 1878 | job_cancel_sync(&state->job->job); |
66d56054 SH |
1879 | |
1880 | aio_context_release(aio_context); | |
3037f364 SH |
1881 | } |
1882 | } | |
1883 | ||
50f43f0f | 1884 | static void drive_backup_clean(BlkActionState *common) |
5d6e96ef SH |
1885 | { |
1886 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
66d56054 | 1887 | AioContext *aio_context; |
5d6e96ef | 1888 | |
66d56054 SH |
1889 | if (!state->bs) { |
1890 | return; | |
5d6e96ef | 1891 | } |
66d56054 SH |
1892 | |
1893 | aio_context = bdrv_get_aio_context(state->bs); | |
1894 | aio_context_acquire(aio_context); | |
1895 | ||
1896 | bdrv_drained_end(state->bs); | |
1897 | ||
1898 | aio_context_release(aio_context); | |
5d6e96ef SH |
1899 | } |
1900 | ||
bd8baecd | 1901 | typedef struct BlockdevBackupState { |
50f43f0f | 1902 | BlkActionState common; |
bd8baecd FZ |
1903 | BlockDriverState *bs; |
1904 | BlockJob *job; | |
bd8baecd FZ |
1905 | } BlockdevBackupState; |
1906 | ||
50f43f0f | 1907 | static void blockdev_backup_prepare(BlkActionState *common, Error **errp) |
bd8baecd FZ |
1908 | { |
1909 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1910 | BlockdevBackup *backup; | |
5b7bfe51 SL |
1911 | BlockDriverState *bs; |
1912 | BlockDriverState *target_bs; | |
edd5adee | 1913 | AioContext *aio_context; |
3ea67e08 SL |
1914 | AioContext *old_context; |
1915 | int ret; | |
bd8baecd | 1916 | |
6a8f9661 | 1917 | assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); |
32bafa8f | 1918 | backup = common->action->u.blockdev_backup.data; |
bd8baecd | 1919 | |
930fe17f | 1920 | bs = bdrv_lookup_bs(backup->device, backup->device, errp); |
cef34eeb | 1921 | if (!bs) { |
ff52bf36 FZ |
1922 | return; |
1923 | } | |
1924 | ||
5b7bfe51 SL |
1925 | target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); |
1926 | if (!target_bs) { | |
bd8baecd FZ |
1927 | return; |
1928 | } | |
1929 | ||
3ea67e08 | 1930 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ |
edd5adee | 1931 | aio_context = bdrv_get_aio_context(bs); |
3ea67e08 SL |
1932 | old_context = bdrv_get_aio_context(target_bs); |
1933 | aio_context_acquire(old_context); | |
1934 | ||
1935 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); | |
1936 | if (ret < 0) { | |
1937 | aio_context_release(old_context); | |
1938 | return; | |
1939 | } | |
1940 | ||
1941 | aio_context_release(old_context); | |
edd5adee | 1942 | aio_context_acquire(aio_context); |
cef34eeb | 1943 | state->bs = bs; |
edd5adee SH |
1944 | |
1945 | /* Paired with .clean() */ | |
ff52bf36 | 1946 | bdrv_drained_begin(state->bs); |
bd8baecd | 1947 | |
5b7bfe51 SL |
1948 | state->job = do_backup_common(qapi_BlockdevBackup_base(backup), |
1949 | bs, target_bs, aio_context, | |
1950 | common->block_job_txn, errp); | |
edd5adee | 1951 | |
edd5adee | 1952 | aio_context_release(aio_context); |
111049a4 | 1953 | } |
bd8baecd | 1954 | |
111049a4 JS |
1955 | static void blockdev_backup_commit(BlkActionState *common) |
1956 | { | |
1957 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
edd5adee SH |
1958 | AioContext *aio_context; |
1959 | ||
1960 | aio_context = bdrv_get_aio_context(state->bs); | |
1961 | aio_context_acquire(aio_context); | |
1962 | ||
111049a4 | 1963 | assert(state->job); |
da01ff7f | 1964 | job_start(&state->job->job); |
edd5adee SH |
1965 | |
1966 | aio_context_release(aio_context); | |
bd8baecd FZ |
1967 | } |
1968 | ||
50f43f0f | 1969 | static void blockdev_backup_abort(BlkActionState *common) |
bd8baecd FZ |
1970 | { |
1971 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
bd8baecd | 1972 | |
111049a4 | 1973 | if (state->job) { |
edd5adee SH |
1974 | AioContext *aio_context; |
1975 | ||
1976 | aio_context = bdrv_get_aio_context(state->bs); | |
1977 | aio_context_acquire(aio_context); | |
1978 | ||
3d70ff53 | 1979 | job_cancel_sync(&state->job->job); |
edd5adee SH |
1980 | |
1981 | aio_context_release(aio_context); | |
bd8baecd FZ |
1982 | } |
1983 | } | |
1984 | ||
50f43f0f | 1985 | static void blockdev_backup_clean(BlkActionState *common) |
bd8baecd FZ |
1986 | { |
1987 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
edd5adee | 1988 | AioContext *aio_context; |
bd8baecd | 1989 | |
edd5adee SH |
1990 | if (!state->bs) { |
1991 | return; | |
bd8baecd | 1992 | } |
edd5adee SH |
1993 | |
1994 | aio_context = bdrv_get_aio_context(state->bs); | |
1995 | aio_context_acquire(aio_context); | |
1996 | ||
1997 | bdrv_drained_end(state->bs); | |
1998 | ||
1999 | aio_context_release(aio_context); | |
bd8baecd FZ |
2000 | } |
2001 | ||
df9a681d | 2002 | typedef struct BlockDirtyBitmapState { |
50f43f0f | 2003 | BlkActionState common; |
df9a681d FZ |
2004 | BdrvDirtyBitmap *bitmap; |
2005 | BlockDriverState *bs; | |
df9a681d FZ |
2006 | HBitmap *backup; |
2007 | bool prepared; | |
c6490447 | 2008 | bool was_enabled; |
df9a681d FZ |
2009 | } BlockDirtyBitmapState; |
2010 | ||
50f43f0f | 2011 | static void block_dirty_bitmap_add_prepare(BlkActionState *common, |
df9a681d FZ |
2012 | Error **errp) |
2013 | { | |
2014 | Error *local_err = NULL; | |
2015 | BlockDirtyBitmapAdd *action; | |
2016 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2017 | common, common); | |
2018 | ||
94d16a64 JS |
2019 | if (action_check_completion_mode(common, errp) < 0) { |
2020 | return; | |
2021 | } | |
2022 | ||
32bafa8f | 2023 | action = common->action->u.block_dirty_bitmap_add.data; |
df9a681d FZ |
2024 | /* AIO context taken and released within qmp_block_dirty_bitmap_add */ |
2025 | qmp_block_dirty_bitmap_add(action->node, action->name, | |
2026 | action->has_granularity, action->granularity, | |
fd5ae4cc | 2027 | action->has_persistent, action->persistent, |
0e2b7f09 | 2028 | action->has_disabled, action->disabled, |
df9a681d FZ |
2029 | &local_err); |
2030 | ||
2031 | if (!local_err) { | |
2032 | state->prepared = true; | |
2033 | } else { | |
2034 | error_propagate(errp, local_err); | |
2035 | } | |
2036 | } | |
2037 | ||
50f43f0f | 2038 | static void block_dirty_bitmap_add_abort(BlkActionState *common) |
df9a681d FZ |
2039 | { |
2040 | BlockDirtyBitmapAdd *action; | |
2041 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2042 | common, common); | |
2043 | ||
32bafa8f | 2044 | action = common->action->u.block_dirty_bitmap_add.data; |
df9a681d FZ |
2045 | /* Should not be able to fail: IF the bitmap was added via .prepare(), |
2046 | * then the node reference and bitmap name must have been valid. | |
2047 | */ | |
2048 | if (state->prepared) { | |
2049 | qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); | |
2050 | } | |
2051 | } | |
2052 | ||
50f43f0f | 2053 | static void block_dirty_bitmap_clear_prepare(BlkActionState *common, |
df9a681d FZ |
2054 | Error **errp) |
2055 | { | |
2056 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2057 | common, common); | |
2058 | BlockDirtyBitmap *action; | |
2059 | ||
94d16a64 JS |
2060 | if (action_check_completion_mode(common, errp) < 0) { |
2061 | return; | |
2062 | } | |
2063 | ||
32bafa8f | 2064 | action = common->action->u.block_dirty_bitmap_clear.data; |
df9a681d FZ |
2065 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2066 | action->name, | |
2067 | &state->bs, | |
df9a681d FZ |
2068 | errp); |
2069 | if (!state->bitmap) { | |
2070 | return; | |
2071 | } | |
2072 | ||
3ae96d66 | 2073 | if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_DEFAULT, errp)) { |
d6883bc9 | 2074 | return; |
df9a681d FZ |
2075 | } |
2076 | ||
2077 | bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); | |
df9a681d FZ |
2078 | } |
2079 | ||
5c4cf8b2 | 2080 | static void block_dirty_bitmap_restore(BlkActionState *common) |
df9a681d FZ |
2081 | { |
2082 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2083 | common, common); | |
2084 | ||
184dd9c4 | 2085 | if (state->backup) { |
56bd6624 | 2086 | bdrv_restore_dirty_bitmap(state->bitmap, state->backup); |
184dd9c4 | 2087 | } |
df9a681d FZ |
2088 | } |
2089 | ||
5c4cf8b2 | 2090 | static void block_dirty_bitmap_free_backup(BlkActionState *common) |
df9a681d FZ |
2091 | { |
2092 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2093 | common, common); | |
2094 | ||
2095 | hbitmap_free(state->backup); | |
2096 | } | |
2097 | ||
c6490447 VSO |
2098 | static void block_dirty_bitmap_enable_prepare(BlkActionState *common, |
2099 | Error **errp) | |
2100 | { | |
2101 | BlockDirtyBitmap *action; | |
2102 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2103 | common, common); | |
2104 | ||
2105 | if (action_check_completion_mode(common, errp) < 0) { | |
2106 | return; | |
2107 | } | |
2108 | ||
0e2b7f09 | 2109 | action = common->action->u.block_dirty_bitmap_enable.data; |
c6490447 VSO |
2110 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2111 | action->name, | |
2112 | NULL, | |
2113 | errp); | |
2114 | if (!state->bitmap) { | |
2115 | return; | |
2116 | } | |
2117 | ||
3ae96d66 | 2118 | if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
b053bb55 JS |
2119 | return; |
2120 | } | |
2121 | ||
c6490447 VSO |
2122 | state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); |
2123 | bdrv_enable_dirty_bitmap(state->bitmap); | |
2124 | } | |
2125 | ||
2126 | static void block_dirty_bitmap_enable_abort(BlkActionState *common) | |
2127 | { | |
2128 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2129 | common, common); | |
2130 | ||
2131 | if (!state->was_enabled) { | |
2132 | bdrv_disable_dirty_bitmap(state->bitmap); | |
2133 | } | |
2134 | } | |
2135 | ||
2136 | static void block_dirty_bitmap_disable_prepare(BlkActionState *common, | |
2137 | Error **errp) | |
2138 | { | |
2139 | BlockDirtyBitmap *action; | |
2140 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2141 | common, common); | |
2142 | ||
2143 | if (action_check_completion_mode(common, errp) < 0) { | |
2144 | return; | |
2145 | } | |
2146 | ||
0e2b7f09 | 2147 | action = common->action->u.block_dirty_bitmap_disable.data; |
c6490447 VSO |
2148 | state->bitmap = block_dirty_bitmap_lookup(action->node, |
2149 | action->name, | |
2150 | NULL, | |
2151 | errp); | |
2152 | if (!state->bitmap) { | |
2153 | return; | |
2154 | } | |
2155 | ||
3ae96d66 | 2156 | if (bdrv_dirty_bitmap_check(state->bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
b053bb55 JS |
2157 | return; |
2158 | } | |
2159 | ||
c6490447 VSO |
2160 | state->was_enabled = bdrv_dirty_bitmap_enabled(state->bitmap); |
2161 | bdrv_disable_dirty_bitmap(state->bitmap); | |
2162 | } | |
2163 | ||
2164 | static void block_dirty_bitmap_disable_abort(BlkActionState *common) | |
2165 | { | |
2166 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2167 | common, common); | |
2168 | ||
2169 | if (state->was_enabled) { | |
2170 | bdrv_enable_dirty_bitmap(state->bitmap); | |
2171 | } | |
2172 | } | |
2173 | ||
eff0829b VSO |
2174 | static BdrvDirtyBitmap *do_block_dirty_bitmap_merge( |
2175 | const char *node, const char *target, | |
2176 | BlockDirtyBitmapMergeSourceList *bitmaps, | |
2177 | HBitmap **backup, Error **errp); | |
360d4e4e | 2178 | |
6fd2e407 VSO |
2179 | static void block_dirty_bitmap_merge_prepare(BlkActionState *common, |
2180 | Error **errp) | |
2181 | { | |
2182 | BlockDirtyBitmapMerge *action; | |
2183 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2184 | common, common); | |
6fd2e407 VSO |
2185 | |
2186 | if (action_check_completion_mode(common, errp) < 0) { | |
2187 | return; | |
2188 | } | |
2189 | ||
0e2b7f09 | 2190 | action = common->action->u.block_dirty_bitmap_merge.data; |
6fd2e407 | 2191 | |
360d4e4e JS |
2192 | state->bitmap = do_block_dirty_bitmap_merge(action->node, action->target, |
2193 | action->bitmaps, &state->backup, | |
2194 | errp); | |
6fd2e407 VSO |
2195 | } |
2196 | ||
c4e4b0fa JS |
2197 | static BdrvDirtyBitmap *do_block_dirty_bitmap_remove( |
2198 | const char *node, const char *name, bool release, | |
2199 | BlockDriverState **bitmap_bs, Error **errp); | |
2200 | ||
2201 | static void block_dirty_bitmap_remove_prepare(BlkActionState *common, | |
2202 | Error **errp) | |
2203 | { | |
2204 | BlockDirtyBitmap *action; | |
2205 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2206 | common, common); | |
2207 | ||
2208 | if (action_check_completion_mode(common, errp) < 0) { | |
2209 | return; | |
2210 | } | |
2211 | ||
2212 | action = common->action->u.block_dirty_bitmap_remove.data; | |
2213 | ||
2214 | state->bitmap = do_block_dirty_bitmap_remove(action->node, action->name, | |
2215 | false, &state->bs, errp); | |
2216 | if (state->bitmap) { | |
2217 | bdrv_dirty_bitmap_skip_store(state->bitmap, true); | |
2218 | bdrv_dirty_bitmap_set_busy(state->bitmap, true); | |
2219 | } | |
2220 | } | |
2221 | ||
2222 | static void block_dirty_bitmap_remove_abort(BlkActionState *common) | |
2223 | { | |
2224 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2225 | common, common); | |
2226 | ||
2227 | if (state->bitmap) { | |
2228 | bdrv_dirty_bitmap_skip_store(state->bitmap, false); | |
2229 | bdrv_dirty_bitmap_set_busy(state->bitmap, false); | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | static void block_dirty_bitmap_remove_commit(BlkActionState *common) | |
2234 | { | |
2235 | BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, | |
2236 | common, common); | |
2237 | ||
2238 | bdrv_dirty_bitmap_set_busy(state->bitmap, false); | |
5deb6cbd | 2239 | bdrv_release_dirty_bitmap(state->bitmap); |
c4e4b0fa JS |
2240 | } |
2241 | ||
50f43f0f | 2242 | static void abort_prepare(BlkActionState *common, Error **errp) |
78b18b78 SH |
2243 | { |
2244 | error_setg(errp, "Transaction aborted using Abort action"); | |
2245 | } | |
2246 | ||
50f43f0f | 2247 | static void abort_commit(BlkActionState *common) |
78b18b78 | 2248 | { |
dfc6f865 | 2249 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
2250 | } |
2251 | ||
50f43f0f | 2252 | static const BlkActionOps actions[] = { |
43de7e2d AG |
2253 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { |
2254 | .instance_size = sizeof(ExternalSnapshotState), | |
2255 | .prepare = external_snapshot_prepare, | |
2256 | .commit = external_snapshot_commit, | |
2257 | .abort = external_snapshot_abort, | |
4ad6f3db | 2258 | .clean = external_snapshot_clean, |
43de7e2d | 2259 | }, |
c8a83e85 | 2260 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 2261 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
2262 | .prepare = external_snapshot_prepare, |
2263 | .commit = external_snapshot_commit, | |
2264 | .abort = external_snapshot_abort, | |
da763e83 | 2265 | .clean = external_snapshot_clean, |
ba0c86a3 | 2266 | }, |
3037f364 SH |
2267 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
2268 | .instance_size = sizeof(DriveBackupState), | |
2269 | .prepare = drive_backup_prepare, | |
111049a4 | 2270 | .commit = drive_backup_commit, |
3037f364 | 2271 | .abort = drive_backup_abort, |
5d6e96ef | 2272 | .clean = drive_backup_clean, |
3037f364 | 2273 | }, |
bd8baecd FZ |
2274 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { |
2275 | .instance_size = sizeof(BlockdevBackupState), | |
2276 | .prepare = blockdev_backup_prepare, | |
111049a4 | 2277 | .commit = blockdev_backup_commit, |
bd8baecd FZ |
2278 | .abort = blockdev_backup_abort, |
2279 | .clean = blockdev_backup_clean, | |
2280 | }, | |
78b18b78 | 2281 | [TRANSACTION_ACTION_KIND_ABORT] = { |
50f43f0f | 2282 | .instance_size = sizeof(BlkActionState), |
78b18b78 SH |
2283 | .prepare = abort_prepare, |
2284 | .commit = abort_commit, | |
2285 | }, | |
bbe86010 WX |
2286 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
2287 | .instance_size = sizeof(InternalSnapshotState), | |
2288 | .prepare = internal_snapshot_prepare, | |
2289 | .abort = internal_snapshot_abort, | |
5d6e96ef | 2290 | .clean = internal_snapshot_clean, |
bbe86010 | 2291 | }, |
df9a681d FZ |
2292 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { |
2293 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2294 | .prepare = block_dirty_bitmap_add_prepare, | |
2295 | .abort = block_dirty_bitmap_add_abort, | |
2296 | }, | |
2297 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { | |
2298 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2299 | .prepare = block_dirty_bitmap_clear_prepare, | |
5c4cf8b2 VSO |
2300 | .commit = block_dirty_bitmap_free_backup, |
2301 | .abort = block_dirty_bitmap_restore, | |
c6490447 | 2302 | }, |
0e2b7f09 | 2303 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE] = { |
c6490447 VSO |
2304 | .instance_size = sizeof(BlockDirtyBitmapState), |
2305 | .prepare = block_dirty_bitmap_enable_prepare, | |
2306 | .abort = block_dirty_bitmap_enable_abort, | |
2307 | }, | |
0e2b7f09 | 2308 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE] = { |
c6490447 VSO |
2309 | .instance_size = sizeof(BlockDirtyBitmapState), |
2310 | .prepare = block_dirty_bitmap_disable_prepare, | |
2311 | .abort = block_dirty_bitmap_disable_abort, | |
66da04dd | 2312 | }, |
0e2b7f09 | 2313 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE] = { |
6fd2e407 VSO |
2314 | .instance_size = sizeof(BlockDirtyBitmapState), |
2315 | .prepare = block_dirty_bitmap_merge_prepare, | |
2316 | .commit = block_dirty_bitmap_free_backup, | |
2317 | .abort = block_dirty_bitmap_restore, | |
2318 | }, | |
c4e4b0fa JS |
2319 | [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE] = { |
2320 | .instance_size = sizeof(BlockDirtyBitmapState), | |
2321 | .prepare = block_dirty_bitmap_remove_prepare, | |
2322 | .commit = block_dirty_bitmap_remove_commit, | |
2323 | .abort = block_dirty_bitmap_remove_abort, | |
2324 | }, | |
66da04dd JS |
2325 | /* Where are transactions for MIRROR, COMMIT and STREAM? |
2326 | * Although these blockjobs use transaction callbacks like the backup job, | |
2327 | * these jobs do not necessarily adhere to transaction semantics. | |
2328 | * These jobs may not fully undo all of their actions on abort, nor do they | |
2329 | * necessarily work in transactions with more than one job in them. | |
2330 | */ | |
ba0c86a3 WX |
2331 | }; |
2332 | ||
94d16a64 JS |
2333 | /** |
2334 | * Allocate a TransactionProperties structure if necessary, and fill | |
2335 | * that structure with desired defaults if they are unset. | |
2336 | */ | |
2337 | static TransactionProperties *get_transaction_properties( | |
2338 | TransactionProperties *props) | |
2339 | { | |
2340 | if (!props) { | |
2341 | props = g_new0(TransactionProperties, 1); | |
2342 | } | |
2343 | ||
2344 | if (!props->has_completion_mode) { | |
2345 | props->has_completion_mode = true; | |
2346 | props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; | |
2347 | } | |
2348 | ||
2349 | return props; | |
2350 | } | |
2351 | ||
8802d1fd | 2352 | /* |
b756b9ce SH |
2353 | * 'Atomic' group operations. The operations are performed as a set, and if |
2354 | * any fail then we roll back all operations in the group. | |
8802d1fd | 2355 | */ |
94d16a64 JS |
2356 | void qmp_transaction(TransactionActionList *dev_list, |
2357 | bool has_props, | |
2358 | struct TransactionProperties *props, | |
2359 | Error **errp) | |
8802d1fd | 2360 | { |
c8a83e85 | 2361 | TransactionActionList *dev_entry = dev_list; |
62c9e416 | 2362 | JobTxn *block_job_txn = NULL; |
50f43f0f | 2363 | BlkActionState *state, *next; |
43e17041 | 2364 | Error *local_err = NULL; |
8802d1fd | 2365 | |
f4de0f8c JS |
2366 | QTAILQ_HEAD(, BlkActionState) snap_bdrv_states; |
2367 | QTAILQ_INIT(&snap_bdrv_states); | |
8802d1fd | 2368 | |
94d16a64 | 2369 | /* Does this transaction get canceled as a group on failure? |
62c9e416 | 2370 | * If not, we don't really need to make a JobTxn. |
94d16a64 JS |
2371 | */ |
2372 | props = get_transaction_properties(props); | |
2373 | if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { | |
7eaa8fb5 | 2374 | block_job_txn = job_txn_new(); |
94d16a64 JS |
2375 | } |
2376 | ||
b756b9ce | 2377 | /* drain all i/o before any operations */ |
8802d1fd JC |
2378 | bdrv_drain_all(); |
2379 | ||
b756b9ce | 2380 | /* We don't do anything in this loop that commits us to the operations */ |
8802d1fd | 2381 | while (NULL != dev_entry) { |
c8a83e85 | 2382 | TransactionAction *dev_info = NULL; |
50f43f0f | 2383 | const BlkActionOps *ops; |
52e7c241 | 2384 | |
8802d1fd JC |
2385 | dev_info = dev_entry->value; |
2386 | dev_entry = dev_entry->next; | |
2387 | ||
6a8f9661 | 2388 | assert(dev_info->type < ARRAY_SIZE(actions)); |
ba0c86a3 | 2389 | |
6a8f9661 | 2390 | ops = &actions[dev_info->type]; |
aa3fe714 HR |
2391 | assert(ops->instance_size > 0); |
2392 | ||
ba5d6ab6 SH |
2393 | state = g_malloc0(ops->instance_size); |
2394 | state->ops = ops; | |
2395 | state->action = dev_info; | |
94d16a64 JS |
2396 | state->block_job_txn = block_job_txn; |
2397 | state->txn_props = props; | |
f4de0f8c | 2398 | QTAILQ_INSERT_TAIL(&snap_bdrv_states, state, entry); |
8802d1fd | 2399 | |
ba5d6ab6 | 2400 | state->ops->prepare(state, &local_err); |
84d18f06 | 2401 | if (local_err) { |
ba0c86a3 WX |
2402 | error_propagate(errp, local_err); |
2403 | goto delete_and_fail; | |
8802d1fd | 2404 | } |
8802d1fd JC |
2405 | } |
2406 | ||
f4de0f8c | 2407 | QTAILQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
2408 | if (state->ops->commit) { |
2409 | state->ops->commit(state); | |
2410 | } | |
8802d1fd JC |
2411 | } |
2412 | ||
2413 | /* success */ | |
2414 | goto exit; | |
2415 | ||
2416 | delete_and_fail: | |
b756b9ce | 2417 | /* failure, and it is all-or-none; roll back all operations */ |
f4de0f8c | 2418 | QTAILQ_FOREACH_REVERSE(state, &snap_bdrv_states, entry) { |
ba5d6ab6 SH |
2419 | if (state->ops->abort) { |
2420 | state->ops->abort(state); | |
ba0c86a3 | 2421 | } |
8802d1fd JC |
2422 | } |
2423 | exit: | |
f4de0f8c | 2424 | QTAILQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
ba5d6ab6 SH |
2425 | if (state->ops->clean) { |
2426 | state->ops->clean(state); | |
ba0c86a3 | 2427 | } |
ba5d6ab6 | 2428 | g_free(state); |
8802d1fd | 2429 | } |
94d16a64 JS |
2430 | if (!has_props) { |
2431 | qapi_free_TransactionProperties(props); | |
2432 | } | |
7eaa8fb5 | 2433 | job_txn_unref(block_job_txn); |
8802d1fd JC |
2434 | } |
2435 | ||
12d3ba82 BC |
2436 | void qmp_block_passwd(bool has_device, const char *device, |
2437 | bool has_node_name, const char *node_name, | |
2438 | const char *password, Error **errp) | |
666daa68 | 2439 | { |
c01c214b DB |
2440 | error_setg(errp, |
2441 | "Setting block passwords directly is no longer supported"); | |
666daa68 MA |
2442 | } |
2443 | ||
341ebc2f JS |
2444 | void qmp_block_dirty_bitmap_add(const char *node, const char *name, |
2445 | bool has_granularity, uint32_t granularity, | |
fd5ae4cc | 2446 | bool has_persistent, bool persistent, |
a6e2ca5f | 2447 | bool has_disabled, bool disabled, |
341ebc2f JS |
2448 | Error **errp) |
2449 | { | |
341ebc2f | 2450 | BlockDriverState *bs; |
fd5ae4cc | 2451 | BdrvDirtyBitmap *bitmap; |
91005a49 | 2452 | AioContext *aio_context; |
341ebc2f JS |
2453 | |
2454 | if (!name || name[0] == '\0') { | |
2455 | error_setg(errp, "Bitmap name cannot be empty"); | |
2456 | return; | |
2457 | } | |
2458 | ||
2459 | bs = bdrv_lookup_bs(node, node, errp); | |
2460 | if (!bs) { | |
2461 | return; | |
2462 | } | |
2463 | ||
91005a49 SL |
2464 | aio_context = bdrv_get_aio_context(bs); |
2465 | aio_context_acquire(aio_context); | |
2466 | ||
341ebc2f JS |
2467 | if (has_granularity) { |
2468 | if (granularity < 512 || !is_power_of_2(granularity)) { | |
2469 | error_setg(errp, "Granularity must be power of 2 " | |
2470 | "and at least 512"); | |
91005a49 | 2471 | goto out; |
341ebc2f JS |
2472 | } |
2473 | } else { | |
2474 | /* Default to cluster size, if available: */ | |
2475 | granularity = bdrv_get_default_bitmap_granularity(bs); | |
2476 | } | |
2477 | ||
fd5ae4cc VSO |
2478 | if (!has_persistent) { |
2479 | persistent = false; | |
2480 | } | |
eb738bb5 | 2481 | |
a6e2ca5f VSO |
2482 | if (!has_disabled) { |
2483 | disabled = false; | |
2484 | } | |
2485 | ||
d2c3080e VSO |
2486 | if (persistent && |
2487 | !bdrv_can_store_new_dirty_bitmap(bs, name, granularity, errp)) | |
2488 | { | |
91005a49 | 2489 | goto out; |
fd5ae4cc VSO |
2490 | } |
2491 | ||
2492 | bitmap = bdrv_create_dirty_bitmap(bs, granularity, name, errp); | |
eb738bb5 | 2493 | if (bitmap == NULL) { |
91005a49 | 2494 | goto out; |
fd5ae4cc | 2495 | } |
eb738bb5 | 2496 | |
a6e2ca5f VSO |
2497 | if (disabled) { |
2498 | bdrv_disable_dirty_bitmap(bitmap); | |
2499 | } | |
2500 | ||
796a3798 | 2501 | bdrv_dirty_bitmap_set_persistence(bitmap, persistent); |
91005a49 SL |
2502 | |
2503 | out: | |
2504 | aio_context_release(aio_context); | |
341ebc2f JS |
2505 | } |
2506 | ||
c4e4b0fa JS |
2507 | static BdrvDirtyBitmap *do_block_dirty_bitmap_remove( |
2508 | const char *node, const char *name, bool release, | |
2509 | BlockDriverState **bitmap_bs, Error **errp) | |
341ebc2f | 2510 | { |
341ebc2f JS |
2511 | BlockDriverState *bs; |
2512 | BdrvDirtyBitmap *bitmap; | |
91005a49 | 2513 | AioContext *aio_context; |
341ebc2f | 2514 | |
2119882c | 2515 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); |
341ebc2f | 2516 | if (!bitmap || !bs) { |
c4e4b0fa | 2517 | return NULL; |
341ebc2f JS |
2518 | } |
2519 | ||
91005a49 SL |
2520 | aio_context = bdrv_get_aio_context(bs); |
2521 | aio_context_acquire(aio_context); | |
2522 | ||
c3edf13c JS |
2523 | if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_BUSY | BDRV_BITMAP_RO, |
2524 | errp)) { | |
91005a49 | 2525 | aio_context_release(aio_context); |
c4e4b0fa | 2526 | return NULL; |
9bd2b08f | 2527 | } |
5c36c1af | 2528 | |
d2c3080e VSO |
2529 | if (bdrv_dirty_bitmap_get_persistence(bitmap) && |
2530 | bdrv_remove_persistent_dirty_bitmap(bs, name, errp) < 0) | |
2531 | { | |
91005a49 SL |
2532 | aio_context_release(aio_context); |
2533 | return NULL; | |
5c36c1af VSO |
2534 | } |
2535 | ||
c4e4b0fa | 2536 | if (release) { |
5deb6cbd | 2537 | bdrv_release_dirty_bitmap(bitmap); |
c4e4b0fa JS |
2538 | } |
2539 | ||
2540 | if (bitmap_bs) { | |
2541 | *bitmap_bs = bs; | |
2542 | } | |
2543 | ||
91005a49 | 2544 | aio_context_release(aio_context); |
c4e4b0fa JS |
2545 | return release ? NULL : bitmap; |
2546 | } | |
2547 | ||
2548 | void qmp_block_dirty_bitmap_remove(const char *node, const char *name, | |
2549 | Error **errp) | |
2550 | { | |
2551 | do_block_dirty_bitmap_remove(node, name, true, NULL, errp); | |
341ebc2f JS |
2552 | } |
2553 | ||
e74e6b78 JS |
2554 | /** |
2555 | * Completely clear a bitmap, for the purposes of synchronizing a bitmap | |
2556 | * immediately after a full backup operation. | |
2557 | */ | |
2558 | void qmp_block_dirty_bitmap_clear(const char *node, const char *name, | |
2559 | Error **errp) | |
2560 | { | |
e74e6b78 JS |
2561 | BdrvDirtyBitmap *bitmap; |
2562 | BlockDriverState *bs; | |
2563 | ||
2119882c | 2564 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); |
e74e6b78 JS |
2565 | if (!bitmap || !bs) { |
2566 | return; | |
2567 | } | |
2568 | ||
3ae96d66 | 2569 | if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) { |
d6883bc9 | 2570 | return; |
e74e6b78 JS |
2571 | } |
2572 | ||
df9a681d | 2573 | bdrv_clear_dirty_bitmap(bitmap, NULL); |
e74e6b78 JS |
2574 | } |
2575 | ||
0e2b7f09 | 2576 | void qmp_block_dirty_bitmap_enable(const char *node, const char *name, |
5c5d2e50 VSO |
2577 | Error **errp) |
2578 | { | |
2579 | BlockDriverState *bs; | |
2580 | BdrvDirtyBitmap *bitmap; | |
2581 | ||
2582 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2583 | if (!bitmap) { | |
2584 | return; | |
2585 | } | |
2586 | ||
3ae96d66 | 2587 | if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
5c5d2e50 VSO |
2588 | return; |
2589 | } | |
2590 | ||
2591 | bdrv_enable_dirty_bitmap(bitmap); | |
2592 | } | |
2593 | ||
0e2b7f09 | 2594 | void qmp_block_dirty_bitmap_disable(const char *node, const char *name, |
5c5d2e50 VSO |
2595 | Error **errp) |
2596 | { | |
2597 | BlockDriverState *bs; | |
2598 | BdrvDirtyBitmap *bitmap; | |
2599 | ||
2600 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2601 | if (!bitmap) { | |
2602 | return; | |
2603 | } | |
2604 | ||
3ae96d66 | 2605 | if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
5c5d2e50 VSO |
2606 | return; |
2607 | } | |
2608 | ||
2609 | bdrv_disable_dirty_bitmap(bitmap); | |
2610 | } | |
2611 | ||
eff0829b VSO |
2612 | static BdrvDirtyBitmap *do_block_dirty_bitmap_merge( |
2613 | const char *node, const char *target, | |
2614 | BlockDirtyBitmapMergeSourceList *bitmaps, | |
2615 | HBitmap **backup, Error **errp) | |
b598e531 VSO |
2616 | { |
2617 | BlockDriverState *bs; | |
360d4e4e | 2618 | BdrvDirtyBitmap *dst, *src, *anon; |
eff0829b | 2619 | BlockDirtyBitmapMergeSourceList *lst; |
360d4e4e | 2620 | Error *local_err = NULL; |
b598e531 | 2621 | |
360d4e4e | 2622 | dst = block_dirty_bitmap_lookup(node, target, &bs, errp); |
b598e531 | 2623 | if (!dst) { |
360d4e4e | 2624 | return NULL; |
b598e531 VSO |
2625 | } |
2626 | ||
360d4e4e JS |
2627 | anon = bdrv_create_dirty_bitmap(bs, bdrv_dirty_bitmap_granularity(dst), |
2628 | NULL, errp); | |
2629 | if (!anon) { | |
2630 | return NULL; | |
2631 | } | |
2632 | ||
2633 | for (lst = bitmaps; lst; lst = lst->next) { | |
eff0829b VSO |
2634 | switch (lst->value->type) { |
2635 | const char *name, *node; | |
2636 | case QTYPE_QSTRING: | |
2637 | name = lst->value->u.local; | |
2638 | src = bdrv_find_dirty_bitmap(bs, name); | |
2639 | if (!src) { | |
2640 | error_setg(errp, "Dirty bitmap '%s' not found", name); | |
2641 | dst = NULL; | |
2642 | goto out; | |
2643 | } | |
2644 | break; | |
2645 | case QTYPE_QDICT: | |
2646 | node = lst->value->u.external.node; | |
2647 | name = lst->value->u.external.name; | |
2648 | src = block_dirty_bitmap_lookup(node, name, NULL, errp); | |
2649 | if (!src) { | |
2650 | dst = NULL; | |
2651 | goto out; | |
2652 | } | |
2653 | break; | |
2654 | default: | |
2655 | abort(); | |
360d4e4e JS |
2656 | } |
2657 | ||
2658 | bdrv_merge_dirty_bitmap(anon, src, NULL, &local_err); | |
2659 | if (local_err) { | |
2660 | error_propagate(errp, local_err); | |
2661 | dst = NULL; | |
2662 | goto out; | |
2663 | } | |
b598e531 VSO |
2664 | } |
2665 | ||
360d4e4e JS |
2666 | /* Merge into dst; dst is unchanged on failure. */ |
2667 | bdrv_merge_dirty_bitmap(dst, anon, backup, errp); | |
2668 | ||
2669 | out: | |
5deb6cbd | 2670 | bdrv_release_dirty_bitmap(anon); |
360d4e4e JS |
2671 | return dst; |
2672 | } | |
2673 | ||
0e2b7f09 | 2674 | void qmp_block_dirty_bitmap_merge(const char *node, const char *target, |
eff0829b VSO |
2675 | BlockDirtyBitmapMergeSourceList *bitmaps, |
2676 | Error **errp) | |
360d4e4e JS |
2677 | { |
2678 | do_block_dirty_bitmap_merge(node, target, bitmaps, NULL, errp); | |
b598e531 VSO |
2679 | } |
2680 | ||
a3b52535 VSO |
2681 | BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node, |
2682 | const char *name, | |
2683 | Error **errp) | |
2684 | { | |
2685 | BdrvDirtyBitmap *bitmap; | |
2686 | BlockDriverState *bs; | |
2687 | BlockDirtyBitmapSha256 *ret = NULL; | |
2688 | char *sha256; | |
2689 | ||
2690 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, errp); | |
2691 | if (!bitmap || !bs) { | |
2692 | return NULL; | |
2693 | } | |
2694 | ||
2695 | sha256 = bdrv_dirty_bitmap_sha256(bitmap, errp); | |
2696 | if (sha256 == NULL) { | |
2697 | return NULL; | |
2698 | } | |
2699 | ||
2700 | ret = g_new(BlockDirtyBitmapSha256, 1); | |
2701 | ret->sha256 = sha256; | |
2702 | ||
2703 | return ret; | |
2704 | } | |
2705 | ||
3b1dbd11 BC |
2706 | void qmp_block_resize(bool has_device, const char *device, |
2707 | bool has_node_name, const char *node_name, | |
2708 | int64_t size, Error **errp) | |
6d4a2b3a | 2709 | { |
3b1dbd11 | 2710 | Error *local_err = NULL; |
7dad9ee6 | 2711 | BlockBackend *blk = NULL; |
6d4a2b3a | 2712 | BlockDriverState *bs; |
927e0e76 | 2713 | AioContext *aio_context; |
8732901e | 2714 | int ret; |
6d4a2b3a | 2715 | |
3b1dbd11 BC |
2716 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
2717 | has_node_name ? node_name : NULL, | |
2718 | &local_err); | |
84d18f06 | 2719 | if (local_err) { |
3b1dbd11 BC |
2720 | error_propagate(errp, local_err); |
2721 | return; | |
2722 | } | |
2723 | ||
927e0e76 SH |
2724 | aio_context = bdrv_get_aio_context(bs); |
2725 | aio_context_acquire(aio_context); | |
2726 | ||
6d4a2b3a | 2727 | if (size < 0) { |
c6bd8c70 | 2728 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
927e0e76 | 2729 | goto out; |
6d4a2b3a CH |
2730 | } |
2731 | ||
9c75e168 | 2732 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
c6bd8c70 | 2733 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
927e0e76 | 2734 | goto out; |
9c75e168 JC |
2735 | } |
2736 | ||
d861ab3a | 2737 | blk = blk_new(bdrv_get_aio_context(bs), BLK_PERM_RESIZE, BLK_PERM_ALL); |
d7086422 KW |
2738 | ret = blk_insert_bs(blk, bs, errp); |
2739 | if (ret < 0) { | |
2740 | goto out; | |
2741 | } | |
7dad9ee6 | 2742 | |
698bdfa0 | 2743 | bdrv_drained_begin(bs); |
c80d8b06 | 2744 | ret = blk_truncate(blk, size, false, PREALLOC_MODE_OFF, errp); |
698bdfa0 | 2745 | bdrv_drained_end(bs); |
927e0e76 SH |
2746 | |
2747 | out: | |
7dad9ee6 | 2748 | blk_unref(blk); |
927e0e76 | 2749 | aio_context_release(aio_context); |
6d4a2b3a | 2750 | } |
12bd451f | 2751 | |
2323322e | 2752 | void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, |
13d8cc51 | 2753 | bool has_base, const char *base, |
312fe09c | 2754 | bool has_base_node, const char *base_node, |
13d8cc51 JC |
2755 | bool has_backing_file, const char *backing_file, |
2756 | bool has_speed, int64_t speed, | |
1d809098 | 2757 | bool has_on_error, BlockdevOnError on_error, |
241ca1ab JS |
2758 | bool has_auto_finalize, bool auto_finalize, |
2759 | bool has_auto_dismiss, bool auto_dismiss, | |
1d809098 | 2760 | Error **errp) |
12bd451f | 2761 | { |
554b6147 | 2762 | BlockDriverState *bs, *iter; |
c8c3080f | 2763 | BlockDriverState *base_bs = NULL; |
f3e69beb | 2764 | AioContext *aio_context; |
fd7f8c65 | 2765 | Error *local_err = NULL; |
13d8cc51 | 2766 | const char *base_name = NULL; |
cf6320df | 2767 | int job_flags = JOB_DEFAULT; |
12bd451f | 2768 | |
1d809098 PB |
2769 | if (!has_on_error) { |
2770 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2771 | } | |
2772 | ||
554b6147 | 2773 | bs = bdrv_lookup_bs(device, device, errp); |
b6c1bae5 | 2774 | if (!bs) { |
12bd451f SH |
2775 | return; |
2776 | } | |
2777 | ||
b6c1bae5 | 2778 | aio_context = bdrv_get_aio_context(bs); |
f3e69beb SH |
2779 | aio_context_acquire(aio_context); |
2780 | ||
312fe09c AG |
2781 | if (has_base && has_base_node) { |
2782 | error_setg(errp, "'base' and 'base-node' cannot be specified " | |
2783 | "at the same time"); | |
2784 | goto out; | |
2785 | } | |
2786 | ||
13d8cc51 | 2787 | if (has_base) { |
c8c3080f MT |
2788 | base_bs = bdrv_find_backing_image(bs, base); |
2789 | if (base_bs == NULL) { | |
c6bd8c70 | 2790 | error_setg(errp, QERR_BASE_NOT_FOUND, base); |
f3e69beb | 2791 | goto out; |
c8c3080f | 2792 | } |
f3e69beb | 2793 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
13d8cc51 | 2794 | base_name = base; |
12bd451f SH |
2795 | } |
2796 | ||
312fe09c AG |
2797 | if (has_base_node) { |
2798 | base_bs = bdrv_lookup_bs(NULL, base_node, errp); | |
2799 | if (!base_bs) { | |
2800 | goto out; | |
2801 | } | |
2802 | if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { | |
2803 | error_setg(errp, "Node '%s' is not a backing image of '%s'", | |
2804 | base_node, device); | |
2805 | goto out; | |
2806 | } | |
2807 | assert(bdrv_get_aio_context(base_bs) == aio_context); | |
f30c66ba | 2808 | bdrv_refresh_filename(base_bs); |
312fe09c AG |
2809 | base_name = base_bs->filename; |
2810 | } | |
2811 | ||
554b6147 AG |
2812 | /* Check for op blockers in the whole chain between bs and base */ |
2813 | for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { | |
2814 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { | |
2815 | goto out; | |
2816 | } | |
2817 | } | |
2818 | ||
13d8cc51 JC |
2819 | /* if we are streaming the entire chain, the result will have no backing |
2820 | * file, and specifying one is therefore an error */ | |
2821 | if (base_bs == NULL && has_backing_file) { | |
2822 | error_setg(errp, "backing file specified, but streaming the " | |
2823 | "entire chain"); | |
f3e69beb | 2824 | goto out; |
13d8cc51 JC |
2825 | } |
2826 | ||
2827 | /* backing_file string overrides base bs filename */ | |
2828 | base_name = has_backing_file ? backing_file : base_name; | |
2829 | ||
241ca1ab JS |
2830 | if (has_auto_finalize && !auto_finalize) { |
2831 | job_flags |= JOB_MANUAL_FINALIZE; | |
2832 | } | |
2833 | if (has_auto_dismiss && !auto_dismiss) { | |
2834 | job_flags |= JOB_MANUAL_DISMISS; | |
2835 | } | |
2836 | ||
2323322e | 2837 | stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, |
cf6320df | 2838 | job_flags, has_speed ? speed : 0, on_error, &local_err); |
84d18f06 | 2839 | if (local_err) { |
fd7f8c65 | 2840 | error_propagate(errp, local_err); |
f3e69beb | 2841 | goto out; |
12bd451f SH |
2842 | } |
2843 | ||
b23c580c | 2844 | trace_qmp_block_stream(bs); |
f3e69beb SH |
2845 | |
2846 | out: | |
2847 | aio_context_release(aio_context); | |
12bd451f | 2848 | } |
2d47c6e9 | 2849 | |
fd62c609 | 2850 | void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, |
3c605f40 | 2851 | bool has_base_node, const char *base_node, |
7676e2c5 | 2852 | bool has_base, const char *base, |
3c605f40 | 2853 | bool has_top_node, const char *top_node, |
7676e2c5 | 2854 | bool has_top, const char *top, |
54e26900 | 2855 | bool has_backing_file, const char *backing_file, |
ed61fc10 | 2856 | bool has_speed, int64_t speed, |
8faad1c7 | 2857 | bool has_on_error, BlockdevOnError on_error, |
0db832f4 | 2858 | bool has_filter_node_name, const char *filter_node_name, |
96fbf534 JS |
2859 | bool has_auto_finalize, bool auto_finalize, |
2860 | bool has_auto_dismiss, bool auto_dismiss, | |
ed61fc10 JC |
2861 | Error **errp) |
2862 | { | |
2863 | BlockDriverState *bs; | |
058223a6 | 2864 | BlockDriverState *iter; |
ed61fc10 | 2865 | BlockDriverState *base_bs, *top_bs; |
9e85cd5c | 2866 | AioContext *aio_context; |
ed61fc10 | 2867 | Error *local_err = NULL; |
5360782d | 2868 | int job_flags = JOB_DEFAULT; |
ed61fc10 | 2869 | |
54504663 HR |
2870 | if (!has_speed) { |
2871 | speed = 0; | |
2872 | } | |
8faad1c7 KW |
2873 | if (!has_on_error) { |
2874 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2875 | } | |
0db832f4 KW |
2876 | if (!has_filter_node_name) { |
2877 | filter_node_name = NULL; | |
2878 | } | |
96fbf534 JS |
2879 | if (has_auto_finalize && !auto_finalize) { |
2880 | job_flags |= JOB_MANUAL_FINALIZE; | |
2881 | } | |
2882 | if (has_auto_dismiss && !auto_dismiss) { | |
2883 | job_flags |= JOB_MANUAL_DISMISS; | |
2884 | } | |
54504663 | 2885 | |
7676e2c5 JC |
2886 | /* Important Note: |
2887 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
2888 | * live commit feature versions; for this to work, we must make sure to | |
2889 | * perform the device lookup before any generic errors that may occur in a | |
2890 | * scenario in which all optional arguments are omitted. */ | |
1d13b167 KW |
2891 | bs = qmp_get_root_bs(device, &local_err); |
2892 | if (!bs) { | |
2893 | bs = bdrv_lookup_bs(device, device, NULL); | |
2894 | if (!bs) { | |
2895 | error_free(local_err); | |
2896 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, | |
2897 | "Device '%s' not found", device); | |
2898 | } else { | |
2899 | error_propagate(errp, local_err); | |
2900 | } | |
ed61fc10 | 2901 | return; |
628ff683 FZ |
2902 | } |
2903 | ||
1d13b167 | 2904 | aio_context = bdrv_get_aio_context(bs); |
9e85cd5c SH |
2905 | aio_context_acquire(aio_context); |
2906 | ||
bb00021d | 2907 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { |
9e85cd5c | 2908 | goto out; |
ed61fc10 | 2909 | } |
ed61fc10 JC |
2910 | |
2911 | /* default top_bs is the active layer */ | |
2912 | top_bs = bs; | |
2913 | ||
3c605f40 KW |
2914 | if (has_top_node && has_top) { |
2915 | error_setg(errp, "'top-node' and 'top' are mutually exclusive"); | |
2916 | goto out; | |
2917 | } else if (has_top_node) { | |
2918 | top_bs = bdrv_lookup_bs(NULL, top_node, errp); | |
2919 | if (top_bs == NULL) { | |
2920 | goto out; | |
2921 | } | |
2922 | if (!bdrv_chain_contains(bs, top_bs)) { | |
2923 | error_setg(errp, "'%s' is not in this backing file chain", | |
2924 | top_node); | |
2925 | goto out; | |
2926 | } | |
2927 | } else if (has_top && top) { | |
f30c66ba HR |
2928 | /* This strcmp() is just a shortcut, there is no need to |
2929 | * refresh @bs's filename. If it mismatches, | |
2930 | * bdrv_find_backing_image() will do the refresh and may still | |
2931 | * return @bs. */ | |
ed61fc10 JC |
2932 | if (strcmp(bs->filename, top) != 0) { |
2933 | top_bs = bdrv_find_backing_image(bs, top); | |
2934 | } | |
2935 | } | |
2936 | ||
2937 | if (top_bs == NULL) { | |
2938 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
9e85cd5c | 2939 | goto out; |
ed61fc10 JC |
2940 | } |
2941 | ||
9e85cd5c SH |
2942 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
2943 | ||
3c605f40 KW |
2944 | if (has_base_node && has_base) { |
2945 | error_setg(errp, "'base-node' and 'base' are mutually exclusive"); | |
2946 | goto out; | |
2947 | } else if (has_base_node) { | |
2948 | base_bs = bdrv_lookup_bs(NULL, base_node, errp); | |
2949 | if (base_bs == NULL) { | |
2950 | goto out; | |
2951 | } | |
2952 | if (!bdrv_chain_contains(top_bs, base_bs)) { | |
2953 | error_setg(errp, "'%s' is not in this backing file chain", | |
2954 | base_node); | |
2955 | goto out; | |
2956 | } | |
2957 | } else if (has_base && base) { | |
d5208c45 JC |
2958 | base_bs = bdrv_find_backing_image(top_bs, base); |
2959 | } else { | |
2960 | base_bs = bdrv_find_base(top_bs); | |
2961 | } | |
2962 | ||
2963 | if (base_bs == NULL) { | |
c6bd8c70 | 2964 | error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); |
9e85cd5c | 2965 | goto out; |
d5208c45 JC |
2966 | } |
2967 | ||
9e85cd5c SH |
2968 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
2969 | ||
058223a6 AG |
2970 | for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { |
2971 | if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
2972 | goto out; | |
2973 | } | |
bb00021d FZ |
2974 | } |
2975 | ||
7676e2c5 JC |
2976 | /* Do not allow attempts to commit an image into itself */ |
2977 | if (top_bs == base_bs) { | |
2978 | error_setg(errp, "cannot commit an image into itself"); | |
9e85cd5c | 2979 | goto out; |
7676e2c5 JC |
2980 | } |
2981 | ||
20a63d2c | 2982 | if (top_bs == bs) { |
54e26900 JC |
2983 | if (has_backing_file) { |
2984 | error_setg(errp, "'backing-file' specified," | |
2985 | " but 'top' is the active layer"); | |
9e85cd5c | 2986 | goto out; |
54e26900 | 2987 | } |
47970dfb | 2988 | commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, |
5360782d | 2989 | job_flags, speed, on_error, |
78bbd910 | 2990 | filter_node_name, NULL, NULL, false, &local_err); |
20a63d2c | 2991 | } else { |
058223a6 AG |
2992 | BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); |
2993 | if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { | |
2994 | goto out; | |
2995 | } | |
5360782d JS |
2996 | commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, job_flags, |
2997 | speed, on_error, has_backing_file ? backing_file : NULL, | |
0db832f4 | 2998 | filter_node_name, &local_err); |
20a63d2c | 2999 | } |
ed61fc10 JC |
3000 | if (local_err != NULL) { |
3001 | error_propagate(errp, local_err); | |
9e85cd5c | 3002 | goto out; |
ed61fc10 | 3003 | } |
9e85cd5c SH |
3004 | |
3005 | out: | |
3006 | aio_context_release(aio_context); | |
ed61fc10 JC |
3007 | } |
3008 | ||
7b0b870b JS |
3009 | /* Common QMP interface for drive-backup and blockdev-backup */ |
3010 | static BlockJob *do_backup_common(BackupCommon *backup, | |
3011 | BlockDriverState *bs, | |
3012 | BlockDriverState *target_bs, | |
3013 | AioContext *aio_context, | |
3014 | JobTxn *txn, Error **errp) | |
99a9addf | 3015 | { |
111049a4 | 3016 | BlockJob *job = NULL; |
d58d8453 | 3017 | BdrvDirtyBitmap *bmap = NULL; |
7b0b870b | 3018 | int job_flags = JOB_DEFAULT; |
99a9addf | 3019 | |
81206a89 PB |
3020 | if (!backup->has_speed) { |
3021 | backup->speed = 0; | |
99a9addf | 3022 | } |
81206a89 PB |
3023 | if (!backup->has_on_source_error) { |
3024 | backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
99a9addf | 3025 | } |
81206a89 PB |
3026 | if (!backup->has_on_target_error) { |
3027 | backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
99a9addf | 3028 | } |
81206a89 PB |
3029 | if (!backup->has_job_id) { |
3030 | backup->job_id = NULL; | |
99a9addf | 3031 | } |
b40dacdc JS |
3032 | if (!backup->has_auto_finalize) { |
3033 | backup->auto_finalize = true; | |
3034 | } | |
3035 | if (!backup->has_auto_dismiss) { | |
3036 | backup->auto_dismiss = true; | |
3037 | } | |
13b9414b PB |
3038 | if (!backup->has_compress) { |
3039 | backup->compress = false; | |
3040 | } | |
99a9addf | 3041 | |
a6c9365a JS |
3042 | if ((backup->sync == MIRROR_SYNC_MODE_BITMAP) || |
3043 | (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL)) { | |
3044 | /* done before desugaring 'incremental' to print the right message */ | |
3045 | if (!backup->has_bitmap) { | |
3046 | error_setg(errp, "must provide a valid bitmap name for " | |
3047 | "'%s' sync mode", MirrorSyncMode_str(backup->sync)); | |
3048 | return NULL; | |
3049 | } | |
3050 | } | |
3051 | ||
c8b56501 JS |
3052 | if (backup->sync == MIRROR_SYNC_MODE_INCREMENTAL) { |
3053 | if (backup->has_bitmap_mode && | |
3054 | backup->bitmap_mode != BITMAP_SYNC_MODE_ON_SUCCESS) { | |
3055 | error_setg(errp, "Bitmap sync mode must be '%s' " | |
3056 | "when using sync mode '%s'", | |
3057 | BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS), | |
3058 | MirrorSyncMode_str(backup->sync)); | |
3059 | return NULL; | |
3060 | } | |
3061 | backup->has_bitmap_mode = true; | |
3062 | backup->sync = MIRROR_SYNC_MODE_BITMAP; | |
3063 | backup->bitmap_mode = BITMAP_SYNC_MODE_ON_SUCCESS; | |
3064 | } | |
3065 | ||
7b0b870b JS |
3066 | if (backup->has_bitmap) { |
3067 | bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); | |
3068 | if (!bmap) { | |
3069 | error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); | |
3070 | return NULL; | |
3071 | } | |
c8b56501 JS |
3072 | if (!backup->has_bitmap_mode) { |
3073 | error_setg(errp, "Bitmap sync mode must be given " | |
3074 | "when providing a bitmap"); | |
3075 | return NULL; | |
3076 | } | |
b30ffbef | 3077 | if (bdrv_dirty_bitmap_check(bmap, BDRV_BITMAP_ALLOW_RO, errp)) { |
7b0b870b JS |
3078 | return NULL; |
3079 | } | |
1a2b8b40 JS |
3080 | |
3081 | /* This does not produce a useful bitmap artifact: */ | |
3082 | if (backup->sync == MIRROR_SYNC_MODE_NONE) { | |
3083 | error_setg(errp, "sync mode '%s' does not produce meaningful bitmap" | |
3084 | " outputs", MirrorSyncMode_str(backup->sync)); | |
3085 | return NULL; | |
3086 | } | |
3087 | ||
3088 | /* If the bitmap isn't used for input or output, this is useless: */ | |
3089 | if (backup->bitmap_mode == BITMAP_SYNC_MODE_NEVER && | |
3090 | backup->sync != MIRROR_SYNC_MODE_BITMAP) { | |
3091 | error_setg(errp, "Bitmap sync mode '%s' has no meaningful effect" | |
3092 | " when combined with sync mode '%s'", | |
3093 | BitmapSyncMode_str(backup->bitmap_mode), | |
3094 | MirrorSyncMode_str(backup->sync)); | |
3095 | return NULL; | |
3096 | } | |
3097 | } | |
3098 | ||
3099 | if (!backup->has_bitmap && backup->has_bitmap_mode) { | |
3100 | error_setg(errp, "Cannot specify bitmap sync mode without a bitmap"); | |
3101 | return NULL; | |
7b0b870b JS |
3102 | } |
3103 | ||
3104 | if (!backup->auto_finalize) { | |
3105 | job_flags |= JOB_MANUAL_FINALIZE; | |
3106 | } | |
3107 | if (!backup->auto_dismiss) { | |
3108 | job_flags |= JOB_MANUAL_DISMISS; | |
3109 | } | |
3110 | ||
3111 | job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, | |
c8b56501 JS |
3112 | backup->sync, bmap, backup->bitmap_mode, |
3113 | backup->compress, | |
00e30f05 | 3114 | backup->filter_node_name, |
c8b56501 JS |
3115 | backup->on_source_error, |
3116 | backup->on_target_error, | |
7b0b870b JS |
3117 | job_flags, NULL, NULL, txn, errp); |
3118 | return job; | |
3119 | } | |
3120 | ||
2288ccfa | 3121 | void qmp_drive_backup(DriveBackup *backup, Error **errp) |
78f51fde | 3122 | { |
2288ccfa SL |
3123 | TransactionAction action = { |
3124 | .type = TRANSACTION_ACTION_KIND_DRIVE_BACKUP, | |
3125 | .u.drive_backup.data = backup, | |
3126 | }; | |
3127 | blockdev_do_action(&action, errp); | |
78f51fde JS |
3128 | } |
3129 | ||
facda544 PK |
3130 | BlockDeviceInfoList *qmp_query_named_block_nodes(bool has_flat, |
3131 | bool flat, | |
3132 | Error **errp) | |
c13163fb | 3133 | { |
facda544 PK |
3134 | bool return_flat = has_flat && flat; |
3135 | ||
3136 | return bdrv_named_nodes_list(return_flat, errp); | |
c13163fb BC |
3137 | } |
3138 | ||
5d3b4e99 VSO |
3139 | XDbgBlockGraph *qmp_x_debug_query_block_graph(Error **errp) |
3140 | { | |
3141 | return bdrv_get_xdbg_block_graph(errp); | |
3142 | } | |
3143 | ||
5b7bfe51 | 3144 | void qmp_blockdev_backup(BlockdevBackup *backup, Error **errp) |
c29c1dd3 | 3145 | { |
5b7bfe51 SL |
3146 | TransactionAction action = { |
3147 | .type = TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP, | |
3148 | .u.blockdev_backup.data = backup, | |
3149 | }; | |
3150 | blockdev_do_action(&action, errp); | |
78f51fde JS |
3151 | } |
3152 | ||
4193cdd7 FZ |
3153 | /* Parameter check and block job starting for drive mirroring. |
3154 | * Caller should hold @device and @target's aio context (must be the same). | |
3155 | **/ | |
71aa9867 | 3156 | static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, |
4193cdd7 FZ |
3157 | BlockDriverState *target, |
3158 | bool has_replaces, const char *replaces, | |
3159 | enum MirrorSyncMode sync, | |
274fccee | 3160 | BlockMirrorBackingMode backing_mode, |
cdf3bc93 | 3161 | bool zero_target, |
4193cdd7 FZ |
3162 | bool has_speed, int64_t speed, |
3163 | bool has_granularity, uint32_t granularity, | |
3164 | bool has_buf_size, int64_t buf_size, | |
3165 | bool has_on_source_error, | |
3166 | BlockdevOnError on_source_error, | |
3167 | bool has_on_target_error, | |
3168 | BlockdevOnError on_target_error, | |
3169 | bool has_unmap, bool unmap, | |
6cdbceb1 KW |
3170 | bool has_filter_node_name, |
3171 | const char *filter_node_name, | |
481debaa | 3172 | bool has_copy_mode, MirrorCopyMode copy_mode, |
a6b58ade JS |
3173 | bool has_auto_finalize, bool auto_finalize, |
3174 | bool has_auto_dismiss, bool auto_dismiss, | |
4193cdd7 | 3175 | Error **errp) |
d9b902db | 3176 | { |
a1999b33 | 3177 | int job_flags = JOB_DEFAULT; |
d9b902db PB |
3178 | |
3179 | if (!has_speed) { | |
3180 | speed = 0; | |
3181 | } | |
b952b558 PB |
3182 | if (!has_on_source_error) { |
3183 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
3184 | } | |
3185 | if (!has_on_target_error) { | |
3186 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
3187 | } | |
eee13dfe PB |
3188 | if (!has_granularity) { |
3189 | granularity = 0; | |
3190 | } | |
08e4ed6c | 3191 | if (!has_buf_size) { |
48ac0a4d | 3192 | buf_size = 0; |
08e4ed6c | 3193 | } |
0fc9f8ea FZ |
3194 | if (!has_unmap) { |
3195 | unmap = true; | |
3196 | } | |
6cdbceb1 KW |
3197 | if (!has_filter_node_name) { |
3198 | filter_node_name = NULL; | |
3199 | } | |
481debaa HR |
3200 | if (!has_copy_mode) { |
3201 | copy_mode = MIRROR_COPY_MODE_BACKGROUND; | |
3202 | } | |
a6b58ade JS |
3203 | if (has_auto_finalize && !auto_finalize) { |
3204 | job_flags |= JOB_MANUAL_FINALIZE; | |
3205 | } | |
3206 | if (has_auto_dismiss && !auto_dismiss) { | |
3207 | job_flags |= JOB_MANUAL_DISMISS; | |
3208 | } | |
08e4ed6c | 3209 | |
eee13dfe | 3210 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
c6bd8c70 MA |
3211 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3212 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
3213 | return; |
3214 | } | |
3215 | if (granularity & (granularity - 1)) { | |
c6bd8c70 MA |
3216 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
3217 | "power of 2"); | |
eee13dfe PB |
3218 | return; |
3219 | } | |
d9b902db | 3220 | |
4193cdd7 FZ |
3221 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { |
3222 | return; | |
3223 | } | |
e40e5027 FZ |
3224 | if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { |
3225 | return; | |
3226 | } | |
4193cdd7 FZ |
3227 | |
3228 | if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { | |
3229 | sync = MIRROR_SYNC_MODE_FULL; | |
3230 | } | |
3231 | ||
74ce9e46 HR |
3232 | if (has_replaces) { |
3233 | BlockDriverState *to_replace_bs; | |
3234 | AioContext *replace_aio_context; | |
3235 | int64_t bs_size, replace_size; | |
3236 | ||
3237 | bs_size = bdrv_getlength(bs); | |
3238 | if (bs_size < 0) { | |
3239 | error_setg_errno(errp, -bs_size, "Failed to query device's size"); | |
3240 | return; | |
3241 | } | |
3242 | ||
3243 | to_replace_bs = check_to_replace_node(bs, replaces, errp); | |
3244 | if (!to_replace_bs) { | |
3245 | return; | |
3246 | } | |
3247 | ||
3248 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); | |
3249 | aio_context_acquire(replace_aio_context); | |
3250 | replace_size = bdrv_getlength(to_replace_bs); | |
3251 | aio_context_release(replace_aio_context); | |
3252 | ||
3253 | if (replace_size < 0) { | |
3254 | error_setg_errno(errp, -replace_size, | |
3255 | "Failed to query the replacement node's size"); | |
3256 | return; | |
3257 | } | |
3258 | if (bs_size != replace_size) { | |
3259 | error_setg(errp, "cannot replace image with a mirror image of " | |
3260 | "different size"); | |
3261 | return; | |
3262 | } | |
3263 | } | |
3264 | ||
4193cdd7 FZ |
3265 | /* pass the node name to replace to mirror start since it's loose coupling |
3266 | * and will allow to check whether the node still exist at mirror completion | |
3267 | */ | |
71aa9867 | 3268 | mirror_start(job_id, bs, target, |
a1999b33 | 3269 | has_replaces ? replaces : NULL, job_flags, |
cdf3bc93 | 3270 | speed, granularity, buf_size, sync, backing_mode, zero_target, |
6cdbceb1 | 3271 | on_source_error, on_target_error, unmap, filter_node_name, |
481debaa | 3272 | copy_mode, errp); |
4193cdd7 FZ |
3273 | } |
3274 | ||
faecd40a | 3275 | void qmp_drive_mirror(DriveMirror *arg, Error **errp) |
4193cdd7 FZ |
3276 | { |
3277 | BlockDriverState *bs; | |
4193cdd7 FZ |
3278 | BlockDriverState *source, *target_bs; |
3279 | AioContext *aio_context; | |
3ea67e08 | 3280 | AioContext *old_context; |
274fccee | 3281 | BlockMirrorBackingMode backing_mode; |
4193cdd7 FZ |
3282 | Error *local_err = NULL; |
3283 | QDict *options = NULL; | |
3284 | int flags; | |
3285 | int64_t size; | |
faecd40a | 3286 | const char *format = arg->format; |
cdf3bc93 | 3287 | bool zero_target; |
8ed7d42f | 3288 | int ret; |
4193cdd7 | 3289 | |
0524e93a KW |
3290 | bs = qmp_get_root_bs(arg->device, errp); |
3291 | if (!bs) { | |
d9b902db PB |
3292 | return; |
3293 | } | |
3294 | ||
cb2af917 PB |
3295 | /* Early check to avoid creating target */ |
3296 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { | |
3297 | return; | |
3298 | } | |
3299 | ||
0524e93a | 3300 | aio_context = bdrv_get_aio_context(bs); |
5a7e7a0b SH |
3301 | aio_context_acquire(aio_context); |
3302 | ||
faecd40a EB |
3303 | if (!arg->has_mode) { |
3304 | arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
4193cdd7 | 3305 | } |
d9b902db | 3306 | |
faecd40a EB |
3307 | if (!arg->has_format) { |
3308 | format = (arg->mode == NEW_IMAGE_MODE_EXISTING | |
3309 | ? NULL : bs->drv->format_name); | |
d9b902db | 3310 | } |
d9b902db | 3311 | |
61de4c68 | 3312 | flags = bs->open_flags | BDRV_O_RDWR; |
760e0063 | 3313 | source = backing_bs(bs); |
faecd40a EB |
3314 | if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { |
3315 | arg->sync = MIRROR_SYNC_MODE_FULL; | |
d9b902db | 3316 | } |
faecd40a | 3317 | if (arg->sync == MIRROR_SYNC_MODE_NONE) { |
117e0c82 HR |
3318 | source = bs; |
3319 | } | |
d9b902db | 3320 | |
ac3c5d83 SH |
3321 | size = bdrv_getlength(bs); |
3322 | if (size < 0) { | |
3323 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
5a7e7a0b | 3324 | goto out; |
ac3c5d83 SH |
3325 | } |
3326 | ||
faecd40a | 3327 | if (arg->has_replaces) { |
faecd40a | 3328 | if (!arg->has_node_name) { |
09158f00 BC |
3329 | error_setg(errp, "a node-name must be provided when replacing a" |
3330 | " named node of the graph"); | |
5a7e7a0b | 3331 | goto out; |
09158f00 | 3332 | } |
09158f00 BC |
3333 | } |
3334 | ||
faecd40a | 3335 | if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { |
274fccee HR |
3336 | backing_mode = MIRROR_SOURCE_BACKING_CHAIN; |
3337 | } else { | |
3338 | backing_mode = MIRROR_OPEN_BACKING_CHAIN; | |
3339 | } | |
3340 | ||
2a32c6e8 JS |
3341 | /* Don't open backing image in create() */ |
3342 | flags |= BDRV_O_NO_BACKING; | |
3343 | ||
faecd40a EB |
3344 | if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) |
3345 | && arg->mode != NEW_IMAGE_MODE_EXISTING) | |
14526864 | 3346 | { |
d9b902db | 3347 | /* create new image w/o backing file */ |
e6641719 | 3348 | assert(format); |
faecd40a | 3349 | bdrv_img_create(arg->target, format, |
9217283d | 3350 | NULL, NULL, NULL, size, flags, false, &local_err); |
d9b902db | 3351 | } else { |
faecd40a | 3352 | switch (arg->mode) { |
d9b902db | 3353 | case NEW_IMAGE_MODE_EXISTING: |
d9b902db PB |
3354 | break; |
3355 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
3356 | /* create new image with backing file */ | |
f30c66ba | 3357 | bdrv_refresh_filename(source); |
faecd40a | 3358 | bdrv_img_create(arg->target, format, |
cf8f2426 LC |
3359 | source->filename, |
3360 | source->drv->format_name, | |
9217283d | 3361 | NULL, size, flags, false, &local_err); |
d9b902db PB |
3362 | break; |
3363 | default: | |
3364 | abort(); | |
3365 | } | |
3366 | } | |
3367 | ||
84d18f06 | 3368 | if (local_err) { |
cf8f2426 | 3369 | error_propagate(errp, local_err); |
5a7e7a0b | 3370 | goto out; |
d9b902db PB |
3371 | } |
3372 | ||
e6641719 | 3373 | options = qdict_new(); |
faecd40a | 3374 | if (arg->has_node_name) { |
46f5ac20 | 3375 | qdict_put_str(options, "node-name", arg->node_name); |
4c828dc6 | 3376 | } |
e6641719 | 3377 | if (format) { |
46f5ac20 | 3378 | qdict_put_str(options, "driver", format); |
e6641719 | 3379 | } |
4c828dc6 | 3380 | |
b812f671 PB |
3381 | /* Mirroring takes care of copy-on-write using the source's backing |
3382 | * file. | |
3383 | */ | |
2a32c6e8 | 3384 | target_bs = bdrv_open(arg->target, NULL, options, flags, errp); |
5b363937 | 3385 | if (!target_bs) { |
5a7e7a0b | 3386 | goto out; |
d9b902db PB |
3387 | } |
3388 | ||
cdf3bc93 HR |
3389 | zero_target = (arg->sync == MIRROR_SYNC_MODE_FULL && |
3390 | (arg->mode == NEW_IMAGE_MODE_EXISTING || | |
3391 | !bdrv_has_zero_init(target_bs))); | |
3392 | ||
3ea67e08 SL |
3393 | |
3394 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ | |
3395 | old_context = bdrv_get_aio_context(target_bs); | |
3396 | aio_context_release(aio_context); | |
3397 | aio_context_acquire(old_context); | |
3398 | ||
8ed7d42f KW |
3399 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); |
3400 | if (ret < 0) { | |
3401 | bdrv_unref(target_bs); | |
3ea67e08 SL |
3402 | aio_context_release(old_context); |
3403 | return; | |
8ed7d42f | 3404 | } |
5a7e7a0b | 3405 | |
3ea67e08 SL |
3406 | aio_context_release(old_context); |
3407 | aio_context_acquire(aio_context); | |
3408 | ||
faecd40a EB |
3409 | blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, |
3410 | arg->has_replaces, arg->replaces, arg->sync, | |
cdf3bc93 HR |
3411 | backing_mode, zero_target, |
3412 | arg->has_speed, arg->speed, | |
faecd40a EB |
3413 | arg->has_granularity, arg->granularity, |
3414 | arg->has_buf_size, arg->buf_size, | |
3415 | arg->has_on_source_error, arg->on_source_error, | |
3416 | arg->has_on_target_error, arg->on_target_error, | |
3417 | arg->has_unmap, arg->unmap, | |
6cdbceb1 | 3418 | false, NULL, |
481debaa | 3419 | arg->has_copy_mode, arg->copy_mode, |
a6b58ade JS |
3420 | arg->has_auto_finalize, arg->auto_finalize, |
3421 | arg->has_auto_dismiss, arg->auto_dismiss, | |
4193cdd7 | 3422 | &local_err); |
e253f4b8 | 3423 | bdrv_unref(target_bs); |
621ff94d | 3424 | error_propagate(errp, local_err); |
5a7e7a0b SH |
3425 | out: |
3426 | aio_context_release(aio_context); | |
d9b902db PB |
3427 | } |
3428 | ||
71aa9867 AG |
3429 | void qmp_blockdev_mirror(bool has_job_id, const char *job_id, |
3430 | const char *device, const char *target, | |
df92562e FZ |
3431 | bool has_replaces, const char *replaces, |
3432 | MirrorSyncMode sync, | |
3433 | bool has_speed, int64_t speed, | |
3434 | bool has_granularity, uint32_t granularity, | |
3435 | bool has_buf_size, int64_t buf_size, | |
3436 | bool has_on_source_error, | |
3437 | BlockdevOnError on_source_error, | |
3438 | bool has_on_target_error, | |
3439 | BlockdevOnError on_target_error, | |
6cdbceb1 KW |
3440 | bool has_filter_node_name, |
3441 | const char *filter_node_name, | |
481debaa | 3442 | bool has_copy_mode, MirrorCopyMode copy_mode, |
a6b58ade JS |
3443 | bool has_auto_finalize, bool auto_finalize, |
3444 | bool has_auto_dismiss, bool auto_dismiss, | |
df92562e FZ |
3445 | Error **errp) |
3446 | { | |
3447 | BlockDriverState *bs; | |
df92562e FZ |
3448 | BlockDriverState *target_bs; |
3449 | AioContext *aio_context; | |
3ea67e08 | 3450 | AioContext *old_context; |
274fccee | 3451 | BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; |
df92562e | 3452 | Error *local_err = NULL; |
cdf3bc93 | 3453 | bool zero_target; |
8ed7d42f | 3454 | int ret; |
df92562e | 3455 | |
07eec652 | 3456 | bs = qmp_get_root_bs(device, errp); |
df92562e | 3457 | if (!bs) { |
df92562e FZ |
3458 | return; |
3459 | } | |
3460 | ||
3461 | target_bs = bdrv_lookup_bs(target, target, errp); | |
3462 | if (!target_bs) { | |
3463 | return; | |
3464 | } | |
3465 | ||
cdf3bc93 HR |
3466 | zero_target = (sync == MIRROR_SYNC_MODE_FULL); |
3467 | ||
3ea67e08 SL |
3468 | /* Honor bdrv_try_set_aio_context() context acquisition requirements. */ |
3469 | old_context = bdrv_get_aio_context(target_bs); | |
df92562e | 3470 | aio_context = bdrv_get_aio_context(bs); |
3ea67e08 | 3471 | aio_context_acquire(old_context); |
df92562e | 3472 | |
8ed7d42f | 3473 | ret = bdrv_try_set_aio_context(target_bs, aio_context, errp); |
3ea67e08 SL |
3474 | |
3475 | aio_context_release(old_context); | |
3476 | aio_context_acquire(aio_context); | |
3477 | ||
8ed7d42f KW |
3478 | if (ret < 0) { |
3479 | goto out; | |
3480 | } | |
df92562e | 3481 | |
71aa9867 | 3482 | blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, |
274fccee | 3483 | has_replaces, replaces, sync, backing_mode, |
cdf3bc93 | 3484 | zero_target, has_speed, speed, |
df92562e FZ |
3485 | has_granularity, granularity, |
3486 | has_buf_size, buf_size, | |
3487 | has_on_source_error, on_source_error, | |
3488 | has_on_target_error, on_target_error, | |
3489 | true, true, | |
6cdbceb1 | 3490 | has_filter_node_name, filter_node_name, |
481debaa | 3491 | has_copy_mode, copy_mode, |
a6b58ade JS |
3492 | has_auto_finalize, auto_finalize, |
3493 | has_auto_dismiss, auto_dismiss, | |
df92562e | 3494 | &local_err); |
621ff94d | 3495 | error_propagate(errp, local_err); |
8ed7d42f | 3496 | out: |
df92562e FZ |
3497 | aio_context_release(aio_context); |
3498 | } | |
3499 | ||
3ddf3efe AG |
3500 | /* Get a block job using its ID and acquire its AioContext */ |
3501 | static BlockJob *find_block_job(const char *id, AioContext **aio_context, | |
24d6bffe | 3502 | Error **errp) |
2d47c6e9 | 3503 | { |
3ddf3efe | 3504 | BlockJob *job; |
2d47c6e9 | 3505 | |
3ddf3efe | 3506 | assert(id != NULL); |
5433c24f | 3507 | |
3ddf3efe | 3508 | *aio_context = NULL; |
3d948cdf | 3509 | |
3ddf3efe | 3510 | job = block_job_get(id); |
5433c24f | 3511 | |
3ddf3efe AG |
3512 | if (!job) { |
3513 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, | |
3514 | "Block job '%s' not found", id); | |
3515 | return NULL; | |
2d47c6e9 | 3516 | } |
3d948cdf | 3517 | |
3ddf3efe AG |
3518 | *aio_context = blk_get_aio_context(job->blk); |
3519 | aio_context_acquire(*aio_context); | |
3d948cdf | 3520 | |
3ddf3efe | 3521 | return job; |
2d47c6e9 SH |
3522 | } |
3523 | ||
882ec7ce | 3524 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 | 3525 | { |
3d948cdf | 3526 | AioContext *aio_context; |
24d6bffe | 3527 | BlockJob *job = find_block_job(device, &aio_context, errp); |
2d47c6e9 SH |
3528 | |
3529 | if (!job) { | |
2d47c6e9 SH |
3530 | return; |
3531 | } | |
3532 | ||
882ec7ce | 3533 | block_job_set_speed(job, speed, errp); |
3d948cdf | 3534 | aio_context_release(aio_context); |
2d47c6e9 | 3535 | } |
370521a1 | 3536 | |
6e37fb81 PB |
3537 | void qmp_block_job_cancel(const char *device, |
3538 | bool has_force, bool force, Error **errp) | |
370521a1 | 3539 | { |
3d948cdf | 3540 | AioContext *aio_context; |
24d6bffe | 3541 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3542 | |
370521a1 | 3543 | if (!job) { |
370521a1 SH |
3544 | return; |
3545 | } | |
3d948cdf SH |
3546 | |
3547 | if (!has_force) { | |
3548 | force = false; | |
3549 | } | |
3550 | ||
b15de828 | 3551 | if (job_user_paused(&job->job) && !force) { |
f231b88d CR |
3552 | error_setg(errp, "The block job for device '%s' is currently paused", |
3553 | device); | |
3d948cdf | 3554 | goto out; |
8acc72a4 | 3555 | } |
370521a1 SH |
3556 | |
3557 | trace_qmp_block_job_cancel(job); | |
3d70ff53 | 3558 | job_user_cancel(&job->job, force, errp); |
3d948cdf SH |
3559 | out: |
3560 | aio_context_release(aio_context); | |
370521a1 | 3561 | } |
fb5458cd | 3562 | |
6e37fb81 PB |
3563 | void qmp_block_job_pause(const char *device, Error **errp) |
3564 | { | |
3d948cdf | 3565 | AioContext *aio_context; |
24d6bffe | 3566 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3567 | |
0ec4dfb8 | 3568 | if (!job) { |
6e37fb81 PB |
3569 | return; |
3570 | } | |
3571 | ||
3572 | trace_qmp_block_job_pause(job); | |
b15de828 | 3573 | job_user_pause(&job->job, errp); |
3d948cdf | 3574 | aio_context_release(aio_context); |
6e37fb81 PB |
3575 | } |
3576 | ||
3577 | void qmp_block_job_resume(const char *device, Error **errp) | |
3578 | { | |
3d948cdf | 3579 | AioContext *aio_context; |
24d6bffe | 3580 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3581 | |
0ec4dfb8 | 3582 | if (!job) { |
6e37fb81 PB |
3583 | return; |
3584 | } | |
3585 | ||
3586 | trace_qmp_block_job_resume(job); | |
b15de828 | 3587 | job_user_resume(&job->job, errp); |
3d948cdf | 3588 | aio_context_release(aio_context); |
6e37fb81 PB |
3589 | } |
3590 | ||
aeae883b PB |
3591 | void qmp_block_job_complete(const char *device, Error **errp) |
3592 | { | |
3d948cdf | 3593 | AioContext *aio_context; |
24d6bffe | 3594 | BlockJob *job = find_block_job(device, &aio_context, errp); |
aeae883b PB |
3595 | |
3596 | if (!job) { | |
aeae883b PB |
3597 | return; |
3598 | } | |
3599 | ||
3600 | trace_qmp_block_job_complete(job); | |
3453d972 | 3601 | job_complete(&job->job, errp); |
3d948cdf | 3602 | aio_context_release(aio_context); |
aeae883b PB |
3603 | } |
3604 | ||
11b61fbc JS |
3605 | void qmp_block_job_finalize(const char *id, Error **errp) |
3606 | { | |
3607 | AioContext *aio_context; | |
3608 | BlockJob *job = find_block_job(id, &aio_context, errp); | |
3609 | ||
3610 | if (!job) { | |
3611 | return; | |
3612 | } | |
3613 | ||
3614 | trace_qmp_block_job_finalize(job); | |
b660a84b | 3615 | job_ref(&job->job); |
7eaa8fb5 | 3616 | job_finalize(&job->job, errp); |
b660a84b SR |
3617 | |
3618 | /* | |
3619 | * Job's context might have changed via job_finalize (and job_txn_apply | |
3620 | * automatically acquires the new one), so make sure we release the correct | |
3621 | * one. | |
3622 | */ | |
3623 | aio_context = blk_get_aio_context(job->blk); | |
3624 | job_unref(&job->job); | |
11b61fbc JS |
3625 | aio_context_release(aio_context); |
3626 | } | |
3627 | ||
75f71059 JS |
3628 | void qmp_block_job_dismiss(const char *id, Error **errp) |
3629 | { | |
3630 | AioContext *aio_context; | |
5f9a6a08 KW |
3631 | BlockJob *bjob = find_block_job(id, &aio_context, errp); |
3632 | Job *job; | |
75f71059 | 3633 | |
5f9a6a08 | 3634 | if (!bjob) { |
75f71059 JS |
3635 | return; |
3636 | } | |
3637 | ||
5f9a6a08 KW |
3638 | trace_qmp_block_job_dismiss(bjob); |
3639 | job = &bjob->job; | |
3640 | job_dismiss(&job, errp); | |
75f71059 JS |
3641 | aio_context_release(aio_context); |
3642 | } | |
3643 | ||
fa40e656 JC |
3644 | void qmp_change_backing_file(const char *device, |
3645 | const char *image_node_name, | |
3646 | const char *backing_file, | |
3647 | Error **errp) | |
3648 | { | |
3649 | BlockDriverState *bs = NULL; | |
729962f6 | 3650 | AioContext *aio_context; |
fa40e656 JC |
3651 | BlockDriverState *image_bs = NULL; |
3652 | Error *local_err = NULL; | |
3653 | bool ro; | |
fa40e656 JC |
3654 | int ret; |
3655 | ||
7b5dca3f KW |
3656 | bs = qmp_get_root_bs(device, errp); |
3657 | if (!bs) { | |
fa40e656 JC |
3658 | return; |
3659 | } | |
3660 | ||
7b5dca3f | 3661 | aio_context = bdrv_get_aio_context(bs); |
729962f6 SH |
3662 | aio_context_acquire(aio_context); |
3663 | ||
fa40e656 JC |
3664 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
3665 | if (local_err) { | |
3666 | error_propagate(errp, local_err); | |
729962f6 | 3667 | goto out; |
fa40e656 JC |
3668 | } |
3669 | ||
3670 | if (!image_bs) { | |
3671 | error_setg(errp, "image file not found"); | |
729962f6 | 3672 | goto out; |
fa40e656 JC |
3673 | } |
3674 | ||
3675 | if (bdrv_find_base(image_bs) == image_bs) { | |
3676 | error_setg(errp, "not allowing backing file change on an image " | |
3677 | "without a backing file"); | |
729962f6 | 3678 | goto out; |
fa40e656 JC |
3679 | } |
3680 | ||
3681 | /* even though we are not necessarily operating on bs, we need it to | |
3682 | * determine if block ops are currently prohibited on the chain */ | |
3683 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
729962f6 | 3684 | goto out; |
fa40e656 JC |
3685 | } |
3686 | ||
3687 | /* final sanity check */ | |
3688 | if (!bdrv_chain_contains(bs, image_bs)) { | |
3689 | error_setg(errp, "'%s' and image file are not in the same chain", | |
3690 | device); | |
729962f6 | 3691 | goto out; |
fa40e656 JC |
3692 | } |
3693 | ||
3694 | /* if not r/w, reopen to make r/w */ | |
fa40e656 JC |
3695 | ro = bdrv_is_read_only(image_bs); |
3696 | ||
3697 | if (ro) { | |
051a60f6 | 3698 | if (bdrv_reopen_set_read_only(image_bs, false, errp) != 0) { |
729962f6 | 3699 | goto out; |
fa40e656 JC |
3700 | } |
3701 | } | |
3702 | ||
3703 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
3704 | image_bs->drv ? image_bs->drv->format_name : ""); | |
3705 | ||
3706 | if (ret < 0) { | |
3707 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
3708 | backing_file); | |
3709 | /* don't exit here, so we can try to restore open flags if | |
3710 | * appropriate */ | |
3711 | } | |
3712 | ||
3713 | if (ro) { | |
051a60f6 | 3714 | bdrv_reopen_set_read_only(image_bs, true, &local_err); |
621ff94d | 3715 | error_propagate(errp, local_err); |
fa40e656 | 3716 | } |
729962f6 SH |
3717 | |
3718 | out: | |
3719 | aio_context_release(aio_context); | |
fa40e656 JC |
3720 | } |
3721 | ||
d26c9a15 KW |
3722 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
3723 | { | |
be4b67bc | 3724 | BlockDriverState *bs; |
d26c9a15 | 3725 | QObject *obj; |
7d5e199a | 3726 | Visitor *v = qobject_output_visitor_new(&obj); |
d26c9a15 | 3727 | QDict *qdict; |
d26c9a15 KW |
3728 | Error *local_err = NULL; |
3729 | ||
3b098d56 | 3730 | visit_type_BlockdevOptions(v, NULL, &options, &local_err); |
84d18f06 | 3731 | if (local_err) { |
d26c9a15 KW |
3732 | error_propagate(errp, local_err); |
3733 | goto fail; | |
3734 | } | |
3735 | ||
3b098d56 | 3736 | visit_complete(v, &obj); |
7dc847eb | 3737 | qdict = qobject_to(QDict, obj); |
d26c9a15 KW |
3738 | |
3739 | qdict_flatten(qdict); | |
3740 | ||
9ec8873e KW |
3741 | if (!qdict_get_try_str(qdict, "node-name")) { |
3742 | error_setg(errp, "'node-name' must be specified for the root node"); | |
3743 | goto fail; | |
3744 | } | |
9c4218e9 | 3745 | |
9ec8873e KW |
3746 | bs = bds_tree_init(qdict, errp); |
3747 | if (!bs) { | |
3748 | goto fail; | |
d26c9a15 KW |
3749 | } |
3750 | ||
89802d5a | 3751 | bdrv_set_monitor_owned(bs); |
9ec8873e | 3752 | |
d26c9a15 | 3753 | fail: |
3b098d56 | 3754 | visit_free(v); |
d26c9a15 KW |
3755 | } |
3756 | ||
1479c730 AG |
3757 | void qmp_x_blockdev_reopen(BlockdevOptions *options, Error **errp) |
3758 | { | |
3759 | BlockDriverState *bs; | |
3760 | AioContext *ctx; | |
3761 | QObject *obj; | |
3762 | Visitor *v = qobject_output_visitor_new(&obj); | |
3763 | Error *local_err = NULL; | |
3764 | BlockReopenQueue *queue; | |
3765 | QDict *qdict; | |
3766 | ||
3767 | /* Check for the selected node name */ | |
3768 | if (!options->has_node_name) { | |
3769 | error_setg(errp, "Node name not specified"); | |
3770 | goto fail; | |
3771 | } | |
3772 | ||
3773 | bs = bdrv_find_node(options->node_name); | |
3774 | if (!bs) { | |
3775 | error_setg(errp, "Cannot find node named '%s'", options->node_name); | |
3776 | goto fail; | |
3777 | } | |
3778 | ||
3779 | /* Put all options in a QDict and flatten it */ | |
3780 | visit_type_BlockdevOptions(v, NULL, &options, &local_err); | |
3781 | if (local_err) { | |
3782 | error_propagate(errp, local_err); | |
3783 | goto fail; | |
3784 | } | |
3785 | ||
3786 | visit_complete(v, &obj); | |
3787 | qdict = qobject_to(QDict, obj); | |
3788 | ||
3789 | qdict_flatten(qdict); | |
3790 | ||
3791 | /* Perform the reopen operation */ | |
3792 | ctx = bdrv_get_aio_context(bs); | |
3793 | aio_context_acquire(ctx); | |
3794 | bdrv_subtree_drained_begin(bs); | |
3795 | queue = bdrv_reopen_queue(NULL, bs, qdict, false); | |
3796 | bdrv_reopen_multiple(queue, errp); | |
3797 | bdrv_subtree_drained_end(bs); | |
3798 | aio_context_release(ctx); | |
3799 | ||
3800 | fail: | |
3801 | visit_free(v); | |
3802 | } | |
3803 | ||
79b7a77e | 3804 | void qmp_blockdev_del(const char *node_name, Error **errp) |
81b936ae AG |
3805 | { |
3806 | AioContext *aio_context; | |
81b936ae AG |
3807 | BlockDriverState *bs; |
3808 | ||
9ec8873e KW |
3809 | bs = bdrv_find_node(node_name); |
3810 | if (!bs) { | |
3811 | error_setg(errp, "Cannot find node %s", node_name); | |
81b936ae AG |
3812 | return; |
3813 | } | |
9ec8873e KW |
3814 | if (bdrv_has_blk(bs)) { |
3815 | error_setg(errp, "Node %s is in use", node_name); | |
3816 | return; | |
81b936ae | 3817 | } |
9ec8873e | 3818 | aio_context = bdrv_get_aio_context(bs); |
81b936ae AG |
3819 | aio_context_acquire(aio_context); |
3820 | ||
9ec8873e KW |
3821 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { |
3822 | goto out; | |
3823 | } | |
9c4218e9 | 3824 | |
70537ed5 | 3825 | if (!QTAILQ_IN_USE(bs, monitor_list)) { |
9ec8873e KW |
3826 | error_setg(errp, "Node %s is not owned by the monitor", |
3827 | bs->node_name); | |
3828 | goto out; | |
81b936ae AG |
3829 | } |
3830 | ||
9ec8873e KW |
3831 | if (bs->refcnt > 1) { |
3832 | error_setg(errp, "Block device %s is in use", | |
3833 | bdrv_get_device_or_node_name(bs)); | |
3834 | goto out; | |
81b936ae AG |
3835 | } |
3836 | ||
9ec8873e KW |
3837 | QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); |
3838 | bdrv_unref(bs); | |
3839 | ||
81b936ae AG |
3840 | out: |
3841 | aio_context_release(aio_context); | |
3842 | } | |
3843 | ||
7f821597 WC |
3844 | static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, |
3845 | const char *child_name) | |
3846 | { | |
3847 | BdrvChild *child; | |
3848 | ||
3849 | QLIST_FOREACH(child, &parent_bs->children, next) { | |
3850 | if (strcmp(child->name, child_name) == 0) { | |
3851 | return child; | |
3852 | } | |
3853 | } | |
3854 | ||
3855 | return NULL; | |
3856 | } | |
3857 | ||
3858 | void qmp_x_blockdev_change(const char *parent, bool has_child, | |
3859 | const char *child, bool has_node, | |
3860 | const char *node, Error **errp) | |
3861 | { | |
3862 | BlockDriverState *parent_bs, *new_bs = NULL; | |
3863 | BdrvChild *p_child; | |
3864 | ||
3865 | parent_bs = bdrv_lookup_bs(parent, parent, errp); | |
3866 | if (!parent_bs) { | |
3867 | return; | |
3868 | } | |
3869 | ||
3870 | if (has_child == has_node) { | |
3871 | if (has_child) { | |
3872 | error_setg(errp, "The parameters child and node are in conflict"); | |
3873 | } else { | |
3874 | error_setg(errp, "Either child or node must be specified"); | |
3875 | } | |
3876 | return; | |
3877 | } | |
3878 | ||
3879 | if (has_child) { | |
3880 | p_child = bdrv_find_child(parent_bs, child); | |
3881 | if (!p_child) { | |
3882 | error_setg(errp, "Node '%s' does not have child '%s'", | |
3883 | parent, child); | |
3884 | return; | |
3885 | } | |
3886 | bdrv_del_child(parent_bs, p_child, errp); | |
3887 | } | |
3888 | ||
3889 | if (has_node) { | |
3890 | new_bs = bdrv_find_node(node); | |
3891 | if (!new_bs) { | |
3892 | error_setg(errp, "Node '%s' not found", node); | |
3893 | return; | |
3894 | } | |
3895 | bdrv_add_child(parent_bs, new_bs, errp); | |
3896 | } | |
3897 | } | |
3898 | ||
fea68bb6 | 3899 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 3900 | { |
fea68bb6 | 3901 | BlockJobInfoList *head = NULL, **p_next = &head; |
f0f55ded | 3902 | BlockJob *job; |
fb5458cd | 3903 | |
f0f55ded | 3904 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
559b935f JS |
3905 | BlockJobInfoList *elem; |
3906 | AioContext *aio_context; | |
69691e72 | 3907 | |
559b935f JS |
3908 | if (block_job_is_internal(job)) { |
3909 | continue; | |
3910 | } | |
3911 | elem = g_new0(BlockJobInfoList, 1); | |
3912 | aio_context = blk_get_aio_context(job->blk); | |
69691e72 | 3913 | aio_context_acquire(aio_context); |
559b935f | 3914 | elem->value = block_job_query(job, errp); |
69691e72 | 3915 | aio_context_release(aio_context); |
559b935f JS |
3916 | if (!elem->value) { |
3917 | g_free(elem); | |
3918 | qapi_free_BlockJobInfoList(head); | |
3919 | return NULL; | |
3920 | } | |
f0f55ded AG |
3921 | *p_next = elem; |
3922 | p_next = &elem->next; | |
fb5458cd | 3923 | } |
fb5458cd | 3924 | |
fea68bb6 | 3925 | return head; |
fb5458cd | 3926 | } |
4d454574 | 3927 | |
ca00bbb1 | 3928 | void qmp_x_blockdev_set_iothread(const char *node_name, StrOrNull *iothread, |
882e9b89 | 3929 | bool has_force, bool force, Error **errp) |
ca00bbb1 SH |
3930 | { |
3931 | AioContext *old_context; | |
3932 | AioContext *new_context; | |
3933 | BlockDriverState *bs; | |
3934 | ||
3935 | bs = bdrv_find_node(node_name); | |
3936 | if (!bs) { | |
3937 | error_setg(errp, "Cannot find node %s", node_name); | |
3938 | return; | |
3939 | } | |
3940 | ||
882e9b89 SH |
3941 | /* Protects against accidents. */ |
3942 | if (!(has_force && force) && bdrv_has_blk(bs)) { | |
3943 | error_setg(errp, "Node %s is associated with a BlockBackend and could " | |
3944 | "be in use (use force=true to override this check)", | |
3945 | node_name); | |
ca00bbb1 SH |
3946 | return; |
3947 | } | |
3948 | ||
3949 | if (iothread->type == QTYPE_QSTRING) { | |
3950 | IOThread *obj = iothread_by_id(iothread->u.s); | |
3951 | if (!obj) { | |
3952 | error_setg(errp, "Cannot find iothread %s", iothread->u.s); | |
3953 | return; | |
3954 | } | |
3955 | ||
3956 | new_context = iothread_get_aio_context(obj); | |
3957 | } else { | |
3958 | new_context = qemu_get_aio_context(); | |
3959 | } | |
3960 | ||
3961 | old_context = bdrv_get_aio_context(bs); | |
3962 | aio_context_acquire(old_context); | |
3963 | ||
8ed7d42f | 3964 | bdrv_try_set_aio_context(bs, new_context, errp); |
ca00bbb1 SH |
3965 | |
3966 | aio_context_release(old_context); | |
3967 | } | |
3968 | ||
0006383e | 3969 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 3970 | .name = "drive", |
0006383e | 3971 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
3972 | .desc = { |
3973 | { | |
4d454574 PB |
3974 | .name = "snapshot", |
3975 | .type = QEMU_OPT_BOOL, | |
3976 | .help = "enable/disable snapshot mode", | |
4d454574 PB |
3977 | },{ |
3978 | .name = "aio", | |
3979 | .type = QEMU_OPT_STRING, | |
f80f2673 | 3980 | .help = "host AIO implementation (threads, native, io_uring)", |
e4b24b49 KW |
3981 | },{ |
3982 | .name = BDRV_OPT_CACHE_WB, | |
3983 | .type = QEMU_OPT_BOOL, | |
3984 | .help = "Enable writeback mode", | |
4d454574 PB |
3985 | },{ |
3986 | .name = "format", | |
3987 | .type = QEMU_OPT_STRING, | |
3988 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
3989 | },{ |
3990 | .name = "rerror", | |
3991 | .type = QEMU_OPT_STRING, | |
3992 | .help = "read error action", | |
3993 | },{ | |
3994 | .name = "werror", | |
3995 | .type = QEMU_OPT_STRING, | |
3996 | .help = "write error action", | |
4d454574 | 3997 | },{ |
4e200cf8 | 3998 | .name = BDRV_OPT_READ_ONLY, |
4d454574 PB |
3999 | .type = QEMU_OPT_BOOL, |
4000 | .help = "open drive file as read-only", | |
a2a7862c PJ |
4001 | }, |
4002 | ||
4003 | THROTTLE_OPTS, | |
4004 | ||
4005 | { | |
76f4afb4 AG |
4006 | .name = "throttling.group", |
4007 | .type = QEMU_OPT_STRING, | |
4008 | .help = "name of the block throttling group", | |
4d454574 PB |
4009 | },{ |
4010 | .name = "copy-on-read", | |
4011 | .type = QEMU_OPT_BOOL, | |
4012 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
4013 | },{ |
4014 | .name = "detect-zeroes", | |
4015 | .type = QEMU_OPT_STRING, | |
4016 | .help = "try to optimize zero writes (off, on, unmap)", | |
362e9299 AG |
4017 | },{ |
4018 | .name = "stats-account-invalid", | |
4019 | .type = QEMU_OPT_BOOL, | |
4020 | .help = "whether to account for invalid I/O operations " | |
4021 | "in the statistics", | |
4022 | },{ | |
4023 | .name = "stats-account-failed", | |
4024 | .type = QEMU_OPT_BOOL, | |
4025 | .help = "whether to account for failed I/O operations " | |
4026 | "in the statistics", | |
4d454574 PB |
4027 | }, |
4028 | { /* end of list */ } | |
4029 | }, | |
4030 | }; | |
0006383e KW |
4031 | |
4032 | QemuOptsList qemu_drive_opts = { | |
4033 | .name = "drive", | |
4034 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
4035 | .desc = { | |
492fdc6f KW |
4036 | /* |
4037 | * no elements => accept any params | |
4038 | * validation will happen later | |
4039 | */ | |
0006383e KW |
4040 | { /* end of list */ } |
4041 | }, | |
4042 | }; |