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