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