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