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