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