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