]>
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 | ||
9c17d615 | 33 | #include "sysemu/blockdev.h" |
0d09e41a | 34 | #include "hw/block/block.h" |
737e150e | 35 | #include "block/blockjob.h" |
83c9089e | 36 | #include "monitor/monitor.h" |
7b1b5d19 | 37 | #include "qapi/qmp/qerror.h" |
1de7afc9 PB |
38 | #include "qemu/option.h" |
39 | #include "qemu/config-file.h" | |
7b1b5d19 | 40 | #include "qapi/qmp/types.h" |
d26c9a15 KW |
41 | #include "qapi-visit.h" |
42 | #include "qapi/qmp-output-visitor.h" | |
9c17d615 | 43 | #include "sysemu/sysemu.h" |
737e150e | 44 | #include "block/block_int.h" |
a4dea8a9 | 45 | #include "qmp-commands.h" |
12bd451f | 46 | #include "trace.h" |
9c17d615 | 47 | #include "sysemu/arch_init.h" |
666daa68 | 48 | |
c9b62a7e | 49 | static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); |
0006383e | 50 | extern QemuOptsList qemu_common_drive_opts; |
666daa68 | 51 | |
1960966d MA |
52 | static const char *const if_name[IF_COUNT] = { |
53 | [IF_NONE] = "none", | |
54 | [IF_IDE] = "ide", | |
55 | [IF_SCSI] = "scsi", | |
56 | [IF_FLOPPY] = "floppy", | |
57 | [IF_PFLASH] = "pflash", | |
58 | [IF_MTD] = "mtd", | |
59 | [IF_SD] = "sd", | |
60 | [IF_VIRTIO] = "virtio", | |
61 | [IF_XEN] = "xen", | |
62 | }; | |
63 | ||
64 | static const int if_max_devs[IF_COUNT] = { | |
27d6bf40 MA |
65 | /* |
66 | * Do not change these numbers! They govern how drive option | |
67 | * index maps to unit and bus. That mapping is ABI. | |
68 | * | |
69 | * All controllers used to imlement if=T drives need to support | |
70 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. | |
71 | * Otherwise, some index values map to "impossible" bus, unit | |
72 | * values. | |
73 | * | |
74 | * For instance, if you change [IF_SCSI] to 255, -drive | |
75 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
76 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
77 | * the drive can't be set up. Regression. | |
78 | */ | |
79 | [IF_IDE] = 2, | |
80 | [IF_SCSI] = 7, | |
1960966d MA |
81 | }; |
82 | ||
14bafc54 MA |
83 | /* |
84 | * We automatically delete the drive when a device using it gets | |
85 | * unplugged. Questionable feature, but we can't just drop it. | |
86 | * Device models call blockdev_mark_auto_del() to schedule the | |
87 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
88 | * when deletion is actually safe. | |
89 | */ | |
90 | void blockdev_mark_auto_del(BlockDriverState *bs) | |
91 | { | |
92 | DriveInfo *dinfo = drive_get_by_blockdev(bs); | |
93 | ||
2d246f01 KW |
94 | if (dinfo && !dinfo->enable_auto_del) { |
95 | return; | |
96 | } | |
97 | ||
12bde0ee PB |
98 | if (bs->job) { |
99 | block_job_cancel(bs->job); | |
100 | } | |
0fc0f1fa RH |
101 | if (dinfo) { |
102 | dinfo->auto_del = 1; | |
103 | } | |
14bafc54 MA |
104 | } |
105 | ||
106 | void blockdev_auto_del(BlockDriverState *bs) | |
107 | { | |
108 | DriveInfo *dinfo = drive_get_by_blockdev(bs); | |
109 | ||
0fc0f1fa | 110 | if (dinfo && dinfo->auto_del) { |
84fb3925 | 111 | drive_put_ref(dinfo); |
14bafc54 MA |
112 | } |
113 | } | |
114 | ||
505a7fb1 MA |
115 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
116 | { | |
117 | int max_devs = if_max_devs[type]; | |
118 | return max_devs ? index / max_devs : 0; | |
119 | } | |
120 | ||
121 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
122 | { | |
123 | int max_devs = if_max_devs[type]; | |
124 | return max_devs ? index % max_devs : index; | |
125 | } | |
126 | ||
2292ddae MA |
127 | QemuOpts *drive_def(const char *optstr) |
128 | { | |
129 | return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); | |
130 | } | |
131 | ||
132 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 133 | const char *optstr) |
666daa68 | 134 | { |
666daa68 | 135 | QemuOpts *opts; |
2292ddae | 136 | char buf[32]; |
666daa68 | 137 | |
2292ddae | 138 | opts = drive_def(optstr); |
666daa68 MA |
139 | if (!opts) { |
140 | return NULL; | |
141 | } | |
2292ddae MA |
142 | if (type != IF_DEFAULT) { |
143 | qemu_opt_set(opts, "if", if_name[type]); | |
144 | } | |
145 | if (index >= 0) { | |
146 | snprintf(buf, sizeof(buf), "%d", index); | |
147 | qemu_opt_set(opts, "index", buf); | |
148 | } | |
666daa68 MA |
149 | if (file) |
150 | qemu_opt_set(opts, "file", file); | |
151 | return opts; | |
152 | } | |
153 | ||
154 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
155 | { | |
156 | DriveInfo *dinfo; | |
157 | ||
158 | /* seek interface, bus and unit */ | |
159 | ||
160 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
161 | if (dinfo->type == type && | |
162 | dinfo->bus == bus && | |
163 | dinfo->unit == unit) | |
164 | return dinfo; | |
165 | } | |
166 | ||
167 | return NULL; | |
168 | } | |
169 | ||
f1bd51ac MA |
170 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
171 | { | |
172 | return drive_get(type, | |
173 | drive_index_to_bus_id(type, index), | |
174 | drive_index_to_unit_id(type, index)); | |
175 | } | |
176 | ||
666daa68 MA |
177 | int drive_get_max_bus(BlockInterfaceType type) |
178 | { | |
179 | int max_bus; | |
180 | DriveInfo *dinfo; | |
181 | ||
182 | max_bus = -1; | |
183 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
184 | if(dinfo->type == type && | |
185 | dinfo->bus > max_bus) | |
186 | max_bus = dinfo->bus; | |
187 | } | |
188 | return max_bus; | |
189 | } | |
190 | ||
13839974 MA |
191 | /* Get a block device. This should only be used for single-drive devices |
192 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
193 | appropriate bus. */ | |
194 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
195 | { | |
196 | static int next_block_unit[IF_COUNT]; | |
197 | ||
198 | return drive_get(type, 0, next_block_unit[type]++); | |
199 | } | |
200 | ||
e4700e59 | 201 | DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) |
666daa68 MA |
202 | { |
203 | DriveInfo *dinfo; | |
204 | ||
205 | QTAILQ_FOREACH(dinfo, &drives, next) { | |
e4700e59 MA |
206 | if (dinfo->bdrv == bs) { |
207 | return dinfo; | |
208 | } | |
666daa68 | 209 | } |
e4700e59 | 210 | return NULL; |
666daa68 MA |
211 | } |
212 | ||
666daa68 MA |
213 | static void bdrv_format_print(void *opaque, const char *name) |
214 | { | |
807105a7 | 215 | error_printf(" %s", name); |
666daa68 MA |
216 | } |
217 | ||
84fb3925 | 218 | static void drive_uninit(DriveInfo *dinfo) |
666daa68 MA |
219 | { |
220 | qemu_opts_del(dinfo->opts); | |
4f6fd349 | 221 | bdrv_unref(dinfo->bdrv); |
7267c094 | 222 | g_free(dinfo->id); |
666daa68 | 223 | QTAILQ_REMOVE(&drives, dinfo, next); |
bb44619b | 224 | g_free(dinfo->serial); |
7267c094 | 225 | g_free(dinfo); |
666daa68 MA |
226 | } |
227 | ||
84fb3925 MT |
228 | void drive_put_ref(DriveInfo *dinfo) |
229 | { | |
230 | assert(dinfo->refcount); | |
231 | if (--dinfo->refcount == 0) { | |
232 | drive_uninit(dinfo); | |
233 | } | |
234 | } | |
235 | ||
236 | void drive_get_ref(DriveInfo *dinfo) | |
237 | { | |
238 | dinfo->refcount++; | |
239 | } | |
240 | ||
aa398a5c SH |
241 | typedef struct { |
242 | QEMUBH *bh; | |
fa510ebf FZ |
243 | BlockDriverState *bs; |
244 | } BDRVPutRefBH; | |
aa398a5c | 245 | |
fa510ebf | 246 | static void bdrv_put_ref_bh(void *opaque) |
aa398a5c | 247 | { |
fa510ebf | 248 | BDRVPutRefBH *s = opaque; |
aa398a5c | 249 | |
fa510ebf | 250 | bdrv_unref(s->bs); |
aa398a5c SH |
251 | qemu_bh_delete(s->bh); |
252 | g_free(s); | |
253 | } | |
254 | ||
255 | /* | |
fa510ebf | 256 | * Release a BDS reference in a BH |
aa398a5c | 257 | * |
fa510ebf FZ |
258 | * It is not safe to use bdrv_unref() from a callback function when the callers |
259 | * still need the BlockDriverState. In such cases we schedule a BH to release | |
260 | * the reference. | |
aa398a5c | 261 | */ |
fa510ebf | 262 | static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) |
aa398a5c | 263 | { |
fa510ebf | 264 | BDRVPutRefBH *s; |
aa398a5c | 265 | |
fa510ebf FZ |
266 | s = g_new(BDRVPutRefBH, 1); |
267 | s->bh = qemu_bh_new(bdrv_put_ref_bh, s); | |
268 | s->bs = bs; | |
aa398a5c SH |
269 | qemu_bh_schedule(s->bh); |
270 | } | |
271 | ||
1ceee0d5 | 272 | static int parse_block_error_action(const char *buf, bool is_read) |
666daa68 MA |
273 | { |
274 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 275 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 276 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 277 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 278 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 279 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 280 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 281 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 282 | } else { |
807105a7 MA |
283 | error_report("'%s' invalid %s error action", |
284 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
285 | return -1; |
286 | } | |
287 | } | |
288 | ||
cc0681c4 | 289 | static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) |
0563e191 | 290 | { |
cc0681c4 BC |
291 | if (throttle_conflicting(cfg)) { |
292 | error_setg(errp, "bps/iops/max total values and read/write values" | |
293 | " cannot be used at the same time"); | |
0563e191 ZYW |
294 | return false; |
295 | } | |
296 | ||
cc0681c4 BC |
297 | if (!throttle_is_valid(cfg)) { |
298 | error_setg(errp, "bps/iops/maxs values must be 0 or greater"); | |
7d81c141 SH |
299 | return false; |
300 | } | |
301 | ||
0563e191 ZYW |
302 | return true; |
303 | } | |
304 | ||
57975222 KW |
305 | static DriveInfo *blockdev_init(QemuOpts *all_opts, |
306 | BlockInterfaceType block_default_type) | |
666daa68 MA |
307 | { |
308 | const char *buf; | |
309 | const char *file = NULL; | |
666daa68 MA |
310 | const char *serial; |
311 | const char *mediastr = ""; | |
312 | BlockInterfaceType type; | |
313 | enum { MEDIA_DISK, MEDIA_CDROM } media; | |
314 | int bus_id, unit_id; | |
315 | int cyls, heads, secs, translation; | |
666daa68 MA |
316 | int max_devs; |
317 | int index; | |
318 | int ro = 0; | |
319 | int bdrv_flags = 0; | |
320 | int on_read_error, on_write_error; | |
321 | const char *devaddr; | |
322 | DriveInfo *dinfo; | |
cc0681c4 | 323 | ThrottleConfig cfg; |
666daa68 | 324 | int snapshot = 0; |
fb0490f6 | 325 | bool copy_on_read; |
666daa68 | 326 | int ret; |
c546194f | 327 | Error *error = NULL; |
0006383e KW |
328 | QemuOpts *opts; |
329 | QDict *bs_opts; | |
330 | const char *id; | |
74fe54f2 | 331 | bool has_driver_specific_opts; |
6db5f5d6 | 332 | BlockDriver *drv = NULL; |
666daa68 | 333 | |
666daa68 | 334 | translation = BIOS_ATA_TRANSLATION_AUTO; |
666daa68 MA |
335 | media = MEDIA_DISK; |
336 | ||
0006383e KW |
337 | /* Check common options by copying from all_opts to opts, all other options |
338 | * are stored in bs_opts. */ | |
339 | id = qemu_opts_id(all_opts); | |
340 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); | |
341 | if (error_is_set(&error)) { | |
342 | qerror_report_err(error); | |
343 | error_free(error); | |
344 | return NULL; | |
345 | } | |
346 | ||
347 | bs_opts = qdict_new(); | |
348 | qemu_opts_to_qdict(all_opts, bs_opts); | |
349 | qemu_opts_absorb_qdict(opts, bs_opts, &error); | |
350 | if (error_is_set(&error)) { | |
351 | qerror_report_err(error); | |
352 | error_free(error); | |
353 | return NULL; | |
354 | } | |
355 | ||
356 | if (id) { | |
357 | qdict_del(bs_opts, "id"); | |
358 | } | |
359 | ||
74fe54f2 KW |
360 | has_driver_specific_opts = !!qdict_size(bs_opts); |
361 | ||
666daa68 MA |
362 | /* extract parameters */ |
363 | bus_id = qemu_opt_get_number(opts, "bus", 0); | |
364 | unit_id = qemu_opt_get_number(opts, "unit", -1); | |
365 | index = qemu_opt_get_number(opts, "index", -1); | |
366 | ||
367 | cyls = qemu_opt_get_number(opts, "cyls", 0); | |
368 | heads = qemu_opt_get_number(opts, "heads", 0); | |
369 | secs = qemu_opt_get_number(opts, "secs", 0); | |
370 | ||
371 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); | |
0f227a94 | 372 | ro = qemu_opt_get_bool(opts, "read-only", 0); |
fb0490f6 | 373 | copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); |
666daa68 MA |
374 | |
375 | file = qemu_opt_get(opts, "file"); | |
376 | serial = qemu_opt_get(opts, "serial"); | |
377 | ||
378 | if ((buf = qemu_opt_get(opts, "if")) != NULL) { | |
1960966d MA |
379 | for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++) |
380 | ; | |
381 | if (type == IF_COUNT) { | |
807105a7 | 382 | error_report("unsupported bus type '%s'", buf); |
666daa68 MA |
383 | return NULL; |
384 | } | |
2d3999fe | 385 | } else { |
2d0d2837 | 386 | type = block_default_type; |
666daa68 | 387 | } |
2d3999fe | 388 | |
1960966d | 389 | max_devs = if_max_devs[type]; |
666daa68 MA |
390 | |
391 | if (cyls || heads || secs) { | |
aaea3f36 | 392 | if (cyls < 1) { |
807105a7 | 393 | error_report("invalid physical cyls number"); |
666daa68 MA |
394 | return NULL; |
395 | } | |
aaea3f36 | 396 | if (heads < 1) { |
807105a7 | 397 | error_report("invalid physical heads number"); |
666daa68 MA |
398 | return NULL; |
399 | } | |
aaea3f36 | 400 | if (secs < 1) { |
807105a7 | 401 | error_report("invalid physical secs number"); |
666daa68 MA |
402 | return NULL; |
403 | } | |
404 | } | |
405 | ||
406 | if ((buf = qemu_opt_get(opts, "trans")) != NULL) { | |
407 | if (!cyls) { | |
e4080f9b | 408 | error_report("'%s' trans must be used with cyls, heads and secs", |
807105a7 | 409 | buf); |
666daa68 MA |
410 | return NULL; |
411 | } | |
412 | if (!strcmp(buf, "none")) | |
413 | translation = BIOS_ATA_TRANSLATION_NONE; | |
414 | else if (!strcmp(buf, "lba")) | |
415 | translation = BIOS_ATA_TRANSLATION_LBA; | |
416 | else if (!strcmp(buf, "auto")) | |
417 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
418 | else { | |
807105a7 | 419 | error_report("'%s' invalid translation type", buf); |
666daa68 MA |
420 | return NULL; |
421 | } | |
422 | } | |
423 | ||
424 | if ((buf = qemu_opt_get(opts, "media")) != NULL) { | |
425 | if (!strcmp(buf, "disk")) { | |
426 | media = MEDIA_DISK; | |
427 | } else if (!strcmp(buf, "cdrom")) { | |
428 | if (cyls || secs || heads) { | |
e7ff8f0e | 429 | error_report("CHS can't be set with media=%s", buf); |
666daa68 MA |
430 | return NULL; |
431 | } | |
432 | media = MEDIA_CDROM; | |
433 | } else { | |
807105a7 | 434 | error_report("'%s' invalid media", buf); |
666daa68 MA |
435 | return NULL; |
436 | } | |
437 | } | |
438 | ||
a9384aff PB |
439 | if ((buf = qemu_opt_get(opts, "discard")) != NULL) { |
440 | if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { | |
441 | error_report("invalid discard option"); | |
442 | return NULL; | |
443 | } | |
444 | } | |
445 | ||
29c4e2b5 KW |
446 | if (qemu_opt_get_bool(opts, "cache.writeback", true)) { |
447 | bdrv_flags |= BDRV_O_CACHE_WB; | |
448 | } | |
449 | if (qemu_opt_get_bool(opts, "cache.direct", false)) { | |
450 | bdrv_flags |= BDRV_O_NOCACHE; | |
451 | } | |
1df6fa4b | 452 | if (qemu_opt_get_bool(opts, "cache.no-flush", false)) { |
29c4e2b5 | 453 | bdrv_flags |= BDRV_O_NO_FLUSH; |
666daa68 MA |
454 | } |
455 | ||
456 | #ifdef CONFIG_LINUX_AIO | |
457 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { | |
458 | if (!strcmp(buf, "native")) { | |
459 | bdrv_flags |= BDRV_O_NATIVE_AIO; | |
460 | } else if (!strcmp(buf, "threads")) { | |
461 | /* this is the default */ | |
462 | } else { | |
807105a7 | 463 | error_report("invalid aio option"); |
666daa68 MA |
464 | return NULL; |
465 | } | |
466 | } | |
467 | #endif | |
468 | ||
469 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 PM |
470 | if (is_help_option(buf)) { |
471 | error_printf("Supported formats:"); | |
472 | bdrv_iterate_format(bdrv_format_print, NULL); | |
473 | error_printf("\n"); | |
474 | return NULL; | |
666daa68 | 475 | } |
74fe54f2 | 476 | |
8f94a6e4 | 477 | drv = bdrv_find_format(buf); |
6db5f5d6 | 478 | if (!drv) { |
8f94a6e4 | 479 | error_report("'%s' invalid format", buf); |
6db5f5d6 MQ |
480 | return NULL; |
481 | } | |
666daa68 MA |
482 | } |
483 | ||
0563e191 | 484 | /* disk I/O throttling */ |
cc0681c4 BC |
485 | memset(&cfg, 0, sizeof(cfg)); |
486 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = | |
57975222 | 487 | qemu_opt_get_number(opts, "throttling.bps-total", 0); |
cc0681c4 | 488 | cfg.buckets[THROTTLE_BPS_READ].avg = |
57975222 | 489 | qemu_opt_get_number(opts, "throttling.bps-read", 0); |
cc0681c4 | 490 | cfg.buckets[THROTTLE_BPS_WRITE].avg = |
57975222 | 491 | qemu_opt_get_number(opts, "throttling.bps-write", 0); |
cc0681c4 | 492 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = |
57975222 | 493 | qemu_opt_get_number(opts, "throttling.iops-total", 0); |
cc0681c4 | 494 | cfg.buckets[THROTTLE_OPS_READ].avg = |
57975222 | 495 | qemu_opt_get_number(opts, "throttling.iops-read", 0); |
cc0681c4 | 496 | cfg.buckets[THROTTLE_OPS_WRITE].avg = |
57975222 | 497 | qemu_opt_get_number(opts, "throttling.iops-write", 0); |
0563e191 | 498 | |
3e9fab69 BC |
499 | cfg.buckets[THROTTLE_BPS_TOTAL].max = |
500 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
501 | cfg.buckets[THROTTLE_BPS_READ].max = | |
502 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
503 | cfg.buckets[THROTTLE_BPS_WRITE].max = | |
504 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
505 | cfg.buckets[THROTTLE_OPS_TOTAL].max = | |
506 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
507 | cfg.buckets[THROTTLE_OPS_READ].max = | |
508 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
509 | cfg.buckets[THROTTLE_OPS_WRITE].max = | |
510 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
cc0681c4 | 511 | |
2024c1df | 512 | cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0); |
cc0681c4 BC |
513 | |
514 | if (!check_throttle_config(&cfg, &error)) { | |
c546194f SH |
515 | error_report("%s", error_get_pretty(error)); |
516 | error_free(error); | |
0563e191 ZYW |
517 | return NULL; |
518 | } | |
519 | ||
0d92d17a JK |
520 | if (qemu_opt_get(opts, "boot") != NULL) { |
521 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " | |
522 | "ignored. Future versions will reject this parameter. Please " | |
523 | "update your scripts.\n"); | |
524 | } | |
525 | ||
92aa5c6d | 526 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 MA |
527 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
528 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { | |
807105a7 | 529 | error_report("werror is not supported by this bus type"); |
666daa68 MA |
530 | return NULL; |
531 | } | |
532 | ||
533 | on_write_error = parse_block_error_action(buf, 0); | |
534 | if (on_write_error < 0) { | |
535 | return NULL; | |
536 | } | |
537 | } | |
538 | ||
92aa5c6d | 539 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 540 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
5dba48a8 | 541 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { |
807105a7 | 542 | error_report("rerror is not supported by this bus type"); |
666daa68 MA |
543 | return NULL; |
544 | } | |
545 | ||
546 | on_read_error = parse_block_error_action(buf, 1); | |
547 | if (on_read_error < 0) { | |
548 | return NULL; | |
549 | } | |
550 | } | |
551 | ||
552 | if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { | |
553 | if (type != IF_VIRTIO) { | |
807105a7 | 554 | error_report("addr is not supported by this bus type"); |
666daa68 MA |
555 | return NULL; |
556 | } | |
557 | } | |
558 | ||
559 | /* compute bus and unit according index */ | |
560 | ||
561 | if (index != -1) { | |
562 | if (bus_id != 0 || unit_id != -1) { | |
807105a7 | 563 | error_report("index cannot be used with bus and unit"); |
666daa68 MA |
564 | return NULL; |
565 | } | |
505a7fb1 MA |
566 | bus_id = drive_index_to_bus_id(type, index); |
567 | unit_id = drive_index_to_unit_id(type, index); | |
666daa68 MA |
568 | } |
569 | ||
570 | /* if user doesn't specify a unit_id, | |
571 | * try to find the first free | |
572 | */ | |
573 | ||
574 | if (unit_id == -1) { | |
575 | unit_id = 0; | |
576 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
577 | unit_id++; | |
578 | if (max_devs && unit_id >= max_devs) { | |
579 | unit_id -= max_devs; | |
580 | bus_id++; | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
585 | /* check unit id */ | |
586 | ||
587 | if (max_devs && unit_id >= max_devs) { | |
807105a7 MA |
588 | error_report("unit %d too big (max is %d)", |
589 | unit_id, max_devs - 1); | |
666daa68 MA |
590 | return NULL; |
591 | } | |
592 | ||
593 | /* | |
4e5d9b57 | 594 | * catch multiple definitions |
666daa68 MA |
595 | */ |
596 | ||
597 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
4e5d9b57 MA |
598 | error_report("drive with bus=%d, unit=%d (index=%d) exists", |
599 | bus_id, unit_id, index); | |
666daa68 MA |
600 | return NULL; |
601 | } | |
602 | ||
603 | /* init */ | |
604 | ||
7267c094 | 605 | dinfo = g_malloc0(sizeof(*dinfo)); |
666daa68 | 606 | if ((buf = qemu_opts_id(opts)) != NULL) { |
7267c094 | 607 | dinfo->id = g_strdup(buf); |
666daa68 MA |
608 | } else { |
609 | /* no id supplied -> create one */ | |
7267c094 | 610 | dinfo->id = g_malloc0(32); |
666daa68 MA |
611 | if (type == IF_IDE || type == IF_SCSI) |
612 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
613 | if (max_devs) | |
614 | snprintf(dinfo->id, 32, "%s%i%s%i", | |
79d21d5b | 615 | if_name[type], bus_id, mediastr, unit_id); |
666daa68 MA |
616 | else |
617 | snprintf(dinfo->id, 32, "%s%s%i", | |
79d21d5b | 618 | if_name[type], mediastr, unit_id); |
666daa68 MA |
619 | } |
620 | dinfo->bdrv = bdrv_new(dinfo->id); | |
80dd1aae KS |
621 | dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; |
622 | dinfo->bdrv->read_only = ro; | |
666daa68 MA |
623 | dinfo->devaddr = devaddr; |
624 | dinfo->type = type; | |
625 | dinfo->bus = bus_id; | |
626 | dinfo->unit = unit_id; | |
317bb412 MA |
627 | dinfo->cyls = cyls; |
628 | dinfo->heads = heads; | |
629 | dinfo->secs = secs; | |
630 | dinfo->trans = translation; | |
0006383e | 631 | dinfo->opts = all_opts; |
84fb3925 | 632 | dinfo->refcount = 1; |
bb44619b KW |
633 | if (serial != NULL) { |
634 | dinfo->serial = g_strdup(serial); | |
635 | } | |
666daa68 MA |
636 | QTAILQ_INSERT_TAIL(&drives, dinfo, next); |
637 | ||
abd7f68d MA |
638 | bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); |
639 | ||
0563e191 | 640 | /* disk I/O throttling */ |
cc0681c4 BC |
641 | if (throttle_enabled(&cfg)) { |
642 | bdrv_io_limits_enable(dinfo->bdrv); | |
643 | bdrv_set_io_limits(dinfo->bdrv, &cfg); | |
644 | } | |
0563e191 | 645 | |
666daa68 MA |
646 | switch(type) { |
647 | case IF_IDE: | |
648 | case IF_SCSI: | |
649 | case IF_XEN: | |
650 | case IF_NONE: | |
2b584959 | 651 | dinfo->media_cd = media == MEDIA_CDROM; |
666daa68 MA |
652 | break; |
653 | case IF_SD: | |
666daa68 | 654 | case IF_FLOPPY: |
666daa68 MA |
655 | case IF_PFLASH: |
656 | case IF_MTD: | |
657 | break; | |
658 | case IF_VIRTIO: | |
0006383e | 659 | { |
666daa68 | 660 | /* add virtio block device */ |
0006383e KW |
661 | QemuOpts *devopts; |
662 | devopts = qemu_opts_create_nofail(qemu_find_opts("device")); | |
eeb9c1b5 | 663 | if (arch_type == QEMU_ARCH_S390X) { |
0006383e | 664 | qemu_opt_set(devopts, "driver", "virtio-blk-s390"); |
eeb9c1b5 | 665 | } else { |
0006383e | 666 | qemu_opt_set(devopts, "driver", "virtio-blk-pci"); |
eeb9c1b5 | 667 | } |
0006383e | 668 | qemu_opt_set(devopts, "drive", dinfo->id); |
666daa68 | 669 | if (devaddr) |
0006383e | 670 | qemu_opt_set(devopts, "addr", devaddr); |
666daa68 | 671 | break; |
0006383e | 672 | } |
2292ddae | 673 | default: |
666daa68 MA |
674 | abort(); |
675 | } | |
dd5b0d71 | 676 | if (!file || !*file) { |
74fe54f2 | 677 | if (has_driver_specific_opts) { |
c2ad1b0c KW |
678 | file = NULL; |
679 | } else { | |
680 | return dinfo; | |
681 | } | |
666daa68 MA |
682 | } |
683 | if (snapshot) { | |
684 | /* always use cache=unsafe with snapshot */ | |
685 | bdrv_flags &= ~BDRV_O_CACHE_MASK; | |
686 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); | |
687 | } | |
688 | ||
fb0490f6 SH |
689 | if (copy_on_read) { |
690 | bdrv_flags |= BDRV_O_COPY_ON_READ; | |
691 | } | |
692 | ||
ed9d4205 BC |
693 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
694 | bdrv_flags |= BDRV_O_INCOMING; | |
695 | } | |
696 | ||
666daa68 MA |
697 | if (media == MEDIA_CDROM) { |
698 | /* CDROM is fine for any interface, don't check. */ | |
699 | ro = 1; | |
700 | } else if (ro == 1) { | |
1e9eb78a JJ |
701 | if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && |
702 | type != IF_NONE && type != IF_PFLASH) { | |
0f227a94 | 703 | error_report("read-only not supported by this bus type"); |
a9ae2bff | 704 | goto err; |
666daa68 MA |
705 | } |
706 | } | |
707 | ||
708 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; | |
709 | ||
04d4abe9 | 710 | if (ro && copy_on_read) { |
0f227a94 | 711 | error_report("warning: disabling copy_on_read on read-only drive"); |
04d4abe9 SH |
712 | } |
713 | ||
74fe54f2 | 714 | QINCREF(bs_opts); |
34b5d2c6 | 715 | ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error); |
0006383e | 716 | |
666daa68 | 717 | if (ret < 0) { |
34b5d2c6 HR |
718 | error_report("could not open disk image %s: %s", |
719 | file ?: dinfo->id, error_get_pretty(error)); | |
a9ae2bff | 720 | goto err; |
666daa68 MA |
721 | } |
722 | ||
723 | if (bdrv_key_required(dinfo->bdrv)) | |
724 | autostart = 0; | |
0006383e | 725 | |
74fe54f2 | 726 | QDECREF(bs_opts); |
0006383e KW |
727 | qemu_opts_del(opts); |
728 | ||
666daa68 | 729 | return dinfo; |
a9ae2bff MA |
730 | |
731 | err: | |
0006383e KW |
732 | qemu_opts_del(opts); |
733 | QDECREF(bs_opts); | |
4f6fd349 | 734 | bdrv_unref(dinfo->bdrv); |
7267c094 | 735 | g_free(dinfo->id); |
a9ae2bff | 736 | QTAILQ_REMOVE(&drives, dinfo, next); |
7267c094 | 737 | g_free(dinfo); |
a9ae2bff | 738 | return NULL; |
666daa68 MA |
739 | } |
740 | ||
57975222 KW |
741 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to) |
742 | { | |
743 | const char *value; | |
744 | ||
745 | value = qemu_opt_get(opts, from); | |
746 | if (value) { | |
747 | qemu_opt_set(opts, to, value); | |
748 | qemu_opt_unset(opts, from); | |
749 | } | |
750 | } | |
751 | ||
752 | DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) | |
753 | { | |
29c4e2b5 | 754 | const char *value; |
2d246f01 | 755 | DriveInfo *dinfo; |
29c4e2b5 | 756 | |
57975222 KW |
757 | /* Change legacy command line options into QMP ones */ |
758 | qemu_opt_rename(all_opts, "iops", "throttling.iops-total"); | |
759 | qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read"); | |
760 | qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write"); | |
761 | ||
762 | qemu_opt_rename(all_opts, "bps", "throttling.bps-total"); | |
763 | qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read"); | |
764 | qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write"); | |
765 | ||
3e9fab69 BC |
766 | qemu_opt_rename(all_opts, "iops_max", "throttling.iops-total-max"); |
767 | qemu_opt_rename(all_opts, "iops_rd_max", "throttling.iops-read-max"); | |
768 | qemu_opt_rename(all_opts, "iops_wr_max", "throttling.iops-write-max"); | |
769 | ||
770 | qemu_opt_rename(all_opts, "bps_max", "throttling.bps-total-max"); | |
771 | qemu_opt_rename(all_opts, "bps_rd_max", "throttling.bps-read-max"); | |
772 | qemu_opt_rename(all_opts, "bps_wr_max", "throttling.bps-write-max"); | |
773 | ||
2024c1df BC |
774 | qemu_opt_rename(all_opts, |
775 | "iops_size", "throttling.iops-size"); | |
776 | ||
0f227a94 KW |
777 | qemu_opt_rename(all_opts, "readonly", "read-only"); |
778 | ||
29c4e2b5 KW |
779 | value = qemu_opt_get(all_opts, "cache"); |
780 | if (value) { | |
781 | int flags = 0; | |
782 | ||
783 | if (bdrv_parse_cache_flags(value, &flags) != 0) { | |
784 | error_report("invalid cache option"); | |
785 | return NULL; | |
786 | } | |
787 | ||
788 | /* Specific options take precedence */ | |
789 | if (!qemu_opt_get(all_opts, "cache.writeback")) { | |
790 | qemu_opt_set_bool(all_opts, "cache.writeback", | |
791 | !!(flags & BDRV_O_CACHE_WB)); | |
792 | } | |
793 | if (!qemu_opt_get(all_opts, "cache.direct")) { | |
794 | qemu_opt_set_bool(all_opts, "cache.direct", | |
795 | !!(flags & BDRV_O_NOCACHE)); | |
796 | } | |
797 | if (!qemu_opt_get(all_opts, "cache.no-flush")) { | |
798 | qemu_opt_set_bool(all_opts, "cache.no-flush", | |
799 | !!(flags & BDRV_O_NO_FLUSH)); | |
800 | } | |
801 | qemu_opt_unset(all_opts, "cache"); | |
802 | } | |
803 | ||
2d246f01 KW |
804 | /* Actual block device init: Functionality shared with blockdev-add */ |
805 | dinfo = blockdev_init(all_opts, block_default_type); | |
806 | if (dinfo == NULL) { | |
807 | goto fail; | |
808 | } | |
809 | ||
810 | /* Set legacy DriveInfo fields */ | |
811 | dinfo->enable_auto_del = true; | |
812 | ||
813 | fail: | |
814 | return dinfo; | |
57975222 KW |
815 | } |
816 | ||
666daa68 MA |
817 | void do_commit(Monitor *mon, const QDict *qdict) |
818 | { | |
666daa68 | 819 | const char *device = qdict_get_str(qdict, "device"); |
6ab4b5ab | 820 | BlockDriverState *bs; |
e8877497 | 821 | int ret; |
666daa68 | 822 | |
6ab4b5ab | 823 | if (!strcmp(device, "all")) { |
e8877497 | 824 | ret = bdrv_commit_all(); |
6ab4b5ab MA |
825 | } else { |
826 | bs = bdrv_find(device); | |
ac59eb95 | 827 | if (!bs) { |
58513bde | 828 | monitor_printf(mon, "Device '%s' not found\n", device); |
ac59eb95 | 829 | return; |
6ab4b5ab | 830 | } |
2d3735d3 | 831 | ret = bdrv_commit(bs); |
58513bde JC |
832 | } |
833 | if (ret < 0) { | |
834 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
835 | strerror(-ret)); | |
666daa68 MA |
836 | } |
837 | } | |
838 | ||
6cc2a415 PB |
839 | static void blockdev_do_action(int kind, void *data, Error **errp) |
840 | { | |
c8a83e85 KW |
841 | TransactionAction action; |
842 | TransactionActionList list; | |
6cc2a415 PB |
843 | |
844 | action.kind = kind; | |
845 | action.data = data; | |
846 | list.value = &action; | |
847 | list.next = NULL; | |
848 | qmp_transaction(&list, errp); | |
849 | } | |
850 | ||
6106e249 LC |
851 | void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file, |
852 | bool has_format, const char *format, | |
6cc2a415 | 853 | bool has_mode, enum NewImageMode mode, |
6106e249 | 854 | Error **errp) |
f8882568 | 855 | { |
6cc2a415 PB |
856 | BlockdevSnapshot snapshot = { |
857 | .device = (char *) device, | |
858 | .snapshot_file = (char *) snapshot_file, | |
859 | .has_format = has_format, | |
860 | .format = (char *) format, | |
861 | .has_mode = has_mode, | |
862 | .mode = mode, | |
863 | }; | |
c8a83e85 KW |
864 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
865 | &snapshot, errp); | |
f8882568 JS |
866 | } |
867 | ||
f323bc9e WX |
868 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
869 | const char *name, | |
870 | Error **errp) | |
871 | { | |
872 | BlockdevSnapshotInternal snapshot = { | |
873 | .device = (char *) device, | |
874 | .name = (char *) name | |
875 | }; | |
876 | ||
877 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
878 | &snapshot, errp); | |
879 | } | |
880 | ||
44e3e053 WX |
881 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
882 | bool has_id, | |
883 | const char *id, | |
884 | bool has_name, | |
885 | const char *name, | |
886 | Error **errp) | |
887 | { | |
888 | BlockDriverState *bs = bdrv_find(device); | |
889 | QEMUSnapshotInfo sn; | |
890 | Error *local_err = NULL; | |
891 | SnapshotInfo *info = NULL; | |
892 | int ret; | |
893 | ||
894 | if (!bs) { | |
895 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
896 | return NULL; | |
897 | } | |
898 | ||
899 | if (!has_id) { | |
900 | id = NULL; | |
901 | } | |
902 | ||
903 | if (!has_name) { | |
904 | name = NULL; | |
905 | } | |
906 | ||
907 | if (!id && !name) { | |
908 | error_setg(errp, "Name or id must be provided"); | |
909 | return NULL; | |
910 | } | |
911 | ||
912 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); | |
913 | if (error_is_set(&local_err)) { | |
914 | error_propagate(errp, local_err); | |
915 | return NULL; | |
916 | } | |
917 | if (!ret) { | |
918 | error_setg(errp, | |
919 | "Snapshot with id '%s' and name '%s' does not exist on " | |
920 | "device '%s'", | |
921 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
922 | return NULL; | |
923 | } | |
924 | ||
925 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
926 | if (error_is_set(&local_err)) { | |
927 | error_propagate(errp, local_err); | |
928 | return NULL; | |
929 | } | |
930 | ||
931 | info = g_malloc0(sizeof(SnapshotInfo)); | |
932 | info->id = g_strdup(sn.id_str); | |
933 | info->name = g_strdup(sn.name); | |
934 | info->date_nsec = sn.date_nsec; | |
935 | info->date_sec = sn.date_sec; | |
936 | info->vm_state_size = sn.vm_state_size; | |
937 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
938 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
939 | ||
940 | return info; | |
941 | } | |
8802d1fd JC |
942 | |
943 | /* New and old BlockDriverState structs for group snapshots */ | |
ba0c86a3 | 944 | |
ba5d6ab6 | 945 | typedef struct BlkTransactionState BlkTransactionState; |
ba0c86a3 WX |
946 | |
947 | /* Only prepare() may fail. In a single transaction, only one of commit() or | |
948 | abort() will be called, clean() will always be called if it present. */ | |
949 | typedef struct BdrvActionOps { | |
950 | /* Size of state struct, in bytes. */ | |
951 | size_t instance_size; | |
952 | /* Prepare the work, must NOT be NULL. */ | |
ba5d6ab6 | 953 | void (*prepare)(BlkTransactionState *common, Error **errp); |
f9ea81e8 | 954 | /* Commit the changes, can be NULL. */ |
ba5d6ab6 | 955 | void (*commit)(BlkTransactionState *common); |
ba0c86a3 | 956 | /* Abort the changes on fail, can be NULL. */ |
ba5d6ab6 | 957 | void (*abort)(BlkTransactionState *common); |
ba0c86a3 | 958 | /* Clean up resource in the end, can be NULL. */ |
ba5d6ab6 | 959 | void (*clean)(BlkTransactionState *common); |
ba0c86a3 WX |
960 | } BdrvActionOps; |
961 | ||
962 | /* | |
963 | * This structure must be arranged as first member in child type, assuming | |
964 | * that compiler will also arrange it to the same address with parent instance. | |
965 | * Later it will be used in free(). | |
966 | */ | |
ba5d6ab6 | 967 | struct BlkTransactionState { |
c8a83e85 | 968 | TransactionAction *action; |
ba0c86a3 | 969 | const BdrvActionOps *ops; |
ba5d6ab6 | 970 | QSIMPLEQ_ENTRY(BlkTransactionState) entry; |
ba0c86a3 WX |
971 | }; |
972 | ||
bbe86010 WX |
973 | /* internal snapshot private data */ |
974 | typedef struct InternalSnapshotState { | |
975 | BlkTransactionState common; | |
976 | BlockDriverState *bs; | |
977 | QEMUSnapshotInfo sn; | |
978 | } InternalSnapshotState; | |
979 | ||
980 | static void internal_snapshot_prepare(BlkTransactionState *common, | |
981 | Error **errp) | |
982 | { | |
983 | const char *device; | |
984 | const char *name; | |
985 | BlockDriverState *bs; | |
986 | QEMUSnapshotInfo old_sn, *sn; | |
987 | bool ret; | |
988 | qemu_timeval tv; | |
989 | BlockdevSnapshotInternal *internal; | |
990 | InternalSnapshotState *state; | |
991 | int ret1; | |
992 | ||
993 | g_assert(common->action->kind == | |
994 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); | |
995 | internal = common->action->blockdev_snapshot_internal_sync; | |
996 | state = DO_UPCAST(InternalSnapshotState, common, common); | |
997 | ||
998 | /* 1. parse input */ | |
999 | device = internal->device; | |
1000 | name = internal->name; | |
1001 | ||
1002 | /* 2. check for validation */ | |
1003 | bs = bdrv_find(device); | |
1004 | if (!bs) { | |
1005 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1006 | return; | |
1007 | } | |
1008 | ||
1009 | if (!bdrv_is_inserted(bs)) { | |
1010 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1011 | return; | |
1012 | } | |
1013 | ||
1014 | if (bdrv_is_read_only(bs)) { | |
1015 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); | |
1016 | return; | |
1017 | } | |
1018 | ||
1019 | if (!bdrv_can_snapshot(bs)) { | |
1020 | error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, | |
1021 | bs->drv->format_name, device, "internal snapshot"); | |
1022 | return; | |
1023 | } | |
1024 | ||
1025 | if (!strlen(name)) { | |
1026 | error_setg(errp, "Name is empty"); | |
1027 | return; | |
1028 | } | |
1029 | ||
1030 | /* check whether a snapshot with name exist */ | |
1031 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, errp); | |
1032 | if (error_is_set(errp)) { | |
1033 | return; | |
1034 | } else if (ret) { | |
1035 | error_setg(errp, | |
1036 | "Snapshot with name '%s' already exists on device '%s'", | |
1037 | name, device); | |
1038 | return; | |
1039 | } | |
1040 | ||
1041 | /* 3. take the snapshot */ | |
1042 | sn = &state->sn; | |
1043 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1044 | qemu_gettimeofday(&tv); | |
1045 | sn->date_sec = tv.tv_sec; | |
1046 | sn->date_nsec = tv.tv_usec * 1000; | |
1047 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1048 | ||
1049 | ret1 = bdrv_snapshot_create(bs, sn); | |
1050 | if (ret1 < 0) { | |
1051 | error_setg_errno(errp, -ret1, | |
1052 | "Failed to create snapshot '%s' on device '%s'", | |
1053 | name, device); | |
1054 | return; | |
1055 | } | |
1056 | ||
1057 | /* 4. succeed, mark a snapshot is created */ | |
1058 | state->bs = bs; | |
1059 | } | |
1060 | ||
1061 | static void internal_snapshot_abort(BlkTransactionState *common) | |
1062 | { | |
1063 | InternalSnapshotState *state = | |
1064 | DO_UPCAST(InternalSnapshotState, common, common); | |
1065 | BlockDriverState *bs = state->bs; | |
1066 | QEMUSnapshotInfo *sn = &state->sn; | |
1067 | Error *local_error = NULL; | |
1068 | ||
1069 | if (!bs) { | |
1070 | return; | |
1071 | } | |
1072 | ||
1073 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { | |
1074 | error_report("Failed to delete snapshot with id '%s' and name '%s' on " | |
1075 | "device '%s' in abort: %s", | |
1076 | sn->id_str, | |
1077 | sn->name, | |
1078 | bdrv_get_device_name(bs), | |
1079 | error_get_pretty(local_error)); | |
1080 | error_free(local_error); | |
1081 | } | |
1082 | } | |
1083 | ||
ba0c86a3 | 1084 | /* external snapshot private data */ |
ba5d6ab6 SH |
1085 | typedef struct ExternalSnapshotState { |
1086 | BlkTransactionState common; | |
8802d1fd JC |
1087 | BlockDriverState *old_bs; |
1088 | BlockDriverState *new_bs; | |
ba5d6ab6 | 1089 | } ExternalSnapshotState; |
8802d1fd | 1090 | |
ba5d6ab6 | 1091 | static void external_snapshot_prepare(BlkTransactionState *common, |
9b9877ee WX |
1092 | Error **errp) |
1093 | { | |
9b9877ee WX |
1094 | BlockDriver *drv; |
1095 | int flags, ret; | |
1096 | Error *local_err = NULL; | |
e2a31e87 WX |
1097 | const char *device; |
1098 | const char *new_image_file; | |
1099 | const char *format = "qcow2"; | |
1100 | enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
ba5d6ab6 SH |
1101 | ExternalSnapshotState *state = |
1102 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1103 | TransactionAction *action = common->action; |
9b9877ee | 1104 | |
e2a31e87 | 1105 | /* get parameters */ |
c8a83e85 | 1106 | g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); |
e2a31e87 WX |
1107 | |
1108 | device = action->blockdev_snapshot_sync->device; | |
1109 | new_image_file = action->blockdev_snapshot_sync->snapshot_file; | |
1110 | if (action->blockdev_snapshot_sync->has_format) { | |
1111 | format = action->blockdev_snapshot_sync->format; | |
1112 | } | |
1113 | if (action->blockdev_snapshot_sync->has_mode) { | |
1114 | mode = action->blockdev_snapshot_sync->mode; | |
1115 | } | |
1116 | ||
1117 | /* start processing */ | |
9b9877ee WX |
1118 | drv = bdrv_find_format(format); |
1119 | if (!drv) { | |
1120 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
1121 | return; | |
1122 | } | |
1123 | ||
ba5d6ab6 SH |
1124 | state->old_bs = bdrv_find(device); |
1125 | if (!state->old_bs) { | |
9b9877ee WX |
1126 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1127 | return; | |
1128 | } | |
1129 | ||
ba5d6ab6 | 1130 | if (!bdrv_is_inserted(state->old_bs)) { |
9b9877ee WX |
1131 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
1132 | return; | |
1133 | } | |
1134 | ||
ba5d6ab6 | 1135 | if (bdrv_in_use(state->old_bs)) { |
9b9877ee WX |
1136 | error_set(errp, QERR_DEVICE_IN_USE, device); |
1137 | return; | |
1138 | } | |
1139 | ||
ba5d6ab6 SH |
1140 | if (!bdrv_is_read_only(state->old_bs)) { |
1141 | if (bdrv_flush(state->old_bs)) { | |
9b9877ee WX |
1142 | error_set(errp, QERR_IO_ERROR); |
1143 | return; | |
1144 | } | |
1145 | } | |
1146 | ||
f6186f49 BC |
1147 | if (bdrv_check_ext_snapshot(state->old_bs) != EXT_SNAPSHOT_ALLOWED) { |
1148 | error_set(errp, QERR_FEATURE_DISABLED, "snapshot"); | |
1149 | return; | |
1150 | } | |
1151 | ||
ba5d6ab6 | 1152 | flags = state->old_bs->open_flags; |
9b9877ee | 1153 | |
9b9877ee WX |
1154 | /* create new image w/backing file */ |
1155 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1156 | bdrv_img_create(new_image_file, format, | |
ba5d6ab6 SH |
1157 | state->old_bs->filename, |
1158 | state->old_bs->drv->format_name, | |
9b9877ee WX |
1159 | NULL, -1, flags, &local_err, false); |
1160 | if (error_is_set(&local_err)) { | |
1161 | error_propagate(errp, local_err); | |
1162 | return; | |
1163 | } | |
1164 | } | |
1165 | ||
1166 | /* We will manually add the backing_hd field to the bs later */ | |
ba5d6ab6 | 1167 | state->new_bs = bdrv_new(""); |
9b9877ee WX |
1168 | /* TODO Inherit bs->options or only take explicit options with an |
1169 | * extended QMP command? */ | |
ba5d6ab6 | 1170 | ret = bdrv_open(state->new_bs, new_image_file, NULL, |
34b5d2c6 | 1171 | flags | BDRV_O_NO_BACKING, drv, &local_err); |
9b9877ee | 1172 | if (ret != 0) { |
34b5d2c6 | 1173 | error_propagate(errp, local_err); |
9b9877ee WX |
1174 | } |
1175 | } | |
1176 | ||
ba5d6ab6 | 1177 | static void external_snapshot_commit(BlkTransactionState *common) |
3b0047e8 | 1178 | { |
ba5d6ab6 SH |
1179 | ExternalSnapshotState *state = |
1180 | DO_UPCAST(ExternalSnapshotState, common, common); | |
ba0c86a3 | 1181 | |
ba5d6ab6 SH |
1182 | /* This removes our old bs and adds the new bs */ |
1183 | bdrv_append(state->new_bs, state->old_bs); | |
3b0047e8 WX |
1184 | /* We don't need (or want) to use the transactional |
1185 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1186 | * don't want to abort all of them if one of them fails the reopen */ | |
ba5d6ab6 | 1187 | bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR, |
3b0047e8 WX |
1188 | NULL); |
1189 | } | |
1190 | ||
ba5d6ab6 | 1191 | static void external_snapshot_abort(BlkTransactionState *common) |
96b86bf7 | 1192 | { |
ba5d6ab6 SH |
1193 | ExternalSnapshotState *state = |
1194 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1195 | if (state->new_bs) { | |
4f6fd349 | 1196 | bdrv_unref(state->new_bs); |
96b86bf7 WX |
1197 | } |
1198 | } | |
1199 | ||
3037f364 SH |
1200 | typedef struct DriveBackupState { |
1201 | BlkTransactionState common; | |
1202 | BlockDriverState *bs; | |
1203 | BlockJob *job; | |
1204 | } DriveBackupState; | |
1205 | ||
1206 | static void drive_backup_prepare(BlkTransactionState *common, Error **errp) | |
1207 | { | |
1208 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1209 | DriveBackup *backup; | |
1210 | Error *local_err = NULL; | |
1211 | ||
1212 | assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); | |
1213 | backup = common->action->drive_backup; | |
1214 | ||
1215 | qmp_drive_backup(backup->device, backup->target, | |
1216 | backup->has_format, backup->format, | |
b53169ea | 1217 | backup->sync, |
3037f364 SH |
1218 | backup->has_mode, backup->mode, |
1219 | backup->has_speed, backup->speed, | |
1220 | backup->has_on_source_error, backup->on_source_error, | |
1221 | backup->has_on_target_error, backup->on_target_error, | |
1222 | &local_err); | |
1223 | if (error_is_set(&local_err)) { | |
1224 | error_propagate(errp, local_err); | |
1225 | state->bs = NULL; | |
1226 | state->job = NULL; | |
1227 | return; | |
1228 | } | |
1229 | ||
1230 | state->bs = bdrv_find(backup->device); | |
1231 | state->job = state->bs->job; | |
1232 | } | |
1233 | ||
1234 | static void drive_backup_abort(BlkTransactionState *common) | |
1235 | { | |
1236 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1237 | BlockDriverState *bs = state->bs; | |
1238 | ||
1239 | /* Only cancel if it's the job we started */ | |
1240 | if (bs && bs->job && bs->job == state->job) { | |
1241 | block_job_cancel_sync(bs->job); | |
1242 | } | |
1243 | } | |
1244 | ||
78b18b78 SH |
1245 | static void abort_prepare(BlkTransactionState *common, Error **errp) |
1246 | { | |
1247 | error_setg(errp, "Transaction aborted using Abort action"); | |
1248 | } | |
1249 | ||
1250 | static void abort_commit(BlkTransactionState *common) | |
1251 | { | |
dfc6f865 | 1252 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
1253 | } |
1254 | ||
ba0c86a3 | 1255 | static const BdrvActionOps actions[] = { |
c8a83e85 | 1256 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 1257 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
1258 | .prepare = external_snapshot_prepare, |
1259 | .commit = external_snapshot_commit, | |
1260 | .abort = external_snapshot_abort, | |
1261 | }, | |
3037f364 SH |
1262 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
1263 | .instance_size = sizeof(DriveBackupState), | |
1264 | .prepare = drive_backup_prepare, | |
1265 | .abort = drive_backup_abort, | |
1266 | }, | |
78b18b78 SH |
1267 | [TRANSACTION_ACTION_KIND_ABORT] = { |
1268 | .instance_size = sizeof(BlkTransactionState), | |
1269 | .prepare = abort_prepare, | |
1270 | .commit = abort_commit, | |
1271 | }, | |
bbe86010 WX |
1272 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
1273 | .instance_size = sizeof(InternalSnapshotState), | |
1274 | .prepare = internal_snapshot_prepare, | |
1275 | .abort = internal_snapshot_abort, | |
1276 | }, | |
ba0c86a3 WX |
1277 | }; |
1278 | ||
8802d1fd JC |
1279 | /* |
1280 | * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail | |
1281 | * then we do not pivot any of the devices in the group, and abandon the | |
1282 | * snapshots | |
1283 | */ | |
c8a83e85 | 1284 | void qmp_transaction(TransactionActionList *dev_list, Error **errp) |
8802d1fd | 1285 | { |
c8a83e85 | 1286 | TransactionActionList *dev_entry = dev_list; |
ba5d6ab6 | 1287 | BlkTransactionState *state, *next; |
43e17041 | 1288 | Error *local_err = NULL; |
8802d1fd | 1289 | |
ba5d6ab6 | 1290 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; |
8802d1fd JC |
1291 | QSIMPLEQ_INIT(&snap_bdrv_states); |
1292 | ||
1293 | /* drain all i/o before any snapshots */ | |
1294 | bdrv_drain_all(); | |
1295 | ||
1296 | /* We don't do anything in this loop that commits us to the snapshot */ | |
1297 | while (NULL != dev_entry) { | |
c8a83e85 | 1298 | TransactionAction *dev_info = NULL; |
ba0c86a3 | 1299 | const BdrvActionOps *ops; |
52e7c241 | 1300 | |
8802d1fd JC |
1301 | dev_info = dev_entry->value; |
1302 | dev_entry = dev_entry->next; | |
1303 | ||
ba0c86a3 WX |
1304 | assert(dev_info->kind < ARRAY_SIZE(actions)); |
1305 | ||
1306 | ops = &actions[dev_info->kind]; | |
aa3fe714 HR |
1307 | assert(ops->instance_size > 0); |
1308 | ||
ba5d6ab6 SH |
1309 | state = g_malloc0(ops->instance_size); |
1310 | state->ops = ops; | |
1311 | state->action = dev_info; | |
1312 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); | |
8802d1fd | 1313 | |
ba5d6ab6 | 1314 | state->ops->prepare(state, &local_err); |
ba0c86a3 WX |
1315 | if (error_is_set(&local_err)) { |
1316 | error_propagate(errp, local_err); | |
1317 | goto delete_and_fail; | |
8802d1fd | 1318 | } |
8802d1fd JC |
1319 | } |
1320 | ||
ba5d6ab6 | 1321 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
1322 | if (state->ops->commit) { |
1323 | state->ops->commit(state); | |
1324 | } | |
8802d1fd JC |
1325 | } |
1326 | ||
1327 | /* success */ | |
1328 | goto exit; | |
1329 | ||
1330 | delete_and_fail: | |
1331 | /* | |
1332 | * failure, and it is all-or-none; abandon each new bs, and keep using | |
1333 | * the original bs for all images | |
1334 | */ | |
ba5d6ab6 SH |
1335 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
1336 | if (state->ops->abort) { | |
1337 | state->ops->abort(state); | |
ba0c86a3 | 1338 | } |
8802d1fd JC |
1339 | } |
1340 | exit: | |
ba5d6ab6 SH |
1341 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
1342 | if (state->ops->clean) { | |
1343 | state->ops->clean(state); | |
ba0c86a3 | 1344 | } |
ba5d6ab6 | 1345 | g_free(state); |
8802d1fd | 1346 | } |
8802d1fd JC |
1347 | } |
1348 | ||
1349 | ||
92d48558 | 1350 | static void eject_device(BlockDriverState *bs, int force, Error **errp) |
666daa68 | 1351 | { |
2d3735d3 SH |
1352 | if (bdrv_in_use(bs)) { |
1353 | error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); | |
1354 | return; | |
1355 | } | |
2c6942fa | 1356 | if (!bdrv_dev_has_removable_media(bs)) { |
92d48558 LC |
1357 | error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs)); |
1358 | return; | |
ea8f942f | 1359 | } |
92d48558 | 1360 | |
025ccaa7 PB |
1361 | if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) { |
1362 | bdrv_dev_eject_request(bs, force); | |
1363 | if (!force) { | |
92d48558 LC |
1364 | error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); |
1365 | return; | |
025ccaa7 | 1366 | } |
666daa68 | 1367 | } |
92d48558 | 1368 | |
3b5276b5 | 1369 | bdrv_close(bs); |
666daa68 MA |
1370 | } |
1371 | ||
c245b6a3 | 1372 | void qmp_eject(const char *device, bool has_force, bool force, Error **errp) |
666daa68 MA |
1373 | { |
1374 | BlockDriverState *bs; | |
666daa68 | 1375 | |
c245b6a3 | 1376 | bs = bdrv_find(device); |
666daa68 | 1377 | if (!bs) { |
c245b6a3 LC |
1378 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1379 | return; | |
92d48558 LC |
1380 | } |
1381 | ||
c245b6a3 | 1382 | eject_device(bs, force, errp); |
666daa68 MA |
1383 | } |
1384 | ||
a4dea8a9 | 1385 | void qmp_block_passwd(const char *device, const char *password, Error **errp) |
666daa68 MA |
1386 | { |
1387 | BlockDriverState *bs; | |
1388 | int err; | |
1389 | ||
a4dea8a9 | 1390 | bs = bdrv_find(device); |
666daa68 | 1391 | if (!bs) { |
a4dea8a9 LC |
1392 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1393 | return; | |
666daa68 MA |
1394 | } |
1395 | ||
a4dea8a9 | 1396 | err = bdrv_set_key(bs, password); |
666daa68 | 1397 | if (err == -EINVAL) { |
a4dea8a9 LC |
1398 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
1399 | return; | |
666daa68 | 1400 | } else if (err < 0) { |
a4dea8a9 LC |
1401 | error_set(errp, QERR_INVALID_PASSWORD); |
1402 | return; | |
666daa68 | 1403 | } |
666daa68 MA |
1404 | } |
1405 | ||
333a96ec LC |
1406 | static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, |
1407 | int bdrv_flags, BlockDriver *drv, | |
1408 | const char *password, Error **errp) | |
1409 | { | |
34b5d2c6 | 1410 | Error *local_err = NULL; |
0eef407c LC |
1411 | int ret; |
1412 | ||
34b5d2c6 | 1413 | ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err); |
0eef407c | 1414 | if (ret < 0) { |
34b5d2c6 | 1415 | error_propagate(errp, local_err); |
333a96ec LC |
1416 | return; |
1417 | } | |
1418 | ||
1419 | if (bdrv_key_required(bs)) { | |
1420 | if (password) { | |
1421 | if (bdrv_set_key(bs, password) < 0) { | |
1422 | error_set(errp, QERR_INVALID_PASSWORD); | |
1423 | } | |
1424 | } else { | |
1425 | error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), | |
1426 | bdrv_get_encrypted_filename(bs)); | |
1427 | } | |
1428 | } else if (password) { | |
1429 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | void qmp_change_blockdev(const char *device, const char *filename, | |
1434 | bool has_format, const char *format, Error **errp) | |
666daa68 MA |
1435 | { |
1436 | BlockDriverState *bs; | |
1437 | BlockDriver *drv = NULL; | |
1438 | int bdrv_flags; | |
92d48558 | 1439 | Error *err = NULL; |
666daa68 MA |
1440 | |
1441 | bs = bdrv_find(device); | |
1442 | if (!bs) { | |
333a96ec LC |
1443 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1444 | return; | |
666daa68 | 1445 | } |
333a96ec LC |
1446 | |
1447 | if (format) { | |
b64ec4e4 | 1448 | drv = bdrv_find_whitelisted_format(format, bs->read_only); |
666daa68 | 1449 | if (!drv) { |
333a96ec LC |
1450 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
1451 | return; | |
666daa68 MA |
1452 | } |
1453 | } | |
333a96ec | 1454 | |
92d48558 LC |
1455 | eject_device(bs, 0, &err); |
1456 | if (error_is_set(&err)) { | |
333a96ec LC |
1457 | error_propagate(errp, err); |
1458 | return; | |
666daa68 | 1459 | } |
333a96ec | 1460 | |
528f7663 | 1461 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
199630b6 | 1462 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
333a96ec LC |
1463 | |
1464 | qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); | |
666daa68 | 1465 | } |
9063f814 | 1466 | |
727f005e | 1467 | /* throttling disk I/O limits */ |
80047da5 | 1468 | void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, |
3e9fab69 BC |
1469 | int64_t bps_wr, |
1470 | int64_t iops, | |
1471 | int64_t iops_rd, | |
1472 | int64_t iops_wr, | |
1473 | bool has_bps_max, | |
1474 | int64_t bps_max, | |
1475 | bool has_bps_rd_max, | |
1476 | int64_t bps_rd_max, | |
1477 | bool has_bps_wr_max, | |
1478 | int64_t bps_wr_max, | |
1479 | bool has_iops_max, | |
1480 | int64_t iops_max, | |
1481 | bool has_iops_rd_max, | |
1482 | int64_t iops_rd_max, | |
1483 | bool has_iops_wr_max, | |
2024c1df BC |
1484 | int64_t iops_wr_max, |
1485 | bool has_iops_size, | |
1486 | int64_t iops_size, Error **errp) | |
727f005e | 1487 | { |
cc0681c4 | 1488 | ThrottleConfig cfg; |
727f005e ZYW |
1489 | BlockDriverState *bs; |
1490 | ||
80047da5 | 1491 | bs = bdrv_find(device); |
727f005e | 1492 | if (!bs) { |
80047da5 LC |
1493 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1494 | return; | |
727f005e ZYW |
1495 | } |
1496 | ||
cc0681c4 BC |
1497 | memset(&cfg, 0, sizeof(cfg)); |
1498 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; | |
1499 | cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; | |
1500 | cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; | |
1501 | ||
1502 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; | |
1503 | cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; | |
1504 | cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; | |
1505 | ||
3e9fab69 BC |
1506 | if (has_bps_max) { |
1507 | cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; | |
1508 | } | |
1509 | if (has_bps_rd_max) { | |
1510 | cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; | |
1511 | } | |
1512 | if (has_bps_wr_max) { | |
1513 | cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; | |
1514 | } | |
1515 | if (has_iops_max) { | |
1516 | cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; | |
1517 | } | |
1518 | if (has_iops_rd_max) { | |
1519 | cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; | |
1520 | } | |
1521 | if (has_iops_wr_max) { | |
1522 | cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; | |
1523 | } | |
727f005e | 1524 | |
2024c1df BC |
1525 | if (has_iops_size) { |
1526 | cfg.op_size = iops_size; | |
1527 | } | |
cc0681c4 BC |
1528 | |
1529 | if (!check_throttle_config(&cfg, errp)) { | |
80047da5 | 1530 | return; |
727f005e ZYW |
1531 | } |
1532 | ||
cc0681c4 | 1533 | if (!bs->io_limits_enabled && throttle_enabled(&cfg)) { |
727f005e | 1534 | bdrv_io_limits_enable(bs); |
cc0681c4 | 1535 | } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) { |
727f005e | 1536 | bdrv_io_limits_disable(bs); |
cc0681c4 BC |
1537 | } |
1538 | ||
1539 | if (bs->io_limits_enabled) { | |
1540 | bdrv_set_io_limits(bs, &cfg); | |
727f005e | 1541 | } |
727f005e ZYW |
1542 | } |
1543 | ||
9063f814 RH |
1544 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1545 | { | |
1546 | const char *id = qdict_get_str(qdict, "id"); | |
1547 | BlockDriverState *bs; | |
9063f814 RH |
1548 | |
1549 | bs = bdrv_find(id); | |
1550 | if (!bs) { | |
1551 | qerror_report(QERR_DEVICE_NOT_FOUND, id); | |
1552 | return -1; | |
1553 | } | |
8591675f MT |
1554 | if (bdrv_in_use(bs)) { |
1555 | qerror_report(QERR_DEVICE_IN_USE, id); | |
1556 | return -1; | |
1557 | } | |
9063f814 RH |
1558 | |
1559 | /* quiesce block driver; prevent further io */ | |
922453bc | 1560 | bdrv_drain_all(); |
9063f814 RH |
1561 | bdrv_flush(bs); |
1562 | bdrv_close(bs); | |
1563 | ||
fa879d62 | 1564 | /* if we have a device attached to this BlockDriverState |
d22b2f41 RH |
1565 | * then we need to make the drive anonymous until the device |
1566 | * can be removed. If this is a drive with no device backing | |
1567 | * then we can just get rid of the block driver state right here. | |
1568 | */ | |
fa879d62 | 1569 | if (bdrv_get_attached_dev(bs)) { |
d22b2f41 | 1570 | bdrv_make_anon(bs); |
293c51a6 SH |
1571 | |
1572 | /* Further I/O must not pause the guest */ | |
1573 | bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT, | |
1574 | BLOCKDEV_ON_ERROR_REPORT); | |
d22b2f41 RH |
1575 | } else { |
1576 | drive_uninit(drive_get_by_blockdev(bs)); | |
9063f814 RH |
1577 | } |
1578 | ||
9063f814 RH |
1579 | return 0; |
1580 | } | |
6d4a2b3a | 1581 | |
5e7caacb | 1582 | void qmp_block_resize(const char *device, int64_t size, Error **errp) |
6d4a2b3a | 1583 | { |
6d4a2b3a | 1584 | BlockDriverState *bs; |
8732901e | 1585 | int ret; |
6d4a2b3a CH |
1586 | |
1587 | bs = bdrv_find(device); | |
1588 | if (!bs) { | |
5e7caacb LC |
1589 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1590 | return; | |
6d4a2b3a CH |
1591 | } |
1592 | ||
1593 | if (size < 0) { | |
939a1cc3 | 1594 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
5e7caacb | 1595 | return; |
6d4a2b3a CH |
1596 | } |
1597 | ||
92b7a08d PL |
1598 | /* complete all in-flight operations before resizing the device */ |
1599 | bdrv_drain_all(); | |
1600 | ||
8732901e KW |
1601 | ret = bdrv_truncate(bs, size); |
1602 | switch (ret) { | |
939a1cc3 SH |
1603 | case 0: |
1604 | break; | |
1605 | case -ENOMEDIUM: | |
1606 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1607 | break; | |
1608 | case -ENOTSUP: | |
1609 | error_set(errp, QERR_UNSUPPORTED); | |
1610 | break; | |
1611 | case -EACCES: | |
1612 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); | |
1613 | break; | |
1614 | case -EBUSY: | |
1615 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
1616 | break; | |
1617 | default: | |
8732901e | 1618 | error_setg_errno(errp, -ret, "Could not resize"); |
939a1cc3 | 1619 | break; |
6d4a2b3a | 1620 | } |
6d4a2b3a | 1621 | } |
12bd451f | 1622 | |
9abf2dba | 1623 | static void block_job_cb(void *opaque, int ret) |
12bd451f SH |
1624 | { |
1625 | BlockDriverState *bs = opaque; | |
1626 | QObject *obj; | |
1627 | ||
9abf2dba | 1628 | trace_block_job_cb(bs, bs->job, ret); |
12bd451f SH |
1629 | |
1630 | assert(bs->job); | |
1631 | obj = qobject_from_block_job(bs->job); | |
1632 | if (ret < 0) { | |
1633 | QDict *dict = qobject_to_qdict(obj); | |
1634 | qdict_put(dict, "error", qstring_from_str(strerror(-ret))); | |
1635 | } | |
1636 | ||
370521a1 SH |
1637 | if (block_job_is_cancelled(bs->job)) { |
1638 | monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj); | |
1639 | } else { | |
1640 | monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj); | |
1641 | } | |
12bd451f | 1642 | qobject_decref(obj); |
aa398a5c | 1643 | |
fa510ebf | 1644 | bdrv_put_ref_bh_schedule(bs); |
12bd451f SH |
1645 | } |
1646 | ||
1647 | void qmp_block_stream(const char *device, bool has_base, | |
1d809098 PB |
1648 | const char *base, bool has_speed, int64_t speed, |
1649 | bool has_on_error, BlockdevOnError on_error, | |
1650 | Error **errp) | |
12bd451f SH |
1651 | { |
1652 | BlockDriverState *bs; | |
c8c3080f | 1653 | BlockDriverState *base_bs = NULL; |
fd7f8c65 | 1654 | Error *local_err = NULL; |
12bd451f | 1655 | |
1d809098 PB |
1656 | if (!has_on_error) { |
1657 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
1658 | } | |
1659 | ||
12bd451f SH |
1660 | bs = bdrv_find(device); |
1661 | if (!bs) { | |
1662 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1663 | return; | |
1664 | } | |
1665 | ||
12bd451f | 1666 | if (base) { |
c8c3080f MT |
1667 | base_bs = bdrv_find_backing_image(bs, base); |
1668 | if (base_bs == NULL) { | |
1669 | error_set(errp, QERR_BASE_NOT_FOUND, base); | |
1670 | return; | |
1671 | } | |
12bd451f SH |
1672 | } |
1673 | ||
c83c66c3 | 1674 | stream_start(bs, base_bs, base, has_speed ? speed : 0, |
1d809098 | 1675 | on_error, block_job_cb, bs, &local_err); |
fd7f8c65 SH |
1676 | if (error_is_set(&local_err)) { |
1677 | error_propagate(errp, local_err); | |
1678 | return; | |
12bd451f SH |
1679 | } |
1680 | ||
1681 | trace_qmp_block_stream(bs, bs->job); | |
1682 | } | |
2d47c6e9 | 1683 | |
ed61fc10 JC |
1684 | void qmp_block_commit(const char *device, |
1685 | bool has_base, const char *base, const char *top, | |
1686 | bool has_speed, int64_t speed, | |
1687 | Error **errp) | |
1688 | { | |
1689 | BlockDriverState *bs; | |
1690 | BlockDriverState *base_bs, *top_bs; | |
1691 | Error *local_err = NULL; | |
1692 | /* This will be part of the QMP command, if/when the | |
1693 | * BlockdevOnError change for blkmirror makes it in | |
1694 | */ | |
92aa5c6d | 1695 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
ed61fc10 JC |
1696 | |
1697 | /* drain all i/o before commits */ | |
1698 | bdrv_drain_all(); | |
1699 | ||
1700 | bs = bdrv_find(device); | |
1701 | if (!bs) { | |
1702 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1703 | return; | |
1704 | } | |
ed61fc10 JC |
1705 | |
1706 | /* default top_bs is the active layer */ | |
1707 | top_bs = bs; | |
1708 | ||
1709 | if (top) { | |
1710 | if (strcmp(bs->filename, top) != 0) { | |
1711 | top_bs = bdrv_find_backing_image(bs, top); | |
1712 | } | |
1713 | } | |
1714 | ||
1715 | if (top_bs == NULL) { | |
1716 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
1717 | return; | |
1718 | } | |
1719 | ||
d5208c45 JC |
1720 | if (has_base && base) { |
1721 | base_bs = bdrv_find_backing_image(top_bs, base); | |
1722 | } else { | |
1723 | base_bs = bdrv_find_base(top_bs); | |
1724 | } | |
1725 | ||
1726 | if (base_bs == NULL) { | |
1727 | error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); | |
1728 | return; | |
1729 | } | |
1730 | ||
ed61fc10 JC |
1731 | commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, |
1732 | &local_err); | |
1733 | if (local_err != NULL) { | |
1734 | error_propagate(errp, local_err); | |
1735 | return; | |
1736 | } | |
ed61fc10 JC |
1737 | } |
1738 | ||
99a9addf SH |
1739 | void qmp_drive_backup(const char *device, const char *target, |
1740 | bool has_format, const char *format, | |
b53169ea | 1741 | enum MirrorSyncMode sync, |
99a9addf SH |
1742 | bool has_mode, enum NewImageMode mode, |
1743 | bool has_speed, int64_t speed, | |
1744 | bool has_on_source_error, BlockdevOnError on_source_error, | |
1745 | bool has_on_target_error, BlockdevOnError on_target_error, | |
1746 | Error **errp) | |
1747 | { | |
1748 | BlockDriverState *bs; | |
1749 | BlockDriverState *target_bs; | |
fc5d3f84 | 1750 | BlockDriverState *source = NULL; |
99a9addf SH |
1751 | BlockDriver *drv = NULL; |
1752 | Error *local_err = NULL; | |
1753 | int flags; | |
1754 | int64_t size; | |
1755 | int ret; | |
1756 | ||
1757 | if (!has_speed) { | |
1758 | speed = 0; | |
1759 | } | |
1760 | if (!has_on_source_error) { | |
1761 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
1762 | } | |
1763 | if (!has_on_target_error) { | |
1764 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
1765 | } | |
1766 | if (!has_mode) { | |
1767 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1768 | } | |
1769 | ||
1770 | bs = bdrv_find(device); | |
1771 | if (!bs) { | |
1772 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1773 | return; | |
1774 | } | |
1775 | ||
1776 | if (!bdrv_is_inserted(bs)) { | |
1777 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1778 | return; | |
1779 | } | |
1780 | ||
1781 | if (!has_format) { | |
1782 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
1783 | } | |
1784 | if (format) { | |
1785 | drv = bdrv_find_format(format); | |
1786 | if (!drv) { | |
1787 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
1788 | return; | |
1789 | } | |
1790 | } | |
1791 | ||
1792 | if (bdrv_in_use(bs)) { | |
1793 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
1794 | return; | |
1795 | } | |
1796 | ||
1797 | flags = bs->open_flags | BDRV_O_RDWR; | |
1798 | ||
fc5d3f84 IM |
1799 | /* See if we have a backing HD we can use to create our new image |
1800 | * on top of. */ | |
1801 | if (sync == MIRROR_SYNC_MODE_TOP) { | |
1802 | source = bs->backing_hd; | |
1803 | if (!source) { | |
1804 | sync = MIRROR_SYNC_MODE_FULL; | |
1805 | } | |
1806 | } | |
1807 | if (sync == MIRROR_SYNC_MODE_NONE) { | |
1808 | source = bs; | |
1809 | } | |
1810 | ||
99a9addf SH |
1811 | size = bdrv_getlength(bs); |
1812 | if (size < 0) { | |
1813 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
1814 | return; | |
1815 | } | |
1816 | ||
1817 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1818 | assert(format && drv); | |
fc5d3f84 IM |
1819 | if (source) { |
1820 | bdrv_img_create(target, format, source->filename, | |
1821 | source->drv->format_name, NULL, | |
1822 | size, flags, &local_err, false); | |
1823 | } else { | |
1824 | bdrv_img_create(target, format, NULL, NULL, NULL, | |
1825 | size, flags, &local_err, false); | |
1826 | } | |
99a9addf SH |
1827 | } |
1828 | ||
1829 | if (error_is_set(&local_err)) { | |
1830 | error_propagate(errp, local_err); | |
1831 | return; | |
1832 | } | |
1833 | ||
1834 | target_bs = bdrv_new(""); | |
34b5d2c6 | 1835 | ret = bdrv_open(target_bs, target, NULL, flags, drv, &local_err); |
99a9addf | 1836 | if (ret < 0) { |
4f6fd349 | 1837 | bdrv_unref(target_bs); |
34b5d2c6 | 1838 | error_propagate(errp, local_err); |
99a9addf SH |
1839 | return; |
1840 | } | |
1841 | ||
fc5d3f84 | 1842 | backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error, |
99a9addf SH |
1843 | block_job_cb, bs, &local_err); |
1844 | if (local_err != NULL) { | |
4f6fd349 | 1845 | bdrv_unref(target_bs); |
99a9addf SH |
1846 | error_propagate(errp, local_err); |
1847 | return; | |
1848 | } | |
99a9addf SH |
1849 | } |
1850 | ||
08e4ed6c PB |
1851 | #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) |
1852 | ||
d9b902db PB |
1853 | void qmp_drive_mirror(const char *device, const char *target, |
1854 | bool has_format, const char *format, | |
1855 | enum MirrorSyncMode sync, | |
1856 | bool has_mode, enum NewImageMode mode, | |
b952b558 | 1857 | bool has_speed, int64_t speed, |
eee13dfe | 1858 | bool has_granularity, uint32_t granularity, |
08e4ed6c | 1859 | bool has_buf_size, int64_t buf_size, |
b952b558 PB |
1860 | bool has_on_source_error, BlockdevOnError on_source_error, |
1861 | bool has_on_target_error, BlockdevOnError on_target_error, | |
1862 | Error **errp) | |
d9b902db | 1863 | { |
d9b902db PB |
1864 | BlockDriverState *bs; |
1865 | BlockDriverState *source, *target_bs; | |
d9b902db PB |
1866 | BlockDriver *drv = NULL; |
1867 | Error *local_err = NULL; | |
1868 | int flags; | |
ac3c5d83 | 1869 | int64_t size; |
d9b902db PB |
1870 | int ret; |
1871 | ||
1872 | if (!has_speed) { | |
1873 | speed = 0; | |
1874 | } | |
b952b558 PB |
1875 | if (!has_on_source_error) { |
1876 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
1877 | } | |
1878 | if (!has_on_target_error) { | |
1879 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
1880 | } | |
d9b902db PB |
1881 | if (!has_mode) { |
1882 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
1883 | } | |
eee13dfe PB |
1884 | if (!has_granularity) { |
1885 | granularity = 0; | |
1886 | } | |
08e4ed6c PB |
1887 | if (!has_buf_size) { |
1888 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
1889 | } | |
1890 | ||
eee13dfe PB |
1891 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
1892 | error_set(errp, QERR_INVALID_PARAMETER, device); | |
1893 | return; | |
1894 | } | |
1895 | if (granularity & (granularity - 1)) { | |
1896 | error_set(errp, QERR_INVALID_PARAMETER, device); | |
1897 | return; | |
1898 | } | |
d9b902db PB |
1899 | |
1900 | bs = bdrv_find(device); | |
1901 | if (!bs) { | |
1902 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1903 | return; | |
1904 | } | |
1905 | ||
1906 | if (!bdrv_is_inserted(bs)) { | |
1907 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1908 | return; | |
1909 | } | |
1910 | ||
1911 | if (!has_format) { | |
1912 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
1913 | } | |
1914 | if (format) { | |
1915 | drv = bdrv_find_format(format); | |
1916 | if (!drv) { | |
1917 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
1918 | return; | |
1919 | } | |
1920 | } | |
1921 | ||
1922 | if (bdrv_in_use(bs)) { | |
1923 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
1924 | return; | |
1925 | } | |
1926 | ||
1927 | flags = bs->open_flags | BDRV_O_RDWR; | |
1928 | source = bs->backing_hd; | |
1929 | if (!source && sync == MIRROR_SYNC_MODE_TOP) { | |
1930 | sync = MIRROR_SYNC_MODE_FULL; | |
1931 | } | |
1932 | ||
ac3c5d83 SH |
1933 | size = bdrv_getlength(bs); |
1934 | if (size < 0) { | |
1935 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
1936 | return; | |
1937 | } | |
1938 | ||
d9b902db PB |
1939 | if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) { |
1940 | /* create new image w/o backing file */ | |
1941 | assert(format && drv); | |
cf8f2426 | 1942 | bdrv_img_create(target, format, |
f382d43a | 1943 | NULL, NULL, NULL, size, flags, &local_err, false); |
d9b902db PB |
1944 | } else { |
1945 | switch (mode) { | |
1946 | case NEW_IMAGE_MODE_EXISTING: | |
d9b902db PB |
1947 | break; |
1948 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
1949 | /* create new image with backing file */ | |
cf8f2426 LC |
1950 | bdrv_img_create(target, format, |
1951 | source->filename, | |
1952 | source->drv->format_name, | |
f382d43a | 1953 | NULL, size, flags, &local_err, false); |
d9b902db PB |
1954 | break; |
1955 | default: | |
1956 | abort(); | |
1957 | } | |
1958 | } | |
1959 | ||
cf8f2426 LC |
1960 | if (error_is_set(&local_err)) { |
1961 | error_propagate(errp, local_err); | |
d9b902db PB |
1962 | return; |
1963 | } | |
1964 | ||
b812f671 PB |
1965 | /* Mirroring takes care of copy-on-write using the source's backing |
1966 | * file. | |
1967 | */ | |
d9b902db | 1968 | target_bs = bdrv_new(""); |
34b5d2c6 HR |
1969 | ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv, |
1970 | &local_err); | |
d9b902db | 1971 | if (ret < 0) { |
4f6fd349 | 1972 | bdrv_unref(target_bs); |
34b5d2c6 | 1973 | error_propagate(errp, local_err); |
d9b902db PB |
1974 | return; |
1975 | } | |
1976 | ||
08e4ed6c | 1977 | mirror_start(bs, target_bs, speed, granularity, buf_size, sync, |
eee13dfe | 1978 | on_source_error, on_target_error, |
b952b558 | 1979 | block_job_cb, bs, &local_err); |
d9b902db | 1980 | if (local_err != NULL) { |
4f6fd349 | 1981 | bdrv_unref(target_bs); |
d9b902db PB |
1982 | error_propagate(errp, local_err); |
1983 | return; | |
1984 | } | |
d9b902db PB |
1985 | } |
1986 | ||
2d47c6e9 SH |
1987 | static BlockJob *find_block_job(const char *device) |
1988 | { | |
1989 | BlockDriverState *bs; | |
1990 | ||
1991 | bs = bdrv_find(device); | |
1992 | if (!bs || !bs->job) { | |
1993 | return NULL; | |
1994 | } | |
1995 | return bs->job; | |
1996 | } | |
1997 | ||
882ec7ce | 1998 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 SH |
1999 | { |
2000 | BlockJob *job = find_block_job(device); | |
2001 | ||
2002 | if (!job) { | |
7ef15070 | 2003 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
2d47c6e9 SH |
2004 | return; |
2005 | } | |
2006 | ||
882ec7ce | 2007 | block_job_set_speed(job, speed, errp); |
2d47c6e9 | 2008 | } |
370521a1 | 2009 | |
6e37fb81 PB |
2010 | void qmp_block_job_cancel(const char *device, |
2011 | bool has_force, bool force, Error **errp) | |
370521a1 SH |
2012 | { |
2013 | BlockJob *job = find_block_job(device); | |
2014 | ||
6e37fb81 PB |
2015 | if (!has_force) { |
2016 | force = false; | |
2017 | } | |
2018 | ||
370521a1 | 2019 | if (!job) { |
7ef15070 | 2020 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
370521a1 SH |
2021 | return; |
2022 | } | |
6e37fb81 | 2023 | if (job->paused && !force) { |
8acc72a4 PB |
2024 | error_set(errp, QERR_BLOCK_JOB_PAUSED, device); |
2025 | return; | |
2026 | } | |
370521a1 SH |
2027 | |
2028 | trace_qmp_block_job_cancel(job); | |
2029 | block_job_cancel(job); | |
2030 | } | |
fb5458cd | 2031 | |
6e37fb81 PB |
2032 | void qmp_block_job_pause(const char *device, Error **errp) |
2033 | { | |
2034 | BlockJob *job = find_block_job(device); | |
2035 | ||
2036 | if (!job) { | |
2037 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2038 | return; | |
2039 | } | |
2040 | ||
2041 | trace_qmp_block_job_pause(job); | |
2042 | block_job_pause(job); | |
2043 | } | |
2044 | ||
2045 | void qmp_block_job_resume(const char *device, Error **errp) | |
2046 | { | |
2047 | BlockJob *job = find_block_job(device); | |
2048 | ||
2049 | if (!job) { | |
2050 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2051 | return; | |
2052 | } | |
2053 | ||
2054 | trace_qmp_block_job_resume(job); | |
2055 | block_job_resume(job); | |
2056 | } | |
2057 | ||
aeae883b PB |
2058 | void qmp_block_job_complete(const char *device, Error **errp) |
2059 | { | |
2060 | BlockJob *job = find_block_job(device); | |
2061 | ||
2062 | if (!job) { | |
2063 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2064 | return; | |
2065 | } | |
2066 | ||
2067 | trace_qmp_block_job_complete(job); | |
2068 | block_job_complete(job, errp); | |
2069 | } | |
2070 | ||
d26c9a15 KW |
2071 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
2072 | { | |
2073 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
2074 | QObject *obj; | |
2075 | QDict *qdict; | |
2076 | DriveInfo *dinfo; | |
2077 | Error *local_err = NULL; | |
2078 | ||
2079 | /* Require an ID in the top level */ | |
2080 | if (!options->has_id) { | |
2081 | error_setg(errp, "Block device needs an ID"); | |
2082 | goto fail; | |
2083 | } | |
2084 | ||
2085 | /* TODO Sort it out in raw-posix and drive_init: Reject aio=native with | |
2086 | * cache.direct=false instead of silently switching to aio=threads, except | |
2087 | * if called from drive_init. | |
2088 | * | |
2089 | * For now, simply forbidding the combination for all drivers will do. */ | |
2090 | if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { | |
2091 | bool direct = options->cache->has_direct && options->cache->direct; | |
2092 | if (!options->has_cache && !direct) { | |
2093 | error_setg(errp, "aio=native requires cache.direct=true"); | |
2094 | goto fail; | |
2095 | } | |
2096 | } | |
2097 | ||
2098 | visit_type_BlockdevOptions(qmp_output_get_visitor(ov), | |
2099 | &options, NULL, &local_err); | |
2100 | if (error_is_set(&local_err)) { | |
2101 | error_propagate(errp, local_err); | |
2102 | goto fail; | |
2103 | } | |
2104 | ||
2105 | obj = qmp_output_get_qobject(ov); | |
2106 | qdict = qobject_to_qdict(obj); | |
2107 | ||
2108 | qdict_flatten(qdict); | |
2109 | ||
2110 | QemuOpts *opts = qemu_opts_from_qdict(&qemu_drive_opts, qdict, &local_err); | |
2111 | if (error_is_set(&local_err)) { | |
2112 | error_propagate(errp, local_err); | |
2113 | goto fail; | |
2114 | } | |
2115 | ||
2116 | dinfo = blockdev_init(opts, IF_NONE); | |
2117 | if (!dinfo) { | |
2118 | error_setg(errp, "Could not open image"); | |
2119 | goto fail; | |
2120 | } | |
2121 | ||
2122 | fail: | |
2123 | qmp_output_visitor_cleanup(ov); | |
2124 | } | |
2125 | ||
fb5458cd SH |
2126 | static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs) |
2127 | { | |
2128 | BlockJobInfoList **prev = opaque; | |
2129 | BlockJob *job = bs->job; | |
2130 | ||
2131 | if (job) { | |
30e628b7 PB |
2132 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); |
2133 | elem->value = block_job_query(bs->job); | |
fb5458cd SH |
2134 | (*prev)->next = elem; |
2135 | *prev = elem; | |
2136 | } | |
2137 | } | |
2138 | ||
2139 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) | |
2140 | { | |
2141 | /* Dummy is a fake list element for holding the head pointer */ | |
2142 | BlockJobInfoList dummy = {}; | |
2143 | BlockJobInfoList *prev = &dummy; | |
2144 | bdrv_iterate(do_qmp_query_block_jobs_one, &prev); | |
2145 | return dummy.next; | |
2146 | } | |
4d454574 | 2147 | |
0006383e | 2148 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 2149 | .name = "drive", |
0006383e | 2150 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
2151 | .desc = { |
2152 | { | |
2153 | .name = "bus", | |
2154 | .type = QEMU_OPT_NUMBER, | |
2155 | .help = "bus number", | |
2156 | },{ | |
2157 | .name = "unit", | |
2158 | .type = QEMU_OPT_NUMBER, | |
2159 | .help = "unit number (i.e. lun for scsi)", | |
2160 | },{ | |
2161 | .name = "if", | |
2162 | .type = QEMU_OPT_STRING, | |
2163 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
2164 | },{ | |
2165 | .name = "index", | |
2166 | .type = QEMU_OPT_NUMBER, | |
2167 | .help = "index number", | |
2168 | },{ | |
2169 | .name = "cyls", | |
2170 | .type = QEMU_OPT_NUMBER, | |
2171 | .help = "number of cylinders (ide disk geometry)", | |
2172 | },{ | |
2173 | .name = "heads", | |
2174 | .type = QEMU_OPT_NUMBER, | |
2175 | .help = "number of heads (ide disk geometry)", | |
2176 | },{ | |
2177 | .name = "secs", | |
2178 | .type = QEMU_OPT_NUMBER, | |
2179 | .help = "number of sectors (ide disk geometry)", | |
2180 | },{ | |
2181 | .name = "trans", | |
2182 | .type = QEMU_OPT_STRING, | |
2183 | .help = "chs translation (auto, lba. none)", | |
2184 | },{ | |
2185 | .name = "media", | |
2186 | .type = QEMU_OPT_STRING, | |
2187 | .help = "media type (disk, cdrom)", | |
2188 | },{ | |
2189 | .name = "snapshot", | |
2190 | .type = QEMU_OPT_BOOL, | |
2191 | .help = "enable/disable snapshot mode", | |
2192 | },{ | |
2193 | .name = "file", | |
2194 | .type = QEMU_OPT_STRING, | |
2195 | .help = "disk image", | |
a9384aff PB |
2196 | },{ |
2197 | .name = "discard", | |
2198 | .type = QEMU_OPT_STRING, | |
2199 | .help = "discard operation (ignore/off, unmap/on)", | |
4d454574 | 2200 | },{ |
29c4e2b5 KW |
2201 | .name = "cache.writeback", |
2202 | .type = QEMU_OPT_BOOL, | |
2203 | .help = "enables writeback mode for any caches", | |
2204 | },{ | |
2205 | .name = "cache.direct", | |
2206 | .type = QEMU_OPT_BOOL, | |
2207 | .help = "enables use of O_DIRECT (bypass the host page cache)", | |
2208 | },{ | |
2209 | .name = "cache.no-flush", | |
2210 | .type = QEMU_OPT_BOOL, | |
2211 | .help = "ignore any flush requests for the device", | |
4d454574 PB |
2212 | },{ |
2213 | .name = "aio", | |
2214 | .type = QEMU_OPT_STRING, | |
2215 | .help = "host AIO implementation (threads, native)", | |
2216 | },{ | |
2217 | .name = "format", | |
2218 | .type = QEMU_OPT_STRING, | |
2219 | .help = "disk format (raw, qcow2, ...)", | |
2220 | },{ | |
2221 | .name = "serial", | |
2222 | .type = QEMU_OPT_STRING, | |
2223 | .help = "disk serial number", | |
2224 | },{ | |
2225 | .name = "rerror", | |
2226 | .type = QEMU_OPT_STRING, | |
2227 | .help = "read error action", | |
2228 | },{ | |
2229 | .name = "werror", | |
2230 | .type = QEMU_OPT_STRING, | |
2231 | .help = "write error action", | |
2232 | },{ | |
2233 | .name = "addr", | |
2234 | .type = QEMU_OPT_STRING, | |
2235 | .help = "pci address (virtio only)", | |
2236 | },{ | |
0f227a94 | 2237 | .name = "read-only", |
4d454574 PB |
2238 | .type = QEMU_OPT_BOOL, |
2239 | .help = "open drive file as read-only", | |
2240 | },{ | |
57975222 | 2241 | .name = "throttling.iops-total", |
4d454574 PB |
2242 | .type = QEMU_OPT_NUMBER, |
2243 | .help = "limit total I/O operations per second", | |
2244 | },{ | |
57975222 | 2245 | .name = "throttling.iops-read", |
4d454574 PB |
2246 | .type = QEMU_OPT_NUMBER, |
2247 | .help = "limit read operations per second", | |
2248 | },{ | |
57975222 | 2249 | .name = "throttling.iops-write", |
4d454574 PB |
2250 | .type = QEMU_OPT_NUMBER, |
2251 | .help = "limit write operations per second", | |
2252 | },{ | |
57975222 | 2253 | .name = "throttling.bps-total", |
4d454574 PB |
2254 | .type = QEMU_OPT_NUMBER, |
2255 | .help = "limit total bytes per second", | |
2256 | },{ | |
57975222 | 2257 | .name = "throttling.bps-read", |
4d454574 PB |
2258 | .type = QEMU_OPT_NUMBER, |
2259 | .help = "limit read bytes per second", | |
2260 | },{ | |
57975222 | 2261 | .name = "throttling.bps-write", |
4d454574 PB |
2262 | .type = QEMU_OPT_NUMBER, |
2263 | .help = "limit write bytes per second", | |
3e9fab69 BC |
2264 | },{ |
2265 | .name = "throttling.iops-total-max", | |
2266 | .type = QEMU_OPT_NUMBER, | |
2267 | .help = "I/O operations burst", | |
2268 | },{ | |
2269 | .name = "throttling.iops-read-max", | |
2270 | .type = QEMU_OPT_NUMBER, | |
2271 | .help = "I/O operations read burst", | |
2272 | },{ | |
2273 | .name = "throttling.iops-write-max", | |
2274 | .type = QEMU_OPT_NUMBER, | |
2275 | .help = "I/O operations write burst", | |
2276 | },{ | |
2277 | .name = "throttling.bps-total-max", | |
2278 | .type = QEMU_OPT_NUMBER, | |
2279 | .help = "total bytes burst", | |
2280 | },{ | |
2281 | .name = "throttling.bps-read-max", | |
2282 | .type = QEMU_OPT_NUMBER, | |
2283 | .help = "total bytes read burst", | |
2284 | },{ | |
2285 | .name = "throttling.bps-write-max", | |
2286 | .type = QEMU_OPT_NUMBER, | |
2287 | .help = "total bytes write burst", | |
2024c1df BC |
2288 | },{ |
2289 | .name = "throttling.iops-size", | |
2290 | .type = QEMU_OPT_NUMBER, | |
2291 | .help = "when limiting by iops max size of an I/O in bytes", | |
4d454574 PB |
2292 | },{ |
2293 | .name = "copy-on-read", | |
2294 | .type = QEMU_OPT_BOOL, | |
2295 | .help = "copy read data from backing file into image file", | |
2296 | },{ | |
2297 | .name = "boot", | |
2298 | .type = QEMU_OPT_BOOL, | |
2299 | .help = "(deprecated, ignored)", | |
2300 | }, | |
2301 | { /* end of list */ } | |
2302 | }, | |
2303 | }; | |
0006383e KW |
2304 | |
2305 | QemuOptsList qemu_drive_opts = { | |
2306 | .name = "drive", | |
2307 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
2308 | .desc = { | |
492fdc6f KW |
2309 | /* |
2310 | * no elements => accept any params | |
2311 | * validation will happen later | |
2312 | */ | |
0006383e KW |
2313 | { /* end of list */ } |
2314 | }, | |
2315 | }; |