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