]>
Commit | Line | Data |
---|---|---|
666daa68 MA |
1 | /* |
2 | * QEMU host block devices | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
7 | * later. See the COPYING file in the top-level directory. | |
3618a094 MA |
8 | * |
9 | * This file incorporates work covered by the following copyright and | |
10 | * permission notice: | |
11 | * | |
12 | * Copyright (c) 2003-2008 Fabrice Bellard | |
13 | * | |
14 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
15 | * of this software and associated documentation files (the "Software"), to deal | |
16 | * in the Software without restriction, including without limitation the rights | |
17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
18 | * copies of the Software, and to permit persons to whom the Software is | |
19 | * furnished to do so, subject to the following conditions: | |
20 | * | |
21 | * The above copyright notice and this permission notice shall be included in | |
22 | * all copies or substantial portions of the Software. | |
23 | * | |
24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
27 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
30 | * THE SOFTWARE. | |
666daa68 MA |
31 | */ |
32 | ||
26f54e9a | 33 | #include "sysemu/block-backend.h" |
9c17d615 | 34 | #include "sysemu/blockdev.h" |
0d09e41a | 35 | #include "hw/block/block.h" |
737e150e | 36 | #include "block/blockjob.h" |
83c9089e | 37 | #include "monitor/monitor.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" | |
9e7dac7c | 43 | #include "qapi/util.h" |
9c17d615 | 44 | #include "sysemu/sysemu.h" |
737e150e | 45 | #include "block/block_int.h" |
a4dea8a9 | 46 | #include "qmp-commands.h" |
12bd451f | 47 | #include "trace.h" |
9c17d615 | 48 | #include "sysemu/arch_init.h" |
666daa68 | 49 | |
1960966d MA |
50 | static const char *const if_name[IF_COUNT] = { |
51 | [IF_NONE] = "none", | |
52 | [IF_IDE] = "ide", | |
53 | [IF_SCSI] = "scsi", | |
54 | [IF_FLOPPY] = "floppy", | |
55 | [IF_PFLASH] = "pflash", | |
56 | [IF_MTD] = "mtd", | |
57 | [IF_SD] = "sd", | |
58 | [IF_VIRTIO] = "virtio", | |
59 | [IF_XEN] = "xen", | |
60 | }; | |
61 | ||
21dff8cf | 62 | static int if_max_devs[IF_COUNT] = { |
27d6bf40 MA |
63 | /* |
64 | * Do not change these numbers! They govern how drive option | |
65 | * index maps to unit and bus. That mapping is ABI. | |
66 | * | |
67 | * All controllers used to imlement if=T drives need to support | |
68 | * if_max_devs[T] units, for any T with if_max_devs[T] != 0. | |
69 | * Otherwise, some index values map to "impossible" bus, unit | |
70 | * values. | |
71 | * | |
72 | * For instance, if you change [IF_SCSI] to 255, -drive | |
73 | * if=scsi,index=12 no longer means bus=1,unit=5, but | |
74 | * bus=0,unit=12. With an lsi53c895a controller (7 units max), | |
75 | * the drive can't be set up. Regression. | |
76 | */ | |
77 | [IF_IDE] = 2, | |
78 | [IF_SCSI] = 7, | |
1960966d MA |
79 | }; |
80 | ||
21dff8cf JS |
81 | /** |
82 | * Boards may call this to offer board-by-board overrides | |
83 | * of the default, global values. | |
84 | */ | |
85 | void override_max_devs(BlockInterfaceType type, int max_devs) | |
86 | { | |
18e46a03 | 87 | BlockBackend *blk; |
21dff8cf JS |
88 | DriveInfo *dinfo; |
89 | ||
90 | if (max_devs <= 0) { | |
91 | return; | |
92 | } | |
93 | ||
18e46a03 MA |
94 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
95 | dinfo = blk_legacy_dinfo(blk); | |
21dff8cf JS |
96 | if (dinfo->type == type) { |
97 | fprintf(stderr, "Cannot override units-per-bus property of" | |
98 | " the %s interface, because a drive of that type has" | |
99 | " already been added.\n", if_name[type]); | |
100 | g_assert_not_reached(); | |
101 | } | |
102 | } | |
103 | ||
104 | if_max_devs[type] = max_devs; | |
105 | } | |
106 | ||
14bafc54 MA |
107 | /* |
108 | * We automatically delete the drive when a device using it gets | |
109 | * unplugged. Questionable feature, but we can't just drop it. | |
110 | * Device models call blockdev_mark_auto_del() to schedule the | |
111 | * automatic deletion, and generic qdev code calls blockdev_auto_del() | |
112 | * when deletion is actually safe. | |
113 | */ | |
4be74634 | 114 | void blockdev_mark_auto_del(BlockBackend *blk) |
14bafc54 | 115 | { |
18e46a03 | 116 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
4be74634 | 117 | BlockDriverState *bs = blk_bs(blk); |
14bafc54 | 118 | |
26f8b3a8 | 119 | if (!dinfo) { |
2d246f01 KW |
120 | return; |
121 | } | |
122 | ||
12bde0ee PB |
123 | if (bs->job) { |
124 | block_job_cancel(bs->job); | |
125 | } | |
26f8b3a8 | 126 | dinfo->auto_del = 1; |
14bafc54 MA |
127 | } |
128 | ||
4be74634 | 129 | void blockdev_auto_del(BlockBackend *blk) |
14bafc54 | 130 | { |
18e46a03 | 131 | DriveInfo *dinfo = blk_legacy_dinfo(blk); |
14bafc54 | 132 | |
0fc0f1fa | 133 | if (dinfo && dinfo->auto_del) { |
b9fe8a7a | 134 | blk_unref(blk); |
14bafc54 MA |
135 | } |
136 | } | |
137 | ||
d8f94e1b JS |
138 | /** |
139 | * Returns the current mapping of how many units per bus | |
140 | * a particular interface can support. | |
141 | * | |
142 | * A positive integer indicates n units per bus. | |
143 | * 0 implies the mapping has not been established. | |
144 | * -1 indicates an invalid BlockInterfaceType was given. | |
145 | */ | |
146 | int drive_get_max_devs(BlockInterfaceType type) | |
147 | { | |
148 | if (type >= IF_IDE && type < IF_COUNT) { | |
149 | return if_max_devs[type]; | |
150 | } | |
151 | ||
152 | return -1; | |
153 | } | |
154 | ||
505a7fb1 MA |
155 | static int drive_index_to_bus_id(BlockInterfaceType type, int index) |
156 | { | |
157 | int max_devs = if_max_devs[type]; | |
158 | return max_devs ? index / max_devs : 0; | |
159 | } | |
160 | ||
161 | static int drive_index_to_unit_id(BlockInterfaceType type, int index) | |
162 | { | |
163 | int max_devs = if_max_devs[type]; | |
164 | return max_devs ? index % max_devs : index; | |
165 | } | |
166 | ||
2292ddae MA |
167 | QemuOpts *drive_def(const char *optstr) |
168 | { | |
169 | return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); | |
170 | } | |
171 | ||
172 | QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, | |
5645b0f4 | 173 | const char *optstr) |
666daa68 | 174 | { |
666daa68 | 175 | QemuOpts *opts; |
2292ddae | 176 | char buf[32]; |
666daa68 | 177 | |
2292ddae | 178 | opts = drive_def(optstr); |
666daa68 MA |
179 | if (!opts) { |
180 | return NULL; | |
181 | } | |
2292ddae MA |
182 | if (type != IF_DEFAULT) { |
183 | qemu_opt_set(opts, "if", if_name[type]); | |
184 | } | |
185 | if (index >= 0) { | |
186 | snprintf(buf, sizeof(buf), "%d", index); | |
187 | qemu_opt_set(opts, "index", buf); | |
188 | } | |
666daa68 MA |
189 | if (file) |
190 | qemu_opt_set(opts, "file", file); | |
191 | return opts; | |
192 | } | |
193 | ||
194 | DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) | |
195 | { | |
18e46a03 | 196 | BlockBackend *blk; |
666daa68 MA |
197 | DriveInfo *dinfo; |
198 | ||
18e46a03 MA |
199 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
200 | dinfo = blk_legacy_dinfo(blk); | |
201 | if (dinfo && dinfo->type == type | |
202 | && dinfo->bus == bus && dinfo->unit == unit) { | |
666daa68 | 203 | return dinfo; |
18e46a03 | 204 | } |
666daa68 MA |
205 | } |
206 | ||
207 | return NULL; | |
208 | } | |
209 | ||
a66c9dc7 JS |
210 | bool drive_check_orphaned(void) |
211 | { | |
18e46a03 | 212 | BlockBackend *blk; |
a66c9dc7 JS |
213 | DriveInfo *dinfo; |
214 | bool rs = false; | |
215 | ||
18e46a03 MA |
216 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
217 | dinfo = blk_legacy_dinfo(blk); | |
a66c9dc7 JS |
218 | /* If dinfo->bdrv->dev is NULL, it has no device attached. */ |
219 | /* Unless this is a default drive, this may be an oversight. */ | |
a7f53e26 | 220 | if (!blk_get_attached_dev(blk) && !dinfo->is_default && |
a66c9dc7 JS |
221 | dinfo->type != IF_NONE) { |
222 | fprintf(stderr, "Warning: Orphaned drive without device: " | |
223 | "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", | |
d3aeb1b7 | 224 | blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type], |
a66c9dc7 JS |
225 | dinfo->bus, dinfo->unit); |
226 | rs = true; | |
227 | } | |
228 | } | |
229 | ||
230 | return rs; | |
231 | } | |
232 | ||
f1bd51ac MA |
233 | DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) |
234 | { | |
235 | return drive_get(type, | |
236 | drive_index_to_bus_id(type, index), | |
237 | drive_index_to_unit_id(type, index)); | |
238 | } | |
239 | ||
666daa68 MA |
240 | int drive_get_max_bus(BlockInterfaceType type) |
241 | { | |
242 | int max_bus; | |
18e46a03 | 243 | BlockBackend *blk; |
666daa68 MA |
244 | DriveInfo *dinfo; |
245 | ||
246 | max_bus = -1; | |
18e46a03 MA |
247 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
248 | dinfo = blk_legacy_dinfo(blk); | |
249 | if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { | |
666daa68 | 250 | max_bus = dinfo->bus; |
18e46a03 | 251 | } |
666daa68 MA |
252 | } |
253 | return max_bus; | |
254 | } | |
255 | ||
13839974 MA |
256 | /* Get a block device. This should only be used for single-drive devices |
257 | (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the | |
258 | appropriate bus. */ | |
259 | DriveInfo *drive_get_next(BlockInterfaceType type) | |
260 | { | |
261 | static int next_block_unit[IF_COUNT]; | |
262 | ||
263 | return drive_get(type, 0, next_block_unit[type]++); | |
264 | } | |
265 | ||
666daa68 MA |
266 | static void bdrv_format_print(void *opaque, const char *name) |
267 | { | |
807105a7 | 268 | error_printf(" %s", name); |
666daa68 MA |
269 | } |
270 | ||
aa398a5c SH |
271 | typedef struct { |
272 | QEMUBH *bh; | |
fa510ebf FZ |
273 | BlockDriverState *bs; |
274 | } BDRVPutRefBH; | |
aa398a5c | 275 | |
fa510ebf | 276 | static void bdrv_put_ref_bh(void *opaque) |
aa398a5c | 277 | { |
fa510ebf | 278 | BDRVPutRefBH *s = opaque; |
aa398a5c | 279 | |
fa510ebf | 280 | bdrv_unref(s->bs); |
aa398a5c SH |
281 | qemu_bh_delete(s->bh); |
282 | g_free(s); | |
283 | } | |
284 | ||
285 | /* | |
fa510ebf | 286 | * Release a BDS reference in a BH |
aa398a5c | 287 | * |
fa510ebf FZ |
288 | * It is not safe to use bdrv_unref() from a callback function when the callers |
289 | * still need the BlockDriverState. In such cases we schedule a BH to release | |
290 | * the reference. | |
aa398a5c | 291 | */ |
fa510ebf | 292 | static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) |
aa398a5c | 293 | { |
fa510ebf | 294 | BDRVPutRefBH *s; |
aa398a5c | 295 | |
fa510ebf FZ |
296 | s = g_new(BDRVPutRefBH, 1); |
297 | s->bh = qemu_bh_new(bdrv_put_ref_bh, s); | |
298 | s->bs = bs; | |
aa398a5c SH |
299 | qemu_bh_schedule(s->bh); |
300 | } | |
301 | ||
b681072d | 302 | static int parse_block_error_action(const char *buf, bool is_read, Error **errp) |
666daa68 MA |
303 | { |
304 | if (!strcmp(buf, "ignore")) { | |
92aa5c6d | 305 | return BLOCKDEV_ON_ERROR_IGNORE; |
666daa68 | 306 | } else if (!is_read && !strcmp(buf, "enospc")) { |
92aa5c6d | 307 | return BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 308 | } else if (!strcmp(buf, "stop")) { |
92aa5c6d | 309 | return BLOCKDEV_ON_ERROR_STOP; |
666daa68 | 310 | } else if (!strcmp(buf, "report")) { |
92aa5c6d | 311 | return BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 312 | } else { |
b681072d KW |
313 | error_setg(errp, "'%s' invalid %s error action", |
314 | buf, is_read ? "read" : "write"); | |
666daa68 MA |
315 | return -1; |
316 | } | |
317 | } | |
318 | ||
cc0681c4 | 319 | static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) |
0563e191 | 320 | { |
cc0681c4 BC |
321 | if (throttle_conflicting(cfg)) { |
322 | error_setg(errp, "bps/iops/max total values and read/write values" | |
323 | " cannot be used at the same time"); | |
0563e191 ZYW |
324 | return false; |
325 | } | |
326 | ||
cc0681c4 BC |
327 | if (!throttle_is_valid(cfg)) { |
328 | error_setg(errp, "bps/iops/maxs values must be 0 or greater"); | |
7d81c141 SH |
329 | return false; |
330 | } | |
331 | ||
0563e191 ZYW |
332 | return true; |
333 | } | |
334 | ||
33cb7dc8 KW |
335 | typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; |
336 | ||
f298d071 | 337 | /* Takes the ownership of bs_opts */ |
18e46a03 MA |
338 | static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, |
339 | Error **errp) | |
666daa68 MA |
340 | { |
341 | const char *buf; | |
666daa68 MA |
342 | int ro = 0; |
343 | int bdrv_flags = 0; | |
344 | int on_read_error, on_write_error; | |
26f54e9a | 345 | BlockBackend *blk; |
a0f1eab1 | 346 | BlockDriverState *bs; |
cc0681c4 | 347 | ThrottleConfig cfg; |
666daa68 | 348 | int snapshot = 0; |
fb0490f6 | 349 | bool copy_on_read; |
666daa68 | 350 | int ret; |
c546194f | 351 | Error *error = NULL; |
0006383e | 352 | QemuOpts *opts; |
0006383e | 353 | const char *id; |
74fe54f2 | 354 | bool has_driver_specific_opts; |
465bee1d | 355 | BlockdevDetectZeroesOptions detect_zeroes; |
6db5f5d6 | 356 | BlockDriver *drv = NULL; |
666daa68 | 357 | |
f298d071 KW |
358 | /* Check common options by copying from bs_opts to opts, all other options |
359 | * stay in bs_opts for processing by bdrv_open(). */ | |
360 | id = qdict_get_try_str(bs_opts, "id"); | |
0006383e | 361 | opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); |
84d18f06 | 362 | if (error) { |
b681072d | 363 | error_propagate(errp, error); |
6376f952 | 364 | goto err_no_opts; |
0006383e KW |
365 | } |
366 | ||
0006383e | 367 | qemu_opts_absorb_qdict(opts, bs_opts, &error); |
84d18f06 | 368 | if (error) { |
b681072d | 369 | error_propagate(errp, error); |
ec9c10d2 | 370 | goto early_err; |
0006383e KW |
371 | } |
372 | ||
373 | if (id) { | |
374 | qdict_del(bs_opts, "id"); | |
375 | } | |
376 | ||
74fe54f2 KW |
377 | has_driver_specific_opts = !!qdict_size(bs_opts); |
378 | ||
666daa68 | 379 | /* extract parameters */ |
666daa68 | 380 | snapshot = qemu_opt_get_bool(opts, "snapshot", 0); |
0f227a94 | 381 | ro = qemu_opt_get_bool(opts, "read-only", 0); |
fb0490f6 | 382 | copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); |
666daa68 | 383 | |
a9384aff PB |
384 | if ((buf = qemu_opt_get(opts, "discard")) != NULL) { |
385 | if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { | |
b681072d | 386 | error_setg(errp, "invalid discard option"); |
ec9c10d2 | 387 | goto early_err; |
a9384aff PB |
388 | } |
389 | } | |
390 | ||
29c4e2b5 KW |
391 | if (qemu_opt_get_bool(opts, "cache.writeback", true)) { |
392 | bdrv_flags |= BDRV_O_CACHE_WB; | |
393 | } | |
394 | if (qemu_opt_get_bool(opts, "cache.direct", false)) { | |
395 | bdrv_flags |= BDRV_O_NOCACHE; | |
396 | } | |
1df6fa4b | 397 | if (qemu_opt_get_bool(opts, "cache.no-flush", false)) { |
29c4e2b5 | 398 | bdrv_flags |= BDRV_O_NO_FLUSH; |
666daa68 MA |
399 | } |
400 | ||
401 | #ifdef CONFIG_LINUX_AIO | |
402 | if ((buf = qemu_opt_get(opts, "aio")) != NULL) { | |
403 | if (!strcmp(buf, "native")) { | |
404 | bdrv_flags |= BDRV_O_NATIVE_AIO; | |
405 | } else if (!strcmp(buf, "threads")) { | |
406 | /* this is the default */ | |
407 | } else { | |
b681072d | 408 | error_setg(errp, "invalid aio option"); |
ec9c10d2 | 409 | goto early_err; |
666daa68 MA |
410 | } |
411 | } | |
412 | #endif | |
413 | ||
414 | if ((buf = qemu_opt_get(opts, "format")) != NULL) { | |
c8057f95 PM |
415 | if (is_help_option(buf)) { |
416 | error_printf("Supported formats:"); | |
417 | bdrv_iterate_format(bdrv_format_print, NULL); | |
418 | error_printf("\n"); | |
ec9c10d2 | 419 | goto early_err; |
666daa68 | 420 | } |
74fe54f2 | 421 | |
8f94a6e4 | 422 | drv = bdrv_find_format(buf); |
6db5f5d6 | 423 | if (!drv) { |
b681072d | 424 | error_setg(errp, "'%s' invalid format", buf); |
ec9c10d2 | 425 | goto early_err; |
6db5f5d6 | 426 | } |
666daa68 MA |
427 | } |
428 | ||
0563e191 | 429 | /* disk I/O throttling */ |
cc0681c4 BC |
430 | memset(&cfg, 0, sizeof(cfg)); |
431 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = | |
57975222 | 432 | qemu_opt_get_number(opts, "throttling.bps-total", 0); |
cc0681c4 | 433 | cfg.buckets[THROTTLE_BPS_READ].avg = |
57975222 | 434 | qemu_opt_get_number(opts, "throttling.bps-read", 0); |
cc0681c4 | 435 | cfg.buckets[THROTTLE_BPS_WRITE].avg = |
57975222 | 436 | qemu_opt_get_number(opts, "throttling.bps-write", 0); |
cc0681c4 | 437 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = |
57975222 | 438 | qemu_opt_get_number(opts, "throttling.iops-total", 0); |
cc0681c4 | 439 | cfg.buckets[THROTTLE_OPS_READ].avg = |
57975222 | 440 | qemu_opt_get_number(opts, "throttling.iops-read", 0); |
cc0681c4 | 441 | cfg.buckets[THROTTLE_OPS_WRITE].avg = |
57975222 | 442 | qemu_opt_get_number(opts, "throttling.iops-write", 0); |
0563e191 | 443 | |
3e9fab69 BC |
444 | cfg.buckets[THROTTLE_BPS_TOTAL].max = |
445 | qemu_opt_get_number(opts, "throttling.bps-total-max", 0); | |
446 | cfg.buckets[THROTTLE_BPS_READ].max = | |
447 | qemu_opt_get_number(opts, "throttling.bps-read-max", 0); | |
448 | cfg.buckets[THROTTLE_BPS_WRITE].max = | |
449 | qemu_opt_get_number(opts, "throttling.bps-write-max", 0); | |
450 | cfg.buckets[THROTTLE_OPS_TOTAL].max = | |
451 | qemu_opt_get_number(opts, "throttling.iops-total-max", 0); | |
452 | cfg.buckets[THROTTLE_OPS_READ].max = | |
453 | qemu_opt_get_number(opts, "throttling.iops-read-max", 0); | |
454 | cfg.buckets[THROTTLE_OPS_WRITE].max = | |
455 | qemu_opt_get_number(opts, "throttling.iops-write-max", 0); | |
cc0681c4 | 456 | |
2024c1df | 457 | cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0); |
cc0681c4 BC |
458 | |
459 | if (!check_throttle_config(&cfg, &error)) { | |
b681072d | 460 | error_propagate(errp, error); |
ec9c10d2 | 461 | goto early_err; |
0563e191 ZYW |
462 | } |
463 | ||
92aa5c6d | 464 | on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; |
666daa68 | 465 | if ((buf = qemu_opt_get(opts, "werror")) != NULL) { |
b681072d | 466 | on_write_error = parse_block_error_action(buf, 0, &error); |
84d18f06 | 467 | if (error) { |
b681072d | 468 | error_propagate(errp, error); |
ec9c10d2 | 469 | goto early_err; |
666daa68 MA |
470 | } |
471 | } | |
472 | ||
92aa5c6d | 473 | on_read_error = BLOCKDEV_ON_ERROR_REPORT; |
666daa68 | 474 | if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { |
b681072d | 475 | on_read_error = parse_block_error_action(buf, 1, &error); |
84d18f06 | 476 | if (error) { |
b681072d | 477 | error_propagate(errp, error); |
ec9c10d2 | 478 | goto early_err; |
666daa68 MA |
479 | } |
480 | } | |
481 | ||
465bee1d | 482 | detect_zeroes = |
9e7dac7c PL |
483 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, |
484 | qemu_opt_get(opts, "detect-zeroes"), | |
485 | BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, | |
486 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, | |
487 | &error); | |
465bee1d PL |
488 | if (error) { |
489 | error_propagate(errp, error); | |
490 | goto early_err; | |
491 | } | |
492 | ||
493 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
494 | !(bdrv_flags & BDRV_O_UNMAP)) { | |
495 | error_setg(errp, "setting detect-zeroes to unmap is not allowed " | |
496 | "without setting discard operation to unmap"); | |
497 | goto early_err; | |
498 | } | |
499 | ||
326642bc | 500 | /* init */ |
7e7d56d9 | 501 | blk = blk_new_with_bs(qemu_opts_id(opts), errp); |
26f54e9a MA |
502 | if (!blk) { |
503 | goto early_err; | |
504 | } | |
7e7d56d9 | 505 | bs = blk_bs(blk); |
a0f1eab1 MA |
506 | bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; |
507 | bs->read_only = ro; | |
508 | bs->detect_zeroes = detect_zeroes; | |
666daa68 | 509 | |
a0f1eab1 | 510 | bdrv_set_on_error(bs, on_read_error, on_write_error); |
abd7f68d | 511 | |
0563e191 | 512 | /* disk I/O throttling */ |
cc0681c4 | 513 | if (throttle_enabled(&cfg)) { |
a0f1eab1 MA |
514 | bdrv_io_limits_enable(bs); |
515 | bdrv_set_io_limits(bs, &cfg); | |
cc0681c4 | 516 | } |
0563e191 | 517 | |
dd5b0d71 | 518 | if (!file || !*file) { |
74fe54f2 | 519 | if (has_driver_specific_opts) { |
c2ad1b0c KW |
520 | file = NULL; |
521 | } else { | |
ec9c10d2 SH |
522 | QDECREF(bs_opts); |
523 | qemu_opts_del(opts); | |
18e46a03 | 524 | return blk; |
c2ad1b0c | 525 | } |
666daa68 MA |
526 | } |
527 | if (snapshot) { | |
528 | /* always use cache=unsafe with snapshot */ | |
529 | bdrv_flags &= ~BDRV_O_CACHE_MASK; | |
530 | bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); | |
531 | } | |
532 | ||
fb0490f6 SH |
533 | if (copy_on_read) { |
534 | bdrv_flags |= BDRV_O_COPY_ON_READ; | |
535 | } | |
536 | ||
ed9d4205 BC |
537 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
538 | bdrv_flags |= BDRV_O_INCOMING; | |
539 | } | |
540 | ||
666daa68 MA |
541 | bdrv_flags |= ro ? 0 : BDRV_O_RDWR; |
542 | ||
74fe54f2 | 543 | QINCREF(bs_opts); |
a0f1eab1 | 544 | ret = bdrv_open(&bs, file, NULL, bs_opts, bdrv_flags, drv, &error); |
fa1d36df | 545 | assert(bs == blk_bs(blk)); |
0006383e | 546 | |
666daa68 | 547 | if (ret < 0) { |
b681072d | 548 | error_setg(errp, "could not open disk image %s: %s", |
d3aeb1b7 | 549 | file ?: blk_name(blk), error_get_pretty(error)); |
b681072d | 550 | error_free(error); |
a9ae2bff | 551 | goto err; |
666daa68 MA |
552 | } |
553 | ||
a0f1eab1 | 554 | if (bdrv_key_required(bs)) { |
666daa68 | 555 | autostart = 0; |
a0f1eab1 | 556 | } |
0006383e | 557 | |
74fe54f2 | 558 | QDECREF(bs_opts); |
0006383e KW |
559 | qemu_opts_del(opts); |
560 | ||
18e46a03 | 561 | return blk; |
a9ae2bff MA |
562 | |
563 | err: | |
26f54e9a | 564 | blk_unref(blk); |
ec9c10d2 | 565 | early_err: |
ec9c10d2 | 566 | qemu_opts_del(opts); |
6376f952 MA |
567 | err_no_opts: |
568 | QDECREF(bs_opts); | |
a9ae2bff | 569 | return NULL; |
666daa68 MA |
570 | } |
571 | ||
5abbf0ee KW |
572 | static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, |
573 | Error **errp) | |
57975222 KW |
574 | { |
575 | const char *value; | |
576 | ||
577 | value = qemu_opt_get(opts, from); | |
578 | if (value) { | |
5abbf0ee KW |
579 | if (qemu_opt_find(opts, to)) { |
580 | error_setg(errp, "'%s' and its alias '%s' can't be used at the " | |
581 | "same time", to, from); | |
582 | return; | |
583 | } | |
20d6cd47 JL |
584 | } |
585 | ||
586 | /* rename all items in opts */ | |
587 | while ((value = qemu_opt_get(opts, from))) { | |
57975222 KW |
588 | qemu_opt_set(opts, to, value); |
589 | qemu_opt_unset(opts, from); | |
590 | } | |
591 | } | |
592 | ||
33cb7dc8 KW |
593 | QemuOptsList qemu_legacy_drive_opts = { |
594 | .name = "drive", | |
595 | .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), | |
596 | .desc = { | |
597 | { | |
87a899c5 KW |
598 | .name = "bus", |
599 | .type = QEMU_OPT_NUMBER, | |
600 | .help = "bus number", | |
601 | },{ | |
602 | .name = "unit", | |
603 | .type = QEMU_OPT_NUMBER, | |
604 | .help = "unit number (i.e. lun for scsi)", | |
605 | },{ | |
606 | .name = "index", | |
607 | .type = QEMU_OPT_NUMBER, | |
608 | .help = "index number", | |
609 | },{ | |
33cb7dc8 KW |
610 | .name = "media", |
611 | .type = QEMU_OPT_STRING, | |
612 | .help = "media type (disk, cdrom)", | |
593d464b KW |
613 | },{ |
614 | .name = "if", | |
615 | .type = QEMU_OPT_STRING, | |
616 | .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", | |
b41a7338 KW |
617 | },{ |
618 | .name = "cyls", | |
619 | .type = QEMU_OPT_NUMBER, | |
620 | .help = "number of cylinders (ide disk geometry)", | |
621 | },{ | |
622 | .name = "heads", | |
623 | .type = QEMU_OPT_NUMBER, | |
624 | .help = "number of heads (ide disk geometry)", | |
625 | },{ | |
626 | .name = "secs", | |
627 | .type = QEMU_OPT_NUMBER, | |
628 | .help = "number of sectors (ide disk geometry)", | |
629 | },{ | |
630 | .name = "trans", | |
631 | .type = QEMU_OPT_STRING, | |
632 | .help = "chs translation (auto, lba, none)", | |
26929298 KW |
633 | },{ |
634 | .name = "boot", | |
635 | .type = QEMU_OPT_BOOL, | |
636 | .help = "(deprecated, ignored)", | |
394c7d4d KW |
637 | },{ |
638 | .name = "addr", | |
639 | .type = QEMU_OPT_STRING, | |
640 | .help = "pci address (virtio only)", | |
bcf83158 KW |
641 | },{ |
642 | .name = "serial", | |
643 | .type = QEMU_OPT_STRING, | |
644 | .help = "disk serial number", | |
d095b465 HR |
645 | },{ |
646 | .name = "file", | |
647 | .type = QEMU_OPT_STRING, | |
648 | .help = "file name", | |
33cb7dc8 | 649 | }, |
0ebd24e0 KW |
650 | |
651 | /* Options that are passed on, but have special semantics with -drive */ | |
652 | { | |
653 | .name = "read-only", | |
654 | .type = QEMU_OPT_BOOL, | |
655 | .help = "open drive file as read-only", | |
ee13ed1c KW |
656 | },{ |
657 | .name = "rerror", | |
658 | .type = QEMU_OPT_STRING, | |
659 | .help = "read error action", | |
660 | },{ | |
661 | .name = "werror", | |
662 | .type = QEMU_OPT_STRING, | |
663 | .help = "write error action", | |
0ebd24e0 KW |
664 | },{ |
665 | .name = "copy-on-read", | |
666 | .type = QEMU_OPT_BOOL, | |
667 | .help = "copy read data from backing file into image file", | |
668 | }, | |
669 | ||
33cb7dc8 KW |
670 | { /* end of list */ } |
671 | }, | |
672 | }; | |
673 | ||
60e19e06 | 674 | DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) |
57975222 | 675 | { |
29c4e2b5 | 676 | const char *value; |
18e46a03 | 677 | BlockBackend *blk; |
33cb7dc8 | 678 | DriveInfo *dinfo = NULL; |
f298d071 | 679 | QDict *bs_opts; |
33cb7dc8 KW |
680 | QemuOpts *legacy_opts; |
681 | DriveMediaType media = MEDIA_DISK; | |
593d464b | 682 | BlockInterfaceType type; |
b41a7338 | 683 | int cyls, heads, secs, translation; |
87a899c5 | 684 | int max_devs, bus_id, unit_id, index; |
394c7d4d | 685 | const char *devaddr; |
ee13ed1c | 686 | const char *werror, *rerror; |
a7fdbcf0 FZ |
687 | bool read_only = false; |
688 | bool copy_on_read; | |
bcf83158 | 689 | const char *serial; |
d095b465 | 690 | const char *filename; |
33cb7dc8 | 691 | Error *local_err = NULL; |
247147fb | 692 | int i; |
29c4e2b5 | 693 | |
57975222 | 694 | /* Change legacy command line options into QMP ones */ |
247147fb KW |
695 | static const struct { |
696 | const char *from; | |
697 | const char *to; | |
698 | } opt_renames[] = { | |
699 | { "iops", "throttling.iops-total" }, | |
700 | { "iops_rd", "throttling.iops-read" }, | |
701 | { "iops_wr", "throttling.iops-write" }, | |
57975222 | 702 | |
247147fb KW |
703 | { "bps", "throttling.bps-total" }, |
704 | { "bps_rd", "throttling.bps-read" }, | |
705 | { "bps_wr", "throttling.bps-write" }, | |
57975222 | 706 | |
247147fb KW |
707 | { "iops_max", "throttling.iops-total-max" }, |
708 | { "iops_rd_max", "throttling.iops-read-max" }, | |
709 | { "iops_wr_max", "throttling.iops-write-max" }, | |
3e9fab69 | 710 | |
247147fb KW |
711 | { "bps_max", "throttling.bps-total-max" }, |
712 | { "bps_rd_max", "throttling.bps-read-max" }, | |
713 | { "bps_wr_max", "throttling.bps-write-max" }, | |
3e9fab69 | 714 | |
247147fb | 715 | { "iops_size", "throttling.iops-size" }, |
2024c1df | 716 | |
247147fb KW |
717 | { "readonly", "read-only" }, |
718 | }; | |
719 | ||
720 | for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { | |
5abbf0ee KW |
721 | qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, |
722 | &local_err); | |
723 | if (local_err) { | |
724 | error_report("%s", error_get_pretty(local_err)); | |
725 | error_free(local_err); | |
726 | return NULL; | |
727 | } | |
247147fb | 728 | } |
0f227a94 | 729 | |
29c4e2b5 KW |
730 | value = qemu_opt_get(all_opts, "cache"); |
731 | if (value) { | |
732 | int flags = 0; | |
733 | ||
734 | if (bdrv_parse_cache_flags(value, &flags) != 0) { | |
735 | error_report("invalid cache option"); | |
736 | return NULL; | |
737 | } | |
738 | ||
739 | /* Specific options take precedence */ | |
740 | if (!qemu_opt_get(all_opts, "cache.writeback")) { | |
741 | qemu_opt_set_bool(all_opts, "cache.writeback", | |
742 | !!(flags & BDRV_O_CACHE_WB)); | |
743 | } | |
744 | if (!qemu_opt_get(all_opts, "cache.direct")) { | |
745 | qemu_opt_set_bool(all_opts, "cache.direct", | |
746 | !!(flags & BDRV_O_NOCACHE)); | |
747 | } | |
748 | if (!qemu_opt_get(all_opts, "cache.no-flush")) { | |
749 | qemu_opt_set_bool(all_opts, "cache.no-flush", | |
750 | !!(flags & BDRV_O_NO_FLUSH)); | |
751 | } | |
752 | qemu_opt_unset(all_opts, "cache"); | |
753 | } | |
754 | ||
f298d071 KW |
755 | /* Get a QDict for processing the options */ |
756 | bs_opts = qdict_new(); | |
757 | qemu_opts_to_qdict(all_opts, bs_opts); | |
758 | ||
87ea75d5 PC |
759 | legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, |
760 | &error_abort); | |
33cb7dc8 | 761 | qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); |
84d18f06 | 762 | if (local_err) { |
e8817e7b | 763 | error_report("%s", error_get_pretty(local_err)); |
33cb7dc8 KW |
764 | error_free(local_err); |
765 | goto fail; | |
766 | } | |
767 | ||
26929298 KW |
768 | /* Deprecated option boot=[on|off] */ |
769 | if (qemu_opt_get(legacy_opts, "boot") != NULL) { | |
770 | fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " | |
771 | "ignored. Future versions will reject this parameter. Please " | |
772 | "update your scripts.\n"); | |
773 | } | |
774 | ||
33cb7dc8 KW |
775 | /* Media type */ |
776 | value = qemu_opt_get(legacy_opts, "media"); | |
777 | if (value) { | |
778 | if (!strcmp(value, "disk")) { | |
779 | media = MEDIA_DISK; | |
780 | } else if (!strcmp(value, "cdrom")) { | |
781 | media = MEDIA_CDROM; | |
a7fdbcf0 | 782 | read_only = true; |
33cb7dc8 KW |
783 | } else { |
784 | error_report("'%s' invalid media", value); | |
785 | goto fail; | |
786 | } | |
787 | } | |
788 | ||
0ebd24e0 | 789 | /* copy-on-read is disabled with a warning for read-only devices */ |
a7fdbcf0 | 790 | read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); |
0ebd24e0 KW |
791 | copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); |
792 | ||
793 | if (read_only && copy_on_read) { | |
794 | error_report("warning: disabling copy-on-read on read-only drive"); | |
795 | copy_on_read = false; | |
796 | } | |
797 | ||
798 | qdict_put(bs_opts, "read-only", | |
799 | qstring_from_str(read_only ? "on" : "off")); | |
800 | qdict_put(bs_opts, "copy-on-read", | |
801 | qstring_from_str(copy_on_read ? "on" :"off")); | |
802 | ||
593d464b KW |
803 | /* Controller type */ |
804 | value = qemu_opt_get(legacy_opts, "if"); | |
805 | if (value) { | |
806 | for (type = 0; | |
807 | type < IF_COUNT && strcmp(value, if_name[type]); | |
808 | type++) { | |
809 | } | |
810 | if (type == IF_COUNT) { | |
811 | error_report("unsupported bus type '%s'", value); | |
812 | goto fail; | |
813 | } | |
814 | } else { | |
815 | type = block_default_type; | |
816 | } | |
817 | ||
b41a7338 KW |
818 | /* Geometry */ |
819 | cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); | |
820 | heads = qemu_opt_get_number(legacy_opts, "heads", 0); | |
821 | secs = qemu_opt_get_number(legacy_opts, "secs", 0); | |
822 | ||
823 | if (cyls || heads || secs) { | |
824 | if (cyls < 1) { | |
825 | error_report("invalid physical cyls number"); | |
826 | goto fail; | |
827 | } | |
828 | if (heads < 1) { | |
829 | error_report("invalid physical heads number"); | |
830 | goto fail; | |
831 | } | |
832 | if (secs < 1) { | |
833 | error_report("invalid physical secs number"); | |
834 | goto fail; | |
835 | } | |
836 | } | |
837 | ||
838 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
839 | value = qemu_opt_get(legacy_opts, "trans"); | |
840 | if (value != NULL) { | |
841 | if (!cyls) { | |
842 | error_report("'%s' trans must be used with cyls, heads and secs", | |
843 | value); | |
844 | goto fail; | |
845 | } | |
846 | if (!strcmp(value, "none")) { | |
847 | translation = BIOS_ATA_TRANSLATION_NONE; | |
848 | } else if (!strcmp(value, "lba")) { | |
849 | translation = BIOS_ATA_TRANSLATION_LBA; | |
f31c41ff PB |
850 | } else if (!strcmp(value, "large")) { |
851 | translation = BIOS_ATA_TRANSLATION_LARGE; | |
852 | } else if (!strcmp(value, "rechs")) { | |
853 | translation = BIOS_ATA_TRANSLATION_RECHS; | |
b41a7338 KW |
854 | } else if (!strcmp(value, "auto")) { |
855 | translation = BIOS_ATA_TRANSLATION_AUTO; | |
856 | } else { | |
857 | error_report("'%s' invalid translation type", value); | |
858 | goto fail; | |
859 | } | |
860 | } | |
861 | ||
862 | if (media == MEDIA_CDROM) { | |
863 | if (cyls || secs || heads) { | |
864 | error_report("CHS can't be set with media=cdrom"); | |
865 | goto fail; | |
866 | } | |
867 | } | |
868 | ||
87a899c5 KW |
869 | /* Device address specified by bus/unit or index. |
870 | * If none was specified, try to find the first free one. */ | |
871 | bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); | |
872 | unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); | |
873 | index = qemu_opt_get_number(legacy_opts, "index", -1); | |
874 | ||
875 | max_devs = if_max_devs[type]; | |
876 | ||
877 | if (index != -1) { | |
878 | if (bus_id != 0 || unit_id != -1) { | |
879 | error_report("index cannot be used with bus and unit"); | |
880 | goto fail; | |
881 | } | |
882 | bus_id = drive_index_to_bus_id(type, index); | |
883 | unit_id = drive_index_to_unit_id(type, index); | |
884 | } | |
885 | ||
886 | if (unit_id == -1) { | |
887 | unit_id = 0; | |
888 | while (drive_get(type, bus_id, unit_id) != NULL) { | |
889 | unit_id++; | |
890 | if (max_devs && unit_id >= max_devs) { | |
891 | unit_id -= max_devs; | |
892 | bus_id++; | |
893 | } | |
894 | } | |
895 | } | |
896 | ||
897 | if (max_devs && unit_id >= max_devs) { | |
898 | error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); | |
899 | goto fail; | |
900 | } | |
901 | ||
902 | if (drive_get(type, bus_id, unit_id) != NULL) { | |
903 | error_report("drive with bus=%d, unit=%d (index=%d) exists", | |
904 | bus_id, unit_id, index); | |
905 | goto fail; | |
906 | } | |
907 | ||
bcf83158 KW |
908 | /* Serial number */ |
909 | serial = qemu_opt_get(legacy_opts, "serial"); | |
910 | ||
87a899c5 KW |
911 | /* no id supplied -> create one */ |
912 | if (qemu_opts_id(all_opts) == NULL) { | |
913 | char *new_id; | |
914 | const char *mediastr = ""; | |
915 | if (type == IF_IDE || type == IF_SCSI) { | |
916 | mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; | |
917 | } | |
918 | if (max_devs) { | |
919 | new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, | |
920 | mediastr, unit_id); | |
921 | } else { | |
922 | new_id = g_strdup_printf("%s%s%i", if_name[type], | |
923 | mediastr, unit_id); | |
924 | } | |
925 | qdict_put(bs_opts, "id", qstring_from_str(new_id)); | |
926 | g_free(new_id); | |
927 | } | |
928 | ||
394c7d4d KW |
929 | /* Add virtio block device */ |
930 | devaddr = qemu_opt_get(legacy_opts, "addr"); | |
931 | if (devaddr && type != IF_VIRTIO) { | |
932 | error_report("addr is not supported by this bus type"); | |
933 | goto fail; | |
934 | } | |
935 | ||
936 | if (type == IF_VIRTIO) { | |
937 | QemuOpts *devopts; | |
87ea75d5 PC |
938 | devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, |
939 | &error_abort); | |
394c7d4d KW |
940 | if (arch_type == QEMU_ARCH_S390X) { |
941 | qemu_opt_set(devopts, "driver", "virtio-blk-s390"); | |
942 | } else { | |
943 | qemu_opt_set(devopts, "driver", "virtio-blk-pci"); | |
944 | } | |
945 | qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id")); | |
946 | if (devaddr) { | |
947 | qemu_opt_set(devopts, "addr", devaddr); | |
948 | } | |
949 | } | |
950 | ||
d095b465 HR |
951 | filename = qemu_opt_get(legacy_opts, "file"); |
952 | ||
ee13ed1c KW |
953 | /* Check werror/rerror compatibility with if=... */ |
954 | werror = qemu_opt_get(legacy_opts, "werror"); | |
955 | if (werror != NULL) { | |
956 | if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && | |
957 | type != IF_NONE) { | |
958 | error_report("werror is not supported by this bus type"); | |
959 | goto fail; | |
960 | } | |
961 | qdict_put(bs_opts, "werror", qstring_from_str(werror)); | |
962 | } | |
963 | ||
964 | rerror = qemu_opt_get(legacy_opts, "rerror"); | |
965 | if (rerror != NULL) { | |
966 | if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && | |
967 | type != IF_NONE) { | |
968 | error_report("rerror is not supported by this bus type"); | |
969 | goto fail; | |
970 | } | |
971 | qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); | |
972 | } | |
973 | ||
2d246f01 | 974 | /* Actual block device init: Functionality shared with blockdev-add */ |
18e46a03 | 975 | blk = blockdev_init(filename, bs_opts, &local_err); |
3cb0e25c | 976 | bs_opts = NULL; |
18e46a03 | 977 | if (!blk) { |
84d18f06 | 978 | if (local_err) { |
e8817e7b | 979 | error_report("%s", error_get_pretty(local_err)); |
b681072d KW |
980 | error_free(local_err); |
981 | } | |
2d246f01 | 982 | goto fail; |
b681072d | 983 | } else { |
84d18f06 | 984 | assert(!local_err); |
2d246f01 KW |
985 | } |
986 | ||
26f8b3a8 MA |
987 | /* Create legacy DriveInfo */ |
988 | dinfo = g_malloc0(sizeof(*dinfo)); | |
f298d071 | 989 | dinfo->opts = all_opts; |
2d246f01 | 990 | |
b41a7338 KW |
991 | dinfo->cyls = cyls; |
992 | dinfo->heads = heads; | |
993 | dinfo->secs = secs; | |
994 | dinfo->trans = translation; | |
995 | ||
ee13ed1c | 996 | dinfo->type = type; |
87a899c5 KW |
997 | dinfo->bus = bus_id; |
998 | dinfo->unit = unit_id; | |
394c7d4d | 999 | dinfo->devaddr = devaddr; |
bcf83158 KW |
1000 | dinfo->serial = g_strdup(serial); |
1001 | ||
26f8b3a8 MA |
1002 | blk_set_legacy_dinfo(blk, dinfo); |
1003 | ||
e34ef046 KW |
1004 | switch(type) { |
1005 | case IF_IDE: | |
1006 | case IF_SCSI: | |
1007 | case IF_XEN: | |
1008 | case IF_NONE: | |
1009 | dinfo->media_cd = media == MEDIA_CDROM; | |
1010 | break; | |
1011 | default: | |
1012 | break; | |
1013 | } | |
1014 | ||
2d246f01 | 1015 | fail: |
33cb7dc8 | 1016 | qemu_opts_del(legacy_opts); |
3cb0e25c | 1017 | QDECREF(bs_opts); |
2d246f01 | 1018 | return dinfo; |
57975222 KW |
1019 | } |
1020 | ||
666daa68 MA |
1021 | void do_commit(Monitor *mon, const QDict *qdict) |
1022 | { | |
666daa68 | 1023 | const char *device = qdict_get_str(qdict, "device"); |
6ab4b5ab | 1024 | BlockDriverState *bs; |
e8877497 | 1025 | int ret; |
666daa68 | 1026 | |
6ab4b5ab | 1027 | if (!strcmp(device, "all")) { |
e8877497 | 1028 | ret = bdrv_commit_all(); |
6ab4b5ab MA |
1029 | } else { |
1030 | bs = bdrv_find(device); | |
ac59eb95 | 1031 | if (!bs) { |
58513bde | 1032 | monitor_printf(mon, "Device '%s' not found\n", device); |
ac59eb95 | 1033 | return; |
6ab4b5ab | 1034 | } |
2d3735d3 | 1035 | ret = bdrv_commit(bs); |
58513bde JC |
1036 | } |
1037 | if (ret < 0) { | |
1038 | monitor_printf(mon, "'commit' error for '%s': %s\n", device, | |
1039 | strerror(-ret)); | |
666daa68 MA |
1040 | } |
1041 | } | |
1042 | ||
6cc2a415 PB |
1043 | static void blockdev_do_action(int kind, void *data, Error **errp) |
1044 | { | |
c8a83e85 KW |
1045 | TransactionAction action; |
1046 | TransactionActionList list; | |
6cc2a415 PB |
1047 | |
1048 | action.kind = kind; | |
1049 | action.data = data; | |
1050 | list.value = &action; | |
1051 | list.next = NULL; | |
1052 | qmp_transaction(&list, errp); | |
1053 | } | |
1054 | ||
0901f67e BC |
1055 | void qmp_blockdev_snapshot_sync(bool has_device, const char *device, |
1056 | bool has_node_name, const char *node_name, | |
1057 | const char *snapshot_file, | |
1058 | bool has_snapshot_node_name, | |
1059 | const char *snapshot_node_name, | |
6106e249 | 1060 | bool has_format, const char *format, |
0901f67e | 1061 | bool has_mode, NewImageMode mode, Error **errp) |
f8882568 | 1062 | { |
6cc2a415 | 1063 | BlockdevSnapshot snapshot = { |
0901f67e | 1064 | .has_device = has_device, |
6cc2a415 | 1065 | .device = (char *) device, |
0901f67e BC |
1066 | .has_node_name = has_node_name, |
1067 | .node_name = (char *) node_name, | |
6cc2a415 | 1068 | .snapshot_file = (char *) snapshot_file, |
0901f67e BC |
1069 | .has_snapshot_node_name = has_snapshot_node_name, |
1070 | .snapshot_node_name = (char *) snapshot_node_name, | |
6cc2a415 PB |
1071 | .has_format = has_format, |
1072 | .format = (char *) format, | |
1073 | .has_mode = has_mode, | |
1074 | .mode = mode, | |
1075 | }; | |
c8a83e85 KW |
1076 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, |
1077 | &snapshot, errp); | |
f8882568 JS |
1078 | } |
1079 | ||
f323bc9e WX |
1080 | void qmp_blockdev_snapshot_internal_sync(const char *device, |
1081 | const char *name, | |
1082 | Error **errp) | |
1083 | { | |
1084 | BlockdevSnapshotInternal snapshot = { | |
1085 | .device = (char *) device, | |
1086 | .name = (char *) name | |
1087 | }; | |
1088 | ||
1089 | blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, | |
1090 | &snapshot, errp); | |
1091 | } | |
1092 | ||
44e3e053 WX |
1093 | SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, |
1094 | bool has_id, | |
1095 | const char *id, | |
1096 | bool has_name, | |
1097 | const char *name, | |
1098 | Error **errp) | |
1099 | { | |
1100 | BlockDriverState *bs = bdrv_find(device); | |
1101 | QEMUSnapshotInfo sn; | |
1102 | Error *local_err = NULL; | |
1103 | SnapshotInfo *info = NULL; | |
1104 | int ret; | |
1105 | ||
1106 | if (!bs) { | |
1107 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1108 | return NULL; | |
1109 | } | |
1110 | ||
1111 | if (!has_id) { | |
1112 | id = NULL; | |
1113 | } | |
1114 | ||
1115 | if (!has_name) { | |
1116 | name = NULL; | |
1117 | } | |
1118 | ||
1119 | if (!id && !name) { | |
1120 | error_setg(errp, "Name or id must be provided"); | |
1121 | return NULL; | |
1122 | } | |
1123 | ||
1124 | ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); | |
84d18f06 | 1125 | if (local_err) { |
44e3e053 WX |
1126 | error_propagate(errp, local_err); |
1127 | return NULL; | |
1128 | } | |
1129 | if (!ret) { | |
1130 | error_setg(errp, | |
1131 | "Snapshot with id '%s' and name '%s' does not exist on " | |
1132 | "device '%s'", | |
1133 | STR_OR_NULL(id), STR_OR_NULL(name), device); | |
1134 | return NULL; | |
1135 | } | |
1136 | ||
1137 | bdrv_snapshot_delete(bs, id, name, &local_err); | |
84d18f06 | 1138 | if (local_err) { |
44e3e053 WX |
1139 | error_propagate(errp, local_err); |
1140 | return NULL; | |
1141 | } | |
1142 | ||
5839e53b | 1143 | info = g_new0(SnapshotInfo, 1); |
44e3e053 WX |
1144 | info->id = g_strdup(sn.id_str); |
1145 | info->name = g_strdup(sn.name); | |
1146 | info->date_nsec = sn.date_nsec; | |
1147 | info->date_sec = sn.date_sec; | |
1148 | info->vm_state_size = sn.vm_state_size; | |
1149 | info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; | |
1150 | info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; | |
1151 | ||
1152 | return info; | |
1153 | } | |
8802d1fd JC |
1154 | |
1155 | /* New and old BlockDriverState structs for group snapshots */ | |
ba0c86a3 | 1156 | |
ba5d6ab6 | 1157 | typedef struct BlkTransactionState BlkTransactionState; |
ba0c86a3 WX |
1158 | |
1159 | /* Only prepare() may fail. In a single transaction, only one of commit() or | |
1160 | abort() will be called, clean() will always be called if it present. */ | |
1161 | typedef struct BdrvActionOps { | |
1162 | /* Size of state struct, in bytes. */ | |
1163 | size_t instance_size; | |
1164 | /* Prepare the work, must NOT be NULL. */ | |
ba5d6ab6 | 1165 | void (*prepare)(BlkTransactionState *common, Error **errp); |
f9ea81e8 | 1166 | /* Commit the changes, can be NULL. */ |
ba5d6ab6 | 1167 | void (*commit)(BlkTransactionState *common); |
ba0c86a3 | 1168 | /* Abort the changes on fail, can be NULL. */ |
ba5d6ab6 | 1169 | void (*abort)(BlkTransactionState *common); |
ba0c86a3 | 1170 | /* Clean up resource in the end, can be NULL. */ |
ba5d6ab6 | 1171 | void (*clean)(BlkTransactionState *common); |
ba0c86a3 WX |
1172 | } BdrvActionOps; |
1173 | ||
1174 | /* | |
1175 | * This structure must be arranged as first member in child type, assuming | |
1176 | * that compiler will also arrange it to the same address with parent instance. | |
1177 | * Later it will be used in free(). | |
1178 | */ | |
ba5d6ab6 | 1179 | struct BlkTransactionState { |
c8a83e85 | 1180 | TransactionAction *action; |
ba0c86a3 | 1181 | const BdrvActionOps *ops; |
ba5d6ab6 | 1182 | QSIMPLEQ_ENTRY(BlkTransactionState) entry; |
ba0c86a3 WX |
1183 | }; |
1184 | ||
bbe86010 WX |
1185 | /* internal snapshot private data */ |
1186 | typedef struct InternalSnapshotState { | |
1187 | BlkTransactionState common; | |
1188 | BlockDriverState *bs; | |
1189 | QEMUSnapshotInfo sn; | |
1190 | } InternalSnapshotState; | |
1191 | ||
1192 | static void internal_snapshot_prepare(BlkTransactionState *common, | |
1193 | Error **errp) | |
1194 | { | |
f70edf99 | 1195 | Error *local_err = NULL; |
bbe86010 WX |
1196 | const char *device; |
1197 | const char *name; | |
1198 | BlockDriverState *bs; | |
1199 | QEMUSnapshotInfo old_sn, *sn; | |
1200 | bool ret; | |
1201 | qemu_timeval tv; | |
1202 | BlockdevSnapshotInternal *internal; | |
1203 | InternalSnapshotState *state; | |
1204 | int ret1; | |
1205 | ||
1206 | g_assert(common->action->kind == | |
1207 | TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); | |
1208 | internal = common->action->blockdev_snapshot_internal_sync; | |
1209 | state = DO_UPCAST(InternalSnapshotState, common, common); | |
1210 | ||
1211 | /* 1. parse input */ | |
1212 | device = internal->device; | |
1213 | name = internal->name; | |
1214 | ||
1215 | /* 2. check for validation */ | |
1216 | bs = bdrv_find(device); | |
1217 | if (!bs) { | |
1218 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1219 | return; | |
1220 | } | |
1221 | ||
1222 | if (!bdrv_is_inserted(bs)) { | |
1223 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1224 | return; | |
1225 | } | |
1226 | ||
1227 | if (bdrv_is_read_only(bs)) { | |
1228 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); | |
1229 | return; | |
1230 | } | |
1231 | ||
1232 | if (!bdrv_can_snapshot(bs)) { | |
1233 | error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, | |
1234 | bs->drv->format_name, device, "internal snapshot"); | |
1235 | return; | |
1236 | } | |
1237 | ||
1238 | if (!strlen(name)) { | |
1239 | error_setg(errp, "Name is empty"); | |
1240 | return; | |
1241 | } | |
1242 | ||
1243 | /* check whether a snapshot with name exist */ | |
f70edf99 MA |
1244 | ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, |
1245 | &local_err); | |
1246 | if (local_err) { | |
1247 | error_propagate(errp, local_err); | |
bbe86010 WX |
1248 | return; |
1249 | } else if (ret) { | |
1250 | error_setg(errp, | |
1251 | "Snapshot with name '%s' already exists on device '%s'", | |
1252 | name, device); | |
1253 | return; | |
1254 | } | |
1255 | ||
1256 | /* 3. take the snapshot */ | |
1257 | sn = &state->sn; | |
1258 | pstrcpy(sn->name, sizeof(sn->name), name); | |
1259 | qemu_gettimeofday(&tv); | |
1260 | sn->date_sec = tv.tv_sec; | |
1261 | sn->date_nsec = tv.tv_usec * 1000; | |
1262 | sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
1263 | ||
1264 | ret1 = bdrv_snapshot_create(bs, sn); | |
1265 | if (ret1 < 0) { | |
1266 | error_setg_errno(errp, -ret1, | |
1267 | "Failed to create snapshot '%s' on device '%s'", | |
1268 | name, device); | |
1269 | return; | |
1270 | } | |
1271 | ||
1272 | /* 4. succeed, mark a snapshot is created */ | |
1273 | state->bs = bs; | |
1274 | } | |
1275 | ||
1276 | static void internal_snapshot_abort(BlkTransactionState *common) | |
1277 | { | |
1278 | InternalSnapshotState *state = | |
1279 | DO_UPCAST(InternalSnapshotState, common, common); | |
1280 | BlockDriverState *bs = state->bs; | |
1281 | QEMUSnapshotInfo *sn = &state->sn; | |
1282 | Error *local_error = NULL; | |
1283 | ||
1284 | if (!bs) { | |
1285 | return; | |
1286 | } | |
1287 | ||
1288 | if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { | |
1289 | error_report("Failed to delete snapshot with id '%s' and name '%s' on " | |
1290 | "device '%s' in abort: %s", | |
1291 | sn->id_str, | |
1292 | sn->name, | |
1293 | bdrv_get_device_name(bs), | |
1294 | error_get_pretty(local_error)); | |
1295 | error_free(local_error); | |
1296 | } | |
1297 | } | |
1298 | ||
ba0c86a3 | 1299 | /* external snapshot private data */ |
ba5d6ab6 SH |
1300 | typedef struct ExternalSnapshotState { |
1301 | BlkTransactionState common; | |
8802d1fd JC |
1302 | BlockDriverState *old_bs; |
1303 | BlockDriverState *new_bs; | |
ba5d6ab6 | 1304 | } ExternalSnapshotState; |
8802d1fd | 1305 | |
ba5d6ab6 | 1306 | static void external_snapshot_prepare(BlkTransactionState *common, |
9b9877ee WX |
1307 | Error **errp) |
1308 | { | |
9b9877ee WX |
1309 | BlockDriver *drv; |
1310 | int flags, ret; | |
0901f67e | 1311 | QDict *options = NULL; |
9b9877ee | 1312 | Error *local_err = NULL; |
0901f67e | 1313 | bool has_device = false; |
e2a31e87 | 1314 | const char *device; |
0901f67e BC |
1315 | bool has_node_name = false; |
1316 | const char *node_name; | |
1317 | bool has_snapshot_node_name = false; | |
1318 | const char *snapshot_node_name; | |
e2a31e87 WX |
1319 | const char *new_image_file; |
1320 | const char *format = "qcow2"; | |
1321 | enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
ba5d6ab6 SH |
1322 | ExternalSnapshotState *state = |
1323 | DO_UPCAST(ExternalSnapshotState, common, common); | |
c8a83e85 | 1324 | TransactionAction *action = common->action; |
9b9877ee | 1325 | |
e2a31e87 | 1326 | /* get parameters */ |
c8a83e85 | 1327 | g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); |
e2a31e87 | 1328 | |
0901f67e | 1329 | has_device = action->blockdev_snapshot_sync->has_device; |
e2a31e87 | 1330 | device = action->blockdev_snapshot_sync->device; |
0901f67e BC |
1331 | has_node_name = action->blockdev_snapshot_sync->has_node_name; |
1332 | node_name = action->blockdev_snapshot_sync->node_name; | |
1333 | has_snapshot_node_name = | |
1334 | action->blockdev_snapshot_sync->has_snapshot_node_name; | |
1335 | snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name; | |
1336 | ||
e2a31e87 WX |
1337 | new_image_file = action->blockdev_snapshot_sync->snapshot_file; |
1338 | if (action->blockdev_snapshot_sync->has_format) { | |
1339 | format = action->blockdev_snapshot_sync->format; | |
1340 | } | |
1341 | if (action->blockdev_snapshot_sync->has_mode) { | |
1342 | mode = action->blockdev_snapshot_sync->mode; | |
1343 | } | |
1344 | ||
1345 | /* start processing */ | |
9b9877ee WX |
1346 | drv = bdrv_find_format(format); |
1347 | if (!drv) { | |
1348 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
1349 | return; | |
1350 | } | |
1351 | ||
0901f67e BC |
1352 | state->old_bs = bdrv_lookup_bs(has_device ? device : NULL, |
1353 | has_node_name ? node_name : NULL, | |
1354 | &local_err); | |
84d18f06 | 1355 | if (local_err) { |
0901f67e BC |
1356 | error_propagate(errp, local_err); |
1357 | return; | |
1358 | } | |
1359 | ||
1360 | if (has_node_name && !has_snapshot_node_name) { | |
1361 | error_setg(errp, "New snapshot node name missing"); | |
1362 | return; | |
1363 | } | |
1364 | ||
1365 | if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) { | |
1366 | error_setg(errp, "New snapshot node name already existing"); | |
9b9877ee WX |
1367 | return; |
1368 | } | |
1369 | ||
ba5d6ab6 | 1370 | if (!bdrv_is_inserted(state->old_bs)) { |
9b9877ee WX |
1371 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); |
1372 | return; | |
1373 | } | |
1374 | ||
3718d8ab FZ |
1375 | if (bdrv_op_is_blocked(state->old_bs, |
1376 | BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { | |
9b9877ee WX |
1377 | return; |
1378 | } | |
1379 | ||
ba5d6ab6 SH |
1380 | if (!bdrv_is_read_only(state->old_bs)) { |
1381 | if (bdrv_flush(state->old_bs)) { | |
9b9877ee WX |
1382 | error_set(errp, QERR_IO_ERROR); |
1383 | return; | |
1384 | } | |
1385 | } | |
1386 | ||
212a5a8f | 1387 | if (!bdrv_is_first_non_filter(state->old_bs)) { |
f6186f49 BC |
1388 | error_set(errp, QERR_FEATURE_DISABLED, "snapshot"); |
1389 | return; | |
1390 | } | |
1391 | ||
ba5d6ab6 | 1392 | flags = state->old_bs->open_flags; |
9b9877ee | 1393 | |
9b9877ee WX |
1394 | /* create new image w/backing file */ |
1395 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
1396 | bdrv_img_create(new_image_file, format, | |
ba5d6ab6 SH |
1397 | state->old_bs->filename, |
1398 | state->old_bs->drv->format_name, | |
9b9877ee | 1399 | NULL, -1, flags, &local_err, false); |
84d18f06 | 1400 | if (local_err) { |
9b9877ee WX |
1401 | error_propagate(errp, local_err); |
1402 | return; | |
1403 | } | |
1404 | } | |
1405 | ||
0901f67e BC |
1406 | if (has_snapshot_node_name) { |
1407 | options = qdict_new(); | |
1408 | qdict_put(options, "node-name", | |
1409 | qstring_from_str(snapshot_node_name)); | |
1410 | } | |
1411 | ||
9b9877ee WX |
1412 | /* TODO Inherit bs->options or only take explicit options with an |
1413 | * extended QMP command? */ | |
f67503e5 | 1414 | assert(state->new_bs == NULL); |
ddf5636d | 1415 | ret = bdrv_open(&state->new_bs, new_image_file, NULL, options, |
34b5d2c6 | 1416 | flags | BDRV_O_NO_BACKING, drv, &local_err); |
f67503e5 | 1417 | /* We will manually add the backing_hd field to the bs later */ |
9b9877ee | 1418 | if (ret != 0) { |
34b5d2c6 | 1419 | error_propagate(errp, local_err); |
9b9877ee WX |
1420 | } |
1421 | } | |
1422 | ||
ba5d6ab6 | 1423 | static void external_snapshot_commit(BlkTransactionState *common) |
3b0047e8 | 1424 | { |
ba5d6ab6 SH |
1425 | ExternalSnapshotState *state = |
1426 | DO_UPCAST(ExternalSnapshotState, common, common); | |
ba0c86a3 | 1427 | |
ba5d6ab6 SH |
1428 | /* This removes our old bs and adds the new bs */ |
1429 | bdrv_append(state->new_bs, state->old_bs); | |
3b0047e8 WX |
1430 | /* We don't need (or want) to use the transactional |
1431 | * bdrv_reopen_multiple() across all the entries at once, because we | |
1432 | * don't want to abort all of them if one of them fails the reopen */ | |
ba5d6ab6 | 1433 | bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR, |
3b0047e8 WX |
1434 | NULL); |
1435 | } | |
1436 | ||
ba5d6ab6 | 1437 | static void external_snapshot_abort(BlkTransactionState *common) |
96b86bf7 | 1438 | { |
ba5d6ab6 SH |
1439 | ExternalSnapshotState *state = |
1440 | DO_UPCAST(ExternalSnapshotState, common, common); | |
1441 | if (state->new_bs) { | |
4f6fd349 | 1442 | bdrv_unref(state->new_bs); |
96b86bf7 WX |
1443 | } |
1444 | } | |
1445 | ||
3037f364 SH |
1446 | typedef struct DriveBackupState { |
1447 | BlkTransactionState common; | |
1448 | BlockDriverState *bs; | |
1449 | BlockJob *job; | |
1450 | } DriveBackupState; | |
1451 | ||
1452 | static void drive_backup_prepare(BlkTransactionState *common, Error **errp) | |
1453 | { | |
1454 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1455 | DriveBackup *backup; | |
1456 | Error *local_err = NULL; | |
1457 | ||
1458 | assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); | |
1459 | backup = common->action->drive_backup; | |
1460 | ||
1461 | qmp_drive_backup(backup->device, backup->target, | |
1462 | backup->has_format, backup->format, | |
b53169ea | 1463 | backup->sync, |
3037f364 SH |
1464 | backup->has_mode, backup->mode, |
1465 | backup->has_speed, backup->speed, | |
1466 | backup->has_on_source_error, backup->on_source_error, | |
1467 | backup->has_on_target_error, backup->on_target_error, | |
1468 | &local_err); | |
84d18f06 | 1469 | if (local_err) { |
3037f364 SH |
1470 | error_propagate(errp, local_err); |
1471 | state->bs = NULL; | |
1472 | state->job = NULL; | |
1473 | return; | |
1474 | } | |
1475 | ||
1476 | state->bs = bdrv_find(backup->device); | |
1477 | state->job = state->bs->job; | |
1478 | } | |
1479 | ||
1480 | static void drive_backup_abort(BlkTransactionState *common) | |
1481 | { | |
1482 | DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); | |
1483 | BlockDriverState *bs = state->bs; | |
1484 | ||
1485 | /* Only cancel if it's the job we started */ | |
1486 | if (bs && bs->job && bs->job == state->job) { | |
1487 | block_job_cancel_sync(bs->job); | |
1488 | } | |
1489 | } | |
1490 | ||
78b18b78 SH |
1491 | static void abort_prepare(BlkTransactionState *common, Error **errp) |
1492 | { | |
1493 | error_setg(errp, "Transaction aborted using Abort action"); | |
1494 | } | |
1495 | ||
1496 | static void abort_commit(BlkTransactionState *common) | |
1497 | { | |
dfc6f865 | 1498 | g_assert_not_reached(); /* this action never succeeds */ |
78b18b78 SH |
1499 | } |
1500 | ||
ba0c86a3 | 1501 | static const BdrvActionOps actions[] = { |
c8a83e85 | 1502 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { |
ba5d6ab6 | 1503 | .instance_size = sizeof(ExternalSnapshotState), |
ba0c86a3 WX |
1504 | .prepare = external_snapshot_prepare, |
1505 | .commit = external_snapshot_commit, | |
1506 | .abort = external_snapshot_abort, | |
1507 | }, | |
3037f364 SH |
1508 | [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { |
1509 | .instance_size = sizeof(DriveBackupState), | |
1510 | .prepare = drive_backup_prepare, | |
1511 | .abort = drive_backup_abort, | |
1512 | }, | |
78b18b78 SH |
1513 | [TRANSACTION_ACTION_KIND_ABORT] = { |
1514 | .instance_size = sizeof(BlkTransactionState), | |
1515 | .prepare = abort_prepare, | |
1516 | .commit = abort_commit, | |
1517 | }, | |
bbe86010 WX |
1518 | [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { |
1519 | .instance_size = sizeof(InternalSnapshotState), | |
1520 | .prepare = internal_snapshot_prepare, | |
1521 | .abort = internal_snapshot_abort, | |
1522 | }, | |
ba0c86a3 WX |
1523 | }; |
1524 | ||
8802d1fd JC |
1525 | /* |
1526 | * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail | |
1527 | * then we do not pivot any of the devices in the group, and abandon the | |
1528 | * snapshots | |
1529 | */ | |
c8a83e85 | 1530 | void qmp_transaction(TransactionActionList *dev_list, Error **errp) |
8802d1fd | 1531 | { |
c8a83e85 | 1532 | TransactionActionList *dev_entry = dev_list; |
ba5d6ab6 | 1533 | BlkTransactionState *state, *next; |
43e17041 | 1534 | Error *local_err = NULL; |
8802d1fd | 1535 | |
ba5d6ab6 | 1536 | QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; |
8802d1fd JC |
1537 | QSIMPLEQ_INIT(&snap_bdrv_states); |
1538 | ||
1539 | /* drain all i/o before any snapshots */ | |
1540 | bdrv_drain_all(); | |
1541 | ||
1542 | /* We don't do anything in this loop that commits us to the snapshot */ | |
1543 | while (NULL != dev_entry) { | |
c8a83e85 | 1544 | TransactionAction *dev_info = NULL; |
ba0c86a3 | 1545 | const BdrvActionOps *ops; |
52e7c241 | 1546 | |
8802d1fd JC |
1547 | dev_info = dev_entry->value; |
1548 | dev_entry = dev_entry->next; | |
1549 | ||
ba0c86a3 WX |
1550 | assert(dev_info->kind < ARRAY_SIZE(actions)); |
1551 | ||
1552 | ops = &actions[dev_info->kind]; | |
aa3fe714 HR |
1553 | assert(ops->instance_size > 0); |
1554 | ||
ba5d6ab6 SH |
1555 | state = g_malloc0(ops->instance_size); |
1556 | state->ops = ops; | |
1557 | state->action = dev_info; | |
1558 | QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); | |
8802d1fd | 1559 | |
ba5d6ab6 | 1560 | state->ops->prepare(state, &local_err); |
84d18f06 | 1561 | if (local_err) { |
ba0c86a3 WX |
1562 | error_propagate(errp, local_err); |
1563 | goto delete_and_fail; | |
8802d1fd | 1564 | } |
8802d1fd JC |
1565 | } |
1566 | ||
ba5d6ab6 | 1567 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
f9ea81e8 SH |
1568 | if (state->ops->commit) { |
1569 | state->ops->commit(state); | |
1570 | } | |
8802d1fd JC |
1571 | } |
1572 | ||
1573 | /* success */ | |
1574 | goto exit; | |
1575 | ||
1576 | delete_and_fail: | |
1577 | /* | |
1578 | * failure, and it is all-or-none; abandon each new bs, and keep using | |
1579 | * the original bs for all images | |
1580 | */ | |
ba5d6ab6 SH |
1581 | QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { |
1582 | if (state->ops->abort) { | |
1583 | state->ops->abort(state); | |
ba0c86a3 | 1584 | } |
8802d1fd JC |
1585 | } |
1586 | exit: | |
ba5d6ab6 SH |
1587 | QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { |
1588 | if (state->ops->clean) { | |
1589 | state->ops->clean(state); | |
ba0c86a3 | 1590 | } |
ba5d6ab6 | 1591 | g_free(state); |
8802d1fd | 1592 | } |
8802d1fd JC |
1593 | } |
1594 | ||
1595 | ||
6007cdd4 | 1596 | static void eject_device(BlockBackend *blk, int force, Error **errp) |
666daa68 | 1597 | { |
6007cdd4 MA |
1598 | BlockDriverState *bs = blk_bs(blk); |
1599 | ||
3718d8ab | 1600 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { |
2d3735d3 SH |
1601 | return; |
1602 | } | |
a7f53e26 | 1603 | if (!blk_dev_has_removable_media(blk)) { |
f231b88d CR |
1604 | error_setg(errp, "Device '%s' is not removable", |
1605 | bdrv_get_device_name(bs)); | |
92d48558 | 1606 | return; |
ea8f942f | 1607 | } |
92d48558 | 1608 | |
a7f53e26 MA |
1609 | if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) { |
1610 | blk_dev_eject_request(blk, force); | |
025ccaa7 | 1611 | if (!force) { |
f231b88d CR |
1612 | error_setg(errp, "Device '%s' is locked", |
1613 | bdrv_get_device_name(bs)); | |
92d48558 | 1614 | return; |
025ccaa7 | 1615 | } |
666daa68 | 1616 | } |
92d48558 | 1617 | |
3b5276b5 | 1618 | bdrv_close(bs); |
666daa68 MA |
1619 | } |
1620 | ||
c245b6a3 | 1621 | void qmp_eject(const char *device, bool has_force, bool force, Error **errp) |
666daa68 | 1622 | { |
6007cdd4 | 1623 | BlockBackend *blk; |
666daa68 | 1624 | |
6007cdd4 MA |
1625 | blk = blk_by_name(device); |
1626 | if (!blk) { | |
c245b6a3 LC |
1627 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1628 | return; | |
92d48558 LC |
1629 | } |
1630 | ||
6007cdd4 | 1631 | eject_device(blk, force, errp); |
666daa68 MA |
1632 | } |
1633 | ||
12d3ba82 BC |
1634 | void qmp_block_passwd(bool has_device, const char *device, |
1635 | bool has_node_name, const char *node_name, | |
1636 | const char *password, Error **errp) | |
666daa68 | 1637 | { |
12d3ba82 | 1638 | Error *local_err = NULL; |
666daa68 MA |
1639 | BlockDriverState *bs; |
1640 | int err; | |
1641 | ||
12d3ba82 BC |
1642 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
1643 | has_node_name ? node_name : NULL, | |
1644 | &local_err); | |
84d18f06 | 1645 | if (local_err) { |
12d3ba82 | 1646 | error_propagate(errp, local_err); |
a4dea8a9 | 1647 | return; |
666daa68 MA |
1648 | } |
1649 | ||
a4dea8a9 | 1650 | err = bdrv_set_key(bs, password); |
666daa68 | 1651 | if (err == -EINVAL) { |
a4dea8a9 LC |
1652 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); |
1653 | return; | |
666daa68 | 1654 | } else if (err < 0) { |
a4dea8a9 LC |
1655 | error_set(errp, QERR_INVALID_PASSWORD); |
1656 | return; | |
666daa68 | 1657 | } |
666daa68 MA |
1658 | } |
1659 | ||
333a96ec LC |
1660 | static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, |
1661 | int bdrv_flags, BlockDriver *drv, | |
1662 | const char *password, Error **errp) | |
1663 | { | |
34b5d2c6 | 1664 | Error *local_err = NULL; |
0eef407c LC |
1665 | int ret; |
1666 | ||
ddf5636d | 1667 | ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err); |
0eef407c | 1668 | if (ret < 0) { |
34b5d2c6 | 1669 | error_propagate(errp, local_err); |
333a96ec LC |
1670 | return; |
1671 | } | |
1672 | ||
1673 | if (bdrv_key_required(bs)) { | |
1674 | if (password) { | |
1675 | if (bdrv_set_key(bs, password) < 0) { | |
1676 | error_set(errp, QERR_INVALID_PASSWORD); | |
1677 | } | |
1678 | } else { | |
1679 | error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), | |
1680 | bdrv_get_encrypted_filename(bs)); | |
1681 | } | |
1682 | } else if (password) { | |
1683 | error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); | |
1684 | } | |
1685 | } | |
1686 | ||
1687 | void qmp_change_blockdev(const char *device, const char *filename, | |
314f7ea7 | 1688 | const char *format, Error **errp) |
666daa68 | 1689 | { |
6007cdd4 | 1690 | BlockBackend *blk; |
666daa68 MA |
1691 | BlockDriverState *bs; |
1692 | BlockDriver *drv = NULL; | |
1693 | int bdrv_flags; | |
92d48558 | 1694 | Error *err = NULL; |
666daa68 | 1695 | |
6007cdd4 MA |
1696 | blk = blk_by_name(device); |
1697 | if (!blk) { | |
333a96ec LC |
1698 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1699 | return; | |
666daa68 | 1700 | } |
6007cdd4 | 1701 | bs = blk_bs(blk); |
333a96ec LC |
1702 | |
1703 | if (format) { | |
b64ec4e4 | 1704 | drv = bdrv_find_whitelisted_format(format, bs->read_only); |
666daa68 | 1705 | if (!drv) { |
333a96ec LC |
1706 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); |
1707 | return; | |
666daa68 MA |
1708 | } |
1709 | } | |
333a96ec | 1710 | |
6007cdd4 | 1711 | eject_device(blk, 0, &err); |
84d18f06 | 1712 | if (err) { |
333a96ec LC |
1713 | error_propagate(errp, err); |
1714 | return; | |
666daa68 | 1715 | } |
333a96ec | 1716 | |
528f7663 | 1717 | bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; |
199630b6 | 1718 | bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; |
333a96ec LC |
1719 | |
1720 | qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); | |
666daa68 | 1721 | } |
9063f814 | 1722 | |
727f005e | 1723 | /* throttling disk I/O limits */ |
80047da5 | 1724 | void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, |
3e9fab69 BC |
1725 | int64_t bps_wr, |
1726 | int64_t iops, | |
1727 | int64_t iops_rd, | |
1728 | int64_t iops_wr, | |
1729 | bool has_bps_max, | |
1730 | int64_t bps_max, | |
1731 | bool has_bps_rd_max, | |
1732 | int64_t bps_rd_max, | |
1733 | bool has_bps_wr_max, | |
1734 | int64_t bps_wr_max, | |
1735 | bool has_iops_max, | |
1736 | int64_t iops_max, | |
1737 | bool has_iops_rd_max, | |
1738 | int64_t iops_rd_max, | |
1739 | bool has_iops_wr_max, | |
2024c1df BC |
1740 | int64_t iops_wr_max, |
1741 | bool has_iops_size, | |
1742 | int64_t iops_size, Error **errp) | |
727f005e | 1743 | { |
cc0681c4 | 1744 | ThrottleConfig cfg; |
727f005e | 1745 | BlockDriverState *bs; |
b15446fd | 1746 | AioContext *aio_context; |
727f005e | 1747 | |
80047da5 | 1748 | bs = bdrv_find(device); |
727f005e | 1749 | if (!bs) { |
80047da5 LC |
1750 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); |
1751 | return; | |
727f005e ZYW |
1752 | } |
1753 | ||
cc0681c4 BC |
1754 | memset(&cfg, 0, sizeof(cfg)); |
1755 | cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; | |
1756 | cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; | |
1757 | cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; | |
1758 | ||
1759 | cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; | |
1760 | cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; | |
1761 | cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; | |
1762 | ||
3e9fab69 BC |
1763 | if (has_bps_max) { |
1764 | cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; | |
1765 | } | |
1766 | if (has_bps_rd_max) { | |
1767 | cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; | |
1768 | } | |
1769 | if (has_bps_wr_max) { | |
1770 | cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; | |
1771 | } | |
1772 | if (has_iops_max) { | |
1773 | cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; | |
1774 | } | |
1775 | if (has_iops_rd_max) { | |
1776 | cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; | |
1777 | } | |
1778 | if (has_iops_wr_max) { | |
1779 | cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; | |
1780 | } | |
727f005e | 1781 | |
2024c1df BC |
1782 | if (has_iops_size) { |
1783 | cfg.op_size = iops_size; | |
1784 | } | |
cc0681c4 BC |
1785 | |
1786 | if (!check_throttle_config(&cfg, errp)) { | |
80047da5 | 1787 | return; |
727f005e ZYW |
1788 | } |
1789 | ||
b15446fd SH |
1790 | aio_context = bdrv_get_aio_context(bs); |
1791 | aio_context_acquire(aio_context); | |
1792 | ||
cc0681c4 | 1793 | if (!bs->io_limits_enabled && throttle_enabled(&cfg)) { |
727f005e | 1794 | bdrv_io_limits_enable(bs); |
cc0681c4 | 1795 | } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) { |
727f005e | 1796 | bdrv_io_limits_disable(bs); |
cc0681c4 BC |
1797 | } |
1798 | ||
1799 | if (bs->io_limits_enabled) { | |
1800 | bdrv_set_io_limits(bs, &cfg); | |
727f005e | 1801 | } |
b15446fd SH |
1802 | |
1803 | aio_context_release(aio_context); | |
727f005e ZYW |
1804 | } |
1805 | ||
9063f814 RH |
1806 | int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1807 | { | |
1808 | const char *id = qdict_get_str(qdict, "id"); | |
7e7d56d9 | 1809 | BlockBackend *blk; |
9063f814 | 1810 | BlockDriverState *bs; |
8ad4202b | 1811 | AioContext *aio_context; |
3718d8ab | 1812 | Error *local_err = NULL; |
9063f814 | 1813 | |
7e7d56d9 MA |
1814 | blk = blk_by_name(id); |
1815 | if (!blk) { | |
b1422f20 | 1816 | error_report("Device '%s' not found", id); |
9063f814 RH |
1817 | return -1; |
1818 | } | |
7e7d56d9 | 1819 | bs = blk_bs(blk); |
8ad4202b | 1820 | |
26f8b3a8 | 1821 | if (!blk_legacy_dinfo(blk)) { |
48f364dd MA |
1822 | error_report("Deleting device added with blockdev-add" |
1823 | " is not supported"); | |
1824 | return -1; | |
1825 | } | |
1826 | ||
8ad4202b SH |
1827 | aio_context = bdrv_get_aio_context(bs); |
1828 | aio_context_acquire(aio_context); | |
1829 | ||
3718d8ab FZ |
1830 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { |
1831 | error_report("%s", error_get_pretty(local_err)); | |
1832 | error_free(local_err); | |
8ad4202b | 1833 | aio_context_release(aio_context); |
8591675f MT |
1834 | return -1; |
1835 | } | |
9063f814 RH |
1836 | |
1837 | /* quiesce block driver; prevent further io */ | |
922453bc | 1838 | bdrv_drain_all(); |
9063f814 RH |
1839 | bdrv_flush(bs); |
1840 | bdrv_close(bs); | |
1841 | ||
fa879d62 | 1842 | /* if we have a device attached to this BlockDriverState |
d22b2f41 RH |
1843 | * then we need to make the drive anonymous until the device |
1844 | * can be removed. If this is a drive with no device backing | |
1845 | * then we can just get rid of the block driver state right here. | |
1846 | */ | |
a7f53e26 | 1847 | if (blk_get_attached_dev(blk)) { |
7e7d56d9 | 1848 | blk_hide_on_behalf_of_do_drive_del(blk); |
293c51a6 SH |
1849 | /* Further I/O must not pause the guest */ |
1850 | bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT, | |
1851 | BLOCKDEV_ON_ERROR_REPORT); | |
d22b2f41 | 1852 | } else { |
b9fe8a7a | 1853 | blk_unref(blk); |
9063f814 RH |
1854 | } |
1855 | ||
8ad4202b | 1856 | aio_context_release(aio_context); |
9063f814 RH |
1857 | return 0; |
1858 | } | |
6d4a2b3a | 1859 | |
3b1dbd11 BC |
1860 | void qmp_block_resize(bool has_device, const char *device, |
1861 | bool has_node_name, const char *node_name, | |
1862 | int64_t size, Error **errp) | |
6d4a2b3a | 1863 | { |
3b1dbd11 | 1864 | Error *local_err = NULL; |
6d4a2b3a | 1865 | BlockDriverState *bs; |
927e0e76 | 1866 | AioContext *aio_context; |
8732901e | 1867 | int ret; |
6d4a2b3a | 1868 | |
3b1dbd11 BC |
1869 | bs = bdrv_lookup_bs(has_device ? device : NULL, |
1870 | has_node_name ? node_name : NULL, | |
1871 | &local_err); | |
84d18f06 | 1872 | if (local_err) { |
3b1dbd11 BC |
1873 | error_propagate(errp, local_err); |
1874 | return; | |
1875 | } | |
1876 | ||
927e0e76 SH |
1877 | aio_context = bdrv_get_aio_context(bs); |
1878 | aio_context_acquire(aio_context); | |
1879 | ||
3b1dbd11 BC |
1880 | if (!bdrv_is_first_non_filter(bs)) { |
1881 | error_set(errp, QERR_FEATURE_DISABLED, "resize"); | |
927e0e76 | 1882 | goto out; |
6d4a2b3a CH |
1883 | } |
1884 | ||
1885 | if (size < 0) { | |
939a1cc3 | 1886 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); |
927e0e76 | 1887 | goto out; |
6d4a2b3a CH |
1888 | } |
1889 | ||
9c75e168 JC |
1890 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { |
1891 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
927e0e76 | 1892 | goto out; |
9c75e168 JC |
1893 | } |
1894 | ||
92b7a08d PL |
1895 | /* complete all in-flight operations before resizing the device */ |
1896 | bdrv_drain_all(); | |
1897 | ||
8732901e KW |
1898 | ret = bdrv_truncate(bs, size); |
1899 | switch (ret) { | |
939a1cc3 SH |
1900 | case 0: |
1901 | break; | |
1902 | case -ENOMEDIUM: | |
1903 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
1904 | break; | |
1905 | case -ENOTSUP: | |
1906 | error_set(errp, QERR_UNSUPPORTED); | |
1907 | break; | |
1908 | case -EACCES: | |
1909 | error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); | |
1910 | break; | |
1911 | case -EBUSY: | |
1912 | error_set(errp, QERR_DEVICE_IN_USE, device); | |
1913 | break; | |
1914 | default: | |
8732901e | 1915 | error_setg_errno(errp, -ret, "Could not resize"); |
939a1cc3 | 1916 | break; |
6d4a2b3a | 1917 | } |
927e0e76 SH |
1918 | |
1919 | out: | |
1920 | aio_context_release(aio_context); | |
6d4a2b3a | 1921 | } |
12bd451f | 1922 | |
9abf2dba | 1923 | static void block_job_cb(void *opaque, int ret) |
12bd451f SH |
1924 | { |
1925 | BlockDriverState *bs = opaque; | |
bcada37b | 1926 | const char *msg = NULL; |
12bd451f | 1927 | |
9abf2dba | 1928 | trace_block_job_cb(bs, bs->job, ret); |
12bd451f SH |
1929 | |
1930 | assert(bs->job); | |
bcada37b | 1931 | |
12bd451f | 1932 | if (ret < 0) { |
bcada37b | 1933 | msg = strerror(-ret); |
12bd451f SH |
1934 | } |
1935 | ||
370521a1 | 1936 | if (block_job_is_cancelled(bs->job)) { |
bcada37b | 1937 | block_job_event_cancelled(bs->job); |
370521a1 | 1938 | } else { |
bcada37b | 1939 | block_job_event_completed(bs->job, msg); |
370521a1 | 1940 | } |
aa398a5c | 1941 | |
fa510ebf | 1942 | bdrv_put_ref_bh_schedule(bs); |
12bd451f SH |
1943 | } |
1944 | ||
13d8cc51 JC |
1945 | void qmp_block_stream(const char *device, |
1946 | bool has_base, const char *base, | |
1947 | bool has_backing_file, const char *backing_file, | |
1948 | bool has_speed, int64_t speed, | |
1d809098 PB |
1949 | bool has_on_error, BlockdevOnError on_error, |
1950 | Error **errp) | |
12bd451f SH |
1951 | { |
1952 | BlockDriverState *bs; | |
c8c3080f | 1953 | BlockDriverState *base_bs = NULL; |
fd7f8c65 | 1954 | Error *local_err = NULL; |
13d8cc51 | 1955 | const char *base_name = NULL; |
12bd451f | 1956 | |
1d809098 PB |
1957 | if (!has_on_error) { |
1958 | on_error = BLOCKDEV_ON_ERROR_REPORT; | |
1959 | } | |
1960 | ||
12bd451f SH |
1961 | bs = bdrv_find(device); |
1962 | if (!bs) { | |
1963 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
1964 | return; | |
1965 | } | |
1966 | ||
628ff683 FZ |
1967 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { |
1968 | return; | |
1969 | } | |
1970 | ||
13d8cc51 | 1971 | if (has_base) { |
c8c3080f MT |
1972 | base_bs = bdrv_find_backing_image(bs, base); |
1973 | if (base_bs == NULL) { | |
1974 | error_set(errp, QERR_BASE_NOT_FOUND, base); | |
1975 | return; | |
1976 | } | |
13d8cc51 | 1977 | base_name = base; |
12bd451f SH |
1978 | } |
1979 | ||
13d8cc51 JC |
1980 | /* if we are streaming the entire chain, the result will have no backing |
1981 | * file, and specifying one is therefore an error */ | |
1982 | if (base_bs == NULL && has_backing_file) { | |
1983 | error_setg(errp, "backing file specified, but streaming the " | |
1984 | "entire chain"); | |
1985 | return; | |
1986 | } | |
1987 | ||
1988 | /* backing_file string overrides base bs filename */ | |
1989 | base_name = has_backing_file ? backing_file : base_name; | |
1990 | ||
1991 | stream_start(bs, base_bs, base_name, has_speed ? speed : 0, | |
1d809098 | 1992 | on_error, block_job_cb, bs, &local_err); |
84d18f06 | 1993 | if (local_err) { |
fd7f8c65 SH |
1994 | error_propagate(errp, local_err); |
1995 | return; | |
12bd451f SH |
1996 | } |
1997 | ||
1998 | trace_qmp_block_stream(bs, bs->job); | |
1999 | } | |
2d47c6e9 | 2000 | |
ed61fc10 | 2001 | void qmp_block_commit(const char *device, |
7676e2c5 JC |
2002 | bool has_base, const char *base, |
2003 | bool has_top, const char *top, | |
54e26900 | 2004 | bool has_backing_file, const char *backing_file, |
ed61fc10 JC |
2005 | bool has_speed, int64_t speed, |
2006 | Error **errp) | |
2007 | { | |
2008 | BlockDriverState *bs; | |
2009 | BlockDriverState *base_bs, *top_bs; | |
2010 | Error *local_err = NULL; | |
2011 | /* This will be part of the QMP command, if/when the | |
2012 | * BlockdevOnError change for blkmirror makes it in | |
2013 | */ | |
92aa5c6d | 2014 | BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; |
ed61fc10 | 2015 | |
54504663 HR |
2016 | if (!has_speed) { |
2017 | speed = 0; | |
2018 | } | |
2019 | ||
ed61fc10 JC |
2020 | /* drain all i/o before commits */ |
2021 | bdrv_drain_all(); | |
2022 | ||
7676e2c5 JC |
2023 | /* Important Note: |
2024 | * libvirt relies on the DeviceNotFound error class in order to probe for | |
2025 | * live commit feature versions; for this to work, we must make sure to | |
2026 | * perform the device lookup before any generic errors that may occur in a | |
2027 | * scenario in which all optional arguments are omitted. */ | |
ed61fc10 JC |
2028 | bs = bdrv_find(device); |
2029 | if (!bs) { | |
2030 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2031 | return; | |
628ff683 FZ |
2032 | } |
2033 | ||
2034 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, errp)) { | |
2035 | return; | |
ed61fc10 | 2036 | } |
ed61fc10 JC |
2037 | |
2038 | /* default top_bs is the active layer */ | |
2039 | top_bs = bs; | |
2040 | ||
7676e2c5 | 2041 | if (has_top && top) { |
ed61fc10 JC |
2042 | if (strcmp(bs->filename, top) != 0) { |
2043 | top_bs = bdrv_find_backing_image(bs, top); | |
2044 | } | |
2045 | } | |
2046 | ||
2047 | if (top_bs == NULL) { | |
2048 | error_setg(errp, "Top image file %s not found", top ? top : "NULL"); | |
2049 | return; | |
2050 | } | |
2051 | ||
d5208c45 JC |
2052 | if (has_base && base) { |
2053 | base_bs = bdrv_find_backing_image(top_bs, base); | |
2054 | } else { | |
2055 | base_bs = bdrv_find_base(top_bs); | |
2056 | } | |
2057 | ||
2058 | if (base_bs == NULL) { | |
2059 | error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); | |
2060 | return; | |
2061 | } | |
2062 | ||
7676e2c5 JC |
2063 | /* Do not allow attempts to commit an image into itself */ |
2064 | if (top_bs == base_bs) { | |
2065 | error_setg(errp, "cannot commit an image into itself"); | |
2066 | return; | |
2067 | } | |
2068 | ||
20a63d2c | 2069 | if (top_bs == bs) { |
54e26900 JC |
2070 | if (has_backing_file) { |
2071 | error_setg(errp, "'backing-file' specified," | |
2072 | " but 'top' is the active layer"); | |
2073 | return; | |
2074 | } | |
20a63d2c FZ |
2075 | commit_active_start(bs, base_bs, speed, on_error, block_job_cb, |
2076 | bs, &local_err); | |
2077 | } else { | |
2078 | commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, | |
54e26900 | 2079 | has_backing_file ? backing_file : NULL, &local_err); |
20a63d2c | 2080 | } |
ed61fc10 JC |
2081 | if (local_err != NULL) { |
2082 | error_propagate(errp, local_err); | |
2083 | return; | |
2084 | } | |
ed61fc10 JC |
2085 | } |
2086 | ||
99a9addf SH |
2087 | void qmp_drive_backup(const char *device, const char *target, |
2088 | bool has_format, const char *format, | |
b53169ea | 2089 | enum MirrorSyncMode sync, |
99a9addf SH |
2090 | bool has_mode, enum NewImageMode mode, |
2091 | bool has_speed, int64_t speed, | |
2092 | bool has_on_source_error, BlockdevOnError on_source_error, | |
2093 | bool has_on_target_error, BlockdevOnError on_target_error, | |
2094 | Error **errp) | |
2095 | { | |
2096 | BlockDriverState *bs; | |
2097 | BlockDriverState *target_bs; | |
fc5d3f84 | 2098 | BlockDriverState *source = NULL; |
99a9addf SH |
2099 | BlockDriver *drv = NULL; |
2100 | Error *local_err = NULL; | |
2101 | int flags; | |
2102 | int64_t size; | |
2103 | int ret; | |
2104 | ||
2105 | if (!has_speed) { | |
2106 | speed = 0; | |
2107 | } | |
2108 | if (!has_on_source_error) { | |
2109 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2110 | } | |
2111 | if (!has_on_target_error) { | |
2112 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2113 | } | |
2114 | if (!has_mode) { | |
2115 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
2116 | } | |
2117 | ||
2118 | bs = bdrv_find(device); | |
2119 | if (!bs) { | |
2120 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2121 | return; | |
2122 | } | |
2123 | ||
2124 | if (!bdrv_is_inserted(bs)) { | |
2125 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
2126 | return; | |
2127 | } | |
2128 | ||
2129 | if (!has_format) { | |
2130 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
2131 | } | |
2132 | if (format) { | |
2133 | drv = bdrv_find_format(format); | |
2134 | if (!drv) { | |
2135 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
2136 | return; | |
2137 | } | |
2138 | } | |
2139 | ||
3718d8ab | 2140 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { |
99a9addf SH |
2141 | return; |
2142 | } | |
2143 | ||
2144 | flags = bs->open_flags | BDRV_O_RDWR; | |
2145 | ||
fc5d3f84 IM |
2146 | /* See if we have a backing HD we can use to create our new image |
2147 | * on top of. */ | |
2148 | if (sync == MIRROR_SYNC_MODE_TOP) { | |
2149 | source = bs->backing_hd; | |
2150 | if (!source) { | |
2151 | sync = MIRROR_SYNC_MODE_FULL; | |
2152 | } | |
2153 | } | |
2154 | if (sync == MIRROR_SYNC_MODE_NONE) { | |
2155 | source = bs; | |
2156 | } | |
2157 | ||
99a9addf SH |
2158 | size = bdrv_getlength(bs); |
2159 | if (size < 0) { | |
2160 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
2161 | return; | |
2162 | } | |
2163 | ||
2164 | if (mode != NEW_IMAGE_MODE_EXISTING) { | |
2165 | assert(format && drv); | |
fc5d3f84 IM |
2166 | if (source) { |
2167 | bdrv_img_create(target, format, source->filename, | |
2168 | source->drv->format_name, NULL, | |
2169 | size, flags, &local_err, false); | |
2170 | } else { | |
2171 | bdrv_img_create(target, format, NULL, NULL, NULL, | |
2172 | size, flags, &local_err, false); | |
2173 | } | |
99a9addf SH |
2174 | } |
2175 | ||
84d18f06 | 2176 | if (local_err) { |
99a9addf SH |
2177 | error_propagate(errp, local_err); |
2178 | return; | |
2179 | } | |
2180 | ||
f67503e5 | 2181 | target_bs = NULL; |
ddf5636d | 2182 | ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err); |
99a9addf | 2183 | if (ret < 0) { |
34b5d2c6 | 2184 | error_propagate(errp, local_err); |
99a9addf SH |
2185 | return; |
2186 | } | |
2187 | ||
fc5d3f84 | 2188 | backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error, |
99a9addf SH |
2189 | block_job_cb, bs, &local_err); |
2190 | if (local_err != NULL) { | |
4f6fd349 | 2191 | bdrv_unref(target_bs); |
99a9addf SH |
2192 | error_propagate(errp, local_err); |
2193 | return; | |
2194 | } | |
99a9addf SH |
2195 | } |
2196 | ||
c13163fb BC |
2197 | BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) |
2198 | { | |
2199 | return bdrv_named_nodes_list(); | |
2200 | } | |
2201 | ||
08e4ed6c PB |
2202 | #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) |
2203 | ||
d9b902db PB |
2204 | void qmp_drive_mirror(const char *device, const char *target, |
2205 | bool has_format, const char *format, | |
4c828dc6 | 2206 | bool has_node_name, const char *node_name, |
09158f00 | 2207 | bool has_replaces, const char *replaces, |
d9b902db PB |
2208 | enum MirrorSyncMode sync, |
2209 | bool has_mode, enum NewImageMode mode, | |
b952b558 | 2210 | bool has_speed, int64_t speed, |
eee13dfe | 2211 | bool has_granularity, uint32_t granularity, |
08e4ed6c | 2212 | bool has_buf_size, int64_t buf_size, |
b952b558 PB |
2213 | bool has_on_source_error, BlockdevOnError on_source_error, |
2214 | bool has_on_target_error, BlockdevOnError on_target_error, | |
2215 | Error **errp) | |
d9b902db | 2216 | { |
d9b902db PB |
2217 | BlockDriverState *bs; |
2218 | BlockDriverState *source, *target_bs; | |
d9b902db PB |
2219 | BlockDriver *drv = NULL; |
2220 | Error *local_err = NULL; | |
4c828dc6 | 2221 | QDict *options = NULL; |
d9b902db | 2222 | int flags; |
ac3c5d83 | 2223 | int64_t size; |
d9b902db PB |
2224 | int ret; |
2225 | ||
2226 | if (!has_speed) { | |
2227 | speed = 0; | |
2228 | } | |
b952b558 PB |
2229 | if (!has_on_source_error) { |
2230 | on_source_error = BLOCKDEV_ON_ERROR_REPORT; | |
2231 | } | |
2232 | if (!has_on_target_error) { | |
2233 | on_target_error = BLOCKDEV_ON_ERROR_REPORT; | |
2234 | } | |
d9b902db PB |
2235 | if (!has_mode) { |
2236 | mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; | |
2237 | } | |
eee13dfe PB |
2238 | if (!has_granularity) { |
2239 | granularity = 0; | |
2240 | } | |
08e4ed6c PB |
2241 | if (!has_buf_size) { |
2242 | buf_size = DEFAULT_MIRROR_BUF_SIZE; | |
2243 | } | |
2244 | ||
eee13dfe | 2245 | if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { |
3cbbe9fd SH |
2246 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", |
2247 | "a value in range [512B, 64MB]"); | |
eee13dfe PB |
2248 | return; |
2249 | } | |
2250 | if (granularity & (granularity - 1)) { | |
3cbbe9fd | 2251 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2"); |
eee13dfe PB |
2252 | return; |
2253 | } | |
d9b902db PB |
2254 | |
2255 | bs = bdrv_find(device); | |
2256 | if (!bs) { | |
2257 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2258 | return; | |
2259 | } | |
2260 | ||
2261 | if (!bdrv_is_inserted(bs)) { | |
2262 | error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); | |
2263 | return; | |
2264 | } | |
2265 | ||
2266 | if (!has_format) { | |
2267 | format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; | |
2268 | } | |
2269 | if (format) { | |
2270 | drv = bdrv_find_format(format); | |
2271 | if (!drv) { | |
2272 | error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); | |
2273 | return; | |
2274 | } | |
2275 | } | |
2276 | ||
3718d8ab | 2277 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { |
d9b902db PB |
2278 | return; |
2279 | } | |
2280 | ||
2281 | flags = bs->open_flags | BDRV_O_RDWR; | |
2282 | source = bs->backing_hd; | |
2283 | if (!source && sync == MIRROR_SYNC_MODE_TOP) { | |
2284 | sync = MIRROR_SYNC_MODE_FULL; | |
2285 | } | |
117e0c82 HR |
2286 | if (sync == MIRROR_SYNC_MODE_NONE) { |
2287 | source = bs; | |
2288 | } | |
d9b902db | 2289 | |
ac3c5d83 SH |
2290 | size = bdrv_getlength(bs); |
2291 | if (size < 0) { | |
2292 | error_setg_errno(errp, -size, "bdrv_getlength failed"); | |
2293 | return; | |
2294 | } | |
2295 | ||
09158f00 BC |
2296 | if (has_replaces) { |
2297 | BlockDriverState *to_replace_bs; | |
2298 | ||
2299 | if (!has_node_name) { | |
2300 | error_setg(errp, "a node-name must be provided when replacing a" | |
2301 | " named node of the graph"); | |
2302 | return; | |
2303 | } | |
2304 | ||
2305 | to_replace_bs = check_to_replace_node(replaces, &local_err); | |
2306 | ||
2307 | if (!to_replace_bs) { | |
2308 | error_propagate(errp, local_err); | |
2309 | return; | |
2310 | } | |
2311 | ||
2312 | if (size != bdrv_getlength(to_replace_bs)) { | |
2313 | error_setg(errp, "cannot replace image with a mirror image of " | |
2314 | "different size"); | |
2315 | return; | |
2316 | } | |
2317 | } | |
2318 | ||
14526864 HR |
2319 | if ((sync == MIRROR_SYNC_MODE_FULL || !source) |
2320 | && mode != NEW_IMAGE_MODE_EXISTING) | |
2321 | { | |
d9b902db PB |
2322 | /* create new image w/o backing file */ |
2323 | assert(format && drv); | |
cf8f2426 | 2324 | bdrv_img_create(target, format, |
f382d43a | 2325 | NULL, NULL, NULL, size, flags, &local_err, false); |
d9b902db PB |
2326 | } else { |
2327 | switch (mode) { | |
2328 | case NEW_IMAGE_MODE_EXISTING: | |
d9b902db PB |
2329 | break; |
2330 | case NEW_IMAGE_MODE_ABSOLUTE_PATHS: | |
2331 | /* create new image with backing file */ | |
cf8f2426 LC |
2332 | bdrv_img_create(target, format, |
2333 | source->filename, | |
2334 | source->drv->format_name, | |
f382d43a | 2335 | NULL, size, flags, &local_err, false); |
d9b902db PB |
2336 | break; |
2337 | default: | |
2338 | abort(); | |
2339 | } | |
2340 | } | |
2341 | ||
84d18f06 | 2342 | if (local_err) { |
cf8f2426 | 2343 | error_propagate(errp, local_err); |
d9b902db PB |
2344 | return; |
2345 | } | |
2346 | ||
4c828dc6 BC |
2347 | if (has_node_name) { |
2348 | options = qdict_new(); | |
2349 | qdict_put(options, "node-name", qstring_from_str(node_name)); | |
2350 | } | |
2351 | ||
b812f671 PB |
2352 | /* Mirroring takes care of copy-on-write using the source's backing |
2353 | * file. | |
2354 | */ | |
f67503e5 | 2355 | target_bs = NULL; |
4c828dc6 BC |
2356 | ret = bdrv_open(&target_bs, target, NULL, options, |
2357 | flags | BDRV_O_NO_BACKING, drv, &local_err); | |
d9b902db | 2358 | if (ret < 0) { |
34b5d2c6 | 2359 | error_propagate(errp, local_err); |
d9b902db PB |
2360 | return; |
2361 | } | |
2362 | ||
09158f00 BC |
2363 | /* pass the node name to replace to mirror start since it's loose coupling |
2364 | * and will allow to check whether the node still exist at mirror completion | |
2365 | */ | |
2366 | mirror_start(bs, target_bs, | |
2367 | has_replaces ? replaces : NULL, | |
2368 | speed, granularity, buf_size, sync, | |
eee13dfe | 2369 | on_source_error, on_target_error, |
b952b558 | 2370 | block_job_cb, bs, &local_err); |
d9b902db | 2371 | if (local_err != NULL) { |
4f6fd349 | 2372 | bdrv_unref(target_bs); |
d9b902db PB |
2373 | error_propagate(errp, local_err); |
2374 | return; | |
2375 | } | |
d9b902db PB |
2376 | } |
2377 | ||
2d47c6e9 SH |
2378 | static BlockJob *find_block_job(const char *device) |
2379 | { | |
2380 | BlockDriverState *bs; | |
2381 | ||
2382 | bs = bdrv_find(device); | |
2383 | if (!bs || !bs->job) { | |
2384 | return NULL; | |
2385 | } | |
2386 | return bs->job; | |
2387 | } | |
2388 | ||
882ec7ce | 2389 | void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) |
2d47c6e9 SH |
2390 | { |
2391 | BlockJob *job = find_block_job(device); | |
2392 | ||
2393 | if (!job) { | |
7ef15070 | 2394 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
2d47c6e9 SH |
2395 | return; |
2396 | } | |
2397 | ||
882ec7ce | 2398 | block_job_set_speed(job, speed, errp); |
2d47c6e9 | 2399 | } |
370521a1 | 2400 | |
6e37fb81 PB |
2401 | void qmp_block_job_cancel(const char *device, |
2402 | bool has_force, bool force, Error **errp) | |
370521a1 SH |
2403 | { |
2404 | BlockJob *job = find_block_job(device); | |
2405 | ||
6e37fb81 PB |
2406 | if (!has_force) { |
2407 | force = false; | |
2408 | } | |
2409 | ||
370521a1 | 2410 | if (!job) { |
7ef15070 | 2411 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); |
370521a1 SH |
2412 | return; |
2413 | } | |
6e37fb81 | 2414 | if (job->paused && !force) { |
f231b88d CR |
2415 | error_setg(errp, "The block job for device '%s' is currently paused", |
2416 | device); | |
8acc72a4 PB |
2417 | return; |
2418 | } | |
370521a1 SH |
2419 | |
2420 | trace_qmp_block_job_cancel(job); | |
2421 | block_job_cancel(job); | |
2422 | } | |
fb5458cd | 2423 | |
6e37fb81 PB |
2424 | void qmp_block_job_pause(const char *device, Error **errp) |
2425 | { | |
2426 | BlockJob *job = find_block_job(device); | |
2427 | ||
2428 | if (!job) { | |
2429 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2430 | return; | |
2431 | } | |
2432 | ||
2433 | trace_qmp_block_job_pause(job); | |
2434 | block_job_pause(job); | |
2435 | } | |
2436 | ||
2437 | void qmp_block_job_resume(const char *device, Error **errp) | |
2438 | { | |
2439 | BlockJob *job = find_block_job(device); | |
2440 | ||
2441 | if (!job) { | |
2442 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2443 | return; | |
2444 | } | |
2445 | ||
2446 | trace_qmp_block_job_resume(job); | |
2447 | block_job_resume(job); | |
2448 | } | |
2449 | ||
aeae883b PB |
2450 | void qmp_block_job_complete(const char *device, Error **errp) |
2451 | { | |
2452 | BlockJob *job = find_block_job(device); | |
2453 | ||
2454 | if (!job) { | |
2455 | error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); | |
2456 | return; | |
2457 | } | |
2458 | ||
2459 | trace_qmp_block_job_complete(job); | |
2460 | block_job_complete(job, errp); | |
2461 | } | |
2462 | ||
fa40e656 JC |
2463 | void qmp_change_backing_file(const char *device, |
2464 | const char *image_node_name, | |
2465 | const char *backing_file, | |
2466 | Error **errp) | |
2467 | { | |
2468 | BlockDriverState *bs = NULL; | |
2469 | BlockDriverState *image_bs = NULL; | |
2470 | Error *local_err = NULL; | |
2471 | bool ro; | |
2472 | int open_flags; | |
2473 | int ret; | |
2474 | ||
2475 | /* find the top layer BDS of the chain */ | |
2476 | bs = bdrv_find(device); | |
2477 | if (!bs) { | |
2478 | error_set(errp, QERR_DEVICE_NOT_FOUND, device); | |
2479 | return; | |
2480 | } | |
2481 | ||
2482 | image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); | |
2483 | if (local_err) { | |
2484 | error_propagate(errp, local_err); | |
2485 | return; | |
2486 | } | |
2487 | ||
2488 | if (!image_bs) { | |
2489 | error_setg(errp, "image file not found"); | |
2490 | return; | |
2491 | } | |
2492 | ||
2493 | if (bdrv_find_base(image_bs) == image_bs) { | |
2494 | error_setg(errp, "not allowing backing file change on an image " | |
2495 | "without a backing file"); | |
2496 | return; | |
2497 | } | |
2498 | ||
2499 | /* even though we are not necessarily operating on bs, we need it to | |
2500 | * determine if block ops are currently prohibited on the chain */ | |
2501 | if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { | |
2502 | return; | |
2503 | } | |
2504 | ||
2505 | /* final sanity check */ | |
2506 | if (!bdrv_chain_contains(bs, image_bs)) { | |
2507 | error_setg(errp, "'%s' and image file are not in the same chain", | |
2508 | device); | |
2509 | return; | |
2510 | } | |
2511 | ||
2512 | /* if not r/w, reopen to make r/w */ | |
2513 | open_flags = image_bs->open_flags; | |
2514 | ro = bdrv_is_read_only(image_bs); | |
2515 | ||
2516 | if (ro) { | |
2517 | bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); | |
2518 | if (local_err) { | |
2519 | error_propagate(errp, local_err); | |
2520 | return; | |
2521 | } | |
2522 | } | |
2523 | ||
2524 | ret = bdrv_change_backing_file(image_bs, backing_file, | |
2525 | image_bs->drv ? image_bs->drv->format_name : ""); | |
2526 | ||
2527 | if (ret < 0) { | |
2528 | error_setg_errno(errp, -ret, "Could not change backing file to '%s'", | |
2529 | backing_file); | |
2530 | /* don't exit here, so we can try to restore open flags if | |
2531 | * appropriate */ | |
2532 | } | |
2533 | ||
2534 | if (ro) { | |
2535 | bdrv_reopen(image_bs, open_flags, &local_err); | |
2536 | if (local_err) { | |
2537 | error_propagate(errp, local_err); /* will preserve prior errp */ | |
2538 | } | |
2539 | } | |
2540 | } | |
2541 | ||
d26c9a15 KW |
2542 | void qmp_blockdev_add(BlockdevOptions *options, Error **errp) |
2543 | { | |
2544 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
18e46a03 | 2545 | BlockBackend *blk; |
d26c9a15 KW |
2546 | QObject *obj; |
2547 | QDict *qdict; | |
d26c9a15 KW |
2548 | Error *local_err = NULL; |
2549 | ||
2550 | /* Require an ID in the top level */ | |
2551 | if (!options->has_id) { | |
2552 | error_setg(errp, "Block device needs an ID"); | |
2553 | goto fail; | |
2554 | } | |
2555 | ||
60e19e06 | 2556 | /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with |
d26c9a15 | 2557 | * cache.direct=false instead of silently switching to aio=threads, except |
60e19e06 | 2558 | * when called from drive_new(). |
d26c9a15 KW |
2559 | * |
2560 | * For now, simply forbidding the combination for all drivers will do. */ | |
2561 | if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { | |
c6e0bd9b KW |
2562 | bool direct = options->has_cache && |
2563 | options->cache->has_direct && | |
2564 | options->cache->direct; | |
2565 | if (!direct) { | |
d26c9a15 KW |
2566 | error_setg(errp, "aio=native requires cache.direct=true"); |
2567 | goto fail; | |
2568 | } | |
2569 | } | |
2570 | ||
2571 | visit_type_BlockdevOptions(qmp_output_get_visitor(ov), | |
2572 | &options, NULL, &local_err); | |
84d18f06 | 2573 | if (local_err) { |
d26c9a15 KW |
2574 | error_propagate(errp, local_err); |
2575 | goto fail; | |
2576 | } | |
2577 | ||
2578 | obj = qmp_output_get_qobject(ov); | |
2579 | qdict = qobject_to_qdict(obj); | |
2580 | ||
2581 | qdict_flatten(qdict); | |
2582 | ||
18e46a03 | 2583 | blk = blockdev_init(NULL, qdict, &local_err); |
84d18f06 | 2584 | if (local_err) { |
b681072d | 2585 | error_propagate(errp, local_err); |
d26c9a15 KW |
2586 | goto fail; |
2587 | } | |
2588 | ||
18e46a03 | 2589 | if (bdrv_key_required(blk_bs(blk))) { |
18e46a03 | 2590 | blk_unref(blk); |
8ae8e904 KW |
2591 | error_setg(errp, "blockdev-add doesn't support encrypted devices"); |
2592 | goto fail; | |
2593 | } | |
2594 | ||
d26c9a15 KW |
2595 | fail: |
2596 | qmp_output_visitor_cleanup(ov); | |
2597 | } | |
2598 | ||
fea68bb6 | 2599 | BlockJobInfoList *qmp_query_block_jobs(Error **errp) |
fb5458cd | 2600 | { |
fea68bb6 MA |
2601 | BlockJobInfoList *head = NULL, **p_next = &head; |
2602 | BlockDriverState *bs; | |
fb5458cd | 2603 | |
fea68bb6 MA |
2604 | for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { |
2605 | if (bs->job) { | |
2606 | BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); | |
2607 | elem->value = block_job_query(bs->job); | |
2608 | *p_next = elem; | |
2609 | p_next = &elem->next; | |
2610 | } | |
fb5458cd | 2611 | } |
fb5458cd | 2612 | |
fea68bb6 | 2613 | return head; |
fb5458cd | 2614 | } |
4d454574 | 2615 | |
0006383e | 2616 | QemuOptsList qemu_common_drive_opts = { |
4d454574 | 2617 | .name = "drive", |
0006383e | 2618 | .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), |
4d454574 PB |
2619 | .desc = { |
2620 | { | |
4d454574 PB |
2621 | .name = "snapshot", |
2622 | .type = QEMU_OPT_BOOL, | |
2623 | .help = "enable/disable snapshot mode", | |
a9384aff PB |
2624 | },{ |
2625 | .name = "discard", | |
2626 | .type = QEMU_OPT_STRING, | |
2627 | .help = "discard operation (ignore/off, unmap/on)", | |
4d454574 | 2628 | },{ |
29c4e2b5 KW |
2629 | .name = "cache.writeback", |
2630 | .type = QEMU_OPT_BOOL, | |
2631 | .help = "enables writeback mode for any caches", | |
2632 | },{ | |
2633 | .name = "cache.direct", | |
2634 | .type = QEMU_OPT_BOOL, | |
2635 | .help = "enables use of O_DIRECT (bypass the host page cache)", | |
2636 | },{ | |
2637 | .name = "cache.no-flush", | |
2638 | .type = QEMU_OPT_BOOL, | |
2639 | .help = "ignore any flush requests for the device", | |
4d454574 PB |
2640 | },{ |
2641 | .name = "aio", | |
2642 | .type = QEMU_OPT_STRING, | |
2643 | .help = "host AIO implementation (threads, native)", | |
2644 | },{ | |
2645 | .name = "format", | |
2646 | .type = QEMU_OPT_STRING, | |
2647 | .help = "disk format (raw, qcow2, ...)", | |
4d454574 PB |
2648 | },{ |
2649 | .name = "rerror", | |
2650 | .type = QEMU_OPT_STRING, | |
2651 | .help = "read error action", | |
2652 | },{ | |
2653 | .name = "werror", | |
2654 | .type = QEMU_OPT_STRING, | |
2655 | .help = "write error action", | |
4d454574 | 2656 | },{ |
0f227a94 | 2657 | .name = "read-only", |
4d454574 PB |
2658 | .type = QEMU_OPT_BOOL, |
2659 | .help = "open drive file as read-only", | |
2660 | },{ | |
57975222 | 2661 | .name = "throttling.iops-total", |
4d454574 PB |
2662 | .type = QEMU_OPT_NUMBER, |
2663 | .help = "limit total I/O operations per second", | |
2664 | },{ | |
57975222 | 2665 | .name = "throttling.iops-read", |
4d454574 PB |
2666 | .type = QEMU_OPT_NUMBER, |
2667 | .help = "limit read operations per second", | |
2668 | },{ | |
57975222 | 2669 | .name = "throttling.iops-write", |
4d454574 PB |
2670 | .type = QEMU_OPT_NUMBER, |
2671 | .help = "limit write operations per second", | |
2672 | },{ | |
57975222 | 2673 | .name = "throttling.bps-total", |
4d454574 PB |
2674 | .type = QEMU_OPT_NUMBER, |
2675 | .help = "limit total bytes per second", | |
2676 | },{ | |
57975222 | 2677 | .name = "throttling.bps-read", |
4d454574 PB |
2678 | .type = QEMU_OPT_NUMBER, |
2679 | .help = "limit read bytes per second", | |
2680 | },{ | |
57975222 | 2681 | .name = "throttling.bps-write", |
4d454574 PB |
2682 | .type = QEMU_OPT_NUMBER, |
2683 | .help = "limit write bytes per second", | |
3e9fab69 BC |
2684 | },{ |
2685 | .name = "throttling.iops-total-max", | |
2686 | .type = QEMU_OPT_NUMBER, | |
2687 | .help = "I/O operations burst", | |
2688 | },{ | |
2689 | .name = "throttling.iops-read-max", | |
2690 | .type = QEMU_OPT_NUMBER, | |
2691 | .help = "I/O operations read burst", | |
2692 | },{ | |
2693 | .name = "throttling.iops-write-max", | |
2694 | .type = QEMU_OPT_NUMBER, | |
2695 | .help = "I/O operations write burst", | |
2696 | },{ | |
2697 | .name = "throttling.bps-total-max", | |
2698 | .type = QEMU_OPT_NUMBER, | |
2699 | .help = "total bytes burst", | |
2700 | },{ | |
2701 | .name = "throttling.bps-read-max", | |
2702 | .type = QEMU_OPT_NUMBER, | |
2703 | .help = "total bytes read burst", | |
2704 | },{ | |
2705 | .name = "throttling.bps-write-max", | |
2706 | .type = QEMU_OPT_NUMBER, | |
2707 | .help = "total bytes write burst", | |
2024c1df BC |
2708 | },{ |
2709 | .name = "throttling.iops-size", | |
2710 | .type = QEMU_OPT_NUMBER, | |
2711 | .help = "when limiting by iops max size of an I/O in bytes", | |
4d454574 PB |
2712 | },{ |
2713 | .name = "copy-on-read", | |
2714 | .type = QEMU_OPT_BOOL, | |
2715 | .help = "copy read data from backing file into image file", | |
465bee1d PL |
2716 | },{ |
2717 | .name = "detect-zeroes", | |
2718 | .type = QEMU_OPT_STRING, | |
2719 | .help = "try to optimize zero writes (off, on, unmap)", | |
4d454574 PB |
2720 | }, |
2721 | { /* end of list */ } | |
2722 | }, | |
2723 | }; | |
0006383e KW |
2724 | |
2725 | QemuOptsList qemu_drive_opts = { | |
2726 | .name = "drive", | |
2727 | .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), | |
2728 | .desc = { | |
492fdc6f KW |
2729 | /* |
2730 | * no elements => accept any params | |
2731 | * validation will happen later | |
2732 | */ | |
0006383e KW |
2733 | { /* end of list */ } |
2734 | }, | |
2735 | }; |