]>
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 | |
fa510ebf | 286 | static void bdrv_put_ref_bh(void *opaque) |
aa398a5c | 287 | { |
fa510ebf | 288 | BDRVPutRefBH *s = opaque; |
aa398a5c | 289 | |
fa510ebf | 290 | bdrv_unref(s->bs); |
aa398a5c SH |
291 | qemu_bh_delete(s->bh); |
292 | g_free(s); | |
293 | } | |
294 | ||
295 | /* | |
fa510ebf | 296 | * Release a BDS reference in a BH |
aa398a5c | 297 | * |
fa510ebf FZ |
298 | * It is not safe to use bdrv_unref() from a callback function when the callers |
299 | * still need the BlockDriverState. In such cases we schedule a BH to release | |
300 | * the reference. | |
aa398a5c | 301 | */ |
fa510ebf | 302 | static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) |
aa398a5c | 303 | { |
fa510ebf | 304 | BDRVPutRefBH *s; |
aa398a5c | 305 | |
fa510ebf FZ |
306 | s = g_new(BDRVPutRefBH, 1); |
307 | s->bh = qemu_bh_new(bdrv_put_ref_bh, s); | |
308 | s->bs = bs; | |
aa398a5c SH |
309 | qemu_bh_schedule(s->bh); |
310 | } | |
311 | ||
b681072d | 312 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
313 | { |
314 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 315 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 316 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 317 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 318 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 319 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 320 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 321 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 322 | } else { |
b681072d KW |
323 | error_setg(errp, "'%s' invalid %s error action", |
324 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
325 | return -1; |
326 | } | |
327 | } | |
328 | ||
cc0681c4 | 329 | static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) |
0563e191 | 330 | { |
cc0681c4 BC |
331 | if (throttle_conflicting(cfg)) { |
332 | error_setg(errp, "bps/iops/max total values and read/write values" | |
333 | " cannot be used at the same time"); | |
0563e191 ZYW |
334 | return false; |
335 | } | |
336 | ||
cc0681c4 BC |
337 | if (!throttle_is_valid(cfg)) { |
338 | error_setg(errp, "bps/iops/maxs values must be 0 or greater"); | |
7d81c141 SH |
339 | return false; |
340 | } | |
341 | ||
ee2bdc33 SH |
342 | if (throttle_max_is_missing_limit(cfg)) { |
343 | error_setg(errp, "bps_max/iops_max require corresponding" | |
344 | " bps/iops values"); | |
345 | return false; | |
346 | } | |
347 | ||
0563e191 ZYW |
348 | return true; |
349 | } | |
350 | ||
33cb7dc8 KW |
351 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
352 | ||
fbf8175e HR |
353 | /* All parameters but @opts are optional and may be set to NULL. */ |
354 | static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, | |
355 | const char **throttling_group, ThrottleConfig *throttle_cfg, | |
356 | BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) | |
357 | { | |
358 | const char *discard; | |
359 | Error *local_error = NULL; | |
360 | const char *aio; | |
361 | ||
362 | if (bdrv_flags) { | |
363 | if (!qemu_opt_get_bool(opts, "read-only", false)) { | |
364 | *bdrv_flags |= BDRV_O_RDWR; | |
365 | } | |
366 | if (qemu_opt_get_bool(opts, "copy-on-read", false)) { | |
367 | *bdrv_flags |= BDRV_O_COPY_ON_READ; | |
368 | } | |
369 | ||
370 | if ((discard = qemu_opt_get(opts, "discard")) != NULL) { | |
371 | if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { | |
372 | error_setg(errp, "Invalid discard option"); | |
373 | return; | |
374 | } | |
375 | } | |
376 | ||
377 | if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) { | |
378 | *bdrv_flags |= BDRV_O_CACHE_WB; | |
379 | } | |
380 | if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) { | |
381 | *bdrv_flags |= BDRV_O_NOCACHE; | |
382 | } | |
383 | if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { | |
384 | *bdrv_flags |= BDRV_O_NO_FLUSH; | |
385 | } | |
386 | ||
387 | if ((aio = qemu_opt_get(opts, "aio")) != NULL) { | |
388 | if (!strcmp(aio, "native")) { | |
389 | *bdrv_flags |= BDRV_O_NATIVE_AIO; | |
390 | } else if (!strcmp(aio, "threads")) { | |
391 | /* this is the default */ | |
392 | } else { | |
393 | error_setg(errp, "invalid aio option"); | |
394 | return; | |
395 | } | |
396 | } | |
397 | } | |
398 | ||
399 | /* disk I/O throttling */ | |
400 | if (throttling_group) { | |
401 | *throttling_group = qemu_opt_get(opts, "throttling.group"); | |
402 | } | |
403 | ||
404 | if (throttle_cfg) { | |
405 | memset(throttle_cfg, 0, sizeof(*throttle_cfg)); | |
406 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = | |
407 | qemu_opt_get_number(opts, "throttling.bps-total", 0); | |
408 | throttle_cfg->buckets[THROTTLE_BPS_READ].avg = | |
409 | qemu_opt_get_number(opts, "throttling.bps-read", 0); | |
410 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = | |
411 | qemu_opt_get_number(opts, "throttling.bps-write", 0); | |
412 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = | |
413 | qemu_opt_get_number(opts, "throttling.iops-total", 0); | |
414 | throttle_cfg->buckets[THROTTLE_OPS_READ].avg = | |
415 | qemu_opt_get_number(opts, "throttling.iops-read", 0); | |
416 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = | |
417 | qemu_opt_get_number(opts, "throttling.iops-write", 0); | |
418 | ||
419 | throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = | |
420 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
421 | throttle_cfg->buckets[THROTTLE_BPS_READ].max = | |
422 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
423 | throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = | |
424 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
425 | throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = | |
426 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
427 | throttle_cfg->buckets[THROTTLE_OPS_READ].max = | |
428 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
429 | throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = | |
430 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
431 | ||
432 | throttle_cfg->op_size = | |
433 | qemu_opt_get_number(opts, "throttling.iops-size", 0); | |
434 | ||
435 | if (!check_throttle_config(throttle_cfg, errp)) { | |
436 | return; | |
437 | } | |
438 | } | |
439 | ||
440 | if (detect_zeroes) { | |
441 | *detect_zeroes = | |
442 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, | |
443 | qemu_opt_get(opts, "detect-zeroes"), | |
444 | BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, | |
445 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, | |
446 | &local_error); | |
447 | if (local_error) { | |
448 | error_propagate(errp, local_error); | |
449 | return; | |
450 | } | |
451 | ||
452 | if (bdrv_flags && | |
453 | *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
454 | !(*bdrv_flags & BDRV_O_UNMAP)) | |
455 | { | |
456 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " | |
457 | "without setting discard operation to unmap"); | |
458 | return; | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
f298d071 | 463 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
464 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
465 | Error **errp) | |
666daa68 MA |
466 | { |
467 | const char *buf; | |
666daa68 MA |
468 | int bdrv_flags = 0; |
469 | int on_read_error, on_write_error; | |
26f54e9a | 470 | BlockBackend *blk; |
a0f1eab1 | 471 | BlockDriverState *bs; |
cc0681c4 | 472 | ThrottleConfig cfg; |
666daa68 | 473 | int snapshot = 0; |
c546194f | 474 | Error *error = NULL; |
0006383e | 475 | QemuOpts *opts; |
0006383e | 476 | const char *id; |
74fe54f2 | 477 | bool has_driver_specific_opts; |
fbf8175e HR |
478 | BlockdevDetectZeroesOptions detect_zeroes = |
479 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; | |
480 | const char *throttling_group = NULL; | |
666daa68 | 481 | |
f298d071 KW |
482 | /* Check common options by copying from bs_opts to opts, all other options |
483 | * stay in bs_opts for processing by bdrv_open(). */ | |
484 | id = qdict_get_try_str(bs_opts, "id"); | |
0006383e | 485 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
84d18f06 | 486 | if (error) { |
b681072d | 487 | error_propagate(errp, error); |
6376f952 | 488 | goto err_no_opts; |
0006383e KW |
489 | } |
490 | ||
0006383e | 491 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
84d18f06 | 492 | if (error) { |
b681072d | 493 | error_propagate(errp, error); |
ec9c10d2 | 494 | goto early_err; |
0006383e KW |
495 | } |
496 | ||
497 | if (id) { | |
498 | qdict_del(bs_opts, "id"); | |
499 | } | |
500 | ||
74fe54f2 KW |
501 | has_driver_specific_opts = !!qdict_size(bs_opts); |
502 | ||
666daa68 | 503 | /* extract parameters */ |
666daa68 | 504 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
666daa68 | 505 | |
fbf8175e HR |
506 | extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, |
507 | &detect_zeroes, &error); | |
508 | if (error) { | |
509 | error_propagate(errp, error); | |
510 | goto early_err; | |
666daa68 | 511 | } |
666daa68 MA |
512 | |
513 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 PM |
514 | if (is_help_option(buf)) { |
515 | error_printf("Supported formats:"); | |
516 | bdrv_iterate_format(bdrv_format_print, NULL); | |
517 | error_printf("\n"); | |
ec9c10d2 | 518 | goto early_err; |
666daa68 | 519 | } |
74fe54f2 | 520 | |
e4342ce5 HR |
521 | if (qdict_haskey(bs_opts, "driver")) { |
522 | error_setg(errp, "Cannot specify both 'driver' and 'format'"); | |
ec9c10d2 | 523 | goto early_err; |
6db5f5d6 | 524 | } |
e4342ce5 | 525 | qdict_put(bs_opts, "driver", qstring_from_str(buf)); |
666daa68 MA |
526 | } |
527 | ||
92aa5c6d | 528 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 529 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 530 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 531 | if (error) { |
b681072d | 532 | error_propagate(errp, error); |
ec9c10d2 | 533 | goto early_err; |
666daa68 MA |
534 | } |
535 | } | |
536 | ||
92aa5c6d | 537 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 538 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 539 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 540 | if (error) { |
b681072d | 541 | error_propagate(errp, error); |
ec9c10d2 | 542 | goto early_err; |
666daa68 MA |
543 | } |
544 | } | |
545 | ||
5ec18f8c HR |
546 | if (snapshot) { |
547 | /* always use cache=unsafe with snapshot */ | |
548 | bdrv_flags &= ~BDRV_O_CACHE_MASK; | |
549 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); | |
550 | } | |
551 | ||
326642bc | 552 | /* init */ |
e4342ce5 | 553 | if ((!file || !*file) && !has_driver_specific_opts) { |
5ec18f8c HR |
554 | BlockBackendRootState *blk_rs; |
555 | ||
556 | blk = blk_new(qemu_opts_id(opts), errp); | |
e4342ce5 HR |
557 | if (!blk) { |
558 | goto early_err; | |
559 | } | |
abd7f68d | 560 | |
5ec18f8c HR |
561 | blk_rs = blk_get_root_state(blk); |
562 | blk_rs->open_flags = bdrv_flags; | |
fbf8175e | 563 | blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); |
5ec18f8c HR |
564 | blk_rs->detect_zeroes = detect_zeroes; |
565 | ||
566 | if (throttle_enabled(&cfg)) { | |
567 | if (!throttling_group) { | |
568 | throttling_group = blk_name(blk); | |
569 | } | |
570 | blk_rs->throttle_group = g_strdup(throttling_group); | |
571 | blk_rs->throttle_state = throttle_group_incref(throttling_group); | |
572 | blk_rs->throttle_state->cfg = cfg; | |
573 | } | |
0563e191 | 574 | |
e4342ce5 HR |
575 | QDECREF(bs_opts); |
576 | } else { | |
577 | if (file && !*file) { | |
c2ad1b0c | 578 | file = NULL; |
c2ad1b0c | 579 | } |
666daa68 | 580 | |
e4342ce5 HR |
581 | blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, |
582 | errp); | |
583 | if (!blk) { | |
584 | goto err_no_bs_opts; | |
585 | } | |
586 | bs = blk_bs(blk); | |
ed9d4205 | 587 | |
5ec18f8c | 588 | bs->detect_zeroes = detect_zeroes; |
666daa68 | 589 | |
5ec18f8c HR |
590 | /* disk I/O throttling */ |
591 | if (throttle_enabled(&cfg)) { | |
592 | if (!throttling_group) { | |
593 | throttling_group = blk_name(blk); | |
594 | } | |
595 | bdrv_io_limits_enable(bs, throttling_group); | |
596 | bdrv_set_io_limits(bs, &cfg); | |
597 | } | |
0006383e | 598 | |
5ec18f8c HR |
599 | if (bdrv_key_required(bs)) { |
600 | autostart = 0; | |
76f4afb4 | 601 | } |
666daa68 MA |
602 | } |
603 | ||
5ec18f8c | 604 | blk_set_on_error(blk, on_read_error, on_write_error); |
0006383e | 605 | |
e4342ce5 | 606 | err_no_bs_opts: |
0006383e | 607 | qemu_opts_del(opts); |
18e46a03 | 608 | return blk; |
a9ae2bff | 609 | |
ec9c10d2 | 610 | early_err: |
ec9c10d2 | 611 | qemu_opts_del(opts); |
6376f952 MA |
612 | err_no_opts: |
613 | QDECREF(bs_opts); | |
a9ae2bff | 614 | return NULL; |
666daa68 MA |
615 | } |
616 | ||
5abbf0ee KW |
617 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
618 | Error **errp) | |
57975222 KW |
619 | { |
620 | const char *value; | |
621 | ||
622 | value = qemu_opt_get(opts, from); | |
623 | if (value) { | |
5abbf0ee KW |
624 | if (qemu_opt_find(opts, to)) { |
625 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
626 | "same time", to, from); | |
627 | return; | |
628 | } | |
20d6cd47 JL |
629 | } |
630 | ||
631 | /* rename all items in opts */ | |
632 | while ((value = qemu_opt_get(opts, from))) { | |
f43e47db | 633 | qemu_opt_set(opts, to, value, &error_abort); |
57975222 KW |
634 | qemu_opt_unset(opts, from); |
635 | } | |
636 | } | |
637 | ||
33cb7dc8 KW |
638 | QemuOptsList qemu_legacy_drive_opts = { |
639 | .name = "drive", | |
640 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
641 | .desc = { | |
642 | { | |
87a899c5 KW |
643 | .name = "bus", |
644 | .type = QEMU_OPT_NUMBER, | |
645 | .help = "bus number", | |
646 | },{ | |
647 | .name = "unit", | |
648 | .type = QEMU_OPT_NUMBER, | |
649 | .help = "unit number (i.e. lun for scsi)", | |
650 | },{ | |
651 | .name = "index", | |
652 | .type = QEMU_OPT_NUMBER, | |
653 | .help = "index number", | |
654 | },{ | |
33cb7dc8 KW |
655 | .name = "media", |
656 | .type = QEMU_OPT_STRING, | |
657 | .help = "media type (disk, cdrom)", | |
593d464b KW |
658 | },{ |
659 | .name = "if", | |
660 | .type = QEMU_OPT_STRING, | |
661 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
b41a7338 KW |
662 | },{ |
663 | .name = "cyls", | |
664 | .type = QEMU_OPT_NUMBER, | |
665 | .help = "number of cylinders (ide disk geometry)", | |
666 | },{ | |
667 | .name = "heads", | |
668 | .type = QEMU_OPT_NUMBER, | |
669 | .help = "number of heads (ide disk geometry)", | |
670 | },{ | |
671 | .name = "secs", | |
672 | .type = QEMU_OPT_NUMBER, | |
673 | .help = "number of sectors (ide disk geometry)", | |
674 | },{ | |
675 | .name = "trans", | |
676 | .type = QEMU_OPT_STRING, | |
677 | .help = "chs translation (auto, lba, none)", | |
26929298 KW |
678 | },{ |
679 | .name = "boot", | |
680 | .type = QEMU_OPT_BOOL, | |
681 | .help = "(deprecated, ignored)", | |
394c7d4d KW |
682 | },{ |
683 | .name = "addr", | |
684 | .type = QEMU_OPT_STRING, | |
685 | .help = "pci address (virtio only)", | |
bcf83158 KW |
686 | },{ |
687 | .name = "serial", | |
688 | .type = QEMU_OPT_STRING, | |
689 | .help = "disk serial number", | |
d095b465 HR |
690 | },{ |
691 | .name = "file", | |
692 | .type = QEMU_OPT_STRING, | |
693 | .help = "file name", | |
33cb7dc8 | 694 | }, |
0ebd24e0 KW |
695 | |
696 | /* Options that are passed on, but have special semantics with -drive */ | |
697 | { | |
698 | .name = "read-only", | |
699 | .type = QEMU_OPT_BOOL, | |
700 | .help = "open drive file as read-only", | |
ee13ed1c KW |
701 | },{ |
702 | .name = "rerror", | |
703 | .type = QEMU_OPT_STRING, | |
704 | .help = "read error action", | |
705 | },{ | |
706 | .name = "werror", | |
707 | .type = QEMU_OPT_STRING, | |
708 | .help = "write error action", | |
0ebd24e0 KW |
709 | },{ |
710 | .name = "copy-on-read", | |
711 | .type = QEMU_OPT_BOOL, | |
712 | .help = "copy read data from backing file into image file", | |
713 | }, | |
714 | ||
33cb7dc8 KW |
715 | { /* end of list */ } |
716 | }, | |
717 | }; | |
718 | ||
60e19e06 | 719 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
57975222 | 720 | { |
29c4e2b5 | 721 | const char *value; |
18e46a03 | 722 | BlockBackend *blk; |
33cb7dc8 | 723 | DriveInfo *dinfo = NULL; |
f298d071 | 724 | QDict *bs_opts; |
33cb7dc8 KW |
725 | QemuOpts *legacy_opts; |
726 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 727 | BlockInterfaceType type; |
b41a7338 | 728 | int cyls, heads, secs, translation; |
87a899c5 | 729 | int max_devs, bus_id, unit_id, index; |
394c7d4d | 730 | const char *devaddr; |
ee13ed1c | 731 | const char *werror, *rerror; |
a7fdbcf0 FZ |
732 | bool read_only = false; |
733 | bool copy_on_read; | |
bcf83158 | 734 | const char *serial; |
d095b465 | 735 | const char *filename; |
33cb7dc8 | 736 | Error *local_err = NULL; |
247147fb | 737 | int i; |
29c4e2b5 | 738 | |
57975222 | 739 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
740 | static const struct { |
741 | const char *from; | |
742 | const char *to; | |
743 | } opt_renames[] = { | |
744 | { "iops", "throttling.iops-total" }, | |
745 | { "iops_rd", "throttling.iops-read" }, | |
746 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 747 | |
247147fb KW |
748 | { "bps", "throttling.bps-total" }, |
749 | { "bps_rd", "throttling.bps-read" }, | |
750 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 751 | |
247147fb KW |
752 | { "iops_max", "throttling.iops-total-max" }, |
753 | { "iops_rd_max", "throttling.iops-read-max" }, | |
754 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 755 | |
247147fb KW |
756 | { "bps_max", "throttling.bps-total-max" }, |
757 | { "bps_rd_max", "throttling.bps-read-max" }, | |
758 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 759 | |
247147fb | 760 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 761 | |
76f4afb4 AG |
762 | { "group", "throttling.group" }, |
763 | ||
247147fb KW |
764 | { "readonly", "read-only" }, |
765 | }; | |
766 | ||
767 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
5abbf0ee KW |
768 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
769 | &local_err); | |
770 | if (local_err) { | |
565f65d2 | 771 | error_report_err(local_err); |
5abbf0ee KW |
772 | return NULL; |
773 | } | |
247147fb | 774 | } |
0f227a94 | 775 | |
29c4e2b5 KW |
776 | value = qemu_opt_get(all_opts, "cache"); |
777 | if (value) { | |
778 | int flags = 0; | |
779 | ||
780 | if (bdrv_parse_cache_flags(value, &flags) != 0) { | |
781 | error_report("invalid cache option"); | |
782 | return NULL; | |
783 | } | |
784 | ||
785 | /* Specific options take precedence */ | |
54861b92 KW |
786 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { |
787 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, | |
cccb7967 | 788 | !!(flags & BDRV_O_CACHE_WB), &error_abort); |
29c4e2b5 | 789 | } |
54861b92 KW |
790 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { |
791 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, | |
cccb7967 | 792 | !!(flags & BDRV_O_NOCACHE), &error_abort); |
29c4e2b5 | 793 | } |
54861b92 KW |
794 | if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { |
795 | qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, | |
cccb7967 | 796 | !!(flags & BDRV_O_NO_FLUSH), &error_abort); |
29c4e2b5 KW |
797 | } |
798 | qemu_opt_unset(all_opts, "cache"); | |
799 | } | |
800 | ||
f298d071 KW |
801 | /* Get a QDict for processing the options */ |
802 | bs_opts = qdict_new(); | |
803 | qemu_opts_to_qdict(all_opts, bs_opts); | |
804 | ||
87ea75d5 PC |
805 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
806 | &error_abort); | |
33cb7dc8 | 807 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
84d18f06 | 808 | if (local_err) { |
565f65d2 | 809 | error_report_err(local_err); |
33cb7dc8 KW |
810 | goto fail; |
811 | } | |
812 | ||
26929298 KW |
813 | /* Deprecated option boot=[on|off] */ |
814 | if (qemu_opt_get(legacy_opts, "boot") != NULL) { | |
815 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " | |
816 | "ignored. Future versions will reject this parameter. Please " | |
817 | "update your scripts.\n"); | |
818 | } | |
819 | ||
33cb7dc8 KW |
820 | /* Media type */ |
821 | value = qemu_opt_get(legacy_opts, "media"); | |
822 | if (value) { | |
823 | if (!strcmp(value, "disk")) { | |
824 | media = MEDIA_DISK; | |
825 | } else if (!strcmp(value, "cdrom")) { | |
826 | media = MEDIA_CDROM; | |
a7fdbcf0 | 827 | read_only = true; |
33cb7dc8 KW |
828 | } else { |
829 | error_report("'%s' invalid media", value); | |
830 | goto fail; | |
831 | } | |
832 | } | |
833 | ||
0ebd24e0 | 834 | /* copy-on-read is disabled with a warning for read-only devices */ |
a7fdbcf0 | 835 | read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); |
0ebd24e0 KW |
836 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
837 | ||
838 | if (read_only && copy_on_read) { | |
839 | error_report("warning: disabling copy-on-read on read-only drive"); | |
840 | copy_on_read = false; | |
841 | } | |
842 | ||
843 | qdict_put(bs_opts, "read-only", | |
844 | qstring_from_str(read_only ? "on" : "off")); | |
845 | qdict_put(bs_opts, "copy-on-read", | |
846 | qstring_from_str(copy_on_read ? "on" :"off")); | |
847 | ||
593d464b KW |
848 | /* Controller type */ |
849 | value = qemu_opt_get(legacy_opts, "if"); | |
850 | if (value) { | |
851 | for (type = 0; | |
852 | type < IF_COUNT && strcmp(value, if_name[type]); | |
853 | type++) { | |
854 | } | |
855 | if (type == IF_COUNT) { | |
856 | error_report("unsupported bus type '%s'", value); | |
857 | goto fail; | |
858 | } | |
859 | } else { | |
860 | type = block_default_type; | |
861 | } | |
862 | ||
b41a7338 KW |
863 | /* Geometry */ |
864 | cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); | |
865 | heads = qemu_opt_get_number(legacy_opts, "heads", 0); | |
866 | secs = qemu_opt_get_number(legacy_opts, "secs", 0); | |
867 | ||
868 | if (cyls || heads || secs) { | |
869 | if (cyls < 1) { | |
870 | error_report("invalid physical cyls number"); | |
871 | goto fail; | |
872 | } | |
873 | if (heads < 1) { | |
874 | error_report("invalid physical heads number"); | |
875 | goto fail; | |
876 | } | |
877 | if (secs < 1) { | |
878 | error_report("invalid physical secs number"); | |
879 | goto fail; | |
880 | } | |
881 | } | |
882 | ||
883 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
884 | value = qemu_opt_get(legacy_opts, "trans"); | |
885 | if (value != NULL) { | |
886 | if (!cyls) { | |
887 | error_report("'%s' trans must be used with cyls, heads and secs", | |
888 | value); | |
889 | goto fail; | |
890 | } | |
891 | if (!strcmp(value, "none")) { | |
892 | translation = BIOS_ATA_TRANSLATION_NONE; | |
893 | } else if (!strcmp(value, "lba")) { | |
894 | translation = BIOS_ATA_TRANSLATION_LBA; | |
f31c41ff PB |
895 | } else if (!strcmp(value, "large")) { |
896 | translation = BIOS_ATA_TRANSLATION_LARGE; | |
897 | } else if (!strcmp(value, "rechs")) { | |
898 | translation = BIOS_ATA_TRANSLATION_RECHS; | |
b41a7338 KW |
899 | } else if (!strcmp(value, "auto")) { |
900 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
901 | } else { | |
902 | error_report("'%s' invalid translation type", value); | |
903 | goto fail; | |
904 | } | |
905 | } | |
906 | ||
907 | if (media == MEDIA_CDROM) { | |
908 | if (cyls || secs || heads) { | |
909 | error_report("CHS can't be set with media=cdrom"); | |
910 | goto fail; | |
911 | } | |
912 | } | |
913 | ||
87a899c5 KW |
914 | /* Device address specified by bus/unit or index. |
915 | * If none was specified, try to find the first free one. */ | |
916 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
917 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
918 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
919 | ||
920 | max_devs = if_max_devs[type]; | |
921 | ||
922 | if (index != -1) { | |
923 | if (bus_id != 0 || unit_id != -1) { | |
924 | error_report("index cannot be used with bus and unit"); | |
925 | goto fail; | |
926 | } | |
927 | bus_id = drive_index_to_bus_id(type, index); | |
928 | unit_id = drive_index_to_unit_id(type, index); | |
929 | } | |
930 | ||
931 | if (unit_id == -1) { | |
932 | unit_id = 0; | |
933 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
934 | unit_id++; | |
935 | if (max_devs && unit_id >= max_devs) { | |
936 | unit_id -= max_devs; | |
937 | bus_id++; | |
938 | } | |
939 | } | |
940 | } | |
941 | ||
942 | if (max_devs && unit_id >= max_devs) { | |
943 | error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); | |
944 | goto fail; | |
945 | } | |
946 | ||
947 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
948 | error_report("drive with bus=%d, unit=%d (index=%d) exists", | |
949 | bus_id, unit_id, index); | |
950 | goto fail; | |
951 | } | |
952 | ||
bcf83158 KW |
953 | /* Serial number */ |
954 | serial = qemu_opt_get(legacy_opts, "serial"); | |
955 | ||
87a899c5 KW |
956 | /* no id supplied -> create one */ |
957 | if (qemu_opts_id(all_opts) == NULL) { | |
958 | char *new_id; | |
959 | const char *mediastr = ""; | |
960 | if (type == IF_IDE || type == IF_SCSI) { | |
961 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
962 | } | |
963 | if (max_devs) { | |
964 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
965 | mediastr, unit_id); | |
966 | } else { | |
967 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
968 | mediastr, unit_id); | |
969 | } | |
970 | qdict_put(bs_opts, "id", qstring_from_str(new_id)); | |
971 | g_free(new_id); | |
972 | } | |
973 | ||
394c7d4d KW |
974 | /* Add virtio block device */ |
975 | devaddr = qemu_opt_get(legacy_opts, "addr"); | |
976 | if (devaddr && type != IF_VIRTIO) { | |
977 | error_report("addr is not supported by this bus type"); | |
978 | goto fail; | |
979 | } | |
980 | ||
981 | if (type == IF_VIRTIO) { | |
982 | QemuOpts *devopts; | |
87ea75d5 PC |
983 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
984 | &error_abort); | |
394c7d4d | 985 | if (arch_type == QEMU_ARCH_S390X) { |
1f68f1d3 | 986 | qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); |
394c7d4d | 987 | } else { |
f43e47db | 988 | qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); |
394c7d4d | 989 | } |
f43e47db MA |
990 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), |
991 | &error_abort); | |
394c7d4d | 992 | if (devaddr) { |
f43e47db | 993 | qemu_opt_set(devopts, "addr", devaddr, &error_abort); |
394c7d4d KW |
994 | } |
995 | } | |
996 | ||
d095b465 HR |
997 | filename = qemu_opt_get(legacy_opts, "file"); |
998 | ||
ee13ed1c KW |
999 | /* Check werror/rerror compatibility with if=... */ |
1000 | werror = qemu_opt_get(legacy_opts, "werror"); | |
1001 | if (werror != NULL) { | |
1002 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
1003 | type != IF_NONE) { | |
1004 | error_report("werror is not supported by this bus type"); | |
1005 | goto fail; | |
1006 | } | |
1007 | qdict_put(bs_opts, "werror", qstring_from_str(werror)); | |
1008 | } | |
1009 | ||
1010 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
1011 | if (rerror != NULL) { | |
1012 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
1013 | type != IF_NONE) { | |
1014 | error_report("rerror is not supported by this bus type"); | |
1015 | goto fail; | |
1016 | } | |
1017 | qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); | |
1018 | } | |
1019 | ||
2d246f01 | 1020 | /* Actual block device init: Functionality shared with blockdev-add */ |
18e46a03 | 1021 | blk = blockdev_init(filename, bs_opts, &local_err); |
3cb0e25c | 1022 | bs_opts = NULL; |
18e46a03 | 1023 | if (!blk) { |
84d18f06 | 1024 | if (local_err) { |
565f65d2 | 1025 | error_report_err(local_err); |
b681072d | 1026 | } |
2d246f01 | 1027 | goto fail; |
b681072d | 1028 | } else { |
84d18f06 | 1029 | assert(!local_err); |
2d246f01 KW |
1030 | } |
1031 | ||
26f8b3a8 MA |
1032 | /* Create legacy DriveInfo */ |
1033 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 1034 | dinfo->opts = all_opts; |
2d246f01 | 1035 | |
b41a7338 KW |
1036 | dinfo->cyls = cyls; |
1037 | dinfo->heads = heads; | |
1038 | dinfo->secs = secs; | |
1039 | dinfo->trans = translation; | |
1040 | ||
ee13ed1c | 1041 | dinfo->type = type; |
87a899c5 KW |
1042 | dinfo->bus = bus_id; |
1043 | dinfo->unit = unit_id; | |
394c7d4d | 1044 | dinfo->devaddr = devaddr; |
bcf83158 KW |
1045 | dinfo->serial = g_strdup(serial); |
1046 | ||
26f8b3a8 MA |
1047 | blk_set_legacy_dinfo(blk, dinfo); |
1048 | ||
e34ef046 KW |
1049 | switch(type) { |
1050 | case IF_IDE: | |
1051 | case IF_SCSI: | |
1052 | case IF_XEN: | |
1053 | case IF_NONE: | |
1054 | dinfo->media_cd = media == MEDIA_CDROM; | |
1055 | break; | |
1056 | default: | |
1057 | break; | |
1058 | } | |
1059 | ||
2d246f01 | 1060 | fail: |
33cb7dc8 | 1061 | qemu_opts_del(legacy_opts); |
3cb0e25c | 1062 | QDECREF(bs_opts); |
2d246f01 | 1063 | return dinfo; |
57975222 KW |
1064 | } |
1065 | ||
3e5a50d6 | 1066 | void hmp_commit(Monitor *mon, const QDict *qdict) |
666daa68 | 1067 | { |
666daa68 | 1068 | const char *device = qdict_get_str(qdict, "device"); |
a0e8544c | 1069 | BlockBackend *blk; |
e8877497 | 1070 | int ret; |
666daa68 | 1071 | |
6ab4b5ab | 1072 | if (!strcmp(device, "all")) { |
e8877497 | 1073 | ret = bdrv_commit_all(); |
6ab4b5ab | 1074 | } else { |
a0e8544c FZ |
1075 | blk = blk_by_name(device); |
1076 | if (!blk) { | |
58513bde | 1077 | monitor_printf(mon, "Device '%s' not found\n", device); |
ac59eb95 | 1078 | return; |
6ab4b5ab | 1079 | } |
5433c24f HR |
1080 | if (!blk_is_available(blk)) { |
1081 | monitor_printf(mon, "Device '%s' has no medium\n", device); | |
1082 | return; | |
1083 | } | |
a0e8544c | 1084 | ret = bdrv_commit(blk_bs(blk)); |
58513bde JC |
1085 | } |
1086 | if (ret < 0) { | |
1087 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
1088 | strerror(-ret)); | |
666daa68 MA |
1089 | } |
1090 | } | |
1091 | ||
6cc2a415 PB |
1092 | static void blockdev_do_action(int kind, void *data, Error **errp) |
1093 | { | |
c8a83e85 KW |
1094 | TransactionAction action; |
1095 | TransactionActionList list; | |
6cc2a415 PB |
1096 | |
1097 | action.kind = kind; | |
1098 | action.data = data; | |
1099 | list.value = &action; | |
1100 | list.next = NULL; | |
1101 | qmp_transaction(&list, errp); | |
1102 | } | |
1103 | ||
0901f67e BC |
1104 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1105 | bool has_node_name, const char *node_name, | |
1106 | const char *snapshot_file, | |
1107 | bool has_snapshot_node_name, | |
1108 | const char *snapshot_node_name, | |
6106e249 | 1109 | bool has_format, const char *format, |
0901f67e | 1110 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1111 | { |
6cc2a415 | 1112 | BlockdevSnapshot snapshot = { |
0901f67e | 1113 | .has_device = has_device, |
6cc2a415 | 1114 | .device = (char *) device, |
0901f67e BC |
1115 | .has_node_name = has_node_name, |
1116 | .node_name = (char *) node_name, | |
6cc2a415 | 1117 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1118 | .has_snapshot_node_name = has_snapshot_node_name, |
1119 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1120 | .has_format = has_format, |
1121 | .format = (char *) format, | |
1122 | .has_mode = has_mode, | |
1123 | .mode = mode, | |
1124 | }; | |
c8a83e85 KW |
1125 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
1126 | &snapshot, errp); | |
f8882568 JS |
1127 | } |
1128 | ||
f323bc9e WX |
1129 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1130 | const char *name, | |
1131 | Error **errp) | |
1132 | { | |
1133 | BlockdevSnapshotInternal snapshot = { | |
1134 | .device = (char *) device, | |
1135 | .name = (char *) name | |
1136 | }; | |
1137 | ||
1138 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
1139 | &snapshot, errp); | |
1140 | } | |
1141 | ||
44e3e053 WX |
1142 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1143 | bool has_id, | |
1144 | const char *id, | |
1145 | bool has_name, | |
1146 | const char *name, | |
1147 | Error **errp) | |
1148 | { | |
a0e8544c FZ |
1149 | BlockDriverState *bs; |
1150 | BlockBackend *blk; | |
4ef3982a | 1151 | AioContext *aio_context; |
44e3e053 WX |
1152 | QEMUSnapshotInfo sn; |
1153 | Error *local_err = NULL; | |
1154 | SnapshotInfo *info = NULL; | |
1155 | int ret; | |
1156 | ||
a0e8544c FZ |
1157 | blk = blk_by_name(device); |
1158 | if (!blk) { | |
75158ebb MA |
1159 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1160 | "Device '%s' not found", device); | |
44e3e053 WX |
1161 | return NULL; |
1162 | } | |
5433c24f HR |
1163 | |
1164 | aio_context = blk_get_aio_context(blk); | |
1165 | aio_context_acquire(aio_context); | |
44e3e053 WX |
1166 | |
1167 | if (!has_id) { | |
1168 | id = NULL; | |
1169 | } | |
1170 | ||
1171 | if (!has_name) { | |
1172 | name = NULL; | |
1173 | } | |
1174 | ||
1175 | if (!id && !name) { | |
1176 | error_setg(errp, "Name or id must be provided"); | |
5433c24f | 1177 | goto out_aio_context; |
44e3e053 WX |
1178 | } |
1179 | ||
5433c24f HR |
1180 | if (!blk_is_available(blk)) { |
1181 | error_setg(errp, "Device '%s' has no medium", device); | |
1182 | goto out_aio_context; | |
1183 | } | |
1184 | bs = blk_bs(blk); | |
4ef3982a | 1185 | |
0b928854 SH |
1186 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { |
1187 | goto out_aio_context; | |
1188 | } | |
1189 | ||
44e3e053 | 1190 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); |
84d18f06 | 1191 | if (local_err) { |
44e3e053 | 1192 | error_propagate(errp, local_err); |
4ef3982a | 1193 | goto out_aio_context; |
44e3e053 WX |
1194 | } |
1195 | if (!ret) { | |
1196 | error_setg(errp, | |
1197 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1198 | "device '%s'", | |
1199 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
4ef3982a | 1200 | goto out_aio_context; |
44e3e053 WX |
1201 | } |
1202 | ||
1203 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1204 | if (local_err) { |
44e3e053 | 1205 | error_propagate(errp, local_err); |
4ef3982a | 1206 | goto out_aio_context; |
44e3e053 WX |
1207 | } |
1208 | ||
4ef3982a SH |
1209 | aio_context_release(aio_context); |
1210 | ||
5839e53b | 1211 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1212 | info->id = g_strdup(sn.id_str); |
1213 | info->name = g_strdup(sn.name); | |
1214 | info->date_nsec = sn.date_nsec; | |
1215 | info->date_sec = sn.date_sec; | |
1216 | info->vm_state_size = sn.vm_state_size; | |
1217 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1218 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
1219 | ||
1220 | return info; | |
4ef3982a SH |
1221 | |
1222 | out_aio_context: | |
1223 | aio_context_release(aio_context); | |
1224 | return NULL; | |
44e3e053 | 1225 | } |
8802d1fd | 1226 | |
341ebc2f JS |
1227 | /** |
1228 | * block_dirty_bitmap_lookup: | |
1229 | * Return a dirty bitmap (if present), after validating | |
1230 | * the node reference and bitmap names. | |
1231 | * | |
1232 | * @node: The name of the BDS node to search for bitmaps | |
1233 | * @name: The name of the bitmap to search for | |
1234 | * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. | |
1235 | * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. | |
1236 | * @errp: Output pointer for error information. Can be NULL. | |
1237 | * | |
1238 | * @return: A bitmap object on success, or NULL on failure. | |
1239 | */ | |
1240 | static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, | |
1241 | const char *name, | |
1242 | BlockDriverState **pbs, | |
1243 | AioContext **paio, | |
1244 | Error **errp) | |
1245 | { | |
1246 | BlockDriverState *bs; | |
1247 | BdrvDirtyBitmap *bitmap; | |
1248 | AioContext *aio_context; | |
1249 | ||
1250 | if (!node) { | |
1251 | error_setg(errp, "Node cannot be NULL"); | |
1252 | return NULL; | |
1253 | } | |
1254 | if (!name) { | |
1255 | error_setg(errp, "Bitmap name cannot be NULL"); | |
1256 | return NULL; | |
1257 | } | |
1258 | bs = bdrv_lookup_bs(node, node, NULL); | |
1259 | if (!bs) { | |
1260 | error_setg(errp, "Node '%s' not found", node); | |
1261 | return NULL; | |
1262 | } | |
1263 | ||
1264 | aio_context = bdrv_get_aio_context(bs); | |
1265 | aio_context_acquire(aio_context); | |
1266 | ||
1267 | bitmap = bdrv_find_dirty_bitmap(bs, name); | |
1268 | if (!bitmap) { | |
1269 | error_setg(errp, "Dirty bitmap '%s' not found", name); | |
1270 | goto fail; | |
1271 | } | |
1272 | ||
1273 | if (pbs) { | |
1274 | *pbs = bs; | |
1275 | } | |
1276 | if (paio) { | |
1277 | *paio = aio_context; | |
1278 | } else { | |
1279 | aio_context_release(aio_context); | |
1280 | } | |
1281 | ||
1282 | return bitmap; | |
1283 | ||
1284 | fail: | |
1285 | aio_context_release(aio_context); | |
1286 | return NULL; | |
1287 | } | |
1288 | ||
b756b9ce | 1289 | /* New and old BlockDriverState structs for atomic group operations */ |
ba0c86a3 | 1290 | |
ba5d6ab6 | 1291 | typedef struct BlkTransactionState BlkTransactionState; |
ba0c86a3 WX |
1292 | |
1293 | /* Only prepare() may fail. In a single transaction, only one of commit() or | |
1294 | abort() will be called, clean() will always be called if it present. */ | |
1295 | typedef struct BdrvActionOps { | |
1296 | /* Size of state struct, in bytes. */ | |
1297 | size_t instance_size; | |
1298 | /* Prepare the work, must NOT be NULL. */ | |
ba5d6ab6 | 1299 | void (*prepare)(BlkTransactionState *common, Error **errp); |
f9ea81e8 | 1300 | /* Commit the changes, can be NULL. */ |
ba5d6ab6 | 1301 | void (*commit)(BlkTransactionState *common); |
ba0c86a3 | 1302 | /* Abort the changes on fail, can be NULL. */ |
ba5d6ab6 | 1303 | void (*abort)(BlkTransactionState *common); |
ba0c86a3 | 1304 | /* Clean up resource in the end, can be NULL. */ |
ba5d6ab6 | 1305 | void (*clean)(BlkTransactionState *common); |
ba0c86a3 WX |
1306 | } BdrvActionOps; |
1307 | ||
1308 | /* | |
1309 | * This structure must be arranged as first member in child type, assuming | |
1310 | * that compiler will also arrange it to the same address with parent instance. | |
1311 | * Later it will be used in free(). | |
1312 | */ | |
ba5d6ab6 | 1313 | struct BlkTransactionState { |
c8a83e85 | 1314 | TransactionAction *action; |
ba0c86a3 | 1315 | const BdrvActionOps *ops; |
ba5d6ab6 | 1316 | QSIMPLEQ_ENTRY(BlkTransactionState) entry; |
ba0c86a3 WX |
1317 | }; |
1318 | ||
bbe86010 WX |
1319 | /* internal snapshot private data */ |
1320 | typedef struct InternalSnapshotState { | |
1321 | BlkTransactionState common; | |
1322 | BlockDriverState *bs; | |
5d6e96ef | 1323 | AioContext *aio_context; |
bbe86010 WX |
1324 | QEMUSnapshotInfo sn; |
1325 | } InternalSnapshotState; | |
1326 | ||
1327 | static void internal_snapshot_prepare(BlkTransactionState *common, | |
1328 | Error **errp) | |
1329 | { | |
f70edf99 | 1330 | Error *local_err = NULL; |
bbe86010 WX |
1331 | const char *device; |
1332 | const char *name; | |
a0e8544c | 1333 | BlockBackend *blk; |
bbe86010 WX |
1334 | BlockDriverState *bs; |
1335 | QEMUSnapshotInfo old_sn, *sn; | |
1336 | bool ret; | |
1337 | qemu_timeval tv; | |
1338 | BlockdevSnapshotInternal *internal; | |
1339 | InternalSnapshotState *state; | |
1340 | int ret1; | |
1341 | ||
1342 | g_assert(common->action->kind == | |
1343 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); | |
1344 | internal = common->action->blockdev_snapshot_internal_sync; | |
1345 | state = DO_UPCAST(InternalSnapshotState, common, common); | |
1346 | ||
1347 | /* 1. parse input */ | |
1348 | device = internal->device; | |
1349 | name = internal->name; | |
1350 | ||
1351 | /* 2. check for validation */ | |
a0e8544c FZ |
1352 | blk = blk_by_name(device); |
1353 | if (!blk) { | |
75158ebb MA |
1354 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1355 | "Device '%s' not found", device); | |
bbe86010 WX |
1356 | return; |
1357 | } | |
1358 | ||
5d6e96ef | 1359 | /* AioContext is released in .clean() */ |
5433c24f | 1360 | state->aio_context = blk_get_aio_context(blk); |
5d6e96ef SH |
1361 | aio_context_acquire(state->aio_context); |
1362 | ||
5433c24f | 1363 | if (!blk_is_available(blk)) { |
c6bd8c70 | 1364 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
bbe86010 WX |
1365 | return; |
1366 | } | |
5433c24f | 1367 | bs = blk_bs(blk); |
bbe86010 | 1368 | |
3dc7ca3c SH |
1369 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { |
1370 | return; | |
1371 | } | |
1372 | ||
bbe86010 | 1373 | if (bdrv_is_read_only(bs)) { |
81e5f78a | 1374 | error_setg(errp, "Device '%s' is read only", device); |
bbe86010 WX |
1375 | return; |
1376 | } | |
1377 | ||
1378 | if (!bdrv_can_snapshot(bs)) { | |
81e5f78a AG |
1379 | error_setg(errp, "Block format '%s' used by device '%s' " |
1380 | "does not support internal snapshots", | |
1381 | bs->drv->format_name, device); | |
bbe86010 WX |
1382 | return; |
1383 | } | |
1384 | ||
1385 | if (!strlen(name)) { | |
1386 | error_setg(errp, "Name is empty"); | |
1387 | return; | |
1388 | } | |
1389 | ||
1390 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1391 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1392 | &local_err); | |
1393 | if (local_err) { | |
1394 | error_propagate(errp, local_err); | |
bbe86010 WX |
1395 | return; |
1396 | } else if (ret) { | |
1397 | error_setg(errp, | |
1398 | "Snapshot with name '%s' already exists on device '%s'", | |
1399 | name, device); | |
1400 | return; | |
1401 | } | |
1402 | ||
1403 | /* 3. take the snapshot */ | |
1404 | sn = &state->sn; | |
1405 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1406 | qemu_gettimeofday(&tv); | |
1407 | sn->date_sec = tv.tv_sec; | |
1408 | sn->date_nsec = tv.tv_usec * 1000; | |
1409 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1410 | ||
1411 | ret1 = bdrv_snapshot_create(bs, sn); | |
1412 | if (ret1 < 0) { | |
1413 | error_setg_errno(errp, -ret1, | |
1414 | "Failed to create snapshot '%s' on device '%s'", | |
1415 | name, device); | |
1416 | return; | |
1417 | } | |
1418 | ||
1419 | /* 4. succeed, mark a snapshot is created */ | |
1420 | state->bs = bs; | |
1421 | } | |
1422 | ||
1423 | static void internal_snapshot_abort(BlkTransactionState *common) | |
1424 | { | |
1425 | InternalSnapshotState *state = | |
1426 | DO_UPCAST(InternalSnapshotState, common, common); | |
1427 | BlockDriverState *bs = state->bs; | |
1428 | QEMUSnapshotInfo *sn = &state->sn; | |
1429 | Error *local_error = NULL; | |
1430 | ||
1431 | if (!bs) { | |
1432 | return; | |
1433 | } | |
1434 | ||
1435 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { | |
1436 | error_report("Failed to delete snapshot with id '%s' and name '%s' on " | |
1437 | "device '%s' in abort: %s", | |
1438 | sn->id_str, | |
1439 | sn->name, | |
1440 | bdrv_get_device_name(bs), | |
1441 | error_get_pretty(local_error)); | |
1442 | error_free(local_error); | |
1443 | } | |
1444 | } | |
1445 | ||
5d6e96ef SH |
1446 | static void internal_snapshot_clean(BlkTransactionState *common) |
1447 | { | |
1448 | InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, | |
1449 | common, common); | |
1450 | ||
1451 | if (state->aio_context) { | |
1452 | aio_context_release(state->aio_context); | |
1453 | } | |
1454 | } | |
1455 | ||
ba0c86a3 | 1456 | /* external snapshot private data */ |
ba5d6ab6 SH |
1457 | typedef struct ExternalSnapshotState { |
1458 | BlkTransactionState common; | |
8802d1fd JC |
1459 | BlockDriverState *old_bs; |
1460 | BlockDriverState *new_bs; | |
5d6e96ef | 1461 | AioContext *aio_context; |
ba5d6ab6 | 1462 | } ExternalSnapshotState; |
8802d1fd | 1463 | |
ba5d6ab6 | 1464 | static void external_snapshot_prepare(BlkTransactionState *common, |
9b9877ee WX |
1465 | Error **errp) |
1466 | { | |
9b9877ee | 1467 | int flags, ret; |
e6641719 | 1468 | QDict *options; |
9b9877ee | 1469 | Error *local_err = NULL; |
0901f67e | 1470 | bool has_device = false; |
e2a31e87 | 1471 | const char *device; |
0901f67e BC |
1472 | bool has_node_name = false; |
1473 | const char *node_name; | |
1474 | bool has_snapshot_node_name = false; | |
1475 | const char *snapshot_node_name; | |
e2a31e87 WX |
1476 | const char *new_image_file; |
1477 | const char *format = "qcow2"; | |
1478 | enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
ba5d6ab6 SH |
1479 | ExternalSnapshotState *state = |
1480 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1481 | TransactionAction *action = common->action; |
9b9877ee | 1482 | |
e2a31e87 | 1483 | /* get parameters */ |
c8a83e85 | 1484 | g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); |
e2a31e87 | 1485 | |
0901f67e | 1486 | has_device = action->blockdev_snapshot_sync->has_device; |
e2a31e87 | 1487 | device = action->blockdev_snapshot_sync->device; |
0901f67e BC |
1488 | has_node_name = action->blockdev_snapshot_sync->has_node_name; |
1489 | node_name = action->blockdev_snapshot_sync->node_name; | |
1490 | has_snapshot_node_name = | |
1491 | action->blockdev_snapshot_sync->has_snapshot_node_name; | |
1492 | snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name; | |
1493 | ||
e2a31e87 WX |
1494 | new_image_file = action->blockdev_snapshot_sync->snapshot_file; |
1495 | if (action->blockdev_snapshot_sync->has_format) { | |
1496 | format = action->blockdev_snapshot_sync->format; | |
1497 | } | |
1498 | if (action->blockdev_snapshot_sync->has_mode) { | |
1499 | mode = action->blockdev_snapshot_sync->mode; | |
1500 | } | |
1501 | ||
1502 | /* start processing */ | |
0901f67e BC |
1503 | state->old_bs = bdrv_lookup_bs(has_device ? device : NULL, |
1504 | has_node_name ? node_name : NULL, | |
1505 | &local_err); | |
84d18f06 | 1506 | if (local_err) { |
0901f67e BC |
1507 | error_propagate(errp, local_err); |
1508 | return; | |
1509 | } | |
1510 | ||
1511 | if (has_node_name && !has_snapshot_node_name) { | |
1512 | error_setg(errp, "New snapshot node name missing"); | |
1513 | return; | |
1514 | } | |
1515 | ||
1516 | if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) { | |
1517 | error_setg(errp, "New snapshot node name already existing"); | |
9b9877ee WX |
1518 | return; |
1519 | } | |
1520 | ||
5d6e96ef SH |
1521 | /* Acquire AioContext now so any threads operating on old_bs stop */ |
1522 | state->aio_context = bdrv_get_aio_context(state->old_bs); | |
1523 | aio_context_acquire(state->aio_context); | |
1524 | ||
ba5d6ab6 | 1525 | if (!bdrv_is_inserted(state->old_bs)) { |
c6bd8c70 | 1526 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
9b9877ee WX |
1527 | return; |
1528 | } | |
1529 | ||
3718d8ab FZ |
1530 | if (bdrv_op_is_blocked(state->old_bs, |
1531 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
9b9877ee WX |
1532 | return; |
1533 | } | |
1534 | ||
ba5d6ab6 SH |
1535 | if (!bdrv_is_read_only(state->old_bs)) { |
1536 | if (bdrv_flush(state->old_bs)) { | |
c6bd8c70 | 1537 | error_setg(errp, QERR_IO_ERROR); |
9b9877ee WX |
1538 | return; |
1539 | } | |
1540 | } | |
1541 | ||
212a5a8f | 1542 | if (!bdrv_is_first_non_filter(state->old_bs)) { |
c6bd8c70 | 1543 | error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); |
f6186f49 BC |
1544 | return; |
1545 | } | |
1546 | ||
ba5d6ab6 | 1547 | flags = state->old_bs->open_flags; |
9b9877ee | 1548 | |
9b9877ee WX |
1549 | /* create new image w/backing file */ |
1550 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1551 | bdrv_img_create(new_image_file, format, | |
ba5d6ab6 SH |
1552 | state->old_bs->filename, |
1553 | state->old_bs->drv->format_name, | |
9b9877ee | 1554 | NULL, -1, flags, &local_err, false); |
84d18f06 | 1555 | if (local_err) { |
9b9877ee WX |
1556 | error_propagate(errp, local_err); |
1557 | return; | |
1558 | } | |
1559 | } | |
1560 | ||
e6641719 | 1561 | options = qdict_new(); |
0901f67e | 1562 | if (has_snapshot_node_name) { |
0901f67e BC |
1563 | qdict_put(options, "node-name", |
1564 | qstring_from_str(snapshot_node_name)); | |
1565 | } | |
e6641719 | 1566 | qdict_put(options, "driver", qstring_from_str(format)); |
0901f67e | 1567 | |
9b9877ee WX |
1568 | /* TODO Inherit bs->options or only take explicit options with an |
1569 | * extended QMP command? */ | |
f67503e5 | 1570 | assert(state->new_bs == NULL); |
ddf5636d | 1571 | ret = bdrv_open(&state->new_bs, new_image_file, NULL, options, |
6ebf9aa2 | 1572 | flags | BDRV_O_NO_BACKING, &local_err); |
f67503e5 | 1573 | /* We will manually add the backing_hd field to the bs later */ |
9b9877ee | 1574 | if (ret != 0) { |
34b5d2c6 | 1575 | error_propagate(errp, local_err); |
9b9877ee WX |
1576 | } |
1577 | } | |
1578 | ||
ba5d6ab6 | 1579 | static void external_snapshot_commit(BlkTransactionState *common) |
3b0047e8 | 1580 | { |
ba5d6ab6 SH |
1581 | ExternalSnapshotState *state = |
1582 | DO_UPCAST(ExternalSnapshotState, common, common); | |
ba0c86a3 | 1583 | |
5d6e96ef SH |
1584 | bdrv_set_aio_context(state->new_bs, state->aio_context); |
1585 | ||
ba5d6ab6 SH |
1586 | /* This removes our old bs and adds the new bs */ |
1587 | bdrv_append(state->new_bs, state->old_bs); | |
3b0047e8 WX |
1588 | /* We don't need (or want) to use the transactional |
1589 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1590 | * don't want to abort all of them if one of them fails the reopen */ | |
dd62f1ca | 1591 | bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, |
3b0047e8 | 1592 | NULL); |
5d6e96ef SH |
1593 | |
1594 | aio_context_release(state->aio_context); | |
3b0047e8 WX |
1595 | } |
1596 | ||
ba5d6ab6 | 1597 | static void external_snapshot_abort(BlkTransactionState *common) |
96b86bf7 | 1598 | { |
ba5d6ab6 SH |
1599 | ExternalSnapshotState *state = |
1600 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1601 | if (state->new_bs) { | |
4f6fd349 | 1602 | bdrv_unref(state->new_bs); |
96b86bf7 | 1603 | } |
5d6e96ef SH |
1604 | if (state->aio_context) { |
1605 | aio_context_release(state->aio_context); | |
1606 | } | |
96b86bf7 WX |
1607 | } |
1608 | ||
3037f364 SH |
1609 | typedef struct DriveBackupState { |
1610 | BlkTransactionState common; | |
1611 | BlockDriverState *bs; | |
5d6e96ef | 1612 | AioContext *aio_context; |
3037f364 SH |
1613 | BlockJob *job; |
1614 | } DriveBackupState; | |
1615 | ||
1616 | static void drive_backup_prepare(BlkTransactionState *common, Error **errp) | |
1617 | { | |
1618 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
a0e8544c | 1619 | BlockBackend *blk; |
3037f364 SH |
1620 | DriveBackup *backup; |
1621 | Error *local_err = NULL; | |
1622 | ||
1623 | assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); | |
1624 | backup = common->action->drive_backup; | |
1625 | ||
a0e8544c FZ |
1626 | blk = blk_by_name(backup->device); |
1627 | if (!blk) { | |
75158ebb MA |
1628 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1629 | "Device '%s' not found", backup->device); | |
5d6e96ef SH |
1630 | return; |
1631 | } | |
1632 | ||
1633 | /* AioContext is released in .clean() */ | |
5433c24f | 1634 | state->aio_context = blk_get_aio_context(blk); |
5d6e96ef SH |
1635 | aio_context_acquire(state->aio_context); |
1636 | ||
3037f364 SH |
1637 | qmp_drive_backup(backup->device, backup->target, |
1638 | backup->has_format, backup->format, | |
b53169ea | 1639 | backup->sync, |
3037f364 SH |
1640 | backup->has_mode, backup->mode, |
1641 | backup->has_speed, backup->speed, | |
d58d8453 | 1642 | backup->has_bitmap, backup->bitmap, |
3037f364 SH |
1643 | backup->has_on_source_error, backup->on_source_error, |
1644 | backup->has_on_target_error, backup->on_target_error, | |
1645 | &local_err); | |
84d18f06 | 1646 | if (local_err) { |
3037f364 | 1647 | error_propagate(errp, local_err); |
3037f364 SH |
1648 | return; |
1649 | } | |
1650 | ||
5433c24f | 1651 | state->bs = blk_bs(blk); |
3037f364 SH |
1652 | state->job = state->bs->job; |
1653 | } | |
1654 | ||
1655 | static void drive_backup_abort(BlkTransactionState *common) | |
1656 | { | |
1657 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1658 | BlockDriverState *bs = state->bs; | |
1659 | ||
1660 | /* Only cancel if it's the job we started */ | |
1661 | if (bs && bs->job && bs->job == state->job) { | |
1662 | block_job_cancel_sync(bs->job); | |
1663 | } | |
1664 | } | |
1665 | ||
5d6e96ef SH |
1666 | static void drive_backup_clean(BlkTransactionState *common) |
1667 | { | |
1668 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1669 | ||
1670 | if (state->aio_context) { | |
1671 | aio_context_release(state->aio_context); | |
1672 | } | |
1673 | } | |
1674 | ||
bd8baecd FZ |
1675 | typedef struct BlockdevBackupState { |
1676 | BlkTransactionState common; | |
1677 | BlockDriverState *bs; | |
1678 | BlockJob *job; | |
1679 | AioContext *aio_context; | |
1680 | } BlockdevBackupState; | |
1681 | ||
1682 | static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp) | |
1683 | { | |
1684 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1685 | BlockdevBackup *backup; | |
5433c24f | 1686 | BlockBackend *blk, *target; |
bd8baecd FZ |
1687 | Error *local_err = NULL; |
1688 | ||
1689 | assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); | |
1690 | backup = common->action->blockdev_backup; | |
1691 | ||
a0e8544c FZ |
1692 | blk = blk_by_name(backup->device); |
1693 | if (!blk) { | |
5b347c54 | 1694 | error_setg(errp, "Device '%s' not found", backup->device); |
bd8baecd FZ |
1695 | return; |
1696 | } | |
1697 | ||
5433c24f HR |
1698 | target = blk_by_name(backup->target); |
1699 | if (!target) { | |
5b347c54 | 1700 | error_setg(errp, "Device '%s' not found", backup->target); |
bd8baecd FZ |
1701 | return; |
1702 | } | |
1703 | ||
1704 | /* AioContext is released in .clean() */ | |
5433c24f HR |
1705 | state->aio_context = blk_get_aio_context(blk); |
1706 | if (state->aio_context != blk_get_aio_context(target)) { | |
bd8baecd FZ |
1707 | state->aio_context = NULL; |
1708 | error_setg(errp, "Backup between two IO threads is not implemented"); | |
1709 | return; | |
1710 | } | |
1711 | aio_context_acquire(state->aio_context); | |
1712 | ||
1713 | qmp_blockdev_backup(backup->device, backup->target, | |
1714 | backup->sync, | |
1715 | backup->has_speed, backup->speed, | |
1716 | backup->has_on_source_error, backup->on_source_error, | |
1717 | backup->has_on_target_error, backup->on_target_error, | |
1718 | &local_err); | |
1719 | if (local_err) { | |
1720 | error_propagate(errp, local_err); | |
1721 | return; | |
1722 | } | |
1723 | ||
5433c24f | 1724 | state->bs = blk_bs(blk); |
bd8baecd FZ |
1725 | state->job = state->bs->job; |
1726 | } | |
1727 | ||
1728 | static void blockdev_backup_abort(BlkTransactionState *common) | |
1729 | { | |
1730 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1731 | BlockDriverState *bs = state->bs; | |
1732 | ||
1733 | /* Only cancel if it's the job we started */ | |
1734 | if (bs && bs->job && bs->job == state->job) { | |
1735 | block_job_cancel_sync(bs->job); | |
1736 | } | |
1737 | } | |
1738 | ||
1739 | static void blockdev_backup_clean(BlkTransactionState *common) | |
1740 | { | |
1741 | BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); | |
1742 | ||
1743 | if (state->aio_context) { | |
1744 | aio_context_release(state->aio_context); | |
1745 | } | |
1746 | } | |
1747 | ||
78b18b78 SH |
1748 | static void abort_prepare(BlkTransactionState *common, Error **errp) |
1749 | { | |
1750 | error_setg(errp, "Transaction aborted using Abort action"); | |
1751 | } | |
1752 | ||
1753 | static void abort_commit(BlkTransactionState *common) | |
1754 | { | |
dfc6f865 | 1755 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
1756 | } |
1757 | ||
ba0c86a3 | 1758 | static const BdrvActionOps actions[] = { |
c8a83e85 | 1759 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 1760 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
1761 | .prepare = external_snapshot_prepare, |
1762 | .commit = external_snapshot_commit, | |
1763 | .abort = external_snapshot_abort, | |
1764 | }, | |
3037f364 SH |
1765 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
1766 | .instance_size = sizeof(DriveBackupState), | |
1767 | .prepare = drive_backup_prepare, | |
1768 | .abort = drive_backup_abort, | |
5d6e96ef | 1769 | .clean = drive_backup_clean, |
3037f364 | 1770 | }, |
bd8baecd FZ |
1771 | [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { |
1772 | .instance_size = sizeof(BlockdevBackupState), | |
1773 | .prepare = blockdev_backup_prepare, | |
1774 | .abort = blockdev_backup_abort, | |
1775 | .clean = blockdev_backup_clean, | |
1776 | }, | |
78b18b78 SH |
1777 | [TRANSACTION_ACTION_KIND_ABORT] = { |
1778 | .instance_size = sizeof(BlkTransactionState), | |
1779 | .prepare = abort_prepare, | |
1780 | .commit = abort_commit, | |
1781 | }, | |
bbe86010 WX |
1782 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
1783 | .instance_size = sizeof(InternalSnapshotState), | |
1784 | .prepare = internal_snapshot_prepare, | |
1785 | .abort = internal_snapshot_abort, | |
5d6e96ef | 1786 | .clean = internal_snapshot_clean, |
bbe86010 | 1787 | }, |
ba0c86a3 WX |
1788 | }; |
1789 | ||
8802d1fd | 1790 | /* |
b756b9ce SH |
1791 | * 'Atomic' group operations. The operations are performed as a set, and if |
1792 | * any fail then we roll back all operations in the group. | |
8802d1fd | 1793 | */ |
c8a83e85 | 1794 | void qmp_transaction(TransactionActionList *dev_list, Error **errp) |
8802d1fd | 1795 | { |
c8a83e85 | 1796 | TransactionActionList *dev_entry = dev_list; |
ba5d6ab6 | 1797 | BlkTransactionState *state, *next; |
43e17041 | 1798 | Error *local_err = NULL; |
8802d1fd | 1799 | |
ba5d6ab6 | 1800 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; |
8802d1fd JC |
1801 | QSIMPLEQ_INIT(&snap_bdrv_states); |
1802 | ||
b756b9ce | 1803 | /* drain all i/o before any operations */ |
8802d1fd JC |
1804 | bdrv_drain_all(); |
1805 | ||
b756b9ce | 1806 | /* We don't do anything in this loop that commits us to the operations */ |
8802d1fd | 1807 | while (NULL != dev_entry) { |
c8a83e85 | 1808 | TransactionAction *dev_info = NULL; |
ba0c86a3 | 1809 | const BdrvActionOps *ops; |
52e7c241 | 1810 | |
8802d1fd JC |
1811 | dev_info = dev_entry->value; |
1812 | dev_entry = dev_entry->next; | |
1813 | ||
ba0c86a3 WX |
1814 | assert(dev_info->kind < ARRAY_SIZE(actions)); |
1815 | ||
1816 | ops = &actions[dev_info->kind]; | |
aa3fe714 HR |
1817 | assert(ops->instance_size > 0); |
1818 | ||
ba5d6ab6 SH |
1819 | state = g_malloc0(ops->instance_size); |
1820 | state->ops = ops; | |
1821 | state->action = dev_info; | |
1822 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); | |
8802d1fd | 1823 | |
ba5d6ab6 | 1824 | state->ops->prepare(state, &local_err); |
84d18f06 | 1825 | if (local_err) { |
ba0c86a3 WX |
1826 | error_propagate(errp, local_err); |
1827 | goto delete_and_fail; | |
8802d1fd | 1828 | } |
8802d1fd JC |
1829 | } |
1830 | ||
ba5d6ab6 | 1831 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
1832 | if (state->ops->commit) { |
1833 | state->ops->commit(state); | |
1834 | } | |
8802d1fd JC |
1835 | } |
1836 | ||
1837 | /* success */ | |
1838 | goto exit; | |
1839 | ||
1840 | delete_and_fail: | |
b756b9ce | 1841 | /* failure, and it is all-or-none; roll back all operations */ |
ba5d6ab6 SH |
1842 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
1843 | if (state->ops->abort) { | |
1844 | state->ops->abort(state); | |
ba0c86a3 | 1845 | } |
8802d1fd JC |
1846 | } |
1847 | exit: | |
ba5d6ab6 SH |
1848 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
1849 | if (state->ops->clean) { | |
1850 | state->ops->clean(state); | |
ba0c86a3 | 1851 | } |
ba5d6ab6 | 1852 | g_free(state); |
8802d1fd | 1853 | } |
8802d1fd JC |
1854 | } |
1855 | ||
1856 | ||
6007cdd4 | 1857 | static void eject_device(BlockBackend *blk, int force, Error **errp) |
666daa68 | 1858 | { |
6007cdd4 | 1859 | BlockDriverState *bs = blk_bs(blk); |
e3442099 SH |
1860 | AioContext *aio_context; |
1861 | ||
5433c24f HR |
1862 | if (!bs) { |
1863 | /* No medium inserted, so there is nothing to do */ | |
1864 | return; | |
1865 | } | |
1866 | ||
e3442099 SH |
1867 | aio_context = bdrv_get_aio_context(bs); |
1868 | aio_context_acquire(aio_context); | |
6007cdd4 | 1869 | |
3718d8ab | 1870 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { |
e3442099 | 1871 | goto out; |
2d3735d3 | 1872 | } |
a7f53e26 | 1873 | if (!blk_dev_has_removable_media(blk)) { |
f231b88d CR |
1874 | error_setg(errp, "Device '%s' is not removable", |
1875 | bdrv_get_device_name(bs)); | |
e3442099 | 1876 | goto out; |
ea8f942f | 1877 | } |
92d48558 | 1878 | |
a7f53e26 MA |
1879 | if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) { |
1880 | blk_dev_eject_request(blk, force); | |
025ccaa7 | 1881 | if (!force) { |
f231b88d CR |
1882 | error_setg(errp, "Device '%s' is locked", |
1883 | bdrv_get_device_name(bs)); | |
e3442099 | 1884 | goto out; |
025ccaa7 | 1885 | } |
666daa68 | 1886 | } |
92d48558 | 1887 | |
3b5276b5 | 1888 | bdrv_close(bs); |
e3442099 SH |
1889 | |
1890 | out: | |
1891 | aio_context_release(aio_context); | |
666daa68 MA |
1892 | } |
1893 | ||
c245b6a3 | 1894 | void qmp_eject(const char *device, bool has_force, bool force, Error **errp) |
666daa68 | 1895 | { |
6007cdd4 | 1896 | BlockBackend *blk; |
666daa68 | 1897 | |
6007cdd4 MA |
1898 | blk = blk_by_name(device); |
1899 | if (!blk) { | |
75158ebb MA |
1900 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1901 | "Device '%s' not found", device); | |
c245b6a3 | 1902 | return; |
92d48558 LC |
1903 | } |
1904 | ||
6007cdd4 | 1905 | eject_device(blk, force, errp); |
666daa68 MA |
1906 | } |
1907 | ||
12d3ba82 BC |
1908 | void qmp_block_passwd(bool has_device, const char *device, |
1909 | bool has_node_name, const char *node_name, | |
1910 | const char *password, Error **errp) | |
666daa68 | 1911 | { |
12d3ba82 | 1912 | Error *local_err = NULL; |
666daa68 | 1913 | BlockDriverState *bs; |
e3442099 | 1914 | AioContext *aio_context; |
666daa68 | 1915 | |
12d3ba82 BC |
1916 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
1917 | has_node_name ? node_name : NULL, | |
1918 | &local_err); | |
84d18f06 | 1919 | if (local_err) { |
12d3ba82 | 1920 | error_propagate(errp, local_err); |
a4dea8a9 | 1921 | return; |
666daa68 MA |
1922 | } |
1923 | ||
e3442099 SH |
1924 | aio_context = bdrv_get_aio_context(bs); |
1925 | aio_context_acquire(aio_context); | |
1926 | ||
4d2855a3 | 1927 | bdrv_add_key(bs, password, errp); |
e3442099 | 1928 | |
e3442099 | 1929 | aio_context_release(aio_context); |
666daa68 MA |
1930 | } |
1931 | ||
e3442099 | 1932 | /* Assumes AioContext is held */ |
5433c24f HR |
1933 | static void qmp_bdrv_open_encrypted(BlockDriverState **pbs, |
1934 | const char *filename, | |
e6641719 | 1935 | int bdrv_flags, const char *format, |
333a96ec LC |
1936 | const char *password, Error **errp) |
1937 | { | |
34b5d2c6 | 1938 | Error *local_err = NULL; |
e6641719 | 1939 | QDict *options = NULL; |
0eef407c LC |
1940 | int ret; |
1941 | ||
e6641719 HR |
1942 | if (format) { |
1943 | options = qdict_new(); | |
1944 | qdict_put(options, "driver", qstring_from_str(format)); | |
1945 | } | |
1946 | ||
5433c24f | 1947 | ret = bdrv_open(pbs, filename, NULL, options, bdrv_flags, &local_err); |
0eef407c | 1948 | if (ret < 0) { |
34b5d2c6 | 1949 | error_propagate(errp, local_err); |
333a96ec LC |
1950 | return; |
1951 | } | |
1952 | ||
5433c24f | 1953 | bdrv_add_key(*pbs, password, errp); |
333a96ec LC |
1954 | } |
1955 | ||
1956 | void qmp_change_blockdev(const char *device, const char *filename, | |
314f7ea7 | 1957 | const char *format, Error **errp) |
666daa68 | 1958 | { |
6007cdd4 | 1959 | BlockBackend *blk; |
666daa68 | 1960 | BlockDriverState *bs; |
e3442099 | 1961 | AioContext *aio_context; |
666daa68 | 1962 | int bdrv_flags; |
5433c24f | 1963 | bool new_bs; |
92d48558 | 1964 | Error *err = NULL; |
666daa68 | 1965 | |
6007cdd4 MA |
1966 | blk = blk_by_name(device); |
1967 | if (!blk) { | |
75158ebb MA |
1968 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1969 | "Device '%s' not found", device); | |
333a96ec | 1970 | return; |
666daa68 | 1971 | } |
6007cdd4 | 1972 | bs = blk_bs(blk); |
5433c24f | 1973 | new_bs = !bs; |
333a96ec | 1974 | |
5433c24f | 1975 | aio_context = blk_get_aio_context(blk); |
e3442099 SH |
1976 | aio_context_acquire(aio_context); |
1977 | ||
6007cdd4 | 1978 | eject_device(blk, 0, &err); |
84d18f06 | 1979 | if (err) { |
333a96ec | 1980 | error_propagate(errp, err); |
e3442099 | 1981 | goto out; |
666daa68 | 1982 | } |
333a96ec | 1983 | |
5433c24f HR |
1984 | bdrv_flags = blk_is_read_only(blk) ? 0 : BDRV_O_RDWR; |
1985 | bdrv_flags |= blk_get_root_state(blk)->open_flags & ~BDRV_O_RDWR; | |
1986 | ||
1987 | qmp_bdrv_open_encrypted(&bs, filename, bdrv_flags, format, NULL, &err); | |
1988 | if (err) { | |
1989 | error_propagate(errp, err); | |
1990 | goto out; | |
1991 | } | |
333a96ec | 1992 | |
5433c24f HR |
1993 | if (new_bs) { |
1994 | blk_insert_bs(blk, bs); | |
1995 | /* Has been sent automatically by bdrv_open() if blk_bs(blk) was not | |
1996 | * NULL */ | |
1997 | blk_dev_change_media_cb(blk, true); | |
1998 | } | |
e3442099 SH |
1999 | |
2000 | out: | |
2001 | aio_context_release(aio_context); | |
666daa68 | 2002 | } |
9063f814 | 2003 | |
727f005e | 2004 | /* throttling disk I/O limits */ |
80047da5 | 2005 | void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, |
3e9fab69 BC |
2006 | int64_t bps_wr, |
2007 | int64_t iops, | |
2008 | int64_t iops_rd, | |
2009 | int64_t iops_wr, | |
2010 | bool has_bps_max, | |
2011 | int64_t bps_max, | |
2012 | bool has_bps_rd_max, | |
2013 | int64_t bps_rd_max, | |
2014 | bool has_bps_wr_max, | |
2015 | int64_t bps_wr_max, | |
2016 | bool has_iops_max, | |
2017 | int64_t iops_max, | |
2018 | bool has_iops_rd_max, | |
2019 | int64_t iops_rd_max, | |
2020 | bool has_iops_wr_max, | |
2024c1df BC |
2021 | int64_t iops_wr_max, |
2022 | bool has_iops_size, | |
76f4afb4 AG |
2023 | int64_t iops_size, |
2024 | bool has_group, | |
2025 | const char *group, Error **errp) | |
727f005e | 2026 | { |
cc0681c4 | 2027 | ThrottleConfig cfg; |
727f005e | 2028 | BlockDriverState *bs; |
a0e8544c | 2029 | BlockBackend *blk; |
b15446fd | 2030 | AioContext *aio_context; |
727f005e | 2031 | |
a0e8544c FZ |
2032 | blk = blk_by_name(device); |
2033 | if (!blk) { | |
75158ebb MA |
2034 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2035 | "Device '%s' not found", device); | |
80047da5 | 2036 | return; |
727f005e | 2037 | } |
5433c24f HR |
2038 | |
2039 | aio_context = blk_get_aio_context(blk); | |
2040 | aio_context_acquire(aio_context); | |
2041 | ||
a0e8544c | 2042 | bs = blk_bs(blk); |
5433c24f HR |
2043 | if (!bs) { |
2044 | error_setg(errp, "Device '%s' has no medium", device); | |
2045 | goto out; | |
2046 | } | |
727f005e | 2047 | |
cc0681c4 BC |
2048 | memset(&cfg, 0, sizeof(cfg)); |
2049 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; | |
2050 | cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; | |
2051 | cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; | |
2052 | ||
2053 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; | |
2054 | cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; | |
2055 | cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; | |
2056 | ||
3e9fab69 BC |
2057 | if (has_bps_max) { |
2058 | cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; | |
2059 | } | |
2060 | if (has_bps_rd_max) { | |
2061 | cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; | |
2062 | } | |
2063 | if (has_bps_wr_max) { | |
2064 | cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; | |
2065 | } | |
2066 | if (has_iops_max) { | |
2067 | cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; | |
2068 | } | |
2069 | if (has_iops_rd_max) { | |
2070 | cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; | |
2071 | } | |
2072 | if (has_iops_wr_max) { | |
2073 | cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; | |
2074 | } | |
727f005e | 2075 | |
2024c1df BC |
2076 | if (has_iops_size) { |
2077 | cfg.op_size = iops_size; | |
2078 | } | |
cc0681c4 BC |
2079 | |
2080 | if (!check_throttle_config(&cfg, errp)) { | |
5433c24f | 2081 | goto out; |
727f005e ZYW |
2082 | } |
2083 | ||
76f4afb4 AG |
2084 | if (throttle_enabled(&cfg)) { |
2085 | /* Enable I/O limits if they're not enabled yet, otherwise | |
2086 | * just update the throttling group. */ | |
2087 | if (!bs->io_limits_enabled) { | |
2088 | bdrv_io_limits_enable(bs, has_group ? group : device); | |
2089 | } else if (has_group) { | |
2090 | bdrv_io_limits_update_group(bs, group); | |
2091 | } | |
2092 | /* Set the new throttling configuration */ | |
cc0681c4 | 2093 | bdrv_set_io_limits(bs, &cfg); |
76f4afb4 AG |
2094 | } else if (bs->io_limits_enabled) { |
2095 | /* If all throttling settings are set to 0, disable I/O limits */ | |
2096 | bdrv_io_limits_disable(bs); | |
727f005e | 2097 | } |
b15446fd | 2098 | |
5433c24f | 2099 | out: |
b15446fd | 2100 | aio_context_release(aio_context); |
727f005e ZYW |
2101 | } |
2102 | ||
341ebc2f JS |
2103 | void qmp_block_dirty_bitmap_add(const char *node, const char *name, |
2104 | bool has_granularity, uint32_t granularity, | |
2105 | Error **errp) | |
2106 | { | |
2107 | AioContext *aio_context; | |
2108 | BlockDriverState *bs; | |
2109 | ||
2110 | if (!name || name[0] == '\0') { | |
2111 | error_setg(errp, "Bitmap name cannot be empty"); | |
2112 | return; | |
2113 | } | |
2114 | ||
2115 | bs = bdrv_lookup_bs(node, node, errp); | |
2116 | if (!bs) { | |
2117 | return; | |
2118 | } | |
2119 | ||
2120 | aio_context = bdrv_get_aio_context(bs); | |
2121 | aio_context_acquire(aio_context); | |
2122 | ||
2123 | if (has_granularity) { | |
2124 | if (granularity < 512 || !is_power_of_2(granularity)) { | |
2125 | error_setg(errp, "Granularity must be power of 2 " | |
2126 | "and at least 512"); | |
2127 | goto out; | |
2128 | } | |
2129 | } else { | |
2130 | /* Default to cluster size, if available: */ | |
2131 | granularity = bdrv_get_default_bitmap_granularity(bs); | |
2132 | } | |
2133 | ||
2134 | bdrv_create_dirty_bitmap(bs, granularity, name, errp); | |
2135 | ||
2136 | out: | |
2137 | aio_context_release(aio_context); | |
2138 | } | |
2139 | ||
2140 | void qmp_block_dirty_bitmap_remove(const char *node, const char *name, | |
2141 | Error **errp) | |
2142 | { | |
2143 | AioContext *aio_context; | |
2144 | BlockDriverState *bs; | |
2145 | BdrvDirtyBitmap *bitmap; | |
2146 | ||
2147 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); | |
2148 | if (!bitmap || !bs) { | |
2149 | return; | |
2150 | } | |
2151 | ||
9bd2b08f JS |
2152 | if (bdrv_dirty_bitmap_frozen(bitmap)) { |
2153 | error_setg(errp, | |
2154 | "Bitmap '%s' is currently frozen and cannot be removed", | |
2155 | name); | |
2156 | goto out; | |
2157 | } | |
20dca810 | 2158 | bdrv_dirty_bitmap_make_anon(bitmap); |
341ebc2f JS |
2159 | bdrv_release_dirty_bitmap(bs, bitmap); |
2160 | ||
9bd2b08f | 2161 | out: |
341ebc2f JS |
2162 | aio_context_release(aio_context); |
2163 | } | |
2164 | ||
e74e6b78 JS |
2165 | /** |
2166 | * Completely clear a bitmap, for the purposes of synchronizing a bitmap | |
2167 | * immediately after a full backup operation. | |
2168 | */ | |
2169 | void qmp_block_dirty_bitmap_clear(const char *node, const char *name, | |
2170 | Error **errp) | |
2171 | { | |
2172 | AioContext *aio_context; | |
2173 | BdrvDirtyBitmap *bitmap; | |
2174 | BlockDriverState *bs; | |
2175 | ||
2176 | bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); | |
2177 | if (!bitmap || !bs) { | |
2178 | return; | |
2179 | } | |
2180 | ||
2181 | if (bdrv_dirty_bitmap_frozen(bitmap)) { | |
2182 | error_setg(errp, | |
2183 | "Bitmap '%s' is currently frozen and cannot be modified", | |
2184 | name); | |
2185 | goto out; | |
2186 | } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { | |
2187 | error_setg(errp, | |
2188 | "Bitmap '%s' is currently disabled and cannot be cleared", | |
2189 | name); | |
2190 | goto out; | |
2191 | } | |
2192 | ||
2193 | bdrv_clear_dirty_bitmap(bitmap); | |
2194 | ||
2195 | out: | |
2196 | aio_context_release(aio_context); | |
2197 | } | |
2198 | ||
072ebe6b | 2199 | void hmp_drive_del(Monitor *mon, const QDict *qdict) |
9063f814 RH |
2200 | { |
2201 | const char *id = qdict_get_str(qdict, "id"); | |
7e7d56d9 | 2202 | BlockBackend *blk; |
9063f814 | 2203 | BlockDriverState *bs; |
8ad4202b | 2204 | AioContext *aio_context; |
3718d8ab | 2205 | Error *local_err = NULL; |
9063f814 | 2206 | |
7e7d56d9 MA |
2207 | blk = blk_by_name(id); |
2208 | if (!blk) { | |
b1422f20 | 2209 | error_report("Device '%s' not found", id); |
072ebe6b | 2210 | return; |
9063f814 | 2211 | } |
8ad4202b | 2212 | |
26f8b3a8 | 2213 | if (!blk_legacy_dinfo(blk)) { |
48f364dd MA |
2214 | error_report("Deleting device added with blockdev-add" |
2215 | " is not supported"); | |
072ebe6b | 2216 | return; |
48f364dd MA |
2217 | } |
2218 | ||
5433c24f | 2219 | aio_context = blk_get_aio_context(blk); |
8ad4202b SH |
2220 | aio_context_acquire(aio_context); |
2221 | ||
5433c24f HR |
2222 | bs = blk_bs(blk); |
2223 | if (bs) { | |
2224 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { | |
2225 | error_report_err(local_err); | |
2226 | aio_context_release(aio_context); | |
2227 | return; | |
2228 | } | |
9063f814 | 2229 | |
5433c24f HR |
2230 | bdrv_close(bs); |
2231 | } | |
9063f814 | 2232 | |
fa879d62 | 2233 | /* if we have a device attached to this BlockDriverState |
d22b2f41 RH |
2234 | * then we need to make the drive anonymous until the device |
2235 | * can be removed. If this is a drive with no device backing | |
2236 | * then we can just get rid of the block driver state right here. | |
2237 | */ | |
a7f53e26 | 2238 | if (blk_get_attached_dev(blk)) { |
3e5a50d6 | 2239 | blk_hide_on_behalf_of_hmp_drive_del(blk); |
293c51a6 | 2240 | /* Further I/O must not pause the guest */ |
373340b2 HR |
2241 | blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, |
2242 | BLOCKDEV_ON_ERROR_REPORT); | |
d22b2f41 | 2243 | } else { |
b9fe8a7a | 2244 | blk_unref(blk); |
9063f814 RH |
2245 | } |
2246 | ||
8ad4202b | 2247 | aio_context_release(aio_context); |
9063f814 | 2248 | } |
6d4a2b3a | 2249 | |
3b1dbd11 BC |
2250 | void qmp_block_resize(bool has_device, const char *device, |
2251 | bool has_node_name, const char *node_name, | |
2252 | int64_t size, Error **errp) | |
6d4a2b3a | 2253 | { |
3b1dbd11 | 2254 | Error *local_err = NULL; |
6d4a2b3a | 2255 | BlockDriverState *bs; |
927e0e76 | 2256 | AioContext *aio_context; |
8732901e | 2257 | int ret; |
6d4a2b3a | 2258 | |
3b1dbd11 BC |
2259 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
2260 | has_node_name ? node_name : NULL, | |
2261 | &local_err); | |
84d18f06 | 2262 | if (local_err) { |
3b1dbd11 BC |
2263 | error_propagate(errp, local_err); |
2264 | return; | |
2265 | } | |
2266 | ||
927e0e76 SH |
2267 | aio_context = bdrv_get_aio_context(bs); |
2268 | aio_context_acquire(aio_context); | |
2269 | ||
3b1dbd11 | 2270 | if (!bdrv_is_first_non_filter(bs)) { |
c6bd8c70 | 2271 | error_setg(errp, QERR_FEATURE_DISABLED, "resize"); |
927e0e76 | 2272 | goto out; |
6d4a2b3a CH |
2273 | } |
2274 | ||
2275 | if (size < 0) { | |
c6bd8c70 | 2276 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
927e0e76 | 2277 | goto out; |
6d4a2b3a CH |
2278 | } |
2279 | ||
9c75e168 | 2280 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
c6bd8c70 | 2281 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
927e0e76 | 2282 | goto out; |
9c75e168 JC |
2283 | } |
2284 | ||
92b7a08d PL |
2285 | /* complete all in-flight operations before resizing the device */ |
2286 | bdrv_drain_all(); | |
2287 | ||
8732901e KW |
2288 | ret = bdrv_truncate(bs, size); |
2289 | switch (ret) { | |
939a1cc3 SH |
2290 | case 0: |
2291 | break; | |
2292 | case -ENOMEDIUM: | |
c6bd8c70 | 2293 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
939a1cc3 SH |
2294 | break; |
2295 | case -ENOTSUP: | |
c6bd8c70 | 2296 | error_setg(errp, QERR_UNSUPPORTED); |
939a1cc3 SH |
2297 | break; |
2298 | case -EACCES: | |
81e5f78a | 2299 | error_setg(errp, "Device '%s' is read only", device); |
939a1cc3 SH |
2300 | break; |
2301 | case -EBUSY: | |
c6bd8c70 | 2302 | error_setg(errp, QERR_DEVICE_IN_USE, device); |
939a1cc3 SH |
2303 | break; |
2304 | default: | |
8732901e | 2305 | error_setg_errno(errp, -ret, "Could not resize"); |
939a1cc3 | 2306 | break; |
6d4a2b3a | 2307 | } |
927e0e76 SH |
2308 | |
2309 | out: | |
2310 | aio_context_release(aio_context); | |
6d4a2b3a | 2311 | } |
12bd451f | 2312 | |
9abf2dba | 2313 | static void block_job_cb(void *opaque, int ret) |
12bd451f | 2314 | { |
723c5d93 SH |
2315 | /* Note that this function may be executed from another AioContext besides |
2316 | * the QEMU main loop. If you need to access anything that assumes the | |
2317 | * QEMU global mutex, use a BH or introduce a mutex. | |
2318 | */ | |
2319 | ||
12bd451f | 2320 | BlockDriverState *bs = opaque; |
bcada37b | 2321 | const char *msg = NULL; |
12bd451f | 2322 | |
9abf2dba | 2323 | trace_block_job_cb(bs, bs->job, ret); |
12bd451f SH |
2324 | |
2325 | assert(bs->job); | |
bcada37b | 2326 | |
12bd451f | 2327 | if (ret < 0) { |
bcada37b | 2328 | msg = strerror(-ret); |
12bd451f SH |
2329 | } |
2330 | ||
370521a1 | 2331 | if (block_job_is_cancelled(bs->job)) { |
bcada37b | 2332 | block_job_event_cancelled(bs->job); |
370521a1 | 2333 | } else { |
bcada37b | 2334 | block_job_event_completed(bs->job, msg); |
370521a1 | 2335 | } |
aa398a5c | 2336 | |
fa510ebf | 2337 | bdrv_put_ref_bh_schedule(bs); |
12bd451f SH |
2338 | } |
2339 | ||
13d8cc51 JC |
2340 | void qmp_block_stream(const char *device, |
2341 | bool has_base, const char *base, | |
2342 | bool has_backing_file, const char *backing_file, | |
2343 | bool has_speed, int64_t speed, | |
1d809098 PB |
2344 | bool has_on_error, BlockdevOnError on_error, |
2345 | Error **errp) | |
12bd451f | 2346 | { |
a0e8544c | 2347 | BlockBackend *blk; |
12bd451f | 2348 | BlockDriverState *bs; |
c8c3080f | 2349 | BlockDriverState *base_bs = NULL; |
f3e69beb | 2350 | AioContext *aio_context; |
fd7f8c65 | 2351 | Error *local_err = NULL; |
13d8cc51 | 2352 | const char *base_name = NULL; |
12bd451f | 2353 | |
1d809098 PB |
2354 | if (!has_on_error) { |
2355 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
2356 | } | |
2357 | ||
a0e8544c FZ |
2358 | blk = blk_by_name(device); |
2359 | if (!blk) { | |
75158ebb MA |
2360 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2361 | "Device '%s' not found", device); | |
12bd451f SH |
2362 | return; |
2363 | } | |
2364 | ||
5433c24f | 2365 | aio_context = blk_get_aio_context(blk); |
f3e69beb SH |
2366 | aio_context_acquire(aio_context); |
2367 | ||
5433c24f HR |
2368 | if (!blk_is_available(blk)) { |
2369 | error_setg(errp, "Device '%s' has no medium", device); | |
2370 | goto out; | |
2371 | } | |
2372 | bs = blk_bs(blk); | |
2373 | ||
628ff683 | 2374 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { |
f3e69beb | 2375 | goto out; |
628ff683 FZ |
2376 | } |
2377 | ||
13d8cc51 | 2378 | if (has_base) { |
c8c3080f MT |
2379 | base_bs = bdrv_find_backing_image(bs, base); |
2380 | if (base_bs == NULL) { | |
c6bd8c70 | 2381 | error_setg(errp, QERR_BASE_NOT_FOUND, base); |
f3e69beb | 2382 | goto out; |
c8c3080f | 2383 | } |
f3e69beb | 2384 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
13d8cc51 | 2385 | base_name = base; |
12bd451f SH |
2386 | } |
2387 | ||
13d8cc51 JC |
2388 | /* if we are streaming the entire chain, the result will have no backing |
2389 | * file, and specifying one is therefore an error */ | |
2390 | if (base_bs == NULL && has_backing_file) { | |
2391 | error_setg(errp, "backing file specified, but streaming the " | |
2392 | "entire chain"); | |
f3e69beb | 2393 | goto out; |
13d8cc51 JC |
2394 | } |
2395 | ||
2396 | /* backing_file string overrides base bs filename */ | |
2397 | base_name = has_backing_file ? backing_file : base_name; | |
2398 | ||
2399 | stream_start(bs, base_bs, base_name, has_speed ? speed : 0, | |
1d809098 | 2400 | on_error, block_job_cb, bs, &local_err); |
84d18f06 | 2401 | if (local_err) { |
fd7f8c65 | 2402 | error_propagate(errp, local_err); |
f3e69beb | 2403 | goto out; |
12bd451f SH |
2404 | } |
2405 | ||
2406 | trace_qmp_block_stream(bs, bs->job); | |
f3e69beb SH |
2407 | |
2408 | out: | |
2409 | aio_context_release(aio_context); | |
12bd451f | 2410 | } |
2d47c6e9 | 2411 | |
ed61fc10 | 2412 | void qmp_block_commit(const char *device, |
7676e2c5 JC |
2413 | bool has_base, const char *base, |
2414 | bool has_top, const char *top, | |
54e26900 | 2415 | bool has_backing_file, const char *backing_file, |
ed61fc10 JC |
2416 | bool has_speed, int64_t speed, |
2417 | Error **errp) | |
2418 | { | |
a0e8544c | 2419 | BlockBackend *blk; |
ed61fc10 JC |
2420 | BlockDriverState *bs; |
2421 | BlockDriverState *base_bs, *top_bs; | |
9e85cd5c | 2422 | AioContext *aio_context; |
ed61fc10 JC |
2423 | Error *local_err = NULL; |
2424 | /* This will be part of the QMP command, if/when the | |
2425 | * BlockdevOnError change for blkmirror makes it in | |
2426 | */ | |
92aa5c6d | 2427 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
ed61fc10 | 2428 | |
54504663 HR |
2429 | if (!has_speed) { |
2430 | speed = 0; | |
2431 | } | |
2432 | ||
7676e2c5 JC |
2433 | /* Important Note: |
2434 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
2435 | * live commit feature versions; for this to work, we must make sure to | |
2436 | * perform the device lookup before any generic errors that may occur in a | |
2437 | * scenario in which all optional arguments are omitted. */ | |
a0e8544c FZ |
2438 | blk = blk_by_name(device); |
2439 | if (!blk) { | |
75158ebb MA |
2440 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2441 | "Device '%s' not found", device); | |
ed61fc10 | 2442 | return; |
628ff683 FZ |
2443 | } |
2444 | ||
5433c24f | 2445 | aio_context = blk_get_aio_context(blk); |
9e85cd5c SH |
2446 | aio_context_acquire(aio_context); |
2447 | ||
5433c24f HR |
2448 | if (!blk_is_available(blk)) { |
2449 | error_setg(errp, "Device '%s' has no medium", device); | |
2450 | goto out; | |
2451 | } | |
2452 | bs = blk_bs(blk); | |
2453 | ||
bb00021d | 2454 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { |
9e85cd5c | 2455 | goto out; |
ed61fc10 | 2456 | } |
ed61fc10 JC |
2457 | |
2458 | /* default top_bs is the active layer */ | |
2459 | top_bs = bs; | |
2460 | ||
7676e2c5 | 2461 | if (has_top && top) { |
ed61fc10 JC |
2462 | if (strcmp(bs->filename, top) != 0) { |
2463 | top_bs = bdrv_find_backing_image(bs, top); | |
2464 | } | |
2465 | } | |
2466 | ||
2467 | if (top_bs == NULL) { | |
2468 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
9e85cd5c | 2469 | goto out; |
ed61fc10 JC |
2470 | } |
2471 | ||
9e85cd5c SH |
2472 | assert(bdrv_get_aio_context(top_bs) == aio_context); |
2473 | ||
d5208c45 JC |
2474 | if (has_base && base) { |
2475 | base_bs = bdrv_find_backing_image(top_bs, base); | |
2476 | } else { | |
2477 | base_bs = bdrv_find_base(top_bs); | |
2478 | } | |
2479 | ||
2480 | if (base_bs == NULL) { | |
c6bd8c70 | 2481 | error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); |
9e85cd5c | 2482 | goto out; |
d5208c45 JC |
2483 | } |
2484 | ||
9e85cd5c SH |
2485 | assert(bdrv_get_aio_context(base_bs) == aio_context); |
2486 | ||
bb00021d FZ |
2487 | if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { |
2488 | goto out; | |
2489 | } | |
2490 | ||
7676e2c5 JC |
2491 | /* Do not allow attempts to commit an image into itself */ |
2492 | if (top_bs == base_bs) { | |
2493 | error_setg(errp, "cannot commit an image into itself"); | |
9e85cd5c | 2494 | goto out; |
7676e2c5 JC |
2495 | } |
2496 | ||
20a63d2c | 2497 | if (top_bs == bs) { |
54e26900 JC |
2498 | if (has_backing_file) { |
2499 | error_setg(errp, "'backing-file' specified," | |
2500 | " but 'top' is the active layer"); | |
9e85cd5c | 2501 | goto out; |
54e26900 | 2502 | } |
20a63d2c FZ |
2503 | commit_active_start(bs, base_bs, speed, on_error, block_job_cb, |
2504 | bs, &local_err); | |
2505 | } else { | |
2506 | commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, | |
54e26900 | 2507 | has_backing_file ? backing_file : NULL, &local_err); |
20a63d2c | 2508 | } |
ed61fc10 JC |
2509 | if (local_err != NULL) { |
2510 | error_propagate(errp, local_err); | |
9e85cd5c | 2511 | goto out; |
ed61fc10 | 2512 | } |
9e85cd5c SH |
2513 | |
2514 | out: | |
2515 | aio_context_release(aio_context); | |
ed61fc10 JC |
2516 | } |
2517 | ||
99a9addf SH |
2518 | void qmp_drive_backup(const char *device, const char *target, |
2519 | bool has_format, const char *format, | |
b53169ea | 2520 | enum MirrorSyncMode sync, |
99a9addf SH |
2521 | bool has_mode, enum NewImageMode mode, |
2522 | bool has_speed, int64_t speed, | |
d58d8453 | 2523 | bool has_bitmap, const char *bitmap, |
99a9addf SH |
2524 | bool has_on_source_error, BlockdevOnError on_source_error, |
2525 | bool has_on_target_error, BlockdevOnError on_target_error, | |
2526 | Error **errp) | |
2527 | { | |
a0e8544c | 2528 | BlockBackend *blk; |
99a9addf SH |
2529 | BlockDriverState *bs; |
2530 | BlockDriverState *target_bs; | |
fc5d3f84 | 2531 | BlockDriverState *source = NULL; |
d58d8453 | 2532 | BdrvDirtyBitmap *bmap = NULL; |
761731b1 | 2533 | AioContext *aio_context; |
e6641719 | 2534 | QDict *options = NULL; |
99a9addf SH |
2535 | Error *local_err = NULL; |
2536 | int flags; | |
2537 | int64_t size; | |
2538 | int ret; | |
2539 | ||
2540 | if (!has_speed) { | |
2541 | speed = 0; | |
2542 | } | |
2543 | if (!has_on_source_error) { | |
2544 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2545 | } | |
2546 | if (!has_on_target_error) { | |
2547 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2548 | } | |
2549 | if (!has_mode) { | |
2550 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
2551 | } | |
2552 | ||
a0e8544c FZ |
2553 | blk = blk_by_name(device); |
2554 | if (!blk) { | |
75158ebb MA |
2555 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2556 | "Device '%s' not found", device); | |
99a9addf SH |
2557 | return; |
2558 | } | |
2559 | ||
5433c24f | 2560 | aio_context = blk_get_aio_context(blk); |
761731b1 SH |
2561 | aio_context_acquire(aio_context); |
2562 | ||
c29c1dd3 FZ |
2563 | /* Although backup_run has this check too, we need to use bs->drv below, so |
2564 | * do an early check redundantly. */ | |
5433c24f | 2565 | if (!blk_is_available(blk)) { |
c6bd8c70 | 2566 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
761731b1 | 2567 | goto out; |
99a9addf | 2568 | } |
5433c24f | 2569 | bs = blk_bs(blk); |
99a9addf SH |
2570 | |
2571 | if (!has_format) { | |
2572 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
2573 | } | |
99a9addf | 2574 | |
c29c1dd3 | 2575 | /* Early check to avoid creating target */ |
3718d8ab | 2576 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
761731b1 | 2577 | goto out; |
99a9addf SH |
2578 | } |
2579 | ||
2580 | flags = bs->open_flags | BDRV_O_RDWR; | |
2581 | ||
fc5d3f84 IM |
2582 | /* See if we have a backing HD we can use to create our new image |
2583 | * on top of. */ | |
2584 | if (sync == MIRROR_SYNC_MODE_TOP) { | |
760e0063 | 2585 | source = backing_bs(bs); |
fc5d3f84 IM |
2586 | if (!source) { |
2587 | sync = MIRROR_SYNC_MODE_FULL; | |
2588 | } | |
2589 | } | |
2590 | if (sync == MIRROR_SYNC_MODE_NONE) { | |
2591 | source = bs; | |
2592 | } | |
2593 | ||
99a9addf SH |
2594 | size = bdrv_getlength(bs); |
2595 | if (size < 0) { | |
2596 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
761731b1 | 2597 | goto out; |
99a9addf SH |
2598 | } |
2599 | ||
2600 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
e6641719 | 2601 | assert(format); |
fc5d3f84 IM |
2602 | if (source) { |
2603 | bdrv_img_create(target, format, source->filename, | |
2604 | source->drv->format_name, NULL, | |
2605 | size, flags, &local_err, false); | |
2606 | } else { | |
2607 | bdrv_img_create(target, format, NULL, NULL, NULL, | |
2608 | size, flags, &local_err, false); | |
2609 | } | |
99a9addf SH |
2610 | } |
2611 | ||
84d18f06 | 2612 | if (local_err) { |
99a9addf | 2613 | error_propagate(errp, local_err); |
761731b1 | 2614 | goto out; |
99a9addf SH |
2615 | } |
2616 | ||
e6641719 HR |
2617 | if (format) { |
2618 | options = qdict_new(); | |
2619 | qdict_put(options, "driver", qstring_from_str(format)); | |
2620 | } | |
2621 | ||
f67503e5 | 2622 | target_bs = NULL; |
6ebf9aa2 | 2623 | ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); |
99a9addf | 2624 | if (ret < 0) { |
34b5d2c6 | 2625 | error_propagate(errp, local_err); |
761731b1 | 2626 | goto out; |
99a9addf SH |
2627 | } |
2628 | ||
761731b1 SH |
2629 | bdrv_set_aio_context(target_bs, aio_context); |
2630 | ||
d58d8453 JS |
2631 | if (has_bitmap) { |
2632 | bmap = bdrv_find_dirty_bitmap(bs, bitmap); | |
2633 | if (!bmap) { | |
2634 | error_setg(errp, "Bitmap '%s' could not be found", bitmap); | |
2635 | goto out; | |
2636 | } | |
2637 | } | |
2638 | ||
2639 | backup_start(bs, target_bs, speed, sync, bmap, | |
2640 | on_source_error, on_target_error, | |
99a9addf SH |
2641 | block_job_cb, bs, &local_err); |
2642 | if (local_err != NULL) { | |
4f6fd349 | 2643 | bdrv_unref(target_bs); |
99a9addf | 2644 | error_propagate(errp, local_err); |
761731b1 | 2645 | goto out; |
99a9addf | 2646 | } |
761731b1 SH |
2647 | |
2648 | out: | |
2649 | aio_context_release(aio_context); | |
99a9addf SH |
2650 | } |
2651 | ||
c13163fb BC |
2652 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) |
2653 | { | |
d5a8ee60 | 2654 | return bdrv_named_nodes_list(errp); |
c13163fb BC |
2655 | } |
2656 | ||
c29c1dd3 FZ |
2657 | void qmp_blockdev_backup(const char *device, const char *target, |
2658 | enum MirrorSyncMode sync, | |
2659 | bool has_speed, int64_t speed, | |
2660 | bool has_on_source_error, | |
2661 | BlockdevOnError on_source_error, | |
2662 | bool has_on_target_error, | |
2663 | BlockdevOnError on_target_error, | |
2664 | Error **errp) | |
2665 | { | |
5433c24f | 2666 | BlockBackend *blk, *target_blk; |
c29c1dd3 FZ |
2667 | BlockDriverState *bs; |
2668 | BlockDriverState *target_bs; | |
2669 | Error *local_err = NULL; | |
2670 | AioContext *aio_context; | |
2671 | ||
2672 | if (!has_speed) { | |
2673 | speed = 0; | |
2674 | } | |
2675 | if (!has_on_source_error) { | |
2676 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2677 | } | |
2678 | if (!has_on_target_error) { | |
2679 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2680 | } | |
2681 | ||
a0e8544c FZ |
2682 | blk = blk_by_name(device); |
2683 | if (!blk) { | |
5b347c54 | 2684 | error_setg(errp, "Device '%s' not found", device); |
c29c1dd3 FZ |
2685 | return; |
2686 | } | |
2687 | ||
5433c24f | 2688 | aio_context = blk_get_aio_context(blk); |
c29c1dd3 FZ |
2689 | aio_context_acquire(aio_context); |
2690 | ||
5433c24f HR |
2691 | if (!blk_is_available(blk)) { |
2692 | error_setg(errp, "Device '%s' has no medium", device); | |
2693 | goto out; | |
2694 | } | |
2695 | bs = blk_bs(blk); | |
2696 | ||
2697 | target_blk = blk_by_name(target); | |
2698 | if (!target_blk) { | |
5b347c54 | 2699 | error_setg(errp, "Device '%s' not found", target); |
c29c1dd3 FZ |
2700 | goto out; |
2701 | } | |
5433c24f HR |
2702 | |
2703 | if (!blk_is_available(target_blk)) { | |
2704 | error_setg(errp, "Device '%s' has no medium", target); | |
2705 | goto out; | |
2706 | } | |
2707 | target_bs = blk_bs(target_blk); | |
c29c1dd3 FZ |
2708 | |
2709 | bdrv_ref(target_bs); | |
2710 | bdrv_set_aio_context(target_bs, aio_context); | |
d58d8453 JS |
2711 | backup_start(bs, target_bs, speed, sync, NULL, on_source_error, |
2712 | on_target_error, block_job_cb, bs, &local_err); | |
c29c1dd3 FZ |
2713 | if (local_err != NULL) { |
2714 | bdrv_unref(target_bs); | |
2715 | error_propagate(errp, local_err); | |
2716 | } | |
2717 | out: | |
2718 | aio_context_release(aio_context); | |
2719 | } | |
2720 | ||
d9b902db PB |
2721 | void qmp_drive_mirror(const char *device, const char *target, |
2722 | bool has_format, const char *format, | |
4c828dc6 | 2723 | bool has_node_name, const char *node_name, |
09158f00 | 2724 | bool has_replaces, const char *replaces, |
d9b902db PB |
2725 | enum MirrorSyncMode sync, |
2726 | bool has_mode, enum NewImageMode mode, | |
b952b558 | 2727 | bool has_speed, int64_t speed, |
eee13dfe | 2728 | bool has_granularity, uint32_t granularity, |
08e4ed6c | 2729 | bool has_buf_size, int64_t buf_size, |
b952b558 PB |
2730 | bool has_on_source_error, BlockdevOnError on_source_error, |
2731 | bool has_on_target_error, BlockdevOnError on_target_error, | |
0fc9f8ea | 2732 | bool has_unmap, bool unmap, |
b952b558 | 2733 | Error **errp) |
d9b902db | 2734 | { |
a0e8544c | 2735 | BlockBackend *blk; |
d9b902db PB |
2736 | BlockDriverState *bs; |
2737 | BlockDriverState *source, *target_bs; | |
5a7e7a0b | 2738 | AioContext *aio_context; |
d9b902db | 2739 | Error *local_err = NULL; |
e6641719 | 2740 | QDict *options; |
d9b902db | 2741 | int flags; |
ac3c5d83 | 2742 | int64_t size; |
d9b902db PB |
2743 | int ret; |
2744 | ||
2745 | if (!has_speed) { | |
2746 | speed = 0; | |
2747 | } | |
b952b558 PB |
2748 | if (!has_on_source_error) { |
2749 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2750 | } | |
2751 | if (!has_on_target_error) { | |
2752 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2753 | } | |
d9b902db PB |
2754 | if (!has_mode) { |
2755 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
2756 | } | |
eee13dfe PB |
2757 | if (!has_granularity) { |
2758 | granularity = 0; | |
2759 | } | |
08e4ed6c | 2760 | if (!has_buf_size) { |
48ac0a4d | 2761 | buf_size = 0; |
08e4ed6c | 2762 | } |
0fc9f8ea FZ |
2763 | if (!has_unmap) { |
2764 | unmap = true; | |
2765 | } | |
08e4ed6c | 2766 | |
eee13dfe | 2767 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
c6bd8c70 MA |
2768 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
2769 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
2770 | return; |
2771 | } | |
2772 | if (granularity & (granularity - 1)) { | |
c6bd8c70 MA |
2773 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
2774 | "power of 2"); | |
eee13dfe PB |
2775 | return; |
2776 | } | |
d9b902db | 2777 | |
a0e8544c FZ |
2778 | blk = blk_by_name(device); |
2779 | if (!blk) { | |
75158ebb MA |
2780 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
2781 | "Device '%s' not found", device); | |
d9b902db PB |
2782 | return; |
2783 | } | |
2784 | ||
5433c24f | 2785 | aio_context = blk_get_aio_context(blk); |
5a7e7a0b SH |
2786 | aio_context_acquire(aio_context); |
2787 | ||
5433c24f | 2788 | if (!blk_is_available(blk)) { |
c6bd8c70 | 2789 | error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
5a7e7a0b | 2790 | goto out; |
d9b902db | 2791 | } |
5433c24f | 2792 | bs = blk_bs(blk); |
d9b902db PB |
2793 | |
2794 | if (!has_format) { | |
2795 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
2796 | } | |
d9b902db | 2797 | |
3718d8ab | 2798 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { |
5a7e7a0b | 2799 | goto out; |
d9b902db PB |
2800 | } |
2801 | ||
2802 | flags = bs->open_flags | BDRV_O_RDWR; | |
760e0063 | 2803 | source = backing_bs(bs); |
d9b902db PB |
2804 | if (!source && sync == MIRROR_SYNC_MODE_TOP) { |
2805 | sync = MIRROR_SYNC_MODE_FULL; | |
2806 | } | |
117e0c82 HR |
2807 | if (sync == MIRROR_SYNC_MODE_NONE) { |
2808 | source = bs; | |
2809 | } | |
d9b902db | 2810 | |
ac3c5d83 SH |
2811 | size = bdrv_getlength(bs); |
2812 | if (size < 0) { | |
2813 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
5a7e7a0b | 2814 | goto out; |
ac3c5d83 SH |
2815 | } |
2816 | ||
09158f00 BC |
2817 | if (has_replaces) { |
2818 | BlockDriverState *to_replace_bs; | |
5a7e7a0b SH |
2819 | AioContext *replace_aio_context; |
2820 | int64_t replace_size; | |
09158f00 BC |
2821 | |
2822 | if (!has_node_name) { | |
2823 | error_setg(errp, "a node-name must be provided when replacing a" | |
2824 | " named node of the graph"); | |
5a7e7a0b | 2825 | goto out; |
09158f00 BC |
2826 | } |
2827 | ||
e12f3784 | 2828 | to_replace_bs = check_to_replace_node(bs, replaces, &local_err); |
09158f00 BC |
2829 | |
2830 | if (!to_replace_bs) { | |
2831 | error_propagate(errp, local_err); | |
5a7e7a0b | 2832 | goto out; |
09158f00 BC |
2833 | } |
2834 | ||
5a7e7a0b SH |
2835 | replace_aio_context = bdrv_get_aio_context(to_replace_bs); |
2836 | aio_context_acquire(replace_aio_context); | |
2837 | replace_size = bdrv_getlength(to_replace_bs); | |
2838 | aio_context_release(replace_aio_context); | |
2839 | ||
2840 | if (size != replace_size) { | |
09158f00 BC |
2841 | error_setg(errp, "cannot replace image with a mirror image of " |
2842 | "different size"); | |
5a7e7a0b | 2843 | goto out; |
09158f00 BC |
2844 | } |
2845 | } | |
2846 | ||
14526864 HR |
2847 | if ((sync == MIRROR_SYNC_MODE_FULL || !source) |
2848 | && mode != NEW_IMAGE_MODE_EXISTING) | |
2849 | { | |
d9b902db | 2850 | /* create new image w/o backing file */ |
e6641719 | 2851 | assert(format); |
cf8f2426 | 2852 | bdrv_img_create(target, format, |
f382d43a | 2853 | NULL, NULL, NULL, size, flags, &local_err, false); |
d9b902db PB |
2854 | } else { |
2855 | switch (mode) { | |
2856 | case NEW_IMAGE_MODE_EXISTING: | |
d9b902db PB |
2857 | break; |
2858 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
2859 | /* create new image with backing file */ | |
cf8f2426 LC |
2860 | bdrv_img_create(target, format, |
2861 | source->filename, | |
2862 | source->drv->format_name, | |
f382d43a | 2863 | NULL, size, flags, &local_err, false); |
d9b902db PB |
2864 | break; |
2865 | default: | |
2866 | abort(); | |
2867 | } | |
2868 | } | |
2869 | ||
84d18f06 | 2870 | if (local_err) { |
cf8f2426 | 2871 | error_propagate(errp, local_err); |
5a7e7a0b | 2872 | goto out; |
d9b902db PB |
2873 | } |
2874 | ||
e6641719 | 2875 | options = qdict_new(); |
4c828dc6 | 2876 | if (has_node_name) { |
4c828dc6 BC |
2877 | qdict_put(options, "node-name", qstring_from_str(node_name)); |
2878 | } | |
e6641719 HR |
2879 | if (format) { |
2880 | qdict_put(options, "driver", qstring_from_str(format)); | |
2881 | } | |
4c828dc6 | 2882 | |
b812f671 PB |
2883 | /* Mirroring takes care of copy-on-write using the source's backing |
2884 | * file. | |
2885 | */ | |
f67503e5 | 2886 | target_bs = NULL; |
4c828dc6 | 2887 | ret = bdrv_open(&target_bs, target, NULL, options, |
6ebf9aa2 | 2888 | flags | BDRV_O_NO_BACKING, &local_err); |
d9b902db | 2889 | if (ret < 0) { |
34b5d2c6 | 2890 | error_propagate(errp, local_err); |
5a7e7a0b | 2891 | goto out; |
d9b902db PB |
2892 | } |
2893 | ||
5a7e7a0b SH |
2894 | bdrv_set_aio_context(target_bs, aio_context); |
2895 | ||
09158f00 BC |
2896 | /* pass the node name to replace to mirror start since it's loose coupling |
2897 | * and will allow to check whether the node still exist at mirror completion | |
2898 | */ | |
2899 | mirror_start(bs, target_bs, | |
2900 | has_replaces ? replaces : NULL, | |
2901 | speed, granularity, buf_size, sync, | |
eee13dfe | 2902 | on_source_error, on_target_error, |
0fc9f8ea | 2903 | unmap, |
b952b558 | 2904 | block_job_cb, bs, &local_err); |
d9b902db | 2905 | if (local_err != NULL) { |
4f6fd349 | 2906 | bdrv_unref(target_bs); |
d9b902db | 2907 | error_propagate(errp, local_err); |
5a7e7a0b | 2908 | goto out; |
d9b902db | 2909 | } |
5a7e7a0b SH |
2910 | |
2911 | out: | |
2912 | aio_context_release(aio_context); | |
d9b902db PB |
2913 | } |
2914 | ||
3d948cdf | 2915 | /* Get the block job for a given device name and acquire its AioContext */ |
24d6bffe MA |
2916 | static BlockJob *find_block_job(const char *device, AioContext **aio_context, |
2917 | Error **errp) | |
2d47c6e9 | 2918 | { |
a0e8544c | 2919 | BlockBackend *blk; |
2d47c6e9 SH |
2920 | BlockDriverState *bs; |
2921 | ||
5433c24f HR |
2922 | *aio_context = NULL; |
2923 | ||
a0e8544c FZ |
2924 | blk = blk_by_name(device); |
2925 | if (!blk) { | |
3d948cdf SH |
2926 | goto notfound; |
2927 | } | |
2928 | ||
5433c24f | 2929 | *aio_context = blk_get_aio_context(blk); |
3d948cdf SH |
2930 | aio_context_acquire(*aio_context); |
2931 | ||
5433c24f HR |
2932 | if (!blk_is_available(blk)) { |
2933 | goto notfound; | |
2934 | } | |
2935 | bs = blk_bs(blk); | |
2936 | ||
3d948cdf | 2937 | if (!bs->job) { |
3d948cdf | 2938 | goto notfound; |
2d47c6e9 | 2939 | } |
3d948cdf | 2940 | |
2d47c6e9 | 2941 | return bs->job; |
3d948cdf SH |
2942 | |
2943 | notfound: | |
2e3a0266 MA |
2944 | error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, |
2945 | "No active block job on device '%s'", device); | |
5433c24f HR |
2946 | if (*aio_context) { |
2947 | aio_context_release(*aio_context); | |
2948 | *aio_context = NULL; | |
2949 | } | |
3d948cdf | 2950 | return NULL; |
2d47c6e9 SH |
2951 | } |
2952 | ||
882ec7ce | 2953 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 | 2954 | { |
3d948cdf | 2955 | AioContext *aio_context; |
24d6bffe | 2956 | BlockJob *job = find_block_job(device, &aio_context, errp); |
2d47c6e9 SH |
2957 | |
2958 | if (!job) { | |
2d47c6e9 SH |
2959 | return; |
2960 | } | |
2961 | ||
882ec7ce | 2962 | block_job_set_speed(job, speed, errp); |
3d948cdf | 2963 | aio_context_release(aio_context); |
2d47c6e9 | 2964 | } |
370521a1 | 2965 | |
6e37fb81 PB |
2966 | void qmp_block_job_cancel(const char *device, |
2967 | bool has_force, bool force, Error **errp) | |
370521a1 | 2968 | { |
3d948cdf | 2969 | AioContext *aio_context; |
24d6bffe | 2970 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 2971 | |
370521a1 | 2972 | if (!job) { |
370521a1 SH |
2973 | return; |
2974 | } | |
3d948cdf SH |
2975 | |
2976 | if (!has_force) { | |
2977 | force = false; | |
2978 | } | |
2979 | ||
751ebd76 | 2980 | if (job->user_paused && !force) { |
f231b88d CR |
2981 | error_setg(errp, "The block job for device '%s' is currently paused", |
2982 | device); | |
3d948cdf | 2983 | goto out; |
8acc72a4 | 2984 | } |
370521a1 SH |
2985 | |
2986 | trace_qmp_block_job_cancel(job); | |
2987 | block_job_cancel(job); | |
3d948cdf SH |
2988 | out: |
2989 | aio_context_release(aio_context); | |
370521a1 | 2990 | } |
fb5458cd | 2991 | |
6e37fb81 PB |
2992 | void qmp_block_job_pause(const char *device, Error **errp) |
2993 | { | |
3d948cdf | 2994 | AioContext *aio_context; |
24d6bffe | 2995 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 2996 | |
751ebd76 | 2997 | if (!job || job->user_paused) { |
6e37fb81 PB |
2998 | return; |
2999 | } | |
3000 | ||
751ebd76 | 3001 | job->user_paused = true; |
6e37fb81 PB |
3002 | trace_qmp_block_job_pause(job); |
3003 | block_job_pause(job); | |
3d948cdf | 3004 | aio_context_release(aio_context); |
6e37fb81 PB |
3005 | } |
3006 | ||
3007 | void qmp_block_job_resume(const char *device, Error **errp) | |
3008 | { | |
3d948cdf | 3009 | AioContext *aio_context; |
24d6bffe | 3010 | BlockJob *job = find_block_job(device, &aio_context, errp); |
6e37fb81 | 3011 | |
751ebd76 | 3012 | if (!job || !job->user_paused) { |
6e37fb81 PB |
3013 | return; |
3014 | } | |
3015 | ||
751ebd76 | 3016 | job->user_paused = false; |
6e37fb81 PB |
3017 | trace_qmp_block_job_resume(job); |
3018 | block_job_resume(job); | |
3d948cdf | 3019 | aio_context_release(aio_context); |
6e37fb81 PB |
3020 | } |
3021 | ||
aeae883b PB |
3022 | void qmp_block_job_complete(const char *device, Error **errp) |
3023 | { | |
3d948cdf | 3024 | AioContext *aio_context; |
24d6bffe | 3025 | BlockJob *job = find_block_job(device, &aio_context, errp); |
aeae883b PB |
3026 | |
3027 | if (!job) { | |
aeae883b PB |
3028 | return; |
3029 | } | |
3030 | ||
3031 | trace_qmp_block_job_complete(job); | |
3032 | block_job_complete(job, errp); | |
3d948cdf | 3033 | aio_context_release(aio_context); |
aeae883b PB |
3034 | } |
3035 | ||
fa40e656 JC |
3036 | void qmp_change_backing_file(const char *device, |
3037 | const char *image_node_name, | |
3038 | const char *backing_file, | |
3039 | Error **errp) | |
3040 | { | |
a0e8544c | 3041 | BlockBackend *blk; |
fa40e656 | 3042 | BlockDriverState *bs = NULL; |
729962f6 | 3043 | AioContext *aio_context; |
fa40e656 JC |
3044 | BlockDriverState *image_bs = NULL; |
3045 | Error *local_err = NULL; | |
3046 | bool ro; | |
3047 | int open_flags; | |
3048 | int ret; | |
3049 | ||
a0e8544c FZ |
3050 | blk = blk_by_name(device); |
3051 | if (!blk) { | |
75158ebb MA |
3052 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
3053 | "Device '%s' not found", device); | |
fa40e656 JC |
3054 | return; |
3055 | } | |
3056 | ||
5433c24f | 3057 | aio_context = blk_get_aio_context(blk); |
729962f6 SH |
3058 | aio_context_acquire(aio_context); |
3059 | ||
5433c24f HR |
3060 | if (!blk_is_available(blk)) { |
3061 | error_setg(errp, "Device '%s' has no medium", device); | |
3062 | goto out; | |
3063 | } | |
3064 | bs = blk_bs(blk); | |
3065 | ||
fa40e656 JC |
3066 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); |
3067 | if (local_err) { | |
3068 | error_propagate(errp, local_err); | |
729962f6 | 3069 | goto out; |
fa40e656 JC |
3070 | } |
3071 | ||
3072 | if (!image_bs) { | |
3073 | error_setg(errp, "image file not found"); | |
729962f6 | 3074 | goto out; |
fa40e656 JC |
3075 | } |
3076 | ||
3077 | if (bdrv_find_base(image_bs) == image_bs) { | |
3078 | error_setg(errp, "not allowing backing file change on an image " | |
3079 | "without a backing file"); | |
729962f6 | 3080 | goto out; |
fa40e656 JC |
3081 | } |
3082 | ||
3083 | /* even though we are not necessarily operating on bs, we need it to | |
3084 | * determine if block ops are currently prohibited on the chain */ | |
3085 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
729962f6 | 3086 | goto out; |
fa40e656 JC |
3087 | } |
3088 | ||
3089 | /* final sanity check */ | |
3090 | if (!bdrv_chain_contains(bs, image_bs)) { | |
3091 | error_setg(errp, "'%s' and image file are not in the same chain", | |
3092 | device); | |
729962f6 | 3093 | goto out; |
fa40e656 JC |
3094 | } |
3095 | ||
3096 | /* if not r/w, reopen to make r/w */ | |
3097 | open_flags = image_bs->open_flags; | |
3098 | ro = bdrv_is_read_only(image_bs); | |
3099 | ||
3100 | if (ro) { | |
3101 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); | |
3102 | if (local_err) { | |
3103 | error_propagate(errp, local_err); | |
729962f6 | 3104 | goto out; |
fa40e656 JC |
3105 | } |
3106 | } | |
3107 | ||
3108 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
3109 | image_bs->drv ? image_bs->drv->format_name : ""); | |
3110 | ||
3111 | if (ret < 0) { | |
3112 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
3113 | backing_file); | |
3114 | /* don't exit here, so we can try to restore open flags if | |
3115 | * appropriate */ | |
3116 | } | |
3117 | ||
3118 | if (ro) { | |
3119 | bdrv_reopen(image_bs, open_flags, &local_err); | |
3120 | if (local_err) { | |
3121 | error_propagate(errp, local_err); /* will preserve prior errp */ | |
3122 | } | |
3123 | } | |
729962f6 SH |
3124 | |
3125 | out: | |
3126 | aio_context_release(aio_context); | |
fa40e656 JC |
3127 | } |
3128 | ||
d26c9a15 KW |
3129 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
3130 | { | |
3131 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
be4b67bc HR |
3132 | BlockDriverState *bs; |
3133 | BlockBackend *blk = NULL; | |
d26c9a15 KW |
3134 | QObject *obj; |
3135 | QDict *qdict; | |
d26c9a15 KW |
3136 | Error *local_err = NULL; |
3137 | ||
60e19e06 | 3138 | /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with |
d26c9a15 | 3139 | * cache.direct=false instead of silently switching to aio=threads, except |
60e19e06 | 3140 | * when called from drive_new(). |
d26c9a15 KW |
3141 | * |
3142 | * For now, simply forbidding the combination for all drivers will do. */ | |
3143 | if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { | |
c6e0bd9b KW |
3144 | bool direct = options->has_cache && |
3145 | options->cache->has_direct && | |
3146 | options->cache->direct; | |
3147 | if (!direct) { | |
d26c9a15 KW |
3148 | error_setg(errp, "aio=native requires cache.direct=true"); |
3149 | goto fail; | |
3150 | } | |
3151 | } | |
3152 | ||
3153 | visit_type_BlockdevOptions(qmp_output_get_visitor(ov), | |
3154 | &options, NULL, &local_err); | |
84d18f06 | 3155 | if (local_err) { |
d26c9a15 KW |
3156 | error_propagate(errp, local_err); |
3157 | goto fail; | |
3158 | } | |
3159 | ||
3160 | obj = qmp_output_get_qobject(ov); | |
3161 | qdict = qobject_to_qdict(obj); | |
3162 | ||
3163 | qdict_flatten(qdict); | |
3164 | ||
be4b67bc HR |
3165 | if (options->has_id) { |
3166 | blk = blockdev_init(NULL, qdict, &local_err); | |
3167 | if (local_err) { | |
3168 | error_propagate(errp, local_err); | |
3169 | goto fail; | |
3170 | } | |
3171 | ||
3172 | bs = blk_bs(blk); | |
3173 | } else { | |
3174 | int ret; | |
3175 | ||
3176 | if (!qdict_get_try_str(qdict, "node-name")) { | |
3177 | error_setg(errp, "'id' and/or 'node-name' need to be specified for " | |
3178 | "the root node"); | |
3179 | goto fail; | |
3180 | } | |
3181 | ||
3182 | bs = NULL; | |
3183 | ret = bdrv_open(&bs, NULL, NULL, qdict, BDRV_O_RDWR | BDRV_O_CACHE_WB, | |
3184 | errp); | |
3185 | if (ret < 0) { | |
3186 | goto fail; | |
3187 | } | |
d26c9a15 KW |
3188 | } |
3189 | ||
be4b67bc HR |
3190 | if (bs && bdrv_key_required(bs)) { |
3191 | if (blk) { | |
3192 | blk_unref(blk); | |
3193 | } else { | |
3194 | bdrv_unref(bs); | |
3195 | } | |
8ae8e904 KW |
3196 | error_setg(errp, "blockdev-add doesn't support encrypted devices"); |
3197 | goto fail; | |
3198 | } | |
3199 | ||
d26c9a15 KW |
3200 | fail: |
3201 | qmp_output_visitor_cleanup(ov); | |
3202 | } | |
3203 | ||
fea68bb6 | 3204 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 3205 | { |
fea68bb6 MA |
3206 | BlockJobInfoList *head = NULL, **p_next = &head; |
3207 | BlockDriverState *bs; | |
fb5458cd | 3208 | |
fea68bb6 | 3209 | for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { |
69691e72 SH |
3210 | AioContext *aio_context = bdrv_get_aio_context(bs); |
3211 | ||
3212 | aio_context_acquire(aio_context); | |
3213 | ||
fea68bb6 MA |
3214 | if (bs->job) { |
3215 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); | |
3216 | elem->value = block_job_query(bs->job); | |
3217 | *p_next = elem; | |
3218 | p_next = &elem->next; | |
3219 | } | |
69691e72 SH |
3220 | |
3221 | aio_context_release(aio_context); | |
fb5458cd | 3222 | } |
fb5458cd | 3223 | |
fea68bb6 | 3224 | return head; |
fb5458cd | 3225 | } |
4d454574 | 3226 | |
0006383e | 3227 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 3228 | .name = "drive", |
0006383e | 3229 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
3230 | .desc = { |
3231 | { | |
4d454574 PB |
3232 | .name = "snapshot", |
3233 | .type = QEMU_OPT_BOOL, | |
3234 | .help = "enable/disable snapshot mode", | |
a9384aff PB |
3235 | },{ |
3236 | .name = "discard", | |
3237 | .type = QEMU_OPT_STRING, | |
3238 | .help = "discard operation (ignore/off, unmap/on)", | |
4d454574 | 3239 | },{ |
54861b92 | 3240 | .name = BDRV_OPT_CACHE_WB, |
29c4e2b5 KW |
3241 | .type = QEMU_OPT_BOOL, |
3242 | .help = "enables writeback mode for any caches", | |
3243 | },{ | |
54861b92 | 3244 | .name = BDRV_OPT_CACHE_DIRECT, |
29c4e2b5 KW |
3245 | .type = QEMU_OPT_BOOL, |
3246 | .help = "enables use of O_DIRECT (bypass the host page cache)", | |
3247 | },{ | |
54861b92 | 3248 | .name = BDRV_OPT_CACHE_NO_FLUSH, |
29c4e2b5 KW |
3249 | .type = QEMU_OPT_BOOL, |
3250 | .help = "ignore any flush requests for the device", | |
4d454574 PB |
3251 | },{ |
3252 | .name = "aio", | |
3253 | .type = QEMU_OPT_STRING, | |
3254 | .help = "host AIO implementation (threads, native)", | |
3255 | },{ | |
3256 | .name = "format", | |
3257 | .type = QEMU_OPT_STRING, | |
3258 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
3259 | },{ |
3260 | .name = "rerror", | |
3261 | .type = QEMU_OPT_STRING, | |
3262 | .help = "read error action", | |
3263 | },{ | |
3264 | .name = "werror", | |
3265 | .type = QEMU_OPT_STRING, | |
3266 | .help = "write error action", | |
4d454574 | 3267 | },{ |
0f227a94 | 3268 | .name = "read-only", |
4d454574 PB |
3269 | .type = QEMU_OPT_BOOL, |
3270 | .help = "open drive file as read-only", | |
3271 | },{ | |
57975222 | 3272 | .name = "throttling.iops-total", |
4d454574 PB |
3273 | .type = QEMU_OPT_NUMBER, |
3274 | .help = "limit total I/O operations per second", | |
3275 | },{ | |
57975222 | 3276 | .name = "throttling.iops-read", |
4d454574 PB |
3277 | .type = QEMU_OPT_NUMBER, |
3278 | .help = "limit read operations per second", | |
3279 | },{ | |
57975222 | 3280 | .name = "throttling.iops-write", |
4d454574 PB |
3281 | .type = QEMU_OPT_NUMBER, |
3282 | .help = "limit write operations per second", | |
3283 | },{ | |
57975222 | 3284 | .name = "throttling.bps-total", |
4d454574 PB |
3285 | .type = QEMU_OPT_NUMBER, |
3286 | .help = "limit total bytes per second", | |
3287 | },{ | |
57975222 | 3288 | .name = "throttling.bps-read", |
4d454574 PB |
3289 | .type = QEMU_OPT_NUMBER, |
3290 | .help = "limit read bytes per second", | |
3291 | },{ | |
57975222 | 3292 | .name = "throttling.bps-write", |
4d454574 PB |
3293 | .type = QEMU_OPT_NUMBER, |
3294 | .help = "limit write bytes per second", | |
3e9fab69 BC |
3295 | },{ |
3296 | .name = "throttling.iops-total-max", | |
3297 | .type = QEMU_OPT_NUMBER, | |
3298 | .help = "I/O operations burst", | |
3299 | },{ | |
3300 | .name = "throttling.iops-read-max", | |
3301 | .type = QEMU_OPT_NUMBER, | |
3302 | .help = "I/O operations read burst", | |
3303 | },{ | |
3304 | .name = "throttling.iops-write-max", | |
3305 | .type = QEMU_OPT_NUMBER, | |
3306 | .help = "I/O operations write burst", | |
3307 | },{ | |
3308 | .name = "throttling.bps-total-max", | |
3309 | .type = QEMU_OPT_NUMBER, | |
3310 | .help = "total bytes burst", | |
3311 | },{ | |
3312 | .name = "throttling.bps-read-max", | |
3313 | .type = QEMU_OPT_NUMBER, | |
3314 | .help = "total bytes read burst", | |
3315 | },{ | |
3316 | .name = "throttling.bps-write-max", | |
3317 | .type = QEMU_OPT_NUMBER, | |
3318 | .help = "total bytes write burst", | |
2024c1df BC |
3319 | },{ |
3320 | .name = "throttling.iops-size", | |
3321 | .type = QEMU_OPT_NUMBER, | |
3322 | .help = "when limiting by iops max size of an I/O in bytes", | |
76f4afb4 AG |
3323 | },{ |
3324 | .name = "throttling.group", | |
3325 | .type = QEMU_OPT_STRING, | |
3326 | .help = "name of the block throttling group", | |
4d454574 PB |
3327 | },{ |
3328 | .name = "copy-on-read", | |
3329 | .type = QEMU_OPT_BOOL, | |
3330 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
3331 | },{ |
3332 | .name = "detect-zeroes", | |
3333 | .type = QEMU_OPT_STRING, | |
3334 | .help = "try to optimize zero writes (off, on, unmap)", | |
4d454574 PB |
3335 | }, |
3336 | { /* end of list */ } | |
3337 | }, | |
3338 | }; | |
0006383e KW |
3339 | |
3340 | QemuOptsList qemu_drive_opts = { | |
3341 | .name = "drive", | |
3342 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
3343 | .desc = { | |
492fdc6f KW |
3344 | /* |
3345 | * no elements => accept any params | |
3346 | * validation will happen later | |
3347 | */ | |
0006383e KW |
3348 | { /* end of list */ } |
3349 | }, | |
3350 | }; |