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