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